Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0
0002 /* Copyright (c) 2021 Facebook */
0003 
0004 #include <argp.h>
0005 #include "bench.h"
0006 #include "bpf_loop_bench.skel.h"
0007 
0008 /* BPF triggering benchmarks */
0009 static struct ctx {
0010     struct bpf_loop_bench *skel;
0011 } ctx;
0012 
0013 static struct {
0014     __u32 nr_loops;
0015 } args = {
0016     .nr_loops = 10,
0017 };
0018 
0019 enum {
0020     ARG_NR_LOOPS = 4000,
0021 };
0022 
0023 static const struct argp_option opts[] = {
0024     { "nr_loops", ARG_NR_LOOPS, "nr_loops", 0,
0025         "Set number of loops for the bpf_loop helper"},
0026     {},
0027 };
0028 
0029 static error_t parse_arg(int key, char *arg, struct argp_state *state)
0030 {
0031     switch (key) {
0032     case ARG_NR_LOOPS:
0033         args.nr_loops = strtol(arg, NULL, 10);
0034         break;
0035     default:
0036         return ARGP_ERR_UNKNOWN;
0037     }
0038 
0039     return 0;
0040 }
0041 
0042 /* exported into benchmark runner */
0043 const struct argp bench_bpf_loop_argp = {
0044     .options = opts,
0045     .parser = parse_arg,
0046 };
0047 
0048 static void validate(void)
0049 {
0050     if (env.consumer_cnt != 1) {
0051         fprintf(stderr, "benchmark doesn't support multi-consumer!\n");
0052         exit(1);
0053     }
0054 }
0055 
0056 static void *producer(void *input)
0057 {
0058     while (true)
0059         /* trigger the bpf program */
0060         syscall(__NR_getpgid);
0061 
0062     return NULL;
0063 }
0064 
0065 static void *consumer(void *input)
0066 {
0067     return NULL;
0068 }
0069 
0070 static void measure(struct bench_res *res)
0071 {
0072     res->hits = atomic_swap(&ctx.skel->bss->hits, 0);
0073 }
0074 
0075 static void setup(void)
0076 {
0077     struct bpf_link *link;
0078 
0079     setup_libbpf();
0080 
0081     ctx.skel = bpf_loop_bench__open_and_load();
0082     if (!ctx.skel) {
0083         fprintf(stderr, "failed to open skeleton\n");
0084         exit(1);
0085     }
0086 
0087     link = bpf_program__attach(ctx.skel->progs.benchmark);
0088     if (!link) {
0089         fprintf(stderr, "failed to attach program!\n");
0090         exit(1);
0091     }
0092 
0093     ctx.skel->bss->nr_loops = args.nr_loops;
0094 }
0095 
0096 const struct bench bench_bpf_loop = {
0097     .name = "bpf-loop",
0098     .validate = validate,
0099     .setup = setup,
0100     .producer_thread = producer,
0101     .consumer_thread = consumer,
0102     .measure = measure,
0103     .report_progress = ops_report_progress,
0104     .report_final = ops_report_final,
0105 };