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