xref: /linux/tools/testing/selftests/bpf/benchs/bench_rename.c (revision cdd5b5a9761fd66d17586e4f4ba6588c70e640ea)
14eaf0b5cSAndrii Nakryiko // SPDX-License-Identifier: GPL-2.0
24eaf0b5cSAndrii Nakryiko /* Copyright (c) 2020 Facebook */
34eaf0b5cSAndrii Nakryiko #include <fcntl.h>
44eaf0b5cSAndrii Nakryiko #include "bench.h"
54eaf0b5cSAndrii Nakryiko #include "test_overhead.skel.h"
64eaf0b5cSAndrii Nakryiko 
74eaf0b5cSAndrii Nakryiko /* BPF triggering benchmarks */
84eaf0b5cSAndrii Nakryiko static struct ctx {
94eaf0b5cSAndrii Nakryiko 	struct test_overhead *skel;
104eaf0b5cSAndrii Nakryiko 	struct counter hits;
114eaf0b5cSAndrii Nakryiko 	int fd;
124eaf0b5cSAndrii Nakryiko } ctx;
134eaf0b5cSAndrii Nakryiko 
validate(void)149a93bf3fSHou Tao static void validate(void)
154eaf0b5cSAndrii Nakryiko {
164eaf0b5cSAndrii Nakryiko 	if (env.producer_cnt != 1) {
174eaf0b5cSAndrii Nakryiko 		fprintf(stderr, "benchmark doesn't support multi-producer!\n");
184eaf0b5cSAndrii Nakryiko 		exit(1);
194eaf0b5cSAndrii Nakryiko 	}
20*970308a7SHou Tao 	if (env.consumer_cnt != 0) {
21*970308a7SHou Tao 		fprintf(stderr, "benchmark doesn't support consumer!\n");
224eaf0b5cSAndrii Nakryiko 		exit(1);
234eaf0b5cSAndrii Nakryiko 	}
244eaf0b5cSAndrii Nakryiko }
254eaf0b5cSAndrii Nakryiko 
producer(void * input)264eaf0b5cSAndrii Nakryiko static void *producer(void *input)
274eaf0b5cSAndrii Nakryiko {
284eaf0b5cSAndrii Nakryiko 	char buf[] = "test_overhead";
294eaf0b5cSAndrii Nakryiko 	int err;
304eaf0b5cSAndrii Nakryiko 
314eaf0b5cSAndrii Nakryiko 	while (true) {
324eaf0b5cSAndrii Nakryiko 		err = write(ctx.fd, buf, sizeof(buf));
334eaf0b5cSAndrii Nakryiko 		if (err < 0) {
344eaf0b5cSAndrii Nakryiko 			fprintf(stderr, "write failed\n");
354eaf0b5cSAndrii Nakryiko 			exit(1);
364eaf0b5cSAndrii Nakryiko 		}
374eaf0b5cSAndrii Nakryiko 		atomic_inc(&ctx.hits.value);
384eaf0b5cSAndrii Nakryiko 	}
394eaf0b5cSAndrii Nakryiko }
404eaf0b5cSAndrii Nakryiko 
measure(struct bench_res * res)414eaf0b5cSAndrii Nakryiko static void measure(struct bench_res *res)
424eaf0b5cSAndrii Nakryiko {
434eaf0b5cSAndrii Nakryiko 	res->hits = atomic_swap(&ctx.hits.value, 0);
444eaf0b5cSAndrii Nakryiko }
454eaf0b5cSAndrii Nakryiko 
setup_ctx(void)469a93bf3fSHou Tao static void setup_ctx(void)
474eaf0b5cSAndrii Nakryiko {
484eaf0b5cSAndrii Nakryiko 	setup_libbpf();
494eaf0b5cSAndrii Nakryiko 
504eaf0b5cSAndrii Nakryiko 	ctx.skel = test_overhead__open_and_load();
514eaf0b5cSAndrii Nakryiko 	if (!ctx.skel) {
524eaf0b5cSAndrii Nakryiko 		fprintf(stderr, "failed to open skeleton\n");
534eaf0b5cSAndrii Nakryiko 		exit(1);
544eaf0b5cSAndrii Nakryiko 	}
554eaf0b5cSAndrii Nakryiko 
564eaf0b5cSAndrii Nakryiko 	ctx.fd = open("/proc/self/comm", O_WRONLY|O_TRUNC);
574eaf0b5cSAndrii Nakryiko 	if (ctx.fd < 0) {
584eaf0b5cSAndrii Nakryiko 		fprintf(stderr, "failed to open /proc/self/comm: %d\n", -errno);
594eaf0b5cSAndrii Nakryiko 		exit(1);
604eaf0b5cSAndrii Nakryiko 	}
614eaf0b5cSAndrii Nakryiko }
624eaf0b5cSAndrii Nakryiko 
attach_bpf(struct bpf_program * prog)634eaf0b5cSAndrii Nakryiko static void attach_bpf(struct bpf_program *prog)
644eaf0b5cSAndrii Nakryiko {
654eaf0b5cSAndrii Nakryiko 	struct bpf_link *link;
664eaf0b5cSAndrii Nakryiko 
674eaf0b5cSAndrii Nakryiko 	link = bpf_program__attach(prog);
68bad2e478SAndrii Nakryiko 	if (!link) {
694eaf0b5cSAndrii Nakryiko 		fprintf(stderr, "failed to attach program!\n");
704eaf0b5cSAndrii Nakryiko 		exit(1);
714eaf0b5cSAndrii Nakryiko 	}
724eaf0b5cSAndrii Nakryiko }
734eaf0b5cSAndrii Nakryiko 
setup_base(void)749a93bf3fSHou Tao static void setup_base(void)
754eaf0b5cSAndrii Nakryiko {
764eaf0b5cSAndrii Nakryiko 	setup_ctx();
774eaf0b5cSAndrii Nakryiko }
784eaf0b5cSAndrii Nakryiko 
setup_kprobe(void)799a93bf3fSHou Tao static void setup_kprobe(void)
804eaf0b5cSAndrii Nakryiko {
814eaf0b5cSAndrii Nakryiko 	setup_ctx();
824eaf0b5cSAndrii Nakryiko 	attach_bpf(ctx.skel->progs.prog1);
834eaf0b5cSAndrii Nakryiko }
844eaf0b5cSAndrii Nakryiko 
setup_kretprobe(void)859a93bf3fSHou Tao static void setup_kretprobe(void)
864eaf0b5cSAndrii Nakryiko {
874eaf0b5cSAndrii Nakryiko 	setup_ctx();
884eaf0b5cSAndrii Nakryiko 	attach_bpf(ctx.skel->progs.prog2);
894eaf0b5cSAndrii Nakryiko }
904eaf0b5cSAndrii Nakryiko 
setup_rawtp(void)919a93bf3fSHou Tao static void setup_rawtp(void)
924eaf0b5cSAndrii Nakryiko {
934eaf0b5cSAndrii Nakryiko 	setup_ctx();
944eaf0b5cSAndrii Nakryiko 	attach_bpf(ctx.skel->progs.prog3);
954eaf0b5cSAndrii Nakryiko }
964eaf0b5cSAndrii Nakryiko 
setup_fentry(void)979a93bf3fSHou Tao static void setup_fentry(void)
984eaf0b5cSAndrii Nakryiko {
994eaf0b5cSAndrii Nakryiko 	setup_ctx();
1004eaf0b5cSAndrii Nakryiko 	attach_bpf(ctx.skel->progs.prog4);
1014eaf0b5cSAndrii Nakryiko }
1024eaf0b5cSAndrii Nakryiko 
setup_fexit(void)1039a93bf3fSHou Tao static void setup_fexit(void)
1044eaf0b5cSAndrii Nakryiko {
1054eaf0b5cSAndrii Nakryiko 	setup_ctx();
1064eaf0b5cSAndrii Nakryiko 	attach_bpf(ctx.skel->progs.prog5);
1074eaf0b5cSAndrii Nakryiko }
1084eaf0b5cSAndrii Nakryiko 
1094eaf0b5cSAndrii Nakryiko const struct bench bench_rename_base = {
1104eaf0b5cSAndrii Nakryiko 	.name = "rename-base",
1114eaf0b5cSAndrii Nakryiko 	.validate = validate,
1124eaf0b5cSAndrii Nakryiko 	.setup = setup_base,
1134eaf0b5cSAndrii Nakryiko 	.producer_thread = producer,
1144eaf0b5cSAndrii Nakryiko 	.measure = measure,
1154eaf0b5cSAndrii Nakryiko 	.report_progress = hits_drops_report_progress,
1164eaf0b5cSAndrii Nakryiko 	.report_final = hits_drops_report_final,
1174eaf0b5cSAndrii Nakryiko };
1184eaf0b5cSAndrii Nakryiko 
1194eaf0b5cSAndrii Nakryiko const struct bench bench_rename_kprobe = {
1204eaf0b5cSAndrii Nakryiko 	.name = "rename-kprobe",
1214eaf0b5cSAndrii Nakryiko 	.validate = validate,
1224eaf0b5cSAndrii Nakryiko 	.setup = setup_kprobe,
1234eaf0b5cSAndrii Nakryiko 	.producer_thread = producer,
1244eaf0b5cSAndrii Nakryiko 	.measure = measure,
1254eaf0b5cSAndrii Nakryiko 	.report_progress = hits_drops_report_progress,
1264eaf0b5cSAndrii Nakryiko 	.report_final = hits_drops_report_final,
1274eaf0b5cSAndrii Nakryiko };
1284eaf0b5cSAndrii Nakryiko 
1294eaf0b5cSAndrii Nakryiko const struct bench bench_rename_kretprobe = {
1304eaf0b5cSAndrii Nakryiko 	.name = "rename-kretprobe",
1314eaf0b5cSAndrii Nakryiko 	.validate = validate,
1324eaf0b5cSAndrii Nakryiko 	.setup = setup_kretprobe,
1334eaf0b5cSAndrii Nakryiko 	.producer_thread = producer,
1344eaf0b5cSAndrii Nakryiko 	.measure = measure,
1354eaf0b5cSAndrii Nakryiko 	.report_progress = hits_drops_report_progress,
1364eaf0b5cSAndrii Nakryiko 	.report_final = hits_drops_report_final,
1374eaf0b5cSAndrii Nakryiko };
1384eaf0b5cSAndrii Nakryiko 
1394eaf0b5cSAndrii Nakryiko const struct bench bench_rename_rawtp = {
1404eaf0b5cSAndrii Nakryiko 	.name = "rename-rawtp",
1414eaf0b5cSAndrii Nakryiko 	.validate = validate,
1424eaf0b5cSAndrii Nakryiko 	.setup = setup_rawtp,
1434eaf0b5cSAndrii Nakryiko 	.producer_thread = producer,
1444eaf0b5cSAndrii Nakryiko 	.measure = measure,
1454eaf0b5cSAndrii Nakryiko 	.report_progress = hits_drops_report_progress,
1464eaf0b5cSAndrii Nakryiko 	.report_final = hits_drops_report_final,
1474eaf0b5cSAndrii Nakryiko };
1484eaf0b5cSAndrii Nakryiko 
1494eaf0b5cSAndrii Nakryiko const struct bench bench_rename_fentry = {
1504eaf0b5cSAndrii Nakryiko 	.name = "rename-fentry",
1514eaf0b5cSAndrii Nakryiko 	.validate = validate,
1524eaf0b5cSAndrii Nakryiko 	.setup = setup_fentry,
1534eaf0b5cSAndrii Nakryiko 	.producer_thread = producer,
1544eaf0b5cSAndrii Nakryiko 	.measure = measure,
1554eaf0b5cSAndrii Nakryiko 	.report_progress = hits_drops_report_progress,
1564eaf0b5cSAndrii Nakryiko 	.report_final = hits_drops_report_final,
1574eaf0b5cSAndrii Nakryiko };
1584eaf0b5cSAndrii Nakryiko 
1594eaf0b5cSAndrii Nakryiko const struct bench bench_rename_fexit = {
1604eaf0b5cSAndrii Nakryiko 	.name = "rename-fexit",
1614eaf0b5cSAndrii Nakryiko 	.validate = validate,
1624eaf0b5cSAndrii Nakryiko 	.setup = setup_fexit,
1634eaf0b5cSAndrii Nakryiko 	.producer_thread = producer,
1644eaf0b5cSAndrii Nakryiko 	.measure = measure,
1654eaf0b5cSAndrii Nakryiko 	.report_progress = hits_drops_report_progress,
1664eaf0b5cSAndrii Nakryiko 	.report_final = hits_drops_report_final,
1674eaf0b5cSAndrii Nakryiko };
168