From patchwork Sat Nov 12 03:22:36 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Namhyung Kim X-Patchwork-Id: 19088 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a5d:6687:0:0:0:0:0 with SMTP id l7csp1089485wru; Fri, 11 Nov 2022 19:24:01 -0800 (PST) X-Google-Smtp-Source: AA0mqf6nRUh2uv7N3vsZDYm9Tanq0jecq5lNvJpLq1M378Q7cTC8+so/bnGRjO5fCbxSLcePhAfF X-Received: by 2002:a17:906:492:b0:7ad:bb54:75d3 with SMTP id f18-20020a170906049200b007adbb5475d3mr4022771eja.484.1668223441023; Fri, 11 Nov 2022 19:24:01 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1668223441; cv=none; d=google.com; s=arc-20160816; b=tDOjQn10kD0OP+w51+i91J45Ev5FdvMe3JuZu6ERebO04MjEqmZ/z/x9MrZ6gI3bf9 93AJ+9WAoGhYzA4kHUiv2t+PVdiGN9slrM9YOTUN6y5fauQoCf/Ttylm/dixswGZa2HT MBVwLVOrLUr9M2hKpzaGqa390EqovqqudAbl6NNQWEaL7EsfT43gqdI5tsfJqc11/tsX f1D7X1e47xCDP/dPmaTo8KIrsWt/hzf1WvGCIgpgsYVHm6GjnzmHoVd/uqKPdlU4Y3GE +3eAomz3lZnTYPU17grBmTZ+ByRy1WN0ZuGf7jO+FnWrcy5YAhC4qBkXRAYJgO3kjmvN Wtdw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from:sender :dkim-signature; bh=AxQ/iN4XHg0FIQpCgFQhcD29e8GRZB9Mx4/w+wLAp6k=; b=KEmglfDXRAphFbsTjb3COzwXLRh7h5kAUIM6xExXicG2Rw3hkY/n6QlODHtjA/WNUQ 7F3u9+3nuG6iG9tAZ7aUyqAPAXTdERC0iRqmitv2S+T6jlUUCX0UwyTMWEiOsQ5PTUYc /6D9WNrFz7Nh9e91SiUNptjKNOTx64jYLPi3CRjwKWA/9rhdlkrS9auEVI8fdZovshgY 2v6m1xbj1BP75j+M0zguGkdj9heuZWcV3SwI8TvrvfsvAJ2iY5XCLA/OeIEXfyO6HUYx aN4mgA/gLx4uHBuHE9QMe8PYlOIV2dP3gtIgXCfjXTAmEjs8pfBYtaChsN5Y+xeKSsRY L+Pg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20210112 header.b=JpwS4rTV; 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=NONE sp=NONE dis=NONE) header.from=kernel.org Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id ec16-20020a170906b6d000b0078db517075fsi2956700ejb.659.2022.11.11.19.23.37; Fri, 11 Nov 2022 19:24:01 -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=@gmail.com header.s=20210112 header.b=JpwS4rTV; 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=NONE sp=NONE dis=NONE) header.from=kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S234503AbiKLDXD (ORCPT + 99 others); Fri, 11 Nov 2022 22:23:03 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:54924 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S234255AbiKLDWz (ORCPT ); Fri, 11 Nov 2022 22:22:55 -0500 Received: from mail-pl1-x62d.google.com (mail-pl1-x62d.google.com [IPv6:2607:f8b0:4864:20::62d]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 859836A77E; Fri, 11 Nov 2022 19:22:53 -0800 (PST) Received: by mail-pl1-x62d.google.com with SMTP id u6so5662064plq.12; Fri, 11 Nov 2022 19:22:53 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:sender:from:to:cc:subject:date :message-id:reply-to; bh=AxQ/iN4XHg0FIQpCgFQhcD29e8GRZB9Mx4/w+wLAp6k=; b=JpwS4rTVbq8z4URe3Q/HHFLpCAKd2Y/ZmR5sUXhhacWWYfAhrucKxKvtTb3iAIpmlp 8SugwyqwsG/VR0tEFc+1XrY7J7z/9R+feg2280FzJD3Xyl4v8lwKK7Yovzub28hH87u7 pyU8GRNXRNVpdB6uV3z0CrPcMULbxXgZK9mPwmvR1t3Uf0al+sb2gsGzXjBtYQUw89iA XTO9BRSjAxP1JzE/xRk2VYj+hwLoia/AbS29KdaetGoRPO9A+veHM+wzYcotOV/g24yi K587VZ39Qz38Eyw/VO3rXfFYBRKLFjGmb9xLntew7xeK7839Mav4n+c/CKmQkujuYlAb 5aqQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:sender:x-gm-message-state:from :to:cc:subject:date:message-id:reply-to; bh=AxQ/iN4XHg0FIQpCgFQhcD29e8GRZB9Mx4/w+wLAp6k=; b=e6eCvupmrAEep+vRywa7ctLyj3TXHyjxvMvqK6NzNY0E6xlJhWY6kgIvy6paD6ndEG WCcrlx4I3lo8wvU+FuFxrPc5EwbfUkyzU+MRhisbGNZeh8cVjUwXQisTMjdTPNJXFAVP AOkmQJMxg06/XppGptIOycg5kkZnTSgcF044/QBAFGvlBSkzrX1hqaMvQXOcwuDaISzJ ltHIZj/U/4htps7znTWvD4ktcblU5Bm1TYmXnNSs86Cyykm+8UXDxFDNrAKjk5H5bfBu GFPt3M/Cetz+x07dcpSYpHCt/JfQFDWbl7isO6/l0chI3zUKpJczHWPTkBvSTQZ+8/OI 4ysw== X-Gm-Message-State: ANoB5plz/xht8BQRew4YQ3fF/SRjSwlwtc9acI3vjbg28JWKs505fBeL iR81ezuuDArwyL5WlErfbvs= X-Received: by 2002:a17:90a:eb92:b0:213:32de:be32 with SMTP id o18-20020a17090aeb9200b0021332debe32mr4973130pjy.2.1668223372955; Fri, 11 Nov 2022 19:22:52 -0800 (PST) Received: from balhae.roam.corp.google.com ([2607:fb90:27dc:8408:33d7:3b01:eee1:84b]) by smtp.gmail.com with ESMTPSA id ij25-20020a170902ab5900b00186f0f59d1esm2434552plb.192.2022.11.11.19.22.51 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 11 Nov 2022 19:22:52 -0800 (PST) Sender: Namhyung Kim From: Namhyung Kim To: Arnaldo Carvalho de Melo , Jiri Olsa Cc: Ingo Molnar , Peter Zijlstra , LKML , Ian Rogers , Adrian Hunter , linux-perf-users@vger.kernel.org, Kan Liang , Zhengjun Xing , James Clark Subject: [PATCH 03/11] perf stat: Fix --metric-only --json output Date: Fri, 11 Nov 2022 19:22:36 -0800 Message-Id: <20221112032244.1077370-4-namhyung@kernel.org> X-Mailer: git-send-email 2.38.1.493.g58b659f92b-goog In-Reply-To: <20221112032244.1077370-1-namhyung@kernel.org> References: <20221112032244.1077370-1-namhyung@kernel.org> MIME-Version: 1.0 X-Spam-Status: No, score=-1.5 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_EF,FREEMAIL_FORGED_FROMDOMAIN,FREEMAIL_FROM, HEADER_FROM_DIFFERENT_DOMAINS,RCVD_IN_DNSWL_NONE,SPF_HELO_NONE, SPF_PASS autolearn=no 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?1749259063179021143?= X-GMAIL-MSGID: =?utf-8?q?1749259063179021143?= Currently it prints all metric headers for JSON output. But actually it skips some metrics with valid_only_metric(). So the output looks like: $ perf stat --metric-only --json true {"unit" : "CPUs utilized", "unit" : "/sec", "unit" : "/sec", "unit" : "/sec", "unit" : "GHz", "unit" : "insn per cycle", "unit" : "/sec", "unit" : "branch-misses of all branches"} {"metric-value" : "3.861"}{"metric-value" : "0.79"}{"metric-value" : "3.04"} As you can see there are 8 units in the header but only 3 metric-values are there. It should skip the unused headers as well. Also each unit should be printed as a separate object like metric values. With this patch: $ perf stat --metric-only --json true {"unit" : "GHz"}{"unit" : "insn per cycle"}{"unit" : "branch-misses of all branches"} {"metric-value" : "4.166"}{"metric-value" : "0.73"}{"metric-value" : "2.96"} Fixes: df936cadfb58 ("perf stat: Add JSON output option") Signed-off-by: Namhyung Kim Acked-by: Ian Rogers --- tools/perf/util/stat-display.c | 22 +++------------------- 1 file changed, 3 insertions(+), 19 deletions(-) diff --git a/tools/perf/util/stat-display.c b/tools/perf/util/stat-display.c index c7b3a1e10263..96ad0c71adc2 100644 --- a/tools/perf/util/stat-display.c +++ b/tools/perf/util/stat-display.c @@ -430,12 +430,12 @@ static void print_metric_header(struct perf_stat_config *config, os->evsel->priv != os->evsel->evlist->selected->priv) return; - if (!valid_only_metric(unit) && !config->json_output) + if (!valid_only_metric(unit)) return; unit = fixunit(tbuf, os->evsel, unit); if (config->json_output) - fprintf(os->fh, "\"unit\" : \"%s\"", unit); + fprintf(os->fh, "{\"unit\" : \"%s\"}", unit); else if (config->csv_output) fprintf(os->fh, "%s%s", unit, config->csv_sep); else @@ -847,10 +847,6 @@ static void print_metric_headers(struct perf_stat_config *config, .new_line = new_line_metric, .force_header = true, }; - bool first = true; - - if (config->json_output && !config->interval) - fprintf(config->output, "{"); if (prefix && !config->json_output) fprintf(config->output, "%s", prefix); @@ -871,18 +867,12 @@ static void print_metric_headers(struct perf_stat_config *config, evlist__for_each_entry(evlist, counter) { os.evsel = counter; - if (!first && config->json_output) - fprintf(config->output, ", "); - first = false; - perf_stat__print_shadow_stats(config, counter, 0, 0, &out, &config->metric_events, &rt_stat); } - if (config->json_output) - fprintf(config->output, "}"); fputc('\n', config->output); } @@ -954,14 +944,8 @@ static void print_interval(struct perf_stat_config *config, } } - if ((num_print_interval == 0 || config->interval_clear) - && metric_only && !config->json_output) + if ((num_print_interval == 0 || config->interval_clear) && metric_only) print_metric_headers(config, evlist, " ", true); - if ((num_print_interval == 0 || config->interval_clear) - && metric_only && config->json_output) { - fprintf(output, "{"); - print_metric_headers(config, evlist, " ", true); - } if (++num_print_interval == 25) num_print_interval = 0; }