]> git.ipfire.org Git - thirdparty/kernel/linux.git/commitdiff
selftests/bpf: Add bench_force_done() for early benchmark completion
authorPuranjay Mohan <puranjay@kernel.org>
Mon, 27 Apr 2026 23:22:58 +0000 (16:22 -0700)
committerAlexei Starovoitov <ast@kernel.org>
Mon, 11 May 2026 22:25:24 +0000 (15:25 -0700)
The bench framework waits for duration_sec to elapse before collecting
results.  Benchmarks that know exactly how many samples they need can
call bench_force_done() to signal completion early, avoiding wasted
wall-clock time.

Also refactor collect_measurements() to reuse bench_force_done()
instead of open-coding the same mutex/cond_signal sequence.

Signed-off-by: Puranjay Mohan <puranjay@kernel.org>
Link: https://lore.kernel.org/r/20260427232313.1582588-2-puranjay@kernel.org
Signed-off-by: Alexei Starovoitov <ast@kernel.org>
tools/testing/selftests/bpf/bench.c
tools/testing/selftests/bpf/bench.h

index 029b3e21f43846d2850bce1b81954ab2e86a9c06..47a4e72208d602aedbea752960059a8f808d2b1c 100644 (file)
@@ -741,6 +741,13 @@ static void setup_benchmark(void)
 static pthread_mutex_t bench_done_mtx = PTHREAD_MUTEX_INITIALIZER;
 static pthread_cond_t bench_done = PTHREAD_COND_INITIALIZER;
 
+void bench_force_done(void)
+{
+       pthread_mutex_lock(&bench_done_mtx);
+       pthread_cond_signal(&bench_done);
+       pthread_mutex_unlock(&bench_done_mtx);
+}
+
 static void collect_measurements(long delta_ns) {
        int iter = state.res_cnt++;
        struct bench_res *res = &state.results[iter];
@@ -750,11 +757,8 @@ static void collect_measurements(long delta_ns) {
        if (bench->report_progress)
                bench->report_progress(iter, res, delta_ns);
 
-       if (iter == env.duration_sec + env.warmup_sec) {
-               pthread_mutex_lock(&bench_done_mtx);
-               pthread_cond_signal(&bench_done);
-               pthread_mutex_unlock(&bench_done_mtx);
-       }
+       if (iter == env.duration_sec + env.warmup_sec)
+               bench_force_done();
 }
 
 int main(int argc, char **argv)
index 7cf21936e7eda0c32dcb06e11adf23610738d1db..89a3fc72f70e29e6c9ccd8ad68854acde0d079b5 100644 (file)
@@ -70,6 +70,7 @@ extern struct env env;
 extern const struct bench *bench;
 
 void setup_libbpf(void);
+void bench_force_done(void);
 void hits_drops_report_progress(int iter, struct bench_res *res, long delta_ns);
 void hits_drops_report_final(struct bench_res res[], int res_cnt);
 void false_hits_report_progress(int iter, struct bench_res *res, long delta_ns);