@@ -148,20 +148,18 @@ filename collisions).
148
148
149
149
== Trace2 API
150
150
151
- All public Trace2 functions and macros are defined in `trace2.h` and
152
- `trace2.c`. All public symbols are prefixed with `trace2_`.
151
+ The Trace2 public API is defined and documented in `trace2.h`; refer to it for
152
+ more information. All public functions and macros are prefixed
153
+ with `trace2_` and are implemented in `trace2.c`.
153
154
154
155
There are no public Trace2 data structures.
155
156
156
157
The Trace2 code also defines a set of private functions and data types
157
158
in the `trace2/` directory. These symbols are prefixed with `tr2_`
158
- and should only be used by functions in `trace2.c`.
159
+ and should only be used by functions in `trace2.c` (or other private
160
+ source files in `trace2/`).
159
161
160
- == Conventions for Public Functions and Macros
161
-
162
- The functions defined by the Trace2 API are declared and documented
163
- in `trace2.h`. It defines the API functions and wrapper macros for
164
- Trace2.
162
+ === Conventions for Public Functions and Macros
165
163
166
164
Some functions have a `_fl()` suffix to indicate that they take `file`
167
165
and `line-number` arguments.
@@ -172,52 +170,7 @@ take a `va_list` argument.
172
170
Some functions have a `_printf_fl()` suffix to indicate that they also
173
171
take a `printf()` style format with a variable number of arguments.
174
172
175
- There are CPP wrapper macros and `#ifdef`s to hide most of these details.
176
- See `trace2.h` for more details. The following discussion will only
177
- describe the simplified forms.
178
-
179
- == Public API
180
-
181
- All Trace2 API functions send a message to all of the active
182
- Trace2 Targets. This section describes the set of available
183
- messages.
184
-
185
- It helps to divide these functions into groups for discussion
186
- purposes.
187
-
188
- === Basic Command Messages
189
-
190
- These are concerned with the lifetime of the overall git process.
191
- e.g: `void trace2_initialize_clock()`, `void trace2_initialize()`,
192
- `int trace2_is_enabled()`, `void trace2_cmd_start(int argc, const char **argv)`.
193
-
194
- === Command Detail Messages
195
-
196
- These are concerned with describing the specific Git command
197
- after the command line, config, and environment are inspected.
198
- e.g: `void trace2_cmd_name(const char *name)`,
199
- `void trace2_cmd_mode(const char *mode)`.
200
-
201
- === Child Process Messages
202
-
203
- These are concerned with the various spawned child processes,
204
- including shell scripts, git commands, editors, pagers, and hooks.
205
-
206
- e.g: `void trace2_child_start(struct child_process *cmd)`.
207
-
208
- === Git Thread Messages
209
-
210
- These messages are concerned with Git thread usage.
211
-
212
- e.g: `void trace2_thread_start(const char *thread_name)`.
213
-
214
- === Region and Data Messages
215
-
216
- These are concerned with recording performance data
217
- over regions or spans of code. e.g:
218
- `void trace2_region_enter(const char *category, const char *label, const struct repository *repo)`.
219
-
220
- Refer to trace2.h for details about all trace2 functions.
173
+ CPP wrapper macros are defined to hide most of these details.
221
174
222
175
== Trace2 Target Formats
223
176
@@ -685,8 +638,8 @@ The "exec_id" field is a command-unique id and is only useful if the
685
638
686
639
`"thread_start"`::
687
640
This event is generated when a thread is started. It is
688
- generated from *within* the new thread's thread-proc (for TLS
689
- reasons ).
641
+ generated from *within* the new thread's thread-proc (because
642
+ it needs to access data in the thread's thread-local storage ).
690
643
+
691
644
------------
692
645
{
@@ -698,7 +651,7 @@ The "exec_id" field is a command-unique id and is only useful if the
698
651
699
652
`"thread_exit"`::
700
653
This event is generated when a thread exits. It is generated
701
- from *within* the thread's thread-proc (for TLS reasons) .
654
+ from *within* the thread's thread-proc.
702
655
+
703
656
------------
704
657
{
@@ -816,6 +769,73 @@ The "value" field may be an integer or a string.
816
769
}
817
770
------------
818
771
772
+ `"th_timer"`::
773
+ This event logs the amount of time that a stopwatch timer was
774
+ running in the thread. This event is generated when a thread
775
+ exits for timers that requested per-thread events.
776
+ +
777
+ ------------
778
+ {
779
+ "event":"th_timer",
780
+ ...
781
+ "category":"my_category",
782
+ "name":"my_timer",
783
+ "intervals":5, # number of time it was started/stopped
784
+ "t_total":0.052741, # total time in seconds it was running
785
+ "t_min":0.010061, # shortest interval
786
+ "t_max":0.011648 # longest interval
787
+ }
788
+ ------------
789
+
790
+ `"timer"`::
791
+ This event logs the amount of time that a stopwatch timer was
792
+ running aggregated across all threads. This event is generated
793
+ when the process exits.
794
+ +
795
+ ------------
796
+ {
797
+ "event":"timer",
798
+ ...
799
+ "category":"my_category",
800
+ "name":"my_timer",
801
+ "intervals":5, # number of time it was started/stopped
802
+ "t_total":0.052741, # total time in seconds it was running
803
+ "t_min":0.010061, # shortest interval
804
+ "t_max":0.011648 # longest interval
805
+ }
806
+ ------------
807
+
808
+ `"th_counter"`::
809
+ This event logs the value of a counter variable in a thread.
810
+ This event is generated when a thread exits for counters that
811
+ requested per-thread events.
812
+ +
813
+ ------------
814
+ {
815
+ "event":"th_counter",
816
+ ...
817
+ "category":"my_category",
818
+ "name":"my_counter",
819
+ "count":23
820
+ }
821
+ ------------
822
+
823
+ `"counter"`::
824
+ This event logs the value of a counter variable across all threads.
825
+ This event is generated when the process exits. The total value
826
+ reported here is the sum across all threads.
827
+ +
828
+ ------------
829
+ {
830
+ "event":"counter",
831
+ ...
832
+ "category":"my_category",
833
+ "name":"my_counter",
834
+ "count":23
835
+ }
836
+ ------------
837
+
838
+
819
839
== Example Trace2 API Usage
820
840
821
841
Here is a hypothetical usage of the Trace2 API showing the intended
@@ -1206,7 +1226,7 @@ worked on 508 items at offset 2032. Thread "th04" worked on 508 items
1206
1226
at offset 508.
1207
1227
+
1208
1228
This example also shows that thread names are assigned in a racy manner
1209
- as each thread starts and allocates TLS storage .
1229
+ as each thread starts.
1210
1230
1211
1231
Config (def param) Events::
1212
1232
@@ -1247,6 +1267,60 @@ d0 | main | data | r0 | 0.002126 | 0.002126 | fsy
1247
1267
d0 | main | exit | | 0.000470 | | | code:0
1248
1268
d0 | main | atexit | | 0.000477 | | | code:0
1249
1269
----------------
1270
+
1271
+ Stopwatch Timer Events::
1272
+
1273
+ Measure the time spent in a function call or span of code
1274
+ that might be called from many places within the code
1275
+ throughout the life of the process.
1276
+ +
1277
+ ----------------
1278
+ static void expensive_function(void)
1279
+ {
1280
+ trace2_timer_start(TRACE2_TIMER_ID_TEST1);
1281
+ ...
1282
+ sleep_millisec(1000); // Do something expensive
1283
+ ...
1284
+ trace2_timer_stop(TRACE2_TIMER_ID_TEST1);
1285
+ }
1286
+
1287
+ static int ut_100timer(int argc, const char **argv)
1288
+ {
1289
+ ...
1290
+
1291
+ expensive_function();
1292
+
1293
+ // Do something else 1...
1294
+
1295
+ expensive_function();
1296
+
1297
+ // Do something else 2...
1298
+
1299
+ expensive_function();
1300
+
1301
+ return 0;
1302
+ }
1303
+ ----------------
1304
+ +
1305
+ In this example, we measure the total time spent in
1306
+ `expensive_function()` regardless of when it is called
1307
+ in the overall flow of the program.
1308
+ +
1309
+ ----------------
1310
+ $ export GIT_TRACE2_PERF_BRIEF=1
1311
+ $ export GIT_TRACE2_PERF=~/log.perf
1312
+ $ t/helper/test-tool trace2 100timer 3 1000
1313
+ ...
1314
+ $ cat ~/log.perf
1315
+ d0 | main | version | | | | | ...
1316
+ d0 | main | start | | 0.001453 | | | t/helper/test-tool trace2 100timer 3 1000
1317
+ d0 | main | cmd_name | | | | | trace2 (trace2)
1318
+ d0 | main | exit | | 3.003667 | | | code:0
1319
+ d0 | main | timer | | | | test | name:test1 intervals:3 total:3.001686 min:1.000254 max:1.000929
1320
+ d0 | main | atexit | | 3.003796 | | | code:0
1321
+ ----------------
1322
+
1323
+
1250
1324
== Future Work
1251
1325
1252
1326
=== Relationship to the Existing Trace Api (api-trace.txt)
0 commit comments