1ec151037SJoanne Koong // SPDX-License-Identifier: GPL-2.0
2ec151037SJoanne Koong /* Copyright (c) 2021 Facebook */
3ec151037SJoanne Koong
4ec151037SJoanne Koong #include <argp.h>
5ec151037SJoanne Koong #include "bench.h"
6ec151037SJoanne Koong #include "bpf_loop_bench.skel.h"
7ec151037SJoanne Koong
8ec151037SJoanne Koong /* BPF triggering benchmarks */
9ec151037SJoanne Koong static struct ctx {
10ec151037SJoanne Koong struct bpf_loop_bench *skel;
11ec151037SJoanne Koong } ctx;
12ec151037SJoanne Koong
13ec151037SJoanne Koong static struct {
14ec151037SJoanne Koong __u32 nr_loops;
15ec151037SJoanne Koong } args = {
16ec151037SJoanne Koong .nr_loops = 10,
17ec151037SJoanne Koong };
18ec151037SJoanne Koong
19ec151037SJoanne Koong enum {
20ec151037SJoanne Koong ARG_NR_LOOPS = 4000,
21ec151037SJoanne Koong };
22ec151037SJoanne Koong
23ec151037SJoanne Koong static const struct argp_option opts[] = {
24ec151037SJoanne Koong { "nr_loops", ARG_NR_LOOPS, "nr_loops", 0,
25ec151037SJoanne Koong "Set number of loops for the bpf_loop helper"},
26ec151037SJoanne Koong {},
27ec151037SJoanne Koong };
28ec151037SJoanne Koong
parse_arg(int key,char * arg,struct argp_state * state)29ec151037SJoanne Koong static error_t parse_arg(int key, char *arg, struct argp_state *state)
30ec151037SJoanne Koong {
31ec151037SJoanne Koong switch (key) {
32ec151037SJoanne Koong case ARG_NR_LOOPS:
33ec151037SJoanne Koong args.nr_loops = strtol(arg, NULL, 10);
34ec151037SJoanne Koong break;
35ec151037SJoanne Koong default:
36ec151037SJoanne Koong return ARGP_ERR_UNKNOWN;
37ec151037SJoanne Koong }
38ec151037SJoanne Koong
39ec151037SJoanne Koong return 0;
40ec151037SJoanne Koong }
41ec151037SJoanne Koong
42ec151037SJoanne Koong /* exported into benchmark runner */
43ec151037SJoanne Koong const struct argp bench_bpf_loop_argp = {
44ec151037SJoanne Koong .options = opts,
45ec151037SJoanne Koong .parser = parse_arg,
46ec151037SJoanne Koong };
47ec151037SJoanne Koong
validate(void)48ec151037SJoanne Koong static void validate(void)
49ec151037SJoanne Koong {
50*970308a7SHou Tao if (env.consumer_cnt != 0) {
51*970308a7SHou Tao fprintf(stderr, "benchmark doesn't support consumer!\n");
52ec151037SJoanne Koong exit(1);
53ec151037SJoanne Koong }
54ec151037SJoanne Koong }
55ec151037SJoanne Koong
producer(void * input)56ec151037SJoanne Koong static void *producer(void *input)
57ec151037SJoanne Koong {
58ec151037SJoanne Koong while (true)
59ec151037SJoanne Koong /* trigger the bpf program */
60ec151037SJoanne Koong syscall(__NR_getpgid);
61ec151037SJoanne Koong
62ec151037SJoanne Koong return NULL;
63ec151037SJoanne Koong }
64ec151037SJoanne Koong
measure(struct bench_res * res)65ec151037SJoanne Koong static void measure(struct bench_res *res)
66ec151037SJoanne Koong {
67ec151037SJoanne Koong res->hits = atomic_swap(&ctx.skel->bss->hits, 0);
68ec151037SJoanne Koong }
69ec151037SJoanne Koong
setup(void)70ec151037SJoanne Koong static void setup(void)
71ec151037SJoanne Koong {
72ec151037SJoanne Koong struct bpf_link *link;
73ec151037SJoanne Koong
74ec151037SJoanne Koong setup_libbpf();
75ec151037SJoanne Koong
76ec151037SJoanne Koong ctx.skel = bpf_loop_bench__open_and_load();
77ec151037SJoanne Koong if (!ctx.skel) {
78ec151037SJoanne Koong fprintf(stderr, "failed to open skeleton\n");
79ec151037SJoanne Koong exit(1);
80ec151037SJoanne Koong }
81ec151037SJoanne Koong
82ec151037SJoanne Koong link = bpf_program__attach(ctx.skel->progs.benchmark);
83ec151037SJoanne Koong if (!link) {
84ec151037SJoanne Koong fprintf(stderr, "failed to attach program!\n");
85ec151037SJoanne Koong exit(1);
86ec151037SJoanne Koong }
87ec151037SJoanne Koong
88ec151037SJoanne Koong ctx.skel->bss->nr_loops = args.nr_loops;
89ec151037SJoanne Koong }
90ec151037SJoanne Koong
91ec151037SJoanne Koong const struct bench bench_bpf_loop = {
92ec151037SJoanne Koong .name = "bpf-loop",
9322ff7aeaSAnton Protopopov .argp = &bench_bpf_loop_argp,
94ec151037SJoanne Koong .validate = validate,
95ec151037SJoanne Koong .setup = setup,
96ec151037SJoanne Koong .producer_thread = producer,
97ec151037SJoanne Koong .measure = measure,
98ec151037SJoanne Koong .report_progress = ops_report_progress,
99ec151037SJoanne Koong .report_final = ops_report_final,
100ec151037SJoanne Koong };
101