Path: blob/master/tools/testing/selftests/bpf/benchs/bench_bpf_loop.c
29270 views
// SPDX-License-Identifier: GPL-2.01/* Copyright (c) 2021 Facebook */23#include <argp.h>4#include "bench.h"5#include "bpf_loop_bench.skel.h"67/* BPF triggering benchmarks */8static struct ctx {9struct bpf_loop_bench *skel;10} ctx;1112static struct {13__u32 nr_loops;14} args = {15.nr_loops = 10,16};1718enum {19ARG_NR_LOOPS = 4000,20};2122static const struct argp_option opts[] = {23{ "nr_loops", ARG_NR_LOOPS, "nr_loops", 0,24"Set number of loops for the bpf_loop helper"},25{},26};2728static error_t parse_arg(int key, char *arg, struct argp_state *state)29{30switch (key) {31case ARG_NR_LOOPS:32args.nr_loops = strtol(arg, NULL, 10);33break;34default:35return ARGP_ERR_UNKNOWN;36}3738return 0;39}4041/* exported into benchmark runner */42const struct argp bench_bpf_loop_argp = {43.options = opts,44.parser = parse_arg,45};4647static void validate(void)48{49if (env.consumer_cnt != 0) {50fprintf(stderr, "benchmark doesn't support consumer!\n");51exit(1);52}53}5455static void *producer(void *input)56{57while (true)58/* trigger the bpf program */59syscall(__NR_getpgid);6061return NULL;62}6364static void measure(struct bench_res *res)65{66res->hits = atomic_swap(&ctx.skel->bss->hits, 0);67}6869static void setup(void)70{71struct bpf_link *link;7273setup_libbpf();7475ctx.skel = bpf_loop_bench__open_and_load();76if (!ctx.skel) {77fprintf(stderr, "failed to open skeleton\n");78exit(1);79}8081link = bpf_program__attach(ctx.skel->progs.benchmark);82if (!link) {83fprintf(stderr, "failed to attach program!\n");84exit(1);85}8687ctx.skel->bss->nr_loops = args.nr_loops;88}8990const struct bench bench_bpf_loop = {91.name = "bpf-loop",92.argp = &bench_bpf_loop_argp,93.validate = validate,94.setup = setup,95.producer_thread = producer,96.measure = measure,97.report_progress = ops_report_progress,98.report_final = ops_report_final,99};100101102