[RFC PATCH 3/4] bpf tools: Save the perf event fds from "maps" sections to 'struct bpf_object'
From: Kaixu Xia
Date: Thu Aug 27 2015 - 06:42:50 EST
This patch saves the perf_event fds from "maps" sections to struct
bpf_object. So we can enable/disable these perf events at the
appropriate time.
Signed-off-by: Kaixu Xia <xiakaixu@xxxxxxxxxx>
---
tools/lib/bpf/libbpf.c | 28 ++++++++++++++++++++++++++--
1 file changed, 26 insertions(+), 2 deletions(-)
diff --git a/tools/lib/bpf/libbpf.c b/tools/lib/bpf/libbpf.c
index 83d79c4..2b3940e 100644
--- a/tools/lib/bpf/libbpf.c
+++ b/tools/lib/bpf/libbpf.c
@@ -126,6 +126,8 @@ struct bpf_object {
*/
size_t nr_map_fds;
bool loaded;
+ int *bpf_event_fds;
+ int nr_bpf_event_fds;
/*
* Information when doing elf related work. Only valid if fd
@@ -629,7 +631,26 @@ bpf_program__collect_reloc(struct bpf_program *prog,
}
static int
-bpf_object__collect_perf_event_maps(void *data, int **pfd)
+bpf_object__save_event_bpf_fd(struct bpf_object *obj, int event_fd)
+{
+ void *bpf_event = obj->bpf_event_fds;
+ int nr_bpf_event = obj->nr_bpf_event_fds;
+
+ bpf_event = realloc(bpf_event, sizeof(int)*(nr_bpf_event + 1));
+
+ if (!bpf_event) {
+ pr_warning("realloc failed\n");
+ return -ENOMEM;
+ }
+
+ obj->bpf_event_fds = bpf_event;
+ obj->nr_bpf_event_fds = nr_bpf_event + 1;
+ obj->bpf_event_fds[nr_bpf_event] = event_fd;
+ return 0;
+}
+
+static int
+bpf_object__collect_perf_event_maps(struct bpf_object *obj, void *data, int **pfd)
{
int i, event_fd;
int maps_fd = **pfd;
@@ -655,6 +676,8 @@ bpf_object__collect_perf_event_maps(void *data, int **pfd)
return -EINVAL;
}
bpf_update_elem(maps_fd, &i, &event_fd, BPF_ANY);
+ if (bpf_object__save_event_bpf_fd(obj, event_fd))
+ return -ENOMEM;
}
return 0;
}
@@ -705,7 +728,7 @@ bpf_object__create_maps(struct bpf_object *obj)
if (def.type == BPF_MAP_TYPE_PERF_EVENT_ARRAY) {
void *data = obj->maps_buf + i * sizeof(struct bpf_map_def);
- err = bpf_object__collect_perf_event_maps(data, &pfd);
+ err = bpf_object__collect_perf_event_maps(obj, data, &pfd);
if (err < 0) {
pr_warning("failed to collect perf_event maps: %s\n",
strerror(errno));
@@ -1074,6 +1097,7 @@ void bpf_object__close(struct bpf_object *obj)
bpf_program__exit(&obj->programs[i]);
}
zfree(&obj->programs);
+ zfree(&obj->bpf_event_fds);
list_del(&obj->list);
free(obj);
--
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/