3 #include "run-command.h"
6 #include "json-writer.h"
7 #include "trace2/tr2_dst.h"
8 #include "trace2/tr2_sid.h"
9 #include "trace2/tr2_tbuf.h"
10 #include "trace2/tr2_tgt.h"
11 #include "trace2/tr2_tls.h"
13 static struct tr2_dst tr2dst_perf
= { "GIT_TR2_PERF", 0, 0, 0 };
16 * Set this environment variable to true to omit the "<time> <file>:<line>"
17 * fields from each line written to the builtin performance target.
19 * Unit tests may want to use this to help with testing.
21 #define TR2_ENVVAR_PERF_BRIEF "GIT_TR2_PERF_BRIEF"
22 static int tr2env_perf_brief
;
24 #define TR2FMT_PERF_FL_WIDTH (50)
25 #define TR2FMT_PERF_MAX_EVENT_NAME (12)
26 #define TR2FMT_PERF_REPO_WIDTH (4)
27 #define TR2FMT_PERF_CATEGORY_WIDTH (10)
29 #define TR2_DOTS_BUFFER_SIZE (100)
30 #define TR2_INDENT (2)
31 #define TR2_INDENT_LENGTH(ctx) (((ctx)->nr_open_regions - 1) * TR2_INDENT)
33 static struct strbuf dots
= STRBUF_INIT
;
35 static int fn_init(void)
37 int want
= tr2_dst_trace_want(&tr2dst_perf
);
44 strbuf_addchars(&dots
, '.', TR2_DOTS_BUFFER_SIZE
);
46 brief
= getenv(TR2_ENVVAR_PERF_BRIEF
);
47 if (brief
&& *brief
&&
48 ((want_brief
= git_parse_maybe_bool(brief
)) != -1))
49 tr2env_perf_brief
= want_brief
;
54 static void fn_term(void)
56 tr2_dst_trace_disable(&tr2dst_perf
);
58 strbuf_release(&dots
);
62 * Format trace line prefix in human-readable classic format for
63 * the performance target:
64 * "[<time> [<file>:<line>] <bar>] <nr_parents> <bar>
65 * <thread_name> <bar> <event_name> <bar> [<repo>] <bar>
66 * [<elapsed_absolute>] [<elapsed_relative>] <bar>
67 * [<category>] <bar> [<dots>] "
69 static void perf_fmt_prepare(const char *event_name
,
70 struct tr2tls_thread_ctx
*ctx
, const char *file
,
71 int line
, const struct repository
*repo
,
72 uint64_t *p_us_elapsed_absolute
,
73 uint64_t *p_us_elapsed_relative
,
74 const char *category
, struct strbuf
*buf
)
78 strbuf_setlen(buf
, 0);
80 if (!tr2env_perf_brief
) {
81 struct tr2_tbuf tb_now
;
83 tr2_tbuf_local_time(&tb_now
);
84 strbuf_addstr(buf
, tb_now
.buf
);
85 strbuf_addch(buf
, ' ');
88 strbuf_addf(buf
, "%s:%d ", file
, line
);
89 while (buf
->len
< TR2FMT_PERF_FL_WIDTH
)
90 strbuf_addch(buf
, ' ');
92 strbuf_addstr(buf
, "| ");
95 strbuf_addf(buf
, "d%d | ", tr2_sid_depth());
96 strbuf_addf(buf
, "%-*s | %-*s | ", TR2_MAX_THREAD_NAME
,
97 ctx
->thread_name
.buf
, TR2FMT_PERF_MAX_EVENT_NAME
,
100 len
= buf
->len
+ TR2FMT_PERF_REPO_WIDTH
;
102 strbuf_addf(buf
, "r%d ", repo
->trace2_repo_id
);
103 while (buf
->len
< len
)
104 strbuf_addch(buf
, ' ');
105 strbuf_addstr(buf
, "| ");
107 if (p_us_elapsed_absolute
)
108 strbuf_addf(buf
, "%9.6f | ",
109 ((double)(*p_us_elapsed_absolute
)) / 1000000.0);
111 strbuf_addf(buf
, "%9s | ", " ");
113 if (p_us_elapsed_relative
)
114 strbuf_addf(buf
, "%9.6f | ",
115 ((double)(*p_us_elapsed_relative
)) / 1000000.0);
117 strbuf_addf(buf
, "%9s | ", " ");
119 strbuf_addf(buf
, "%-*s | ", TR2FMT_PERF_CATEGORY_WIDTH
,
120 (category
? category
: ""));
122 if (ctx
->nr_open_regions
> 0) {
123 int len_indent
= TR2_INDENT_LENGTH(ctx
);
124 while (len_indent
> dots
.len
) {
125 strbuf_addbuf(buf
, &dots
);
126 len_indent
-= dots
.len
;
128 strbuf_addf(buf
, "%.*s", len_indent
, dots
.buf
);
132 static void perf_io_write_fl(const char *file
, int line
, const char *event_name
,
133 const struct repository
*repo
,
134 uint64_t *p_us_elapsed_absolute
,
135 uint64_t *p_us_elapsed_relative
,
136 const char *category
,
137 const struct strbuf
*buf_payload
)
139 struct tr2tls_thread_ctx
*ctx
= tr2tls_get_self();
140 struct strbuf buf_line
= STRBUF_INIT
;
142 perf_fmt_prepare(event_name
, ctx
, file
, line
, repo
,
143 p_us_elapsed_absolute
, p_us_elapsed_relative
, category
,
145 strbuf_addbuf(&buf_line
, buf_payload
);
146 tr2_dst_write_line(&tr2dst_perf
, &buf_line
);
147 strbuf_release(&buf_line
);
150 static void fn_version_fl(const char *file
, int line
)
152 const char *event_name
= "version";
153 struct strbuf buf_payload
= STRBUF_INIT
;
155 strbuf_addstr(&buf_payload
, git_version_string
);
157 perf_io_write_fl(file
, line
, event_name
, NULL
, NULL
, NULL
, NULL
,
159 strbuf_release(&buf_payload
);
162 static void fn_start_fl(const char *file
, int line
, const char **argv
)
164 const char *event_name
= "start";
165 struct strbuf buf_payload
= STRBUF_INIT
;
167 sq_quote_argv_pretty(&buf_payload
, argv
);
169 perf_io_write_fl(file
, line
, event_name
, NULL
, NULL
, NULL
, NULL
,
171 strbuf_release(&buf_payload
);
174 static void fn_exit_fl(const char *file
, int line
, uint64_t us_elapsed_absolute
,
177 const char *event_name
= "exit";
178 struct strbuf buf_payload
= STRBUF_INIT
;
180 strbuf_addf(&buf_payload
, "code:%d", code
);
182 perf_io_write_fl(file
, line
, event_name
, NULL
, &us_elapsed_absolute
,
183 NULL
, NULL
, &buf_payload
);
184 strbuf_release(&buf_payload
);
187 static void fn_signal(uint64_t us_elapsed_absolute
, int signo
)
189 const char *event_name
= "signal";
190 struct strbuf buf_payload
= STRBUF_INIT
;
192 strbuf_addf(&buf_payload
, "signo:%d", signo
);
194 perf_io_write_fl(__FILE__
, __LINE__
, event_name
, NULL
,
195 &us_elapsed_absolute
, NULL
, NULL
, &buf_payload
);
196 strbuf_release(&buf_payload
);
199 static void fn_atexit(uint64_t us_elapsed_absolute
, int code
)
201 const char *event_name
= "atexit";
202 struct strbuf buf_payload
= STRBUF_INIT
;
204 strbuf_addf(&buf_payload
, "code:%d", code
);
206 perf_io_write_fl(__FILE__
, __LINE__
, event_name
, NULL
,
207 &us_elapsed_absolute
, NULL
, NULL
, &buf_payload
);
208 strbuf_release(&buf_payload
);
211 static void maybe_append_string_va(struct strbuf
*buf
, const char *fmt
,
214 if (fmt
&& *fmt
&& ap
) {
217 va_copy(copy_ap
, ap
);
218 strbuf_vaddf(buf
, fmt
, copy_ap
);
224 strbuf_addstr(buf
, fmt
);
229 static void fn_error_va_fl(const char *file
, int line
, const char *fmt
,
232 const char *event_name
= "error";
233 struct strbuf buf_payload
= STRBUF_INIT
;
235 maybe_append_string_va(&buf_payload
, fmt
, ap
);
237 perf_io_write_fl(file
, line
, event_name
, NULL
, NULL
, NULL
, NULL
,
239 strbuf_release(&buf_payload
);
242 static void fn_command_path_fl(const char *file
, int line
, const char *pathname
)
244 const char *event_name
= "cmd_path";
245 struct strbuf buf_payload
= STRBUF_INIT
;
247 strbuf_addstr(&buf_payload
, pathname
);
249 perf_io_write_fl(file
, line
, event_name
, NULL
, NULL
, NULL
, NULL
,
251 strbuf_release(&buf_payload
);
254 static void fn_command_name_fl(const char *file
, int line
, const char *name
,
255 const char *hierarchy
)
257 const char *event_name
= "cmd_name";
258 struct strbuf buf_payload
= STRBUF_INIT
;
260 strbuf_addstr(&buf_payload
, name
);
261 if (hierarchy
&& *hierarchy
)
262 strbuf_addf(&buf_payload
, " (%s)", hierarchy
);
264 perf_io_write_fl(file
, line
, event_name
, NULL
, NULL
, NULL
, NULL
,
266 strbuf_release(&buf_payload
);
269 static void fn_command_mode_fl(const char *file
, int line
, const char *mode
)
271 const char *event_name
= "cmd_mode";
272 struct strbuf buf_payload
= STRBUF_INIT
;
274 strbuf_addstr(&buf_payload
, mode
);
276 perf_io_write_fl(file
, line
, event_name
, NULL
, NULL
, NULL
, NULL
,
278 strbuf_release(&buf_payload
);
281 static void fn_alias_fl(const char *file
, int line
, const char *alias
,
284 const char *event_name
= "alias";
285 struct strbuf buf_payload
= STRBUF_INIT
;
287 strbuf_addf(&buf_payload
, "alias:%s argv:", alias
);
288 sq_quote_argv_pretty(&buf_payload
, argv
);
290 perf_io_write_fl(file
, line
, event_name
, NULL
, NULL
, NULL
, NULL
,
292 strbuf_release(&buf_payload
);
295 static void fn_child_start_fl(const char *file
, int line
,
296 uint64_t us_elapsed_absolute
,
297 const struct child_process
*cmd
)
299 const char *event_name
= "child_start";
300 struct strbuf buf_payload
= STRBUF_INIT
;
302 if (cmd
->trace2_hook_name
) {
303 strbuf_addf(&buf_payload
, "[ch%d] class:hook hook:%s",
304 cmd
->trace2_child_id
, cmd
->trace2_hook_name
);
306 const char *child_class
=
307 cmd
->trace2_child_class
? cmd
->trace2_child_class
: "?";
308 strbuf_addf(&buf_payload
, "[ch%d] class:%s",
309 cmd
->trace2_child_id
, child_class
);
313 strbuf_addstr(&buf_payload
, " cd:");
314 sq_quote_buf_pretty(&buf_payload
, cmd
->dir
);
317 strbuf_addstr(&buf_payload
, " argv:");
319 strbuf_addstr(&buf_payload
, " git");
320 sq_quote_argv_pretty(&buf_payload
, cmd
->argv
);
322 perf_io_write_fl(file
, line
, event_name
, NULL
, &us_elapsed_absolute
,
323 NULL
, NULL
, &buf_payload
);
324 strbuf_release(&buf_payload
);
327 static void fn_child_exit_fl(const char *file
, int line
,
328 uint64_t us_elapsed_absolute
, int cid
, int pid
,
329 int code
, uint64_t us_elapsed_child
)
331 const char *event_name
= "child_exit";
332 struct strbuf buf_payload
= STRBUF_INIT
;
334 strbuf_addf(&buf_payload
, "[ch%d] pid:%d code:%d", cid
, pid
, code
);
336 perf_io_write_fl(file
, line
, event_name
, NULL
, &us_elapsed_absolute
,
337 &us_elapsed_child
, NULL
, &buf_payload
);
338 strbuf_release(&buf_payload
);
341 static void fn_thread_start_fl(const char *file
, int line
,
342 uint64_t us_elapsed_absolute
)
344 const char *event_name
= "thread_start";
345 struct strbuf buf_payload
= STRBUF_INIT
;
347 perf_io_write_fl(file
, line
, event_name
, NULL
, &us_elapsed_absolute
,
348 NULL
, NULL
, &buf_payload
);
349 strbuf_release(&buf_payload
);
352 static void fn_thread_exit_fl(const char *file
, int line
,
353 uint64_t us_elapsed_absolute
,
354 uint64_t us_elapsed_thread
)
356 const char *event_name
= "thread_exit";
357 struct strbuf buf_payload
= STRBUF_INIT
;
359 perf_io_write_fl(file
, line
, event_name
, NULL
, &us_elapsed_absolute
,
360 &us_elapsed_thread
, NULL
, &buf_payload
);
361 strbuf_release(&buf_payload
);
364 static void fn_exec_fl(const char *file
, int line
, uint64_t us_elapsed_absolute
,
365 int exec_id
, const char *exe
, const char **argv
)
367 const char *event_name
= "exec";
368 struct strbuf buf_payload
= STRBUF_INIT
;
370 strbuf_addf(&buf_payload
, "id:%d ", exec_id
);
371 strbuf_addstr(&buf_payload
, "argv:");
373 strbuf_addf(&buf_payload
, " %s", exe
);
374 sq_quote_argv_pretty(&buf_payload
, argv
);
376 perf_io_write_fl(file
, line
, event_name
, NULL
, &us_elapsed_absolute
,
377 NULL
, NULL
, &buf_payload
);
378 strbuf_release(&buf_payload
);
381 static void fn_exec_result_fl(const char *file
, int line
,
382 uint64_t us_elapsed_absolute
, int exec_id
,
385 const char *event_name
= "exec_result";
386 struct strbuf buf_payload
= STRBUF_INIT
;
388 strbuf_addf(&buf_payload
, "id:%d code:%d", exec_id
, code
);
390 strbuf_addf(&buf_payload
, " err:%s", strerror(code
));
392 perf_io_write_fl(file
, line
, event_name
, NULL
, &us_elapsed_absolute
,
393 NULL
, NULL
, &buf_payload
);
394 strbuf_release(&buf_payload
);
397 static void fn_param_fl(const char *file
, int line
, const char *param
,
400 const char *event_name
= "def_param";
401 struct strbuf buf_payload
= STRBUF_INIT
;
403 strbuf_addf(&buf_payload
, "%s:%s", param
, value
);
405 perf_io_write_fl(file
, line
, event_name
, NULL
, NULL
, NULL
, NULL
,
407 strbuf_release(&buf_payload
);
410 static void fn_repo_fl(const char *file
, int line
,
411 const struct repository
*repo
)
413 const char *event_name
= "def_repo";
414 struct strbuf buf_payload
= STRBUF_INIT
;
416 strbuf_addstr(&buf_payload
, "worktree:");
417 sq_quote_buf_pretty(&buf_payload
, repo
->worktree
);
419 perf_io_write_fl(file
, line
, event_name
, repo
, NULL
, NULL
, NULL
,
421 strbuf_release(&buf_payload
);
424 static void fn_region_enter_printf_va_fl(const char *file
, int line
,
425 uint64_t us_elapsed_absolute
,
426 const char *category
,
428 const struct repository
*repo
,
429 const char *fmt
, va_list ap
)
431 const char *event_name
= "region_enter";
432 struct strbuf buf_payload
= STRBUF_INIT
;
435 strbuf_addf(&buf_payload
, "label:%s ", label
);
436 maybe_append_string_va(&buf_payload
, fmt
, ap
);
438 perf_io_write_fl(file
, line
, event_name
, repo
, &us_elapsed_absolute
,
439 NULL
, category
, &buf_payload
);
440 strbuf_release(&buf_payload
);
443 static void fn_region_leave_printf_va_fl(
444 const char *file
, int line
, uint64_t us_elapsed_absolute
,
445 uint64_t us_elapsed_region
, const char *category
, const char *label
,
446 const struct repository
*repo
, const char *fmt
, va_list ap
)
448 const char *event_name
= "region_leave";
449 struct strbuf buf_payload
= STRBUF_INIT
;
452 strbuf_addf(&buf_payload
, "label:%s ", label
);
453 maybe_append_string_va(&buf_payload
, fmt
, ap
);
455 perf_io_write_fl(file
, line
, event_name
, repo
, &us_elapsed_absolute
,
456 &us_elapsed_region
, category
, &buf_payload
);
457 strbuf_release(&buf_payload
);
460 static void fn_data_fl(const char *file
, int line
, uint64_t us_elapsed_absolute
,
461 uint64_t us_elapsed_region
, const char *category
,
462 const struct repository
*repo
, const char *key
,
465 const char *event_name
= "data";
466 struct strbuf buf_payload
= STRBUF_INIT
;
468 strbuf_addf(&buf_payload
, "%s:%s", key
, value
);
470 perf_io_write_fl(file
, line
, event_name
, repo
, &us_elapsed_absolute
,
471 &us_elapsed_region
, category
, &buf_payload
);
472 strbuf_release(&buf_payload
);
475 static void fn_data_json_fl(const char *file
, int line
,
476 uint64_t us_elapsed_absolute
,
477 uint64_t us_elapsed_region
, const char *category
,
478 const struct repository
*repo
, const char *key
,
479 const struct json_writer
*value
)
481 const char *event_name
= "data_json";
482 struct strbuf buf_payload
= STRBUF_INIT
;
484 strbuf_addf(&buf_payload
, "%s:%s", key
, value
->json
.buf
);
486 perf_io_write_fl(file
, line
, event_name
, repo
, &us_elapsed_absolute
,
487 &us_elapsed_region
, category
, &buf_payload
);
488 strbuf_release(&buf_payload
);
491 static void fn_printf_va_fl(const char *file
, int line
,
492 uint64_t us_elapsed_absolute
, const char *fmt
,
495 const char *event_name
= "printf";
496 struct strbuf buf_payload
= STRBUF_INIT
;
498 maybe_append_string_va(&buf_payload
, fmt
, ap
);
500 perf_io_write_fl(file
, line
, event_name
, NULL
, &us_elapsed_absolute
,
501 NULL
, NULL
, &buf_payload
);
502 strbuf_release(&buf_payload
);
505 struct tr2_tgt tr2_tgt_perf
= {
529 fn_region_enter_printf_va_fl
,
530 fn_region_leave_printf_va_fl
,