Received: by 2002:a05:6a10:2726:0:0:0:0 with SMTP id ib38csp848690pxb; Wed, 6 Apr 2022 01:50:35 -0700 (PDT) X-Google-Smtp-Source: ABdhPJxZQcoyDomfHOicEtQoEPKZNv3Rk7huDWDcEa5jSjYA5ATdb/njLIP3EJj7hzpHfuCTBEhb X-Received: by 2002:a63:e442:0:b0:398:c06b:169c with SMTP id i2-20020a63e442000000b00398c06b169cmr6358518pgk.170.1649235035181; Wed, 06 Apr 2022 01:50:35 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1649235035; cv=none; d=google.com; s=arc-20160816; b=E7fOwrn3uMsUvI9qdUymYR40gw5ygiDJhTGrWVJPRD2FjZxEbI6uaI4ZEzeFs7ycid o5wxkWL/6irvp420BMxRIbxESODdsLzz9HPekeD9/kZhGgCiCHcrsPsxjl5Mo+qHEJ8u gGp/CZaF/lU14I5/PS2nCU51d/ZgtfqpxSY5L68tE2kbbOeEh81l4pqnpXyqQ+Fd1KX8 pfaZ4u/CaGS2xFzz5zvBc5tbIs9l3kRWKzrzI8WKB+GViHgjZ+4/qzSHvOFkIO74ngH4 uYl2rRCA1Om6cicLFxGzzI112z//PE9YiH8t0LzlruiTdKoiayG4DQvTim4EEjtcu20n cIjw== 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 :user-agent:references:in-reply-to:message-id:date:subject:cc:to :from:dkim-signature; bh=r27cSq6vmi+A1hbdPKAVdvQ292v0I0M/Zky3YcSf/54=; b=UnKxBoJATufWgOOxYQYJBUOTvsLf3TDsJ1t/bgaeOOew5Vy5adxEwv5NCmfN2nyTH1 coXffOTCJbUOaV21AEWNhyar716VDoiYuOrvkDu/JaU5pPKiB3zbwvJMc+f978Udah+q iBoeyrxcaVlXioPlVesH3/GhtbAWMMNKwpQi8JV6Ukrtiq390hlalPK3vLu3Y3OdI9uZ G2NZMtBw3F0AsfZwAtFX8nTuZxCnx7+Pj12271UV9vqX/CP0gsU9jewsyoJBTgdmMqud 3P0oBY3xaSauhiCCFJncI3e/UE8IekQp7XNOO9m87p6wFzjhxYPYp9WomwnmgTpiCA+G DIJQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=eGgJk6FI; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linuxfoundation.org Return-Path: Received: from lindbergh.monkeyblade.net (lindbergh.monkeyblade.net. [2620:137:e000::1:18]) by mx.google.com with ESMTPS id b2-20020a170902d88200b00153b2d165e4si15027559plz.492.2022.04.06.01.50.34 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 06 Apr 2022 01:50:35 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:18 as permitted sender) client-ip=2620:137:e000::1:18; Authentication-Results: mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=eGgJk6FI; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linuxfoundation.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by lindbergh.monkeyblade.net (Postfix) with ESMTP id 06ADE2CDBCD; Wed, 6 Apr 2022 01:35:06 -0700 (PDT) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1839257AbiDFA6n (ORCPT + 99 others); Tue, 5 Apr 2022 20:58:43 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:54244 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1349560AbiDEJuQ (ORCPT ); Tue, 5 Apr 2022 05:50:16 -0400 Received: from dfw.source.kernel.org (dfw.source.kernel.org [IPv6:2604:1380:4641:c500::1]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 066A19B; Tue, 5 Apr 2022 02:48:19 -0700 (PDT) Received: from smtp.kernel.org (relay.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by dfw.source.kernel.org (Postfix) with ESMTPS id 97A05615E5; Tue, 5 Apr 2022 09:48:18 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 7D80BC385A1; Tue, 5 Apr 2022 09:48:17 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1649152098; bh=do+8eEDPBQpwYEPcf4XkVabUT7HYYAJoLGuwgo0KY/c=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=eGgJk6FIfNO9v6hJv0/RWLdpZSrtPQCRTPREGRmIAMmMDq8GbCJffD6gbn0B0N1zi zjWUq/PcTWRZvcvIJKrxP8uG1EioGQrf3C/FK/a2z5+gz5GVuY9aWmTMwC8L1IZ6Yu B75yHycSIsNOfCsc2+y2HtdbS6mvLr5xaAOYm02g= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, Thomas Richter , Jiri Olsa , Namhyung Kim , Sumanth Korikkar , Heiko Carstens , Sven Schnelle , Vasily Gorbik , Arnaldo Carvalho de Melo , Sasha Levin Subject: [PATCH 5.15 653/913] perf stat: Fix forked applications enablement of counters Date: Tue, 5 Apr 2022 09:28:35 +0200 Message-Id: <20220405070359.412755534@linuxfoundation.org> X-Mailer: git-send-email 2.35.1 In-Reply-To: <20220405070339.801210740@linuxfoundation.org> References: <20220405070339.801210740@linuxfoundation.org> User-Agent: quilt/0.66 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-2.0 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS, MAILING_LIST_MULTI,RDNS_NONE,SPF_HELO_NONE,T_SCC_BODY_TEXT_LINE 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 From: Thomas Richter [ Upstream commit d0a0a511493d269514fcbd852481cdca32c95350 ] I have run into the following issue: # perf stat -a -e new_pmu/INSTRUCTION_7/ -- mytest -c1 7 Performance counter stats for 'system wide': 0 new_pmu/INSTRUCTION_7/ 0.000366428 seconds time elapsed # The new PMU for s390 counts the execution of certain CPU instructions. The root cause is the extremely small run time of the mytest program. It just executes some assembly instructions and then exits. In above invocation the instruction is executed exactly one time (-c1 option). The PMU is expected to report this one time execution by a counter value of one, but fails to do so in some cases, not all. Debugging reveals the invocation of the child process is done *before* the counter events are installed and enabled. Tracing reveals that sometimes the child process starts and exits before the event is installed on all CPUs. The more CPUs the machine has, the more often this miscount happens. Fix this by reversing the start of the work load after the events have been installed on the specified CPUs. Now the comment also matches the code. Output after: # perf stat -a -e new_pmu/INSTRUCTION_7/ -- mytest -c1 7 Performance counter stats for 'system wide': 1 new_pmu/INSTRUCTION_7/ 0.000366428 seconds time elapsed # Now the correct result is reported rock solid all the time regardless how many CPUs are online. Reviewers notes: Jiri: Right, without -a the event has enable_on_exec so the race does not matter, but it's a problem for system wide with fork. Namhyung: Agreed. Also we may move the enable_counters() and the clock code out of the if block to be shared with the else block. Fixes: acf2892270dcc428 ("perf stat: Use perf_evlist__prepare/start_workload()") Signed-off-by: Thomas Richter Acked-by: Jiri Olsa Acked-by: Namhyung Kim Acked-by: Sumanth Korikkar Cc: Heiko Carstens Cc: Sven Schnelle Cc: Vasily Gorbik Link: https://lore.kernel.org/r/20220317155346.577384-1-tmricht@linux.ibm.com Signed-off-by: Arnaldo Carvalho de Melo Signed-off-by: Sasha Levin --- tools/perf/builtin-stat.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tools/perf/builtin-stat.c b/tools/perf/builtin-stat.c index f0ecfda34ece..1a194edb5452 100644 --- a/tools/perf/builtin-stat.c +++ b/tools/perf/builtin-stat.c @@ -956,10 +956,10 @@ static int __run_perf_stat(int argc, const char **argv, int run_idx) * Enable counters and exec the command: */ if (forks) { - evlist__start_workload(evsel_list); err = enable_counters(); if (err) return -1; + evlist__start_workload(evsel_list); t0 = rdclock(); clock_gettime(CLOCK_MONOTONIC, &ref_time); -- 2.34.1