]> git.ipfire.org Git - thirdparty/git.git/blame - trace2.h
trace2: use size_t alloc,nr_open_regions in tr2tls_thread_ctx
[thirdparty/git.git] / trace2.h
CommitLineData
ee4512ed
JH
1#ifndef TRACE2_H
2#define TRACE2_H
3
6c51cb52
HW
4/**
5 * The Trace2 API can be used to print debug, performance, and telemetry
6 * information to stderr or a file. The Trace2 feature is inactive unless
7 * explicitly enabled by enabling one or more Trace2 Targets.
8 *
9 * The Trace2 API is intended to replace the existing (Trace1)
10 * printf-style tracing provided by the existing `GIT_TRACE` and
11 * `GIT_TRACE_PERFORMANCE` facilities. During initial implementation,
12 * Trace2 and Trace1 may operate in parallel.
13 *
14 * The Trace2 API defines a set of high-level messages with known fields,
15 * such as (`start`: `argv`) and (`exit`: {`exit-code`, `elapsed-time`}).
16 *
17 * Trace2 instrumentation throughout the Git code base sends Trace2
18 * messages to the enabled Trace2 Targets. Targets transform these
19 * messages content into purpose-specific formats and write events to
20 * their data streams. In this manner, the Trace2 API can drive
21 * many different types of analysis.
22 *
23 * Targets are defined using a VTable allowing easy extension to other
24 * formats in the future. This might be used to define a binary format,
25 * for example.
26 *
27 * Trace2 is controlled using `trace2.*` config values in the system and
28 * global config files and `GIT_TRACE2*` environment variables. Trace2 does
29 * not read from repo local or worktree config files or respect `-c`
30 * command line config settings.
31 *
32 * For more info about: trace2 targets, conventions for public functions and
33 * macros, trace2 target formats and examples on trace2 API usage refer to
34 * Documentation/technical/api-trace2.txt
35 *
36 */
37
ee4512ed
JH
38struct child_process;
39struct repository;
40struct json_writer;
41
42/*
43 * The public TRACE2 routines are grouped into the following groups:
44 *
45 * [] trace2_initialize -- initialization.
46 * [] trace2_cmd_* -- emit command/control messages.
47 * [] trace2_child* -- emit child start/stop messages.
48 * [] trace2_exec* -- emit exec start/stop messages.
49 * [] trace2_thread* -- emit thread start/stop messages.
50 * [] trace2_def* -- emit definition/parameter mesasges.
51 * [] trace2_region* -- emit region nesting messages.
52 * [] trace2_data* -- emit region/thread/repo data messages.
53 * [] trace2_printf* -- legacy trace[1] messages.
54 */
55
a0897249
JH
56/*
57 * Initialize the TRACE2 clock and do nothing else, in particular
58 * no mallocs, no system inspection, and no environment inspection.
59 *
60 * This should be called at the very top of main() to capture the
61 * process start time. This is intended to reduce chicken-n-egg
62 * bootstrap pressure.
63 *
64 * It is safe to call this more than once. This allows capturing
65 * absolute startup costs on Windows which uses a little trickery
66 * to do setup work before common-main.c:main() is called.
67 *
68 * The main trace2_initialize_fl() may be called a little later
69 * after more infrastructure is established.
70 */
71void trace2_initialize_clock(void);
72
ee4512ed
JH
73/*
74 * Initialize TRACE2 tracing facility if any of the builtin TRACE2
bce9db6d 75 * targets are enabled in the system config or the environment.
6c51cb52
HW
76 * This includes setting up the Trace2 thread local storage (TLS).
77 * Emits a 'version' message containing the version of git
78 * and the Trace2 protocol.
79 *
80 * This function should be called from `main()` as early as possible in
81 * the life of the process after essential process initialization.
ee4512ed
JH
82 *
83 * Cleanup/Termination is handled automatically by a registered
84 * atexit() routine.
85 */
86void trace2_initialize_fl(const char *file, int line);
87
88#define trace2_initialize() trace2_initialize_fl(__FILE__, __LINE__)
89
90/*
6c51cb52 91 * Return 1 if trace2 is enabled (at least one target is active).
ee4512ed
JH
92 */
93int trace2_is_enabled(void);
94
95/*
96 * Emit a 'start' event with the original (unmodified) argv.
97 */
98void trace2_cmd_start_fl(const char *file, int line, const char **argv);
99
100#define trace2_cmd_start(argv) trace2_cmd_start_fl(__FILE__, __LINE__, (argv))
101
102/*
103 * Emit an 'exit' event.
ee4512ed 104 */
19d75948 105void trace2_cmd_exit_fl(const char *file, int line, int code);
ee4512ed
JH
106
107#define trace2_cmd_exit(code) (trace2_cmd_exit_fl(__FILE__, __LINE__, (code)))
108
109/*
110 * Emit an 'error' event.
111 *
112 * Write an error message to the TRACE2 targets.
113 */
114void trace2_cmd_error_va_fl(const char *file, int line, const char *fmt,
115 va_list ap);
116
117#define trace2_cmd_error_va(fmt, ap) \
118 trace2_cmd_error_va_fl(__FILE__, __LINE__, (fmt), (ap))
119
120/*
121 * Emit a 'pathname' event with the canonical pathname of the current process
122 * This gives post-processors a simple field to identify the command without
123 * having to parse the argv. For example, to distinguish invocations from
124 * installed versus debug executables.
125 */
126void trace2_cmd_path_fl(const char *file, int line, const char *pathname);
127
128#define trace2_cmd_path(p) trace2_cmd_path_fl(__FILE__, __LINE__, (p))
129
2f732bf1
ES
130/*
131 * Emit an 'ancestry' event with the process name of the current process's
132 * parent process.
133 * This gives post-processors a way to determine what invoked the command and
134 * learn more about usage patterns.
135 */
136void trace2_cmd_ancestry_fl(const char *file, int line, const char **parent_names);
137
138#define trace2_cmd_ancestry(v) trace2_cmd_ancestry_fl(__FILE__, __LINE__, (v))
139
ee4512ed
JH
140/*
141 * Emit a 'cmd_name' event with the canonical name of the command.
142 * This gives post-processors a simple field to identify the command
143 * without having to parse the argv.
144 */
145void trace2_cmd_name_fl(const char *file, int line, const char *name);
146
147#define trace2_cmd_name(v) trace2_cmd_name_fl(__FILE__, __LINE__, (v))
148
149/*
150 * Emit a 'cmd_mode' event to further describe the command being run.
151 * For example, "checkout" can checkout a single file or can checkout a
152 * different branch. This gives post-processors a simple field to compare
153 * equivalent commands without having to parse the argv.
154 */
155void trace2_cmd_mode_fl(const char *file, int line, const char *mode);
156
157#define trace2_cmd_mode(sv) trace2_cmd_mode_fl(__FILE__, __LINE__, (sv))
158
159/*
6c51cb52
HW
160 * Emits an "alias" message containing the alias used and the argument
161 * expansion.
ee4512ed
JH
162 */
163void trace2_cmd_alias_fl(const char *file, int line, const char *alias,
164 const char **argv);
165
166#define trace2_cmd_alias(alias, argv) \
167 trace2_cmd_alias_fl(__FILE__, __LINE__, (alias), (argv))
168
169/*
6c51cb52 170 * Emit one or more 'def_param' events for "important" configuration
ee4512ed
JH
171 * settings.
172 *
bce9db6d
JH
173 * Use the TR2_SYSENV_CFG_PARAM setting to register a comma-separated
174 * list of patterns configured important. For example:
175 * git config --system trace2.configParams 'core.*,remote.*.url'
176 * or:
e4b75d6a 177 * GIT_TRACE2_CONFIG_PARAMS=core.*,remote.*.url"
ee4512ed
JH
178 *
179 * Note: this routine does a read-only iteration on the config data
180 * (using read_early_config()), so it must not be called until enough
181 * of the process environment has been established. This includes the
182 * location of the git and worktree directories, expansion of any "-c"
183 * and "-C" command line options, and etc.
184 */
185void trace2_cmd_list_config_fl(const char *file, int line);
186
187#define trace2_cmd_list_config() trace2_cmd_list_config_fl(__FILE__, __LINE__)
188
3d3adaad
JS
189/*
190 * Emit one or more 'def_param' events for "important" environment variables.
191 *
192 * Use the TR2_SYSENV_ENV_VARS setting to register a comma-separated list of
193 * environment variables considered important. For example:
194 * git config --system trace2.envVars 'GIT_HTTP_USER_AGENT,GIT_CONFIG'
195 * or:
196 * GIT_TRACE2_ENV_VARS="GIT_HTTP_USER_AGENT,GIT_CONFIG"
197 */
198void trace2_cmd_list_env_vars_fl(const char *file, int line);
199
200#define trace2_cmd_list_env_vars() trace2_cmd_list_env_vars_fl(__FILE__, __LINE__)
201
ee4512ed
JH
202/*
203 * Emit a "def_param" event for the given config key/value pair IF
6c51cb52 204 * we consider the key to be "important".
ee4512ed
JH
205 *
206 * Use this for new/updated config settings created/updated after
207 * trace2_cmd_list_config() is called.
208 */
209void trace2_cmd_set_config_fl(const char *file, int line, const char *key,
210 const char *value);
211
212#define trace2_cmd_set_config(k, v) \
213 trace2_cmd_set_config_fl(__FILE__, __LINE__, (k), (v))
214
6c51cb52
HW
215/**
216 * Emits a "child_start" message containing the "child-id",
217 * "child-argv", and "child-classification".
ee4512ed
JH
218 *
219 * Before calling optionally set "cmd->trace2_child_class" to a string
220 * describing the type of the child process. For example, "editor" or
221 * "pager".
6c51cb52
HW
222 *
223 * This function assigns a unique "child-id" to `cmd->trace2_child_id`.
224 * This field is used later during the "child_exit" message to associate
225 * it with the "child_start" message.
226 *
227 * This function should be called before spawning the child process.
ee4512ed
JH
228 */
229void trace2_child_start_fl(const char *file, int line,
230 struct child_process *cmd);
231
232#define trace2_child_start(cmd) trace2_child_start_fl(__FILE__, __LINE__, (cmd))
233
6c51cb52
HW
234/**
235 * Emits a "child_exit" message containing the "child-id",
236 * the child's elapsed time and exit-code.
237 *
238 * The reported elapsed time includes the process creation overhead and
239 * time spend waiting for it to exit, so it may be slightly longer than
240 * the time reported by the child itself.
241 *
242 * This function should be called after reaping the child process.
ee4512ed
JH
243 */
244void trace2_child_exit_fl(const char *file, int line, struct child_process *cmd,
245 int child_exit_code);
246
247#define trace2_child_exit(cmd, code) \
248 trace2_child_exit_fl(__FILE__, __LINE__, (cmd), (code))
249
64bc7524
JH
250/**
251 * Emits a "child_ready" message containing the "child-id" and a flag
252 * indicating whether the child was considered "ready" when we
253 * released it.
254 *
255 * This function should be called after starting a daemon process in
256 * the background (and after giving it sufficient time to boot
257 * up) to indicate that we no longer control or own it.
258 *
259 * The "ready" argument should contain one of { "ready", "timeout",
260 * "error" } to indicate the state of the running daemon when we
261 * released it.
262 *
263 * If the daemon process fails to start or it exits or is terminated
264 * while we are still waiting for it, the caller should emit a
265 * regular "child_exit" to report the normal process exit information.
266 *
267 */
268void trace2_child_ready_fl(const char *file, int line,
269 struct child_process *cmd,
270 const char *ready);
271
272#define trace2_child_ready(cmd, ready) \
273 trace2_child_ready_fl(__FILE__, __LINE__, (cmd), (ready))
274
6c51cb52 275/**
ee4512ed
JH
276 * Emit an 'exec' event prior to calling one of exec(), execv(),
277 * execvp(), and etc. On Unix-derived systems, this will be the
278 * last event emitted for the current process, unless the exec
279 * fails. On Windows, exec() behaves like 'child_start' and a
280 * waitpid(), so additional events may be emitted.
281 *
6c51cb52
HW
282 * Returns a unique "exec-id". This value is used later
283 * if the exec() fails and a "exec-result" message is necessary.
ee4512ed
JH
284 */
285int trace2_exec_fl(const char *file, int line, const char *exe,
286 const char **argv);
287
288#define trace2_exec(exe, argv) trace2_exec_fl(__FILE__, __LINE__, (exe), (argv))
289
6c51cb52 290/**
ee4512ed
JH
291 * Emit an 'exec_result' when possible. On Unix-derived systems,
292 * this should be called after exec() returns (which only happens
293 * when there is an error starting the new process). On Windows,
294 * this should be called after the waitpid().
295 *
296 * The "exec_id" should be the value returned from trace2_exec().
297 */
298void trace2_exec_result_fl(const char *file, int line, int exec_id, int code);
299
300#define trace2_exec_result(id, code) \
301 trace2_exec_result_fl(__FILE__, __LINE__, (id), (code))
302
303/*
304 * Emit a 'thread_start' event. This must be called from inside the
305 * thread-proc to set up the trace2 TLS data for the thread.
306 *
307 * Thread names should be descriptive, like "preload_index".
308 * Thread names will be decorated with an instance number automatically.
309 */
310void trace2_thread_start_fl(const char *file, int line,
311 const char *thread_name);
312
313#define trace2_thread_start(thread_name) \
314 trace2_thread_start_fl(__FILE__, __LINE__, (thread_name))
315
316/*
317 * Emit a 'thread_exit' event. This must be called from inside the
318 * thread-proc to report thread-specific data and cleanup TLS data
319 * for the thread.
320 */
321void trace2_thread_exit_fl(const char *file, int line);
322
323#define trace2_thread_exit() trace2_thread_exit_fl(__FILE__, __LINE__)
324
325/*
6c51cb52 326 * Emits a "def_param" message containing a key/value pair.
ee4512ed 327 *
6c51cb52
HW
328 * This message is intended to report some global aspect of the current
329 * command, such as a configuration setting or command line switch that
330 * significantly affects program performance or behavior, such as
331 * `core.abbrev`, `status.showUntrackedFiles`, or `--no-ahead-behind`.
ee4512ed
JH
332 */
333void trace2_def_param_fl(const char *file, int line, const char *param,
334 const char *value);
335
336#define trace2_def_param(param, value) \
337 trace2_def_param_fl(__FILE__, __LINE__, (param), (value))
338
339/*
340 * Tell trace2 about a newly instantiated repo object and assign
341 * a trace2-repo-id to be used in subsequent activity events.
342 *
343 * Emits a 'worktree' event for this repo instance.
6c51cb52
HW
344 *
345 * Region and data messages may refer to this repo-id.
346 *
347 * The main/top-level repository will have repo-id value 1 (aka "r1").
348 *
349 * The repo-id field is in anticipation of future in-proc submodule
350 * repositories.
ee4512ed
JH
351 */
352void trace2_def_repo_fl(const char *file, int line, struct repository *repo);
353
354#define trace2_def_repo(repo) trace2_def_repo_fl(__FILE__, __LINE__, repo)
355
6c51cb52 356/**
ee4512ed
JH
357 * Emit a 'region_enter' event for <category>.<label> with optional
358 * repo-id and printf message.
359 *
6c51cb52
HW
360 * This function pushes a new region nesting stack level on the current
361 * thread and starts a clock for the new stack frame.
362 *
363 * The `category` field is an arbitrary category name used to classify
364 * regions by feature area, such as "status" or "index". At this time
365 * it is only just printed along with the rest of the message. It may
366 * be used in the future to filter messages.
367 *
368 * The `label` field is an arbitrary label used to describe the activity
369 * being started, such as "read_recursive" or "do_read_index".
370 *
371 * The `repo` field, if set, will be used to get the "repo-id", so that
b031f478 372 * recursive operations can be attributed to the correct repository.
ee4512ed
JH
373 */
374void trace2_region_enter_fl(const char *file, int line, const char *category,
ad006fe4 375 const char *label, const struct repository *repo, ...);
ee4512ed
JH
376
377#define trace2_region_enter(category, label, repo) \
378 trace2_region_enter_fl(__FILE__, __LINE__, (category), (label), (repo))
379
380void trace2_region_enter_printf_va_fl(const char *file, int line,
381 const char *category, const char *label,
382 const struct repository *repo,
383 const char *fmt, va_list ap);
384
385#define trace2_region_enter_printf_va(category, label, repo, fmt, ap) \
386 trace2_region_enter_printf_va_fl(__FILE__, __LINE__, (category), \
387 (label), (repo), (fmt), (ap))
388
389void trace2_region_enter_printf_fl(const char *file, int line,
390 const char *category, const char *label,
391 const struct repository *repo,
392 const char *fmt, ...);
393
ee4512ed
JH
394#define trace2_region_enter_printf(category, label, repo, ...) \
395 trace2_region_enter_printf_fl(__FILE__, __LINE__, (category), (label), \
396 (repo), __VA_ARGS__)
ee4512ed 397
6c51cb52 398/**
ee4512ed
JH
399 * Emit a 'region_leave' event for <category>.<label> with optional
400 * repo-id and printf message.
401 *
402 * Leave current nesting level and report the elapsed time spent
403 * in this nesting level.
6c51cb52
HW
404 *
405 * The `category`, `label`, and `repo` fields are the same as
406 * trace2_region_enter_fl. The `category` and `label` do not
407 * need to match the corresponding "region_enter" message,
408 * but it makes the data stream easier to understand.
ee4512ed
JH
409 */
410void trace2_region_leave_fl(const char *file, int line, const char *category,
ad006fe4 411 const char *label, const struct repository *repo, ...);
ee4512ed
JH
412
413#define trace2_region_leave(category, label, repo) \
414 trace2_region_leave_fl(__FILE__, __LINE__, (category), (label), (repo))
415
416void trace2_region_leave_printf_va_fl(const char *file, int line,
417 const char *category, const char *label,
418 const struct repository *repo,
419 const char *fmt, va_list ap);
420
421#define trace2_region_leave_printf_va(category, label, repo, fmt, ap) \
422 trace2_region_leave_printf_va_fl(__FILE__, __LINE__, (category), \
423 (label), (repo), (fmt), (ap))
424
425void trace2_region_leave_printf_fl(const char *file, int line,
426 const char *category, const char *label,
427 const struct repository *repo,
428 const char *fmt, ...);
429
ee4512ed
JH
430#define trace2_region_leave_printf(category, label, repo, ...) \
431 trace2_region_leave_printf_fl(__FILE__, __LINE__, (category), (label), \
432 (repo), __VA_ARGS__)
ee4512ed 433
6c51cb52 434/**
ee4512ed
JH
435 * Emit a key-value pair 'data' event of the form <category>.<key> = <value>.
436 * This event implicitly contains information about thread, nesting region,
437 * and optional repo-id.
6c51cb52
HW
438 * This could be used to print the number of files in a directory during
439 * a multi-threaded recursive tree walk.
ee4512ed
JH
440 *
441 * On event-based TRACE2 targets, this generates a 'data' event suitable
442 * for post-processing. On printf-based TRACE2 targets, this is converted
443 * into a fixed-format printf message.
444 */
445void trace2_data_string_fl(const char *file, int line, const char *category,
446 const struct repository *repo, const char *key,
447 const char *value);
448
449#define trace2_data_string(category, repo, key, value) \
450 trace2_data_string_fl(__FILE__, __LINE__, (category), (repo), (key), \
451 (value))
452
453void trace2_data_intmax_fl(const char *file, int line, const char *category,
454 const struct repository *repo, const char *key,
455 intmax_t value);
456
457#define trace2_data_intmax(category, repo, key, value) \
458 trace2_data_intmax_fl(__FILE__, __LINE__, (category), (repo), (key), \
459 (value))
460
461void trace2_data_json_fl(const char *file, int line, const char *category,
462 const struct repository *repo, const char *key,
463 const struct json_writer *jw);
464
465#define trace2_data_json(category, repo, key, value) \
466 trace2_data_json_fl(__FILE__, __LINE__, (category), (repo), (key), \
467 (value))
468
469/*
470 * Emit a 'printf' event.
471 *
472 * Write an arbitrary formatted message to the TRACE2 targets. These
473 * text messages should be considered as human-readable strings without
474 * any formatting guidelines. Post-processors may choose to ignore
475 * them.
476 */
477void trace2_printf_va_fl(const char *file, int line, const char *fmt,
478 va_list ap);
479
480#define trace2_printf_va(fmt, ap) \
481 trace2_printf_va_fl(__FILE__, __LINE__, (fmt), (ap))
482
483void trace2_printf_fl(const char *file, int line, const char *fmt, ...);
484
ee4512ed 485#define trace2_printf(...) trace2_printf_fl(__FILE__, __LINE__, __VA_ARGS__)
ee4512ed 486
353d3d77
JH
487/*
488 * Optional platform-specific code to dump information about the
489 * current and any parent process(es). This is intended to allow
490 * post-processors to know who spawned this git instance and anything
26c6f251 491 * else that the platform may be able to tell us about the current process.
353d3d77 492 */
26c6f251
JH
493
494enum trace2_process_info_reason {
495 TRACE2_PROCESS_INFO_STARTUP,
496 TRACE2_PROCESS_INFO_EXIT,
497};
498
26c6f251 499void trace2_collect_process_info(enum trace2_process_info_reason reason);
353d3d77 500
e97e1cf4
JS
501const char *trace2_session_id(void);
502
ee4512ed 503#endif /* TRACE2_H */