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