trace2.h 18 KB

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