From patchwork Thu May 19 03:20:01 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Ian Rogers X-Patchwork-Id: 12854483 X-Patchwork-Delegate: bpf@iogearbox.net 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 7418DC433F5 for ; Thu, 19 May 2022 03:20:22 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S233186AbiESDUT (ORCPT ); Wed, 18 May 2022 23:20:19 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:45542 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233179AbiESDUQ (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 9137F506CD for ; Wed, 18 May 2022 20:20:14 -0700 (PDT) Received: by mail-yb1-xb4a.google.com with SMTP id k17-20020a252411000000b0064ea6c388baso2538017ybk.0 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=T2yevF4uW+L1//6XLJwCN7R3ufgsda+Y1GTznHKvtJ5yQGX2wmR+gDqzu1hX4InYAp WZLydDLvsfiKgwdwY639hOgBmuSithkR7pNb/axaKPxIlnlNicaPHmjHsdfMZLiJmSq3 KPxuJ5GwnARCdE5jKdtsoBBA3oHRnEu8ysOcLi53FTfREPNk8LqRC9c6Ay21RfArVzfF Hdap3PvWJOOVTs5hh7MU6yuarzqLWFgyqpp1XBpJpSur3zI00yz+QKhkUzZny6wEBLlY fuxa5T01ekCRhOUudQTy1akViTF0U6qI78j1lCxd8XjKIhR8qDskbhibmkBC+U/TT8uP zZXw== X-Gm-Message-State: AOAM533/SQy7jk7Nbfiue3Ui5EXrisZLFj1OyZDQdSv926oKbtZe4758 +tHWgD9EwA1rfGlf1OXAdIjl2+/HQ+82 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: netdev@vger.kernel.org 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=fWQR=sCuiSMktvUtcbOLidEpUJLCybVF6=BRvORcDOq+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 = &event->stat; struct evsel *counter; + int cpu_map_idx; count.val = st->val; count.ena = 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) = count; + cpu_map_idx = perf_cpu_map__idx(evsel__cpus(counter), (struct perf_cpu){.cpu = st->cpu}); + if (cpu_map_idx == -1) { + pr_err("Invalid CPU %d for event %s.\n", st->cpu, evsel__name(counter)); + return -EINVAL; + } + ptr = perf_counts(counter->counts, cpu_map_idx, st->thread); + if (ptr == 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 = count; counter->supported = true; return 0; } From patchwork Thu May 19 03:20:02 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Ian Rogers X-Patchwork-Id: 12854484 X-Patchwork-Delegate: bpf@iogearbox.net 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 B7ED0C433EF for ; Thu, 19 May 2022 03:20:36 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S233227AbiESDUc (ORCPT ); Wed, 18 May 2022 23:20:32 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:45628 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233152AbiESDUS (ORCPT ); Wed, 18 May 2022 23:20:18 -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 F3A7356432 for ; Wed, 18 May 2022 20:20:16 -0700 (PDT) Received: by mail-yb1-xb4a.google.com with SMTP id j2-20020a2597c2000000b0064b3e54191aso3280023ybo.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=OqO5esNPOa+oply7p+G4RpzQMF7frAKNiMGojXeFWyGznZifGBHan4ES6zzC+cYzdE Hv+duGPl6IZq44wKZaHecbf1Mg7q8FE4jJFBVDOTv787i5/iEyuxYwe2OnTunrGzLHal kLJrPf/n+uOmZf40L0bVNAcWIDS9zcXJRn0uhdZjymta3VaTnYo7PRwP1Dn24xYxjNpm dHmLyo3zySZBZK73ORzlZ95qetpi4660yX52kcWCkim398q/8ZhcNZDtkzCJqAmpSgOZ VJOEPFbyZi2WR2eJpeXl058/51uRq7h2MomzTNn7AON7R06pYvG3nwbGTl5nZqrJlzkL sXcQ== X-Gm-Message-State: AOAM530fCWWiL9R96CQzAXo+jTO5J8CecL2JAOSnnjE8lwwsT6Q87/hN dqaCuAOQsACOglpjY9BLbJCfMvNaGuOD 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: netdev@vger.kernel.org This would have caught: https://lore.kernel.org/linux-perf-users/CAP-5=fWQR=sCuiSMktvUtcbOLidEpUJLCybVF6=BRvORcDOq+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]" } +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=1 + 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() { } test_default_stat +test_stat_record_report test_topdown_groups test_topdown_weak_groups exit $err From patchwork Thu May 19 03:20:03 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Ian Rogers X-Patchwork-Id: 12854485 X-Patchwork-Delegate: bpf@iogearbox.net 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 7DFB9C433EF for ; Thu, 19 May 2022 03:20:43 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S233240AbiESDUl (ORCPT ); Wed, 18 May 2022 23:20:41 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:45786 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233175AbiESDUa (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 5D54D57118 for ; Wed, 18 May 2022 20:20:19 -0700 (PDT) Received: by mail-yw1-x1149.google.com with SMTP id 00721157ae682-2eb7d137101so35726287b3.12 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=jS7uJW77ZjaXWy7WK2UGqHT3seM+HgBhGjV6X4kMmiuI15PYoa/zGCrS/8P5J34i0l amCuSV/ia5r6I/oH7Ye7JEEUmg9/aXkQCOeO1Ppj+66KcThSh+pBVY0iS2vf+pg2CvVS QFETWuYPdWJcrdRuTP/PCirT2eGOFoH2PvZXw3YKDxZPzakaB5DRmSsfX4w1W8e6A8QT BsKrGE3aImZ0kAxVNYasrJNLkMtlqFqdFS0ZTQl3oiL19FI+cjHMx3K5jO0jiSugpDYz 5Elv6zB7JiHryffYOlRBDm9s1G0tgLfPM3WZEzIFfI6w0y/vUIr42AtH18F7pdpbrF9K AD1w== X-Gm-Message-State: AOAM532bqgwMrjC6CBcvdgz9mLzSKqgv1Hd1baOg+SJLcZ3AERPxm5L6 J1GRjd2sKfoZJRM4Zjs5h1YWpOOWofxI 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: netdev@vger.kernel.org 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) = perf_cpu_map__cpu(cpus, idx)) +#define perf_cpu_map__for_each_idx(idx, cpus) \ + for ((idx) = 0; (idx) < perf_cpu_map__nr(cpus); (idx)++) + #endif /* __LIBPERF_CPUMAP_H */ From patchwork Thu May 19 03:20:04 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Ian Rogers X-Patchwork-Id: 12854486 X-Patchwork-Delegate: bpf@iogearbox.net 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 9B27DC433FE for ; Thu, 19 May 2022 03:21:20 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S233158AbiESDVR (ORCPT ); Wed, 18 May 2022 23:21:17 -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 S232290AbiESDUk (ORCPT ); Wed, 18 May 2022 23:20:40 -0400 Received: from mail-yb1-xb49.google.com (mail-yb1-xb49.google.com [IPv6:2607:f8b0:4864:20::b49]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 76FE571D87 for ; Wed, 18 May 2022 20:20:21 -0700 (PDT) Received: by mail-yb1-xb49.google.com with SMTP id g7-20020a5b0707000000b0064f39e75da4so178852ybq.17 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=VpiZGPgdHmoW+DRAktSzviPceIbrO2aoEINrjw3soDTm8WkSbpdH2MVu7+L0WQsqmJ QCyL1Xgr/vppAgt2bu4nxZykQG9Ie9PYou+lFGdJMY+thvRSfVKStLiiGDMWq7KZ60kP MrkXZZGPX/k5IMZBix7CFnBWXAlQr1t9AA9m3sZI8Mx/4HVElzSmnLcUsDQkEs94/bzE zykad3+avBLGj6x1Z5mz9hHiu2kGBbsCJx6f3paAA1FQwJqhEA0VQLeE41KnmJMTqe7A YJZ7lzPl6gSWulRza2M4hf/3gGxTz6eKdIX9LEnr8T438GHAJZTihCVmng3cti1xcRMM AIpw== X-Gm-Message-State: AOAM531Ggdv/7Q3AMhyCQeLPW7ezC/+ek3wsM7Afz81pYpLCYowSWQB/ UtXdb36XDw7R1OaUAz+nRB2RoIjAEu1x 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: netdev@vger.kernel.org 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) static int bpf_program_profiler__read(struct evsel *evsel) { - // perf_cpu_map uses /sys/devices/system/cpu/online - int num_cpu = 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 = 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 = 0; - int err, cpu; + int err, idx, bpf_cpu; if (list_empty(&evsel->bpf_counter_list)) return -EAGAIN; - for (cpu = 0; cpu < num_cpu; cpu++) { - perf_counts(evsel->counts, cpu, 0)->val = 0; - perf_counts(evsel->counts, cpu, 0)->ena = 0; - perf_counts(evsel->counts, cpu, 0)->run = 0; + perf_cpu_map__for_each_idx(idx, evsel__cpus(evsel)) { + counts = perf_counts(evsel->counts, idx, 0); + counts->val = 0; + counts->ena = 0; + counts->run = 0; } list_for_each_entry(counter, &evsel->bpf_counter_list, list) { struct bpf_prog_profiler_bpf *skel = counter->skel; @@ -256,10 +256,15 @@ static int bpf_program_profiler__read(struct evsel *evsel) return err; } - for (cpu = 0; cpu < num_cpu; cpu++) { - perf_counts(evsel->counts, cpu, 0)->val += values[cpu].counter; - perf_counts(evsel->counts, cpu, 0)->ena += values[cpu].enabled; - perf_counts(evsel->counts, cpu, 0)->run += values[cpu].running; + for (bpf_cpu = 0; bpf_cpu < num_cpu_bpf; bpf_cpu++) { + idx = perf_cpu_map__idx(evsel__cpus(evsel), + (struct perf_cpu){.cpu = bpf_cpu}); + if (idx == -1) + continue; + counts = perf_counts(evsel->counts, idx, 0); + counts->val += values[bpf_cpu].counter; + counts->ena += values[bpf_cpu].enabled; + counts->run += values[bpf_cpu].running; } } return 0; @@ -621,6 +626,7 @@ static int bperf__read(struct evsel *evsel) struct bperf_follower_bpf *skel = evsel->follower_skel; __u32 num_cpu_bpf = cpu__max_cpu().cpu; struct bpf_perf_event_value values[num_cpu_bpf]; + struct perf_counts_values *counts; int reading_map_fd, err = 0; __u32 i; int j; @@ -639,29 +645,32 @@ static int bperf__read(struct evsel *evsel) case BPERF_FILTER_GLOBAL: assert(i == 0); - perf_cpu_map__for_each_cpu(entry, j, all_cpu_map) { - cpu = entry.cpu; - perf_counts(evsel->counts, cpu, 0)->val = values[cpu].counter; - perf_counts(evsel->counts, cpu, 0)->ena = values[cpu].enabled; - perf_counts(evsel->counts, cpu, 0)->run = values[cpu].running; + perf_cpu_map__for_each_cpu(entry, j, evsel__cpus(evsel)) { + counts = perf_counts(evsel->counts, j, 0); + counts->val = values[entry.cpu].counter; + counts->ena = values[entry.cpu].enabled; + counts->run = values[entry.cpu].running; } break; case BPERF_FILTER_CPU: - cpu = evsel->core.cpus->map[i].cpu; - perf_counts(evsel->counts, i, 0)->val = values[cpu].counter; - perf_counts(evsel->counts, i, 0)->ena = values[cpu].enabled; - perf_counts(evsel->counts, i, 0)->run = values[cpu].running; + cpu = perf_cpu_map__cpu(evsel__cpus(evsel), i).cpu; + assert(cpu >= 0); + counts = perf_counts(evsel->counts, i, 0); + counts->val = values[cpu].counter; + counts->ena = values[cpu].enabled; + counts->run = values[cpu].running; break; case BPERF_FILTER_PID: case BPERF_FILTER_TGID: - perf_counts(evsel->counts, 0, i)->val = 0; - perf_counts(evsel->counts, 0, i)->ena = 0; - perf_counts(evsel->counts, 0, i)->run = 0; + counts = perf_counts(evsel->counts, 0, i); + counts->val = 0; + counts->ena = 0; + counts->run = 0; for (cpu = 0; cpu < num_cpu_bpf; cpu++) { - perf_counts(evsel->counts, 0, i)->val += values[cpu].counter; - perf_counts(evsel->counts, 0, i)->ena += values[cpu].enabled; - perf_counts(evsel->counts, 0, i)->run += values[cpu].running; + counts->val += values[cpu].counter; + counts->ena += values[cpu].enabled; + counts->run += values[cpu].running; } break; default: From patchwork Thu May 19 03:20:05 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Ian Rogers X-Patchwork-Id: 12854487 X-Patchwork-Delegate: bpf@iogearbox.net 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 24A35C433EF for ; Thu, 19 May 2022 03:21:23 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S233228AbiESDVU (ORCPT ); Wed, 18 May 2022 23:21:20 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:46418 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233262AbiESDUm (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 BBD1DD9EAA for ; Wed, 18 May 2022 20:20:23 -0700 (PDT) Received: by mail-yw1-x1149.google.com with SMTP id 00721157ae682-2f4dfd09d7fso36283897b3.0 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=c4JrFGW96sR8RsbHWdXjV9eAaVJQLJZB2c7E1A8A6AV+gq/XiSD5IEHFxZ0fAdMMJN sYazunOD95HIyEEblhFOYqORD4D/66ms2r4wns/ToZKjGOhwx0CRAU8WcX1hwKY3TIt5 sBxAbjwxEog8L4mXj0bUsL1RJwOfWgi4ypISlZY3UAufIRgCdnCI/T4Ha4bZ/rXlbrss GmxtObXXdmfJoXJk7DCRyeGBSXgM/dTYwTWyVw8OPf+2Wh0MX4z07qjRSoU8NkMGMl6p q/SYwQUHkCsC3c4CLQQuNdFlgT5fmKbNOuI6KLwdJHNIi9vaGsyI1C0SsiNXgRcCIEKM pvMg== X-Gm-Message-State: AOAM5319KkoBLwNjOfTS/WOxef7idnYltxFJy4O2upM7v8PTW1sDYeY+ XnijopyHnnAM3j3Hw+M/2cnxPOUrVVFA 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: netdev@vger.kernel.org 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) 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 = 0; + int nthreads = perf_thread_map__nr(counter->core.threads); + int i = 0; double uval; struct perf_aggr_thread_value *buf; @@ -776,13 +776,17 @@ static struct perf_aggr_thread_value *sort_aggr_thread( if (!buf) return NULL; - for (thread = 0; thread < nthreads; thread++) { + for (int thread = 0; thread < nthreads; thread++) { + int idx; u64 ena = 0, run = 0, val = 0; - for (cpu = 0; cpu < ncpus; cpu++) { - val += perf_counts(counter->counts, cpu, thread)->val; - ena += perf_counts(counter->counts, cpu, thread)->ena; - run += perf_counts(counter->counts, cpu, thread)->run; + perf_cpu_map__for_each_idx(idx, evsel__cpus(counter)) { + struct perf_counts_values *counts = + perf_counts(counter->counts, idx, thread); + + val += counts->val; + ena += counts->ena; + run += counts->run; } uval = val * counter->scale; @@ -817,13 +821,11 @@ static void print_aggr_thread(struct perf_stat_config *config, struct evsel *counter, char *prefix) { FILE *output = config->output; - int nthreads = perf_thread_map__nr(counter->core.threads); - int ncpus = perf_cpu_map__nr(counter->core.cpus); int thread, sorted_threads; struct aggr_cpu_id id; struct perf_aggr_thread_value *buf; - buf = sort_aggr_thread(counter, nthreads, ncpus, &sorted_threads, _target); + buf = 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 *evlist) static void evsel__copy_prev_raw_counts(struct evsel *evsel) { - int ncpus = evsel__nr_cpus(evsel); - int nthreads = perf_thread_map__nr(evsel->core.threads); + int idx, nthreads = perf_thread_map__nr(evsel->core.threads); for (int thread = 0; thread < nthreads; thread++) { - for (int cpu = 0; cpu < ncpus; cpu++) { - *perf_counts(evsel->counts, cpu, thread) = - *perf_counts(evsel->prev_raw_counts, cpu, - thread); + perf_cpu_map__for_each_idx(idx, evsel__cpus(evsel)) { + *perf_counts(evsel->counts, idx, thread) = + *perf_counts(evsel->prev_raw_counts, idx, thread); } }