From patchwork Wed Sep 23 11:22:26 2015 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Wang Nan X-Patchwork-Id: 54041 Return-Path: X-Original-To: linaro@patches.linaro.org Delivered-To: linaro@patches.linaro.org Received: from mail-wi0-f197.google.com (mail-wi0-f197.google.com [209.85.212.197]) by patches.linaro.org (Postfix) with ESMTPS id 8EA3322E14 for ; Wed, 23 Sep 2015 11:25:54 +0000 (UTC) Received: by wisv5 with SMTP id v5sf22799373wis.0 for ; Wed, 23 Sep 2015 04:25:53 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20130820; h=x-gm-message-state:delivered-to:from:to:cc:subject:date:message-id :in-reply-to:references:mime-version:content-type:sender:precedence :list-id:x-original-sender:x-original-authentication-results :mailing-list:list-post:list-help:list-archive:list-unsubscribe; bh=SjqSVwhK5Ckiflk9NLF9Umj9hcXguWcoKvgKeLVs1dA=; b=aZZyvzhDpqhPx/On9xgBH3wAfLcCpH/ixVqRCJx8YDNTTqm5bVfCZjsxkZi0BEqF/N 3yVyfF5MKgX//rH6/+SJ6q0qN8Lj7sLWmY1n5ncfQ/q33EwGu0RQhYzpcIuMOI1tb/6Y rA4RzyFV6Y+rcmpDSqpYuicn7+nZPQonnRqxbwEsNTaGbYYjYwGg4ijtLRqi/B6mU9F7 FK0ORBMjFXOQTQ+d8IereLheXxVpUNpCb7L7COE8NHKfHemcQoiTHrRowvAIY8CdMwjc 6x22PjfTLE6mJHCoYvqbK9eAnCOC/Xmtc4ouDto734gUYqJVIqI/DiITCXUmrYk+0BKD niQg== X-Gm-Message-State: ALoCoQnlqqlcOEXIp9OIa8w9Nu+B0iHoL38AWGFLdt0a86n3/eTVmwjLNhXnPRMw2rXpAdxqlKiT X-Received: by 10.112.158.202 with SMTP id ww10mr5270912lbb.13.1443007553829; Wed, 23 Sep 2015 04:25:53 -0700 (PDT) X-BeenThere: patchwork-forward@linaro.org Received: by 10.25.40.133 with SMTP id o127ls96475lfo.38.gmail; Wed, 23 Sep 2015 04:25:53 -0700 (PDT) X-Received: by 10.112.140.104 with SMTP id rf8mr9369150lbb.30.1443007553662; Wed, 23 Sep 2015 04:25:53 -0700 (PDT) Received: from mail-la0-f49.google.com (mail-la0-f49.google.com. [209.85.215.49]) by mx.google.com with ESMTPS id q6si3509147lah.68.2015.09.23.04.25.53 for (version=TLSv1.2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Wed, 23 Sep 2015 04:25:53 -0700 (PDT) Received-SPF: pass (google.com: domain of patch+caf_=patchwork-forward=linaro.org@linaro.org designates 209.85.215.49 as permitted sender) client-ip=209.85.215.49; Received: by lagj9 with SMTP id j9so46390839lag.2 for ; Wed, 23 Sep 2015 04:25:53 -0700 (PDT) X-Received: by 10.25.150.199 with SMTP id y190mr3766769lfd.35.1443007553483; Wed, 23 Sep 2015 04:25:53 -0700 (PDT) X-Forwarded-To: patchwork-forward@linaro.org X-Forwarded-For: patch@linaro.org patchwork-forward@linaro.org Delivered-To: patch@linaro.org Received: by 10.112.59.35 with SMTP id w3csp1044270lbq; Wed, 23 Sep 2015 04:25:52 -0700 (PDT) X-Received: by 10.68.100.226 with SMTP id fb2mr37333373pbb.92.1443007552414; Wed, 23 Sep 2015 04:25:52 -0700 (PDT) Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id tx3si9869300pbc.224.2015.09.23.04.25.44; Wed, 23 Sep 2015 04:25:52 -0700 (PDT) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1754322AbbIWLZm (ORCPT + 30 others); Wed, 23 Sep 2015 07:25:42 -0400 Received: from szxga03-in.huawei.com ([119.145.14.66]:57456 "EHLO szxga03-in.huawei.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1753842AbbIWLZj (ORCPT ); Wed, 23 Sep 2015 07:25:39 -0400 Received: from 172.24.1.48 (EHLO szxeml426-hub.china.huawei.com) ([172.24.1.48]) by szxrg03-dlp.huawei.com (MOS 4.4.3-GA FastPath queued) with ESMTP id BNW12492; Wed, 23 Sep 2015 19:23:08 +0800 (CST) Received: from linux-4hy3.site (10.107.193.248) by szxeml426-hub.china.huawei.com (10.82.67.181) with Microsoft SMTP Server id 14.3.235.1; Wed, 23 Sep 2015 19:22:59 +0800 From: Wang Nan To: CC: , , Wang Nan , Alexei Starovoitov , Brendan Gregg , Daniel Borkmann , "David Ahern" , He Kuang , Jiri Olsa , Kaixu Xia , Masami Hiramatsu , Namhyung Kim , "Paul Mackerras" , Peter Zijlstra , Zefan Li Subject: [PATCH 05/22] perf tools: Collect perf_evsel in BPF object files Date: Wed, 23 Sep 2015 11:22:26 +0000 Message-ID: <1443007363-124182-6-git-send-email-wangnan0@huawei.com> X-Mailer: git-send-email 1.8.3.4 In-Reply-To: <1443007363-124182-1-git-send-email-wangnan0@huawei.com> References: <1443007363-124182-1-git-send-email-wangnan0@huawei.com> MIME-Version: 1.0 X-Originating-IP: [10.107.193.248] X-CFilter-Loop: Reflected X-Mirapoint-Virus-RAPID-Raw: score=unknown(0), refid=str=0001.0A010203.56028B9C.0080, ss=1, re=0.000, recu=0.000, reip=0.000, cl=1, cld=1, fgs=0, ip=0.0.0.0, so=2013-05-26 15:14:31, dmn=2013-03-21 17:37:32 X-Mirapoint-Loop-Id: bf4e41ac17c9b33278c58c758111aac1 Sender: linux-kernel-owner@vger.kernel.org Precedence: list List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-Removed-Original-Auth: Dkim didn't pass. X-Original-Sender: wangnan0@huawei.com X-Original-Authentication-Results: mx.google.com; spf=pass (google.com: domain of patch+caf_=patchwork-forward=linaro.org@linaro.org designates 209.85.215.49 as permitted sender) smtp.mailfrom=patch+caf_=patchwork-forward=linaro.org@linaro.org Mailing-list: list patchwork-forward@linaro.org; contact patchwork-forward+owners@linaro.org X-Google-Group-Id: 836684582541 List-Post: , List-Help: , List-Archive: List-Unsubscribe: , This patch collects 'struct perf_evsel' for every probing points in BPF object file(s) and fill 'struct evlist'. The previous introduced dummy event now removed. After this patch, following command: # perf record --event filter.o ls Can trace on each probing points defined in filter.o. The core of this patch is bpf__foreach_tev(), which calls a callback function for each 'struct probe_trace_event' events for a bpf program with their file descriptors. Callback function add_bpf_event() creates evsels by calling parse_events_add_tracepoint(). Since bpf-loader.c will not be built if libbpf is turned off, an empty bpf__foreach_tev() is defined in bpf-loader.h to avoid compiling error. Signed-off-by: Wang Nan Cc: Alexei Starovoitov Cc: Brendan Gregg Cc: Daniel Borkmann Cc: David Ahern Cc: He Kuang Cc: Jiri Olsa Cc: Kaixu Xia Cc: Masami Hiramatsu Cc: Namhyung Kim Cc: Paul Mackerras Cc: Peter Zijlstra Cc: Zefan Li Cc: pi3orama@163.com Cc: Arnaldo Carvalho de Melo Link: http://lkml.kernel.org/n/ebpf-6yw9eg0ej3l4jnqhinngkw86@git.kernel.org --- tools/perf/util/bpf-loader.c | 40 +++++++++++++++++++++++++++++++++++ tools/perf/util/bpf-loader.h | 15 +++++++++++++ tools/perf/util/parse-events.c | 48 ++++++++++++++++++++++++++++++++++++------ 3 files changed, 97 insertions(+), 6 deletions(-) diff --git a/tools/perf/util/bpf-loader.c b/tools/perf/util/bpf-loader.c index 39732a4..33e592c 100644 --- a/tools/perf/util/bpf-loader.c +++ b/tools/perf/util/bpf-loader.c @@ -254,6 +254,46 @@ int bpf__load(struct bpf_object *obj) return 0; } +int bpf__foreach_tev(struct bpf_object *obj, + bpf_prog_iter_callback_t func, + void *arg) +{ + struct bpf_program *prog; + int err; + + bpf_object__for_each_program(prog, obj) { + struct probe_trace_event *tev; + struct perf_probe_event *pev; + struct bpf_prog_priv *priv; + int i, fd; + + err = bpf_program__get_private(prog, + (void **)&priv); + if (err || !priv) { + pr_debug("bpf: failed to get private field\n"); + return -EINVAL; + } + + pev = &priv->pev; + for (i = 0; i < pev->ntevs; i++) { + tev = &pev->tevs[i]; + + fd = bpf_program__fd(prog); + if (fd < 0) { + pr_debug("bpf: failed to get file descriptor\n"); + return fd; + } + + err = (*func)(tev, fd, arg); + if (err) { + pr_debug("bpf: call back failed, stop iterate\n"); + return err; + } + } + } + return 0; +} + #define bpf__strerror_head(err, buf, size) \ char sbuf[STRERR_BUFSIZE], *emsg;\ if (!size)\ diff --git a/tools/perf/util/bpf-loader.h b/tools/perf/util/bpf-loader.h index b091ceb..4adfc03 100644 --- a/tools/perf/util/bpf-loader.h +++ b/tools/perf/util/bpf-loader.h @@ -8,11 +8,15 @@ #include #include #include +#include "probe-event.h" #include "debug.h" struct bpf_object; #define PERF_BPF_PROBE_GROUP "perf_bpf_probe" +typedef int (*bpf_prog_iter_callback_t)(struct probe_trace_event *tev, + int fd, void *arg); + #ifdef HAVE_LIBBPF_SUPPORT struct bpf_object *bpf__prepare_load(const char *filename); @@ -26,6 +30,9 @@ int bpf__strerror_probe(struct bpf_object *obj, int err, int bpf__load(struct bpf_object *obj); int bpf__strerror_load(struct bpf_object *obj, int err, char *buf, size_t size); + +int bpf__foreach_tev(struct bpf_object *obj, + bpf_prog_iter_callback_t func, void *arg); #else static inline struct bpf_object * bpf__prepare_load(const char *filename __maybe_unused) @@ -41,6 +48,14 @@ static inline int bpf__unprobe(struct bpf_object *obj __maybe_unused) { return 0 static inline int bpf__load(struct bpf_object *obj __maybe_unused) { return 0; } static inline int +bpf__foreach_tev(struct bpf_object *obj __maybe_unused, + bpf_prog_iter_callback_t func __maybe_unused, + void *arg __maybe_unused) +{ + return 0; +} + +static inline int __bpf_strerror(char *buf, size_t size) { if (!size) diff --git a/tools/perf/util/parse-events.c b/tools/perf/util/parse-events.c index a7152f3..c3505fb 100644 --- a/tools/perf/util/parse-events.c +++ b/tools/perf/util/parse-events.c @@ -516,6 +516,39 @@ int parse_events_add_tracepoint(struct list_head *list, int *idx, return add_tracepoint_event(list, idx, sys, event, error); } +struct __add_bpf_event_param { + struct parse_events_evlist *data; + struct list_head *list; +}; + +static int add_bpf_event(struct probe_trace_event *tev, int fd, + void *_param) +{ + struct __add_bpf_event_param *param = _param; + struct parse_events_evlist *evlist = param->data; + struct list_head *list = param->list; + int err; + + pr_debug("add bpf event %s:%s and attach bpf program %d\n", + tev->group, tev->event, fd); + + err = parse_events_add_tracepoint(list, &evlist->idx, tev->group, + tev->event, evlist->error); + if (err) { + struct perf_evsel *evsel, *tmp; + + pr_debug("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 err; + } + pr_debug("adding %s:%s\n", tev->group, tev->event); + return 0; +} + int parse_events_load_bpf_obj(struct parse_events_evlist *data, struct list_head *list, struct bpf_object *obj) @@ -523,6 +556,7 @@ int parse_events_load_bpf_obj(struct parse_events_evlist *data, int err; char errbuf[BUFSIZ]; static bool registered_unprobe_atexit = false; + struct __add_bpf_event_param param = {data, list}; if (IS_ERR(obj) || !obj) { snprintf(errbuf, sizeof(errbuf), @@ -548,12 +582,14 @@ int parse_events_load_bpf_obj(struct parse_events_evlist *data, goto errout; } - /* - * Temporary add a dummy event here so we can check whether - * basic bpf loader works. Will be removed in following patch. - */ - return parse_events_add_numeric(data, list, PERF_TYPE_SOFTWARE, - PERF_COUNT_SW_DUMMY, NULL); + err = bpf__foreach_tev(obj, add_bpf_event, ¶m); + if (err) { + snprintf(errbuf, sizeof(errbuf), + "Attach events in BPF object failed"); + goto errout; + } + + return 0; errout: data->error->help = strdup("(add -v to see detail)"); data->error->str = strdup(errbuf);