Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

basic tracing #5757

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open

basic tracing #5757

wants to merge 2 commits into from

Conversation

c-cube
Copy link
Contributor

@c-cube c-cube commented Dec 8, 2023

This is WIP.

I'm adding tracing to find why opam is so slow on my machine. It gives excellent insights on where time is spent (e.g after opam upd, all 30k files are read one by one, to update the cache).

I've been instrumenting things as I explore but it needs to be more principled in general I think.

The overhead when tracing is not active (ie TRACE_FILE is not set) should be fairly low.

@kit-ty-kate
Copy link
Member

Thanks a lot for this! I have a couple of questions:

  • I’m not familiar with this tracing format. Is it used by a visualisation tool? If so which one?
  • If not, what are the advantages compared to --debug-level=3?
  • Is the dependency on mtime necessary or could we use something that already exists for --debug-level=3 for example?

@c-cube
Copy link
Contributor Author

c-cube commented Dec 8, 2023

  • the tracing format is the classic JSON file for chrome://tracing, which can also be read by https://ui.perfetto.dev/
    (see also past example of this 1). So you can easily visualize it.

    It can look like this:
    2023-12-08-153651_2042x736_scrot

  • I think we could use Unix.gettimeofday() instead, I think, we don't need that high precision in general. I got a bit over enthusiastic.

@c-cube c-cube marked this pull request as ready for review December 13, 2023 16:25
Copy link
Member

@kit-ty-kate kit-ty-kate left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM on first glance

@kit-ty-kate
Copy link
Member

We’re trying to get 2.2.0 out the door asap and i’d prefer to merge this after the release to avoid unnecessary time spent in extra maintenance and CI work (don’t worry the CI failure is unrelated to you changes).
Is that ok for you?

@c-cube
Copy link
Contributor Author

c-cube commented Jan 2, 2024

Of course, no worries! This is really a tool to debug/improve opam itself, not something end users urgently need, I think.

this cuts the time spent on parsing `pacman -Si` significantly down
Copy link
Member

@kit-ty-kate kit-ty-kate left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've squashed the PR and cleaned a couple trailing whitespaces. It looks good overall but there are a couple of things to split off into their own PRs I think

@@ -395,6 +395,7 @@ let simulate_local_pinnings ?quiet ?(for_view=false) st to_pin =

let simulate_autopin st ?quiet ?(for_view=false) ?locked ?recurse ?subpath
atom_or_local_list =
OpamTrace.with_span "AuxCommands.simulate_autopin" @@ fun () ->
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it's better to give the full name of the module even if the Opam prefix is repetitive

Suggested change
OpamTrace.with_span "AuxCommands.simulate_autopin" @@ fun () ->
OpamTrace.with_span "OpamAuxCommands.simulate_autopin" @@ fun () ->

(* ## setup ## *)

let setup ~trace_file () : unit =
match trace_file, Sys.getenv_opt "TRACE_FILE" with
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this should be OPAMTRACEFILE instead and be added through OpamCoreConfig and OpamArg like the rest of the environment variables

Comment on lines -695 to +715
(* old compat version (Re 1.2.0)
{[Re_str.split (Re_str.regexp (Printf.sprintf "[%c]+" c)) s]} *)
Re.(split (compile (rep1 (char c)))) s
let acc = ref [] in
let in_run = ref false in
let slice_start = ref 0 in

for i=0 to String.length s-1 do
if String.get s i = c then (
if not !in_run then (
if i > !slice_start then
acc := String.sub s !slice_start (i - !slice_start) :: !acc;
in_run := true;
)
) else (
if !in_run then (
in_run := false;
slice_start := i;
)
)
done;
if not !in_run && !slice_start < String.length s then
acc := String.sub s !slice_start (String.length s - !slice_start) :: !acc;
List.rev !acc
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this change should be in its own PR

let print_message =
if Sys.win32 then
fun ch fmt ->
flush (if ch = `stdout then stderr else stdout);
fun ~force_flush ch fmt ->
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is this for performance improvement or does the tracing itself need it? If it's the former, i think this should be in its own PR

@@ -0,0 +1,9 @@
(** Tracing *)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
(** Tracing *)
(**************************************************************************)
(* *)
(* Copyright 2023 Simon Cruanes *)
(* *)
(* All rights reserved. This file is distributed under the terms of the *)
(* GNU Lesser General Public License version 2.1, with the special *)
(* exception on linking described in the file LICENSE. *)
(* *)
(**************************************************************************)
(** Tracing *)

@@ -0,0 +1,110 @@
module Json = OpamJson
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
module Json = OpamJson
(**************************************************************************)
(* *)
(* Copyright 2023 Simon Cruanes *)
(* *)
(* All rights reserved. This file is distributed under the terms of the *)
(* GNU Lesser General Public License version 2.1, with the special *)
(* exception on linking described in the file LICENSE. *)
(* *)
(**************************************************************************)
module Json = OpamJson

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants