Skip to contentSkip to conversation

perf tools: Improve llvm compiling and test

PATCH In review Wang Nan 3 participants 11 messages Nov 4, 2015 – Nov 8, 2015 perf events

lore
Share & tools
lore

Wang Nan

author
#0 lore raw

cover 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
Signature · 1 line
1.8.3.4
author
#1 lore raw

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__,
};

Cc Arnaldo Carvalho de Melo <[email protected]>
Cc Alexei Starovoitov <[email protected]>
Cc Namhyung Kim <[email protected]>
Cc Zefan Li <[email protected]>
---
 tools/perf/util/llvm-utils.c | 24 ++++++++++++++++++------
 1 file changed, 18 insertions(+), 6 deletions(-)
tools/perf/util/llvm-utils.c +18 -6
index 4f6a478..80eecef 100644
@@ -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);
Signature · 1 line
1.8.3.4
#2 lore raw

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:

Quoted 2 lines

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 :-)

Quoted 2 lines

system. BPF program can use this macro to create a map with same number of system CPUs. For exmaple:

example

Quoted 7 lines

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

Quoted 61 lines
Cc Arnaldo Carvalho de Melo <[email protected]>
Cc Alexei Starovoitov <[email protected]>
Cc Namhyung Kim <[email protected]>
Cc Zefan Li <[email protected]>
---
 tools/perf/util/llvm-utils.c | 24 ++++++++++++++++++------
 1 file changed, 18 insertions(+), 6 deletions(-)
tools/perf/util/llvm-utils.c +18 -6
index 4f6a478..80eecef 100644
@@ -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);
Signature · 1 line
1.8.3.4
#3 lore raw

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

Quoted 4 lines · Arnaldo Carvalho de Melo

Em Wed, Nov 04, 2015 at 11:20:04AM +0000, Wang Nan escreveu:

Quoted 2 lines

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.

#4 lore raw

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

In reply to Wang Nan
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__,
};

---
 tools/perf/util/llvm-utils.c | 24 ++++++++++++++++++------
 1 file changed, 18 insertions(+), 6 deletions(-)
tools/perf/util/llvm-utils.c +18 -6
index 4f6a478..80eecef 100644
@@ -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);
author
#5 lore raw

PATCH 2/4 perf tools: Pass LINUX_VERSION_CODE to BPF program when compiling

In reply to Wang Nan

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]

Cc Arnaldo Carvalho de Melo <[email protected]>
Cc Alexei Starovoitov <[email protected]>
Cc Namhyung Kim <[email protected]>
Cc Zefan Li <[email protected]>
---
 tools/perf/util/llvm-utils.c | 27 +++++++++++++++++++++++++++
 1 file changed, 27 insertions(+)
tools/perf/util/llvm-utils.c +27 -0
index 80eecef..8ee25be 100644
@@ -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);
Signature · 1 line
1.8.3.4
#6 lore raw

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:

Quoted 6 lines

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

Quoted 70 lines
Cc Arnaldo Carvalho de Melo <[email protected]>
Cc Alexei Starovoitov <[email protected]>
Cc Namhyung Kim <[email protected]>
Cc Zefan Li <[email protected]>
---
 tools/perf/util/llvm-utils.c | 27 +++++++++++++++++++++++++++
 1 file changed, 27 insertions(+)
tools/perf/util/llvm-utils.c +27 -0
index 80eecef..8ee25be 100644
@@ -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);
Signature · 1 line
1.8.3.4
#7 lore raw

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

In reply to Wang Nan
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
  #
---
 tools/perf/util/llvm-utils.c | 27 +++++++++++++++++++++++++++
 1 file changed, 27 insertions(+)
tools/perf/util/llvm-utils.c +27 -0
index 80eecef..8ee25be 100644
@@ -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);
author
#8 lore raw

PATCH 3/4 perf test: Enforce LLVM test: update basic BPF test program

In reply to Wang Nan

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.

Cc Arnaldo Carvalho de Melo <[email protected]>
Cc Zefan Li <[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
tools/perf/tests/Build +8 -1
index 50de225..6c095b3 100644
@@ -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
tools/perf/tests/bpf-script-example.c +4 -0
index 410a70b..0ec9c2c 100644
@@ -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'
tools/perf/tests/llvm.c +101 -30

190 lines not on this page · raw message

tools/perf/tests/llvm.h new file +16 -0
new file mode 100644
index 0000000..bd63cee
@@ -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
Signature · 1 line
1.8.3.4
#9 lore raw

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:

Quoted 11 lines

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]$

author
#10 lore raw

PATCH 4/4 perf test: Enforce LLVM test: add kbuild test

In reply to Wang Nan

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
Cc Arnaldo Carvalho de Melo <[email protected]>
Cc Zefan Li <[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
tools/perf/tests/Build +8 -1
index 6c095b3..a47b211 100644
@@ -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
tools/perf/tests/bpf-script-test-kbuild.c new file +21 -0
new file mode 100644
index 0000000..3626924
@@ -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;
tools/perf/tests/llvm.c +4 -0
index f4f1437..6941e66 100644
@@ -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",
+	},
 };
 
 
tools/perf/tests/llvm.h +2 -0
index bd63cee..d91d8f4 100644
@@ -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,
 };
 
Signature · 1 line
1.8.3.4