xref: /openbmc/linux/tools/testing/selftests/bpf/bench.h (revision 34d6f206a88c2651d216bd3487ac956a40b2ba8e)
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>
13*4bff8cc5STony Ambardar #include <limits.h>
148e7c2a02SAndrii Nakryiko 
158e7c2a02SAndrii Nakryiko struct cpu_set {
168e7c2a02SAndrii Nakryiko 	bool *cpus;
178e7c2a02SAndrii Nakryiko 	int cpus_len;
188e7c2a02SAndrii Nakryiko 	int next_cpu;
198e7c2a02SAndrii Nakryiko };
208e7c2a02SAndrii Nakryiko 
218e7c2a02SAndrii Nakryiko struct env {
228e7c2a02SAndrii Nakryiko 	char *bench_name;
238e7c2a02SAndrii Nakryiko 	int duration_sec;
248e7c2a02SAndrii Nakryiko 	int warmup_sec;
258e7c2a02SAndrii Nakryiko 	bool verbose;
268e7c2a02SAndrii Nakryiko 	bool list;
278e7c2a02SAndrii Nakryiko 	bool affinity;
2890c22503SAnton Protopopov 	bool quiet;
298e7c2a02SAndrii Nakryiko 	int consumer_cnt;
308e7c2a02SAndrii Nakryiko 	int producer_cnt;
31da77ae2bSHou Tao 	int nr_cpus;
328e7c2a02SAndrii Nakryiko 	struct cpu_set prod_cpus;
338e7c2a02SAndrii Nakryiko 	struct cpu_set cons_cpus;
348e7c2a02SAndrii Nakryiko };
358e7c2a02SAndrii Nakryiko 
362b4b2621SDave Marchevsky struct basic_stats {
372b4b2621SDave Marchevsky 	double mean;
382b4b2621SDave Marchevsky 	double stddev;
392b4b2621SDave Marchevsky };
402b4b2621SDave Marchevsky 
418e7c2a02SAndrii Nakryiko struct bench_res {
428e7c2a02SAndrii Nakryiko 	long hits;
438e7c2a02SAndrii Nakryiko 	long drops;
4457fd1c63SJoanne Koong 	long false_hits;
4573087489SDave Marchevsky 	long important_hits;
462b4b2621SDave Marchevsky 	unsigned long gp_ns;
472b4b2621SDave Marchevsky 	unsigned long gp_ct;
482b4b2621SDave Marchevsky 	unsigned int stime;
498e7c2a02SAndrii Nakryiko };
508e7c2a02SAndrii Nakryiko 
518e7c2a02SAndrii Nakryiko struct bench {
528e7c2a02SAndrii Nakryiko 	const char *name;
5322ff7aeaSAnton Protopopov 	const struct argp *argp;
549a93bf3fSHou Tao 	void (*validate)(void);
559a93bf3fSHou Tao 	void (*setup)(void);
568e7c2a02SAndrii Nakryiko 	void *(*producer_thread)(void *ctx);
578e7c2a02SAndrii Nakryiko 	void *(*consumer_thread)(void *ctx);
588e7c2a02SAndrii Nakryiko 	void (*measure)(struct bench_res* res);
598e7c2a02SAndrii Nakryiko 	void (*report_progress)(int iter, struct bench_res* res, long delta_ns);
608e7c2a02SAndrii Nakryiko 	void (*report_final)(struct bench_res res[], int res_cnt);
618e7c2a02SAndrii Nakryiko };
628e7c2a02SAndrii Nakryiko 
638e7c2a02SAndrii Nakryiko struct counter {
648e7c2a02SAndrii Nakryiko 	long value;
658e7c2a02SAndrii Nakryiko } __attribute__((aligned(128)));
668e7c2a02SAndrii Nakryiko 
678e7c2a02SAndrii Nakryiko extern struct env env;
688e7c2a02SAndrii Nakryiko extern const struct bench *bench;
698e7c2a02SAndrii Nakryiko 
709a93bf3fSHou Tao void setup_libbpf(void);
718e7c2a02SAndrii Nakryiko void hits_drops_report_progress(int iter, struct bench_res *res, long delta_ns);
728e7c2a02SAndrii Nakryiko void hits_drops_report_final(struct bench_res res[], int res_cnt);
7357fd1c63SJoanne Koong void false_hits_report_progress(int iter, struct bench_res *res, long delta_ns);
7457fd1c63SJoanne Koong void false_hits_report_final(struct bench_res res[], int res_cnt);
75ec151037SJoanne Koong void ops_report_progress(int iter, struct bench_res *res, long delta_ns);
76ec151037SJoanne Koong void ops_report_final(struct bench_res res[], int res_cnt);
7773087489SDave Marchevsky void local_storage_report_progress(int iter, struct bench_res *res,
7873087489SDave Marchevsky 				   long delta_ns);
7973087489SDave Marchevsky void local_storage_report_final(struct bench_res res[], int res_cnt);
802b4b2621SDave Marchevsky void grace_period_latency_basic_stats(struct bench_res res[], int res_cnt,
812b4b2621SDave Marchevsky 				      struct basic_stats *gp_stat);
822b4b2621SDave Marchevsky void grace_period_ticks_basic_stats(struct bench_res res[], int res_cnt,
832b4b2621SDave Marchevsky 				    struct basic_stats *gp_stat);
848e7c2a02SAndrii Nakryiko 
atomic_inc(long * value)858e7c2a02SAndrii Nakryiko static inline void atomic_inc(long *value)
868e7c2a02SAndrii Nakryiko {
878e7c2a02SAndrii Nakryiko 	(void)__atomic_add_fetch(value, 1, __ATOMIC_RELAXED);
888e7c2a02SAndrii Nakryiko }
898e7c2a02SAndrii Nakryiko 
atomic_add(long * value,long n)908e7c2a02SAndrii Nakryiko static inline void atomic_add(long *value, long n)
918e7c2a02SAndrii Nakryiko {
928e7c2a02SAndrii Nakryiko 	(void)__atomic_add_fetch(value, n, __ATOMIC_RELAXED);
938e7c2a02SAndrii Nakryiko }
948e7c2a02SAndrii Nakryiko 
atomic_swap(long * value,long n)958e7c2a02SAndrii Nakryiko static inline long atomic_swap(long *value, long n)
968e7c2a02SAndrii Nakryiko {
978e7c2a02SAndrii Nakryiko 	return __atomic_exchange_n(value, n, __ATOMIC_RELAXED);
988e7c2a02SAndrii Nakryiko }
99