]> git.ipfire.org Git - thirdparty/git.git/blame - trace2/tr2_tgt_normal.c
Merge branch 'ob/t9001-indent-fix'
[thirdparty/git.git] / trace2 / tr2_tgt_normal.c
CommitLineData
15db4e7f 1#include "git-compat-util.h"
ee4512ed 2#include "config.h"
d1cbe1e6 3#include "repository.h"
ee4512ed
JH
4#include "run-command.h"
5#include "quote.h"
6#include "version.h"
7#include "trace2/tr2_dst.h"
bce9db6d 8#include "trace2/tr2_sysenv.h"
ee4512ed
JH
9#include "trace2/tr2_tbuf.h"
10#include "trace2/tr2_tgt.h"
11#include "trace2/tr2_tls.h"
8ad57564 12#include "trace2/tr2_tmr.h"
ee4512ed 13
4996e0b0
ÆAB
14static struct tr2_dst tr2dst_normal = {
15 .sysenv_var = TR2_SYSENV_NORMAL,
16};
ee4512ed
JH
17
18/*
bce9db6d 19 * Use the TR2_SYSENV_NORMAL_BRIEF setting to omit the "<time> <file>:<line>"
ee4512ed
JH
20 * fields from each line written to the builtin normal target.
21 *
22 * Unit tests may want to use this to help with testing.
23 */
bce9db6d 24static int tr2env_normal_be_brief;
ee4512ed
JH
25
26#define TR2FMT_NORMAL_FL_WIDTH (50)
27
28static int fn_init(void)
29{
30 int want = tr2_dst_trace_want(&tr2dst_normal);
31 int want_brief;
bce9db6d 32 const char *brief;
ee4512ed
JH
33
34 if (!want)
35 return want;
36
bce9db6d 37 brief = tr2_sysenv_get(TR2_SYSENV_NORMAL_BRIEF);
ee4512ed
JH
38 if (brief && *brief &&
39 ((want_brief = git_parse_maybe_bool(brief)) != -1))
bce9db6d 40 tr2env_normal_be_brief = want_brief;
ee4512ed
JH
41
42 return want;
43}
44
45static void fn_term(void)
46{
47 tr2_dst_trace_disable(&tr2dst_normal);
48}
49
50static void normal_fmt_prepare(const char *file, int line, struct strbuf *buf)
51{
52 strbuf_setlen(buf, 0);
53
bce9db6d 54 if (!tr2env_normal_be_brief) {
ee4512ed
JH
55 struct tr2_tbuf tb_now;
56
57 tr2_tbuf_local_time(&tb_now);
58 strbuf_addstr(buf, tb_now.buf);
59 strbuf_addch(buf, ' ');
60
61 if (file && *file)
62 strbuf_addf(buf, "%s:%d ", file, line);
63 while (buf->len < TR2FMT_NORMAL_FL_WIDTH)
64 strbuf_addch(buf, ' ');
65 }
66}
67
68static void normal_io_write_fl(const char *file, int line,
69 const struct strbuf *buf_payload)
70{
71 struct strbuf buf_line = STRBUF_INIT;
72
73 normal_fmt_prepare(file, line, &buf_line);
74 strbuf_addbuf(&buf_line, buf_payload);
75 tr2_dst_write_line(&tr2dst_normal, &buf_line);
76 strbuf_release(&buf_line);
77}
78
79static void fn_version_fl(const char *file, int line)
80{
81 struct strbuf buf_payload = STRBUF_INIT;
82
83 strbuf_addf(&buf_payload, "version %s", git_version_string);
84 normal_io_write_fl(file, line, &buf_payload);
85 strbuf_release(&buf_payload);
86}
87
39f43177
JH
88static void fn_start_fl(const char *file, int line,
89 uint64_t us_elapsed_absolute, const char **argv)
ee4512ed
JH
90{
91 struct strbuf buf_payload = STRBUF_INIT;
92
93 strbuf_addstr(&buf_payload, "start ");
e3443055 94 sq_append_quote_argv_pretty(&buf_payload, argv);
ee4512ed
JH
95 normal_io_write_fl(file, line, &buf_payload);
96 strbuf_release(&buf_payload);
97}
98
99static void fn_exit_fl(const char *file, int line, uint64_t us_elapsed_absolute,
100 int code)
101{
102 struct strbuf buf_payload = STRBUF_INIT;
103 double elapsed = (double)us_elapsed_absolute / 1000000.0;
104
105 strbuf_addf(&buf_payload, "exit elapsed:%.6f code:%d", elapsed, code);
106 normal_io_write_fl(file, line, &buf_payload);
107 strbuf_release(&buf_payload);
108}
109
110static void fn_signal(uint64_t us_elapsed_absolute, int signo)
111{
112 struct strbuf buf_payload = STRBUF_INIT;
113 double elapsed = (double)us_elapsed_absolute / 1000000.0;
114
115 strbuf_addf(&buf_payload, "signal elapsed:%.6f code:%d", elapsed,
116 signo);
117 normal_io_write_fl(__FILE__, __LINE__, &buf_payload);
118 strbuf_release(&buf_payload);
119}
120
121static void fn_atexit(uint64_t us_elapsed_absolute, int code)
122{
123 struct strbuf buf_payload = STRBUF_INIT;
124 double elapsed = (double)us_elapsed_absolute / 1000000.0;
125
126 strbuf_addf(&buf_payload, "atexit elapsed:%.6f code:%d", elapsed, code);
127 normal_io_write_fl(__FILE__, __LINE__, &buf_payload);
128 strbuf_release(&buf_payload);
129}
130
131static void maybe_append_string_va(struct strbuf *buf, const char *fmt,
132 va_list ap)
133{
ad006fe4 134 if (fmt && *fmt) {
ee4512ed
JH
135 va_list copy_ap;
136
137 va_copy(copy_ap, ap);
138 strbuf_vaddf(buf, fmt, copy_ap);
139 va_end(copy_ap);
140 return;
141 }
ee4512ed
JH
142}
143
144static void fn_error_va_fl(const char *file, int line, const char *fmt,
145 va_list ap)
146{
147 struct strbuf buf_payload = STRBUF_INIT;
148
ad43e378
JH
149 strbuf_addstr(&buf_payload, "error");
150 if (fmt && *fmt) {
151 strbuf_addch(&buf_payload, ' ');
152 maybe_append_string_va(&buf_payload, fmt, ap);
153 }
ee4512ed
JH
154 normal_io_write_fl(file, line, &buf_payload);
155 strbuf_release(&buf_payload);
156}
157
158static void fn_command_path_fl(const char *file, int line, const char *pathname)
159{
160 struct strbuf buf_payload = STRBUF_INIT;
161
162 strbuf_addf(&buf_payload, "cmd_path %s", pathname);
163 normal_io_write_fl(file, line, &buf_payload);
164 strbuf_release(&buf_payload);
165}
166
2f732bf1
ES
167static void fn_command_ancestry_fl(const char *file, int line, const char **parent_names)
168{
169 const char *parent_name = NULL;
170 struct strbuf buf_payload = STRBUF_INIT;
171
172 /* cmd_ancestry parent <- grandparent <- great-grandparent */
173 strbuf_addstr(&buf_payload, "cmd_ancestry ");
174 while ((parent_name = *parent_names++)) {
175 strbuf_addstr(&buf_payload, parent_name);
176 /* if we'll write another one after this, add a delimiter */
177 if (parent_names && *parent_names)
178 strbuf_addstr(&buf_payload, " <- ");
179 }
180
181 normal_io_write_fl(file, line, &buf_payload);
182 strbuf_release(&buf_payload);
183}
184
ee4512ed
JH
185static void fn_command_name_fl(const char *file, int line, const char *name,
186 const char *hierarchy)
187{
188 struct strbuf buf_payload = STRBUF_INIT;
189
190 strbuf_addf(&buf_payload, "cmd_name %s", name);
191 if (hierarchy && *hierarchy)
192 strbuf_addf(&buf_payload, " (%s)", hierarchy);
193 normal_io_write_fl(file, line, &buf_payload);
194 strbuf_release(&buf_payload);
195}
196
197static void fn_command_mode_fl(const char *file, int line, const char *mode)
198{
199 struct strbuf buf_payload = STRBUF_INIT;
200
201 strbuf_addf(&buf_payload, "cmd_mode %s", mode);
202 normal_io_write_fl(file, line, &buf_payload);
203 strbuf_release(&buf_payload);
204}
205
206static void fn_alias_fl(const char *file, int line, const char *alias,
207 const char **argv)
208{
209 struct strbuf buf_payload = STRBUF_INIT;
210
e3443055
JH
211 strbuf_addf(&buf_payload, "alias %s -> ", alias);
212 sq_append_quote_argv_pretty(&buf_payload, argv);
ee4512ed
JH
213 normal_io_write_fl(file, line, &buf_payload);
214 strbuf_release(&buf_payload);
215}
216
217static void fn_child_start_fl(const char *file, int line,
218 uint64_t us_elapsed_absolute,
219 const struct child_process *cmd)
220{
221 struct strbuf buf_payload = STRBUF_INIT;
222
e3443055 223 strbuf_addf(&buf_payload, "child_start[%d]", cmd->trace2_child_id);
ee4512ed
JH
224
225 if (cmd->dir) {
e3443055 226 strbuf_addstr(&buf_payload, " cd ");
ee4512ed 227 sq_quote_buf_pretty(&buf_payload, cmd->dir);
e3443055 228 strbuf_addstr(&buf_payload, ";");
ee4512ed
JH
229 }
230
231 /*
232 * TODO if (cmd->env) { Consider dumping changes to environment. }
233 * See trace_add_env() in run-command.c as used by original trace.c
234 */
235
e3443055 236 strbuf_addch(&buf_payload, ' ');
ee4512ed 237 if (cmd->git_cmd)
e3443055 238 strbuf_addstr(&buf_payload, "git ");
d3b21597 239 sq_append_quote_argv_pretty(&buf_payload, cmd->args.v);
ee4512ed
JH
240
241 normal_io_write_fl(file, line, &buf_payload);
242 strbuf_release(&buf_payload);
243}
244
245static void fn_child_exit_fl(const char *file, int line,
246 uint64_t us_elapsed_absolute, int cid, int pid,
247 int code, uint64_t us_elapsed_child)
248{
249 struct strbuf buf_payload = STRBUF_INIT;
250 double elapsed = (double)us_elapsed_child / 1000000.0;
251
252 strbuf_addf(&buf_payload, "child_exit[%d] pid:%d code:%d elapsed:%.6f",
253 cid, pid, code, elapsed);
254 normal_io_write_fl(file, line, &buf_payload);
255 strbuf_release(&buf_payload);
256}
257
64bc7524
JH
258static void fn_child_ready_fl(const char *file, int line,
259 uint64_t us_elapsed_absolute, int cid, int pid,
260 const char *ready, uint64_t us_elapsed_child)
261{
262 struct strbuf buf_payload = STRBUF_INIT;
263 double elapsed = (double)us_elapsed_child / 1000000.0;
264
265 strbuf_addf(&buf_payload, "child_ready[%d] pid:%d ready:%s elapsed:%.6f",
266 cid, pid, ready, elapsed);
267 normal_io_write_fl(file, line, &buf_payload);
268 strbuf_release(&buf_payload);
269}
270
ee4512ed
JH
271static void fn_exec_fl(const char *file, int line, uint64_t us_elapsed_absolute,
272 int exec_id, const char *exe, const char **argv)
273{
274 struct strbuf buf_payload = STRBUF_INIT;
275
276 strbuf_addf(&buf_payload, "exec[%d] ", exec_id);
e3443055 277 if (exe) {
ee4512ed 278 strbuf_addstr(&buf_payload, exe);
e3443055
JH
279 strbuf_addch(&buf_payload, ' ');
280 }
281 sq_append_quote_argv_pretty(&buf_payload, argv);
ee4512ed
JH
282 normal_io_write_fl(file, line, &buf_payload);
283 strbuf_release(&buf_payload);
284}
285
286static void fn_exec_result_fl(const char *file, int line,
287 uint64_t us_elapsed_absolute, int exec_id,
288 int code)
289{
290 struct strbuf buf_payload = STRBUF_INIT;
291
292 strbuf_addf(&buf_payload, "exec_result[%d] code:%d", exec_id, code);
293 if (code > 0)
294 strbuf_addf(&buf_payload, " err:%s", strerror(code));
295 normal_io_write_fl(file, line, &buf_payload);
296 strbuf_release(&buf_payload);
297}
298
299static void fn_param_fl(const char *file, int line, const char *param,
dc902084 300 const char *value, const struct key_value_info *kvi)
ee4512ed
JH
301{
302 struct strbuf buf_payload = STRBUF_INIT;
dc902084 303 enum config_scope scope = kvi->scope;
35ae40ea 304 const char *scope_name = config_scope_name(scope);
ee4512ed 305
35ae40ea
TL
306 strbuf_addf(&buf_payload, "def_param scope:%s %s=%s", scope_name, param,
307 value);
ee4512ed
JH
308 normal_io_write_fl(file, line, &buf_payload);
309 strbuf_release(&buf_payload);
310}
311
312static void fn_repo_fl(const char *file, int line,
313 const struct repository *repo)
314{
315 struct strbuf buf_payload = STRBUF_INIT;
316
317 strbuf_addstr(&buf_payload, "worktree ");
318 sq_quote_buf_pretty(&buf_payload, repo->worktree);
319 normal_io_write_fl(file, line, &buf_payload);
320 strbuf_release(&buf_payload);
321}
322
323static void fn_printf_va_fl(const char *file, int line,
324 uint64_t us_elapsed_absolute, const char *fmt,
325 va_list ap)
326{
327 struct strbuf buf_payload = STRBUF_INIT;
328
329 maybe_append_string_va(&buf_payload, fmt, ap);
330 normal_io_write_fl(file, line, &buf_payload);
331 strbuf_release(&buf_payload);
332}
333
8ad57564
JH
334static void fn_timer(const struct tr2_timer_metadata *meta,
335 const struct tr2_timer *timer,
336 int is_final_data)
337{
338 const char *event_name = is_final_data ? "timer" : "th_timer";
339 struct strbuf buf_payload = STRBUF_INIT;
340 double t_total = NS_TO_SEC(timer->total_ns);
341 double t_min = NS_TO_SEC(timer->min_ns);
342 double t_max = NS_TO_SEC(timer->max_ns);
343
344 strbuf_addf(&buf_payload, ("%s %s/%s"
345 " intervals:%"PRIu64
346 " total:%8.6f min:%8.6f max:%8.6f"),
347 event_name, meta->category, meta->name,
348 timer->interval_count,
349 t_total, t_min, t_max);
350
351 normal_io_write_fl(__FILE__, __LINE__, &buf_payload);
352 strbuf_release(&buf_payload);
353}
354
81071626
JH
355static void fn_counter(const struct tr2_counter_metadata *meta,
356 const struct tr2_counter *counter,
357 int is_final_data)
358{
359 const char *event_name = is_final_data ? "counter" : "th_counter";
360 struct strbuf buf_payload = STRBUF_INIT;
361
362 strbuf_addf(&buf_payload, "%s %s/%s value:%"PRIu64,
363 event_name, meta->category, meta->name,
364 counter->value);
365
366 normal_io_write_fl(__FILE__, __LINE__, &buf_payload);
367 strbuf_release(&buf_payload);
368}
369
ee4512ed 370struct tr2_tgt tr2_tgt_normal = {
98593057
ÆAB
371 .pdst = &tr2dst_normal,
372
373 .pfn_init = fn_init,
374 .pfn_term = fn_term,
375
376 .pfn_version_fl = fn_version_fl,
377 .pfn_start_fl = fn_start_fl,
378 .pfn_exit_fl = fn_exit_fl,
379 .pfn_signal = fn_signal,
380 .pfn_atexit = fn_atexit,
381 .pfn_error_va_fl = fn_error_va_fl,
382 .pfn_command_path_fl = fn_command_path_fl,
383 .pfn_command_ancestry_fl = fn_command_ancestry_fl,
384 .pfn_command_name_fl = fn_command_name_fl,
385 .pfn_command_mode_fl = fn_command_mode_fl,
386 .pfn_alias_fl = fn_alias_fl,
387 .pfn_child_start_fl = fn_child_start_fl,
388 .pfn_child_exit_fl = fn_child_exit_fl,
389 .pfn_child_ready_fl = fn_child_ready_fl,
390 .pfn_thread_start_fl = NULL,
391 .pfn_thread_exit_fl = NULL,
392 .pfn_exec_fl = fn_exec_fl,
393 .pfn_exec_result_fl = fn_exec_result_fl,
394 .pfn_param_fl = fn_param_fl,
395 .pfn_repo_fl = fn_repo_fl,
396 .pfn_region_enter_printf_va_fl = NULL,
397 .pfn_region_leave_printf_va_fl = NULL,
398 .pfn_data_fl = NULL,
399 .pfn_data_json_fl = NULL,
400 .pfn_printf_va_fl = fn_printf_va_fl,
8ad57564 401 .pfn_timer = fn_timer,
81071626 402 .pfn_counter = fn_counter,
ee4512ed 403};