Merge branch 'jk/disable-commit-graph-during-upload-pack'
[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 };
14
15 /*
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.
21  */
22 #define TR2_EVENT_VERSION "1"
23
24 /*
25  * Region nesting limit for messages written to the event target.
26  *
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.
30  *
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.
34  */
35 static int tr2env_event_max_nesting_levels = 2;
36
37 /*
38  * Use the TR2_SYSENV_EVENT_BRIEF to omit the <time>, <file>, and
39  * <line> fields from most events.
40  */
41 static int tr2env_event_be_brief;
42
43 static int fn_init(void)
44 {
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;
50
51         if (!want)
52                 return want;
53
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;
57
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;
62
63         return want;
64 }
65
66 static void fn_term(void)
67 {
68         tr2_dst_trace_disable(&tr2dst_event);
69 }
70
71 /*
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>
80  */
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)
84 {
85         struct tr2tls_thread_ctx *ctx = tr2tls_get_self();
86         struct tr2_tbuf tb_now;
87
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);
91
92         /*
93          * In brief mode, only emit <time> on these 2 event types.
94          */
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);
99         }
100
101         if (!tr2env_event_be_brief && file && *file) {
102                 jw_object_string(jw, "file", file);
103                 jw_object_intmax(jw, "line", line);
104         }
105
106         if (repo)
107                 jw_object_intmax(jw, "repo", repo->trace2_repo_id);
108 }
109
110 static void fn_version_fl(const char *file, int line)
111 {
112         const char *event_name = "version";
113         struct json_writer jw = JSON_WRITER_INIT;
114
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);
120
121         tr2_dst_write_line(&tr2dst_event, &jw.json);
122         jw_release(&jw);
123 }
124
125 static void fn_start_fl(const char *file, int line,
126                         uint64_t us_elapsed_absolute, const char **argv)
127 {
128         const char *event_name = "start";
129         struct json_writer jw = JSON_WRITER_INIT;
130         double t_abs = (double)us_elapsed_absolute / 1000000.0;
131
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);
139
140         tr2_dst_write_line(&tr2dst_event, &jw.json);
141         jw_release(&jw);
142 }
143
144 static void fn_exit_fl(const char *file, int line, uint64_t us_elapsed_absolute,
145                        int code)
146 {
147         const char *event_name = "exit";
148         struct json_writer jw = JSON_WRITER_INIT;
149         double t_abs = (double)us_elapsed_absolute / 1000000.0;
150
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);
156
157         tr2_dst_write_line(&tr2dst_event, &jw.json);
158         jw_release(&jw);
159 }
160
161 static void fn_signal(uint64_t us_elapsed_absolute, int signo)
162 {
163         const char *event_name = "signal";
164         struct json_writer jw = JSON_WRITER_INIT;
165         double t_abs = (double)us_elapsed_absolute / 1000000.0;
166
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);
172
173         tr2_dst_write_line(&tr2dst_event, &jw.json);
174         jw_release(&jw);
175 }
176
177 static void fn_atexit(uint64_t us_elapsed_absolute, int code)
178 {
179         const char *event_name = "atexit";
180         struct json_writer jw = JSON_WRITER_INIT;
181         double t_abs = (double)us_elapsed_absolute / 1000000.0;
182
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);
188
189         tr2_dst_write_line(&tr2dst_event, &jw.json);
190         jw_release(&jw);
191 }
192
193 static void maybe_add_string_va(struct json_writer *jw, const char *field_name,
194                                 const char *fmt, va_list ap)
195 {
196         if (fmt && *fmt) {
197                 va_list copy_ap;
198                 struct strbuf buf = STRBUF_INIT;
199
200                 va_copy(copy_ap, ap);
201                 strbuf_vaddf(&buf, fmt, copy_ap);
202                 va_end(copy_ap);
203
204                 jw_object_string(jw, field_name, buf.buf);
205                 strbuf_release(&buf);
206                 return;
207         }
208 }
209
210 static void fn_error_va_fl(const char *file, int line, const char *fmt,
211                            va_list ap)
212 {
213         const char *event_name = "error";
214         struct json_writer jw = JSON_WRITER_INIT;
215
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);
219         /*
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.
224          */
225         if (fmt && *fmt)
226                 jw_object_string(&jw, "fmt", fmt);
227         jw_end(&jw);
228
229         tr2_dst_write_line(&tr2dst_event, &jw.json);
230         jw_release(&jw);
231 }
232
233 static void fn_command_path_fl(const char *file, int line, const char *pathname)
234 {
235         const char *event_name = "cmd_path";
236         struct json_writer jw = JSON_WRITER_INIT;
237
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);
242
243         tr2_dst_write_line(&tr2dst_event, &jw.json);
244         jw_release(&jw);
245 }
246
247 static void fn_command_name_fl(const char *file, int line, const char *name,
248                                const char *hierarchy)
249 {
250         const char *event_name = "cmd_name";
251         struct json_writer jw = JSON_WRITER_INIT;
252
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);
259
260         tr2_dst_write_line(&tr2dst_event, &jw.json);
261         jw_release(&jw);
262 }
263
264 static void fn_command_mode_fl(const char *file, int line, const char *mode)
265 {
266         const char *event_name = "cmd_mode";
267         struct json_writer jw = JSON_WRITER_INIT;
268
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);
273
274         tr2_dst_write_line(&tr2dst_event, &jw.json);
275         jw_release(&jw);
276 }
277
278 static void fn_alias_fl(const char *file, int line, const char *alias,
279                         const char **argv)
280 {
281         const char *event_name = "alias";
282         struct json_writer jw = JSON_WRITER_INIT;
283
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);
291
292         tr2_dst_write_line(&tr2dst_event, &jw.json);
293         jw_release(&jw);
294 }
295
296 static void fn_child_start_fl(const char *file, int line,
297                               uint64_t us_elapsed_absolute,
298                               const struct child_process *cmd)
299 {
300         const char *event_name = "child_start";
301         struct json_writer jw = JSON_WRITER_INIT;
302
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);
313         }
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);
323
324         tr2_dst_write_line(&tr2dst_event, &jw.json);
325         jw_release(&jw);
326 }
327
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)
331 {
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;
335
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);
343
344         tr2_dst_write_line(&tr2dst_event, &jw.json);
345
346         jw_release(&jw);
347 }
348
349 static void fn_thread_start_fl(const char *file, int line,
350                                uint64_t us_elapsed_absolute)
351 {
352         const char *event_name = "thread_start";
353         struct json_writer jw = JSON_WRITER_INIT;
354
355         jw_object_begin(&jw, 0);
356         event_fmt_prepare(event_name, file, line, NULL, &jw);
357         jw_end(&jw);
358
359         tr2_dst_write_line(&tr2dst_event, &jw.json);
360         jw_release(&jw);
361 }
362
363 static void fn_thread_exit_fl(const char *file, int line,
364                               uint64_t us_elapsed_absolute,
365                               uint64_t us_elapsed_thread)
366 {
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;
370
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);
375
376         tr2_dst_write_line(&tr2dst_event, &jw.json);
377         jw_release(&jw);
378 }
379
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)
382 {
383         const char *event_name = "exec";
384         struct json_writer jw = JSON_WRITER_INIT;
385
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);
395
396         tr2_dst_write_line(&tr2dst_event, &jw.json);
397         jw_release(&jw);
398 }
399
400 static void fn_exec_result_fl(const char *file, int line,
401                               uint64_t us_elapsed_absolute, int exec_id,
402                               int code)
403 {
404         const char *event_name = "exec_result";
405         struct json_writer jw = JSON_WRITER_INIT;
406
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);
412
413         tr2_dst_write_line(&tr2dst_event, &jw.json);
414         jw_release(&jw);
415 }
416
417 static void fn_param_fl(const char *file, int line, const char *param,
418                         const char *value)
419 {
420         const char *event_name = "def_param";
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_string(&jw, "param", param);
426         jw_object_string(&jw, "value", value);
427         jw_end(&jw);
428
429         tr2_dst_write_line(&tr2dst_event, &jw.json);
430         jw_release(&jw);
431 }
432
433 static void fn_repo_fl(const char *file, int line,
434                        const struct repository *repo)
435 {
436         const char *event_name = "def_repo";
437         struct json_writer jw = JSON_WRITER_INIT;
438
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);
443
444         tr2_dst_write_line(&tr2dst_event, &jw.json);
445         jw_release(&jw);
446 }
447
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)
454 {
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;
459
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);
469
470                 tr2_dst_write_line(&tr2dst_event, &jw.json);
471                 jw_release(&jw);
472         }
473 }
474
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)
479 {
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;
485
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);
496
497                 tr2_dst_write_line(&tr2dst_event, &jw.json);
498                 jw_release(&jw);
499         }
500 }
501
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)
506 {
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;
513
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);
523
524                 tr2_dst_write_line(&tr2dst_event, &jw.json);
525                 jw_release(&jw);
526         }
527 }
528
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)
534 {
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;
541
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);
551
552                 tr2_dst_write_line(&tr2dst_event, &jw.json);
553                 jw_release(&jw);
554         }
555 }
556
557 struct tr2_tgt tr2_tgt_event = {
558         &tr2dst_event,
559
560         fn_init,
561         fn_term,
562
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 */
586 };