Messages in this thread Patch in this message | | | From | Wang Nan <> | Subject | [RFC PATCH v4 24/29] perf record: Enable passing bpf object file to --event | Date | Wed, 27 May 2015 05:19:59 +0000 |
| |
By introducing new rules in tools/perf/util/parse-events.[ly], this patch enables 'perf record --event bpf_file.o' to select events by an eBPF object file. It calls parse_events_load_bpf() to load that file, which uses bpf__prepare_load() and finally calls bpf_object__open() for the object files.
Instead of introducing evsel to evlist during parsing, events selected by eBPF object files are appended separately. The reason is:
1. During parsing, the probing points have not been initialized.
2. Currently we are unable to call add_perf_probe_events() twice, therefore we have to wait until all such events are collected, then probe all points by one call.
The real probing and selecting is reside in following patches.
Signed-off-by: Wang Nan <wangnan0@huawei.com> --- tools/perf/util/Build | 1 + tools/perf/util/bpf-loader.c | 60 ++++++++++++++++++++++++++++++++++++++++++ tools/perf/util/bpf-loader.h | 11 ++++++++ tools/perf/util/debug.c | 5 ++++ tools/perf/util/debug.h | 1 + tools/perf/util/parse-events.c | 16 +++++++++++ tools/perf/util/parse-events.h | 2 ++ tools/perf/util/parse-events.l | 5 +++- tools/perf/util/parse-events.y | 18 ++++++++++++- 9 files changed, 117 insertions(+), 2 deletions(-) create mode 100644 tools/perf/util/bpf-loader.c create mode 100644 tools/perf/util/bpf-loader.h
diff --git a/tools/perf/util/Build b/tools/perf/util/Build index 797490a..609f6d6 100644 --- a/tools/perf/util/Build +++ b/tools/perf/util/Build @@ -75,6 +75,7 @@ libperf-$(CONFIG_X86) += tsc.o libperf-y += cloexec.o libperf-y += thread-stack.o +libperf-$(CONFIG_LIBELF) += bpf-loader.o libperf-$(CONFIG_LIBELF) += symbol-elf.o libperf-$(CONFIG_LIBELF) += probe-event.o diff --git a/tools/perf/util/bpf-loader.c b/tools/perf/util/bpf-loader.c new file mode 100644 index 0000000..eef15f3 --- /dev/null +++ b/tools/perf/util/bpf-loader.c @@ -0,0 +1,60 @@ +/* + * bpf-loader.c + * + * Copyright (C) 2015 Wang Nan <wangnan0@huawei.com> + * Copyright (C) 2015 Huawei Inc. + */ + +#include <bpf/libbpf.h> +#include "perf.h" +#include "debug.h" +#include "bpf-loader.h" + +#define DEFINE_PRINT_FN(name, level) \ +static int libbpf_##name(const char *fmt, ...) \ +{ \ + va_list args; \ + int ret; \ + \ + va_start(args, fmt); \ + ret = veprintf(level, verbose, pr_fmt(fmt), args);\ + va_end(args); \ + return ret; \ +} + +DEFINE_PRINT_FN(warning, 0) +DEFINE_PRINT_FN(info, 0) +DEFINE_PRINT_FN(debug, 1) + +static bool libbpf_initialized = false; + +int bpf__prepare_load(const char *filename) +{ + struct bpf_object *obj; + + if (!libbpf_initialized) + libbpf_set_print(libbpf_warning, + libbpf_info, + libbpf_debug); + + obj = bpf_object__open(filename); + if (!obj) { + pr_err("bpf: failed to load %s\n", filename); + return -EINVAL; + } + + /* + * Throw object pointer away: it will be retrived using + * bpf_objects iterater. + */ + + return 0; +} + +void bpf__clear(void) +{ + struct bpf_object *obj, *tmp; + + bpf_object__for_each(obj, tmp) + bpf_object__close(obj); +} diff --git a/tools/perf/util/bpf-loader.h b/tools/perf/util/bpf-loader.h new file mode 100644 index 0000000..d5c22da --- /dev/null +++ b/tools/perf/util/bpf-loader.h @@ -0,0 +1,11 @@ +/* + * Copyright (C) 2015, Wang Nan <wangnan0@huawei.com> + * Copyright (C) 2015, Huawei Inc. + */ +#ifndef __BPF_LOADER_H +#define __BPF_LOADER_H + +int bpf__prepare_load(const char *filename); + +void bpf__clear(void); +#endif diff --git a/tools/perf/util/debug.c b/tools/perf/util/debug.c index 2da5581..86d9c73 100644 --- a/tools/perf/util/debug.c +++ b/tools/perf/util/debug.c @@ -36,6 +36,11 @@ static int _eprintf(int level, int var, const char *fmt, va_list args) return ret; } +int veprintf(int level, int var, const char *fmt, va_list args) +{ + return _eprintf(level, var, fmt, args); +} + int eprintf(int level, int var, const char *fmt, ...) { va_list args; diff --git a/tools/perf/util/debug.h b/tools/perf/util/debug.h index caac2fd..8b9a088 100644 --- a/tools/perf/util/debug.h +++ b/tools/perf/util/debug.h @@ -50,6 +50,7 @@ void pr_stat(const char *fmt, ...); int eprintf(int level, int var, const char *fmt, ...) __attribute__((format(printf, 3, 4))); int eprintf_time(int level, int var, u64 t, const char *fmt, ...) __attribute__((format(printf, 4, 5))); +int veprintf(int level, int var, const char *fmt, va_list args); int perf_debug_option(const char *str); diff --git a/tools/perf/util/parse-events.c b/tools/perf/util/parse-events.c index be06553..6030ea3 100644 --- a/tools/perf/util/parse-events.c +++ b/tools/perf/util/parse-events.c @@ -17,6 +17,7 @@ #include "parse-events-flex.h" #include "pmu.h" #include "thread_map.h" +#include "bpf-loader.h" #define MAX_NAME_LEN 100 @@ -471,6 +472,21 @@ int parse_events_add_tracepoint(struct list_head *list, int *idx, return add_tracepoint_event(list, idx, sys, event); } +int parse_events_load_bpf(struct list_head *list __maybe_unused, + int *idx __maybe_unused, + char *bpf_file_name) +{ + /* + * Currently don't link any event to list. BPF object files + * should be saved to a seprated list and processed together. + * + * Things could be changed if we solve perf probe reentering + * problem. After that probe events file by file is possible. + * However, probing cost is still need to be considered. + */ + return bpf__prepare_load(bpf_file_name); +} + static int parse_breakpoint_type(const char *type, struct perf_event_attr *attr) { diff --git a/tools/perf/util/parse-events.h b/tools/perf/util/parse-events.h index 52a2dda..a8a0f01 100644 --- a/tools/perf/util/parse-events.h +++ b/tools/perf/util/parse-events.h @@ -99,6 +99,8 @@ int parse_events__modifier_group(struct list_head *list, char *event_mod); int parse_events_name(struct list_head *list, char *name); int parse_events_add_tracepoint(struct list_head *list, int *idx, char *sys, char *event); +int parse_events_load_bpf(struct list_head *list, int *idx, + char *bpf_file_name); int parse_events_add_numeric(struct list_head *list, int *idx, u32 type, u64 config, struct list_head *head_config); diff --git a/tools/perf/util/parse-events.l b/tools/perf/util/parse-events.l index 8895cf3..a35cd4d 100644 --- a/tools/perf/util/parse-events.l +++ b/tools/perf/util/parse-events.l @@ -94,6 +94,7 @@ static int term(yyscan_t scanner, int type) group [^,{}/]*[{][^}]*[}][^,{}/]* event_pmu [^,{}/]+[/][^/]*[/][^,{}/]* event [^,{}/]+ +bpf_file .*\.(o|bpf) num_dec [0-9]+ num_hex 0x[a-fA-F0-9]+ @@ -131,7 +132,8 @@ modifier_bp [rwx]{1,3} } {event_pmu} | -{event} { +{event} | +{bpf_file} { str(yyscanner, PE_EVENT_NAME); BEGIN(INITIAL); yyless(0); return PE_EVENT_NAME; @@ -229,6 +231,7 @@ r{num_raw_hex} { return raw(yyscanner); } {modifier_event} { return str(yyscanner, PE_MODIFIER_EVENT); } {name} { return pmu_str_check(yyscanner); } +{bpf_file} { return str(yyscanner, PE_BPF_FILE); } "/" { BEGIN(config); return '/'; } - { return '-'; } , { BEGIN(event); return ','; } diff --git a/tools/perf/util/parse-events.y b/tools/perf/util/parse-events.y index 72def07..e9c4861 100644 --- a/tools/perf/util/parse-events.y +++ b/tools/perf/util/parse-events.y @@ -43,6 +43,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_FILE %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 @@ -54,6 +55,7 @@ static inc_group_count(struct list_head *list, %type <num> PE_RAW %type <num> PE_TERM %type <str> PE_NAME +%type <str> PE_BPF_FILE %type <str> PE_NAME_CACHE_TYPE %type <str> PE_NAME_CACHE_OP_RESULT %type <str> PE_MODIFIER_EVENT @@ -70,6 +72,7 @@ static inc_group_count(struct list_head *list, %type <head> event_legacy_tracepoint %type <head> event_legacy_numeric %type <head> event_legacy_raw +%type <head> event_bpf_file %type <head> event_def %type <head> event_mod %type <head> event_name @@ -199,7 +202,8 @@ event_def: event_pmu | event_legacy_mem | event_legacy_tracepoint sep_dc | event_legacy_numeric sep_dc | - event_legacy_raw sep_dc + event_legacy_raw sep_dc | + event_bpf_file event_pmu: PE_NAME '/' event_config '/' @@ -416,6 +420,18 @@ PE_RAW $$ = list; } +event_bpf_file: +PE_BPF_FILE +{ + struct parse_events_evlist *data = _data; + struct list_head *list; + + ALLOC_LIST(list); + ABORT_ON(parse_events_load_bpf(list, &data->idx, $1)); + $$ = list; +} + + start_terms: event_config { struct parse_events_terms *data = _data; -- 1.8.3.4
| |