]> git.ipfire.org Git - thirdparty/git.git/blame - trace2/tr2_tgt_event.c
The sixth batch
[thirdparty/git.git] / trace2 / tr2_tgt_event.c
CommitLineData
ee4512ed
JH
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"
bce9db6d 9#include "trace2/tr2_sysenv.h"
ee4512ed
JH
10#include "trace2/tr2_tgt.h"
11#include "trace2/tr2_tls.h"
12
87db61a4 13static struct tr2_dst tr2dst_event = { TR2_SYSENV_EVENT, 0, 0, 0, 0 };
ee4512ed
JH
14
15/*
87db61a4
JS
16 * The version number of the JSON data generated by the EVENT target in this
17 * source file. The version should be incremented if new event types are added,
18 * if existing fields are removed, or if there are significant changes in
19 * interpretation of existing events or fields. Smaller changes, such as adding
20 * a new field to an existing event, do not require an increment to the EVENT
21 * format version.
ee4512ed 22 */
87db61a4 23#define TR2_EVENT_VERSION "2"
ee4512ed
JH
24
25/*
26 * Region nesting limit for messages written to the event target.
27 *
28 * The "region_enter" and "region_leave" messages (especially recursive
29 * messages such as those produced while diving the worktree or index)
30 * are primarily intended for the performance target during debugging.
31 *
32 * Some of the outer-most messages, however, may be of interest to the
bce9db6d
JH
33 * event target. Use the TR2_SYSENV_EVENT_NESTING setting to increase
34 * region details in the event target.
ee4512ed 35 */
bce9db6d 36static int tr2env_event_max_nesting_levels = 2;
ee4512ed
JH
37
38/*
bce9db6d 39 * Use the TR2_SYSENV_EVENT_BRIEF to omit the <time>, <file>, and
ee4512ed
JH
40 * <line> fields from most events.
41 */
bce9db6d 42static int tr2env_event_be_brief;
ee4512ed
JH
43
44static int fn_init(void)
45{
46 int want = tr2_dst_trace_want(&tr2dst_event);
bce9db6d 47 int max_nesting;
ee4512ed 48 int want_brief;
bce9db6d
JH
49 const char *nesting;
50 const char *brief;
ee4512ed
JH
51
52 if (!want)
53 return want;
54
bce9db6d
JH
55 nesting = tr2_sysenv_get(TR2_SYSENV_EVENT_NESTING);
56 if (nesting && *nesting && ((max_nesting = atoi(nesting)) > 0))
57 tr2env_event_max_nesting_levels = max_nesting;
ee4512ed 58
bce9db6d
JH
59 brief = tr2_sysenv_get(TR2_SYSENV_EVENT_BRIEF);
60 if (brief && *brief &&
61 ((want_brief = git_parse_maybe_bool(brief)) != -1))
62 tr2env_event_be_brief = want_brief;
ee4512ed
JH
63
64 return want;
65}
66
67static void fn_term(void)
68{
69 tr2_dst_trace_disable(&tr2dst_event);
70}
71
72/*
73 * Append common key-value pairs to the currently open JSON object.
74 * "event:"<event_name>"
75 * "sid":"<sid>"
76 * "thread":"<thread_name>"
77 * "time":"<time>"
78 * "file":"<filename>"
79 * "line":<line_number>
80 * "repo":<repo_id>
81 */
82static void event_fmt_prepare(const char *event_name, const char *file,
83 int line, const struct repository *repo,
84 struct json_writer *jw)
85{
86 struct tr2tls_thread_ctx *ctx = tr2tls_get_self();
87 struct tr2_tbuf tb_now;
88
89 jw_object_string(jw, "event", event_name);
90 jw_object_string(jw, "sid", tr2_sid_get());
91 jw_object_string(jw, "thread", ctx->thread_name.buf);
92
93 /*
94 * In brief mode, only emit <time> on these 2 event types.
95 */
bce9db6d 96 if (!tr2env_event_be_brief || !strcmp(event_name, "version") ||
ee4512ed 97 !strcmp(event_name, "atexit")) {
bad229ae 98 tr2_tbuf_utc_datetime_extended(&tb_now);
ee4512ed
JH
99 jw_object_string(jw, "time", tb_now.buf);
100 }
101
bce9db6d 102 if (!tr2env_event_be_brief && file && *file) {
ee4512ed
JH
103 jw_object_string(jw, "file", file);
104 jw_object_intmax(jw, "line", line);
105 }
106
107 if (repo)
108 jw_object_intmax(jw, "repo", repo->trace2_repo_id);
109}
110
87db61a4
JS
111static void fn_too_many_files_fl(const char *file, int line)
112{
113 const char *event_name = "too_many_files";
114 struct json_writer jw = JSON_WRITER_INIT;
115
116 jw_object_begin(&jw, 0);
117 event_fmt_prepare(event_name, file, line, NULL, &jw);
118 jw_end(&jw);
119
120 tr2_dst_write_line(&tr2dst_event, &jw.json);
121 jw_release(&jw);
122}
123
ee4512ed
JH
124static void fn_version_fl(const char *file, int line)
125{
126 const char *event_name = "version";
127 struct json_writer jw = JSON_WRITER_INIT;
128
129 jw_object_begin(&jw, 0);
130 event_fmt_prepare(event_name, file, line, NULL, &jw);
131 jw_object_string(&jw, "evt", TR2_EVENT_VERSION);
132 jw_object_string(&jw, "exe", git_version_string);
133 jw_end(&jw);
134
135 tr2_dst_write_line(&tr2dst_event, &jw.json);
136 jw_release(&jw);
87db61a4
JS
137
138 if (tr2dst_event.too_many_files)
139 fn_too_many_files_fl(file, line);
ee4512ed
JH
140}
141
39f43177
JH
142static void fn_start_fl(const char *file, int line,
143 uint64_t us_elapsed_absolute, const char **argv)
ee4512ed
JH
144{
145 const char *event_name = "start";
146 struct json_writer jw = JSON_WRITER_INIT;
39f43177 147 double t_abs = (double)us_elapsed_absolute / 1000000.0;
ee4512ed
JH
148
149 jw_object_begin(&jw, 0);
150 event_fmt_prepare(event_name, file, line, NULL, &jw);
39f43177 151 jw_object_double(&jw, "t_abs", 6, t_abs);
ee4512ed
JH
152 jw_object_inline_begin_array(&jw, "argv");
153 jw_array_argv(&jw, argv);
154 jw_end(&jw);
155 jw_end(&jw);
156
157 tr2_dst_write_line(&tr2dst_event, &jw.json);
158 jw_release(&jw);
159}
160
161static void fn_exit_fl(const char *file, int line, uint64_t us_elapsed_absolute,
162 int code)
163{
164 const char *event_name = "exit";
165 struct json_writer jw = JSON_WRITER_INIT;
166 double t_abs = (double)us_elapsed_absolute / 1000000.0;
167
168 jw_object_begin(&jw, 0);
169 event_fmt_prepare(event_name, file, line, NULL, &jw);
170 jw_object_double(&jw, "t_abs", 6, t_abs);
171 jw_object_intmax(&jw, "code", code);
172 jw_end(&jw);
173
174 tr2_dst_write_line(&tr2dst_event, &jw.json);
175 jw_release(&jw);
176}
177
178static void fn_signal(uint64_t us_elapsed_absolute, int signo)
179{
180 const char *event_name = "signal";
181 struct json_writer jw = JSON_WRITER_INIT;
182 double t_abs = (double)us_elapsed_absolute / 1000000.0;
183
184 jw_object_begin(&jw, 0);
185 event_fmt_prepare(event_name, __FILE__, __LINE__, NULL, &jw);
186 jw_object_double(&jw, "t_abs", 6, t_abs);
187 jw_object_intmax(&jw, "signo", signo);
188 jw_end(&jw);
189
190 tr2_dst_write_line(&tr2dst_event, &jw.json);
191 jw_release(&jw);
192}
193
194static void fn_atexit(uint64_t us_elapsed_absolute, int code)
195{
196 const char *event_name = "atexit";
197 struct json_writer jw = JSON_WRITER_INIT;
198 double t_abs = (double)us_elapsed_absolute / 1000000.0;
199
200 jw_object_begin(&jw, 0);
201 event_fmt_prepare(event_name, __FILE__, __LINE__, NULL, &jw);
202 jw_object_double(&jw, "t_abs", 6, t_abs);
203 jw_object_intmax(&jw, "code", code);
204 jw_end(&jw);
205
206 tr2_dst_write_line(&tr2dst_event, &jw.json);
207 jw_release(&jw);
208}
209
210static void maybe_add_string_va(struct json_writer *jw, const char *field_name,
211 const char *fmt, va_list ap)
212{
ad006fe4 213 if (fmt && *fmt) {
ee4512ed
JH
214 va_list copy_ap;
215 struct strbuf buf = STRBUF_INIT;
216
217 va_copy(copy_ap, ap);
218 strbuf_vaddf(&buf, fmt, copy_ap);
219 va_end(copy_ap);
220
221 jw_object_string(jw, field_name, buf.buf);
222 strbuf_release(&buf);
223 return;
224 }
ee4512ed
JH
225}
226
227static void fn_error_va_fl(const char *file, int line, const char *fmt,
228 va_list ap)
229{
230 const char *event_name = "error";
231 struct json_writer jw = JSON_WRITER_INIT;
232
233 jw_object_begin(&jw, 0);
234 event_fmt_prepare(event_name, file, line, NULL, &jw);
235 maybe_add_string_va(&jw, "msg", fmt, ap);
236 /*
237 * Also emit the format string as a field in case
238 * post-processors want to aggregate common error
239 * messages by type without argument fields (such
240 * as pathnames or branch names) cluttering it up.
241 */
242 if (fmt && *fmt)
243 jw_object_string(&jw, "fmt", fmt);
244 jw_end(&jw);
245
246 tr2_dst_write_line(&tr2dst_event, &jw.json);
247 jw_release(&jw);
248}
249
250static void fn_command_path_fl(const char *file, int line, const char *pathname)
251{
252 const char *event_name = "cmd_path";
253 struct json_writer jw = JSON_WRITER_INIT;
254
255 jw_object_begin(&jw, 0);
256 event_fmt_prepare(event_name, file, line, NULL, &jw);
257 jw_object_string(&jw, "path", pathname);
258 jw_end(&jw);
259
260 tr2_dst_write_line(&tr2dst_event, &jw.json);
261 jw_release(&jw);
262}
263
2f732bf1
ES
264static void fn_command_ancestry_fl(const char *file, int line, const char **parent_names)
265{
266 const char *event_name = "cmd_ancestry";
267 const char *parent_name = NULL;
268 struct json_writer jw = JSON_WRITER_INIT;
269
270 jw_object_begin(&jw, 0);
271 event_fmt_prepare(event_name, file, line, NULL, &jw);
272 jw_object_inline_begin_array(&jw, "ancestry");
273
274 while ((parent_name = *parent_names++))
275 jw_array_string(&jw, parent_name);
276
277 jw_end(&jw); /* 'ancestry' array */
278 jw_end(&jw); /* event object */
279
280 tr2_dst_write_line(&tr2dst_event, &jw.json);
281 jw_release(&jw);
282}
283
ee4512ed
JH
284static void fn_command_name_fl(const char *file, int line, const char *name,
285 const char *hierarchy)
286{
287 const char *event_name = "cmd_name";
288 struct json_writer jw = JSON_WRITER_INIT;
289
290 jw_object_begin(&jw, 0);
291 event_fmt_prepare(event_name, file, line, NULL, &jw);
292 jw_object_string(&jw, "name", name);
293 if (hierarchy && *hierarchy)
294 jw_object_string(&jw, "hierarchy", hierarchy);
295 jw_end(&jw);
296
297 tr2_dst_write_line(&tr2dst_event, &jw.json);
298 jw_release(&jw);
299}
300
301static void fn_command_mode_fl(const char *file, int line, const char *mode)
302{
303 const char *event_name = "cmd_mode";
304 struct json_writer jw = JSON_WRITER_INIT;
305
306 jw_object_begin(&jw, 0);
307 event_fmt_prepare(event_name, file, line, NULL, &jw);
308 jw_object_string(&jw, "name", mode);
309 jw_end(&jw);
310
311 tr2_dst_write_line(&tr2dst_event, &jw.json);
312 jw_release(&jw);
313}
314
315static void fn_alias_fl(const char *file, int line, const char *alias,
316 const char **argv)
317{
318 const char *event_name = "alias";
319 struct json_writer jw = JSON_WRITER_INIT;
320
321 jw_object_begin(&jw, 0);
322 event_fmt_prepare(event_name, file, line, NULL, &jw);
323 jw_object_string(&jw, "alias", alias);
324 jw_object_inline_begin_array(&jw, "argv");
325 jw_array_argv(&jw, argv);
326 jw_end(&jw);
327 jw_end(&jw);
328
329 tr2_dst_write_line(&tr2dst_event, &jw.json);
330 jw_release(&jw);
331}
332
333static void fn_child_start_fl(const char *file, int line,
334 uint64_t us_elapsed_absolute,
335 const struct child_process *cmd)
336{
337 const char *event_name = "child_start";
338 struct json_writer jw = JSON_WRITER_INIT;
339
340 jw_object_begin(&jw, 0);
341 event_fmt_prepare(event_name, file, line, NULL, &jw);
342 jw_object_intmax(&jw, "child_id", cmd->trace2_child_id);
343 if (cmd->trace2_hook_name) {
344 jw_object_string(&jw, "child_class", "hook");
345 jw_object_string(&jw, "hook_name", cmd->trace2_hook_name);
346 } else {
347 const char *child_class =
348 cmd->trace2_child_class ? cmd->trace2_child_class : "?";
349 jw_object_string(&jw, "child_class", child_class);
350 }
351 if (cmd->dir)
352 jw_object_string(&jw, "cd", cmd->dir);
353 jw_object_bool(&jw, "use_shell", cmd->use_shell);
354 jw_object_inline_begin_array(&jw, "argv");
355 if (cmd->git_cmd)
356 jw_array_string(&jw, "git");
357 jw_array_argv(&jw, cmd->argv);
358 jw_end(&jw);
359 jw_end(&jw);
360
361 tr2_dst_write_line(&tr2dst_event, &jw.json);
362 jw_release(&jw);
363}
364
365static void fn_child_exit_fl(const char *file, int line,
366 uint64_t us_elapsed_absolute, int cid, int pid,
367 int code, uint64_t us_elapsed_child)
368{
369 const char *event_name = "child_exit";
370 struct json_writer jw = JSON_WRITER_INIT;
371 double t_rel = (double)us_elapsed_child / 1000000.0;
372
373 jw_object_begin(&jw, 0);
374 event_fmt_prepare(event_name, file, line, NULL, &jw);
375 jw_object_intmax(&jw, "child_id", cid);
376 jw_object_intmax(&jw, "pid", pid);
377 jw_object_intmax(&jw, "code", code);
378 jw_object_double(&jw, "t_rel", 6, t_rel);
379 jw_end(&jw);
380
381 tr2_dst_write_line(&tr2dst_event, &jw.json);
382
383 jw_release(&jw);
384}
385
386static void fn_thread_start_fl(const char *file, int line,
387 uint64_t us_elapsed_absolute)
388{
389 const char *event_name = "thread_start";
390 struct json_writer jw = JSON_WRITER_INIT;
391
392 jw_object_begin(&jw, 0);
393 event_fmt_prepare(event_name, file, line, NULL, &jw);
394 jw_end(&jw);
395
396 tr2_dst_write_line(&tr2dst_event, &jw.json);
397 jw_release(&jw);
398}
399
400static void fn_thread_exit_fl(const char *file, int line,
401 uint64_t us_elapsed_absolute,
402 uint64_t us_elapsed_thread)
403{
404 const char *event_name = "thread_exit";
405 struct json_writer jw = JSON_WRITER_INIT;
406 double t_rel = (double)us_elapsed_thread / 1000000.0;
407
408 jw_object_begin(&jw, 0);
409 event_fmt_prepare(event_name, file, line, NULL, &jw);
410 jw_object_double(&jw, "t_rel", 6, t_rel);
411 jw_end(&jw);
412
413 tr2_dst_write_line(&tr2dst_event, &jw.json);
414 jw_release(&jw);
415}
416
417static void fn_exec_fl(const char *file, int line, uint64_t us_elapsed_absolute,
418 int exec_id, const char *exe, const char **argv)
419{
420 const char *event_name = "exec";
421 struct json_writer jw = JSON_WRITER_INIT;
422
423 jw_object_begin(&jw, 0);
424 event_fmt_prepare(event_name, file, line, NULL, &jw);
425 jw_object_intmax(&jw, "exec_id", exec_id);
426 if (exe)
427 jw_object_string(&jw, "exe", exe);
428 jw_object_inline_begin_array(&jw, "argv");
429 jw_array_argv(&jw, argv);
430 jw_end(&jw);
431 jw_end(&jw);
432
433 tr2_dst_write_line(&tr2dst_event, &jw.json);
434 jw_release(&jw);
435}
436
437static void fn_exec_result_fl(const char *file, int line,
438 uint64_t us_elapsed_absolute, int exec_id,
439 int code)
440{
441 const char *event_name = "exec_result";
442 struct json_writer jw = JSON_WRITER_INIT;
443
444 jw_object_begin(&jw, 0);
445 event_fmt_prepare(event_name, file, line, NULL, &jw);
446 jw_object_intmax(&jw, "exec_id", exec_id);
447 jw_object_intmax(&jw, "code", code);
448 jw_end(&jw);
449
450 tr2_dst_write_line(&tr2dst_event, &jw.json);
451 jw_release(&jw);
452}
453
454static void fn_param_fl(const char *file, int line, const char *param,
455 const char *value)
456{
457 const char *event_name = "def_param";
458 struct json_writer jw = JSON_WRITER_INIT;
459
460 jw_object_begin(&jw, 0);
461 event_fmt_prepare(event_name, file, line, NULL, &jw);
462 jw_object_string(&jw, "param", param);
463 jw_object_string(&jw, "value", value);
464 jw_end(&jw);
465
466 tr2_dst_write_line(&tr2dst_event, &jw.json);
467 jw_release(&jw);
468}
469
470static void fn_repo_fl(const char *file, int line,
471 const struct repository *repo)
472{
473 const char *event_name = "def_repo";
474 struct json_writer jw = JSON_WRITER_INIT;
475
476 jw_object_begin(&jw, 0);
477 event_fmt_prepare(event_name, file, line, repo, &jw);
478 jw_object_string(&jw, "worktree", repo->worktree);
479 jw_end(&jw);
480
481 tr2_dst_write_line(&tr2dst_event, &jw.json);
482 jw_release(&jw);
483}
484
485static void fn_region_enter_printf_va_fl(const char *file, int line,
486 uint64_t us_elapsed_absolute,
487 const char *category,
488 const char *label,
489 const struct repository *repo,
490 const char *fmt, va_list ap)
491{
492 const char *event_name = "region_enter";
493 struct tr2tls_thread_ctx *ctx = tr2tls_get_self();
bce9db6d 494 if (ctx->nr_open_regions <= tr2env_event_max_nesting_levels) {
ee4512ed
JH
495 struct json_writer jw = JSON_WRITER_INIT;
496
497 jw_object_begin(&jw, 0);
498 event_fmt_prepare(event_name, file, line, repo, &jw);
499 jw_object_intmax(&jw, "nesting", ctx->nr_open_regions);
500 if (category)
501 jw_object_string(&jw, "category", category);
502 if (label)
503 jw_object_string(&jw, "label", label);
504 maybe_add_string_va(&jw, "msg", fmt, ap);
505 jw_end(&jw);
506
507 tr2_dst_write_line(&tr2dst_event, &jw.json);
508 jw_release(&jw);
509 }
510}
511
512static void fn_region_leave_printf_va_fl(
513 const char *file, int line, uint64_t us_elapsed_absolute,
514 uint64_t us_elapsed_region, const char *category, const char *label,
515 const struct repository *repo, const char *fmt, va_list ap)
516{
517 const char *event_name = "region_leave";
518 struct tr2tls_thread_ctx *ctx = tr2tls_get_self();
bce9db6d 519 if (ctx->nr_open_regions <= tr2env_event_max_nesting_levels) {
ee4512ed
JH
520 struct json_writer jw = JSON_WRITER_INIT;
521 double t_rel = (double)us_elapsed_region / 1000000.0;
522
523 jw_object_begin(&jw, 0);
524 event_fmt_prepare(event_name, file, line, repo, &jw);
525 jw_object_double(&jw, "t_rel", 6, t_rel);
526 jw_object_intmax(&jw, "nesting", ctx->nr_open_regions);
527 if (category)
528 jw_object_string(&jw, "category", category);
529 if (label)
530 jw_object_string(&jw, "label", label);
531 maybe_add_string_va(&jw, "msg", fmt, ap);
532 jw_end(&jw);
533
534 tr2_dst_write_line(&tr2dst_event, &jw.json);
535 jw_release(&jw);
536 }
537}
538
539static void fn_data_fl(const char *file, int line, uint64_t us_elapsed_absolute,
540 uint64_t us_elapsed_region, const char *category,
541 const struct repository *repo, const char *key,
542 const char *value)
543{
544 const char *event_name = "data";
545 struct tr2tls_thread_ctx *ctx = tr2tls_get_self();
bce9db6d 546 if (ctx->nr_open_regions <= tr2env_event_max_nesting_levels) {
ee4512ed
JH
547 struct json_writer jw = JSON_WRITER_INIT;
548 double t_abs = (double)us_elapsed_absolute / 1000000.0;
549 double t_rel = (double)us_elapsed_region / 1000000.0;
550
551 jw_object_begin(&jw, 0);
552 event_fmt_prepare(event_name, file, line, repo, &jw);
553 jw_object_double(&jw, "t_abs", 6, t_abs);
554 jw_object_double(&jw, "t_rel", 6, t_rel);
555 jw_object_intmax(&jw, "nesting", ctx->nr_open_regions);
556 jw_object_string(&jw, "category", category);
557 jw_object_string(&jw, "key", key);
558 jw_object_string(&jw, "value", value);
559 jw_end(&jw);
560
561 tr2_dst_write_line(&tr2dst_event, &jw.json);
562 jw_release(&jw);
563 }
564}
565
566static void fn_data_json_fl(const char *file, int line,
567 uint64_t us_elapsed_absolute,
568 uint64_t us_elapsed_region, const char *category,
569 const struct repository *repo, const char *key,
570 const struct json_writer *value)
571{
572 const char *event_name = "data_json";
573 struct tr2tls_thread_ctx *ctx = tr2tls_get_self();
bce9db6d 574 if (ctx->nr_open_regions <= tr2env_event_max_nesting_levels) {
ee4512ed
JH
575 struct json_writer jw = JSON_WRITER_INIT;
576 double t_abs = (double)us_elapsed_absolute / 1000000.0;
577 double t_rel = (double)us_elapsed_region / 1000000.0;
578
579 jw_object_begin(&jw, 0);
580 event_fmt_prepare(event_name, file, line, repo, &jw);
581 jw_object_double(&jw, "t_abs", 6, t_abs);
582 jw_object_double(&jw, "t_rel", 6, t_rel);
583 jw_object_intmax(&jw, "nesting", ctx->nr_open_regions);
584 jw_object_string(&jw, "category", category);
585 jw_object_string(&jw, "key", key);
586 jw_object_sub_jw(&jw, "value", value);
587 jw_end(&jw);
588
589 tr2_dst_write_line(&tr2dst_event, &jw.json);
590 jw_release(&jw);
591 }
592}
593
594struct tr2_tgt tr2_tgt_event = {
595 &tr2dst_event,
596
597 fn_init,
598 fn_term,
599
600 fn_version_fl,
601 fn_start_fl,
602 fn_exit_fl,
603 fn_signal,
604 fn_atexit,
605 fn_error_va_fl,
606 fn_command_path_fl,
2f732bf1 607 fn_command_ancestry_fl,
ee4512ed
JH
608 fn_command_name_fl,
609 fn_command_mode_fl,
610 fn_alias_fl,
611 fn_child_start_fl,
612 fn_child_exit_fl,
613 fn_thread_start_fl,
614 fn_thread_exit_fl,
615 fn_exec_fl,
616 fn_exec_result_fl,
617 fn_param_fl,
618 fn_repo_fl,
619 fn_region_enter_printf_va_fl,
620 fn_region_leave_printf_va_fl,
621 fn_data_fl,
622 fn_data_json_fl,
623 NULL, /* printf */
624};