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 
14 struct cpu_set {
15 	bool *cpus;
16 	int cpus_len;
17 	int next_cpu;
18 };
19 
20 struct env {
21 	char *bench_name;
22 	int duration_sec;
23 	int warmup_sec;
24 	bool verbose;
25 	bool list;
26 	bool affinity;
27 	int consumer_cnt;
28 	int producer_cnt;
29 	struct cpu_set prod_cpus;
30 	struct cpu_set cons_cpus;
31 };
32 
33 struct basic_stats {
34 	double mean;
35 	double stddev;
36 };
37 
38 struct bench_res {
39 	long hits;
40 	long drops;
41 	long false_hits;
42 	long important_hits;
43 	unsigned long gp_ns;
44 	unsigned long gp_ct;
45 	unsigned int stime;
46 };
47 
48 struct bench {
49 	const char *name;
50 	void (*validate)(void);
51 	void (*setup)(void);
52 	void *(*producer_thread)(void *ctx);
53 	void *(*consumer_thread)(void *ctx);
54 	void (*measure)(struct bench_res* res);
55 	void (*report_progress)(int iter, struct bench_res* res, long delta_ns);
56 	void (*report_final)(struct bench_res res[], int res_cnt);
57 };
58 
59 struct counter {
60 	long value;
61 } __attribute__((aligned(128)));
62 
63 extern struct env env;
64 extern const struct bench *bench;
65 
66 void setup_libbpf(void);
67 void hits_drops_report_progress(int iter, struct bench_res *res, long delta_ns);
68 void hits_drops_report_final(struct bench_res res[], int res_cnt);
69 void false_hits_report_progress(int iter, struct bench_res *res, long delta_ns);
70 void false_hits_report_final(struct bench_res res[], int res_cnt);
71 void ops_report_progress(int iter, struct bench_res *res, long delta_ns);
72 void ops_report_final(struct bench_res res[], int res_cnt);
73 void local_storage_report_progress(int iter, struct bench_res *res,
74 				   long delta_ns);
75 void local_storage_report_final(struct bench_res res[], int res_cnt);
76 void grace_period_latency_basic_stats(struct bench_res res[], int res_cnt,
77 				      struct basic_stats *gp_stat);
78 void grace_period_ticks_basic_stats(struct bench_res res[], int res_cnt,
79 				    struct basic_stats *gp_stat);
80 
81 static inline __u64 get_time_ns(void)
82 {
83 	struct timespec t;
84 
85 	clock_gettime(CLOCK_MONOTONIC, &t);
86 
87 	return (u64)t.tv_sec * 1000000000 + t.tv_nsec;
88 }
89 
90 static inline void atomic_inc(long *value)
91 {
92 	(void)__atomic_add_fetch(value, 1, __ATOMIC_RELAXED);
93 }
94 
95 static inline void atomic_add(long *value, long n)
96 {
97 	(void)__atomic_add_fetch(value, n, __ATOMIC_RELAXED);
98 }
99 
100 static inline long atomic_swap(long *value, long n)
101 {
102 	return __atomic_exchange_n(value, n, __ATOMIC_RELAXED);
103 }
104