0001
0002
0003 #include <fcntl.h>
0004 #include "bench.h"
0005 #include "test_overhead.skel.h"
0006
0007
0008 static struct ctx {
0009 struct test_overhead *skel;
0010 struct counter hits;
0011 int fd;
0012 } ctx;
0013
0014 static void validate(void)
0015 {
0016 if (env.producer_cnt != 1) {
0017 fprintf(stderr, "benchmark doesn't support multi-producer!\n");
0018 exit(1);
0019 }
0020 if (env.consumer_cnt != 1) {
0021 fprintf(stderr, "benchmark doesn't support multi-consumer!\n");
0022 exit(1);
0023 }
0024 }
0025
0026 static void *producer(void *input)
0027 {
0028 char buf[] = "test_overhead";
0029 int err;
0030
0031 while (true) {
0032 err = write(ctx.fd, buf, sizeof(buf));
0033 if (err < 0) {
0034 fprintf(stderr, "write failed\n");
0035 exit(1);
0036 }
0037 atomic_inc(&ctx.hits.value);
0038 }
0039 }
0040
0041 static void measure(struct bench_res *res)
0042 {
0043 res->hits = atomic_swap(&ctx.hits.value, 0);
0044 }
0045
0046 static void setup_ctx(void)
0047 {
0048 setup_libbpf();
0049
0050 ctx.skel = test_overhead__open_and_load();
0051 if (!ctx.skel) {
0052 fprintf(stderr, "failed to open skeleton\n");
0053 exit(1);
0054 }
0055
0056 ctx.fd = open("/proc/self/comm", O_WRONLY|O_TRUNC);
0057 if (ctx.fd < 0) {
0058 fprintf(stderr, "failed to open /proc/self/comm: %d\n", -errno);
0059 exit(1);
0060 }
0061 }
0062
0063 static void attach_bpf(struct bpf_program *prog)
0064 {
0065 struct bpf_link *link;
0066
0067 link = bpf_program__attach(prog);
0068 if (!link) {
0069 fprintf(stderr, "failed to attach program!\n");
0070 exit(1);
0071 }
0072 }
0073
0074 static void setup_base(void)
0075 {
0076 setup_ctx();
0077 }
0078
0079 static void setup_kprobe(void)
0080 {
0081 setup_ctx();
0082 attach_bpf(ctx.skel->progs.prog1);
0083 }
0084
0085 static void setup_kretprobe(void)
0086 {
0087 setup_ctx();
0088 attach_bpf(ctx.skel->progs.prog2);
0089 }
0090
0091 static void setup_rawtp(void)
0092 {
0093 setup_ctx();
0094 attach_bpf(ctx.skel->progs.prog3);
0095 }
0096
0097 static void setup_fentry(void)
0098 {
0099 setup_ctx();
0100 attach_bpf(ctx.skel->progs.prog4);
0101 }
0102
0103 static void setup_fexit(void)
0104 {
0105 setup_ctx();
0106 attach_bpf(ctx.skel->progs.prog5);
0107 }
0108
0109 static void *consumer(void *input)
0110 {
0111 return NULL;
0112 }
0113
0114 const struct bench bench_rename_base = {
0115 .name = "rename-base",
0116 .validate = validate,
0117 .setup = setup_base,
0118 .producer_thread = producer,
0119 .consumer_thread = consumer,
0120 .measure = measure,
0121 .report_progress = hits_drops_report_progress,
0122 .report_final = hits_drops_report_final,
0123 };
0124
0125 const struct bench bench_rename_kprobe = {
0126 .name = "rename-kprobe",
0127 .validate = validate,
0128 .setup = setup_kprobe,
0129 .producer_thread = producer,
0130 .consumer_thread = consumer,
0131 .measure = measure,
0132 .report_progress = hits_drops_report_progress,
0133 .report_final = hits_drops_report_final,
0134 };
0135
0136 const struct bench bench_rename_kretprobe = {
0137 .name = "rename-kretprobe",
0138 .validate = validate,
0139 .setup = setup_kretprobe,
0140 .producer_thread = producer,
0141 .consumer_thread = consumer,
0142 .measure = measure,
0143 .report_progress = hits_drops_report_progress,
0144 .report_final = hits_drops_report_final,
0145 };
0146
0147 const struct bench bench_rename_rawtp = {
0148 .name = "rename-rawtp",
0149 .validate = validate,
0150 .setup = setup_rawtp,
0151 .producer_thread = producer,
0152 .consumer_thread = consumer,
0153 .measure = measure,
0154 .report_progress = hits_drops_report_progress,
0155 .report_final = hits_drops_report_final,
0156 };
0157
0158 const struct bench bench_rename_fentry = {
0159 .name = "rename-fentry",
0160 .validate = validate,
0161 .setup = setup_fentry,
0162 .producer_thread = producer,
0163 .consumer_thread = consumer,
0164 .measure = measure,
0165 .report_progress = hits_drops_report_progress,
0166 .report_final = hits_drops_report_final,
0167 };
0168
0169 const struct bench bench_rename_fexit = {
0170 .name = "rename-fexit",
0171 .validate = validate,
0172 .setup = setup_fexit,
0173 .producer_thread = producer,
0174 .consumer_thread = consumer,
0175 .measure = measure,
0176 .report_progress = hits_drops_report_progress,
0177 .report_final = hits_drops_report_final,
0178 };