xref: /linux/tools/testing/selftests/bpf/bench.h (revision ae28ed4578e6d5a481e39c5a9827f27048661fdd)
1 /* SPDX-License-Identifier: GPL-2.0 */
2 #pragma once
3 #include <stdlib.h>
4 #include <stdbool.h>
5 #include <linux/err.h>
6 #include <errno.h>
7 #include <unistd.h>
8 #include <bpf/bpf.h>
9 #include <bpf/libbpf.h>
10 #include <math.h>
11 #include <time.h>
12 #include <sys/syscall.h>
13 #include <limits.h>
14 
15 struct cpu_set {
16 	bool *cpus;
17 	int cpus_len;
18 	int next_cpu;
19 };
20 
21 struct env {
22 	char *bench_name;
23 	int duration_sec;
24 	int warmup_sec;
25 	bool verbose;
26 	bool list;
27 	bool affinity;
28 	bool quiet;
29 	int consumer_cnt;
30 	int producer_cnt;
31 	int nr_cpus;
32 	struct cpu_set prod_cpus;
33 	struct cpu_set cons_cpus;
34 };
35 
36 struct basic_stats {
37 	double mean;
38 	double stddev;
39 };
40 
41 struct bench_res {
42 	long hits;
43 	long drops;
44 	long false_hits;
45 	long important_hits;
46 	unsigned long gp_ns;
47 	unsigned long gp_ct;
48 	unsigned int stime;
49 	unsigned long duration_ns;
50 };
51 
52 struct bench {
53 	const char *name;
54 	const struct argp *argp;
55 	void (*validate)(void);
56 	void (*setup)(void);
57 	void *(*producer_thread)(void *ctx);
58 	void *(*consumer_thread)(void *ctx);
59 	void (*measure)(struct bench_res* res);
60 	void (*report_progress)(int iter, struct bench_res* res, long delta_ns);
61 	void (*report_final)(struct bench_res res[], int res_cnt);
62 };
63 
64 struct counter {
65 	long value;
66 } __attribute__((aligned(128)));
67 
68 extern struct env env;
69 extern const struct bench *bench;
70 
71 void setup_libbpf(void);
72 void hits_drops_report_progress(int iter, struct bench_res *res, long delta_ns);
73 void hits_drops_report_final(struct bench_res res[], int res_cnt);
74 void false_hits_report_progress(int iter, struct bench_res *res, long delta_ns);
75 void false_hits_report_final(struct bench_res res[], int res_cnt);
76 void ops_report_progress(int iter, struct bench_res *res, long delta_ns);
77 void ops_report_final(struct bench_res res[], int res_cnt);
78 void local_storage_report_progress(int iter, struct bench_res *res,
79 				   long delta_ns);
80 void local_storage_report_final(struct bench_res res[], int res_cnt);
81 void grace_period_latency_basic_stats(struct bench_res res[], int res_cnt,
82 				      struct basic_stats *gp_stat);
83 void grace_period_ticks_basic_stats(struct bench_res res[], int res_cnt,
84 				    struct basic_stats *gp_stat);
85 
86 static inline void atomic_inc(long *value)
87 {
88 	(void)__atomic_add_fetch(value, 1, __ATOMIC_RELAXED);
89 }
90 
91 static inline void atomic_add(long *value, long n)
92 {
93 	(void)__atomic_add_fetch(value, n, __ATOMIC_RELAXED);
94 }
95 
96 static inline long atomic_swap(long *value, long n)
97 {
98 	return __atomic_exchange_n(value, n, __ATOMIC_RELAXED);
99 }
100