Skip to content

Commit 83e57b0

Browse files
steadmongitster
authored andcommitted
trace2: discard new traces if target directory has too many files
trace2 can write files into a target directory. With heavy usage, this directory can fill up with files, causing difficulty for trace-processing systems. This patch adds a config option (trace2.maxFiles) to set a maximum number of files that trace2 will write to a target directory. The following behavior is enabled when the maxFiles is set to a positive integer: When trace2 would write a file to a target directory, first check whether or not the traces should be discarded. Traces should be discarded if: * there is a sentinel file declaring that there are too many files * OR, the number of files exceeds trace2.maxFiles. In the latter case, we create a sentinel file named git-trace2-discard to speed up future checks. The assumption is that a separate trace-processing system is dealing with the generated traces; once it processes and removes the sentinel file, it should be safe to generate new trace files again. The default value for trace2.maxFiles is zero, which disables the file count check. The config can also be overridden with a new environment variable: GIT_TRACE2_MAX_FILES. Signed-off-by: Josh Steadmon <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 2254101 commit 83e57b0

File tree

5 files changed

+112
-0
lines changed

5 files changed

+112
-0
lines changed

Documentation/config/trace2.txt

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,3 +54,9 @@ trace2.destinationDebug::
5454
By default, these errors are suppressed and tracing is
5555
silently disabled. May be overridden by the
5656
`GIT_TRACE2_DST_DEBUG` environment variable.
57+
58+
trace2.maxFiles::
59+
Integer. When writing trace files to a target directory, do not
60+
write additional traces if we would exceed this many files. Instead,
61+
write a sentinel file that will block further tracing to this
62+
directory. Defaults to 0, which disables this check.

t/t0212-trace2-event.sh

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -265,4 +265,21 @@ test_expect_success JSON_PP 'using global config, event stream, error event' '
265265
test_cmp expect actual
266266
'
267267

268+
test_expect_success 'discard traces when there are too many files' '
269+
mkdir trace_target_dir &&
270+
test_when_finished "rm -r trace_target_dir" &&
271+
(
272+
GIT_TRACE2_MAX_FILES=5 &&
273+
export GIT_TRACE2_MAX_FILES &&
274+
cd trace_target_dir &&
275+
test_seq $GIT_TRACE2_MAX_FILES >../expected_filenames.txt &&
276+
xargs touch <../expected_filenames.txt &&
277+
cd .. &&
278+
GIT_TRACE2_EVENT="$(pwd)/trace_target_dir" test-tool trace2 001return 0
279+
) &&
280+
echo git-trace2-discard >>expected_filenames.txt &&
281+
ls trace_target_dir >ls_output.txt &&
282+
test_cmp expected_filenames.txt ls_output.txt
283+
'
284+
268285
test_done

trace2/tr2_dst.c

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,19 @@
88
*/
99
#define MAX_AUTO_ATTEMPTS 10
1010

11+
/*
12+
* Sentinel file used to detect when we should discard new traces to avoid
13+
* writing too many trace files to a directory.
14+
*/
15+
#define DISCARD_SENTINEL_NAME "git-trace2-discard"
16+
17+
/*
18+
* When set to zero, disables directory file count checks. Otherwise, controls
19+
* how many files we can write to a directory before entering discard mode.
20+
* This can be overridden via the TR2_SYSENV_MAX_FILES setting.
21+
*/
22+
static int tr2env_max_files = 0;
23+
1124
static int tr2_dst_want_warning(void)
1225
{
1326
static int tr2env_dst_debug = -1;
@@ -32,6 +45,67 @@ void tr2_dst_trace_disable(struct tr2_dst *dst)
3245
dst->need_close = 0;
3346
}
3447

48+
/*
49+
* Check to make sure we're not overloading the target directory with too many
50+
* files. First get the threshold (if present) from the config or envvar. If
51+
* it's zero or unset, disable this check. Next check for the presence of a
52+
* sentinel file, then check file count. If we are overloaded, create the
53+
* sentinel file if it doesn't already exist.
54+
*
55+
* We expect that some trace processing system is gradually collecting files
56+
* from the target directory; after it removes the sentinel file we'll start
57+
* writing traces again.
58+
*/
59+
static int tr2_dst_too_many_files(const char *tgt_prefix)
60+
{
61+
int file_count = 0, max_files = 0, ret = 0;
62+
const char *max_files_var;
63+
DIR *dirp;
64+
struct strbuf path = STRBUF_INIT, sentinel_path = STRBUF_INIT;
65+
struct stat statbuf;
66+
67+
/* Get the config or envvar and decide if we should continue this check */
68+
max_files_var = tr2_sysenv_get(TR2_SYSENV_MAX_FILES);
69+
if (max_files_var && *max_files_var && ((max_files = atoi(max_files_var)) >= 0))
70+
tr2env_max_files = max_files;
71+
72+
if (!tr2env_max_files) {
73+
ret = 0;
74+
goto cleanup;
75+
}
76+
77+
strbuf_addstr(&path, tgt_prefix);
78+
if (!is_dir_sep(path.buf[path.len - 1])) {
79+
strbuf_addch(&path, '/');
80+
}
81+
82+
/* check sentinel */
83+
strbuf_addbuf(&sentinel_path, &path);
84+
strbuf_addstr(&sentinel_path, DISCARD_SENTINEL_NAME);
85+
if (!stat(sentinel_path.buf, &statbuf)) {
86+
ret = 1;
87+
goto cleanup;
88+
}
89+
90+
/* check file count */
91+
dirp = opendir(path.buf);
92+
while (file_count < tr2env_max_files && dirp && readdir(dirp))
93+
file_count++;
94+
if (dirp)
95+
closedir(dirp);
96+
97+
if (file_count >= tr2env_max_files) {
98+
creat(sentinel_path.buf, 0666);
99+
ret = 1;
100+
goto cleanup;
101+
}
102+
103+
cleanup:
104+
strbuf_release(&path);
105+
strbuf_release(&sentinel_path);
106+
return ret;
107+
}
108+
35109
static int tr2_dst_try_auto_path(struct tr2_dst *dst, const char *tgt_prefix)
36110
{
37111
int fd;
@@ -50,6 +124,16 @@ static int tr2_dst_try_auto_path(struct tr2_dst *dst, const char *tgt_prefix)
50124
strbuf_addstr(&path, sid);
51125
base_path_len = path.len;
52126

127+
if (tr2_dst_too_many_files(tgt_prefix)) {
128+
strbuf_release(&path);
129+
if (tr2_dst_want_warning())
130+
warning("trace2: not opening %s trace file due to too "
131+
"many files in target directory %s",
132+
tr2_sysenv_display_name(dst->sysenv_var),
133+
tgt_prefix);
134+
return 0;
135+
}
136+
53137
for (attempt_count = 0; attempt_count < MAX_AUTO_ATTEMPTS; attempt_count++) {
54138
if (attempt_count > 0) {
55139
strbuf_setlen(&path, base_path_len);

trace2/tr2_sysenv.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,9 @@ static struct tr2_sysenv_entry tr2_sysenv_settings[] = {
4949
"trace2.perftarget" },
5050
[TR2_SYSENV_PERF_BRIEF] = { "GIT_TRACE2_PERF_BRIEF",
5151
"trace2.perfbrief" },
52+
53+
[TR2_SYSENV_MAX_FILES] = { "GIT_TRACE2_MAX_FILES",
54+
"trace2.maxfiles" },
5255
};
5356
/* clang-format on */
5457

trace2/tr2_sysenv.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@ enum tr2_sysenv_variable {
2424
TR2_SYSENV_PERF,
2525
TR2_SYSENV_PERF_BRIEF,
2626

27+
TR2_SYSENV_MAX_FILES,
28+
2729
TR2_SYSENV_MUST_BE_LAST
2830
};
2931

0 commit comments

Comments
 (0)