Path: blob/master/tools/testing/selftests/bpf/benchs/bench_rename.c
29270 views
// SPDX-License-Identifier: GPL-2.01/* Copyright (c) 2020 Facebook */2#include <fcntl.h>3#include "bench.h"4#include "test_overhead.skel.h"56/* BPF triggering benchmarks */7static struct ctx {8struct test_overhead *skel;9struct counter hits;10int fd;11} ctx;1213static void validate(void)14{15if (env.producer_cnt != 1) {16fprintf(stderr, "benchmark doesn't support multi-producer!\n");17exit(1);18}19if (env.consumer_cnt != 0) {20fprintf(stderr, "benchmark doesn't support consumer!\n");21exit(1);22}23}2425static void *producer(void *input)26{27char buf[] = "test_overhead";28int err;2930while (true) {31err = write(ctx.fd, buf, sizeof(buf));32if (err < 0) {33fprintf(stderr, "write failed\n");34exit(1);35}36atomic_inc(&ctx.hits.value);37}38}3940static void measure(struct bench_res *res)41{42res->hits = atomic_swap(&ctx.hits.value, 0);43}4445static void setup_ctx(void)46{47setup_libbpf();4849ctx.skel = test_overhead__open_and_load();50if (!ctx.skel) {51fprintf(stderr, "failed to open skeleton\n");52exit(1);53}5455ctx.fd = open("/proc/self/comm", O_WRONLY|O_TRUNC);56if (ctx.fd < 0) {57fprintf(stderr, "failed to open /proc/self/comm: %d\n", -errno);58exit(1);59}60}6162static void attach_bpf(struct bpf_program *prog)63{64struct bpf_link *link;6566link = bpf_program__attach(prog);67if (!link) {68fprintf(stderr, "failed to attach program!\n");69exit(1);70}71}7273static void setup_base(void)74{75setup_ctx();76}7778static void setup_kprobe(void)79{80setup_ctx();81attach_bpf(ctx.skel->progs.prog1);82}8384static void setup_kretprobe(void)85{86setup_ctx();87attach_bpf(ctx.skel->progs.prog2);88}8990static void setup_rawtp(void)91{92setup_ctx();93attach_bpf(ctx.skel->progs.prog3);94}9596static void setup_fentry(void)97{98setup_ctx();99attach_bpf(ctx.skel->progs.prog4);100}101102static void setup_fexit(void)103{104setup_ctx();105attach_bpf(ctx.skel->progs.prog5);106}107108const struct bench bench_rename_base = {109.name = "rename-base",110.validate = validate,111.setup = setup_base,112.producer_thread = producer,113.measure = measure,114.report_progress = hits_drops_report_progress,115.report_final = hits_drops_report_final,116};117118const struct bench bench_rename_kprobe = {119.name = "rename-kprobe",120.validate = validate,121.setup = setup_kprobe,122.producer_thread = producer,123.measure = measure,124.report_progress = hits_drops_report_progress,125.report_final = hits_drops_report_final,126};127128const struct bench bench_rename_kretprobe = {129.name = "rename-kretprobe",130.validate = validate,131.setup = setup_kretprobe,132.producer_thread = producer,133.measure = measure,134.report_progress = hits_drops_report_progress,135.report_final = hits_drops_report_final,136};137138const struct bench bench_rename_rawtp = {139.name = "rename-rawtp",140.validate = validate,141.setup = setup_rawtp,142.producer_thread = producer,143.measure = measure,144.report_progress = hits_drops_report_progress,145.report_final = hits_drops_report_final,146};147148const struct bench bench_rename_fentry = {149.name = "rename-fentry",150.validate = validate,151.setup = setup_fentry,152.producer_thread = producer,153.measure = measure,154.report_progress = hits_drops_report_progress,155.report_final = hits_drops_report_final,156};157158const struct bench bench_rename_fexit = {159.name = "rename-fexit",160.validate = validate,161.setup = setup_fexit,162.producer_thread = producer,163.measure = measure,164.report_progress = hits_drops_report_progress,165.report_final = hits_drops_report_final,166};167168169