selftest/bpf: Add BPF triggering benchmark
authorAndrii Nakryiko <andriin@fb.com>
Tue, 12 May 2020 19:24:45 +0000 (12:24 -0700)
committerAlexei Starovoitov <ast@kernel.org>
Wed, 13 May 2020 19:19:38 +0000 (12:19 -0700)
It is sometimes desirable to be able to trigger BPF program from user-space
with minimal overhead. sys_enter would seem to be a good candidate, yet in
a lot of cases there will be a lot of noise from syscalls triggered by other
processes on the system. So while searching for low-overhead alternative, I've
stumbled upon getpgid() syscall, which seems to be specific enough to not
suffer from accidental syscall by other apps.

This set of benchmarks compares tp, raw_tp w/ filtering by syscall ID, kprobe,
fentry and fmod_ret with returning error (so that syscall would not be
executed), to determine the lowest-overhead way. Here are results on my
machine (using benchs/run_bench_trigger.sh script):

  base      :    9.200 ± 0.319M/s
  tp        :    6.690 ± 0.125M/s
  rawtp     :    8.571 ± 0.214M/s
  kprobe    :    6.431 ± 0.048M/s
  fentry    :    8.955 ± 0.241M/s
  fmodret   :    8.903 ± 0.135M/s

So it seems like fmodret doesn't give much benefit for such lightweight
syscall. Raw tracepoint is pretty decent despite additional filtering logic,
but it will be called for any other syscall in the system, which rules it out.
Fentry, though, seems to be adding the least amoung of overhead and achieves
97.3% of performance of baseline no-BPF-attached syscall.

Using getpgid() seems to be preferable to set_task_comm() approach from
test_overhead, as it's about 2.35x faster in a baseline performance.

Signed-off-by: Andrii Nakryiko <andriin@fb.com>
Signed-off-by: Alexei Starovoitov <ast@kernel.org>
Acked-by: John Fastabend <john.fastabend@gmail.com>
Acked-by: Yonghong Song <yhs@fb.com>
Link: https://lore.kernel.org/bpf/20200512192445.2351848-5-andriin@fb.com
tools/testing/selftests/bpf/Makefile
tools/testing/selftests/bpf/bench.c
tools/testing/selftests/bpf/benchs/bench_trigger.c [new file with mode: 0644]
tools/testing/selftests/bpf/benchs/run_bench_trigger.sh [new file with mode: 0755]
tools/testing/selftests/bpf/progs/trigger_bench.c [new file with mode: 0644]

index 1a079e91482f49fd1411deb0e39b98034b4e9187..e716e931d0c967d0feb5212e760b2fd35de03ebb 100644 (file)
@@ -412,11 +412,13 @@ $(OUTPUT)/bench_%.o: benchs/bench_%.c bench.h
        $(call msg,CC,,$@)
        $(CC) $(CFLAGS) -c $(filter %.c,$^) $(LDLIBS) -o $@
 $(OUTPUT)/bench_rename.o: $(OUTPUT)/test_overhead.skel.h
+$(OUTPUT)/bench_trigger.o: $(OUTPUT)/trigger_bench.skel.h
 $(OUTPUT)/bench.o: bench.h testing_helpers.h
 $(OUTPUT)/bench: LDLIBS += -lm
 $(OUTPUT)/bench: $(OUTPUT)/bench.o $(OUTPUT)/testing_helpers.o \
                 $(OUTPUT)/bench_count.o \
-                $(OUTPUT)/bench_rename.o
+                $(OUTPUT)/bench_rename.o \
+                $(OUTPUT)/bench_trigger.o
        $(call msg,BINARY,,$@)
        $(CC) $(LDFLAGS) -o $@ $(filter %.a %.o,$^) $(LDLIBS)
 
index c9e8b7dbaf66df9bdc363c8f609f53d1c773ec5c..8c0dfbfe60885d21d684245a313292cc86418aa6 100644 (file)
@@ -304,6 +304,12 @@ extern const struct bench bench_rename_rawtp;
 extern const struct bench bench_rename_fentry;
 extern const struct bench bench_rename_fexit;
 extern const struct bench bench_rename_fmodret;
+extern const struct bench bench_trig_base;
+extern const struct bench bench_trig_tp;
+extern const struct bench bench_trig_rawtp;
+extern const struct bench bench_trig_kprobe;
+extern const struct bench bench_trig_fentry;
+extern const struct bench bench_trig_fmodret;
 
 static const struct bench *benchs[] = {
        &bench_count_global,
@@ -315,6 +321,12 @@ static const struct bench *benchs[] = {
        &bench_rename_fentry,
        &bench_rename_fexit,
        &bench_rename_fmodret,
+       &bench_trig_base,
+       &bench_trig_tp,
+       &bench_trig_rawtp,
+       &bench_trig_kprobe,
+       &bench_trig_fentry,
+       &bench_trig_fmodret,
 };
 
 static void setup_benchmark()
diff --git a/tools/testing/selftests/bpf/benchs/bench_trigger.c b/tools/testing/selftests/bpf/benchs/bench_trigger.c
new file mode 100644 (file)
index 0000000..49c2283
--- /dev/null
@@ -0,0 +1,167 @@
+// SPDX-License-Identifier: GPL-2.0
+/* Copyright (c) 2020 Facebook */
+#include "bench.h"
+#include "trigger_bench.skel.h"
+
+/* BPF triggering benchmarks */
+static struct trigger_ctx {
+       struct trigger_bench *skel;
+} ctx;
+
+static struct counter base_hits;
+
+static void trigger_validate()
+{
+       if (env.consumer_cnt != 1) {
+               fprintf(stderr, "benchmark doesn't support multi-consumer!\n");
+               exit(1);
+       }
+}
+
+static void *trigger_base_producer(void *input)
+{
+       while (true) {
+               (void)syscall(__NR_getpgid);
+               atomic_inc(&base_hits.value);
+       }
+       return NULL;
+}
+
+static void trigger_base_measure(struct bench_res *res)
+{
+       res->hits = atomic_swap(&base_hits.value, 0);
+}
+
+static void *trigger_producer(void *input)
+{
+       while (true)
+               (void)syscall(__NR_getpgid);
+       return NULL;
+}
+
+static void trigger_measure(struct bench_res *res)
+{
+       res->hits = atomic_swap(&ctx.skel->bss->hits, 0);
+}
+
+static void setup_ctx()
+{
+       setup_libbpf();
+
+       ctx.skel = trigger_bench__open_and_load();
+       if (!ctx.skel) {
+               fprintf(stderr, "failed to open skeleton\n");
+               exit(1);
+       }
+}
+
+static void attach_bpf(struct bpf_program *prog)
+{
+       struct bpf_link *link;
+
+       link = bpf_program__attach(prog);
+       if (IS_ERR(link)) {
+               fprintf(stderr, "failed to attach program!\n");
+               exit(1);
+       }
+}
+
+static void trigger_tp_setup()
+{
+       setup_ctx();
+       attach_bpf(ctx.skel->progs.bench_trigger_tp);
+}
+
+static void trigger_rawtp_setup()
+{
+       setup_ctx();
+       attach_bpf(ctx.skel->progs.bench_trigger_raw_tp);
+}
+
+static void trigger_kprobe_setup()
+{
+       setup_ctx();
+       attach_bpf(ctx.skel->progs.bench_trigger_kprobe);
+}
+
+static void trigger_fentry_setup()
+{
+       setup_ctx();
+       attach_bpf(ctx.skel->progs.bench_trigger_fentry);
+}
+
+static void trigger_fmodret_setup()
+{
+       setup_ctx();
+       attach_bpf(ctx.skel->progs.bench_trigger_fmodret);
+}
+
+static void *trigger_consumer(void *input)
+{
+       return NULL;
+}
+
+const struct bench bench_trig_base = {
+       .name = "trig-base",
+       .validate = trigger_validate,
+       .producer_thread = trigger_base_producer,
+       .consumer_thread = trigger_consumer,
+       .measure = trigger_base_measure,
+       .report_progress = hits_drops_report_progress,
+       .report_final = hits_drops_report_final,
+};
+
+const struct bench bench_trig_tp = {
+       .name = "trig-tp",
+       .validate = trigger_validate,
+       .setup = trigger_tp_setup,
+       .producer_thread = trigger_producer,
+       .consumer_thread = trigger_consumer,
+       .measure = trigger_measure,
+       .report_progress = hits_drops_report_progress,
+       .report_final = hits_drops_report_final,
+};
+
+const struct bench bench_trig_rawtp = {
+       .name = "trig-rawtp",
+       .validate = trigger_validate,
+       .setup = trigger_rawtp_setup,
+       .producer_thread = trigger_producer,
+       .consumer_thread = trigger_consumer,
+       .measure = trigger_measure,
+       .report_progress = hits_drops_report_progress,
+       .report_final = hits_drops_report_final,
+};
+
+const struct bench bench_trig_kprobe = {
+       .name = "trig-kprobe",
+       .validate = trigger_validate,
+       .setup = trigger_kprobe_setup,
+       .producer_thread = trigger_producer,
+       .consumer_thread = trigger_consumer,
+       .measure = trigger_measure,
+       .report_progress = hits_drops_report_progress,
+       .report_final = hits_drops_report_final,
+};
+
+const struct bench bench_trig_fentry = {
+       .name = "trig-fentry",
+       .validate = trigger_validate,
+       .setup = trigger_fentry_setup,
+       .producer_thread = trigger_producer,
+       .consumer_thread = trigger_consumer,
+       .measure = trigger_measure,
+       .report_progress = hits_drops_report_progress,
+       .report_final = hits_drops_report_final,
+};
+
+const struct bench bench_trig_fmodret = {
+       .name = "trig-fmodret",
+       .validate = trigger_validate,
+       .setup = trigger_fmodret_setup,
+       .producer_thread = trigger_producer,
+       .consumer_thread = trigger_consumer,
+       .measure = trigger_measure,
+       .report_progress = hits_drops_report_progress,
+       .report_final = hits_drops_report_final,
+};
diff --git a/tools/testing/selftests/bpf/benchs/run_bench_trigger.sh b/tools/testing/selftests/bpf/benchs/run_bench_trigger.sh
new file mode 100755 (executable)
index 0000000..78e83f2
--- /dev/null
@@ -0,0 +1,9 @@
+#!/bin/bash
+
+set -eufo pipefail
+
+for i in base tp rawtp kprobe fentry fmodret
+do
+       summary=$(sudo ./bench -w2 -d5 -a trig-$i | tail -n1 | cut -d'(' -f1 | cut -d' ' -f3-)
+       printf "%-10s: %s\n" $i "$summary"
+done
diff --git a/tools/testing/selftests/bpf/progs/trigger_bench.c b/tools/testing/selftests/bpf/progs/trigger_bench.c
new file mode 100644 (file)
index 0000000..8b36b66
--- /dev/null
@@ -0,0 +1,47 @@
+// SPDX-License-Identifier: GPL-2.0
+// Copyright (c) 2020 Facebook
+
+#include <linux/bpf.h>
+#include <asm/unistd.h>
+#include <bpf/bpf_helpers.h>
+#include <bpf/bpf_tracing.h>
+
+char _license[] SEC("license") = "GPL";
+
+long hits = 0;
+
+SEC("tp/syscalls/sys_enter_getpgid")
+int bench_trigger_tp(void *ctx)
+{
+       __sync_add_and_fetch(&hits, 1);
+       return 0;
+}
+
+SEC("raw_tp/sys_enter")
+int BPF_PROG(bench_trigger_raw_tp, struct pt_regs *regs, long id)
+{
+       if (id == __NR_getpgid)
+               __sync_add_and_fetch(&hits, 1);
+       return 0;
+}
+
+SEC("kprobe/__x64_sys_getpgid")
+int bench_trigger_kprobe(void *ctx)
+{
+       __sync_add_and_fetch(&hits, 1);
+       return 0;
+}
+
+SEC("fentry/__x64_sys_getpgid")
+int bench_trigger_fentry(void *ctx)
+{
+       __sync_add_and_fetch(&hits, 1);
+       return 0;
+}
+
+SEC("fmod_ret/__x64_sys_getpgid")
+int bench_trigger_fmodret(void *ctx)
+{
+       __sync_add_and_fetch(&hits, 1);
+       return -22;
+}