2016-11-15 04:08:18

by Wang Nan

[permalink] [raw]
Subject: [PATCH 00/34] perf clang: Builtin clang and perfhook support

This is version 2 of perf builtin clang patch series. Compare to v1,
add an exciting feature: jit compiling perf hook functions. This
features allows script writer report result through BPF map in a
customized way.

At the end of this cover letter lists an example shows how to capture
and report a syscall histogram.

This patchset is based on current perf/core.

In this patchset:

Patch 1 - 4 are bugfixes left in my local tree.
Patch 5 - 7 are preparation in libbpf.
Patch 8 - 9 introduce perf hook to perf.
Patch 10 - 21 are patches support builtin clang. Some of then are already
collected by Arnaldo in tmp.perf/builtin-clang. They are slightly
adjusted in this v2 series.
Patch 22 - 29 add JIT compiling to builting clang and add them to perf hook.
Patch 30 - 34 are easy of use improvements. 1) builtin clang defines macros
and helpers by default using builtin include headers. Default
headers can be turned off by -UBUILTIN_CLANG_DEFAULT_INCLUDE.
2) allow JITted perf hooks access more functions in libc.

Example: build syscall histogram, exclude syscalls issued by perf itself.
Please note following improvements:
1. Don't need define many bpf helpers by hand.
See bpf_map_lookup_elem and bpf_get_current_pid_tgid.
2. See how pid of git is passed to BPF script attached to
raw_syscalls:sys_enter.

$ cat ./count_syscalls.c
typedef unsigned long u64;

#define BPF_MAP_TYPE_HASH 1
#define BPF_MAP_TYPE_ARRAY 2

enum GVAL {
G_perf_pid,
NR_GVALS
};

struct bpf_map_def SEC("maps") GVALS = {
.type = BPF_MAP_TYPE_ARRAY,
.key_size = sizeof(int),
.value_size = sizeof(u64),
.max_entries = NR_GVALS,
};

struct bpf_map_def SEC("maps") syscall_counter = {
.type = BPF_MAP_TYPE_HASH,
.key_size = sizeof(u64),
.value_size = sizeof(u64),
.max_entries = 512,
};

SEC("raw_syscalls:sys_enter")
int func(void *ctx)
{
int key = G_perf_pid;
u64 id = *((u64 *)(ctx + 8));
int self_pid = bpf_get_current_pid_tgid() & 0xffffffff;
int *perf_pid = bpf_map_lookup_elem(&GVALS, &key);
u64 *counter;

if (!perf_pid)
return 0;
if (*perf_pid == self_pid)
return 0;
counter = bpf_map_lookup_elem(&syscall_counter, &id);
if (!counter) {
u64 value = 1;
bpf_map_update_elem(&syscall_counter, &id, &value, 0);
return 0;
}
__sync_fetch_and_add(counter, 1);
return 0;
}

SEC("perfhook:record_start")
void record_start(void *ctx)
{
int perf_pid = getpid(), key = G_perf_pid;
printf("Start count, perfpid=%d\n", perf_pid);
jit_helper__map_update_elem(ctx, &GVALS, &key, &perf_pid, 0);
}

SEC("perfhook:record_end")
void record_end(void *ctx)
{
u64 key = -1, value;
while (!jit_helper__map_get_next_key(ctx, &syscall_counter, &key, &key)) {
jit_helper__map_lookup_elem(ctx, &syscall_counter, &key, &value);
printf("syscall %ld\tcount: %ld\n", (long)key, (long)value);
}
}
char _license[] SEC("license") = "GPL";
int _version SEC("version") = LINUX_VERSION_CODE;

$ sudo -s
# ulimit -l unlimited
# perf record -e ./count_syscalls.c echo "Haha"
Start count, perfpid=25209
Haha
[ perf record: Woken up 1 times to write data ]
syscall 8 count: 6
syscall 11 count: 1
syscall 4 count: 6
syscall 21 count: 1
syscall 5 count: 3
syscall 231 count: 1
syscall 45 count: 3
syscall 0 count: 24
syscall 257 count: 1
syscall 59 count: 4
syscall 23 count: 9
syscall 78 count: 2
syscall 41 count: 4
syscall 72 count: 8
syscall 10 count: 3
syscall 321 count: 1
syscall 298 count: 7
syscall 16 count: 21
syscall 9 count: 16
syscall 1 count: 114
syscall 12 count: 3
syscall 14 count: 35
syscall 158 count: 1
syscall 2 count: 15
syscall 7 count: 18
syscall 3 count: 11
[ perf record: Captured and wrote 0.011 MB perf.data ]

Eric Leblond (1):
tools lib bpf: fix maps resolution

Wang Nan (33):
perf tools: Fix kernel version error in ubuntu
perf record: Fix segfault when running with suid and kptr_restrict is
1
tools perf: Add missing struct defeinition in probe_event.h
tools lib bpf: Add missing bpf map functions
tools lib bpf: Add private field for bpf_object
tools lib bpf: Retrive bpf_map through offset of bpf_map_def
perf tools: Introduce perf hooks
perf tools: Pass context to perf hook functions
perf llvm: Extract helpers in llvm-utils.c
tools build: Add feature detection for LLVM
tools build: Add feature detection for clang
perf build: Add clang and llvm compile and linking support
perf clang: Add builtin clang support ant test case
perf clang: Use real file system for #include
perf clang: Allow passing CFLAGS to builtin clang
perf clang: Update test case to use real BPF script
perf clang: Support compile IR to BPF object and add testcase
perf clang: Compile BPF script use builtin clang support
perf clang: Pass full path to builtin clang
perf clang: Pass CFLAGS to builtin clang
perf clang jit: Wrap llvm::Module using PerfModule
perf clang jit: Insignt BPF and JIT functions in a Module
perf clang jit: add PerfModule::doJIT to JIT perfhook functions.
perf clang jit: Export functions for jitted code
perf clang jit: Actually JIT and hook in bpf loader
perf clang jit: Collect the lowest address in maps section as map_base
perf clang jit: Access BPF map
perf clang jit: Allow jitted perf hook access BPF maps
perf clang: Link BPF functions declaration into perf
perf clang: Declare BPF functions for BPF scripts automatically
perf clang: Include helpers to BPF scripts
perf clang builtin: Define hook helpers by default
perf clang git: Export getpid() to perf hook

tools/build/feature/Makefile | 18 +
tools/build/feature/test-clang.cpp | 21 ++
tools/build/feature/test-llvm.cpp | 8 +
tools/lib/bpf/bpf.c | 56 +++
tools/lib/bpf/bpf.h | 7 +
tools/lib/bpf/libbpf.c | 177 ++++++---
tools/lib/bpf/libbpf.h | 13 +
tools/perf/Makefile.config | 62 +++-
tools/perf/Makefile.perf | 23 +-
tools/perf/builtin-record.c | 11 +
tools/perf/tests/Build | 4 +-
tools/perf/tests/bpf-script-example.c | 30 +-
tools/perf/tests/bpf-script-test-kbuild.c | 2 +
tools/perf/tests/bpf-script-test-prologue.c | 6 +-
tools/perf/tests/bpf-script-test-relocation.c | 17 +-
tools/perf/tests/builtin-test.c | 13 +
tools/perf/tests/clang.c | 50 +++
tools/perf/tests/llvm.h | 7 +
tools/perf/tests/make | 2 +
tools/perf/tests/perf-hooks.c | 48 +++
tools/perf/tests/tests.h | 4 +
tools/perf/util/Build | 5 +
tools/perf/util/bpf-loader.c | 102 +++++-
tools/perf/util/bpf-loader.h | 19 +
tools/perf/util/c++/Build | 4 +
tools/perf/util/c++/bpf-funcs-str.c | 228 ++++++++++++
tools/perf/util/c++/bpf-helper-str.c | 24 ++
tools/perf/util/c++/clang-bpf-includes.h | 13 +
tools/perf/util/c++/clang-c.h | 63 ++++
tools/perf/util/c++/clang-test.cpp | 99 +++++
tools/perf/util/c++/clang.cpp | 498 ++++++++++++++++++++++++++
tools/perf/util/c++/clang.h | 61 ++++
tools/perf/util/jit-helpers.c | 60 ++++
tools/perf/util/jit-helpers.h | 30 ++
tools/perf/util/llvm-utils.c | 76 +++-
tools/perf/util/llvm-utils.h | 15 +-
tools/perf/util/perf-hooks-list.h | 3 +
tools/perf/util/perf-hooks.c | 88 +++++
tools/perf/util/perf-hooks.h | 39 ++
tools/perf/util/probe-event.h | 2 +
tools/perf/util/symbol.c | 2 +-
tools/perf/util/util-cxx.h | 26 ++
tools/perf/util/util.c | 55 ++-
43 files changed, 1991 insertions(+), 100 deletions(-)
create mode 100644 tools/build/feature/test-clang.cpp
create mode 100644 tools/build/feature/test-llvm.cpp
create mode 100644 tools/perf/tests/clang.c
create mode 100644 tools/perf/tests/perf-hooks.c
create mode 100644 tools/perf/util/c++/Build
create mode 100644 tools/perf/util/c++/bpf-funcs-str.c
create mode 100644 tools/perf/util/c++/bpf-helper-str.c
create mode 100644 tools/perf/util/c++/clang-bpf-includes.h
create mode 100644 tools/perf/util/c++/clang-c.h
create mode 100644 tools/perf/util/c++/clang-test.cpp
create mode 100644 tools/perf/util/c++/clang.cpp
create mode 100644 tools/perf/util/c++/clang.h
create mode 100644 tools/perf/util/jit-helpers.c
create mode 100644 tools/perf/util/jit-helpers.h
create mode 100644 tools/perf/util/perf-hooks-list.h
create mode 100644 tools/perf/util/perf-hooks.c
create mode 100644 tools/perf/util/perf-hooks.h
create mode 100644 tools/perf/util/util-cxx.h

Signed-off-by: Wang Nan <[email protected]>
Cc: Arnaldo Carvalho de Melo <[email protected]>
Cc: Alexei Starovoitov <[email protected]>
Cc: He Kuang <[email protected]>
Cc: Jiri Olsa <[email protected]>
Cc: Zefan Li <[email protected]>
Cc: [email protected]

--
2.10.1


2016-11-15 04:08:06

by Wang Nan

[permalink] [raw]
Subject: [PATCH 06/34] tools lib bpf: Add private field for bpf_object

Like other classes defined in libbpf.h (like map and program), allow
'object' class has its own private data.

Signed-off-by: Wang Nan <[email protected]>
Cc: Alexei Starovoitov <[email protected]>
Cc: Arnaldo Carvalho de Melo <[email protected]>
Cc: Li Zefan <[email protected]>
---
tools/lib/bpf/libbpf.c | 23 +++++++++++++++++++++++
tools/lib/bpf/libbpf.h | 5 +++++
2 files changed, 28 insertions(+)

diff --git a/tools/lib/bpf/libbpf.c b/tools/lib/bpf/libbpf.c
index 96a2b2f..866d5cd 100644
--- a/tools/lib/bpf/libbpf.c
+++ b/tools/lib/bpf/libbpf.c
@@ -229,6 +229,10 @@ struct bpf_object {
* all objects.
*/
struct list_head list;
+
+ void *priv;
+ bpf_object_clear_priv_t clear_priv;
+
char path[];
};
#define obj_elf_valid(o) ((o)->efile.elf)
@@ -1229,6 +1233,9 @@ void bpf_object__close(struct bpf_object *obj)
if (!obj)
return;

+ if (obj->clear_priv)
+ obj->clear_priv(obj, obj->priv);
+
bpf_object__elf_finish(obj);
bpf_object__unload(obj);

@@ -1282,6 +1289,22 @@ unsigned int bpf_object__kversion(struct bpf_object *obj)
return obj ? obj->kern_version : 0;
}

+int bpf_object__set_priv(struct bpf_object *obj, void *priv,
+ bpf_object_clear_priv_t clear_priv)
+{
+ if (obj->priv && obj->clear_priv)
+ obj->clear_priv(obj, obj->priv);
+
+ obj->priv = priv;
+ obj->clear_priv = clear_priv;
+ return 0;
+}
+
+void *bpf_object__priv(struct bpf_object *obj)
+{
+ return obj ? obj->priv : ERR_PTR(-EINVAL);
+}
+
struct bpf_program *
bpf_program__next(struct bpf_program *prev, struct bpf_object *obj)
{
diff --git a/tools/lib/bpf/libbpf.h b/tools/lib/bpf/libbpf.h
index dd7a513..0c0b012 100644
--- a/tools/lib/bpf/libbpf.h
+++ b/tools/lib/bpf/libbpf.h
@@ -79,6 +79,11 @@ struct bpf_object *bpf_object__next(struct bpf_object *prev);
(pos) != NULL; \
(pos) = (tmp), (tmp) = bpf_object__next(tmp))

+typedef void (*bpf_object_clear_priv_t)(struct bpf_object *, void *);
+int bpf_object__set_priv(struct bpf_object *obj, void *priv,
+ bpf_object_clear_priv_t clear_priv);
+void *bpf_object__priv(struct bpf_object *prog);
+
/* Accessors of bpf_program. */
struct bpf_program;
struct bpf_program *bpf_program__next(struct bpf_program *prog,
--
2.10.1

2016-11-15 04:08:07

by Wang Nan

[permalink] [raw]
Subject: [PATCH 02/34] perf record: Fix segfault when running with suid and kptr_restrict is 1

Before this patch perf panic if kptr_restrict set to 1 and perf is owned
by root with suid set:

$ whoami
wangnan
$ ls -l ./perf
-rwsr-xr-x 1 root root 19781908 Sep 21 19:29 /home/wangnan/perf
$ cat /proc/sys/kernel/kptr_restrict
1
$ cat /proc/sys/kernel/perf_event_paranoid
-1
$ ./perf record -a
Segmentation fault (core dumped)

The reason is perf assumes it is allowed to read kptr from /proc/kallsyms
when euid is root, but in fact kernel doesn't allow it reading kptr when
euid and uid are not match with each other:

$ cp /bin/cat .
$ sudo chown root:root ./cat
$ sudo chmod u+s ./cat
$ cat /proc/kallsyms | grep do_fork
0000000000000000 T _do_fork <--- kptr is hidden even euid is root
$ sudo cat /proc/kallsyms | grep do_fork
ffffffff81080230 T _do_fork

See lib/vsprintf.c for kernel side code.

This patch fixes this problem by checking both uid and euid.

Signed-off-by: Wang Nan <[email protected]>
Cc: Arnaldo Carvalho de Melo <[email protected]>
---
tools/perf/util/symbol.c | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/tools/perf/util/symbol.c b/tools/perf/util/symbol.c
index aecff69..420ada9 100644
--- a/tools/perf/util/symbol.c
+++ b/tools/perf/util/symbol.c
@@ -1962,7 +1962,7 @@ static bool symbol__read_kptr_restrict(void)
char line[8];

if (fgets(line, sizeof(line), fp) != NULL)
- value = (geteuid() != 0) ?
+ value = ((geteuid() != 0) || (getuid() != 0)) ?
(atoi(line) != 0) :
(atoi(line) == 2);

--
2.10.1

2016-11-15 04:08:32

by Wang Nan

[permalink] [raw]
Subject: [PATCH 21/34] perf clang: Pass CFLAGS to builtin clang

Pass -DLINUX_VERSION_CODE, -D__NR_CPUS__, llvm.clang-opt config options
and CFLAGS detected by kbuild detector to builtin clang so BPF scripts
can use kernel headers and user defined options like external clang
compiler.

Test:
# perf record -v --dry-run -e tools/perf/tests/bpf-script-test-kbuild.c ls 2>&1 | grep built
bpf: builtin compiling successful

Committer notes:

Before installing the required clang/llvm devel files to have it
builtin:

# perf record -v --dry-run -e tools/perf/tests/bpf-script-test-kbuild.c ls 2>&1 | grep built
bpf: builtin compiling failed: -95, try external compiler

I.e. it falls back to using the external compiler.

Signed-off-by: Wang Nan <[email protected]>
Cc: Arnaldo Carvalho de Melo <[email protected]>
Cc: Alexei Starovoitov <[email protected]>
Cc: He Kuang <[email protected]>
Cc: Jiri Olsa <[email protected]>
Cc: Zefan Li <[email protected]>
Cc: [email protected]
---
tools/perf/util/c++/clang.cpp | 105 ++++++++++++++++++++++++++++++++++++++++--
tools/perf/util/llvm-utils.h | 9 +++-
2 files changed, 109 insertions(+), 5 deletions(-)

diff --git a/tools/perf/util/c++/clang.cpp b/tools/perf/util/c++/clang.cpp
index 610f1cf..3a3b9791 100644
--- a/tools/perf/util/c++/clang.cpp
+++ b/tools/perf/util/c++/clang.cpp
@@ -27,6 +27,8 @@

#include "clang.h"
#include "clang-c.h"
+#include "llvm-utils.h"
+#include "util-cxx.h"

namespace perf {

@@ -147,6 +149,101 @@ getBPFObjectFromModule(llvm::Module *Module)
return std::move(Buffer);
}

+class ClangOptions {
+ llvm::SmallString<PATH_MAX> FileName;
+ llvm::SmallString<64> KVerDef;
+ llvm::SmallString<64> NRCpusDef;
+ char *kbuild_dir;
+ char *kbuild_include_opts;
+ char *clang_opt;
+public:
+ ClangOptions(const char *filename) : FileName(filename),
+ KVerDef(""),
+ NRCpusDef(""),
+ kbuild_dir(NULL),
+ kbuild_include_opts(NULL),
+ clang_opt(NULL)
+ {
+ llvm::sys::fs::make_absolute(FileName);
+
+ unsigned int kver;
+ if (!fetch_kernel_version(&kver, NULL, 0))
+ KVerDef = "-DLINUX_VERSION_CODE=" + std::to_string(kver);
+
+ int nr_cpus = llvm__get_nr_cpus();
+ if (nr_cpus > 0)
+ NRCpusDef = "-D__NR_CPUS__=" + std::to_string(nr_cpus);
+
+ if (llvm_param.clang_opt)
+ clang_opt = strdup(llvm_param.clang_opt);
+
+ llvm__get_kbuild_opts(&kbuild_dir, &kbuild_include_opts);
+ if (!kbuild_dir || !kbuild_include_opts) {
+ free(kbuild_dir);
+ free(kbuild_include_opts);
+ kbuild_dir = kbuild_include_opts = NULL;
+ }
+ }
+
+ ~ClangOptions()
+ {
+ free(kbuild_dir);
+ free(kbuild_include_opts);
+ free(clang_opt);
+ }
+
+ static void fillCFlagsFromString(opt::ArgStringList &CFlags, char *s, bool check = false)
+ {
+ if (!s)
+ return;
+
+ SmallVector<StringRef, 0> Terms;
+ StringRef Opts(s);
+ Opts.split(Terms, ' ');
+
+ for (auto i = Terms.begin(); i != Terms.end(); i++)
+ s[i->end() - Opts.begin()] = '\0';
+
+ for (auto i = Terms.begin(); i != Terms.end(); i++) {
+ if (!check) {
+ CFlags.push_back(i->begin());
+ continue;
+ }
+
+ if (i->startswith("-I"))
+ CFlags.push_back(i->begin());
+ else if (i->startswith("-D"))
+ CFlags.push_back(i->begin());
+ else if (*i == "-include") {
+ CFlags.push_back((i++)->begin());
+ /* Let clang report this error */
+ if (i == Terms.end())
+ break;
+ CFlags.push_back(i->begin());
+ }
+ }
+ }
+
+ void getCFlags(opt::ArgStringList &CFlags)
+ {
+ CFlags.push_back(KVerDef.c_str());
+ CFlags.push_back(NRCpusDef.c_str());
+
+ fillCFlagsFromString(CFlags, clang_opt);
+ fillCFlagsFromString(CFlags, kbuild_include_opts, true);
+
+ if (kbuild_dir) {
+ CFlags.push_back("-working-directory");
+ CFlags.push_back(kbuild_dir);
+ }
+ }
+
+ const char *getFileName(void)
+ {
+ return FileName.c_str();
+ }
+};
+
}

extern "C" {
@@ -174,11 +271,11 @@ int perf_clang__compile_bpf(const char *_filename,
if (!p_obj_buf || !p_obj_buf_sz)
return -EINVAL;

- llvm::SmallString<PATH_MAX> FileName(_filename);
- llvm::sys::fs::make_absolute(FileName);
-
+ ClangOptions Opts(_filename);
llvm::opt::ArgStringList CFlags;
- auto M = getModuleFromSource(std::move(CFlags), FileName.data());
+
+ Opts.getCFlags(CFlags);
+ auto M = getModuleFromSource(std::move(CFlags), Opts.getFileName());
if (!M)
return -EINVAL;
auto O = getBPFObjectFromModule(&*M);
diff --git a/tools/perf/util/llvm-utils.h b/tools/perf/util/llvm-utils.h
index c87a2a9..263a00d 100644
--- a/tools/perf/util/llvm-utils.h
+++ b/tools/perf/util/llvm-utils.h
@@ -5,7 +5,11 @@
#ifndef __LLVM_UTILS_H
#define __LLVM_UTILS_H

-#include "debug.h"
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+#include <errno.h>

struct llvm_param {
/* Path of clang executable */
@@ -56,4 +60,7 @@ void llvm__get_kbuild_opts(char **kbuild_dir, char **kbuild_include_opts);
int llvm__get_nr_cpus(void);

void llvm__dump_obj(const char *path, void *obj_buf, size_t size);
+#ifdef __cplusplus
+}
+#endif
#endif
--
2.10.1

2016-11-15 04:08:40

by Wang Nan

[permalink] [raw]
Subject: [PATCH 04/34] tools lib bpf: fix maps resolution

From: Eric Leblond <[email protected]>

It is not correct to assimilate the elf data of the maps section
to an array of map definition. In fact the sizes differ. The
offset provided in the symbol section has to be used instead.

This patch fixes a bug causing a elf with two maps not to load
correctly.

Wang Nan added:

This patch requires a name for each BPF map, so array of BPF maps is
not allowed. This restriction is reasonable, because kernel verifier
forbid indexing BPF map from such array unless the index is a fixed
value, but if the index is fixed why not merging it into name?

For example:

Program like this:
...
unsigned long cpu = get_smp_processor_id();
int *pval = map_lookup_elem(&map_array[cpu], &key);
...

Generates bytecode like this:

0: (b7) r1 = 0
1: (63) *(u32 *)(r10 -4) = r1
2: (b7) r1 = 680997
3: (63) *(u32 *)(r10 -8) = r1
4: (85) call 8
5: (67) r0 <<= 4
6: (18) r1 = 0x112dd000
8: (0f) r0 += r1
9: (bf) r2 = r10
10: (07) r2 += -4
11: (bf) r1 = r0
12: (85) call 1

Where instruction 8 is the computation, 8 and 11 render r1 to an invalid
value for function map_lookup_elem, causes verifier report error.

Signed-off-by: Eric Leblond <[email protected]>
Signed-off-by: Wang Nan <[email protected]>
[Merge bpf_object__init_maps_name into bpf_object__init_maps
Fix segfault for buggy BPF script
Validate obj->maps
]
Cc: Alexei Starovoitov <[email protected]>
Cc: Arnaldo Carvalho de Melo <[email protected]>
Cc: Li Zefan <[email protected]>
---
tools/lib/bpf/libbpf.c | 142 ++++++++++++++++++++++++++++++++++---------------
1 file changed, 98 insertions(+), 44 deletions(-)

diff --git a/tools/lib/bpf/libbpf.c b/tools/lib/bpf/libbpf.c
index b699aea..96a2b2f 100644
--- a/tools/lib/bpf/libbpf.c
+++ b/tools/lib/bpf/libbpf.c
@@ -185,6 +185,7 @@ struct bpf_program {
struct bpf_map {
int fd;
char *name;
+ size_t offset;
struct bpf_map_def def;
void *priv;
bpf_map_clear_priv_t clear_priv;
@@ -513,57 +514,106 @@ bpf_object__init_kversion(struct bpf_object *obj,
}

static int
-bpf_object__init_maps(struct bpf_object *obj, void *data,
- size_t size)
+bpf_object__validate_maps(struct bpf_object *obj)
{
- size_t nr_maps;
int i;

- nr_maps = size / sizeof(struct bpf_map_def);
- if (!data || !nr_maps) {
- pr_debug("%s doesn't need map definition\n",
- obj->path);
+ /*
+ * If there's only 1 map, the only error case should have been
+ * catched in bpf_object__init_maps().
+ */
+ if (!obj->maps || !obj->nr_maps || (obj->nr_maps == 1))
return 0;
- }

- pr_debug("maps in %s: %zd bytes\n", obj->path, size);
+ for (i = 1; i < obj->nr_maps; i++) {
+ const struct bpf_map *a = &obj->maps[i - 1];
+ const struct bpf_map *b = &obj->maps[i];

- obj->maps = calloc(nr_maps, sizeof(obj->maps[0]));
- if (!obj->maps) {
- pr_warning("alloc maps for object failed\n");
- return -ENOMEM;
+ if (b->offset - a->offset < sizeof(struct bpf_map_def)) {
+ pr_warning("corrupted map section in %s: map \"%s\" too small\n",
+ obj->path, a->name);
+ return -EINVAL;
+ }
}
- obj->nr_maps = nr_maps;
-
- for (i = 0; i < nr_maps; i++) {
- struct bpf_map_def *def = &obj->maps[i].def;
+ return 0;
+}

- /*
- * fill all fd with -1 so won't close incorrect
- * fd (fd=0 is stdin) when failure (zclose won't close
- * negative fd)).
- */
- obj->maps[i].fd = -1;
+static int compare_bpf_map(const void *_a, const void *_b)
+{
+ const struct bpf_map *a = _a;
+ const struct bpf_map *b = _b;

- /* Save map definition into obj->maps */
- *def = ((struct bpf_map_def *)data)[i];
- }
- return 0;
+ return a->offset - b->offset;
}

static int
-bpf_object__init_maps_name(struct bpf_object *obj)
+bpf_object__init_maps(struct bpf_object *obj)
{
- int i;
+ int i, map_idx, nr_maps = 0;
+ Elf_Scn *scn;
+ Elf_Data *data;
Elf_Data *symbols = obj->efile.symbols;

- if (!symbols || obj->efile.maps_shndx < 0)
+ if (obj->efile.maps_shndx < 0)
+ return -EINVAL;
+ if (!symbols)
+ return -EINVAL;
+
+ scn = elf_getscn(obj->efile.elf, obj->efile.maps_shndx);
+ if (scn)
+ data = elf_getdata(scn, NULL);
+ if (!scn || !data) {
+ pr_warning("failed to get Elf_Data from map section %d\n",
+ obj->efile.maps_shndx);
return -EINVAL;
+ }

+ /*
+ * Count number of maps. Each map has a name.
+ * Array of maps is not supported: only the first element is
+ * considered.
+ *
+ * TODO: Detect array of map and report error.
+ */
for (i = 0; i < symbols->d_size / sizeof(GElf_Sym); i++) {
GElf_Sym sym;
- size_t map_idx;
+
+ if (!gelf_getsym(symbols, i, &sym))
+ continue;
+ if (sym.st_shndx != obj->efile.maps_shndx)
+ continue;
+ nr_maps++;
+ }
+
+ /* Alloc obj->maps and fill nr_maps. */
+ pr_debug("maps in %s: %d maps in %zd bytes\n", obj->path,
+ nr_maps, data->d_size);
+
+ if (!nr_maps)
+ return 0;
+
+ obj->maps = calloc(nr_maps, sizeof(obj->maps[0]));
+ if (!obj->maps) {
+ pr_warning("alloc maps for object failed\n");
+ return -ENOMEM;
+ }
+ obj->nr_maps = nr_maps;
+
+ /*
+ * fill all fd with -1 so won't close incorrect
+ * fd (fd=0 is stdin) when failure (zclose won't close
+ * negative fd)).
+ */
+ for (i = 0; i < nr_maps; i++)
+ obj->maps[i].fd = -1;
+
+ /*
+ * Fill obj->maps using data in "maps" section.
+ */
+ for (i = 0, map_idx = 0; i < symbols->d_size / sizeof(GElf_Sym); i++) {
+ GElf_Sym sym;
const char *map_name;
+ struct bpf_map_def *def;

if (!gelf_getsym(symbols, i, &sym))
continue;
@@ -573,21 +623,27 @@ bpf_object__init_maps_name(struct bpf_object *obj)
map_name = elf_strptr(obj->efile.elf,
obj->efile.strtabidx,
sym.st_name);
- map_idx = sym.st_value / sizeof(struct bpf_map_def);
- if (map_idx >= obj->nr_maps) {
- pr_warning("index of map \"%s\" is buggy: %zu > %zu\n",
- map_name, map_idx, obj->nr_maps);
- continue;
+ obj->maps[map_idx].offset = sym.st_value;
+ if (sym.st_value + sizeof(struct bpf_map_def) > data->d_size) {
+ pr_warning("corrupted maps section in %s: last map \"%s\" too small\n",
+ obj->path, map_name);
+ return -EINVAL;
}
+
obj->maps[map_idx].name = strdup(map_name);
if (!obj->maps[map_idx].name) {
pr_warning("failed to alloc map name\n");
return -ENOMEM;
}
- pr_debug("map %zu is \"%s\"\n", map_idx,
+ pr_debug("map %d is \"%s\"\n", map_idx,
obj->maps[map_idx].name);
+ def = (struct bpf_map_def *)(data->d_buf + sym.st_value);
+ obj->maps[map_idx].def = *def;
+ map_idx++;
}
- return 0;
+
+ qsort(obj->maps, obj->nr_maps, sizeof(obj->maps[0]), compare_bpf_map);
+ return bpf_object__validate_maps(obj);
}

static int bpf_object__elf_collect(struct bpf_object *obj)
@@ -645,11 +701,9 @@ static int bpf_object__elf_collect(struct bpf_object *obj)
err = bpf_object__init_kversion(obj,
data->d_buf,
data->d_size);
- else if (strcmp(name, "maps") == 0) {
- err = bpf_object__init_maps(obj, data->d_buf,
- data->d_size);
+ else if (strcmp(name, "maps") == 0)
obj->efile.maps_shndx = idx;
- } else if (sh.sh_type == SHT_SYMTAB) {
+ else if (sh.sh_type == SHT_SYMTAB) {
if (obj->efile.symbols) {
pr_warning("bpf: multiple SYMTAB in %s\n",
obj->path);
@@ -698,7 +752,7 @@ static int bpf_object__elf_collect(struct bpf_object *obj)
return LIBBPF_ERRNO__FORMAT;
}
if (obj->efile.maps_shndx >= 0)
- err = bpf_object__init_maps_name(obj);
+ err = bpf_object__init_maps(obj);
out:
return err;
}
@@ -807,7 +861,7 @@ bpf_object__create_maps(struct bpf_object *obj)
zclose(obj->maps[j].fd);
return err;
}
- pr_debug("create map: fd=%d\n", *pfd);
+ pr_debug("create map %s: fd=%d\n", obj->maps[i].name, *pfd);
}

return 0;
--
2.10.1

2016-11-15 04:08:30

by Wang Nan

[permalink] [raw]
Subject: [PATCH 16/34] perf clang: Allow passing CFLAGS to builtin clang

Improve getModuleFromSource() API to accept a cflags list. This feature
will be used to pass LINUX_VERSION_CODE and -I flags.

Signed-off-by: Wang Nan <[email protected]>
Cc: Alexei Starovoitov <[email protected]>
Cc: He Kuang <[email protected]>
Cc: Jiri Olsa <[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/c++/clang-test.cpp | 5 +++--
tools/perf/util/c++/clang.cpp | 21 +++++++++++++--------
tools/perf/util/c++/clang.h | 8 ++++++--
3 files changed, 22 insertions(+), 12 deletions(-)

diff --git a/tools/perf/util/c++/clang-test.cpp b/tools/perf/util/c++/clang-test.cpp
index 3da6bfa..0f484fb 100644
--- a/tools/perf/util/c++/clang-test.cpp
+++ b/tools/perf/util/c++/clang-test.cpp
@@ -16,8 +16,9 @@ int test__clang_to_IR(void)
perf_clang_scope _scope;

std::unique_ptr<llvm::Module> M =
- perf::getModuleFromSource("perf-test.c",
- "int myfunc(void) {return 1;}");
+ perf::getModuleFromSource({"-DRESULT=1"},
+ "perf-test.c",
+ "int myfunc(void) {return RESULT;}");

if (!M)
return -1;
diff --git a/tools/perf/util/c++/clang.cpp b/tools/perf/util/c++/clang.cpp
index cf96199..715ca0a 100644
--- a/tools/perf/util/c++/clang.cpp
+++ b/tools/perf/util/c++/clang.cpp
@@ -29,7 +29,8 @@ static std::unique_ptr<llvm::LLVMContext> LLVMCtx;
using namespace clang;

static CompilerInvocation *
-createCompilerInvocation(StringRef& Path, DiagnosticsEngine& Diags)
+createCompilerInvocation(llvm::opt::ArgStringList CFlags, StringRef& Path,
+ DiagnosticsEngine& Diags)
{
llvm::opt::ArgStringList CCArgs {
"-cc1",
@@ -45,6 +46,8 @@ createCompilerInvocation(StringRef& Path, DiagnosticsEngine& Diags)
"-Wno-unused-value",
"-Wno-pointer-sign",
"-x", "c"};
+
+ CCArgs.append(CFlags.begin(), CFlags.end());
CompilerInvocation *CI = tooling::newInvocation(&Diags, CCArgs);

FrontendOptions& Opts = CI->getFrontendOpts();
@@ -54,8 +57,8 @@ createCompilerInvocation(StringRef& Path, DiagnosticsEngine& Diags)
}

static std::unique_ptr<llvm::Module>
-getModuleFromSource(StringRef Path,
- IntrusiveRefCntPtr<vfs::FileSystem> VFS)
+getModuleFromSource(llvm::opt::ArgStringList CFlags,
+ StringRef Path, IntrusiveRefCntPtr<vfs::FileSystem> VFS)
{
CompilerInstance Clang;
Clang.createDiagnostics();
@@ -63,7 +66,8 @@ getModuleFromSource(StringRef Path,
Clang.setVirtualFileSystem(&*VFS);

IntrusiveRefCntPtr<CompilerInvocation> CI =
- createCompilerInvocation(Path, Clang.getDiagnostics());
+ createCompilerInvocation(std::move(CFlags), Path,
+ Clang.getDiagnostics());
Clang.setInvocation(&*CI);

std::unique_ptr<CodeGenAction> Act(new EmitLLVMOnlyAction(&*LLVMCtx));
@@ -74,7 +78,8 @@ getModuleFromSource(StringRef Path,
}

std::unique_ptr<llvm::Module>
-getModuleFromSource(StringRef Name, StringRef Content)
+getModuleFromSource(llvm::opt::ArgStringList CFlags,
+ StringRef Name, StringRef Content)
{
using namespace vfs;

@@ -90,14 +95,14 @@ getModuleFromSource(StringRef Name, StringRef Content)
OverlayFS->pushOverlay(MemFS);
MemFS->addFile(Twine(Name), 0, llvm::MemoryBuffer::getMemBuffer(Content));

- return getModuleFromSource(Name, OverlayFS);
+ return getModuleFromSource(std::move(CFlags), Name, OverlayFS);
}

std::unique_ptr<llvm::Module>
-getModuleFromSource(StringRef Path)
+getModuleFromSource(llvm::opt::ArgStringList CFlags, StringRef Path)
{
IntrusiveRefCntPtr<vfs::FileSystem> VFS(vfs::getRealFileSystem());
- return getModuleFromSource(Path, VFS);
+ return getModuleFromSource(std::move(CFlags), Path, VFS);
}

}
diff --git a/tools/perf/util/c++/clang.h b/tools/perf/util/c++/clang.h
index 90aff01..b4fc2a9 100644
--- a/tools/perf/util/c++/clang.h
+++ b/tools/perf/util/c++/clang.h
@@ -4,16 +4,20 @@
#include "llvm/ADT/StringRef.h"
#include "llvm/IR/LLVMContext.h"
#include "llvm/IR/Module.h"
+#include "llvm/Option/Option.h"
#include <memory>
+
namespace perf {

using namespace llvm;

std::unique_ptr<Module>
-getModuleFromSource(StringRef Name, StringRef Content);
+getModuleFromSource(opt::ArgStringList CFlags,
+ StringRef Name, StringRef Content);

std::unique_ptr<Module>
-getModuleFromSource(StringRef Path);
+getModuleFromSource(opt::ArgStringList CFlags,
+ StringRef Path);

}
#endif
--
2.10.1

2016-11-15 04:08:29

by Wang Nan

[permalink] [raw]
Subject: [PATCH 28/34] perf clang jit: Access BPF map

---
tools/perf/util/bpf-loader.c | 37 +++++++++++++++++++++++++++++++++++++
tools/perf/util/bpf-loader.h | 19 +++++++++++++++++++
2 files changed, 56 insertions(+)

diff --git a/tools/perf/util/bpf-loader.c b/tools/perf/util/bpf-loader.c
index 81c6fed..86aa99b 100644
--- a/tools/perf/util/bpf-loader.c
+++ b/tools/perf/util/bpf-loader.c
@@ -19,6 +19,7 @@
#include "parse-events.h"
#include "llvm-utils.h"
#include "c++/clang-c.h"
+#include "asm/bug.h" // for WARN_ONCE

#define DEFINE_PRINT_FN(name, level) \
static int libbpf_##name(const char *fmt, ...) \
@@ -1644,6 +1645,28 @@ int bpf__setup_stdout(struct perf_evlist *evlist __maybe_unused)
return 0;
}

+int bpf__map_fd(struct bpf_object *obj, void *jit_map)
+{
+ struct bpf_obj_priv *priv = bpf_object__priv(obj);
+ struct bpf_map *map;
+ size_t map_offset;
+ void *map_base;
+
+ if (IS_ERR(priv))
+ return PTR_ERR(priv);
+ if (!priv)
+ return -EINVAL;
+
+ map_base = priv->map_base;
+ map_offset = jit_map - map_base;
+ map = bpf_object__find_map_by_offset(obj, map_offset);
+ WARN_ONCE(IS_ERR(map), "can't find map offset %zu from '%s'\n",
+ map_offset, bpf_object__name(obj));
+ if (IS_ERR(map))
+ return -ENOENT;
+ return bpf_map__fd(map);
+}
+
#define ERRNO_OFFSET(e) ((e) - __BPF_LOADER_ERRNO__START)
#define ERRCODE_OFFSET(c) ERRNO_OFFSET(BPF_LOADER_ERRNO__##c)
#define NR_ERRNO (__BPF_LOADER_ERRNO__END - __BPF_LOADER_ERRNO__START)
@@ -1825,3 +1848,17 @@ int bpf__strerror_setup_stdout(struct perf_evlist *evlist __maybe_unused,
bpf__strerror_end(buf, size);
return 0;
}
+
+int bpf__strerror_map_fd(struct bpf_object *obj, void *jit_map,
+ int err, char *buf, size_t size)
+{
+ struct bpf_obj_priv *priv = bpf_object__priv(obj);
+ ptrdiff_t offset = priv ? jit_map - priv->map_base : jit_map - NULL;
+
+ bpf__strerror_head(err, buf, size);
+ bpf__strerror_entry(EINVAL, "No map in BPF object %s", bpf_object__name(obj));
+ bpf__strerror_entry(ENOENT, "Can't find map offset %lx in BPF object %s",
+ (unsigned long)offset, bpf_object__name(obj));
+ bpf__strerror_end(buf, size);
+ return 0;
+}
diff --git a/tools/perf/util/bpf-loader.h b/tools/perf/util/bpf-loader.h
index f2b737b..c40812b 100644
--- a/tools/perf/util/bpf-loader.h
+++ b/tools/perf/util/bpf-loader.h
@@ -84,6 +84,9 @@ int bpf__setup_stdout(struct perf_evlist *evlist);
int bpf__strerror_setup_stdout(struct perf_evlist *evlist, int err,
char *buf, size_t size);

+int bpf__map_fd(struct bpf_object *obj, void *jit_map);
+int bpf__strerror_map_fd(struct bpf_object *obj, void *jit_map,
+ int err, char *buf, size_t size);
#else
static inline struct bpf_object *
bpf__prepare_load(const char *filename __maybe_unused,
@@ -136,6 +139,13 @@ bpf__setup_stdout(struct perf_evlist *evlist __maybe_unused)
}

static inline int
+bpf__map_fd(struct bpf_object *obj __maybe_unused,
+ void *map_ptr __maybe_unused)
+{
+ return -ENOTSUP;
+}
+
+static inline int
__bpf_strerror(char *buf, size_t size)
{
if (!size)
@@ -196,5 +206,14 @@ bpf__strerror_setup_stdout(struct perf_evlist *evlist __maybe_unused,
{
return __bpf_strerror(buf, size);
}
+
+static inline int
+bpf__strerror_map_fd(struct bpf_object *obj __maybe_unused,
+ void *jit_map __maybe_unused,
+ int err __maybe_unused,
+ char *buf, size_t size);
+{
+ return __bpf_strerror(buf, size);
+}
#endif
#endif
--
2.10.1

2016-11-15 04:08:28

by Wang Nan

[permalink] [raw]
Subject: [PATCH 23/34] perf clang jit: Insignt BPF and JIT functions in a Module

Identify BPF functions, JIT functions and maps during init. Functions in
section starting with "perfhook:" are JIT functions. They will be JIT
compiled and hooked at perfhooks.

During init of PerfModule, mark JIT functions as AvailableExternallyLinkage.
LLVM skips functions with linkage like this so they won't be compiled
into BPF objects.

Signed-off-by: Wang Nan <[email protected]>
Cc: Arnaldo Carvalho de Melo <[email protected]>
Cc: Alexei Starovoitov <[email protected]>
Cc: He Kuang <[email protected]>
Cc: Jiri Olsa <[email protected]>
Cc: Zefan Li <[email protected]>
Cc: [email protected]
---
tools/perf/util/c++/clang.cpp | 32 ++++++++++++++++++++++++++++++++
tools/perf/util/c++/clang.h | 7 +++++++
2 files changed, 39 insertions(+)

diff --git a/tools/perf/util/c++/clang.cpp b/tools/perf/util/c++/clang.cpp
index d31b0a5..98d05e2 100644
--- a/tools/perf/util/c++/clang.cpp
+++ b/tools/perf/util/c++/clang.cpp
@@ -115,15 +115,47 @@ getModuleFromSource(llvm::opt::ArgStringList CFlags, StringRef Path)

PerfModule::PerfModule(std::unique_ptr<llvm::Module>&& M) : Module(std::move(M))
{
+ for (llvm::Function& F : *Module) {
+ if (F.getLinkage() != llvm::GlobalValue::ExternalLinkage)
+ continue;
+
+ if (StringRef(F.getSection()).startswith("perfhook:"))
+ JITFunctions.insert(&F);
+ else
+ BPFFunctions.insert(&F);
+ }

+ for (auto V = Module->global_begin(); V != Module->global_end(); V++) {
+ llvm::GlobalVariable *GV = &*V;
+ if (StringRef(GV->getSection()) == llvm::StringRef("maps"))
+ Maps.insert(GV);
+ }
}

+void PerfModule::prepareBPF(void)
+{
+ for (llvm::Function *F : JITFunctions)
+ F->setLinkage(llvm::GlobalValue::AvailableExternallyLinkage);
+ for (llvm::Function *F : BPFFunctions)
+ F->setLinkage(llvm::GlobalValue::ExternalLinkage);
+
+}
+
+void PerfModule::prepareJIT(void)
+{
+ for (llvm::Function *F : BPFFunctions)
+ F->setLinkage(llvm::GlobalValue::AvailableExternallyLinkage);
+ for (llvm::Function *F : JITFunctions)
+ F->setLinkage(llvm::GlobalValue::ExternalLinkage);
+
+}

std::unique_ptr<llvm::SmallVectorImpl<char>>
PerfModule::toBPFObject(void)
{
using namespace llvm;

+ prepareBPF();
std::string TargetTriple("bpf-pc-linux");
std::string Error;
const Target* Target = TargetRegistry::lookupTarget(TargetTriple, Error);
diff --git a/tools/perf/util/c++/clang.h b/tools/perf/util/c++/clang.h
index cbb291b..1eb71a6 100644
--- a/tools/perf/util/c++/clang.h
+++ b/tools/perf/util/c++/clang.h
@@ -6,6 +6,7 @@
#include "llvm/IR/Module.h"
#include "llvm/Option/Option.h"
#include <memory>
+#include <set>

namespace perf {

@@ -14,6 +15,12 @@ using namespace llvm;
class PerfModule {
private:
std::unique_ptr<llvm::Module> Module;
+
+ std::set<llvm::GlobalVariable *> Maps;
+ std::set<llvm::Function *> BPFFunctions;
+ std::set<llvm::Function *> JITFunctions;
+ void prepareBPF(void);
+ void prepareJIT(void);
public:
inline llvm::Module *getModule(void)
{
--
2.10.1

2016-11-15 04:09:37

by Wang Nan

[permalink] [raw]
Subject: [PATCH 33/34] perf clang builtin: Define hook helpers by default

Append prototypes of helpers appears in exported_funcs to default
include file.

Signed-off-by: Wang Nan <[email protected]>
Cc: Arnaldo Carvalho de Melo <[email protected]>
Cc: Alexei Starovoitov <[email protected]>
Cc: He Kuang <[email protected]>
Cc: Jiri Olsa <[email protected]>
Cc: Zefan Li <[email protected]>
Cc: [email protected]
---
tools/perf/util/c++/bpf-helper-str.c | 8 ++++++++
1 file changed, 8 insertions(+)

diff --git a/tools/perf/util/c++/bpf-helper-str.c b/tools/perf/util/c++/bpf-helper-str.c
index 17f915c..69aca3c 100644
--- a/tools/perf/util/c++/bpf-helper-str.c
+++ b/tools/perf/util/c++/bpf-helper-str.c
@@ -10,6 +10,14 @@ const char clang_builtin_bpf_helper_str[] =
" unsigned int max_entries;\n"
"};\n"
"#define SEC(NAME) __attribute__((section(NAME), used))\n"
+"extern void test__clang_callback(int);\n"
+"extern int printf(const char *, ...);\n"
+"extern int puts(const char *);\n"
+"extern int jit_helper__map_update_elem(void *, void *, void *, void *, unsigned long);\n"
+"extern int jit_helper__map_lookup_elem(void *, void *, void *, void *);\n"
+"extern int jit_helper__map_get_next_key(void *, void *, void *, void *);\n"
+"extern int jit_helper__map_pin(void *, void *, const char *);\n"
+"extern int jit_helper__map_get(const char *);\n"
"#endif\n"
"#endif"
;
--
2.10.1

2016-11-15 04:09:58

by Wang Nan

[permalink] [raw]
Subject: [PATCH 25/34] perf clang jit: Export functions for jitted code

After this patch functions attached on perf hooks is allowed to invoke
external functions. Add a testcase for this feature.

Signed-off-by: Wang Nan <[email protected]>
Cc: Arnaldo Carvalho de Melo <[email protected]>
Cc: Alexei Starovoitov <[email protected]>
Cc: He Kuang <[email protected]>
Cc: Jiri Olsa <[email protected]>
Cc: Zefan Li <[email protected]>
Cc: [email protected]
---
tools/perf/tests/Build | 2 +-
tools/perf/tests/bpf-script-example.c | 4 ++++
tools/perf/util/c++/clang-c.h | 2 ++
tools/perf/util/c++/clang-test.cpp | 9 +++++++++
tools/perf/util/c++/clang.cpp | 17 ++++++++++++++++-
5 files changed, 32 insertions(+), 2 deletions(-)

diff --git a/tools/perf/tests/Build b/tools/perf/tests/Build
index 6676c2d..d6e6e00 100644
--- a/tools/perf/tests/Build
+++ b/tools/perf/tests/Build
@@ -49,7 +49,7 @@ $(OUTPUT)tests/llvm-src-base.c: tests/bpf-script-example.c tests/Build
$(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)sed -e 's/\\/\\\\/g' -e 's/"/\\"/g' -e 's/\(.*\)/"\1\\n"/g' $< >> $@
$(Q)echo ';' >> $@

$(OUTPUT)tests/llvm-src-kbuild.c: tests/bpf-script-test-kbuild.c tests/Build
diff --git a/tools/perf/tests/bpf-script-example.c b/tools/perf/tests/bpf-script-example.c
index 265036e..ccbc19c 100644
--- a/tools/perf/tests/bpf-script-example.c
+++ b/tools/perf/tests/bpf-script-example.c
@@ -48,9 +48,13 @@ char _license[] SEC("license") = "GPL";
int _version SEC("version") = LINUX_VERSION_CODE;

#ifdef TEST_PERF_HOOK
+extern int printf(const char *fmt, ...);
+extern void test__clang_callback(int x);
SEC("perfhook:test")
void hook_test(void)
{
+ printf("Hello, hook_test\n");
+ test__clang_callback(1234);
return;
}
#endif
diff --git a/tools/perf/util/c++/clang-c.h b/tools/perf/util/c++/clang-c.h
index 5ebcb41..9f75e41 100644
--- a/tools/perf/util/c++/clang-c.h
+++ b/tools/perf/util/c++/clang-c.h
@@ -16,6 +16,8 @@ extern int test__clang_to_IR(void);
extern int test__clang_to_obj(void);
extern int test__clang_jit(void);

+extern void test__clang_callback(int x);
+
extern int perf_clang__compile_bpf(const char *filename,
void **p_obj_buf,
size_t *p_obj_buf_sz);
diff --git a/tools/perf/util/c++/clang-test.cpp b/tools/perf/util/c++/clang-test.cpp
index 2b4aa8d..0bdb807 100644
--- a/tools/perf/util/c++/clang-test.cpp
+++ b/tools/perf/util/c++/clang-test.cpp
@@ -68,6 +68,13 @@ int test__clang_to_obj(void)
return 0;
}

+static int callback_flag;
+
+void test__clang_callback(int x)
+{
+ callback_flag = x;
+}
+
int test__clang_jit(void)
{
perf_clang_scope _scope;
@@ -84,6 +91,8 @@ int test__clang_jit(void)
perf_hooks__set_hook(i.first.c_str(), i.second, NULL);

perf_hooks__invoke_test();
+ if (callback_flag != 1234)
+ return -1;
return 0;
}

diff --git a/tools/perf/util/c++/clang.cpp b/tools/perf/util/c++/clang.cpp
index 03012b2..325fbe4 100644
--- a/tools/perf/util/c++/clang.cpp
+++ b/tools/perf/util/c++/clang.cpp
@@ -30,6 +30,8 @@
#include "llvm/Target/TargetOptions.h"
#include <memory>
#include <vector>
+#include <set>
+#include <tuple>

#include "clang.h"
#include "clang-c.h"
@@ -194,6 +196,15 @@ PerfModule::toBPFObject(void)
return std::move(Buffer);
}

+static std::map<const std::string, const void *> exported_funcs =
+{
+#define EXPORT(f) {#f, (const void *)&f}
+ EXPORT(test__clang_callback),
+ EXPORT(printf),
+ EXPORT(puts),
+#undef EXPORT
+};
+
/*
* Use a global memory manager so allocated code and data won't be released
* when object destroy.
@@ -220,7 +231,11 @@ int PerfModule::doJIT(void)

auto Resolver = createLambdaResolver(
[](const std::string &Name) {
- return RuntimeDyld::SymbolInfo(nullptr);
+ auto i = exported_funcs.find(Name);
+ if (i == exported_funcs.end())
+ return RuntimeDyld::SymbolInfo(nullptr);
+ return RuntimeDyld::SymbolInfo((uint64_t)(i->second),
+ JITSymbolFlags::Exported);
},
[](const std::string &Name) {
return RuntimeDyld::SymbolInfo(nullptr);
--
2.10.1

2016-11-15 04:10:00

by Wang Nan

[permalink] [raw]
Subject: [PATCH 27/34] perf clang jit: Collect the lowest address in maps section as map_base

During jitting, find the lowest address in maps section and store its
value to _map_base. Pass its value out through perf_clang__compile_bpf().
map_base is useful for jitted functions accessing BPF maps.

Signed-off-by: Wang Nan <[email protected]>
Cc: Arnaldo Carvalho de Melo <[email protected]>
Cc: Alexei Starovoitov <[email protected]>
Cc: He Kuang <[email protected]>
Cc: Jiri Olsa <[email protected]>
Cc: Zefan Li <[email protected]>
Cc: [email protected]
---
tools/perf/util/bpf-loader.c | 39 +++++++++++++++++++++++++++++++++++++--
tools/perf/util/c++/clang-c.h | 6 ++++--
tools/perf/util/c++/clang.cpp | 15 +++++++++++++--
tools/perf/util/c++/clang.h | 5 +++++
4 files changed, 59 insertions(+), 6 deletions(-)

diff --git a/tools/perf/util/bpf-loader.c b/tools/perf/util/bpf-loader.c
index e50045f..81c6fed 100644
--- a/tools/perf/util/bpf-loader.c
+++ b/tools/perf/util/bpf-loader.c
@@ -47,6 +47,10 @@ struct bpf_prog_priv {
int *type_mapping;
};

+struct bpf_obj_priv {
+ void *map_base;
+};
+
static bool libbpf_initialized;

struct bpf_object *
@@ -70,9 +74,20 @@ bpf__prepare_load_buffer(void *obj_buf, size_t obj_buf_sz, const char *name)
return obj;
}

+static void
+clear_obj_priv(struct bpf_object *obj __maybe_unused,
+ void *_priv)
+{
+ struct bpf_obj_priv *priv = _priv;
+
+ free(priv);
+}
+
struct bpf_object *bpf__prepare_load(const char *filename, bool source)
{
struct bpf_object *obj;
+ void *map_base = NULL;
+ int err;

if (!libbpf_initialized) {
libbpf_set_print(libbpf_warning,
@@ -82,14 +97,14 @@ struct bpf_object *bpf__prepare_load(const char *filename, bool source)
}

if (source) {
- int err;
void *obj_buf;
size_t obj_buf_sz;
jitted_funcs_map_t jitted_funcs_map;

perf_clang__init();
err = perf_clang__compile_bpf(filename, &obj_buf,
- &obj_buf_sz, &jitted_funcs_map);
+ &obj_buf_sz, &jitted_funcs_map,
+ &map_base);
perf_clang__cleanup();
if (err) {
pr_warning("bpf: builtin compiling failed: %d, try external compiler\n", err);
@@ -119,7 +134,27 @@ struct bpf_object *bpf__prepare_load(const char *filename, bool source)
return obj;
}

+ if (map_base) {
+ struct bpf_obj_priv *priv = calloc(sizeof(*priv), 1);
+
+ if (!priv) {
+ pr_debug("bpf: failed to alloc priv for object\n");
+ err = -ENOMEM;
+ goto errout;
+ }
+ priv->map_base = map_base;
+
+ err = bpf_object__set_priv(obj, priv, clear_obj_priv);
+ if (err) {
+ pr_debug("Failed to set priv for object '%s'\n", filename);
+ goto errout;
+ }
+ }
+
return obj;
+errout:
+ bpf_object__close(obj);
+ return ERR_PTR(err);
}

void bpf__clear(void)
diff --git a/tools/perf/util/c++/clang-c.h b/tools/perf/util/c++/clang-c.h
index 021b1ad..4cf651b 100644
--- a/tools/perf/util/c++/clang-c.h
+++ b/tools/perf/util/c++/clang-c.h
@@ -22,7 +22,8 @@ extern void test__clang_callback(int x);
extern int perf_clang__compile_bpf(const char *filename,
void **p_obj_buf,
size_t *p_obj_buf_sz,
- jitted_funcs_map_t *p_funcs_map);
+ jitted_funcs_map_t *p_funcs_map,
+ void **p_map_base);

extern int
perf_clang__hook_jitted_func(jitted_funcs_map_t map, void *ctx, bool is_err);
@@ -40,7 +41,8 @@ static inline int
perf_clang__compile_bpf(const char *filename __maybe_unused,
void **p_obj_buf __maybe_unused,
size_t *p_obj_buf_sz __maybe_unused,
- jitted_funcs_map_t *p_funcs_map __maybe_unused)
+ jitted_funcs_map_t *p_funcs_map __maybe_unused,
+ void **p_map_base __maybe_unused)
{
return -ENOTSUP;
}
diff --git a/tools/perf/util/c++/clang.cpp b/tools/perf/util/c++/clang.cpp
index f2608f5..f8ea9bd 100644
--- a/tools/perf/util/c++/clang.cpp
+++ b/tools/perf/util/c++/clang.cpp
@@ -122,7 +122,7 @@ getModuleFromSource(llvm::opt::ArgStringList CFlags, StringRef Path)
return getModuleFromSource(std::move(CFlags), Path, VFS);
}

-PerfModule::PerfModule(std::unique_ptr<llvm::Module>&& M) : Module(std::move(M))
+PerfModule::PerfModule(std::unique_ptr<llvm::Module>&& M) : Module(std::move(M)), _map_base(NULL)
{
for (llvm::Function& F : *Module) {
if (F.getLinkage() != llvm::GlobalValue::ExternalLinkage)
@@ -247,6 +247,13 @@ int PerfModule::doJIT(void)
&JITMemoryManager,
std::move(Resolver));

+ void *map_base = NULL;
+ for (llvm::GlobalValue *map : Maps) {
+ JITSymbol sym = CompileLayer.findSymbol(map->getName().str(), true);
+ void *address = (void *)(intptr_t)sym.getAddress();
+ if (!map_base || address < map_base)
+ map_base = address;
+ }

for (Function *F : JITFunctions) {
JITSymbol sym = CompileLayer.findSymbol(F->getName().str(), true);
@@ -266,6 +273,7 @@ int PerfModule::doJIT(void)
<< hook << ", only one is used\n";
JITResult[hook] = func;
}
+ _map_base = map_base;
return 0;
}

@@ -389,7 +397,8 @@ void perf_clang__cleanup(void)
int perf_clang__compile_bpf(const char *_filename,
void **p_obj_buf,
size_t *p_obj_buf_sz,
- jitted_funcs_map_t *p_funcs_map)
+ jitted_funcs_map_t *p_funcs_map,
+ void **p_map_base)
{
using namespace perf;

@@ -422,6 +431,8 @@ int perf_clang__compile_bpf(const char *_filename,

if (p_funcs_map)
*p_funcs_map = (jitted_funcs_map_t)(M->copyJITResult());
+ if (p_map_base)
+ *p_map_base = M->getMapBase();
return 0;
}

diff --git a/tools/perf/util/c++/clang.h b/tools/perf/util/c++/clang.h
index df2eb8f..aacedc2 100644
--- a/tools/perf/util/c++/clang.h
+++ b/tools/perf/util/c++/clang.h
@@ -26,6 +26,7 @@ class PerfModule {
std::set<llvm::Function *> JITFunctions;

HookMap JITResult;
+ void *_map_base;

void prepareBPF(void);
void prepareJIT(void);
@@ -38,6 +39,10 @@ class PerfModule {
{
return new HookMap(JITResult);
}
+ inline void *getMapBase(void)
+ {
+ return _map_base;
+ }

PerfModule(std::unique_ptr<llvm::Module>&& M);

--
2.10.1

2016-11-15 04:10:17

by Wang Nan

[permalink] [raw]
Subject: [PATCH 09/34] perf tools: Pass context to perf hook functions

Pass a pointer to perf hook functions so they receive context
information created durnig setup.

Signed-off-by: Wang Nan <[email protected]>
Cc: Arnaldo Carvalho de Melo <[email protected]>
Cc: Alexei Starovoitov <[email protected]>
Cc: He Kuang <[email protected]>
Cc: Jiri Olsa <[email protected]>
Cc: Zefan Li <[email protected]>
Cc: [email protected]
---
tools/perf/tests/perf-hooks.c | 14 +++++++++-----
tools/perf/util/perf-hooks.c | 10 +++++++---
tools/perf/util/perf-hooks.h | 6 ++++--
3 files changed, 20 insertions(+), 10 deletions(-)

diff --git a/tools/perf/tests/perf-hooks.c b/tools/perf/tests/perf-hooks.c
index 9338cb2..665ecc1 100644
--- a/tools/perf/tests/perf-hooks.c
+++ b/tools/perf/tests/perf-hooks.c
@@ -15,13 +15,13 @@ static void sigsegv_handler(int sig __maybe_unused)
exit(-1);
}

-static int hook_flags;

-static void the_hook(void)
+static void the_hook(void *_hook_flags)
{
+ int *hook_flags = _hook_flags;
int *p = NULL;

- hook_flags = 1234;
+ *hook_flags = 1234;

/* Generate a segfault, test perf_hooks__recover */
*p = 0;
@@ -29,13 +29,17 @@ static void the_hook(void)

int test__perf_hooks(int subtest __maybe_unused)
{
+ int hook_flags = 0;
+
signal(SIGSEGV, sigsegv_handler);
- perf_hooks__set_hook("test", the_hook);
+ perf_hooks__set_hook("test", the_hook, &hook_flags);
perf_hooks__invoke_test();

/* hook is triggered? */
- if (hook_flags != 1234)
+ if (hook_flags != 1234) {
+ pr_debug("Setting failed: %d (%p)\n", hook_flags, &hook_flags);
return TEST_FAIL;
+ }

/* the buggy hook is removed? */
if (perf_hooks__get_hook("test"))
diff --git a/tools/perf/util/perf-hooks.c b/tools/perf/util/perf-hooks.c
index 4ce88e3..cb36830 100644
--- a/tools/perf/util/perf-hooks.c
+++ b/tools/perf/util/perf-hooks.c
@@ -27,7 +27,7 @@ void perf_hooks__invoke(const struct perf_hook_desc *desc)
*(current_perf_hook->p_hook_func) = NULL;
} else {
current_perf_hook = desc;
- (**desc->p_hook_func)();
+ (**desc->p_hook_func)(desc->hook_ctx);
}
current_perf_hook = NULL;
}
@@ -41,7 +41,9 @@ void perf_hooks__recover(void)
#define PERF_HOOK(name) \
perf_hook_func_t __perf_hook_func_##name = NULL; \
struct perf_hook_desc __perf_hook_desc_##name = \
- {.hook_name = #name, .p_hook_func = &__perf_hook_func_##name};
+ {.hook_name = #name, \
+ .p_hook_func = &__perf_hook_func_##name, \
+ .hook_ctx = NULL};
#include "perf-hooks-list.h"
#undef PERF_HOOK

@@ -54,7 +56,8 @@ static struct perf_hook_desc *perf_hooks[] = {
#undef PERF_HOOK

int perf_hooks__set_hook(const char *hook_name,
- perf_hook_func_t hook_func)
+ perf_hook_func_t hook_func,
+ void *hook_ctx)
{
unsigned int i;

@@ -65,6 +68,7 @@ int perf_hooks__set_hook(const char *hook_name,
if (*(perf_hooks[i]->p_hook_func))
pr_warning("Overwrite existing hook: %s\n", hook_name);
*(perf_hooks[i]->p_hook_func) = hook_func;
+ perf_hooks[i]->hook_ctx = hook_ctx;
return 0;
}
return -ENOENT;
diff --git a/tools/perf/util/perf-hooks.h b/tools/perf/util/perf-hooks.h
index 1d482b2..838d579 100644
--- a/tools/perf/util/perf-hooks.h
+++ b/tools/perf/util/perf-hooks.h
@@ -5,10 +5,11 @@
extern "C" {
#endif

-typedef void (*perf_hook_func_t)(void);
+typedef void (*perf_hook_func_t)(void *ctx);
struct perf_hook_desc {
const char * const hook_name;
perf_hook_func_t * const p_hook_func;
+ void *hook_ctx;
};

extern void perf_hooks__invoke(const struct perf_hook_desc *);
@@ -26,7 +27,8 @@ static inline void perf_hooks__invoke_##name(void) \

extern int
perf_hooks__set_hook(const char *hook_name,
- perf_hook_func_t hook_func);
+ perf_hook_func_t hook_func,
+ void *hook_ctx);

extern perf_hook_func_t
perf_hooks__get_hook(const char *hook_name);
--
2.10.1

2016-11-15 04:08:26

by Wang Nan

[permalink] [raw]
Subject: [PATCH 22/34] perf clang jit: Wrap llvm::Module using PerfModule

Use PerfModule wrap llvm::Module and return perf::PerfModule in APIs to
replace llvm::Module. Following commits are going to add new functions
to PerfModule.

getBPFObjectFromModule is merged to a method of perf::PerfModule.

Signed-off-by: Wang Nan <[email protected]>
Cc: Arnaldo Carvalho de Melo <[email protected]>
Cc: Alexei Starovoitov <[email protected]>
Cc: He Kuang <[email protected]>
Cc: Jiri Olsa <[email protected]>
Cc: Zefan Li <[email protected]>
Cc: [email protected]
---
tools/perf/util/c++/clang-test.cpp | 10 +++++-----
tools/perf/util/c++/clang.cpp | 20 +++++++++++++-------
tools/perf/util/c++/clang.h | 22 ++++++++++++++++------
3 files changed, 34 insertions(+), 18 deletions(-)

diff --git a/tools/perf/util/c++/clang-test.cpp b/tools/perf/util/c++/clang-test.cpp
index 9b11e8c..fb05e56 100644
--- a/tools/perf/util/c++/clang-test.cpp
+++ b/tools/perf/util/c++/clang-test.cpp
@@ -13,18 +13,18 @@ public:
~perf_clang_scope() {perf_clang__cleanup();}
};

-static std::unique_ptr<llvm::Module>
+static std::unique_ptr<perf::PerfModule>
__test__clang_to_IR(void)
{
unsigned int kernel_version;

if (fetch_kernel_version(&kernel_version, NULL, 0))
- return std::unique_ptr<llvm::Module>(nullptr);
+ return std::unique_ptr<perf::PerfModule>(nullptr);

std::string cflag_kver("-DLINUX_VERSION_CODE=" +
std::to_string(kernel_version));

- std::unique_ptr<llvm::Module> M =
+ std::unique_ptr<perf::PerfModule> M =
perf::getModuleFromSource({cflag_kver.c_str()},
"perf-test.c",
test_llvm__bpf_base_prog);
@@ -39,7 +39,7 @@ int test__clang_to_IR(void)
auto M = __test__clang_to_IR();
if (!M)
return -1;
- for (llvm::Function& F : *M)
+ for (llvm::Function& F : *(M->getModule()))
if (F.getName() == "bpf_func__SyS_epoll_wait")
return 0;
return -1;
@@ -53,7 +53,7 @@ int test__clang_to_obj(void)
if (!M)
return -1;

- auto Buffer = perf::getBPFObjectFromModule(&*M);
+ auto Buffer = M->toBPFObject();
if (!Buffer)
return -1;
return 0;
diff --git a/tools/perf/util/c++/clang.cpp b/tools/perf/util/c++/clang.cpp
index 3a3b9791..d31b0a5 100644
--- a/tools/perf/util/c++/clang.cpp
+++ b/tools/perf/util/c++/clang.cpp
@@ -64,7 +64,7 @@ createCompilerInvocation(llvm::opt::ArgStringList CFlags, StringRef& Path,
return CI;
}

-static std::unique_ptr<llvm::Module>
+static std::unique_ptr<PerfModule>
getModuleFromSource(llvm::opt::ArgStringList CFlags,
StringRef Path, IntrusiveRefCntPtr<vfs::FileSystem> VFS)
{
@@ -80,12 +80,12 @@ getModuleFromSource(llvm::opt::ArgStringList CFlags,

std::unique_ptr<CodeGenAction> Act(new EmitLLVMOnlyAction(&*LLVMCtx));
if (!Clang.ExecuteAction(*Act))
- return std::unique_ptr<llvm::Module>(nullptr);
+ return std::unique_ptr<PerfModule>(nullptr);

- return Act->takeModule();
+ return std::unique_ptr<PerfModule>(new PerfModule(std::move(Act->takeModule())));
}

-std::unique_ptr<llvm::Module>
+std::unique_ptr<PerfModule>
getModuleFromSource(llvm::opt::ArgStringList CFlags,
StringRef Name, StringRef Content)
{
@@ -106,15 +106,21 @@ getModuleFromSource(llvm::opt::ArgStringList CFlags,
return getModuleFromSource(std::move(CFlags), Name, OverlayFS);
}

-std::unique_ptr<llvm::Module>
+std::unique_ptr<PerfModule>
getModuleFromSource(llvm::opt::ArgStringList CFlags, StringRef Path)
{
IntrusiveRefCntPtr<vfs::FileSystem> VFS(vfs::getRealFileSystem());
return getModuleFromSource(std::move(CFlags), Path, VFS);
}

+PerfModule::PerfModule(std::unique_ptr<llvm::Module>&& M) : Module(std::move(M))
+{
+
+}
+
+
std::unique_ptr<llvm::SmallVectorImpl<char>>
-getBPFObjectFromModule(llvm::Module *Module)
+PerfModule::toBPFObject(void)
{
using namespace llvm;

@@ -278,7 +284,7 @@ int perf_clang__compile_bpf(const char *_filename,
auto M = getModuleFromSource(std::move(CFlags), Opts.getFileName());
if (!M)
return -EINVAL;
- auto O = getBPFObjectFromModule(&*M);
+ auto O = M->toBPFObject();
if (!O)
return -EINVAL;

diff --git a/tools/perf/util/c++/clang.h b/tools/perf/util/c++/clang.h
index dd8b042..cbb291b 100644
--- a/tools/perf/util/c++/clang.h
+++ b/tools/perf/util/c++/clang.h
@@ -11,16 +11,26 @@ namespace perf {

using namespace llvm;

-std::unique_ptr<Module>
+class PerfModule {
+private:
+ std::unique_ptr<llvm::Module> Module;
+public:
+ inline llvm::Module *getModule(void)
+ {
+ return Module.get();
+ }
+
+ PerfModule(std::unique_ptr<llvm::Module>&& M);
+
+ std::unique_ptr<llvm::SmallVectorImpl<char>> toBPFObject(void);
+};
+
+std::unique_ptr<PerfModule>
getModuleFromSource(opt::ArgStringList CFlags,
StringRef Name, StringRef Content);

-std::unique_ptr<Module>
+std::unique_ptr<PerfModule>
getModuleFromSource(opt::ArgStringList CFlags,
StringRef Path);
-
-std::unique_ptr<llvm::SmallVectorImpl<char>>
-getBPFObjectFromModule(llvm::Module *Module);
-
}
#endif
--
2.10.1

2016-11-15 04:10:53

by Wang Nan

[permalink] [raw]
Subject: [PATCH 29/34] perf clang jit: Allow jitted perf hook access BPF maps

Newly introduced jit-helpers.[ch] defines a series of helpers which helps
jitted perf hook functions accessing BPF maps defined in their BPF scripts.
The helpers fetches fd of 'struct bpf_map' from 'struct bpf_object' and the
address of 'struct bpf_map_def' in jitted file. 'struct bpf_object' is the
context passed to hooks.

Example:

$ cat ./test.c
/*******************************************************/
#define SEC(name) __attribute__((section(name), used))
#define BPF_MAP_TYPE_ARRAY 2
#define BPF_MAP_TYPE_PERF_EVENT_ARRAY 4
#define BPF_FUNC_map_lookup_elem 1
static void *(*bpf_map_lookup_elem)(void *map, void *key) =
(void *) BPF_FUNC_map_lookup_elem;
struct bpf_map_def {
unsigned int type;
unsigned int key_size;
unsigned int value_size;
unsigned int max_entries;
};
struct bpf_map_def SEC("maps") counter = {
.type = BPF_MAP_TYPE_ARRAY,
.key_size = sizeof(int),
.value_size = sizeof(int),
.max_entries = 1,
};
extern int jit_helper__map_update_elem(void *ctx, struct bpf_map_def *map,
void *key, void *value, unsigned long flags);
extern int jit_helper__map_lookup_elem(void *ctx, struct bpf_map_def *map,
void *key, void *value);
SEC("sys_close=SyS_close")
int sys_close(void *ctx)
{
int key = 0;
int *value;
value = bpf_map_lookup_elem(&counter, &key);
if (!value)
return 0;
__sync_fetch_and_add(value, 1);
return 0;
}
extern int printf(const char *fmt, ...);
SEC("perfhook:record_start")
void record_start(void *ctx)
{
int key = 0;
int value = 100000000;
printf("Welcom to perf record\n");
jit_helper__map_update_elem(ctx, &counter, &key, &value, 0);
}

SEC("perfhook:record_end")
void record_end(void *ctx)
{
int key = 0;
int value;
jit_helper__map_lookup_elem(ctx, &counter, &key, &value);
printf("Goodbye, perf record, value=%d\n", value);
}
char _license[] SEC("license") = "GPL";
int _version SEC("version") = LINUX_VERSION_CODE;
/*******************************************************/
$ sudo perf record -e ./test.c echo Hehe
Welcom to perf record
Hehe
[ perf record: Woken up 1 times to write data ]
Goodbye, perf record, value=100000644
[ perf record: Captured and wrote 0.014 MB perf.data ]

Signed-off-by: Wang Nan <[email protected]>
Cc: Arnaldo Carvalho de Melo <[email protected]>
Cc: Alexei Starovoitov <[email protected]>
Cc: He Kuang <[email protected]>
Cc: Jiri Olsa <[email protected]>
Cc: Zefan Li <[email protected]>
Cc: [email protected]
---
tools/perf/util/Build | 1 +
tools/perf/util/c++/clang.cpp | 10 +++++++-
tools/perf/util/jit-helpers.c | 60 +++++++++++++++++++++++++++++++++++++++++++
tools/perf/util/jit-helpers.h | 30 ++++++++++++++++++++++
4 files changed, 100 insertions(+), 1 deletion(-)
create mode 100644 tools/perf/util/jit-helpers.c
create mode 100644 tools/perf/util/jit-helpers.h

diff --git a/tools/perf/util/Build b/tools/perf/util/Build
index 743a889..33773cb 100644
--- a/tools/perf/util/Build
+++ b/tools/perf/util/Build
@@ -124,6 +124,7 @@ libperf-$(CONFIG_DWARF) += genelf_debug.o
endif

libperf-y += perf-hooks.o
+libperf-y += jit-helpers.o

libperf-$(CONFIG_CXX) += c++/

diff --git a/tools/perf/util/c++/clang.cpp b/tools/perf/util/c++/clang.cpp
index f8ea9bd..e500525 100644
--- a/tools/perf/util/c++/clang.cpp
+++ b/tools/perf/util/c++/clang.cpp
@@ -38,6 +38,7 @@
#include "llvm-utils.h"
#include "util-cxx.h"
#include "perf-hooks.h"
+#include "jit-helpers.h"

namespace perf {

@@ -196,12 +197,19 @@ PerfModule::toBPFObject(void)
return std::move(Buffer);
}

+#define __stringify_1(x) #x
+#define __stringify(x) __stringify_1(x)
static std::map<const std::string, const void *> exported_funcs =
{
-#define EXPORT(f) {#f, (const void *)&f}
+#define EXPORT(f) {__stringify(f), (const void *)&f}
EXPORT(test__clang_callback),
EXPORT(printf),
EXPORT(puts),
+ EXPORT(JIT_HELPER_FUNC_NAME(map_update_elem)),
+ EXPORT(JIT_HELPER_FUNC_NAME(map_lookup_elem)),
+ EXPORT(JIT_HELPER_FUNC_NAME(map_get_next_key)),
+ EXPORT(JIT_HELPER_FUNC_NAME(map_pin)),
+ EXPORT(JIT_HELPER_FUNC_NAME(map_get)),
#undef EXPORT
};

diff --git a/tools/perf/util/jit-helpers.c b/tools/perf/util/jit-helpers.c
new file mode 100644
index 0000000..993de14
--- /dev/null
+++ b/tools/perf/util/jit-helpers.c
@@ -0,0 +1,60 @@
+/*
+ * jit-helper.c
+ *
+ * Copyright (C) 2016 Wang Nan <[email protected]>
+ * Copyright (C) 2016 Huawei Inc.
+ *
+ * Provide helpers which can be invoked by jit scripts attached to
+ * perf hooks.
+ */
+
+#include <util/jit-helpers.h>
+#include <util/bpf-loader.h>
+#include <bpf/libbpf.h>
+#include <bpf/bpf.h>
+
+#include "asm/bug.h"
+
+static int get_bpf_map_fd(struct bpf_object *obj, void *map)
+{
+ int fd;
+ char errbuf[BUFSIZ];
+
+ fd = bpf__map_fd(obj, map);
+ if (fd < 0) {
+ bpf__strerror_map_fd(obj, map, fd, errbuf, sizeof(errbuf));
+ WARN_ONCE(fd < 0, "Failed to get map fd: %s\n", errbuf);
+ }
+ return fd;
+}
+
+#define PARAMS(args...) args
+#define DEFINE_JIT_BPF_MAP_HELPER(name, proto, args) \
+ JIT_BPF_MAP_HELPER(name, proto) { \
+ int map_fd = get_bpf_map_fd(ctx, map); \
+ \
+ if (map_fd < 0) \
+ return map_fd; \
+ return bpf_map_##name(map_fd, args); \
+ }
+
+DEFINE_JIT_BPF_MAP_HELPER(update_elem,
+ PARAMS(void *key, void *value, u64 flags),
+ PARAMS(key, value, flags))
+
+DEFINE_JIT_BPF_MAP_HELPER(lookup_elem,
+ PARAMS(void *key, void *value),
+ PARAMS(key, value))
+
+DEFINE_JIT_BPF_MAP_HELPER(get_next_key,
+ PARAMS(void *key, void *next_key),
+ PARAMS(key, next_key))
+
+DEFINE_JIT_BPF_MAP_HELPER(pin,
+ PARAMS(const char *pathname),
+ PARAMS(pathname));
+
+JIT_HELPER(int, map_get, const char *pathname)
+{
+ return bpf_map_get(pathname);
+}
diff --git a/tools/perf/util/jit-helpers.h b/tools/perf/util/jit-helpers.h
new file mode 100644
index 0000000..49e0578
--- /dev/null
+++ b/tools/perf/util/jit-helpers.h
@@ -0,0 +1,30 @@
+#ifndef JIT_HELPERS_H
+#define JIT_HELPERS_H
+
+#include <stdint.h>
+#include <util/perf-hooks.h>
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+#define JIT_HELPER_FUNC_NAME(name) jit_helper__##name
+
+#define JIT_HELPER(type, name, ...) \
+type JIT_HELPER_FUNC_NAME(name)(__VA_ARGS__)
+
+#define JIT_BPF_MAP_HELPER(name, ...) \
+ JIT_HELPER(int, map_##name, void *ctx, void *map, ##__VA_ARGS__)
+
+extern JIT_BPF_MAP_HELPER(update_elem, void *key, void *value, uint64_t flags);
+extern JIT_BPF_MAP_HELPER(lookup_elem, void *key, void *value);
+extern JIT_BPF_MAP_HELPER(get_next_key, void *key, void *next_key);
+extern JIT_BPF_MAP_HELPER(pin, const char *pathname);
+
+extern JIT_HELPER(int, map_get, const char *pathname);
+
+#ifdef __cplusplus
+}
+#endif
+
+#endif
--
2.10.1

2016-11-15 04:11:07

by Wang Nan

[permalink] [raw]
Subject: [PATCH 34/34] perf clang git: Export getpid() to perf hook

After this patch perf hooks can retrive pid of perf itself by calling
getpid. It is important for excluding event from perf.

Signed-off-by: Wang Nan <[email protected]>
Cc: Arnaldo Carvalho de Melo <[email protected]>
Cc: Alexei Starovoitov <[email protected]>
Cc: He Kuang <[email protected]>
Cc: Jiri Olsa <[email protected]>
Cc: Zefan Li <[email protected]>
Cc: [email protected]
---
tools/perf/util/c++/bpf-helper-str.c | 1 +
tools/perf/util/c++/clang.cpp | 1 +
2 files changed, 2 insertions(+)

diff --git a/tools/perf/util/c++/bpf-helper-str.c b/tools/perf/util/c++/bpf-helper-str.c
index 69aca3c..eedee89 100644
--- a/tools/perf/util/c++/bpf-helper-str.c
+++ b/tools/perf/util/c++/bpf-helper-str.c
@@ -13,6 +13,7 @@ const char clang_builtin_bpf_helper_str[] =
"extern void test__clang_callback(int);\n"
"extern int printf(const char *, ...);\n"
"extern int puts(const char *);\n"
+"extern int getpid(void);\n"
"extern int jit_helper__map_update_elem(void *, void *, void *, void *, unsigned long);\n"
"extern int jit_helper__map_lookup_elem(void *, void *, void *, void *);\n"
"extern int jit_helper__map_get_next_key(void *, void *, void *, void *);\n"
diff --git a/tools/perf/util/c++/clang.cpp b/tools/perf/util/c++/clang.cpp
index db31fb2..d6af336 100644
--- a/tools/perf/util/c++/clang.cpp
+++ b/tools/perf/util/c++/clang.cpp
@@ -236,6 +236,7 @@ static std::map<const std::string, const void *> exported_funcs =
EXPORT(test__clang_callback),
EXPORT(printf),
EXPORT(puts),
+ EXPORT(getpid),
EXPORT(JIT_HELPER_FUNC_NAME(map_update_elem)),
EXPORT(JIT_HELPER_FUNC_NAME(map_lookup_elem)),
EXPORT(JIT_HELPER_FUNC_NAME(map_get_next_key)),
--
2.10.1

2016-11-15 04:11:09

by Wang Nan

[permalink] [raw]
Subject: [PATCH 07/34] tools lib bpf: Retrive bpf_map through offset of bpf_map_def

Add a new API to libbpf, caller is able to get bpf_map through the
offset of bpf_map_def to 'maps' section.

The API will be used to help jitted perf hook code find fd of a map.

Signed-off-by: Wang Nan <[email protected]>
Cc: Arnaldo Carvalho de Melo <[email protected]>
Cc: Alexei Starovoitov <[email protected]>
Cc: Jiri Olsa <[email protected]>
Cc: Zefan Li <[email protected]>
Cc: [email protected]
---
tools/lib/bpf/libbpf.c | 12 ++++++++++++
tools/lib/bpf/libbpf.h | 8 ++++++++
2 files changed, 20 insertions(+)

diff --git a/tools/lib/bpf/libbpf.c b/tools/lib/bpf/libbpf.c
index 866d5cd..2e97459 100644
--- a/tools/lib/bpf/libbpf.c
+++ b/tools/lib/bpf/libbpf.c
@@ -1524,3 +1524,15 @@ bpf_object__find_map_by_name(struct bpf_object *obj, const char *name)
}
return NULL;
}
+
+struct bpf_map *
+bpf_object__find_map_by_offset(struct bpf_object *obj, size_t offset)
+{
+ int i;
+
+ for (i = 0; i < obj->nr_maps; i++) {
+ if (obj->maps[i].offset == offset)
+ return &obj->maps[i];
+ }
+ return ERR_PTR(-ENOENT);
+}
diff --git a/tools/lib/bpf/libbpf.h b/tools/lib/bpf/libbpf.h
index 0c0b012..a5a8b86 100644
--- a/tools/lib/bpf/libbpf.h
+++ b/tools/lib/bpf/libbpf.h
@@ -24,6 +24,7 @@
#include <stdio.h>
#include <stdbool.h>
#include <linux/err.h>
+#include <sys/types.h> // for size_t

enum libbpf_errno {
__LIBBPF_ERRNO__START = 4000,
@@ -200,6 +201,13 @@ struct bpf_map;
struct bpf_map *
bpf_object__find_map_by_name(struct bpf_object *obj, const char *name);

+/*
+ * Get bpf_map through the offset of corresponding struct bpf_map_def
+ * in the bpf object file.
+ */
+struct bpf_map *
+bpf_object__find_map_by_offset(struct bpf_object *obj, size_t offset);
+
struct bpf_map *
bpf_map__next(struct bpf_map *map, struct bpf_object *obj);
#define bpf_map__for_each(pos, obj) \
--
2.10.1

2016-11-15 04:11:47

by Wang Nan

[permalink] [raw]
Subject: [PATCH 26/34] perf clang jit: Actually JIT and hook in bpf loader

Makes perf_clang__compile_bpf() actually uses clang jit to compile perf
hooks. Returns a map through perf_clang__compile_bpf(), and set hooks
after bpf_object is created.

After this path jitting takes actions for bpf loader. For example:
$ cat ./test.c
/******************************************************/
#define SEC(name) __attribute__((section(name), used))
SEC("dofork=_do_fork")
int dofork(void *ctx)
{
return 0;
}
extern int printf(const char *fmt, ...);
SEC("perfhook:record_start")
void record_start(void)
{
printf("Welcom to perf record\n");
}
SEC("perfhook:record_end")
void record_end(void)
{
printf("Goodbye, perf record\n");
}
char _license[] SEC("license") = "GPL";
int _version SEC("version") = LINUX_VERSION_CODE;
/******************************************************/
$ perf record -e ./test.c sleep 1
Welcom to perf record
[ perf record: Woken up 1 times to write data ]
Goodbye, perf record
[ perf record: Captured and wrote 0.014 MB perf.data ]

Signed-off-by: Wang Nan <[email protected]>
Cc: Arnaldo Carvalho de Melo <[email protected]>
Cc: Alexei Starovoitov <[email protected]>
Cc: He Kuang <[email protected]>
Cc: Jiri Olsa <[email protected]>
Cc: Zefan Li <[email protected]>
Cc: [email protected]
---
tools/perf/util/bpf-loader.c | 11 ++++++++++-
tools/perf/util/c++/clang-c.h | 18 ++++++++++++++++--
tools/perf/util/c++/clang.cpp | 28 +++++++++++++++++++++++++++-
3 files changed, 53 insertions(+), 4 deletions(-)

diff --git a/tools/perf/util/bpf-loader.c b/tools/perf/util/bpf-loader.c
index a0ea334f..e50045f 100644
--- a/tools/perf/util/bpf-loader.c
+++ b/tools/perf/util/bpf-loader.c
@@ -85,9 +85,11 @@ struct bpf_object *bpf__prepare_load(const char *filename, bool source)
int err;
void *obj_buf;
size_t obj_buf_sz;
+ jitted_funcs_map_t jitted_funcs_map;

perf_clang__init();
- err = perf_clang__compile_bpf(filename, &obj_buf, &obj_buf_sz);
+ err = perf_clang__compile_bpf(filename, &obj_buf,
+ &obj_buf_sz, &jitted_funcs_map);
perf_clang__cleanup();
if (err) {
pr_warning("bpf: builtin compiling failed: %d, try external compiler\n", err);
@@ -101,6 +103,13 @@ struct bpf_object *bpf__prepare_load(const char *filename, bool source)
if (!IS_ERR(obj) && llvm_param.dump_obj)
llvm__dump_obj(filename, obj_buf, obj_buf_sz);

+ /*
+ * Call perf_clang__hook_jitted_func even IS_ERR(obj) to make sure
+ * the C++ map pointer is deleted.
+ */
+ if (jitted_funcs_map)
+ perf_clang__hook_jitted_func(jitted_funcs_map, obj, IS_ERR(obj));
+
free(obj_buf);
} else
obj = bpf_object__open(filename);
diff --git a/tools/perf/util/c++/clang-c.h b/tools/perf/util/c++/clang-c.h
index 9f75e41..021b1ad 100644
--- a/tools/perf/util/c++/clang-c.h
+++ b/tools/perf/util/c++/clang-c.h
@@ -8,6 +8,7 @@
extern "C" {
#endif

+typedef void *jitted_funcs_map_t;
#ifdef HAVE_LIBCLANGLLVM_SUPPORT
extern void perf_clang__init(void);
extern void perf_clang__cleanup(void);
@@ -20,7 +21,11 @@ extern void test__clang_callback(int x);

extern int perf_clang__compile_bpf(const char *filename,
void **p_obj_buf,
- size_t *p_obj_buf_sz);
+ size_t *p_obj_buf_sz,
+ jitted_funcs_map_t *p_funcs_map);
+
+extern int
+perf_clang__hook_jitted_func(jitted_funcs_map_t map, void *ctx, bool is_err);
#else


@@ -34,7 +39,16 @@ static inline int test__clang_jit(void) { return -1;}
static inline int
perf_clang__compile_bpf(const char *filename __maybe_unused,
void **p_obj_buf __maybe_unused,
- size_t *p_obj_buf_sz __maybe_unused)
+ size_t *p_obj_buf_sz __maybe_unused,
+ jitted_funcs_map_t *p_funcs_map __maybe_unused)
+{
+ return -ENOTSUP;
+}
+
+static inline int
+perf_clang__hook_jitted_func(jitted_funcs_map_t map __maybe_unused,
+ void *ctx __maybe_unused,
+ bool is_err __maybe_unused)
{
return -ENOTSUP;
}
diff --git a/tools/perf/util/c++/clang.cpp b/tools/perf/util/c++/clang.cpp
index 325fbe4..f2608f5 100644
--- a/tools/perf/util/c++/clang.cpp
+++ b/tools/perf/util/c++/clang.cpp
@@ -388,7 +388,8 @@ void perf_clang__cleanup(void)

int perf_clang__compile_bpf(const char *_filename,
void **p_obj_buf,
- size_t *p_obj_buf_sz)
+ size_t *p_obj_buf_sz,
+ jitted_funcs_map_t *p_funcs_map)
{
using namespace perf;

@@ -415,6 +416,31 @@ int perf_clang__compile_bpf(const char *_filename,
memcpy(buffer, O->data(), size);
*p_obj_buf = buffer;
*p_obj_buf_sz = size;
+
+ if (M->doJIT())
+ return -1;
+
+ if (p_funcs_map)
+ *p_funcs_map = (jitted_funcs_map_t)(M->copyJITResult());
+ return 0;
+}
+
+int perf_clang__hook_jitted_func(jitted_funcs_map_t map, void *ctx, bool is_err)
+{
+ std::unique_ptr<perf::PerfModule::HookMap>
+ hook_map((perf::PerfModule::HookMap *)map);
+
+ /* Do nothing but ensure map is deleted */
+ if (is_err)
+ return -1;
+
+ for (auto i : *hook_map) {
+ const char *hook_name = i.first.c_str();
+ perf_hook_func_t hook_func = i.second;
+
+ if (perf_hooks__set_hook(hook_name, hook_func, ctx))
+ return -1;
+ }
return 0;
}
}
--
2.10.1

2016-11-15 04:11:53

by Wang Nan

[permalink] [raw]
Subject: [PATCH 32/34] perf clang: Include helpers to BPF scripts

Automatically include some commonly used macros and struct definitions
into BPF scripts. Script writers are no longer required to define
'SEC' and 'struct bpf_map_def' in each of their scripts.

Signed-off-by: Wang Nan <[email protected]>
Cc: Arnaldo Carvalho de Melo <[email protected]>
Cc: Alexei Starovoitov <[email protected]>
Cc: He Kuang <[email protected]>
Cc: Jiri Olsa <[email protected]>
Cc: Zefan Li <[email protected]>
Cc: [email protected]
---
tools/perf/tests/bpf-script-example.c | 2 +-
tools/perf/tests/bpf-script-test-kbuild.c | 2 ++
tools/perf/tests/bpf-script-test-prologue.c | 4 +++-
tools/perf/tests/bpf-script-test-relocation.c | 3 +--
tools/perf/util/c++/Build | 1 +
tools/perf/util/c++/bpf-helper-str.c | 15 +++++++++++++++
tools/perf/util/c++/clang-bpf-includes.h | 1 +
tools/perf/util/c++/clang.cpp | 1 +
8 files changed, 25 insertions(+), 4 deletions(-)
create mode 100644 tools/perf/util/c++/bpf-helper-str.c

diff --git a/tools/perf/tests/bpf-script-example.c b/tools/perf/tests/bpf-script-example.c
index 42dc341..e60bebf 100644
--- a/tools/perf/tests/bpf-script-example.c
+++ b/tools/perf/tests/bpf-script-example.c
@@ -9,6 +9,7 @@
#define BPF_ANY 0
#define BPF_MAP_TYPE_ARRAY 2

+#ifndef BUILTIN_CLANG_DEFAULT_INCLUDE
struct bpf_map_def {
unsigned int type;
unsigned int key_size;
@@ -18,7 +19,6 @@ struct bpf_map_def {

#define SEC(NAME) __attribute__((section(NAME), used))

-#ifndef BUILTIN_CLANG_DEFAULT_INCLUDE
#define BPF_FUNC_map_lookup_elem 1
#define BPF_FUNC_map_update_elem 2

diff --git a/tools/perf/tests/bpf-script-test-kbuild.c b/tools/perf/tests/bpf-script-test-kbuild.c
index 3626924..f1b48a4 100644
--- a/tools/perf/tests/bpf-script-test-kbuild.c
+++ b/tools/perf/tests/bpf-script-test-kbuild.c
@@ -6,7 +6,9 @@
# error Need LINUX_VERSION_CODE
# error Example: for 4.2 kernel, put 'clang-opt="-DLINUX_VERSION_CODE=0x40200" into llvm section of ~/.perfconfig'
#endif
+#ifndef BUILTIN_CLANG_DEFAULT_INCLUDE
#define SEC(NAME) __attribute__((section(NAME), used))
+#endif

#include <uapi/linux/fs.h>
#include <uapi/asm/ptrace.h>
diff --git a/tools/perf/tests/bpf-script-test-prologue.c b/tools/perf/tests/bpf-script-test-prologue.c
index ada812b..e2176c9 100644
--- a/tools/perf/tests/bpf-script-test-prologue.c
+++ b/tools/perf/tests/bpf-script-test-prologue.c
@@ -6,7 +6,6 @@
# 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>

@@ -14,6 +13,9 @@
#define FMODE_WRITE 0x2

#ifndef BUILTIN_CLANG_DEFAULT_INCLUDE
+
+#define SEC(NAME) __attribute__((section(NAME), used))
+
static void (*bpf_trace_printk)(const char *fmt, int fmt_size, ...) =
(void *) 6;
#endif
diff --git a/tools/perf/tests/bpf-script-test-relocation.c b/tools/perf/tests/bpf-script-test-relocation.c
index 57c96a3..bb54926 100644
--- a/tools/perf/tests/bpf-script-test-relocation.c
+++ b/tools/perf/tests/bpf-script-test-relocation.c
@@ -9,6 +9,7 @@
#define BPF_ANY 0
#define BPF_MAP_TYPE_ARRAY 2

+#ifndef BUILTIN_CLANG_DEFAULT_INCLUDE
struct bpf_map_def {
unsigned int type;
unsigned int key_size;
@@ -17,8 +18,6 @@ struct bpf_map_def {
};

#define SEC(NAME) __attribute__((section(NAME), used))
-
-#ifndef BUILTIN_CLANG_DEFAULT_INCLUDE
#define BPF_FUNC_map_lookup_elem 1
#define BPF_FUNC_map_update_elem 2

diff --git a/tools/perf/util/c++/Build b/tools/perf/util/c++/Build
index bd71abf..faa0268 100644
--- a/tools/perf/util/c++/Build
+++ b/tools/perf/util/c++/Build
@@ -1,3 +1,4 @@
libperf-$(CONFIG_CLANGLLVM) += clang.o
libperf-$(CONFIG_CLANGLLVM) += clang-test.o
libperf-$(CONFIG_CLANGLLVM) += bpf-funcs-str.o
+libperf-$(CONFIG_CLANGLLVM) += bpf-helper-str.o
diff --git a/tools/perf/util/c++/bpf-helper-str.c b/tools/perf/util/c++/bpf-helper-str.c
new file mode 100644
index 0000000..17f915c
--- /dev/null
+++ b/tools/perf/util/c++/bpf-helper-str.c
@@ -0,0 +1,15 @@
+#include "clang-bpf-includes.h"
+const char clang_builtin_bpf_helper_str[] =
+"#ifdef BUILTIN_CLANG_DEFAULT_INCLUDE\n"
+"#ifndef BPF_HELPER_DEFINED\n"
+"#define BPF_HELPER_DEFINED\n"
+"struct bpf_map_def {\n"
+" unsigned int type;\n"
+" unsigned int key_size;\n"
+" unsigned int value_size;\n"
+" unsigned int max_entries;\n"
+"};\n"
+"#define SEC(NAME) __attribute__((section(NAME), used))\n"
+"#endif\n"
+"#endif"
+;
diff --git a/tools/perf/util/c++/clang-bpf-includes.h b/tools/perf/util/c++/clang-bpf-includes.h
index 385a5bb..577b40c 100644
--- a/tools/perf/util/c++/clang-bpf-includes.h
+++ b/tools/perf/util/c++/clang-bpf-includes.h
@@ -5,6 +5,7 @@ extern "C" {
#endif

extern const char clang_builtin_bpf_funcs_str[];
+extern const char clang_builtin_bpf_helper_str[];

#ifdef __cplusplus
}
diff --git a/tools/perf/util/c++/clang.cpp b/tools/perf/util/c++/clang.cpp
index 411a38c..db31fb2 100644
--- a/tools/perf/util/c++/clang.cpp
+++ b/tools/perf/util/c++/clang.cpp
@@ -48,6 +48,7 @@ static struct BPFHeader {
llvm::StringRef Content;
} BPFHeaders[] = {
{"/virtual/bpf-funcs.h", clang_builtin_bpf_funcs_str},
+ {"/virtual/bpf-helper.h", clang_builtin_bpf_helper_str},
};

static std::unique_ptr<llvm::LLVMContext> LLVMCtx;
--
2.10.1

2016-11-15 04:11:58

by Wang Nan

[permalink] [raw]
Subject: [PATCH 24/34] perf clang jit: add PerfModule::doJIT to JIT perfhook functions

PerfModule::doJIT JIT compile perfhook functions and saves result into
a map. Add a test case for it.

At this stage perfhook functions can do no useful things because they
can't invoke external functions and can't return value. Following
commits are going to make improvment.

Don't hook functions right after jitted because bpf_object is unavailable
during jitting but it should be the context of jitted functions.

Signed-off-by: Wang Nan <[email protected]>
Cc: Arnaldo Carvalho de Melo <[email protected]>
Cc: Alexei Starovoitov <[email protected]>
Cc: He Kuang <[email protected]>
Cc: Jiri Olsa <[email protected]>
Cc: Zefan Li <[email protected]>
Cc: [email protected]
---
tools/perf/tests/bpf-script-example.c | 8 ++++
tools/perf/tests/clang.c | 4 ++
tools/perf/util/c++/clang-c.h | 2 +
tools/perf/util/c++/clang-test.cpp | 32 +++++++++++++++-
tools/perf/util/c++/clang.cpp | 71 +++++++++++++++++++++++++++++++++++
tools/perf/util/c++/clang.h | 13 +++++++
6 files changed, 128 insertions(+), 2 deletions(-)

diff --git a/tools/perf/tests/bpf-script-example.c b/tools/perf/tests/bpf-script-example.c
index 268e5f8..265036e 100644
--- a/tools/perf/tests/bpf-script-example.c
+++ b/tools/perf/tests/bpf-script-example.c
@@ -46,3 +46,11 @@ int bpf_func__SyS_epoll_wait(void *ctx)
}
char _license[] SEC("license") = "GPL";
int _version SEC("version") = LINUX_VERSION_CODE;
+
+#ifdef TEST_PERF_HOOK
+SEC("perfhook:test")
+void hook_test(void)
+{
+ return;
+}
+#endif
diff --git a/tools/perf/tests/clang.c b/tools/perf/tests/clang.c
index 2964c06..f274e62 100644
--- a/tools/perf/tests/clang.c
+++ b/tools/perf/tests/clang.c
@@ -16,6 +16,10 @@ static struct {
.func = test__clang_to_obj,
.desc = "Test builtin clang compile C source to ELF object",
},
+ {
+ .func = test__clang_jit,
+ .desc = "Test builtin clang compile mixed BPF and native code",
+ },
#endif
};

diff --git a/tools/perf/util/c++/clang-c.h b/tools/perf/util/c++/clang-c.h
index 0eadd79..5ebcb41 100644
--- a/tools/perf/util/c++/clang-c.h
+++ b/tools/perf/util/c++/clang-c.h
@@ -14,6 +14,7 @@ extern void perf_clang__cleanup(void);

extern int test__clang_to_IR(void);
extern int test__clang_to_obj(void);
+extern int test__clang_jit(void);

extern int perf_clang__compile_bpf(const char *filename,
void **p_obj_buf,
@@ -26,6 +27,7 @@ static inline void perf_clang__cleanup(void) { }

static inline int test__clang_to_IR(void) { return -1; }
static inline int test__clang_to_obj(void) { return -1;}
+static inline int test__clang_jit(void) { return -1;}

static inline int
perf_clang__compile_bpf(const char *filename __maybe_unused,
diff --git a/tools/perf/util/c++/clang-test.cpp b/tools/perf/util/c++/clang-test.cpp
index fb05e56..2b4aa8d 100644
--- a/tools/perf/util/c++/clang-test.cpp
+++ b/tools/perf/util/c++/clang-test.cpp
@@ -5,6 +5,7 @@

#include <util-cxx.h>
#include <tests/llvm.h>
+#include <perf-hooks.h>
#include <string>

class perf_clang_scope {
@@ -14,7 +15,7 @@ public:
};

static std::unique_ptr<perf::PerfModule>
-__test__clang_to_IR(void)
+__test__clang_to_IR(bool perfhook)
{
unsigned int kernel_version;

@@ -23,14 +24,22 @@ __test__clang_to_IR(void)

std::string cflag_kver("-DLINUX_VERSION_CODE=" +
std::to_string(kernel_version));
+ std::string cflag_perfhook(perfhook ? "-DTEST_PERF_HOOK=1" : "");

std::unique_ptr<perf::PerfModule> M =
- perf::getModuleFromSource({cflag_kver.c_str()},
+ perf::getModuleFromSource({cflag_kver.c_str(),
+ cflag_perfhook.c_str()},
"perf-test.c",
test_llvm__bpf_base_prog);
return M;
}

+static std::unique_ptr<perf::PerfModule>
+__test__clang_to_IR(void)
+{
+ return __test__clang_to_IR(false);
+}
+
extern "C" {
int test__clang_to_IR(void)
{
@@ -59,4 +68,23 @@ int test__clang_to_obj(void)
return 0;
}

+int test__clang_jit(void)
+{
+ perf_clang_scope _scope;
+
+ auto M = __test__clang_to_IR(true);
+ if (!M)
+ return -1;
+
+ if (M->doJIT())
+ return -1;
+
+ std::unique_ptr<perf::PerfModule::HookMap> hooks(M->copyJITResult());
+ for (auto i : *hooks)
+ perf_hooks__set_hook(i.first.c_str(), i.second, NULL);
+
+ perf_hooks__invoke_test();
+ return 0;
+}
+
}
diff --git a/tools/perf/util/c++/clang.cpp b/tools/perf/util/c++/clang.cpp
index 98d05e2..03012b2 100644
--- a/tools/perf/util/c++/clang.cpp
+++ b/tools/perf/util/c++/clang.cpp
@@ -14,9 +14,14 @@
#include "clang/Frontend/TextDiagnosticPrinter.h"
#include "clang/Tooling/Tooling.h"
#include "llvm/ADT/SmallString.h"
+#include "llvm/ExecutionEngine/Orc/CompileUtils.h"
+#include "llvm/ExecutionEngine/Orc/IRCompileLayer.h"
+#include "llvm/ExecutionEngine/Orc/LambdaResolver.h"
+#include "llvm/ExecutionEngine/Orc/ObjectLinkingLayer.h"
#include "llvm/IR/LegacyPassManager.h"
#include "llvm/IR/Module.h"
#include "llvm/Option/Option.h"
+#include "llvm/Support/DynamicLibrary.h"
#include "llvm/Support/FileSystem.h"
#include "llvm/Support/ManagedStatic.h"
#include "llvm/Support/TargetRegistry.h"
@@ -24,11 +29,13 @@
#include "llvm/Target/TargetMachine.h"
#include "llvm/Target/TargetOptions.h"
#include <memory>
+#include <vector>

#include "clang.h"
#include "clang-c.h"
#include "llvm-utils.h"
#include "util-cxx.h"
+#include "perf-hooks.h"

namespace perf {

@@ -187,6 +194,66 @@ PerfModule::toBPFObject(void)
return std::move(Buffer);
}

+/*
+ * Use a global memory manager so allocated code and data won't be released
+ * when object destroy.
+ */
+static llvm::SectionMemoryManager JITMemoryManager;
+
+int PerfModule::doJIT(void)
+{
+ using namespace orc;
+
+ prepareJIT();
+
+ std::unique_ptr<TargetMachine> TM(EngineBuilder().selectTarget());
+ if (!TM) {
+ llvm::errs() << "Can't get target machine\n";
+ return -1;
+ }
+ const DataLayout DL(TM->createDataLayout());
+ Module->setDataLayout(DL);
+ Module->setTargetTriple(TM->getTargetTriple().normalize());
+
+ ObjectLinkingLayer<> ObjectLayer;
+ IRCompileLayer<decltype(ObjectLayer)> CompileLayer(ObjectLayer, SimpleCompiler(*TM));
+
+ auto Resolver = createLambdaResolver(
+ [](const std::string &Name) {
+ return RuntimeDyld::SymbolInfo(nullptr);
+ },
+ [](const std::string &Name) {
+ return RuntimeDyld::SymbolInfo(nullptr);
+ });
+
+ std::vector<llvm::Module *> Ms;
+ Ms.push_back(getModule());
+ CompileLayer.addModuleSet(std::move(Ms),
+ &JITMemoryManager,
+ std::move(Resolver));
+
+
+ for (Function *F : JITFunctions) {
+ JITSymbol sym = CompileLayer.findSymbol(F->getName().str(), true);
+
+ /*
+ * Type of F->getSection() is moving from
+ * const char * to StringRef.
+ * Convert it to std::string so we don't need
+ * consider this API change.
+ */
+ std::string sec(F->getSection());
+ std::string hook(&sec.c_str()[sizeof("perfhook:") - 1]);
+ perf_hook_func_t func = (perf_hook_func_t)(intptr_t)sym.getAddress();
+
+ if (JITResult[hook])
+ llvm::errs() << "Warning: multiple functions on hook "
+ << hook << ", only one is used\n";
+ JITResult[hook] = func;
+ }
+ return 0;
+}
+
class ClangOptions {
llvm::SmallString<PATH_MAX> FileName;
llvm::SmallString<64> KVerDef;
@@ -292,6 +359,10 @@ void perf_clang__init(void)
LLVMInitializeBPFTarget();
LLVMInitializeBPFTargetMC();
LLVMInitializeBPFAsmPrinter();
+
+ llvm::InitializeNativeTarget();
+ llvm::InitializeNativeTargetAsmPrinter();
+ llvm::InitializeNativeTargetAsmParser();
}

void perf_clang__cleanup(void)
diff --git a/tools/perf/util/c++/clang.h b/tools/perf/util/c++/clang.h
index 1eb71a6..df2eb8f 100644
--- a/tools/perf/util/c++/clang.h
+++ b/tools/perf/util/c++/clang.h
@@ -7,18 +7,26 @@
#include "llvm/Option/Option.h"
#include <memory>
#include <set>
+#include <map>
+
+#include "util/perf-hooks.h"

namespace perf {

using namespace llvm;

class PerfModule {
+public:
+ typedef std::map<std::string, perf_hook_func_t> HookMap;
private:
std::unique_ptr<llvm::Module> Module;

std::set<llvm::GlobalVariable *> Maps;
std::set<llvm::Function *> BPFFunctions;
std::set<llvm::Function *> JITFunctions;
+
+ HookMap JITResult;
+
void prepareBPF(void);
void prepareJIT(void);
public:
@@ -26,10 +34,15 @@ class PerfModule {
{
return Module.get();
}
+ inline HookMap *copyJITResult(void)
+ {
+ return new HookMap(JITResult);
+ }

PerfModule(std::unique_ptr<llvm::Module>&& M);

std::unique_ptr<llvm::SmallVectorImpl<char>> toBPFObject(void);
+ int doJIT(void);
};

std::unique_ptr<PerfModule>
--
2.10.1

2016-11-15 04:11:42

by Wang Nan

[permalink] [raw]
Subject: [PATCH 30/34] perf clang: Link BPF functions declaration into perf

Use a shell script to generate BPF functions declarations from kernel
source code, embed the generated header into a C string. Following
commits will utilizes clang's virtual file system to automatically
include this header to all BPF scripts.

The generated header is wrapped by a BUILTIN_CLANG_NO_DEFAULT_INCLUDE.
This macro will be used by following commits to allow user disable this
and other builtin includes.

Signed-off-by: Wang Nan <[email protected]>
Cc: Arnaldo Carvalho de Melo <[email protected]>
Cc: Alexei Starovoitov <[email protected]>
Cc: He Kuang <[email protected]>
Cc: Jiri Olsa <[email protected]>
Cc: Zefan Li <[email protected]>
Cc: [email protected]
---
tools/perf/util/c++/Build | 1 +
tools/perf/util/c++/bpf-funcs-str.c | 228 +++++++++++++++++++++++++++++++
tools/perf/util/c++/clang-bpf-includes.h | 12 ++
3 files changed, 241 insertions(+)
create mode 100644 tools/perf/util/c++/bpf-funcs-str.c
create mode 100644 tools/perf/util/c++/clang-bpf-includes.h

diff --git a/tools/perf/util/c++/Build b/tools/perf/util/c++/Build
index 988fef1..bd71abf 100644
--- a/tools/perf/util/c++/Build
+++ b/tools/perf/util/c++/Build
@@ -1,2 +1,3 @@
libperf-$(CONFIG_CLANGLLVM) += clang.o
libperf-$(CONFIG_CLANGLLVM) += clang-test.o
+libperf-$(CONFIG_CLANGLLVM) += bpf-funcs-str.o
diff --git a/tools/perf/util/c++/bpf-funcs-str.c b/tools/perf/util/c++/bpf-funcs-str.c
new file mode 100644
index 0000000..f6bcf76
--- /dev/null
+++ b/tools/perf/util/c++/bpf-funcs-str.c
@@ -0,0 +1,228 @@
+/*
+ * This file is generated by following script:
+ *
+ * #!/bin/bash
+ * TEMP_KBUILD=$(mktemp -d)
+ * KERNEL_DIR=$(pwd)
+ * OUTPUT=tools/perf/util/c++/bpf-funcs-str.c
+ * rm -rf $OUTPUT
+ * echo "Use temp dir: $TEMP_KBUILD"
+ * function finish()
+ * {
+ * rm -rf $TEMP_KBUILD
+ * }
+ * trap finish EXIT
+ * SRCLIST=$(find -name "*.c" | xargs grep bpf_func_proto -l)
+ * cd $TEMP_KBUILD
+ * yes '' | make -C $KERNEL_DIR O=`pwd` oldconfig
+ * cat << EOF >> ./.config
+ * CONFIG_BPF=y
+ * CONFIG_BPF_SYSCALL=y
+ * CONFIG_PERF_EVENTS=y
+ * CONFIG_SOCK_CGROUP_DATA=y
+ * EOF
+ * yes '' | make -C $KERNEL_DIR O=`pwd` oldconfig
+ * FIXOBJLIST=""
+ * for src in ${SRCLIST}
+ * do
+ * mkdir -p $(dirname $src)
+ * cat << EOF > "${src}-fix.c"
+ * #include <linux/init.h>
+ * #undef __init
+ * #define __init __attribute__((constructor))
+ * #include "`basename $src`"
+ * EOF
+ * if [ $(basename $src) == "syscall.c" ]
+ * then
+ * cat << EOF >> "${src}-fix.c"
+ * const struct bpf_verifier_ops *
+ * find_prog_type_export(enum bpf_prog_type type)
+ * {
+ * struct bpf_prog_aux aux;
+ * struct bpf_prog p = {.aux = &aux };
+ * if (find_prog_type(type, &p))
+ * return NULL;
+ * return p.aux->ops;
+ * }
+ * EOF
+ * fi
+ * FIXOBJLIST="$FIXOBJLIST ${src}-fix.o"
+ * done
+ * function dolink()
+ * {
+ * touch ./syms.c
+ * echo gcc kernel/bpf/main.o ./syms.c $FIXOBJLIST -o ./gen
+ * gcc kernel/bpf/main.o ./syms.c $FIXOBJLIST -o ./gen
+ * }
+ * MAIN=kernel/bpf/main.c
+ * cat << EOF > $MAIN
+ * #include <uapi/linux/bpf.h>
+ * #include <linux/bpf.h>
+ * struct bpf_func {
+ * const char *name;
+ * int id;
+ * } bpf_funcs[] = {
+ * EOF
+ * grep '^[[:space:]]BPF_FUNC_[^ ]*,' $KERNEL_DIR/include/uapi/linux/bpf.h | \
+ * sed -e 's/.*BPF_FUNC_\([^,]*\),.*$/\1/g' | \
+ * xargs -n 1 sh -c 'echo {.name = \"$1\", .id = BPF_FUNC_$1}, >> '"$MAIN" sh
+ * cat << EOF >> $MAIN
+ * {NULL, -1},
+ * };
+ * int capable(int x) {return 1;}
+ * int trace_printk_init_buffers(void) {return 0;}
+ * static int x;
+ * void *metadata_dst_alloc_percpu(int a, int b) {return &x;}
+ * int ___ratelimit(void *a, const void *func) {return 0;}
+ * extern const struct bpf_verifier_ops *
+ * find_prog_type_export(enum bpf_prog_type type);
+ * extern int printf(const char *fmt, ...);
+ * int main(int argc, char *argv[])
+ * {
+ * struct bpf_func *f = &bpf_funcs[0];
+ * printf("#ifndef BPF_FUNCS_DEFINED\n");
+ * printf("#define BPF_FUNCS_DEFINED\n");
+ * while (f->id != -1) {
+ * enum bpf_prog_type t;
+ * const enum bpf_arg_type *argt;
+ * const struct bpf_verifier_ops *ops = NULL;
+ * const struct bpf_func_proto *proto = NULL;
+ * if (f->id == 0)
+ * goto skip;
+ * for (t = BPF_PROG_TYPE_UNSPEC + 1; ; t++) {
+ * ops = find_prog_type_export(t);
+ * if (!ops)
+ * break;
+ * proto = ops->get_func_proto(f->id);
+ * if (proto)
+ * break;
+ * }
+ * if (!proto) {
+ * printf("static void (*%s)(void) = (void *)-1;\n", f->name);
+ * continue;
+ * }
+ * printf("static ");
+ * switch (proto->ret_type) {
+ * case RET_INTEGER:
+ * printf("long ");
+ * break;
+ * case RET_PTR_TO_MAP_VALUE_OR_NULL:
+ * printf("void *");
+ * break;
+ * default:
+ * case RET_VOID:
+ * printf("void ");
+ * break;
+ * }
+ * printf("(*bpf_%s)(", f->name);
+ * for (argt = &proto->arg1_type; argt <= &proto->arg5_type; argt++) {
+ * if (*argt == ARG_DONTCARE) {
+ * if (argt == &proto->arg1_type)
+ * printf("void");
+ * else if (strcmp(f->name, "trace_printk") == 0) {
+ * printf(", ...");
+ * goto finish;
+ * }
+ * goto finish;
+ * }
+ * if (argt != &proto->arg1_type)
+ * printf(", ");
+ * switch (*argt) {
+ * case ARG_CONST_MAP_PTR:
+ * case ARG_PTR_TO_MAP_KEY:
+ * case ARG_PTR_TO_MAP_VALUE:
+ * case ARG_PTR_TO_STACK:
+ * case ARG_PTR_TO_RAW_STACK:
+ * case ARG_PTR_TO_CTX:
+ * printf("void *");
+ * break;
+ * default:
+ * printf("unsigned long");
+ * break;
+ * }
+ * }
+ * finish:
+ * printf(") = (void *)%d;\n", f->id);
+ * skip:
+ * f++;
+ * }
+ * printf("#endif\n");
+ * return 0;
+ * }
+ * EOF
+ * make -j8 KBUILD_CFLAGS_KERNEL=-mpreferred-stack-boundary=4 kernel/bpf/main.o $FIXOBJLIST
+ * rm -f ./syms.c
+ * export LANG=POSIX
+ * export LC_ALL=POSIX
+ * dolink 2>&1 | \
+ * grep 'undefined reference' | \
+ * awk -F "\`" '{print $2}' | \
+ * sed "s/'$//g" | sort | uniq | \
+ * xargs -n 1 sh -c 'echo "int $1 __attribute__((weak));" >> ./syms.c' sh
+ * dolink
+ * cd $KERNEL_DIR
+ * unset X
+ * cat << EOF > $OUTPUT
+ * /$X*
+ * * This file is generated by following script:
+ * *
+ * EOF
+ * cat $0 | awk '$0 != "" {print " * " $0}' >> $OUTPUT
+ * echo ' *''/' >> $OUTPUT
+ * echo '#include "clang-bpf-includes.h"' >> $OUTPUT
+ * echo 'const char clang_builtin_bpf_funcs_str[] =' >> $OUTPUT
+ * echo '"#ifdef BUILTIN_CLANG_DEFAULT_INCLUDE\n"' >> $OUTPUT
+ * $TEMP_KBUILD/gen | awk '{print "\""$0"\\n\""}' >> $OUTPUT
+ * echo '"#endif\n"' >> $OUTPUT
+ * echo ';' >> $OUTPUT
+ * echo "Finish generating " $OUTPUT
+ */
+#include "clang-bpf-includes.h"
+const char clang_builtin_bpf_funcs_str[] =
+"#ifdef BUILTIN_CLANG_DEFAULT_INCLUDE\n"
+"#ifndef BPF_FUNCS_DEFINED\n"
+"#define BPF_FUNCS_DEFINED\n"
+"static void *(*bpf_map_lookup_elem)(void *, void *) = (void *)1;\n"
+"static long (*bpf_map_update_elem)(void *, void *, void *, unsigned long) = (void *)2;\n"
+"static long (*bpf_map_delete_elem)(void *, void *) = (void *)3;\n"
+"static long (*bpf_probe_read)(void *, unsigned long, unsigned long) = (void *)4;\n"
+"static long (*bpf_ktime_get_ns)(void) = (void *)5;\n"
+"static long (*bpf_trace_printk)(void *, unsigned long, ...) = (void *)6;\n"
+"static long (*bpf_get_prandom_u32)(void) = (void *)7;\n"
+"static long (*bpf_get_smp_processor_id)(void) = (void *)8;\n"
+"static long (*bpf_skb_store_bytes)(void *, unsigned long, void *, unsigned long, unsigned long) = (void *)9;\n"
+"static long (*bpf_l3_csum_replace)(void *, unsigned long, unsigned long, unsigned long, unsigned long) = (void *)10;\n"
+"static long (*bpf_l4_csum_replace)(void *, unsigned long, unsigned long, unsigned long, unsigned long) = (void *)11;\n"
+"static void (*bpf_tail_call)(void *, void *, unsigned long) = (void *)12;\n"
+"static long (*bpf_clone_redirect)(void *, unsigned long, unsigned long) = (void *)13;\n"
+"static long (*bpf_get_current_pid_tgid)(void) = (void *)14;\n"
+"static long (*bpf_get_current_uid_gid)(void) = (void *)15;\n"
+"static long (*bpf_get_current_comm)(void *, unsigned long) = (void *)16;\n"
+"static long (*bpf_get_cgroup_classid)(void *) = (void *)17;\n"
+"static long (*bpf_skb_vlan_push)(void *, unsigned long, unsigned long) = (void *)18;\n"
+"static long (*bpf_skb_vlan_pop)(void *) = (void *)19;\n"
+"static long (*bpf_skb_get_tunnel_key)(void *, void *, unsigned long, unsigned long) = (void *)20;\n"
+"static long (*bpf_skb_set_tunnel_key)(void *, void *, unsigned long, unsigned long) = (void *)21;\n"
+"static long (*bpf_perf_event_read)(void *, unsigned long) = (void *)22;\n"
+"static long (*bpf_redirect)(unsigned long, unsigned long) = (void *)23;\n"
+"static long (*bpf_get_route_realm)(void *) = (void *)24;\n"
+"static long (*bpf_perf_event_output)(void *, void *, unsigned long, void *, unsigned long) = (void *)25;\n"
+"static long (*bpf_skb_load_bytes)(void *, unsigned long, void *, unsigned long) = (void *)26;\n"
+"static long (*bpf_get_stackid)(void *, void *, unsigned long) = (void *)27;\n"
+"static long (*bpf_csum_diff)(void *, unsigned long, void *, unsigned long, unsigned long) = (void *)28;\n"
+"static long (*bpf_skb_get_tunnel_opt)(void *, void *, unsigned long) = (void *)29;\n"
+"static long (*bpf_skb_set_tunnel_opt)(void *, void *, unsigned long) = (void *)30;\n"
+"static long (*bpf_skb_change_proto)(void *, unsigned long, unsigned long) = (void *)31;\n"
+"static long (*bpf_skb_change_type)(void *, unsigned long) = (void *)32;\n"
+"static long (*bpf_skb_under_cgroup)(void *, void *, unsigned long) = (void *)33;\n"
+"static long (*bpf_get_hash_recalc)(void *) = (void *)34;\n"
+"static long (*bpf_get_current_task)(void) = (void *)35;\n"
+"static long (*bpf_probe_write_user)(unsigned long, void *, unsigned long) = (void *)36;\n"
+"static long (*bpf_current_task_under_cgroup)(void *, unsigned long) = (void *)37;\n"
+"static long (*bpf_skb_change_tail)(void *, unsigned long, unsigned long) = (void *)38;\n"
+"static long (*bpf_skb_pull_data)(void *, unsigned long) = (void *)39;\n"
+"static long (*bpf_csum_update)(void *, unsigned long) = (void *)40;\n"
+"static long (*bpf_set_hash_invalid)(void *) = (void *)41;\n"
+"#endif\n"
+"#endif\n"
+;
diff --git a/tools/perf/util/c++/clang-bpf-includes.h b/tools/perf/util/c++/clang-bpf-includes.h
new file mode 100644
index 0000000..385a5bb
--- /dev/null
+++ b/tools/perf/util/c++/clang-bpf-includes.h
@@ -0,0 +1,12 @@
+#ifndef CLANG_BPF_INCLUDS_H
+#define CLANG_BPF_INCLUDS_H
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+extern const char clang_builtin_bpf_funcs_str[];
+
+#ifdef __cplusplus
+}
+#endif
+#endif
--
2.10.1

2016-11-15 04:11:40

by Wang Nan

[permalink] [raw]
Subject: [PATCH 31/34] perf clang: Declare BPF functions for BPF scripts automatically

Use Clang's OverlayFileSystem, add '-include' options to make builtin
clang define BPF functions. After this patch BPF script writer needn't
define BPF functions by their own.

Add -DBUILTIN_CLANG_DEFAULT_INCLUDE to builtin clang so when adopting
builtin clang BPF functions can be automatically defined, and keep
undefined when using external clang. Passing a
-UBUILTIN_CLANG_DEFAULT_INCLUDE to cancel this defaudefinition.

Test cases are updated to avoid redefinition of these functions.

Signed-off-by: Wang Nan <[email protected]>
Cc: Arnaldo Carvalho de Melo <[email protected]>
Cc: Alexei Starovoitov <[email protected]>
Cc: He Kuang <[email protected]>
Cc: Jiri Olsa <[email protected]>
Cc: Zefan Li <[email protected]>
Cc: [email protected]
---
tools/perf/tests/bpf-script-example.c | 18 +++++++++------
tools/perf/tests/bpf-script-test-prologue.c | 2 ++
tools/perf/tests/bpf-script-test-relocation.c | 18 +++++++++------
tools/perf/util/c++/clang.cpp | 33 ++++++++++++++++++++++++++-
4 files changed, 56 insertions(+), 15 deletions(-)

diff --git a/tools/perf/tests/bpf-script-example.c b/tools/perf/tests/bpf-script-example.c
index ccbc19c..42dc341 100644
--- a/tools/perf/tests/bpf-script-example.c
+++ b/tools/perf/tests/bpf-script-example.c
@@ -8,13 +8,6 @@
#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;
@@ -24,6 +17,17 @@ struct bpf_map_def {
};

#define SEC(NAME) __attribute__((section(NAME), used))
+
+#ifndef BUILTIN_CLANG_DEFAULT_INCLUDE
+#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;
+#endif
+
struct bpf_map_def SEC("maps") flip_table = {
.type = BPF_MAP_TYPE_ARRAY,
.key_size = sizeof(int),
diff --git a/tools/perf/tests/bpf-script-test-prologue.c b/tools/perf/tests/bpf-script-test-prologue.c
index 7230e62..ada812b 100644
--- a/tools/perf/tests/bpf-script-test-prologue.c
+++ b/tools/perf/tests/bpf-script-test-prologue.c
@@ -13,8 +13,10 @@
#define FMODE_READ 0x1
#define FMODE_WRITE 0x2

+#ifndef BUILTIN_CLANG_DEFAULT_INCLUDE
static void (*bpf_trace_printk)(const char *fmt, int fmt_size, ...) =
(void *) 6;
+#endif

SEC("func=null_lseek file->f_mode offset orig")
int bpf_func__null_lseek(void *ctx, int err, unsigned long f_mode,
diff --git a/tools/perf/tests/bpf-script-test-relocation.c b/tools/perf/tests/bpf-script-test-relocation.c
index 93af774..57c96a3 100644
--- a/tools/perf/tests/bpf-script-test-relocation.c
+++ b/tools/perf/tests/bpf-script-test-relocation.c
@@ -8,13 +8,6 @@
#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;
@@ -24,6 +17,17 @@ struct bpf_map_def {
};

#define SEC(NAME) __attribute__((section(NAME), used))
+
+#ifndef BUILTIN_CLANG_DEFAULT_INCLUDE
+#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;
+#endif
+
struct bpf_map_def SEC("maps") my_table = {
.type = BPF_MAP_TYPE_ARRAY,
.key_size = sizeof(int),
diff --git a/tools/perf/util/c++/clang.cpp b/tools/perf/util/c++/clang.cpp
index e500525..411a38c 100644
--- a/tools/perf/util/c++/clang.cpp
+++ b/tools/perf/util/c++/clang.cpp
@@ -39,9 +39,17 @@
#include "util-cxx.h"
#include "perf-hooks.h"
#include "jit-helpers.h"
+#include "clang-bpf-includes.h"

namespace perf {

+static struct BPFHeader {
+ llvm::StringRef Path;
+ llvm::StringRef Content;
+} BPFHeaders[] = {
+ {"/virtual/bpf-funcs.h", clang_builtin_bpf_funcs_str},
+};
+
static std::unique_ptr<llvm::LLVMContext> LLVMCtx;

using namespace clang;
@@ -66,6 +74,11 @@ createCompilerInvocation(llvm::opt::ArgStringList CFlags, StringRef& Path,
"-x", "c"};

CCArgs.append(CFlags.begin(), CFlags.end());
+ for (BPFHeader &h : BPFHeaders) {
+ CCArgs.append(1, "-include");
+ CCArgs.append(1, h.Path.begin());
+ }
+
CompilerInvocation *CI = tooling::newInvocation(&Diags, CCArgs);

FrontendOptions& Opts = CI->getFrontendOpts();
@@ -74,6 +87,22 @@ createCompilerInvocation(llvm::opt::ArgStringList CFlags, StringRef& Path,
return CI;
}

+static IntrusiveRefCntPtr<vfs::FileSystem>
+addBPFHeaders(IntrusiveRefCntPtr<vfs::FileSystem> VFS)
+{
+ using namespace vfs;
+
+ llvm::IntrusiveRefCntPtr<OverlayFileSystem> OverlayFS(
+ new OverlayFileSystem(VFS));
+ llvm::IntrusiveRefCntPtr<InMemoryFileSystem> MemFS(
+ new InMemoryFileSystem(true));
+ OverlayFS->pushOverlay(MemFS);
+
+ for (BPFHeader &h : BPFHeaders)
+ MemFS->addFile(h.Path, 0, llvm::MemoryBuffer::getMemBuffer(h.Content));
+ return OverlayFS;
+}
+
static std::unique_ptr<PerfModule>
getModuleFromSource(llvm::opt::ArgStringList CFlags,
StringRef Path, IntrusiveRefCntPtr<vfs::FileSystem> VFS)
@@ -81,7 +110,8 @@ getModuleFromSource(llvm::opt::ArgStringList CFlags,
CompilerInstance Clang;
Clang.createDiagnostics();

- Clang.setVirtualFileSystem(&*VFS);
+ IntrusiveRefCntPtr<vfs::FileSystem> OverlayVFS = addBPFHeaders(VFS);
+ Clang.setVirtualFileSystem(&*OverlayVFS);

IntrusiveRefCntPtr<CompilerInvocation> CI =
createCompilerInvocation(std::move(CFlags), Path,
@@ -364,6 +394,7 @@ public:
{
CFlags.push_back(KVerDef.c_str());
CFlags.push_back(NRCpusDef.c_str());
+ CFlags.push_back("-DBUILTIN_CLANG_DEFAULT_INCLUDE");

fillCFlagsFromString(CFlags, clang_opt);
fillCFlagsFromString(CFlags, kbuild_include_opts, true);
--
2.10.1

2016-11-15 04:12:49

by Wang Nan

[permalink] [raw]
Subject: [PATCH 17/34] perf clang: Update test case to use real BPF script

Allow C++ code to use util.h and tests/llvm.h. Let 'perf test' compile a
real BPF script.

Signed-off-by: Wang Nan <[email protected]>
Cc: Alexei Starovoitov <[email protected]>
Cc: He Kuang <[email protected]>
Cc: Jiri Olsa <[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/Makefile.config | 27 +++++++++++++++------------
tools/perf/tests/llvm.h | 7 +++++++
tools/perf/util/c++/clang-test.cpp | 17 ++++++++++++++---
tools/perf/util/util-cxx.h | 26 ++++++++++++++++++++++++++
4 files changed, 62 insertions(+), 15 deletions(-)
create mode 100644 tools/perf/util/util-cxx.h

diff --git a/tools/perf/Makefile.config b/tools/perf/Makefile.config
index b7c9c80..09c2a98 100644
--- a/tools/perf/Makefile.config
+++ b/tools/perf/Makefile.config
@@ -212,24 +212,27 @@ ifeq ($(DEBUG),0)
endif
endif

-CFLAGS += -I$(src-perf)/util/include
-CFLAGS += -I$(src-perf)/arch/$(ARCH)/include
-CFLAGS += -I$(srctree)/tools/include/uapi
-CFLAGS += -I$(srctree)/tools/include/
-CFLAGS += -I$(srctree)/tools/arch/$(ARCH)/include/uapi
-CFLAGS += -I$(srctree)/tools/arch/$(ARCH)/include/
-CFLAGS += -I$(srctree)/tools/arch/$(ARCH)/
+INC_FLAGS += -I$(src-perf)/util/include
+INC_FLAGS += -I$(src-perf)/arch/$(ARCH)/include
+INC_FLAGS += -I$(srctree)/tools/include/uapi
+INC_FLAGS += -I$(srctree)/tools/include/
+INC_FLAGS += -I$(srctree)/tools/arch/$(ARCH)/include/uapi
+INC_FLAGS += -I$(srctree)/tools/arch/$(ARCH)/include/
+INC_FLAGS += -I$(srctree)/tools/arch/$(ARCH)/

# $(obj-perf) for generated common-cmds.h
# $(obj-perf)/util for generated bison/flex headers
ifneq ($(OUTPUT),)
-CFLAGS += -I$(obj-perf)/util
-CFLAGS += -I$(obj-perf)
+INC_FLAGS += -I$(obj-perf)/util
+INC_FLAGS += -I$(obj-perf)
endif

-CFLAGS += -I$(src-perf)/util
-CFLAGS += -I$(src-perf)
-CFLAGS += -I$(srctree)/tools/lib/
+INC_FLAGS += -I$(src-perf)/util
+INC_FLAGS += -I$(src-perf)
+INC_FLAGS += -I$(srctree)/tools/lib/
+
+CFLAGS += $(INC_FLAGS)
+CXXFLAGS += $(INC_FLAGS)

CFLAGS += -D_LARGEFILE64_SOURCE -D_FILE_OFFSET_BITS=64 -D_GNU_SOURCE

diff --git a/tools/perf/tests/llvm.h b/tools/perf/tests/llvm.h
index 0eaa604..b835717 100644
--- a/tools/perf/tests/llvm.h
+++ b/tools/perf/tests/llvm.h
@@ -1,6 +1,10 @@
#ifndef PERF_TEST_LLVM_H
#define PERF_TEST_LLVM_H

+#ifdef __cplusplus
+extern "C" {
+#endif
+
#include <stddef.h> /* for size_t */
#include <stdbool.h> /* for bool */

@@ -20,4 +24,7 @@ enum test_llvm__testcase {
int test_llvm__fetch_bpf_obj(void **p_obj_buf, size_t *p_obj_buf_sz,
enum test_llvm__testcase index, bool force,
bool *should_load_fail);
+#ifdef __cplusplus
+}
+#endif
#endif
diff --git a/tools/perf/util/c++/clang-test.cpp b/tools/perf/util/c++/clang-test.cpp
index 0f484fb..d84e760 100644
--- a/tools/perf/util/c++/clang-test.cpp
+++ b/tools/perf/util/c++/clang-test.cpp
@@ -3,6 +3,10 @@
#include "llvm/IR/Function.h"
#include "llvm/IR/LLVMContext.h"

+#include <util-cxx.h>
+#include <tests/llvm.h>
+#include <string>
+
class perf_clang_scope {
public:
explicit perf_clang_scope() {perf_clang__init();}
@@ -14,17 +18,24 @@ extern "C" {
int test__clang_to_IR(void)
{
perf_clang_scope _scope;
+ unsigned int kernel_version;
+
+ if (fetch_kernel_version(&kernel_version, NULL, 0))
+ return -1;
+
+ std::string cflag_kver("-DLINUX_VERSION_CODE=" +
+ std::to_string(kernel_version));

std::unique_ptr<llvm::Module> M =
- perf::getModuleFromSource({"-DRESULT=1"},
+ perf::getModuleFromSource({cflag_kver.c_str()},
"perf-test.c",
- "int myfunc(void) {return RESULT;}");
+ test_llvm__bpf_base_prog);

if (!M)
return -1;

for (llvm::Function& F : *M)
- if (F.getName() == "myfunc")
+ if (F.getName() == "bpf_func__SyS_epoll_wait")
return 0;
return -1;
}
diff --git a/tools/perf/util/util-cxx.h b/tools/perf/util/util-cxx.h
new file mode 100644
index 0000000..0e0e019
--- /dev/null
+++ b/tools/perf/util/util-cxx.h
@@ -0,0 +1,26 @@
+/*
+ * Support C++ source use utilities defined in util.h
+ */
+
+#ifndef PERF_UTIL_UTIL_CXX_H
+#define PERF_UTIL_UTIL_CXX_H
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+/*
+ * Now 'new' is the only C++ keyword found in util.h:
+ * in tools/include/linux/rbtree.h
+ *
+ * Other keywords, like class and delete, should be
+ * redefined if necessary.
+ */
+#define new _new
+#include "util.h"
+#undef new
+
+#ifdef __cplusplus
+}
+#endif
+#endif
--
2.10.1

2016-11-15 04:12:53

by Wang Nan

[permalink] [raw]
Subject: [PATCH 20/34] perf clang: Pass full path to builtin clang

If clang changes its working directory, relative path passed to
perf_clang__compile_bpf() becomes invalid. Before running clang,
convert it to absolute path so file can be found even working directory
is changed.

Signed-off-by: Wang Nan <[email protected]>
Cc: Arnaldo Carvalho de Melo <[email protected]>
Cc: Alexei Starovoitov <[email protected]>
Cc: He Kuang <[email protected]>
Cc: Jiri Olsa <[email protected]>
Cc: Zefan Li <[email protected]>
Cc: [email protected]
---
tools/perf/util/c++/clang.cpp | 8 ++++++--
1 file changed, 6 insertions(+), 2 deletions(-)

diff --git a/tools/perf/util/c++/clang.cpp b/tools/perf/util/c++/clang.cpp
index 1e97415..610f1cf 100644
--- a/tools/perf/util/c++/clang.cpp
+++ b/tools/perf/util/c++/clang.cpp
@@ -13,6 +13,7 @@
#include "clang/Frontend/CompilerInstance.h"
#include "clang/Frontend/TextDiagnosticPrinter.h"
#include "clang/Tooling/Tooling.h"
+#include "llvm/ADT/SmallString.h"
#include "llvm/IR/LegacyPassManager.h"
#include "llvm/IR/Module.h"
#include "llvm/Option/Option.h"
@@ -164,7 +165,7 @@ void perf_clang__cleanup(void)
llvm::llvm_shutdown();
}

-int perf_clang__compile_bpf(const char *filename,
+int perf_clang__compile_bpf(const char *_filename,
void **p_obj_buf,
size_t *p_obj_buf_sz)
{
@@ -173,8 +174,11 @@ int perf_clang__compile_bpf(const char *filename,
if (!p_obj_buf || !p_obj_buf_sz)
return -EINVAL;

+ llvm::SmallString<PATH_MAX> FileName(_filename);
+ llvm::sys::fs::make_absolute(FileName);
+
llvm::opt::ArgStringList CFlags;
- auto M = getModuleFromSource(std::move(CFlags), filename);
+ auto M = getModuleFromSource(std::move(CFlags), FileName.data());
if (!M)
return -EINVAL;
auto O = getBPFObjectFromModule(&*M);
--
2.10.1

2016-11-15 04:12:46

by Wang Nan

[permalink] [raw]
Subject: [PATCH 18/34] perf clang: Support compile IR to BPF object and add testcase

getBPFObjectFromModule() is introduced to compile LLVM IR(Module)
to BPF object. Add new testcase for it.

Test result:
$ ./buildperf/perf test -v clang
51: Test builtin clang support :
51.1: Test builtin clang compile C source to IR :
--- start ---
test child forked, pid 21822
test child finished with 0
---- end ----
Test builtin clang support subtest 0: Ok
51.2: Test builtin clang compile C source to ELF object :
--- start ---
test child forked, pid 21823
test child finished with 0
---- end ----
Test builtin clang support subtest 1: Ok

Signed-off-by: Wang Nan <[email protected]>
Cc: Arnaldo Carvalho de Melo <[email protected]>
Cc: Alexei Starovoitov <[email protected]>
Cc: He Kuang <[email protected]>
Cc: Jiri Olsa <[email protected]>
Cc: Zefan Li <[email protected]>
Cc: [email protected]
---
tools/perf/tests/clang.c | 6 ++++-
tools/perf/util/c++/clang-c.h | 1 +
tools/perf/util/c++/clang-test.cpp | 31 +++++++++++++++++++++-----
tools/perf/util/c++/clang.cpp | 45 ++++++++++++++++++++++++++++++++++++++
tools/perf/util/c++/clang.h | 3 +++
5 files changed, 79 insertions(+), 7 deletions(-)

diff --git a/tools/perf/tests/clang.c b/tools/perf/tests/clang.c
index 57ee160..2964c06 100644
--- a/tools/perf/tests/clang.c
+++ b/tools/perf/tests/clang.c
@@ -12,6 +12,10 @@ static struct {
.func = test__clang_to_IR,
.desc = "Test builtin clang compile C source to IR",
},
+ {
+ .func = test__clang_to_obj,
+ .desc = "Test builtin clang compile C source to ELF object",
+ },
#endif
};

@@ -33,7 +37,7 @@ int test__clang(int i __maybe_unused)
return TEST_SKIP;
}
#else
-int test__clang(int i __maybe_unused)
+int test__clang(int i)
{
if (i < 0 || i >= (int)ARRAY_SIZE(clang_testcase_table))
return TEST_FAIL;
diff --git a/tools/perf/util/c++/clang-c.h b/tools/perf/util/c++/clang-c.h
index dcde4b5..22b3936 100644
--- a/tools/perf/util/c++/clang-c.h
+++ b/tools/perf/util/c++/clang-c.h
@@ -9,6 +9,7 @@ extern void perf_clang__init(void);
extern void perf_clang__cleanup(void);

extern int test__clang_to_IR(void);
+extern int test__clang_to_obj(void);

#ifdef __cplusplus
}
diff --git a/tools/perf/util/c++/clang-test.cpp b/tools/perf/util/c++/clang-test.cpp
index d84e760..9b11e8c 100644
--- a/tools/perf/util/c++/clang-test.cpp
+++ b/tools/perf/util/c++/clang-test.cpp
@@ -13,15 +13,13 @@ public:
~perf_clang_scope() {perf_clang__cleanup();}
};

-extern "C" {
-
-int test__clang_to_IR(void)
+static std::unique_ptr<llvm::Module>
+__test__clang_to_IR(void)
{
- perf_clang_scope _scope;
unsigned int kernel_version;

if (fetch_kernel_version(&kernel_version, NULL, 0))
- return -1;
+ return std::unique_ptr<llvm::Module>(nullptr);

std::string cflag_kver("-DLINUX_VERSION_CODE=" +
std::to_string(kernel_version));
@@ -30,14 +28,35 @@ int test__clang_to_IR(void)
perf::getModuleFromSource({cflag_kver.c_str()},
"perf-test.c",
test_llvm__bpf_base_prog);
+ return M;
+}
+
+extern "C" {
+int test__clang_to_IR(void)
+{
+ perf_clang_scope _scope;

+ auto M = __test__clang_to_IR();
if (!M)
return -1;
-
for (llvm::Function& F : *M)
if (F.getName() == "bpf_func__SyS_epoll_wait")
return 0;
return -1;
}

+int test__clang_to_obj(void)
+{
+ perf_clang_scope _scope;
+
+ auto M = __test__clang_to_IR();
+ if (!M)
+ return -1;
+
+ auto Buffer = perf::getBPFObjectFromModule(&*M);
+ if (!Buffer)
+ return -1;
+ return 0;
+}
+
}
diff --git a/tools/perf/util/c++/clang.cpp b/tools/perf/util/c++/clang.cpp
index 715ca0a..2a1a75d 100644
--- a/tools/perf/util/c++/clang.cpp
+++ b/tools/perf/util/c++/clang.cpp
@@ -13,10 +13,15 @@
#include "clang/Frontend/CompilerInstance.h"
#include "clang/Frontend/TextDiagnosticPrinter.h"
#include "clang/Tooling/Tooling.h"
+#include "llvm/IR/LegacyPassManager.h"
#include "llvm/IR/Module.h"
#include "llvm/Option/Option.h"
#include "llvm/Support/FileSystem.h"
#include "llvm/Support/ManagedStatic.h"
+#include "llvm/Support/TargetRegistry.h"
+#include "llvm/Support/TargetSelect.h"
+#include "llvm/Target/TargetMachine.h"
+#include "llvm/Target/TargetOptions.h"
#include <memory>

#include "clang.h"
@@ -105,12 +110,52 @@ getModuleFromSource(llvm::opt::ArgStringList CFlags, StringRef Path)
return getModuleFromSource(std::move(CFlags), Path, VFS);
}

+std::unique_ptr<llvm::SmallVectorImpl<char>>
+getBPFObjectFromModule(llvm::Module *Module)
+{
+ using namespace llvm;
+
+ std::string TargetTriple("bpf-pc-linux");
+ std::string Error;
+ const Target* Target = TargetRegistry::lookupTarget(TargetTriple, Error);
+ if (!Target) {
+ llvm::errs() << Error;
+ return std::unique_ptr<llvm::SmallVectorImpl<char>>(nullptr);
+ }
+
+ llvm::TargetOptions Opt;
+ TargetMachine *TargetMachine =
+ Target->createTargetMachine(TargetTriple,
+ "generic", "",
+ Opt, Reloc::Static);
+
+ Module->setDataLayout(TargetMachine->createDataLayout());
+ Module->setTargetTriple(TargetTriple);
+
+ std::unique_ptr<SmallVectorImpl<char>> Buffer(new SmallVector<char, 0>());
+ raw_svector_ostream ostream(*Buffer);
+
+ legacy::PassManager PM;
+ if (TargetMachine->addPassesToEmitFile(PM, ostream,
+ TargetMachine::CGFT_ObjectFile)) {
+ llvm::errs() << "TargetMachine can't emit a file of this type\n";
+ return std::unique_ptr<llvm::SmallVectorImpl<char>>(nullptr);;
+ }
+ PM.run(*Module);
+
+ return std::move(Buffer);
+}
+
}

extern "C" {
void perf_clang__init(void)
{
perf::LLVMCtx.reset(new llvm::LLVMContext());
+ LLVMInitializeBPFTargetInfo();
+ LLVMInitializeBPFTarget();
+ LLVMInitializeBPFTargetMC();
+ LLVMInitializeBPFAsmPrinter();
}

void perf_clang__cleanup(void)
diff --git a/tools/perf/util/c++/clang.h b/tools/perf/util/c++/clang.h
index b4fc2a9..dd8b042 100644
--- a/tools/perf/util/c++/clang.h
+++ b/tools/perf/util/c++/clang.h
@@ -19,5 +19,8 @@ std::unique_ptr<Module>
getModuleFromSource(opt::ArgStringList CFlags,
StringRef Path);

+std::unique_ptr<llvm::SmallVectorImpl<char>>
+getBPFObjectFromModule(llvm::Module *Module);
+
}
#endif
--
2.10.1

2016-11-15 04:08:16

by Wang Nan

[permalink] [raw]
Subject: [PATCH 03/34] tools perf: Add missing struct defeinition in probe_event.h

Commit 0b3c2264ae30 ("perf symbols: Fix kallsyms perf test on ppc64le")
referes struct symbol in probe_event.h, but forget include its definition.
Gcc will complain for it.

Signed-off-by: Wang Nan <[email protected]>
Cc: Naveen N. Rao <[email protected]>
Cc: Arnaldo Carvalho de Melo <[email protected]>
---
tools/perf/util/probe-event.h | 2 ++
1 file changed, 2 insertions(+)

diff --git a/tools/perf/util/probe-event.h b/tools/perf/util/probe-event.h
index 8091d15..5d4e940 100644
--- a/tools/perf/util/probe-event.h
+++ b/tools/perf/util/probe-event.h
@@ -18,6 +18,8 @@ struct probe_conf {
extern struct probe_conf probe_conf;
extern bool probe_event_dry_run;

+struct symbol;
+
/* kprobe-tracer and uprobe-tracer tracing point */
struct probe_trace_point {
char *realname; /* function real name (if needed) */
--
2.10.1

2016-11-15 04:13:29

by Wang Nan

[permalink] [raw]
Subject: [PATCH 19/34] perf clang: Compile BPF script use builtin clang support

After this patch, perf utilizes builtin clang support to build BPF
script, no longer depend on external clang.

Test:

$ type clang
-bash: type: clang: not found
$ cat ~/.perfconfig
$ echo '#define LINUX_VERSION_CODE 0x040700' > ./test.c
$ cat ./tools/perf/tests/bpf-script-example.c >> ./test.c
$ ./perf record -v --dry-run -e ./test.c 2>&1 | grep builtin
bpf: builtin compiling successful

Can't pass cflags so unable to include kernel headers now. Will be fixed
by following commits.

Signed-off-by: Wang Nan <[email protected]>
Cc: Alexei Starovoitov <[email protected]>
Cc: He Kuang <[email protected]>
Cc: Jiri Olsa <[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/bpf-loader.c | 15 +++++++++++----
tools/perf/util/c++/clang-c.h | 26 ++++++++++++++++++++++++++
tools/perf/util/c++/clang.cpp | 29 +++++++++++++++++++++++++++++
3 files changed, 66 insertions(+), 4 deletions(-)

diff --git a/tools/perf/util/bpf-loader.c b/tools/perf/util/bpf-loader.c
index cf16b941..a0ea334f 100644
--- a/tools/perf/util/bpf-loader.c
+++ b/tools/perf/util/bpf-loader.c
@@ -14,11 +14,11 @@
#include "debug.h"
#include "bpf-loader.h"
#include "bpf-prologue.h"
-#include "llvm-utils.h"
#include "probe-event.h"
#include "probe-finder.h" // for MAX_PROBES
#include "parse-events.h"
#include "llvm-utils.h"
+#include "c++/clang-c.h"

#define DEFINE_PRINT_FN(name, level) \
static int libbpf_##name(const char *fmt, ...) \
@@ -86,9 +86,16 @@ struct bpf_object *bpf__prepare_load(const char *filename, bool source)
void *obj_buf;
size_t obj_buf_sz;

- err = llvm__compile_bpf(filename, &obj_buf, &obj_buf_sz);
- if (err)
- return ERR_PTR(-BPF_LOADER_ERRNO__COMPILE);
+ perf_clang__init();
+ err = perf_clang__compile_bpf(filename, &obj_buf, &obj_buf_sz);
+ perf_clang__cleanup();
+ if (err) {
+ pr_warning("bpf: builtin compiling failed: %d, try external compiler\n", err);
+ err = llvm__compile_bpf(filename, &obj_buf, &obj_buf_sz);
+ if (err)
+ return ERR_PTR(-BPF_LOADER_ERRNO__COMPILE);
+ } else
+ pr_debug("bpf: builtin compiling successful\n");
obj = bpf_object__open_buffer(obj_buf, obj_buf_sz, filename);

if (!IS_ERR(obj) && llvm_param.dump_obj)
diff --git a/tools/perf/util/c++/clang-c.h b/tools/perf/util/c++/clang-c.h
index 22b3936..0eadd79 100644
--- a/tools/perf/util/c++/clang-c.h
+++ b/tools/perf/util/c++/clang-c.h
@@ -1,16 +1,42 @@
#ifndef PERF_UTIL_CLANG_C_H
#define PERF_UTIL_CLANG_C_H

+#include <stddef.h> /* for size_t */
+#include <util-cxx.h> /* for __maybe_unused */
+
#ifdef __cplusplus
extern "C" {
#endif

+#ifdef HAVE_LIBCLANGLLVM_SUPPORT
extern void perf_clang__init(void);
extern void perf_clang__cleanup(void);

extern int test__clang_to_IR(void);
extern int test__clang_to_obj(void);

+extern int perf_clang__compile_bpf(const char *filename,
+ void **p_obj_buf,
+ size_t *p_obj_buf_sz);
+#else
+
+
+static inline void perf_clang__init(void) { }
+static inline void perf_clang__cleanup(void) { }
+
+static inline int test__clang_to_IR(void) { return -1; }
+static inline int test__clang_to_obj(void) { return -1;}
+
+static inline int
+perf_clang__compile_bpf(const char *filename __maybe_unused,
+ void **p_obj_buf __maybe_unused,
+ size_t *p_obj_buf_sz __maybe_unused)
+{
+ return -ENOTSUP;
+}
+
+#endif
+
#ifdef __cplusplus
}
#endif
diff --git a/tools/perf/util/c++/clang.cpp b/tools/perf/util/c++/clang.cpp
index 2a1a75d..1e97415 100644
--- a/tools/perf/util/c++/clang.cpp
+++ b/tools/perf/util/c++/clang.cpp
@@ -163,4 +163,33 @@ void perf_clang__cleanup(void)
perf::LLVMCtx.reset(nullptr);
llvm::llvm_shutdown();
}
+
+int perf_clang__compile_bpf(const char *filename,
+ void **p_obj_buf,
+ size_t *p_obj_buf_sz)
+{
+ using namespace perf;
+
+ if (!p_obj_buf || !p_obj_buf_sz)
+ return -EINVAL;
+
+ llvm::opt::ArgStringList CFlags;
+ auto M = getModuleFromSource(std::move(CFlags), filename);
+ if (!M)
+ return -EINVAL;
+ auto O = getBPFObjectFromModule(&*M);
+ if (!O)
+ return -EINVAL;
+
+ size_t size = O->size_in_bytes();
+ void *buffer;
+
+ buffer = malloc(size);
+ if (!buffer)
+ return -ENOMEM;
+ memcpy(buffer, O->data(), size);
+ *p_obj_buf = buffer;
+ *p_obj_buf_sz = size;
+ return 0;
+}
}
--
2.10.1

2016-11-15 04:13:31

by Wang Nan

[permalink] [raw]
Subject: [PATCH 14/34] perf clang: Add builtin clang support ant test case

Add basic clang support in clang.cpp and test__clang() testcase. The
first testcase checks if builtin clang is able to generate LLVM IR.

tests/clang.c is a proxy. Real testcase resides in
utils/c++/clang-test.cpp in c++ and exports C interface to perf test
subsystem.

Test result:

$ perf test -v clang
51: Test builtin clang support :
51.1: Test builtin clang compile C source to IR :
--- start ---
test child forked, pid 13215
test child finished with 0
---- end ----
Test builtin clang support subtest 0: Ok

Signed-off-by: Wang Nan <[email protected]>
Cc: Arnaldo Carvalho de Melo <[email protected]>
Cc: Alexei Starovoitov <[email protected]>
Cc: He Kuang <[email protected]>
Cc: Jiri Olsa <[email protected]>
Cc: Zefan Li <[email protected]>
Cc: [email protected]
---
tools/perf/tests/Build | 1 +
tools/perf/tests/builtin-test.c | 9 ++++
tools/perf/tests/clang.c | 42 +++++++++++++++++
tools/perf/tests/tests.h | 3 ++
tools/perf/util/Build | 2 +
tools/perf/util/c++/Build | 2 +
tools/perf/util/c++/clang-c.h | 16 +++++++
tools/perf/util/c++/clang-test.cpp | 31 ++++++++++++
tools/perf/util/c++/clang.cpp | 96 ++++++++++++++++++++++++++++++++++++++
tools/perf/util/c++/clang.h | 16 +++++++
10 files changed, 218 insertions(+)
create mode 100644 tools/perf/tests/clang.c
create mode 100644 tools/perf/util/c++/Build
create mode 100644 tools/perf/util/c++/clang-c.h
create mode 100644 tools/perf/util/c++/clang-test.cpp
create mode 100644 tools/perf/util/c++/clang.cpp
create mode 100644 tools/perf/util/c++/clang.h

diff --git a/tools/perf/tests/Build b/tools/perf/tests/Build
index af3ec94..6676c2d 100644
--- a/tools/perf/tests/Build
+++ b/tools/perf/tests/Build
@@ -43,6 +43,7 @@ perf-y += sdt.o
perf-y += is_printable_array.o
perf-y += bitmap.o
perf-y += perf-hooks.o
+perf-y += clang.o

$(OUTPUT)tests/llvm-src-base.c: tests/bpf-script-example.c tests/Build
$(call rule_mkdir)
diff --git a/tools/perf/tests/builtin-test.c b/tools/perf/tests/builtin-test.c
index dab83f7..33aaa52 100644
--- a/tools/perf/tests/builtin-test.c
+++ b/tools/perf/tests/builtin-test.c
@@ -234,6 +234,15 @@ static struct test generic_tests[] = {
.func = test__perf_hooks,
},
{
+ .desc = "Test builtin clang support",
+ .func = test__clang,
+ .subtest = {
+ .skip_if_fail = true,
+ .get_nr = test__clang_subtest_get_nr,
+ .get_desc = test__clang_subtest_get_desc,
+ }
+ },
+ {
.func = NULL,
},
};
diff --git a/tools/perf/tests/clang.c b/tools/perf/tests/clang.c
new file mode 100644
index 0000000..57ee160
--- /dev/null
+++ b/tools/perf/tests/clang.c
@@ -0,0 +1,42 @@
+#include "tests.h"
+#include "debug.h"
+#include "util.h"
+#include "c++/clang-c.h"
+
+static struct {
+ int (*func)(void);
+ const char *desc;
+} clang_testcase_table[] = {
+#ifdef HAVE_LIBCLANGLLVM_SUPPORT
+ {
+ .func = test__clang_to_IR,
+ .desc = "Test builtin clang compile C source to IR",
+ },
+#endif
+};
+
+int test__clang_subtest_get_nr(void)
+{
+ return (int)ARRAY_SIZE(clang_testcase_table);
+}
+
+const char *test__clang_subtest_get_desc(int i)
+{
+ if (i < 0 || i >= (int)ARRAY_SIZE(clang_testcase_table))
+ return NULL;
+ return clang_testcase_table[i].desc;
+}
+
+#ifndef HAVE_LIBCLANGLLVM_SUPPORT
+int test__clang(int i __maybe_unused)
+{
+ return TEST_SKIP;
+}
+#else
+int test__clang(int i __maybe_unused)
+{
+ if (i < 0 || i >= (int)ARRAY_SIZE(clang_testcase_table))
+ return TEST_FAIL;
+ return clang_testcase_table[i].func();
+}
+#endif
diff --git a/tools/perf/tests/tests.h b/tools/perf/tests/tests.h
index 3a1f98f..0d7b251 100644
--- a/tools/perf/tests/tests.h
+++ b/tools/perf/tests/tests.h
@@ -92,6 +92,9 @@ int test__sdt_event(int subtest);
int test__is_printable_array(int subtest);
int test__bitmap_print(int subtest);
int test__perf_hooks(int subtest);
+int test__clang(int subtest);
+const char *test__clang_subtest_get_desc(int subtest);
+int test__clang_subtest_get_nr(void);

#if defined(__arm__) || defined(__aarch64__)
#ifdef HAVE_DWARF_UNWIND_SUPPORT
diff --git a/tools/perf/util/Build b/tools/perf/util/Build
index b2a47aa..743a889 100644
--- a/tools/perf/util/Build
+++ b/tools/perf/util/Build
@@ -125,6 +125,8 @@ endif

libperf-y += perf-hooks.o

+libperf-$(CONFIG_CXX) += c++/
+
CFLAGS_config.o += -DETC_PERFCONFIG="BUILD_STR($(ETC_PERFCONFIG_SQ))"
# avoid compiler warnings in 32-bit mode
CFLAGS_genelf_debug.o += -Wno-packed
diff --git a/tools/perf/util/c++/Build b/tools/perf/util/c++/Build
new file mode 100644
index 0000000..988fef1
--- /dev/null
+++ b/tools/perf/util/c++/Build
@@ -0,0 +1,2 @@
+libperf-$(CONFIG_CLANGLLVM) += clang.o
+libperf-$(CONFIG_CLANGLLVM) += clang-test.o
diff --git a/tools/perf/util/c++/clang-c.h b/tools/perf/util/c++/clang-c.h
new file mode 100644
index 0000000..dcde4b5
--- /dev/null
+++ b/tools/perf/util/c++/clang-c.h
@@ -0,0 +1,16 @@
+#ifndef PERF_UTIL_CLANG_C_H
+#define PERF_UTIL_CLANG_C_H
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+extern void perf_clang__init(void);
+extern void perf_clang__cleanup(void);
+
+extern int test__clang_to_IR(void);
+
+#ifdef __cplusplus
+}
+#endif
+#endif
diff --git a/tools/perf/util/c++/clang-test.cpp b/tools/perf/util/c++/clang-test.cpp
new file mode 100644
index 0000000..3da6bfa
--- /dev/null
+++ b/tools/perf/util/c++/clang-test.cpp
@@ -0,0 +1,31 @@
+#include "clang.h"
+#include "clang-c.h"
+#include "llvm/IR/Function.h"
+#include "llvm/IR/LLVMContext.h"
+
+class perf_clang_scope {
+public:
+ explicit perf_clang_scope() {perf_clang__init();}
+ ~perf_clang_scope() {perf_clang__cleanup();}
+};
+
+extern "C" {
+
+int test__clang_to_IR(void)
+{
+ perf_clang_scope _scope;
+
+ std::unique_ptr<llvm::Module> M =
+ perf::getModuleFromSource("perf-test.c",
+ "int myfunc(void) {return 1;}");
+
+ if (!M)
+ return -1;
+
+ for (llvm::Function& F : *M)
+ if (F.getName() == "myfunc")
+ return 0;
+ return -1;
+}
+
+}
diff --git a/tools/perf/util/c++/clang.cpp b/tools/perf/util/c++/clang.cpp
new file mode 100644
index 0000000..c17b117
--- /dev/null
+++ b/tools/perf/util/c++/clang.cpp
@@ -0,0 +1,96 @@
+/*
+ * llvm C frontend for perf. Support dynamically compile C file
+ *
+ * Inspired by clang example code:
+ * http://llvm.org/svn/llvm-project/cfe/trunk/examples/clang-interpreter/main.cpp
+ *
+ * Copyright (C) 2016 Wang Nan <[email protected]>
+ * Copyright (C) 2016 Huawei Inc.
+ */
+
+#include "clang/CodeGen/CodeGenAction.h"
+#include "clang/Frontend/CompilerInvocation.h"
+#include "clang/Frontend/CompilerInstance.h"
+#include "clang/Frontend/TextDiagnosticPrinter.h"
+#include "clang/Tooling/Tooling.h"
+#include "llvm/IR/Module.h"
+#include "llvm/Option/Option.h"
+#include "llvm/Support/ManagedStatic.h"
+#include <memory>
+
+#include "clang.h"
+#include "clang-c.h"
+
+namespace perf {
+
+static std::unique_ptr<llvm::LLVMContext> LLVMCtx;
+
+using namespace clang;
+
+static vfs::InMemoryFileSystem *
+buildVFS(StringRef& Name, StringRef& Content)
+{
+ vfs::InMemoryFileSystem *VFS = new vfs::InMemoryFileSystem(true);
+ VFS->addFile(Twine(Name), 0, llvm::MemoryBuffer::getMemBuffer(Content));
+ return VFS;
+}
+
+static CompilerInvocation *
+createCompilerInvocation(StringRef& Path, DiagnosticsEngine& Diags)
+{
+ llvm::opt::ArgStringList CCArgs {
+ "-cc1",
+ "-triple", "bpf-pc-linux",
+ "-fsyntax-only",
+ "-ferror-limit", "19",
+ "-fmessage-length", "127",
+ "-O2",
+ "-nostdsysteminc",
+ "-nobuiltininc",
+ "-vectorize-loops",
+ "-vectorize-slp",
+ "-Wno-unused-value",
+ "-Wno-pointer-sign",
+ "-x", "c"};
+ CompilerInvocation *CI = tooling::newInvocation(&Diags, CCArgs);
+
+ FrontendOptions& Opts = CI->getFrontendOpts();
+ Opts.Inputs.clear();
+ Opts.Inputs.emplace_back(Path, IK_C);
+ return CI;
+}
+
+std::unique_ptr<llvm::Module>
+getModuleFromSource(StringRef Name, StringRef Content)
+{
+ CompilerInstance Clang;
+ Clang.createDiagnostics();
+
+ IntrusiveRefCntPtr<vfs::FileSystem> VFS = buildVFS(Name, Content);
+ Clang.setVirtualFileSystem(&*VFS);
+
+ IntrusiveRefCntPtr<CompilerInvocation> CI =
+ createCompilerInvocation(Name, Clang.getDiagnostics());
+ Clang.setInvocation(&*CI);
+
+ std::unique_ptr<CodeGenAction> Act(new EmitLLVMOnlyAction(&*LLVMCtx));
+ if (!Clang.ExecuteAction(*Act))
+ return std::unique_ptr<llvm::Module>(nullptr);
+
+ return Act->takeModule();
+}
+
+}
+
+extern "C" {
+void perf_clang__init(void)
+{
+ perf::LLVMCtx.reset(new llvm::LLVMContext());
+}
+
+void perf_clang__cleanup(void)
+{
+ perf::LLVMCtx.reset(nullptr);
+ llvm::llvm_shutdown();
+}
+}
diff --git a/tools/perf/util/c++/clang.h b/tools/perf/util/c++/clang.h
new file mode 100644
index 0000000..f64483b
--- /dev/null
+++ b/tools/perf/util/c++/clang.h
@@ -0,0 +1,16 @@
+#ifndef PERF_UTIL_CLANG_H
+#define PERF_UTIL_CLANG_H
+
+#include "llvm/ADT/StringRef.h"
+#include "llvm/IR/LLVMContext.h"
+#include "llvm/IR/Module.h"
+#include <memory>
+namespace perf {
+
+using namespace llvm;
+
+std::unique_ptr<Module>
+getModuleFromSource(StringRef Name, StringRef Content);
+
+}
+#endif
--
2.10.1

2016-11-15 04:14:19

by Wang Nan

[permalink] [raw]
Subject: [PATCH 08/34] perf tools: Introduce perf hooks

Perf hooks allow hooking user code at perf events. They can be used for
manipulation of BPF maps, taking snapshot and reporting results. In this
patch two perf hook points are introduced: record_start and record_end.

To avoid buggy user actions, a SIGSEGV signal handler is introduced into
'perf record'. It turns off perf hook if it causes a segfault and report
an error to help debugging.

A test case for perf hook is introduced.

Test result:
$ ./buildperf/perf test -v hook
50: Test perf hooks :
--- start ---
test child forked, pid 10311
SIGSEGV is observed as expected, try to recover.
Fatal error (SEGFAULT) in perf hook 'test'
test child finished with 0
---- end ----
Test perf hooks: Ok

Signed-off-by: Wang Nan <[email protected]>
Cc: Arnaldo Carvalho de Melo <[email protected]>
Cc: Alexei Starovoitov <[email protected]>
Cc: He Kuang <[email protected]>
Cc: Jiri Olsa <[email protected]>
---
tools/perf/builtin-record.c | 11 +++++
tools/perf/tests/Build | 1 +
tools/perf/tests/builtin-test.c | 4 ++
tools/perf/tests/perf-hooks.c | 44 ++++++++++++++++++++
tools/perf/tests/tests.h | 1 +
tools/perf/util/Build | 2 +
tools/perf/util/perf-hooks-list.h | 3 ++
tools/perf/util/perf-hooks.c | 84 +++++++++++++++++++++++++++++++++++++++
tools/perf/util/perf-hooks.h | 37 +++++++++++++++++
9 files changed, 187 insertions(+)
create mode 100644 tools/perf/tests/perf-hooks.c
create mode 100644 tools/perf/util/perf-hooks-list.h
create mode 100644 tools/perf/util/perf-hooks.c
create mode 100644 tools/perf/util/perf-hooks.h

diff --git a/tools/perf/builtin-record.c b/tools/perf/builtin-record.c
index 67d2a90..fa26865 100644
--- a/tools/perf/builtin-record.c
+++ b/tools/perf/builtin-record.c
@@ -37,6 +37,7 @@
#include "util/llvm-utils.h"
#include "util/bpf-loader.h"
#include "util/trigger.h"
+#include "util/perf-hooks.h"
#include "asm/bug.h"

#include <unistd.h>
@@ -206,6 +207,12 @@ static void sig_handler(int sig)
done = 1;
}

+static void sigsegv_handler(int sig)
+{
+ perf_hooks__recover();
+ sighandler_dump_stack(sig);
+}
+
static void record__sig_exit(void)
{
if (signr == -1)
@@ -833,6 +840,7 @@ static int __cmd_record(struct record *rec, int argc, const char **argv)
signal(SIGCHLD, sig_handler);
signal(SIGINT, sig_handler);
signal(SIGTERM, sig_handler);
+ signal(SIGSEGV, sigsegv_handler);

if (rec->opts.auxtrace_snapshot_mode || rec->switch_output) {
signal(SIGUSR2, snapshot_sig_handler);
@@ -970,6 +978,7 @@ static int __cmd_record(struct record *rec, int argc, const char **argv)

trigger_ready(&auxtrace_snapshot_trigger);
trigger_ready(&switch_output_trigger);
+ perf_hooks__invoke_record_start();
for (;;) {
unsigned long long hits = rec->samples;

@@ -1114,6 +1123,8 @@ static int __cmd_record(struct record *rec, int argc, const char **argv)
}
}

+ perf_hooks__invoke_record_end();
+
if (!err && !quiet) {
char samples[128];
const char *postfix = rec->timestamp_filename ?
diff --git a/tools/perf/tests/Build b/tools/perf/tests/Build
index 8a4ce49..af3ec94 100644
--- a/tools/perf/tests/Build
+++ b/tools/perf/tests/Build
@@ -42,6 +42,7 @@ perf-y += backward-ring-buffer.o
perf-y += sdt.o
perf-y += is_printable_array.o
perf-y += bitmap.o
+perf-y += perf-hooks.o

$(OUTPUT)tests/llvm-src-base.c: tests/bpf-script-example.c tests/Build
$(call rule_mkdir)
diff --git a/tools/perf/tests/builtin-test.c b/tools/perf/tests/builtin-test.c
index 778668a..dab83f7 100644
--- a/tools/perf/tests/builtin-test.c
+++ b/tools/perf/tests/builtin-test.c
@@ -230,6 +230,10 @@ static struct test generic_tests[] = {
.func = test__bitmap_print,
},
{
+ .desc = "Test perf hooks",
+ .func = test__perf_hooks,
+ },
+ {
.func = NULL,
},
};
diff --git a/tools/perf/tests/perf-hooks.c b/tools/perf/tests/perf-hooks.c
new file mode 100644
index 0000000..9338cb2
--- /dev/null
+++ b/tools/perf/tests/perf-hooks.c
@@ -0,0 +1,44 @@
+#include <signal.h>
+#include <stdlib.h>
+
+#include "tests.h"
+#include "debug.h"
+#include "util.h"
+#include "perf-hooks.h"
+
+static void sigsegv_handler(int sig __maybe_unused)
+{
+ pr_debug("SIGSEGV is observed as expected, try to recover.\n");
+ perf_hooks__recover();
+ signal(SIGSEGV, SIG_DFL);
+ raise(SIGSEGV);
+ exit(-1);
+}
+
+static int hook_flags;
+
+static void the_hook(void)
+{
+ int *p = NULL;
+
+ hook_flags = 1234;
+
+ /* Generate a segfault, test perf_hooks__recover */
+ *p = 0;
+}
+
+int test__perf_hooks(int subtest __maybe_unused)
+{
+ signal(SIGSEGV, sigsegv_handler);
+ perf_hooks__set_hook("test", the_hook);
+ perf_hooks__invoke_test();
+
+ /* hook is triggered? */
+ if (hook_flags != 1234)
+ return TEST_FAIL;
+
+ /* the buggy hook is removed? */
+ if (perf_hooks__get_hook("test"))
+ return TEST_FAIL;
+ return TEST_OK;
+}
diff --git a/tools/perf/tests/tests.h b/tools/perf/tests/tests.h
index 7c196c5..3a1f98f 100644
--- a/tools/perf/tests/tests.h
+++ b/tools/perf/tests/tests.h
@@ -91,6 +91,7 @@ int test__cpu_map_print(int subtest);
int test__sdt_event(int subtest);
int test__is_printable_array(int subtest);
int test__bitmap_print(int subtest);
+int test__perf_hooks(int subtest);

#if defined(__arm__) || defined(__aarch64__)
#ifdef HAVE_DWARF_UNWIND_SUPPORT
diff --git a/tools/perf/util/Build b/tools/perf/util/Build
index 1dc67ef..b2a47aa 100644
--- a/tools/perf/util/Build
+++ b/tools/perf/util/Build
@@ -123,6 +123,8 @@ libperf-$(CONFIG_LIBELF) += genelf.o
libperf-$(CONFIG_DWARF) += genelf_debug.o
endif

+libperf-y += perf-hooks.o
+
CFLAGS_config.o += -DETC_PERFCONFIG="BUILD_STR($(ETC_PERFCONFIG_SQ))"
# avoid compiler warnings in 32-bit mode
CFLAGS_genelf_debug.o += -Wno-packed
diff --git a/tools/perf/util/perf-hooks-list.h b/tools/perf/util/perf-hooks-list.h
new file mode 100644
index 0000000..2867c07
--- /dev/null
+++ b/tools/perf/util/perf-hooks-list.h
@@ -0,0 +1,3 @@
+PERF_HOOK(record_start)
+PERF_HOOK(record_end)
+PERF_HOOK(test)
diff --git a/tools/perf/util/perf-hooks.c b/tools/perf/util/perf-hooks.c
new file mode 100644
index 0000000..4ce88e3
--- /dev/null
+++ b/tools/perf/util/perf-hooks.c
@@ -0,0 +1,84 @@
+/*
+ * perf_hooks.c
+ *
+ * Copyright (C) 2016 Wang Nan <[email protected]>
+ * Copyright (C) 2016 Huawei Inc.
+ */
+
+#include <errno.h>
+#include <stdlib.h>
+#include <setjmp.h>
+#include <linux/err.h>
+#include "util/util.h"
+#include "util/debug.h"
+#include "util/perf-hooks.h"
+
+static sigjmp_buf jmpbuf;
+static const struct perf_hook_desc *current_perf_hook;
+
+void perf_hooks__invoke(const struct perf_hook_desc *desc)
+{
+ if (!(desc && desc->p_hook_func && *desc->p_hook_func))
+ return;
+
+ if (sigsetjmp(jmpbuf, 1)) {
+ pr_warning("Fatal error (SEGFAULT) in perf hook '%s'\n",
+ desc->hook_name);
+ *(current_perf_hook->p_hook_func) = NULL;
+ } else {
+ current_perf_hook = desc;
+ (**desc->p_hook_func)();
+ }
+ current_perf_hook = NULL;
+}
+
+void perf_hooks__recover(void)
+{
+ if (current_perf_hook)
+ siglongjmp(jmpbuf, 1);
+}
+
+#define PERF_HOOK(name) \
+perf_hook_func_t __perf_hook_func_##name = NULL; \
+struct perf_hook_desc __perf_hook_desc_##name = \
+ {.hook_name = #name, .p_hook_func = &__perf_hook_func_##name};
+#include "perf-hooks-list.h"
+#undef PERF_HOOK
+
+#define PERF_HOOK(name) \
+ &__perf_hook_desc_##name,
+
+static struct perf_hook_desc *perf_hooks[] = {
+#include "perf-hooks-list.h"
+};
+#undef PERF_HOOK
+
+int perf_hooks__set_hook(const char *hook_name,
+ perf_hook_func_t hook_func)
+{
+ unsigned int i;
+
+ for (i = 0; i < ARRAY_SIZE(perf_hooks); i++) {
+ if (strcmp(hook_name, perf_hooks[i]->hook_name) != 0)
+ continue;
+
+ if (*(perf_hooks[i]->p_hook_func))
+ pr_warning("Overwrite existing hook: %s\n", hook_name);
+ *(perf_hooks[i]->p_hook_func) = hook_func;
+ return 0;
+ }
+ return -ENOENT;
+}
+
+perf_hook_func_t perf_hooks__get_hook(const char *hook_name)
+{
+ unsigned int i;
+
+ for (i = 0; i < ARRAY_SIZE(perf_hooks); i++) {
+ if (strcmp(hook_name, perf_hooks[i]->hook_name) != 0)
+ continue;
+
+ return *(perf_hooks[i]->p_hook_func);
+ }
+ return ERR_PTR(-ENOENT);
+}
diff --git a/tools/perf/util/perf-hooks.h b/tools/perf/util/perf-hooks.h
new file mode 100644
index 0000000..1d482b2
--- /dev/null
+++ b/tools/perf/util/perf-hooks.h
@@ -0,0 +1,37 @@
+#ifndef PERF_UTIL_PERF_HOOKS_H
+#define PERF_UTIL_PERF_HOOKS_H
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+typedef void (*perf_hook_func_t)(void);
+struct perf_hook_desc {
+ const char * const hook_name;
+ perf_hook_func_t * const p_hook_func;
+};
+
+extern void perf_hooks__invoke(const struct perf_hook_desc *);
+extern void perf_hooks__recover(void);
+
+#define PERF_HOOK(name) \
+extern struct perf_hook_desc __perf_hook_desc_##name; \
+static inline void perf_hooks__invoke_##name(void) \
+{ \
+ perf_hooks__invoke(&__perf_hook_desc_##name); \
+}
+
+#include "perf-hooks-list.h"
+#undef PERF_HOOK
+
+extern int
+perf_hooks__set_hook(const char *hook_name,
+ perf_hook_func_t hook_func);
+
+extern perf_hook_func_t
+perf_hooks__get_hook(const char *hook_name);
+
+#ifdef __cplusplus
+}
+#endif
+#endif
--
2.10.1

2016-11-15 04:14:24

by Wang Nan

[permalink] [raw]
Subject: [PATCH 13/34] perf build: Add clang and llvm compile and linking support

Add necessary c++ flags and link libraries to support builtin clang and
LLVM. Add all llvm and clang libraries, so don't need to worry about
clang changes its libraries setting. However, linking perf would take
much longer than usual.

Signed-off-by: Wang Nan <[email protected]>
Cc: Alexei Starovoitov <[email protected]>
Cc: He Kuang <[email protected]>
Cc: Jiri Olsa <[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/Makefile.config | 35 +++++++++++++++++++++++++++++++++++
tools/perf/Makefile.perf | 23 ++++++++++++++++++++++-
tools/perf/tests/make | 2 ++
3 files changed, 59 insertions(+), 1 deletion(-)

diff --git a/tools/perf/Makefile.config b/tools/perf/Makefile.config
index 8a493d4..b7c9c80 100644
--- a/tools/perf/Makefile.config
+++ b/tools/perf/Makefile.config
@@ -136,6 +136,7 @@ endif
# Treat warnings as errors unless directed not to
ifneq ($(WERROR),0)
CFLAGS += -Werror
+ CXXFLAGS += -Werror
endif

ifndef DEBUG
@@ -182,6 +183,13 @@ CFLAGS += -Wall
CFLAGS += -Wextra
CFLAGS += -std=gnu99

+CXXFLAGS += -std=gnu++11 -fno-exceptions -fno-rtti
+CXXFLAGS += -Wall
+CXXFLAGS += -fno-omit-frame-pointer
+CXXFLAGS += -ggdb3
+CXXFLAGS += -funwind-tables
+CXXFLAGS += -Wno-strict-aliasing
+
# Enforce a non-executable stack, as we may regress (again) in the future by
# adding assembler files missing the .GNU-stack linker note.
LDFLAGS += -Wl,-z,noexecstack
@@ -783,6 +791,33 @@ ifndef NO_JVMTI
endif
endif

+USE_CXX = 0
+USE_CLANGLLVM = 0
+ifdef LIBCLANGLLVM
+ $(call feature_check,cxx)
+ ifneq ($(feature-cxx), 1)
+ msg := $(warning No g++ found, disable clang and llvm support. Please install g++)
+ else
+ $(call feature_check,llvm)
+ ifneq ($(feature-llvm), 1)
+ msg := $(warning No libLLVM found, disable clang and llvm support. Please install llvm-dev)
+ else
+ $(call feature_check,clang)
+ ifneq ($(feature-clang), 1)
+ msg := $(warning No libclang found, disable clang and llvm support. Please install libclang-dev)
+ else
+ CFLAGS += -DHAVE_LIBCLANGLLVM_SUPPORT
+ CXXFLAGS += -DHAVE_LIBCLANGLLVM_SUPPORT -I$(shell $(LLVM_CONFIG) --includedir)
+ $(call detected,CONFIG_CXX)
+ $(call detected,CONFIG_CLANGLLVM)
+ USE_CXX = 1
+ USE_LLVM = 1
+ USE_CLANG = 1
+ endif
+ endif
+ endif
+endif
+
# Among the variables below, these:
# perfexecdir
# template_dir
diff --git a/tools/perf/Makefile.perf b/tools/perf/Makefile.perf
index 3cb1df4..dfb20dd 100644
--- a/tools/perf/Makefile.perf
+++ b/tools/perf/Makefile.perf
@@ -88,6 +88,10 @@ include ../scripts/utilities.mak
# and bypass the feature detection
#
# Define NO_JVMTI if you do not want jvmti agent built
+#
+# Define LIBCLANGLLVM if you DO want builtin clang and llvm support.
+# When selected, pass LLVM_CONFIG=/path/to/llvm-config to `make' if
+# llvm-config is not in $PATH.

# As per kernel Makefile, avoid funny character set dependencies
unexport LC_ALL
@@ -143,6 +147,7 @@ endef
$(call allow-override,CC,$(CROSS_COMPILE)gcc)
$(call allow-override,AR,$(CROSS_COMPILE)ar)
$(call allow-override,LD,$(CROSS_COMPILE)ld)
+$(call allow-override,CXX,$(CROSS_COMPILE)g++)

LD += $(EXTRA_LDFLAGS)

@@ -151,6 +156,7 @@ HOSTLD ?= ld
HOSTAR ?= ar

PKG_CONFIG = $(CROSS_COMPILE)pkg-config
+LLVM_CONFIG ?= llvm-config

RM = rm -f
LN = ln -f
@@ -338,6 +344,21 @@ endif

LIBS = -Wl,--whole-archive $(PERFLIBS) -Wl,--no-whole-archive -Wl,--start-group $(EXTLIBS) -Wl,--end-group

+ifeq ($(USE_CLANG), 1)
+ CLANGLIBS_LIST = AST Basic CodeGen Driver Frontend Lex Tooling Edit Sema Analysis Parse Serialization
+ LIBCLANG = $(foreach l,$(CLANGLIBS_LIST),$(wildcard $(shell $(LLVM_CONFIG) --libdir)/libclang$(l).a))
+ LIBS += -Wl,--start-group $(LIBCLANG) -Wl,--end-group
+endif
+
+ifeq ($(USE_LLVM), 1)
+ LIBLLVM = $(shell $(LLVM_CONFIG) --libs all) $(shell $(LLVM_CONFIG) --system-libs)
+ LIBS += -L$(shell $(LLVM_CONFIG) --libdir) $(LIBLLVM)
+endif
+
+ifeq ($(USE_CXX), 1)
+ LIBS += -lstdc++
+endif
+
export INSTALL SHELL_PATH

### Build rules
@@ -356,7 +377,7 @@ strip: $(PROGRAMS) $(OUTPUT)perf

PERF_IN := $(OUTPUT)perf-in.o

-export srctree OUTPUT RM CC LD AR CFLAGS V BISON FLEX AWK
+export srctree OUTPUT RM CC CXX LD AR CFLAGS CXXFLAGS V BISON FLEX AWK
export HOSTCC HOSTLD HOSTAR
include $(srctree)/tools/build/Makefile.include

diff --git a/tools/perf/tests/make b/tools/perf/tests/make
index 08ed7f1..aa49b66 100644
--- a/tools/perf/tests/make
+++ b/tools/perf/tests/make
@@ -83,6 +83,7 @@ make_no_libbpf := NO_LIBBPF=1
make_no_libcrypto := NO_LIBCRYPTO=1
make_with_babeltrace:= LIBBABELTRACE=1
make_no_sdt := NO_SDT=1
+make_with_clangllvm := LIBCLANGLLVM=1
make_tags := tags
make_cscope := cscope
make_help := help
@@ -139,6 +140,7 @@ run += make_no_libbionic
run += make_no_auxtrace
run += make_no_libbpf
run += make_with_babeltrace
+run += make_with_clangllvm
run += make_help
run += make_doc
run += make_perf_o
--
2.10.1

2016-11-15 04:14:16

by Wang Nan

[permalink] [raw]
Subject: [PATCH 10/34] perf llvm: Extract helpers in llvm-utils.c

Following commits will use builtin clang to compile BPF script.
llvm__get_kbuild_opts() and llvm__get_nr_cpus() are extracted to help
building '-DKERNEL_VERSION_CODE' and '-D__NR_CPUS__' macros.

Doing object dumping in bpf loader, so futher builtin clang compiling
needn't consider it.

Signed-off-by: Wang Nan <[email protected]>
Cc: Arnaldo Carvalho de Melo <[email protected]>
Cc: Alexei Starovoitov <[email protected]>
Cc: He Kuang <[email protected]>
Cc: Jiri Olsa <[email protected]>
Cc: Zefan Li <[email protected]>
Cc: [email protected]
---
tools/perf/util/bpf-loader.c | 4 +++
tools/perf/util/llvm-utils.c | 76 +++++++++++++++++++++++++++++++++-----------
tools/perf/util/llvm-utils.h | 6 ++++
3 files changed, 68 insertions(+), 18 deletions(-)

diff --git a/tools/perf/util/bpf-loader.c b/tools/perf/util/bpf-loader.c
index a5fd275..cf16b941 100644
--- a/tools/perf/util/bpf-loader.c
+++ b/tools/perf/util/bpf-loader.c
@@ -90,6 +90,10 @@ struct bpf_object *bpf__prepare_load(const char *filename, bool source)
if (err)
return ERR_PTR(-BPF_LOADER_ERRNO__COMPILE);
obj = bpf_object__open_buffer(obj_buf, obj_buf_sz, filename);
+
+ if (!IS_ERR(obj) && llvm_param.dump_obj)
+ llvm__dump_obj(filename, obj_buf, obj_buf_sz);
+
free(obj_buf);
} else
obj = bpf_object__open(filename);
diff --git a/tools/perf/util/llvm-utils.c b/tools/perf/util/llvm-utils.c
index 27b6f303..b23ff44 100644
--- a/tools/perf/util/llvm-utils.c
+++ b/tools/perf/util/llvm-utils.c
@@ -7,6 +7,7 @@
#include <limits.h>
#include <stdio.h>
#include <stdlib.h>
+#include <linux/err.h>
#include "debug.h"
#include "llvm-utils.h"
#include "config.h"
@@ -282,9 +283,10 @@ static const char *kinc_fetch_script =
"rm -rf $TMPDIR\n"
"exit $RET\n";

-static inline void
-get_kbuild_opts(char **kbuild_dir, char **kbuild_include_opts)
+void llvm__get_kbuild_opts(char **kbuild_dir, char **kbuild_include_opts)
{
+ static char *saved_kbuild_dir;
+ static char *saved_kbuild_include_opts;
int err;

if (!kbuild_dir || !kbuild_include_opts)
@@ -293,10 +295,28 @@ get_kbuild_opts(char **kbuild_dir, char **kbuild_include_opts)
*kbuild_dir = NULL;
*kbuild_include_opts = NULL;

+ if (saved_kbuild_dir && saved_kbuild_include_opts &&
+ !IS_ERR(saved_kbuild_dir) && !IS_ERR(saved_kbuild_include_opts)) {
+ *kbuild_dir = strdup(saved_kbuild_dir);
+ *kbuild_include_opts = strdup(saved_kbuild_include_opts);
+
+ if (*kbuild_dir && *kbuild_include_opts)
+ return;
+
+ zfree(kbuild_dir);
+ zfree(kbuild_include_opts);
+ /*
+ * Don't fall through: it may breaks saved_kbuild_dir and
+ * saved_kbuild_include_opts if detect them again when
+ * memory is low.
+ */
+ return;
+ }
+
if (llvm_param.kbuild_dir && !llvm_param.kbuild_dir[0]) {
pr_debug("[llvm.kbuild-dir] is set to \"\" deliberately.\n");
pr_debug("Skip kbuild options detection.\n");
- return;
+ goto errout;
}

err = detect_kbuild_dir(kbuild_dir);
@@ -306,7 +326,7 @@ get_kbuild_opts(char **kbuild_dir, char **kbuild_include_opts)
"Hint:\tSet correct kbuild directory using 'kbuild-dir' option in [llvm]\n"
" \tsection of ~/.perfconfig or set it to \"\" to suppress kbuild\n"
" \tdetection.\n\n");
- return;
+ goto errout;
}

pr_debug("Kernel build dir is set to %s\n", *kbuild_dir);
@@ -325,14 +345,43 @@ get_kbuild_opts(char **kbuild_dir, char **kbuild_include_opts)

free(*kbuild_dir);
*kbuild_dir = NULL;
- return;
+ goto errout;
}

pr_debug("include option is set to %s\n", *kbuild_include_opts);
+
+ saved_kbuild_dir = strdup(*kbuild_dir);
+ saved_kbuild_include_opts = strdup(*kbuild_include_opts);
+
+ if (!saved_kbuild_dir || !saved_kbuild_include_opts) {
+ zfree(&saved_kbuild_dir);
+ zfree(&saved_kbuild_include_opts);
+ }
+ return;
+errout:
+ saved_kbuild_dir = ERR_PTR(-EINVAL);
+ saved_kbuild_include_opts = ERR_PTR(-EINVAL);
}

-static void
-dump_obj(const char *path, void *obj_buf, size_t size)
+int llvm__get_nr_cpus(void)
+{
+ static int nr_cpus_avail = 0;
+ char serr[STRERR_BUFSIZE];
+
+ if (nr_cpus_avail > 0)
+ return nr_cpus_avail;
+
+ 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", str_error_r(errno, serr, sizeof(serr)));
+ nr_cpus_avail = 128;
+ }
+ return nr_cpus_avail;
+}
+
+void llvm__dump_obj(const char *path, void *obj_buf, size_t size)
{
char *obj_path = strdup(path);
FILE *fp;
@@ -406,15 +455,9 @@ int llvm__compile_bpf(const char *path, void **p_obj_buf,
* This is an optional work. Even it fail we can continue our
* work. Needn't to check error return.
*/
- get_kbuild_opts(&kbuild_dir, &kbuild_include_opts);
+ llvm__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", str_error_r(errno, serr, sizeof(serr)));
- nr_cpus_avail = 128;
- }
+ nr_cpus_avail = llvm__get_nr_cpus();
snprintf(nr_cpus_avail_str, sizeof(nr_cpus_avail_str), "%d",
nr_cpus_avail);

@@ -453,9 +496,6 @@ int llvm__compile_bpf(const char *path, void **p_obj_buf,
free(kbuild_dir);
free(kbuild_include_opts);

- if (llvm_param.dump_obj)
- dump_obj(path, obj_buf, obj_buf_sz);
-
if (!p_obj_buf)
free(obj_buf);
else
diff --git a/tools/perf/util/llvm-utils.h b/tools/perf/util/llvm-utils.h
index 9f501ce..c87a2a9 100644
--- a/tools/perf/util/llvm-utils.h
+++ b/tools/perf/util/llvm-utils.h
@@ -50,4 +50,10 @@ int llvm__compile_bpf(const char *path, void **p_obj_buf, size_t *p_obj_buf_sz);

/* This function is for test__llvm() use only */
int llvm__search_clang(void);
+
+/* Following functions are reused by builtin clang support */
+void llvm__get_kbuild_opts(char **kbuild_dir, char **kbuild_include_opts);
+int llvm__get_nr_cpus(void);
+
+void llvm__dump_obj(const char *path, void *obj_buf, size_t size);
#endif
--
2.10.1

2016-11-15 04:14:12

by Wang Nan

[permalink] [raw]
Subject: [PATCH 15/34] perf clang: Use real file system for #include

Utilize clang's OverlayFileSystem facility, allow CompilerInstance to
access real file system.

With this patch '#include' directive can be used.

Add a new getModuleFromSource for real file.

Signed-off-by: Wang Nan <[email protected]>
Cc: Arnaldo Carvalho de Melo <[email protected]>
Cc: Alexei Starovoitov <[email protected]>
Cc: He Kuang <[email protected]>
Cc: Jiri Olsa <[email protected]>
Cc: Zefan Li <[email protected]>
Cc: [email protected]
---
tools/perf/util/c++/clang.cpp | 44 +++++++++++++++++++++++++++++++------------
tools/perf/util/c++/clang.h | 3 +++
2 files changed, 35 insertions(+), 12 deletions(-)

diff --git a/tools/perf/util/c++/clang.cpp b/tools/perf/util/c++/clang.cpp
index c17b117..cf96199 100644
--- a/tools/perf/util/c++/clang.cpp
+++ b/tools/perf/util/c++/clang.cpp
@@ -15,6 +15,7 @@
#include "clang/Tooling/Tooling.h"
#include "llvm/IR/Module.h"
#include "llvm/Option/Option.h"
+#include "llvm/Support/FileSystem.h"
#include "llvm/Support/ManagedStatic.h"
#include <memory>

@@ -27,14 +28,6 @@ static std::unique_ptr<llvm::LLVMContext> LLVMCtx;

using namespace clang;

-static vfs::InMemoryFileSystem *
-buildVFS(StringRef& Name, StringRef& Content)
-{
- vfs::InMemoryFileSystem *VFS = new vfs::InMemoryFileSystem(true);
- VFS->addFile(Twine(Name), 0, llvm::MemoryBuffer::getMemBuffer(Content));
- return VFS;
-}
-
static CompilerInvocation *
createCompilerInvocation(StringRef& Path, DiagnosticsEngine& Diags)
{
@@ -60,17 +53,17 @@ createCompilerInvocation(StringRef& Path, DiagnosticsEngine& Diags)
return CI;
}

-std::unique_ptr<llvm::Module>
-getModuleFromSource(StringRef Name, StringRef Content)
+static std::unique_ptr<llvm::Module>
+getModuleFromSource(StringRef Path,
+ IntrusiveRefCntPtr<vfs::FileSystem> VFS)
{
CompilerInstance Clang;
Clang.createDiagnostics();

- IntrusiveRefCntPtr<vfs::FileSystem> VFS = buildVFS(Name, Content);
Clang.setVirtualFileSystem(&*VFS);

IntrusiveRefCntPtr<CompilerInvocation> CI =
- createCompilerInvocation(Name, Clang.getDiagnostics());
+ createCompilerInvocation(Path, Clang.getDiagnostics());
Clang.setInvocation(&*CI);

std::unique_ptr<CodeGenAction> Act(new EmitLLVMOnlyAction(&*LLVMCtx));
@@ -80,6 +73,33 @@ getModuleFromSource(StringRef Name, StringRef Content)
return Act->takeModule();
}

+std::unique_ptr<llvm::Module>
+getModuleFromSource(StringRef Name, StringRef Content)
+{
+ using namespace vfs;
+
+ llvm::IntrusiveRefCntPtr<OverlayFileSystem> OverlayFS(
+ new OverlayFileSystem(getRealFileSystem()));
+ llvm::IntrusiveRefCntPtr<InMemoryFileSystem> MemFS(
+ new InMemoryFileSystem(true));
+
+ /*
+ * pushOverlay helps setting working dir for MemFS. Must call
+ * before addFile.
+ */
+ OverlayFS->pushOverlay(MemFS);
+ MemFS->addFile(Twine(Name), 0, llvm::MemoryBuffer::getMemBuffer(Content));
+
+ return getModuleFromSource(Name, OverlayFS);
+}
+
+std::unique_ptr<llvm::Module>
+getModuleFromSource(StringRef Path)
+{
+ IntrusiveRefCntPtr<vfs::FileSystem> VFS(vfs::getRealFileSystem());
+ return getModuleFromSource(Path, VFS);
+}
+
}

extern "C" {
diff --git a/tools/perf/util/c++/clang.h b/tools/perf/util/c++/clang.h
index f64483b..90aff01 100644
--- a/tools/perf/util/c++/clang.h
+++ b/tools/perf/util/c++/clang.h
@@ -12,5 +12,8 @@ using namespace llvm;
std::unique_ptr<Module>
getModuleFromSource(StringRef Name, StringRef Content);

+std::unique_ptr<Module>
+getModuleFromSource(StringRef Path);
+
}
#endif
--
2.10.1

2016-11-15 04:15:08

by Wang Nan

[permalink] [raw]
Subject: [PATCH 11/34] tools build: Add feature detection for LLVM

Check if basic LLVM compiling environment is ready.

Use llvm-config to detect include and library directories. Avoid using
'llvm-config --cxxflags' because its result contain some unwanted flags
like --sysroot (if LLVM is built by yocto).

Use '?=' to set LLVM_CONFIG, so explicitly passing LLVM_CONFIG to make
would override it.

Use 'llvm-config --libs BPF' to check if BPF backend is compiled in.
Since now BPF bytecode is the only required backend, no need to waste
time linking llvm and clang if BPF backend is missing. This also
introduce an implicit requirement that LLVM should be new enough. Old
LLVM doesn't support BPF backend.

Signed-off-by: Wang Nan <[email protected]>
Cc: Alexei Starovoitov <[email protected]>
Cc: He Kuang <[email protected]>
Cc: Jiri Olsa <[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/build/feature/Makefile | 8 ++++++++
tools/build/feature/test-llvm.cpp | 8 ++++++++
2 files changed, 16 insertions(+)
create mode 100644 tools/build/feature/test-llvm.cpp

diff --git a/tools/build/feature/Makefile b/tools/build/feature/Makefile
index 8f668bc..c09de59 100644
--- a/tools/build/feature/Makefile
+++ b/tools/build/feature/Makefile
@@ -55,6 +55,7 @@ FILES := $(addprefix $(OUTPUT),$(FILES))
CC := $(CROSS_COMPILE)gcc -MD
CXX := $(CROSS_COMPILE)g++ -MD
PKG_CONFIG := $(CROSS_COMPILE)pkg-config
+LLVM_CONFIG ?= llvm-config

all: $(FILES)

@@ -229,6 +230,13 @@ $(OUTPUT)test-cxx.bin:
$(OUTPUT)test-jvmti.bin:
$(BUILD)

+$(OUTPUT)test-llvm.bin:
+ $(BUILDXX) -std=gnu++11 \
+ -I$(shell $(LLVM_CONFIG) --includedir) \
+ -L$(shell $(LLVM_CONFIG) --libdir) \
+ $(shell $(LLVM_CONFIG) --libs Core BPF) \
+ $(shell $(LLVM_CONFIG) --system-libs)
+
-include $(OUTPUT)*.d

###############################
diff --git a/tools/build/feature/test-llvm.cpp b/tools/build/feature/test-llvm.cpp
new file mode 100644
index 0000000..d8d2cee
--- /dev/null
+++ b/tools/build/feature/test-llvm.cpp
@@ -0,0 +1,8 @@
+#include "llvm/Support/ManagedStatic.h"
+#include "llvm/Support/raw_ostream.h"
+int main()
+{
+ llvm::errs() << "Hello World!\n";
+ llvm::llvm_shutdown();
+ return 0;
+}
--
2.10.1

2016-11-15 04:15:27

by Wang Nan

[permalink] [raw]
Subject: [PATCH 12/34] tools build: Add feature detection for clang

Check if basic clang compiling environment is ready.

Doesn't like 'llvm-config --libs' which can returns llvm libraries in
right order and duplicates some libraries if necessary, there's no
correspondence for clang libraries (-lclangxxx). to avoid extra
complexity and to avoid new clang breaking libraries ordering, use
--start-group and --end-group.

In this test case, manually identify required clang libs and hope it to
be stable. Putting all clang libraries here is possible (use make's
wildcard), but then feature checking becomes very slow.

Signed-off-by: Wang Nan <[email protected]>
Cc: Alexei Starovoitov <[email protected]>
Cc: He Kuang <[email protected]>
Cc: Jiri Olsa <[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/build/feature/Makefile | 10 ++++++++++
tools/build/feature/test-clang.cpp | 21 +++++++++++++++++++++
2 files changed, 31 insertions(+)
create mode 100644 tools/build/feature/test-clang.cpp

diff --git a/tools/build/feature/Makefile b/tools/build/feature/Makefile
index c09de59..871d553 100644
--- a/tools/build/feature/Makefile
+++ b/tools/build/feature/Makefile
@@ -237,6 +237,16 @@ $(OUTPUT)test-llvm.bin:
$(shell $(LLVM_CONFIG) --libs Core BPF) \
$(shell $(LLVM_CONFIG) --system-libs)

+$(OUTPUT)test-clang.bin:
+ $(BUILDXX) -std=gnu++11 \
+ -I$(shell $(LLVM_CONFIG) --includedir) \
+ -L$(shell $(LLVM_CONFIG) --libdir) \
+ -Wl,--start-group -lclangBasic -lclangDriver \
+ -lclangFrontend -lclangEdit -lclangLex \
+ -lclangAST -Wl,--end-group \
+ $(shell $(LLVM_CONFIG) --libs Core option) \
+ $(shell $(LLVM_CONFIG) --system-libs)
+
-include $(OUTPUT)*.d

###############################
diff --git a/tools/build/feature/test-clang.cpp b/tools/build/feature/test-clang.cpp
new file mode 100644
index 0000000..e23c1b1
--- /dev/null
+++ b/tools/build/feature/test-clang.cpp
@@ -0,0 +1,21 @@
+#include "clang/Basic/VirtualFileSystem.h"
+#include "clang/Driver/Driver.h"
+#include "clang/Frontend/TextDiagnosticPrinter.h"
+#include "llvm/ADT/IntrusiveRefCntPtr.h"
+#include "llvm/Support/ManagedStatic.h"
+#include "llvm/Support/raw_ostream.h"
+
+using namespace clang;
+using namespace clang::driver;
+
+int main()
+{
+ IntrusiveRefCntPtr<DiagnosticIDs> DiagID(new DiagnosticIDs());
+ IntrusiveRefCntPtr<DiagnosticOptions> DiagOpts = new DiagnosticOptions();
+
+ DiagnosticsEngine Diags(DiagID, &*DiagOpts);
+ Driver TheDriver("test", "bpf-pc-linux", Diags);
+
+ llvm::llvm_shutdown();
+ return 0;
+}
--
2.10.1

2016-11-15 04:15:52

by Wang Nan

[permalink] [raw]
Subject: [PATCH 05/34] tools lib bpf: Add missing bpf map functions

Add more BPF map operations to libbpf.

Signed-off-by: Wang Nan <[email protected]>
Cc: Alexei Starovoitov <[email protected]>
Cc: Arnaldo Carvalho de Melo <[email protected]>
Cc: Li Zefan <[email protected]>
---
tools/lib/bpf/bpf.c | 56 +++++++++++++++++++++++++++++++++++++++++++++++++++++
tools/lib/bpf/bpf.h | 7 +++++++
2 files changed, 63 insertions(+)

diff --git a/tools/lib/bpf/bpf.c b/tools/lib/bpf/bpf.c
index 4212ed6..e966248 100644
--- a/tools/lib/bpf/bpf.c
+++ b/tools/lib/bpf/bpf.c
@@ -110,3 +110,59 @@ int bpf_map_update_elem(int fd, void *key, void *value,

return sys_bpf(BPF_MAP_UPDATE_ELEM, &attr, sizeof(attr));
}
+
+int bpf_map_lookup_elem(int fd, void *key, void *value)
+{
+ union bpf_attr attr;
+
+ bzero(&attr, sizeof(attr));
+ attr.map_fd = fd;
+ attr.key = ptr_to_u64(key);
+ attr.value = ptr_to_u64(value);
+
+ return sys_bpf(BPF_MAP_LOOKUP_ELEM, &attr, sizeof(attr));
+}
+
+int bpf_map_delete_elem(int fd, void *key)
+{
+ union bpf_attr attr;
+
+ bzero(&attr, sizeof(attr));
+ attr.map_fd = fd;
+ attr.key = ptr_to_u64(key);
+
+ return sys_bpf(BPF_MAP_DELETE_ELEM, &attr, sizeof(attr));
+}
+
+int bpf_map_get_next_key(int fd, void *key, void *next_key)
+{
+ union bpf_attr attr;
+
+ bzero(&attr, sizeof(attr));
+ attr.map_fd = fd;
+ attr.key = ptr_to_u64(key);
+ attr.next_key = ptr_to_u64(next_key);
+
+ return sys_bpf(BPF_MAP_GET_NEXT_KEY, &attr, sizeof(attr));
+}
+
+int bpf_map_pin(int fd, const char *pathname)
+{
+ union bpf_attr attr;
+
+ bzero(&attr, sizeof(attr));
+ attr.pathname = ptr_to_u64((void *)pathname);
+ attr.bpf_fd = fd;
+
+ return sys_bpf(BPF_OBJ_PIN, &attr, sizeof(attr));
+}
+
+int bpf_map_get(const char *pathname)
+{
+ union bpf_attr attr;
+
+ bzero(&attr, sizeof(attr));
+ attr.pathname = ptr_to_u64((void *)pathname);
+
+ return sys_bpf(BPF_OBJ_GET, &attr, sizeof(attr));
+}
diff --git a/tools/lib/bpf/bpf.h b/tools/lib/bpf/bpf.h
index e8ba540..5b3e52b 100644
--- a/tools/lib/bpf/bpf.h
+++ b/tools/lib/bpf/bpf.h
@@ -35,4 +35,11 @@ int bpf_load_program(enum bpf_prog_type type, struct bpf_insn *insns,

int bpf_map_update_elem(int fd, void *key, void *value,
u64 flags);
+
+int bpf_map_lookup_elem(int fd, void *key, void *value);
+int bpf_map_delete_elem(int fd, void *key);
+int bpf_map_get_next_key(int fd, void *key, void *next_key);
+int bpf_map_pin(int fd, const char *pathname);
+int bpf_map_get(const char *pathname);
+
#endif
--
2.10.1

2016-11-15 04:16:05

by Wang Nan

[permalink] [raw]
Subject: [PATCH 01/34] perf tools: Fix kernel version error in ubuntu

On ubuntu the internal kernel version code is different from what can
be retrived from uname:

$ uname -r
4.4.0-47-generic
$ cat /lib/modules/`uname -r`/build/include/generated/uapi/linux/version.h
#define LINUX_VERSION_CODE 263192
#define KERNEL_VERSION(a,b,c) (((a) << 16) + ((b) << 8) + (c))
$ cat /lib/modules/`uname -r`/build/include/generated/utsrelease.h
#define UTS_RELEASE "4.4.0-47-generic"
#define UTS_UBUNTU_RELEASE_ABI 47
$ cat /proc/version_signature
Ubuntu 4.4.0-47.68-generic 4.4.24

The macro LINUX_VERSION_CODE is set to 4.4.24 (263192 == 0x40418), but
`uname -r` reports 4.4.0.

This mismatch causes LINUX_VERSION_CODE macro passed to BPF script become
an incorrect value, results in magic failure in BPF loading:

$ sudo ./buildperf/perf record -e ./tools/perf/tests/bpf-script-example.c ls
event syntax error: './tools/perf/tests/bpf-script-example.c'
\___ Failed to load program for unknown reason

According to Ubuntu document (https://wiki.ubuntu.com/Kernel/FAQ), the
correct kernel version can be retrived through /proc/version_signature, which
is ubuntu specific.

This patch checks the existance of /proc/version_signature, and returns
version number through parsing this file instead of uname. Version string
is untouched (value returns from uname) because `uname -r` is required
to be consistence with path of kbuild directory in /lib/module.

Signed-off-by: Wang Nan <[email protected]>
Cc: Arnaldo Carvalho de Melo <[email protected]>
---
tools/perf/util/util.c | 55 ++++++++++++++++++++++++++++++++++++++++++++++++--
1 file changed, 53 insertions(+), 2 deletions(-)

diff --git a/tools/perf/util/util.c b/tools/perf/util/util.c
index 5bbd1f6..67ac765 100644
--- a/tools/perf/util/util.c
+++ b/tools/perf/util/util.c
@@ -637,12 +637,63 @@ bool find_process(const char *name)
return ret ? false : true;
}

+static int
+fetch_ubuntu_kernel_version(unsigned int *puint)
+{
+ ssize_t len;
+ size_t line_len = 0;
+ char *ptr, *line = NULL;
+ int version, patchlevel, sublevel, err;
+ FILE *vsig = fopen("/proc/version_signature", "r");
+
+ if (!vsig) {
+ pr_debug("Open /proc/version_signature failed: %s\n",
+ strerror(errno));
+ return -1;
+ }
+
+ len = getline(&line, &line_len, vsig);
+ fclose(vsig);
+ err = -1;
+ if (len <= 0) {
+ pr_debug("Reading from /proc/version_signature failed: %s\n",
+ strerror(errno));
+ goto errout;
+ }
+
+ ptr = strrchr(line, ' ');
+ if (!ptr) {
+ pr_debug("Parsing /proc/version_signature failed: %s\n", line);
+ goto errout;
+ }
+
+ err = sscanf(ptr + 1, "%d.%d.%d",
+ &version, &patchlevel, &sublevel);
+ if (err != 3) {
+ pr_debug("Unable to get kernel version from /proc/version_signature '%s'\n",
+ line);
+ goto errout;
+ }
+
+ if (puint)
+ *puint = (version << 16) + (patchlevel << 8) + sublevel;
+ err = 0;
+errout:
+ free(line);
+ return err;
+}
+
int
fetch_kernel_version(unsigned int *puint, char *str,
size_t str_size)
{
struct utsname utsname;
int version, patchlevel, sublevel, err;
+ bool int_ver_ready = false;
+
+ if (access("/proc/version_signature", R_OK) == 0)
+ if (!fetch_ubuntu_kernel_version(puint))
+ int_ver_ready = true;

if (uname(&utsname))
return -1;
@@ -656,12 +707,12 @@ fetch_kernel_version(unsigned int *puint, char *str,
&version, &patchlevel, &sublevel);

if (err != 3) {
- pr_debug("Unablt to get kernel version from uname '%s'\n",
+ pr_debug("Unable to get kernel version from uname '%s'\n",
utsname.release);
return -1;
}

- if (puint)
+ if (puint && !int_ver_ready)
*puint = (version << 16) + (patchlevel << 8) + sublevel;
return 0;
}
--
2.10.1

2016-11-15 04:33:10

by Wang Nan

[permalink] [raw]
Subject: Re: [PATCH 00/34] perf clang: Builtin clang and perfhook support



On 2016/11/15 12:05, Wang Nan wrote:
> $ sudo -s
> # ulimit -l unlimited
> # perf record -e ./count_syscalls.c echo "Haha"
> Start count, perfpid=25209
> Haha
> [ perf record: Woken up 1 times to write data ]
> syscall 8 count: 6
> syscall 11 count: 1
> syscall 4 count: 6
> syscall 21 count: 1
> syscall 5 count: 3
> syscall 231 count: 1
> syscall 45 count: 3
> syscall 0 count: 24
> syscall 257 count: 1
> syscall 59 count: 4
> syscall 23 count: 9
> syscall 78 count: 2
> syscall 41 count: 4
> syscall 72 count: 8
> syscall 10 count: 3
> syscall 321 count: 1
> syscall 298 count: 7
> syscall 16 count: 21
> syscall 9 count: 16
> syscall 1 count: 114
> syscall 12 count: 3
> syscall 14 count: 35
> syscall 158 count: 1
> syscall 2 count: 15
> syscall 7 count: 18
> syscall 3 count: 11
> [ perf record: Captured and wrote 0.011 MB perf.data ]

Note that this example counts system wide syscall histogram, not
only 'echo' proc. The in-kernel BPF script doesn't know pid of 'echo'
so can't filter base on it. I'm planning adding more perf hook points
to pass information like this.

Thank you.

2016-11-15 04:58:17

by Alexei Starovoitov

[permalink] [raw]
Subject: Re: [PATCH 00/34] perf clang: Builtin clang and perfhook support

On Mon, Nov 14, 2016 at 8:05 PM, Wang Nan <[email protected]> wrote:
> This is version 2 of perf builtin clang patch series. Compare to v1,
> add an exciting feature: jit compiling perf hook functions. This
> features allows script writer report result through BPF map in a
> customized way.

looks great.

> SEC("perfhook:record_start")
> void record_start(void *ctx)
> {
> int perf_pid = getpid(), key = G_perf_pid;
> printf("Start count, perfpid=%d\n", perf_pid);
> jit_helper__map_update_elem(ctx, &GVALS, &key, &perf_pid, 0);

the name, I think, is too verbose.
Why not to keep them as bpf_map_update_elem
even for user space programs?

> SEC("perfhook:record_end")
> void record_end(void *ctx)
> {
> u64 key = -1, value;
> while (!jit_helper__map_get_next_key(ctx, &syscall_counter, &key, &key)) {
> jit_helper__map_lookup_elem(ctx, &syscall_counter, &key, &value);
> printf("syscall %ld\tcount: %ld\n", (long)key, (long)value);

this loop will be less verbose as well.

2016-11-15 05:04:33

by Wang Nan

[permalink] [raw]
Subject: Re: [PATCH 00/34] perf clang: Builtin clang and perfhook support



On 2016/11/15 12:57, Alexei Starovoitov wrote:
> On Mon, Nov 14, 2016 at 8:05 PM, Wang Nan <[email protected]> wrote:
>> This is version 2 of perf builtin clang patch series. Compare to v1,
>> add an exciting feature: jit compiling perf hook functions. This
>> features allows script writer report result through BPF map in a
>> customized way.
> looks great.
>
>> SEC("perfhook:record_start")
>> void record_start(void *ctx)
>> {
>> int perf_pid = getpid(), key = G_perf_pid;
>> printf("Start count, perfpid=%d\n", perf_pid);
>> jit_helper__map_update_elem(ctx, &GVALS, &key, &perf_pid, 0);
> the name, I think, is too verbose.
> Why not to keep them as bpf_map_update_elem
> even for user space programs?

I can make it shorter by give it a better name or use a wrapper like

BPF_MAP(update_elem)

but the only thing I can't do is to make perfhook and in-kernel script
use a uniform name for these bpf_map functions, because
bpf_map_update_elem is already defined:

"static long (*bpf_map_update_elem)(void *, void *, void *, unsigned
long) = (void *)2;\n"


>> SEC("perfhook:record_end")
>> void record_end(void *ctx)
>> {
>> u64 key = -1, value;
>> while (!jit_helper__map_get_next_key(ctx, &syscall_counter, &key, &key)) {
>> jit_helper__map_lookup_elem(ctx, &syscall_counter, &key, &value);
>> printf("syscall %ld\tcount: %ld\n", (long)key, (long)value);
> this loop will be less verbose as well.


2016-11-15 05:21:48

by Alexei Starovoitov

[permalink] [raw]
Subject: Re: [PATCH 00/34] perf clang: Builtin clang and perfhook support

On Mon, Nov 14, 2016 at 9:03 PM, Wangnan (F) <[email protected]> wrote:
>
>
> On 2016/11/15 12:57, Alexei Starovoitov wrote:
>>
>> On Mon, Nov 14, 2016 at 8:05 PM, Wang Nan <[email protected]> wrote:
>>>
>>> This is version 2 of perf builtin clang patch series. Compare to v1,
>>> add an exciting feature: jit compiling perf hook functions. This
>>> features allows script writer report result through BPF map in a
>>> customized way.
>>
>> looks great.
>>
>>> SEC("perfhook:record_start")
>>> void record_start(void *ctx)
>>> {
>>> int perf_pid = getpid(), key = G_perf_pid;
>>> printf("Start count, perfpid=%d\n", perf_pid);
>>> jit_helper__map_update_elem(ctx, &GVALS, &key, &perf_pid, 0);
>>
>> the name, I think, is too verbose.
>> Why not to keep them as bpf_map_update_elem
>> even for user space programs?
>
>
> I can make it shorter by give it a better name or use a wrapper like
>
> BPF_MAP(update_elem)

the macro isn't pretty, since function calls won't look like calls.

> but the only thing I can't do is to make perfhook and in-kernel script
> use a uniform name for these bpf_map functions, because
> bpf_map_update_elem is already defined:
>
> "static long (*bpf_map_update_elem)(void *, void *, void *, unsigned long) =
> (void *)2;\n"

right. i guess you could have #ifdef it, so it's different for bpf backend
and for native.
Another alternative is to call it map_update_elem or map_update
or bpf_map_update. Something shorter is already a win.
'jit_helper__' prefix is an implementation detail. The users don't
need to know and don't need to spell it out everywhere.

2016-11-15 05:38:35

by Wang Nan

[permalink] [raw]
Subject: Re: [PATCH 00/34] perf clang: Builtin clang and perfhook support



On 2016/11/15 13:21, Alexei Starovoitov wrote:
> On Mon, Nov 14, 2016 at 9:03 PM, Wangnan (F) <[email protected]> wrote:
>>
>> On 2016/11/15 12:57, Alexei Starovoitov wrote:
>>> On Mon, Nov 14, 2016 at 8:05 PM, Wang Nan <[email protected]> wrote:
>>>> This is version 2 of perf builtin clang patch series. Compare to v1,
>>>> add an exciting feature: jit compiling perf hook functions. This
>>>> features allows script writer report result through BPF map in a
>>>> customized way.
>>> looks great.
>>>
>>>> SEC("perfhook:record_start")
>>>> void record_start(void *ctx)
>>>> {
>>>> int perf_pid = getpid(), key = G_perf_pid;
>>>> printf("Start count, perfpid=%d\n", perf_pid);
>>>> jit_helper__map_update_elem(ctx, &GVALS, &key, &perf_pid, 0);
>>> the name, I think, is too verbose.
>>> Why not to keep them as bpf_map_update_elem
>>> even for user space programs?
>>
>> I can make it shorter by give it a better name or use a wrapper like
>>
>> BPF_MAP(update_elem)
> the macro isn't pretty, since function calls won't look like calls.
>
>> but the only thing I can't do is to make perfhook and in-kernel script
>> use a uniform name for these bpf_map functions, because
>> bpf_map_update_elem is already defined:
>>
>> "static long (*bpf_map_update_elem)(void *, void *, void *, unsigned long) =
>> (void *)2;\n"
> right. i guess you could have #ifdef it, so it's different for bpf backend
> and for native.

Then the '.c' -> LLVM IR compiling should be done twice for BPF
and for JIT to make the macro work. In current implementation
we have only one LLVM IR. It is faster and can make sure the data
layout ("maps" section) is identical.

> Another alternative is to call it map_update_elem or map_update
> or bpf_map_update. Something shorter is already a win.
> 'jit_helper__' prefix is an implementation detail. The users don't
> need to know and don't need to spell it out everywhere.
Good. Let choose a better name for them.

Thank you.

2016-11-17 03:24:40

by Wang Nan

[permalink] [raw]
Subject: Re: [PATCH 05/34] tools lib bpf: Add missing bpf map functions



On 2016/11/15 12:05, Wang Nan wrote:
> Add more BPF map operations to libbpf.
>
> Signed-off-by: Wang Nan <[email protected]>
> Cc: Alexei Starovoitov <[email protected]>
> Cc: Arnaldo Carvalho de Melo <[email protected]>
> Cc: Li Zefan <[email protected]>
> ---
> tools/lib/bpf/bpf.c | 56 +++++++++++++++++++++++++++++++++++++++++++++++++++++
> tools/lib/bpf/bpf.h | 7 +++++++
> 2 files changed, 63 insertions(+)
>
> diff --git a/tools/lib/bpf/bpf.c b/tools/lib/bpf/bpf.c
> index 4212ed6..e966248 100644
> --- a/tools/lib/bpf/bpf.c
> +++ b/tools/lib/bpf/bpf.c
> @@ -110,3 +110,59 @@ int bpf_map_update_elem(int fd, void *key, void *value,
>
> return sys_bpf(BPF_MAP_UPDATE_ELEM, &attr, sizeof(attr));
> }
> +
> +int bpf_map_lookup_elem(int fd, void *key, void *value)
> +{
> + union bpf_attr attr;
> +
> + bzero(&attr, sizeof(attr));
> + attr.map_fd = fd;
> + attr.key = ptr_to_u64(key);
> + attr.value = ptr_to_u64(value);
> +
> + return sys_bpf(BPF_MAP_LOOKUP_ELEM, &attr, sizeof(attr));
> +}
> +
> +int bpf_map_delete_elem(int fd, void *key)
> +{
> + union bpf_attr attr;
> +
> + bzero(&attr, sizeof(attr));
> + attr.map_fd = fd;
> + attr.key = ptr_to_u64(key);
> +
> + return sys_bpf(BPF_MAP_DELETE_ELEM, &attr, sizeof(attr));
> +}
> +
> +int bpf_map_get_next_key(int fd, void *key, void *next_key)
> +{
> + union bpf_attr attr;
> +
> + bzero(&attr, sizeof(attr));
> + attr.map_fd = fd;
> + attr.key = ptr_to_u64(key);
> + attr.next_key = ptr_to_u64(next_key);
> +
> + return sys_bpf(BPF_MAP_GET_NEXT_KEY, &attr, sizeof(attr));
> +}
> +
> +int bpf_map_pin(int fd, const char *pathname)
> +{
> + union bpf_attr attr;
> +
> + bzero(&attr, sizeof(attr));
> + attr.pathname = ptr_to_u64((void *)pathname);
> + attr.bpf_fd = fd;
> +
> + return sys_bpf(BPF_OBJ_PIN, &attr, sizeof(attr));
> +}
> +
> +int bpf_map_get(const char *pathname)
> +{
> + union bpf_attr attr;
> +
> + bzero(&attr, sizeof(attr));
> + attr.pathname = ptr_to_u64((void *)pathname);
> +
> + return sys_bpf(BPF_OBJ_GET, &attr, sizeof(attr));
> +}

bpf_map_{pin,get} should be rename to bpf_obj_{pin,get} since they can
be used on BPF program. Thanks to Joe Stringer.

> diff --git a/tools/lib/bpf/bpf.h b/tools/lib/bpf/bpf.h
> index e8ba540..5b3e52b 100644
> --- a/tools/lib/bpf/bpf.h
> +++ b/tools/lib/bpf/bpf.h
> @@ -35,4 +35,11 @@ int bpf_load_program(enum bpf_prog_type type, struct bpf_insn *insns,
>
> int bpf_map_update_elem(int fd, void *key, void *value,
> u64 flags);
> +
> +int bpf_map_lookup_elem(int fd, void *key, void *value);
> +int bpf_map_delete_elem(int fd, void *key);
> +int bpf_map_get_next_key(int fd, void *key, void *next_key);
> +int bpf_map_pin(int fd, const char *pathname);
> +int bpf_map_get(const char *pathname);
> +
> #endif


2016-11-25 14:40:13

by Arnaldo Carvalho de Melo

[permalink] [raw]
Subject: Re: [PATCH 05/34] tools lib bpf: Add missing bpf map functions

Em Thu, Nov 17, 2016 at 11:23:58AM +0800, Wangnan (F) escreveu:
>
>
> On 2016/11/15 12:05, Wang Nan wrote:
> >Add more BPF map operations to libbpf.
> >
> >Signed-off-by: Wang Nan <[email protected]>
> >Cc: Alexei Starovoitov <[email protected]>
> >Cc: Arnaldo Carvalho de Melo <[email protected]>
> >Cc: Li Zefan <[email protected]>
> >---
> > tools/lib/bpf/bpf.c | 56 +++++++++++++++++++++++++++++++++++++++++++++++++++++
> > tools/lib/bpf/bpf.h | 7 +++++++
> > 2 files changed, 63 insertions(+)
> >
> >diff --git a/tools/lib/bpf/bpf.c b/tools/lib/bpf/bpf.c
> >index 4212ed6..e966248 100644
> >--- a/tools/lib/bpf/bpf.c
> >+++ b/tools/lib/bpf/bpf.c
> >@@ -110,3 +110,59 @@ int bpf_map_update_elem(int fd, void *key, void *value,
> > return sys_bpf(BPF_MAP_UPDATE_ELEM, &attr, sizeof(attr));
> > }
> >+
> >+int bpf_map_lookup_elem(int fd, void *key, void *value)
> >+{
> >+ union bpf_attr attr;
> >+
> >+ bzero(&attr, sizeof(attr));
> >+ attr.map_fd = fd;
> >+ attr.key = ptr_to_u64(key);
> >+ attr.value = ptr_to_u64(value);
> >+
> >+ return sys_bpf(BPF_MAP_LOOKUP_ELEM, &attr, sizeof(attr));
> >+}
> >+
> >+int bpf_map_delete_elem(int fd, void *key)
> >+{
> >+ union bpf_attr attr;
> >+
> >+ bzero(&attr, sizeof(attr));
> >+ attr.map_fd = fd;
> >+ attr.key = ptr_to_u64(key);
> >+
> >+ return sys_bpf(BPF_MAP_DELETE_ELEM, &attr, sizeof(attr));
> >+}
> >+
> >+int bpf_map_get_next_key(int fd, void *key, void *next_key)
> >+{
> >+ union bpf_attr attr;
> >+
> >+ bzero(&attr, sizeof(attr));
> >+ attr.map_fd = fd;
> >+ attr.key = ptr_to_u64(key);
> >+ attr.next_key = ptr_to_u64(next_key);
> >+
> >+ return sys_bpf(BPF_MAP_GET_NEXT_KEY, &attr, sizeof(attr));
> >+}
> >+
> >+int bpf_map_pin(int fd, const char *pathname)
> >+{
> >+ union bpf_attr attr;
> >+
> >+ bzero(&attr, sizeof(attr));
> >+ attr.pathname = ptr_to_u64((void *)pathname);
> >+ attr.bpf_fd = fd;
> >+
> >+ return sys_bpf(BPF_OBJ_PIN, &attr, sizeof(attr));
> >+}
> >+
> >+int bpf_map_get(const char *pathname)
> >+{
> >+ union bpf_attr attr;
> >+
> >+ bzero(&attr, sizeof(attr));
> >+ attr.pathname = ptr_to_u64((void *)pathname);
> >+
> >+ return sys_bpf(BPF_OBJ_GET, &attr, sizeof(attr));
> >+}
>
> bpf_map_{pin,get} should be rename to bpf_obj_{pin,get} since they can
> be used on BPF program. Thanks to Joe Stringer.

Ok, and keep:

+int bpf_map_lookup_elem(int fd, void *key, void *value);
+int bpf_map_delete_elem(int fd, void *key);
+int bpf_map_get_next_key(int fd, void *key, void *next_key);

as-is?

I'll push te first four patches in this series, will continue after
clarifying this one.

- Arnaldo


> >diff --git a/tools/lib/bpf/bpf.h b/tools/lib/bpf/bpf.h
> >index e8ba540..5b3e52b 100644
> >--- a/tools/lib/bpf/bpf.h
> >+++ b/tools/lib/bpf/bpf.h
> >@@ -35,4 +35,11 @@ int bpf_load_program(enum bpf_prog_type type, struct bpf_insn *insns,
> > int bpf_map_update_elem(int fd, void *key, void *value,
> > u64 flags);
> >+
> >+int bpf_map_lookup_elem(int fd, void *key, void *value);
> >+int bpf_map_delete_elem(int fd, void *key);
> >+int bpf_map_get_next_key(int fd, void *key, void *next_key);
> >+int bpf_map_pin(int fd, const char *pathname);
> >+int bpf_map_get(const char *pathname);
> >+
> > #endif
>

Subject: [tip:perf/core] perf tools: Fix kernel version error in ubuntu

Commit-ID: d18acd15c6dfb669f0463afa31ac5343594d2fe2
Gitweb: http://git.kernel.org/tip/d18acd15c6dfb669f0463afa31ac5343594d2fe2
Author: Wang Nan <[email protected]>
AuthorDate: Tue, 15 Nov 2016 04:05:44 +0000
Committer: Arnaldo Carvalho de Melo <[email protected]>
CommitDate: Fri, 25 Nov 2016 11:00:30 -0300

perf tools: Fix kernel version error in ubuntu

On ubuntu the internal kernel version code is different from what can
be retrived from uname:

$ uname -r
4.4.0-47-generic
$ cat /lib/modules/`uname -r`/build/include/generated/uapi/linux/version.h
#define LINUX_VERSION_CODE 263192
#define KERNEL_VERSION(a,b,c) (((a) << 16) + ((b) << 8) + (c))
$ cat /lib/modules/`uname -r`/build/include/generated/utsrelease.h
#define UTS_RELEASE "4.4.0-47-generic"
#define UTS_UBUNTU_RELEASE_ABI 47
$ cat /proc/version_signature
Ubuntu 4.4.0-47.68-generic 4.4.24

The macro LINUX_VERSION_CODE is set to 4.4.24 (263192 == 0x40418), but
`uname -r` reports 4.4.0.

This mismatch causes LINUX_VERSION_CODE macro passed to BPF script become
an incorrect value, results in magic failure in BPF loading:

$ sudo ./buildperf/perf record -e ./tools/perf/tests/bpf-script-example.c ls
event syntax error: './tools/perf/tests/bpf-script-example.c'
\___ Failed to load program for unknown reason

According to Ubuntu document (https://wiki.ubuntu.com/Kernel/FAQ), the
correct kernel version can be retrived through /proc/version_signature, which
is ubuntu specific.

This patch checks the existance of /proc/version_signature, and returns
version number through parsing this file instead of uname. Version string
is untouched (value returns from uname) because `uname -r` is required
to be consistence with path of kbuild directory in /lib/module.

Signed-off-by: Wang Nan <[email protected]>
Cc: Alexei Starovoitov <[email protected]>
Cc: He Kuang <[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/util.c | 55 ++++++++++++++++++++++++++++++++++++++++++++++++--
1 file changed, 53 insertions(+), 2 deletions(-)

diff --git a/tools/perf/util/util.c b/tools/perf/util/util.c
index 5bbd1f6..67ac765 100644
--- a/tools/perf/util/util.c
+++ b/tools/perf/util/util.c
@@ -637,12 +637,63 @@ bool find_process(const char *name)
return ret ? false : true;
}

+static int
+fetch_ubuntu_kernel_version(unsigned int *puint)
+{
+ ssize_t len;
+ size_t line_len = 0;
+ char *ptr, *line = NULL;
+ int version, patchlevel, sublevel, err;
+ FILE *vsig = fopen("/proc/version_signature", "r");
+
+ if (!vsig) {
+ pr_debug("Open /proc/version_signature failed: %s\n",
+ strerror(errno));
+ return -1;
+ }
+
+ len = getline(&line, &line_len, vsig);
+ fclose(vsig);
+ err = -1;
+ if (len <= 0) {
+ pr_debug("Reading from /proc/version_signature failed: %s\n",
+ strerror(errno));
+ goto errout;
+ }
+
+ ptr = strrchr(line, ' ');
+ if (!ptr) {
+ pr_debug("Parsing /proc/version_signature failed: %s\n", line);
+ goto errout;
+ }
+
+ err = sscanf(ptr + 1, "%d.%d.%d",
+ &version, &patchlevel, &sublevel);
+ if (err != 3) {
+ pr_debug("Unable to get kernel version from /proc/version_signature '%s'\n",
+ line);
+ goto errout;
+ }
+
+ if (puint)
+ *puint = (version << 16) + (patchlevel << 8) + sublevel;
+ err = 0;
+errout:
+ free(line);
+ return err;
+}
+
int
fetch_kernel_version(unsigned int *puint, char *str,
size_t str_size)
{
struct utsname utsname;
int version, patchlevel, sublevel, err;
+ bool int_ver_ready = false;
+
+ if (access("/proc/version_signature", R_OK) == 0)
+ if (!fetch_ubuntu_kernel_version(puint))
+ int_ver_ready = true;

if (uname(&utsname))
return -1;
@@ -656,12 +707,12 @@ fetch_kernel_version(unsigned int *puint, char *str,
&version, &patchlevel, &sublevel);

if (err != 3) {
- pr_debug("Unablt to get kernel version from uname '%s'\n",
+ pr_debug("Unable to get kernel version from uname '%s'\n",
utsname.release);
return -1;
}

- if (puint)
+ if (puint && !int_ver_ready)
*puint = (version << 16) + (patchlevel << 8) + sublevel;
return 0;
}

Subject: [tip:perf/core] perf record: Fix segfault when running with suid and kptr_restrict is 1

Commit-ID: 3dbe46c5245f61328797738c6a0a6cd4bf921f61
Gitweb: http://git.kernel.org/tip/3dbe46c5245f61328797738c6a0a6cd4bf921f61
Author: Wang Nan <[email protected]>
AuthorDate: Tue, 15 Nov 2016 04:05:45 +0000
Committer: Arnaldo Carvalho de Melo <[email protected]>
CommitDate: Fri, 25 Nov 2016 11:11:10 -0300

perf record: Fix segfault when running with suid and kptr_restrict is 1

Before this patch perf panics if kptr_restrict is set to 1 and perf is
owned by root with suid set:

$ whoami
wangnan
$ ls -l ./perf
-rwsr-xr-x 1 root root 19781908 Sep 21 19:29 /home/wangnan/perf
$ cat /proc/sys/kernel/kptr_restrict
1
$ cat /proc/sys/kernel/perf_event_paranoid
-1
$ ./perf record -a
Segmentation fault (core dumped)
$

The reason is that perf assumes it is allowed to read kptr from
/proc/kallsyms when euid is root, but in fact the kernel doesn't allow
reading kptr when euid and uid do not match with each other:

$ cp /bin/cat .
$ sudo chown root:root ./cat
$ sudo chmod u+s ./cat
$ cat /proc/kallsyms | grep do_fork
0000000000000000 T _do_fork <--- kptr is hidden even euid is root
$ sudo cat /proc/kallsyms | grep do_fork
ffffffff81080230 T _do_fork

See lib/vsprintf.c for kernel side code.

This patch fixes this problem by checking both uid and euid.

Signed-off-by: Wang Nan <[email protected]>
Tested-by: Arnaldo Carvalho de Melo <[email protected]>
Cc: Alexei Starovoitov <[email protected]>
Cc: He Kuang <[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/symbol.c | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/tools/perf/util/symbol.c b/tools/perf/util/symbol.c
index aecff69..420ada9 100644
--- a/tools/perf/util/symbol.c
+++ b/tools/perf/util/symbol.c
@@ -1962,7 +1962,7 @@ static bool symbol__read_kptr_restrict(void)
char line[8];

if (fgets(line, sizeof(line), fp) != NULL)
- value = (geteuid() != 0) ?
+ value = ((geteuid() != 0) || (getuid() != 0)) ?
(atoi(line) != 0) :
(atoi(line) == 2);


Subject: [tip:perf/core] tools lib bpf: Fix maps resolution

Commit-ID: 4708bbda5cb2f6cdc331744597527143f46394d5
Gitweb: http://git.kernel.org/tip/4708bbda5cb2f6cdc331744597527143f46394d5
Author: Eric Leblond <[email protected]>
AuthorDate: Tue, 15 Nov 2016 04:05:47 +0000
Committer: Arnaldo Carvalho de Melo <[email protected]>
CommitDate: Fri, 25 Nov 2016 11:27:33 -0300

tools lib bpf: Fix maps resolution

It is not correct to assimilate the elf data of the maps section to an
array of map definition. In fact the sizes differ. The offset provided
in the symbol section has to be used instead.

This patch fixes a bug causing a elf with two maps not to load
correctly.

Wang Nan added:

This patch requires a name for each BPF map, so array of BPF maps is not
allowed. This restriction is reasonable, because kernel verifier forbid
indexing BPF map from such array unless the index is a fixed value, but
if the index is fixed why not merging it into name?

For example:

Program like this:
...
unsigned long cpu = get_smp_processor_id();
int *pval = map_lookup_elem(&map_array[cpu], &key);
...

Generates bytecode like this:

0: (b7) r1 = 0
1: (63) *(u32 *)(r10 -4) = r1
2: (b7) r1 = 680997
3: (63) *(u32 *)(r10 -8) = r1
4: (85) call 8
5: (67) r0 <<= 4
6: (18) r1 = 0x112dd000
8: (0f) r0 += r1
9: (bf) r2 = r10
10: (07) r2 += -4
11: (bf) r1 = r0
12: (85) call 1

Where instruction 8 is the computation, 8 and 11 render r1 to an invalid
value for function map_lookup_elem, causes verifier report error.

Signed-off-by: Eric Leblond <[email protected]>
Cc: Alexei Starovoitov <[email protected]>
Cc: He Kuang <[email protected]>
Cc: Wang Nan <[email protected]>
[ Merge bpf_object__init_maps_name into bpf_object__init_maps.
Fix segfault for buggy BPF script Validate obj->maps ]
Cc: Zefan Li <[email protected]>
Cc: [email protected]
Link: http://lkml.kernel.org/r/[email protected]
Signed-off-by: Wang Nan <[email protected]>
Signed-off-by: Arnaldo Carvalho de Melo <[email protected]>
---
tools/lib/bpf/libbpf.c | 142 ++++++++++++++++++++++++++++++++++---------------
1 file changed, 98 insertions(+), 44 deletions(-)

diff --git a/tools/lib/bpf/libbpf.c b/tools/lib/bpf/libbpf.c
index b699aea..96a2b2f 100644
--- a/tools/lib/bpf/libbpf.c
+++ b/tools/lib/bpf/libbpf.c
@@ -185,6 +185,7 @@ struct bpf_program {
struct bpf_map {
int fd;
char *name;
+ size_t offset;
struct bpf_map_def def;
void *priv;
bpf_map_clear_priv_t clear_priv;
@@ -513,57 +514,106 @@ bpf_object__init_kversion(struct bpf_object *obj,
}

static int
-bpf_object__init_maps(struct bpf_object *obj, void *data,
- size_t size)
+bpf_object__validate_maps(struct bpf_object *obj)
{
- size_t nr_maps;
int i;

- nr_maps = size / sizeof(struct bpf_map_def);
- if (!data || !nr_maps) {
- pr_debug("%s doesn't need map definition\n",
- obj->path);
+ /*
+ * If there's only 1 map, the only error case should have been
+ * catched in bpf_object__init_maps().
+ */
+ if (!obj->maps || !obj->nr_maps || (obj->nr_maps == 1))
return 0;
- }

- pr_debug("maps in %s: %zd bytes\n", obj->path, size);
+ for (i = 1; i < obj->nr_maps; i++) {
+ const struct bpf_map *a = &obj->maps[i - 1];
+ const struct bpf_map *b = &obj->maps[i];

- obj->maps = calloc(nr_maps, sizeof(obj->maps[0]));
- if (!obj->maps) {
- pr_warning("alloc maps for object failed\n");
- return -ENOMEM;
+ if (b->offset - a->offset < sizeof(struct bpf_map_def)) {
+ pr_warning("corrupted map section in %s: map \"%s\" too small\n",
+ obj->path, a->name);
+ return -EINVAL;
+ }
}
- obj->nr_maps = nr_maps;
-
- for (i = 0; i < nr_maps; i++) {
- struct bpf_map_def *def = &obj->maps[i].def;
+ return 0;
+}

- /*
- * fill all fd with -1 so won't close incorrect
- * fd (fd=0 is stdin) when failure (zclose won't close
- * negative fd)).
- */
- obj->maps[i].fd = -1;
+static int compare_bpf_map(const void *_a, const void *_b)
+{
+ const struct bpf_map *a = _a;
+ const struct bpf_map *b = _b;

- /* Save map definition into obj->maps */
- *def = ((struct bpf_map_def *)data)[i];
- }
- return 0;
+ return a->offset - b->offset;
}

static int
-bpf_object__init_maps_name(struct bpf_object *obj)
+bpf_object__init_maps(struct bpf_object *obj)
{
- int i;
+ int i, map_idx, nr_maps = 0;
+ Elf_Scn *scn;
+ Elf_Data *data;
Elf_Data *symbols = obj->efile.symbols;

- if (!symbols || obj->efile.maps_shndx < 0)
+ if (obj->efile.maps_shndx < 0)
+ return -EINVAL;
+ if (!symbols)
+ return -EINVAL;
+
+ scn = elf_getscn(obj->efile.elf, obj->efile.maps_shndx);
+ if (scn)
+ data = elf_getdata(scn, NULL);
+ if (!scn || !data) {
+ pr_warning("failed to get Elf_Data from map section %d\n",
+ obj->efile.maps_shndx);
return -EINVAL;
+ }

+ /*
+ * Count number of maps. Each map has a name.
+ * Array of maps is not supported: only the first element is
+ * considered.
+ *
+ * TODO: Detect array of map and report error.
+ */
for (i = 0; i < symbols->d_size / sizeof(GElf_Sym); i++) {
GElf_Sym sym;
- size_t map_idx;
+
+ if (!gelf_getsym(symbols, i, &sym))
+ continue;
+ if (sym.st_shndx != obj->efile.maps_shndx)
+ continue;
+ nr_maps++;
+ }
+
+ /* Alloc obj->maps and fill nr_maps. */
+ pr_debug("maps in %s: %d maps in %zd bytes\n", obj->path,
+ nr_maps, data->d_size);
+
+ if (!nr_maps)
+ return 0;
+
+ obj->maps = calloc(nr_maps, sizeof(obj->maps[0]));
+ if (!obj->maps) {
+ pr_warning("alloc maps for object failed\n");
+ return -ENOMEM;
+ }
+ obj->nr_maps = nr_maps;
+
+ /*
+ * fill all fd with -1 so won't close incorrect
+ * fd (fd=0 is stdin) when failure (zclose won't close
+ * negative fd)).
+ */
+ for (i = 0; i < nr_maps; i++)
+ obj->maps[i].fd = -1;
+
+ /*
+ * Fill obj->maps using data in "maps" section.
+ */
+ for (i = 0, map_idx = 0; i < symbols->d_size / sizeof(GElf_Sym); i++) {
+ GElf_Sym sym;
const char *map_name;
+ struct bpf_map_def *def;

if (!gelf_getsym(symbols, i, &sym))
continue;
@@ -573,21 +623,27 @@ bpf_object__init_maps_name(struct bpf_object *obj)
map_name = elf_strptr(obj->efile.elf,
obj->efile.strtabidx,
sym.st_name);
- map_idx = sym.st_value / sizeof(struct bpf_map_def);
- if (map_idx >= obj->nr_maps) {
- pr_warning("index of map \"%s\" is buggy: %zu > %zu\n",
- map_name, map_idx, obj->nr_maps);
- continue;
+ obj->maps[map_idx].offset = sym.st_value;
+ if (sym.st_value + sizeof(struct bpf_map_def) > data->d_size) {
+ pr_warning("corrupted maps section in %s: last map \"%s\" too small\n",
+ obj->path, map_name);
+ return -EINVAL;
}
+
obj->maps[map_idx].name = strdup(map_name);
if (!obj->maps[map_idx].name) {
pr_warning("failed to alloc map name\n");
return -ENOMEM;
}
- pr_debug("map %zu is \"%s\"\n", map_idx,
+ pr_debug("map %d is \"%s\"\n", map_idx,
obj->maps[map_idx].name);
+ def = (struct bpf_map_def *)(data->d_buf + sym.st_value);
+ obj->maps[map_idx].def = *def;
+ map_idx++;
}
- return 0;
+
+ qsort(obj->maps, obj->nr_maps, sizeof(obj->maps[0]), compare_bpf_map);
+ return bpf_object__validate_maps(obj);
}

static int bpf_object__elf_collect(struct bpf_object *obj)
@@ -645,11 +701,9 @@ static int bpf_object__elf_collect(struct bpf_object *obj)
err = bpf_object__init_kversion(obj,
data->d_buf,
data->d_size);
- else if (strcmp(name, "maps") == 0) {
- err = bpf_object__init_maps(obj, data->d_buf,
- data->d_size);
+ else if (strcmp(name, "maps") == 0)
obj->efile.maps_shndx = idx;
- } else if (sh.sh_type == SHT_SYMTAB) {
+ else if (sh.sh_type == SHT_SYMTAB) {
if (obj->efile.symbols) {
pr_warning("bpf: multiple SYMTAB in %s\n",
obj->path);
@@ -698,7 +752,7 @@ static int bpf_object__elf_collect(struct bpf_object *obj)
return LIBBPF_ERRNO__FORMAT;
}
if (obj->efile.maps_shndx >= 0)
- err = bpf_object__init_maps_name(obj);
+ err = bpf_object__init_maps(obj);
out:
return err;
}
@@ -807,7 +861,7 @@ bpf_object__create_maps(struct bpf_object *obj)
zclose(obj->maps[j].fd);
return err;
}
- pr_debug("create map: fd=%d\n", *pfd);
+ pr_debug("create map %s: fd=%d\n", obj->maps[i].name, *pfd);
}

return 0;

Subject: [tip:perf/core] perf tools: Add missing struct definition in probe_event.h

Commit-ID: d6be16719e0b65f586ae4a301f02407422e6b5dd
Gitweb: http://git.kernel.org/tip/d6be16719e0b65f586ae4a301f02407422e6b5dd
Author: Wang Nan <[email protected]>
AuthorDate: Tue, 15 Nov 2016 04:05:46 +0000
Committer: Arnaldo Carvalho de Melo <[email protected]>
CommitDate: Fri, 25 Nov 2016 11:25:46 -0300

perf tools: Add missing struct definition in probe_event.h

Commit 0b3c2264ae30 ("perf symbols: Fix kallsyms perf test on ppc64le")
refers struct symbol in probe_event.h, but forgets to include its
definition. Gcc will complain about it when that definition is not
added, by sheer luck, by some other header included before
probe_event.h.

Signed-off-by: Wang Nan <[email protected]>
Cc: Alexei Starovoitov <[email protected]>
Cc: He Kuang <[email protected]>
Cc: Naveen N. Rao <[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/probe-event.h | 2 ++
1 file changed, 2 insertions(+)

diff --git a/tools/perf/util/probe-event.h b/tools/perf/util/probe-event.h
index 8091d15..5d4e940 100644
--- a/tools/perf/util/probe-event.h
+++ b/tools/perf/util/probe-event.h
@@ -18,6 +18,8 @@ struct probe_conf {
extern struct probe_conf probe_conf;
extern bool probe_event_dry_run;

+struct symbol;
+
/* kprobe-tracer and uprobe-tracer tracing point */
struct probe_trace_point {
char *realname; /* function real name (if needed) */