Skip to content

Commit

Permalink
tracing: add optional name for pools
Browse files Browse the repository at this point in the history
  • Loading branch information
c-cube committed Feb 7, 2024
1 parent 6ed870a commit 27b213e
Show file tree
Hide file tree
Showing 7 changed files with 23 additions and 2 deletions.
9 changes: 8 additions & 1 deletion src/fifo_pool.ml
Original file line number Diff line number Diff line change
Expand Up @@ -78,11 +78,12 @@ type ('a, 'b) create_args =
?on_exn:(exn -> Printexc.raw_backtrace -> unit) ->
?around_task:(t -> 'b) * (t -> 'b -> unit) ->
?num_threads:int ->
?name:string ->
'a

let create ?(on_init_thread = default_thread_init_exit_)
?(on_exit_thread = default_thread_init_exit_) ?(on_exn = fun _ _ -> ())
?around_task ?num_threads () : t =
?around_task ?num_threads ?name () : t =
(* wrapper *)
let around_task =
match around_task with
Expand Down Expand Up @@ -128,6 +129,12 @@ let create ?(on_init_thread = default_thread_init_exit_)
let t_id = Thread.id thread in
on_init_thread ~dom_id:dom_idx ~t_id ();
(* set thread name *)
Option.iter
(fun name ->
Tracing_.set_thread_name (Printf.sprintf "%s.worker.%d" name i))
name;
let run () = worker_thread_ pool runner ~on_exn ~around_task in
(* now run the main loop *)
Expand Down
2 changes: 2 additions & 0 deletions src/fifo_pool.mli
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ type ('a, 'b) create_args =
?on_exn:(exn -> Printexc.raw_backtrace -> unit) ->
?around_task:(t -> 'b) * (t -> 'b -> unit) ->
?num_threads:int ->
?name:string ->
'a
(** Arguments used in {!create}. See {!create} for explanations. *)

Expand All @@ -35,6 +36,7 @@ val create : (unit -> t, _) create_args
@param on_exit_thread called at the end of each worker thread in the pool.
@param around_task a pair of [before, after] functions
ran around each task. See {!Pool.create_args}.
@param name name for the pool, used in tracing (since NEXT_RELEASE)
*)

val with_ : (unit -> (t -> 'a) -> 'a, _) create_args
Expand Down
1 change: 1 addition & 0 deletions src/tracing_.dummy.ml
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@ let enabled () = false
let dummy_span = 0L
let enter_span _name = dummy_span
let exit_span = ignore
let set_thread_name = ignore
1 change: 1 addition & 0 deletions src/tracing_.mli
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@ val dummy_span : int64
val enter_span : string -> int64
val exit_span : int64 -> unit
val enabled : unit -> bool
val set_thread_name : string -> unit
1 change: 1 addition & 0 deletions src/tracing_.real.ml
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ module Trace = Trace_core
let enabled = Trace.enabled
let dummy_span = Int64.min_int
let dummy_file_ = "<unknown file>"
let set_thread_name = Trace.set_thread_name

let[@inline] enter_span name : int64 =
if name = "" then
Expand Down
9 changes: 8 additions & 1 deletion src/ws_pool.ml
Original file line number Diff line number Diff line change
Expand Up @@ -247,14 +247,15 @@ type ('a, 'b) create_args =
?on_exn:(exn -> Printexc.raw_backtrace -> unit) ->
?around_task:(t -> 'b) * (t -> 'b -> unit) ->
?num_threads:int ->
?name:string ->
'a
(** Arguments used in {!create}. See {!create} for explanations. *)

let dummy_task_ = { f = ignore; name = "DUMMY_TASK" }

let create ?(on_init_thread = default_thread_init_exit_)
?(on_exit_thread = default_thread_init_exit_) ?(on_exn = fun _ _ -> ())
?around_task ?num_threads () : t =
?around_task ?num_threads ?name () : t =
let pool_id_ = Id.create () in
(* wrapper *)
let around_task =
Expand Down Expand Up @@ -320,6 +321,12 @@ let create ?(on_init_thread = default_thread_init_exit_)
let t_id = Thread.id thread in
on_init_thread ~dom_id:dom_idx ~t_id ();
(* set thread name *)
Option.iter
(fun name ->
Tracing_.set_thread_name (Printf.sprintf "%s.worker.%d" name i))
name;
let run () = worker_thread_ pool ~runner w in
(* now run the main loop *)
Expand Down
2 changes: 2 additions & 0 deletions src/ws_pool.mli
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ type ('a, 'b) create_args =
?on_exn:(exn -> Printexc.raw_backtrace -> unit) ->
?around_task:(t -> 'b) * (t -> 'b -> unit) ->
?num_threads:int ->
?name:string ->
'a
(** Arguments used in {!create}. See {!create} for explanations. *)

Expand All @@ -44,6 +45,7 @@ val create : (unit -> t, _) create_args
before a task is processed,
on the worker thread about to run it, and returns [x]; and [after pool x] is called by
the same thread after the task is over. (since 0.2)
@param name a name for this thread pool, used if tracing is enabled (since NEXT_RELEASE)
*)

val with_ : (unit -> (t -> 'a) -> 'a, _) create_args
Expand Down

0 comments on commit 27b213e

Please sign in to comment.