Skip to content

Commit e5be3c6

Browse files
committed
Merge branch 'jh/trace2-timers-and-counters'
Two new facilities, "timer" and "counter", are introduced to the trace2 API. * jh/trace2-timers-and-counters: trace2: add global counter mechanism trace2: add stopwatch timers trace2: convert ctx.thread_name from strbuf to pointer trace2: improve thread-name documentation in the thread-context trace2: rename the thread_name argument to trace2_thread_start api-trace2.txt: elminate section describing the public trace2 API tr2tls: clarify TLS terminology trace2: use size_t alloc,nr_open_regions in tr2tls_thread_ctx
2 parents c112d8d + 8107162 commit e5be3c6

File tree

17 files changed

+1361
-102
lines changed

17 files changed

+1361
-102
lines changed

Documentation/technical/api-trace2.txt

Lines changed: 132 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -148,20 +148,18 @@ filename collisions).
148148

149149
== Trace2 API
150150

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`.
153154

154155
There are no public Trace2 data structures.
155156

156157
The Trace2 code also defines a set of private functions and data types
157158
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/`).
159161

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
165163

166164
Some functions have a `_fl()` suffix to indicate that they take `file`
167165
and `line-number` arguments.
@@ -172,52 +170,7 @@ take a `va_list` argument.
172170
Some functions have a `_printf_fl()` suffix to indicate that they also
173171
take a `printf()` style format with a variable number of arguments.
174172

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.
221174

222175
== Trace2 Target Formats
223176

@@ -685,8 +638,8 @@ The "exec_id" field is a command-unique id and is only useful if the
685638

686639
`"thread_start"`::
687640
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).
690643
+
691644
------------
692645
{
@@ -698,7 +651,7 @@ The "exec_id" field is a command-unique id and is only useful if the
698651

699652
`"thread_exit"`::
700653
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.
702655
+
703656
------------
704657
{
@@ -816,6 +769,73 @@ The "value" field may be an integer or a string.
816769
}
817770
------------
818771

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+
819839
== Example Trace2 API Usage
820840

821841
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
12061226
at offset 508.
12071227
+
12081228
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.
12101230

12111231
Config (def param) Events::
12121232

@@ -1247,6 +1267,60 @@ d0 | main | data | r0 | 0.002126 | 0.002126 | fsy
12471267
d0 | main | exit | | 0.000470 | | | code:0
12481268
d0 | main | atexit | | 0.000477 | | | code:0
12491269
----------------
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+
12501324
== Future Work
12511325

12521326
=== Relationship to the Existing Trace Api (api-trace.txt)

Makefile

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1095,6 +1095,7 @@ LIB_OBJS += trace.o
10951095
LIB_OBJS += trace2.o
10961096
LIB_OBJS += trace2/tr2_cfg.o
10971097
LIB_OBJS += trace2/tr2_cmd_name.o
1098+
LIB_OBJS += trace2/tr2_ctr.o
10981099
LIB_OBJS += trace2/tr2_dst.o
10991100
LIB_OBJS += trace2/tr2_sid.o
11001101
LIB_OBJS += trace2/tr2_sysenv.o
@@ -1103,6 +1104,7 @@ LIB_OBJS += trace2/tr2_tgt_event.o
11031104
LIB_OBJS += trace2/tr2_tgt_normal.o
11041105
LIB_OBJS += trace2/tr2_tgt_perf.o
11051106
LIB_OBJS += trace2/tr2_tls.o
1107+
LIB_OBJS += trace2/tr2_tmr.o
11061108
LIB_OBJS += trailer.o
11071109
LIB_OBJS += transport-helper.o
11081110
LIB_OBJS += transport.o

0 commit comments

Comments
 (0)