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 bench_res {
34 	long hits;
35 	long drops;
36 	long false_hits;
37 };
38 
39 struct bench {
40 	const char *name;
41 	void (*validate)(void);
42 	void (*setup)(void);
43 	void *(*producer_thread)(void *ctx);
44 	void *(*consumer_thread)(void *ctx);
45 	void (*measure)(struct bench_res* res);
46 	void (*report_progress)(int iter, struct bench_res* res, long delta_ns);
47 	void (*report_final)(struct bench_res res[], int res_cnt);
48 };
49 
50 struct counter {
51 	long value;
52 } __attribute__((aligned(128)));
53 
54 extern struct env env;
55 extern const struct bench *bench;
56 
57 void setup_libbpf(void);
58 void hits_drops_report_progress(int iter, struct bench_res *res, long delta_ns);
59 void hits_drops_report_final(struct bench_res res[], int res_cnt);
60 void false_hits_report_progress(int iter, struct bench_res *res, long delta_ns);
61 void false_hits_report_final(struct bench_res res[], int res_cnt);
62 void ops_report_progress(int iter, struct bench_res *res, long delta_ns);
63 void ops_report_final(struct bench_res res[], int res_cnt);
64 
65 static inline __u64 get_time_ns(void)
66 {
67 	struct timespec t;
68 
69 	clock_gettime(CLOCK_MONOTONIC, &t);
70 
71 	return (u64)t.tv_sec * 1000000000 + t.tv_nsec;
72 }
73 
74 static inline void atomic_inc(long *value)
75 {
76 	(void)__atomic_add_fetch(value, 1, __ATOMIC_RELAXED);
77 }
78 
79 static inline void atomic_add(long *value, long n)
80 {
81 	(void)__atomic_add_fetch(value, n, __ATOMIC_RELAXED);
82 }
83 
84 static inline long atomic_swap(long *value, long n)
85 {
86 	return __atomic_exchange_n(value, n, __ATOMIC_RELAXED);
87 }
88