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_ctr.h"
12 #include "trace2/tr2_dst.h"
13 #include "trace2/tr2_sid.h"
14 #include "trace2/tr2_sysenv.h"
15 #include "trace2/tr2_tgt.h"
16 #include "trace2/tr2_tls.h"
17 #include "trace2/tr2_tmr.h"
19 static int trace2_enabled
;
21 static int tr2_next_child_id
; /* modify under lock */
22 static int tr2_next_exec_id
; /* modify under lock */
23 static int tr2_next_repo_id
= 1; /* modify under lock. zero is reserved */
26 * A table of the builtin TRACE2 targets. Each of these may be independently
27 * enabled or disabled. Each TRACE2 API method will try to write an event to
28 * *each* of the enabled targets.
30 /* clang-format off */
31 static struct tr2_tgt
*tr2_tgt_builtins
[] =
40 /* clang-format off */
41 #define for_each_builtin(j, tgt_j) \
42 for (j = 0, tgt_j = tr2_tgt_builtins[j]; \
44 j++, tgt_j = tr2_tgt_builtins[j])
47 /* clang-format off */
48 #define for_each_wanted_builtin(j, tgt_j) \
49 for_each_builtin(j, tgt_j) \
50 if (tr2_dst_trace_want(tgt_j->pdst))
54 * Force (rather than lazily) initialize any of the requested
55 * builtin TRACE2 targets at startup (and before we've seen an
56 * actual TRACE2 event call) so we can see if we need to setup
57 * private data structures and thread-local storage.
59 * Return the number of builtin targets enabled.
61 static int tr2_tgt_want_builtins(void)
63 struct tr2_tgt
*tgt_j
;
67 for_each_builtin (j
, tgt_j
)
68 if (tgt_j
->pfn_init())
75 * Properly terminate each builtin target. Give each target
76 * a chance to write a summary event and/or flush if necessary
77 * and then close the fd.
79 static void tr2_tgt_disable_builtins(void)
81 struct tr2_tgt
*tgt_j
;
84 for_each_builtin (j
, tgt_j
)
89 * The signature of this function must match the pfn_timer
90 * method in the targets. (Think of this is an apply operation
91 * across the set of active targets.)
93 static void tr2_tgt_emit_a_timer(const struct tr2_timer_metadata
*meta
,
94 const struct tr2_timer
*timer
,
97 struct tr2_tgt
*tgt_j
;
100 for_each_wanted_builtin (j
, tgt_j
)
101 if (tgt_j
->pfn_timer
)
102 tgt_j
->pfn_timer(meta
, timer
, is_final_data
);
106 * The signature of this function must match the pfn_counter
107 * method in the targets.
109 static void tr2_tgt_emit_a_counter(const struct tr2_counter_metadata
*meta
,
110 const struct tr2_counter
*counter
,
113 struct tr2_tgt
*tgt_j
;
116 for_each_wanted_builtin (j
, tgt_j
)
117 if (tgt_j
->pfn_counter
)
118 tgt_j
->pfn_counter(meta
, counter
, is_final_data
);
121 static int tr2main_exit_code
;
124 * Our atexit routine should run after everything has finished.
126 * Note that events generated here might not actually appear if
127 * we are writing to fd 1 or 2 and our atexit routine runs after
128 * the pager's atexit routine (since it closes them to shutdown
131 static void tr2main_atexit_handler(void)
133 struct tr2_tgt
*tgt_j
;
136 uint64_t us_elapsed_absolute
;
138 us_now
= getnanotime() / 1000;
139 us_elapsed_absolute
= tr2tls_absolute_elapsed(us_now
);
142 * Clear any unbalanced regions so that our atexit message
143 * does not appear nested. This improves the appearance of
144 * the trace output if someone calls die(), for example.
146 tr2tls_pop_unwind_self();
149 * Some timers want per-thread details. If the main thread
150 * used one of those timers, emit the details now (before
151 * we emit the aggregate timer values).
153 * Likewise for counters.
155 tr2_emit_per_thread_timers(tr2_tgt_emit_a_timer
);
156 tr2_emit_per_thread_counters(tr2_tgt_emit_a_counter
);
159 * Add stopwatch timer and counter data for the main thread to
160 * the final totals. And then emit the final values.
162 * Technically, we shouldn't need to hold the lock to update
163 * and output the final_timer_block and final_counter_block
164 * (since all other threads should be dead by now), but it
165 * doesn't hurt anything.
168 tr2_update_final_timers();
169 tr2_update_final_counters();
170 tr2_emit_final_timers(tr2_tgt_emit_a_timer
);
171 tr2_emit_final_counters(tr2_tgt_emit_a_counter
);
174 for_each_wanted_builtin (j
, tgt_j
)
175 if (tgt_j
->pfn_atexit
)
176 tgt_j
->pfn_atexit(us_elapsed_absolute
,
179 tr2_tgt_disable_builtins();
183 tr2_cmd_name_release();
184 tr2_cfg_free_patterns();
185 tr2_cfg_free_env_vars();
186 tr2_sysenv_release();
191 static void tr2main_signal_handler(int signo
)
193 struct tr2_tgt
*tgt_j
;
196 uint64_t us_elapsed_absolute
;
198 us_now
= getnanotime() / 1000;
199 us_elapsed_absolute
= tr2tls_absolute_elapsed(us_now
);
201 for_each_wanted_builtin (j
, tgt_j
)
202 if (tgt_j
->pfn_signal
)
203 tgt_j
->pfn_signal(us_elapsed_absolute
, signo
);
209 void trace2_initialize_clock(void)
211 tr2tls_start_process_clock();
214 void trace2_initialize_fl(const char *file
, int line
)
216 struct tr2_tgt
*tgt_j
;
224 if (!tr2_tgt_want_builtins())
230 atexit(tr2main_atexit_handler
);
231 sigchain_push(SIGPIPE
, tr2main_signal_handler
);
235 * Emit 'version' message on each active builtin target.
237 for_each_wanted_builtin (j
, tgt_j
)
238 if (tgt_j
->pfn_version_fl
)
239 tgt_j
->pfn_version_fl(file
, line
);
242 int trace2_is_enabled(void)
244 return trace2_enabled
;
247 void trace2_cmd_start_fl(const char *file
, int line
, const char **argv
)
249 struct tr2_tgt
*tgt_j
;
252 uint64_t us_elapsed_absolute
;
257 us_now
= getnanotime() / 1000;
258 us_elapsed_absolute
= tr2tls_absolute_elapsed(us_now
);
260 for_each_wanted_builtin (j
, tgt_j
)
261 if (tgt_j
->pfn_start_fl
)
262 tgt_j
->pfn_start_fl(file
, line
, us_elapsed_absolute
,
266 void trace2_cmd_exit_fl(const char *file
, int line
, int code
)
268 struct tr2_tgt
*tgt_j
;
271 uint64_t us_elapsed_absolute
;
276 trace_git_fsync_stats();
277 trace2_collect_process_info(TRACE2_PROCESS_INFO_EXIT
);
279 tr2main_exit_code
= code
;
281 us_now
= getnanotime() / 1000;
282 us_elapsed_absolute
= tr2tls_absolute_elapsed(us_now
);
284 for_each_wanted_builtin (j
, tgt_j
)
285 if (tgt_j
->pfn_exit_fl
)
286 tgt_j
->pfn_exit_fl(file
, line
, us_elapsed_absolute
,
290 void trace2_cmd_error_va_fl(const char *file
, int line
, const char *fmt
,
293 struct tr2_tgt
*tgt_j
;
300 * We expect each target function to treat 'ap' as constant
301 * and use va_copy (because an 'ap' can only be walked once).
303 for_each_wanted_builtin (j
, tgt_j
)
304 if (tgt_j
->pfn_error_va_fl
)
305 tgt_j
->pfn_error_va_fl(file
, line
, fmt
, ap
);
308 void trace2_cmd_path_fl(const char *file
, int line
, const char *pathname
)
310 struct tr2_tgt
*tgt_j
;
316 for_each_wanted_builtin (j
, tgt_j
)
317 if (tgt_j
->pfn_command_path_fl
)
318 tgt_j
->pfn_command_path_fl(file
, line
, pathname
);
321 void trace2_cmd_ancestry_fl(const char *file
, int line
, const char **parent_names
)
323 struct tr2_tgt
*tgt_j
;
329 for_each_wanted_builtin (j
, tgt_j
)
330 if (tgt_j
->pfn_command_ancestry_fl
)
331 tgt_j
->pfn_command_ancestry_fl(file
, line
, parent_names
);
334 void trace2_cmd_name_fl(const char *file
, int line
, const char *name
)
336 struct tr2_tgt
*tgt_j
;
337 const char *hierarchy
;
343 tr2_cmd_name_append_hierarchy(name
);
344 hierarchy
= tr2_cmd_name_get_hierarchy();
346 for_each_wanted_builtin (j
, tgt_j
)
347 if (tgt_j
->pfn_command_name_fl
)
348 tgt_j
->pfn_command_name_fl(file
, line
, name
, hierarchy
);
351 void trace2_cmd_mode_fl(const char *file
, int line
, const char *mode
)
353 struct tr2_tgt
*tgt_j
;
359 for_each_wanted_builtin (j
, tgt_j
)
360 if (tgt_j
->pfn_command_mode_fl
)
361 tgt_j
->pfn_command_mode_fl(file
, line
, mode
);
364 void trace2_cmd_alias_fl(const char *file
, int line
, const char *alias
,
367 struct tr2_tgt
*tgt_j
;
373 for_each_wanted_builtin (j
, tgt_j
)
374 if (tgt_j
->pfn_alias_fl
)
375 tgt_j
->pfn_alias_fl(file
, line
, alias
, argv
);
378 void trace2_cmd_list_config_fl(const char *file
, int line
)
383 tr2_cfg_list_config_fl(file
, line
);
386 void trace2_cmd_list_env_vars_fl(const char *file
, int line
)
391 tr2_list_env_vars_fl(file
, line
);
394 void trace2_cmd_set_config_fl(const char *file
, int line
, const char *key
,
400 tr2_cfg_set_fl(file
, line
, key
, value
);
403 void trace2_child_start_fl(const char *file
, int line
,
404 struct child_process
*cmd
)
406 struct tr2_tgt
*tgt_j
;
409 uint64_t us_elapsed_absolute
;
414 us_now
= getnanotime() / 1000;
415 us_elapsed_absolute
= tr2tls_absolute_elapsed(us_now
);
417 cmd
->trace2_child_id
= tr2tls_locked_increment(&tr2_next_child_id
);
418 cmd
->trace2_child_us_start
= us_now
;
420 for_each_wanted_builtin (j
, tgt_j
)
421 if (tgt_j
->pfn_child_start_fl
)
422 tgt_j
->pfn_child_start_fl(file
, line
,
423 us_elapsed_absolute
, cmd
);
426 void trace2_child_exit_fl(const char *file
, int line
, struct child_process
*cmd
,
429 struct tr2_tgt
*tgt_j
;
432 uint64_t us_elapsed_absolute
;
433 uint64_t us_elapsed_child
;
438 us_now
= getnanotime() / 1000;
439 us_elapsed_absolute
= tr2tls_absolute_elapsed(us_now
);
441 if (cmd
->trace2_child_us_start
)
442 us_elapsed_child
= us_now
- cmd
->trace2_child_us_start
;
444 us_elapsed_child
= 0;
446 for_each_wanted_builtin (j
, tgt_j
)
447 if (tgt_j
->pfn_child_exit_fl
)
448 tgt_j
->pfn_child_exit_fl(file
, line
,
450 cmd
->trace2_child_id
, cmd
->pid
,
455 void trace2_child_ready_fl(const char *file
, int line
,
456 struct child_process
*cmd
,
459 struct tr2_tgt
*tgt_j
;
462 uint64_t us_elapsed_absolute
;
463 uint64_t us_elapsed_child
;
468 us_now
= getnanotime() / 1000;
469 us_elapsed_absolute
= tr2tls_absolute_elapsed(us_now
);
471 if (cmd
->trace2_child_us_start
)
472 us_elapsed_child
= us_now
- cmd
->trace2_child_us_start
;
474 us_elapsed_child
= 0;
476 for_each_wanted_builtin (j
, tgt_j
)
477 if (tgt_j
->pfn_child_ready_fl
)
478 tgt_j
->pfn_child_ready_fl(file
, line
,
480 cmd
->trace2_child_id
,
486 int trace2_exec_fl(const char *file
, int line
, const char *exe
,
489 struct tr2_tgt
*tgt_j
;
493 uint64_t us_elapsed_absolute
;
498 us_now
= getnanotime() / 1000;
499 us_elapsed_absolute
= tr2tls_absolute_elapsed(us_now
);
501 exec_id
= tr2tls_locked_increment(&tr2_next_exec_id
);
503 for_each_wanted_builtin (j
, tgt_j
)
504 if (tgt_j
->pfn_exec_fl
)
505 tgt_j
->pfn_exec_fl(file
, line
, us_elapsed_absolute
,
511 void trace2_exec_result_fl(const char *file
, int line
, int exec_id
, int code
)
513 struct tr2_tgt
*tgt_j
;
516 uint64_t us_elapsed_absolute
;
521 us_now
= getnanotime() / 1000;
522 us_elapsed_absolute
= tr2tls_absolute_elapsed(us_now
);
524 for_each_wanted_builtin (j
, tgt_j
)
525 if (tgt_j
->pfn_exec_result_fl
)
526 tgt_j
->pfn_exec_result_fl(
527 file
, line
, us_elapsed_absolute
, exec_id
, code
);
530 void trace2_thread_start_fl(const char *file
, int line
, const char *thread_base_name
)
532 struct tr2_tgt
*tgt_j
;
535 uint64_t us_elapsed_absolute
;
540 if (tr2tls_is_main_thread()) {
542 * We should only be called from the new thread's thread-proc,
543 * so this is technically a bug. But in those cases where the
544 * main thread also runs the thread-proc function (or when we
545 * are built with threading disabled), we need to allow it.
547 * Convert this call to a region-enter so the nesting looks
550 trace2_region_enter_printf_fl(file
, line
, NULL
, NULL
, NULL
,
551 "thread-proc on main: %s",
556 us_now
= getnanotime() / 1000;
557 us_elapsed_absolute
= tr2tls_absolute_elapsed(us_now
);
559 tr2tls_create_self(thread_base_name
, us_now
);
561 for_each_wanted_builtin (j
, tgt_j
)
562 if (tgt_j
->pfn_thread_start_fl
)
563 tgt_j
->pfn_thread_start_fl(file
, line
,
564 us_elapsed_absolute
);
567 void trace2_thread_exit_fl(const char *file
, int line
)
569 struct tr2_tgt
*tgt_j
;
572 uint64_t us_elapsed_absolute
;
573 uint64_t us_elapsed_thread
;
578 if (tr2tls_is_main_thread()) {
580 * We should only be called from the exiting thread's
581 * thread-proc, so this is technically a bug. But in
582 * those cases where the main thread also runs the
583 * thread-proc function (or when we are built with
584 * threading disabled), we need to allow it.
586 * Convert this call to a region-leave so the nesting
589 trace2_region_leave_printf_fl(file
, line
, NULL
, NULL
, NULL
,
590 "thread-proc on main");
594 us_now
= getnanotime() / 1000;
595 us_elapsed_absolute
= tr2tls_absolute_elapsed(us_now
);
598 * Clear any unbalanced regions and then get the relative time
599 * for the outer-most region (which we pushed when the thread
600 * started). This gives us the run time of the thread.
602 tr2tls_pop_unwind_self();
603 us_elapsed_thread
= tr2tls_region_elasped_self(us_now
);
606 * Some timers want per-thread details. If this thread used
607 * one of those timers, emit the details now.
609 * Likewise for counters.
611 tr2_emit_per_thread_timers(tr2_tgt_emit_a_timer
);
612 tr2_emit_per_thread_counters(tr2_tgt_emit_a_counter
);
615 * Add stopwatch timer and counter data from the current
616 * (non-main) thread to the final totals. (We'll accumulate
617 * data for the main thread later during "atexit".)
620 tr2_update_final_timers();
621 tr2_update_final_counters();
624 for_each_wanted_builtin (j
, tgt_j
)
625 if (tgt_j
->pfn_thread_exit_fl
)
626 tgt_j
->pfn_thread_exit_fl(file
, line
,
633 void trace2_def_param_fl(const char *file
, int line
, const char *param
,
636 struct tr2_tgt
*tgt_j
;
642 for_each_wanted_builtin (j
, tgt_j
)
643 if (tgt_j
->pfn_param_fl
)
644 tgt_j
->pfn_param_fl(file
, line
, param
, value
);
647 void trace2_def_repo_fl(const char *file
, int line
, struct repository
*repo
)
649 struct tr2_tgt
*tgt_j
;
655 if (repo
->trace2_repo_id
)
658 repo
->trace2_repo_id
= tr2tls_locked_increment(&tr2_next_repo_id
);
660 for_each_wanted_builtin (j
, tgt_j
)
661 if (tgt_j
->pfn_repo_fl
)
662 tgt_j
->pfn_repo_fl(file
, line
, repo
);
665 void trace2_region_enter_printf_va_fl(const char *file
, int line
,
666 const char *category
, const char *label
,
667 const struct repository
*repo
,
668 const char *fmt
, va_list ap
)
670 struct tr2_tgt
*tgt_j
;
673 uint64_t us_elapsed_absolute
;
678 us_now
= getnanotime() / 1000;
679 us_elapsed_absolute
= tr2tls_absolute_elapsed(us_now
);
682 * Print the region-enter message at the current nesting
683 * (indentation) level and then push a new level.
685 * We expect each target function to treat 'ap' as constant
688 for_each_wanted_builtin (j
, tgt_j
)
689 if (tgt_j
->pfn_region_enter_printf_va_fl
)
690 tgt_j
->pfn_region_enter_printf_va_fl(
691 file
, line
, us_elapsed_absolute
, category
,
692 label
, repo
, fmt
, ap
);
694 tr2tls_push_self(us_now
);
697 void trace2_region_enter_fl(const char *file
, int line
, const char *category
,
698 const char *label
, const struct repository
*repo
, ...)
702 trace2_region_enter_printf_va_fl(file
, line
, category
, label
, repo
,
708 void trace2_region_enter_printf_fl(const char *file
, int line
,
709 const char *category
, const char *label
,
710 const struct repository
*repo
,
711 const char *fmt
, ...)
716 trace2_region_enter_printf_va_fl(file
, line
, category
, label
, repo
, fmt
,
721 void trace2_region_leave_printf_va_fl(const char *file
, int line
,
722 const char *category
, const char *label
,
723 const struct repository
*repo
,
724 const char *fmt
, va_list ap
)
726 struct tr2_tgt
*tgt_j
;
729 uint64_t us_elapsed_absolute
;
730 uint64_t us_elapsed_region
;
735 us_now
= getnanotime() / 1000;
736 us_elapsed_absolute
= tr2tls_absolute_elapsed(us_now
);
739 * Get the elapsed time in the current region before we
740 * pop it off the stack. Pop the stack. And then print
741 * the perf message at the new (shallower) level so that
742 * it lines up with the corresponding push/enter.
744 us_elapsed_region
= tr2tls_region_elasped_self(us_now
);
749 * We expect each target function to treat 'ap' as constant
752 for_each_wanted_builtin (j
, tgt_j
)
753 if (tgt_j
->pfn_region_leave_printf_va_fl
)
754 tgt_j
->pfn_region_leave_printf_va_fl(
755 file
, line
, us_elapsed_absolute
,
756 us_elapsed_region
, category
, label
, repo
, fmt
,
760 void trace2_region_leave_fl(const char *file
, int line
, const char *category
,
761 const char *label
, const struct repository
*repo
, ...)
765 trace2_region_leave_printf_va_fl(file
, line
, category
, label
, repo
,
770 void trace2_region_leave_printf_fl(const char *file
, int line
,
771 const char *category
, const char *label
,
772 const struct repository
*repo
,
773 const char *fmt
, ...)
778 trace2_region_leave_printf_va_fl(file
, line
, category
, label
, repo
, fmt
,
783 void trace2_data_string_fl(const char *file
, int line
, const char *category
,
784 const struct repository
*repo
, const char *key
,
787 struct tr2_tgt
*tgt_j
;
790 uint64_t us_elapsed_absolute
;
791 uint64_t us_elapsed_region
;
796 us_now
= getnanotime() / 1000;
797 us_elapsed_absolute
= tr2tls_absolute_elapsed(us_now
);
798 us_elapsed_region
= tr2tls_region_elasped_self(us_now
);
800 for_each_wanted_builtin (j
, tgt_j
)
801 if (tgt_j
->pfn_data_fl
)
802 tgt_j
->pfn_data_fl(file
, line
, us_elapsed_absolute
,
803 us_elapsed_region
, category
, repo
,
807 void trace2_data_intmax_fl(const char *file
, int line
, const char *category
,
808 const struct repository
*repo
, const char *key
,
811 struct strbuf buf_string
= STRBUF_INIT
;
816 strbuf_addf(&buf_string
, "%" PRIdMAX
, value
);
817 trace2_data_string_fl(file
, line
, category
, repo
, key
, buf_string
.buf
);
818 strbuf_release(&buf_string
);
821 void trace2_data_json_fl(const char *file
, int line
, const char *category
,
822 const struct repository
*repo
, const char *key
,
823 const struct json_writer
*value
)
825 struct tr2_tgt
*tgt_j
;
828 uint64_t us_elapsed_absolute
;
829 uint64_t us_elapsed_region
;
834 us_now
= getnanotime() / 1000;
835 us_elapsed_absolute
= tr2tls_absolute_elapsed(us_now
);
836 us_elapsed_region
= tr2tls_region_elasped_self(us_now
);
838 for_each_wanted_builtin (j
, tgt_j
)
839 if (tgt_j
->pfn_data_json_fl
)
840 tgt_j
->pfn_data_json_fl(file
, line
, us_elapsed_absolute
,
841 us_elapsed_region
, category
,
845 void trace2_printf_va_fl(const char *file
, int line
, const char *fmt
,
848 struct tr2_tgt
*tgt_j
;
851 uint64_t us_elapsed_absolute
;
856 us_now
= getnanotime() / 1000;
857 us_elapsed_absolute
= tr2tls_absolute_elapsed(us_now
);
860 * We expect each target function to treat 'ap' as constant
863 for_each_wanted_builtin (j
, tgt_j
)
864 if (tgt_j
->pfn_printf_va_fl
)
865 tgt_j
->pfn_printf_va_fl(file
, line
, us_elapsed_absolute
,
869 void trace2_printf_fl(const char *file
, int line
, const char *fmt
, ...)
874 trace2_printf_va_fl(file
, line
, fmt
, ap
);
878 void trace2_timer_start(enum trace2_timer_id tid
)
883 if (tid
< 0 || tid
>= TRACE2_NUMBER_OF_TIMERS
)
884 BUG("trace2_timer_start: invalid timer id: %d", tid
);
886 tr2_start_timer(tid
);
889 void trace2_timer_stop(enum trace2_timer_id tid
)
894 if (tid
< 0 || tid
>= TRACE2_NUMBER_OF_TIMERS
)
895 BUG("trace2_timer_stop: invalid timer id: %d", tid
);
900 void trace2_counter_add(enum trace2_counter_id cid
, uint64_t value
)
905 if (cid
< 0 || cid
>= TRACE2_NUMBER_OF_COUNTERS
)
906 BUG("trace2_counter_add: invalid counter id: %d", cid
);
908 tr2_counter_increment(cid
, value
);
911 const char *trace2_session_id(void)
913 return tr2_sid_get();