The twelfth batch
[git.git] / trace2 / tr2_tgt_event.c
blobc5c8cfbbaa065bccfbc058efd9eaffc71507d011
1 #include "cache.h"
2 #include "config.h"
3 #include "json-writer.h"
4 #include "run-command.h"
5 #include "version.h"
6 #include "trace2/tr2_dst.h"
7 #include "trace2/tr2_tbuf.h"
8 #include "trace2/tr2_sid.h"
9 #include "trace2/tr2_sysenv.h"
10 #include "trace2/tr2_tgt.h"
11 #include "trace2/tr2_tls.h"
13 static struct tr2_dst tr2dst_event = {
14 .sysenv_var = TR2_SYSENV_EVENT,
18 * The version number of the JSON data generated by the EVENT target in this
19 * source file. The version should be incremented if new event types are added,
20 * if existing fields are removed, or if there are significant changes in
21 * interpretation of existing events or fields. Smaller changes, such as adding
22 * a new field to an existing event, do not require an increment to the EVENT
23 * format version.
25 #define TR2_EVENT_VERSION "3"
28 * Region nesting limit for messages written to the event target.
30 * The "region_enter" and "region_leave" messages (especially recursive
31 * messages such as those produced while diving the worktree or index)
32 * are primarily intended for the performance target during debugging.
34 * Some of the outer-most messages, however, may be of interest to the
35 * event target. Use the TR2_SYSENV_EVENT_NESTING setting to increase
36 * region details in the event target.
38 static int tr2env_event_max_nesting_levels = 2;
41 * Use the TR2_SYSENV_EVENT_BRIEF to omit the <time>, <file>, and
42 * <line> fields from most events.
44 static int tr2env_event_be_brief;
46 static int fn_init(void)
48 int want = tr2_dst_trace_want(&tr2dst_event);
49 int max_nesting;
50 int want_brief;
51 const char *nesting;
52 const char *brief;
54 if (!want)
55 return want;
57 nesting = tr2_sysenv_get(TR2_SYSENV_EVENT_NESTING);
58 if (nesting && *nesting && ((max_nesting = atoi(nesting)) > 0))
59 tr2env_event_max_nesting_levels = max_nesting;
61 brief = tr2_sysenv_get(TR2_SYSENV_EVENT_BRIEF);
62 if (brief && *brief &&
63 ((want_brief = git_parse_maybe_bool(brief)) != -1))
64 tr2env_event_be_brief = want_brief;
66 return want;
69 static void fn_term(void)
71 tr2_dst_trace_disable(&tr2dst_event);
75 * Append common key-value pairs to the currently open JSON object.
76 * "event:"<event_name>"
77 * "sid":"<sid>"
78 * "thread":"<thread_name>"
79 * "time":"<time>"
80 * "file":"<filename>"
81 * "line":<line_number>
82 * "repo":<repo_id>
84 static void event_fmt_prepare(const char *event_name, const char *file,
85 int line, const struct repository *repo,
86 struct json_writer *jw)
88 struct tr2tls_thread_ctx *ctx = tr2tls_get_self();
89 struct tr2_tbuf tb_now;
91 jw_object_string(jw, "event", event_name);
92 jw_object_string(jw, "sid", tr2_sid_get());
93 jw_object_string(jw, "thread", ctx->thread_name.buf);
96 * In brief mode, only emit <time> on these 2 event types.
98 if (!tr2env_event_be_brief || !strcmp(event_name, "version") ||
99 !strcmp(event_name, "atexit")) {
100 tr2_tbuf_utc_datetime_extended(&tb_now);
101 jw_object_string(jw, "time", tb_now.buf);
104 if (!tr2env_event_be_brief && file && *file) {
105 jw_object_string(jw, "file", file);
106 jw_object_intmax(jw, "line", line);
109 if (repo)
110 jw_object_intmax(jw, "repo", repo->trace2_repo_id);
113 static void fn_too_many_files_fl(const char *file, int line)
115 const char *event_name = "too_many_files";
116 struct json_writer jw = JSON_WRITER_INIT;
118 jw_object_begin(&jw, 0);
119 event_fmt_prepare(event_name, file, line, NULL, &jw);
120 jw_end(&jw);
122 tr2_dst_write_line(&tr2dst_event, &jw.json);
123 jw_release(&jw);
126 static void fn_version_fl(const char *file, int line)
128 const char *event_name = "version";
129 struct json_writer jw = JSON_WRITER_INIT;
131 jw_object_begin(&jw, 0);
132 event_fmt_prepare(event_name, file, line, NULL, &jw);
133 jw_object_string(&jw, "evt", TR2_EVENT_VERSION);
134 jw_object_string(&jw, "exe", git_version_string);
135 jw_end(&jw);
137 tr2_dst_write_line(&tr2dst_event, &jw.json);
138 jw_release(&jw);
140 if (tr2dst_event.too_many_files)
141 fn_too_many_files_fl(file, line);
144 static void fn_start_fl(const char *file, int line,
145 uint64_t us_elapsed_absolute, const char **argv)
147 const char *event_name = "start";
148 struct json_writer jw = JSON_WRITER_INIT;
149 double t_abs = (double)us_elapsed_absolute / 1000000.0;
151 jw_object_begin(&jw, 0);
152 event_fmt_prepare(event_name, file, line, NULL, &jw);
153 jw_object_double(&jw, "t_abs", 6, t_abs);
154 jw_object_inline_begin_array(&jw, "argv");
155 jw_array_argv(&jw, argv);
156 jw_end(&jw);
157 jw_end(&jw);
159 tr2_dst_write_line(&tr2dst_event, &jw.json);
160 jw_release(&jw);
163 static void fn_exit_fl(const char *file, int line, uint64_t us_elapsed_absolute,
164 int code)
166 const char *event_name = "exit";
167 struct json_writer jw = JSON_WRITER_INIT;
168 double t_abs = (double)us_elapsed_absolute / 1000000.0;
170 jw_object_begin(&jw, 0);
171 event_fmt_prepare(event_name, file, line, NULL, &jw);
172 jw_object_double(&jw, "t_abs", 6, t_abs);
173 jw_object_intmax(&jw, "code", code);
174 jw_end(&jw);
176 tr2_dst_write_line(&tr2dst_event, &jw.json);
177 jw_release(&jw);
180 static void fn_signal(uint64_t us_elapsed_absolute, int signo)
182 const char *event_name = "signal";
183 struct json_writer jw = JSON_WRITER_INIT;
184 double t_abs = (double)us_elapsed_absolute / 1000000.0;
186 jw_object_begin(&jw, 0);
187 event_fmt_prepare(event_name, __FILE__, __LINE__, NULL, &jw);
188 jw_object_double(&jw, "t_abs", 6, t_abs);
189 jw_object_intmax(&jw, "signo", signo);
190 jw_end(&jw);
192 tr2_dst_write_line(&tr2dst_event, &jw.json);
193 jw_release(&jw);
196 static void fn_atexit(uint64_t us_elapsed_absolute, int code)
198 const char *event_name = "atexit";
199 struct json_writer jw = JSON_WRITER_INIT;
200 double t_abs = (double)us_elapsed_absolute / 1000000.0;
202 jw_object_begin(&jw, 0);
203 event_fmt_prepare(event_name, __FILE__, __LINE__, NULL, &jw);
204 jw_object_double(&jw, "t_abs", 6, t_abs);
205 jw_object_intmax(&jw, "code", code);
206 jw_end(&jw);
208 tr2_dst_write_line(&tr2dst_event, &jw.json);
209 jw_release(&jw);
212 static void maybe_add_string_va(struct json_writer *jw, const char *field_name,
213 const char *fmt, va_list ap)
215 if (fmt && *fmt) {
216 va_list copy_ap;
217 struct strbuf buf = STRBUF_INIT;
219 va_copy(copy_ap, ap);
220 strbuf_vaddf(&buf, fmt, copy_ap);
221 va_end(copy_ap);
223 jw_object_string(jw, field_name, buf.buf);
224 strbuf_release(&buf);
225 return;
229 static void fn_error_va_fl(const char *file, int line, const char *fmt,
230 va_list ap)
232 const char *event_name = "error";
233 struct json_writer jw = JSON_WRITER_INIT;
235 jw_object_begin(&jw, 0);
236 event_fmt_prepare(event_name, file, line, NULL, &jw);
237 maybe_add_string_va(&jw, "msg", fmt, ap);
239 * Also emit the format string as a field in case
240 * post-processors want to aggregate common error
241 * messages by type without argument fields (such
242 * as pathnames or branch names) cluttering it up.
244 if (fmt && *fmt)
245 jw_object_string(&jw, "fmt", fmt);
246 jw_end(&jw);
248 tr2_dst_write_line(&tr2dst_event, &jw.json);
249 jw_release(&jw);
252 static void fn_command_path_fl(const char *file, int line, const char *pathname)
254 const char *event_name = "cmd_path";
255 struct json_writer jw = JSON_WRITER_INIT;
257 jw_object_begin(&jw, 0);
258 event_fmt_prepare(event_name, file, line, NULL, &jw);
259 jw_object_string(&jw, "path", pathname);
260 jw_end(&jw);
262 tr2_dst_write_line(&tr2dst_event, &jw.json);
263 jw_release(&jw);
266 static void fn_command_ancestry_fl(const char *file, int line, const char **parent_names)
268 const char *event_name = "cmd_ancestry";
269 const char *parent_name = NULL;
270 struct json_writer jw = JSON_WRITER_INIT;
272 jw_object_begin(&jw, 0);
273 event_fmt_prepare(event_name, file, line, NULL, &jw);
274 jw_object_inline_begin_array(&jw, "ancestry");
276 while ((parent_name = *parent_names++))
277 jw_array_string(&jw, parent_name);
279 jw_end(&jw); /* 'ancestry' array */
280 jw_end(&jw); /* event object */
282 tr2_dst_write_line(&tr2dst_event, &jw.json);
283 jw_release(&jw);
286 static void fn_command_name_fl(const char *file, int line, const char *name,
287 const char *hierarchy)
289 const char *event_name = "cmd_name";
290 struct json_writer jw = JSON_WRITER_INIT;
292 jw_object_begin(&jw, 0);
293 event_fmt_prepare(event_name, file, line, NULL, &jw);
294 jw_object_string(&jw, "name", name);
295 if (hierarchy && *hierarchy)
296 jw_object_string(&jw, "hierarchy", hierarchy);
297 jw_end(&jw);
299 tr2_dst_write_line(&tr2dst_event, &jw.json);
300 jw_release(&jw);
303 static void fn_command_mode_fl(const char *file, int line, const char *mode)
305 const char *event_name = "cmd_mode";
306 struct json_writer jw = JSON_WRITER_INIT;
308 jw_object_begin(&jw, 0);
309 event_fmt_prepare(event_name, file, line, NULL, &jw);
310 jw_object_string(&jw, "name", mode);
311 jw_end(&jw);
313 tr2_dst_write_line(&tr2dst_event, &jw.json);
314 jw_release(&jw);
317 static void fn_alias_fl(const char *file, int line, const char *alias,
318 const char **argv)
320 const char *event_name = "alias";
321 struct json_writer jw = JSON_WRITER_INIT;
323 jw_object_begin(&jw, 0);
324 event_fmt_prepare(event_name, file, line, NULL, &jw);
325 jw_object_string(&jw, "alias", alias);
326 jw_object_inline_begin_array(&jw, "argv");
327 jw_array_argv(&jw, argv);
328 jw_end(&jw);
329 jw_end(&jw);
331 tr2_dst_write_line(&tr2dst_event, &jw.json);
332 jw_release(&jw);
335 static void fn_child_start_fl(const char *file, int line,
336 uint64_t us_elapsed_absolute,
337 const struct child_process *cmd)
339 const char *event_name = "child_start";
340 struct json_writer jw = JSON_WRITER_INIT;
342 jw_object_begin(&jw, 0);
343 event_fmt_prepare(event_name, file, line, NULL, &jw);
344 jw_object_intmax(&jw, "child_id", cmd->trace2_child_id);
345 if (cmd->trace2_hook_name) {
346 jw_object_string(&jw, "child_class", "hook");
347 jw_object_string(&jw, "hook_name", cmd->trace2_hook_name);
348 } else {
349 const char *child_class =
350 cmd->trace2_child_class ? cmd->trace2_child_class : "?";
351 jw_object_string(&jw, "child_class", child_class);
353 if (cmd->dir)
354 jw_object_string(&jw, "cd", cmd->dir);
355 jw_object_bool(&jw, "use_shell", cmd->use_shell);
356 jw_object_inline_begin_array(&jw, "argv");
357 if (cmd->git_cmd)
358 jw_array_string(&jw, "git");
359 jw_array_argv(&jw, cmd->args.v);
360 jw_end(&jw);
361 jw_end(&jw);
363 tr2_dst_write_line(&tr2dst_event, &jw.json);
364 jw_release(&jw);
367 static void fn_child_exit_fl(const char *file, int line,
368 uint64_t us_elapsed_absolute, int cid, int pid,
369 int code, uint64_t us_elapsed_child)
371 const char *event_name = "child_exit";
372 struct json_writer jw = JSON_WRITER_INIT;
373 double t_rel = (double)us_elapsed_child / 1000000.0;
375 jw_object_begin(&jw, 0);
376 event_fmt_prepare(event_name, file, line, NULL, &jw);
377 jw_object_intmax(&jw, "child_id", cid);
378 jw_object_intmax(&jw, "pid", pid);
379 jw_object_intmax(&jw, "code", code);
380 jw_object_double(&jw, "t_rel", 6, t_rel);
381 jw_end(&jw);
383 tr2_dst_write_line(&tr2dst_event, &jw.json);
385 jw_release(&jw);
388 static void fn_child_ready_fl(const char *file, int line,
389 uint64_t us_elapsed_absolute, int cid, int pid,
390 const char *ready, uint64_t us_elapsed_child)
392 const char *event_name = "child_ready";
393 struct json_writer jw = JSON_WRITER_INIT;
394 double t_rel = (double)us_elapsed_child / 1000000.0;
396 jw_object_begin(&jw, 0);
397 event_fmt_prepare(event_name, file, line, NULL, &jw);
398 jw_object_intmax(&jw, "child_id", cid);
399 jw_object_intmax(&jw, "pid", pid);
400 jw_object_string(&jw, "ready", ready);
401 jw_object_double(&jw, "t_rel", 6, t_rel);
402 jw_end(&jw);
404 tr2_dst_write_line(&tr2dst_event, &jw.json);
406 jw_release(&jw);
409 static void fn_thread_start_fl(const char *file, int line,
410 uint64_t us_elapsed_absolute)
412 const char *event_name = "thread_start";
413 struct json_writer jw = JSON_WRITER_INIT;
415 jw_object_begin(&jw, 0);
416 event_fmt_prepare(event_name, file, line, NULL, &jw);
417 jw_end(&jw);
419 tr2_dst_write_line(&tr2dst_event, &jw.json);
420 jw_release(&jw);
423 static void fn_thread_exit_fl(const char *file, int line,
424 uint64_t us_elapsed_absolute,
425 uint64_t us_elapsed_thread)
427 const char *event_name = "thread_exit";
428 struct json_writer jw = JSON_WRITER_INIT;
429 double t_rel = (double)us_elapsed_thread / 1000000.0;
431 jw_object_begin(&jw, 0);
432 event_fmt_prepare(event_name, file, line, NULL, &jw);
433 jw_object_double(&jw, "t_rel", 6, t_rel);
434 jw_end(&jw);
436 tr2_dst_write_line(&tr2dst_event, &jw.json);
437 jw_release(&jw);
440 static void fn_exec_fl(const char *file, int line, uint64_t us_elapsed_absolute,
441 int exec_id, const char *exe, const char **argv)
443 const char *event_name = "exec";
444 struct json_writer jw = JSON_WRITER_INIT;
446 jw_object_begin(&jw, 0);
447 event_fmt_prepare(event_name, file, line, NULL, &jw);
448 jw_object_intmax(&jw, "exec_id", exec_id);
449 if (exe)
450 jw_object_string(&jw, "exe", exe);
451 jw_object_inline_begin_array(&jw, "argv");
452 jw_array_argv(&jw, argv);
453 jw_end(&jw);
454 jw_end(&jw);
456 tr2_dst_write_line(&tr2dst_event, &jw.json);
457 jw_release(&jw);
460 static void fn_exec_result_fl(const char *file, int line,
461 uint64_t us_elapsed_absolute, int exec_id,
462 int code)
464 const char *event_name = "exec_result";
465 struct json_writer jw = JSON_WRITER_INIT;
467 jw_object_begin(&jw, 0);
468 event_fmt_prepare(event_name, file, line, NULL, &jw);
469 jw_object_intmax(&jw, "exec_id", exec_id);
470 jw_object_intmax(&jw, "code", code);
471 jw_end(&jw);
473 tr2_dst_write_line(&tr2dst_event, &jw.json);
474 jw_release(&jw);
477 static void fn_param_fl(const char *file, int line, const char *param,
478 const char *value)
480 const char *event_name = "def_param";
481 struct json_writer jw = JSON_WRITER_INIT;
483 jw_object_begin(&jw, 0);
484 event_fmt_prepare(event_name, file, line, NULL, &jw);
485 jw_object_string(&jw, "param", param);
486 jw_object_string(&jw, "value", value);
487 jw_end(&jw);
489 tr2_dst_write_line(&tr2dst_event, &jw.json);
490 jw_release(&jw);
493 static void fn_repo_fl(const char *file, int line,
494 const struct repository *repo)
496 const char *event_name = "def_repo";
497 struct json_writer jw = JSON_WRITER_INIT;
499 jw_object_begin(&jw, 0);
500 event_fmt_prepare(event_name, file, line, repo, &jw);
501 jw_object_string(&jw, "worktree", repo->worktree);
502 jw_end(&jw);
504 tr2_dst_write_line(&tr2dst_event, &jw.json);
505 jw_release(&jw);
508 static void fn_region_enter_printf_va_fl(const char *file, int line,
509 uint64_t us_elapsed_absolute,
510 const char *category,
511 const char *label,
512 const struct repository *repo,
513 const char *fmt, va_list ap)
515 const char *event_name = "region_enter";
516 struct tr2tls_thread_ctx *ctx = tr2tls_get_self();
517 if (ctx->nr_open_regions <= tr2env_event_max_nesting_levels) {
518 struct json_writer jw = JSON_WRITER_INIT;
520 jw_object_begin(&jw, 0);
521 event_fmt_prepare(event_name, file, line, repo, &jw);
522 jw_object_intmax(&jw, "nesting", ctx->nr_open_regions);
523 if (category)
524 jw_object_string(&jw, "category", category);
525 if (label)
526 jw_object_string(&jw, "label", label);
527 maybe_add_string_va(&jw, "msg", fmt, ap);
528 jw_end(&jw);
530 tr2_dst_write_line(&tr2dst_event, &jw.json);
531 jw_release(&jw);
535 static void fn_region_leave_printf_va_fl(
536 const char *file, int line, uint64_t us_elapsed_absolute,
537 uint64_t us_elapsed_region, const char *category, const char *label,
538 const struct repository *repo, const char *fmt, va_list ap)
540 const char *event_name = "region_leave";
541 struct tr2tls_thread_ctx *ctx = tr2tls_get_self();
542 if (ctx->nr_open_regions <= tr2env_event_max_nesting_levels) {
543 struct json_writer jw = JSON_WRITER_INIT;
544 double t_rel = (double)us_elapsed_region / 1000000.0;
546 jw_object_begin(&jw, 0);
547 event_fmt_prepare(event_name, file, line, repo, &jw);
548 jw_object_double(&jw, "t_rel", 6, t_rel);
549 jw_object_intmax(&jw, "nesting", ctx->nr_open_regions);
550 if (category)
551 jw_object_string(&jw, "category", category);
552 if (label)
553 jw_object_string(&jw, "label", label);
554 maybe_add_string_va(&jw, "msg", fmt, ap);
555 jw_end(&jw);
557 tr2_dst_write_line(&tr2dst_event, &jw.json);
558 jw_release(&jw);
562 static void fn_data_fl(const char *file, int line, uint64_t us_elapsed_absolute,
563 uint64_t us_elapsed_region, const char *category,
564 const struct repository *repo, const char *key,
565 const char *value)
567 const char *event_name = "data";
568 struct tr2tls_thread_ctx *ctx = tr2tls_get_self();
569 if (ctx->nr_open_regions <= tr2env_event_max_nesting_levels) {
570 struct json_writer jw = JSON_WRITER_INIT;
571 double t_abs = (double)us_elapsed_absolute / 1000000.0;
572 double t_rel = (double)us_elapsed_region / 1000000.0;
574 jw_object_begin(&jw, 0);
575 event_fmt_prepare(event_name, file, line, repo, &jw);
576 jw_object_double(&jw, "t_abs", 6, t_abs);
577 jw_object_double(&jw, "t_rel", 6, t_rel);
578 jw_object_intmax(&jw, "nesting", ctx->nr_open_regions);
579 jw_object_string(&jw, "category", category);
580 jw_object_string(&jw, "key", key);
581 jw_object_string(&jw, "value", value);
582 jw_end(&jw);
584 tr2_dst_write_line(&tr2dst_event, &jw.json);
585 jw_release(&jw);
589 static void fn_data_json_fl(const char *file, int line,
590 uint64_t us_elapsed_absolute,
591 uint64_t us_elapsed_region, const char *category,
592 const struct repository *repo, const char *key,
593 const struct json_writer *value)
595 const char *event_name = "data_json";
596 struct tr2tls_thread_ctx *ctx = tr2tls_get_self();
597 if (ctx->nr_open_regions <= tr2env_event_max_nesting_levels) {
598 struct json_writer jw = JSON_WRITER_INIT;
599 double t_abs = (double)us_elapsed_absolute / 1000000.0;
600 double t_rel = (double)us_elapsed_region / 1000000.0;
602 jw_object_begin(&jw, 0);
603 event_fmt_prepare(event_name, file, line, repo, &jw);
604 jw_object_double(&jw, "t_abs", 6, t_abs);
605 jw_object_double(&jw, "t_rel", 6, t_rel);
606 jw_object_intmax(&jw, "nesting", ctx->nr_open_regions);
607 jw_object_string(&jw, "category", category);
608 jw_object_string(&jw, "key", key);
609 jw_object_sub_jw(&jw, "value", value);
610 jw_end(&jw);
612 tr2_dst_write_line(&tr2dst_event, &jw.json);
613 jw_release(&jw);
617 struct tr2_tgt tr2_tgt_event = {
618 .pdst = &tr2dst_event,
620 .pfn_init = fn_init,
621 .pfn_term = fn_term,
623 .pfn_version_fl = fn_version_fl,
624 .pfn_start_fl = fn_start_fl,
625 .pfn_exit_fl = fn_exit_fl,
626 .pfn_signal = fn_signal,
627 .pfn_atexit = fn_atexit,
628 .pfn_error_va_fl = fn_error_va_fl,
629 .pfn_command_path_fl = fn_command_path_fl,
630 .pfn_command_ancestry_fl = fn_command_ancestry_fl,
631 .pfn_command_name_fl = fn_command_name_fl,
632 .pfn_command_mode_fl = fn_command_mode_fl,
633 .pfn_alias_fl = fn_alias_fl,
634 .pfn_child_start_fl = fn_child_start_fl,
635 .pfn_child_exit_fl = fn_child_exit_fl,
636 .pfn_child_ready_fl = fn_child_ready_fl,
637 .pfn_thread_start_fl = fn_thread_start_fl,
638 .pfn_thread_exit_fl = fn_thread_exit_fl,
639 .pfn_exec_fl = fn_exec_fl,
640 .pfn_exec_result_fl = fn_exec_result_fl,
641 .pfn_param_fl = fn_param_fl,
642 .pfn_repo_fl = fn_repo_fl,
643 .pfn_region_enter_printf_va_fl = fn_region_enter_printf_va_fl,
644 .pfn_region_leave_printf_va_fl = fn_region_leave_printf_va_fl,
645 .pfn_data_fl = fn_data_fl,
646 .pfn_data_json_fl = fn_data_json_fl,
647 .pfn_printf_va_fl = NULL,