Merge tag 'l10n-2.22.0-rnd3' of git://github.com/git-l10n/git-po
[git/debian.git] / trace2 / tr2_tgt_event.c
blobc2852d1bd2bd856d518b5ce499d38e7b13bb452c
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;
209 if (fmt && *fmt) {
210 jw_object_string(jw, field_name, fmt);
211 return;
215 static void fn_error_va_fl(const char *file, int line, const char *fmt,
216 va_list ap)
218 const char *event_name = "error";
219 struct json_writer jw = JSON_WRITER_INIT;
221 jw_object_begin(&jw, 0);
222 event_fmt_prepare(event_name, file, line, NULL, &jw);
223 maybe_add_string_va(&jw, "msg", fmt, ap);
225 * Also emit the format string as a field in case
226 * post-processors want to aggregate common error
227 * messages by type without argument fields (such
228 * as pathnames or branch names) cluttering it up.
230 if (fmt && *fmt)
231 jw_object_string(&jw, "fmt", fmt);
232 jw_end(&jw);
234 tr2_dst_write_line(&tr2dst_event, &jw.json);
235 jw_release(&jw);
238 static void fn_command_path_fl(const char *file, int line, const char *pathname)
240 const char *event_name = "cmd_path";
241 struct json_writer jw = JSON_WRITER_INIT;
243 jw_object_begin(&jw, 0);
244 event_fmt_prepare(event_name, file, line, NULL, &jw);
245 jw_object_string(&jw, "path", pathname);
246 jw_end(&jw);
248 tr2_dst_write_line(&tr2dst_event, &jw.json);
249 jw_release(&jw);
252 static void fn_command_name_fl(const char *file, int line, const char *name,
253 const char *hierarchy)
255 const char *event_name = "cmd_name";
256 struct json_writer jw = JSON_WRITER_INIT;
258 jw_object_begin(&jw, 0);
259 event_fmt_prepare(event_name, file, line, NULL, &jw);
260 jw_object_string(&jw, "name", name);
261 if (hierarchy && *hierarchy)
262 jw_object_string(&jw, "hierarchy", hierarchy);
263 jw_end(&jw);
265 tr2_dst_write_line(&tr2dst_event, &jw.json);
266 jw_release(&jw);
269 static void fn_command_mode_fl(const char *file, int line, const char *mode)
271 const char *event_name = "cmd_mode";
272 struct json_writer jw = JSON_WRITER_INIT;
274 jw_object_begin(&jw, 0);
275 event_fmt_prepare(event_name, file, line, NULL, &jw);
276 jw_object_string(&jw, "name", mode);
277 jw_end(&jw);
279 tr2_dst_write_line(&tr2dst_event, &jw.json);
280 jw_release(&jw);
283 static void fn_alias_fl(const char *file, int line, const char *alias,
284 const char **argv)
286 const char *event_name = "alias";
287 struct json_writer jw = JSON_WRITER_INIT;
289 jw_object_begin(&jw, 0);
290 event_fmt_prepare(event_name, file, line, NULL, &jw);
291 jw_object_string(&jw, "alias", alias);
292 jw_object_inline_begin_array(&jw, "argv");
293 jw_array_argv(&jw, argv);
294 jw_end(&jw);
295 jw_end(&jw);
297 tr2_dst_write_line(&tr2dst_event, &jw.json);
298 jw_release(&jw);
301 static void fn_child_start_fl(const char *file, int line,
302 uint64_t us_elapsed_absolute,
303 const struct child_process *cmd)
305 const char *event_name = "child_start";
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_intmax(&jw, "child_id", cmd->trace2_child_id);
311 if (cmd->trace2_hook_name) {
312 jw_object_string(&jw, "child_class", "hook");
313 jw_object_string(&jw, "hook_name", cmd->trace2_hook_name);
314 } else {
315 const char *child_class =
316 cmd->trace2_child_class ? cmd->trace2_child_class : "?";
317 jw_object_string(&jw, "child_class", child_class);
319 if (cmd->dir)
320 jw_object_string(&jw, "cd", cmd->dir);
321 jw_object_bool(&jw, "use_shell", cmd->use_shell);
322 jw_object_inline_begin_array(&jw, "argv");
323 if (cmd->git_cmd)
324 jw_array_string(&jw, "git");
325 jw_array_argv(&jw, cmd->argv);
326 jw_end(&jw);
327 jw_end(&jw);
329 tr2_dst_write_line(&tr2dst_event, &jw.json);
330 jw_release(&jw);
333 static void fn_child_exit_fl(const char *file, int line,
334 uint64_t us_elapsed_absolute, int cid, int pid,
335 int code, uint64_t us_elapsed_child)
337 const char *event_name = "child_exit";
338 struct json_writer jw = JSON_WRITER_INIT;
339 double t_rel = (double)us_elapsed_child / 1000000.0;
341 jw_object_begin(&jw, 0);
342 event_fmt_prepare(event_name, file, line, NULL, &jw);
343 jw_object_intmax(&jw, "child_id", cid);
344 jw_object_intmax(&jw, "pid", pid);
345 jw_object_intmax(&jw, "code", code);
346 jw_object_double(&jw, "t_rel", 6, t_rel);
347 jw_end(&jw);
349 tr2_dst_write_line(&tr2dst_event, &jw.json);
351 jw_release(&jw);
354 static void fn_thread_start_fl(const char *file, int line,
355 uint64_t us_elapsed_absolute)
357 const char *event_name = "thread_start";
358 struct json_writer jw = JSON_WRITER_INIT;
360 jw_object_begin(&jw, 0);
361 event_fmt_prepare(event_name, file, line, NULL, &jw);
362 jw_end(&jw);
364 tr2_dst_write_line(&tr2dst_event, &jw.json);
365 jw_release(&jw);
368 static void fn_thread_exit_fl(const char *file, int line,
369 uint64_t us_elapsed_absolute,
370 uint64_t us_elapsed_thread)
372 const char *event_name = "thread_exit";
373 struct json_writer jw = JSON_WRITER_INIT;
374 double t_rel = (double)us_elapsed_thread / 1000000.0;
376 jw_object_begin(&jw, 0);
377 event_fmt_prepare(event_name, file, line, NULL, &jw);
378 jw_object_double(&jw, "t_rel", 6, t_rel);
379 jw_end(&jw);
381 tr2_dst_write_line(&tr2dst_event, &jw.json);
382 jw_release(&jw);
385 static void fn_exec_fl(const char *file, int line, uint64_t us_elapsed_absolute,
386 int exec_id, const char *exe, const char **argv)
388 const char *event_name = "exec";
389 struct json_writer jw = JSON_WRITER_INIT;
391 jw_object_begin(&jw, 0);
392 event_fmt_prepare(event_name, file, line, NULL, &jw);
393 jw_object_intmax(&jw, "exec_id", exec_id);
394 if (exe)
395 jw_object_string(&jw, "exe", exe);
396 jw_object_inline_begin_array(&jw, "argv");
397 jw_array_argv(&jw, argv);
398 jw_end(&jw);
399 jw_end(&jw);
401 tr2_dst_write_line(&tr2dst_event, &jw.json);
402 jw_release(&jw);
405 static void fn_exec_result_fl(const char *file, int line,
406 uint64_t us_elapsed_absolute, int exec_id,
407 int code)
409 const char *event_name = "exec_result";
410 struct json_writer jw = JSON_WRITER_INIT;
412 jw_object_begin(&jw, 0);
413 event_fmt_prepare(event_name, file, line, NULL, &jw);
414 jw_object_intmax(&jw, "exec_id", exec_id);
415 jw_object_intmax(&jw, "code", code);
416 jw_end(&jw);
418 tr2_dst_write_line(&tr2dst_event, &jw.json);
419 jw_release(&jw);
422 static void fn_param_fl(const char *file, int line, const char *param,
423 const char *value)
425 const char *event_name = "def_param";
426 struct json_writer jw = JSON_WRITER_INIT;
428 jw_object_begin(&jw, 0);
429 event_fmt_prepare(event_name, file, line, NULL, &jw);
430 jw_object_string(&jw, "param", param);
431 jw_object_string(&jw, "value", value);
432 jw_end(&jw);
434 tr2_dst_write_line(&tr2dst_event, &jw.json);
435 jw_release(&jw);
438 static void fn_repo_fl(const char *file, int line,
439 const struct repository *repo)
441 const char *event_name = "def_repo";
442 struct json_writer jw = JSON_WRITER_INIT;
444 jw_object_begin(&jw, 0);
445 event_fmt_prepare(event_name, file, line, repo, &jw);
446 jw_object_string(&jw, "worktree", repo->worktree);
447 jw_end(&jw);
449 tr2_dst_write_line(&tr2dst_event, &jw.json);
450 jw_release(&jw);
453 static void fn_region_enter_printf_va_fl(const char *file, int line,
454 uint64_t us_elapsed_absolute,
455 const char *category,
456 const char *label,
457 const struct repository *repo,
458 const char *fmt, va_list ap)
460 const char *event_name = "region_enter";
461 struct tr2tls_thread_ctx *ctx = tr2tls_get_self();
462 if (ctx->nr_open_regions <= tr2env_event_max_nesting_levels) {
463 struct json_writer jw = JSON_WRITER_INIT;
465 jw_object_begin(&jw, 0);
466 event_fmt_prepare(event_name, file, line, repo, &jw);
467 jw_object_intmax(&jw, "nesting", ctx->nr_open_regions);
468 if (category)
469 jw_object_string(&jw, "category", category);
470 if (label)
471 jw_object_string(&jw, "label", label);
472 maybe_add_string_va(&jw, "msg", fmt, ap);
473 jw_end(&jw);
475 tr2_dst_write_line(&tr2dst_event, &jw.json);
476 jw_release(&jw);
480 static void fn_region_leave_printf_va_fl(
481 const char *file, int line, uint64_t us_elapsed_absolute,
482 uint64_t us_elapsed_region, const char *category, const char *label,
483 const struct repository *repo, const char *fmt, va_list ap)
485 const char *event_name = "region_leave";
486 struct tr2tls_thread_ctx *ctx = tr2tls_get_self();
487 if (ctx->nr_open_regions <= tr2env_event_max_nesting_levels) {
488 struct json_writer jw = JSON_WRITER_INIT;
489 double t_rel = (double)us_elapsed_region / 1000000.0;
491 jw_object_begin(&jw, 0);
492 event_fmt_prepare(event_name, file, line, repo, &jw);
493 jw_object_double(&jw, "t_rel", 6, t_rel);
494 jw_object_intmax(&jw, "nesting", ctx->nr_open_regions);
495 if (category)
496 jw_object_string(&jw, "category", category);
497 if (label)
498 jw_object_string(&jw, "label", label);
499 maybe_add_string_va(&jw, "msg", fmt, ap);
500 jw_end(&jw);
502 tr2_dst_write_line(&tr2dst_event, &jw.json);
503 jw_release(&jw);
507 static void fn_data_fl(const char *file, int line, uint64_t us_elapsed_absolute,
508 uint64_t us_elapsed_region, const char *category,
509 const struct repository *repo, const char *key,
510 const char *value)
512 const char *event_name = "data";
513 struct tr2tls_thread_ctx *ctx = tr2tls_get_self();
514 if (ctx->nr_open_regions <= tr2env_event_max_nesting_levels) {
515 struct json_writer jw = JSON_WRITER_INIT;
516 double t_abs = (double)us_elapsed_absolute / 1000000.0;
517 double t_rel = (double)us_elapsed_region / 1000000.0;
519 jw_object_begin(&jw, 0);
520 event_fmt_prepare(event_name, file, line, repo, &jw);
521 jw_object_double(&jw, "t_abs", 6, t_abs);
522 jw_object_double(&jw, "t_rel", 6, t_rel);
523 jw_object_intmax(&jw, "nesting", ctx->nr_open_regions);
524 jw_object_string(&jw, "category", category);
525 jw_object_string(&jw, "key", key);
526 jw_object_string(&jw, "value", value);
527 jw_end(&jw);
529 tr2_dst_write_line(&tr2dst_event, &jw.json);
530 jw_release(&jw);
534 static void fn_data_json_fl(const char *file, int line,
535 uint64_t us_elapsed_absolute,
536 uint64_t us_elapsed_region, const char *category,
537 const struct repository *repo, const char *key,
538 const struct json_writer *value)
540 const char *event_name = "data_json";
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_abs = (double)us_elapsed_absolute / 1000000.0;
545 double t_rel = (double)us_elapsed_region / 1000000.0;
547 jw_object_begin(&jw, 0);
548 event_fmt_prepare(event_name, file, line, repo, &jw);
549 jw_object_double(&jw, "t_abs", 6, t_abs);
550 jw_object_double(&jw, "t_rel", 6, t_rel);
551 jw_object_intmax(&jw, "nesting", ctx->nr_open_regions);
552 jw_object_string(&jw, "category", category);
553 jw_object_string(&jw, "key", key);
554 jw_object_sub_jw(&jw, "value", value);
555 jw_end(&jw);
557 tr2_dst_write_line(&tr2dst_event, &jw.json);
558 jw_release(&jw);
562 struct tr2_tgt tr2_tgt_event = {
563 &tr2dst_event,
565 fn_init,
566 fn_term,
568 fn_version_fl,
569 fn_start_fl,
570 fn_exit_fl,
571 fn_signal,
572 fn_atexit,
573 fn_error_va_fl,
574 fn_command_path_fl,
575 fn_command_name_fl,
576 fn_command_mode_fl,
577 fn_alias_fl,
578 fn_child_start_fl,
579 fn_child_exit_fl,
580 fn_thread_start_fl,
581 fn_thread_exit_fl,
582 fn_exec_fl,
583 fn_exec_result_fl,
584 fn_param_fl,
585 fn_repo_fl,
586 fn_region_enter_printf_va_fl,
587 fn_region_leave_printf_va_fl,
588 fn_data_fl,
589 fn_data_json_fl,
590 NULL, /* printf */