Skip to content

Commit bce9db6

Browse files
jeffhostetlergitster
authored andcommitted
trace2: use system/global config for default trace2 settings
Teach git to read the system and global config files for default Trace2 settings. This allows system-wide Trace2 settings to be installed and inherited to make it easier to manage a collection of systems. The original GIT_TR2* environment variables are loaded afterwards and can be used to override the system settings. Only the system and global config files are used. Repo and worktree local config files are ignored. Likewise, the "-c" command line arguments are also ignored. These limits are for performance reasons. (1) For users not using Trace2, there should be minimal overhead to detect that Trace2 is not enabled. In particular, Trace2 should not allocate lots of otherwise unused data strucutres. (2) For accurate performance measurements, Trace2 should be initialized as early in the git process as possible, and before most of the normal git process initialization (which involves discovering the .git directory and reading a hierarchy of config files). Signed-off-by: Jeff Hostetler <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 800a7f9 commit bce9db6

14 files changed

+340
-76
lines changed

Makefile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1005,6 +1005,7 @@ LIB_OBJS += trace2/tr2_cfg.o
10051005
LIB_OBJS += trace2/tr2_cmd_name.o
10061006
LIB_OBJS += trace2/tr2_dst.o
10071007
LIB_OBJS += trace2/tr2_sid.o
1008+
LIB_OBJS += trace2/tr2_sysenv.o
10081009
LIB_OBJS += trace2/tr2_tbuf.o
10091010
LIB_OBJS += trace2/tr2_tgt_event.o
10101011
LIB_OBJS += trace2/tr2_tgt_normal.o

t/t0210-trace2-normal.sh

Lines changed: 44 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,11 @@
33
test_description='test trace2 facility (normal target)'
44
. ./test-lib.sh
55

6+
# Turn off any inherited trace2 settings for this test.
7+
sane_unset GIT_TR2 GIT_TR2_PERF GIT_TR2_EVENT
8+
sane_unset GIT_TR2_BRIEF
9+
sane_unset GIT_TR2_CONFIG_PARAMS
10+
611
# Add t/helper directory to PATH so that we can use a relative
712
# path to run nested instances of test-tool.exe (see 004child).
813
# This helps with HEREDOC comparisons later.
@@ -15,11 +20,6 @@ PATH="$TTDIR:$PATH" && export PATH
1520
# Warning: So you may see extra lines in artifact files when
1621
# Warning: interactively debugging.
1722

18-
# Turn off any inherited trace2 settings for this test.
19-
unset GIT_TR2 GIT_TR2_PERF GIT_TR2_EVENT
20-
unset GIT_TR2_BRIEF
21-
unset GIT_TR2_CONFIG_PARAMS
22-
2323
V=$(git version | sed -e 's/^git version //') && export V
2424

2525
# There are multiple trace2 targets: normal, perf, and event.
@@ -132,4 +132,43 @@ test_expect_success 'normal stream, error event' '
132132
test_cmp expect actual
133133
'
134134

135+
sane_unset GIT_TR2_BRIEF
136+
137+
# Now test without environment variables and get all Trace2 settings
138+
# from the global config.
139+
140+
test_expect_success 'using global config, normal stream, return code 0' '
141+
test_when_finished "rm trace.normal actual expect" &&
142+
test_config_global trace2.normalBrief 1 &&
143+
test_config_global trace2.normalTarget "$(pwd)/trace.normal" &&
144+
test-tool trace2 001return 0 &&
145+
perl "$TEST_DIRECTORY/t0210/scrub_normal.perl" <trace.normal >actual &&
146+
cat >expect <<-EOF &&
147+
version $V
148+
start _EXE_ trace2 001return 0
149+
cmd_name trace2 (trace2)
150+
exit elapsed:_TIME_ code:0
151+
atexit elapsed:_TIME_ code:0
152+
EOF
153+
test_cmp expect actual
154+
'
155+
156+
test_expect_success 'using global config with include' '
157+
test_when_finished "rm trace.normal actual expect real.gitconfig" &&
158+
test_config_global trace2.normalBrief 1 &&
159+
test_config_global trace2.normalTarget "$(pwd)/trace.normal" &&
160+
mv "$(pwd)/.gitconfig" "$(pwd)/real.gitconfig" &&
161+
test_config_global include.path "$(pwd)/real.gitconfig" &&
162+
test-tool trace2 001return 0 &&
163+
perl "$TEST_DIRECTORY/t0210/scrub_normal.perl" <trace.normal >actual &&
164+
cat >expect <<-EOF &&
165+
version $V
166+
start _EXE_ trace2 001return 0
167+
cmd_name trace2 (trace2)
168+
exit elapsed:_TIME_ code:0
169+
atexit elapsed:_TIME_ code:0
170+
EOF
171+
test_cmp expect actual
172+
'
173+
135174
test_done

t/t0211-trace2-perf.sh

Lines changed: 26 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,11 @@
33
test_description='test trace2 facility (perf target)'
44
. ./test-lib.sh
55

6+
# Turn off any inherited trace2 settings for this test.
7+
sane_unset GIT_TR2 GIT_TR2_PERF GIT_TR2_EVENT
8+
sane_unset GIT_TR2_PERF_BRIEF
9+
sane_unset GIT_TR2_CONFIG_PARAMS
10+
611
# Add t/helper directory to PATH so that we can use a relative
712
# path to run nested instances of test-tool.exe (see 004child).
813
# This helps with HEREDOC comparisons later.
@@ -15,11 +20,6 @@ PATH="$TTDIR:$PATH" && export PATH
1520
# Warning: So you may see extra lines in artifact files when
1621
# Warning: interactively debugging.
1722

18-
# Turn off any inherited trace2 settings for this test.
19-
unset GIT_TR2 GIT_TR2_PERF GIT_TR2_EVENT
20-
unset GIT_TR2_PERF_BRIEF
21-
unset GIT_TR2_CONFIG_PARAMS
22-
2323
V=$(git version | sed -e 's/^git version //') && export V
2424

2525
# There are multiple trace2 targets: normal, perf, and event.
@@ -150,4 +150,25 @@ test_expect_success 'perf stream, child processes' '
150150
test_cmp expect actual
151151
'
152152

153+
sane_unset GIT_TR2_PERF_BRIEF
154+
155+
# Now test without environment variables and get all Trace2 settings
156+
# from the global config.
157+
158+
test_expect_success 'using global config, perf stream, return code 0' '
159+
test_when_finished "rm trace.perf actual expect" &&
160+
test_config_global trace2.perfBrief 1 &&
161+
test_config_global trace2.perfTarget "$(pwd)/trace.perf" &&
162+
test-tool trace2 001return 0 &&
163+
perl "$TEST_DIRECTORY/t0211/scrub_perf.perl" <trace.perf >actual &&
164+
cat >expect <<-EOF &&
165+
d0|main|version|||||$V
166+
d0|main|start||_T_ABS_|||_EXE_ trace2 001return 0
167+
d0|main|cmd_name|||||trace2 (trace2)
168+
d0|main|exit||_T_ABS_|||code:0
169+
d0|main|atexit||_T_ABS_|||code:0
170+
EOF
171+
test_cmp expect actual
172+
'
173+
153174
test_done

t/t0212-trace2-event.sh

Lines changed: 37 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,11 @@
33
test_description='test trace2 facility'
44
. ./test-lib.sh
55

6+
# Turn off any inherited trace2 settings for this test.
7+
sane_unset GIT_TR2 GIT_TR2_PERF GIT_TR2_EVENT
8+
sane_unset GIT_TR2_BARE
9+
sane_unset GIT_TR2_CONFIG_PARAMS
10+
611
perl -MJSON::PP -e 0 >/dev/null 2>&1 && test_set_prereq JSON_PP
712

813
# Add t/helper directory to PATH so that we can use a relative
@@ -17,11 +22,6 @@ PATH="$TTDIR:$PATH" && export PATH
1722
# Warning: So you may see extra lines in artifact files when
1823
# Warning: interactively debugging.
1924

20-
# Turn off any inherited trace2 settings for this test.
21-
unset GIT_TR2 GIT_TR2_PERF GIT_TR2_EVENT
22-
unset GIT_TR2_BARE
23-
unset GIT_TR2_CONFIG_PARAMS
24-
2525
V=$(git version | sed -e 's/^git version //') && export V
2626

2727
# There are multiple trace2 targets: normal, perf, and event.
@@ -233,4 +233,36 @@ test_expect_success JSON_PP 'basic trace2_data' '
233233
test_cmp expect actual
234234
'
235235

236+
# Now test without environment variables and get all Trace2 settings
237+
# from the global config.
238+
239+
test_expect_success JSON_PP 'using global config, event stream, error event' '
240+
test_when_finished "rm trace.event actual expect" &&
241+
test_config_global trace2.eventTarget "$(pwd)/trace.event" &&
242+
test-tool trace2 003error "hello world" "this is a test" &&
243+
perl "$TEST_DIRECTORY/t0212/parse_events.perl" <trace.event >actual &&
244+
sed -e "s/^|//" >expect <<-EOF &&
245+
|VAR1 = {
246+
| "_SID0_":{
247+
| "argv":[
248+
| "_EXE_",
249+
| "trace2",
250+
| "003error",
251+
| "hello world",
252+
| "this is a test"
253+
| ],
254+
| "errors":[
255+
| "%s",
256+
| "%s"
257+
| ],
258+
| "exit_code":0,
259+
| "hierarchy":"trace2",
260+
| "name":"trace2",
261+
| "version":"$V"
262+
| }
263+
|};
264+
EOF
265+
test_cmp expect actual
266+
'
267+
236268
test_done

trace2.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
#include "trace2/tr2_cmd_name.h"
1111
#include "trace2/tr2_dst.h"
1212
#include "trace2/tr2_sid.h"
13+
#include "trace2/tr2_sysenv.h"
1314
#include "trace2/tr2_tgt.h"
1415
#include "trace2/tr2_tls.h"
1516

@@ -120,6 +121,7 @@ static void tr2main_atexit_handler(void)
120121
tr2_sid_release();
121122
tr2_cmd_name_release();
122123
tr2_cfg_free_patterns();
124+
tr2_sysenv_release();
123125

124126
trace2_enabled = 0;
125127
}
@@ -155,6 +157,8 @@ void trace2_initialize_fl(const char *file, int line)
155157
if (trace2_enabled)
156158
return;
157159

160+
tr2_sysenv_load();
161+
158162
if (!tr2_tgt_want_builtins())
159163
return;
160164
trace2_enabled = 1;

trace2.h

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,8 @@ void trace2_initialize_clock(void);
3838

3939
/*
4040
* Initialize TRACE2 tracing facility if any of the builtin TRACE2
41-
* targets are enabled in the environment. Emits a 'version' event.
41+
* targets are enabled in the system config or the environment.
42+
* Emits a 'version' event.
4243
*
4344
* Cleanup/Termination is handled automatically by a registered
4445
* atexit() routine.
@@ -125,10 +126,11 @@ void trace2_cmd_alias_fl(const char *file, int line, const char *alias,
125126
* Emit one or more 'def_param' events for "interesting" configuration
126127
* settings.
127128
*
128-
* The environment variable "GIT_TR2_CONFIG_PARAMS" can be set to a
129-
* list of patterns considered important. For example:
130-
*
131-
* GIT_TR2_CONFIG_PARAMS="core.*,remote.*.url"
129+
* Use the TR2_SYSENV_CFG_PARAM setting to register a comma-separated
130+
* list of patterns configured important. For example:
131+
* git config --system trace2.configParams 'core.*,remote.*.url'
132+
* or:
133+
* GIT_TR2_CONFIG_PARAMS=core.*,remote.*.url"
132134
*
133135
* Note: this routine does a read-only iteration on the config data
134136
* (using read_early_config()), so it must not be called until enough

trace2/tr2_cfg.c

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
#include "cache.h"
22
#include "config.h"
3-
#include "tr2_cfg.h"
4-
5-
#define TR2_ENVVAR_CFG_PARAM "GIT_TR2_CONFIG_PARAMS"
3+
#include "trace2/tr2_cfg.h"
4+
#include "trace2/tr2_sysenv.h"
65

76
static struct strbuf **tr2_cfg_patterns;
87
static int tr2_cfg_count_patterns;
@@ -21,7 +20,7 @@ static int tr2_cfg_load_patterns(void)
2120
return tr2_cfg_count_patterns;
2221
tr2_cfg_loaded = 1;
2322

24-
envvar = getenv(TR2_ENVVAR_CFG_PARAM);
23+
envvar = tr2_sysenv_get(TR2_SYSENV_CFG_PARAM);
2524
if (!envvar || !*envvar)
2625
return tr2_cfg_count_patterns;
2726

trace2/tr2_dst.c

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,20 @@
11
#include "cache.h"
22
#include "trace2/tr2_dst.h"
3+
#include "trace2/tr2_sysenv.h"
34

45
/*
56
* If a Trace2 target cannot be opened for writing, we should issue a
67
* warning to stderr, but this is very annoying if the target is a pipe
78
* or socket and beyond the user's control -- especially since every
89
* git command (and sub-command) will print the message. So we silently
910
* eat these warnings and just discard the trace data.
10-
*
11-
* Enable the following environment variable to see these warnings.
1211
*/
13-
#define TR2_ENVVAR_DST_DEBUG "GIT_TR2_DST_DEBUG"
14-
1512
static int tr2_dst_want_warning(void)
1613
{
1714
static int tr2env_dst_debug = -1;
1815

1916
if (tr2env_dst_debug == -1) {
20-
const char *env_value = getenv(TR2_ENVVAR_DST_DEBUG);
17+
const char *env_value = tr2_sysenv_get(TR2_SYSENV_DST_DEBUG);
2118
if (!env_value || !*env_value)
2219
tr2env_dst_debug = 0;
2320
else
@@ -42,7 +39,9 @@ static int tr2_dst_try_path(struct tr2_dst *dst, const char *tgt_value)
4239
if (fd == -1) {
4340
if (tr2_dst_want_warning())
4441
warning("trace2: could not open '%s' for '%s' tracing: %s",
45-
tgt_value, dst->env_var_name, strerror(errno));
42+
tgt_value,
43+
tr2_sysenv_display_name(dst->sysenv_var),
44+
strerror(errno));
4645

4746
tr2_dst_trace_disable(dst);
4847
return 0;
@@ -116,7 +115,8 @@ static int tr2_dst_try_unix_domain_socket(struct tr2_dst *dst,
116115
if (!path || !*path) {
117116
if (tr2_dst_want_warning())
118117
warning("trace2: invalid AF_UNIX value '%s' for '%s' tracing",
119-
tgt_value, dst->env_var_name);
118+
tgt_value,
119+
tr2_sysenv_display_name(dst->sysenv_var));
120120

121121
tr2_dst_trace_disable(dst);
122122
return 0;
@@ -126,7 +126,7 @@ static int tr2_dst_try_unix_domain_socket(struct tr2_dst *dst,
126126
strlen(path) >= sizeof(((struct sockaddr_un *)0)->sun_path)) {
127127
if (tr2_dst_want_warning())
128128
warning("trace2: invalid AF_UNIX path '%s' for '%s' tracing",
129-
path, dst->env_var_name);
129+
path, tr2_sysenv_display_name(dst->sysenv_var));
130130

131131
tr2_dst_trace_disable(dst);
132132
return 0;
@@ -148,7 +148,8 @@ static int tr2_dst_try_unix_domain_socket(struct tr2_dst *dst,
148148
error:
149149
if (tr2_dst_want_warning())
150150
warning("trace2: could not connect to socket '%s' for '%s' tracing: %s",
151-
path, dst->env_var_name, strerror(e));
151+
path, tr2_sysenv_display_name(dst->sysenv_var),
152+
strerror(e));
152153

153154
tr2_dst_trace_disable(dst);
154155
return 0;
@@ -168,7 +169,7 @@ static void tr2_dst_malformed_warning(struct tr2_dst *dst,
168169
struct strbuf buf = STRBUF_INIT;
169170

170171
strbuf_addf(&buf, "trace2: unknown value for '%s': '%s'",
171-
dst->env_var_name, tgt_value);
172+
tr2_sysenv_display_name(dst->sysenv_var), tgt_value);
172173
warning("%s", buf.buf);
173174

174175
strbuf_release(&buf);
@@ -184,7 +185,7 @@ int tr2_dst_get_trace_fd(struct tr2_dst *dst)
184185

185186
dst->initialized = 1;
186187

187-
tgt_value = getenv(dst->env_var_name);
188+
tgt_value = tr2_sysenv_get(dst->sysenv_var);
188189

189190
if (!tgt_value || !strcmp(tgt_value, "") || !strcmp(tgt_value, "0") ||
190191
!strcasecmp(tgt_value, "false")) {
@@ -246,7 +247,8 @@ void tr2_dst_write_line(struct tr2_dst *dst, struct strbuf *buf_line)
246247
return;
247248

248249
if (tr2_dst_want_warning())
249-
warning("unable to write trace to '%s': %s", dst->env_var_name,
250+
warning("unable to write trace to '%s': %s",
251+
tr2_sysenv_display_name(dst->sysenv_var),
250252
strerror(errno));
251253
tr2_dst_trace_disable(dst);
252254
}

trace2/tr2_dst.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,10 @@
22
#define TR2_DST_H
33

44
struct strbuf;
5+
#include "trace2/tr2_sysenv.h"
56

67
struct tr2_dst {
7-
const char *const env_var_name;
8+
enum tr2_sysenv_variable sysenv_var;
89
int fd;
910
unsigned int initialized : 1;
1011
unsigned int need_close : 1;

0 commit comments

Comments
 (0)