@@ -15,8 +15,8 @@ let next_tef_pid = ref 0
1515
1616module Make (Name : Name ): S =
1717struct
18- let enabled = ref false
19- let options = ref dummy_options
18+ let enabled_dls = Domain.DLS. new_key ( fun () -> false )
19+ let options_dls = Domain.DLS. new_key ( fun () -> dummy_options)
2020 let tef_pid =
2121 let tef_pid = ! next_tef_pid in
2222 incr next_tef_pid;
@@ -55,15 +55,16 @@ struct
5555 let current_allocated = Gc. allocated_bytes
5656
5757 let create_frame tree =
58+ let options = Domain.DLS. get options_dls in
5859 {
5960 tree;
60- start_cputime = if ! options.cputime then current_cputime () else 0.0 ;
61- start_walltime = if ! options.walltime then current_walltime () else 0.0 ;
62- start_allocated = if ! options.allocated then current_allocated () else 0.0 ;
61+ start_cputime = if options.cputime then current_cputime () else 0.0 ;
62+ start_walltime = if options.walltime then current_walltime () else 0.0 ;
63+ start_allocated = if options.allocated then current_allocated () else 0.0 ;
6364 }
6465
6566 (* * Stack of currently active timing frames. *)
66- let current: frame Stack. t = Stack. create ()
67+ let current: frame Stack. t Domain.DLS. key = Domain.DLS. new_key ( fun () -> Stack. create () )
6768
6869 let reset () =
6970 (* Reset tree. *)
@@ -73,30 +74,32 @@ struct
7374 root.count < - 0 ;
7475 root.children < - [] ;
7576 (* Reset frames. *)
76- if not (Stack. is_empty current) then ( (* If ever started. In case reset before first start. *)
77- Stack. clear current;
78- Stack. push (create_frame root) current
77+ if not (Stack. is_empty ( Domain.DLS. get current) ) then ( (* If ever started. In case reset before first start. *)
78+ Stack. clear ( Domain.DLS. get current) ;
79+ Stack. push (create_frame root) ( Domain.DLS. get current)
7980 )
8081
8182 let start options' =
82- options := options';
83- if ! options.tef then (
83+ Domain.DLS. set options_dls options';
84+ let options = Domain.DLS. get options_dls in
85+ if options.tef then (
8486 (* Override TEF process and thread name for track rendering. *)
8587 Catapult.Tracing. emit ~pid: tef_pid " thread_name" ~cat: [" __firefox_profiler_hack__" ] ~args: [(" name" , `String Name. name)] Catapult.Event_type. M ;
8688 (* First event must have category, otherwise Firefox Profiler refuses to open. *)
8789 Catapult.Tracing. emit ~pid: tef_pid " process_name" ~args: [(" name" , `String Name. name)] Catapult.Event_type. M
8890 );
89- enabled := true ;
90- if Stack. is_empty current then (* If never started. *)
91- Stack. push (create_frame root) current
91+ Domain.DLS. set enabled_dls true ;
92+ if Stack. is_empty ( Domain.DLS. get current) then (* If never started. *)
93+ Stack. push (create_frame root) ( Domain.DLS. get current)
9294
9395 let stop () =
94- enabled := false
96+ Domain.DLS. set enabled_dls false
9597
9698 let enter ?args name =
99+ let options = Domain.DLS. get options_dls in
97100 (* Find the right tree. *)
98101 let tree: tree =
99- let {tree; _} = Stack. top current in
102+ let {tree; _} = Stack. top ( Domain.DLS. get current) in
100103 let rec loop = function
101104 | child :: _ when child.name = name -> child
102105 | _ :: children' -> loop children'
@@ -108,32 +111,34 @@ struct
108111 in
109112 loop tree.children
110113 in
111- Stack. push (create_frame tree) current;
112- if ! options.tef then
114+ Stack. push (create_frame tree) ( Domain.DLS. get current) ;
115+ if options.tef then
113116 Catapult.Tracing. begin ' ~pid: tef_pid ?args name
114117
115118 (* * Add current frame measurements to tree node accumulators. *)
116119 let add_frame_to_tree frame tree =
117- if ! options.cputime then (
120+ let options = Domain.DLS. get options_dls in
121+ if options.cputime then (
118122 let diff = current_cputime () -. frame.start_cputime in
119123 tree.cputime < - tree.cputime +. diff
120124 );
121- if ! options.walltime then (
125+ if options.walltime then (
122126 let diff = current_walltime () -. frame.start_walltime in
123127 tree.walltime < - tree.walltime +. diff
124128 );
125- if ! options.allocated then (
129+ if options.allocated then (
126130 let diff = current_allocated () -. frame.start_allocated in
127131 tree.allocated < - tree.allocated +. diff
128132 );
129- if ! options.count then
133+ if options.count then
130134 tree.count < - tree.count + 1
131135
132136 let exit name =
133- let {tree; _} as frame = Stack. pop current in
137+ let options = Domain.DLS. get options_dls in
138+ let {tree; _} as frame = Stack. pop (Domain.DLS. get current) in
134139 assert (tree.name = name);
135140 add_frame_to_tree frame tree;
136- if ! options.tef then
141+ if options.tef then
137142 Catapult.Tracing. exit' ~pid: tef_pid name
138143
139144 let wrap ?args name f x =
@@ -149,15 +154,15 @@ struct
149154 (* Shortcutting measurement functions to avoid any work when disabled. *)
150155
151156 let enter ?args name =
152- if ! enabled then
157+ if Domain.DLS. get enabled_dls then
153158 enter ?args name
154159
155160 let exit name =
156- if ! enabled then
161+ if Domain.DLS. get enabled_dls then
157162 exit name
158163
159164 let wrap ?args name f x =
160- if ! enabled then
165+ if Domain.DLS. get enabled_dls then
161166 wrap ?args name f x
162167 else
163168 f x
@@ -177,32 +182,34 @@ struct
177182 tree (* no need to recurse, current doesn't go into subtree *)
178183 in
179184 (* Folding the stack also reverses it such that the root frame is at the beginning. *)
180- let current_rev = Stack. fold (fun acc frame -> frame :: acc) [] current in
185+ let current_rev = Stack. fold (fun acc frame -> frame :: acc) [] ( Domain.DLS. get current) in
181186 tree_with_current current_rev root
182187
183188 let rec pp_tree ppf node =
189+ let options = Domain.DLS. get options_dls in
184190 Format. fprintf ppf " @[<v 2>%-25s " node.name;
185- if ! options.cputime then
191+ if options.cputime then
186192 Format. fprintf ppf " %9.3fs" node.cputime;
187- if ! options.walltime then
193+ if options.walltime then
188194 Format. fprintf ppf " %10.3fs" node.walltime;
189- if ! options.allocated then
195+ if options.allocated then
190196 Format. fprintf ppf " %10.2fMB" (node.allocated /. 1_000_000.0 ); (* TODO: or should it be 1024-based (MiB)? *)
191- if ! options.count then
197+ if options.count then
192198 Format. fprintf ppf " %7d×" node.count;
193199 (* cut also before first child *)
194200 List. iter (Format. fprintf ppf " @,%a" pp_tree) (List. rev node.children);
195201 Format. fprintf ppf " @]"
196202
197203 let pp_header ppf =
204+ let options = Domain.DLS. get options_dls in
198205 Format. fprintf ppf " %-25s " " " ;
199- if ! options.cputime then
206+ if options.cputime then
200207 Format. fprintf ppf " cputime" ;
201- if ! options.walltime then
208+ if options.walltime then
202209 Format. fprintf ppf " walltime" ;
203- if ! options.allocated then
210+ if options.allocated then
204211 Format. fprintf ppf " allocated" ;
205- if ! options.count then
212+ if options.count then
206213 Format. fprintf ppf " count" ;
207214 Format. fprintf ppf " @\n "
208215
0 commit comments