api-trace2.txt 38 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172
  1. = Trace2 API
  2. The Trace2 API can be used to print debug, performance, and telemetry
  3. information to stderr or a file. The Trace2 feature is inactive unless
  4. explicitly enabled by enabling one or more Trace2 Targets.
  5. The Trace2 API is intended to replace the existing (Trace1)
  6. printf-style tracing provided by the existing `GIT_TRACE` and
  7. `GIT_TRACE_PERFORMANCE` facilities. During initial implementation,
  8. Trace2 and Trace1 may operate in parallel.
  9. The Trace2 API defines a set of high-level messages with known fields,
  10. such as (`start`: `argv`) and (`exit`: {`exit-code`, `elapsed-time`}).
  11. Trace2 instrumentation throughout the Git code base sends Trace2
  12. messages to the enabled Trace2 Targets. Targets transform these
  13. messages content into purpose-specific formats and write events to
  14. their data streams. In this manner, the Trace2 API can drive
  15. many different types of analysis.
  16. Targets are defined using a VTable allowing easy extension to other
  17. formats in the future. This might be used to define a binary format,
  18. for example.
  19. Trace2 is controlled using `trace2.*` config values in the system and
  20. global config files and `GIT_TRACE2*` environment variables. Trace2 does
  21. not read from repo local or worktree config files or respect `-c`
  22. command line config settings.
  23. == Trace2 Targets
  24. Trace2 defines the following set of Trace2 Targets.
  25. Format details are given in a later section.
  26. === The Normal Format Target
  27. The normal format target is a tradition printf format and similar
  28. to GIT_TRACE format. This format is enabled with the `GIT_TRACE2`
  29. environment variable or the `trace2.normalTarget` system or global
  30. config setting.
  31. For example
  32. ------------
  33. $ export GIT_TRACE2=~/log.normal
  34. $ git version
  35. git version 2.20.1.155.g426c96fcdb
  36. ------------
  37. or
  38. ------------
  39. $ git config --global trace2.normalTarget ~/log.normal
  40. $ git version
  41. git version 2.20.1.155.g426c96fcdb
  42. ------------
  43. yields
  44. ------------
  45. $ cat ~/log.normal
  46. 12:28:42.620009 common-main.c:38 version 2.20.1.155.g426c96fcdb
  47. 12:28:42.620989 common-main.c:39 start git version
  48. 12:28:42.621101 git.c:432 cmd_name version (version)
  49. 12:28:42.621215 git.c:662 exit elapsed:0.001227 code:0
  50. 12:28:42.621250 trace2/tr2_tgt_normal.c:124 atexit elapsed:0.001265 code:0
  51. ------------
  52. === The Performance Format Target
  53. The performance format target (PERF) is a column-based format to
  54. replace GIT_TRACE_PERFORMANCE and is suitable for development and
  55. testing, possibly to complement tools like gprof. This format is
  56. enabled with the `GIT_TRACE2_PERF` environment variable or the
  57. `trace2.perfTarget` system or global config setting.
  58. For example
  59. ------------
  60. $ export GIT_TRACE2_PERF=~/log.perf
  61. $ git version
  62. git version 2.20.1.155.g426c96fcdb
  63. ------------
  64. or
  65. ------------
  66. $ git config --global trace2.perfTarget ~/log.perf
  67. $ git version
  68. git version 2.20.1.155.g426c96fcdb
  69. ------------
  70. yields
  71. ------------
  72. $ cat ~/log.perf
  73. 12:28:42.620675 common-main.c:38 | d0 | main | version | | | | | 2.20.1.155.g426c96fcdb
  74. 12:28:42.621001 common-main.c:39 | d0 | main | start | | 0.001173 | | | git version
  75. 12:28:42.621111 git.c:432 | d0 | main | cmd_name | | | | | version (version)
  76. 12:28:42.621225 git.c:662 | d0 | main | exit | | 0.001227 | | | code:0
  77. 12:28:42.621259 trace2/tr2_tgt_perf.c:211 | d0 | main | atexit | | 0.001265 | | | code:0
  78. ------------
  79. === The Event Format Target
  80. The event format target is a JSON-based format of event data suitable
  81. for telemetry analysis. This format is enabled with the `GIT_TRACE2_EVENT`
  82. environment variable or the `trace2.eventTarget` system or global config
  83. setting.
  84. For example
  85. ------------
  86. $ export GIT_TRACE2_EVENT=~/log.event
  87. $ git version
  88. git version 2.20.1.155.g426c96fcdb
  89. ------------
  90. or
  91. ------------
  92. $ git config --global trace2.eventTarget ~/log.event
  93. $ git version
  94. git version 2.20.1.155.g426c96fcdb
  95. ------------
  96. yields
  97. ------------
  98. $ cat ~/log.event
  99. {"event":"version","sid":"sid":"20190408T191610.507018Z-H9b68c35f-P000059a8","thread":"main","time":"2019-01-16T17:28:42.620713Z","file":"common-main.c","line":38,"evt":"2","exe":"2.20.1.155.g426c96fcdb"}
  100. {"event":"start","sid":"20190408T191610.507018Z-H9b68c35f-P000059a8","thread":"main","time":"2019-01-16T17:28:42.621027Z","file":"common-main.c","line":39,"t_abs":0.001173,"argv":["git","version"]}
  101. {"event":"cmd_name","sid":"20190408T191610.507018Z-H9b68c35f-P000059a8","thread":"main","time":"2019-01-16T17:28:42.621122Z","file":"git.c","line":432,"name":"version","hierarchy":"version"}
  102. {"event":"exit","sid":"20190408T191610.507018Z-H9b68c35f-P000059a8","thread":"main","time":"2019-01-16T17:28:42.621236Z","file":"git.c","line":662,"t_abs":0.001227,"code":0}
  103. {"event":"atexit","sid":"20190408T191610.507018Z-H9b68c35f-P000059a8","thread":"main","time":"2019-01-16T17:28:42.621268Z","file":"trace2/tr2_tgt_event.c","line":163,"t_abs":0.001265,"code":0}
  104. ------------
  105. === Enabling a Target
  106. To enable a target, set the corresponding environment variable or
  107. system or global config value to one of the following:
  108. include::../trace2-target-values.txt[]
  109. When trace files are written to a target directory, they will be named according
  110. to the last component of the SID (optionally followed by a counter to avoid
  111. filename collisions).
  112. == Trace2 API
  113. All public Trace2 functions and macros are defined in `trace2.h` and
  114. `trace2.c`. All public symbols are prefixed with `trace2_`.
  115. There are no public Trace2 data structures.
  116. The Trace2 code also defines a set of private functions and data types
  117. in the `trace2/` directory. These symbols are prefixed with `tr2_`
  118. and should only be used by functions in `trace2.c`.
  119. == Conventions for Public Functions and Macros
  120. The functions defined by the Trace2 API are declared and documented
  121. in `trace2.h`. It defines the API functions and wrapper macros for
  122. Trace2.
  123. Some functions have a `_fl()` suffix to indicate that they take `file`
  124. and `line-number` arguments.
  125. Some functions have a `_va_fl()` suffix to indicate that they also
  126. take a `va_list` argument.
  127. Some functions have a `_printf_fl()` suffix to indicate that they also
  128. take a varargs argument.
  129. There are CPP wrapper macros and ifdefs to hide most of these details.
  130. See `trace2.h` for more details. The following discussion will only
  131. describe the simplified forms.
  132. == Public API
  133. All Trace2 API functions send a message to all of the active
  134. Trace2 Targets. This section describes the set of available
  135. messages.
  136. It helps to divide these functions into groups for discussion
  137. purposes.
  138. === Basic Command Messages
  139. These are concerned with the lifetime of the overall git process.
  140. e.g: `void trace2_initialize_clock()`, `void trace2_initialize()`,
  141. `int trace2_is_enabled()`, `void trace2_cmd_start(int argc, const char **argv)`.
  142. === Command Detail Messages
  143. These are concerned with describing the specific Git command
  144. after the command line, config, and environment are inspected.
  145. e.g: `void trace2_cmd_name(const char *name)`,
  146. `void trace2_cmd_mode(const char *mode)`.
  147. === Child Process Messages
  148. These are concerned with the various spawned child processes,
  149. including shell scripts, git commands, editors, pagers, and hooks.
  150. e.g: `void trace2_child_start(struct child_process *cmd)`.
  151. === Git Thread Messages
  152. These messages are concerned with Git thread usage.
  153. e.g: `void trace2_thread_start(const char *thread_name)`.
  154. === Region and Data Messages
  155. These are concerned with recording performance data
  156. over regions or spans of code. e.g:
  157. `void trace2_region_enter(const char *category, const char *label, const struct repository *repo)`.
  158. Refer to trace2.h for details about all trace2 functions.
  159. == Trace2 Target Formats
  160. === NORMAL Format
  161. Events are written as lines of the form:
  162. ------------
  163. [<time> SP <filename>:<line> SP+] <event-name> [[SP] <event-message>] LF
  164. ------------
  165. `<event-name>`::
  166. is the event name.
  167. `<event-message>`::
  168. is a free-form printf message intended for human consumption.
  169. +
  170. Note that this may contain embedded LF or CRLF characters that are
  171. not escaped, so the event may spill across multiple lines.
  172. If `GIT_TRACE2_BRIEF` or `trace2.normalBrief` is true, the `time`, `filename`,
  173. and `line` fields are omitted.
  174. This target is intended to be more of a summary (like GIT_TRACE) and
  175. less detailed than the other targets. It ignores thread, region, and
  176. data messages, for example.
  177. === PERF Format
  178. Events are written as lines of the form:
  179. ------------
  180. [<time> SP <filename>:<line> SP+
  181. BAR SP] d<depth> SP
  182. BAR SP <thread-name> SP+
  183. BAR SP <event-name> SP+
  184. BAR SP [r<repo-id>] SP+
  185. BAR SP [<t_abs>] SP+
  186. BAR SP [<t_rel>] SP+
  187. BAR SP [<category>] SP+
  188. BAR SP DOTS* <perf-event-message>
  189. LF
  190. ------------
  191. `<depth>`::
  192. is the git process depth. This is the number of parent
  193. git processes. A top-level git command has depth value "d0".
  194. A child of it has depth value "d1". A second level child
  195. has depth value "d2" and so on.
  196. `<thread-name>`::
  197. is a unique name for the thread. The primary thread
  198. is called "main". Other thread names are of the form "th%d:%s"
  199. and include a unique number and the name of the thread-proc.
  200. `<event-name>`::
  201. is the event name.
  202. `<repo-id>`::
  203. when present, is a number indicating the repository
  204. in use. A `def_repo` event is emitted when a repository is
  205. opened. This defines the repo-id and associated worktree.
  206. Subsequent repo-specific events will reference this repo-id.
  207. +
  208. Currently, this is always "r1" for the main repository.
  209. This field is in anticipation of in-proc submodules in the future.
  210. `<t_abs>`::
  211. when present, is the absolute time in seconds since the
  212. program started.
  213. `<t_rel>`::
  214. when present, is time in seconds relative to the start of
  215. the current region. For a thread-exit event, it is the elapsed
  216. time of the thread.
  217. `<category>`::
  218. is present on region and data events and is used to
  219. indicate a broad category, such as "index" or "status".
  220. `<perf-event-message>`::
  221. is a free-form printf message intended for human consumption.
  222. ------------
  223. 15:33:33.532712 wt-status.c:2310 | d0 | main | region_enter | r1 | 0.126064 | | status | label:print
  224. 15:33:33.532712 wt-status.c:2331 | d0 | main | region_leave | r1 | 0.127568 | 0.001504 | status | label:print
  225. ------------
  226. If `GIT_TRACE2_PERF_BRIEF` or `trace2.perfBrief` is true, the `time`, `file`,
  227. and `line` fields are omitted.
  228. ------------
  229. d0 | main | region_leave | r1 | 0.011717 | 0.009122 | index | label:preload
  230. ------------
  231. The PERF target is intended for interactive performance analysis
  232. during development and is quite noisy.
  233. === EVENT Format
  234. Each event is a JSON-object containing multiple key/value pairs
  235. written as a single line and followed by a LF.
  236. ------------
  237. '{' <key> ':' <value> [',' <key> ':' <value>]* '}' LF
  238. ------------
  239. Some key/value pairs are common to all events and some are
  240. event-specific.
  241. ==== Common Key/Value Pairs
  242. The following key/value pairs are common to all events:
  243. ------------
  244. {
  245. "event":"version",
  246. "sid":"20190408T191827.272759Z-H9b68c35f-P00003510",
  247. "thread":"main",
  248. "time":"2019-04-08T19:18:27.282761Z",
  249. "file":"common-main.c",
  250. "line":42,
  251. ...
  252. }
  253. ------------
  254. `"event":<event>`::
  255. is the event name.
  256. `"sid":<sid>`::
  257. is the session-id. This is a unique string to identify the
  258. process instance to allow all events emitted by a process to
  259. be identified. A session-id is used instead of a PID because
  260. PIDs are recycled by the OS. For child git processes, the
  261. session-id is prepended with the session-id of the parent git
  262. process to allow parent-child relationships to be identified
  263. during post-processing.
  264. `"thread":<thread>`::
  265. is the thread name.
  266. `"time":<time>`::
  267. is the UTC time of the event.
  268. `"file":<filename>`::
  269. is source file generating the event.
  270. `"line":<line-number>`::
  271. is the integer source line number generating the event.
  272. `"repo":<repo-id>`::
  273. when present, is the integer repo-id as described previously.
  274. If `GIT_TRACE2_EVENT_BRIEF` or `trace2.eventBrief` is true, the `file`
  275. and `line` fields are omitted from all events and the `time` field is
  276. only present on the "start" and "atexit" events.
  277. ==== Event-Specific Key/Value Pairs
  278. `"version"`::
  279. This event gives the version of the executable and the EVENT format. It
  280. should always be the first event in a trace session. The EVENT format
  281. version will be incremented if new event types are added, if existing
  282. fields are removed, or if there are significant changes in
  283. interpretation of existing events or fields. Smaller changes, such as
  284. adding a new field to an existing event, will not require an increment
  285. to the EVENT format version.
  286. +
  287. ------------
  288. {
  289. "event":"version",
  290. ...
  291. "evt":"2", # EVENT format version
  292. "exe":"2.20.1.155.g426c96fcdb" # git version
  293. }
  294. ------------
  295. `"discard"`::
  296. This event is written to the git-trace2-discard sentinel file if there
  297. are too many files in the target trace directory (see the
  298. trace2.maxFiles config option).
  299. +
  300. ------------
  301. {
  302. "event":"discard",
  303. ...
  304. }
  305. ------------
  306. `"start"`::
  307. This event contains the complete argv received by main().
  308. +
  309. ------------
  310. {
  311. "event":"start",
  312. ...
  313. "t_abs":0.001227, # elapsed time in seconds
  314. "argv":["git","version"]
  315. }
  316. ------------
  317. `"exit"`::
  318. This event is emitted when git calls `exit()`.
  319. +
  320. ------------
  321. {
  322. "event":"exit",
  323. ...
  324. "t_abs":0.001227, # elapsed time in seconds
  325. "code":0 # exit code
  326. }
  327. ------------
  328. `"atexit"`::
  329. This event is emitted by the Trace2 `atexit` routine during
  330. final shutdown. It should be the last event emitted by the
  331. process.
  332. +
  333. (The elapsed time reported here is greater than the time reported in
  334. the "exit" event because it runs after all other atexit tasks have
  335. completed.)
  336. +
  337. ------------
  338. {
  339. "event":"atexit",
  340. ...
  341. "t_abs":0.001227, # elapsed time in seconds
  342. "code":0 # exit code
  343. }
  344. ------------
  345. `"signal"`::
  346. This event is emitted when the program is terminated by a user
  347. signal. Depending on the platform, the signal event may
  348. prevent the "atexit" event from being generated.
  349. +
  350. ------------
  351. {
  352. "event":"signal",
  353. ...
  354. "t_abs":0.001227, # elapsed time in seconds
  355. "signo":13 # SIGTERM, SIGINT, etc.
  356. }
  357. ------------
  358. `"error"`::
  359. This event is emitted when one of the `error()`, `die()`,
  360. or `usage()` functions are called.
  361. +
  362. ------------
  363. {
  364. "event":"error",
  365. ...
  366. "msg":"invalid option: --cahced", # formatted error message
  367. "fmt":"invalid option: %s" # error format string
  368. }
  369. ------------
  370. +
  371. The error event may be emitted more than once. The format string
  372. allows post-processors to group errors by type without worrying
  373. about specific error arguments.
  374. `"cmd_path"`::
  375. This event contains the discovered full path of the git
  376. executable (on platforms that are configured to resolve it).
  377. +
  378. ------------
  379. {
  380. "event":"cmd_path",
  381. ...
  382. "path":"C:/work/gfw/git.exe"
  383. }
  384. ------------
  385. `"cmd_name"`::
  386. This event contains the command name for this git process
  387. and the hierarchy of commands from parent git processes.
  388. +
  389. ------------
  390. {
  391. "event":"cmd_name",
  392. ...
  393. "name":"pack-objects",
  394. "hierarchy":"push/pack-objects"
  395. }
  396. ------------
  397. +
  398. Normally, the "name" field contains the canonical name of the
  399. command. When a canonical name is not available, one of
  400. these special values are used:
  401. +
  402. ------------
  403. "_query_" # "git --html-path"
  404. "_run_dashed_" # when "git foo" tries to run "git-foo"
  405. "_run_shell_alias_" # alias expansion to a shell command
  406. "_run_git_alias_" # alias expansion to a git command
  407. "_usage_" # usage error
  408. ------------
  409. `"cmd_mode"`::
  410. This event, when present, describes the command variant This
  411. event may be emitted more than once.
  412. +
  413. ------------
  414. {
  415. "event":"cmd_mode",
  416. ...
  417. "name":"branch"
  418. }
  419. ------------
  420. +
  421. The "name" field is an arbitrary string to describe the command mode.
  422. For example, checkout can checkout a branch or an individual file.
  423. And these variations typically have different performance
  424. characteristics that are not comparable.
  425. `"alias"`::
  426. This event is present when an alias is expanded.
  427. +
  428. ------------
  429. {
  430. "event":"alias",
  431. ...
  432. "alias":"l", # registered alias
  433. "argv":["log","--graph"] # alias expansion
  434. }
  435. ------------
  436. `"child_start"`::
  437. This event describes a child process that is about to be
  438. spawned.
  439. +
  440. ------------
  441. {
  442. "event":"child_start",
  443. ...
  444. "child_id":2,
  445. "child_class":"?",
  446. "use_shell":false,
  447. "argv":["git","rev-list","--objects","--stdin","--not","--all","--quiet"]
  448. "hook_name":"<hook_name>" # present when child_class is "hook"
  449. "cd":"<path>" # present when cd is required
  450. }
  451. ------------
  452. +
  453. The "child_id" field can be used to match this child_start with the
  454. corresponding child_exit event.
  455. +
  456. The "child_class" field is a rough classification, such as "editor",
  457. "pager", "transport/*", and "hook". Unclassified children are classified
  458. with "?".
  459. `"child_exit"`::
  460. This event is generated after the current process has returned
  461. from the waitpid() and collected the exit information from the
  462. child.
  463. +
  464. ------------
  465. {
  466. "event":"child_exit",
  467. ...
  468. "child_id":2,
  469. "pid":14708, # child PID
  470. "code":0, # child exit-code
  471. "t_rel":0.110605 # observed run-time of child process
  472. }
  473. ------------
  474. +
  475. Note that the session-id of the child process is not available to
  476. the current/spawning process, so the child's PID is reported here as
  477. a hint for post-processing. (But it is only a hint because the child
  478. process may be a shell script which doesn't have a session-id.)
  479. +
  480. Note that the `t_rel` field contains the observed run time in seconds
  481. for the child process (starting before the fork/exec/spawn and
  482. stopping after the waitpid() and includes OS process creation overhead).
  483. So this time will be slightly larger than the atexit time reported by
  484. the child process itself.
  485. `"exec"`::
  486. This event is generated before git attempts to `exec()`
  487. another command rather than starting a child process.
  488. +
  489. ------------
  490. {
  491. "event":"exec",
  492. ...
  493. "exec_id":0,
  494. "exe":"git",
  495. "argv":["foo", "bar"]
  496. }
  497. ------------
  498. +
  499. The "exec_id" field is a command-unique id and is only useful if the
  500. `exec()` fails and a corresponding exec_result event is generated.
  501. `"exec_result"`::
  502. This event is generated if the `exec()` fails and control
  503. returns to the current git command.
  504. +
  505. ------------
  506. {
  507. "event":"exec_result",
  508. ...
  509. "exec_id":0,
  510. "code":1 # error code (errno) from exec()
  511. }
  512. ------------
  513. `"thread_start"`::
  514. This event is generated when a thread is started. It is
  515. generated from *within* the new thread's thread-proc (for TLS
  516. reasons).
  517. +
  518. ------------
  519. {
  520. "event":"thread_start",
  521. ...
  522. "thread":"th02:preload_thread" # thread name
  523. }
  524. ------------
  525. `"thread_exit"`::
  526. This event is generated when a thread exits. It is generated
  527. from *within* the thread's thread-proc (for TLS reasons).
  528. +
  529. ------------
  530. {
  531. "event":"thread_exit",
  532. ...
  533. "thread":"th02:preload_thread", # thread name
  534. "t_rel":0.007328 # thread elapsed time
  535. }
  536. ------------
  537. `"def_param"`::
  538. This event is generated to log a global parameter, such as a config
  539. setting, command-line flag, or environment variable.
  540. +
  541. ------------
  542. {
  543. "event":"def_param",
  544. ...
  545. "param":"core.abbrev",
  546. "value":"7"
  547. }
  548. ------------
  549. `"def_repo"`::
  550. This event defines a repo-id and associates it with the root
  551. of the worktree.
  552. +
  553. ------------
  554. {
  555. "event":"def_repo",
  556. ...
  557. "repo":1,
  558. "worktree":"/Users/jeffhost/work/gfw"
  559. }
  560. ------------
  561. +
  562. As stated earlier, the repo-id is currently always 1, so there will
  563. only be one def_repo event. Later, if in-proc submodules are
  564. supported, a def_repo event should be emitted for each submodule
  565. visited.
  566. `"region_enter"`::
  567. This event is generated when entering a region.
  568. +
  569. ------------
  570. {
  571. "event":"region_enter",
  572. ...
  573. "repo":1, # optional
  574. "nesting":1, # current region stack depth
  575. "category":"index", # optional
  576. "label":"do_read_index", # optional
  577. "msg":".git/index" # optional
  578. }
  579. ------------
  580. +
  581. The `category` field may be used in a future enhancement to
  582. do category-based filtering.
  583. +
  584. `GIT_TRACE2_EVENT_NESTING` or `trace2.eventNesting` can be used to
  585. filter deeply nested regions and data events. It defaults to "2".
  586. `"region_leave"`::
  587. This event is generated when leaving a region.
  588. +
  589. ------------
  590. {
  591. "event":"region_leave",
  592. ...
  593. "repo":1, # optional
  594. "t_rel":0.002876, # time spent in region in seconds
  595. "nesting":1, # region stack depth
  596. "category":"index", # optional
  597. "label":"do_read_index", # optional
  598. "msg":".git/index" # optional
  599. }
  600. ------------
  601. `"data"`::
  602. This event is generated to log a thread- and region-local
  603. key/value pair.
  604. +
  605. ------------
  606. {
  607. "event":"data",
  608. ...
  609. "repo":1, # optional
  610. "t_abs":0.024107, # absolute elapsed time
  611. "t_rel":0.001031, # elapsed time in region/thread
  612. "nesting":2, # region stack depth
  613. "category":"index",
  614. "key":"read/cache_nr",
  615. "value":"3552"
  616. }
  617. ------------
  618. +
  619. The "value" field may be an integer or a string.
  620. `"data-json"`::
  621. This event is generated to log a pre-formatted JSON string
  622. containing structured data.
  623. +
  624. ------------
  625. {
  626. "event":"data_json",
  627. ...
  628. "repo":1, # optional
  629. "t_abs":0.015905,
  630. "t_rel":0.015905,
  631. "nesting":1,
  632. "category":"process",
  633. "key":"windows/ancestry",
  634. "value":["bash.exe","bash.exe"]
  635. }
  636. ------------
  637. == Example Trace2 API Usage
  638. Here is a hypothetical usage of the Trace2 API showing the intended
  639. usage (without worrying about the actual Git details).
  640. Initialization::
  641. Initialization happens in `main()`. Behind the scenes, an
  642. `atexit` and `signal` handler are registered.
  643. +
  644. ----------------
  645. int main(int argc, const char **argv)
  646. {
  647. int exit_code;
  648. trace2_initialize();
  649. trace2_cmd_start(argv);
  650. exit_code = cmd_main(argc, argv);
  651. trace2_cmd_exit(exit_code);
  652. return exit_code;
  653. }
  654. ----------------
  655. Command Details::
  656. After the basics are established, additional command
  657. information can be sent to Trace2 as it is discovered.
  658. +
  659. ----------------
  660. int cmd_checkout(int argc, const char **argv)
  661. {
  662. trace2_cmd_name("checkout");
  663. trace2_cmd_mode("branch");
  664. trace2_def_repo(the_repository);
  665. // emit "def_param" messages for "interesting" config settings.
  666. trace2_cmd_list_config();
  667. if (do_something())
  668. trace2_cmd_error("Path '%s': cannot do something", path);
  669. return 0;
  670. }
  671. ----------------
  672. Child Processes::
  673. Wrap code spawning child processes.
  674. +
  675. ----------------
  676. void run_child(...)
  677. {
  678. int child_exit_code;
  679. struct child_process cmd = CHILD_PROCESS_INIT;
  680. ...
  681. cmd.trace2_child_class = "editor";
  682. trace2_child_start(&cmd);
  683. child_exit_code = spawn_child_and_wait_for_it();
  684. trace2_child_exit(&cmd, child_exit_code);
  685. }
  686. ----------------
  687. +
  688. For example, the following fetch command spawned ssh, index-pack,
  689. rev-list, and gc. This example also shows that fetch took
  690. 5.199 seconds and of that 4.932 was in ssh.
  691. +
  692. ----------------
  693. $ export GIT_TRACE2_BRIEF=1
  694. $ export GIT_TRACE2=~/log.normal
  695. $ git fetch origin
  696. ...
  697. ----------------
  698. +
  699. ----------------
  700. $ cat ~/log.normal
  701. version 2.20.1.vfs.1.1.47.g534dbe1ad1
  702. start git fetch origin
  703. worktree /Users/jeffhost/work/gfw
  704. cmd_name fetch (fetch)
  705. child_start[0] ssh git@github.com ...
  706. child_start[1] git index-pack ...
  707. ... (Trace2 events from child processes omitted)
  708. child_exit[1] pid:14707 code:0 elapsed:0.076353
  709. child_exit[0] pid:14706 code:0 elapsed:4.931869
  710. child_start[2] git rev-list ...
  711. ... (Trace2 events from child process omitted)
  712. child_exit[2] pid:14708 code:0 elapsed:0.110605
  713. child_start[3] git gc --auto
  714. ... (Trace2 events from child process omitted)
  715. child_exit[3] pid:14709 code:0 elapsed:0.006240
  716. exit elapsed:5.198503 code:0
  717. atexit elapsed:5.198541 code:0
  718. ----------------
  719. +
  720. When a git process is a (direct or indirect) child of another
  721. git process, it inherits Trace2 context information. This
  722. allows the child to print the command hierarchy. This example
  723. shows gc as child[3] of fetch. When the gc process reports
  724. its name as "gc", it also reports the hierarchy as "fetch/gc".
  725. (In this example, trace2 messages from the child process is
  726. indented for clarity.)
  727. +
  728. ----------------
  729. $ export GIT_TRACE2_BRIEF=1
  730. $ export GIT_TRACE2=~/log.normal
  731. $ git fetch origin
  732. ...
  733. ----------------
  734. +
  735. ----------------
  736. $ cat ~/log.normal
  737. version 2.20.1.160.g5676107ecd.dirty
  738. start git fetch official
  739. worktree /Users/jeffhost/work/gfw
  740. cmd_name fetch (fetch)
  741. ...
  742. child_start[3] git gc --auto
  743. version 2.20.1.160.g5676107ecd.dirty
  744. start /Users/jeffhost/work/gfw/git gc --auto
  745. worktree /Users/jeffhost/work/gfw
  746. cmd_name gc (fetch/gc)
  747. exit elapsed:0.001959 code:0
  748. atexit elapsed:0.001997 code:0
  749. child_exit[3] pid:20303 code:0 elapsed:0.007564
  750. exit elapsed:3.868938 code:0
  751. atexit elapsed:3.868970 code:0
  752. ----------------
  753. Regions::
  754. Regions can be use to time an interesting section of code.
  755. +
  756. ----------------
  757. void wt_status_collect(struct wt_status *s)
  758. {
  759. trace2_region_enter("status", "worktrees", s->repo);
  760. wt_status_collect_changes_worktree(s);
  761. trace2_region_leave("status", "worktrees", s->repo);
  762. trace2_region_enter("status", "index", s->repo);
  763. wt_status_collect_changes_index(s);
  764. trace2_region_leave("status", "index", s->repo);
  765. trace2_region_enter("status", "untracked", s->repo);
  766. wt_status_collect_untracked(s);
  767. trace2_region_leave("status", "untracked", s->repo);
  768. }
  769. void wt_status_print(struct wt_status *s)
  770. {
  771. trace2_region_enter("status", "print", s->repo);
  772. switch (s->status_format) {
  773. ...
  774. }
  775. trace2_region_leave("status", "print", s->repo);
  776. }
  777. ----------------
  778. +
  779. In this example, scanning for untracked files ran from +0.012568 to
  780. +0.027149 (since the process started) and took 0.014581 seconds.
  781. +
  782. ----------------
  783. $ export GIT_TRACE2_PERF_BRIEF=1
  784. $ export GIT_TRACE2_PERF=~/log.perf
  785. $ git status
  786. ...
  787. $ cat ~/log.perf
  788. d0 | main | version | | | | | 2.20.1.160.g5676107ecd.dirty
  789. d0 | main | start | | 0.001173 | | | git status
  790. d0 | main | def_repo | r1 | | | | worktree:/Users/jeffhost/work/gfw
  791. d0 | main | cmd_name | | | | | status (status)
  792. ...
  793. d0 | main | region_enter | r1 | 0.010988 | | status | label:worktrees
  794. d0 | main | region_leave | r1 | 0.011236 | 0.000248 | status | label:worktrees
  795. d0 | main | region_enter | r1 | 0.011260 | | status | label:index
  796. d0 | main | region_leave | r1 | 0.012542 | 0.001282 | status | label:index
  797. d0 | main | region_enter | r1 | 0.012568 | | status | label:untracked
  798. d0 | main | region_leave | r1 | 0.027149 | 0.014581 | status | label:untracked
  799. d0 | main | region_enter | r1 | 0.027411 | | status | label:print
  800. d0 | main | region_leave | r1 | 0.028741 | 0.001330 | status | label:print
  801. d0 | main | exit | | 0.028778 | | | code:0
  802. d0 | main | atexit | | 0.028809 | | | code:0
  803. ----------------
  804. +
  805. Regions may be nested. This causes messages to be indented in the
  806. PERF target, for example.
  807. Elapsed times are relative to the start of the corresponding nesting
  808. level as expected. For example, if we add region message to:
  809. +
  810. ----------------
  811. static enum path_treatment read_directory_recursive(struct dir_struct *dir,
  812. struct index_state *istate, const char *base, int baselen,
  813. struct untracked_cache_dir *untracked, int check_only,
  814. int stop_at_first_file, const struct pathspec *pathspec)
  815. {
  816. enum path_treatment state, subdir_state, dir_state = path_none;
  817. trace2_region_enter_printf("dir", "read_recursive", NULL, "%.*s", baselen, base);
  818. ...
  819. trace2_region_leave_printf("dir", "read_recursive", NULL, "%.*s", baselen, base);
  820. return dir_state;
  821. }
  822. ----------------
  823. +
  824. We can further investigate the time spent scanning for untracked files.
  825. +
  826. ----------------
  827. $ export GIT_TRACE2_PERF_BRIEF=1
  828. $ export GIT_TRACE2_PERF=~/log.perf
  829. $ git status
  830. ...
  831. $ cat ~/log.perf
  832. d0 | main | version | | | | | 2.20.1.162.gb4ccea44db.dirty
  833. d0 | main | start | | 0.001173 | | | git status
  834. d0 | main | def_repo | r1 | | | | worktree:/Users/jeffhost/work/gfw
  835. d0 | main | cmd_name | | | | | status (status)
  836. ...
  837. d0 | main | region_enter | r1 | 0.015047 | | status | label:untracked
  838. d0 | main | region_enter | | 0.015132 | | dir | ..label:read_recursive
  839. d0 | main | region_enter | | 0.016341 | | dir | ....label:read_recursive vcs-svn/
  840. d0 | main | region_leave | | 0.016422 | 0.000081 | dir | ....label:read_recursive vcs-svn/
  841. d0 | main | region_enter | | 0.016446 | | dir | ....label:read_recursive xdiff/
  842. d0 | main | region_leave | | 0.016522 | 0.000076 | dir | ....label:read_recursive xdiff/
  843. d0 | main | region_enter | | 0.016612 | | dir | ....label:read_recursive git-gui/
  844. d0 | main | region_enter | | 0.016698 | | dir | ......label:read_recursive git-gui/po/
  845. d0 | main | region_enter | | 0.016810 | | dir | ........label:read_recursive git-gui/po/glossary/
  846. d0 | main | region_leave | | 0.016863 | 0.000053 | dir | ........label:read_recursive git-gui/po/glossary/
  847. ...
  848. d0 | main | region_enter | | 0.031876 | | dir | ....label:read_recursive builtin/
  849. d0 | main | region_leave | | 0.032270 | 0.000394 | dir | ....label:read_recursive builtin/
  850. d0 | main | region_leave | | 0.032414 | 0.017282 | dir | ..label:read_recursive
  851. d0 | main | region_leave | r1 | 0.032454 | 0.017407 | status | label:untracked
  852. ...
  853. d0 | main | exit | | 0.034279 | | | code:0
  854. d0 | main | atexit | | 0.034322 | | | code:0
  855. ----------------
  856. +
  857. Trace2 regions are similar to the existing trace_performance_enter()
  858. and trace_performance_leave() routines, but are thread safe and
  859. maintain per-thread stacks of timers.
  860. Data Messages::
  861. Data messages added to a region.
  862. +
  863. ----------------
  864. int read_index_from(struct index_state *istate, const char *path,
  865. const char *gitdir)
  866. {
  867. trace2_region_enter_printf("index", "do_read_index", the_repository, "%s", path);
  868. ...
  869. trace2_data_intmax("index", the_repository, "read/version", istate->version);
  870. trace2_data_intmax("index", the_repository, "read/cache_nr", istate->cache_nr);
  871. trace2_region_leave_printf("index", "do_read_index", the_repository, "%s", path);
  872. }
  873. ----------------
  874. +
  875. This example shows that the index contained 3552 entries.
  876. +
  877. ----------------
  878. $ export GIT_TRACE2_PERF_BRIEF=1
  879. $ export GIT_TRACE2_PERF=~/log.perf
  880. $ git status
  881. ...
  882. $ cat ~/log.perf
  883. d0 | main | version | | | | | 2.20.1.156.gf9916ae094.dirty
  884. d0 | main | start | | 0.001173 | | | git status
  885. d0 | main | def_repo | r1 | | | | worktree:/Users/jeffhost/work/gfw
  886. d0 | main | cmd_name | | | | | status (status)
  887. d0 | main | region_enter | r1 | 0.001791 | | index | label:do_read_index .git/index
  888. d0 | main | data | r1 | 0.002494 | 0.000703 | index | ..read/version:2
  889. d0 | main | data | r1 | 0.002520 | 0.000729 | index | ..read/cache_nr:3552
  890. d0 | main | region_leave | r1 | 0.002539 | 0.000748 | index | label:do_read_index .git/index
  891. ...
  892. ----------------
  893. Thread Events::
  894. Thread messages added to a thread-proc.
  895. +
  896. For example, the multithreaded preload-index code can be
  897. instrumented with a region around the thread pool and then
  898. per-thread start and exit events within the threadproc.
  899. +
  900. ----------------
  901. static void *preload_thread(void *_data)
  902. {
  903. // start the per-thread clock and emit a message.
  904. trace2_thread_start("preload_thread");
  905. // report which chunk of the array this thread was assigned.
  906. trace2_data_intmax("index", the_repository, "offset", p->offset);
  907. trace2_data_intmax("index", the_repository, "count", nr);
  908. do {
  909. ...
  910. } while (--nr > 0);
  911. ...
  912. // report elapsed time taken by this thread.
  913. trace2_thread_exit();
  914. return NULL;
  915. }
  916. void preload_index(struct index_state *index,
  917. const struct pathspec *pathspec,
  918. unsigned int refresh_flags)
  919. {
  920. trace2_region_enter("index", "preload", the_repository);
  921. for (i = 0; i < threads; i++) {
  922. ... /* create thread */
  923. }
  924. for (i = 0; i < threads; i++) {
  925. ... /* join thread */
  926. }
  927. trace2_region_leave("index", "preload", the_repository);
  928. }
  929. ----------------
  930. +
  931. In this example preload_index() was executed by the `main` thread
  932. and started the `preload` region. Seven threads, named
  933. `th01:preload_thread` through `th07:preload_thread`, were started.
  934. Events from each thread are atomically appended to the shared target
  935. stream as they occur so they may appear in random order with respect
  936. other threads. Finally, the main thread waits for the threads to
  937. finish and leaves the region.
  938. +
  939. Data events are tagged with the active thread name. They are used
  940. to report the per-thread parameters.
  941. +
  942. ----------------
  943. $ export GIT_TRACE2_PERF_BRIEF=1
  944. $ export GIT_TRACE2_PERF=~/log.perf
  945. $ git status
  946. ...
  947. $ cat ~/log.perf
  948. ...
  949. d0 | main | region_enter | r1 | 0.002595 | | index | label:preload
  950. d0 | th01:preload_thread | thread_start | | 0.002699 | | |
  951. d0 | th02:preload_thread | thread_start | | 0.002721 | | |
  952. d0 | th01:preload_thread | data | r1 | 0.002736 | 0.000037 | index | offset:0
  953. d0 | th02:preload_thread | data | r1 | 0.002751 | 0.000030 | index | offset:2032
  954. d0 | th03:preload_thread | thread_start | | 0.002711 | | |
  955. d0 | th06:preload_thread | thread_start | | 0.002739 | | |
  956. d0 | th01:preload_thread | data | r1 | 0.002766 | 0.000067 | index | count:508
  957. d0 | th06:preload_thread | data | r1 | 0.002856 | 0.000117 | index | offset:2540
  958. d0 | th03:preload_thread | data | r1 | 0.002824 | 0.000113 | index | offset:1016
  959. d0 | th04:preload_thread | thread_start | | 0.002710 | | |
  960. d0 | th02:preload_thread | data | r1 | 0.002779 | 0.000058 | index | count:508
  961. d0 | th06:preload_thread | data | r1 | 0.002966 | 0.000227 | index | count:508
  962. d0 | th07:preload_thread | thread_start | | 0.002741 | | |
  963. d0 | th07:preload_thread | data | r1 | 0.003017 | 0.000276 | index | offset:3048
  964. d0 | th05:preload_thread | thread_start | | 0.002712 | | |
  965. d0 | th05:preload_thread | data | r1 | 0.003067 | 0.000355 | index | offset:1524
  966. d0 | th05:preload_thread | data | r1 | 0.003090 | 0.000378 | index | count:508
  967. d0 | th07:preload_thread | data | r1 | 0.003037 | 0.000296 | index | count:504
  968. d0 | th03:preload_thread | data | r1 | 0.002971 | 0.000260 | index | count:508
  969. d0 | th04:preload_thread | data | r1 | 0.002983 | 0.000273 | index | offset:508
  970. d0 | th04:preload_thread | data | r1 | 0.007311 | 0.004601 | index | count:508
  971. d0 | th05:preload_thread | thread_exit | | 0.008781 | 0.006069 | |
  972. d0 | th01:preload_thread | thread_exit | | 0.009561 | 0.006862 | |
  973. d0 | th03:preload_thread | thread_exit | | 0.009742 | 0.007031 | |
  974. d0 | th06:preload_thread | thread_exit | | 0.009820 | 0.007081 | |
  975. d0 | th02:preload_thread | thread_exit | | 0.010274 | 0.007553 | |
  976. d0 | th07:preload_thread | thread_exit | | 0.010477 | 0.007736 | |
  977. d0 | th04:preload_thread | thread_exit | | 0.011657 | 0.008947 | |
  978. d0 | main | region_leave | r1 | 0.011717 | 0.009122 | index | label:preload
  979. ...
  980. d0 | main | exit | | 0.029996 | | | code:0
  981. d0 | main | atexit | | 0.030027 | | | code:0
  982. ----------------
  983. +
  984. In this example, the preload region took 0.009122 seconds. The 7 threads
  985. took between 0.006069 and 0.008947 seconds to work on their portion of
  986. the index. Thread "th01" worked on 508 items at offset 0. Thread "th02"
  987. worked on 508 items at offset 2032. Thread "th04" worked on 508 items
  988. at offset 508.
  989. +
  990. This example also shows that thread names are assigned in a racy manner
  991. as each thread starts and allocates TLS storage.
  992. == Future Work
  993. === Relationship to the Existing Trace Api (api-trace.txt)
  994. There are a few issues to resolve before we can completely
  995. switch to Trace2.
  996. * Updating existing tests that assume GIT_TRACE format messages.
  997. * How to best handle custom GIT_TRACE_<key> messages?
  998. ** The GIT_TRACE_<key> mechanism allows each <key> to write to a
  999. different file (in addition to just stderr).
  1000. ** Do we want to maintain that ability or simply write to the existing
  1001. Trace2 targets (and convert <key> to a "category").