diff options
author | Wang Nan <wangnan0@huawei.com> | 2015-10-14 08:41:20 -0400 |
---|---|---|
committer | Arnaldo Carvalho de Melo <acme@redhat.com> | 2015-10-29 16:16:23 -0400 |
commit | d509db0473e40134286271b1d1adadccf42ac467 (patch) | |
tree | e9ec6d2dad1ff0465f2d68edee9e8fcf8c510a62 /tools/perf/tests/bpf-script-example.c | |
parent | 71dc2326252ff1bcdddc05db03c0f831d16c9447 (diff) |
perf tools: Compile scriptlets to BPF objects when passing '.c' to --event
This patch provides infrastructure for passing source files to --event
directly using:
# perf record --event bpf-file.c command
This patch does following works:
1) Allow passing '.c' file to '--event'. parse_events_load_bpf() is
expanded to allow caller tell it whether the passed file is source
file or object.
2) llvm__compile_bpf() is called to compile the '.c' file, the result
is saved into memory. Use bpf_object__open_buffer() to load the
in-memory object.
Introduces a bpf-script-example.c so we can manually test it:
# perf record --clang-opt "-DLINUX_VERSION_CODE=0x40200" --event ./bpf-script-example.c sleep 1
Note that '--clang-opt' must put before '--event'.
Futher patches will merge it into a testcase so can be tested automatically.
Signed-off-by: Wang Nan <wangnan0@huawei.com>
Acked-by: Alexei Starovoitov <ast@plumgrid.com>
Cc: Brendan Gregg <brendan.d.gregg@gmail.com>
Cc: Daniel Borkmann <daniel@iogearbox.net>
Cc: David Ahern <dsahern@gmail.com>
Cc: He Kuang <hekuang@huawei.com>
Cc: Jiri Olsa <jolsa@kernel.org>
Cc: Kaixu Xia <xiakaixu@huawei.com>
Cc: Masami Hiramatsu <masami.hiramatsu.pt@hitachi.com>
Cc: Namhyung Kim <namhyung@kernel.org>
Cc: Peter Zijlstra <a.p.zijlstra@chello.nl>
Cc: Zefan Li <lizefan@huawei.com>
Cc: pi3orama@163.com
Link: http://lkml.kernel.org/r/1444826502-49291-10-git-send-email-wangnan0@huawei.com
Signed-off-by: He Kuang <hekuang@huawei.com>
Signed-off-by: Arnaldo Carvalho de Melo <acme@redhat.com>
Diffstat (limited to 'tools/perf/tests/bpf-script-example.c')
-rw-r--r-- | tools/perf/tests/bpf-script-example.c | 44 |
1 files changed, 44 insertions, 0 deletions
diff --git a/tools/perf/tests/bpf-script-example.c b/tools/perf/tests/bpf-script-example.c new file mode 100644 index 000000000000..410a70b93b93 --- /dev/null +++ b/tools/perf/tests/bpf-script-example.c | |||
@@ -0,0 +1,44 @@ | |||
1 | #ifndef LINUX_VERSION_CODE | ||
2 | # error Need LINUX_VERSION_CODE | ||
3 | # error Example: for 4.2 kernel, put 'clang-opt="-DLINUX_VERSION_CODE=0x40200" into llvm section of ~/.perfconfig' | ||
4 | #endif | ||
5 | #define BPF_ANY 0 | ||
6 | #define BPF_MAP_TYPE_ARRAY 2 | ||
7 | #define BPF_FUNC_map_lookup_elem 1 | ||
8 | #define BPF_FUNC_map_update_elem 2 | ||
9 | |||
10 | static void *(*bpf_map_lookup_elem)(void *map, void *key) = | ||
11 | (void *) BPF_FUNC_map_lookup_elem; | ||
12 | static void *(*bpf_map_update_elem)(void *map, void *key, void *value, int flags) = | ||
13 | (void *) BPF_FUNC_map_update_elem; | ||
14 | |||
15 | struct bpf_map_def { | ||
16 | unsigned int type; | ||
17 | unsigned int key_size; | ||
18 | unsigned int value_size; | ||
19 | unsigned int max_entries; | ||
20 | }; | ||
21 | |||
22 | #define SEC(NAME) __attribute__((section(NAME), used)) | ||
23 | struct bpf_map_def SEC("maps") flip_table = { | ||
24 | .type = BPF_MAP_TYPE_ARRAY, | ||
25 | .key_size = sizeof(int), | ||
26 | .value_size = sizeof(int), | ||
27 | .max_entries = 1, | ||
28 | }; | ||
29 | |||
30 | SEC("func=sys_epoll_pwait") | ||
31 | int bpf_func__sys_epoll_pwait(void *ctx) | ||
32 | { | ||
33 | int ind =0; | ||
34 | int *flag = bpf_map_lookup_elem(&flip_table, &ind); | ||
35 | int new_flag; | ||
36 | if (!flag) | ||
37 | return 0; | ||
38 | /* flip flag and store back */ | ||
39 | new_flag = !*flag; | ||
40 | bpf_map_update_elem(&flip_table, &ind, &new_flag, BPF_ANY); | ||
41 | return new_flag; | ||
42 | } | ||
43 | char _license[] SEC("license") = "GPL"; | ||
44 | int _version SEC("version") = LINUX_VERSION_CODE; | ||