Fifth batch
[git.git] / trace2 / tr2_tgt_event.c
blob9bcac20d1b5a3da6a996c85adbaf4eb19ffb9f15
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 = { TR2_SYSENV_EVENT, 0, 0, 0 };
16 * The version number of the JSON data generated by the EVENT target
17 * in this source file. Update this if you make a significant change
18 * to the JSON fields or message structure. You probably do not need
19 * to update this if you just add another call to one of the existing
20 * TRACE2 API methods.
22 #define TR2_EVENT_VERSION "1"
25 * Region nesting limit for messages written to the event target.
27 * The "region_enter" and "region_leave" messages (especially recursive
28 * messages such as those produced while diving the worktree or index)
29 * are primarily intended for the performance target during debugging.
31 * Some of the outer-most messages, however, may be of interest to the
32 * event target. Use the TR2_SYSENV_EVENT_NESTING setting to increase
33 * region details in the event target.
35 static int tr2env_event_max_nesting_levels = 2;
38 * Use the TR2_SYSENV_EVENT_BRIEF to omit the <time>, <file>, and
39 * <line> fields from most events.
41 static int tr2env_event_be_brief;
43 static int fn_init(void)
45 int want = tr2_dst_trace_want(&tr2dst_event);
46 int max_nesting;
47 int want_brief;
48 const char *nesting;
49 const char *brief;
51 if (!want)
52 return want;
54 nesting = tr2_sysenv_get(TR2_SYSENV_EVENT_NESTING);
55 if (nesting && *nesting && ((max_nesting = atoi(nesting)) > 0))
56 tr2env_event_max_nesting_levels = max_nesting;
58 brief = tr2_sysenv_get(TR2_SYSENV_EVENT_BRIEF);
59 if (brief && *brief &&
60 ((want_brief = git_parse_maybe_bool(brief)) != -1))
61 tr2env_event_be_brief = want_brief;
63 return want;
66 static void fn_term(void)
68 tr2_dst_trace_disable(&tr2dst_event);
72 * Append common key-value pairs to the currently open JSON object.
73 * "event:"<event_name>"
74 * "sid":"<sid>"
75 * "thread":"<thread_name>"
76 * "time":"<time>"
77 * "file":"<filename>"
78 * "line":<line_number>
79 * "repo":<repo_id>
81 static void event_fmt_prepare(const char *event_name, const char *file,
82 int line, const struct repository *repo,
83 struct json_writer *jw)
85 struct tr2tls_thread_ctx *ctx = tr2tls_get_self();
86 struct tr2_tbuf tb_now;
88 jw_object_string(jw, "event", event_name);
89 jw_object_string(jw, "sid", tr2_sid_get());
90 jw_object_string(jw, "thread", ctx->thread_name.buf);
93 * In brief mode, only emit <time> on these 2 event types.
95 if (!tr2env_event_be_brief || !strcmp(event_name, "version") ||
96 !strcmp(event_name, "atexit")) {
97 tr2_tbuf_utc_datetime_extended(&tb_now);
98 jw_object_string(jw, "time", tb_now.buf);
101 if (!tr2env_event_be_brief && file && *file) {
102 jw_object_string(jw, "file", file);
103 jw_object_intmax(jw, "line", line);
106 if (repo)
107 jw_object_intmax(jw, "repo", repo->trace2_repo_id);
110 static void fn_version_fl(const char *file, int line)
112 const char *event_name = "version";
113 struct json_writer jw = JSON_WRITER_INIT;
115 jw_object_begin(&jw, 0);
116 event_fmt_prepare(event_name, file, line, NULL, &jw);
117 jw_object_string(&jw, "evt", TR2_EVENT_VERSION);
118 jw_object_string(&jw, "exe", git_version_string);
119 jw_end(&jw);
121 tr2_dst_write_line(&tr2dst_event, &jw.json);
122 jw_release(&jw);
125 static void fn_start_fl(const char *file, int line,
126 uint64_t us_elapsed_absolute, const char **argv)
128 const char *event_name = "start";
129 struct json_writer jw = JSON_WRITER_INIT;
130 double t_abs = (double)us_elapsed_absolute / 1000000.0;
132 jw_object_begin(&jw, 0);
133 event_fmt_prepare(event_name, file, line, NULL, &jw);
134 jw_object_double(&jw, "t_abs", 6, t_abs);
135 jw_object_inline_begin_array(&jw, "argv");
136 jw_array_argv(&jw, argv);
137 jw_end(&jw);
138 jw_end(&jw);
140 tr2_dst_write_line(&tr2dst_event, &jw.json);
141 jw_release(&jw);
144 static void fn_exit_fl(const char *file, int line, uint64_t us_elapsed_absolute,
145 int code)
147 const char *event_name = "exit";
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_intmax(&jw, "code", code);
155 jw_end(&jw);
157 tr2_dst_write_line(&tr2dst_event, &jw.json);
158 jw_release(&jw);
161 static void fn_signal(uint64_t us_elapsed_absolute, int signo)
163 const char *event_name = "signal";
164 struct json_writer jw = JSON_WRITER_INIT;
165 double t_abs = (double)us_elapsed_absolute / 1000000.0;
167 jw_object_begin(&jw, 0);
168 event_fmt_prepare(event_name, __FILE__, __LINE__, NULL, &jw);
169 jw_object_double(&jw, "t_abs", 6, t_abs);
170 jw_object_intmax(&jw, "signo", signo);
171 jw_end(&jw);
173 tr2_dst_write_line(&tr2dst_event, &jw.json);
174 jw_release(&jw);
177 static void fn_atexit(uint64_t us_elapsed_absolute, int code)
179 const char *event_name = "atexit";
180 struct json_writer jw = JSON_WRITER_INIT;
181 double t_abs = (double)us_elapsed_absolute / 1000000.0;
183 jw_object_begin(&jw, 0);
184 event_fmt_prepare(event_name, __FILE__, __LINE__, NULL, &jw);
185 jw_object_double(&jw, "t_abs", 6, t_abs);
186 jw_object_intmax(&jw, "code", code);
187 jw_end(&jw);
189 tr2_dst_write_line(&tr2dst_event, &jw.json);
190 jw_release(&jw);
193 static void maybe_add_string_va(struct json_writer *jw, const char *field_name,
194 const char *fmt, va_list ap)
196 if (fmt && *fmt) {
197 va_list copy_ap;
198 struct strbuf buf = STRBUF_INIT;
200 va_copy(copy_ap, ap);
201 strbuf_vaddf(&buf, fmt, copy_ap);
202 va_end(copy_ap);
204 jw_object_string(jw, field_name, buf.buf);
205 strbuf_release(&buf);
206 return;
210 static void fn_error_va_fl(const char *file, int line, const char *fmt,
211 va_list ap)
213 const char *event_name = "error";
214 struct json_writer jw = JSON_WRITER_INIT;
216 jw_object_begin(&jw, 0);
217 event_fmt_prepare(event_name, file, line, NULL, &jw);
218 maybe_add_string_va(&jw, "msg", fmt, ap);
220 * Also emit the format string as a field in case
221 * post-processors want to aggregate common error
222 * messages by type without argument fields (such
223 * as pathnames or branch names) cluttering it up.
225 if (fmt && *fmt)
226 jw_object_string(&jw, "fmt", fmt);
227 jw_end(&jw);
229 tr2_dst_write_line(&tr2dst_event, &jw.json);
230 jw_release(&jw);
233 static void fn_command_path_fl(const char *file, int line, const char *pathname)
235 const char *event_name = "cmd_path";
236 struct json_writer jw = JSON_WRITER_INIT;
238 jw_object_begin(&jw, 0);
239 event_fmt_prepare(event_name, file, line, NULL, &jw);
240 jw_object_string(&jw, "path", pathname);
241 jw_end(&jw);
243 tr2_dst_write_line(&tr2dst_event, &jw.json);
244 jw_release(&jw);
247 static void fn_command_name_fl(const char *file, int line, const char *name,
248 const char *hierarchy)
250 const char *event_name = "cmd_name";
251 struct json_writer jw = JSON_WRITER_INIT;
253 jw_object_begin(&jw, 0);
254 event_fmt_prepare(event_name, file, line, NULL, &jw);
255 jw_object_string(&jw, "name", name);
256 if (hierarchy && *hierarchy)
257 jw_object_string(&jw, "hierarchy", hierarchy);
258 jw_end(&jw);
260 tr2_dst_write_line(&tr2dst_event, &jw.json);
261 jw_release(&jw);
264 static void fn_command_mode_fl(const char *file, int line, const char *mode)
266 const char *event_name = "cmd_mode";
267 struct json_writer jw = JSON_WRITER_INIT;
269 jw_object_begin(&jw, 0);
270 event_fmt_prepare(event_name, file, line, NULL, &jw);
271 jw_object_string(&jw, "name", mode);
272 jw_end(&jw);
274 tr2_dst_write_line(&tr2dst_event, &jw.json);
275 jw_release(&jw);
278 static void fn_alias_fl(const char *file, int line, const char *alias,
279 const char **argv)
281 const char *event_name = "alias";
282 struct json_writer jw = JSON_WRITER_INIT;
284 jw_object_begin(&jw, 0);
285 event_fmt_prepare(event_name, file, line, NULL, &jw);
286 jw_object_string(&jw, "alias", alias);
287 jw_object_inline_begin_array(&jw, "argv");
288 jw_array_argv(&jw, argv);
289 jw_end(&jw);
290 jw_end(&jw);
292 tr2_dst_write_line(&tr2dst_event, &jw.json);
293 jw_release(&jw);
296 static void fn_child_start_fl(const char *file, int line,
297 uint64_t us_elapsed_absolute,
298 const struct child_process *cmd)
300 const char *event_name = "child_start";
301 struct json_writer jw = JSON_WRITER_INIT;
303 jw_object_begin(&jw, 0);
304 event_fmt_prepare(event_name, file, line, NULL, &jw);
305 jw_object_intmax(&jw, "child_id", cmd->trace2_child_id);
306 if (cmd->trace2_hook_name) {
307 jw_object_string(&jw, "child_class", "hook");
308 jw_object_string(&jw, "hook_name", cmd->trace2_hook_name);
309 } else {
310 const char *child_class =
311 cmd->trace2_child_class ? cmd->trace2_child_class : "?";
312 jw_object_string(&jw, "child_class", child_class);
314 if (cmd->dir)
315 jw_object_string(&jw, "cd", cmd->dir);
316 jw_object_bool(&jw, "use_shell", cmd->use_shell);
317 jw_object_inline_begin_array(&jw, "argv");
318 if (cmd->git_cmd)
319 jw_array_string(&jw, "git");
320 jw_array_argv(&jw, cmd->argv);
321 jw_end(&jw);
322 jw_end(&jw);
324 tr2_dst_write_line(&tr2dst_event, &jw.json);
325 jw_release(&jw);
328 static void fn_child_exit_fl(const char *file, int line,
329 uint64_t us_elapsed_absolute, int cid, int pid,
330 int code, uint64_t us_elapsed_child)
332 const char *event_name = "child_exit";
333 struct json_writer jw = JSON_WRITER_INIT;
334 double t_rel = (double)us_elapsed_child / 1000000.0;
336 jw_object_begin(&jw, 0);
337 event_fmt_prepare(event_name, file, line, NULL, &jw);
338 jw_object_intmax(&jw, "child_id", cid);
339 jw_object_intmax(&jw, "pid", pid);
340 jw_object_intmax(&jw, "code", code);
341 jw_object_double(&jw, "t_rel", 6, t_rel);
342 jw_end(&jw);
344 tr2_dst_write_line(&tr2dst_event, &jw.json);
346 jw_release(&jw);
349 static void fn_thread_start_fl(const char *file, int line,
350 uint64_t us_elapsed_absolute)
352 const char *event_name = "thread_start";
353 struct json_writer jw = JSON_WRITER_INIT;
355 jw_object_begin(&jw, 0);
356 event_fmt_prepare(event_name, file, line, NULL, &jw);
357 jw_end(&jw);
359 tr2_dst_write_line(&tr2dst_event, &jw.json);
360 jw_release(&jw);
363 static void fn_thread_exit_fl(const char *file, int line,
364 uint64_t us_elapsed_absolute,
365 uint64_t us_elapsed_thread)
367 const char *event_name = "thread_exit";
368 struct json_writer jw = JSON_WRITER_INIT;
369 double t_rel = (double)us_elapsed_thread / 1000000.0;
371 jw_object_begin(&jw, 0);
372 event_fmt_prepare(event_name, file, line, NULL, &jw);
373 jw_object_double(&jw, "t_rel", 6, t_rel);
374 jw_end(&jw);
376 tr2_dst_write_line(&tr2dst_event, &jw.json);
377 jw_release(&jw);
380 static void fn_exec_fl(const char *file, int line, uint64_t us_elapsed_absolute,
381 int exec_id, const char *exe, const char **argv)
383 const char *event_name = "exec";
384 struct json_writer jw = JSON_WRITER_INIT;
386 jw_object_begin(&jw, 0);
387 event_fmt_prepare(event_name, file, line, NULL, &jw);
388 jw_object_intmax(&jw, "exec_id", exec_id);
389 if (exe)
390 jw_object_string(&jw, "exe", exe);
391 jw_object_inline_begin_array(&jw, "argv");
392 jw_array_argv(&jw, argv);
393 jw_end(&jw);
394 jw_end(&jw);
396 tr2_dst_write_line(&tr2dst_event, &jw.json);
397 jw_release(&jw);
400 static void fn_exec_result_fl(const char *file, int line,
401 uint64_t us_elapsed_absolute, int exec_id,
402 int code)
404 const char *event_name = "exec_result";
405 struct json_writer jw = JSON_WRITER_INIT;
407 jw_object_begin(&jw, 0);
408 event_fmt_prepare(event_name, file, line, NULL, &jw);
409 jw_object_intmax(&jw, "exec_id", exec_id);
410 jw_object_intmax(&jw, "code", code);
411 jw_end(&jw);
413 tr2_dst_write_line(&tr2dst_event, &jw.json);
414 jw_release(&jw);
417 static void fn_param_fl(const char *file, int line, const char *param,
418 const char *value)
420 const char *event_name = "def_param";
421 struct json_writer jw = JSON_WRITER_INIT;
423 jw_object_begin(&jw, 0);
424 event_fmt_prepare(event_name, file, line, NULL, &jw);
425 jw_object_string(&jw, "param", param);
426 jw_object_string(&jw, "value", value);
427 jw_end(&jw);
429 tr2_dst_write_line(&tr2dst_event, &jw.json);
430 jw_release(&jw);
433 static void fn_repo_fl(const char *file, int line,
434 const struct repository *repo)
436 const char *event_name = "def_repo";
437 struct json_writer jw = JSON_WRITER_INIT;
439 jw_object_begin(&jw, 0);
440 event_fmt_prepare(event_name, file, line, repo, &jw);
441 jw_object_string(&jw, "worktree", repo->worktree);
442 jw_end(&jw);
444 tr2_dst_write_line(&tr2dst_event, &jw.json);
445 jw_release(&jw);
448 static void fn_region_enter_printf_va_fl(const char *file, int line,
449 uint64_t us_elapsed_absolute,
450 const char *category,
451 const char *label,
452 const struct repository *repo,
453 const char *fmt, va_list ap)
455 const char *event_name = "region_enter";
456 struct tr2tls_thread_ctx *ctx = tr2tls_get_self();
457 if (ctx->nr_open_regions <= tr2env_event_max_nesting_levels) {
458 struct json_writer jw = JSON_WRITER_INIT;
460 jw_object_begin(&jw, 0);
461 event_fmt_prepare(event_name, file, line, repo, &jw);
462 jw_object_intmax(&jw, "nesting", ctx->nr_open_regions);
463 if (category)
464 jw_object_string(&jw, "category", category);
465 if (label)
466 jw_object_string(&jw, "label", label);
467 maybe_add_string_va(&jw, "msg", fmt, ap);
468 jw_end(&jw);
470 tr2_dst_write_line(&tr2dst_event, &jw.json);
471 jw_release(&jw);
475 static void fn_region_leave_printf_va_fl(
476 const char *file, int line, uint64_t us_elapsed_absolute,
477 uint64_t us_elapsed_region, const char *category, const char *label,
478 const struct repository *repo, const char *fmt, va_list ap)
480 const char *event_name = "region_leave";
481 struct tr2tls_thread_ctx *ctx = tr2tls_get_self();
482 if (ctx->nr_open_regions <= tr2env_event_max_nesting_levels) {
483 struct json_writer jw = JSON_WRITER_INIT;
484 double t_rel = (double)us_elapsed_region / 1000000.0;
486 jw_object_begin(&jw, 0);
487 event_fmt_prepare(event_name, file, line, repo, &jw);
488 jw_object_double(&jw, "t_rel", 6, t_rel);
489 jw_object_intmax(&jw, "nesting", ctx->nr_open_regions);
490 if (category)
491 jw_object_string(&jw, "category", category);
492 if (label)
493 jw_object_string(&jw, "label", label);
494 maybe_add_string_va(&jw, "msg", fmt, ap);
495 jw_end(&jw);
497 tr2_dst_write_line(&tr2dst_event, &jw.json);
498 jw_release(&jw);
502 static void fn_data_fl(const char *file, int line, uint64_t us_elapsed_absolute,
503 uint64_t us_elapsed_region, const char *category,
504 const struct repository *repo, const char *key,
505 const char *value)
507 const char *event_name = "data";
508 struct tr2tls_thread_ctx *ctx = tr2tls_get_self();
509 if (ctx->nr_open_regions <= tr2env_event_max_nesting_levels) {
510 struct json_writer jw = JSON_WRITER_INIT;
511 double t_abs = (double)us_elapsed_absolute / 1000000.0;
512 double t_rel = (double)us_elapsed_region / 1000000.0;
514 jw_object_begin(&jw, 0);
515 event_fmt_prepare(event_name, file, line, repo, &jw);
516 jw_object_double(&jw, "t_abs", 6, t_abs);
517 jw_object_double(&jw, "t_rel", 6, t_rel);
518 jw_object_intmax(&jw, "nesting", ctx->nr_open_regions);
519 jw_object_string(&jw, "category", category);
520 jw_object_string(&jw, "key", key);
521 jw_object_string(&jw, "value", value);
522 jw_end(&jw);
524 tr2_dst_write_line(&tr2dst_event, &jw.json);
525 jw_release(&jw);
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)
535 const char *event_name = "data_json";
536 struct tr2tls_thread_ctx *ctx = tr2tls_get_self();
537 if (ctx->nr_open_regions <= tr2env_event_max_nesting_levels) {
538 struct json_writer jw = JSON_WRITER_INIT;
539 double t_abs = (double)us_elapsed_absolute / 1000000.0;
540 double t_rel = (double)us_elapsed_region / 1000000.0;
542 jw_object_begin(&jw, 0);
543 event_fmt_prepare(event_name, file, line, repo, &jw);
544 jw_object_double(&jw, "t_abs", 6, t_abs);
545 jw_object_double(&jw, "t_rel", 6, t_rel);
546 jw_object_intmax(&jw, "nesting", ctx->nr_open_regions);
547 jw_object_string(&jw, "category", category);
548 jw_object_string(&jw, "key", key);
549 jw_object_sub_jw(&jw, "value", value);
550 jw_end(&jw);
552 tr2_dst_write_line(&tr2dst_event, &jw.json);
553 jw_release(&jw);
557 struct tr2_tgt tr2_tgt_event = {
558 &tr2dst_event,
560 fn_init,
561 fn_term,
563 fn_version_fl,
564 fn_start_fl,
565 fn_exit_fl,
566 fn_signal,
567 fn_atexit,
568 fn_error_va_fl,
569 fn_command_path_fl,
570 fn_command_name_fl,
571 fn_command_mode_fl,
572 fn_alias_fl,
573 fn_child_start_fl,
574 fn_child_exit_fl,
575 fn_thread_start_fl,
576 fn_thread_exit_fl,
577 fn_exec_fl,
578 fn_exec_result_fl,
579 fn_param_fl,
580 fn_repo_fl,
581 fn_region_enter_printf_va_fl,
582 fn_region_leave_printf_va_fl,
583 fn_data_fl,
584 fn_data_json_fl,
585 NULL, /* printf */