2015-11-04 11:20:32

by Wang Nan

[permalink] [raw]
Subject: [PATCH 0/4] perf tools: Improve llvm compiling and test

This patchset is based on perf/core in Arnaldo's repository.

The first 2 patches addes some macro which helps writing BPF programs
easier.

The last 2 patches enforce 'perf test LLVM', improves coding.

Wang Nan (4):
perf tools: Pass available CPU number to clang compiler
perf tools: Pass LINUX_VERSION_CODE to BPF program when compiling
perf test: Enforce LLVM test: update basic BPF test program
perf test: Enforce LLVM test: add kbuild test

tools/perf/tests/Build | 16 +++-
tools/perf/tests/bpf-script-example.c | 4 +
tools/perf/tests/bpf-script-test-kbuild.c | 21 +++++
tools/perf/tests/llvm.c | 135 +++++++++++++++++++++++-------
tools/perf/tests/llvm.h | 18 ++++
tools/perf/util/llvm-utils.c | 51 +++++++++--
6 files changed, 208 insertions(+), 37 deletions(-)
create mode 100644 tools/perf/tests/bpf-script-test-kbuild.c
create mode 100644 tools/perf/tests/llvm.h

--
1.8.3.4


2015-11-04 11:20:43

by Wang Nan

[permalink] [raw]
Subject: [PATCH 1/4] perf tools: Pass available CPU number to clang compiler

This patch introduces a new macro "__NR_CPUS__" to perf's embedded
clang compiler, which represent the available CPU counters in this
system. BPF program can use this macro to create a map with same
number of system CPUs. For exmaple:

struct bpf_map_def SEC("maps") pmu_map = {
.type = BPF_MAP_TYPE_PERF_EVENT_ARRAY,
.key_size = sizeof(int),
.value_size = sizeof(u32),
.max_entries = __NR_CPUS__,
};

Signed-off-by: Wang Nan <[email protected]>
Cc: Arnaldo Carvalho de Melo <[email protected]>
Cc: Alexei Starovoitov <[email protected]>
Cc: Namhyung Kim <[email protected]>
Cc: Zefan Li <[email protected]>
Cc: [email protected]
---
tools/perf/util/llvm-utils.c | 24 ++++++++++++++++++------
1 file changed, 18 insertions(+), 6 deletions(-)

diff --git a/tools/perf/util/llvm-utils.c b/tools/perf/util/llvm-utils.c
index 4f6a478..80eecef 100644
--- a/tools/perf/util/llvm-utils.c
+++ b/tools/perf/util/llvm-utils.c
@@ -11,10 +11,11 @@
#include "cache.h"

#define CLANG_BPF_CMD_DEFAULT_TEMPLATE \
- "$CLANG_EXEC -D__KERNEL__ $CLANG_OPTIONS " \
- "$KERNEL_INC_OPTIONS -Wno-unused-value " \
- "-Wno-pointer-sign -working-directory " \
- "$WORKING_DIR -c \"$CLANG_SOURCE\" -target bpf -O2 -o -"
+ "$CLANG_EXEC -D__KERNEL__ -D__NR_CPUS__=$NR_CPUS "\
+ "$CLANG_OPTIONS $KERNEL_INC_OPTIONS " \
+ "-Wno-unused-value -Wno-pointer-sign " \
+ "-working-directory $WORKING_DIR " \
+ "-c \"$CLANG_SOURCE\" -target bpf -O2 -o -"

struct llvm_param llvm_param = {
.clang_path = "clang",
@@ -326,8 +327,8 @@ get_kbuild_opts(char **kbuild_dir, char **kbuild_include_opts)
int llvm__compile_bpf(const char *path, void **p_obj_buf,
size_t *p_obj_buf_sz)
{
- int err;
- char clang_path[PATH_MAX];
+ int err, nr_cpus_avail;
+ char clang_path[PATH_MAX], nr_cpus_avail_str[64];
const char *clang_opt = llvm_param.clang_opt;
const char *template = llvm_param.clang_bpf_cmd_template;
char *kbuild_dir = NULL, *kbuild_include_opts = NULL;
@@ -354,6 +355,17 @@ int llvm__compile_bpf(const char *path, void **p_obj_buf,
*/
get_kbuild_opts(&kbuild_dir, &kbuild_include_opts);

+ nr_cpus_avail = sysconf(_SC_NPROCESSORS_CONF);
+ if (nr_cpus_avail <= 0) {
+ pr_err(
+"WARNING:\tunable to get available CPUs in this system: %s\n"
+" \tUse 128 instead.\n", strerror(errno));
+ nr_cpus_avail = 128;
+ }
+ snprintf(nr_cpus_avail_str, sizeof(nr_cpus_avail_str), "%d",
+ nr_cpus_avail);
+
+ force_set_env("NR_CPUS", nr_cpus_avail_str);
force_set_env("CLANG_EXEC", clang_path);
force_set_env("CLANG_OPTIONS", clang_opt);
force_set_env("KERNEL_INC_OPTIONS", kbuild_include_opts);
--
1.8.3.4

2015-11-04 11:20:55

by Wang Nan

[permalink] [raw]
Subject: [PATCH 2/4] perf tools: Pass LINUX_VERSION_CODE to BPF program when compiling

Arnaldo suggests to make LINUX_VERSION_CODE works like __func__ and
__FILE__ so user don't need to care setting right linux version
too much. In this patch, perf llvm transfers LINUX_VERSION_CODE macro
through clang cmdline.

[1] http://lkml.kernel.org/r/[email protected]

Signed-off-by: Wang Nan <[email protected]>
Cc: Arnaldo Carvalho de Melo <[email protected]>
Cc: Alexei Starovoitov <[email protected]>
Cc: Namhyung Kim <[email protected]>
Cc: Zefan Li <[email protected]>
Cc: [email protected]
---
tools/perf/util/llvm-utils.c | 27 +++++++++++++++++++++++++++
1 file changed, 27 insertions(+)

diff --git a/tools/perf/util/llvm-utils.c b/tools/perf/util/llvm-utils.c
index 80eecef..8ee25be 100644
--- a/tools/perf/util/llvm-utils.c
+++ b/tools/perf/util/llvm-utils.c
@@ -12,6 +12,7 @@

#define CLANG_BPF_CMD_DEFAULT_TEMPLATE \
"$CLANG_EXEC -D__KERNEL__ -D__NR_CPUS__=$NR_CPUS "\
+ "-DLINUX_VERSION_CODE=$LINUX_VERSION_CODE " \
"$CLANG_OPTIONS $KERNEL_INC_OPTIONS " \
"-Wno-unused-value -Wno-pointer-sign " \
"-working-directory $WORKING_DIR " \
@@ -324,11 +325,33 @@ get_kbuild_opts(char **kbuild_dir, char **kbuild_include_opts)
pr_debug("include option is set to %s\n", *kbuild_include_opts);
}

+static unsigned long
+fetch_kernel_version(void)
+{
+ struct utsname utsname;
+ int version, patchlevel, sublevel, err;
+
+ if (uname(&utsname))
+ return 0;
+
+ err = sscanf(utsname.release, "%d.%d.%d",
+ &version, &patchlevel, &sublevel);
+
+ if (err != 3) {
+ pr_debug("Unablt to get kernel version from uname '%s'\n",
+ utsname.release);
+ return 0;
+ }
+
+ return (version << 16) + (patchlevel << 8) + sublevel;
+}
+
int llvm__compile_bpf(const char *path, void **p_obj_buf,
size_t *p_obj_buf_sz)
{
int err, nr_cpus_avail;
char clang_path[PATH_MAX], nr_cpus_avail_str[64];
+ char linux_version_code_str[64];
const char *clang_opt = llvm_param.clang_opt;
const char *template = llvm_param.clang_bpf_cmd_template;
char *kbuild_dir = NULL, *kbuild_include_opts = NULL;
@@ -365,7 +388,11 @@ int llvm__compile_bpf(const char *path, void **p_obj_buf,
snprintf(nr_cpus_avail_str, sizeof(nr_cpus_avail_str), "%d",
nr_cpus_avail);

+ snprintf(linux_version_code_str, sizeof(linux_version_code_str),
+ "0x%lx", fetch_kernel_version());
+
force_set_env("NR_CPUS", nr_cpus_avail_str);
+ force_set_env("LINUX_VERSION_CODE", linux_version_code_str);
force_set_env("CLANG_EXEC", clang_path);
force_set_env("CLANG_OPTIONS", clang_opt);
force_set_env("KERNEL_INC_OPTIONS", kbuild_include_opts);
--
1.8.3.4

2015-11-04 11:21:18

by Wang Nan

[permalink] [raw]
Subject: [PATCH 3/4] perf test: Enforce LLVM test: update basic BPF test program

This patch replaces the original toy BPF program with previous introduced
bpf-script-example.c. Dynamically embedded it into 'llvm-src-base.c'.

The newly introduced BPF program attaches a BPF program to
'sys_epoll_pwait()'. perf itself never use that syscall, so further test
can verify their result with it. The program would generate 1 sample
in every 2 calls of epoll_pwait() system call.

Since the resuling BPF object is useful, test_llvm__fetch_bpf_obj() is
introduced for creating BPF objects for source. llvm test is rewritten
according to it.

Signed-off-by: He Kuang <[email protected]>
Signed-off-by: Wang Nan <[email protected]>
Cc: Arnaldo Carvalho de Melo <[email protected]>
Cc: Zefan Li <[email protected]>
Cc: [email protected]
---
tools/perf/tests/Build | 9 ++-
tools/perf/tests/bpf-script-example.c | 4 ++
tools/perf/tests/llvm.c | 131 ++++++++++++++++++++++++++--------
tools/perf/tests/llvm.h | 16 +++++
4 files changed, 129 insertions(+), 31 deletions(-)
create mode 100644 tools/perf/tests/llvm.h

diff --git a/tools/perf/tests/Build b/tools/perf/tests/Build
index 50de225..6c095b3 100644
--- a/tools/perf/tests/Build
+++ b/tools/perf/tests/Build
@@ -31,9 +31,16 @@ perf-y += sample-parsing.o
perf-y += parse-no-sample-id-all.o
perf-y += kmod-path.o
perf-y += thread-map.o
-perf-y += llvm.o
+perf-y += llvm.o llvm-src-base.o
perf-y += topology.o

+$(OUTPUT)tests/llvm-src-base.c: tests/bpf-script-example.c
+ $(call rule_mkdir)
+ $(Q)echo '#include <tests/llvm.h>' > $@
+ $(Q)echo 'const char test_llvm__bpf_base_prog[] =' >> $@
+ $(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-example.c b/tools/perf/tests/bpf-script-example.c
index 410a70b..0ec9c2c 100644
--- a/tools/perf/tests/bpf-script-example.c
+++ b/tools/perf/tests/bpf-script-example.c
@@ -1,3 +1,7 @@
+/*
+ * bpf-script-example.c
+ * Test basic LLVM building
+ */
#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'
diff --git a/tools/perf/tests/llvm.c b/tools/perf/tests/llvm.c
index 8f713f6..f4f1437 100644
--- a/tools/perf/tests/llvm.c
+++ b/tools/perf/tests/llvm.c
@@ -2,6 +2,7 @@
#include <bpf/libbpf.h>
#include <util/llvm-utils.h>
#include <util/cache.h>
+#include "llvm.h"
#include "tests.h"
#include "debug.h"

@@ -11,16 +12,6 @@ static int perf_config_cb(const char *var, const char *val,
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)
{
@@ -28,25 +19,47 @@ static int test__bpf_parsing(void *obj_buf, size_t obj_buf_sz)

obj = bpf_object__open_buffer(obj_buf, obj_buf_sz, NULL);
if (IS_ERR(obj))
- return -1;
+ return TEST_FAIL;
bpf_object__close(obj);
- return 0;
+ return TEST_OK;
}
#else
static int test__bpf_parsing(void *obj_buf __maybe_unused,
size_t obj_buf_sz __maybe_unused)
{
pr_debug("Skip bpf parsing\n");
- return 0;
+ return TEST_OK;
}
#endif

-int test__llvm(void)
+static struct {
+ const char *source;
+ const char *desc;
+} bpf_source_table[__LLVM_TESTCASE_MAX] = {
+ [LLVM_TESTCASE_BASE] = {
+ .source = test_llvm__bpf_base_prog,
+ .desc = "Basic BPF llvm compiling test",
+ },
+};
+
+
+int
+test_llvm__fetch_bpf_obj(void **p_obj_buf,
+ size_t *p_obj_buf_sz,
+ enum test_llvm__testcase index,
+ bool force)
{
- char *tmpl_new, *clang_opt_new;
- void *obj_buf;
- size_t obj_buf_sz;
- int err, old_verbose;
+ const char *source;
+ const char *desc;
+ const char *tmpl_old, *clang_opt_old;
+ char *tmpl_new = NULL, *clang_opt_new = NULL;
+ int err, old_verbose, ret = TEST_FAIL;
+
+ if (index >= __LLVM_TESTCASE_MAX)
+ return TEST_FAIL;
+
+ source = bpf_source_table[index].source;
+ desc = bpf_source_table[index].desc;

perf_config(perf_config_cb, NULL);

@@ -54,42 +67,100 @@ int test__llvm(void)
* Skip this test if user's .perfconfig doesn't set [llvm] section
* and clang is not found in $PATH, and this is not perf test -v
*/
- if (verbose == 0 && !llvm_param.user_set_param && llvm__search_clang()) {
+ if (!force && (verbose == 0 &&
+ !llvm_param.user_set_param &&
+ llvm__search_clang())) {
pr_debug("No clang and no verbosive, skip this test\n");
return TEST_SKIP;
}

- old_verbose = verbose;
/*
* llvm is verbosity when error. Suppress all error output if
* not 'perf test -v'.
*/
+ old_verbose = verbose;
if (verbose == 0)
verbose = -1;

+ *p_obj_buf = NULL;
+ *p_obj_buf_sz = 0;
+
if (!llvm_param.clang_bpf_cmd_template)
- return -1;
+ goto out;

if (!llvm_param.clang_opt)
llvm_param.clang_opt = strdup("");

- err = asprintf(&tmpl_new, "echo '%s' | %s", test_bpf_prog,
- llvm_param.clang_bpf_cmd_template);
+ err = asprintf(&tmpl_new, "echo '%s' | %s%s", source,
+ llvm_param.clang_bpf_cmd_template,
+ old_verbose ? "" : " 2>/dev/null");
if (err < 0)
- return -1;
+ goto out;
err = asprintf(&clang_opt_new, "-xc %s", llvm_param.clang_opt);
if (err < 0)
- return -1;
+ goto out;

+ tmpl_old = llvm_param.clang_bpf_cmd_template;
llvm_param.clang_bpf_cmd_template = tmpl_new;
+ clang_opt_old = llvm_param.clang_opt;
llvm_param.clang_opt = clang_opt_new;
- err = llvm__compile_bpf("-", &obj_buf, &obj_buf_sz);
+
+ err = llvm__compile_bpf("-", p_obj_buf, p_obj_buf_sz);
+
+ llvm_param.clang_bpf_cmd_template = tmpl_old;
+ llvm_param.clang_opt = clang_opt_old;

verbose = old_verbose;
if (err)
- return TEST_FAIL;
+ goto out;
+
+ ret = TEST_OK;
+out:
+ free(tmpl_new);
+ free(clang_opt_new);
+ if (ret != TEST_OK)
+ pr_debug("Failed to compile test case: '%s'\n", desc);
+ return ret;
+}

- err = test__bpf_parsing(obj_buf, obj_buf_sz);
- free(obj_buf);
- return err;
+int test__llvm(void)
+{
+ enum test_llvm__testcase i;
+
+ for (i = 0; i < __LLVM_TESTCASE_MAX; i++) {
+ int ret;
+ void *obj_buf = NULL;
+ size_t obj_buf_sz = 0;
+
+ ret = test_llvm__fetch_bpf_obj(&obj_buf, &obj_buf_sz,
+ i, false);
+
+ if (ret == TEST_OK) {
+ ret = test__bpf_parsing(obj_buf, obj_buf_sz);
+ if (ret != TEST_OK)
+ pr_debug("Failed to parse test case '%s'\n",
+ bpf_source_table[i].desc);
+ }
+ free(obj_buf);
+
+ switch (ret) {
+ case TEST_SKIP:
+ return TEST_SKIP;
+ case TEST_OK:
+ break;
+ default:
+ /*
+ * Test 0 is the basic LLVM test. If test 0
+ * fail, the basic LLVM support not functional
+ * so the whole test should fail. If other test
+ * case fail, it can be fixed by adjusting
+ * config so don't report error.
+ */
+ if (i == 0)
+ return TEST_FAIL;
+ else
+ return TEST_SKIP;
+ }
+ }
+ return TEST_OK;
}
diff --git a/tools/perf/tests/llvm.h b/tools/perf/tests/llvm.h
new file mode 100644
index 0000000..bd63cee
--- /dev/null
+++ b/tools/perf/tests/llvm.h
@@ -0,0 +1,16 @@
+#ifndef PERF_TEST_LLVM_H
+#define PERF_TEST_LLVM_H
+
+#include <stddef.h> /* for size_t */
+#include <stdbool.h> /* for bool */
+
+extern const char test_llvm__bpf_base_prog[];
+
+enum test_llvm__testcase {
+ LLVM_TESTCASE_BASE,
+ __LLVM_TESTCASE_MAX,
+};
+
+int test_llvm__fetch_bpf_obj(void **p_obj_buf, size_t *p_obj_buf_sz,
+ enum test_llvm__testcase index, bool force);
+#endif
--
1.8.3.4

2015-11-04 11:21:15

by Wang Nan

[permalink] [raw]
Subject: [PATCH 4/4] perf test: Enforce LLVM test: add kbuild test

This patch adds a kbuild testcase to check whether kernel headers
can be correctly found.

For example:
# mv /lib/modules/4.3.0-rc5{,.bak}
# perf test LLVM

38: Test LLVM searching and compiling : Skip

# perf test -v LLVM
...
<stdin>:11:10: fatal error: 'uapi/linux/fs.h' file not found
#include <uapi/linux/fs.h>
^
1 error generated.
ERROR: unable to compile -
Hint: Check error message shown above.
Hint: You can also pre-compile it into .o using:
clang -target bpf -O2 -c -
with proper -I and -D options.
Failed to compile test case: 'Test kbuild searching'
test child finished with -2

Signed-off-by: Wang Nan <[email protected]>
Cc: Arnaldo Carvalho de Melo <[email protected]>
Cc: Zefan Li <[email protected]>
Cc: [email protected]
---
tools/perf/tests/Build | 9 ++++++++-
tools/perf/tests/bpf-script-test-kbuild.c | 21 +++++++++++++++++++++
tools/perf/tests/llvm.c | 4 ++++
tools/perf/tests/llvm.h | 2 ++
4 files changed, 35 insertions(+), 1 deletion(-)
create mode 100644 tools/perf/tests/bpf-script-test-kbuild.c

diff --git a/tools/perf/tests/Build b/tools/perf/tests/Build
index 6c095b3..a47b211 100644
--- a/tools/perf/tests/Build
+++ b/tools/perf/tests/Build
@@ -31,7 +31,7 @@ perf-y += sample-parsing.o
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
+perf-y += llvm.o llvm-src-base.o llvm-src-kbuild.o
perf-y += topology.o

$(OUTPUT)tests/llvm-src-base.c: tests/bpf-script-example.c
@@ -41,6 +41,13 @@ $(OUTPUT)tests/llvm-src-base.c: tests/bpf-script-example.c
$(Q)sed -e 's/"/\\"/g' -e 's/\(.*\)/"\1\\n"/g' $< >> $@
$(Q)echo ';' >> $@

+$(OUTPUT)tests/llvm-src-kbuild.c: tests/bpf-script-test-kbuild.c
+ $(call rule_mkdir)
+ $(Q)echo '#include <tests/llvm.h>' > $@
+ $(Q)echo 'const char test_llvm__bpf_test_kbuild_prog[] =' >> $@
+ $(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-kbuild.c b/tools/perf/tests/bpf-script-test-kbuild.c
new file mode 100644
index 0000000..3626924
--- /dev/null
+++ b/tools/perf/tests/bpf-script-test-kbuild.c
@@ -0,0 +1,21 @@
+/*
+ * bpf-script-test-kbuild.c
+ * Test include from kernel header
+ */
+#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 SEC(NAME) __attribute__((section(NAME), used))
+
+#include <uapi/linux/fs.h>
+#include <uapi/asm/ptrace.h>
+
+SEC("func=vfs_llseek")
+int bpf_func__vfs_llseek(void *ctx)
+{
+ return 0;
+}
+
+char _license[] SEC("license") = "GPL";
+int _version SEC("version") = LINUX_VERSION_CODE;
diff --git a/tools/perf/tests/llvm.c b/tools/perf/tests/llvm.c
index f4f1437..6941e66 100644
--- a/tools/perf/tests/llvm.c
+++ b/tools/perf/tests/llvm.c
@@ -40,6 +40,10 @@ static struct {
.source = test_llvm__bpf_base_prog,
.desc = "Basic BPF llvm compiling test",
},
+ [LLVM_TESTCASE_KBUILD] = {
+ .source = test_llvm__bpf_test_kbuild_prog,
+ .desc = "Test kbuild searching",
+ },
};


diff --git a/tools/perf/tests/llvm.h b/tools/perf/tests/llvm.h
index bd63cee..d91d8f4 100644
--- a/tools/perf/tests/llvm.h
+++ b/tools/perf/tests/llvm.h
@@ -5,9 +5,11 @@
#include <stdbool.h> /* for bool */

extern const char test_llvm__bpf_base_prog[];
+extern const char test_llvm__bpf_test_kbuild_prog[];

enum test_llvm__testcase {
LLVM_TESTCASE_BASE,
+ LLVM_TESTCASE_KBUILD,
__LLVM_TESTCASE_MAX,
};

--
1.8.3.4

2015-11-04 22:10:04

by Arnaldo Carvalho de Melo

[permalink] [raw]
Subject: Re: [PATCH 1/4] perf tools: Pass available CPU number to clang compiler

Em Wed, Nov 04, 2015 at 11:20:04AM +0000, Wang Nan escreveu:
> This patch introduces a new macro "__NR_CPUS__" to perf's embedded
> clang compiler, which represent the available CPU counters in this

available "CPU counters"? ENOPARSE :-)

> system. BPF program can use this macro to create a map with same
> number of system CPUs. For exmaple:
example
>
> struct bpf_map_def SEC("maps") pmu_map = {
> .type = BPF_MAP_TYPE_PERF_EVENT_ARRAY,
> .key_size = sizeof(int),
> .value_size = sizeof(u32),
> .max_entries = __NR_CPUS__,
> };

I wonder if we shouldn't use the getconf() parameter here, i.e. define
_SC_NPROCESSORS_CONF and also provide _SC_NPROCESSORS_ONLN.

The kernel uses NR_CPUS, for accessing CONFIG_NR_CPUS, Alexei, what do
you think?

- Arnaldo

> Signed-off-by: Wang Nan <[email protected]>
> Cc: Arnaldo Carvalho de Melo <[email protected]>
> Cc: Alexei Starovoitov <[email protected]>
> Cc: Namhyung Kim <[email protected]>
> Cc: Zefan Li <[email protected]>
> Cc: [email protected]
> ---
> tools/perf/util/llvm-utils.c | 24 ++++++++++++++++++------
> 1 file changed, 18 insertions(+), 6 deletions(-)
>
> diff --git a/tools/perf/util/llvm-utils.c b/tools/perf/util/llvm-utils.c
> index 4f6a478..80eecef 100644
> --- a/tools/perf/util/llvm-utils.c
> +++ b/tools/perf/util/llvm-utils.c
> @@ -11,10 +11,11 @@
> #include "cache.h"
>
> #define CLANG_BPF_CMD_DEFAULT_TEMPLATE \
> - "$CLANG_EXEC -D__KERNEL__ $CLANG_OPTIONS " \
> - "$KERNEL_INC_OPTIONS -Wno-unused-value " \
> - "-Wno-pointer-sign -working-directory " \
> - "$WORKING_DIR -c \"$CLANG_SOURCE\" -target bpf -O2 -o -"
> + "$CLANG_EXEC -D__KERNEL__ -D__NR_CPUS__=$NR_CPUS "\
> + "$CLANG_OPTIONS $KERNEL_INC_OPTIONS " \
> + "-Wno-unused-value -Wno-pointer-sign " \
> + "-working-directory $WORKING_DIR " \
> + "-c \"$CLANG_SOURCE\" -target bpf -O2 -o -"
>
> struct llvm_param llvm_param = {
> .clang_path = "clang",
> @@ -326,8 +327,8 @@ get_kbuild_opts(char **kbuild_dir, char **kbuild_include_opts)
> int llvm__compile_bpf(const char *path, void **p_obj_buf,
> size_t *p_obj_buf_sz)
> {
> - int err;
> - char clang_path[PATH_MAX];
> + int err, nr_cpus_avail;
> + char clang_path[PATH_MAX], nr_cpus_avail_str[64];
> const char *clang_opt = llvm_param.clang_opt;
> const char *template = llvm_param.clang_bpf_cmd_template;
> char *kbuild_dir = NULL, *kbuild_include_opts = NULL;
> @@ -354,6 +355,17 @@ int llvm__compile_bpf(const char *path, void **p_obj_buf,
> */
> get_kbuild_opts(&kbuild_dir, &kbuild_include_opts);
>
> + nr_cpus_avail = sysconf(_SC_NPROCESSORS_CONF);
> + if (nr_cpus_avail <= 0) {
> + pr_err(
> +"WARNING:\tunable to get available CPUs in this system: %s\n"
> +" \tUse 128 instead.\n", strerror(errno));
> + nr_cpus_avail = 128;
> + }
> + snprintf(nr_cpus_avail_str, sizeof(nr_cpus_avail_str), "%d",
> + nr_cpus_avail);
> +
> + force_set_env("NR_CPUS", nr_cpus_avail_str);
> force_set_env("CLANG_EXEC", clang_path);
> force_set_env("CLANG_OPTIONS", clang_opt);
> force_set_env("KERNEL_INC_OPTIONS", kbuild_include_opts);
> --
> 1.8.3.4

2015-11-04 22:23:28

by Arnaldo Carvalho de Melo

[permalink] [raw]
Subject: Re: [PATCH 2/4] perf tools: Pass LINUX_VERSION_CODE to BPF program when compiling

Em Wed, Nov 04, 2015 at 11:20:05AM +0000, Wang Nan escreveu:
> Arnaldo suggests to make LINUX_VERSION_CODE works like __func__ and
> __FILE__ so user don't need to care setting right linux version
> too much. In this patch, perf llvm transfers LINUX_VERSION_CODE macro
> through clang cmdline.
>
> [1] http://lkml.kernel.org/r/[email protected]

Tested, updated the comment, applied and pushed to my perf/core branch,
please continue from there, I'll try to push it tomorrow.

- Arnaldo

> Signed-off-by: Wang Nan <[email protected]>
> Cc: Arnaldo Carvalho de Melo <[email protected]>
> Cc: Alexei Starovoitov <[email protected]>
> Cc: Namhyung Kim <[email protected]>
> Cc: Zefan Li <[email protected]>
> Cc: [email protected]
> ---
> tools/perf/util/llvm-utils.c | 27 +++++++++++++++++++++++++++
> 1 file changed, 27 insertions(+)
>
> diff --git a/tools/perf/util/llvm-utils.c b/tools/perf/util/llvm-utils.c
> index 80eecef..8ee25be 100644
> --- a/tools/perf/util/llvm-utils.c
> +++ b/tools/perf/util/llvm-utils.c
> @@ -12,6 +12,7 @@
>
> #define CLANG_BPF_CMD_DEFAULT_TEMPLATE \
> "$CLANG_EXEC -D__KERNEL__ -D__NR_CPUS__=$NR_CPUS "\
> + "-DLINUX_VERSION_CODE=$LINUX_VERSION_CODE " \
> "$CLANG_OPTIONS $KERNEL_INC_OPTIONS " \
> "-Wno-unused-value -Wno-pointer-sign " \
> "-working-directory $WORKING_DIR " \
> @@ -324,11 +325,33 @@ get_kbuild_opts(char **kbuild_dir, char **kbuild_include_opts)
> pr_debug("include option is set to %s\n", *kbuild_include_opts);
> }
>
> +static unsigned long
> +fetch_kernel_version(void)
> +{
> + struct utsname utsname;
> + int version, patchlevel, sublevel, err;
> +
> + if (uname(&utsname))
> + return 0;
> +
> + err = sscanf(utsname.release, "%d.%d.%d",
> + &version, &patchlevel, &sublevel);
> +
> + if (err != 3) {
> + pr_debug("Unablt to get kernel version from uname '%s'\n",
> + utsname.release);
> + return 0;
> + }
> +
> + return (version << 16) + (patchlevel << 8) + sublevel;
> +}
> +
> int llvm__compile_bpf(const char *path, void **p_obj_buf,
> size_t *p_obj_buf_sz)
> {
> int err, nr_cpus_avail;
> char clang_path[PATH_MAX], nr_cpus_avail_str[64];
> + char linux_version_code_str[64];
> const char *clang_opt = llvm_param.clang_opt;
> const char *template = llvm_param.clang_bpf_cmd_template;
> char *kbuild_dir = NULL, *kbuild_include_opts = NULL;
> @@ -365,7 +388,11 @@ int llvm__compile_bpf(const char *path, void **p_obj_buf,
> snprintf(nr_cpus_avail_str, sizeof(nr_cpus_avail_str), "%d",
> nr_cpus_avail);
>
> + snprintf(linux_version_code_str, sizeof(linux_version_code_str),
> + "0x%lx", fetch_kernel_version());
> +
> force_set_env("NR_CPUS", nr_cpus_avail_str);
> + force_set_env("LINUX_VERSION_CODE", linux_version_code_str);
> force_set_env("CLANG_EXEC", clang_path);
> force_set_env("CLANG_OPTIONS", clang_opt);
> force_set_env("KERNEL_INC_OPTIONS", kbuild_include_opts);
> --
> 1.8.3.4

2015-11-04 22:24:05

by Arnaldo Carvalho de Melo

[permalink] [raw]
Subject: Re: [PATCH 3/4] perf test: Enforce LLVM test: update basic BPF test program

Em Wed, Nov 04, 2015 at 11:20:06AM +0000, Wang Nan escreveu:
> This patch replaces the original toy BPF program with previous introduced
> bpf-script-example.c. Dynamically embedded it into 'llvm-src-base.c'.
>
> The newly introduced BPF program attaches a BPF program to
> 'sys_epoll_pwait()'. perf itself never use that syscall, so further test
> can verify their result with it. The program would generate 1 sample
> in every 2 calls of epoll_pwait() system call.
>
> Since the resuling BPF object is useful, test_llvm__fetch_bpf_obj() is
> introduced for creating BPF objects for source. llvm test is rewritten
> according to it.


[acme@zoo linux]$ am /wb/1.patch
Applying: perf test: Enforce LLVM test: update basic BPF test program
/home/acme/git/linux/.git/rebase-apply/patch:149: space before tab in indent.
*p_obj_buf = NULL;
/home/acme/git/linux/.git/rebase-apply/patch:150: space before tab in indent.
*p_obj_buf_sz = 0;
error: patch failed: tools/perf/tests/llvm.c:28
error: tools/perf/tests/llvm.c: patch does not apply
Patch failed at 0001 perf test: Enforce LLVM test: update basic BPF test program
The copy of the patch that failed is found in:
/home/acme/git/linux/.git/rebase-apply/patch
When you have resolved this problem, run "git am --continue".
If you prefer to skip this patch, run "git am --skip" instead.
To restore the original branch and stop patching, run "git am --abort".
[acme@zoo linux]$

2015-11-05 01:41:58

by Wang Nan

[permalink] [raw]
Subject: Re: [PATCH 1/4] perf tools: Pass available CPU number to clang compiler



On 2015/11/5 6:09, Arnaldo Carvalho de Melo wrote:
> Em Wed, Nov 04, 2015 at 11:20:04AM +0000, Wang Nan escreveu:
>> This patch introduces a new macro "__NR_CPUS__" to perf's embedded
>> clang compiler, which represent the available CPU counters in this
> available "CPU counters"? ENOPARSE :-)

Your changing is better.

This patch is for support Alexei's BPF output and Xia Kaixu's BPF
pmu reading. In these cases we need to create an array with
same number of slots with system CPUs.

Thank you.

Subject: [tip:perf/urgent] perf llvm: Pass number of configured CPUs to clang compiler

Commit-ID: 59f41af980f95cbd556a6dc2e064b412abc439cf
Gitweb: http://git.kernel.org/tip/59f41af980f95cbd556a6dc2e064b412abc439cf
Author: Wang Nan <[email protected]>
AuthorDate: Wed, 4 Nov 2015 11:20:04 +0000
Committer: Arnaldo Carvalho de Melo <[email protected]>
CommitDate: Thu, 5 Nov 2015 12:47:02 -0300

perf llvm: Pass number of configured CPUs to clang compiler

This patch introduces a new macro "__NR_CPUS__" to perf's embedded clang
compiler, which represent the number of configured CPUs in this system.
BPF programs can use this macro to create a map with the same number of
system CPUs. For example:

struct bpf_map_def SEC("maps") pmu_map = {
.type = BPF_MAP_TYPE_PERF_EVENT_ARRAY,
.key_size = sizeof(int),
.value_size = sizeof(u32),
.max_entries = __NR_CPUS__,
};

Signed-off-by: Wang Nan <[email protected]>
Cc: Alexei Starovoitov <[email protected]>
Cc: Namhyung Kim <[email protected]>
Cc: Zefan Li <[email protected]>
Cc: [email protected]
Link: http://lkml.kernel.org/r/[email protected]
Signed-off-by: Arnaldo Carvalho de Melo <[email protected]>
---
tools/perf/util/llvm-utils.c | 24 ++++++++++++++++++------
1 file changed, 18 insertions(+), 6 deletions(-)

diff --git a/tools/perf/util/llvm-utils.c b/tools/perf/util/llvm-utils.c
index 4f6a478..80eecef 100644
--- a/tools/perf/util/llvm-utils.c
+++ b/tools/perf/util/llvm-utils.c
@@ -11,10 +11,11 @@
#include "cache.h"

#define CLANG_BPF_CMD_DEFAULT_TEMPLATE \
- "$CLANG_EXEC -D__KERNEL__ $CLANG_OPTIONS " \
- "$KERNEL_INC_OPTIONS -Wno-unused-value " \
- "-Wno-pointer-sign -working-directory " \
- "$WORKING_DIR -c \"$CLANG_SOURCE\" -target bpf -O2 -o -"
+ "$CLANG_EXEC -D__KERNEL__ -D__NR_CPUS__=$NR_CPUS "\
+ "$CLANG_OPTIONS $KERNEL_INC_OPTIONS " \
+ "-Wno-unused-value -Wno-pointer-sign " \
+ "-working-directory $WORKING_DIR " \
+ "-c \"$CLANG_SOURCE\" -target bpf -O2 -o -"

struct llvm_param llvm_param = {
.clang_path = "clang",
@@ -326,8 +327,8 @@ get_kbuild_opts(char **kbuild_dir, char **kbuild_include_opts)
int llvm__compile_bpf(const char *path, void **p_obj_buf,
size_t *p_obj_buf_sz)
{
- int err;
- char clang_path[PATH_MAX];
+ int err, nr_cpus_avail;
+ char clang_path[PATH_MAX], nr_cpus_avail_str[64];
const char *clang_opt = llvm_param.clang_opt;
const char *template = llvm_param.clang_bpf_cmd_template;
char *kbuild_dir = NULL, *kbuild_include_opts = NULL;
@@ -354,6 +355,17 @@ int llvm__compile_bpf(const char *path, void **p_obj_buf,
*/
get_kbuild_opts(&kbuild_dir, &kbuild_include_opts);

+ nr_cpus_avail = sysconf(_SC_NPROCESSORS_CONF);
+ if (nr_cpus_avail <= 0) {
+ pr_err(
+"WARNING:\tunable to get available CPUs in this system: %s\n"
+" \tUse 128 instead.\n", strerror(errno));
+ nr_cpus_avail = 128;
+ }
+ snprintf(nr_cpus_avail_str, sizeof(nr_cpus_avail_str), "%d",
+ nr_cpus_avail);
+
+ force_set_env("NR_CPUS", nr_cpus_avail_str);
force_set_env("CLANG_EXEC", clang_path);
force_set_env("CLANG_OPTIONS", clang_opt);
force_set_env("KERNEL_INC_OPTIONS", kbuild_include_opts);

Subject: [tip:perf/urgent] perf llvm: Pass LINUX_VERSION_CODE to BPF program when compiling

Commit-ID: 4a4f66a1a7031675745812729ade94ad1caf1db6
Gitweb: http://git.kernel.org/tip/4a4f66a1a7031675745812729ade94ad1caf1db6
Author: Wang Nan <[email protected]>
AuthorDate: Wed, 4 Nov 2015 11:20:05 +0000
Committer: Arnaldo Carvalho de Melo <[email protected]>
CommitDate: Thu, 5 Nov 2015 12:47:50 -0300

perf llvm: Pass LINUX_VERSION_CODE to BPF program when compiling

Arnaldo suggests to make LINUX_VERSION_CODE works like __func__ and
__FILE__ so user don't need to care setting right linux version too
much. In this patch, perf llvm transfers LINUX_VERSION_CODE macro
through clang cmdline.

[1] http://lkml.kernel.org/r/[email protected]

Committer notes:

Before, forgetting to update the version:

# uname -r
4.3.0-rc1+
# cat bpf.c
__attribute__((section("fork=_do_fork"), used))
int fork(void *ctx)
{
return 1;
}

char _license[] __attribute__((section("license"), used)) = "GPL";
int _version __attribute__((section("version"), used)) = 0x40200;
#
# perf record -e bpf.c sleep 1
event syntax error: 'bpf.c'
\___ Invalid argument: Are you root and runing a CONFIG_BPF_SYSCALL kernel?

(add -v to see detail)
Run 'perf list' for a list of valid events

Usage: perf record [<options>] [<command>]
or: perf record [<options>] -- <command> [<options>]

-e, --event <event> event selector. use 'perf list' to list available events
#

After:

# grep version bpf.c
int _version __attribute__((section("version"), used)) = LINUX_VERSION_CODE;
# perf record -e bpf.c sleep 1
[ perf record: Woken up 1 times to write data ]
[ perf record: Captured and wrote 0.017 MB perf.data ]
# perf evlist -v
perf_bpf_probe:fork: type: 2, size: 112, config: 0x5ee, { sample_period,
sample_freq }: 1, sample_type: IP|TID|TIME|CPU|PERIOD|RAW, disabled: 1,
inherit: 1, mmap: 1, comm: 1, enable_on_exec: 1, task: 1, sample_id_all:
1, exclude_guest: 1, mmap2: 1, comm_exec: 1
#

Suggested-and-Tested-by: Arnaldo Carvalho de Melo <[email protected]>
Signed-off-by: Wang Nan <[email protected]>
Cc: Alexei Starovoitov <[email protected]>
Cc: Namhyung Kim <[email protected]>
Cc: Zefan Li <[email protected]>
Cc: [email protected]
Link: http://lkml.kernel.org/r/[email protected]
Signed-off-by: Arnaldo Carvalho de Melo <[email protected]>
---
tools/perf/util/llvm-utils.c | 27 +++++++++++++++++++++++++++
1 file changed, 27 insertions(+)

diff --git a/tools/perf/util/llvm-utils.c b/tools/perf/util/llvm-utils.c
index 80eecef..8ee25be 100644
--- a/tools/perf/util/llvm-utils.c
+++ b/tools/perf/util/llvm-utils.c
@@ -12,6 +12,7 @@

#define CLANG_BPF_CMD_DEFAULT_TEMPLATE \
"$CLANG_EXEC -D__KERNEL__ -D__NR_CPUS__=$NR_CPUS "\
+ "-DLINUX_VERSION_CODE=$LINUX_VERSION_CODE " \
"$CLANG_OPTIONS $KERNEL_INC_OPTIONS " \
"-Wno-unused-value -Wno-pointer-sign " \
"-working-directory $WORKING_DIR " \
@@ -324,11 +325,33 @@ get_kbuild_opts(char **kbuild_dir, char **kbuild_include_opts)
pr_debug("include option is set to %s\n", *kbuild_include_opts);
}

+static unsigned long
+fetch_kernel_version(void)
+{
+ struct utsname utsname;
+ int version, patchlevel, sublevel, err;
+
+ if (uname(&utsname))
+ return 0;
+
+ err = sscanf(utsname.release, "%d.%d.%d",
+ &version, &patchlevel, &sublevel);
+
+ if (err != 3) {
+ pr_debug("Unablt to get kernel version from uname '%s'\n",
+ utsname.release);
+ return 0;
+ }
+
+ return (version << 16) + (patchlevel << 8) + sublevel;
+}
+
int llvm__compile_bpf(const char *path, void **p_obj_buf,
size_t *p_obj_buf_sz)
{
int err, nr_cpus_avail;
char clang_path[PATH_MAX], nr_cpus_avail_str[64];
+ char linux_version_code_str[64];
const char *clang_opt = llvm_param.clang_opt;
const char *template = llvm_param.clang_bpf_cmd_template;
char *kbuild_dir = NULL, *kbuild_include_opts = NULL;
@@ -365,7 +388,11 @@ int llvm__compile_bpf(const char *path, void **p_obj_buf,
snprintf(nr_cpus_avail_str, sizeof(nr_cpus_avail_str), "%d",
nr_cpus_avail);

+ snprintf(linux_version_code_str, sizeof(linux_version_code_str),
+ "0x%lx", fetch_kernel_version());
+
force_set_env("NR_CPUS", nr_cpus_avail_str);
+ force_set_env("LINUX_VERSION_CODE", linux_version_code_str);
force_set_env("CLANG_EXEC", clang_path);
force_set_env("CLANG_OPTIONS", clang_opt);
force_set_env("KERNEL_INC_OPTIONS", kbuild_include_opts);