]> git.ipfire.org Git - thirdparty/git.git/blob - trace2/tr2_tgt_normal.c
ci: deprecate ci/config/allow-ref script
[thirdparty/git.git] / trace2 / tr2_tgt_normal.c
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"
12
13 static struct tr2_dst tr2dst_normal = {
14 .sysenv_var = TR2_SYSENV_NORMAL,
15 };
16
17 /*
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.
20 *
21 * Unit tests may want to use this to help with testing.
22 */
23 static int tr2env_normal_be_brief;
24
25 #define TR2FMT_NORMAL_FL_WIDTH (50)
26
27 static int fn_init(void)
28 {
29 int want = tr2_dst_trace_want(&tr2dst_normal);
30 int want_brief;
31 const char *brief;
32
33 if (!want)
34 return want;
35
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;
40
41 return want;
42 }
43
44 static void fn_term(void)
45 {
46 tr2_dst_trace_disable(&tr2dst_normal);
47 }
48
49 static void normal_fmt_prepare(const char *file, int line, struct strbuf *buf)
50 {
51 strbuf_setlen(buf, 0);
52
53 if (!tr2env_normal_be_brief) {
54 struct tr2_tbuf tb_now;
55
56 tr2_tbuf_local_time(&tb_now);
57 strbuf_addstr(buf, tb_now.buf);
58 strbuf_addch(buf, ' ');
59
60 if (file && *file)
61 strbuf_addf(buf, "%s:%d ", file, line);
62 while (buf->len < TR2FMT_NORMAL_FL_WIDTH)
63 strbuf_addch(buf, ' ');
64 }
65 }
66
67 static void normal_io_write_fl(const char *file, int line,
68 const struct strbuf *buf_payload)
69 {
70 struct strbuf buf_line = STRBUF_INIT;
71
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);
76 }
77
78 static void fn_version_fl(const char *file, int line)
79 {
80 struct strbuf buf_payload = STRBUF_INIT;
81
82 strbuf_addf(&buf_payload, "version %s", git_version_string);
83 normal_io_write_fl(file, line, &buf_payload);
84 strbuf_release(&buf_payload);
85 }
86
87 static void fn_start_fl(const char *file, int line,
88 uint64_t us_elapsed_absolute, const char **argv)
89 {
90 struct strbuf buf_payload = STRBUF_INIT;
91
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);
96 }
97
98 static void fn_exit_fl(const char *file, int line, uint64_t us_elapsed_absolute,
99 int code)
100 {
101 struct strbuf buf_payload = STRBUF_INIT;
102 double elapsed = (double)us_elapsed_absolute / 1000000.0;
103
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);
107 }
108
109 static void fn_signal(uint64_t us_elapsed_absolute, int signo)
110 {
111 struct strbuf buf_payload = STRBUF_INIT;
112 double elapsed = (double)us_elapsed_absolute / 1000000.0;
113
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);
118 }
119
120 static void fn_atexit(uint64_t us_elapsed_absolute, int code)
121 {
122 struct strbuf buf_payload = STRBUF_INIT;
123 double elapsed = (double)us_elapsed_absolute / 1000000.0;
124
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);
128 }
129
130 static void maybe_append_string_va(struct strbuf *buf, const char *fmt,
131 va_list ap)
132 {
133 if (fmt && *fmt) {
134 va_list copy_ap;
135
136 va_copy(copy_ap, ap);
137 strbuf_vaddf(buf, fmt, copy_ap);
138 va_end(copy_ap);
139 return;
140 }
141 }
142
143 static void fn_error_va_fl(const char *file, int line, const char *fmt,
144 va_list ap)
145 {
146 struct strbuf buf_payload = STRBUF_INIT;
147
148 strbuf_addstr(&buf_payload, "error");
149 if (fmt && *fmt) {
150 strbuf_addch(&buf_payload, ' ');
151 maybe_append_string_va(&buf_payload, fmt, ap);
152 }
153 normal_io_write_fl(file, line, &buf_payload);
154 strbuf_release(&buf_payload);
155 }
156
157 static void fn_command_path_fl(const char *file, int line, const char *pathname)
158 {
159 struct strbuf buf_payload = STRBUF_INIT;
160
161 strbuf_addf(&buf_payload, "cmd_path %s", pathname);
162 normal_io_write_fl(file, line, &buf_payload);
163 strbuf_release(&buf_payload);
164 }
165
166 static void fn_command_ancestry_fl(const char *file, int line, const char **parent_names)
167 {
168 const char *parent_name = NULL;
169 struct strbuf buf_payload = STRBUF_INIT;
170
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, " <- ");
178 }
179
180 normal_io_write_fl(file, line, &buf_payload);
181 strbuf_release(&buf_payload);
182 }
183
184 static void fn_command_name_fl(const char *file, int line, const char *name,
185 const char *hierarchy)
186 {
187 struct strbuf buf_payload = STRBUF_INIT;
188
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);
194 }
195
196 static void fn_command_mode_fl(const char *file, int line, const char *mode)
197 {
198 struct strbuf buf_payload = STRBUF_INIT;
199
200 strbuf_addf(&buf_payload, "cmd_mode %s", mode);
201 normal_io_write_fl(file, line, &buf_payload);
202 strbuf_release(&buf_payload);
203 }
204
205 static void fn_alias_fl(const char *file, int line, const char *alias,
206 const char **argv)
207 {
208 struct strbuf buf_payload = STRBUF_INIT;
209
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);
214 }
215
216 static void fn_child_start_fl(const char *file, int line,
217 uint64_t us_elapsed_absolute,
218 const struct child_process *cmd)
219 {
220 struct strbuf buf_payload = STRBUF_INIT;
221
222 strbuf_addf(&buf_payload, "child_start[%d]", cmd->trace2_child_id);
223
224 if (cmd->dir) {
225 strbuf_addstr(&buf_payload, " cd ");
226 sq_quote_buf_pretty(&buf_payload, cmd->dir);
227 strbuf_addstr(&buf_payload, ";");
228 }
229
230 /*
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
233 */
234
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);
239
240 normal_io_write_fl(file, line, &buf_payload);
241 strbuf_release(&buf_payload);
242 }
243
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)
247 {
248 struct strbuf buf_payload = STRBUF_INIT;
249 double elapsed = (double)us_elapsed_child / 1000000.0;
250
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);
255 }
256
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)
260 {
261 struct strbuf buf_payload = STRBUF_INIT;
262 double elapsed = (double)us_elapsed_child / 1000000.0;
263
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);
268 }
269
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)
272 {
273 struct strbuf buf_payload = STRBUF_INIT;
274
275 strbuf_addf(&buf_payload, "exec[%d] ", exec_id);
276 if (exe) {
277 strbuf_addstr(&buf_payload, exe);
278 strbuf_addch(&buf_payload, ' ');
279 }
280 sq_append_quote_argv_pretty(&buf_payload, argv);
281 normal_io_write_fl(file, line, &buf_payload);
282 strbuf_release(&buf_payload);
283 }
284
285 static void fn_exec_result_fl(const char *file, int line,
286 uint64_t us_elapsed_absolute, int exec_id,
287 int code)
288 {
289 struct strbuf buf_payload = STRBUF_INIT;
290
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);
296 }
297
298 static void fn_param_fl(const char *file, int line, const char *param,
299 const char *value)
300 {
301 struct strbuf buf_payload = STRBUF_INIT;
302 enum config_scope scope = current_config_scope();
303 const char *scope_name = config_scope_name(scope);
304
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);
309 }
310
311 static void fn_repo_fl(const char *file, int line,
312 const struct repository *repo)
313 {
314 struct strbuf buf_payload = STRBUF_INIT;
315
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);
320 }
321
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)
325 {
326 struct strbuf buf_payload = STRBUF_INIT;
327
328 maybe_append_string_va(&buf_payload, fmt, ap);
329 normal_io_write_fl(file, line, &buf_payload);
330 strbuf_release(&buf_payload);
331 }
332
333 static void fn_timer(const struct tr2_timer_metadata *meta,
334 const struct tr2_timer *timer,
335 int is_final_data)
336 {
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);
342
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);
349
350 normal_io_write_fl(__FILE__, __LINE__, &buf_payload);
351 strbuf_release(&buf_payload);
352 }
353
354 static void fn_counter(const struct tr2_counter_metadata *meta,
355 const struct tr2_counter *counter,
356 int is_final_data)
357 {
358 const char *event_name = is_final_data ? "counter" : "th_counter";
359 struct strbuf buf_payload = STRBUF_INIT;
360
361 strbuf_addf(&buf_payload, "%s %s/%s value:%"PRIu64,
362 event_name, meta->category, meta->name,
363 counter->value);
364
365 normal_io_write_fl(__FILE__, __LINE__, &buf_payload);
366 strbuf_release(&buf_payload);
367 }
368
369 struct tr2_tgt tr2_tgt_normal = {
370 .pdst = &tr2dst_normal,
371
372 .pfn_init = fn_init,
373 .pfn_term = fn_term,
374
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,
402 };