1 #define DISABLE_SIGN_COMPARE_WARNINGS
3 #include "git-compat-util.h"
5 #include "repository.h"
6 #include "run-command.h"
8 #include "thread-utils.h"
11 #include "trace2/tr2_cfg.h"
12 #include "trace2/tr2_cmd_name.h"
13 #include "trace2/tr2_ctr.h"
14 #include "trace2/tr2_dst.h"
15 #include "trace2/tr2_sid.h"
16 #include "trace2/tr2_sysenv.h"
17 #include "trace2/tr2_tgt.h"
18 #include "trace2/tr2_tls.h"
19 #include "trace2/tr2_tmr.h"
21 static int trace2_enabled
;
22 static int trace2_redact
= 1;
24 static int tr2_next_child_id
; /* modify under lock */
25 static int tr2_next_exec_id
; /* modify under lock */
26 static int tr2_next_repo_id
= 1; /* modify under lock. zero is reserved */
29 * A table of the builtin TRACE2 targets. Each of these may be independently
30 * enabled or disabled. Each TRACE2 API method will try to write an event to
31 * *each* of the enabled targets.
33 /* clang-format off */
34 static struct tr2_tgt
*tr2_tgt_builtins
[] =
43 /* clang-format off */
44 #define for_each_builtin(j, tgt_j) \
45 for (j = 0, tgt_j = tr2_tgt_builtins[j]; \
47 j++, tgt_j = tr2_tgt_builtins[j])
50 /* clang-format off */
51 #define for_each_wanted_builtin(j, tgt_j) \
52 for_each_builtin(j, tgt_j) \
53 if (tr2_dst_trace_want(tgt_j->pdst))
57 * Force (rather than lazily) initialize any of the requested
58 * builtin TRACE2 targets at startup (and before we've seen an
59 * actual TRACE2 event call) so we can see if we need to setup
60 * private data structures and thread-local storage.
62 * Return the number of builtin targets enabled.
64 static int tr2_tgt_want_builtins(void)
66 struct tr2_tgt
*tgt_j
;
70 for_each_builtin (j
, tgt_j
)
71 if (tgt_j
->pfn_init())
78 * Properly terminate each builtin target. Give each target
79 * a chance to write a summary event and/or flush if necessary
80 * and then close the fd.
82 static void tr2_tgt_disable_builtins(void)
84 struct tr2_tgt
*tgt_j
;
87 for_each_builtin (j
, tgt_j
)
92 * The signature of this function must match the pfn_timer
93 * method in the targets. (Think of this is an apply operation
94 * across the set of active targets.)
96 static void tr2_tgt_emit_a_timer(const struct tr2_timer_metadata
*meta
,
97 const struct tr2_timer
*timer
,
100 struct tr2_tgt
*tgt_j
;
103 for_each_wanted_builtin (j
, tgt_j
)
104 if (tgt_j
->pfn_timer
)
105 tgt_j
->pfn_timer(meta
, timer
, is_final_data
);
109 * The signature of this function must match the pfn_counter
110 * method in the targets.
112 static void tr2_tgt_emit_a_counter(const struct tr2_counter_metadata
*meta
,
113 const struct tr2_counter
*counter
,
116 struct tr2_tgt
*tgt_j
;
119 for_each_wanted_builtin (j
, tgt_j
)
120 if (tgt_j
->pfn_counter
)
121 tgt_j
->pfn_counter(meta
, counter
, is_final_data
);
124 static int tr2main_exit_code
;
127 * Our atexit routine should run after everything has finished.
129 * Note that events generated here might not actually appear if
130 * we are writing to fd 1 or 2 and our atexit routine runs after
131 * the pager's atexit routine (since it closes them to shutdown
134 static void tr2main_atexit_handler(void)
136 struct tr2_tgt
*tgt_j
;
139 uint64_t us_elapsed_absolute
;
141 us_now
= getnanotime() / 1000;
142 us_elapsed_absolute
= tr2tls_absolute_elapsed(us_now
);
145 * Clear any unbalanced regions so that our atexit message
146 * does not appear nested. This improves the appearance of
147 * the trace output if someone calls die(), for example.
149 tr2tls_pop_unwind_self();
152 * Some timers want per-thread details. If the main thread
153 * used one of those timers, emit the details now (before
154 * we emit the aggregate timer values).
156 * Likewise for counters.
158 tr2_emit_per_thread_timers(tr2_tgt_emit_a_timer
);
159 tr2_emit_per_thread_counters(tr2_tgt_emit_a_counter
);
162 * Add stopwatch timer and counter data for the main thread to
163 * the final totals. And then emit the final values.
165 * Technically, we shouldn't need to hold the lock to update
166 * and output the final_timer_block and final_counter_block
167 * (since all other threads should be dead by now), but it
168 * doesn't hurt anything.
171 tr2_update_final_timers();
172 tr2_update_final_counters();
173 tr2_emit_final_timers(tr2_tgt_emit_a_timer
);
174 tr2_emit_final_counters(tr2_tgt_emit_a_counter
);
177 for_each_wanted_builtin (j
, tgt_j
)
178 if (tgt_j
->pfn_atexit
)
179 tgt_j
->pfn_atexit(us_elapsed_absolute
,
182 tr2_tgt_disable_builtins();
186 tr2_cmd_name_release();
187 tr2_cfg_free_patterns();
188 tr2_cfg_free_env_vars();
189 tr2_sysenv_release();
194 static void tr2main_signal_handler(int signo
)
196 struct tr2_tgt
*tgt_j
;
199 uint64_t us_elapsed_absolute
;
201 us_now
= getnanotime() / 1000;
202 us_elapsed_absolute
= tr2tls_absolute_elapsed(us_now
);
204 for_each_wanted_builtin (j
, tgt_j
)
205 if (tgt_j
->pfn_signal
)
206 tgt_j
->pfn_signal(us_elapsed_absolute
, signo
);
212 void trace2_initialize_clock(void)
214 tr2tls_start_process_clock();
217 void trace2_initialize_fl(const char *file
, int line
)
219 struct tr2_tgt
*tgt_j
;
227 if (!tr2_tgt_want_builtins())
230 if (!git_env_bool("GIT_TRACE2_REDACT", 1))
235 atexit(tr2main_atexit_handler
);
236 sigchain_push(SIGPIPE
, tr2main_signal_handler
);
240 * Emit 'version' message on each active builtin target.
242 for_each_wanted_builtin (j
, tgt_j
)
243 if (tgt_j
->pfn_version_fl
)
244 tgt_j
->pfn_version_fl(file
, line
);
247 int trace2_is_enabled(void)
249 return trace2_enabled
;
253 * Redacts an argument, i.e. ensures that no password in
254 * https://user:password@host/-style URLs is logged.
256 * Returns the original if nothing needed to be redacted.
257 * Returns a pointer that needs to be `free()`d otherwise.
259 static const char *redact_arg(const char *arg
)
261 const char *p
, *colon
;
264 if (!trace2_redact
||
265 (!skip_prefix(arg
, "https://", &p
) &&
266 !skip_prefix(arg
, "http://", &p
)))
269 at
= strcspn(p
, "@/");
273 colon
= memchr(p
, ':', at
);
277 return xstrfmt("%.*s:<REDACTED>%s", (int)(colon
- arg
), arg
, p
+ at
);
281 * Redacts arguments in an argument list.
283 * Returns the original if nothing needed to be redacted.
284 * Otherwise, returns a new array that needs to be released
285 * via `free_redacted_argv()`.
287 static const char **redact_argv(const char **argv
)
290 const char *redacted
= NULL
;
296 for (i
= 0; argv
[i
]; i
++)
297 if ((redacted
= redact_arg(argv
[i
])) != argv
[i
])
303 for (j
= 0; argv
[j
]; j
++)
304 ; /* keep counting */
306 ALLOC_ARRAY(ret
, j
+ 1);
309 for (j
= 0; j
< i
; j
++)
312 for (++i
; argv
[i
]; i
++) {
313 redacted
= redact_arg(argv
[i
]);
314 ret
[i
] = redacted
? redacted
: argv
[i
];
320 static void free_redacted_argv(const char **redacted
, const char **argv
)
324 if (redacted
!= argv
) {
325 for (i
= 0; argv
[i
]; i
++)
326 if (redacted
[i
] != argv
[i
])
327 free((void *)redacted
[i
]);
328 free((void *)redacted
);
332 void trace2_cmd_start_fl(const char *file
, int line
, const char **argv
)
334 struct tr2_tgt
*tgt_j
;
337 uint64_t us_elapsed_absolute
;
338 const char **redacted
;
343 us_now
= getnanotime() / 1000;
344 us_elapsed_absolute
= tr2tls_absolute_elapsed(us_now
);
346 redacted
= redact_argv(argv
);
348 for_each_wanted_builtin (j
, tgt_j
)
349 if (tgt_j
->pfn_start_fl
)
350 tgt_j
->pfn_start_fl(file
, line
, us_elapsed_absolute
,
353 free_redacted_argv(redacted
, argv
);
356 void trace2_cmd_exit_fl(const char *file
, int line
, int code
)
358 struct tr2_tgt
*tgt_j
;
361 uint64_t us_elapsed_absolute
;
366 trace2_collect_process_info(TRACE2_PROCESS_INFO_EXIT
);
368 tr2main_exit_code
= code
;
370 us_now
= getnanotime() / 1000;
371 us_elapsed_absolute
= tr2tls_absolute_elapsed(us_now
);
373 for_each_wanted_builtin (j
, tgt_j
)
374 if (tgt_j
->pfn_exit_fl
)
375 tgt_j
->pfn_exit_fl(file
, line
, us_elapsed_absolute
,
379 void trace2_cmd_error_va_fl(const char *file
, int line
, const char *fmt
,
382 struct tr2_tgt
*tgt_j
;
389 * We expect each target function to treat 'ap' as constant
390 * and use va_copy (because an 'ap' can only be walked once).
392 for_each_wanted_builtin (j
, tgt_j
)
393 if (tgt_j
->pfn_error_va_fl
)
394 tgt_j
->pfn_error_va_fl(file
, line
, fmt
, ap
);
397 void trace2_cmd_path_fl(const char *file
, int line
, const char *pathname
)
399 struct tr2_tgt
*tgt_j
;
405 for_each_wanted_builtin (j
, tgt_j
)
406 if (tgt_j
->pfn_command_path_fl
)
407 tgt_j
->pfn_command_path_fl(file
, line
, pathname
);
410 void trace2_cmd_ancestry_fl(const char *file
, int line
, const char **parent_names
)
412 struct tr2_tgt
*tgt_j
;
418 for_each_wanted_builtin (j
, tgt_j
)
419 if (tgt_j
->pfn_command_ancestry_fl
)
420 tgt_j
->pfn_command_ancestry_fl(file
, line
, parent_names
);
423 void trace2_cmd_name_fl(const char *file
, int line
, const char *name
)
425 struct tr2_tgt
*tgt_j
;
426 const char *hierarchy
;
432 tr2_cmd_name_append_hierarchy(name
);
433 hierarchy
= tr2_cmd_name_get_hierarchy();
435 for_each_wanted_builtin (j
, tgt_j
)
436 if (tgt_j
->pfn_command_name_fl
)
437 tgt_j
->pfn_command_name_fl(file
, line
, name
, hierarchy
);
439 trace2_cmd_list_config();
440 trace2_cmd_list_env_vars();
443 void trace2_cmd_mode_fl(const char *file
, int line
, const char *mode
)
445 struct tr2_tgt
*tgt_j
;
451 for_each_wanted_builtin (j
, tgt_j
)
452 if (tgt_j
->pfn_command_mode_fl
)
453 tgt_j
->pfn_command_mode_fl(file
, line
, mode
);
456 void trace2_cmd_alias_fl(const char *file
, int line
, const char *alias
,
459 struct tr2_tgt
*tgt_j
;
465 for_each_wanted_builtin (j
, tgt_j
)
466 if (tgt_j
->pfn_alias_fl
)
467 tgt_j
->pfn_alias_fl(file
, line
, alias
, argv
);
470 void trace2_cmd_list_config_fl(const char *file
, int line
)
472 static int emitted
= 0;
481 tr2_cfg_list_config_fl(file
, line
);
484 void trace2_cmd_list_env_vars_fl(const char *file
, int line
)
486 static int emitted
= 0;
495 tr2_list_env_vars_fl(file
, line
);
498 void trace2_cmd_set_config_fl(const char *file
, int line
, const char *key
,
504 tr2_cfg_set_fl(file
, line
, key
, value
);
507 void trace2_child_start_fl(const char *file
, int line
,
508 struct child_process
*cmd
)
510 struct tr2_tgt
*tgt_j
;
513 uint64_t us_elapsed_absolute
;
514 const char **orig_argv
= cmd
->args
.v
;
519 us_now
= getnanotime() / 1000;
520 us_elapsed_absolute
= tr2tls_absolute_elapsed(us_now
);
522 cmd
->trace2_child_id
= tr2tls_locked_increment(&tr2_next_child_id
);
523 cmd
->trace2_child_us_start
= us_now
;
526 * The `pfn_child_start_fl` API takes a `struct child_process`
527 * rather than a simple `argv` for the child because some
528 * targets make use of the additional context bits/values. So
529 * temporarily replace the original argv (inside the `strvec`)
530 * with a possibly redacted version.
532 cmd
->args
.v
= redact_argv(orig_argv
);
534 for_each_wanted_builtin (j
, tgt_j
)
535 if (tgt_j
->pfn_child_start_fl
)
536 tgt_j
->pfn_child_start_fl(file
, line
,
537 us_elapsed_absolute
, cmd
);
539 if (cmd
->args
.v
!= orig_argv
) {
540 free_redacted_argv(cmd
->args
.v
, orig_argv
);
541 cmd
->args
.v
= orig_argv
;
545 void trace2_child_exit_fl(const char *file
, int line
, struct child_process
*cmd
,
548 struct tr2_tgt
*tgt_j
;
551 uint64_t us_elapsed_absolute
;
552 uint64_t us_elapsed_child
;
557 us_now
= getnanotime() / 1000;
558 us_elapsed_absolute
= tr2tls_absolute_elapsed(us_now
);
560 if (cmd
->trace2_child_us_start
)
561 us_elapsed_child
= us_now
- cmd
->trace2_child_us_start
;
563 us_elapsed_child
= 0;
565 for_each_wanted_builtin (j
, tgt_j
)
566 if (tgt_j
->pfn_child_exit_fl
)
567 tgt_j
->pfn_child_exit_fl(file
, line
,
569 cmd
->trace2_child_id
, cmd
->pid
,
574 void trace2_child_ready_fl(const char *file
, int line
,
575 struct child_process
*cmd
,
578 struct tr2_tgt
*tgt_j
;
581 uint64_t us_elapsed_absolute
;
582 uint64_t us_elapsed_child
;
587 us_now
= getnanotime() / 1000;
588 us_elapsed_absolute
= tr2tls_absolute_elapsed(us_now
);
590 if (cmd
->trace2_child_us_start
)
591 us_elapsed_child
= us_now
- cmd
->trace2_child_us_start
;
593 us_elapsed_child
= 0;
595 for_each_wanted_builtin (j
, tgt_j
)
596 if (tgt_j
->pfn_child_ready_fl
)
597 tgt_j
->pfn_child_ready_fl(file
, line
,
599 cmd
->trace2_child_id
,
605 int trace2_exec_fl(const char *file
, int line
, const char *exe
,
608 struct tr2_tgt
*tgt_j
;
612 uint64_t us_elapsed_absolute
;
613 const char **redacted
;
618 us_now
= getnanotime() / 1000;
619 us_elapsed_absolute
= tr2tls_absolute_elapsed(us_now
);
621 exec_id
= tr2tls_locked_increment(&tr2_next_exec_id
);
623 redacted
= redact_argv(argv
);
625 for_each_wanted_builtin (j
, tgt_j
)
626 if (tgt_j
->pfn_exec_fl
)
627 tgt_j
->pfn_exec_fl(file
, line
, us_elapsed_absolute
,
628 exec_id
, exe
, redacted
);
630 free_redacted_argv(redacted
, argv
);
635 void trace2_exec_result_fl(const char *file
, int line
, int exec_id
, int code
)
637 struct tr2_tgt
*tgt_j
;
640 uint64_t us_elapsed_absolute
;
645 us_now
= getnanotime() / 1000;
646 us_elapsed_absolute
= tr2tls_absolute_elapsed(us_now
);
648 for_each_wanted_builtin (j
, tgt_j
)
649 if (tgt_j
->pfn_exec_result_fl
)
650 tgt_j
->pfn_exec_result_fl(
651 file
, line
, us_elapsed_absolute
, exec_id
, code
);
654 void trace2_thread_start_fl(const char *file
, int line
, const char *thread_base_name
)
656 struct tr2_tgt
*tgt_j
;
659 uint64_t us_elapsed_absolute
;
664 if (tr2tls_is_main_thread()) {
666 * We should only be called from the new thread's thread-proc,
667 * so this is technically a bug. But in those cases where the
668 * main thread also runs the thread-proc function (or when we
669 * are built with threading disabled), we need to allow it.
671 * Convert this call to a region-enter so the nesting looks
674 trace2_region_enter_printf_fl(file
, line
, NULL
, NULL
, NULL
,
675 "thread-proc on main: %s",
680 us_now
= getnanotime() / 1000;
681 us_elapsed_absolute
= tr2tls_absolute_elapsed(us_now
);
683 tr2tls_create_self(thread_base_name
, us_now
);
685 for_each_wanted_builtin (j
, tgt_j
)
686 if (tgt_j
->pfn_thread_start_fl
)
687 tgt_j
->pfn_thread_start_fl(file
, line
,
688 us_elapsed_absolute
);
691 void trace2_thread_exit_fl(const char *file
, int line
)
693 struct tr2_tgt
*tgt_j
;
696 uint64_t us_elapsed_absolute
;
697 uint64_t us_elapsed_thread
;
702 if (tr2tls_is_main_thread()) {
704 * We should only be called from the exiting thread's
705 * thread-proc, so this is technically a bug. But in
706 * those cases where the main thread also runs the
707 * thread-proc function (or when we are built with
708 * threading disabled), we need to allow it.
710 * Convert this call to a region-leave so the nesting
713 trace2_region_leave_printf_fl(file
, line
, NULL
, NULL
, NULL
,
714 "thread-proc on main");
718 us_now
= getnanotime() / 1000;
719 us_elapsed_absolute
= tr2tls_absolute_elapsed(us_now
);
722 * Clear any unbalanced regions and then get the relative time
723 * for the outer-most region (which we pushed when the thread
724 * started). This gives us the run time of the thread.
726 tr2tls_pop_unwind_self();
727 us_elapsed_thread
= tr2tls_region_elasped_self(us_now
);
730 * Some timers want per-thread details. If this thread used
731 * one of those timers, emit the details now.
733 * Likewise for counters.
735 tr2_emit_per_thread_timers(tr2_tgt_emit_a_timer
);
736 tr2_emit_per_thread_counters(tr2_tgt_emit_a_counter
);
739 * Add stopwatch timer and counter data from the current
740 * (non-main) thread to the final totals. (We'll accumulate
741 * data for the main thread later during "atexit".)
744 tr2_update_final_timers();
745 tr2_update_final_counters();
748 for_each_wanted_builtin (j
, tgt_j
)
749 if (tgt_j
->pfn_thread_exit_fl
)
750 tgt_j
->pfn_thread_exit_fl(file
, line
,
757 void trace2_def_param_fl(const char *file
, int line
, const char *param
,
758 const char *value
, const struct key_value_info
*kvi
)
760 struct tr2_tgt
*tgt_j
;
762 const char *redacted
;
767 redacted
= value
? redact_arg(value
) : NULL
;
769 for_each_wanted_builtin (j
, tgt_j
)
770 if (tgt_j
->pfn_param_fl
)
771 tgt_j
->pfn_param_fl(file
, line
, param
, redacted
, kvi
);
773 if (redacted
!= value
)
774 free((void *)redacted
);
777 void trace2_def_repo_fl(const char *file
, int line
, struct repository
*repo
)
779 struct tr2_tgt
*tgt_j
;
785 if (repo
->trace2_repo_id
)
788 repo
->trace2_repo_id
= tr2tls_locked_increment(&tr2_next_repo_id
);
790 for_each_wanted_builtin (j
, tgt_j
)
791 if (tgt_j
->pfn_repo_fl
)
792 tgt_j
->pfn_repo_fl(file
, line
, repo
);
795 void trace2_region_enter_printf_va_fl(const char *file
, int line
,
796 const char *category
, const char *label
,
797 const struct repository
*repo
,
798 const char *fmt
, va_list ap
)
800 struct tr2_tgt
*tgt_j
;
803 uint64_t us_elapsed_absolute
;
808 us_now
= getnanotime() / 1000;
809 us_elapsed_absolute
= tr2tls_absolute_elapsed(us_now
);
812 * Print the region-enter message at the current nesting
813 * (indentation) level and then push a new level.
815 * We expect each target function to treat 'ap' as constant
818 for_each_wanted_builtin (j
, tgt_j
)
819 if (tgt_j
->pfn_region_enter_printf_va_fl
)
820 tgt_j
->pfn_region_enter_printf_va_fl(
821 file
, line
, us_elapsed_absolute
, category
,
822 label
, repo
, fmt
, ap
);
824 tr2tls_push_self(us_now
);
827 void trace2_region_enter_fl(const char *file
, int line
, const char *category
,
828 const char *label
, const struct repository
*repo
, ...)
832 trace2_region_enter_printf_va_fl(file
, line
, category
, label
, repo
,
838 void trace2_region_enter_printf_fl(const char *file
, int line
,
839 const char *category
, const char *label
,
840 const struct repository
*repo
,
841 const char *fmt
, ...)
846 trace2_region_enter_printf_va_fl(file
, line
, category
, label
, repo
, fmt
,
851 void trace2_region_leave_printf_va_fl(const char *file
, int line
,
852 const char *category
, const char *label
,
853 const struct repository
*repo
,
854 const char *fmt
, va_list ap
)
856 struct tr2_tgt
*tgt_j
;
859 uint64_t us_elapsed_absolute
;
860 uint64_t us_elapsed_region
;
865 us_now
= getnanotime() / 1000;
866 us_elapsed_absolute
= tr2tls_absolute_elapsed(us_now
);
869 * Get the elapsed time in the current region before we
870 * pop it off the stack. Pop the stack. And then print
871 * the perf message at the new (shallower) level so that
872 * it lines up with the corresponding push/enter.
874 us_elapsed_region
= tr2tls_region_elasped_self(us_now
);
879 * We expect each target function to treat 'ap' as constant
882 for_each_wanted_builtin (j
, tgt_j
)
883 if (tgt_j
->pfn_region_leave_printf_va_fl
)
884 tgt_j
->pfn_region_leave_printf_va_fl(
885 file
, line
, us_elapsed_absolute
,
886 us_elapsed_region
, category
, label
, repo
, fmt
,
890 void trace2_region_leave_fl(const char *file
, int line
, const char *category
,
891 const char *label
, const struct repository
*repo
, ...)
895 trace2_region_leave_printf_va_fl(file
, line
, category
, label
, repo
,
900 void trace2_region_leave_printf_fl(const char *file
, int line
,
901 const char *category
, const char *label
,
902 const struct repository
*repo
,
903 const char *fmt
, ...)
908 trace2_region_leave_printf_va_fl(file
, line
, category
, label
, repo
, fmt
,
913 void trace2_data_string_fl(const char *file
, int line
, const char *category
,
914 const struct repository
*repo
, const char *key
,
917 struct tr2_tgt
*tgt_j
;
920 uint64_t us_elapsed_absolute
;
921 uint64_t us_elapsed_region
;
926 us_now
= getnanotime() / 1000;
927 us_elapsed_absolute
= tr2tls_absolute_elapsed(us_now
);
928 us_elapsed_region
= tr2tls_region_elasped_self(us_now
);
930 for_each_wanted_builtin (j
, tgt_j
)
931 if (tgt_j
->pfn_data_fl
)
932 tgt_j
->pfn_data_fl(file
, line
, us_elapsed_absolute
,
933 us_elapsed_region
, category
, repo
,
937 void trace2_data_intmax_fl(const char *file
, int line
, const char *category
,
938 const struct repository
*repo
, const char *key
,
941 struct strbuf buf_string
= STRBUF_INIT
;
946 strbuf_addf(&buf_string
, "%" PRIdMAX
, value
);
947 trace2_data_string_fl(file
, line
, category
, repo
, key
, buf_string
.buf
);
948 strbuf_release(&buf_string
);
951 void trace2_data_json_fl(const char *file
, int line
, const char *category
,
952 const struct repository
*repo
, const char *key
,
953 const struct json_writer
*value
)
955 struct tr2_tgt
*tgt_j
;
958 uint64_t us_elapsed_absolute
;
959 uint64_t us_elapsed_region
;
964 us_now
= getnanotime() / 1000;
965 us_elapsed_absolute
= tr2tls_absolute_elapsed(us_now
);
966 us_elapsed_region
= tr2tls_region_elasped_self(us_now
);
968 for_each_wanted_builtin (j
, tgt_j
)
969 if (tgt_j
->pfn_data_json_fl
)
970 tgt_j
->pfn_data_json_fl(file
, line
, us_elapsed_absolute
,
971 us_elapsed_region
, category
,
975 void trace2_printf_va_fl(const char *file
, int line
, const char *fmt
,
978 struct tr2_tgt
*tgt_j
;
981 uint64_t us_elapsed_absolute
;
986 us_now
= getnanotime() / 1000;
987 us_elapsed_absolute
= tr2tls_absolute_elapsed(us_now
);
990 * We expect each target function to treat 'ap' as constant
993 for_each_wanted_builtin (j
, tgt_j
)
994 if (tgt_j
->pfn_printf_va_fl
)
995 tgt_j
->pfn_printf_va_fl(file
, line
, us_elapsed_absolute
,
999 void trace2_printf_fl(const char *file
, int line
, const char *fmt
, ...)
1004 trace2_printf_va_fl(file
, line
, fmt
, ap
);
1008 void trace2_timer_start(enum trace2_timer_id tid
)
1010 if (!trace2_enabled
)
1013 if (tid
< 0 || tid
>= TRACE2_NUMBER_OF_TIMERS
)
1014 BUG("trace2_timer_start: invalid timer id: %d", tid
);
1016 tr2_start_timer(tid
);
1019 void trace2_timer_stop(enum trace2_timer_id tid
)
1021 if (!trace2_enabled
)
1024 if (tid
< 0 || tid
>= TRACE2_NUMBER_OF_TIMERS
)
1025 BUG("trace2_timer_stop: invalid timer id: %d", tid
);
1027 tr2_stop_timer(tid
);
1030 void trace2_counter_add(enum trace2_counter_id cid
, uint64_t value
)
1032 if (!trace2_enabled
)
1035 if (cid
< 0 || cid
>= TRACE2_NUMBER_OF_COUNTERS
)
1036 BUG("trace2_counter_add: invalid counter id: %d", cid
);
1038 tr2_counter_increment(cid
, value
);
1041 const char *trace2_session_id(void)
1043 return tr2_sid_get();