1171 lines · 37.7 KB · Text
RawDownload
= Trace2 API The Trace2 API can be used to print debug, performance, and telemetryinformation to stderr or a file.  The Trace2 feature is inactive unlessexplicitly enabled by enabling one or more Trace2 Targets. The Trace2 API is intended to replace the existing (Trace1)printf-style tracing provided by the existing `GIT_TRACE` and`GIT_TRACE_PERFORMANCE` facilities.  During initial implementation,Trace2 and Trace1 may operate in parallel. The Trace2 API defines a set of high-level messages with known fields,such as (`start`: `argv`) and (`exit`: {`exit-code`, `elapsed-time`}). Trace2 instrumentation throughout the Git code base sends Trace2messages to the enabled Trace2 Targets.  Targets transform thesemessages content into purpose-specific formats and write events totheir data streams.  In this manner, the Trace2 API can drivemany different types of analysis. Targets are defined using a VTable allowing easy extension to otherformats in the future.  This might be used to define a binary format,for example. Trace2 is controlled using `trace2.*` config values in the system andglobal config files and `GIT_TRACE2*` environment variables.  Trace2 doesnot read from repo local or worktree config files or respect `-c`command line config settings. == Trace2 Targets Trace2 defines the following set of Trace2 Targets.Format details are given in a later section. === The Normal Format Target The normal format target is a tradition printf format and similarto GIT_TRACE format.  This format is enabled with the `GIT_TRACE2`environment variable or the `trace2.normalTarget` system or globalconfig setting. For example ------------$ export GIT_TRACE2=~/log.normal$ git versiongit version 2.20.1.155.g426c96fcdb------------ or ------------$ git config --global trace2.normalTarget ~/log.normal$ git versiongit version 2.20.1.155.g426c96fcdb------------ yields ------------$ cat ~/log.normal12:28:42.620009 common-main.c:38                  version 2.20.1.155.g426c96fcdb12:28:42.620989 common-main.c:39                  start git version12:28:42.621101 git.c:432                         cmd_name version (version)12:28:42.621215 git.c:662                         exit elapsed:0.001227 code:012:28:42.621250 trace2/tr2_tgt_normal.c:124       atexit elapsed:0.001265 code:0------------ === The Performance Format Target The performance format target (PERF) is a column-based format toreplace GIT_TRACE_PERFORMANCE and is suitable for development andtesting, possibly to complement tools like gprof.  This format isenabled with the `GIT_TRACE2_PERF` environment variable or the`trace2.perfTarget` system or global config setting. For example ------------$ export GIT_TRACE2_PERF=~/log.perf$ git versiongit version 2.20.1.155.g426c96fcdb------------ or ------------$ git config --global trace2.perfTarget ~/log.perf$ git versiongit version 2.20.1.155.g426c96fcdb------------ yields ------------$ cat ~/log.perf12:28:42.620675 common-main.c:38                  | d0 | main                     | version      |     |           |           |            | 2.20.1.155.g426c96fcdb12:28:42.621001 common-main.c:39                  | d0 | main                     | start        |     |  0.001173 |           |            | git version12:28:42.621111 git.c:432                         | d0 | main                     | cmd_name     |     |           |           |            | version (version)12:28:42.621225 git.c:662                         | d0 | main                     | exit         |     |  0.001227 |           |            | code:012:28:42.621259 trace2/tr2_tgt_perf.c:211         | d0 | main                     | atexit       |     |  0.001265 |           |            | code:0------------ === The Event Format Target The event format target is a JSON-based format of event data suitablefor telemetry analysis.  This format is enabled with the `GIT_TRACE2_EVENT`environment variable or the `trace2.eventTarget` system or global configsetting. For example ------------$ export GIT_TRACE2_EVENT=~/log.event$ git versiongit version 2.20.1.155.g426c96fcdb------------ or ------------$ git config --global trace2.eventTarget ~/log.event$ git versiongit version 2.20.1.155.g426c96fcdb------------ yields ------------$ cat ~/log.event{"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"}{"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"]}{"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"}{"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}{"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}------------ === Enabling a Target To enable a target, set the corresponding environment variable orsystem or global config value to one of the following: include::../trace2-target-values.txt[] When trace files are written to a target directory, they will be named accordingto the last component of the SID (optionally followed by a counter to avoidfilename collisions). == Trace2 API All public Trace2 functions and macros are defined in `trace2.h` and`trace2.c`.  All public symbols are prefixed with `trace2_`. There are no public Trace2 data structures. The Trace2 code also defines a set of private functions and data typesin the `trace2/` directory.  These symbols are prefixed with `tr2_`and should only be used by functions in `trace2.c`. == Conventions for Public Functions and Macros The functions defined by the Trace2 API are declared and documentedin `trace2.h`.  It defines the API functions and wrapper macros forTrace2. Some functions have a `_fl()` suffix to indicate that they take `file`and `line-number` arguments. Some functions have a `_va_fl()` suffix to indicate that they alsotake a `va_list` argument. Some functions have a `_printf_fl()` suffix to indicate that they alsotake a varargs argument. There are CPP wrapper macros and ifdefs to hide most of these details.See `trace2.h` for more details.  The following discussion will onlydescribe the simplified forms. == Public API All Trace2 API functions send a message to all of the activeTrace2 Targets.  This section describes the set of availablemessages. It helps to divide these functions into groups for discussionpurposes. === Basic Command Messages These are concerned with the lifetime of the overall git process.e.g: `void trace2_initialize_clock()`, `void trace2_initialize()`,`int trace2_is_enabled()`, `void trace2_cmd_start(int argc, const char **argv)`. === Command Detail Messages These are concerned with describing the specific Git commandafter the command line, config, and environment are inspected.e.g: `void trace2_cmd_name(const char *name)`,`void trace2_cmd_mode(const char *mode)`. === Child Process Messages These are concerned with the various spawned child processes,including shell scripts, git commands, editors, pagers, and hooks. e.g: `void trace2_child_start(struct child_process *cmd)`. === Git Thread Messages These messages are concerned with Git thread usage. e.g: `void trace2_thread_start(const char *thread_name)`. === Region and Data Messages These are concerned with recording performance dataover regions or spans of code. e.g:`void trace2_region_enter(const char *category, const char *label, const struct repository *repo)`. Refer to trace2.h for details about all trace2 functions. == Trace2 Target Formats === NORMAL Format Events are written as lines of the form: ------------[<time> SP <filename>:<line> SP+] <event-name> [[SP] <event-message>] LF------------ `<event-name>`:: 	is the event name. `<event-message>`::	is a free-form printf message intended for human consumption.+Note that this may contain embedded LF or CRLF characters that arenot escaped, so the event may spill across multiple lines. If `GIT_TRACE2_BRIEF` or `trace2.normalBrief` is true, the `time`, `filename`,and `line` fields are omitted. This target is intended to be more of a summary (like GIT_TRACE) andless detailed than the other targets.  It ignores thread, region, anddata messages, for example. === PERF Format Events are written as lines of the form: ------------[<time> SP <filename>:<line> SP+    BAR SP] d<depth> SP    BAR SP <thread-name> SP+    BAR SP <event-name> SP+    BAR SP [r<repo-id>] SP+    BAR SP [<t_abs>] SP+    BAR SP [<t_rel>] SP+    BAR SP [<category>] SP+    BAR SP DOTS* <perf-event-message>    LF------------ `<depth>`::	is the git process depth.  This is the number of parent	git processes.  A top-level git command has depth value "d0".	A child of it has depth value "d1".  A second level child	has depth value "d2" and so on. `<thread-name>`::	is a unique name for the thread.  The primary thread	is called "main".  Other thread names are of the form "th%d:%s"	and include a unique number and the name of the thread-proc. `<event-name>`::	is the event name. `<repo-id>`::	when present, is a number indicating the repository	in use.  A `def_repo` event is emitted when a repository is	opened.  This defines the repo-id and associated worktree.	Subsequent repo-specific events will reference this repo-id.+Currently, this is always "r1" for the main repository.This field is in anticipation of in-proc submodules in the future. `<t_abs>`::	when present, is the absolute time in seconds since the	program started. `<t_rel>`::	when present, is time in seconds relative to the start of	the current region.  For a thread-exit event, it is the elapsed	time of the thread. `<category>`::	is present on region and data events and is used to	indicate a broad category, such as "index" or "status". `<perf-event-message>`::	is a free-form printf message intended for human consumption. ------------15:33:33.532712 wt-status.c:2310                  | d0 | main                     | region_enter | r1  |  0.126064 |           | status     | label:print15:33:33.532712 wt-status.c:2331                  | d0 | main                     | region_leave | r1  |  0.127568 |  0.001504 | status     | label:print------------ If `GIT_TRACE2_PERF_BRIEF` or `trace2.perfBrief` is true, the `time`, `file`,and `line` fields are omitted. ------------d0 | main                     | region_leave | r1  |  0.011717 |  0.009122 | index      | label:preload------------ The PERF target is intended for interactive performance analysisduring development and is quite noisy. === EVENT Format Each event is a JSON-object containing multiple key/value pairswritten as a single line and followed by a LF. ------------'{' <key> ':' <value> [',' <key> ':' <value>]* '}' LF------------ Some key/value pairs are common to all events and some areevent-specific. ==== Common Key/Value Pairs The following key/value pairs are common to all events: ------------{	"event":"version",	"sid":"20190408T191827.272759Z-H9b68c35f-P00003510",	"thread":"main",	"time":"2019-04-08T19:18:27.282761Z",	"file":"common-main.c",	"line":42,	...}------------ `"event":<event>`::	is the event name. `"sid":<sid>`::	is the session-id.  This is a unique string to identify the	process instance to allow all events emitted by a process to	be identified.  A session-id is used instead of a PID because	PIDs are recycled by the OS.  For child git processes, the	session-id is prepended with the session-id of the parent git	process to allow parent-child relationships to be identified	during post-processing. `"thread":<thread>`::	is the thread name. `"time":<time>`::	is the UTC time of the event. `"file":<filename>`::	is source file generating the event. `"line":<line-number>`::	is the integer source line number generating the event. `"repo":<repo-id>`::	when present, is the integer repo-id as described previously. If `GIT_TRACE2_EVENT_BRIEF` or `trace2.eventBrief` is true, the `file`and `line` fields are omitted from all events and the `time` field isonly present on the "start" and "atexit" events. ==== Event-Specific Key/Value Pairs `"version"`::	This event gives the version of the executable and the EVENT format. It	should always be the first event in a trace session. The EVENT format	version will be incremented if new event types are added, if existing	fields are removed, or if there are significant changes in	interpretation of existing events or fields. Smaller changes, such as	adding a new field to an existing event, will not require an increment	to the EVENT format version.+------------{	"event":"version",	...	"evt":"2",		       # EVENT format version	"exe":"2.20.1.155.g426c96fcdb" # git version}------------ `"discard"`::	This event is written to the git-trace2-discard sentinel file if there	are too many files in the target trace directory (see the	trace2.maxFiles config option).+------------{	"event":"discard",	...}------------ `"start"`::	This event contains the complete argv received by main().+------------{	"event":"start",	...	"t_abs":0.001227, # elapsed time in seconds	"argv":["git","version"]}------------ `"exit"`::	This event is emitted when git calls `exit()`.+------------{	"event":"exit",	...	"t_abs":0.001227, # elapsed time in seconds	"code":0	  # exit code}------------ `"atexit"`::	This event is emitted by the Trace2 `atexit` routine during	final shutdown.  It should be the last event emitted by the	process.+(The elapsed time reported here is greater than the time reported inthe "exit" event because it runs after all other atexit tasks havecompleted.)+------------{	"event":"atexit",	...	"t_abs":0.001227, # elapsed time in seconds	"code":0          # exit code}------------ `"signal"`::	This event is emitted when the program is terminated by a user	signal.  Depending on the platform, the signal event may	prevent the "atexit" event from being generated.+------------{	"event":"signal",	...	"t_abs":0.001227,  # elapsed time in seconds	"signo":13         # SIGTERM, SIGINT, etc.}------------ `"error"`::	This event is emitted when one of the `error()`, `die()`,	or `usage()` functions are called.+------------{	"event":"error",	...	"msg":"invalid option: --cahced", # formatted error message	"fmt":"invalid option: %s"	  # error format string}------------+The error event may be emitted more than once.  The format stringallows post-processors to group errors by type without worryingabout specific error arguments. `"cmd_path"`::	This event contains the discovered full path of the git	executable (on platforms that are configured to resolve it).+------------{	"event":"cmd_path",	...	"path":"C:/work/gfw/git.exe"}------------ `"cmd_name"`::	This event contains the command name for this git process	and the hierarchy of commands from parent git processes.+------------

Lines 1–500 of 1171.