From patchwork Wed Nov 16 07:12:59 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Ian Rogers X-Patchwork-Id: 20745 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a05:7300:a40e:b0:83:7221:86ba with SMTP id ck14csp252079dyb; Tue, 15 Nov 2022 23:27:07 -0800 (PST) X-Google-Smtp-Source: AA0mqf4zGnznfoeMb7rSaaULhQIVMCK3tLwGkg0W3YfwTBJqlz7MjkuTc1yDJP1749084ALmNsu4 X-Received: by 2002:a17:906:4d58:b0:781:bc28:f455 with SMTP id b24-20020a1709064d5800b00781bc28f455mr16454896ejv.170.1668583627321; Tue, 15 Nov 2022 23:27:07 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1668583627; cv=none; d=google.com; s=arc-20160816; b=CRlKvtPYjRbECl1AgkuZzqLmmuE2kjGf9ksZuUMxgD3ekJ2uJNEaOxj+17mK7fR63N AQhFciClK1j9ZhatUvPP6BGQntqceF0L9ErMyMF9e72/+21CPIdlZ+vSrgiRFn7PoRlg YCO3J24sWewYClmNQi0xn9AcR8MYOtRQM70WnMokqDx5G/y84sZEJ4a7cZiku54ZNRiq fBS707JJmeGLydQppZJAoOW1sctIOTMQ4NpcAD9N/P7NTZIWTVZDcph1vrkM65MDj/Jm Xum5T4lVq3OrmFRYrhebJ0Mg+boDMlffuPqhsDckeBjlLh7qxTkGlgyYqrMPP1NPXgCH qChg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:cc:to:from:subject:references:mime-version :message-id:in-reply-to:date:dkim-signature; bh=wK54JSKXe0LqXOZ3d575hQDmq52LEert5D/pS8cJUkE=; b=rDaezvCz1ZEQFwR9X/oWjHidYXP5t3dPW6tPBjDnByjUHHoiY8IzNBMn+0eKEiqAyQ vqpHKjpxKl/rqD/iG0ikZ1okmp10zCz5xftmHdgVjaO0hUBgUs4cYYgfYaQLor/CZEJ4 X5Rz8ijQeDAva6rRbGg41DDoohIcfyrDrsDXqf5e4V/je6QN7EA7G/0h26hweXud7vNE vfBkTfjGQMpP0RzC4OtMpJlCWYKZtr904eG6WL59Q50+Gncdizr0EhjQAl8fEx8jTP+L 5VKAJGVbrpbssUMIcH3zNl/HF2sz0ZiRNemlhjQzMxVmdfbcLr31tZB0eQTH3DTIzofy h8+A== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20210112 header.b=U1d7MimG; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id hp36-20020a1709073e2400b007879bb73291si15523653ejc.807.2022.11.15.23.26.43; Tue, 15 Nov 2022 23:27:07 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@google.com header.s=20210112 header.b=U1d7MimG; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232234AbiKPHN1 (ORCPT + 99 others); Wed, 16 Nov 2022 02:13:27 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:41268 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232715AbiKPHNR (ORCPT ); Wed, 16 Nov 2022 02:13:17 -0500 Received: from mail-yw1-x114a.google.com (mail-yw1-x114a.google.com [IPv6:2607:f8b0:4864:20::114a]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 32B8E20F59 for ; Tue, 15 Nov 2022 23:13:13 -0800 (PST) Received: by mail-yw1-x114a.google.com with SMTP id 00721157ae682-385bbf9bc8fso38274697b3.23 for ; Tue, 15 Nov 2022 23:13:13 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20210112; h=cc:to:from:subject:references:mime-version:message-id:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=wK54JSKXe0LqXOZ3d575hQDmq52LEert5D/pS8cJUkE=; b=U1d7MimG9gAoAlzFe+38ukpZbqtI6+WSbYJnASee2xaNcWt9opU7e/VTdFaLjmpQZ2 X6zxHepXPQVF/knczaV0Pk8JFjDlEBpBHbz22Y4XJdOcTCUABE2+IndsM8s4fHNawIC9 MD1gxzR8AmL78bWa8wfA+IVoUCxS5DHItTEtHlR2nrlphjwbkXfl+hxyZdBarRoMrzr3 NLndOR5LqnrWjzZmVrvhyHo143J9+5KkXgPEmb1ylBLCZkMwVDWkTAftNfK6J3O5TMag GaUcHNH+DaKajLT22/xCtcYz0jKRXgawxf9IPirCd6aZfH/lds7D6x85YrlWF35ZX96L V0Fg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=cc:to:from:subject:references:mime-version:message-id:in-reply-to :date:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=wK54JSKXe0LqXOZ3d575hQDmq52LEert5D/pS8cJUkE=; b=JKjGTNSjhIyfJSxKCKo4RN9LROrDMz7pZnTtJH2zsxlNFnkICNPCYXrLOhv9pI7US8 FdvQWWLeh9X4qkMZnZ3N5iUeqSvpajLywOM6JWWw0w6r5HfWHTZzZK34wmNBHwW4uODC 5gIEpYKXtTIyHp+Ad1DBGbxyRQM/IaFWLmhhIKPQlcFXFG/mUaYiiToezfofC3kyhsUg 3FsOiReGkJx18pJBzbCgx2ldySlkPQtxgglVBG15e4YJwkoL2mToL+f/kOB75siLAsf8 Y4oHl03psuknSsjjCeqBsH6EBuQV7cRrSqOVl88x1W2LN5rxypAPzTW/es6Wy8LboXPY LPzw== X-Gm-Message-State: ANoB5pnQDhAyJDhj8VEl4IQfkhT8TKUWDP/l8TKObKqRC1+vg3W8WCmR JUBjEClK3opCvYrEloUQVvwjCtDLliOX X-Received: from irogers.svl.corp.google.com ([2620:15c:2d4:203:bf0f:58f3:342e:c1ec]) (user=irogers job=sendgmr) by 2002:a05:690c:e:b0:388:2fb:57d9 with SMTP id bc14-20020a05690c000e00b0038802fb57d9mr4675335ywb.371.1668582792508; Tue, 15 Nov 2022 23:13:12 -0800 (PST) Date: Tue, 15 Nov 2022 23:12:59 -0800 In-Reply-To: <20221116071259.2832681-1-irogers@google.com> Message-Id: <20221116071259.2832681-5-irogers@google.com> Mime-Version: 1.0 References: <20221116071259.2832681-1-irogers@google.com> X-Mailer: git-send-email 2.38.1.431.g37b22c650d-goog Subject: [PATCH v1 4/4] perf list: List callback support for libpfm From: Ian Rogers To: Weilin Wang , Perry Taylor , Caleb Biggers , Leo Yan , Adrian Hunter , Peter Zijlstra , Ingo Molnar , Arnaldo Carvalho de Melo , Mark Rutland , Alexander Shishkin , Jiri Olsa , Namhyung Kim , Sandipan Das , Kajol Jain , Zhengjun Xing , Kan Liang , Ravi Bangoria , Xin Gao , Rob Herring , linux-kernel@vger.kernel.org, linux-perf-users@vger.kernel.org Cc: Stephane Eranian , Ian Rogers X-Spam-Status: No, score=-9.6 required=5.0 tests=BAYES_00,DKIMWL_WL_MED, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_NONE, SPF_HELO_NONE,SPF_PASS,USER_IN_DEF_DKIM_WL autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1749636745991328419?= X-GMAIL-MSGID: =?utf-8?q?1749636745991328419?= Missed previously, add libpfm support for 'perf list' callbacks and thereby json support. Fixes: df936cadfb58ba93 ("perf stat: Add JSON output option") Signed-off-by: Ian Rogers --- tools/perf/util/pfm.c | 154 ++++++++++++++++++------------------------ tools/perf/util/pfm.h | 6 +- 2 files changed, 70 insertions(+), 90 deletions(-) diff --git a/tools/perf/util/pfm.c b/tools/perf/util/pfm.c index f0bcfcab1a93..ac3227ba769c 100644 --- a/tools/perf/util/pfm.c +++ b/tools/perf/util/pfm.c @@ -12,6 +12,7 @@ #include "util/parse-events.h" #include "util/pmu.h" #include "util/pfm.h" +#include "util/strbuf.h" #include #include @@ -130,53 +131,36 @@ static const char *srcs[PFM_ATTR_CTRL_MAX] = { }; static void -print_attr_flags(pfm_event_attr_info_t *info) +print_attr_flags(struct strbuf *buf, const pfm_event_attr_info_t *info) { - int n = 0; + if (info->is_dfl) + strbuf_addf(buf, "[default] "); - if (info->is_dfl) { - printf("[default] "); - n++; - } - - if (info->is_precise) { - printf("[precise] "); - n++; - } - - if (!n) - printf("- "); + if (info->is_precise) + strbuf_addf(buf, "[precise] "); } static void -print_libpfm_events_detailed(pfm_event_info_t *info, bool long_desc) +print_libpfm_event(const struct print_callbacks *print_cb, void *print_state, + const pfm_pmu_info_t *pinfo, const pfm_event_info_t *info, + struct strbuf *buf) { - pfm_event_attr_info_t ainfo; - const char *src; int j, ret; + char topic[80], name[80]; - ainfo.size = sizeof(ainfo); + strbuf_setlen(buf, 0); + snprintf(topic, sizeof(topic), "pfm %s", pinfo->name); - printf(" %s\n", info->name); - printf(" [%s]\n", info->desc); - if (long_desc) { - if (info->equiv) - printf(" Equiv: %s\n", info->equiv); + snprintf(name, sizeof(name), "%s::%s", pinfo->name, info->name); + strbuf_addf(buf, "Code: 0x%"PRIx64"\n", info->code); - printf(" Code : 0x%"PRIx64"\n", info->code); - } pfm_for_each_event_attr(j, info) { - ret = pfm_get_event_attr_info(info->idx, j, - PFM_OS_PERF_EVENT_EXT, &ainfo); - if (ret != PFM_SUCCESS) - continue; - - if (ainfo.type == PFM_ATTR_UMASK) { - printf(" %s:%s\n", info->name, ainfo.name); - printf(" [%s]\n", ainfo.desc); - } + pfm_event_attr_info_t ainfo; + const char *src; - if (!long_desc) + ainfo.size = sizeof(ainfo); + ret = pfm_get_event_attr_info(info->idx, j, PFM_OS_PERF_EVENT_EXT, &ainfo); + if (ret != PFM_SUCCESS) continue; if (ainfo.ctrl >= PFM_ATTR_CTRL_MAX) @@ -184,64 +168,74 @@ print_libpfm_events_detailed(pfm_event_info_t *info, bool long_desc) src = srcs[ainfo.ctrl]; switch (ainfo.type) { - case PFM_ATTR_UMASK: - printf(" Umask : 0x%02"PRIx64" : %s: ", - ainfo.code, src); - print_attr_flags(&ainfo); - putchar('\n'); + case PFM_ATTR_UMASK: /* Ignore for now */ break; case PFM_ATTR_MOD_BOOL: - printf(" Modif : %s: [%s] : %s (boolean)\n", src, - ainfo.name, ainfo.desc); + strbuf_addf(buf, " Modif: %s: [%s] : %s (boolean)\n", src, + ainfo.name, ainfo.desc); break; case PFM_ATTR_MOD_INTEGER: - printf(" Modif : %s: [%s] : %s (integer)\n", src, - ainfo.name, ainfo.desc); + strbuf_addf(buf, " Modif: %s: [%s] : %s (integer)\n", src, + ainfo.name, ainfo.desc); break; case PFM_ATTR_NONE: case PFM_ATTR_RAW_UMASK: case PFM_ATTR_MAX: default: - printf(" Attr : %s: [%s] : %s\n", src, - ainfo.name, ainfo.desc); + strbuf_addf(buf, " Attr: %s: [%s] : %s\n", src, + ainfo.name, ainfo.desc); } } -} + print_cb->print_event(print_state, + pinfo->name, + topic, + name, info->equiv, + /*scale_unit=*/NULL, + /*deprecated=*/NULL, "PFM event", + info->desc, /*long_desc=*/NULL, + /*encoding_desc=*/buf->buf, + /*metric_name=*/NULL, /*metric_expr=*/NULL); -/* - * list all pmu::event:umask, pmu::event - * printed events may not be all valid combinations of umask for an event - */ -static void -print_libpfm_events_raw(pfm_pmu_info_t *pinfo, pfm_event_info_t *info) -{ - pfm_event_attr_info_t ainfo; - int j, ret; - bool has_umask = false; + pfm_for_each_event_attr(j, info) { + pfm_event_attr_info_t ainfo; + const char *src; - ainfo.size = sizeof(ainfo); + strbuf_setlen(buf, 0); - pfm_for_each_event_attr(j, info) { - ret = pfm_get_event_attr_info(info->idx, j, - PFM_OS_PERF_EVENT_EXT, &ainfo); + ainfo.size = sizeof(ainfo); + ret = pfm_get_event_attr_info(info->idx, j, PFM_OS_PERF_EVENT_EXT, &ainfo); if (ret != PFM_SUCCESS) continue; - if (ainfo.type != PFM_ATTR_UMASK) - continue; + if (ainfo.ctrl >= PFM_ATTR_CTRL_MAX) + ainfo.ctrl = PFM_ATTR_CTRL_UNKNOWN; - printf("%s::%s:%s\n", pinfo->name, info->name, ainfo.name); - has_umask = true; + src = srcs[ainfo.ctrl]; + if (ainfo.type == PFM_ATTR_UMASK) { + strbuf_addf(buf, "Umask: 0x%02"PRIx64" : %s: ", + ainfo.code, src); + print_attr_flags(buf, &ainfo); + snprintf(name, sizeof(name), "%s::%s:%s", + pinfo->name, info->name, ainfo.name); + print_cb->print_event(print_state, + pinfo->name, + topic, + name, /*alias=*/NULL, + /*scale_unit=*/NULL, + /*deprecated=*/NULL, "PFM event", + ainfo.desc, /*long_desc=*/NULL, + /*encoding_desc=*/buf->buf, + /*metric_name=*/NULL, /*metric_expr=*/NULL); + } } - if (!has_umask) - printf("%s::%s\n", pinfo->name, info->name); } -void print_libpfm_events(bool name_only, bool long_desc) +void print_libpfm_events(const struct print_callbacks *print_cb, void *print_state) { pfm_event_info_t info; pfm_pmu_info_t pinfo; - int i, p, ret; + int p, ret; + struct strbuf storage; libpfm_initialize(); @@ -249,12 +243,9 @@ void print_libpfm_events(bool name_only, bool long_desc) info.size = sizeof(info); pinfo.size = sizeof(pinfo); - if (!name_only) - puts("\nList of pre-defined events (to be used in --pfm-events):\n"); + strbuf_init(&storage, 2048); pfm_for_all_pmus(p) { - bool printed_pmu = false; - ret = pfm_get_pmu_info(p, &pinfo); if (ret != PFM_SUCCESS) continue; @@ -267,25 +258,14 @@ void print_libpfm_events(bool name_only, bool long_desc) if (pinfo.pmu == PFM_PMU_PERF_EVENT) continue; - for (i = pinfo.first_event; i != -1; - i = pfm_get_event_next(i)) { - + for (int i = pinfo.first_event; i != -1; i = pfm_get_event_next(i)) { ret = pfm_get_event_info(i, PFM_OS_PERF_EVENT_EXT, &info); if (ret != PFM_SUCCESS) continue; - if (!name_only && !printed_pmu) { - printf("%s:\n", pinfo.name); - printed_pmu = true; - } - - if (!name_only) - print_libpfm_events_detailed(&info, long_desc); - else - print_libpfm_events_raw(&pinfo, &info); + print_libpfm_event(print_cb, print_state, &pinfo, &info, &storage); } - if (!name_only && printed_pmu) - putchar('\n'); } + strbuf_release(&storage); } diff --git a/tools/perf/util/pfm.h b/tools/perf/util/pfm.h index 7d70dda87012..9cc9bb1e0949 100644 --- a/tools/perf/util/pfm.h +++ b/tools/perf/util/pfm.h @@ -7,13 +7,14 @@ #ifndef __PERF_PFM_H #define __PERF_PFM_H +#include "print-events.h" #include #ifdef HAVE_LIBPFM int parse_libpfm_events_option(const struct option *opt, const char *str, int unset); -void print_libpfm_events(bool name_only, bool long_desc); +void print_libpfm_events(const struct print_callbacks *print_cb, void *print_state); #else #include @@ -26,8 +27,7 @@ static inline int parse_libpfm_events_option( return 0; } -static inline void print_libpfm_events(bool name_only __maybe_unused, - bool long_desc __maybe_unused) +static inline void print_libpfm_events(const struct print_callbacks *print_cb, void *print_state) { }