3 #include "json-writer.h"
5 #include "run-command.h"
7 #include "thread-utils.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"
13 #include "trace2/tr2_tgt.h"
14 #include "trace2/tr2_tls.h"
16 static int trace2_enabled
;
18 static int tr2_next_child_id
; /* modify under lock */
19 static int tr2_next_exec_id
; /* modify under lock */
20 static int tr2_next_repo_id
= 1; /* modify under lock. zero is reserved */
23 * A table of the builtin TRACE2 targets. Each of these may be independently
24 * enabled or disabled. Each TRACE2 API method will try to write an event to
25 * *each* of the enabled targets.
27 /* clang-format off */
28 static struct tr2_tgt
*tr2_tgt_builtins
[] =
37 /* clang-format off */
38 #define for_each_builtin(j, tgt_j) \
39 for (j = 0, tgt_j = tr2_tgt_builtins[j]; \
41 j++, tgt_j = tr2_tgt_builtins[j])
44 /* clang-format off */
45 #define for_each_wanted_builtin(j, tgt_j) \
46 for_each_builtin(j, tgt_j) \
47 if (tr2_dst_trace_want(tgt_j->pdst))
51 * Force (rather than lazily) initialize any of the requested
52 * builtin TRACE2 targets at startup (and before we've seen an
53 * actual TRACE2 event call) so we can see if we need to setup
54 * the TR2 and TLS machinery.
56 * Return the number of builtin targets enabled.
58 static int tr2_tgt_want_builtins(void)
60 struct tr2_tgt
*tgt_j
;
64 for_each_builtin (j
, tgt_j
)
65 if (tgt_j
->pfn_init())
72 * Properly terminate each builtin target. Give each target
73 * a chance to write a summary event and/or flush if necessary
74 * and then close the fd.
76 static void tr2_tgt_disable_builtins(void)
78 struct tr2_tgt
*tgt_j
;
81 for_each_builtin (j
, tgt_j
)
85 static int tr2main_exit_code
;
88 * Our atexit routine should run after everything has finished.
90 * Note that events generated here might not actually appear if
91 * we are writing to fd 1 or 2 and our atexit routine runs after
92 * the pager's atexit routine (since it closes them to shutdown
95 static void tr2main_atexit_handler(void)
97 struct tr2_tgt
*tgt_j
;
100 uint64_t us_elapsed_absolute
;
102 us_now
= getnanotime() / 1000;
103 us_elapsed_absolute
= tr2tls_absolute_elapsed(us_now
);
106 * Clear any unbalanced regions so that our atexit message
107 * does not appear nested. This improves the appearance of
108 * the trace output if someone calls die(), for example.
110 tr2tls_pop_unwind_self();
112 for_each_wanted_builtin (j
, tgt_j
)
113 if (tgt_j
->pfn_atexit
)
114 tgt_j
->pfn_atexit(us_elapsed_absolute
,
117 tr2_tgt_disable_builtins();
121 tr2_cmd_name_release();
122 tr2_cfg_free_patterns();
127 static void tr2main_signal_handler(int signo
)
129 struct tr2_tgt
*tgt_j
;
132 uint64_t us_elapsed_absolute
;
134 us_now
= getnanotime() / 1000;
135 us_elapsed_absolute
= tr2tls_absolute_elapsed(us_now
);
137 for_each_wanted_builtin (j
, tgt_j
)
138 if (tgt_j
->pfn_signal
)
139 tgt_j
->pfn_signal(us_elapsed_absolute
, signo
);
145 void trace2_initialize_clock(void)
147 tr2tls_start_process_clock();
150 void trace2_initialize_fl(const char *file
, int line
)
152 struct tr2_tgt
*tgt_j
;
158 if (!tr2_tgt_want_builtins())
164 atexit(tr2main_atexit_handler
);
165 sigchain_push(SIGPIPE
, tr2main_signal_handler
);
169 * Emit 'version' message on each active builtin target.
171 for_each_wanted_builtin (j
, tgt_j
)
172 if (tgt_j
->pfn_version_fl
)
173 tgt_j
->pfn_version_fl(file
, line
);
176 int trace2_is_enabled(void)
178 return trace2_enabled
;
181 void trace2_cmd_start_fl(const char *file
, int line
, const char **argv
)
183 struct tr2_tgt
*tgt_j
;
186 uint64_t us_elapsed_absolute
;
191 us_now
= getnanotime() / 1000;
192 us_elapsed_absolute
= tr2tls_absolute_elapsed(us_now
);
194 for_each_wanted_builtin (j
, tgt_j
)
195 if (tgt_j
->pfn_start_fl
)
196 tgt_j
->pfn_start_fl(file
, line
, us_elapsed_absolute
,
200 int trace2_cmd_exit_fl(const char *file
, int line
, int code
)
202 struct tr2_tgt
*tgt_j
;
205 uint64_t us_elapsed_absolute
;
212 tr2main_exit_code
= code
;
214 us_now
= getnanotime() / 1000;
215 us_elapsed_absolute
= tr2tls_absolute_elapsed(us_now
);
217 for_each_wanted_builtin (j
, tgt_j
)
218 if (tgt_j
->pfn_exit_fl
)
219 tgt_j
->pfn_exit_fl(file
, line
, us_elapsed_absolute
,
225 void trace2_cmd_error_va_fl(const char *file
, int line
, const char *fmt
,
228 struct tr2_tgt
*tgt_j
;
235 * We expect each target function to treat 'ap' as constant
236 * and use va_copy (because an 'ap' can only be walked once).
238 for_each_wanted_builtin (j
, tgt_j
)
239 if (tgt_j
->pfn_error_va_fl
)
240 tgt_j
->pfn_error_va_fl(file
, line
, fmt
, ap
);
243 void trace2_cmd_path_fl(const char *file
, int line
, const char *pathname
)
245 struct tr2_tgt
*tgt_j
;
251 for_each_wanted_builtin (j
, tgt_j
)
252 if (tgt_j
->pfn_command_path_fl
)
253 tgt_j
->pfn_command_path_fl(file
, line
, pathname
);
256 void trace2_cmd_name_fl(const char *file
, int line
, const char *name
)
258 struct tr2_tgt
*tgt_j
;
259 const char *hierarchy
;
265 tr2_cmd_name_append_hierarchy(name
);
266 hierarchy
= tr2_cmd_name_get_hierarchy();
268 for_each_wanted_builtin (j
, tgt_j
)
269 if (tgt_j
->pfn_command_name_fl
)
270 tgt_j
->pfn_command_name_fl(file
, line
, name
, hierarchy
);
273 void trace2_cmd_mode_fl(const char *file
, int line
, const char *mode
)
275 struct tr2_tgt
*tgt_j
;
281 for_each_wanted_builtin (j
, tgt_j
)
282 if (tgt_j
->pfn_command_mode_fl
)
283 tgt_j
->pfn_command_mode_fl(file
, line
, mode
);
286 void trace2_cmd_alias_fl(const char *file
, int line
, const char *alias
,
289 struct tr2_tgt
*tgt_j
;
295 for_each_wanted_builtin (j
, tgt_j
)
296 if (tgt_j
->pfn_alias_fl
)
297 tgt_j
->pfn_alias_fl(file
, line
, alias
, argv
);
300 void trace2_cmd_list_config_fl(const char *file
, int line
)
305 tr2_cfg_list_config_fl(file
, line
);
308 void trace2_cmd_set_config_fl(const char *file
, int line
, const char *key
,
314 tr2_cfg_set_fl(file
, line
, key
, value
);
317 void trace2_child_start_fl(const char *file
, int line
,
318 struct child_process
*cmd
)
320 struct tr2_tgt
*tgt_j
;
323 uint64_t us_elapsed_absolute
;
328 us_now
= getnanotime() / 1000;
329 us_elapsed_absolute
= tr2tls_absolute_elapsed(us_now
);
331 cmd
->trace2_child_id
= tr2tls_locked_increment(&tr2_next_child_id
);
332 cmd
->trace2_child_us_start
= us_now
;
334 for_each_wanted_builtin (j
, tgt_j
)
335 if (tgt_j
->pfn_child_start_fl
)
336 tgt_j
->pfn_child_start_fl(file
, line
,
337 us_elapsed_absolute
, cmd
);
340 void trace2_child_exit_fl(const char *file
, int line
, struct child_process
*cmd
,
343 struct tr2_tgt
*tgt_j
;
346 uint64_t us_elapsed_absolute
;
347 uint64_t us_elapsed_child
;
352 us_now
= getnanotime() / 1000;
353 us_elapsed_absolute
= tr2tls_absolute_elapsed(us_now
);
355 if (cmd
->trace2_child_us_start
)
356 us_elapsed_child
= us_now
- cmd
->trace2_child_us_start
;
358 us_elapsed_child
= 0;
360 for_each_wanted_builtin (j
, tgt_j
)
361 if (tgt_j
->pfn_child_exit_fl
)
362 tgt_j
->pfn_child_exit_fl(file
, line
,
364 cmd
->trace2_child_id
, cmd
->pid
,
369 int trace2_exec_fl(const char *file
, int line
, const char *exe
,
372 struct tr2_tgt
*tgt_j
;
376 uint64_t us_elapsed_absolute
;
381 us_now
= getnanotime() / 1000;
382 us_elapsed_absolute
= tr2tls_absolute_elapsed(us_now
);
384 exec_id
= tr2tls_locked_increment(&tr2_next_exec_id
);
386 for_each_wanted_builtin (j
, tgt_j
)
387 if (tgt_j
->pfn_exec_fl
)
388 tgt_j
->pfn_exec_fl(file
, line
, us_elapsed_absolute
,
394 void trace2_exec_result_fl(const char *file
, int line
, int exec_id
, int code
)
396 struct tr2_tgt
*tgt_j
;
399 uint64_t us_elapsed_absolute
;
404 us_now
= getnanotime() / 1000;
405 us_elapsed_absolute
= tr2tls_absolute_elapsed(us_now
);
407 for_each_wanted_builtin (j
, tgt_j
)
408 if (tgt_j
->pfn_exec_result_fl
)
409 tgt_j
->pfn_exec_result_fl(
410 file
, line
, us_elapsed_absolute
, exec_id
, code
);
413 void trace2_thread_start_fl(const char *file
, int line
, const char *thread_name
)
415 struct tr2_tgt
*tgt_j
;
418 uint64_t us_elapsed_absolute
;
423 if (tr2tls_is_main_thread()) {
425 * We should only be called from the new thread's thread-proc,
426 * so this is technically a bug. But in those cases where the
427 * main thread also runs the thread-proc function (or when we
428 * are built with threading disabled), we need to allow it.
430 * Convert this call to a region-enter so the nesting looks
433 trace2_region_enter_printf_fl(file
, line
, NULL
, NULL
, NULL
,
434 "thread-proc on main: %s",
439 us_now
= getnanotime() / 1000;
440 us_elapsed_absolute
= tr2tls_absolute_elapsed(us_now
);
442 tr2tls_create_self(thread_name
, us_now
);
444 for_each_wanted_builtin (j
, tgt_j
)
445 if (tgt_j
->pfn_thread_start_fl
)
446 tgt_j
->pfn_thread_start_fl(file
, line
,
447 us_elapsed_absolute
);
450 void trace2_thread_exit_fl(const char *file
, int line
)
452 struct tr2_tgt
*tgt_j
;
455 uint64_t us_elapsed_absolute
;
456 uint64_t us_elapsed_thread
;
461 if (tr2tls_is_main_thread()) {
463 * We should only be called from the exiting thread's
464 * thread-proc, so this is technically a bug. But in
465 * those cases where the main thread also runs the
466 * thread-proc function (or when we are built with
467 * threading disabled), we need to allow it.
469 * Convert this call to a region-leave so the nesting
472 trace2_region_leave_printf_fl(file
, line
, NULL
, NULL
, NULL
,
473 "thread-proc on main");
477 us_now
= getnanotime() / 1000;
478 us_elapsed_absolute
= tr2tls_absolute_elapsed(us_now
);
481 * Clear any unbalanced regions and then get the relative time
482 * for the outer-most region (which we pushed when the thread
483 * started). This gives us the run time of the thread.
485 tr2tls_pop_unwind_self();
486 us_elapsed_thread
= tr2tls_region_elasped_self(us_now
);
488 for_each_wanted_builtin (j
, tgt_j
)
489 if (tgt_j
->pfn_thread_exit_fl
)
490 tgt_j
->pfn_thread_exit_fl(file
, line
,
497 void trace2_def_param_fl(const char *file
, int line
, const char *param
,
500 struct tr2_tgt
*tgt_j
;
506 for_each_wanted_builtin (j
, tgt_j
)
507 if (tgt_j
->pfn_param_fl
)
508 tgt_j
->pfn_param_fl(file
, line
, param
, value
);
511 void trace2_def_repo_fl(const char *file
, int line
, struct repository
*repo
)
513 struct tr2_tgt
*tgt_j
;
519 if (repo
->trace2_repo_id
)
522 repo
->trace2_repo_id
= tr2tls_locked_increment(&tr2_next_repo_id
);
524 for_each_wanted_builtin (j
, tgt_j
)
525 if (tgt_j
->pfn_repo_fl
)
526 tgt_j
->pfn_repo_fl(file
, line
, repo
);
529 void trace2_region_enter_printf_va_fl(const char *file
, int line
,
530 const char *category
, const char *label
,
531 const struct repository
*repo
,
532 const char *fmt
, va_list ap
)
534 struct tr2_tgt
*tgt_j
;
537 uint64_t us_elapsed_absolute
;
542 us_now
= getnanotime() / 1000;
543 us_elapsed_absolute
= tr2tls_absolute_elapsed(us_now
);
546 * Print the region-enter message at the current nesting
547 * (indentation) level and then push a new level.
549 * We expect each target function to treat 'ap' as constant
552 for_each_wanted_builtin (j
, tgt_j
)
553 if (tgt_j
->pfn_region_enter_printf_va_fl
)
554 tgt_j
->pfn_region_enter_printf_va_fl(
555 file
, line
, us_elapsed_absolute
, category
,
556 label
, repo
, fmt
, ap
);
558 tr2tls_push_self(us_now
);
561 void trace2_region_enter_fl(const char *file
, int line
, const char *category
,
562 const char *label
, const struct repository
*repo
)
564 trace2_region_enter_printf_va_fl(file
, line
, category
, label
, repo
,
568 void trace2_region_enter_printf_fl(const char *file
, int line
,
569 const char *category
, const char *label
,
570 const struct repository
*repo
,
571 const char *fmt
, ...)
576 trace2_region_enter_printf_va_fl(file
, line
, category
, label
, repo
, fmt
,
581 #ifndef HAVE_VARIADIC_MACROS
582 void trace2_region_enter_printf(const char *category
, const char *label
,
583 const struct repository
*repo
, const char *fmt
,
589 trace2_region_enter_printf_va_fl(NULL
, 0, category
, label
, repo
, fmt
,
595 void trace2_region_leave_printf_va_fl(const char *file
, int line
,
596 const char *category
, const char *label
,
597 const struct repository
*repo
,
598 const char *fmt
, va_list ap
)
600 struct tr2_tgt
*tgt_j
;
603 uint64_t us_elapsed_absolute
;
604 uint64_t us_elapsed_region
;
609 us_now
= getnanotime() / 1000;
610 us_elapsed_absolute
= tr2tls_absolute_elapsed(us_now
);
613 * Get the elapsed time in the current region before we
614 * pop it off the stack. Pop the stack. And then print
615 * the perf message at the new (shallower) level so that
616 * it lines up with the corresponding push/enter.
618 us_elapsed_region
= tr2tls_region_elasped_self(us_now
);
623 * We expect each target function to treat 'ap' as constant
626 for_each_wanted_builtin (j
, tgt_j
)
627 if (tgt_j
->pfn_region_leave_printf_va_fl
)
628 tgt_j
->pfn_region_leave_printf_va_fl(
629 file
, line
, us_elapsed_absolute
,
630 us_elapsed_region
, category
, label
, repo
, fmt
,
634 void trace2_region_leave_fl(const char *file
, int line
, const char *category
,
635 const char *label
, const struct repository
*repo
)
637 trace2_region_leave_printf_va_fl(file
, line
, category
, label
, repo
,
641 void trace2_region_leave_printf_fl(const char *file
, int line
,
642 const char *category
, const char *label
,
643 const struct repository
*repo
,
644 const char *fmt
, ...)
649 trace2_region_leave_printf_va_fl(file
, line
, category
, label
, repo
, fmt
,
654 #ifndef HAVE_VARIADIC_MACROS
655 void trace2_region_leave_printf(const char *category
, const char *label
,
656 const struct repository
*repo
, const char *fmt
,
662 trace2_region_leave_printf_va_fl(NULL
, 0, category
, label
, repo
, fmt
,
668 void trace2_data_string_fl(const char *file
, int line
, const char *category
,
669 const struct repository
*repo
, const char *key
,
672 struct tr2_tgt
*tgt_j
;
675 uint64_t us_elapsed_absolute
;
676 uint64_t us_elapsed_region
;
681 us_now
= getnanotime() / 1000;
682 us_elapsed_absolute
= tr2tls_absolute_elapsed(us_now
);
683 us_elapsed_region
= tr2tls_region_elasped_self(us_now
);
685 for_each_wanted_builtin (j
, tgt_j
)
686 if (tgt_j
->pfn_data_fl
)
687 tgt_j
->pfn_data_fl(file
, line
, us_elapsed_absolute
,
688 us_elapsed_region
, category
, repo
,
692 void trace2_data_intmax_fl(const char *file
, int line
, const char *category
,
693 const struct repository
*repo
, const char *key
,
696 struct strbuf buf_string
= STRBUF_INIT
;
701 strbuf_addf(&buf_string
, "%" PRIdMAX
, value
);
702 trace2_data_string_fl(file
, line
, category
, repo
, key
, buf_string
.buf
);
703 strbuf_release(&buf_string
);
706 void trace2_data_json_fl(const char *file
, int line
, const char *category
,
707 const struct repository
*repo
, const char *key
,
708 const struct json_writer
*value
)
710 struct tr2_tgt
*tgt_j
;
713 uint64_t us_elapsed_absolute
;
714 uint64_t us_elapsed_region
;
719 us_now
= getnanotime() / 1000;
720 us_elapsed_absolute
= tr2tls_absolute_elapsed(us_now
);
721 us_elapsed_region
= tr2tls_region_elasped_self(us_now
);
723 for_each_wanted_builtin (j
, tgt_j
)
724 if (tgt_j
->pfn_data_fl
)
725 tgt_j
->pfn_data_json_fl(file
, line
, us_elapsed_absolute
,
726 us_elapsed_region
, category
,
730 void trace2_printf_va_fl(const char *file
, int line
, const char *fmt
,
733 struct tr2_tgt
*tgt_j
;
736 uint64_t us_elapsed_absolute
;
741 us_now
= getnanotime() / 1000;
742 us_elapsed_absolute
= tr2tls_absolute_elapsed(us_now
);
745 * We expect each target function to treat 'ap' as constant
748 for_each_wanted_builtin (j
, tgt_j
)
749 if (tgt_j
->pfn_printf_va_fl
)
750 tgt_j
->pfn_printf_va_fl(file
, line
, us_elapsed_absolute
,
754 void trace2_printf_fl(const char *file
, int line
, const char *fmt
, ...)
759 trace2_printf_va_fl(file
, line
, fmt
, ap
);
763 #ifndef HAVE_VARIADIC_MACROS
764 void trace2_printf(const char *fmt
, ...)
769 trace2_printf_va_fl(NULL
, 0, fmt
, ap
);