Path: blob/master/tools/testing/selftests/bpf/benchs/bench_strncmp.c
29270 views
// SPDX-License-Identifier: GPL-2.01/* Copyright (C) 2021. Huawei Technologies Co., Ltd */2#include <argp.h>3#include "bench.h"4#include "strncmp_bench.skel.h"56static struct strncmp_ctx {7struct strncmp_bench *skel;8} ctx;910static struct strncmp_args {11u32 cmp_str_len;12} args = {13.cmp_str_len = 32,14};1516enum {17ARG_CMP_STR_LEN = 5000,18};1920static const struct argp_option opts[] = {21{ "cmp-str-len", ARG_CMP_STR_LEN, "CMP_STR_LEN", 0,22"Set the length of compared string" },23{},24};2526static error_t strncmp_parse_arg(int key, char *arg, struct argp_state *state)27{28switch (key) {29case ARG_CMP_STR_LEN:30args.cmp_str_len = strtoul(arg, NULL, 10);31if (!args.cmp_str_len ||32args.cmp_str_len >= sizeof(ctx.skel->bss->str)) {33fprintf(stderr, "Invalid cmp str len (limit %zu)\n",34sizeof(ctx.skel->bss->str));35argp_usage(state);36}37break;38default:39return ARGP_ERR_UNKNOWN;40}4142return 0;43}4445const struct argp bench_strncmp_argp = {46.options = opts,47.parser = strncmp_parse_arg,48};4950static void strncmp_validate(void)51{52if (env.consumer_cnt != 0) {53fprintf(stderr, "strncmp benchmark doesn't support consumer!\n");54exit(1);55}56}5758static void strncmp_setup(void)59{60int err;61char *target;62size_t i, sz;6364sz = sizeof(ctx.skel->rodata->target);65if (!sz || sz < sizeof(ctx.skel->bss->str)) {66fprintf(stderr, "invalid string size (target %zu, src %zu)\n",67sz, sizeof(ctx.skel->bss->str));68exit(1);69}7071setup_libbpf();7273ctx.skel = strncmp_bench__open();74if (!ctx.skel) {75fprintf(stderr, "failed to open skeleton\n");76exit(1);77}7879srandom(time(NULL));80target = ctx.skel->rodata->target;81for (i = 0; i < sz - 1; i++)82target[i] = '1' + random() % 9;83target[sz - 1] = '\0';8485ctx.skel->rodata->cmp_str_len = args.cmp_str_len;8687memcpy(ctx.skel->bss->str, target, args.cmp_str_len);88ctx.skel->bss->str[args.cmp_str_len] = '\0';89/* Make bss->str < rodata->target */90ctx.skel->bss->str[args.cmp_str_len - 1] -= 1;9192err = strncmp_bench__load(ctx.skel);93if (err) {94fprintf(stderr, "failed to load skeleton\n");95strncmp_bench__destroy(ctx.skel);96exit(1);97}98}99100static void strncmp_attach_prog(struct bpf_program *prog)101{102struct bpf_link *link;103104link = bpf_program__attach(prog);105if (!link) {106fprintf(stderr, "failed to attach program!\n");107exit(1);108}109}110111static void strncmp_no_helper_setup(void)112{113strncmp_setup();114strncmp_attach_prog(ctx.skel->progs.strncmp_no_helper);115}116117static void strncmp_helper_setup(void)118{119strncmp_setup();120strncmp_attach_prog(ctx.skel->progs.strncmp_helper);121}122123static void *strncmp_producer(void *ctx)124{125while (true)126(void)syscall(__NR_getpgid);127return NULL;128}129130static void strncmp_measure(struct bench_res *res)131{132res->hits = atomic_swap(&ctx.skel->bss->hits, 0);133}134135const struct bench bench_strncmp_no_helper = {136.name = "strncmp-no-helper",137.argp = &bench_strncmp_argp,138.validate = strncmp_validate,139.setup = strncmp_no_helper_setup,140.producer_thread = strncmp_producer,141.measure = strncmp_measure,142.report_progress = hits_drops_report_progress,143.report_final = hits_drops_report_final,144};145146const struct bench bench_strncmp_helper = {147.name = "strncmp-helper",148.argp = &bench_strncmp_argp,149.validate = strncmp_validate,150.setup = strncmp_helper_setup,151.producer_thread = strncmp_producer,152.measure = strncmp_measure,153.report_progress = hits_drops_report_progress,154.report_final = hits_drops_report_final,155};156157158