Skip to content

Commit

Permalink
Update tracing section of README
Browse files Browse the repository at this point in the history
  • Loading branch information
talex5 committed Jan 26, 2024
1 parent d62d073 commit 51e5ece
Show file tree
Hide file tree
Showing 7 changed files with 398 additions and 277 deletions.
68 changes: 14 additions & 54 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -220,70 +220,30 @@ Calling an operation that performs an effect (such as `yield`) can switch to a d
When OCaml's tracing is turned on, Eio writes events about many actions,
such as creating fibers or resolving promises.

[examples/trace](./examples/trace/) shows how to consume the events manually:
You can use [eio-trace][] to capture a trace and display it in a window.
For example, this is a trace of the counting example above:

<!-- $MDX skip -->
```sh
$ dune exec -- ./examples/trace/main.exe
+tracer: starting
30926487700447:ring 0: create fiber 0
30926487702032:ring 0: running fiber 0
30926487705057:ring 0: create switch 1
30926487707264:ring 0: create fiber 2
30926487707512:ring 0: running fiber 2
30926487720213:ring 0: log "tracer: starting"
+server: starting
+client: connecting socket...
+server: got connection from client
+server: read "Hello" from socket
30926487769298:ring 0: running fiber 0
30926487769877:ring 0: create fiber 3
30926487770083:ring 0: running fiber 3
30926487771198:ring 0: create switch 4
30926487807888:ring 0: create switch 5
30926487808329:ring 0: create fiber 6
30926487808555:ring 0: running fiber 6
30926487812219:ring 0: log "server: starting"
30926487818883:ring 0: running fiber 3
30926487819091:ring 0: create fiber 7
30926487819155:ring 0: running fiber 7
30926487822428:ring 0: log "client: connecting socket..."
30926487901604:ring 0: running fiber 3
30926487904947:ring 0: running fiber 0
30926487907318:ring 0: running fiber 6
30926487917202:ring 0: log "server: got connection from client"
30926487929993:ring 0: running fiber 6
30926487941403:ring 0: running fiber 7
30926487948000:ring 0: running fiber 7
30926487971898:ring 0: resolve 7
30926487974810:ring 0: running fiber 6
30926487975215:ring 0: running fiber 6
30926487977869:ring 0: running fiber 6
30926487984514:ring 0: log "server: read \"Hello\" from socket"
30926487990785:ring 0: resolve 6
30926487991752:ring 0: running fiber 3
30926488022310:ring 0: resolve 3
30926497839725:ring 0: running fiber 2
+tracer: stopping
```
dune build ./examples
eio-trace run -- ./_build/default/examples/both/main.exe
```

<p align='center'>
<img src="./doc/traces/both-posix.svg"/>
</p>

The upper horizontal bar is the initial fiber, and the brackets show `Fiber.both` creating a second fiber.
The green segments show when each fiber is running.
Note that the output from `traceln` appears in the trace as well as on the console.
In the eio-trace window, scrolling with the mouse or touchpad with zoom in or out of the diagram.

There are various third-party tools that can consume this data
There are various third-party tools that can also consume this data
(but may currently require patches to support the new system):

- [Meio][] (Monitoring for Eio) provides an interactive console-based UI for exploring running fibers.
- [Olly][] can save Perfetto traces and report statistics.
- [mirage-trace-viewer][] renders traces visually.

For example, this is how mirage-trace-viewer renders the counting example above:

<p align='center'>
<img src="./doc/trace.svg"/>
</p>

This shows the two counting threads as two horizonal lines.
The white regions indicate when each thread was running.
[examples/trace](./examples/trace/) shows how to consume the events manually.

## Cancellation

Expand Down
223 changes: 0 additions & 223 deletions doc/trace.svg

This file was deleted.

2 changes: 2 additions & 0 deletions doc/traces/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
all:
eio-trace render -f both-posix.fxt both-posix.svg
Binary file added doc/traces/both-posix.fxt
Binary file not shown.
372 changes: 372 additions & 0 deletions doc/traces/both-posix.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
3 changes: 3 additions & 0 deletions examples/both/dune
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
(executable
(name main)
(libraries eio_main))
7 changes: 7 additions & 0 deletions examples/both/main.ml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
open Eio.Std

let () =
Eio_main.run @@ fun _env ->
Fiber.both
(fun () -> for x = 1 to 3 do traceln "x = %d" x; Fiber.yield () done)
(fun () -> for y = 1 to 3 do traceln "y = %d" y; Fiber.yield () done)

0 comments on commit 51e5ece

Please sign in to comment.