Merge branch 'jk/test-lsan-denoise-output'
[git.git] / trace2 / tr2_tgt_normal.c
blobfbbef68dfc01a546240384fbf12582ccb9d64c54
1 #include "cache.h"
2 #include "config.h"
3 #include "run-command.h"
4 #include "quote.h"
5 #include "version.h"
6 #include "trace2/tr2_dst.h"
7 #include "trace2/tr2_sysenv.h"
8 #include "trace2/tr2_tbuf.h"
9 #include "trace2/tr2_tgt.h"
10 #include "trace2/tr2_tls.h"
11 #include "trace2/tr2_tmr.h"
13 static struct tr2_dst tr2dst_normal = {
14 .sysenv_var = TR2_SYSENV_NORMAL,
18 * Use the TR2_SYSENV_NORMAL_BRIEF setting to omit the "<time> <file>:<line>"
19 * fields from each line written to the builtin normal target.
21 * Unit tests may want to use this to help with testing.
23 static int tr2env_normal_be_brief;
25 #define TR2FMT_NORMAL_FL_WIDTH (50)
27 static int fn_init(void)
29 int want = tr2_dst_trace_want(&tr2dst_normal);
30 int want_brief;
31 const char *brief;
33 if (!want)
34 return want;
36 brief = tr2_sysenv_get(TR2_SYSENV_NORMAL_BRIEF);
37 if (brief && *brief &&
38 ((want_brief = git_parse_maybe_bool(brief)) != -1))
39 tr2env_normal_be_brief = want_brief;
41 return want;
44 static void fn_term(void)
46 tr2_dst_trace_disable(&tr2dst_normal);
49 static void normal_fmt_prepare(const char *file, int line, struct strbuf *buf)
51 strbuf_setlen(buf, 0);
53 if (!tr2env_normal_be_brief) {
54 struct tr2_tbuf tb_now;
56 tr2_tbuf_local_time(&tb_now);
57 strbuf_addstr(buf, tb_now.buf);
58 strbuf_addch(buf, ' ');
60 if (file && *file)
61 strbuf_addf(buf, "%s:%d ", file, line);
62 while (buf->len < TR2FMT_NORMAL_FL_WIDTH)
63 strbuf_addch(buf, ' ');
67 static void normal_io_write_fl(const char *file, int line,
68 const struct strbuf *buf_payload)
70 struct strbuf buf_line = STRBUF_INIT;
72 normal_fmt_prepare(file, line, &buf_line);
73 strbuf_addbuf(&buf_line, buf_payload);
74 tr2_dst_write_line(&tr2dst_normal, &buf_line);
75 strbuf_release(&buf_line);
78 static void fn_version_fl(const char *file, int line)
80 struct strbuf buf_payload = STRBUF_INIT;
82 strbuf_addf(&buf_payload, "version %s", git_version_string);
83 normal_io_write_fl(file, line, &buf_payload);
84 strbuf_release(&buf_payload);
87 static void fn_start_fl(const char *file, int line,
88 uint64_t us_elapsed_absolute, const char **argv)
90 struct strbuf buf_payload = STRBUF_INIT;
92 strbuf_addstr(&buf_payload, "start ");
93 sq_append_quote_argv_pretty(&buf_payload, argv);
94 normal_io_write_fl(file, line, &buf_payload);
95 strbuf_release(&buf_payload);
98 static void fn_exit_fl(const char *file, int line, uint64_t us_elapsed_absolute,
99 int code)
101 struct strbuf buf_payload = STRBUF_INIT;
102 double elapsed = (double)us_elapsed_absolute / 1000000.0;
104 strbuf_addf(&buf_payload, "exit elapsed:%.6f code:%d", elapsed, code);
105 normal_io_write_fl(file, line, &buf_payload);
106 strbuf_release(&buf_payload);
109 static void fn_signal(uint64_t us_elapsed_absolute, int signo)
111 struct strbuf buf_payload = STRBUF_INIT;
112 double elapsed = (double)us_elapsed_absolute / 1000000.0;
114 strbuf_addf(&buf_payload, "signal elapsed:%.6f code:%d", elapsed,
115 signo);
116 normal_io_write_fl(__FILE__, __LINE__, &buf_payload);
117 strbuf_release(&buf_payload);
120 static void fn_atexit(uint64_t us_elapsed_absolute, int code)
122 struct strbuf buf_payload = STRBUF_INIT;
123 double elapsed = (double)us_elapsed_absolute / 1000000.0;
125 strbuf_addf(&buf_payload, "atexit elapsed:%.6f code:%d", elapsed, code);
126 normal_io_write_fl(__FILE__, __LINE__, &buf_payload);
127 strbuf_release(&buf_payload);
130 static void maybe_append_string_va(struct strbuf *buf, const char *fmt,
131 va_list ap)
133 if (fmt && *fmt) {
134 va_list copy_ap;
136 va_copy(copy_ap, ap);
137 strbuf_vaddf(buf, fmt, copy_ap);
138 va_end(copy_ap);
139 return;
143 static void fn_error_va_fl(const char *file, int line, const char *fmt,
144 va_list ap)
146 struct strbuf buf_payload = STRBUF_INIT;
148 strbuf_addstr(&buf_payload, "error");
149 if (fmt && *fmt) {
150 strbuf_addch(&buf_payload, ' ');
151 maybe_append_string_va(&buf_payload, fmt, ap);
153 normal_io_write_fl(file, line, &buf_payload);
154 strbuf_release(&buf_payload);
157 static void fn_command_path_fl(const char *file, int line, const char *pathname)
159 struct strbuf buf_payload = STRBUF_INIT;
161 strbuf_addf(&buf_payload, "cmd_path %s", pathname);
162 normal_io_write_fl(file, line, &buf_payload);
163 strbuf_release(&buf_payload);
166 static void fn_command_ancestry_fl(const char *file, int line, const char **parent_names)
168 const char *parent_name = NULL;
169 struct strbuf buf_payload = STRBUF_INIT;
171 /* cmd_ancestry parent <- grandparent <- great-grandparent */
172 strbuf_addstr(&buf_payload, "cmd_ancestry ");
173 while ((parent_name = *parent_names++)) {
174 strbuf_addstr(&buf_payload, parent_name);
175 /* if we'll write another one after this, add a delimiter */
176 if (parent_names && *parent_names)
177 strbuf_addstr(&buf_payload, " <- ");
180 normal_io_write_fl(file, line, &buf_payload);
181 strbuf_release(&buf_payload);
184 static void fn_command_name_fl(const char *file, int line, const char *name,
185 const char *hierarchy)
187 struct strbuf buf_payload = STRBUF_INIT;
189 strbuf_addf(&buf_payload, "cmd_name %s", name);
190 if (hierarchy && *hierarchy)
191 strbuf_addf(&buf_payload, " (%s)", hierarchy);
192 normal_io_write_fl(file, line, &buf_payload);
193 strbuf_release(&buf_payload);
196 static void fn_command_mode_fl(const char *file, int line, const char *mode)
198 struct strbuf buf_payload = STRBUF_INIT;
200 strbuf_addf(&buf_payload, "cmd_mode %s", mode);
201 normal_io_write_fl(file, line, &buf_payload);
202 strbuf_release(&buf_payload);
205 static void fn_alias_fl(const char *file, int line, const char *alias,
206 const char **argv)
208 struct strbuf buf_payload = STRBUF_INIT;
210 strbuf_addf(&buf_payload, "alias %s -> ", alias);
211 sq_append_quote_argv_pretty(&buf_payload, argv);
212 normal_io_write_fl(file, line, &buf_payload);
213 strbuf_release(&buf_payload);
216 static void fn_child_start_fl(const char *file, int line,
217 uint64_t us_elapsed_absolute,
218 const struct child_process *cmd)
220 struct strbuf buf_payload = STRBUF_INIT;
222 strbuf_addf(&buf_payload, "child_start[%d]", cmd->trace2_child_id);
224 if (cmd->dir) {
225 strbuf_addstr(&buf_payload, " cd ");
226 sq_quote_buf_pretty(&buf_payload, cmd->dir);
227 strbuf_addstr(&buf_payload, ";");
231 * TODO if (cmd->env) { Consider dumping changes to environment. }
232 * See trace_add_env() in run-command.c as used by original trace.c
235 strbuf_addch(&buf_payload, ' ');
236 if (cmd->git_cmd)
237 strbuf_addstr(&buf_payload, "git ");
238 sq_append_quote_argv_pretty(&buf_payload, cmd->args.v);
240 normal_io_write_fl(file, line, &buf_payload);
241 strbuf_release(&buf_payload);
244 static void fn_child_exit_fl(const char *file, int line,
245 uint64_t us_elapsed_absolute, int cid, int pid,
246 int code, uint64_t us_elapsed_child)
248 struct strbuf buf_payload = STRBUF_INIT;
249 double elapsed = (double)us_elapsed_child / 1000000.0;
251 strbuf_addf(&buf_payload, "child_exit[%d] pid:%d code:%d elapsed:%.6f",
252 cid, pid, code, elapsed);
253 normal_io_write_fl(file, line, &buf_payload);
254 strbuf_release(&buf_payload);
257 static void fn_child_ready_fl(const char *file, int line,
258 uint64_t us_elapsed_absolute, int cid, int pid,
259 const char *ready, uint64_t us_elapsed_child)
261 struct strbuf buf_payload = STRBUF_INIT;
262 double elapsed = (double)us_elapsed_child / 1000000.0;
264 strbuf_addf(&buf_payload, "child_ready[%d] pid:%d ready:%s elapsed:%.6f",
265 cid, pid, ready, elapsed);
266 normal_io_write_fl(file, line, &buf_payload);
267 strbuf_release(&buf_payload);
270 static void fn_exec_fl(const char *file, int line, uint64_t us_elapsed_absolute,
271 int exec_id, const char *exe, const char **argv)
273 struct strbuf buf_payload = STRBUF_INIT;
275 strbuf_addf(&buf_payload, "exec[%d] ", exec_id);
276 if (exe) {
277 strbuf_addstr(&buf_payload, exe);
278 strbuf_addch(&buf_payload, ' ');
280 sq_append_quote_argv_pretty(&buf_payload, argv);
281 normal_io_write_fl(file, line, &buf_payload);
282 strbuf_release(&buf_payload);
285 static void fn_exec_result_fl(const char *file, int line,
286 uint64_t us_elapsed_absolute, int exec_id,
287 int code)
289 struct strbuf buf_payload = STRBUF_INIT;
291 strbuf_addf(&buf_payload, "exec_result[%d] code:%d", exec_id, code);
292 if (code > 0)
293 strbuf_addf(&buf_payload, " err:%s", strerror(code));
294 normal_io_write_fl(file, line, &buf_payload);
295 strbuf_release(&buf_payload);
298 static void fn_param_fl(const char *file, int line, const char *param,
299 const char *value)
301 struct strbuf buf_payload = STRBUF_INIT;
302 enum config_scope scope = current_config_scope();
303 const char *scope_name = config_scope_name(scope);
305 strbuf_addf(&buf_payload, "def_param scope:%s %s=%s", scope_name, param,
306 value);
307 normal_io_write_fl(file, line, &buf_payload);
308 strbuf_release(&buf_payload);
311 static void fn_repo_fl(const char *file, int line,
312 const struct repository *repo)
314 struct strbuf buf_payload = STRBUF_INIT;
316 strbuf_addstr(&buf_payload, "worktree ");
317 sq_quote_buf_pretty(&buf_payload, repo->worktree);
318 normal_io_write_fl(file, line, &buf_payload);
319 strbuf_release(&buf_payload);
322 static void fn_printf_va_fl(const char *file, int line,
323 uint64_t us_elapsed_absolute, const char *fmt,
324 va_list ap)
326 struct strbuf buf_payload = STRBUF_INIT;
328 maybe_append_string_va(&buf_payload, fmt, ap);
329 normal_io_write_fl(file, line, &buf_payload);
330 strbuf_release(&buf_payload);
333 static void fn_timer(const struct tr2_timer_metadata *meta,
334 const struct tr2_timer *timer,
335 int is_final_data)
337 const char *event_name = is_final_data ? "timer" : "th_timer";
338 struct strbuf buf_payload = STRBUF_INIT;
339 double t_total = NS_TO_SEC(timer->total_ns);
340 double t_min = NS_TO_SEC(timer->min_ns);
341 double t_max = NS_TO_SEC(timer->max_ns);
343 strbuf_addf(&buf_payload, ("%s %s/%s"
344 " intervals:%"PRIu64
345 " total:%8.6f min:%8.6f max:%8.6f"),
346 event_name, meta->category, meta->name,
347 timer->interval_count,
348 t_total, t_min, t_max);
350 normal_io_write_fl(__FILE__, __LINE__, &buf_payload);
351 strbuf_release(&buf_payload);
354 static void fn_counter(const struct tr2_counter_metadata *meta,
355 const struct tr2_counter *counter,
356 int is_final_data)
358 const char *event_name = is_final_data ? "counter" : "th_counter";
359 struct strbuf buf_payload = STRBUF_INIT;
361 strbuf_addf(&buf_payload, "%s %s/%s value:%"PRIu64,
362 event_name, meta->category, meta->name,
363 counter->value);
365 normal_io_write_fl(__FILE__, __LINE__, &buf_payload);
366 strbuf_release(&buf_payload);
369 struct tr2_tgt tr2_tgt_normal = {
370 .pdst = &tr2dst_normal,
372 .pfn_init = fn_init,
373 .pfn_term = fn_term,
375 .pfn_version_fl = fn_version_fl,
376 .pfn_start_fl = fn_start_fl,
377 .pfn_exit_fl = fn_exit_fl,
378 .pfn_signal = fn_signal,
379 .pfn_atexit = fn_atexit,
380 .pfn_error_va_fl = fn_error_va_fl,
381 .pfn_command_path_fl = fn_command_path_fl,
382 .pfn_command_ancestry_fl = fn_command_ancestry_fl,
383 .pfn_command_name_fl = fn_command_name_fl,
384 .pfn_command_mode_fl = fn_command_mode_fl,
385 .pfn_alias_fl = fn_alias_fl,
386 .pfn_child_start_fl = fn_child_start_fl,
387 .pfn_child_exit_fl = fn_child_exit_fl,
388 .pfn_child_ready_fl = fn_child_ready_fl,
389 .pfn_thread_start_fl = NULL,
390 .pfn_thread_exit_fl = NULL,
391 .pfn_exec_fl = fn_exec_fl,
392 .pfn_exec_result_fl = fn_exec_result_fl,
393 .pfn_param_fl = fn_param_fl,
394 .pfn_repo_fl = fn_repo_fl,
395 .pfn_region_enter_printf_va_fl = NULL,
396 .pfn_region_leave_printf_va_fl = NULL,
397 .pfn_data_fl = NULL,
398 .pfn_data_json_fl = NULL,
399 .pfn_printf_va_fl = fn_printf_va_fl,
400 .pfn_timer = fn_timer,
401 .pfn_counter = fn_counter,