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