Return-Path: Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1752703AbbH0Kmr (ORCPT ); Thu, 27 Aug 2015 06:42:47 -0400 Received: from szxga01-in.huawei.com ([58.251.152.64]:35833 "EHLO szxga01-in.huawei.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1751936AbbH0Kmp (ORCPT ); Thu, 27 Aug 2015 06:42:45 -0400 From: Kaixu Xia To: , , , , , , , CC: , , , Subject: [RFC PATCH 3/4] bpf tools: Save the perf event fds from "maps" sections to 'struct bpf_object' Date: Thu, 27 Aug 2015 10:42:21 +0000 Message-ID: <1440672142-89311-4-git-send-email-xiakaixu@huawei.com> X-Mailer: git-send-email 1.8.3.4 In-Reply-To: <1440672142-89311-1-git-send-email-xiakaixu@huawei.com> References: <1440672142-89311-1-git-send-email-xiakaixu@huawei.com> MIME-Version: 1.0 Content-Type: text/plain X-Originating-IP: [10.107.193.250] X-CFilter-Loop: Reflected Sender: linux-kernel-owner@vger.kernel.org List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Length: 2527 Lines: 84 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 --- 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@vger.kernel.org More majordomo info at http://vger.kernel.org/majordomo-info.html Please read the FAQ at http://www.tux.org/lkml/