mirror of
https://github.com/ocaml-tracing/ocaml-trace.git
synced 2026-03-13 14:06:24 -04:00
feat trace-fuchsia: require thread-local-storage 0.2
This commit is contained in:
parent
1e20dab45c
commit
6383fcfff9
3 changed files with 31 additions and 20 deletions
|
|
@ -55,7 +55,7 @@
|
||||||
(ocaml (>= 4.08))
|
(ocaml (>= 4.08))
|
||||||
(trace (= :version))
|
(trace (= :version))
|
||||||
(mtime (>= 2.0))
|
(mtime (>= 2.0))
|
||||||
thread-local-storage
|
(thread-local-storage (>= 0.2))
|
||||||
base-bigarray
|
base-bigarray
|
||||||
base-unix
|
base-unix
|
||||||
dune)
|
dune)
|
||||||
|
|
|
||||||
|
|
@ -140,9 +140,11 @@ type state = {
|
||||||
at the end. This is a tid-sharded array of maps. *)
|
at the end. This is a tid-sharded array of maps. *)
|
||||||
}
|
}
|
||||||
|
|
||||||
let key_thread_local_st : per_thread_state TLS.key =
|
let key_thread_local_st : per_thread_state TLS.t = TLS.create ()
|
||||||
TLS.new_key (fun () ->
|
|
||||||
|
let[@inline never] mk_thread_local_st () =
|
||||||
let tid = Thread.id @@ Thread.self () in
|
let tid = Thread.id @@ Thread.self () in
|
||||||
|
let st =
|
||||||
{
|
{
|
||||||
tid;
|
tid;
|
||||||
state_id = A.get state_id_;
|
state_id = A.get state_id_;
|
||||||
|
|
@ -150,16 +152,25 @@ let key_thread_local_st : per_thread_state TLS.key =
|
||||||
local_span_id_gen = A.make 0;
|
local_span_id_gen = A.make 0;
|
||||||
out = None;
|
out = None;
|
||||||
spans = Span_info_stack.create ();
|
spans = Span_info_stack.create ();
|
||||||
})
|
}
|
||||||
|
in
|
||||||
|
TLS.set key_thread_local_st st;
|
||||||
|
st
|
||||||
|
|
||||||
|
let[@inline] get_thread_local_st () =
|
||||||
|
match TLS.get_opt key_thread_local_st with
|
||||||
|
| Some k -> k
|
||||||
|
| None -> mk_thread_local_st ()
|
||||||
|
|
||||||
let out_of_st (st : state) : Output.t =
|
let out_of_st (st : state) : Output.t =
|
||||||
FWrite.Output.create () ~buf_pool:st.buf_pool ~send_buf:(fun buf ->
|
FWrite.Output.create () ~buf_pool:st.buf_pool ~send_buf:(fun buf ->
|
||||||
try B_queue.push st.events (E_write_buf buf) with B_queue.Closed -> ())
|
try B_queue.push st.events (E_write_buf buf) with B_queue.Closed -> ())
|
||||||
|
|
||||||
module C (St : sig
|
module C
|
||||||
|
(St : sig
|
||||||
val st : state
|
val st : state
|
||||||
end)
|
end)
|
||||||
() =
|
() =
|
||||||
struct
|
struct
|
||||||
open St
|
open St
|
||||||
|
|
||||||
|
|
@ -208,7 +219,7 @@ struct
|
||||||
|
|
||||||
(** Obtain the output for the current thread *)
|
(** Obtain the output for the current thread *)
|
||||||
let[@inline] get_thread_output () : Output.t * per_thread_state =
|
let[@inline] get_thread_output () : Output.t * per_thread_state =
|
||||||
let tls = TLS.get key_thread_local_st in
|
let tls = get_thread_local_st () in
|
||||||
if tls.state_id != state_id || tls.out == None then
|
if tls.state_id != state_id || tls.out == None then
|
||||||
update_or_init_local_state tls;
|
update_or_init_local_state tls;
|
||||||
let out =
|
let out =
|
||||||
|
|
@ -240,7 +251,7 @@ struct
|
||||||
)
|
)
|
||||||
|
|
||||||
let enter_span ~__FUNCTION__:_ ~__FILE__:_ ~__LINE__:_ ~data name : span =
|
let enter_span ~__FUNCTION__:_ ~__FILE__:_ ~__LINE__:_ ~data name : span =
|
||||||
let tls = TLS.get key_thread_local_st in
|
let tls = get_thread_local_st () in
|
||||||
let span = Int64.of_int (A.fetch_and_add tls.local_span_id_gen 1) in
|
let span = Int64.of_int (A.fetch_and_add tls.local_span_id_gen 1) in
|
||||||
let time_ns = Time.now_ns () in
|
let time_ns = Time.now_ns () in
|
||||||
Span_info_stack.push tls.spans span ~name ~data ~start_time_ns:time_ns;
|
Span_info_stack.push tls.spans span ~name ~data ~start_time_ns:time_ns;
|
||||||
|
|
@ -282,7 +293,7 @@ struct
|
||||||
reraise exn
|
reraise exn
|
||||||
|
|
||||||
let add_data_to_span span data =
|
let add_data_to_span span data =
|
||||||
let tls = TLS.get key_thread_local_st in
|
let tls = get_thread_local_st () in
|
||||||
match Span_info_stack.find_ tls.spans span with
|
match Span_info_stack.find_ tls.spans span with
|
||||||
| None -> !on_tracing_error (spf "unknown span %Ld" span)
|
| None -> !on_tracing_error (spf "unknown span %Ld" span)
|
||||||
| Some idx -> Span_info_stack.add_data tls.spans idx data
|
| Some idx -> Span_info_stack.add_data tls.spans idx data
|
||||||
|
|
|
||||||
|
|
@ -13,7 +13,7 @@ depends: [
|
||||||
"ocaml" {>= "4.08"}
|
"ocaml" {>= "4.08"}
|
||||||
"trace" {= version}
|
"trace" {= version}
|
||||||
"mtime" {>= "2.0"}
|
"mtime" {>= "2.0"}
|
||||||
"thread-local-storage"
|
"thread-local-storage" {>= "0.2"}
|
||||||
"base-bigarray"
|
"base-bigarray"
|
||||||
"base-unix"
|
"base-unix"
|
||||||
"dune" {>= "2.9"}
|
"dune" {>= "2.9"}
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue