@@ -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 ();
@@ -98,24 +98,36 @@ bool le(int i_one, int i_two)
9898}
9999
100100long 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 )
102103{
103104 bool good ;
104105
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 );
107114 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 ;
110118 }
111119 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 ;
114123 }
115124
116125 good = eval (one , two );
117126 printf ("%s\n" , good ? "✔️" : "❌" );
118127
128+ out :
129+ ksft_test_result (good , "%s %s %s\n" , name_one , name_eval , name_two );
130+
119131 return good ? 0 : 1 ;
120132}
121133
@@ -142,27 +154,34 @@ int main(int argc, char *argv[])
142154 unsigned long long samples , calc ;
143155 unsigned long long native , filter1 , filter2 , bitmap1 , bitmap2 ;
144156 unsigned long long entry , per_filter1 , per_filter2 ;
157+ bool skip = false;
145158
146159 setbuf (stdout , NULL );
147160
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 ("" );
149166 system ("uname -a" );
150167
151- printf ("Current BPF sysctl settings:\n" );
168+ ksft_print_msg ("Current BPF sysctl settings:\n" );
152169 /* Avoid using "sysctl" which may not be installed. */
170+ ksft_print_msg ("" );
153171 system ("grep -H . /proc/sys/net/core/bpf_jit_enable" );
172+ ksft_print_msg ("" );
154173 system ("grep -H . /proc/sys/net/core/bpf_jit_harden" );
155174
156175 if (argc > 1 )
157176 samples = strtoull (argv [1 ], NULL , 0 );
158177 else
159178 samples = calibrate ();
160179
161- printf ("Benchmarking %llu syscalls...\n" , samples );
180+ ksft_print_msg ("Benchmarking %llu syscalls...\n" , samples );
162181
163182 /* Native call */
164183 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 );
166185
167186 ret = prctl (PR_SET_NO_NEW_PRIVS , 1 , 0 , 0 , 0 );
168187 assert (ret == 0 );
@@ -172,35 +191,37 @@ int main(int argc, char *argv[])
172191 assert (ret == 0 );
173192
174193 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 );
176195
177196 /* Second filter resulting in a bitmap */
178197 ret = prctl (PR_SET_SECCOMP , SECCOMP_MODE_FILTER , & bitmap_prog );
179198 assert (ret == 0 );
180199
181200 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 );
183202
184203 /* Third filter, can no longer be converted to bitmap */
185204 ret = prctl (PR_SET_SECCOMP , SECCOMP_MODE_FILTER , & prog );
186205 assert (ret == 0 );
187206
188207 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 );
190209
191210 /* Fourth filter, can not be converted to bitmap because of filter 3 */
192211 ret = prctl (PR_SET_SECCOMP , SECCOMP_MODE_FILTER , & bitmap_prog );
193212 assert (ret == 0 );
194213
195214 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 );
197216
198217 /* Estimations */
199218#define ESTIMATE (fmt , var , what ) do { \
200219 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+ } \
204225 } while (0)
205226
206227 ESTIMATE ("total seccomp overhead for 1 bitmapped filter" , calc ,
@@ -218,31 +239,34 @@ int main(int argc, char *argv[])
218239 ESTIMATE ("seccomp per-filter overhead (filters / 4)" , per_filter2 ,
219240 (filter2 - native - entry ) / 4 );
220241
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 );
224247 if (bits )
225- goto more_samples ;
248+ skip = true ;
226249
227250 ret |= compare ("per-filter (last 2 diff)" , "≈" , "per-filter (filters / 4)" ,
228- per_filter1 , approx , per_filter2 );
251+ per_filter1 , approx , per_filter2 , skip );
229252
230253 bits = compare ("1 bitmapped" , "≈" , "2 bitmapped" ,
231- bitmap1 - native , approx , bitmap2 - native );
254+ bitmap1 - native , approx , bitmap2 - native , skip );
232255 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 ;
235258 }
236259
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 );
239264 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 );
243267
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 () ;
248272}
0 commit comments