@@ -15,8 +15,8 @@ let next_tef_pid = ref 0
15
15
16
16
module Make (Name : Name ): S =
17
17
struct
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)
20
20
let tef_pid =
21
21
let tef_pid = ! next_tef_pid in
22
22
incr next_tef_pid;
@@ -55,15 +55,16 @@ struct
55
55
let current_allocated = Gc. allocated_bytes
56
56
57
57
let create_frame tree =
58
+ let options = Domain.DLS. get options_dls in
58
59
{
59
60
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 ;
63
64
}
64
65
65
66
(* * 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 () )
67
68
68
69
let reset () =
69
70
(* Reset tree. *)
@@ -73,30 +74,32 @@ struct
73
74
root.count < - 0 ;
74
75
root.children < - [] ;
75
76
(* 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)
79
80
)
80
81
81
82
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 (
84
86
(* Override TEF process and thread name for track rendering. *)
85
87
Catapult.Tracing. emit ~pid: tef_pid " thread_name" ~cat: [" __firefox_profiler_hack__" ] ~args: [(" name" , `String Name. name)] Catapult.Event_type. M ;
86
88
(* First event must have category, otherwise Firefox Profiler refuses to open. *)
87
89
Catapult.Tracing. emit ~pid: tef_pid " process_name" ~args: [(" name" , `String Name. name)] Catapult.Event_type. M
88
90
);
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)
92
94
93
95
let stop () =
94
- enabled := false
96
+ Domain.DLS. set enabled_dls false
95
97
96
98
let enter ?args name =
99
+ let options = Domain.DLS. get options_dls in
97
100
(* Find the right tree. *)
98
101
let tree: tree =
99
- let {tree; _} = Stack. top current in
102
+ let {tree; _} = Stack. top ( Domain.DLS. get current) in
100
103
let rec loop = function
101
104
| child :: _ when child.name = name -> child
102
105
| _ :: children' -> loop children'
@@ -108,32 +111,34 @@ struct
108
111
in
109
112
loop tree.children
110
113
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
113
116
Catapult.Tracing. begin ' ~pid: tef_pid ?args name
114
117
115
118
(* * Add current frame measurements to tree node accumulators. *)
116
119
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 (
118
122
let diff = current_cputime () -. frame.start_cputime in
119
123
tree.cputime < - tree.cputime +. diff
120
124
);
121
- if ! options.walltime then (
125
+ if options.walltime then (
122
126
let diff = current_walltime () -. frame.start_walltime in
123
127
tree.walltime < - tree.walltime +. diff
124
128
);
125
- if ! options.allocated then (
129
+ if options.allocated then (
126
130
let diff = current_allocated () -. frame.start_allocated in
127
131
tree.allocated < - tree.allocated +. diff
128
132
);
129
- if ! options.count then
133
+ if options.count then
130
134
tree.count < - tree.count + 1
131
135
132
136
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
134
139
assert (tree.name = name);
135
140
add_frame_to_tree frame tree;
136
- if ! options.tef then
141
+ if options.tef then
137
142
Catapult.Tracing. exit' ~pid: tef_pid name
138
143
139
144
let wrap ?args name f x =
@@ -149,15 +154,15 @@ struct
149
154
(* Shortcutting measurement functions to avoid any work when disabled. *)
150
155
151
156
let enter ?args name =
152
- if ! enabled then
157
+ if Domain.DLS. get enabled_dls then
153
158
enter ?args name
154
159
155
160
let exit name =
156
- if ! enabled then
161
+ if Domain.DLS. get enabled_dls then
157
162
exit name
158
163
159
164
let wrap ?args name f x =
160
- if ! enabled then
165
+ if Domain.DLS. get enabled_dls then
161
166
wrap ?args name f x
162
167
else
163
168
f x
@@ -177,32 +182,34 @@ struct
177
182
tree (* no need to recurse, current doesn't go into subtree *)
178
183
in
179
184
(* 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
181
186
tree_with_current current_rev root
182
187
183
188
let rec pp_tree ppf node =
189
+ let options = Domain.DLS. get options_dls in
184
190
Format. fprintf ppf " @[<v 2>%-25s " node.name;
185
- if ! options.cputime then
191
+ if options.cputime then
186
192
Format. fprintf ppf " %9.3fs" node.cputime;
187
- if ! options.walltime then
193
+ if options.walltime then
188
194
Format. fprintf ppf " %10.3fs" node.walltime;
189
- if ! options.allocated then
195
+ if options.allocated then
190
196
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
192
198
Format. fprintf ppf " %7d×" node.count;
193
199
(* cut also before first child *)
194
200
List. iter (Format. fprintf ppf " @,%a" pp_tree) (List. rev node.children);
195
201
Format. fprintf ppf " @]"
196
202
197
203
let pp_header ppf =
204
+ let options = Domain.DLS. get options_dls in
198
205
Format. fprintf ppf " %-25s " " " ;
199
- if ! options.cputime then
206
+ if options.cputime then
200
207
Format. fprintf ppf " cputime" ;
201
- if ! options.walltime then
208
+ if options.walltime then
202
209
Format. fprintf ppf " walltime" ;
203
- if ! options.allocated then
210
+ if options.allocated then
204
211
Format. fprintf ppf " allocated" ;
205
- if ! options.count then
212
+ if options.count then
206
213
Format. fprintf ppf " count" ;
207
214
Format. fprintf ppf " @\n "
208
215
0 commit comments