bench.h (1990B)
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 14struct cpu_set { 15 bool *cpus; 16 int cpus_len; 17 int next_cpu; 18}; 19 20struct 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 33struct bench_res { 34 long hits; 35 long drops; 36 long false_hits; 37}; 38 39struct 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 50struct counter { 51 long value; 52} __attribute__((aligned(128))); 53 54extern struct env env; 55extern const struct bench *bench; 56 57void setup_libbpf(void); 58void hits_drops_report_progress(int iter, struct bench_res *res, long delta_ns); 59void hits_drops_report_final(struct bench_res res[], int res_cnt); 60void false_hits_report_progress(int iter, struct bench_res *res, long delta_ns); 61void false_hits_report_final(struct bench_res res[], int res_cnt); 62void ops_report_progress(int iter, struct bench_res *res, long delta_ns); 63void ops_report_final(struct bench_res res[], int res_cnt); 64 65static 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 74static inline void atomic_inc(long *value) 75{ 76 (void)__atomic_add_fetch(value, 1, __ATOMIC_RELAXED); 77} 78 79static inline void atomic_add(long *value, long n) 80{ 81 (void)__atomic_add_fetch(value, n, __ATOMIC_RELAXED); 82} 83 84static inline long atomic_swap(long *value, long n) 85{ 86 return __atomic_exchange_n(value, n, __ATOMIC_RELAXED); 87}