[RFC PATCH v8 43/49] perf tools: Iterater over tev instead of pev in bpf__for_each_program

From: Wang Nan
Date: Wed Jun 24 2015 - 08:34:17 EST


This patch renames bpf__for_each_program() to bpf__for_each_tev() and
makes it iterate over 'struct probe_trace_event' instead of
'struct probe_trace_event' during the loop. The callback (add_bpf_event())
now get rid of the iteration.

This is preparation for further patches, which will allow generating
multiple instances form one BPF program and install then onto different
'struct probe_trace_event'.

Signed-off-by: Wang Nan <wangnan0@xxxxxxxxxx>
---
tools/perf/util/bpf-loader.c | 31 +++++++++++++++++++------------
tools/perf/util/bpf-loader.h | 8 ++++----
tools/perf/util/evlist.c | 42 ++++++++++++++++--------------------------
3 files changed, 39 insertions(+), 42 deletions(-)

diff --git a/tools/perf/util/bpf-loader.c b/tools/perf/util/bpf-loader.c
index 2936adf..8812e24 100644
--- a/tools/perf/util/bpf-loader.c
+++ b/tools/perf/util/bpf-loader.c
@@ -266,8 +266,7 @@ errout:
}

int
-bpf__for_each_program(bpf_prog_iter_callback_t func,
- void *arg)
+bpf__for_each_tev(bpf_prog_iter_callback_t func, void *arg)
{
struct bpf_object *obj, *tmp;
struct bpf_program *prog;
@@ -275,8 +274,10 @@ bpf__for_each_program(bpf_prog_iter_callback_t func,

bpf_object__for_each(obj, tmp) {
bpf_object__for_each_program(prog, obj) {
+ struct probe_trace_event *tev;
+ struct perf_probe_event *pev;
struct bpf_prog_priv *priv;
- int fd;
+ int i, fd;

err = bpf_program__get_private(prog,
(void **)&priv);
@@ -284,16 +285,22 @@ bpf__for_each_program(bpf_prog_iter_callback_t func,
pr_err("bpf: failed to get private field\n");
return -EINVAL;
}
- err = bpf_program__get_fd(prog, &fd);
- if (err || fd < 0) {
- pr_err("bpf: failed to get file descriptor\n");
- return -EINVAL;
- }

- err = func(priv->pev, fd, arg);
- if (err) {
- pr_err("bpf: call back failed, stop iterate\n");
- return err;
+ pev = priv->pev;
+ for (i = 0; i < pev->ntevs; i++) {
+ tev = &pev->tevs[i];
+
+ err = bpf_program__get_fd(prog, &fd);
+
+ if (err || fd < 0) {
+ pr_err("bpf: failed to get file descriptor\n");
+ return -EINVAL;
+ }
+ err = func(tev, fd, arg);
+ if (err) {
+ pr_err("bpf: call back failed, stop iterate\n");
+ return err;
+ }
}
}
}
diff --git a/tools/perf/util/bpf-loader.h b/tools/perf/util/bpf-loader.h
index da03e12..83697e0 100644
--- a/tools/perf/util/bpf-loader.h
+++ b/tools/perf/util/bpf-loader.h
@@ -11,7 +11,7 @@

#define PERF_BPF_PROBE_GROUP "perf_bpf_probe"

-typedef int (*bpf_prog_iter_callback_t)(struct perf_probe_event *pev,
+typedef int (*bpf_prog_iter_callback_t)(struct probe_trace_event *tev,
int fd, void *arg);

#ifdef HAVE_LIBBPF_SUPPORT
@@ -22,7 +22,7 @@ int bpf__load(void);

void bpf__clear(void);

-int bpf__for_each_program(bpf_prog_iter_callback_t func, void *arg);
+int bpf__for_each_tev(bpf_prog_iter_callback_t func, void *arg);
#else
static inline int bpf__prepare_load(const char *filename __maybe_unused,
bool source __maybe_unused)
@@ -37,8 +37,8 @@ static inline int bpf__load(void) { return 0; }
static inline void bpf__clear(void) { }

static inline int
-bpf__for_each_program(bpf_prog_iter_callback_t func __maybe_unused,
- void *arg __maybe_unused)
+bpf__for_each_tev(bpf_prog_iter_callback_t func __maybe_unused,
+ void *arg __maybe_unused)
{
return 0;
}
diff --git a/tools/perf/util/evlist.c b/tools/perf/util/evlist.c
index 4022bd1..e50aed3 100644
--- a/tools/perf/util/evlist.c
+++ b/tools/perf/util/evlist.c
@@ -195,42 +195,32 @@ error:
return -ENOMEM;
}

-static int add_bpf_event(struct perf_probe_event *pev, int fd,
+static int add_bpf_event(struct probe_trace_event *tev, int fd,
void *arg)
{
struct perf_evlist *evlist = arg;
struct perf_evsel *pos;
struct list_head list;
- int err, idx, entries, i;
-
- if (!pev || !pev->ntevs) {
- pr_err("Internal error: add bpf event before probing\n");
- return -EINVAL;
- }
+ int err, idx, entries;

pr_debug("add bpf event %s:%s and attach bpf program %d\n",
- pev->group, pev->event, fd);
+ tev->group, tev->event, fd);
INIT_LIST_HEAD(&list);
idx = evlist->nr_entries;

- for (i = 0; i < pev->ntevs; i++) {
- struct probe_trace_event *tev = &pev->tevs[i];
-
- pr_debug("adding %s:%s\n", tev->group, tev->event);
- err = parse_events_add_tracepoint(&list, &idx,
- tev->group,
- tev->event);
- if (err) {
- struct perf_evsel *evsel, *tmp;
-
- pr_err("Failed to add BPF event %s:%s\n",
- tev->group, tev->event);
- list_for_each_entry_safe(evsel, tmp, &list, node) {
- list_del(&evsel->node);
- perf_evsel__delete(evsel);
- }
- return -EINVAL;
+ pr_debug("adding %s:%s\n", tev->group, tev->event);
+ err = parse_events_add_tracepoint(&list, &idx, tev->group,
+ tev->event);
+ if (err) {
+ struct perf_evsel *evsel, *tmp;
+
+ pr_err("Failed to add BPF event %s:%s\n",
+ tev->group, tev->event);
+ list_for_each_entry_safe(evsel, tmp, &list, node) {
+ list_del(&evsel->node);
+ perf_evsel__delete(evsel);
}
+ return -EINVAL;
}

list_for_each_entry(pos, &list, node)
@@ -242,7 +232,7 @@ static int add_bpf_event(struct perf_probe_event *pev, int fd,

int perf_evlist__add_bpf(struct perf_evlist *evlist)
{
- return bpf__for_each_program(add_bpf_event, evlist);
+ return bpf__for_each_tev(add_bpf_event, evlist);
}

static int perf_evlist__add_attrs(struct perf_evlist *evlist,
--
1.8.3.4

--
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in
the body of a message to majordomo@xxxxxxxxxxxxxxx
More majordomo info at http://vger.kernel.org/majordomo-info.html
Please read the FAQ at http://www.tux.org/lkml/