WIP FPC-III support
[linux/fpc-iii.git] / tools / testing / selftests / bpf / bench.h
blobc1f48a473b022dec6820609306f7ce40312b759b
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>
14 struct cpu_set {
15 bool *cpus;
16 int cpus_len;
17 int next_cpu;
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;
33 struct bench_res {
34 long hits;
35 long drops;
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);
49 struct counter {
50 long value;
51 } __attribute__((aligned(128)));
53 extern struct env env;
54 extern const struct bench *bench;
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);
60 static inline __u64 get_time_ns() {
61 struct timespec t;
63 clock_gettime(CLOCK_MONOTONIC, &t);
65 return (u64)t.tv_sec * 1000000000 + t.tv_nsec;
68 static inline void atomic_inc(long *value)
70 (void)__atomic_add_fetch(value, 1, __ATOMIC_RELAXED);
73 static inline void atomic_add(long *value, long n)
75 (void)__atomic_add_fetch(value, n, __ATOMIC_RELAXED);
78 static inline long atomic_swap(long *value, long n)
80 return __atomic_exchange_n(value, n, __ATOMIC_RELAXED);