From patchwork Fri Sep 9 17:52:37 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Namhyung Kim X-Patchwork-Id: 12972037 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 44726ECAAD5 for ; Fri, 9 Sep 2022 17:52:50 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231778AbiIIRwr (ORCPT ); Fri, 9 Sep 2022 13:52:47 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:50392 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231760AbiIIRwp (ORCPT ); Fri, 9 Sep 2022 13:52:45 -0400 Received: from mail-pg1-x532.google.com (mail-pg1-x532.google.com [IPv6:2607:f8b0:4864:20::532]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 22ABB12BF9B; Fri, 9 Sep 2022 10:52:44 -0700 (PDT) Received: by mail-pg1-x532.google.com with SMTP id c24so2240464pgg.11; Fri, 09 Sep 2022 10:52:44 -0700 (PDT) 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; bh=9ehd9/6UapbHdanxJIohnP32ZiPMfDsE+vEMO9Oxisw=; b=GdQO7MFZo/mvI9h+l7XEm/Wp1hfXEkCTy2o2tcY5spbLZdcRdWqBZzqcD1O3ISOJIS 2U61C2nKU63VkvHRyW4Zs3jGvv1amQPSbFdU8c3NBcCdWC3O8YBIQyatXvSNesg40TuF wydSJF9JtQWJnAajdyzcL0Gwlc6cuj12yJuqRbByyC8auEX2QVFWhXadyLeCWXMR5QgL 8AmhhTiOG6rOarPggi5fbS3xOoWEEK9VExL5teE4qmInqO6i+KjLxPdP/7nq3mNCpxVP Bdw6riQ4Nm4hvAaTVuuWaE7O4UotWH6hq9RtY2qLmkapG/6YwLL+DbBEiQsY+oJGeHOu 9mtA== 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; bh=9ehd9/6UapbHdanxJIohnP32ZiPMfDsE+vEMO9Oxisw=; b=3KkKNnBu3Ej0nF6H4S1baudrbz4qoetGjea0IWEgKX7+sEgMzeEk8C9p/xSLWTOxGX U2RhL9NTu3JHgvRnTQ/QEZLs3yUedq/mk9glH2AKPKUugNNozjejVsx8SoljUHpzx8/s ecgINAt+OLfeLlkMLMpXuw3zkLiMZdSdzwAVMsiNpLQ0jbJ+xEZzGHakybXdse7Sp5mP zriKgA4LCqHphsCh/x8I+2KAH3gAg/WqbAjRsTq7aJl89D7TBMpMnnyiQ+YShmmXnvZr qBMunn89DFxoX6GvSDz10ohoCbr9xIPi2iwW72Keldo0+WsQl/XXZu9Kwc7ALoYR1RkS 65GQ== X-Gm-Message-State: ACgBeo24sV0mkCSZ4asolxfjma0vlH1PQFgFLxlYzHbTtBXBJ9Jpe+VY 9bKz+cRkprk8U037m1eqQC8= X-Google-Smtp-Source: AA6agR5VeBR8pieqlgGzhpp7NvCL0LzjfMZM8QKF6lol1MJ3x/59ENgdHawWhEZ+SMsPnz4WXG9LAA== X-Received: by 2002:a05:6a00:e8a:b0:535:cc5c:3d87 with SMTP id bo10-20020a056a000e8a00b00535cc5c3d87mr15404635pfb.24.1662745963556; Fri, 09 Sep 2022 10:52:43 -0700 (PDT) Received: from youngsil.svl.corp.google.com ([2620:15c:2d4:203:b77b:e812:1879:ec2f]) by smtp.gmail.com with ESMTPSA id w207-20020a627bd8000000b0052d46b43006sm4275pfc.156.2022.09.09.10.52.42 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 09 Sep 2022 10:52:43 -0700 (PDT) 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, Song Liu , bpf@vger.kernel.org Subject: [PATCH 1/3] perf stat: Fix BPF program section name Date: Fri, 9 Sep 2022 10:52:37 -0700 Message-Id: <20220909175239.206268-2-namhyung@kernel.org> X-Mailer: git-send-email 2.37.2.789.g6183377224-goog In-Reply-To: <20220909175239.206268-1-namhyung@kernel.org> References: <20220909175239.206268-1-namhyung@kernel.org> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: bpf@vger.kernel.org It seems the recent libbpf got more strict about the section name. I'm seeing a failure like this: $ sudo ./perf stat -a --bpf-counters --for-each-cgroup ^. sleep 1 libbpf: prog 'on_cgrp_switch': missing BPF prog type, check ELF section name 'perf_events' libbpf: prog 'on_cgrp_switch': failed to load: -22 libbpf: failed to load object 'bperf_cgroup_bpf' libbpf: failed to load BPF skeleton 'bperf_cgroup_bpf': -22 Failed to load cgroup skeleton The section name should be 'perf_event' (without the trailing 's'). Although it's related to the libbpf change, it'd be better fix the section name in the first place. Fixes: 944138f048f7 ("perf stat: Enable BPF counter with --for-each-cgroup") Signed-off-by: Namhyung Kim --- tools/perf/util/bpf_skel/bperf_cgroup.bpf.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tools/perf/util/bpf_skel/bperf_cgroup.bpf.c b/tools/perf/util/bpf_skel/bperf_cgroup.bpf.c index 292c430768b5..c72f8ad96f75 100644 --- a/tools/perf/util/bpf_skel/bperf_cgroup.bpf.c +++ b/tools/perf/util/bpf_skel/bperf_cgroup.bpf.c @@ -176,7 +176,7 @@ static int bperf_cgroup_count(void) } // This will be attached to cgroup-switches event for each cpu -SEC("perf_events") +SEC("perf_event") int BPF_PROG(on_cgrp_switch) { return bperf_cgroup_count(); From patchwork Fri Sep 9 17:52:38 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Namhyung Kim X-Patchwork-Id: 12972038 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 2E241ECAAD3 for ; Fri, 9 Sep 2022 17:52:52 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231742AbiIIRws (ORCPT ); Fri, 9 Sep 2022 13:52:48 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:50400 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231748AbiIIRwq (ORCPT ); Fri, 9 Sep 2022 13:52:46 -0400 Received: from mail-pj1-x102c.google.com (mail-pj1-x102c.google.com [IPv6:2607:f8b0:4864:20::102c]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 58693131BDF; Fri, 9 Sep 2022 10:52:45 -0700 (PDT) Received: by mail-pj1-x102c.google.com with SMTP id o2-20020a17090a9f8200b0020025a22208so6048407pjp.2; Fri, 09 Sep 2022 10:52:45 -0700 (PDT) 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; bh=nccN246kEYfk3UGon7MpN8QFDo7wKcoEwjdZMc/8ixo=; b=C0nBse47EjnFFj4RXiFv3bLuYED86LZEGJtlmdDiTwsGcUu9jiBnW3+ATHOBcfBgBP wd0gKD12QAphskf3+GSP46WEAbHGYA/g/3GUQ8fTwvdbIfxRACTLu0J9SUdOr44jKwmn 7L5o3NLuEKiTW0Q16PhHnm2Ala9f6HNRIkFSHEWt9EH1VCCN1XUflS4QhJPg9DM/g3aE J9zFKCcSz6ocEGvvLCLwRjYFeCvMICWm7+r2Uun5XcFBs6tnJqhsvDt6NYg+sMEXZ2pe INJP6fxeCz5DfiEaUnvx/NLwgOixanCYquSEXwgVud1cY/bW5v3c87GTZ5mshYwog2xb Cdkw== 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; bh=nccN246kEYfk3UGon7MpN8QFDo7wKcoEwjdZMc/8ixo=; b=4yiV7qREdRJ0OqDS3NA89JQS/VoF9s8GETMc3QLmV8kJ3LYSx5d8hN5+eadXFLdFcV A6Ilvdul1EECCuEz+4Zme4KGR1ELpnlrHpDEI+KNLgNi+fs2gjp7RGFxIGNmeqIrOdTm 7Jipvy3KJt9wTZ+pcdYsW5IngDbRAOK/t42hDMP3g8o9L5TgEmMEvzw7sGEhl3reT+Yw /53bO9qEVkq3iyHCcdpojmKMBWQsBT3yE29GLt9WaEB+N8O3nkVMAweT/CAE6gV6NYqL y1xqEgLnU3Sp1bXjym1SQd5Mm52AA0XqpEUYRnfj+9WBfUIsAut/Cu0qIgxI3lcC1cNw 0R0w== X-Gm-Message-State: ACgBeo26W18h+uBVV9cssjhTfXrxuIuRYHzsm1g6i06WkjO4NAli3x97 TujtB7BJ4FZ4nwxd4MtbY+k= X-Google-Smtp-Source: AA6agR72c8909lrCR5qLSQlF90tm/upGtfs2DtFTL/CKfXgc6poeL020ZplAPKtJ1b+6Z7A5IQ0uUg== X-Received: by 2002:a17:902:f548:b0:178:44b:4ea9 with SMTP id h8-20020a170902f54800b00178044b4ea9mr5485456plf.77.1662745964748; Fri, 09 Sep 2022 10:52:44 -0700 (PDT) Received: from youngsil.svl.corp.google.com ([2620:15c:2d4:203:b77b:e812:1879:ec2f]) by smtp.gmail.com with ESMTPSA id w207-20020a627bd8000000b0052d46b43006sm4275pfc.156.2022.09.09.10.52.43 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 09 Sep 2022 10:52:44 -0700 (PDT) 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, Song Liu , bpf@vger.kernel.org Subject: [PATCH 2/3] perf stat: Fix cpu map index in bperf cgroup code Date: Fri, 9 Sep 2022 10:52:38 -0700 Message-Id: <20220909175239.206268-3-namhyung@kernel.org> X-Mailer: git-send-email 2.37.2.789.g6183377224-goog In-Reply-To: <20220909175239.206268-1-namhyung@kernel.org> References: <20220909175239.206268-1-namhyung@kernel.org> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: bpf@vger.kernel.org The previous cpu map introduced a bug in the bperf cgroup counter. This results in a failure when user gives a partial cpu map starting from non-zero. $ sudo ./perf stat -C 1-2 --bpf-counters --for-each-cgroup ^. sleep 1 libbpf: prog 'on_cgrp_switch': failed to create BPF link for perf_event FD 0: -9 (Bad file descriptor) Failed to attach cgroup program To get the FD of an evsel, it should use a map index not the CPU number. Fixes: 0255571a1605 ("perf cpumap: Switch to using perf_cpu_map API") Signed-off-by: Namhyung Kim --- tools/perf/util/bpf_counter_cgroup.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tools/perf/util/bpf_counter_cgroup.c b/tools/perf/util/bpf_counter_cgroup.c index 63b9db657442..97c69a249c6e 100644 --- a/tools/perf/util/bpf_counter_cgroup.c +++ b/tools/perf/util/bpf_counter_cgroup.c @@ -95,7 +95,7 @@ static int bperf_load_program(struct evlist *evlist) perf_cpu_map__for_each_cpu(cpu, i, evlist->core.all_cpus) { link = bpf_program__attach_perf_event(skel->progs.on_cgrp_switch, - FD(cgrp_switch, cpu.cpu)); + FD(cgrp_switch, i)); if (IS_ERR(link)) { pr_err("Failed to attach cgroup program\n"); err = PTR_ERR(link); @@ -123,7 +123,7 @@ static int bperf_load_program(struct evlist *evlist) map_fd = bpf_map__fd(skel->maps.events); perf_cpu_map__for_each_cpu(cpu, j, evlist->core.all_cpus) { - int fd = FD(evsel, cpu.cpu); + int fd = FD(evsel, j); __u32 idx = evsel->core.idx * total_cpus + cpu.cpu; err = bpf_map_update_elem(map_fd, &idx, &fd, From patchwork Fri Sep 9 17:52:39 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Namhyung Kim X-Patchwork-Id: 12972039 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 AEE20C6FA8A for ; Fri, 9 Sep 2022 17:52:52 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231808AbiIIRwt (ORCPT ); Fri, 9 Sep 2022 13:52:49 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:50392 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231752AbiIIRwr (ORCPT ); Fri, 9 Sep 2022 13:52:47 -0400 Received: from mail-pl1-x634.google.com (mail-pl1-x634.google.com [IPv6:2607:f8b0:4864:20::634]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 73DE712EDA5; Fri, 9 Sep 2022 10:52:46 -0700 (PDT) Received: by mail-pl1-x634.google.com with SMTP id 9so2454328plj.11; Fri, 09 Sep 2022 10:52:46 -0700 (PDT) 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; bh=R/Un3jPNK+3vfi+QVT0kN6ybavOhwptFNDHmeMY+Fvg=; b=Al1HQQaG1N7GdsJGbw2dOJLsEx3YgSO0Q6jb4gNJOTrscMphMfJemXgv2d0CYIzmT9 m0pHa+Mn/4xIw5FSp5aBdR7j0kISDswJqIPGdwdaquENe2WRAAYHLgO7nhqSubf/bHcw xw8UAZQtu/BWhIu3sE81VZpH5k32q+l/k2DLQbqbIXP+v37HyRsVFD/gLVOaDbImJnFF iTmgJaIm3C/+XXs2tSbQh1q/xQOaySGbj3NFtnpcI2bIrfVRib3dHWYnOOKsws0ACAMY Yf4F/R6V6SX98GmbHmHd0dlOu8ohxme4egLJXs1oYqfO8mRvONcG4NBhLNjXBICwdGGJ Ya1A== 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; bh=R/Un3jPNK+3vfi+QVT0kN6ybavOhwptFNDHmeMY+Fvg=; b=b/dUYNla1IwQWaoXYbuLuVkNO0/jifKgY9Qs1eSiyKMn1UxtWm5TWbauE4Kej9wOna 69SGDmDfr6JLhv6cyV8cn1A93aTbM0kH8FvBoC5rYNC3Q+Dj9lPBXLnWEsz+7JOCmdEN uot6SxFPWu56VTgc9wCcGucgs+5I1gZBUQJGu/jfcfOfy0VY3WWXpjOjQwTXBZ05kBpH Xsxfua1NeUOZ7jzhUKIZXjNOHFIKc5i/FLhvNp5IecmizQqWgqemS/dlrjOWDSom3ebP 14Xv5WK38DAaSmG61ITgy7Nvpce8tK8SnaH1Cq6HTUMtE1H+/8d0xovfshwqKyDSb3hU MbLQ== X-Gm-Message-State: ACgBeo1bLRworYAbQZTmlmjPqf8vPA5Seu0Zu7wE0ytILOeKgQ091bOc GHoME26AdYvTB8RlocdnjF8rASxuzwA= X-Google-Smtp-Source: AA6agR4sv30OTSZPB0uPYzoQ1BvrUivAsPMPIQ4EcOhO4kvfCjFvYQVvAhe0wZDzWSua9CG/6D1YNg== X-Received: by 2002:a17:902:e78e:b0:175:5313:2a46 with SMTP id cp14-20020a170902e78e00b0017553132a46mr15163924plb.53.1662745965901; Fri, 09 Sep 2022 10:52:45 -0700 (PDT) Received: from youngsil.svl.corp.google.com ([2620:15c:2d4:203:b77b:e812:1879:ec2f]) by smtp.gmail.com with ESMTPSA id w207-20020a627bd8000000b0052d46b43006sm4275pfc.156.2022.09.09.10.52.44 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 09 Sep 2022 10:52:45 -0700 (PDT) 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, Song Liu , bpf@vger.kernel.org Subject: [PATCH 3/3] perf test: Add a new test for perf stat cgroup BPF counter Date: Fri, 9 Sep 2022 10:52:39 -0700 Message-Id: <20220909175239.206268-4-namhyung@kernel.org> X-Mailer: git-send-email 2.37.2.789.g6183377224-goog In-Reply-To: <20220909175239.206268-1-namhyung@kernel.org> References: <20220909175239.206268-1-namhyung@kernel.org> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: bpf@vger.kernel.org $ sudo ./perf test -v each-cgroup 96: perf stat --bpf-counters --for-each-cgroup test : --- start --- test child forked, pid 79600 test child finished with 0 ---- end ---- perf stat --bpf-counters --for-each-cgroup test: Ok Signed-off-by: Namhyung Kim --- .../tests/shell/stat_bpf_counters_cgrp.sh | 83 +++++++++++++++++++ 1 file changed, 83 insertions(+) create mode 100755 tools/perf/tests/shell/stat_bpf_counters_cgrp.sh diff --git a/tools/perf/tests/shell/stat_bpf_counters_cgrp.sh b/tools/perf/tests/shell/stat_bpf_counters_cgrp.sh new file mode 100755 index 000000000000..d724855d097c --- /dev/null +++ b/tools/perf/tests/shell/stat_bpf_counters_cgrp.sh @@ -0,0 +1,83 @@ +#!/bin/sh +# perf stat --bpf-counters --for-each-cgroup test +# SPDX-License-Identifier: GPL-2.0 + +set -e + +test_cgroups= +if [ "$1" = "-v" ]; then + verbose="1" +fi + +# skip if --bpf-counters --for-each-cgroup is not supported +check_bpf_counter() +{ + if ! perf stat -a --bpf-counters --for-each-cgroup / true > /dev/null 2>&1; then + if [ "${verbose}" = "1" ]; then + echo "Skipping: --bpf-counters --for-each-cgroup not supported" + perf --no-pager stat -a --bpf-counters --for-each-cgroup / true || true + fi + exit 2 + fi +} + +# find two cgroups to measure +find_cgroups() +{ + # try usual systemd slices first + if [ -d /sys/fs/cgroup/system.slice -a -d /sys/fs/cgroup/user.slice ]; then + test_cgroups="system.slice,user.slice" + return + fi + + # try root and self cgroups + local self_cgrp=$(grep perf_event /proc/self/cgroup | cut -d: -f3) + if [ -z ${self_cgrp} ]; then + # cgroup v2 doesn't specify perf_event + self_cgrp=$(grep ^0: /proc/self/cgroup | cut -d: -f3) + fi + + if [ -z ${self_cgrp} ]; then + test_cgroups="/" + else + test_cgroups="/,${self_cgrp}" + fi +} + +# As cgroup events are cpu-wide, we cannot simply compare the result. +# Just check if it runs without failure and has non-zero results. +check_system_wide_counted() +{ + local output + + output=$(perf stat -a --bpf-counters --for-each-cgroup ${test_cgroups} -e cpu-clock -x, sleep 1 2>&1) + if echo ${output} | grep -q -F "&1) + if echo ${output} | grep -q -F "