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_sysenv.h"
10 #include "trace2/tr2_tbuf.h"
11 #include "trace2/tr2_tgt.h"
12 #include "trace2/tr2_tls.h"
14 static struct tr2_dst tr2dst_perf
= { TR2_SYSENV_PERF
, 0, 0, 0 };
17 * Use TR2_SYSENV_PERF_BRIEF to omit the "<time> <file>:<line>"
18 * fields from each line written to the builtin performance target.
20 * Unit tests may want to use this to help with testing.
22 static int tr2env_perf_be_brief
;
24 #define TR2FMT_PERF_FL_WIDTH (28)
25 #define TR2FMT_PERF_MAX_EVENT_NAME (12)
26 #define TR2FMT_PERF_REPO_WIDTH (3)
27 #define TR2FMT_PERF_CATEGORY_WIDTH (12)
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
= tr2_sysenv_get(TR2_SYSENV_PERF_BRIEF
);
47 if (brief
&& *brief
&&
48 ((want_brief
= git_parse_maybe_bool(brief
)) != -1))
49 tr2env_perf_be_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_be_brief
) {
81 struct tr2_tbuf tb_now
;
84 tr2_tbuf_local_time(&tb_now
);
85 strbuf_addstr(buf
, tb_now
.buf
);
86 strbuf_addch(buf
, ' ');
88 fl_end_col
= buf
->len
+ TR2FMT_PERF_FL_WIDTH
;
91 struct strbuf buf_fl
= STRBUF_INIT
;
93 strbuf_addf(&buf_fl
, "%s:%d", file
, line
);
95 if (buf_fl
.len
<= TR2FMT_PERF_FL_WIDTH
)
96 strbuf_addbuf(buf
, &buf_fl
);
98 size_t avail
= TR2FMT_PERF_FL_WIDTH
- 3;
99 strbuf_addstr(buf
, "...");
101 &buf_fl
.buf
[buf_fl
.len
- avail
],
105 strbuf_release(&buf_fl
);
108 while (buf
->len
< fl_end_col
)
109 strbuf_addch(buf
, ' ');
111 strbuf_addstr(buf
, " | ");
114 strbuf_addf(buf
, "d%d | ", tr2_sid_depth());
115 strbuf_addf(buf
, "%-*s | %-*s | ", TR2_MAX_THREAD_NAME
,
116 ctx
->thread_name
.buf
, TR2FMT_PERF_MAX_EVENT_NAME
,
119 len
= buf
->len
+ TR2FMT_PERF_REPO_WIDTH
;
121 strbuf_addf(buf
, "r%d ", repo
->trace2_repo_id
);
122 while (buf
->len
< len
)
123 strbuf_addch(buf
, ' ');
124 strbuf_addstr(buf
, " | ");
126 if (p_us_elapsed_absolute
)
127 strbuf_addf(buf
, "%9.6f | ",
128 ((double)(*p_us_elapsed_absolute
)) / 1000000.0);
130 strbuf_addf(buf
, "%9s | ", " ");
132 if (p_us_elapsed_relative
)
133 strbuf_addf(buf
, "%9.6f | ",
134 ((double)(*p_us_elapsed_relative
)) / 1000000.0);
136 strbuf_addf(buf
, "%9s | ", " ");
138 strbuf_addf(buf
, "%-*.*s | ", TR2FMT_PERF_CATEGORY_WIDTH
,
139 TR2FMT_PERF_CATEGORY_WIDTH
, (category
? category
: ""));
141 if (ctx
->nr_open_regions
> 0) {
142 int len_indent
= TR2_INDENT_LENGTH(ctx
);
143 while (len_indent
> dots
.len
) {
144 strbuf_addbuf(buf
, &dots
);
145 len_indent
-= dots
.len
;
147 strbuf_addf(buf
, "%.*s", len_indent
, dots
.buf
);
151 static void perf_io_write_fl(const char *file
, int line
, const char *event_name
,
152 const struct repository
*repo
,
153 uint64_t *p_us_elapsed_absolute
,
154 uint64_t *p_us_elapsed_relative
,
155 const char *category
,
156 const struct strbuf
*buf_payload
)
158 struct tr2tls_thread_ctx
*ctx
= tr2tls_get_self();
159 struct strbuf buf_line
= STRBUF_INIT
;
161 perf_fmt_prepare(event_name
, ctx
, file
, line
, repo
,
162 p_us_elapsed_absolute
, p_us_elapsed_relative
, category
,
164 strbuf_addbuf(&buf_line
, buf_payload
);
165 tr2_dst_write_line(&tr2dst_perf
, &buf_line
);
166 strbuf_release(&buf_line
);
169 static void fn_version_fl(const char *file
, int line
)
171 const char *event_name
= "version";
172 struct strbuf buf_payload
= STRBUF_INIT
;
174 strbuf_addstr(&buf_payload
, git_version_string
);
176 perf_io_write_fl(file
, line
, event_name
, NULL
, NULL
, NULL
, NULL
,
178 strbuf_release(&buf_payload
);
181 static void fn_start_fl(const char *file
, int line
,
182 uint64_t us_elapsed_absolute
, const char **argv
)
184 const char *event_name
= "start";
185 struct strbuf buf_payload
= STRBUF_INIT
;
187 sq_quote_argv_pretty(&buf_payload
, argv
);
189 perf_io_write_fl(file
, line
, event_name
, NULL
, &us_elapsed_absolute
,
190 NULL
, NULL
, &buf_payload
);
191 strbuf_release(&buf_payload
);
194 static void fn_exit_fl(const char *file
, int line
, uint64_t us_elapsed_absolute
,
197 const char *event_name
= "exit";
198 struct strbuf buf_payload
= STRBUF_INIT
;
200 strbuf_addf(&buf_payload
, "code:%d", code
);
202 perf_io_write_fl(file
, line
, event_name
, NULL
, &us_elapsed_absolute
,
203 NULL
, NULL
, &buf_payload
);
204 strbuf_release(&buf_payload
);
207 static void fn_signal(uint64_t us_elapsed_absolute
, int signo
)
209 const char *event_name
= "signal";
210 struct strbuf buf_payload
= STRBUF_INIT
;
212 strbuf_addf(&buf_payload
, "signo:%d", signo
);
214 perf_io_write_fl(__FILE__
, __LINE__
, event_name
, NULL
,
215 &us_elapsed_absolute
, NULL
, NULL
, &buf_payload
);
216 strbuf_release(&buf_payload
);
219 static void fn_atexit(uint64_t us_elapsed_absolute
, int code
)
221 const char *event_name
= "atexit";
222 struct strbuf buf_payload
= STRBUF_INIT
;
224 strbuf_addf(&buf_payload
, "code:%d", code
);
226 perf_io_write_fl(__FILE__
, __LINE__
, event_name
, NULL
,
227 &us_elapsed_absolute
, NULL
, NULL
, &buf_payload
);
228 strbuf_release(&buf_payload
);
231 static void maybe_append_string_va(struct strbuf
*buf
, const char *fmt
,
237 va_copy(copy_ap
, ap
);
238 strbuf_vaddf(buf
, fmt
, copy_ap
);
244 static void fn_error_va_fl(const char *file
, int line
, const char *fmt
,
247 const char *event_name
= "error";
248 struct strbuf buf_payload
= STRBUF_INIT
;
250 maybe_append_string_va(&buf_payload
, fmt
, ap
);
252 perf_io_write_fl(file
, line
, event_name
, NULL
, NULL
, NULL
, NULL
,
254 strbuf_release(&buf_payload
);
257 static void fn_command_path_fl(const char *file
, int line
, const char *pathname
)
259 const char *event_name
= "cmd_path";
260 struct strbuf buf_payload
= STRBUF_INIT
;
262 strbuf_addstr(&buf_payload
, pathname
);
264 perf_io_write_fl(file
, line
, event_name
, NULL
, NULL
, NULL
, NULL
,
266 strbuf_release(&buf_payload
);
269 static void fn_command_name_fl(const char *file
, int line
, const char *name
,
270 const char *hierarchy
)
272 const char *event_name
= "cmd_name";
273 struct strbuf buf_payload
= STRBUF_INIT
;
275 strbuf_addstr(&buf_payload
, name
);
276 if (hierarchy
&& *hierarchy
)
277 strbuf_addf(&buf_payload
, " (%s)", hierarchy
);
279 perf_io_write_fl(file
, line
, event_name
, NULL
, NULL
, NULL
, NULL
,
281 strbuf_release(&buf_payload
);
284 static void fn_command_mode_fl(const char *file
, int line
, const char *mode
)
286 const char *event_name
= "cmd_mode";
287 struct strbuf buf_payload
= STRBUF_INIT
;
289 strbuf_addstr(&buf_payload
, mode
);
291 perf_io_write_fl(file
, line
, event_name
, NULL
, NULL
, NULL
, NULL
,
293 strbuf_release(&buf_payload
);
296 static void fn_alias_fl(const char *file
, int line
, const char *alias
,
299 const char *event_name
= "alias";
300 struct strbuf buf_payload
= STRBUF_INIT
;
302 strbuf_addf(&buf_payload
, "alias:%s argv:", alias
);
303 sq_quote_argv_pretty(&buf_payload
, argv
);
305 perf_io_write_fl(file
, line
, event_name
, NULL
, NULL
, NULL
, NULL
,
307 strbuf_release(&buf_payload
);
310 static void fn_child_start_fl(const char *file
, int line
,
311 uint64_t us_elapsed_absolute
,
312 const struct child_process
*cmd
)
314 const char *event_name
= "child_start";
315 struct strbuf buf_payload
= STRBUF_INIT
;
317 if (cmd
->trace2_hook_name
) {
318 strbuf_addf(&buf_payload
, "[ch%d] class:hook hook:%s",
319 cmd
->trace2_child_id
, cmd
->trace2_hook_name
);
321 const char *child_class
=
322 cmd
->trace2_child_class
? cmd
->trace2_child_class
: "?";
323 strbuf_addf(&buf_payload
, "[ch%d] class:%s",
324 cmd
->trace2_child_id
, child_class
);
328 strbuf_addstr(&buf_payload
, " cd:");
329 sq_quote_buf_pretty(&buf_payload
, cmd
->dir
);
332 strbuf_addstr(&buf_payload
, " argv:");
334 strbuf_addstr(&buf_payload
, " git");
335 sq_quote_argv_pretty(&buf_payload
, cmd
->argv
);
337 perf_io_write_fl(file
, line
, event_name
, NULL
, &us_elapsed_absolute
,
338 NULL
, NULL
, &buf_payload
);
339 strbuf_release(&buf_payload
);
342 static void fn_child_exit_fl(const char *file
, int line
,
343 uint64_t us_elapsed_absolute
, int cid
, int pid
,
344 int code
, uint64_t us_elapsed_child
)
346 const char *event_name
= "child_exit";
347 struct strbuf buf_payload
= STRBUF_INIT
;
349 strbuf_addf(&buf_payload
, "[ch%d] pid:%d code:%d", cid
, pid
, code
);
351 perf_io_write_fl(file
, line
, event_name
, NULL
, &us_elapsed_absolute
,
352 &us_elapsed_child
, NULL
, &buf_payload
);
353 strbuf_release(&buf_payload
);
356 static void fn_thread_start_fl(const char *file
, int line
,
357 uint64_t us_elapsed_absolute
)
359 const char *event_name
= "thread_start";
360 struct strbuf buf_payload
= STRBUF_INIT
;
362 perf_io_write_fl(file
, line
, event_name
, NULL
, &us_elapsed_absolute
,
363 NULL
, NULL
, &buf_payload
);
364 strbuf_release(&buf_payload
);
367 static void fn_thread_exit_fl(const char *file
, int line
,
368 uint64_t us_elapsed_absolute
,
369 uint64_t us_elapsed_thread
)
371 const char *event_name
= "thread_exit";
372 struct strbuf buf_payload
= STRBUF_INIT
;
374 perf_io_write_fl(file
, line
, event_name
, NULL
, &us_elapsed_absolute
,
375 &us_elapsed_thread
, NULL
, &buf_payload
);
376 strbuf_release(&buf_payload
);
379 static void fn_exec_fl(const char *file
, int line
, uint64_t us_elapsed_absolute
,
380 int exec_id
, const char *exe
, const char **argv
)
382 const char *event_name
= "exec";
383 struct strbuf buf_payload
= STRBUF_INIT
;
385 strbuf_addf(&buf_payload
, "id:%d ", exec_id
);
386 strbuf_addstr(&buf_payload
, "argv:");
388 strbuf_addf(&buf_payload
, " %s", exe
);
389 sq_quote_argv_pretty(&buf_payload
, argv
);
391 perf_io_write_fl(file
, line
, event_name
, NULL
, &us_elapsed_absolute
,
392 NULL
, NULL
, &buf_payload
);
393 strbuf_release(&buf_payload
);
396 static void fn_exec_result_fl(const char *file
, int line
,
397 uint64_t us_elapsed_absolute
, int exec_id
,
400 const char *event_name
= "exec_result";
401 struct strbuf buf_payload
= STRBUF_INIT
;
403 strbuf_addf(&buf_payload
, "id:%d code:%d", exec_id
, code
);
405 strbuf_addf(&buf_payload
, " err:%s", strerror(code
));
407 perf_io_write_fl(file
, line
, event_name
, NULL
, &us_elapsed_absolute
,
408 NULL
, NULL
, &buf_payload
);
409 strbuf_release(&buf_payload
);
412 static void fn_param_fl(const char *file
, int line
, const char *param
,
415 const char *event_name
= "def_param";
416 struct strbuf buf_payload
= STRBUF_INIT
;
418 strbuf_addf(&buf_payload
, "%s:%s", param
, value
);
420 perf_io_write_fl(file
, line
, event_name
, NULL
, NULL
, NULL
, NULL
,
422 strbuf_release(&buf_payload
);
425 static void fn_repo_fl(const char *file
, int line
,
426 const struct repository
*repo
)
428 const char *event_name
= "def_repo";
429 struct strbuf buf_payload
= STRBUF_INIT
;
431 strbuf_addstr(&buf_payload
, "worktree:");
432 sq_quote_buf_pretty(&buf_payload
, repo
->worktree
);
434 perf_io_write_fl(file
, line
, event_name
, repo
, NULL
, NULL
, NULL
,
436 strbuf_release(&buf_payload
);
439 static void fn_region_enter_printf_va_fl(const char *file
, int line
,
440 uint64_t us_elapsed_absolute
,
441 const char *category
,
443 const struct repository
*repo
,
444 const char *fmt
, va_list ap
)
446 const char *event_name
= "region_enter";
447 struct strbuf buf_payload
= STRBUF_INIT
;
450 strbuf_addf(&buf_payload
, "label:%s", label
);
452 strbuf_addch(&buf_payload
, ' ');
453 maybe_append_string_va(&buf_payload
, fmt
, ap
);
456 perf_io_write_fl(file
, line
, event_name
, repo
, &us_elapsed_absolute
,
457 NULL
, category
, &buf_payload
);
458 strbuf_release(&buf_payload
);
461 static void fn_region_leave_printf_va_fl(
462 const char *file
, int line
, uint64_t us_elapsed_absolute
,
463 uint64_t us_elapsed_region
, const char *category
, const char *label
,
464 const struct repository
*repo
, const char *fmt
, va_list ap
)
466 const char *event_name
= "region_leave";
467 struct strbuf buf_payload
= STRBUF_INIT
;
470 strbuf_addf(&buf_payload
, "label:%s", label
);
472 strbuf_addch(&buf_payload
, ' ' );
473 maybe_append_string_va(&buf_payload
, fmt
, ap
);
476 perf_io_write_fl(file
, line
, event_name
, repo
, &us_elapsed_absolute
,
477 &us_elapsed_region
, category
, &buf_payload
);
478 strbuf_release(&buf_payload
);
481 static void fn_data_fl(const char *file
, int line
, uint64_t us_elapsed_absolute
,
482 uint64_t us_elapsed_region
, const char *category
,
483 const struct repository
*repo
, const char *key
,
486 const char *event_name
= "data";
487 struct strbuf buf_payload
= STRBUF_INIT
;
489 strbuf_addf(&buf_payload
, "%s:%s", key
, value
);
491 perf_io_write_fl(file
, line
, event_name
, repo
, &us_elapsed_absolute
,
492 &us_elapsed_region
, category
, &buf_payload
);
493 strbuf_release(&buf_payload
);
496 static void fn_data_json_fl(const char *file
, int line
,
497 uint64_t us_elapsed_absolute
,
498 uint64_t us_elapsed_region
, const char *category
,
499 const struct repository
*repo
, const char *key
,
500 const struct json_writer
*value
)
502 const char *event_name
= "data_json";
503 struct strbuf buf_payload
= STRBUF_INIT
;
505 strbuf_addf(&buf_payload
, "%s:%s", key
, value
->json
.buf
);
507 perf_io_write_fl(file
, line
, event_name
, repo
, &us_elapsed_absolute
,
508 &us_elapsed_region
, category
, &buf_payload
);
509 strbuf_release(&buf_payload
);
512 static void fn_printf_va_fl(const char *file
, int line
,
513 uint64_t us_elapsed_absolute
, const char *fmt
,
516 const char *event_name
= "printf";
517 struct strbuf buf_payload
= STRBUF_INIT
;
519 maybe_append_string_va(&buf_payload
, fmt
, ap
);
521 perf_io_write_fl(file
, line
, event_name
, NULL
, &us_elapsed_absolute
,
522 NULL
, NULL
, &buf_payload
);
523 strbuf_release(&buf_payload
);
526 struct tr2_tgt tr2_tgt_perf
= {
550 fn_region_enter_printf_va_fl
,
551 fn_region_leave_printf_va_fl
,