add thread/process name setting

This commit is contained in:
Simon Cruanes 2023-06-09 16:07:49 -04:00
parent da3388dbf9
commit b6db37fab0
No known key found for this signature in database
GPG key ID: EBFFF6F283F3A2B4
4 changed files with 52 additions and 0 deletions

View file

@ -28,6 +28,12 @@ module type S = sig
string -> string ->
unit unit
val name_thread : string -> unit
(** Give a name to the current thread *)
val name_process : string -> unit
(** Give a name to the current process *)
val shutdown : unit -> unit val shutdown : unit -> unit
(** Shutdown collector, possibly waiting for it to finish sending data. *) (** Shutdown collector, possibly waiting for it to finish sending data. *)
end end

View file

@ -60,6 +60,11 @@ type event =
id: span; id: span;
time_us: float; time_us: float;
} }
| E_name_process of { name: string }
| E_name_thread of {
tid: int;
name: string;
}
module Span_tbl = Hashtbl.Make (struct module Span_tbl = Hashtbl.Make (struct
include Int64 include Int64
@ -172,6 +177,23 @@ module Writer = struct
(emit_args_o_ pp_user_data_) (emit_args_o_ pp_user_data_)
args; args;
() ()
let emit_name_thread ~tid ~name (self : t) : unit =
emit_sep_ self;
Printf.fprintf self.oc
{json|{"pid": %d,"tid": %d,"name":"thread_name","ph":"M"%a}|json} self.pid
tid
(emit_args_o_ pp_user_data_)
[ "name", `String name ];
()
let emit_name_process ~name (self : t) : unit =
emit_sep_ self;
Printf.fprintf self.oc
{json|{"pid": %d,"name":"process_name","ph":"M"%a}|json} self.pid
(emit_args_o_ pp_user_data_)
[ "name", `String name ];
()
end end
let bg_thread ~out (events : event B_queue.t) : unit = let bg_thread ~out (events : event B_queue.t) : unit =
@ -205,6 +227,8 @@ let bg_thread ~out (events : event B_queue.t) : unit =
Span_tbl.remove spans id; Span_tbl.remove spans id;
Writer.emit_duration_event ~tid ~name ~start:start_us ~end_:stop_us Writer.emit_duration_event ~tid ~name ~start:start_us ~end_:stop_us
~args:data writer) ~args:data writer)
| E_name_process { name } -> Writer.emit_name_process ~name writer
| E_name_thread { tid; name } -> Writer.emit_name_thread ~tid ~name writer
in in
try try
@ -275,6 +299,12 @@ let collector ~out () : collector =
B_queue.push events B_queue.push events
(E_message (E_message
{ (* __FUNCTION__; __FILE__; __LINE__; *) tid; time_us; msg; data }) { (* __FUNCTION__; __FILE__; __LINE__; *) tid; time_us; msg; data })
let name_process name : unit = B_queue.push events (E_name_process { name })
let name_thread name : unit =
let tid = get_tid_ () in
B_queue.push events (E_name_thread { tid; name })
end in end in
(module M) (module M)

View file

@ -76,6 +76,16 @@ let messagef ?__FUNCTION__ ~__FILE__ ~__LINE__ ?data k =
C.message ?__FUNCTION__ ~__FILE__ ~__LINE__ ~data str) C.message ?__FUNCTION__ ~__FILE__ ~__LINE__ ~data str)
fmt) fmt)
let set_thread_name name : unit =
match A.get collector with
| None -> ()
| Some (module C) -> C.name_thread name
let set_process_name name : unit =
match A.get collector with
| None -> ()
| Some (module C) -> C.name_process name
let setup_collector c : unit = let setup_collector c : unit =
while while
let cur = A.get collector in let cur = A.get collector in

View file

@ -48,6 +48,12 @@ val messagef :
((('a, Format.formatter, unit, unit) format4 -> 'a) -> unit) -> ((('a, Format.formatter, unit, unit) format4 -> 'a) -> unit) ->
unit unit
val set_thread_name : string -> unit
(** Give a name to the current thread. *)
val set_process_name : string -> unit
(** Give a name to the current process. *)
(** {2 Collector} *) (** {2 Collector} *)
type collector = (module Collector.S) type collector = (module Collector.S)