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