From nobody Thu May 7 20:26:43 2026 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 60D07C433EF for ; Thu, 19 May 2022 03:20:29 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229953AbiESDU0 (ORCPT ); Wed, 18 May 2022 23:20:26 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:45540 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233175AbiESDUQ (ORCPT ); Wed, 18 May 2022 23:20:16 -0400 Received: from mail-yb1-xb4a.google.com (mail-yb1-xb4a.google.com [IPv6:2607:f8b0:4864:20::b4a]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 914F254BF7 for ; Wed, 18 May 2022 20:20:14 -0700 (PDT) Received: by mail-yb1-xb4a.google.com with SMTP id i17-20020a259d11000000b0064cd3084085so3321969ybp.9 for ; Wed, 18 May 2022 20:20:14 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20210112; h=date:in-reply-to:message-id:mime-version:references:subject:from:to :cc; bh=Ufua0rfEsqMCPLeh09lss9xKZavLX++00V1DsERJjuU=; b=C2Q3j4NOoL4Znn2cGiCWb4Z782Kd2RARJteKmjYTOGdWO6+Brzes/Fo4nRvRkBFGUB J81EYI6UBSwSDerNAoiP30Qs30Ii38BnskBz3WuRNFTJfZ5DujjkKEXhM5bS3io5SU7j LcQxaHJ+gy1ko9Zqrx1du2j26kXBIZS+6fUywTcYG0PL4WjiTEsXPXe7jY9TLdEdllpt 5CRxECRfwb/Zx+fAEWGZY/MhkC7ExuppQP2pXqWTe36nsC0hdiWuduiaPv+hfylYKL8X 8LHaBWv3xRmCkO0xvARKNFAWDYZb6sd6n3xIqRcRBdfbwxVk1cnz5pV9Ld9AUKsmCvFE 7gbQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:date:in-reply-to:message-id:mime-version :references:subject:from:to:cc; bh=Ufua0rfEsqMCPLeh09lss9xKZavLX++00V1DsERJjuU=; b=n/t1OSd3lKXMyQgR8jAAZMTqRJoCkVFV3Y7kxWoQ2zpArzBng1rJxw9CQplpp+4PAC WmlhchtH0gUx5D6ciRdejkEY2N8N8yxfqTW7Cocb3gfZ+YUL06277OVTe9/zsYwtK+jY I45xvgWeMC/s/kRZ353tyMiQ8w+UE8mOaTiNukNFwka4hbJmWsRP/M11ONHj74n2QQGb l2PA0RLue9GCJZpFdM/EbIyzka65JHV0qcjNSb3AN7RQSpSPGAnVFctvu39lxoUUeHMC V3WkeLXz+ZzKkaorlRew7aP7jybbtQ3xPbTirJy/SZe2ysoMF8Gr57PLXYM+jW2o4WKg Tt+A== X-Gm-Message-State: AOAM532/OffIc+EzBDrjAOQB/AejUE+rHzozPoMjUqu0mK984IV9wBpN YMq7099QGWm66D/sF6rs/vdI0doPF8yo X-Google-Smtp-Source: ABdhPJx3T641FWbwHWHy2wSXrWrEb93gC2BG4MiPOUUip0yv3E7lm0SEMw+8KmEckfWymjCEbsRsFDLqc+hA X-Received: from irogers.svl.corp.google.com ([2620:15c:2cd:202:a233:bf3c:6ac:2a98]) (user=irogers job=sendgmr) by 2002:a25:dc02:0:b0:64d:b6a3:e0bf with SMTP id y2-20020a25dc02000000b0064db6a3e0bfmr2460794ybe.41.1652930413690; Wed, 18 May 2022 20:20:13 -0700 (PDT) Date: Wed, 18 May 2022 20:20:01 -0700 In-Reply-To: <20220519032005.1273691-1-irogers@google.com> Message-Id: <20220519032005.1273691-2-irogers@google.com> Mime-Version: 1.0 References: <20220519032005.1273691-1-irogers@google.com> X-Mailer: git-send-email 2.36.1.124.g0e6072fb45-goog Subject: [PATCH 1/5] perf stat: Fix and validate inputs in stat events From: Ian Rogers To: Michael Petlan , Peter Zijlstra , Ingo Molnar , Arnaldo Carvalho de Melo , Mark Rutland , Alexander Shishkin , Jiri Olsa , Namhyung Kim , Alexei Starovoitov , Daniel Borkmann , Andrii Nakryiko , Martin KaFai Lau , Song Liu , Yonghong Song , John Fastabend , KP Singh , James Clark , Kan Liang , Quentin Monnet , Dave Marchevsky , Zhengjun Xing , Lv Ruyi , linux-perf-users@vger.kernel.org, linux-kernel@vger.kernel.org, netdev@vger.kernel.org, bpf@vger.kernel.org Cc: Stephane Eranian , Ian Rogers Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Stat events can come from disk and so need a degree of validation. They contain a CPU which needs looking up via CPU map to access a counter. Add the CPU to index translation, alongside validity checking. Discussion thread: https://lore.kernel.org/linux-perf-users/CAP-5=3DfWQR=3DsCuiSMktvUtcbOLidEp= UJLCybVF6=3DBRvORcDOq+g@mail.gmail.com/ Fixes: 7ac0089d138f ("perf evsel: Pass cpu not cpu map index to synthesize") Suggested-by: Michael Petlan Signed-off-by: Ian Rogers --- tools/perf/util/stat.c | 17 ++++++++++++++--- 1 file changed, 14 insertions(+), 3 deletions(-) diff --git a/tools/perf/util/stat.c b/tools/perf/util/stat.c index 4a5f3b8ff820..a77c28232298 100644 --- a/tools/perf/util/stat.c +++ b/tools/perf/util/stat.c @@ -474,9 +474,10 @@ int perf_stat_process_counter(struct perf_stat_config = *config, int perf_event__process_stat_event(struct perf_session *session, union perf_event *event) { - struct perf_counts_values count; + struct perf_counts_values count, *ptr; struct perf_record_stat *st =3D &event->stat; struct evsel *counter; + int cpu_map_idx; =20 count.val =3D st->val; count.ena =3D st->ena; @@ -487,8 +488,18 @@ int perf_event__process_stat_event(struct perf_session= *session, pr_err("Failed to resolve counter for stat event.\n"); return -EINVAL; } - - *perf_counts(counter->counts, st->cpu, st->thread) =3D count; + cpu_map_idx =3D perf_cpu_map__idx(evsel__cpus(counter), (struct perf_cpu)= {.cpu =3D st->cpu}); + if (cpu_map_idx =3D=3D -1) { + pr_err("Invalid CPU %d for event %s.\n", st->cpu, evsel__name(counter)); + return -EINVAL; + } + ptr =3D perf_counts(counter->counts, cpu_map_idx, st->thread); + if (ptr =3D=3D NULL) { + pr_err("Failed to find perf count for CPU %d thread %d on event %s.\n", + st->cpu, st->thread, evsel__name(counter)); + return -EINVAL; + } + *ptr =3D count; counter->supported =3D true; return 0; } --=20 2.36.1.124.g0e6072fb45-goog From nobody Thu May 7 20:26:43 2026 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 7D368C433F5 for ; Thu, 19 May 2022 03:20:40 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232342AbiESDUg (ORCPT ); Wed, 18 May 2022 23:20:36 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:45626 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233140AbiESDUR (ORCPT ); Wed, 18 May 2022 23:20:17 -0400 Received: from mail-yw1-x1149.google.com (mail-yw1-x1149.google.com [IPv6:2607:f8b0:4864:20::1149]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id F2B31562F8 for ; Wed, 18 May 2022 20:20:16 -0700 (PDT) Received: by mail-yw1-x1149.google.com with SMTP id 00721157ae682-2ff17535eabso35411677b3.20 for ; Wed, 18 May 2022 20:20:16 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20210112; h=date:in-reply-to:message-id:mime-version:references:subject:from:to :cc; bh=1limainpPz/ktpnhi2JQ14ZpISEsYFULN5eyOmNi+Sg=; b=pwFiLjJF7F42X0N/SJOqIg+cAOdZbOcMsmo5i34y2KmksTi8MtPUEB0qc2c1HLmR45 cwJ+5gBpTjYMw6GcKjAMzXgbB1OabmEHtfbVS54w4nOVjbgkSeaHB3iHWUXZHP2ALe3x VQEwVtR95jEfnd8/bokpy2LqYaPKYh9gBUuaxoXxMYCUTqaRobWi283YJ3TSSG41nrbt qRNBzT0SblEn+0obrQlf3TQmAJZjTysd9Nd3biW0tKlOZmJLCSbwZWbWOTBh/e52vftR eMjhM+TnOzRsRXC/6+QLoB7+4aMy32X6UJTMmOW4tvIOHKwWu8jbuidFy/Oo4R3UYu+U bfFg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:date:in-reply-to:message-id:mime-version :references:subject:from:to:cc; bh=1limainpPz/ktpnhi2JQ14ZpISEsYFULN5eyOmNi+Sg=; b=u5LuUCuqiWUcBmL8+spNCwGNztJsMsYGrEvUOlrHMMiCWicfF3syM7QBrPQf7E16En mMHbMWteAr1H7TVajHBhRoIiAAq5sXRNG11KKyMYxcYJ6qCm9EBxJKMnlWV35ODHyDSa lLfb0v7sVvRV5RDSUDbecXYqVCLAWM/lRUiURP9bPLaSsdDqrqcCKaGy1Lq46vFIXEcx chcPKXmjRmWsACbnSMixm/Ndz+0uCstQaV9SdTMguSLBnM+lSmESKzQu7j1q2vjCm4ac r9UtAEIxSVFVDnNXFh/R7mUMs9cjMWFWVfo/i92cx6ddFW5YN730DbUSLDo4OcOvkLL7 saeQ== X-Gm-Message-State: AOAM530XtFeqCYlXfpB1mxW3aat0rLpx/B8M16STKpSWSWbpmTZ+W11Q 1yShONTudARoZr6NINVRwjG6HwY2S1tY X-Google-Smtp-Source: ABdhPJy8cyFjJSoZ/WeYBph7EUcVHsLsZB60kq0ih2WbPF8sSVl+a37Ci3uckkELeD+m4XbHoiUKuH2yLtsG X-Received: from irogers.svl.corp.google.com ([2620:15c:2cd:202:a233:bf3c:6ac:2a98]) (user=irogers job=sendgmr) by 2002:a81:4104:0:b0:2f1:b59f:22a2 with SMTP id o4-20020a814104000000b002f1b59f22a2mr2662292ywa.328.1652930416067; Wed, 18 May 2022 20:20:16 -0700 (PDT) Date: Wed, 18 May 2022 20:20:02 -0700 In-Reply-To: <20220519032005.1273691-1-irogers@google.com> Message-Id: <20220519032005.1273691-3-irogers@google.com> Mime-Version: 1.0 References: <20220519032005.1273691-1-irogers@google.com> X-Mailer: git-send-email 2.36.1.124.g0e6072fb45-goog Subject: [PATCH 2/5] perf stat: Add stat record+report test From: Ian Rogers To: Michael Petlan , Peter Zijlstra , Ingo Molnar , Arnaldo Carvalho de Melo , Mark Rutland , Alexander Shishkin , Jiri Olsa , Namhyung Kim , Alexei Starovoitov , Daniel Borkmann , Andrii Nakryiko , Martin KaFai Lau , Song Liu , Yonghong Song , John Fastabend , KP Singh , James Clark , Kan Liang , Quentin Monnet , Dave Marchevsky , Zhengjun Xing , Lv Ruyi , linux-perf-users@vger.kernel.org, linux-kernel@vger.kernel.org, netdev@vger.kernel.org, bpf@vger.kernel.org Cc: Stephane Eranian , Ian Rogers Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" This would have caught: https://lore.kernel.org/linux-perf-users/CAP-5=3DfWQR=3DsCuiSMktvUtcbOLidEp= UJLCybVF6=3DBRvORcDOq+g@mail.gmail.com/ Signed-off-by: Ian Rogers --- tools/perf/tests/shell/stat.sh | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/tools/perf/tests/shell/stat.sh b/tools/perf/tests/shell/stat.sh index c7894764d4a6..9313ef2739e0 100755 --- a/tools/perf/tests/shell/stat.sh +++ b/tools/perf/tests/shell/stat.sh @@ -16,6 +16,18 @@ test_default_stat() { echo "Basic stat command test [Success]" } =20 +test_stat_record_report() { + echo "stat record and report test" + if ! perf stat record -o - true | perf stat report -i - 2>&1 | \ + egrep -q "Performance counter stats for 'pipe':" + then + echo "stat record and report test [Failed]" + err=3D1 + return + fi + echo "stat record and report test [Success]" +} + test_topdown_groups() { # Topdown events must be grouped with the slots event first. Test that # parse-events reorders this. @@ -62,6 +74,7 @@ test_topdown_weak_groups() { } =20 test_default_stat +test_stat_record_report test_topdown_groups test_topdown_weak_groups exit $err --=20 2.36.1.124.g0e6072fb45-goog From nobody Thu May 7 20:26:43 2026 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 44468C433FE for ; Thu, 19 May 2022 03:20:46 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S233314AbiESDUo (ORCPT ); Wed, 18 May 2022 23:20:44 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:46008 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233199AbiESDUa (ORCPT ); Wed, 18 May 2022 23:20:30 -0400 Received: from mail-yw1-x1149.google.com (mail-yw1-x1149.google.com [IPv6:2607:f8b0:4864:20::1149]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 2AE8356C2E for ; Wed, 18 May 2022 20:20:19 -0700 (PDT) Received: by mail-yw1-x1149.google.com with SMTP id 00721157ae682-2f8398e99dcso35841757b3.9 for ; Wed, 18 May 2022 20:20:19 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20210112; h=date:in-reply-to:message-id:mime-version:references:subject:from:to :cc; bh=Vw3KOirpyKTYToukj5eB9PrIoaDLrka25Tzs+xxN/h8=; b=Z2zErAYQFBvOZsDLGgCVtdKnayqjrK54AFt8EP2E2Oh158l/ncAdDQG5+nkAH0QlIV w7DnPDdy3FXOINVtVZBSOzvkUEgdfz4/+VoRyMyoOLgsr994CrD1CRg/RdsxA/GvT/Hz bm+Zf9z8iRcc0HpGKykfpNTndP5y04D6WwiZYddS+fKufM6Lx5V09WN5tsvU1ge+HQzY /qhqa3u9ZpQtKhRgjOGtsCLskUfiTO7Em0DfZMhyWFHgyQZhLF2WpF9G8TQvNeRMQtSK +CiaQJsSHY3iz9Kx8ES9aAG0WeS3ryGXwQ4Mkd0cqEVT1HYxMXCyp8t5FehS8WYhnB68 Q8bw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:date:in-reply-to:message-id:mime-version :references:subject:from:to:cc; bh=Vw3KOirpyKTYToukj5eB9PrIoaDLrka25Tzs+xxN/h8=; b=oyGwJvbKCbGdzGcmIW8ZkFblEaTtLX9wq9fs7IqGXTt5pJcfQS3JDnQIGFaVJmYaYs oRilWo56KeZJYqLJFRUvmeAyLNGV2JpyiOwFRCxIaV2b6VGBDlv2lF6h3r7MO+rdFp03 N+n+MpEattdnubPbk2XqcAaShexsxxVIVeaghwVyn86yLFxtAWzYsWPy5L9zxDTTMA9B DDUN3977rorDZ1dNmKMOr4E841rikdmk+RnyGcGOA/drSbDCM1TgoSzbtDZ0tUBeClnG FTZo9w6XjVxhJTWdKKJgiMZT7FjaHw9hM/l6yoVlk6FcKV3CpaTkWYiOP+sIBIkt0/sO XCnQ== X-Gm-Message-State: AOAM530+tt48eUgGZ0wQc9rAqHgMGys2VVbtO+r2esN9UcmgmhDxgq6j 0B6EX/5wMJxir3kv1aVhtk/nKlQfgGeV X-Google-Smtp-Source: ABdhPJyaaIVriNuy73np/4IMrVPFn3NEblefL3EEPUZ7Ayeru4qkvqLGkIUzJ87NJEh8n9bjaM+xtdCjwHRq X-Received: from irogers.svl.corp.google.com ([2620:15c:2cd:202:a233:bf3c:6ac:2a98]) (user=irogers job=sendgmr) by 2002:a25:2d67:0:b0:64d:a9b9:4954 with SMTP id s39-20020a252d67000000b0064da9b94954mr2547523ybe.5.1652930418306; Wed, 18 May 2022 20:20:18 -0700 (PDT) Date: Wed, 18 May 2022 20:20:03 -0700 In-Reply-To: <20220519032005.1273691-1-irogers@google.com> Message-Id: <20220519032005.1273691-4-irogers@google.com> Mime-Version: 1.0 References: <20220519032005.1273691-1-irogers@google.com> X-Mailer: git-send-email 2.36.1.124.g0e6072fb45-goog Subject: [PATCH 3/5] perf cpumap: Add perf_cpu_map__for_each_idx From: Ian Rogers To: Michael Petlan , Peter Zijlstra , Ingo Molnar , Arnaldo Carvalho de Melo , Mark Rutland , Alexander Shishkin , Jiri Olsa , Namhyung Kim , Alexei Starovoitov , Daniel Borkmann , Andrii Nakryiko , Martin KaFai Lau , Song Liu , Yonghong Song , John Fastabend , KP Singh , James Clark , Kan Liang , Quentin Monnet , Dave Marchevsky , Zhengjun Xing , Lv Ruyi , linux-perf-users@vger.kernel.org, linux-kernel@vger.kernel.org, netdev@vger.kernel.org, bpf@vger.kernel.org Cc: Stephane Eranian , Ian Rogers Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" A variant of perf_cpu_map__for_each_cpu that just iterates index values without the corresponding load of the CPU. Signed-off-by: Ian Rogers --- tools/lib/perf/include/perf/cpumap.h | 3 +++ 1 file changed, 3 insertions(+) diff --git a/tools/lib/perf/include/perf/cpumap.h b/tools/lib/perf/include/= perf/cpumap.h index 4a2edbdb5e2b..24de795b09bb 100644 --- a/tools/lib/perf/include/perf/cpumap.h +++ b/tools/lib/perf/include/perf/cpumap.h @@ -31,4 +31,7 @@ LIBPERF_API bool perf_cpu_map__has(const struct perf_cpu_= map *map, struct perf_c (idx) < perf_cpu_map__nr(cpus); \ (idx)++, (cpu) =3D perf_cpu_map__cpu(cpus, idx)) =20 +#define perf_cpu_map__for_each_idx(idx, cpus) \ + for ((idx) =3D 0; (idx) < perf_cpu_map__nr(cpus); (idx)++) + #endif /* __LIBPERF_CPUMAP_H */ --=20 2.36.1.124.g0e6072fb45-goog From nobody Thu May 7 20:26:43 2026 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 39CD9C433EF for ; Thu, 19 May 2022 03:21:16 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S233255AbiESDUw (ORCPT ); Wed, 18 May 2022 23:20:52 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:46286 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233234AbiESDUk (ORCPT ); Wed, 18 May 2022 23:20:40 -0400 Received: from mail-yb1-xb4a.google.com (mail-yb1-xb4a.google.com [IPv6:2607:f8b0:4864:20::b4a]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 871879AE56 for ; Wed, 18 May 2022 20:20:21 -0700 (PDT) Received: by mail-yb1-xb4a.google.com with SMTP id e202-20020a25e7d3000000b0064dc69dfa9cso3263003ybh.16 for ; Wed, 18 May 2022 20:20:21 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20210112; h=date:in-reply-to:message-id:mime-version:references:subject:from:to :cc; bh=dwmBV4G5Hrx+MlkRr6GaHrCf5HeOC3stEpj1wSJ3O8A=; b=o5NjPzUznWE13DNTK0wFIG1NKbT9cGiL1lhWxHLGQ5r4bBD8e+wKWhYW3xk+khrigG p3tBuKe6wk9kILLORw3RjNJgw3fqxOvAEaLz5tmFWYrQen1SMb3WKawQMFJShV1sw045 6iL9u6tHbnB4pXOc9c6ABTFFyfysO6NiVWBmEFBu4urYthwKcLYO0JgBZ2O9zSjB5Ts/ OdPHakQX8hM2snxYehC8fyLK8FCJQxjZv22U/i+kN81boFVA+QYITEfEXoRMV+OYQVmA T/RiO5aoShu5XWQgrx+lb4PJs44plrEyd0wRRsN4CuB5yPzK+oAWfZ7oVifHOgBeNsB+ EuMA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:date:in-reply-to:message-id:mime-version :references:subject:from:to:cc; bh=dwmBV4G5Hrx+MlkRr6GaHrCf5HeOC3stEpj1wSJ3O8A=; b=xumLI3c/W4T9MGBJuz8WSGV9sjh4cZftdqhhxrWoD0etvXbERs1l5ktv6jDxicBSLi 1AbzmbRrMFdxvZa1WgdtfAd4X0AUmWC5lshdijMjNJD66zylIpvd9W56iqysir2xvzLh lvbDQrPD/d3IjLUIOYOVGrnCpBIZ19hOCeIQvrZqOQKNYVTqBKIYwSOWlVpckQdTSkxp f2kaDRDLc95F3F5MXhhaeeFZwHc8wPcqoMHtdisj4J6SEDoQqf69JQdBc0Npo9dhRxTl 5wfqJthCDJWcSz/bWreDNZe0jKQIQp6LGeEFw95jT+UrKKBJmAXkNf332s/S2oUJJldf x5pg== X-Gm-Message-State: AOAM531D8TLOunXEwz6w6TQZxKPg+8PLrkxmdta3IZ6GN5Ez5QHNPZdG J0S+SHqaGav4iThowJbfciPgYdIRtZFU X-Google-Smtp-Source: ABdhPJzSCHP7XeMQopIcnWVduwBBs6JvQNIRox8q0b96Ga/SO581j62Doa4cRRTYzHBanNwOkT+LHDHRV6G3 X-Received: from irogers.svl.corp.google.com ([2620:15c:2cd:202:a233:bf3c:6ac:2a98]) (user=irogers job=sendgmr) by 2002:a25:230f:0:b0:64d:76e2:6aa5 with SMTP id j15-20020a25230f000000b0064d76e26aa5mr2481170ybj.116.1652930420577; Wed, 18 May 2022 20:20:20 -0700 (PDT) Date: Wed, 18 May 2022 20:20:04 -0700 In-Reply-To: <20220519032005.1273691-1-irogers@google.com> Message-Id: <20220519032005.1273691-5-irogers@google.com> Mime-Version: 1.0 References: <20220519032005.1273691-1-irogers@google.com> X-Mailer: git-send-email 2.36.1.124.g0e6072fb45-goog Subject: [PATCH 4/5] perf bpf_counter: Tidy use of CPU map index From: Ian Rogers To: Michael Petlan , Peter Zijlstra , Ingo Molnar , Arnaldo Carvalho de Melo , Mark Rutland , Alexander Shishkin , Jiri Olsa , Namhyung Kim , Alexei Starovoitov , Daniel Borkmann , Andrii Nakryiko , Martin KaFai Lau , Song Liu , Yonghong Song , John Fastabend , KP Singh , James Clark , Kan Liang , Quentin Monnet , Dave Marchevsky , Zhengjun Xing , Lv Ruyi , linux-perf-users@vger.kernel.org, linux-kernel@vger.kernel.org, netdev@vger.kernel.org, bpf@vger.kernel.org Cc: Stephane Eranian , Ian Rogers Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" BPF counters are typically running across all CPUs and so the CPU map index and CPU number are the same. There may be cases with offline CPUs where this isn't the case and so ensure the cpu map index for perf_counts is going to be a valid index by explicitly iterating over the CPU map. This also makes it clearer that users of perf_counts are using an index. Collapse some multiple uses of perf_counts into single uses. Signed-off-by: Ian Rogers --- tools/perf/util/bpf_counter.c | 61 ++++++++++++++++++++--------------- 1 file changed, 35 insertions(+), 26 deletions(-) diff --git a/tools/perf/util/bpf_counter.c b/tools/perf/util/bpf_counter.c index 3ce8d03cb7ec..d4931f54e1dd 100644 --- a/tools/perf/util/bpf_counter.c +++ b/tools/perf/util/bpf_counter.c @@ -224,25 +224,25 @@ static int bpf_program_profiler__disable(struct evsel= *evsel) =20 static int bpf_program_profiler__read(struct evsel *evsel) { - // perf_cpu_map uses /sys/devices/system/cpu/online - int num_cpu =3D evsel__nr_cpus(evsel); // BPF_MAP_TYPE_PERCPU_ARRAY uses /sys/devices/system/cpu/possible // Sometimes possible > online, like on a Ryzen 3900X that has 24 // threads but its possible showed 0-31 -acme int num_cpu_bpf =3D libbpf_num_possible_cpus(); struct bpf_perf_event_value values[num_cpu_bpf]; struct bpf_counter *counter; + struct perf_counts_values *counts; int reading_map_fd; __u32 key =3D 0; - int err, cpu; + int err, idx, bpf_cpu; =20 if (list_empty(&evsel->bpf_counter_list)) return -EAGAIN; =20 - for (cpu =3D 0; cpu < num_cpu; cpu++) { - perf_counts(evsel->counts, cpu, 0)->val =3D 0; - perf_counts(evsel->counts, cpu, 0)->ena =3D 0; - perf_counts(evsel->counts, cpu, 0)->run =3D 0; + perf_cpu_map__for_each_idx(idx, evsel__cpus(evsel)) { + counts =3D perf_counts(evsel->counts, idx, 0); + counts->val =3D 0; + counts->ena =3D 0; + counts->run =3D 0; } list_for_each_entry(counter, &evsel->bpf_counter_list, list) { struct bpf_prog_profiler_bpf *skel =3D counter->skel; @@ -256,10 +256,15 @@ static int bpf_program_profiler__read(struct evsel *e= vsel) return err; } =20 - for (cpu =3D 0; cpu < num_cpu; cpu++) { - perf_counts(evsel->counts, cpu, 0)->val +=3D values[cpu].counter; - perf_counts(evsel->counts, cpu, 0)->ena +=3D values[cpu].enabled; - perf_counts(evsel->counts, cpu, 0)->run +=3D values[cpu].running; + for (bpf_cpu =3D 0; bpf_cpu < num_cpu_bpf; bpf_cpu++) { + idx =3D perf_cpu_map__idx(evsel__cpus(evsel), + (struct perf_cpu){.cpu =3D bpf_cpu}); + if (idx =3D=3D -1) + continue; + counts =3D perf_counts(evsel->counts, idx, 0); + counts->val +=3D values[bpf_cpu].counter; + counts->ena +=3D values[bpf_cpu].enabled; + counts->run +=3D values[bpf_cpu].running; } } return 0; @@ -621,6 +626,7 @@ static int bperf__read(struct evsel *evsel) struct bperf_follower_bpf *skel =3D evsel->follower_skel; __u32 num_cpu_bpf =3D cpu__max_cpu().cpu; struct bpf_perf_event_value values[num_cpu_bpf]; + struct perf_counts_values *counts; int reading_map_fd, err =3D 0; __u32 i; int j; @@ -639,29 +645,32 @@ static int bperf__read(struct evsel *evsel) case BPERF_FILTER_GLOBAL: assert(i =3D=3D 0); =20 - perf_cpu_map__for_each_cpu(entry, j, all_cpu_map) { - cpu =3D entry.cpu; - perf_counts(evsel->counts, cpu, 0)->val =3D values[cpu].counter; - perf_counts(evsel->counts, cpu, 0)->ena =3D values[cpu].enabled; - perf_counts(evsel->counts, cpu, 0)->run =3D values[cpu].running; + perf_cpu_map__for_each_cpu(entry, j, evsel__cpus(evsel)) { + counts =3D perf_counts(evsel->counts, j, 0); + counts->val =3D values[entry.cpu].counter; + counts->ena =3D values[entry.cpu].enabled; + counts->run =3D values[entry.cpu].running; } break; case BPERF_FILTER_CPU: - cpu =3D evsel->core.cpus->map[i].cpu; - perf_counts(evsel->counts, i, 0)->val =3D values[cpu].counter; - perf_counts(evsel->counts, i, 0)->ena =3D values[cpu].enabled; - perf_counts(evsel->counts, i, 0)->run =3D values[cpu].running; + cpu =3D perf_cpu_map__cpu(evsel__cpus(evsel), i).cpu; + assert(cpu >=3D 0); + counts =3D perf_counts(evsel->counts, i, 0); + counts->val =3D values[cpu].counter; + counts->ena =3D values[cpu].enabled; + counts->run =3D values[cpu].running; break; case BPERF_FILTER_PID: case BPERF_FILTER_TGID: - perf_counts(evsel->counts, 0, i)->val =3D 0; - perf_counts(evsel->counts, 0, i)->ena =3D 0; - perf_counts(evsel->counts, 0, i)->run =3D 0; + counts =3D perf_counts(evsel->counts, 0, i); + counts->val =3D 0; + counts->ena =3D 0; + counts->run =3D 0; =20 for (cpu =3D 0; cpu < num_cpu_bpf; cpu++) { - perf_counts(evsel->counts, 0, i)->val +=3D values[cpu].counter; - perf_counts(evsel->counts, 0, i)->ena +=3D values[cpu].enabled; - perf_counts(evsel->counts, 0, i)->run +=3D values[cpu].running; + counts->val +=3D values[cpu].counter; + counts->ena +=3D values[cpu].enabled; + counts->run +=3D values[cpu].running; } break; default: --=20 2.36.1.124.g0e6072fb45-goog From nobody Thu May 7 20:26:43 2026 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 20900C433F5 for ; Thu, 19 May 2022 03:21:26 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232249AbiESDVW (ORCPT ); Wed, 18 May 2022 23:21:22 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:45928 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233197AbiESDUm (ORCPT ); Wed, 18 May 2022 23:20:42 -0400 Received: from mail-yw1-x1149.google.com (mail-yw1-x1149.google.com [IPv6:2607:f8b0:4864:20::1149]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id BE094D9EAB for ; Wed, 18 May 2022 20:20:23 -0700 (PDT) Received: by mail-yw1-x1149.google.com with SMTP id 00721157ae682-2fede3f229cso35849267b3.8 for ; Wed, 18 May 2022 20:20:23 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20210112; h=date:in-reply-to:message-id:mime-version:references:subject:from:to :cc; bh=zOPOwprRKTfkv2cXPwTUV+M8mGP7sAVRSfOVmHTlIYc=; b=WgulU1MbbPm1ebPPVzee939RrLS/wfdClBy6WBsf9AxICbC5Jn4XjlZn2jzTFpNpCt cA1TFPFpox6xAhSvd2pr2XEpxu+K5vMcfO471UGnyxPePEvaDERSfh+RSH8uolfL4XN1 9ROqphMUPAw44LRD7MTSe+0oUcTe8FJrSqLGkhYJY2xh30dCJnUNpzxU1q+uv81GirGh V3QH3DRXWAd1GenJoyU54JsP8CHQfayRQyyvyTKb7W/+a5lrSYHTm830EF/Av3Ob4u4j VvQQ7p5q0HdTP/gcguhnhukM3bfWOVBjwoyVJrGIXLqO8vRMtVmNEwjcEaRq6Yb5GAW2 dGiw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:date:in-reply-to:message-id:mime-version :references:subject:from:to:cc; bh=zOPOwprRKTfkv2cXPwTUV+M8mGP7sAVRSfOVmHTlIYc=; b=LyZ8wcBO8OkDdp4CYkADdI0yhArGGExtLSTdb4a1tw8pijvi1CmVqiO8bM/JHNnF27 a+K9h9n75kPJngCW+7F37EPhlqO+pBe/hM0eHVpmnDaDGOGVwuRQuHIJN+dDHMXiIsOQ MesZkgI9gKZ8W3qWucbrX/VP2vKpVpTfST+ZA5j6ilfwtrdhjuOyM85PrQ6e5KacTXgc uFT1crsK4YG3eGmrpJ6jD4u2xCFMYV6FrKBys9fRMucmwKFF1K5MKEcBw+k1n5uPBfo8 CoHwJbKRWw3AHviEe/MriF9MOfWI7tXll8Qx6xNoEjtFjFy8v6ufqCjlipaO9HcVE+10 UHGA== X-Gm-Message-State: AOAM532Yb3K8kyYnl2R+4hekGEhKJTE47Z2wW2NNgbNAHC1zM5+UAKDK Wjic3qphB9Yz6ZLtgjbHgHBWG+g55+TM X-Google-Smtp-Source: ABdhPJy7PWcWfvRRE3C2+0YljTgf2w/TKm+iAAXYj/ISK6hEj7VzZa35KP78WgH+HVtPpjpMIzDkfvY/bJ53 X-Received: from irogers.svl.corp.google.com ([2620:15c:2cd:202:a233:bf3c:6ac:2a98]) (user=irogers job=sendgmr) by 2002:a81:dd07:0:b0:2eb:f701:286e with SMTP id e7-20020a81dd07000000b002ebf701286emr2483795ywn.341.1652930422878; Wed, 18 May 2022 20:20:22 -0700 (PDT) Date: Wed, 18 May 2022 20:20:05 -0700 In-Reply-To: <20220519032005.1273691-1-irogers@google.com> Message-Id: <20220519032005.1273691-6-irogers@google.com> Mime-Version: 1.0 References: <20220519032005.1273691-1-irogers@google.com> X-Mailer: git-send-email 2.36.1.124.g0e6072fb45-goog Subject: [PATCH 5/5] perf stat: Make use of index clearer with perf_counts From: Ian Rogers To: Michael Petlan , Peter Zijlstra , Ingo Molnar , Arnaldo Carvalho de Melo , Mark Rutland , Alexander Shishkin , Jiri Olsa , Namhyung Kim , Alexei Starovoitov , Daniel Borkmann , Andrii Nakryiko , Martin KaFai Lau , Song Liu , Yonghong Song , John Fastabend , KP Singh , James Clark , Kan Liang , Quentin Monnet , Dave Marchevsky , Zhengjun Xing , Lv Ruyi , linux-perf-users@vger.kernel.org, linux-kernel@vger.kernel.org, netdev@vger.kernel.org, bpf@vger.kernel.org Cc: Stephane Eranian , Ian Rogers Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Try to disambiguate further when perf_counts is being accessed it is with a cpu map index rather than a CPU. Signed-off-by: Ian Rogers --- tools/perf/util/stat-display.c | 22 ++++++++++++---------- tools/perf/util/stat.c | 10 ++++------ 2 files changed, 16 insertions(+), 16 deletions(-) diff --git a/tools/perf/util/stat-display.c b/tools/perf/util/stat-display.c index 98669ca5a86b..606f09b09226 100644 --- a/tools/perf/util/stat-display.c +++ b/tools/perf/util/stat-display.c @@ -764,11 +764,11 @@ static int cmp_val(const void *a, const void *b) =20 static struct perf_aggr_thread_value *sort_aggr_thread( struct evsel *counter, - int nthreads, int ncpus, int *ret, struct target *_target) { - int cpu, thread, i =3D 0; + int nthreads =3D perf_thread_map__nr(counter->core.threads); + int i =3D 0; double uval; struct perf_aggr_thread_value *buf; =20 @@ -776,13 +776,17 @@ static struct perf_aggr_thread_value *sort_aggr_threa= d( if (!buf) return NULL; =20 - for (thread =3D 0; thread < nthreads; thread++) { + for (int thread =3D 0; thread < nthreads; thread++) { + int idx; u64 ena =3D 0, run =3D 0, val =3D 0; =20 - for (cpu =3D 0; cpu < ncpus; cpu++) { - val +=3D perf_counts(counter->counts, cpu, thread)->val; - ena +=3D perf_counts(counter->counts, cpu, thread)->ena; - run +=3D perf_counts(counter->counts, cpu, thread)->run; + perf_cpu_map__for_each_idx(idx, evsel__cpus(counter)) { + struct perf_counts_values *counts =3D + perf_counts(counter->counts, idx, thread); + + val +=3D counts->val; + ena +=3D counts->ena; + run +=3D counts->run; } =20 uval =3D val * counter->scale; @@ -817,13 +821,11 @@ static void print_aggr_thread(struct perf_stat_config= *config, struct evsel *counter, char *prefix) { FILE *output =3D config->output; - int nthreads =3D perf_thread_map__nr(counter->core.threads); - int ncpus =3D perf_cpu_map__nr(counter->core.cpus); int thread, sorted_threads; struct aggr_cpu_id id; struct perf_aggr_thread_value *buf; =20 - buf =3D sort_aggr_thread(counter, nthreads, ncpus, &sorted_threads, _targ= et); + buf =3D sort_aggr_thread(counter, &sorted_threads, _target); if (!buf) { perror("cannot sort aggr thread"); return; diff --git a/tools/perf/util/stat.c b/tools/perf/util/stat.c index a77c28232298..37ea2d044708 100644 --- a/tools/perf/util/stat.c +++ b/tools/perf/util/stat.c @@ -237,14 +237,12 @@ void evlist__reset_prev_raw_counts(struct evlist *evl= ist) =20 static void evsel__copy_prev_raw_counts(struct evsel *evsel) { - int ncpus =3D evsel__nr_cpus(evsel); - int nthreads =3D perf_thread_map__nr(evsel->core.threads); + int idx, nthreads =3D perf_thread_map__nr(evsel->core.threads); =20 for (int thread =3D 0; thread < nthreads; thread++) { - for (int cpu =3D 0; cpu < ncpus; cpu++) { - *perf_counts(evsel->counts, cpu, thread) =3D - *perf_counts(evsel->prev_raw_counts, cpu, - thread); + perf_cpu_map__for_each_idx(idx, evsel__cpus(evsel)) { + *perf_counts(evsel->counts, idx, thread) =3D + *perf_counts(evsel->prev_raw_counts, idx, thread); } } =20 --=20 2.36.1.124.g0e6072fb45-goog