xref: /openbmc/linux/tools/testing/selftests/bpf/bench.h (revision 9a93bf3fda3d03762868b1424e898395ffc71575)
18e7c2a02SAndrii Nakryiko /* SPDX-License-Identifier: GPL-2.0 */
28e7c2a02SAndrii Nakryiko #pragma once
38e7c2a02SAndrii Nakryiko #include <stdlib.h>
48e7c2a02SAndrii Nakryiko #include <stdbool.h>
58e7c2a02SAndrii Nakryiko #include <linux/err.h>
68e7c2a02SAndrii Nakryiko #include <errno.h>
78e7c2a02SAndrii Nakryiko #include <unistd.h>
88e7c2a02SAndrii Nakryiko #include <bpf/bpf.h>
98e7c2a02SAndrii Nakryiko #include <bpf/libbpf.h>
108e7c2a02SAndrii Nakryiko #include <math.h>
118e7c2a02SAndrii Nakryiko #include <time.h>
128e7c2a02SAndrii Nakryiko #include <sys/syscall.h>
138e7c2a02SAndrii Nakryiko 
148e7c2a02SAndrii Nakryiko struct cpu_set {
158e7c2a02SAndrii Nakryiko 	bool *cpus;
168e7c2a02SAndrii Nakryiko 	int cpus_len;
178e7c2a02SAndrii Nakryiko 	int next_cpu;
188e7c2a02SAndrii Nakryiko };
198e7c2a02SAndrii Nakryiko 
208e7c2a02SAndrii Nakryiko struct env {
218e7c2a02SAndrii Nakryiko 	char *bench_name;
228e7c2a02SAndrii Nakryiko 	int duration_sec;
238e7c2a02SAndrii Nakryiko 	int warmup_sec;
248e7c2a02SAndrii Nakryiko 	bool verbose;
258e7c2a02SAndrii Nakryiko 	bool list;
268e7c2a02SAndrii Nakryiko 	bool affinity;
278e7c2a02SAndrii Nakryiko 	int consumer_cnt;
288e7c2a02SAndrii Nakryiko 	int producer_cnt;
298e7c2a02SAndrii Nakryiko 	struct cpu_set prod_cpus;
308e7c2a02SAndrii Nakryiko 	struct cpu_set cons_cpus;
318e7c2a02SAndrii Nakryiko };
328e7c2a02SAndrii Nakryiko 
338e7c2a02SAndrii Nakryiko struct bench_res {
348e7c2a02SAndrii Nakryiko 	long hits;
358e7c2a02SAndrii Nakryiko 	long drops;
3657fd1c63SJoanne Koong 	long false_hits;
378e7c2a02SAndrii Nakryiko };
388e7c2a02SAndrii Nakryiko 
398e7c2a02SAndrii Nakryiko struct bench {
408e7c2a02SAndrii Nakryiko 	const char *name;
41*9a93bf3fSHou Tao 	void (*validate)(void);
42*9a93bf3fSHou Tao 	void (*setup)(void);
438e7c2a02SAndrii Nakryiko 	void *(*producer_thread)(void *ctx);
448e7c2a02SAndrii Nakryiko 	void *(*consumer_thread)(void *ctx);
458e7c2a02SAndrii Nakryiko 	void (*measure)(struct bench_res* res);
468e7c2a02SAndrii Nakryiko 	void (*report_progress)(int iter, struct bench_res* res, long delta_ns);
478e7c2a02SAndrii Nakryiko 	void (*report_final)(struct bench_res res[], int res_cnt);
488e7c2a02SAndrii Nakryiko };
498e7c2a02SAndrii Nakryiko 
508e7c2a02SAndrii Nakryiko struct counter {
518e7c2a02SAndrii Nakryiko 	long value;
528e7c2a02SAndrii Nakryiko } __attribute__((aligned(128)));
538e7c2a02SAndrii Nakryiko 
548e7c2a02SAndrii Nakryiko extern struct env env;
558e7c2a02SAndrii Nakryiko extern const struct bench *bench;
568e7c2a02SAndrii Nakryiko 
57*9a93bf3fSHou Tao void setup_libbpf(void);
588e7c2a02SAndrii Nakryiko void hits_drops_report_progress(int iter, struct bench_res *res, long delta_ns);
598e7c2a02SAndrii Nakryiko void hits_drops_report_final(struct bench_res res[], int res_cnt);
6057fd1c63SJoanne Koong void false_hits_report_progress(int iter, struct bench_res *res, long delta_ns);
6157fd1c63SJoanne Koong void false_hits_report_final(struct bench_res res[], int res_cnt);
62ec151037SJoanne Koong void ops_report_progress(int iter, struct bench_res *res, long delta_ns);
63ec151037SJoanne Koong void ops_report_final(struct bench_res res[], int res_cnt);
648e7c2a02SAndrii Nakryiko 
65*9a93bf3fSHou Tao static inline __u64 get_time_ns(void)
66*9a93bf3fSHou Tao {
678e7c2a02SAndrii Nakryiko 	struct timespec t;
688e7c2a02SAndrii Nakryiko 
698e7c2a02SAndrii Nakryiko 	clock_gettime(CLOCK_MONOTONIC, &t);
708e7c2a02SAndrii Nakryiko 
718e7c2a02SAndrii Nakryiko 	return (u64)t.tv_sec * 1000000000 + t.tv_nsec;
728e7c2a02SAndrii Nakryiko }
738e7c2a02SAndrii Nakryiko 
748e7c2a02SAndrii Nakryiko static inline void atomic_inc(long *value)
758e7c2a02SAndrii Nakryiko {
768e7c2a02SAndrii Nakryiko 	(void)__atomic_add_fetch(value, 1, __ATOMIC_RELAXED);
778e7c2a02SAndrii Nakryiko }
788e7c2a02SAndrii Nakryiko 
798e7c2a02SAndrii Nakryiko static inline void atomic_add(long *value, long n)
808e7c2a02SAndrii Nakryiko {
818e7c2a02SAndrii Nakryiko 	(void)__atomic_add_fetch(value, n, __ATOMIC_RELAXED);
828e7c2a02SAndrii Nakryiko }
838e7c2a02SAndrii Nakryiko 
848e7c2a02SAndrii Nakryiko static inline long atomic_swap(long *value, long n)
858e7c2a02SAndrii Nakryiko {
868e7c2a02SAndrii Nakryiko 	return __atomic_exchange_n(value, n, __ATOMIC_RELAXED);
878e7c2a02SAndrii Nakryiko }
88