]>
Commit | Line | Data |
---|---|---|
ee4512ed JH |
1 | #include "cache.h" |
2 | #include "config.h" | |
3 | #include "json-writer.h" | |
4 | #include "quote.h" | |
5 | #include "run-command.h" | |
6 | #include "sigchain.h" | |
7 | #include "thread-utils.h" | |
8 | #include "version.h" | |
9 | #include "trace2/tr2_cfg.h" | |
10 | #include "trace2/tr2_cmd_name.h" | |
11 | #include "trace2/tr2_dst.h" | |
12 | #include "trace2/tr2_sid.h" | |
bce9db6d | 13 | #include "trace2/tr2_sysenv.h" |
ee4512ed JH |
14 | #include "trace2/tr2_tgt.h" |
15 | #include "trace2/tr2_tls.h" | |
16 | ||
17 | static int trace2_enabled; | |
18 | ||
19 | static int tr2_next_child_id; /* modify under lock */ | |
20 | static int tr2_next_exec_id; /* modify under lock */ | |
21 | static int tr2_next_repo_id = 1; /* modify under lock. zero is reserved */ | |
22 | ||
23 | /* | |
24 | * A table of the builtin TRACE2 targets. Each of these may be independently | |
25 | * enabled or disabled. Each TRACE2 API method will try to write an event to | |
26 | * *each* of the enabled targets. | |
27 | */ | |
28 | /* clang-format off */ | |
29 | static struct tr2_tgt *tr2_tgt_builtins[] = | |
30 | { | |
31 | &tr2_tgt_normal, | |
32 | &tr2_tgt_perf, | |
33 | &tr2_tgt_event, | |
34 | NULL | |
35 | }; | |
36 | /* clang-format on */ | |
37 | ||
38 | /* clang-format off */ | |
39 | #define for_each_builtin(j, tgt_j) \ | |
40 | for (j = 0, tgt_j = tr2_tgt_builtins[j]; \ | |
41 | tgt_j; \ | |
42 | j++, tgt_j = tr2_tgt_builtins[j]) | |
43 | /* clang-format on */ | |
44 | ||
45 | /* clang-format off */ | |
46 | #define for_each_wanted_builtin(j, tgt_j) \ | |
47 | for_each_builtin(j, tgt_j) \ | |
48 | if (tr2_dst_trace_want(tgt_j->pdst)) | |
49 | /* clang-format on */ | |
50 | ||
51 | /* | |
52 | * Force (rather than lazily) initialize any of the requested | |
53 | * builtin TRACE2 targets at startup (and before we've seen an | |
54 | * actual TRACE2 event call) so we can see if we need to setup | |
55 | * the TR2 and TLS machinery. | |
56 | * | |
57 | * Return the number of builtin targets enabled. | |
58 | */ | |
59 | static int tr2_tgt_want_builtins(void) | |
60 | { | |
61 | struct tr2_tgt *tgt_j; | |
62 | int j; | |
63 | int sum = 0; | |
64 | ||
65 | for_each_builtin (j, tgt_j) | |
66 | if (tgt_j->pfn_init()) | |
67 | sum++; | |
68 | ||
69 | return sum; | |
70 | } | |
71 | ||
72 | /* | |
73 | * Properly terminate each builtin target. Give each target | |
74 | * a chance to write a summary event and/or flush if necessary | |
75 | * and then close the fd. | |
76 | */ | |
77 | static void tr2_tgt_disable_builtins(void) | |
78 | { | |
79 | struct tr2_tgt *tgt_j; | |
80 | int j; | |
81 | ||
82 | for_each_builtin (j, tgt_j) | |
83 | tgt_j->pfn_term(); | |
84 | } | |
85 | ||
86 | static int tr2main_exit_code; | |
87 | ||
88 | /* | |
89 | * Our atexit routine should run after everything has finished. | |
90 | * | |
91 | * Note that events generated here might not actually appear if | |
92 | * we are writing to fd 1 or 2 and our atexit routine runs after | |
93 | * the pager's atexit routine (since it closes them to shutdown | |
94 | * the pipes). | |
95 | */ | |
96 | static void tr2main_atexit_handler(void) | |
97 | { | |
98 | struct tr2_tgt *tgt_j; | |
99 | int j; | |
100 | uint64_t us_now; | |
101 | uint64_t us_elapsed_absolute; | |
102 | ||
103 | us_now = getnanotime() / 1000; | |
104 | us_elapsed_absolute = tr2tls_absolute_elapsed(us_now); | |
105 | ||
106 | /* | |
107 | * Clear any unbalanced regions so that our atexit message | |
108 | * does not appear nested. This improves the appearance of | |
109 | * the trace output if someone calls die(), for example. | |
110 | */ | |
111 | tr2tls_pop_unwind_self(); | |
112 | ||
113 | for_each_wanted_builtin (j, tgt_j) | |
114 | if (tgt_j->pfn_atexit) | |
115 | tgt_j->pfn_atexit(us_elapsed_absolute, | |
116 | tr2main_exit_code); | |
117 | ||
118 | tr2_tgt_disable_builtins(); | |
119 | ||
120 | tr2tls_release(); | |
121 | tr2_sid_release(); | |
122 | tr2_cmd_name_release(); | |
123 | tr2_cfg_free_patterns(); | |
3d3adaad | 124 | tr2_cfg_free_env_vars(); |
bce9db6d | 125 | tr2_sysenv_release(); |
ee4512ed JH |
126 | |
127 | trace2_enabled = 0; | |
128 | } | |
129 | ||
130 | static void tr2main_signal_handler(int signo) | |
131 | { | |
132 | struct tr2_tgt *tgt_j; | |
133 | int j; | |
134 | uint64_t us_now; | |
135 | uint64_t us_elapsed_absolute; | |
136 | ||
137 | us_now = getnanotime() / 1000; | |
138 | us_elapsed_absolute = tr2tls_absolute_elapsed(us_now); | |
139 | ||
140 | for_each_wanted_builtin (j, tgt_j) | |
141 | if (tgt_j->pfn_signal) | |
142 | tgt_j->pfn_signal(us_elapsed_absolute, signo); | |
143 | ||
144 | sigchain_pop(signo); | |
145 | raise(signo); | |
146 | } | |
147 | ||
a0897249 JH |
148 | void trace2_initialize_clock(void) |
149 | { | |
150 | tr2tls_start_process_clock(); | |
151 | } | |
152 | ||
ee4512ed JH |
153 | void trace2_initialize_fl(const char *file, int line) |
154 | { | |
155 | struct tr2_tgt *tgt_j; | |
156 | int j; | |
157 | ||
158 | if (trace2_enabled) | |
159 | return; | |
160 | ||
bce9db6d JH |
161 | tr2_sysenv_load(); |
162 | ||
ee4512ed JH |
163 | if (!tr2_tgt_want_builtins()) |
164 | return; | |
165 | trace2_enabled = 1; | |
166 | ||
167 | tr2_sid_get(); | |
168 | ||
169 | atexit(tr2main_atexit_handler); | |
170 | sigchain_push(SIGPIPE, tr2main_signal_handler); | |
171 | tr2tls_init(); | |
172 | ||
173 | /* | |
174 | * Emit 'version' message on each active builtin target. | |
175 | */ | |
176 | for_each_wanted_builtin (j, tgt_j) | |
177 | if (tgt_j->pfn_version_fl) | |
178 | tgt_j->pfn_version_fl(file, line); | |
179 | } | |
180 | ||
181 | int trace2_is_enabled(void) | |
182 | { | |
183 | return trace2_enabled; | |
184 | } | |
185 | ||
186 | void trace2_cmd_start_fl(const char *file, int line, const char **argv) | |
187 | { | |
188 | struct tr2_tgt *tgt_j; | |
189 | int j; | |
39f43177 JH |
190 | uint64_t us_now; |
191 | uint64_t us_elapsed_absolute; | |
ee4512ed JH |
192 | |
193 | if (!trace2_enabled) | |
194 | return; | |
195 | ||
39f43177 JH |
196 | us_now = getnanotime() / 1000; |
197 | us_elapsed_absolute = tr2tls_absolute_elapsed(us_now); | |
198 | ||
ee4512ed JH |
199 | for_each_wanted_builtin (j, tgt_j) |
200 | if (tgt_j->pfn_start_fl) | |
39f43177 JH |
201 | tgt_j->pfn_start_fl(file, line, us_elapsed_absolute, |
202 | argv); | |
ee4512ed JH |
203 | } |
204 | ||
205 | int trace2_cmd_exit_fl(const char *file, int line, int code) | |
206 | { | |
207 | struct tr2_tgt *tgt_j; | |
208 | int j; | |
209 | uint64_t us_now; | |
210 | uint64_t us_elapsed_absolute; | |
211 | ||
212 | code &= 0xff; | |
213 | ||
214 | if (!trace2_enabled) | |
215 | return code; | |
216 | ||
9a498767 | 217 | trace_git_fsync_stats(); |
26c6f251 JH |
218 | trace2_collect_process_info(TRACE2_PROCESS_INFO_EXIT); |
219 | ||
ee4512ed JH |
220 | tr2main_exit_code = code; |
221 | ||
222 | us_now = getnanotime() / 1000; | |
223 | us_elapsed_absolute = tr2tls_absolute_elapsed(us_now); | |
224 | ||
225 | for_each_wanted_builtin (j, tgt_j) | |
226 | if (tgt_j->pfn_exit_fl) | |
227 | tgt_j->pfn_exit_fl(file, line, us_elapsed_absolute, | |
228 | code); | |
229 | ||
230 | return code; | |
231 | } | |
232 | ||
233 | void trace2_cmd_error_va_fl(const char *file, int line, const char *fmt, | |
234 | va_list ap) | |
235 | { | |
236 | struct tr2_tgt *tgt_j; | |
237 | int j; | |
238 | ||
239 | if (!trace2_enabled) | |
240 | return; | |
241 | ||
242 | /* | |
243 | * We expect each target function to treat 'ap' as constant | |
244 | * and use va_copy (because an 'ap' can only be walked once). | |
245 | */ | |
246 | for_each_wanted_builtin (j, tgt_j) | |
247 | if (tgt_j->pfn_error_va_fl) | |
248 | tgt_j->pfn_error_va_fl(file, line, fmt, ap); | |
249 | } | |
250 | ||
251 | void trace2_cmd_path_fl(const char *file, int line, const char *pathname) | |
252 | { | |
253 | struct tr2_tgt *tgt_j; | |
254 | int j; | |
255 | ||
256 | if (!trace2_enabled) | |
257 | return; | |
258 | ||
259 | for_each_wanted_builtin (j, tgt_j) | |
260 | if (tgt_j->pfn_command_path_fl) | |
261 | tgt_j->pfn_command_path_fl(file, line, pathname); | |
262 | } | |
263 | ||
2f732bf1 ES |
264 | void trace2_cmd_ancestry_fl(const char *file, int line, const char **parent_names) |
265 | { | |
266 | struct tr2_tgt *tgt_j; | |
267 | int j; | |
268 | ||
269 | if (!trace2_enabled) | |
270 | return; | |
271 | ||
272 | for_each_wanted_builtin (j, tgt_j) | |
273 | if (tgt_j->pfn_command_ancestry_fl) | |
274 | tgt_j->pfn_command_ancestry_fl(file, line, parent_names); | |
275 | } | |
276 | ||
ee4512ed JH |
277 | void trace2_cmd_name_fl(const char *file, int line, const char *name) |
278 | { | |
279 | struct tr2_tgt *tgt_j; | |
280 | const char *hierarchy; | |
281 | int j; | |
282 | ||
283 | if (!trace2_enabled) | |
284 | return; | |
285 | ||
286 | tr2_cmd_name_append_hierarchy(name); | |
287 | hierarchy = tr2_cmd_name_get_hierarchy(); | |
288 | ||
289 | for_each_wanted_builtin (j, tgt_j) | |
290 | if (tgt_j->pfn_command_name_fl) | |
291 | tgt_j->pfn_command_name_fl(file, line, name, hierarchy); | |
292 | } | |
293 | ||
294 | void trace2_cmd_mode_fl(const char *file, int line, const char *mode) | |
295 | { | |
296 | struct tr2_tgt *tgt_j; | |
297 | int j; | |
298 | ||
299 | if (!trace2_enabled) | |
300 | return; | |
301 | ||
302 | for_each_wanted_builtin (j, tgt_j) | |
303 | if (tgt_j->pfn_command_mode_fl) | |
304 | tgt_j->pfn_command_mode_fl(file, line, mode); | |
305 | } | |
306 | ||
307 | void trace2_cmd_alias_fl(const char *file, int line, const char *alias, | |
308 | const char **argv) | |
309 | { | |
310 | struct tr2_tgt *tgt_j; | |
311 | int j; | |
312 | ||
313 | if (!trace2_enabled) | |
314 | return; | |
315 | ||
316 | for_each_wanted_builtin (j, tgt_j) | |
317 | if (tgt_j->pfn_alias_fl) | |
318 | tgt_j->pfn_alias_fl(file, line, alias, argv); | |
319 | } | |
320 | ||
321 | void trace2_cmd_list_config_fl(const char *file, int line) | |
322 | { | |
323 | if (!trace2_enabled) | |
324 | return; | |
325 | ||
326 | tr2_cfg_list_config_fl(file, line); | |
327 | } | |
328 | ||
3d3adaad JS |
329 | void trace2_cmd_list_env_vars_fl(const char *file, int line) |
330 | { | |
331 | if (!trace2_enabled) | |
332 | return; | |
333 | ||
334 | tr2_list_env_vars_fl(file, line); | |
335 | } | |
336 | ||
ee4512ed JH |
337 | void trace2_cmd_set_config_fl(const char *file, int line, const char *key, |
338 | const char *value) | |
339 | { | |
340 | if (!trace2_enabled) | |
341 | return; | |
342 | ||
343 | tr2_cfg_set_fl(file, line, key, value); | |
344 | } | |
345 | ||
346 | void trace2_child_start_fl(const char *file, int line, | |
347 | struct child_process *cmd) | |
348 | { | |
349 | struct tr2_tgt *tgt_j; | |
350 | int j; | |
351 | uint64_t us_now; | |
352 | uint64_t us_elapsed_absolute; | |
353 | ||
354 | if (!trace2_enabled) | |
355 | return; | |
356 | ||
357 | us_now = getnanotime() / 1000; | |
358 | us_elapsed_absolute = tr2tls_absolute_elapsed(us_now); | |
359 | ||
360 | cmd->trace2_child_id = tr2tls_locked_increment(&tr2_next_child_id); | |
361 | cmd->trace2_child_us_start = us_now; | |
362 | ||
363 | for_each_wanted_builtin (j, tgt_j) | |
364 | if (tgt_j->pfn_child_start_fl) | |
365 | tgt_j->pfn_child_start_fl(file, line, | |
366 | us_elapsed_absolute, cmd); | |
367 | } | |
368 | ||
369 | void trace2_child_exit_fl(const char *file, int line, struct child_process *cmd, | |
370 | int child_exit_code) | |
371 | { | |
372 | struct tr2_tgt *tgt_j; | |
373 | int j; | |
374 | uint64_t us_now; | |
375 | uint64_t us_elapsed_absolute; | |
376 | uint64_t us_elapsed_child; | |
377 | ||
378 | if (!trace2_enabled) | |
379 | return; | |
380 | ||
381 | us_now = getnanotime() / 1000; | |
382 | us_elapsed_absolute = tr2tls_absolute_elapsed(us_now); | |
383 | ||
384 | if (cmd->trace2_child_us_start) | |
385 | us_elapsed_child = us_now - cmd->trace2_child_us_start; | |
386 | else | |
387 | us_elapsed_child = 0; | |
388 | ||
389 | for_each_wanted_builtin (j, tgt_j) | |
390 | if (tgt_j->pfn_child_exit_fl) | |
391 | tgt_j->pfn_child_exit_fl(file, line, | |
392 | us_elapsed_absolute, | |
393 | cmd->trace2_child_id, cmd->pid, | |
394 | child_exit_code, | |
395 | us_elapsed_child); | |
396 | } | |
397 | ||
64bc7524 JH |
398 | void trace2_child_ready_fl(const char *file, int line, |
399 | struct child_process *cmd, | |
400 | const char *ready) | |
401 | { | |
402 | struct tr2_tgt *tgt_j; | |
403 | int j; | |
404 | uint64_t us_now; | |
405 | uint64_t us_elapsed_absolute; | |
406 | uint64_t us_elapsed_child; | |
407 | ||
408 | if (!trace2_enabled) | |
409 | return; | |
410 | ||
411 | us_now = getnanotime() / 1000; | |
412 | us_elapsed_absolute = tr2tls_absolute_elapsed(us_now); | |
413 | ||
414 | if (cmd->trace2_child_us_start) | |
415 | us_elapsed_child = us_now - cmd->trace2_child_us_start; | |
416 | else | |
417 | us_elapsed_child = 0; | |
418 | ||
419 | for_each_wanted_builtin (j, tgt_j) | |
420 | if (tgt_j->pfn_child_ready_fl) | |
421 | tgt_j->pfn_child_ready_fl(file, line, | |
422 | us_elapsed_absolute, | |
423 | cmd->trace2_child_id, | |
424 | cmd->pid, | |
425 | ready, | |
426 | us_elapsed_child); | |
427 | } | |
428 | ||
ee4512ed JH |
429 | int trace2_exec_fl(const char *file, int line, const char *exe, |
430 | const char **argv) | |
431 | { | |
432 | struct tr2_tgt *tgt_j; | |
433 | int j; | |
434 | int exec_id; | |
435 | uint64_t us_now; | |
436 | uint64_t us_elapsed_absolute; | |
437 | ||
438 | if (!trace2_enabled) | |
439 | return -1; | |
440 | ||
441 | us_now = getnanotime() / 1000; | |
442 | us_elapsed_absolute = tr2tls_absolute_elapsed(us_now); | |
443 | ||
444 | exec_id = tr2tls_locked_increment(&tr2_next_exec_id); | |
445 | ||
446 | for_each_wanted_builtin (j, tgt_j) | |
447 | if (tgt_j->pfn_exec_fl) | |
448 | tgt_j->pfn_exec_fl(file, line, us_elapsed_absolute, | |
449 | exec_id, exe, argv); | |
450 | ||
451 | return exec_id; | |
452 | } | |
453 | ||
454 | void trace2_exec_result_fl(const char *file, int line, int exec_id, int code) | |
455 | { | |
456 | struct tr2_tgt *tgt_j; | |
457 | int j; | |
458 | uint64_t us_now; | |
459 | uint64_t us_elapsed_absolute; | |
460 | ||
461 | if (!trace2_enabled) | |
462 | return; | |
463 | ||
464 | us_now = getnanotime() / 1000; | |
465 | us_elapsed_absolute = tr2tls_absolute_elapsed(us_now); | |
466 | ||
467 | for_each_wanted_builtin (j, tgt_j) | |
468 | if (tgt_j->pfn_exec_result_fl) | |
469 | tgt_j->pfn_exec_result_fl( | |
470 | file, line, us_elapsed_absolute, exec_id, code); | |
471 | } | |
472 | ||
473 | void trace2_thread_start_fl(const char *file, int line, const char *thread_name) | |
474 | { | |
475 | struct tr2_tgt *tgt_j; | |
476 | int j; | |
477 | uint64_t us_now; | |
478 | uint64_t us_elapsed_absolute; | |
479 | ||
480 | if (!trace2_enabled) | |
481 | return; | |
482 | ||
483 | if (tr2tls_is_main_thread()) { | |
484 | /* | |
485 | * We should only be called from the new thread's thread-proc, | |
486 | * so this is technically a bug. But in those cases where the | |
487 | * main thread also runs the thread-proc function (or when we | |
488 | * are built with threading disabled), we need to allow it. | |
489 | * | |
490 | * Convert this call to a region-enter so the nesting looks | |
491 | * correct. | |
492 | */ | |
493 | trace2_region_enter_printf_fl(file, line, NULL, NULL, NULL, | |
494 | "thread-proc on main: %s", | |
495 | thread_name); | |
496 | return; | |
497 | } | |
498 | ||
499 | us_now = getnanotime() / 1000; | |
500 | us_elapsed_absolute = tr2tls_absolute_elapsed(us_now); | |
501 | ||
a0897249 | 502 | tr2tls_create_self(thread_name, us_now); |
ee4512ed JH |
503 | |
504 | for_each_wanted_builtin (j, tgt_j) | |
505 | if (tgt_j->pfn_thread_start_fl) | |
506 | tgt_j->pfn_thread_start_fl(file, line, | |
507 | us_elapsed_absolute); | |
508 | } | |
509 | ||
510 | void trace2_thread_exit_fl(const char *file, int line) | |
511 | { | |
512 | struct tr2_tgt *tgt_j; | |
513 | int j; | |
514 | uint64_t us_now; | |
515 | uint64_t us_elapsed_absolute; | |
516 | uint64_t us_elapsed_thread; | |
517 | ||
518 | if (!trace2_enabled) | |
519 | return; | |
520 | ||
521 | if (tr2tls_is_main_thread()) { | |
522 | /* | |
523 | * We should only be called from the exiting thread's | |
524 | * thread-proc, so this is technically a bug. But in | |
525 | * those cases where the main thread also runs the | |
526 | * thread-proc function (or when we are built with | |
527 | * threading disabled), we need to allow it. | |
528 | * | |
529 | * Convert this call to a region-leave so the nesting | |
530 | * looks correct. | |
531 | */ | |
532 | trace2_region_leave_printf_fl(file, line, NULL, NULL, NULL, | |
533 | "thread-proc on main"); | |
534 | return; | |
535 | } | |
536 | ||
537 | us_now = getnanotime() / 1000; | |
538 | us_elapsed_absolute = tr2tls_absolute_elapsed(us_now); | |
539 | ||
540 | /* | |
541 | * Clear any unbalanced regions and then get the relative time | |
542 | * for the outer-most region (which we pushed when the thread | |
543 | * started). This gives us the run time of the thread. | |
544 | */ | |
545 | tr2tls_pop_unwind_self(); | |
546 | us_elapsed_thread = tr2tls_region_elasped_self(us_now); | |
547 | ||
548 | for_each_wanted_builtin (j, tgt_j) | |
549 | if (tgt_j->pfn_thread_exit_fl) | |
550 | tgt_j->pfn_thread_exit_fl(file, line, | |
551 | us_elapsed_absolute, | |
552 | us_elapsed_thread); | |
553 | ||
554 | tr2tls_unset_self(); | |
555 | } | |
556 | ||
557 | void trace2_def_param_fl(const char *file, int line, const char *param, | |
558 | const char *value) | |
559 | { | |
560 | struct tr2_tgt *tgt_j; | |
561 | int j; | |
562 | ||
563 | if (!trace2_enabled) | |
564 | return; | |
565 | ||
566 | for_each_wanted_builtin (j, tgt_j) | |
567 | if (tgt_j->pfn_param_fl) | |
568 | tgt_j->pfn_param_fl(file, line, param, value); | |
569 | } | |
570 | ||
571 | void trace2_def_repo_fl(const char *file, int line, struct repository *repo) | |
572 | { | |
573 | struct tr2_tgt *tgt_j; | |
574 | int j; | |
575 | ||
576 | if (!trace2_enabled) | |
577 | return; | |
578 | ||
579 | if (repo->trace2_repo_id) | |
580 | return; | |
581 | ||
582 | repo->trace2_repo_id = tr2tls_locked_increment(&tr2_next_repo_id); | |
583 | ||
584 | for_each_wanted_builtin (j, tgt_j) | |
585 | if (tgt_j->pfn_repo_fl) | |
586 | tgt_j->pfn_repo_fl(file, line, repo); | |
587 | } | |
588 | ||
589 | void trace2_region_enter_printf_va_fl(const char *file, int line, | |
590 | const char *category, const char *label, | |
591 | const struct repository *repo, | |
592 | const char *fmt, va_list ap) | |
593 | { | |
594 | struct tr2_tgt *tgt_j; | |
595 | int j; | |
596 | uint64_t us_now; | |
597 | uint64_t us_elapsed_absolute; | |
598 | ||
599 | if (!trace2_enabled) | |
600 | return; | |
601 | ||
602 | us_now = getnanotime() / 1000; | |
603 | us_elapsed_absolute = tr2tls_absolute_elapsed(us_now); | |
604 | ||
605 | /* | |
606 | * Print the region-enter message at the current nesting | |
607 | * (indentation) level and then push a new level. | |
608 | * | |
609 | * We expect each target function to treat 'ap' as constant | |
610 | * and use va_copy. | |
611 | */ | |
612 | for_each_wanted_builtin (j, tgt_j) | |
613 | if (tgt_j->pfn_region_enter_printf_va_fl) | |
614 | tgt_j->pfn_region_enter_printf_va_fl( | |
615 | file, line, us_elapsed_absolute, category, | |
616 | label, repo, fmt, ap); | |
617 | ||
618 | tr2tls_push_self(us_now); | |
619 | } | |
620 | ||
621 | void trace2_region_enter_fl(const char *file, int line, const char *category, | |
ad006fe4 | 622 | const char *label, const struct repository *repo, ...) |
ee4512ed | 623 | { |
ad006fe4 TB |
624 | va_list ap; |
625 | va_start(ap, repo); | |
ee4512ed | 626 | trace2_region_enter_printf_va_fl(file, line, category, label, repo, |
ad006fe4 TB |
627 | NULL, ap); |
628 | va_end(ap); | |
629 | ||
ee4512ed JH |
630 | } |
631 | ||
632 | void trace2_region_enter_printf_fl(const char *file, int line, | |
633 | const char *category, const char *label, | |
634 | const struct repository *repo, | |
635 | const char *fmt, ...) | |
636 | { | |
637 | va_list ap; | |
638 | ||
639 | va_start(ap, fmt); | |
640 | trace2_region_enter_printf_va_fl(file, line, category, label, repo, fmt, | |
641 | ap); | |
642 | va_end(ap); | |
643 | } | |
644 | ||
ee4512ed JH |
645 | void trace2_region_leave_printf_va_fl(const char *file, int line, |
646 | const char *category, const char *label, | |
647 | const struct repository *repo, | |
648 | const char *fmt, va_list ap) | |
649 | { | |
650 | struct tr2_tgt *tgt_j; | |
651 | int j; | |
652 | uint64_t us_now; | |
653 | uint64_t us_elapsed_absolute; | |
654 | uint64_t us_elapsed_region; | |
655 | ||
656 | if (!trace2_enabled) | |
657 | return; | |
658 | ||
659 | us_now = getnanotime() / 1000; | |
660 | us_elapsed_absolute = tr2tls_absolute_elapsed(us_now); | |
661 | ||
662 | /* | |
663 | * Get the elapsed time in the current region before we | |
664 | * pop it off the stack. Pop the stack. And then print | |
665 | * the perf message at the new (shallower) level so that | |
666 | * it lines up with the corresponding push/enter. | |
667 | */ | |
668 | us_elapsed_region = tr2tls_region_elasped_self(us_now); | |
669 | ||
670 | tr2tls_pop_self(); | |
671 | ||
672 | /* | |
673 | * We expect each target function to treat 'ap' as constant | |
674 | * and use va_copy. | |
675 | */ | |
676 | for_each_wanted_builtin (j, tgt_j) | |
677 | if (tgt_j->pfn_region_leave_printf_va_fl) | |
678 | tgt_j->pfn_region_leave_printf_va_fl( | |
679 | file, line, us_elapsed_absolute, | |
680 | us_elapsed_region, category, label, repo, fmt, | |
681 | ap); | |
682 | } | |
683 | ||
684 | void trace2_region_leave_fl(const char *file, int line, const char *category, | |
ad006fe4 | 685 | const char *label, const struct repository *repo, ...) |
ee4512ed | 686 | { |
ad006fe4 TB |
687 | va_list ap; |
688 | va_start(ap, repo); | |
ee4512ed | 689 | trace2_region_leave_printf_va_fl(file, line, category, label, repo, |
ad006fe4 TB |
690 | NULL, ap); |
691 | va_end(ap); | |
ee4512ed JH |
692 | } |
693 | ||
694 | void trace2_region_leave_printf_fl(const char *file, int line, | |
695 | const char *category, const char *label, | |
696 | const struct repository *repo, | |
697 | const char *fmt, ...) | |
698 | { | |
699 | va_list ap; | |
700 | ||
701 | va_start(ap, fmt); | |
702 | trace2_region_leave_printf_va_fl(file, line, category, label, repo, fmt, | |
703 | ap); | |
704 | va_end(ap); | |
705 | } | |
706 | ||
ee4512ed JH |
707 | void trace2_data_string_fl(const char *file, int line, const char *category, |
708 | const struct repository *repo, const char *key, | |
709 | const char *value) | |
710 | { | |
711 | struct tr2_tgt *tgt_j; | |
712 | int j; | |
713 | uint64_t us_now; | |
714 | uint64_t us_elapsed_absolute; | |
715 | uint64_t us_elapsed_region; | |
716 | ||
717 | if (!trace2_enabled) | |
718 | return; | |
719 | ||
720 | us_now = getnanotime() / 1000; | |
721 | us_elapsed_absolute = tr2tls_absolute_elapsed(us_now); | |
722 | us_elapsed_region = tr2tls_region_elasped_self(us_now); | |
723 | ||
724 | for_each_wanted_builtin (j, tgt_j) | |
725 | if (tgt_j->pfn_data_fl) | |
726 | tgt_j->pfn_data_fl(file, line, us_elapsed_absolute, | |
727 | us_elapsed_region, category, repo, | |
728 | key, value); | |
729 | } | |
730 | ||
731 | void trace2_data_intmax_fl(const char *file, int line, const char *category, | |
732 | const struct repository *repo, const char *key, | |
733 | intmax_t value) | |
734 | { | |
735 | struct strbuf buf_string = STRBUF_INIT; | |
736 | ||
737 | if (!trace2_enabled) | |
738 | return; | |
739 | ||
740 | strbuf_addf(&buf_string, "%" PRIdMAX, value); | |
741 | trace2_data_string_fl(file, line, category, repo, key, buf_string.buf); | |
742 | strbuf_release(&buf_string); | |
743 | } | |
744 | ||
745 | void trace2_data_json_fl(const char *file, int line, const char *category, | |
746 | const struct repository *repo, const char *key, | |
747 | const struct json_writer *value) | |
748 | { | |
749 | struct tr2_tgt *tgt_j; | |
750 | int j; | |
751 | uint64_t us_now; | |
752 | uint64_t us_elapsed_absolute; | |
753 | uint64_t us_elapsed_region; | |
754 | ||
755 | if (!trace2_enabled) | |
756 | return; | |
757 | ||
758 | us_now = getnanotime() / 1000; | |
759 | us_elapsed_absolute = tr2tls_absolute_elapsed(us_now); | |
760 | us_elapsed_region = tr2tls_region_elasped_self(us_now); | |
761 | ||
762 | for_each_wanted_builtin (j, tgt_j) | |
22a73383 | 763 | if (tgt_j->pfn_data_json_fl) |
ee4512ed JH |
764 | tgt_j->pfn_data_json_fl(file, line, us_elapsed_absolute, |
765 | us_elapsed_region, category, | |
766 | repo, key, value); | |
767 | } | |
768 | ||
769 | void trace2_printf_va_fl(const char *file, int line, const char *fmt, | |
770 | va_list ap) | |
771 | { | |
772 | struct tr2_tgt *tgt_j; | |
773 | int j; | |
774 | uint64_t us_now; | |
775 | uint64_t us_elapsed_absolute; | |
776 | ||
777 | if (!trace2_enabled) | |
778 | return; | |
779 | ||
780 | us_now = getnanotime() / 1000; | |
781 | us_elapsed_absolute = tr2tls_absolute_elapsed(us_now); | |
782 | ||
783 | /* | |
784 | * We expect each target function to treat 'ap' as constant | |
785 | * and use va_copy. | |
786 | */ | |
787 | for_each_wanted_builtin (j, tgt_j) | |
788 | if (tgt_j->pfn_printf_va_fl) | |
789 | tgt_j->pfn_printf_va_fl(file, line, us_elapsed_absolute, | |
790 | fmt, ap); | |
791 | } | |
792 | ||
793 | void trace2_printf_fl(const char *file, int line, const char *fmt, ...) | |
794 | { | |
795 | va_list ap; | |
796 | ||
797 | va_start(ap, fmt); | |
798 | trace2_printf_va_fl(file, line, fmt, ap); | |
799 | va_end(ap); | |
800 | } | |
801 | ||
e97e1cf4 JS |
802 | const char *trace2_session_id(void) |
803 | { | |
804 | return tr2_sid_get(); | |
805 | } |