Skip to content

Commit 5820cfe

Browse files
broonieshuahkh
authored andcommitted
kselftest/seccomp: Use kselftest output functions for benchmark
In preparation for trying to output the test results themselves in TAP format rework all the prints in the benchmark to use the kselftest output functions. The uses of system() all produce single line output so we can avoid having to deal with fully managing the child process and continue to use system() by simply printing an empty message before we invoke system(). We also leave one printf() used to complete a line of output in place. Tested-by: Anders Roxell <[email protected]> Acked-by: Kees Cook <[email protected]> Signed-off-by: Mark Brown <[email protected]> Signed-off-by: Shuah Khan <[email protected]>
1 parent f1fea72 commit 5820cfe

File tree

1 file changed

+24
-21
lines changed

1 file changed

+24
-21
lines changed

tools/testing/selftests/seccomp/seccomp_benchmark.c

Lines changed: 24 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -38,10 +38,10 @@ unsigned long long timing(clockid_t clk_id, unsigned long long samples)
3838
i *= 1000000000ULL;
3939
i += finish.tv_nsec - start.tv_nsec;
4040

41-
printf("%lu.%09lu - %lu.%09lu = %llu (%.1fs)\n",
42-
finish.tv_sec, finish.tv_nsec,
43-
start.tv_sec, start.tv_nsec,
44-
i, (double)i / 1000000000.0);
41+
ksft_print_msg("%lu.%09lu - %lu.%09lu = %llu (%.1fs)\n",
42+
finish.tv_sec, finish.tv_nsec,
43+
start.tv_sec, start.tv_nsec,
44+
i, (double)i / 1000000000.0);
4545

4646
return i;
4747
}
@@ -53,7 +53,7 @@ unsigned long long calibrate(void)
5353
pid_t pid, ret;
5454
int seconds = 15;
5555

56-
printf("Calibrating sample size for %d seconds worth of syscalls ...\n", seconds);
56+
ksft_print_msg("Calibrating sample size for %d seconds worth of syscalls ...\n", seconds);
5757

5858
samples = 0;
5959
pid = getpid();
@@ -102,14 +102,14 @@ long compare(const char *name_one, const char *name_eval, const char *name_two,
102102
{
103103
bool good;
104104

105-
printf("\t%s %s %s (%lld %s %lld): ", name_one, name_eval, name_two,
106-
(long long)one, name_eval, (long long)two);
105+
ksft_print_msg("\t%s %s %s (%lld %s %lld): ", name_one, name_eval, name_two,
106+
(long long)one, name_eval, (long long)two);
107107
if (one > INT_MAX) {
108-
printf("Miscalculation! Measurement went negative: %lld\n", (long long)one);
108+
ksft_print_msg("Miscalculation! Measurement went negative: %lld\n", (long long)one);
109109
return 1;
110110
}
111111
if (two > INT_MAX) {
112-
printf("Miscalculation! Measurement went negative: %lld\n", (long long)two);
112+
ksft_print_msg("Miscalculation! Measurement went negative: %lld\n", (long long)two);
113113
return 1;
114114
}
115115

@@ -145,24 +145,27 @@ int main(int argc, char *argv[])
145145

146146
setbuf(stdout, NULL);
147147

148-
printf("Running on:\n");
148+
ksft_print_msg("Running on:\n");
149+
ksft_print_msg("");
149150
system("uname -a");
150151

151-
printf("Current BPF sysctl settings:\n");
152+
ksft_print_msg("Current BPF sysctl settings:\n");
152153
/* Avoid using "sysctl" which may not be installed. */
154+
ksft_print_msg("");
153155
system("grep -H . /proc/sys/net/core/bpf_jit_enable");
156+
ksft_print_msg("");
154157
system("grep -H . /proc/sys/net/core/bpf_jit_harden");
155158

156159
if (argc > 1)
157160
samples = strtoull(argv[1], NULL, 0);
158161
else
159162
samples = calibrate();
160163

161-
printf("Benchmarking %llu syscalls...\n", samples);
164+
ksft_print_msg("Benchmarking %llu syscalls...\n", samples);
162165

163166
/* Native call */
164167
native = timing(CLOCK_PROCESS_CPUTIME_ID, samples) / samples;
165-
printf("getpid native: %llu ns\n", native);
168+
ksft_print_msg("getpid native: %llu ns\n", native);
166169

167170
ret = prctl(PR_SET_NO_NEW_PRIVS, 1, 0, 0, 0);
168171
assert(ret == 0);
@@ -172,33 +175,33 @@ int main(int argc, char *argv[])
172175
assert(ret == 0);
173176

174177
bitmap1 = timing(CLOCK_PROCESS_CPUTIME_ID, samples) / samples;
175-
printf("getpid RET_ALLOW 1 filter (bitmap): %llu ns\n", bitmap1);
178+
ksft_print_msg("getpid RET_ALLOW 1 filter (bitmap): %llu ns\n", bitmap1);
176179

177180
/* Second filter resulting in a bitmap */
178181
ret = prctl(PR_SET_SECCOMP, SECCOMP_MODE_FILTER, &bitmap_prog);
179182
assert(ret == 0);
180183

181184
bitmap2 = timing(CLOCK_PROCESS_CPUTIME_ID, samples) / samples;
182-
printf("getpid RET_ALLOW 2 filters (bitmap): %llu ns\n", bitmap2);
185+
ksft_print_msg("getpid RET_ALLOW 2 filters (bitmap): %llu ns\n", bitmap2);
183186

184187
/* Third filter, can no longer be converted to bitmap */
185188
ret = prctl(PR_SET_SECCOMP, SECCOMP_MODE_FILTER, &prog);
186189
assert(ret == 0);
187190

188191
filter1 = timing(CLOCK_PROCESS_CPUTIME_ID, samples) / samples;
189-
printf("getpid RET_ALLOW 3 filters (full): %llu ns\n", filter1);
192+
ksft_print_msg("getpid RET_ALLOW 3 filters (full): %llu ns\n", filter1);
190193

191194
/* Fourth filter, can not be converted to bitmap because of filter 3 */
192195
ret = prctl(PR_SET_SECCOMP, SECCOMP_MODE_FILTER, &bitmap_prog);
193196
assert(ret == 0);
194197

195198
filter2 = timing(CLOCK_PROCESS_CPUTIME_ID, samples) / samples;
196-
printf("getpid RET_ALLOW 4 filters (full): %llu ns\n", filter2);
199+
ksft_print_msg("getpid RET_ALLOW 4 filters (full): %llu ns\n", filter2);
197200

198201
/* Estimations */
199202
#define ESTIMATE(fmt, var, what) do { \
200203
var = (what); \
201-
printf("Estimated " fmt ": %llu ns\n", var); \
204+
ksft_print_msg("Estimated " fmt ": %llu ns\n", var); \
202205
if (var > INT_MAX) \
203206
goto more_samples; \
204207
} while (0)
@@ -218,7 +221,7 @@ int main(int argc, char *argv[])
218221
ESTIMATE("seccomp per-filter overhead (filters / 4)", per_filter2,
219222
(filter2 - native - entry) / 4);
220223

221-
printf("Expectations:\n");
224+
ksft_print_msg("Expectations:\n");
222225
ret |= compare("native", "≤", "1 bitmap", native, le, bitmap1);
223226
bits = compare("native", "≤", "1 filter", native, le, filter1);
224227
if (bits)
@@ -230,7 +233,7 @@ int main(int argc, char *argv[])
230233
bits = compare("1 bitmapped", "≈", "2 bitmapped",
231234
bitmap1 - native, approx, bitmap2 - native);
232235
if (bits) {
233-
printf("Skipping constant action bitmap expectations: they appear unsupported.\n");
236+
ksft_print_msg("Skipping constant action bitmap expectations: they appear unsupported.\n");
234237
goto out;
235238
}
236239

@@ -242,7 +245,7 @@ int main(int argc, char *argv[])
242245
goto out;
243246

244247
more_samples:
245-
printf("Saw unexpected benchmark result. Try running again with more samples?\n");
248+
ksft_print_msg("Saw unexpected benchmark result. Try running again with more samples?\n");
246249
out:
247250
return 0;
248251
}

0 commit comments

Comments
 (0)