@@ -38,10 +38,10 @@ unsigned long long timing(clockid_t clk_id, unsigned long long samples)
38
38
i *= 1000000000ULL ;
39
39
i += finish .tv_nsec - start .tv_nsec ;
40
40
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 );
45
45
46
46
return i ;
47
47
}
@@ -53,7 +53,7 @@ unsigned long long calibrate(void)
53
53
pid_t pid , ret ;
54
54
int seconds = 15 ;
55
55
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 );
57
57
58
58
samples = 0 ;
59
59
pid = getpid ();
@@ -98,24 +98,36 @@ bool le(int i_one, int i_two)
98
98
}
99
99
100
100
long compare (const char * name_one , const char * name_eval , const char * name_two ,
101
- unsigned long long one , bool (* eval )(int , int ), unsigned long long two )
101
+ unsigned long long one , bool (* eval )(int , int ), unsigned long long two ,
102
+ bool skip )
102
103
{
103
104
bool good ;
104
105
105
- printf ("\t%s %s %s (%lld %s %lld): " , name_one , name_eval , name_two ,
106
- (long long )one , name_eval , (long long )two );
106
+ if (skip ) {
107
+ ksft_test_result_skip ("%s %s %s\n" , name_one , name_eval ,
108
+ name_two );
109
+ return 0 ;
110
+ }
111
+
112
+ ksft_print_msg ("\t%s %s %s (%lld %s %lld): " , name_one , name_eval , name_two ,
113
+ (long long )one , name_eval , (long long )two );
107
114
if (one > INT_MAX ) {
108
- printf ("Miscalculation! Measurement went negative: %lld\n" , (long long )one );
109
- return 1 ;
115
+ ksft_print_msg ("Miscalculation! Measurement went negative: %lld\n" , (long long )one );
116
+ good = false;
117
+ goto out ;
110
118
}
111
119
if (two > INT_MAX ) {
112
- printf ("Miscalculation! Measurement went negative: %lld\n" , (long long )two );
113
- return 1 ;
120
+ ksft_print_msg ("Miscalculation! Measurement went negative: %lld\n" , (long long )two );
121
+ good = false;
122
+ goto out ;
114
123
}
115
124
116
125
good = eval (one , two );
117
126
printf ("%s\n" , good ? "✔️" : "❌" );
118
127
128
+ out :
129
+ ksft_test_result (good , "%s %s %s\n" , name_one , name_eval , name_two );
130
+
119
131
return good ? 0 : 1 ;
120
132
}
121
133
@@ -142,27 +154,34 @@ int main(int argc, char *argv[])
142
154
unsigned long long samples , calc ;
143
155
unsigned long long native , filter1 , filter2 , bitmap1 , bitmap2 ;
144
156
unsigned long long entry , per_filter1 , per_filter2 ;
157
+ bool skip = false;
145
158
146
159
setbuf (stdout , NULL );
147
160
148
- printf ("Running on:\n" );
161
+ ksft_print_header ();
162
+ ksft_set_plan (7 );
163
+
164
+ ksft_print_msg ("Running on:\n" );
165
+ ksft_print_msg ("" );
149
166
system ("uname -a" );
150
167
151
- printf ("Current BPF sysctl settings:\n" );
168
+ ksft_print_msg ("Current BPF sysctl settings:\n" );
152
169
/* Avoid using "sysctl" which may not be installed. */
170
+ ksft_print_msg ("" );
153
171
system ("grep -H . /proc/sys/net/core/bpf_jit_enable" );
172
+ ksft_print_msg ("" );
154
173
system ("grep -H . /proc/sys/net/core/bpf_jit_harden" );
155
174
156
175
if (argc > 1 )
157
176
samples = strtoull (argv [1 ], NULL , 0 );
158
177
else
159
178
samples = calibrate ();
160
179
161
- printf ("Benchmarking %llu syscalls...\n" , samples );
180
+ ksft_print_msg ("Benchmarking %llu syscalls...\n" , samples );
162
181
163
182
/* Native call */
164
183
native = timing (CLOCK_PROCESS_CPUTIME_ID , samples ) / samples ;
165
- printf ("getpid native: %llu ns\n" , native );
184
+ ksft_print_msg ("getpid native: %llu ns\n" , native );
166
185
167
186
ret = prctl (PR_SET_NO_NEW_PRIVS , 1 , 0 , 0 , 0 );
168
187
assert (ret == 0 );
@@ -172,35 +191,37 @@ int main(int argc, char *argv[])
172
191
assert (ret == 0 );
173
192
174
193
bitmap1 = timing (CLOCK_PROCESS_CPUTIME_ID , samples ) / samples ;
175
- printf ("getpid RET_ALLOW 1 filter (bitmap): %llu ns\n" , bitmap1 );
194
+ ksft_print_msg ("getpid RET_ALLOW 1 filter (bitmap): %llu ns\n" , bitmap1 );
176
195
177
196
/* Second filter resulting in a bitmap */
178
197
ret = prctl (PR_SET_SECCOMP , SECCOMP_MODE_FILTER , & bitmap_prog );
179
198
assert (ret == 0 );
180
199
181
200
bitmap2 = timing (CLOCK_PROCESS_CPUTIME_ID , samples ) / samples ;
182
- printf ("getpid RET_ALLOW 2 filters (bitmap): %llu ns\n" , bitmap2 );
201
+ ksft_print_msg ("getpid RET_ALLOW 2 filters (bitmap): %llu ns\n" , bitmap2 );
183
202
184
203
/* Third filter, can no longer be converted to bitmap */
185
204
ret = prctl (PR_SET_SECCOMP , SECCOMP_MODE_FILTER , & prog );
186
205
assert (ret == 0 );
187
206
188
207
filter1 = timing (CLOCK_PROCESS_CPUTIME_ID , samples ) / samples ;
189
- printf ("getpid RET_ALLOW 3 filters (full): %llu ns\n" , filter1 );
208
+ ksft_print_msg ("getpid RET_ALLOW 3 filters (full): %llu ns\n" , filter1 );
190
209
191
210
/* Fourth filter, can not be converted to bitmap because of filter 3 */
192
211
ret = prctl (PR_SET_SECCOMP , SECCOMP_MODE_FILTER , & bitmap_prog );
193
212
assert (ret == 0 );
194
213
195
214
filter2 = timing (CLOCK_PROCESS_CPUTIME_ID , samples ) / samples ;
196
- printf ("getpid RET_ALLOW 4 filters (full): %llu ns\n" , filter2 );
215
+ ksft_print_msg ("getpid RET_ALLOW 4 filters (full): %llu ns\n" , filter2 );
197
216
198
217
/* Estimations */
199
218
#define ESTIMATE (fmt , var , what ) do { \
200
219
var = (what); \
201
- printf("Estimated " fmt ": %llu ns\n", var); \
202
- if (var > INT_MAX) \
203
- goto more_samples; \
220
+ ksft_print_msg("Estimated " fmt ": %llu ns\n", var); \
221
+ if (var > INT_MAX) { \
222
+ skip = true; \
223
+ ret |= 1; \
224
+ } \
204
225
} while (0)
205
226
206
227
ESTIMATE ("total seccomp overhead for 1 bitmapped filter" , calc ,
@@ -218,31 +239,34 @@ int main(int argc, char *argv[])
218
239
ESTIMATE ("seccomp per-filter overhead (filters / 4)" , per_filter2 ,
219
240
(filter2 - native - entry ) / 4 );
220
241
221
- printf ("Expectations:\n" );
222
- ret |= compare ("native" , "≤" , "1 bitmap" , native , le , bitmap1 );
223
- bits = compare ("native" , "≤" , "1 filter" , native , le , filter1 );
242
+ ksft_print_msg ("Expectations:\n" );
243
+ ret |= compare ("native" , "≤" , "1 bitmap" , native , le , bitmap1 ,
244
+ skip );
245
+ bits = compare ("native" , "≤" , "1 filter" , native , le , filter1 ,
246
+ skip );
224
247
if (bits )
225
- goto more_samples ;
248
+ skip = true ;
226
249
227
250
ret |= compare ("per-filter (last 2 diff)" , "≈" , "per-filter (filters / 4)" ,
228
- per_filter1 , approx , per_filter2 );
251
+ per_filter1 , approx , per_filter2 , skip );
229
252
230
253
bits = compare ("1 bitmapped" , "≈" , "2 bitmapped" ,
231
- bitmap1 - native , approx , bitmap2 - native );
254
+ bitmap1 - native , approx , bitmap2 - native , skip );
232
255
if (bits ) {
233
- printf ("Skipping constant action bitmap expectations: they appear unsupported.\n" );
234
- goto out ;
256
+ ksft_print_msg ("Skipping constant action bitmap expectations: they appear unsupported.\n" );
257
+ skip = true ;
235
258
}
236
259
237
- ret |= compare ("entry" , "≈" , "1 bitmapped" , entry , approx , bitmap1 - native );
238
- ret |= compare ("entry" , "≈" , "2 bitmapped" , entry , approx , bitmap2 - native );
260
+ ret |= compare ("entry" , "≈" , "1 bitmapped" , entry , approx ,
261
+ bitmap1 - native , skip );
262
+ ret |= compare ("entry" , "≈" , "2 bitmapped" , entry , approx ,
263
+ bitmap2 - native , skip );
239
264
ret |= compare ("native + entry + (per filter * 4)" , "≈" , "4 filters total" ,
240
- entry + (per_filter1 * 4 ) + native , approx , filter2 );
241
- if (ret == 0 )
242
- goto out ;
265
+ entry + (per_filter1 * 4 ) + native , approx , filter2 ,
266
+ skip );
243
267
244
- more_samples :
245
- printf ("Saw unexpected benchmark result. Try running again with more samples?\n" );
246
- out :
247
- return 0 ;
268
+ if ( ret )
269
+ ksft_print_msg ("Saw unexpected benchmark result. Try running again with more samples?\n" );
270
+
271
+ ksft_finished () ;
248
272
}
0 commit comments