All of lore.kernel.org
 help / color / mirror / Atom feed
From: Wang Nan <wangnan0@huawei.com>
To: <acme@kernel.org>, <ast@plumgrid.com>,
	<brendan.d.gregg@gmail.com>, <daniel@iogearbox.net>,
	<namhyung@kernel.org>, <masami.hiramatsu.pt@hitachi.com>,
	<paulus@samba.org>, <a.p.zijlstra@chello.nl>, <mingo@redhat.com>,
	<jolsa@kernel.org>, <dsahern@gmail.com>
Cc: <linux-kernel@vger.kernel.org>, <lizefan@huawei.com>,
	<hekuang@huawei.com>, <xiakaixu@huawei.com>, <pi3orama@163.com>
Subject: [RFC PATCH v6 23/32] perf record: Enable passing bpf object file to --event
Date: Tue, 9 Jun 2015 05:50:27 +0000	[thread overview]
Message-ID: <1433829036-23687-24-git-send-email-wangnan0@huawei.com> (raw)
In-Reply-To: <1433829036-23687-1-git-send-email-wangnan0@huawei.com>

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.

'bpf-loader.[ch]' are introduced in this patch. Which will be the
interface between perf and libbpf. bpf__prepare_load() resides in
bpf-loader.c. Dummy functions should be used because bpf-loader.c is
available only when CONFIG_LIBBPF is on.

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   | 24 +++++++++++++++++
 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 |  3 +++
 tools/perf/util/parse-events.y | 18 ++++++++++++-
 9 files changed, 129 insertions(+), 1 deletion(-)
 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 e4b676d..1e1025f 100644
--- a/tools/perf/util/Build
+++ b/tools/perf/util/Build
@@ -77,6 +77,7 @@ libperf-y += thread-stack.o
 libperf-$(CONFIG_AUXTRACE) += auxtrace.o
 libperf-y += parse-branch-options.o
 
+libperf-$(CONFIG_LIBBPF) += 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..e950baa
--- /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..39d8d1a
--- /dev/null
+++ b/tools/perf/util/bpf-loader.h
@@ -0,0 +1,24 @@
+/*
+ * Copyright (C) 2015, Wang Nan <wangnan0@huawei.com>
+ * Copyright (C) 2015, Huawei Inc.
+ */
+#ifndef __BPF_LOADER_H
+#define __BPF_LOADER_H
+
+#include <linux/compiler.h>
+#include "debug.h"
+
+#ifdef HAVE_LIBBPF_SUPPORT
+int bpf__prepare_load(const char *filename);
+
+void bpf__clear(void);
+#else
+static inline int bpf__prepare_load(const char *filename __maybe_unused)
+{
+	pr_err("ERROR: eBPF object loading is disabled during compiling.\n");
+	return -1;
+}
+
+static inline void bpf__clear(void) { }
+#endif
+#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 2a4d1ec..6f7a7b1 100644
--- a/tools/perf/util/parse-events.c
+++ b/tools/perf/util/parse-events.c
@@ -18,6 +18,7 @@
 #include "pmu.h"
 #include "thread_map.h"
 #include "asm/bug.h"
+#include "bpf-loader.h"
 
 #define MAX_NAME_LEN 100
 
@@ -472,6 +473,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 131f29b..41b962a 100644
--- a/tools/perf/util/parse-events.h
+++ b/tools/perf/util/parse-events.h
@@ -114,6 +114,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 parse_events_evlist *data,
 			     struct list_head *list,
 			     u32 type, u64 config,
diff --git a/tools/perf/util/parse-events.l b/tools/perf/util/parse-events.l
index 09e738f..4acea38 100644
--- a/tools/perf/util/parse-events.l
+++ b/tools/perf/util/parse-events.l
@@ -115,6 +115,7 @@ do {							\
 group		[^,{}/]*[{][^}]*[}][^,{}/]*
 event_pmu	[^,{}/]+[/][^/]*[/][^,{}/]*
 event		[^,{}/]+
+bpf_object	.*\.(o|bpf)
 
 num_dec		[0-9]+
 num_hex		0x[a-fA-F0-9]+
@@ -159,6 +160,7 @@ modifier_bp	[rwx]{1,3}
 		}
 
 {event_pmu}	|
+{bpf_object}	|
 {event}		{
 			BEGIN(INITIAL);
 			REWIND(1);
@@ -262,6 +264,7 @@ r{num_raw_hex}		{ return raw(yyscanner); }
 
 {modifier_event}	{ return str(yyscanner, PE_MODIFIER_EVENT); }
 {name}			{ return pmu_str_check(yyscanner); }
+{bpf_object}		{ return str(yyscanner, PE_BPF_OBJECT); }
 "/"			{ BEGIN(config); return '/'; }
 -			{ return '-'; }
 ,			{ BEGIN(event); return ','; }
diff --git a/tools/perf/util/parse-events.y b/tools/perf/util/parse-events.y
index 591905a..481f3cd 100644
--- a/tools/perf/util/parse-events.y
+++ b/tools/perf/util/parse-events.y
@@ -42,6 +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_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
@@ -53,6 +54,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_OBJECT
 %type <str> PE_NAME_CACHE_TYPE
 %type <str> PE_NAME_CACHE_OP_RESULT
 %type <str> PE_MODIFIER_EVENT
@@ -69,6 +71,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
@@ -198,7 +201,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 '/'
@@ -420,6 +424,18 @@ PE_RAW
 	$$ = list;
 }
 
+event_bpf_file:
+PE_BPF_OBJECT
+{
+	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


  parent reply	other threads:[~2015-06-09  5:55 UTC|newest]

Thread overview: 58+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2015-06-09  5:50 [RFC PATCH v6 00/32] perf tools: filtering events using eBPF programs Wang Nan
2015-06-09  5:50 ` [RFC PATCH v6 01/32] tools build: Add feature check for eBPF API Wang Nan
2015-06-09  5:50 ` [RFC PATCH v6 02/32] bpf tools: Introduce 'bpf' library to tools Wang Nan
2015-06-09  5:50 ` [RFC PATCH v6 03/32] bpf tools: Allow caller to set printing function Wang Nan
2015-06-09  5:50 ` [RFC PATCH v6 04/32] bpf tools: Open eBPF object file and do basic validation Wang Nan
2015-06-09  5:50 ` [RFC PATCH v6 05/32] bpf tools: Read eBPF object from buffer Wang Nan
2015-06-09  5:50 ` [RFC PATCH v6 06/32] bpf tools: Check endianess and make libbpf fail early Wang Nan
2015-06-09  5:50 ` [RFC PATCH v6 07/32] bpf tools: Iterate over ELF sections to collect information Wang Nan
2015-06-09  5:50 ` [RFC PATCH v6 08/32] bpf tools: Collect version and license from ELF sections Wang Nan
2015-06-09  5:50 ` [RFC PATCH v6 09/32] bpf tools: Collect map definitions from 'maps' section Wang Nan
2015-06-09  5:50 ` [RFC PATCH v6 10/32] bpf tools: Collect symbol table from SHT_SYMTAB section Wang Nan
2015-06-09  5:50 ` [RFC PATCH v6 11/32] bpf tools: Collect eBPF programs from their own sections Wang Nan
2015-06-09  5:50 ` [RFC PATCH v6 12/32] bpf tools: Collect relocation sections from SHT_REL sections Wang Nan
2015-06-09  5:50 ` [RFC PATCH v6 13/32] bpf tools: Record map accessing instructions for each program Wang Nan
2015-06-09  5:50 ` [RFC PATCH v6 14/32] bpf tools: Add bpf.c/h for common bpf operations Wang Nan
2015-06-09  5:50 ` [RFC PATCH v6 15/32] bpf tools: Create eBPF maps defined in an object file Wang Nan
2015-06-09  5:50 ` [RFC PATCH v6 16/32] bpf tools: Relocate eBPF programs Wang Nan
2015-06-09  5:50 ` [RFC PATCH v6 17/32] bpf tools: Introduce bpf_load_program() to bpf.c Wang Nan
2015-06-09  5:50 ` [RFC PATCH v6 18/32] bpf tools: Load eBPF programs in object files into kernel Wang Nan
2015-06-09  5:50 ` [RFC PATCH v6 19/32] bpf tools: Introduce accessors for struct bpf_program Wang Nan
2015-06-09  5:50 ` [RFC PATCH v6 20/32] bpf tools: Introduce accessors for struct bpf_object Wang Nan
2015-06-09  5:50 ` [RFC PATCH v6 21/32] bpf tools: Link all bpf objects onto a list Wang Nan
2015-06-09  5:50 ` [RFC PATCH v6 22/32] perf tools: Make perf depend on libbpf Wang Nan
2015-06-09 10:29   ` Wangnan (F)
2015-06-09  5:50 ` Wang Nan [this message]
2015-06-09  5:50 ` [RFC PATCH v6 24/32] perf record: Compile scriptlets if pass '.c' to --event Wang Nan
2015-06-09 21:48   ` Alexei Starovoitov
2015-06-10  0:06     ` Wangnan (F)
2015-06-10  0:28       ` Alexei Starovoitov
2015-06-11  7:19       ` Namhyung Kim
2015-06-11  7:35         ` Wangnan (F)
2015-06-11 17:42           ` Alexei Starovoitov
2015-06-09  5:50 ` [RFC PATCH v6 25/32] perf tools: Add 'bpf.' config section to perf default config Wang Nan
2015-06-09 23:43   ` Alexei Starovoitov
2015-06-10  0:47     ` Wangnan (F)
2015-06-10  1:09       ` Alexei Starovoitov
2015-06-10  2:23         ` Wangnan (F)
2015-06-10  3:55           ` Alexei Starovoitov
2015-06-11  7:45     ` Namhyung Kim
2015-06-11  8:09       ` Wangnan (F)
2015-06-11 14:50         ` Namhyung Kim
2015-06-09  5:50 ` [RFC PATCH v6 26/32] perf tools: Parse probe points of eBPF programs during preparation Wang Nan
2015-06-09  5:50 ` [RFC PATCH v6 27/32] perf probe: Attach trace_probe_event with perf_probe_event Wang Nan
2015-06-09  5:50 ` [RFC PATCH v6 28/32] perf record: Probe at kprobe points Wang Nan
2015-06-11 14:32   ` Namhyung Kim
2015-06-09  5:50 ` [RFC PATCH v6 29/32] perf record: Load all eBPF object into kernel Wang Nan
2015-06-09  5:50 ` [RFC PATCH v6 30/32] perf tools: Add bpf_fd field to evsel and config it Wang Nan
2015-06-09  5:50 ` [RFC PATCH v6 31/32] perf tools: Attach eBPF program to perf event Wang Nan
2015-06-09  5:50 ` [RFC PATCH v6 32/32] perf record: Add LLVM options for compiling BPF scripts Wang Nan
2015-06-09 13:32   ` Wangnan (F)
2015-06-10  0:02   ` Alexei Starovoitov
2015-06-10  0:17     ` Wangnan (F)
2015-06-10  1:34       ` Alexei Starovoitov
2015-06-09 13:59 ` [RFC PATCH v6 00/32] perf tools: filtering events using eBPF programs Arnaldo Carvalho de Melo
2015-06-09 23:45   ` Wangnan (F)
2015-06-09 21:30 ` Alexei Starovoitov
2015-06-09 21:44   ` Arnaldo Carvalho de Melo
2015-06-09 23:16     ` Alexei Starovoitov

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=1433829036-23687-24-git-send-email-wangnan0@huawei.com \
    --to=wangnan0@huawei.com \
    --cc=a.p.zijlstra@chello.nl \
    --cc=acme@kernel.org \
    --cc=ast@plumgrid.com \
    --cc=brendan.d.gregg@gmail.com \
    --cc=daniel@iogearbox.net \
    --cc=dsahern@gmail.com \
    --cc=hekuang@huawei.com \
    --cc=jolsa@kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=lizefan@huawei.com \
    --cc=masami.hiramatsu.pt@hitachi.com \
    --cc=mingo@redhat.com \
    --cc=namhyung@kernel.org \
    --cc=paulus@samba.org \
    --cc=pi3orama@163.com \
    --cc=xiakaixu@huawei.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.