Return-Path: Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1750971AbbFLFkq (ORCPT ); Fri, 12 Jun 2015 01:40:46 -0400 Received: from szxga02-in.huawei.com ([119.145.14.65]:11074 "EHLO szxga02-in.huawei.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1753097AbbFLFgj (ORCPT ); Fri, 12 Jun 2015 01:36:39 -0400 From: Wang Nan To: , , , , , , , , , , CC: , , , , Subject: [RFC PATCH v7 25/37] perf tests: Add LLVM test for eBPF on-the-fly compiling Date: Fri, 12 Jun 2015 05:35:33 +0000 Message-ID: <1434087345-127225-26-git-send-email-wangnan0@huawei.com> X-Mailer: git-send-email 1.8.3.4 In-Reply-To: <1434087345-127225-1-git-send-email-wangnan0@huawei.com> References: <1434087345-127225-1-git-send-email-wangnan0@huawei.com> MIME-Version: 1.0 Content-Type: text/plain X-Originating-IP: [10.107.197.200] X-CFilter-Loop: Reflected Sender: linux-kernel-owner@vger.kernel.org List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Length: 4203 Lines: 151 Previous patch introduces llvm__compile_bpf() to compile source file to eBPF object. This patch adds testcase to test it. It also test libbpf by opening generated object. Signed-off-by: Wang Nan --- tools/perf/tests/Build | 1 + tools/perf/tests/builtin-test.c | 4 ++ tools/perf/tests/llvm.c | 85 +++++++++++++++++++++++++++++++++++++++++ tools/perf/tests/tests.h | 1 + 4 files changed, 91 insertions(+) create mode 100644 tools/perf/tests/llvm.c diff --git a/tools/perf/tests/Build b/tools/perf/tests/Build index ee41e70..5d74604 100644 --- a/tools/perf/tests/Build +++ b/tools/perf/tests/Build @@ -31,6 +31,7 @@ perf-y += code-reading.o perf-y += sample-parsing.o perf-y += parse-no-sample-id-all.o perf-y += kmod-path.o +perf-y += llvm.o perf-$(CONFIG_X86) += perf-time-to-tsc.o diff --git a/tools/perf/tests/builtin-test.c b/tools/perf/tests/builtin-test.c index 87b9961..4d28fd9 100644 --- a/tools/perf/tests/builtin-test.c +++ b/tools/perf/tests/builtin-test.c @@ -171,6 +171,10 @@ static struct test { .func = test__kmod_path__parse, }, { + .desc = "Test LLVM searching and compiling", + .func = test__llvm, + }, + { .func = NULL, }, }; diff --git a/tools/perf/tests/llvm.c b/tools/perf/tests/llvm.c new file mode 100644 index 0000000..272be1b --- /dev/null +++ b/tools/perf/tests/llvm.c @@ -0,0 +1,85 @@ +#include +#include +#include +#include +#include "tests.h" +#include "debug.h" + +static int perf_config_cb(const char *var, const char *val, + void *arg __maybe_unused) +{ + return perf_default_config(var, val, arg); +} + +/* + * Randomly give it a "version" section since we don't really load it + * into kernel + */ +static const char test_bpf_prog[] = + "__attribute__((section(\"do_fork\"), used)) " + "int fork(void *ctx) {return 0;} " + "char _license[] __attribute__((section(\"license\"), used)) = \"GPL\";" + "int _version __attribute__((section(\"version\"), used)) = 0x40100;"; + +#ifdef HAVE_LIBBPF_SUPPORT +static int test__bpf_parsing(void *obj_buf, size_t obj_buf_sz) +{ + struct bpf_object *obj; + + obj = bpf_object__open_buffer(obj_buf, obj_buf_sz); + if (!obj) + return -1; + bpf_object__close(obj); + return 0; +} +#else +static int test__bpf_parsing(void *obj_buf __maybe_unused, + size_t obj_buf_sz __maybe_unused) +{ + fprintf(stderr, " (skip bpf parsing)"); + return 0; +} +#endif + +#define new_string(n, fmt, ...) \ +do { \ + n##_sz = snprintf(NULL, 0, fmt, __VA_ARGS__);\ + if (n##_sz == 0) \ + return -1; \ + n##_new = malloc(n##_sz + 1); \ + if (!n##_new) \ + return -1; \ + snprintf(n##_new, n##_sz + 1, fmt, __VA_ARGS__);\ + n##_new[n##_sz] = '\0'; \ +} while(0) + +int test__llvm(void) +{ + char *tmpl_new, *clang_opt_new; + size_t tmpl_sz, clang_opt_sz; + void *obj_buf; + size_t obj_buf_sz; + int err; + + perf_config(perf_config_cb, NULL); + + if (!llvm_param.clang_bpf_cmd_template) + return -1; + + if (!llvm_param.clang_opt) + llvm_param.clang_opt = strdup(""); + + new_string(tmpl, "echo '%s' | %s", test_bpf_prog, + llvm_param.clang_bpf_cmd_template); + new_string(clang_opt, "-xc %s", llvm_param.clang_opt); + + llvm_param.clang_bpf_cmd_template = tmpl_new; + llvm_param.clang_opt = clang_opt_new; + err = llvm__compile_bpf("-", &obj_buf, &obj_buf_sz); + if (err) + return -1; + + err = test__bpf_parsing(obj_buf, obj_buf_sz); + free(obj_buf); + return err; +} diff --git a/tools/perf/tests/tests.h b/tools/perf/tests/tests.h index 8e5038b..5b29796 100644 --- a/tools/perf/tests/tests.h +++ b/tools/perf/tests/tests.h @@ -61,6 +61,7 @@ int test__switch_tracking(void); int test__fdarray__filter(void); int test__fdarray__add(void); int test__kmod_path__parse(void); +int test__llvm(void); #if defined(__x86_64__) || defined(__i386__) || defined(__arm__) || defined(__aarch64__) #ifdef HAVE_DWARF_UNWIND_SUPPORT -- 1.8.3.4 -- 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/