perf tools: Compile scriptlets to BPF objects when passing '.c' to --event
authorWang Nan <wangnan0@huawei.com>
Wed, 14 Oct 2015 12:41:20 +0000 (12:41 +0000)
committerArnaldo Carvalho de Melo <acme@redhat.com>
Thu, 29 Oct 2015 20:16:23 +0000 (17:16 -0300)
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>
tools/perf/tests/bpf-script-example.c [new file with mode: 0644]
tools/perf/util/bpf-loader.c
tools/perf/util/bpf-loader.h
tools/perf/util/parse-events.c
tools/perf/util/parse-events.h
tools/perf/util/parse-events.l
tools/perf/util/parse-events.y

diff --git a/tools/perf/tests/bpf-script-example.c b/tools/perf/tests/bpf-script-example.c
new file mode 100644 (file)
index 0000000..410a70b
--- /dev/null
@@ -0,0 +1,44 @@
+#ifndef LINUX_VERSION_CODE
+# error Need LINUX_VERSION_CODE
+# error Example: for 4.2 kernel, put 'clang-opt="-DLINUX_VERSION_CODE=0x40200" into llvm section of ~/.perfconfig'
+#endif
+#define 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;
+       unsigned int key_size;
+       unsigned int value_size;
+       unsigned int max_entries;
+};
+
+#define SEC(NAME) __attribute__((section(NAME), used))
+struct bpf_map_def SEC("maps") flip_table = {
+       .type = BPF_MAP_TYPE_ARRAY,
+       .key_size = sizeof(int),
+       .value_size = sizeof(int),
+       .max_entries = 1,
+};
+
+SEC("func=sys_epoll_pwait")
+int bpf_func__sys_epoll_pwait(void *ctx)
+{
+       int ind =0;
+       int *flag = bpf_map_lookup_elem(&flip_table, &ind);
+       int new_flag;
+       if (!flag)
+               return 0;
+       /* flip flag and store back */
+       new_flag = !*flag;
+       bpf_map_update_elem(&flip_table, &ind, &new_flag, BPF_ANY);
+       return new_flag;
+}
+char _license[] SEC("license") = "GPL";
+int _version SEC("version") = LINUX_VERSION_CODE;
index aa784a498c4838c2a70356f7e3902fc19bc59a2a..ba6f7526b282805c62337479926256949fe09bfc 100644 (file)
@@ -12,6 +12,7 @@
 #include "bpf-loader.h"
 #include "probe-event.h"
 #include "probe-finder.h" // for MAX_PROBES
+#include "llvm-utils.h"
 
 #define DEFINE_PRINT_FN(name, level) \
 static int libbpf_##name(const char *fmt, ...) \
@@ -33,7 +34,7 @@ struct bpf_prog_priv {
        struct perf_probe_event pev;
 };
 
-struct bpf_object *bpf__prepare_load(const char *filename)
+struct bpf_object *bpf__prepare_load(const char *filename, bool source)
 {
        struct bpf_object *obj;
        static bool libbpf_initialized;
@@ -45,7 +46,19 @@ struct bpf_object *bpf__prepare_load(const char *filename)
                libbpf_initialized = true;
        }
 
-       obj = bpf_object__open(filename);
+       if (source) {
+               int err;
+               void *obj_buf;
+               size_t obj_buf_sz;
+
+               err = llvm__compile_bpf(filename, &obj_buf, &obj_buf_sz);
+               if (err)
+                       return ERR_PTR(err);
+               obj = bpf_object__open_buffer(obj_buf, obj_buf_sz, filename);
+               free(obj_buf);
+       } else
+               obj = bpf_object__open(filename);
+
        if (!obj) {
                pr_debug("bpf: failed to load %s\n", filename);
                return ERR_PTR(-EINVAL);
index a8f25ee06fc5916593fbc824d2e6cece533f3043..ccd8d7fd79d3443bde634534580883a19adde53e 100644 (file)
@@ -18,7 +18,7 @@ typedef int (*bpf_prog_iter_callback_t)(struct probe_trace_event *tev,
                                        int fd, void *arg);
 
 #ifdef HAVE_LIBBPF_SUPPORT
-struct bpf_object *bpf__prepare_load(const char *filename);
+struct bpf_object *bpf__prepare_load(const char *filename, bool source);
 
 void bpf__clear(void);
 
@@ -34,7 +34,8 @@ int bpf__foreach_tev(struct bpf_object *obj,
                     bpf_prog_iter_callback_t func, void *arg);
 #else
 static inline struct bpf_object *
-bpf__prepare_load(const char *filename __maybe_unused)
+bpf__prepare_load(const char *filename __maybe_unused,
+                 bool source __maybe_unused)
 {
        pr_debug("ERROR: eBPF object loading is disabled during compiling.\n");
        return ERR_PTR(-ENOTSUP);
index cee8c619ec7e8fa5c9dfffc4b8c8244ed649fd22..bee60583839a7cdae0dd44f5c48dd818d53cb5e4 100644 (file)
@@ -626,11 +626,12 @@ errout:
 
 int parse_events_load_bpf(struct parse_events_evlist *data,
                          struct list_head *list,
-                         char *bpf_file_name)
+                         char *bpf_file_name,
+                         bool source)
 {
        struct bpf_object *obj;
 
-       obj = bpf__prepare_load(bpf_file_name);
+       obj = bpf__prepare_load(bpf_file_name, source);
        if (IS_ERR(obj) || !obj) {
                char errbuf[BUFSIZ];
                int err;
index 765018a17448a8a0f0131997d2c51c717fabf9c6..f1a6db107241b1c8ffaf03a3514ba1549df6fd1b 100644 (file)
@@ -125,7 +125,8 @@ int parse_events_add_tracepoint(struct list_head *list, int *idx,
                                struct list_head *head_config);
 int parse_events_load_bpf(struct parse_events_evlist *data,
                          struct list_head *list,
-                         char *bpf_file_name);
+                         char *bpf_file_name,
+                         bool source);
 /* Provide this function for perf test */
 struct bpf_object;
 int parse_events_load_bpf_obj(struct parse_events_evlist *data,
index cf330ebf812cb02f4270fd6fdadefa0590b13eab..58c5831ffd5c22133f48a4c1a3a07721c71362fa 100644 (file)
@@ -116,6 +116,7 @@ group               [^,{}/]*[{][^}]*[}][^,{}/]*
 event_pmu      [^,{}/]+[/][^/]*[/][^,{}/]*
 event          [^,{}/]+
 bpf_object     .*\.(o|bpf)
+bpf_source     .*\.c
 
 num_dec                [0-9]+
 num_hex                0x[a-fA-F0-9]+
@@ -161,6 +162,7 @@ modifier_bp [rwx]{1,3}
 
 {event_pmu}    |
 {bpf_object}   |
+{bpf_source}   |
 {event}                {
                        BEGIN(INITIAL);
                        REWIND(1);
@@ -269,6 +271,7 @@ r{num_raw_hex}              { return raw(yyscanner); }
 
 {modifier_event}       { return str(yyscanner, PE_MODIFIER_EVENT); }
 {bpf_object}           { return str(yyscanner, PE_BPF_OBJECT); }
+{bpf_source}           { return str(yyscanner, PE_BPF_SOURCE); }
 {name}                 { return pmu_str_check(yyscanner); }
 "/"                    { BEGIN(config); return '/'; }
 -                      { return '-'; }
index 497f19b20f0b9c91577bb8ae1861203e0d68bcee..ad379968d4c10c0fb7bb2ddc3bacce3dc1166f43 100644 (file)
@@ -42,7 +42,7 @@ static inc_group_count(struct list_head *list,
 %token PE_VALUE PE_VALUE_SYM_HW PE_VALUE_SYM_SW PE_RAW PE_TERM
 %token PE_EVENT_NAME
 %token PE_NAME
-%token PE_BPF_OBJECT
+%token PE_BPF_OBJECT PE_BPF_SOURCE
 %token PE_MODIFIER_EVENT PE_MODIFIER_BP
 %token PE_NAME_CACHE_TYPE PE_NAME_CACHE_OP_RESULT
 %token PE_PREFIX_MEM PE_PREFIX_RAW PE_PREFIX_GROUP
@@ -55,6 +55,7 @@ static inc_group_count(struct list_head *list,
 %type <num> PE_TERM
 %type <str> PE_NAME
 %type <str> PE_BPF_OBJECT
+%type <str> PE_BPF_SOURCE
 %type <str> PE_NAME_CACHE_TYPE
 %type <str> PE_NAME_CACHE_OP_RESULT
 %type <str> PE_MODIFIER_EVENT
@@ -461,7 +462,17 @@ PE_BPF_OBJECT
        struct list_head *list;
 
        ALLOC_LIST(list);
-       ABORT_ON(parse_events_load_bpf(data, list, $1));
+       ABORT_ON(parse_events_load_bpf(data, list, $1, false));
+       $$ = list;
+}
+|
+PE_BPF_SOURCE
+{
+       struct parse_events_evlist *data = _data;
+       struct list_head *list;
+
+       ALLOC_LIST(list);
+       ABORT_ON(parse_events_load_bpf(data, list, $1, true));
        $$ = list;
 }