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 };
50 
51 struct bench {
52 	const char *name;
53 	const struct argp *argp;
54 	void (*validate)(void);
55 	void (*setup)(void);
56 	void *(*producer_thread)(void *ctx);
57 	void *(*consumer_thread)(void *ctx);
58 	void (*measure)(struct bench_res* res);
59 	void (*report_progress)(int iter, struct bench_res* res, long delta_ns);
60 	void (*report_final)(struct bench_res res[], int res_cnt);
61 };
62 
63 struct counter {
64 	long value;
65 } __attribute__((aligned(128)));
66 
67 extern struct env env;
68 extern const struct bench *bench;
69 
70 void setup_libbpf(void);
71 void hits_drops_report_progress(int iter, struct bench_res *res, long delta_ns);
72 void hits_drops_report_final(struct bench_res res[], int res_cnt);
73 void false_hits_report_progress(int iter, struct bench_res *res, long delta_ns);
74 void false_hits_report_final(struct bench_res res[], int res_cnt);
75 void ops_report_progress(int iter, struct bench_res *res, long delta_ns);
76 void ops_report_final(struct bench_res res[], int res_cnt);
77 void local_storage_report_progress(int iter, struct bench_res *res,
78 				   long delta_ns);
79 void local_storage_report_final(struct bench_res res[], int res_cnt);
80 void grace_period_latency_basic_stats(struct bench_res res[], int res_cnt,
81 				      struct basic_stats *gp_stat);
82 void grace_period_ticks_basic_stats(struct bench_res res[], int res_cnt,
83 				    struct basic_stats *gp_stat);
84 
atomic_inc(long * value)85 static inline void atomic_inc(long *value)
86 {
87 	(void)__atomic_add_fetch(value, 1, __ATOMIC_RELAXED);
88 }
89 
atomic_add(long * value,long n)90 static inline void atomic_add(long *value, long n)
91 {
92 	(void)__atomic_add_fetch(value, n, __ATOMIC_RELAXED);
93 }
94 
atomic_swap(long * value,long n)95 static inline long atomic_swap(long *value, long n)
96 {
97 	return __atomic_exchange_n(value, n, __ATOMIC_RELAXED);
98 }
99