]>
Commit | Line | Data |
---|---|---|
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 |
38 | struct child_process; |
39 | struct repository; | |
40 | struct 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 | */ | |
71 | void 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 | */ | |
86 | void 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 | */ |
93 | int trace2_is_enabled(void); | |
94 | ||
95 | /* | |
96 | * Emit a 'start' event with the original (unmodified) argv. | |
97 | */ | |
98 | void 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. | |
104 | * | |
105 | * Write the exit-code that will be passed to exit() or returned | |
106 | * from main(). | |
107 | * | |
108 | * Use this prior to actually calling exit(). | |
109 | * See "#define exit()" in git-compat-util.h | |
110 | */ | |
111 | int trace2_cmd_exit_fl(const char *file, int line, int code); | |
112 | ||
113 | #define trace2_cmd_exit(code) (trace2_cmd_exit_fl(__FILE__, __LINE__, (code))) | |
114 | ||
115 | /* | |
116 | * Emit an 'error' event. | |
117 | * | |
118 | * Write an error message to the TRACE2 targets. | |
119 | */ | |
120 | void trace2_cmd_error_va_fl(const char *file, int line, const char *fmt, | |
121 | va_list ap); | |
122 | ||
123 | #define trace2_cmd_error_va(fmt, ap) \ | |
124 | trace2_cmd_error_va_fl(__FILE__, __LINE__, (fmt), (ap)) | |
125 | ||
126 | /* | |
127 | * Emit a 'pathname' event with the canonical pathname of the current process | |
128 | * This gives post-processors a simple field to identify the command without | |
129 | * having to parse the argv. For example, to distinguish invocations from | |
130 | * installed versus debug executables. | |
131 | */ | |
132 | void trace2_cmd_path_fl(const char *file, int line, const char *pathname); | |
133 | ||
134 | #define trace2_cmd_path(p) trace2_cmd_path_fl(__FILE__, __LINE__, (p)) | |
135 | ||
136 | /* | |
137 | * Emit a 'cmd_name' event with the canonical name of the command. | |
138 | * This gives post-processors a simple field to identify the command | |
139 | * without having to parse the argv. | |
140 | */ | |
141 | void trace2_cmd_name_fl(const char *file, int line, const char *name); | |
142 | ||
143 | #define trace2_cmd_name(v) trace2_cmd_name_fl(__FILE__, __LINE__, (v)) | |
144 | ||
145 | /* | |
146 | * Emit a 'cmd_mode' event to further describe the command being run. | |
147 | * For example, "checkout" can checkout a single file or can checkout a | |
148 | * different branch. This gives post-processors a simple field to compare | |
149 | * equivalent commands without having to parse the argv. | |
150 | */ | |
151 | void trace2_cmd_mode_fl(const char *file, int line, const char *mode); | |
152 | ||
153 | #define trace2_cmd_mode(sv) trace2_cmd_mode_fl(__FILE__, __LINE__, (sv)) | |
154 | ||
155 | /* | |
6c51cb52 HW |
156 | * Emits an "alias" message containing the alias used and the argument |
157 | * expansion. | |
ee4512ed JH |
158 | */ |
159 | void trace2_cmd_alias_fl(const char *file, int line, const char *alias, | |
160 | const char **argv); | |
161 | ||
162 | #define trace2_cmd_alias(alias, argv) \ | |
163 | trace2_cmd_alias_fl(__FILE__, __LINE__, (alias), (argv)) | |
164 | ||
165 | /* | |
6c51cb52 | 166 | * Emit one or more 'def_param' events for "important" configuration |
ee4512ed JH |
167 | * settings. |
168 | * | |
bce9db6d JH |
169 | * Use the TR2_SYSENV_CFG_PARAM setting to register a comma-separated |
170 | * list of patterns configured important. For example: | |
171 | * git config --system trace2.configParams 'core.*,remote.*.url' | |
172 | * or: | |
e4b75d6a | 173 | * GIT_TRACE2_CONFIG_PARAMS=core.*,remote.*.url" |
ee4512ed JH |
174 | * |
175 | * Note: this routine does a read-only iteration on the config data | |
176 | * (using read_early_config()), so it must not be called until enough | |
177 | * of the process environment has been established. This includes the | |
178 | * location of the git and worktree directories, expansion of any "-c" | |
179 | * and "-C" command line options, and etc. | |
180 | */ | |
181 | void trace2_cmd_list_config_fl(const char *file, int line); | |
182 | ||
183 | #define trace2_cmd_list_config() trace2_cmd_list_config_fl(__FILE__, __LINE__) | |
184 | ||
185 | /* | |
186 | * Emit a "def_param" event for the given config key/value pair IF | |
6c51cb52 | 187 | * we consider the key to be "important". |
ee4512ed JH |
188 | * |
189 | * Use this for new/updated config settings created/updated after | |
190 | * trace2_cmd_list_config() is called. | |
191 | */ | |
192 | void trace2_cmd_set_config_fl(const char *file, int line, const char *key, | |
193 | const char *value); | |
194 | ||
195 | #define trace2_cmd_set_config(k, v) \ | |
196 | trace2_cmd_set_config_fl(__FILE__, __LINE__, (k), (v)) | |
197 | ||
6c51cb52 HW |
198 | /** |
199 | * Emits a "child_start" message containing the "child-id", | |
200 | * "child-argv", and "child-classification". | |
ee4512ed JH |
201 | * |
202 | * Before calling optionally set "cmd->trace2_child_class" to a string | |
203 | * describing the type of the child process. For example, "editor" or | |
204 | * "pager". | |
6c51cb52 HW |
205 | * |
206 | * This function assigns a unique "child-id" to `cmd->trace2_child_id`. | |
207 | * This field is used later during the "child_exit" message to associate | |
208 | * it with the "child_start" message. | |
209 | * | |
210 | * This function should be called before spawning the child process. | |
ee4512ed JH |
211 | */ |
212 | void trace2_child_start_fl(const char *file, int line, | |
213 | struct child_process *cmd); | |
214 | ||
215 | #define trace2_child_start(cmd) trace2_child_start_fl(__FILE__, __LINE__, (cmd)) | |
216 | ||
6c51cb52 HW |
217 | /** |
218 | * Emits a "child_exit" message containing the "child-id", | |
219 | * the child's elapsed time and exit-code. | |
220 | * | |
221 | * The reported elapsed time includes the process creation overhead and | |
222 | * time spend waiting for it to exit, so it may be slightly longer than | |
223 | * the time reported by the child itself. | |
224 | * | |
225 | * This function should be called after reaping the child process. | |
ee4512ed JH |
226 | */ |
227 | void trace2_child_exit_fl(const char *file, int line, struct child_process *cmd, | |
228 | int child_exit_code); | |
229 | ||
230 | #define trace2_child_exit(cmd, code) \ | |
231 | trace2_child_exit_fl(__FILE__, __LINE__, (cmd), (code)) | |
232 | ||
6c51cb52 | 233 | /** |
ee4512ed JH |
234 | * Emit an 'exec' event prior to calling one of exec(), execv(), |
235 | * execvp(), and etc. On Unix-derived systems, this will be the | |
236 | * last event emitted for the current process, unless the exec | |
237 | * fails. On Windows, exec() behaves like 'child_start' and a | |
238 | * waitpid(), so additional events may be emitted. | |
239 | * | |
6c51cb52 HW |
240 | * Returns a unique "exec-id". This value is used later |
241 | * if the exec() fails and a "exec-result" message is necessary. | |
ee4512ed JH |
242 | */ |
243 | int trace2_exec_fl(const char *file, int line, const char *exe, | |
244 | const char **argv); | |
245 | ||
246 | #define trace2_exec(exe, argv) trace2_exec_fl(__FILE__, __LINE__, (exe), (argv)) | |
247 | ||
6c51cb52 | 248 | /** |
ee4512ed JH |
249 | * Emit an 'exec_result' when possible. On Unix-derived systems, |
250 | * this should be called after exec() returns (which only happens | |
251 | * when there is an error starting the new process). On Windows, | |
252 | * this should be called after the waitpid(). | |
253 | * | |
254 | * The "exec_id" should be the value returned from trace2_exec(). | |
255 | */ | |
256 | void trace2_exec_result_fl(const char *file, int line, int exec_id, int code); | |
257 | ||
258 | #define trace2_exec_result(id, code) \ | |
259 | trace2_exec_result_fl(__FILE__, __LINE__, (id), (code)) | |
260 | ||
261 | /* | |
262 | * Emit a 'thread_start' event. This must be called from inside the | |
263 | * thread-proc to set up the trace2 TLS data for the thread. | |
264 | * | |
265 | * Thread names should be descriptive, like "preload_index". | |
266 | * Thread names will be decorated with an instance number automatically. | |
267 | */ | |
268 | void trace2_thread_start_fl(const char *file, int line, | |
269 | const char *thread_name); | |
270 | ||
271 | #define trace2_thread_start(thread_name) \ | |
272 | trace2_thread_start_fl(__FILE__, __LINE__, (thread_name)) | |
273 | ||
274 | /* | |
275 | * Emit a 'thread_exit' event. This must be called from inside the | |
276 | * thread-proc to report thread-specific data and cleanup TLS data | |
277 | * for the thread. | |
278 | */ | |
279 | void trace2_thread_exit_fl(const char *file, int line); | |
280 | ||
281 | #define trace2_thread_exit() trace2_thread_exit_fl(__FILE__, __LINE__) | |
282 | ||
283 | /* | |
6c51cb52 | 284 | * Emits a "def_param" message containing a key/value pair. |
ee4512ed | 285 | * |
6c51cb52 HW |
286 | * This message is intended to report some global aspect of the current |
287 | * command, such as a configuration setting or command line switch that | |
288 | * significantly affects program performance or behavior, such as | |
289 | * `core.abbrev`, `status.showUntrackedFiles`, or `--no-ahead-behind`. | |
ee4512ed JH |
290 | */ |
291 | void trace2_def_param_fl(const char *file, int line, const char *param, | |
292 | const char *value); | |
293 | ||
294 | #define trace2_def_param(param, value) \ | |
295 | trace2_def_param_fl(__FILE__, __LINE__, (param), (value)) | |
296 | ||
297 | /* | |
298 | * Tell trace2 about a newly instantiated repo object and assign | |
299 | * a trace2-repo-id to be used in subsequent activity events. | |
300 | * | |
301 | * Emits a 'worktree' event for this repo instance. | |
6c51cb52 HW |
302 | * |
303 | * Region and data messages may refer to this repo-id. | |
304 | * | |
305 | * The main/top-level repository will have repo-id value 1 (aka "r1"). | |
306 | * | |
307 | * The repo-id field is in anticipation of future in-proc submodule | |
308 | * repositories. | |
ee4512ed JH |
309 | */ |
310 | void trace2_def_repo_fl(const char *file, int line, struct repository *repo); | |
311 | ||
312 | #define trace2_def_repo(repo) trace2_def_repo_fl(__FILE__, __LINE__, repo) | |
313 | ||
6c51cb52 | 314 | /** |
ee4512ed JH |
315 | * Emit a 'region_enter' event for <category>.<label> with optional |
316 | * repo-id and printf message. | |
317 | * | |
6c51cb52 HW |
318 | * This function pushes a new region nesting stack level on the current |
319 | * thread and starts a clock for the new stack frame. | |
320 | * | |
321 | * The `category` field is an arbitrary category name used to classify | |
322 | * regions by feature area, such as "status" or "index". At this time | |
323 | * it is only just printed along with the rest of the message. It may | |
324 | * be used in the future to filter messages. | |
325 | * | |
326 | * The `label` field is an arbitrary label used to describe the activity | |
327 | * being started, such as "read_recursive" or "do_read_index". | |
328 | * | |
329 | * The `repo` field, if set, will be used to get the "repo-id", so that | |
330 | * recursive oerations can be attributed to the correct repository. | |
ee4512ed JH |
331 | */ |
332 | void trace2_region_enter_fl(const char *file, int line, const char *category, | |
ad006fe4 | 333 | const char *label, const struct repository *repo, ...); |
ee4512ed JH |
334 | |
335 | #define trace2_region_enter(category, label, repo) \ | |
336 | trace2_region_enter_fl(__FILE__, __LINE__, (category), (label), (repo)) | |
337 | ||
338 | void trace2_region_enter_printf_va_fl(const char *file, int line, | |
339 | const char *category, const char *label, | |
340 | const struct repository *repo, | |
341 | const char *fmt, va_list ap); | |
342 | ||
343 | #define trace2_region_enter_printf_va(category, label, repo, fmt, ap) \ | |
344 | trace2_region_enter_printf_va_fl(__FILE__, __LINE__, (category), \ | |
345 | (label), (repo), (fmt), (ap)) | |
346 | ||
347 | void trace2_region_enter_printf_fl(const char *file, int line, | |
348 | const char *category, const char *label, | |
349 | const struct repository *repo, | |
350 | const char *fmt, ...); | |
351 | ||
352 | #ifdef HAVE_VARIADIC_MACROS | |
353 | #define trace2_region_enter_printf(category, label, repo, ...) \ | |
354 | trace2_region_enter_printf_fl(__FILE__, __LINE__, (category), (label), \ | |
355 | (repo), __VA_ARGS__) | |
356 | #else | |
357 | /* clang-format off */ | |
358 | __attribute__((format (region_enter_printf, 4, 5))) | |
359 | void trace2_region_enter_printf(const char *category, const char *label, | |
360 | const struct repository *repo, const char *fmt, | |
361 | ...); | |
362 | /* clang-format on */ | |
363 | #endif | |
364 | ||
6c51cb52 | 365 | /** |
ee4512ed JH |
366 | * Emit a 'region_leave' event for <category>.<label> with optional |
367 | * repo-id and printf message. | |
368 | * | |
369 | * Leave current nesting level and report the elapsed time spent | |
370 | * in this nesting level. | |
6c51cb52 HW |
371 | * |
372 | * The `category`, `label`, and `repo` fields are the same as | |
373 | * trace2_region_enter_fl. The `category` and `label` do not | |
374 | * need to match the corresponding "region_enter" message, | |
375 | * but it makes the data stream easier to understand. | |
ee4512ed JH |
376 | */ |
377 | void trace2_region_leave_fl(const char *file, int line, const char *category, | |
ad006fe4 | 378 | const char *label, const struct repository *repo, ...); |
ee4512ed JH |
379 | |
380 | #define trace2_region_leave(category, label, repo) \ | |
381 | trace2_region_leave_fl(__FILE__, __LINE__, (category), (label), (repo)) | |
382 | ||
383 | void trace2_region_leave_printf_va_fl(const char *file, int line, | |
384 | const char *category, const char *label, | |
385 | const struct repository *repo, | |
386 | const char *fmt, va_list ap); | |
387 | ||
388 | #define trace2_region_leave_printf_va(category, label, repo, fmt, ap) \ | |
389 | trace2_region_leave_printf_va_fl(__FILE__, __LINE__, (category), \ | |
390 | (label), (repo), (fmt), (ap)) | |
391 | ||
392 | void trace2_region_leave_printf_fl(const char *file, int line, | |
393 | const char *category, const char *label, | |
394 | const struct repository *repo, | |
395 | const char *fmt, ...); | |
396 | ||
397 | #ifdef HAVE_VARIADIC_MACROS | |
398 | #define trace2_region_leave_printf(category, label, repo, ...) \ | |
399 | trace2_region_leave_printf_fl(__FILE__, __LINE__, (category), (label), \ | |
400 | (repo), __VA_ARGS__) | |
401 | #else | |
402 | /* clang-format off */ | |
403 | __attribute__((format (region_leave_printf, 4, 5))) | |
404 | void trace2_region_leave_printf(const char *category, const char *label, | |
405 | const struct repository *repo, const char *fmt, | |
406 | ...); | |
407 | /* clang-format on */ | |
408 | #endif | |
409 | ||
6c51cb52 | 410 | /** |
ee4512ed JH |
411 | * Emit a key-value pair 'data' event of the form <category>.<key> = <value>. |
412 | * This event implicitly contains information about thread, nesting region, | |
413 | * and optional repo-id. | |
6c51cb52 HW |
414 | * This could be used to print the number of files in a directory during |
415 | * a multi-threaded recursive tree walk. | |
ee4512ed JH |
416 | * |
417 | * On event-based TRACE2 targets, this generates a 'data' event suitable | |
418 | * for post-processing. On printf-based TRACE2 targets, this is converted | |
419 | * into a fixed-format printf message. | |
420 | */ | |
421 | void trace2_data_string_fl(const char *file, int line, const char *category, | |
422 | const struct repository *repo, const char *key, | |
423 | const char *value); | |
424 | ||
425 | #define trace2_data_string(category, repo, key, value) \ | |
426 | trace2_data_string_fl(__FILE__, __LINE__, (category), (repo), (key), \ | |
427 | (value)) | |
428 | ||
429 | void trace2_data_intmax_fl(const char *file, int line, const char *category, | |
430 | const struct repository *repo, const char *key, | |
431 | intmax_t value); | |
432 | ||
433 | #define trace2_data_intmax(category, repo, key, value) \ | |
434 | trace2_data_intmax_fl(__FILE__, __LINE__, (category), (repo), (key), \ | |
435 | (value)) | |
436 | ||
437 | void trace2_data_json_fl(const char *file, int line, const char *category, | |
438 | const struct repository *repo, const char *key, | |
439 | const struct json_writer *jw); | |
440 | ||
441 | #define trace2_data_json(category, repo, key, value) \ | |
442 | trace2_data_json_fl(__FILE__, __LINE__, (category), (repo), (key), \ | |
443 | (value)) | |
444 | ||
445 | /* | |
446 | * Emit a 'printf' event. | |
447 | * | |
448 | * Write an arbitrary formatted message to the TRACE2 targets. These | |
449 | * text messages should be considered as human-readable strings without | |
450 | * any formatting guidelines. Post-processors may choose to ignore | |
451 | * them. | |
452 | */ | |
453 | void trace2_printf_va_fl(const char *file, int line, const char *fmt, | |
454 | va_list ap); | |
455 | ||
456 | #define trace2_printf_va(fmt, ap) \ | |
457 | trace2_printf_va_fl(__FILE__, __LINE__, (fmt), (ap)) | |
458 | ||
459 | void trace2_printf_fl(const char *file, int line, const char *fmt, ...); | |
460 | ||
461 | #ifdef HAVE_VARIADIC_MACROS | |
462 | #define trace2_printf(...) trace2_printf_fl(__FILE__, __LINE__, __VA_ARGS__) | |
463 | #else | |
464 | /* clang-format off */ | |
465 | __attribute__((format (printf, 1, 2))) | |
466 | void trace2_printf(const char *fmt, ...); | |
467 | /* clang-format on */ | |
468 | #endif | |
469 | ||
353d3d77 JH |
470 | /* |
471 | * Optional platform-specific code to dump information about the | |
472 | * current and any parent process(es). This is intended to allow | |
473 | * post-processors to know who spawned this git instance and anything | |
26c6f251 | 474 | * else that the platform may be able to tell us about the current process. |
353d3d77 | 475 | */ |
26c6f251 JH |
476 | |
477 | enum trace2_process_info_reason { | |
478 | TRACE2_PROCESS_INFO_STARTUP, | |
479 | TRACE2_PROCESS_INFO_EXIT, | |
480 | }; | |
481 | ||
353d3d77 | 482 | #if defined(GIT_WINDOWS_NATIVE) |
26c6f251 | 483 | void trace2_collect_process_info(enum trace2_process_info_reason reason); |
353d3d77 | 484 | #else |
26c6f251 JH |
485 | #define trace2_collect_process_info(reason) \ |
486 | do { \ | |
353d3d77 JH |
487 | } while (0) |
488 | #endif | |
489 | ||
ee4512ed | 490 | #endif /* TRACE2_H */ |