]> git.ipfire.org Git - thirdparty/git.git/blob - trace2/tr2_tgt_event.c
Merge branch 'en/fetch-negotiation-default-fix'
[thirdparty/git.git] / trace2 / tr2_tgt_event.c
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"
12
13 static struct tr2_dst tr2dst_event = { TR2_SYSENV_EVENT, 0, 0, 0, 0 };
14
15 /*
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.
22 */
23 #define TR2_EVENT_VERSION "3"
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
33 * event target. Use the TR2_SYSENV_EVENT_NESTING setting to increase
34 * region details in the event target.
35 */
36 static int tr2env_event_max_nesting_levels = 2;
37
38 /*
39 * Use the TR2_SYSENV_EVENT_BRIEF to omit the <time>, <file>, and
40 * <line> fields from most events.
41 */
42 static int tr2env_event_be_brief;
43
44 static int fn_init(void)
45 {
46 int want = tr2_dst_trace_want(&tr2dst_event);
47 int max_nesting;
48 int want_brief;
49 const char *nesting;
50 const char *brief;
51
52 if (!want)
53 return want;
54
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;
58
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;
63
64 return want;
65 }
66
67 static 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 */
82 static 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 */
96 if (!tr2env_event_be_brief || !strcmp(event_name, "version") ||
97 !strcmp(event_name, "atexit")) {
98 tr2_tbuf_utc_datetime_extended(&tb_now);
99 jw_object_string(jw, "time", tb_now.buf);
100 }
101
102 if (!tr2env_event_be_brief && file && *file) {
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
111 static 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
124 static 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);
137
138 if (tr2dst_event.too_many_files)
139 fn_too_many_files_fl(file, line);
140 }
141
142 static void fn_start_fl(const char *file, int line,
143 uint64_t us_elapsed_absolute, const char **argv)
144 {
145 const char *event_name = "start";
146 struct json_writer jw = JSON_WRITER_INIT;
147 double t_abs = (double)us_elapsed_absolute / 1000000.0;
148
149 jw_object_begin(&jw, 0);
150 event_fmt_prepare(event_name, file, line, NULL, &jw);
151 jw_object_double(&jw, "t_abs", 6, t_abs);
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
161 static 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
178 static 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
194 static 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
210 static void maybe_add_string_va(struct json_writer *jw, const char *field_name,
211 const char *fmt, va_list ap)
212 {
213 if (fmt && *fmt) {
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 }
225 }
226
227 static 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
250 static 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
264 static 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
284 static 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
301 static 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
315 static 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
333 static 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->args.v);
358 jw_end(&jw);
359 jw_end(&jw);
360
361 tr2_dst_write_line(&tr2dst_event, &jw.json);
362 jw_release(&jw);
363 }
364
365 static 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
386 static void fn_child_ready_fl(const char *file, int line,
387 uint64_t us_elapsed_absolute, int cid, int pid,
388 const char *ready, uint64_t us_elapsed_child)
389 {
390 const char *event_name = "child_ready";
391 struct json_writer jw = JSON_WRITER_INIT;
392 double t_rel = (double)us_elapsed_child / 1000000.0;
393
394 jw_object_begin(&jw, 0);
395 event_fmt_prepare(event_name, file, line, NULL, &jw);
396 jw_object_intmax(&jw, "child_id", cid);
397 jw_object_intmax(&jw, "pid", pid);
398 jw_object_string(&jw, "ready", ready);
399 jw_object_double(&jw, "t_rel", 6, t_rel);
400 jw_end(&jw);
401
402 tr2_dst_write_line(&tr2dst_event, &jw.json);
403
404 jw_release(&jw);
405 }
406
407 static void fn_thread_start_fl(const char *file, int line,
408 uint64_t us_elapsed_absolute)
409 {
410 const char *event_name = "thread_start";
411 struct json_writer jw = JSON_WRITER_INIT;
412
413 jw_object_begin(&jw, 0);
414 event_fmt_prepare(event_name, file, line, NULL, &jw);
415 jw_end(&jw);
416
417 tr2_dst_write_line(&tr2dst_event, &jw.json);
418 jw_release(&jw);
419 }
420
421 static void fn_thread_exit_fl(const char *file, int line,
422 uint64_t us_elapsed_absolute,
423 uint64_t us_elapsed_thread)
424 {
425 const char *event_name = "thread_exit";
426 struct json_writer jw = JSON_WRITER_INIT;
427 double t_rel = (double)us_elapsed_thread / 1000000.0;
428
429 jw_object_begin(&jw, 0);
430 event_fmt_prepare(event_name, file, line, NULL, &jw);
431 jw_object_double(&jw, "t_rel", 6, t_rel);
432 jw_end(&jw);
433
434 tr2_dst_write_line(&tr2dst_event, &jw.json);
435 jw_release(&jw);
436 }
437
438 static void fn_exec_fl(const char *file, int line, uint64_t us_elapsed_absolute,
439 int exec_id, const char *exe, const char **argv)
440 {
441 const char *event_name = "exec";
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 if (exe)
448 jw_object_string(&jw, "exe", exe);
449 jw_object_inline_begin_array(&jw, "argv");
450 jw_array_argv(&jw, argv);
451 jw_end(&jw);
452 jw_end(&jw);
453
454 tr2_dst_write_line(&tr2dst_event, &jw.json);
455 jw_release(&jw);
456 }
457
458 static void fn_exec_result_fl(const char *file, int line,
459 uint64_t us_elapsed_absolute, int exec_id,
460 int code)
461 {
462 const char *event_name = "exec_result";
463 struct json_writer jw = JSON_WRITER_INIT;
464
465 jw_object_begin(&jw, 0);
466 event_fmt_prepare(event_name, file, line, NULL, &jw);
467 jw_object_intmax(&jw, "exec_id", exec_id);
468 jw_object_intmax(&jw, "code", code);
469 jw_end(&jw);
470
471 tr2_dst_write_line(&tr2dst_event, &jw.json);
472 jw_release(&jw);
473 }
474
475 static void fn_param_fl(const char *file, int line, const char *param,
476 const char *value)
477 {
478 const char *event_name = "def_param";
479 struct json_writer jw = JSON_WRITER_INIT;
480
481 jw_object_begin(&jw, 0);
482 event_fmt_prepare(event_name, file, line, NULL, &jw);
483 jw_object_string(&jw, "param", param);
484 jw_object_string(&jw, "value", value);
485 jw_end(&jw);
486
487 tr2_dst_write_line(&tr2dst_event, &jw.json);
488 jw_release(&jw);
489 }
490
491 static void fn_repo_fl(const char *file, int line,
492 const struct repository *repo)
493 {
494 const char *event_name = "def_repo";
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_string(&jw, "worktree", repo->worktree);
500 jw_end(&jw);
501
502 tr2_dst_write_line(&tr2dst_event, &jw.json);
503 jw_release(&jw);
504 }
505
506 static void fn_region_enter_printf_va_fl(const char *file, int line,
507 uint64_t us_elapsed_absolute,
508 const char *category,
509 const char *label,
510 const struct repository *repo,
511 const char *fmt, va_list ap)
512 {
513 const char *event_name = "region_enter";
514 struct tr2tls_thread_ctx *ctx = tr2tls_get_self();
515 if (ctx->nr_open_regions <= tr2env_event_max_nesting_levels) {
516 struct json_writer jw = JSON_WRITER_INIT;
517
518 jw_object_begin(&jw, 0);
519 event_fmt_prepare(event_name, file, line, repo, &jw);
520 jw_object_intmax(&jw, "nesting", ctx->nr_open_regions);
521 if (category)
522 jw_object_string(&jw, "category", category);
523 if (label)
524 jw_object_string(&jw, "label", label);
525 maybe_add_string_va(&jw, "msg", fmt, ap);
526 jw_end(&jw);
527
528 tr2_dst_write_line(&tr2dst_event, &jw.json);
529 jw_release(&jw);
530 }
531 }
532
533 static void fn_region_leave_printf_va_fl(
534 const char *file, int line, uint64_t us_elapsed_absolute,
535 uint64_t us_elapsed_region, const char *category, const char *label,
536 const struct repository *repo, const char *fmt, va_list ap)
537 {
538 const char *event_name = "region_leave";
539 struct tr2tls_thread_ctx *ctx = tr2tls_get_self();
540 if (ctx->nr_open_regions <= tr2env_event_max_nesting_levels) {
541 struct json_writer jw = JSON_WRITER_INIT;
542 double t_rel = (double)us_elapsed_region / 1000000.0;
543
544 jw_object_begin(&jw, 0);
545 event_fmt_prepare(event_name, file, line, repo, &jw);
546 jw_object_double(&jw, "t_rel", 6, t_rel);
547 jw_object_intmax(&jw, "nesting", ctx->nr_open_regions);
548 if (category)
549 jw_object_string(&jw, "category", category);
550 if (label)
551 jw_object_string(&jw, "label", label);
552 maybe_add_string_va(&jw, "msg", fmt, ap);
553 jw_end(&jw);
554
555 tr2_dst_write_line(&tr2dst_event, &jw.json);
556 jw_release(&jw);
557 }
558 }
559
560 static void fn_data_fl(const char *file, int line, uint64_t us_elapsed_absolute,
561 uint64_t us_elapsed_region, const char *category,
562 const struct repository *repo, const char *key,
563 const char *value)
564 {
565 const char *event_name = "data";
566 struct tr2tls_thread_ctx *ctx = tr2tls_get_self();
567 if (ctx->nr_open_regions <= tr2env_event_max_nesting_levels) {
568 struct json_writer jw = JSON_WRITER_INIT;
569 double t_abs = (double)us_elapsed_absolute / 1000000.0;
570 double t_rel = (double)us_elapsed_region / 1000000.0;
571
572 jw_object_begin(&jw, 0);
573 event_fmt_prepare(event_name, file, line, repo, &jw);
574 jw_object_double(&jw, "t_abs", 6, t_abs);
575 jw_object_double(&jw, "t_rel", 6, t_rel);
576 jw_object_intmax(&jw, "nesting", ctx->nr_open_regions);
577 jw_object_string(&jw, "category", category);
578 jw_object_string(&jw, "key", key);
579 jw_object_string(&jw, "value", value);
580 jw_end(&jw);
581
582 tr2_dst_write_line(&tr2dst_event, &jw.json);
583 jw_release(&jw);
584 }
585 }
586
587 static void fn_data_json_fl(const char *file, int line,
588 uint64_t us_elapsed_absolute,
589 uint64_t us_elapsed_region, const char *category,
590 const struct repository *repo, const char *key,
591 const struct json_writer *value)
592 {
593 const char *event_name = "data_json";
594 struct tr2tls_thread_ctx *ctx = tr2tls_get_self();
595 if (ctx->nr_open_regions <= tr2env_event_max_nesting_levels) {
596 struct json_writer jw = JSON_WRITER_INIT;
597 double t_abs = (double)us_elapsed_absolute / 1000000.0;
598 double t_rel = (double)us_elapsed_region / 1000000.0;
599
600 jw_object_begin(&jw, 0);
601 event_fmt_prepare(event_name, file, line, repo, &jw);
602 jw_object_double(&jw, "t_abs", 6, t_abs);
603 jw_object_double(&jw, "t_rel", 6, t_rel);
604 jw_object_intmax(&jw, "nesting", ctx->nr_open_regions);
605 jw_object_string(&jw, "category", category);
606 jw_object_string(&jw, "key", key);
607 jw_object_sub_jw(&jw, "value", value);
608 jw_end(&jw);
609
610 tr2_dst_write_line(&tr2dst_event, &jw.json);
611 jw_release(&jw);
612 }
613 }
614
615 struct tr2_tgt tr2_tgt_event = {
616 &tr2dst_event,
617
618 fn_init,
619 fn_term,
620
621 fn_version_fl,
622 fn_start_fl,
623 fn_exit_fl,
624 fn_signal,
625 fn_atexit,
626 fn_error_va_fl,
627 fn_command_path_fl,
628 fn_command_ancestry_fl,
629 fn_command_name_fl,
630 fn_command_mode_fl,
631 fn_alias_fl,
632 fn_child_start_fl,
633 fn_child_exit_fl,
634 fn_child_ready_fl,
635 fn_thread_start_fl,
636 fn_thread_exit_fl,
637 fn_exec_fl,
638 fn_exec_result_fl,
639 fn_param_fl,
640 fn_repo_fl,
641 fn_region_enter_printf_va_fl,
642 fn_region_leave_printf_va_fl,
643 fn_data_fl,
644 fn_data_json_fl,
645 NULL, /* printf */
646 };