]> git.ipfire.org Git - thirdparty/git.git/blob - trace2/tr2_tgt_perf.c
Merge branch 'js/t5563-portability-fix'
[thirdparty/git.git] / trace2 / tr2_tgt_perf.c
1 #include "git-compat-util.h"
2 #include "config.h"
3 #include "run-command.h"
4 #include "quote.h"
5 #include "version.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"
13 #include "trace2/tr2_tmr.h"
14
15 static struct tr2_dst tr2dst_perf = {
16 .sysenv_var = TR2_SYSENV_PERF,
17 };
18
19 /*
20 * Use TR2_SYSENV_PERF_BRIEF to omit the "<time> <file>:<line>"
21 * fields from each line written to the builtin performance target.
22 *
23 * Unit tests may want to use this to help with testing.
24 */
25 static int tr2env_perf_be_brief;
26
27 #define TR2FMT_PERF_FL_WIDTH (28)
28 #define TR2FMT_PERF_MAX_EVENT_NAME (12)
29 #define TR2FMT_PERF_REPO_WIDTH (3)
30 #define TR2FMT_PERF_CATEGORY_WIDTH (12)
31
32 #define TR2_INDENT (2)
33 #define TR2_INDENT_LENGTH(ctx) (((ctx)->nr_open_regions - 1) * TR2_INDENT)
34
35 static int fn_init(void)
36 {
37 int want = tr2_dst_trace_want(&tr2dst_perf);
38 int want_brief;
39 const char *brief;
40
41 if (!want)
42 return want;
43
44 brief = tr2_sysenv_get(TR2_SYSENV_PERF_BRIEF);
45 if (brief && *brief &&
46 ((want_brief = git_parse_maybe_bool(brief)) != -1))
47 tr2env_perf_be_brief = want_brief;
48
49 return want;
50 }
51
52 static void fn_term(void)
53 {
54 tr2_dst_trace_disable(&tr2dst_perf);
55 }
56
57 /*
58 * Format trace line prefix in human-readable classic format for
59 * the performance target:
60 * "[<time> [<file>:<line>] <bar>] <nr_parents> <bar>
61 * <thread_name> <bar> <event_name> <bar> [<repo>] <bar>
62 * [<elapsed_absolute>] [<elapsed_relative>] <bar>
63 * [<category>] <bar> [<dots>] "
64 */
65 static void perf_fmt_prepare(const char *event_name,
66 struct tr2tls_thread_ctx *ctx, const char *file,
67 int line, const struct repository *repo,
68 uint64_t *p_us_elapsed_absolute,
69 uint64_t *p_us_elapsed_relative,
70 const char *category, struct strbuf *buf)
71 {
72 int len;
73
74 strbuf_setlen(buf, 0);
75
76 if (!tr2env_perf_be_brief) {
77 struct tr2_tbuf tb_now;
78 size_t fl_end_col;
79
80 tr2_tbuf_local_time(&tb_now);
81 strbuf_addstr(buf, tb_now.buf);
82 strbuf_addch(buf, ' ');
83
84 fl_end_col = buf->len + TR2FMT_PERF_FL_WIDTH;
85
86 if (file && *file) {
87 struct strbuf buf_fl = STRBUF_INIT;
88
89 strbuf_addf(&buf_fl, "%s:%d", file, line);
90
91 if (buf_fl.len <= TR2FMT_PERF_FL_WIDTH)
92 strbuf_addbuf(buf, &buf_fl);
93 else {
94 size_t avail = TR2FMT_PERF_FL_WIDTH - 3;
95 strbuf_addstr(buf, "...");
96 strbuf_add(buf,
97 &buf_fl.buf[buf_fl.len - avail],
98 avail);
99 }
100
101 strbuf_release(&buf_fl);
102 }
103
104 while (buf->len < fl_end_col)
105 strbuf_addch(buf, ' ');
106
107 strbuf_addstr(buf, " | ");
108 }
109
110 strbuf_addf(buf, "d%d | ", tr2_sid_depth());
111 strbuf_addf(buf, "%-*s | %-*s | ", TR2_MAX_THREAD_NAME,
112 ctx->thread_name, TR2FMT_PERF_MAX_EVENT_NAME,
113 event_name);
114
115 len = buf->len + TR2FMT_PERF_REPO_WIDTH;
116 if (repo)
117 strbuf_addf(buf, "r%d ", repo->trace2_repo_id);
118 while (buf->len < len)
119 strbuf_addch(buf, ' ');
120 strbuf_addstr(buf, " | ");
121
122 if (p_us_elapsed_absolute)
123 strbuf_addf(buf, "%9.6f | ",
124 ((double)(*p_us_elapsed_absolute)) / 1000000.0);
125 else
126 strbuf_addf(buf, "%9s | ", " ");
127
128 if (p_us_elapsed_relative)
129 strbuf_addf(buf, "%9.6f | ",
130 ((double)(*p_us_elapsed_relative)) / 1000000.0);
131 else
132 strbuf_addf(buf, "%9s | ", " ");
133
134 strbuf_addf(buf, "%-*.*s | ", TR2FMT_PERF_CATEGORY_WIDTH,
135 TR2FMT_PERF_CATEGORY_WIDTH, (category ? category : ""));
136
137 if (ctx->nr_open_regions > 0)
138 strbuf_addchars(buf, '.', TR2_INDENT_LENGTH(ctx));
139 }
140
141 static void perf_io_write_fl(const char *file, int line, const char *event_name,
142 const struct repository *repo,
143 uint64_t *p_us_elapsed_absolute,
144 uint64_t *p_us_elapsed_relative,
145 const char *category,
146 const struct strbuf *buf_payload)
147 {
148 struct tr2tls_thread_ctx *ctx = tr2tls_get_self();
149 struct strbuf buf_line = STRBUF_INIT;
150
151 perf_fmt_prepare(event_name, ctx, file, line, repo,
152 p_us_elapsed_absolute, p_us_elapsed_relative, category,
153 &buf_line);
154 strbuf_addbuf(&buf_line, buf_payload);
155 tr2_dst_write_line(&tr2dst_perf, &buf_line);
156 strbuf_release(&buf_line);
157 }
158
159 static void fn_version_fl(const char *file, int line)
160 {
161 const char *event_name = "version";
162 struct strbuf buf_payload = STRBUF_INIT;
163
164 strbuf_addstr(&buf_payload, git_version_string);
165
166 perf_io_write_fl(file, line, event_name, NULL, NULL, NULL, NULL,
167 &buf_payload);
168 strbuf_release(&buf_payload);
169 }
170
171 static void fn_start_fl(const char *file, int line,
172 uint64_t us_elapsed_absolute, const char **argv)
173 {
174 const char *event_name = "start";
175 struct strbuf buf_payload = STRBUF_INIT;
176
177 sq_append_quote_argv_pretty(&buf_payload, argv);
178
179 perf_io_write_fl(file, line, event_name, NULL, &us_elapsed_absolute,
180 NULL, NULL, &buf_payload);
181 strbuf_release(&buf_payload);
182 }
183
184 static void fn_exit_fl(const char *file, int line, uint64_t us_elapsed_absolute,
185 int code)
186 {
187 const char *event_name = "exit";
188 struct strbuf buf_payload = STRBUF_INIT;
189
190 strbuf_addf(&buf_payload, "code:%d", code);
191
192 perf_io_write_fl(file, line, event_name, NULL, &us_elapsed_absolute,
193 NULL, NULL, &buf_payload);
194 strbuf_release(&buf_payload);
195 }
196
197 static void fn_signal(uint64_t us_elapsed_absolute, int signo)
198 {
199 const char *event_name = "signal";
200 struct strbuf buf_payload = STRBUF_INIT;
201
202 strbuf_addf(&buf_payload, "signo:%d", signo);
203
204 perf_io_write_fl(__FILE__, __LINE__, event_name, NULL,
205 &us_elapsed_absolute, NULL, NULL, &buf_payload);
206 strbuf_release(&buf_payload);
207 }
208
209 static void fn_atexit(uint64_t us_elapsed_absolute, int code)
210 {
211 const char *event_name = "atexit";
212 struct strbuf buf_payload = STRBUF_INIT;
213
214 strbuf_addf(&buf_payload, "code:%d", code);
215
216 perf_io_write_fl(__FILE__, __LINE__, event_name, NULL,
217 &us_elapsed_absolute, NULL, NULL, &buf_payload);
218 strbuf_release(&buf_payload);
219 }
220
221 static void maybe_append_string_va(struct strbuf *buf, const char *fmt,
222 va_list ap)
223 {
224 if (fmt && *fmt) {
225 va_list copy_ap;
226
227 va_copy(copy_ap, ap);
228 strbuf_vaddf(buf, fmt, copy_ap);
229 va_end(copy_ap);
230 return;
231 }
232 }
233
234 static void fn_error_va_fl(const char *file, int line, const char *fmt,
235 va_list ap)
236 {
237 const char *event_name = "error";
238 struct strbuf buf_payload = STRBUF_INIT;
239
240 maybe_append_string_va(&buf_payload, fmt, ap);
241
242 perf_io_write_fl(file, line, event_name, NULL, NULL, NULL, NULL,
243 &buf_payload);
244 strbuf_release(&buf_payload);
245 }
246
247 static void fn_command_path_fl(const char *file, int line, const char *pathname)
248 {
249 const char *event_name = "cmd_path";
250 struct strbuf buf_payload = STRBUF_INIT;
251
252 strbuf_addstr(&buf_payload, pathname);
253
254 perf_io_write_fl(file, line, event_name, NULL, NULL, NULL, NULL,
255 &buf_payload);
256 strbuf_release(&buf_payload);
257 }
258
259 static void fn_command_ancestry_fl(const char *file, int line, const char **parent_names)
260 {
261 const char *event_name = "cmd_ancestry";
262 struct strbuf buf_payload = STRBUF_INIT;
263
264 strbuf_addstr(&buf_payload, "ancestry:[");
265 /* It's not an argv but the rules are basically the same. */
266 sq_append_quote_argv_pretty(&buf_payload, parent_names);
267 strbuf_addch(&buf_payload, ']');
268
269 perf_io_write_fl(file, line, event_name, NULL, NULL, NULL, NULL,
270 &buf_payload);
271 strbuf_release(&buf_payload);
272 }
273
274 static void fn_command_name_fl(const char *file, int line, const char *name,
275 const char *hierarchy)
276 {
277 const char *event_name = "cmd_name";
278 struct strbuf buf_payload = STRBUF_INIT;
279
280 strbuf_addstr(&buf_payload, name);
281 if (hierarchy && *hierarchy)
282 strbuf_addf(&buf_payload, " (%s)", hierarchy);
283
284 perf_io_write_fl(file, line, event_name, NULL, NULL, NULL, NULL,
285 &buf_payload);
286 strbuf_release(&buf_payload);
287 }
288
289 static void fn_command_mode_fl(const char *file, int line, const char *mode)
290 {
291 const char *event_name = "cmd_mode";
292 struct strbuf buf_payload = STRBUF_INIT;
293
294 strbuf_addstr(&buf_payload, mode);
295
296 perf_io_write_fl(file, line, event_name, NULL, NULL, NULL, NULL,
297 &buf_payload);
298 strbuf_release(&buf_payload);
299 }
300
301 static void fn_alias_fl(const char *file, int line, const char *alias,
302 const char **argv)
303 {
304 const char *event_name = "alias";
305 struct strbuf buf_payload = STRBUF_INIT;
306
307 strbuf_addf(&buf_payload, "alias:%s argv:[", alias);
308 sq_append_quote_argv_pretty(&buf_payload, argv);
309 strbuf_addch(&buf_payload, ']');
310
311 perf_io_write_fl(file, line, event_name, NULL, NULL, NULL, NULL,
312 &buf_payload);
313 strbuf_release(&buf_payload);
314 }
315
316 static void fn_child_start_fl(const char *file, int line,
317 uint64_t us_elapsed_absolute,
318 const struct child_process *cmd)
319 {
320 const char *event_name = "child_start";
321 struct strbuf buf_payload = STRBUF_INIT;
322
323 if (cmd->trace2_hook_name) {
324 strbuf_addf(&buf_payload, "[ch%d] class:hook hook:%s",
325 cmd->trace2_child_id, cmd->trace2_hook_name);
326 } else {
327 const char *child_class =
328 cmd->trace2_child_class ? cmd->trace2_child_class : "?";
329 strbuf_addf(&buf_payload, "[ch%d] class:%s",
330 cmd->trace2_child_id, child_class);
331 }
332
333 if (cmd->dir) {
334 strbuf_addstr(&buf_payload, " cd:");
335 sq_quote_buf_pretty(&buf_payload, cmd->dir);
336 }
337
338 strbuf_addstr(&buf_payload, " argv:[");
339 if (cmd->git_cmd) {
340 strbuf_addstr(&buf_payload, "git");
341 if (cmd->args.nr)
342 strbuf_addch(&buf_payload, ' ');
343 }
344 sq_append_quote_argv_pretty(&buf_payload, cmd->args.v);
345 strbuf_addch(&buf_payload, ']');
346
347 perf_io_write_fl(file, line, event_name, NULL, &us_elapsed_absolute,
348 NULL, NULL, &buf_payload);
349 strbuf_release(&buf_payload);
350 }
351
352 static void fn_child_exit_fl(const char *file, int line,
353 uint64_t us_elapsed_absolute, int cid, int pid,
354 int code, uint64_t us_elapsed_child)
355 {
356 const char *event_name = "child_exit";
357 struct strbuf buf_payload = STRBUF_INIT;
358
359 strbuf_addf(&buf_payload, "[ch%d] pid:%d code:%d", cid, pid, code);
360
361 perf_io_write_fl(file, line, event_name, NULL, &us_elapsed_absolute,
362 &us_elapsed_child, NULL, &buf_payload);
363 strbuf_release(&buf_payload);
364 }
365
366 static void fn_child_ready_fl(const char *file, int line,
367 uint64_t us_elapsed_absolute, int cid, int pid,
368 const char *ready, uint64_t us_elapsed_child)
369 {
370 const char *event_name = "child_ready";
371 struct strbuf buf_payload = STRBUF_INIT;
372
373 strbuf_addf(&buf_payload, "[ch%d] pid:%d ready:%s", cid, pid, ready);
374
375 perf_io_write_fl(file, line, event_name, NULL, &us_elapsed_absolute,
376 &us_elapsed_child, NULL, &buf_payload);
377 strbuf_release(&buf_payload);
378 }
379
380 static void fn_thread_start_fl(const char *file, int line,
381 uint64_t us_elapsed_absolute)
382 {
383 const char *event_name = "thread_start";
384 struct strbuf buf_payload = STRBUF_INIT;
385
386 perf_io_write_fl(file, line, event_name, NULL, &us_elapsed_absolute,
387 NULL, NULL, &buf_payload);
388 strbuf_release(&buf_payload);
389 }
390
391 static void fn_thread_exit_fl(const char *file, int line,
392 uint64_t us_elapsed_absolute,
393 uint64_t us_elapsed_thread)
394 {
395 const char *event_name = "thread_exit";
396 struct strbuf buf_payload = STRBUF_INIT;
397
398 perf_io_write_fl(file, line, event_name, NULL, &us_elapsed_absolute,
399 &us_elapsed_thread, NULL, &buf_payload);
400 strbuf_release(&buf_payload);
401 }
402
403 static void fn_exec_fl(const char *file, int line, uint64_t us_elapsed_absolute,
404 int exec_id, const char *exe, const char **argv)
405 {
406 const char *event_name = "exec";
407 struct strbuf buf_payload = STRBUF_INIT;
408
409 strbuf_addf(&buf_payload, "id:%d ", exec_id);
410 strbuf_addstr(&buf_payload, "argv:[");
411 if (exe) {
412 strbuf_addstr(&buf_payload, exe);
413 if (argv[0])
414 strbuf_addch(&buf_payload, ' ');
415 }
416 sq_append_quote_argv_pretty(&buf_payload, argv);
417 strbuf_addch(&buf_payload, ']');
418
419 perf_io_write_fl(file, line, event_name, NULL, &us_elapsed_absolute,
420 NULL, NULL, &buf_payload);
421 strbuf_release(&buf_payload);
422 }
423
424 static void fn_exec_result_fl(const char *file, int line,
425 uint64_t us_elapsed_absolute, int exec_id,
426 int code)
427 {
428 const char *event_name = "exec_result";
429 struct strbuf buf_payload = STRBUF_INIT;
430
431 strbuf_addf(&buf_payload, "id:%d code:%d", exec_id, code);
432 if (code > 0)
433 strbuf_addf(&buf_payload, " err:%s", strerror(code));
434
435 perf_io_write_fl(file, line, event_name, NULL, &us_elapsed_absolute,
436 NULL, NULL, &buf_payload);
437 strbuf_release(&buf_payload);
438 }
439
440 static void fn_param_fl(const char *file, int line, const char *param,
441 const char *value)
442 {
443 const char *event_name = "def_param";
444 struct strbuf buf_payload = STRBUF_INIT;
445 struct strbuf scope_payload = STRBUF_INIT;
446 enum config_scope scope = current_config_scope();
447 const char *scope_name = config_scope_name(scope);
448
449 strbuf_addf(&buf_payload, "%s:%s", param, value);
450 strbuf_addf(&scope_payload, "%s:%s", "scope", scope_name);
451
452 perf_io_write_fl(file, line, event_name, NULL, NULL, NULL,
453 scope_payload.buf, &buf_payload);
454 strbuf_release(&buf_payload);
455 strbuf_release(&scope_payload);
456 }
457
458 static void fn_repo_fl(const char *file, int line,
459 const struct repository *repo)
460 {
461 const char *event_name = "def_repo";
462 struct strbuf buf_payload = STRBUF_INIT;
463
464 strbuf_addstr(&buf_payload, "worktree:");
465 sq_quote_buf_pretty(&buf_payload, repo->worktree);
466
467 perf_io_write_fl(file, line, event_name, repo, NULL, NULL, NULL,
468 &buf_payload);
469 strbuf_release(&buf_payload);
470 }
471
472 static void fn_region_enter_printf_va_fl(const char *file, int line,
473 uint64_t us_elapsed_absolute,
474 const char *category,
475 const char *label,
476 const struct repository *repo,
477 const char *fmt, va_list ap)
478 {
479 const char *event_name = "region_enter";
480 struct strbuf buf_payload = STRBUF_INIT;
481
482 if (label)
483 strbuf_addf(&buf_payload, "label:%s", label);
484 if (fmt && *fmt) {
485 strbuf_addch(&buf_payload, ' ');
486 maybe_append_string_va(&buf_payload, fmt, ap);
487 }
488
489 perf_io_write_fl(file, line, event_name, repo, &us_elapsed_absolute,
490 NULL, category, &buf_payload);
491 strbuf_release(&buf_payload);
492 }
493
494 static void fn_region_leave_printf_va_fl(
495 const char *file, int line, uint64_t us_elapsed_absolute,
496 uint64_t us_elapsed_region, const char *category, const char *label,
497 const struct repository *repo, const char *fmt, va_list ap)
498 {
499 const char *event_name = "region_leave";
500 struct strbuf buf_payload = STRBUF_INIT;
501
502 if (label)
503 strbuf_addf(&buf_payload, "label:%s", label);
504 if (fmt && *fmt) {
505 strbuf_addch(&buf_payload, ' ' );
506 maybe_append_string_va(&buf_payload, fmt, ap);
507 }
508
509 perf_io_write_fl(file, line, event_name, repo, &us_elapsed_absolute,
510 &us_elapsed_region, category, &buf_payload);
511 strbuf_release(&buf_payload);
512 }
513
514 static void fn_data_fl(const char *file, int line, uint64_t us_elapsed_absolute,
515 uint64_t us_elapsed_region, const char *category,
516 const struct repository *repo, const char *key,
517 const char *value)
518 {
519 const char *event_name = "data";
520 struct strbuf buf_payload = STRBUF_INIT;
521
522 strbuf_addf(&buf_payload, "%s:%s", key, value);
523
524 perf_io_write_fl(file, line, event_name, repo, &us_elapsed_absolute,
525 &us_elapsed_region, category, &buf_payload);
526 strbuf_release(&buf_payload);
527 }
528
529 static void fn_data_json_fl(const char *file, int line,
530 uint64_t us_elapsed_absolute,
531 uint64_t us_elapsed_region, const char *category,
532 const struct repository *repo, const char *key,
533 const struct json_writer *value)
534 {
535 const char *event_name = "data_json";
536 struct strbuf buf_payload = STRBUF_INIT;
537
538 strbuf_addf(&buf_payload, "%s:%s", key, value->json.buf);
539
540 perf_io_write_fl(file, line, event_name, repo, &us_elapsed_absolute,
541 &us_elapsed_region, category, &buf_payload);
542 strbuf_release(&buf_payload);
543 }
544
545 static void fn_printf_va_fl(const char *file, int line,
546 uint64_t us_elapsed_absolute, const char *fmt,
547 va_list ap)
548 {
549 const char *event_name = "printf";
550 struct strbuf buf_payload = STRBUF_INIT;
551
552 maybe_append_string_va(&buf_payload, fmt, ap);
553
554 perf_io_write_fl(file, line, event_name, NULL, &us_elapsed_absolute,
555 NULL, NULL, &buf_payload);
556 strbuf_release(&buf_payload);
557 }
558
559 static void fn_timer(const struct tr2_timer_metadata *meta,
560 const struct tr2_timer *timer,
561 int is_final_data)
562 {
563 const char *event_name = is_final_data ? "timer" : "th_timer";
564 struct strbuf buf_payload = STRBUF_INIT;
565 double t_total = NS_TO_SEC(timer->total_ns);
566 double t_min = NS_TO_SEC(timer->min_ns);
567 double t_max = NS_TO_SEC(timer->max_ns);
568
569 strbuf_addf(&buf_payload, ("name:%s"
570 " intervals:%"PRIu64
571 " total:%8.6f min:%8.6f max:%8.6f"),
572 meta->name,
573 timer->interval_count,
574 t_total, t_min, t_max);
575
576 perf_io_write_fl(__FILE__, __LINE__, event_name, NULL, NULL, NULL,
577 meta->category, &buf_payload);
578 strbuf_release(&buf_payload);
579 }
580
581 static void fn_counter(const struct tr2_counter_metadata *meta,
582 const struct tr2_counter *counter,
583 int is_final_data)
584 {
585 const char *event_name = is_final_data ? "counter" : "th_counter";
586 struct strbuf buf_payload = STRBUF_INIT;
587
588 strbuf_addf(&buf_payload, "name:%s value:%"PRIu64,
589 meta->name,
590 counter->value);
591
592 perf_io_write_fl(__FILE__, __LINE__, event_name, NULL, NULL, NULL,
593 meta->category, &buf_payload);
594 strbuf_release(&buf_payload);
595 }
596
597 struct tr2_tgt tr2_tgt_perf = {
598 .pdst = &tr2dst_perf,
599
600 .pfn_init = fn_init,
601 .pfn_term = fn_term,
602
603 .pfn_version_fl = fn_version_fl,
604 .pfn_start_fl = fn_start_fl,
605 .pfn_exit_fl = fn_exit_fl,
606 .pfn_signal = fn_signal,
607 .pfn_atexit = fn_atexit,
608 .pfn_error_va_fl = fn_error_va_fl,
609 .pfn_command_path_fl = fn_command_path_fl,
610 .pfn_command_ancestry_fl = fn_command_ancestry_fl,
611 .pfn_command_name_fl = fn_command_name_fl,
612 .pfn_command_mode_fl = fn_command_mode_fl,
613 .pfn_alias_fl = fn_alias_fl,
614 .pfn_child_start_fl = fn_child_start_fl,
615 .pfn_child_exit_fl = fn_child_exit_fl,
616 .pfn_child_ready_fl = fn_child_ready_fl,
617 .pfn_thread_start_fl = fn_thread_start_fl,
618 .pfn_thread_exit_fl = fn_thread_exit_fl,
619 .pfn_exec_fl = fn_exec_fl,
620 .pfn_exec_result_fl = fn_exec_result_fl,
621 .pfn_param_fl = fn_param_fl,
622 .pfn_repo_fl = fn_repo_fl,
623 .pfn_region_enter_printf_va_fl = fn_region_enter_printf_va_fl,
624 .pfn_region_leave_printf_va_fl = fn_region_leave_printf_va_fl,
625 .pfn_data_fl = fn_data_fl,
626 .pfn_data_json_fl = fn_data_json_fl,
627 .pfn_printf_va_fl = fn_printf_va_fl,
628 .pfn_timer = fn_timer,
629 .pfn_counter = fn_counter,
630 };