xref: /linux/tools/testing/selftests/bpf/bench.h (revision 69050f8d6d075dc01af7a5f2f550a8067510366f)
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 	bool stacktrace;
30 	int consumer_cnt;
31 	int producer_cnt;
32 	int nr_cpus;
33 	struct cpu_set prod_cpus;
34 	struct cpu_set cons_cpus;
35 };
36 
37 struct basic_stats {
38 	double mean;
39 	double stddev;
40 };
41 
42 struct bench_res {
43 	long hits;
44 	long drops;
45 	long false_hits;
46 	long important_hits;
47 	unsigned long gp_ns;
48 	unsigned long gp_ct;
49 	unsigned int stime;
50 	unsigned long duration_ns;
51 };
52 
53 struct bench {
54 	const char *name;
55 	const struct argp *argp;
56 	void (*validate)(void);
57 	void (*setup)(void);
58 	void *(*producer_thread)(void *ctx);
59 	void *(*consumer_thread)(void *ctx);
60 	void (*measure)(struct bench_res* res);
61 	void (*report_progress)(int iter, struct bench_res* res, long delta_ns);
62 	void (*report_final)(struct bench_res res[], int res_cnt);
63 };
64 
65 struct counter {
66 	long value;
67 } __attribute__((aligned(128)));
68 
69 extern struct env env;
70 extern const struct bench *bench;
71 
72 void setup_libbpf(void);
73 void hits_drops_report_progress(int iter, struct bench_res *res, long delta_ns);
74 void hits_drops_report_final(struct bench_res res[], int res_cnt);
75 void false_hits_report_progress(int iter, struct bench_res *res, long delta_ns);
76 void false_hits_report_final(struct bench_res res[], int res_cnt);
77 void ops_report_progress(int iter, struct bench_res *res, long delta_ns);
78 void ops_report_final(struct bench_res res[], int res_cnt);
79 void local_storage_report_progress(int iter, struct bench_res *res,
80 				   long delta_ns);
81 void local_storage_report_final(struct bench_res res[], int res_cnt);
82 void grace_period_latency_basic_stats(struct bench_res res[], int res_cnt,
83 				      struct basic_stats *gp_stat);
84 void grace_period_ticks_basic_stats(struct bench_res res[], int res_cnt,
85 				    struct basic_stats *gp_stat);
86 
87 static inline void atomic_inc(long *value)
88 {
89 	(void)__atomic_add_fetch(value, 1, __ATOMIC_RELAXED);
90 }
91 
92 static inline void atomic_add(long *value, long n)
93 {
94 	(void)__atomic_add_fetch(value, n, __ATOMIC_RELAXED);
95 }
96 
97 static inline long atomic_swap(long *value, long n)
98 {
99 	return __atomic_exchange_n(value, n, __ATOMIC_RELAXED);
100 }
101