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
;
189 for_each_wanted_builtin (j
, tgt_j
)
190 if (tgt_j
->pfn_start_fl
)
191 tgt_j
->pfn_start_fl(file
, line
, argv
);
194 int trace2_cmd_exit_fl(const char *file
, int line
, int code
)
196 struct tr2_tgt
*tgt_j
;
199 uint64_t us_elapsed_absolute
;
206 tr2main_exit_code
= code
;
208 us_now
= getnanotime() / 1000;
209 us_elapsed_absolute
= tr2tls_absolute_elapsed(us_now
);
211 for_each_wanted_builtin (j
, tgt_j
)
212 if (tgt_j
->pfn_exit_fl
)
213 tgt_j
->pfn_exit_fl(file
, line
, us_elapsed_absolute
,
219 void trace2_cmd_error_va_fl(const char *file
, int line
, const char *fmt
,
222 struct tr2_tgt
*tgt_j
;
229 * We expect each target function to treat 'ap' as constant
230 * and use va_copy (because an 'ap' can only be walked once).
232 for_each_wanted_builtin (j
, tgt_j
)
233 if (tgt_j
->pfn_error_va_fl
)
234 tgt_j
->pfn_error_va_fl(file
, line
, fmt
, ap
);
237 void trace2_cmd_path_fl(const char *file
, int line
, const char *pathname
)
239 struct tr2_tgt
*tgt_j
;
245 for_each_wanted_builtin (j
, tgt_j
)
246 if (tgt_j
->pfn_command_path_fl
)
247 tgt_j
->pfn_command_path_fl(file
, line
, pathname
);
250 void trace2_cmd_name_fl(const char *file
, int line
, const char *name
)
252 struct tr2_tgt
*tgt_j
;
253 const char *hierarchy
;
259 tr2_cmd_name_append_hierarchy(name
);
260 hierarchy
= tr2_cmd_name_get_hierarchy();
262 for_each_wanted_builtin (j
, tgt_j
)
263 if (tgt_j
->pfn_command_name_fl
)
264 tgt_j
->pfn_command_name_fl(file
, line
, name
, hierarchy
);
267 void trace2_cmd_mode_fl(const char *file
, int line
, const char *mode
)
269 struct tr2_tgt
*tgt_j
;
275 for_each_wanted_builtin (j
, tgt_j
)
276 if (tgt_j
->pfn_command_mode_fl
)
277 tgt_j
->pfn_command_mode_fl(file
, line
, mode
);
280 void trace2_cmd_alias_fl(const char *file
, int line
, const char *alias
,
283 struct tr2_tgt
*tgt_j
;
289 for_each_wanted_builtin (j
, tgt_j
)
290 if (tgt_j
->pfn_alias_fl
)
291 tgt_j
->pfn_alias_fl(file
, line
, alias
, argv
);
294 void trace2_cmd_list_config_fl(const char *file
, int line
)
299 tr2_cfg_list_config_fl(file
, line
);
302 void trace2_cmd_set_config_fl(const char *file
, int line
, const char *key
,
308 tr2_cfg_set_fl(file
, line
, key
, value
);
311 void trace2_child_start_fl(const char *file
, int line
,
312 struct child_process
*cmd
)
314 struct tr2_tgt
*tgt_j
;
317 uint64_t us_elapsed_absolute
;
322 us_now
= getnanotime() / 1000;
323 us_elapsed_absolute
= tr2tls_absolute_elapsed(us_now
);
325 cmd
->trace2_child_id
= tr2tls_locked_increment(&tr2_next_child_id
);
326 cmd
->trace2_child_us_start
= us_now
;
328 for_each_wanted_builtin (j
, tgt_j
)
329 if (tgt_j
->pfn_child_start_fl
)
330 tgt_j
->pfn_child_start_fl(file
, line
,
331 us_elapsed_absolute
, cmd
);
334 void trace2_child_exit_fl(const char *file
, int line
, struct child_process
*cmd
,
337 struct tr2_tgt
*tgt_j
;
340 uint64_t us_elapsed_absolute
;
341 uint64_t us_elapsed_child
;
346 us_now
= getnanotime() / 1000;
347 us_elapsed_absolute
= tr2tls_absolute_elapsed(us_now
);
349 if (cmd
->trace2_child_us_start
)
350 us_elapsed_child
= us_now
- cmd
->trace2_child_us_start
;
352 us_elapsed_child
= 0;
354 for_each_wanted_builtin (j
, tgt_j
)
355 if (tgt_j
->pfn_child_exit_fl
)
356 tgt_j
->pfn_child_exit_fl(file
, line
,
358 cmd
->trace2_child_id
, cmd
->pid
,
363 int trace2_exec_fl(const char *file
, int line
, const char *exe
,
366 struct tr2_tgt
*tgt_j
;
370 uint64_t us_elapsed_absolute
;
375 us_now
= getnanotime() / 1000;
376 us_elapsed_absolute
= tr2tls_absolute_elapsed(us_now
);
378 exec_id
= tr2tls_locked_increment(&tr2_next_exec_id
);
380 for_each_wanted_builtin (j
, tgt_j
)
381 if (tgt_j
->pfn_exec_fl
)
382 tgt_j
->pfn_exec_fl(file
, line
, us_elapsed_absolute
,
388 void trace2_exec_result_fl(const char *file
, int line
, int exec_id
, int code
)
390 struct tr2_tgt
*tgt_j
;
393 uint64_t us_elapsed_absolute
;
398 us_now
= getnanotime() / 1000;
399 us_elapsed_absolute
= tr2tls_absolute_elapsed(us_now
);
401 for_each_wanted_builtin (j
, tgt_j
)
402 if (tgt_j
->pfn_exec_result_fl
)
403 tgt_j
->pfn_exec_result_fl(
404 file
, line
, us_elapsed_absolute
, exec_id
, code
);
407 void trace2_thread_start_fl(const char *file
, int line
, const char *thread_name
)
409 struct tr2_tgt
*tgt_j
;
412 uint64_t us_elapsed_absolute
;
417 if (tr2tls_is_main_thread()) {
419 * We should only be called from the new thread's thread-proc,
420 * so this is technically a bug. But in those cases where the
421 * main thread also runs the thread-proc function (or when we
422 * are built with threading disabled), we need to allow it.
424 * Convert this call to a region-enter so the nesting looks
427 trace2_region_enter_printf_fl(file
, line
, NULL
, NULL
, NULL
,
428 "thread-proc on main: %s",
433 us_now
= getnanotime() / 1000;
434 us_elapsed_absolute
= tr2tls_absolute_elapsed(us_now
);
436 tr2tls_create_self(thread_name
, us_now
);
438 for_each_wanted_builtin (j
, tgt_j
)
439 if (tgt_j
->pfn_thread_start_fl
)
440 tgt_j
->pfn_thread_start_fl(file
, line
,
441 us_elapsed_absolute
);
444 void trace2_thread_exit_fl(const char *file
, int line
)
446 struct tr2_tgt
*tgt_j
;
449 uint64_t us_elapsed_absolute
;
450 uint64_t us_elapsed_thread
;
455 if (tr2tls_is_main_thread()) {
457 * We should only be called from the exiting thread's
458 * thread-proc, so this is technically a bug. But in
459 * those cases where the main thread also runs the
460 * thread-proc function (or when we are built with
461 * threading disabled), we need to allow it.
463 * Convert this call to a region-leave so the nesting
466 trace2_region_leave_printf_fl(file
, line
, NULL
, NULL
, NULL
,
467 "thread-proc on main");
471 us_now
= getnanotime() / 1000;
472 us_elapsed_absolute
= tr2tls_absolute_elapsed(us_now
);
475 * Clear any unbalanced regions and then get the relative time
476 * for the outer-most region (which we pushed when the thread
477 * started). This gives us the run time of the thread.
479 tr2tls_pop_unwind_self();
480 us_elapsed_thread
= tr2tls_region_elasped_self(us_now
);
482 for_each_wanted_builtin (j
, tgt_j
)
483 if (tgt_j
->pfn_thread_exit_fl
)
484 tgt_j
->pfn_thread_exit_fl(file
, line
,
491 void trace2_def_param_fl(const char *file
, int line
, const char *param
,
494 struct tr2_tgt
*tgt_j
;
500 for_each_wanted_builtin (j
, tgt_j
)
501 if (tgt_j
->pfn_param_fl
)
502 tgt_j
->pfn_param_fl(file
, line
, param
, value
);
505 void trace2_def_repo_fl(const char *file
, int line
, struct repository
*repo
)
507 struct tr2_tgt
*tgt_j
;
513 if (repo
->trace2_repo_id
)
516 repo
->trace2_repo_id
= tr2tls_locked_increment(&tr2_next_repo_id
);
518 for_each_wanted_builtin (j
, tgt_j
)
519 if (tgt_j
->pfn_repo_fl
)
520 tgt_j
->pfn_repo_fl(file
, line
, repo
);
523 void trace2_region_enter_printf_va_fl(const char *file
, int line
,
524 const char *category
, const char *label
,
525 const struct repository
*repo
,
526 const char *fmt
, va_list ap
)
528 struct tr2_tgt
*tgt_j
;
531 uint64_t us_elapsed_absolute
;
536 us_now
= getnanotime() / 1000;
537 us_elapsed_absolute
= tr2tls_absolute_elapsed(us_now
);
540 * Print the region-enter message at the current nesting
541 * (indentation) level and then push a new level.
543 * We expect each target function to treat 'ap' as constant
546 for_each_wanted_builtin (j
, tgt_j
)
547 if (tgt_j
->pfn_region_enter_printf_va_fl
)
548 tgt_j
->pfn_region_enter_printf_va_fl(
549 file
, line
, us_elapsed_absolute
, category
,
550 label
, repo
, fmt
, ap
);
552 tr2tls_push_self(us_now
);
555 void trace2_region_enter_fl(const char *file
, int line
, const char *category
,
556 const char *label
, const struct repository
*repo
)
558 trace2_region_enter_printf_va_fl(file
, line
, category
, label
, repo
,
562 void trace2_region_enter_printf_fl(const char *file
, int line
,
563 const char *category
, const char *label
,
564 const struct repository
*repo
,
565 const char *fmt
, ...)
570 trace2_region_enter_printf_va_fl(file
, line
, category
, label
, repo
, fmt
,
575 #ifndef HAVE_VARIADIC_MACROS
576 void trace2_region_enter_printf(const char *category
, const char *label
,
577 const struct repository
*repo
, const char *fmt
,
583 trace2_region_enter_printf_va_fl(NULL
, 0, category
, label
, repo
, fmt
,
589 void trace2_region_leave_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
)
594 struct tr2_tgt
*tgt_j
;
597 uint64_t us_elapsed_absolute
;
598 uint64_t us_elapsed_region
;
603 us_now
= getnanotime() / 1000;
604 us_elapsed_absolute
= tr2tls_absolute_elapsed(us_now
);
607 * Get the elapsed time in the current region before we
608 * pop it off the stack. Pop the stack. And then print
609 * the perf message at the new (shallower) level so that
610 * it lines up with the corresponding push/enter.
612 us_elapsed_region
= tr2tls_region_elasped_self(us_now
);
617 * We expect each target function to treat 'ap' as constant
620 for_each_wanted_builtin (j
, tgt_j
)
621 if (tgt_j
->pfn_region_leave_printf_va_fl
)
622 tgt_j
->pfn_region_leave_printf_va_fl(
623 file
, line
, us_elapsed_absolute
,
624 us_elapsed_region
, category
, label
, repo
, fmt
,
628 void trace2_region_leave_fl(const char *file
, int line
, const char *category
,
629 const char *label
, const struct repository
*repo
)
631 trace2_region_leave_printf_va_fl(file
, line
, category
, label
, repo
,
635 void trace2_region_leave_printf_fl(const char *file
, int line
,
636 const char *category
, const char *label
,
637 const struct repository
*repo
,
638 const char *fmt
, ...)
643 trace2_region_leave_printf_va_fl(file
, line
, category
, label
, repo
, fmt
,
648 #ifndef HAVE_VARIADIC_MACROS
649 void trace2_region_leave_printf(const char *category
, const char *label
,
650 const struct repository
*repo
, const char *fmt
,
656 trace2_region_leave_printf_va_fl(NULL
, 0, category
, label
, repo
, fmt
,
662 void trace2_data_string_fl(const char *file
, int line
, const char *category
,
663 const struct repository
*repo
, const char *key
,
666 struct tr2_tgt
*tgt_j
;
669 uint64_t us_elapsed_absolute
;
670 uint64_t us_elapsed_region
;
675 us_now
= getnanotime() / 1000;
676 us_elapsed_absolute
= tr2tls_absolute_elapsed(us_now
);
677 us_elapsed_region
= tr2tls_region_elasped_self(us_now
);
679 for_each_wanted_builtin (j
, tgt_j
)
680 if (tgt_j
->pfn_data_fl
)
681 tgt_j
->pfn_data_fl(file
, line
, us_elapsed_absolute
,
682 us_elapsed_region
, category
, repo
,
686 void trace2_data_intmax_fl(const char *file
, int line
, const char *category
,
687 const struct repository
*repo
, const char *key
,
690 struct strbuf buf_string
= STRBUF_INIT
;
695 strbuf_addf(&buf_string
, "%" PRIdMAX
, value
);
696 trace2_data_string_fl(file
, line
, category
, repo
, key
, buf_string
.buf
);
697 strbuf_release(&buf_string
);
700 void trace2_data_json_fl(const char *file
, int line
, const char *category
,
701 const struct repository
*repo
, const char *key
,
702 const struct json_writer
*value
)
704 struct tr2_tgt
*tgt_j
;
707 uint64_t us_elapsed_absolute
;
708 uint64_t us_elapsed_region
;
713 us_now
= getnanotime() / 1000;
714 us_elapsed_absolute
= tr2tls_absolute_elapsed(us_now
);
715 us_elapsed_region
= tr2tls_region_elasped_self(us_now
);
717 for_each_wanted_builtin (j
, tgt_j
)
718 if (tgt_j
->pfn_data_fl
)
719 tgt_j
->pfn_data_json_fl(file
, line
, us_elapsed_absolute
,
720 us_elapsed_region
, category
,
724 void trace2_printf_va_fl(const char *file
, int line
, const char *fmt
,
727 struct tr2_tgt
*tgt_j
;
730 uint64_t us_elapsed_absolute
;
735 us_now
= getnanotime() / 1000;
736 us_elapsed_absolute
= tr2tls_absolute_elapsed(us_now
);
739 * We expect each target function to treat 'ap' as constant
742 for_each_wanted_builtin (j
, tgt_j
)
743 if (tgt_j
->pfn_printf_va_fl
)
744 tgt_j
->pfn_printf_va_fl(file
, line
, us_elapsed_absolute
,
748 void trace2_printf_fl(const char *file
, int line
, const char *fmt
, ...)
753 trace2_printf_va_fl(file
, line
, fmt
, ap
);
757 #ifndef HAVE_VARIADIC_MACROS
758 void trace2_printf(const char *fmt
, ...)
763 trace2_printf_va_fl(NULL
, 0, fmt
, ap
);