-
Notifications
You must be signed in to change notification settings - Fork 71
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #635 from talex5/trace
Switch from CTF to OCaml 5.1 runtime events
- Loading branch information
Showing
29 changed files
with
478 additions
and
573 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
(executable | ||
(name main) | ||
(libraries eio.runtime_events eio_main)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
(* This example shows how to trace an Eio program. | ||
The [main] function creates a listening socket and has a client connect and send a message, | ||
which is handled by a server fiber. | ||
At the same time, another fiber is displaying trace events. | ||
For simplicity, this example runs the tracer in the same process as the program being traced, | ||
but typically they would be separate processes. *) | ||
|
||
open Eio.Std | ||
|
||
(* [handle pp] handles an event by writing the timestamp, ring ID and user data with [traceln]. | ||
[pp] is used to format the user data. *) | ||
let handle pp : _ Eio_runtime_events.handler = | ||
fun ring ts v -> | ||
(* Note: don't use traceln here, as it will just generate more log events! *) | ||
Fmt.epr "%9Ld:ring %d: %a@." (Runtime_events.Timestamp.to_int64 ts) ring pp v | ||
|
||
let callbacks = | ||
Runtime_events.Callbacks.create () | ||
(* Uncomment to trace GC events too: *) | ||
(* | ||
~runtime_begin:(handle (fun f phase -> Fmt.pf f "begin %s" (Runtime_events.runtime_phase_name phase))) | ||
~runtime_end:(handle (fun f phase -> Fmt.pf f "end %s" (Runtime_events.runtime_phase_name phase))) | ||
*) | ||
~lost_events:(fun ring n -> traceln "ring %d lost %d events" ring n) | ||
|> Eio_runtime_events.add_callbacks | ||
~fiber:(handle (Fmt.fmt "running fiber %d")) | ||
~create:(handle (fun f (id, ty) -> Fmt.pf f "create %s %d" (Eio_runtime_events.ty_to_string ty) id)) | ||
~resolve:(handle (Fmt.fmt "resolve %d")) | ||
~log:(handle (Fmt.fmt "log %S")) | ||
~name:(handle (fun f (id, name) -> Fmt.pf f "%d is named %S" id name)) | ||
(* (see lib_eio/runtime_events/eio_runtime_events.mli for more event types) *) | ||
|
||
(* Read and display trace events from [cursor] until [finished]. *) | ||
let trace ~finished (clock, delay) cursor = | ||
traceln "tracer: starting"; | ||
let rec aux () = | ||
let _ : int = Runtime_events.read_poll cursor callbacks None in | ||
if !finished then ( | ||
traceln "tracer: stopping" | ||
) else ( | ||
Eio.Time.Mono.sleep clock delay; | ||
aux () | ||
) | ||
in | ||
aux () | ||
|
||
(* The program to be traced. *) | ||
let main net = | ||
Switch.run @@ fun sw -> | ||
let addr = `Tcp (Eio.Net.Ipaddr.V4.loopback, 8123) in | ||
let s = Eio.Net.listen ~sw ~backlog:1 ~reuse_addr:true net addr in | ||
Fiber.both | ||
(fun () -> | ||
traceln "server: starting"; | ||
let c, _addr = Eio.Net.accept ~sw s in | ||
traceln "server: got connection from client"; | ||
let msg = Eio.Flow.read_all c in | ||
traceln "server: read %S from socket" msg | ||
) | ||
(fun () -> | ||
traceln "client: connecting socket..."; | ||
let c = Eio.Net.connect ~sw net addr in | ||
Eio.Flow.copy_string "Hello" c; | ||
Eio.Flow.close c | ||
) | ||
|
||
(* Enable tracing then run the [main] and [trace] fibers. *) | ||
let () = | ||
Runtime_events.start (); | ||
let cursor = Runtime_events.create_cursor None in (* Create a in-process cursor *) | ||
Eio_main.run @@ fun env -> | ||
let finished = ref false in | ||
Fiber.both | ||
(fun () -> trace ~finished (env#mono_clock, 0.01) cursor) | ||
(fun () -> main env#net; finished := true) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
(library | ||
(name eio__core) | ||
(public_name eio.core) | ||
(libraries cstruct hmap lwt-dllist fmt optint domain-local-await)) | ||
(libraries cstruct hmap lwt-dllist fmt optint domain-local-await eio.runtime_events)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.