diff --git a/lib_eio/core/cancel.ml b/lib_eio/core/cancel.ml index 0bc6965fc..3d477d2a4 100644 --- a/lib_eio/core/cancel.ml +++ b/lib_eio/core/cancel.ml @@ -195,7 +195,7 @@ module Fiber_context = struct let make ~cc ~vars = let tid = Trace.mint_id () in - Trace.note_created tid Trace.Task; + Trace.create tid Fiber; let t = { tid; cancel_context = cc; cancel_node = None; cancel_fn = ignore; vars } in t.cancel_node <- Some (Lwt_dllist.add_r t cc.fibers); t diff --git a/lib_eio/core/fiber.ml b/lib_eio/core/fiber.ml index 2e615dad9..0785e8a12 100644 --- a/lib_eio/core/fiber.ml +++ b/lib_eio/core/fiber.ml @@ -19,10 +19,10 @@ let fork ~sw f = Switch.with_op sw @@ fun () -> match f () with | () -> - Trace.note_resolved (Cancel.Fiber_context.tid new_fiber) ~ex:None + Trace.resolve (Cancel.Fiber_context.tid new_fiber) ~ex:None | exception ex -> Switch.fail sw ex; (* The [with_op] ensures this will succeed *) - Trace.note_resolved (Cancel.Fiber_context.tid new_fiber) ~ex:(Some ex) + Trace.resolve (Cancel.Fiber_context.tid new_fiber) ~ex:(Some ex) ) (* else the fiber should report the error to [sw], but [sw] is failed anyway *) let fork_daemon ~sw f = @@ -35,13 +35,13 @@ let fork_daemon ~sw f = match f () with | `Stop_daemon -> (* The daemon asked to stop. *) - Trace.note_resolved (Cancel.Fiber_context.tid new_fiber) ~ex:None + Trace.resolve (Cancel.Fiber_context.tid new_fiber) ~ex:None | exception Cancel.Cancelled Exit when not (Cancel.is_on sw.cancel) -> (* The daemon was cancelled because all non-daemon fibers are finished. *) - Trace.note_resolved (Cancel.Fiber_context.tid new_fiber) ~ex:None + Trace.resolve (Cancel.Fiber_context.tid new_fiber) ~ex:None | exception ex -> Switch.fail sw ex; (* The [with_daemon] ensures this will succeed *) - Trace.note_resolved (Cancel.Fiber_context.tid new_fiber) ~ex:(Some ex) + Trace.resolve (Cancel.Fiber_context.tid new_fiber) ~ex:(Some ex) ) (* else the fiber should report the error to [sw], but [sw] is failed anyway *) let fork_promise ~sw f = diff --git a/lib_eio/core/promise.ml b/lib_eio/core/promise.ml index 84b3d8e0a..ca15c74b0 100644 --- a/lib_eio/core/promise.ml +++ b/lib_eio/core/promise.ml @@ -26,19 +26,19 @@ let create_with_id id = let create ?label () = let id = Trace.mint_id () in - Trace.note_created ?label id Trace.Promise; + Trace.create ?label id Promise; create_with_id id let create_resolved x = let id = Trace.mint_id () in - Trace.note_created id Trace.Promise; + Trace.create id Promise; to_public_promise { id; state = Atomic.make (Resolved x) } let await t = let t = of_public_promise t in match Atomic.get t.state with | Resolved x -> - Trace.note_read t.id; + Trace.read t.id; x | Unresolved b -> Suspend.enter (fun ctx enqueue -> @@ -53,7 +53,7 @@ let await t = | Unresolved _ -> (* We observed the promise to be still unresolved after registering a waiter. Therefore any resolution must happen after we were registered and we will be notified. *) - Trace.note_try_read t.id; + Trace.try_read t.id; Cancel.Fiber_context.set_cancel_fn ctx (fun ex -> if Broadcast.cancel request then enqueue (Error ex) (* else already resumed *) @@ -61,7 +61,7 @@ let await t = ); match Atomic.get t.state with | Resolved x -> - Trace.note_read t.id; + Trace.read t.id; x | Unresolved _ -> assert false @@ -76,7 +76,7 @@ let resolve t v = | Resolved _ -> invalid_arg "Can't resolve already-resolved promise" | Unresolved b as prev -> if Atomic.compare_and_set t.state prev (Resolved v) then ( - Trace.note_resolved t.id ~ex:None; + Trace.resolve t.id ~ex:None; Broadcast.resume_all b ) else ( (* Otherwise, the promise was already resolved. Retry (to get the error). *) diff --git a/lib_eio/core/single_waiter.ml b/lib_eio/core/single_waiter.ml index d7378a8e5..aa27d7b37 100644 --- a/lib_eio/core/single_waiter.ml +++ b/lib_eio/core/single_waiter.ml @@ -19,6 +19,6 @@ let await t id = t.wake <- (fun x -> Cancel.Fiber_context.clear_cancel_fn ctx; t.wake <- ignore; - Trace.note_read ~reader:id ctx.tid; + Trace.read ~reader:id ctx.tid; enqueue x ) diff --git a/lib_eio/core/switch.ml b/lib_eio/core/switch.ml index ce52145f3..891f7db7f 100644 --- a/lib_eio/core/switch.ml +++ b/lib_eio/core/switch.ml @@ -51,7 +51,7 @@ let combine_exn ex = function let fail ?(bt=Printexc.get_raw_backtrace ()) t ex = check_our_domain t; if t.exs = None then - Trace.note_resolved t.id ~ex:(Some ex); + Trace.resolve t.id ~ex:(Some ex); t.exs <- Some (combine_exn (ex, bt) t.exs); try Cancel.cancel t.cancel ex @@ -91,7 +91,7 @@ let or_raise = function let rec await_idle t = (* Wait for fibers to finish: *) while t.fibers > 0 do - Trace.note_try_read t.id; + Trace.try_read t.id; Single_waiter.await t.waiter t.id done; (* Call on_release handlers: *) @@ -119,7 +119,7 @@ let maybe_raise_exs t = let create cancel = let id = Trace.mint_id () in - Trace.note_created id Trace.Switch; + Trace.create id Switch; { id; fibers = 1; (* The main function counts as a fiber *) @@ -135,7 +135,7 @@ let run_internal t fn = | v -> dec_fibers t; await_idle t; - Trace.note_read t.id; + Trace.read t.id; maybe_raise_exs t; (* Check for failure while finishing *) (* Success. *) v @@ -146,7 +146,7 @@ let run_internal t fn = dec_fibers t; fail ~bt t ex; await_idle t; - Trace.note_read t.id; + Trace.read t.id; maybe_raise_exs t; assert false diff --git a/lib_eio/core/trace.ml b/lib_eio/core/trace.ml index e6f0d6b28..d4987beb4 100644 --- a/lib_eio/core/trace.ml +++ b/lib_eio/core/trace.ml @@ -47,27 +47,8 @@ let mint_id () = Domain.DLS.set next_id_key next_id_local_succ; next_id_local -type hiatus_reason = - | Wait_for_work - | Suspend - | Hibernate - -type event = - | Wait - | Task - | Bind - | Try - | Choose - | Pick - | Join - | Map - | Condition - | On_success - | On_failure - | On_termination - | On_any - | Ignore_result - | Async +type ty = + | Fiber | Promise | Semaphore | Switch @@ -80,21 +61,7 @@ let current_thread = ref (-1) let int_of_thread_type t = match t with - | Wait -> 0 - | Task -> 1 - | Bind -> 2 - | Try -> 3 - | Choose -> 4 - | Pick -> 5 - | Join -> 6 - | Map -> 7 - | Condition -> 8 - | On_success -> 9 - | On_failure -> 10 - | On_termination -> 11 - | On_any -> 12 - | Ignore_result -> 13 - | Async -> 14 + | Fiber -> 1 | Promise -> 15 | Semaphore -> 16 | Switch -> 17 @@ -207,12 +174,12 @@ module Control = struct let op_fails = 3 (* let op_becomes = 4 *) let op_label = 5 - let op_increase = 6 + (* let op_increase = 6 *) let op_switch = 7 (* let op_gc = 8 *) (* let op_old_signal = 9 *) let op_try_read = 10 - let op_counter_value = 11 + (* let op_counter_value = 11 *) let op_read_later = 12 let op_signal = 13 @@ -329,20 +296,6 @@ module Control = struct |> write_string log.log msg |> end_event - let note_increase log counter amount = - add_event log op_increase (17 + String.length counter) - |> write_tid log.log !current_thread - |> write64 log.log (Int64.of_int amount) - |> write_string log.log counter - |> end_event - - let note_counter_value log counter value = - add_event log op_counter_value (17 + String.length counter) - |> write_tid log.log !current_thread - |> write64 log.log (Int64.of_int value) - |> write_string log.log counter - |> end_event - let note_switch log new_current = if new_current <> !current_thread then ( current_thread := new_current; @@ -397,42 +350,34 @@ let label name = | None -> () | Some log -> Control.note_label log !current_thread name -let note_fork () = - let child = mint_id () in - begin match !Control.event_log with - | None -> () - | Some log -> Control.note_created log child Task - end; - child - -let note_created ?label id ty = +let create ?label id ty = match !Control.event_log with | None -> () | Some log -> Control.note_created log id ty; Option.iter (Control.note_label log id) label -let note_switch new_current = +let fiber new_current = match !Control.event_log with | None -> () | Some log -> Control.note_switch log new_current -let note_hiatus _reason = +let hiatus () = match !Control.event_log with | None -> () | Some log -> Control.note_suspend log () -let note_resume new_current = +let resume new_current = match !Control.event_log with | None -> () | Some log -> Control.note_switch log new_current -let note_try_read input = +let try_read input = match !Control.event_log with | None -> () | Some log -> Control.note_try_read log !current_thread input -let note_read ?reader input = +let read ?reader input = match !Control.event_log with | None -> () | Some log -> @@ -443,12 +388,12 @@ let note_read ?reader input = in Control.note_read log ~reader input -let note_resolved id ~ex = +let resolve id ~ex = match !Control.event_log with | None -> () | Some log -> Control.note_resolved log id ~ex -let note_signal ?src dst = +let signal ?src dst = match !Control.event_log with | None -> () | Some log -> @@ -458,18 +403,3 @@ let note_signal ?src dst = | Some x -> x in Control.note_signal ~src log dst - -let note_increase counter amount = - match !Control.event_log with - | None -> () - | Some log -> Control.note_increase log counter amount - -let note_counter_value counter value = - match !Control.event_log with - | None -> () - | Some log -> Control.note_counter_value log counter value - -let should_resolve thread = - match !Control.event_log with - | None -> () - | Some log -> Control.note_label log thread "__should_resolve" (* Hack! *) diff --git a/lib_eio/core/trace.mli b/lib_eio/core/trace.mli index f611b48c6..6e50b0cf1 100644 --- a/lib_eio/core/trace.mli +++ b/lib_eio/core/trace.mli @@ -9,79 +9,47 @@ type id = private int val label : string -> unit (** [label msg] attaches text [msg] to the current thread. *) -val note_increase : string -> int -> unit -(** [note_increase counter delta] records that [counter] increased by [delta]. - If [delta] is negative, this records a decrease. *) - -val note_counter_value : string -> int -> unit -(** [note_counter_value counter value] records that [counter] is now [value]. *) - -val should_resolve : id -> unit -(** [should_resolve id] records that [id] is expected to resolve, and should be highlighted if it doesn't. *) - (** {2 Recording system events} These are normally only called by the scheduler. *) -type hiatus_reason = - | Wait_for_work - | Suspend - | Hibernate - -type event = - | Wait - | Task - | Bind - | Try - | Choose - | Pick - | Join - | Map - | Condition - | On_success - | On_failure - | On_termination - | On_any - | Ignore_result - | Async +type ty = + | Fiber | Promise | Semaphore | Switch | Stream | Mutex -(** Types of threads or other recorded objects. *) +(** Types of recorded objects. *) val mint_id : unit -> id (** [mint_id ()] is a fresh unique [id]. *) -val note_created : ?label:string -> id -> event -> unit -(** [note_created t id ty] records the creation of [id]. *) +val create : ?label:string -> id -> ty -> unit +(** [create t id ty] records the creation of [id]. *) -val note_read : ?reader:id -> id -> unit -(** [note_read src] records that promise [src]'s value was read. +val read : ?reader:id -> id -> unit +(** [read src] records that promise [src]'s value was read. @param reader The thread doing the read (default is the current thread). *) -val note_try_read : id -> unit -(** [note_try_read src] records that the current thread wants to read from [src] (which is not currently ready). *) +val try_read : id -> unit +(** [try_read src] records that the current thread wants to read from [src] (which is not currently ready). *) -val note_switch : id -> unit -(** [note_switch id] records that [id] is now the current thread. *) +val fiber : id -> unit +(** [fiber id] records that fiber [id] is now running. *) -val note_hiatus : hiatus_reason -> unit -(** [note_hiatus r] records that the system will sleep for reason [r]. *) +val hiatus : unit -> unit +(** [hiatus ()] records that the system will sleep for reason [r]. *) -val note_resume : id -> unit -(** [note_resume id] records that the system has resumed (used after {!note_hiatus}), +val resume : id -> unit +(** [resume id] records that the system has resumed (used after {!hiatus}), and is now running [id]. *) -val note_fork : unit -> id -(** [note_fork ()] records that a new thread has been forked and returns a fresh ID for it. *) - -val note_resolved : id -> ex:exn option -> unit -(** [note_resolved id ~ex] records that [id] is now resolved. +val resolve : id -> ex:exn option -> unit +(** [resolve id ~ex] records that [id] is now resolved. If [ex = None] then [id] was successful, otherwise it failed with exception [ex]. *) -val note_signal : ?src:id -> id -> unit -(** [note_signal ~src dst] records that [dst] was signalled. +val signal : ?src:id -> id -> unit +(** [signal ~src dst] records that [dst] was signalled. @param src The thread sending the signal (default is the current thread). *) (** {2 Controlling tracing} *) diff --git a/lib_eio/eio_mutex.ml b/lib_eio/eio_mutex.ml index 124cc1e64..18f7a5de5 100644 --- a/lib_eio/eio_mutex.ml +++ b/lib_eio/eio_mutex.ml @@ -20,7 +20,7 @@ type t = { (* {R} t = create () {mutex t R} *) let create () = let id = Trace.mint_id () in - Trace.note_created id Trace.Mutex; + Trace.create id Mutex; { id; mutex = Mutex.create (); @@ -33,7 +33,7 @@ let create () = let unlock t = Mutex.lock t.mutex; (* We now have ownership of [t.state] and [t.waiters]. *) - Trace.note_signal t.id; + Trace.signal t.id; match t.state with | Unlocked -> Mutex.unlock t.mutex; @@ -55,7 +55,7 @@ let lock t = Mutex.lock t.mutex; match t.state with | Locked -> - Trace.note_try_read t.id; + Trace.try_read t.id; begin match Waiters.await ~mutex:(Some t.mutex) t.waiters t.id with | `Error ex -> raise ex (* Poisoned; stop waiting *) | `Take -> @@ -64,7 +64,7 @@ let lock t = () end | Unlocked -> - Trace.note_read t.id; + Trace.read t.id; t.state <- Locked; (* We transfer R from the state to our caller. *) (* {locked t * R} *) Mutex.unlock t.mutex @@ -77,11 +77,11 @@ let try_lock t = Mutex.lock t.mutex; match t.state with | Locked -> - Trace.note_try_read t.id; + Trace.try_read t.id; Mutex.unlock t.mutex; false | Unlocked -> - Trace.note_read t.id; + Trace.read t.id; t.state <- Locked; (* We transfer R from the state to our caller. *) Mutex.unlock t.mutex; (* {locked t * R} *) diff --git a/lib_eio/semaphore.ml b/lib_eio/semaphore.ml index e0c83da94..8846ec375 100644 --- a/lib_eio/semaphore.ml +++ b/lib_eio/semaphore.ml @@ -5,14 +5,14 @@ type t = { let make n = let id = Trace.mint_id () in - Trace.note_created id Trace.Semaphore; + Trace.create id Semaphore; { id; state = Sem_state.create n; } let release t = - Trace.note_signal t.id; + Trace.signal t.id; Sem_state.release t.state let acquire t = @@ -24,7 +24,7 @@ let acquire t = match Sem_state.suspend t.state (fun () -> enqueue (Ok ())) with | None -> () (* Already resumed *) | Some request -> - Trace.note_try_read t.id; + Trace.try_read t.id; match Fiber_context.get_error ctx with | Some ex -> if Sem_state.cancel request then enqueue (Error ex); @@ -36,7 +36,7 @@ let acquire t = ) ) ); - Trace.note_read t.id + Trace.read t.id let get_value t = max 0 (Atomic.get t.state.state) diff --git a/lib_eio/stream.ml b/lib_eio/stream.ml index 4148b838b..eba7514b4 100644 --- a/lib_eio/stream.ml +++ b/lib_eio/stream.ml @@ -30,7 +30,7 @@ module Locking = struct let create capacity = assert (capacity > 0); let id = Trace.mint_id () in - Trace.note_created id Trace.Stream; + Trace.create id Stream; { mutex = Mutex.create (); id; diff --git a/lib_eio/utils/suspended.ml b/lib_eio/utils/suspended.ml index 72b4facb6..e12a3f131 100644 --- a/lib_eio/utils/suspended.ml +++ b/lib_eio/utils/suspended.ml @@ -11,9 +11,9 @@ type 'a t = { let tid t = Eio.Private.Fiber_context.tid t.fiber let continue t v = - Trace.note_switch (tid t); + Trace.fiber (tid t); continue t.k v let discontinue t ex = - Trace.note_switch (tid t); + Trace.fiber (tid t); discontinue t.k ex diff --git a/lib_eio/waiters.ml b/lib_eio/waiters.ml index 4443e4c2f..9bd4e0208 100644 --- a/lib_eio/waiters.ml +++ b/lib_eio/waiters.ml @@ -47,7 +47,7 @@ let await_internal ~mutex (t:'a t) id ctx enqueue = let resolved_waiter = ref Hook.null in let finished = Atomic.make false in let enqueue x = - Trace.note_read ~reader:id (Fiber_context.tid ctx); + Trace.read ~reader:id (Fiber_context.tid ctx); enqueue x in let cancel ex = diff --git a/lib_eio_linux/sched.ml b/lib_eio_linux/sched.ml index cf824a497..27b90ba26 100644 --- a/lib_eio_linux/sched.ml +++ b/lib_eio_linux/sched.ml @@ -244,9 +244,9 @@ let rec schedule ({run_q; sleep_q; mem_q; uring; _} as st) : [`Exit_scheduler] = (* At this point we're not going to check [run_q] again before sleeping. If [need_wakeup] is still [true], this is fine because we don't promise to do that. If [need_wakeup = false], a wake-up event will arrive and wake us up soon. *) - Trace.(note_hiatus Wait_for_work); + Trace.hiatus (); let result = Uring.wait ?timeout uring in - Trace.note_resume system_thread; + Trace.resume system_thread; Atomic.set st.need_wakeup false; Lf_queue.push run_q IO; (* Re-inject IO job in the run queue *) match result with @@ -388,7 +388,7 @@ let monitor_event_fd t = let run ~extra_effects st main arg = let rec fork ~new_fiber:fiber fn = let open Effect.Deep in - Trace.note_switch (Fiber_context.tid fiber); + Trace.fiber (Fiber_context.tid fiber); match_with fn () { retc = (fun () -> Fiber_context.destroy fiber; schedule st); exnc = (fun ex -> diff --git a/lib_eio_posix/sched.ml b/lib_eio_posix/sched.ml index 842f08bb4..0fadc8308 100644 --- a/lib_eio_posix/sched.ml +++ b/lib_eio_posix/sched.ml @@ -209,12 +209,12 @@ let rec next t : [`Exit_scheduler] = (* At this point we're not going to check [run_q] again before sleeping. If [need_wakeup] is still [true], this is fine because we don't promise to do that. If [need_wakeup = false], a wake-up event will arrive and wake us up soon. *) - Trace.(note_hiatus Wait_for_work); + Trace.hiatus (); let nready = try Poll.ppoll_or_poll t.poll (t.poll_maxi + 1) timeout with Unix.Unix_error (Unix.EINTR, _, "") -> 0 in - Trace.note_resume system_thread; + Trace.fiber system_thread; Atomic.set t.need_wakeup false; Lf_queue.push t.run_q IO; (* Re-inject IO job in the run queue *) Poll.iter_ready t.poll nready (ready t); @@ -325,7 +325,7 @@ let enter fn = Effect.perform (Enter fn) let run ~extra_effects t main x = let rec fork ~new_fiber:fiber fn = let open Effect.Deep in - Trace.note_switch (Fiber_context.tid fiber); + Trace.fiber (Fiber_context.tid fiber); match_with fn () { retc = (fun () -> Fiber_context.destroy fiber; next t); exnc = (fun ex ->