Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions dune-project
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,7 @@
(ocaml (>= "5.0.0~"))
hdr_histogram
(cmdliner (>= 1.1.0))
tracing
(ocaml_intrinsics (>= v0.16.1))
(trace-fuchsia (>= 0.10))
(menhir :with-test)
(alcotest (and :with-test (>= 1.9.0)))))

Expand Down
2 changes: 1 addition & 1 deletion lib/olly_trace/olly_format_backend/dune
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,4 @@
(name olly_format_fuchsia)
(modules olly_format_fuchsia)
(optional)
(libraries olly_format_backend tracing))
(libraries olly_format_backend trace trace-fuchsia))
49 changes: 32 additions & 17 deletions lib/olly_trace/olly_format_backend/olly_format_fuchsia.ml
Original file line number Diff line number Diff line change
@@ -1,41 +1,56 @@
open Olly_format_backend
open Tracing

let name = "fuchsia"
let description = "Perfetto"

type trace = { doms : Trace.Thread.t array; file : Trace.t }
module Trace = Trace_fuchsia.Writer

type trace = {
doms : Trace.Thread_ref.t array;
buf : Trace_fuchsia.Buf_chain.t;
subscriber : Trace_fuchsia.Subscriber.t;
exporter : Trace_fuchsia.Exporter.t;
}

let flush trace =
Trace_fuchsia.Buf_chain.ready_all_non_empty trace.buf;
Trace_fuchsia.Buf_chain.pop_ready trace.buf ~f:trace.exporter.write_bufs;
trace.exporter.flush ()

let create ~filename =
let file = Trace.create_for_file ~base_time:None ~filename in
let buf_pool = Trace_fuchsia.Buf_pool.create () in
let buf = Trace_fuchsia.Buf_chain.create ~sharded:false ~buf_pool () in
let oc = Out_channel.open_bin filename in
let exporter = Trace_fuchsia.Exporter.of_out_channel ~close_channel:true oc in
let subscriber = Trace_fuchsia.Subscriber.create ~buf_pool ~pid:0 ~exporter () in
(* Adds the headers to output *)
Trace_fuchsia.Subscriber.Callbacks.on_init subscriber ~time_ns:0L;
let doms =
let max_doms = 128 in
Array.init max_doms (fun i ->
(* Use a different pid for each domain *)
Trace.allocate_thread file ~pid:i ~name:(Printf.sprintf "Ring_id %d" i))
Trace.Thread_ref.ref (i + 1))
in
{ doms; file }
{ doms; buf; subscriber; exporter }

let close trace = Trace.close trace.file
let ts_to_span ts = ts |> Int64.to_int |> Core.Time_ns.Span.of_int_ns
let close trace =
flush trace;
Trace_fuchsia.Subscriber.close trace.subscriber

let emit trace evt =
let open Event in
let thread = trace.doms.(evt.ring_id)
and category = "PERF"
and time = ts_to_span evt.ts
let t_ref = trace.doms.(evt.ring_id)
and time_ns = evt.ts
and name = evt.name in
match evt.kind with
| SpanBegin | SpanEnd ->
let write =
if evt.kind = SpanBegin then Trace.write_duration_begin
else Trace.write_duration_end
if evt.kind = SpanBegin then Trace.Event.Duration_begin.encode
else Trace.Event.Duration_end.encode
Comment on lines +48 to +49
Copy link

Choose a reason for hiding this comment

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

you could direct use the Trace_fuchsia.Subscriber' callbacks here, too, without going through Trace.Event`.

in
write trace.file ~args:[] ~thread ~category ~name ~time
write trace.buf ~args:[] ~t_ref ~name ~time_ns ()
| Counter value ->
Trace.write_counter trace.file ~thread ~category ~name ~time
~args:[ ("v", Int value) ]
Trace.Event.Counter.encode trace.buf ~t_ref ~name ~time_ns ~args:[ ("v", A_int value) ] ()
Copy link

Choose a reason for hiding this comment

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

same here, with subscriber

| Instant ->
Trace.write_duration_instant trace.file ~args:[] ~thread ~category ~name
~time
Trace.Event.Instant.encode trace.buf ~name ~args:[] ~t_ref ~time_ns ()
| _ -> ()
3 changes: 1 addition & 2 deletions runtime_events_tools.opam
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,7 @@ depends: [
"ocaml" {>= "5.0.0~"}
"hdr_histogram"
"cmdliner" {>= "1.1.0"}
"tracing"
"ocaml_intrinsics" {>= "v0.16.1"}
"trace-fuchsia" {>= "0.10"}
"menhir" {with-test}
"alcotest" {with-test & >= "1.9.0"}
"odoc" {with-doc}
Expand Down
8 changes: 6 additions & 2 deletions test/dune
Original file line number Diff line number Diff line change
Expand Up @@ -56,15 +56,19 @@
(run olly_bare trace --format=json test-bare-json.trace ./test_fib.exe))))

; Dummy process for testing process launching.

(executables
(names run_endlessly)
(modules run_endlessly)
(libraries unix))

(test
(name test_launch)
(modules test_launch)
(package runtime_events_tools)
(enabled_if
(>= %{ocaml_version} 5.1.0))
(libraries olly_common alcotest)
(deps run_endlessly.exe)
(action (run %{test} --show-errors)))
(deps run_endlessly.exe)
(action
(run %{test} --show-errors)))
Loading