diff mbox

[18/31] perf test: Add 'perf test BPF'

Message ID 1440822125-52691-19-git-send-email-wangnan0@huawei.com
State New
Headers show

Commit Message

Wang Nan Aug. 29, 2015, 4:21 a.m. UTC
This patch adds BPF testcase for testing BPF event filtering.

By utilizing the result of 'perf test LLVM', this patch compiles the
eBPF sample program then test it ability. The BPF script in 'perf test
LLVM' collects half of execution of epoll_pwait(). This patch runs 111
times of it, so the resule should contains 56 samples.

Signed-off-by: Wang Nan <wangnan0@huawei.com>
Cc: Arnaldo Carvalho de Melo <acme@redhat.com>
Cc: Alexei Starovoitov <ast@plumgrid.com>
Cc: Brendan Gregg <brendan.d.gregg@gmail.com>
Cc: Daniel Borkmann <daniel@iogearbox.net>
Cc: David Ahern <dsahern@gmail.com>
Cc: He Kuang <hekuang@huawei.com>
Cc: Jiri Olsa <jolsa@kernel.org>
Cc: Kaixu Xia <xiakaixu@huawei.com>
Cc: Masami Hiramatsu <masami.hiramatsu.pt@hitachi.com>
Cc: Namhyung Kim <namhyung@kernel.org>
Cc: Peter Zijlstra <a.p.zijlstra@chello.nl>
Cc: Zefan Li <lizefan@huawei.com>
Cc: pi3orama@163.com
Link: http://lkml.kernel.org/n/1440151770-129878-16-git-send-email-wangnan0@huawei.com
---
 tools/perf/tests/Build          |   1 +
 tools/perf/tests/bpf.c          | 170 ++++++++++++++++++++++++++++++++++++++++
 tools/perf/tests/builtin-test.c |   4 +
 tools/perf/tests/llvm.c         |  19 +++++
 tools/perf/tests/llvm.h         |   1 +
 tools/perf/tests/tests.h        |   1 +
 tools/perf/util/bpf-loader.c    |  14 ++++
 tools/perf/util/bpf-loader.h    |   8 ++
 8 files changed, 218 insertions(+)
 create mode 100644 tools/perf/tests/bpf.c

Comments

Namhyung Kim Sept. 2, 2015, 12:45 p.m. UTC | #1
On Sat, Aug 29, 2015 at 04:21:52AM +0000, Wang Nan wrote:
> This patch adds BPF testcase for testing BPF event filtering.
> 
> By utilizing the result of 'perf test LLVM', this patch compiles the
> eBPF sample program then test it ability. The BPF script in 'perf test
> LLVM' collects half of execution of epoll_pwait(). This patch runs 111
> times of it, so the resule should contains 56 samples.
> 
> Signed-off-by: Wang Nan <wangnan0@huawei.com>
> Cc: Arnaldo Carvalho de Melo <acme@redhat.com>
> Cc: Alexei Starovoitov <ast@plumgrid.com>
> Cc: Brendan Gregg <brendan.d.gregg@gmail.com>
> Cc: Daniel Borkmann <daniel@iogearbox.net>
> Cc: David Ahern <dsahern@gmail.com>
> Cc: He Kuang <hekuang@huawei.com>
> Cc: Jiri Olsa <jolsa@kernel.org>
> Cc: Kaixu Xia <xiakaixu@huawei.com>
> Cc: Masami Hiramatsu <masami.hiramatsu.pt@hitachi.com>
> Cc: Namhyung Kim <namhyung@kernel.org>
> Cc: Peter Zijlstra <a.p.zijlstra@chello.nl>
> Cc: Zefan Li <lizefan@huawei.com>
> Cc: pi3orama@163.com
> Link: http://lkml.kernel.org/n/1440151770-129878-16-git-send-email-wangnan0@huawei.com
> ---

[SNIP]

> +static int prepare_bpf(void *obj_buf, size_t obj_buf_sz)
> +{
> +	int err;
> +	char errbuf[BUFSIZ];
> +
> +	err = bpf__prepare_load_buffer(obj_buf, obj_buf_sz, NULL);
> +	if (err) {
> +		bpf__strerror_prepare_load("[buffer]", false, err, errbuf,
> +					   sizeof(errbuf));
> +		fprintf(stderr, " (%s)", errbuf);
> +		return TEST_FAIL;
> +	}
> +
> +	err = bpf__probe();
> +	if (err) {
> +		bpf__strerror_load(err, errbuf, sizeof(errbuf));
> +		fprintf(stderr, " (%s)", errbuf);
> +		if (getuid() != 0)

geteuid() ?

Thanks,
Namhyung


> +			fprintf(stderr, " (try run as root)");
> +		return TEST_FAIL;
> +	}
> +
> +	err = bpf__load();
> +	if (err) {
> +		bpf__strerror_load(err, errbuf, sizeof(errbuf));
> +		fprintf(stderr, " (%s)", errbuf);
> +		return TEST_FAIL;
> +	}
> +
> +	return 0;
> +}
--
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in
the body of a message to majordomo@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html
Please read the FAQ at  http://www.tux.org/lkml/
pi3orama Sept. 5, 2015, 12:21 p.m. UTC | #2
On 09/02/2015 08:45 PM, Namhyung Kim wrote:
> On Sat, Aug 29, 2015 at 04:21:52AM +0000, Wang Nan wrote:
>> This patch adds BPF testcase for testing BPF event filtering.
>>
>> By utilizing the result of 'perf test LLVM', this patch compiles the
>> eBPF sample program then test it ability. The BPF script in 'perf test
>> LLVM' collects half of execution of epoll_pwait(). This patch runs 111
>> times of it, so the resule should contains 56 samples.
>>
>> Signed-off-by: Wang Nan <wangnan0@huawei.com>
>> Cc: Arnaldo Carvalho de Melo <acme@redhat.com>
>> Cc: Alexei Starovoitov <ast@plumgrid.com>
>> Cc: Brendan Gregg <brendan.d.gregg@gmail.com>
>> Cc: Daniel Borkmann <daniel@iogearbox.net>
>> Cc: David Ahern <dsahern@gmail.com>
>> Cc: He Kuang <hekuang@huawei.com>
>> Cc: Jiri Olsa <jolsa@kernel.org>
>> Cc: Kaixu Xia <xiakaixu@huawei.com>
>> Cc: Masami Hiramatsu <masami.hiramatsu.pt@hitachi.com>
>> Cc: Namhyung Kim <namhyung@kernel.org>
>> Cc: Peter Zijlstra <a.p.zijlstra@chello.nl>
>> Cc: Zefan Li <lizefan@huawei.com>
>> Cc: pi3orama@163.com
>> Link: http://lkml.kernel.org/n/1440151770-129878-16-git-send-email-wangnan0@huawei.com
>> ---
>
> [SNIP]
>
>> +static int prepare_bpf(void *obj_buf, size_t obj_buf_sz)
>> +{
>> +	int err;
>> +	char errbuf[BUFSIZ];
>> +
>> +	err = bpf__prepare_load_buffer(obj_buf, obj_buf_sz, NULL);
>> +	if (err) {
>> +		bpf__strerror_prepare_load("[buffer]", false, err, errbuf,
>> +					   sizeof(errbuf));
>> +		fprintf(stderr, " (%s)", errbuf);
>> +		return TEST_FAIL;
>> +	}
>> +
>> +	err = bpf__probe();
>> +	if (err) {
>> +		bpf__strerror_load(err, errbuf, sizeof(errbuf));
>> +		fprintf(stderr, " (%s)", errbuf);
>> +		if (getuid() != 0)
>
> geteuid() ?
>
> Thanks,
> Namhyung
>

Already changed.

Thank you.

>
>> +			fprintf(stderr, " (try run as root)");
>> +		return TEST_FAIL;
>> +	}
>> +
>> +	err = bpf__load();
>> +	if (err) {
>> +		bpf__strerror_load(err, errbuf, sizeof(errbuf));
>> +		fprintf(stderr, " (%s)", errbuf);
>> +		return TEST_FAIL;
>> +	}
>> +
>> +	return 0;
>> +}

--
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in
the body of a message to majordomo@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html
Please read the FAQ at  http://www.tux.org/lkml/
diff mbox

Patch

diff --git a/tools/perf/tests/Build b/tools/perf/tests/Build
index 8c98409..7ceb448 100644
--- a/tools/perf/tests/Build
+++ b/tools/perf/tests/Build
@@ -33,6 +33,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.o
+perf-y += bpf.o
 
 $(OUTPUT)tests/llvm-src.c: tests/bpf-script-example.c
 	$(Q)echo '#include <tests/llvm.h>' > $@
diff --git a/tools/perf/tests/bpf.c b/tools/perf/tests/bpf.c
new file mode 100644
index 0000000..6c238ca
--- /dev/null
+++ b/tools/perf/tests/bpf.c
@@ -0,0 +1,170 @@ 
+#include <stdio.h>
+#include <sys/epoll.h>
+#include <util/bpf-loader.h>
+#include <util/evlist.h>
+#include "tests.h"
+#include "llvm.h"
+#include "debug.h"
+#define NR_ITERS       111
+
+#ifdef HAVE_LIBBPF_SUPPORT
+
+static int epoll_pwait_loop(void)
+{
+	int i;
+
+	/* Should fail NR_ITERS times */
+	for (i = 0; i < NR_ITERS; i++)
+		epoll_pwait(-(i + 1), NULL, 0, 0, NULL);
+	return 0;
+}
+
+static int prepare_bpf(void *obj_buf, size_t obj_buf_sz)
+{
+	int err;
+	char errbuf[BUFSIZ];
+
+	err = bpf__prepare_load_buffer(obj_buf, obj_buf_sz, NULL);
+	if (err) {
+		bpf__strerror_prepare_load("[buffer]", false, err, errbuf,
+					   sizeof(errbuf));
+		fprintf(stderr, " (%s)", errbuf);
+		return TEST_FAIL;
+	}
+
+	err = bpf__probe();
+	if (err) {
+		bpf__strerror_load(err, errbuf, sizeof(errbuf));
+		fprintf(stderr, " (%s)", errbuf);
+		if (getuid() != 0)
+			fprintf(stderr, " (try run as root)");
+		return TEST_FAIL;
+	}
+
+	err = bpf__load();
+	if (err) {
+		bpf__strerror_load(err, errbuf, sizeof(errbuf));
+		fprintf(stderr, " (%s)", errbuf);
+		return TEST_FAIL;
+	}
+
+	return 0;
+}
+
+static int do_test(void)
+{
+	struct record_opts opts = {
+		.target = {
+			.uid = UINT_MAX,
+			.uses_mmap = true,
+		},
+		.freq	      = 0,
+		.mmap_pages   = 256,
+		.default_interval = 1,
+	};
+
+	int err, i, count = 0;
+	char pid[16];
+	char sbuf[STRERR_BUFSIZE];
+	struct perf_evlist *evlist;
+
+	snprintf(pid, sizeof(pid), "%d", getpid());
+	pid[sizeof(pid) - 1] = '\0';
+	opts.target.tid = opts.target.pid = pid;
+
+	/* Instead of perf_evlist__new_default, don't add default events */
+	evlist = perf_evlist__new();
+	if (!evlist) {
+		pr_debug("No ehough memory to create evlist\n");
+		return -ENOMEM;
+	}
+
+	err = perf_evlist__create_maps(evlist, &opts.target);
+	if (err < 0) {
+		pr_debug("Not enough memory to create thread/cpu maps\n");
+		goto out_delete_evlist;
+	}
+
+	err = perf_evlist__add_bpf(evlist);
+	if (err) {
+		fprintf(stderr, " (Failed to add events selected by BPF)");
+		goto out_delete_evlist;
+	}
+
+	perf_evlist__config(evlist, &opts);
+
+	err = perf_evlist__open(evlist);
+	if (err < 0) {
+		pr_debug("perf_evlist__open: %s\n",
+			 strerror_r(errno, sbuf, sizeof(sbuf)));
+		goto out_delete_evlist;
+	}
+
+	err = perf_evlist__mmap(evlist, opts.mmap_pages, false);
+	if (err < 0) {
+		pr_debug("perf_evlist__mmap: %s\n",
+			 strerror_r(errno, sbuf, sizeof(sbuf)));
+		goto out_delete_evlist;
+	}
+
+	perf_evlist__enable(evlist);
+	epoll_pwait_loop();
+	perf_evlist__disable(evlist);
+
+	for (i = 0; i < evlist->nr_mmaps; i++) {
+		union perf_event *event;
+
+		while ((event = perf_evlist__mmap_read(evlist, i)) != NULL) {
+			const u32 type = event->header.type;
+
+			if (type == PERF_RECORD_SAMPLE)
+				count ++;
+		}
+	}
+
+	if (count != (NR_ITERS + 1) / 2) {
+		fprintf(stderr, " (filter result incorrect)");
+		err = -EBADF;
+	}
+
+out_delete_evlist:
+	perf_evlist__delete(evlist);
+	if (err)
+		return TEST_FAIL;
+	return 0;
+}
+
+int test__bpf(void)
+{
+	int err;
+	void *obj_buf;
+	size_t obj_buf_sz;
+
+	test_llvm__fetch_bpf_obj(&obj_buf, &obj_buf_sz);
+	if (!obj_buf || !obj_buf_sz) {
+		if (verbose == 0)
+			fprintf(stderr, " (fix 'perf test LLVM' first)");
+		return TEST_SKIP;
+	}
+
+	err = prepare_bpf(obj_buf, obj_buf_sz);
+	if (err)
+		goto out;
+
+	err = do_test();
+	if (err)
+		goto out;
+out:
+	bpf__unprobe();
+	bpf__clear();
+	if (err)
+		return TEST_FAIL;
+	return 0;
+}
+
+#else
+int test__bpf(void)
+{
+	return TEST_SKIP;
+}
+#endif
diff --git a/tools/perf/tests/builtin-test.c b/tools/perf/tests/builtin-test.c
index 1a349e8..c32c836 100644
--- a/tools/perf/tests/builtin-test.c
+++ b/tools/perf/tests/builtin-test.c
@@ -183,6 +183,10 @@  static struct test {
 		.cleanup = test__llvm_cleanup,
 	},
 	{
+		.desc = "Test BPF filter",
+		.func = test__bpf,
+	},
+	{
 		.func = NULL,
 	},
 };
diff --git a/tools/perf/tests/llvm.c b/tools/perf/tests/llvm.c
index 236bf39..fd5fdb0 100644
--- a/tools/perf/tests/llvm.c
+++ b/tools/perf/tests/llvm.c
@@ -174,3 +174,22 @@  void test__llvm_cleanup(void)
 			(~((unsigned long)page_size - 1));
 	munmap((void *)boundary, buf_end - boundary);
 }
+
+void
+test_llvm__fetch_bpf_obj(void **p_obj_buf, size_t *p_obj_buf_sz)
+{
+	*p_obj_buf = NULL;
+	*p_obj_buf_sz = 0;
+
+	if (!p_test_llvm__bpf_result) {
+		test__llvm_prepare();
+		test__llvm();
+		test__llvm_cleanup();
+	}
+
+	if (!p_test_llvm__bpf_result)
+		return;
+
+	*p_obj_buf = p_test_llvm__bpf_result->object;
+	*p_obj_buf_sz = p_test_llvm__bpf_result->size;
+}
diff --git a/tools/perf/tests/llvm.h b/tools/perf/tests/llvm.h
index 1e89e46..2fd7ed6 100644
--- a/tools/perf/tests/llvm.h
+++ b/tools/perf/tests/llvm.h
@@ -10,5 +10,6 @@  struct test_llvm__bpf_result {
 
 extern struct test_llvm__bpf_result *p_test_llvm__bpf_result;
 extern const char test_llvm__bpf_prog[];
+void test_llvm__fetch_bpf_obj(void **p_obj_buf, size_t *p_obj_buf_sz);
 
 #endif
diff --git a/tools/perf/tests/tests.h b/tools/perf/tests/tests.h
index 0d79f04..f8ded73 100644
--- a/tools/perf/tests/tests.h
+++ b/tools/perf/tests/tests.h
@@ -65,6 +65,7 @@  int test__thread_map(void);
 int test__llvm(void);
 void test__llvm_prepare(void);
 void test__llvm_cleanup(void);
+int test__bpf(void);
 
 #if defined(__x86_64__) || defined(__i386__) || defined(__arm__) || defined(__aarch64__)
 #ifdef HAVE_DWARF_UNWIND_SUPPORT
diff --git a/tools/perf/util/bpf-loader.c b/tools/perf/util/bpf-loader.c
index c2aafe2..95e529b 100644
--- a/tools/perf/util/bpf-loader.c
+++ b/tools/perf/util/bpf-loader.c
@@ -153,6 +153,20 @@  sync_bpf_program_pev(struct bpf_program *prog)
 	return 0;
 }
 
+int bpf__prepare_load_buffer(void *obj_buf, size_t obj_buf_sz,
+			     const char *name)
+{
+	struct bpf_object *obj;
+
+	obj = bpf_object__open_buffer(obj_buf, obj_buf_sz, name);
+	if (!obj) {
+		pr_debug("bpf: failed to load buffer\n");
+		return -EINVAL;
+	}
+
+	return 0;
+}
+
 int bpf__prepare_load(const char *filename, bool source)
 {
 	struct bpf_object *obj;
diff --git a/tools/perf/util/bpf-loader.h b/tools/perf/util/bpf-loader.h
index 97aed65..dead4d4 100644
--- a/tools/perf/util/bpf-loader.h
+++ b/tools/perf/util/bpf-loader.h
@@ -19,6 +19,8 @@  typedef int (*bpf_prog_iter_callback_t)(struct probe_trace_event *tev,
 
 #ifdef HAVE_LIBBPF_SUPPORT
 int bpf__prepare_load(const char *filename, bool source);
+int bpf__prepare_load_buffer(void *obj_buf, size_t obj_buf_sz,
+			     const char *name);
 int bpf__strerror_prepare_load(const char *filename, bool source,
 			       int err, char *buf, size_t size);
 int bpf__probe(void);
@@ -39,6 +41,12 @@  static inline int bpf__prepare_load(const char *filename __maybe_unused,
 	return -1;
 }
 
+static inline int bpf__prepare_load_buffer(void *obj_buf __maybe_unused,
+					   size_t obj_buf_sz __maybe_unused)
+{
+	return bpf__prepare_load(NULL, false);
+}
+
 static inline int bpf__probe(void) { return 0; }
 static inline int bpf__unprobe(void) { return 0; }
 static inline int bpf__load(void) { return 0; }