[<prev] [next>] [<thread-prev] [thread-next>] [day] [month] [year] [list]
Message-ID: <1466940078-65581-27-git-send-email-hekuang@huawei.com>
Date: Sun, 26 Jun 2016 11:21:18 +0000
From: He Kuang <hekuang@...wei.com>
To: <acme@...nel.org>, <peterz@...radead.org>, <mingo@...hat.com>,
<jolsa@...hat.com>, <brendan.d.gregg@...il.com>, <ast@...nel.org>,
<alexander.shishkin@...ux.intel.com>, <wangnan0@...wei.com>,
<hekuang@...wei.com>
CC: <linux-kernel@...r.kernel.org>
Subject: [RFC PATCH v2 26/26] perf tests: Add uBPF test case
From: Wang Nan <wangnan0@...wei.com>
Introduce a BPF script use uBPF, test compiling, helper and hook.
Validate passing information through helper and hooks.
Signed-off-by: Wang Nan <wangnan0@...wei.com>
Signed-off-by: He Kuang <hekuang@...wei.com>
---
tools/perf/tests/Build | 8 +++
tools/perf/tests/bpf-script-test-ubpf.c | 88 +++++++++++++++++++++++++++++++++
tools/perf/tests/bpf.c | 78 ++++++++++++++++++++++++++++-
tools/perf/tests/llvm.c | 4 ++
tools/perf/tests/llvm.h | 2 +
5 files changed, 178 insertions(+), 2 deletions(-)
create mode 100644 tools/perf/tests/bpf-script-test-ubpf.c
diff --git a/tools/perf/tests/Build b/tools/perf/tests/Build
index 66a2898..74da237 100644
--- a/tools/perf/tests/Build
+++ b/tools/perf/tests/Build
@@ -32,6 +32,7 @@ perf-y += parse-no-sample-id-all.o
perf-y += kmod-path.o
perf-y += thread-map.o
perf-y += llvm.o llvm-src-base.o llvm-src-kbuild.o llvm-src-prologue.o llvm-src-relocation.o
+perf-y += llvm-src-ubpf.o
perf-y += bpf.o
perf-y += topology.o
perf-y += cpumap.o
@@ -68,6 +69,13 @@ $(OUTPUT)tests/llvm-src-relocation.c: tests/bpf-script-test-relocation.c tests/B
$(Q)sed -e 's/"/\\"/g' -e 's/\(.*\)/"\1\\n"/g' $< >> $@
$(Q)echo ';' >> $@
+$(OUTPUT)tests/llvm-src-ubpf.c: tests/bpf-script-test-ubpf.c tests/Build
+ $(call rule_mkdir)
+ $(Q)echo '#include <tests/llvm.h>' > $@
+ $(Q)echo 'const char test_llvm__bpf_test_ubpf[] =' >> $@
+ $(Q)sed -e 's/"/\\"/g' -e 's/\(.*\)/"\1\\n"/g' $< >> $@
+ $(Q)echo ';' >> $@
+
ifeq ($(ARCH),$(filter $(ARCH),x86 arm arm64))
perf-$(CONFIG_DWARF_UNWIND) += dwarf-unwind.o
endif
diff --git a/tools/perf/tests/bpf-script-test-ubpf.c b/tools/perf/tests/bpf-script-test-ubpf.c
new file mode 100644
index 0000000..5064ee9
--- /dev/null
+++ b/tools/perf/tests/bpf-script-test-ubpf.c
@@ -0,0 +1,88 @@
+/*
+ * bpf-script-test-ubpf.c
+ * Test user space BPF
+ */
+
+#ifndef LINUX_VERSION_CODE
+# error Need LINUX_VERSION_CODE
+# error Example: for 4.2 kernel, put 'clang-opt="-DLINUX_VERSION_CODE=0x40200" into llvm section of ~/.perfconfig'
+#endif
+#define BPF_ANY 0
+#define BPF_MAP_TYPE_ARRAY 2
+#define BPF_FUNC_map_lookup_elem 1
+#define BPF_FUNC_map_update_elem 2
+
+static void *(*bpf_map_lookup_elem)(void *map, void *key) =
+ (void *) BPF_FUNC_map_lookup_elem;
+static
+void *(*bpf_map_update_elem)(void *map, void *key, void *value, int flags) =
+ (void *) BPF_FUNC_map_update_elem;
+
+struct bpf_map_def {
+ unsigned int type;
+ unsigned int key_size;
+ unsigned int value_size;
+ unsigned int max_entries;
+};
+
+#define SEC(NAME) __attribute__((section(NAME), used))
+SEC("maps")
+struct bpf_map_def counter = {
+ .type = BPF_MAP_TYPE_ARRAY,
+ .key_size = sizeof(int),
+ .value_size = sizeof(int),
+ .max_entries = 1,
+};
+
+SEC("func=sys_epoll_pwait")
+int bpf_func__sys_epoll_pwait(void *ctx)
+{
+ int ind = 0;
+ int *flag = bpf_map_lookup_elem(&counter, &ind);
+
+ if (!flag)
+ return 0;
+ __sync_fetch_and_add(flag, 1);
+ return 0;
+}
+char _license[] SEC("license") = "GPL";
+int _version SEC("version") = LINUX_VERSION_CODE;
+
+#define UBPF_FUNC_printf 4
+#define UBPF_FUNC_map_lookup_elem 5
+#define UBPF_FUNC_map_update_elem 6
+#define UBPF_FUNC_test_report 63
+
+static int (*ubpf_printf)(char *fmt, ...) = (void *)UBPF_FUNC_printf;
+static void
+(*ubpf_map_lookup_elem)(struct bpf_map_def *, void *, void *) =
+ (void *)UBPF_FUNC_map_lookup_elem;
+static void
+(*ubpf_map_update_elem)(struct bpf_map_def *, void *, void *, int flags) =
+ (void *)UBPF_FUNC_map_update_elem;
+static void (*ubpf_test_report)(int) = (void *)UBPF_FUNC_test_report;
+
+struct perf_record_end_ctx {
+ int samples;
+ int dummy;
+};
+
+SEC("UBPF;perf_record_start")
+int perf_record_start(void)
+{
+ int idx = 0, val = 1000;
+
+ ubpf_map_update_elem(&counter, &idx, &val, 0);
+ return 0;
+}
+
+SEC("UBPF;perf_record_end")
+int perf_record_end(struct perf_record_end_ctx *ctx)
+{
+ int idx = 0, val;
+
+ ubpf_map_lookup_elem(&counter, &idx, &val);
+ ubpf_test_report(val + ctx->samples);
+
+ return 0;
+}
diff --git a/tools/perf/tests/bpf.c b/tools/perf/tests/bpf.c
index f31eed3..0c094e6 100644
--- a/tools/perf/tests/bpf.c
+++ b/tools/perf/tests/bpf.c
@@ -5,10 +5,13 @@
#include <util/evlist.h>
#include <linux/bpf.h>
#include <linux/filter.h>
+#include <bpf/libbpf.h>
#include <bpf/bpf.h>
#include "tests.h"
#include "llvm.h"
#include "debug.h"
+#include "ubpf-helpers.h"
+#include "ubpf-hooks.h"
#define NR_ITERS 111
#ifdef HAVE_LIBBPF_SUPPORT
@@ -46,6 +49,35 @@ static int llseek_loop(void)
#endif
+union testcase_context {
+ void *ptr;
+ unsigned long num;
+};
+
+#ifdef HAVE_UBPF_SUPPORT
+static int __ubpf_report_val;
+
+static void test_report(int val)
+{
+ printf("test_report val = %d\n", val);
+ __ubpf_report_val = val;
+}
+
+static int ubpf_prepare(union testcase_context *ctx __maybe_unused)
+{
+ ubpf_hook_perf_record_start(0);
+ return 0;
+}
+
+static int ubpf_verify(union testcase_context *ctx __maybe_unused)
+{
+ ubpf_hook_perf_record_end(1234, 0);
+ if (__ubpf_report_val != 1234 + NR_ITERS + 1000)
+ return TEST_FAIL;
+ return TEST_OK;
+}
+#endif
+
static struct {
enum test_llvm__testcase prog_id;
const char *desc;
@@ -54,6 +86,10 @@ static struct {
const char *msg_load_fail;
int (*target_func)(void);
int expect_result;
+
+ union testcase_context context;
+ int (*prepare)(union testcase_context *);
+ int (*verify)(union testcase_context *);
} bpf_testcase_table[] = {
{
LLVM_TESTCASE_BASE,
@@ -63,6 +99,7 @@ static struct {
"load bpf object failed",
&epoll_pwait_loop,
(NR_ITERS + 1) / 2,
+ {0}, NULL, NULL
},
#ifdef HAVE_BPF_PROLOGUE
{
@@ -73,6 +110,7 @@ static struct {
"check your vmlinux setting?",
&llseek_loop,
(NR_ITERS + 1) / 4,
+ {0}, NULL, NULL
},
#endif
{
@@ -83,11 +121,27 @@ static struct {
"libbpf error when dealing with relocation",
NULL,
0,
+ {0}, NULL, NULL
},
+#ifdef HAVE_UBPF_SUPPORT
+ {
+ LLVM_TESTCASE_BPF_UBPF,
+ "Test UBPF support",
+ "[bpf_ubpf_test]",
+ "fix 'perf test LLVM' first",
+ "failed to load UBPF",
+ &epoll_pwait_loop,
+ 0,
+ {0}, ubpf_prepare, ubpf_verify,
+ }
+#endif
};
static int do_test(struct bpf_object *obj, int (*func)(void),
- int expect)
+ int expect,
+ int (*prepare)(union testcase_context *),
+ int (*verify)(union testcase_context *),
+ union testcase_context *ctx)
{
struct record_opts opts = {
.target = {
@@ -154,6 +208,14 @@ static int do_test(struct bpf_object *obj, int (*func)(void),
goto out_delete_evlist;
}
+ if (prepare) {
+ err = prepare(ctx);
+ if (err < 0) {
+ pr_debug("prepare fail\n");
+ goto out_delete_evlist;
+ }
+ }
+
perf_evlist__enable(evlist);
(*func)();
perf_evlist__disable(evlist);
@@ -176,6 +238,8 @@ static int do_test(struct bpf_object *obj, int (*func)(void),
ret = TEST_OK;
+ if (verify)
+ ret = verify(ctx);
out_delete_evlist:
perf_evlist__delete(evlist);
return ret;
@@ -201,6 +265,13 @@ static int __test__bpf(int idx)
size_t obj_buf_sz;
struct bpf_object *obj;
+#ifdef HAVE_UBPF_SUPPORT
+ ret = libbpf_set_ubpf_func(63, "test_report", test_report);
+ if (ret) {
+ pr_debug("Unable to set UBPF helper function\n");
+ return TEST_FAIL;
+ }
+#endif
ret = test_llvm__fetch_bpf_obj(&obj_buf, &obj_buf_sz,
bpf_testcase_table[idx].prog_id,
true, NULL);
@@ -229,7 +300,10 @@ static int __test__bpf(int idx)
if (obj)
ret = do_test(obj,
bpf_testcase_table[idx].target_func,
- bpf_testcase_table[idx].expect_result);
+ bpf_testcase_table[idx].expect_result,
+ bpf_testcase_table[idx].prepare,
+ bpf_testcase_table[idx].verify,
+ &bpf_testcase_table[idx].context);
out:
bpf__clear();
return ret;
diff --git a/tools/perf/tests/llvm.c b/tools/perf/tests/llvm.c
index cff564f..92b0a42 100644
--- a/tools/perf/tests/llvm.c
+++ b/tools/perf/tests/llvm.c
@@ -48,6 +48,10 @@ static struct {
.desc = "Compile source for BPF relocation test",
.should_load_fail = true,
},
+ [LLVM_TESTCASE_BPF_UBPF] = {
+ .source = test_llvm__bpf_test_ubpf,
+ .desc = "Compile source for UBPF test",
+ },
};
int
diff --git a/tools/perf/tests/llvm.h b/tools/perf/tests/llvm.h
index 0eaa604..8ae4aae 100644
--- a/tools/perf/tests/llvm.h
+++ b/tools/perf/tests/llvm.h
@@ -8,12 +8,14 @@ extern const char test_llvm__bpf_base_prog[];
extern const char test_llvm__bpf_test_kbuild_prog[];
extern const char test_llvm__bpf_test_prologue_prog[];
extern const char test_llvm__bpf_test_relocation[];
+extern const char test_llvm__bpf_test_ubpf[];
enum test_llvm__testcase {
LLVM_TESTCASE_BASE,
LLVM_TESTCASE_KBUILD,
LLVM_TESTCASE_BPF_PROLOGUE,
LLVM_TESTCASE_BPF_RELOCATION,
+ LLVM_TESTCASE_BPF_UBPF,
__LLVM_TESTCASE_MAX,
};
--
1.8.5.2
Powered by blists - more mailing lists