Received: by 2002:ac0:e350:0:0:0:0:0 with SMTP id g16csp381063imn; Fri, 29 Jul 2022 09:38:59 -0700 (PDT) X-Google-Smtp-Source: AGRyM1v46z20VaFeSvzUidrNsYmH29Uo3oaouhNZNNTmfDhaZYCTiLdfszlnvBBg4BT5iX2wgfWV X-Received: by 2002:a05:6402:2755:b0:43b:e6d4:846f with SMTP id z21-20020a056402275500b0043be6d4846fmr4479418edd.36.1659112738767; Fri, 29 Jul 2022 09:38:58 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1659112738; cv=none; d=google.com; s=arc-20160816; b=yCEYrjC9NnAAxL1e2j4mbRsQ0jaZcc5/ykl0ZUPxyo3dh/8iYfJey/s2bbd+C1AdEp TOLRbABTcuCXQatyiOo3PNCOU9qdI/o1wZbVcX6Ic7uORzgIjWPMS9jU1V5WA0oOieP3 Q2ZOH9+xcnU+GEehs45rbexHegfRRgc+TmFw3a4Ce1VUrqMzwMjnx2CZO6Kq099xHcXQ CHe2M1C1OhMtRa+WUHAyvRNkYTvaL3xtSPNKcQIExgUay6WdjouVVY1QM+Zsnfk6Tw1a cyRa/Zohy4Bjzs4kO3kpUM9EPz6WTnDFjWakhrE7bzFtYEyGmluHGbRcwlMjI9t9z4us Qm/g== 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; bh=JOBg0YxACa9EQibD0dHfqA1Cn2SM+NHfJuu43YxRkF0=; b=bwZAcFGw40qyqnkGEkbzpTSnSGELgzKhFpSzLkvIT5MxTTh2xQACIH5vzAkuJoHSNf dTrERiWGzJ9LPpdn+oyVsb19obgadGEbMwg7L0hpm2QVfQIP7H5UdGI9Kr1jsAHEs2mn oMPoxmltFNWPZ8ccRZQ5IvFDgA7YAZguLkb89fPsAbrZQtWWmKCuLMlfFDzVGRde+HgA 6xWOC30jRQJumAyG0Q/IZglauEUk1BlgOk5qZHVGya5oIFymYl/uDxmY+iJh5FtN0geM SJP0BspRTSN53T5BwL2ePSwXAl1AXDx3jt5JCK1Bj456IoHKQt2PrQK/esosvWi0lw1F 1kRw== ARC-Authentication-Results: i=1; mx.google.com; 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=fail (p=NONE sp=NONE dis=NONE) header.from=arm.com Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id b4-20020a170906150400b0072b3a874867si3188589ejd.874.2022.07.29.09.38.33; Fri, 29 Jul 2022 09:38:58 -0700 (PDT) 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; 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=fail (p=NONE sp=NONE dis=NONE) header.from=arm.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S237402AbiG2QN3 (ORCPT + 99 others); Fri, 29 Jul 2022 12:13:29 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:38124 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S236850AbiG2QNV (ORCPT ); Fri, 29 Jul 2022 12:13:21 -0400 Received: from foss.arm.com (foss.arm.com [217.140.110.172]) by lindbergh.monkeyblade.net (Postfix) with ESMTP id 9836048C93; Fri, 29 Jul 2022 09:13:20 -0700 (PDT) Received: from usa-sjc-imap-foss1.foss.arm.com (unknown [10.121.207.14]) by usa-sjc-mx-foss1.foss.arm.com (Postfix) with ESMTP id F2962106F; Fri, 29 Jul 2022 09:13:20 -0700 (PDT) Received: from localhost.localdomain (H2XD2X12VG.cambridge.arm.com [10.1.36.148]) by usa-sjc-imap-foss1.foss.arm.com (Postfix) with ESMTPA id 946D23F73B; Fri, 29 Jul 2022 09:13:18 -0700 (PDT) From: =?UTF-8?q?Adri=C3=A1n=20Herrera=20Arcila?= To: linux-kernel@vger.kernel.org, linux-perf-users@vger.kernel.org, acme@kernel.org Cc: peterz@infradead.org, mingo@redhat.com, mark.rutland@arm.com, alexander.shishkin@linux.intel.com, jolsa@kernel.org, namhyung@kernel.org, leo.yan@linaro.org, songliubraving@fb.com, james.clark@arm.com, =?UTF-8?q?Adri=C3=A1n=20Herrera=20Arcila?= Subject: [PATCH 2/2] perf stat: fix unexpected delay behaviour Date: Fri, 29 Jul 2022 16:12:44 +0000 Message-Id: <20220729161244.10522-2-adrian.herrera@arm.com> X-Mailer: git-send-email 2.36.1 In-Reply-To: <20220729161244.10522-1-adrian.herrera@arm.com> References: <20220729161244.10522-1-adrian.herrera@arm.com> MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-6.9 required=5.0 tests=BAYES_00,RCVD_IN_DNSWL_HI, SPF_HELO_NONE,SPF_NONE 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 The described --delay behaviour is to delay the enablement of events, but not the execution of the command, if one is passed, which is incorrectly the current behaviour. This patch decouples the enablement from the delay, and enables events before or after launching the workload dependent on the options passed by the user. This code structure is inspired by that in perf-record, and tries to be consistent with it. Link: https://lore.kernel.org/linux-perf-users/7BFD066E-B0A8-49D4-B635-379328F0CF4C@fb.com Fixes: d0a0a511493d ("perf stat: Fix forked applications enablement of counters") Signed-off-by: Adrián Herrera Arcila --- tools/perf/builtin-stat.c | 56 ++++++++++++++++++++++----------------- 1 file changed, 32 insertions(+), 24 deletions(-) diff --git a/tools/perf/builtin-stat.c b/tools/perf/builtin-stat.c index 318ffd119dad..f98c823b16dd 100644 --- a/tools/perf/builtin-stat.c +++ b/tools/perf/builtin-stat.c @@ -559,7 +559,7 @@ static bool handle_interval(unsigned int interval, int *times) return false; } -static int enable_counters(void) +static int enable_bpf_counters(void) { struct evsel *evsel; int err; @@ -572,28 +572,6 @@ static int enable_counters(void) if (err) return err; } - - if (stat_config.initial_delay < 0) { - pr_info(EVLIST_DISABLED_MSG); - return 0; - } - - if (stat_config.initial_delay > 0) { - pr_info(EVLIST_DISABLED_MSG); - usleep(stat_config.initial_delay * USEC_PER_MSEC); - } - - /* - * We need to enable counters only if: - * - we don't have tracee (attaching to task or cpu) - * - we have initial delay configured - */ - if (!target__none(&target) || stat_config.initial_delay) { - if (!all_counters_use_bpf) - evlist__enable(evsel_list); - if (stat_config.initial_delay > 0) - pr_info(EVLIST_ENABLED_MSG); - } return 0; } @@ -966,10 +944,24 @@ static int __run_perf_stat(int argc, const char **argv, int run_idx) return err; } - err = enable_counters(); + err = enable_bpf_counters(); if (err) return -1; + /* + * Enable events manually here if perf-stat is run: + * 1. with a target (any of --all-cpus, --cpu, --pid or --tid) + * 2. without measurement delay (no --delay) + * 3. without all events associated to BPF + * + * This is because if run with a target, events are not enabled + * on exec if a workload is passed, and because there is no delay + * we ensure to enable them before the workload starts + */ + if (!target__none(&target) && !stat_config.initial_delay && + !all_counters_use_bpf) + evlist__enable(evsel_list); + /* Exec the command, if any */ if (forks) evlist__start_workload(evsel_list); @@ -977,6 +969,22 @@ static int __run_perf_stat(int argc, const char **argv, int run_idx) t0 = rdclock(); clock_gettime(CLOCK_MONOTONIC, &ref_time); + /* + * If a measurement delay was specified, start it, and if positive, + * enable events manually after. We respect the delay even if all + * events are associated to BPF + */ + if (stat_config.initial_delay) { + /* At this point, events are guaranteed disabled */ + pr_info(EVLIST_DISABLED_MSG); + if (stat_config.initial_delay > 0) { + usleep(stat_config.initial_delay * USEC_PER_MSEC); + if (!all_counters_use_bpf) + evlist__enable(evsel_list); + pr_info(EVLIST_ENABLED_MSG); + } + } + if (forks) { if (interval || timeout || evlist__ctlfd_initialized(evsel_list)) status = dispatch_events(forks, timeout, interval, ×); -- 2.36.1