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