^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1) /* SPDX-License-Identifier: GPL-2.0 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2) #pragma once
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3) #include <stdlib.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4) #include <stdbool.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5) #include <linux/err.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6) #include <errno.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7) #include <unistd.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 8) #include <bpf/bpf.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 9) #include <bpf/libbpf.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10) #include <math.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 11) #include <time.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 12) #include <sys/syscall.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 13)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 14) struct cpu_set {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15) bool *cpus;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16) int cpus_len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17) int next_cpu;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20) struct env {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 21) char *bench_name;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22) int duration_sec;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23) int warmup_sec;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24) bool verbose;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25) bool list;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26) bool affinity;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27) int consumer_cnt;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28) int producer_cnt;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29) struct cpu_set prod_cpus;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30) struct cpu_set cons_cpus;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33) struct bench_res {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34) long hits;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35) long drops;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38) struct bench {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39) const char *name;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40) void (*validate)();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41) void (*setup)();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42) void *(*producer_thread)(void *ctx);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43) void *(*consumer_thread)(void *ctx);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44) void (*measure)(struct bench_res* res);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45) void (*report_progress)(int iter, struct bench_res* res, long delta_ns);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46) void (*report_final)(struct bench_res res[], int res_cnt);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49) struct counter {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 50) long value;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 51) } __attribute__((aligned(128)));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 52)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 53) extern struct env env;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 54) extern const struct bench *bench;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 55)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 56) void setup_libbpf();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 57) void hits_drops_report_progress(int iter, struct bench_res *res, long delta_ns);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 58) void hits_drops_report_final(struct bench_res res[], int res_cnt);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 59)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 60) static inline __u64 get_time_ns() {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 61) struct timespec t;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 62)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 63) clock_gettime(CLOCK_MONOTONIC, &t);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 64)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 65) return (u64)t.tv_sec * 1000000000 + t.tv_nsec;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 66) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 67)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 68) static inline void atomic_inc(long *value)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 69) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 70) (void)__atomic_add_fetch(value, 1, __ATOMIC_RELAXED);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 71) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 72)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 73) static inline void atomic_add(long *value, long n)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 74) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 75) (void)__atomic_add_fetch(value, n, __ATOMIC_RELAXED);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 76) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 77)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 78) static inline long atomic_swap(long *value, long n)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 79) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 80) return __atomic_exchange_n(value, n, __ATOMIC_RELAXED);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 81) }