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