From patchwork Wed Jan 4 06:43:59 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Namhyung Kim X-Patchwork-Id: 13088224 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 CBE5DC46467 for ; Wed, 4 Jan 2023 06:44:25 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S233487AbjADGoX (ORCPT ); Wed, 4 Jan 2023 01:44:23 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:53926 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233260AbjADGoI (ORCPT ); Wed, 4 Jan 2023 01:44:08 -0500 Received: from mail-pg1-x52d.google.com (mail-pg1-x52d.google.com [IPv6:2607:f8b0:4864:20::52d]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 2392D1157; Tue, 3 Jan 2023 22:44:08 -0800 (PST) Received: by mail-pg1-x52d.google.com with SMTP id v3so21612939pgh.4; Tue, 03 Jan 2023 22:44:08 -0800 (PST) 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 :message-id:reply-to; bh=xp5Hj1uOMunUvWIMLUWPDJI/aB8Townrf00dqDCpqV0=; b=LkFL3IY5NRqL+oGa6Y7xnMopkhn4Ww6YfUfKJd8GzoypjwuYO2qNLNOOUS2sVoEtsV xxm96hfdketMDudUKLcqBH2Ufj3pLbhEjIkTA//n8RILprvvg8r1zrgPtYGnMk9//JLk XJzzcpoWg55qyInfWCpkWUB7ExANRRbI/0k/BFtT4mvBYnMCJeiV8P2KC0HOcUO88Q9u Ok1rrQXTqejGtiGFpFX6BEPVNnc2WHUU2OHhnwMOPELKTUhQzyDFMog8J6W+WWtpdzBZ 8rxrRsLnYfS+lbARtTAvzd7ai4wwk4/80okYyF8WiPfzNADuIgFwIxhpLd4cO1/kcJrn uFdg== 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:message-id:reply-to; bh=xp5Hj1uOMunUvWIMLUWPDJI/aB8Townrf00dqDCpqV0=; b=tMQCkbFwWVB62nIvaA1KElf8vm1z/LAQ88xrsPKS3YJtnujy6H3qHwK6OXNfPgRuCQ Tl6ITTiYy3bY0mCR20NUP5GRGUYIh/JWq3Xj9oOeaxHr2BfCWTJcCKeIeGDkMLMsXjRk cF2NOOFx2F3l0HuEIehQ0qkNYdB4HJVTDY4fkYa3OBHnlHW4I0tGI/jfBnAOGA09xCEO RCNEj69lqIM4YKtCKeeltBLO8BmeihPjUt+KR7fYy3+MHo6bdICuZq6BxeSBZBiasrh+ vWMrykGHi3TVoQ2AvTiwDzinhA4IPX5LBNEwqrdStKMxxqrbv4oIHKzC7ftDzQJ+Us7P posA== X-Gm-Message-State: AFqh2ko6jLEROWqa4l6VQzy0zEn5aIePlrcmOVq/ax39apJdX1W8AZHe P9ZBpEY/yrdbJHee7dECHco= X-Google-Smtp-Source: AMrXdXuMSYqs+mlsHE72dr8FOqodIU8hNiVbIAv6Hd+MlQVf5sQ85f1sy6KZ2P9j3pxPnyN+D2E4ZA== X-Received: by 2002:a05:6a00:1a45:b0:582:294d:f465 with SMTP id h5-20020a056a001a4500b00582294df465mr15550932pfv.13.1672814647626; Tue, 03 Jan 2023 22:44:07 -0800 (PST) Received: from youngsil.svl.corp.google.com ([2620:15c:2d4:203:d1f4:a47f:3a41:e1a2]) by smtp.gmail.com with ESMTPSA id e28-20020a056a0000dc00b00576f7bd92cdsm11829257pfj.14.2023.01.03.22.44.06 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 03 Jan 2023 22:44:07 -0800 (PST) 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/4] perf bpf_counter: Add more error messages for bperf Date: Tue, 3 Jan 2023 22:43:59 -0800 Message-Id: <20230104064402.1551516-2-namhyung@kernel.org> X-Mailer: git-send-email 2.39.0.314.g84b9a713c41-goog In-Reply-To: <20230104064402.1551516-1-namhyung@kernel.org> References: <20230104064402.1551516-1-namhyung@kernel.org> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: bpf@vger.kernel.org I found perf stat silently exits when it failed in bperf__load(). Let's add some error messages to notify users. $ sudo ./perf stat -a --bpf-counters -ddd sleep 1 bpf_map_update_elem failed: err=-7 Signed-off-by: Namhyung Kim --- tools/perf/util/bpf_counter.c | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/tools/perf/util/bpf_counter.c b/tools/perf/util/bpf_counter.c index eeee899fcf34..7f5cc1aa4903 100644 --- a/tools/perf/util/bpf_counter.c +++ b/tools/perf/util/bpf_counter.c @@ -471,8 +471,10 @@ static int bperf__load(struct evsel *evsel, struct target *target) if (!all_cpu_map) { all_cpu_map = perf_cpu_map__new(NULL); - if (!all_cpu_map) + if (!all_cpu_map) { + pr_err("failed to create all cpu map\n"); return -1; + } } evsel->bperf_leader_prog_fd = -1; @@ -493,13 +495,16 @@ static int bperf__load(struct evsel *evsel, struct target *target) err = bpf_map_lookup_elem(attr_map_fd, &evsel->core.attr, &entry); if (err) { err = bpf_map_update_elem(attr_map_fd, &evsel->core.attr, &entry, BPF_ANY); - if (err) + if (err) { + pr_err("updating perf_event_attr map failed: err=%d\n", err); goto out; + } } evsel->bperf_leader_link_fd = bpf_link_get_fd_by_id(entry.link_id); if (evsel->bperf_leader_link_fd < 0 && bperf_reload_leader_program(evsel, attr_map_fd, &entry)) { + pr_err("reload leader program failed\n"); err = -1; goto out; } From patchwork Wed Jan 4 06:44:00 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Namhyung Kim X-Patchwork-Id: 13088223 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 DDDAAC54EBC for ; Wed, 4 Jan 2023 06:44:24 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S233475AbjADGoW (ORCPT ); Wed, 4 Jan 2023 01:44:22 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:53932 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233306AbjADGoK (ORCPT ); Wed, 4 Jan 2023 01:44:10 -0500 Received: from mail-pg1-x533.google.com (mail-pg1-x533.google.com [IPv6:2607:f8b0:4864:20::533]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 8B3DA2E5; Tue, 3 Jan 2023 22:44:09 -0800 (PST) Received: by mail-pg1-x533.google.com with SMTP id s67so12303129pgs.3; Tue, 03 Jan 2023 22:44:09 -0800 (PST) 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 :message-id:reply-to; bh=qYyFBSOYl+N37EChskGvYBFRtSPcjv1yDLUNKRSs7MM=; b=UxEyTrBRmwjsHTMfS+syQvxgGQTx/wNtXpdL5cuHQwnv9y5GBApVJRFXRBX4qaXd3c CwhCtWGNf0BEazBtrSOOwhtX3Dvz4ur/jOWBK9OXMbdOcYgj3FFm4sJUkLqbMSQw7Eqc iJMM7atv89+rSMEBWArvTDyEo0rwYtrssbu1saCb4XvzdJowP/7WffiCb78dvuPvNzv9 bT3C7pAQHqFYHGF+Dchsf8onVKCCoWfcYY5Uh7D+aI4zkMGI4B4B9PW5Ymcyue6X9stj YejIXrbKqCJWO9dPG7TbBSIEyBp8vL3y0/fsFRTfg+Snd0+boMjNhPYVxN0Jum3RT7t5 DsSg== 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:message-id:reply-to; bh=qYyFBSOYl+N37EChskGvYBFRtSPcjv1yDLUNKRSs7MM=; b=wIACj2qh0PetSc57r9qphdmRTGZvSiW5IxCZ8iOF3HmWPCkiiLEl7X4n/RLZasZgXY pGeCUtzAmSoBMzOCceIolSXrAPzvo2PBILK1t6OhoUhtwxhCLx23KWN2BRc1fE2SeSwL 9O7VHeeWjTDfTlfvq2/g/PvWdDuwRsvIJtgJJGJ8d/j4bnirtdXNW0k5WvJ+NT6aSlXq 2XKZkeQp1TXCOiAMqBkqsFg0/Ak6eoESgUH8k8GUJ7mnte659GzglTQqoW+cH0eVhPxM INSvcJVnxfg+mGbbMPrcAr3yeoM8IQpcWA/Au6d0WtB/4IfiLb/dKtLef59AWxXmwixR I66w== X-Gm-Message-State: AFqh2kp2V+KvrDuca9QRvWnWanriHJ0eJl0v5Vp29sd9dykIw1BkdIYs Y3uq1Jj25uO8/qL99S6+DgU= X-Google-Smtp-Source: AMrXdXtCnxUS1iLq5maFRNCCpIOOBxtOeCn7BQJMzWYnCTvADviYd1Y3orzjT55h9TvNZsaPnFiVWg== X-Received: by 2002:a05:6a00:84c:b0:581:1ee0:75a with SMTP id q12-20020a056a00084c00b005811ee0075amr40159114pfk.32.1672814649048; Tue, 03 Jan 2023 22:44:09 -0800 (PST) Received: from youngsil.svl.corp.google.com ([2620:15c:2d4:203:d1f4:a47f:3a41:e1a2]) by smtp.gmail.com with ESMTPSA id e28-20020a056a0000dc00b00576f7bd92cdsm11829257pfj.14.2023.01.03.22.44.07 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 03 Jan 2023 22:44:08 -0800 (PST) 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/4] perf bpf_counter: Increase perf_attr_map entries to 32 Date: Tue, 3 Jan 2023 22:44:00 -0800 Message-Id: <20230104064402.1551516-3-namhyung@kernel.org> X-Mailer: git-send-email 2.39.0.314.g84b9a713c41-goog In-Reply-To: <20230104064402.1551516-1-namhyung@kernel.org> References: <20230104064402.1551516-1-namhyung@kernel.org> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: bpf@vger.kernel.org The current size 16 cannot hold all events when user gave -dd or -ddd. As it's a part of perf stat, let's increase the size to 32. # unlink previous map to change the size $ sudo unlink /sys/fs/bpf/perf_attr_map $ sudo ./perf stat -a --bpf-counters -ddd sleep 1 Performance counter stats for 'system wide': 35,927.41 msec cpu-clock # 35.802 CPUs utilized 12,629 context-switches # 351.514 /sec 209 cpu-migrations # 5.817 /sec 826 page-faults # 22.991 /sec 2,155,729,621 cycles # 0.060 GHz (30.43%) 1,053,849,500 instructions # 0.49 insn per cycle (38.18%) 232,711,500 branches # 6.477 M/sec (38.44%) 10,693,352 branch-misses # 4.60% of all branches (31.27%) 267,561,655 L1-dcache-loads # 7.447 M/sec (30.58%) 27,290,728 L1-dcache-load-misses # 10.20% of all L1-dcache accesses (30.48%) 12,651,208 LLC-loads # 352.133 K/sec (30.78%) 1,274,018 LLC-load-misses # 10.07% of all LL-cache accesses (38.70%) L1-icache-loads 75,916,358 L1-icache-load-misses # 0.00% of all L1-icache accesses (38.11%) 273,330,559 dTLB-loads # 7.608 M/sec (30.54%) 2,864,458 dTLB-load-misses # 1.05% of all dTLB cache accesses (38.77%) 361,507 iTLB-loads # 10.062 K/sec (30.59%) 848,031 iTLB-load-misses # 234.58% of all iTLB cache accesses (31.30%) L1-dcache-prefetches L1-dcache-prefetch-misses Signed-off-by: Namhyung Kim --- tools/perf/util/bpf_counter.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tools/perf/util/bpf_counter.c b/tools/perf/util/bpf_counter.c index 7f5cc1aa4903..de6331f5263c 100644 --- a/tools/perf/util/bpf_counter.c +++ b/tools/perf/util/bpf_counter.c @@ -28,7 +28,7 @@ #include "bpf_skel/bperf_leader.skel.h" #include "bpf_skel/bperf_follower.skel.h" -#define ATTR_MAP_SIZE 16 +#define ATTR_MAP_SIZE 32 static inline void *u64_to_ptr(__u64 ptr) { From patchwork Wed Jan 4 06:44:01 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Namhyung Kim X-Patchwork-Id: 13088225 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 9A022C4332F for ; Wed, 4 Jan 2023 06:44:26 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S233516AbjADGoZ (ORCPT ); Wed, 4 Jan 2023 01:44:25 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:53938 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233309AbjADGoL (ORCPT ); Wed, 4 Jan 2023 01:44:11 -0500 Received: from mail-pf1-x434.google.com (mail-pf1-x434.google.com [IPv6:2607:f8b0:4864:20::434]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 149232E5; Tue, 3 Jan 2023 22:44:11 -0800 (PST) Received: by mail-pf1-x434.google.com with SMTP id a30so6550984pfr.6; Tue, 03 Jan 2023 22:44:11 -0800 (PST) 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 :message-id:reply-to; bh=4cgcHCT3G9N5bTmGeqZ7j5U+CmWU3a6ei0Q30f2GNqI=; b=H4XigTn/KLhd6ZcA1McBlsmhzTWkRx7D+UVTw8GwUYTqFDz3RaJToZj0GC9EQNCsl9 7hNBQ2S0GOYoQYJyZJTz/KLsD0RZwG9XIwHf+5VTu8lx0+YA2LP3u5zumDRUAsf1Qnyi FUJZgIGolIhocllM3NyrARFA/PJ2naa4TT4ipJTu4Rv76MOyU6HVPuG8ZEEiBB08wqTz gRjKhdzIZVBcH6hgWIYohNzA7Dz20vBQet+V4U0eZ6bDH3kZmJSbMAJKOYViwYeuIffU 6VR1ypLTxSiAZ40307j6IfxmskOQHHQSOkjfhSWVUgPZO7ND9PTfAbUq7RJBL7tPmxHr d99w== 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:message-id:reply-to; bh=4cgcHCT3G9N5bTmGeqZ7j5U+CmWU3a6ei0Q30f2GNqI=; b=AXt0keKTznIBlU5DvsUcVCV8G3PTb9OC/LM9oMlgyyKcUTjAGtQqQzFu0fiCwsyupx a5RHzeHPrN6gzEoxNKJsKGW2MBN6uMveUS5wcylnzSlIfl+afO5TlfrWbLl8eqDe8lQG G3W8uHbqXGMGDdT7BnyZO4u6yT22HR0KjQOG3q9g1pwEWtvhO2MRxO20iTWr48nzloKy sC0hcF3rpDtVNMbs4lfkVOOclLu10B5NzNzovV7fTBKf8v5eKwbG1GN2sjCJvXCO01BE I/2e55Zwpr/DqEjt69dNxx1SIJrpeJT/iqp/wBmaMI+yQwvdJNlWQCMhRUTBn/iBP6Ow TlTw== X-Gm-Message-State: AFqh2kqzQHTvcYuzO/RgryoW6RZINTZSEWzapro/8vbqxATvzSCKT5S/ UYz6FM4uSPRk3CNYMdnZqWY= X-Google-Smtp-Source: AMrXdXtMxebpv/nZ2uvkGSrllvMdXKVtPGNELstI0HkNaA27TNkom7A8iCa4bGKDte7kZLdqu3mmFg== X-Received: by 2002:a62:32c3:0:b0:580:f1b0:2211 with SMTP id y186-20020a6232c3000000b00580f1b02211mr32038887pfy.18.1672814650468; Tue, 03 Jan 2023 22:44:10 -0800 (PST) Received: from youngsil.svl.corp.google.com ([2620:15c:2d4:203:d1f4:a47f:3a41:e1a2]) by smtp.gmail.com with ESMTPSA id e28-20020a056a0000dc00b00576f7bd92cdsm11829257pfj.14.2023.01.03.22.44.09 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 03 Jan 2023 22:44:09 -0800 (PST) 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/4] perf bpf_counter: Handle unsupported cgroup events Date: Tue, 3 Jan 2023 22:44:01 -0800 Message-Id: <20230104064402.1551516-4-namhyung@kernel.org> X-Mailer: git-send-email 2.39.0.314.g84b9a713c41-goog In-Reply-To: <20230104064402.1551516-1-namhyung@kernel.org> References: <20230104064402.1551516-1-namhyung@kernel.org> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: bpf@vger.kernel.org When --for-each-cgroup option is used, it failed when any of events is not supported and it exited immediately. This is not how perf stat handles the unsupported events. Let's ignore the failure and proceed with others. Before: $ sudo ./perf stat -a --bpf-counters -e L1-icache-loads,L1-dcache-loads \ > --for-each-cgroup system.slice,user.slice sleep 1 Failed to open first cgroup events After: $ sudo ./perf stat -a --bpf-counters -e L1-icache-loads,L1-dcache-loads \ > --for-each-cgroup system.slice,user.slice sleep 1 Performance counter stats for 'system wide': L1-icache-loads system.slice 29,892,418 L1-dcache-loads system.slice L1-icache-loads user.slice 52,497,220 L1-dcache-loads user.slice Fixes: 944138f048f7d ("perf stat: Enable BPF counter with --for-each-cgroup") Signed-off-by: Namhyung Kim --- tools/perf/util/bpf_counter_cgroup.c | 14 +++----------- 1 file changed, 3 insertions(+), 11 deletions(-) diff --git a/tools/perf/util/bpf_counter_cgroup.c b/tools/perf/util/bpf_counter_cgroup.c index 3c2df7522f6f..1c82377ed78b 100644 --- a/tools/perf/util/bpf_counter_cgroup.c +++ b/tools/perf/util/bpf_counter_cgroup.c @@ -116,27 +116,19 @@ static int bperf_load_program(struct evlist *evlist) /* open single copy of the events w/o cgroup */ err = evsel__open_per_cpu(evsel, evsel->core.cpus, -1); - if (err) { - pr_err("Failed to open first cgroup events\n"); - goto out; - } + if (err == 0) + evsel->supported = true; map_fd = bpf_map__fd(skel->maps.events); perf_cpu_map__for_each_cpu(cpu, j, evsel->core.cpus) { int fd = FD(evsel, j); __u32 idx = evsel->core.idx * total_cpus + cpu.cpu; - err = bpf_map_update_elem(map_fd, &idx, &fd, - BPF_ANY); - if (err < 0) { - pr_err("Failed to update perf_event fd\n"); - goto out; - } + bpf_map_update_elem(map_fd, &idx, &fd, BPF_ANY); } evsel->cgrp = leader_cgrp; } - evsel->supported = true; if (evsel->cgrp == cgrp) continue; From patchwork Wed Jan 4 06:44:02 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Namhyung Kim X-Patchwork-Id: 13088226 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 886E8C46467 for ; Wed, 4 Jan 2023 06:44:28 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S233239AbjADGoZ (ORCPT ); Wed, 4 Jan 2023 01:44:25 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:53944 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233329AbjADGoN (ORCPT ); Wed, 4 Jan 2023 01:44:13 -0500 Received: from mail-pj1-x1031.google.com (mail-pj1-x1031.google.com [IPv6:2607:f8b0:4864:20::1031]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 7DCFB2E5; Tue, 3 Jan 2023 22:44:12 -0800 (PST) Received: by mail-pj1-x1031.google.com with SMTP id o21so5092701pjw.0; Tue, 03 Jan 2023 22:44:12 -0800 (PST) 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 :message-id:reply-to; bh=+bliKfRbQDwAdV97iBZEDSuzGn0Y1LH+Sf/kDY4qOSE=; b=cxIOOkUt8IWLH0cIGaDmPPocHS4+b8nZeOZSKJTwIzIioIzzrJEWbsBdB20yQSLTiB rv8aZVuBiQlm99kquR8FaBb5qXjzD79AkKjTdku4QyfvVOwmu8K78HoD/z1UNroLYNP5 uvhs7YDAwdb/IZ9WOWNum7qPngiGL0EOxJHL5VqnCJFRbY3AhYhmuDygjdIqjcS3vLHB 5Iq7jDnCGpfFyzMHVh47Q0pUyrTzYlbqy9HNk5ZUuHdSiePuspbY78ZhES0RlLlYoiaY 7C6x/p60oVKAZENXtwlbtXZ4qsjWf0yKWuQmEQX7N3WZ8qyYmTDPAx6YW9zPCi4QPykU 2eTA== 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:message-id:reply-to; bh=+bliKfRbQDwAdV97iBZEDSuzGn0Y1LH+Sf/kDY4qOSE=; b=3Mp2WTqkhoHjIxIR+9ORj+TBZL16Xc60e1/Q/sKuFFM+uLUcO/h0jdcq8DprogvWIw UfQE5nV1f8JS8vOJil6mE0DCYRa+JMNq+9TWPPlHv7O/xiYAoneSzeuNekm8P+n2kXDm us6ALSvGpeSk8N9cAYZp/wXuOH7M3f9jEPToqUsGiDj/SEzqrkUgtzM1JFYpV8tEdPYD f7jO5BiSqeEMk+04GsTpeDDrndI8CuvJP0zc5gcFWqPcKbSwqyMgtsrqz3d14SH6Gy8E +b6E3Va5+bmnx988VWHivGnX5NUzSD5X7BYszdqFh5lcUnaPvQGWOhA56d/2m1O2C60A hK6w== X-Gm-Message-State: AFqh2kosCJs5JRPHsQ/cl2XDeORLQIxQjQgmkYyNJo2Qwe32q1JaUDih D5sgBbDWWho8M85BmT+jwzM= X-Google-Smtp-Source: AMrXdXtBbMbi88hRDwiINdwpS40V2CHVQ+KcNL2U0hUv4HmmvG5LPPknJ0oCV8HBTVfS8GQyPNf4Rw== X-Received: by 2002:a05:6a20:659d:b0:b0:275d:3036 with SMTP id p29-20020a056a20659d00b000b0275d3036mr56741439pzh.24.1672814651855; Tue, 03 Jan 2023 22:44:11 -0800 (PST) Received: from youngsil.svl.corp.google.com ([2620:15c:2d4:203:d1f4:a47f:3a41:e1a2]) by smtp.gmail.com with ESMTPSA id e28-20020a056a0000dc00b00576f7bd92cdsm11829257pfj.14.2023.01.03.22.44.10 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 03 Jan 2023 22:44:11 -0800 (PST) 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 4/4] perf stat: Do not use the same cgroup more than once Date: Tue, 3 Jan 2023 22:44:02 -0800 Message-Id: <20230104064402.1551516-5-namhyung@kernel.org> X-Mailer: git-send-email 2.39.0.314.g84b9a713c41-goog In-Reply-To: <20230104064402.1551516-1-namhyung@kernel.org> References: <20230104064402.1551516-1-namhyung@kernel.org> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: bpf@vger.kernel.org The --for-each-cgroup can have the same cgroup multiple times, but it makes bpf counters confused (since they have the same cgroup id), and the last cgroup events are counted only. Let's check the cgroup name before adding a new entry. Before: $ sudo ./perf stat -a --bpf-counters --for-each-cgroup /,/ sleep 1 Performance counter stats for 'system wide': msec cpu-clock / context-switches / cpu-migrations / page-faults / cycles / instructions / branches / branch-misses / 8,016.04 msec cpu-clock / # 7.998 CPUs utilized 6,152 context-switches / # 767.461 /sec 250 cpu-migrations / # 31.187 /sec 442 page-faults / # 55.139 /sec 613,111,487 cycles / # 0.076 GHz 280,599,604 instructions / # 0.46 insn per cycle 57,692,724 branches / # 7.197 M/sec 3,385,168 branch-misses / # 5.87% of all branches 1.002220125 seconds time elapsed After: $ sudo ./perf stat -a --bpf-counters --for-each-cgroup /,/ sleep 1 Performance counter stats for 'system wide': 8,013.38 msec cpu-clock / # 7.998 CPUs utilized 6,859 context-switches / # 855.944 /sec 334 cpu-migrations / # 41.680 /sec 345 page-faults / # 43.053 /sec 782,326,119 cycles / # 0.098 GHz 471,645,724 instructions / # 0.60 insn per cycle 94,963,430 branches / # 11.851 M/sec 3,685,511 branch-misses / # 3.88% of all branches 1.001864539 seconds time elapsed Fixes: bb1c15b60b981 ("perf stat: Support regex pattern in --for-each-cgroup") Signed-off-by: Namhyung Kim --- tools/perf/util/cgroup.c | 23 ++++++++++++++++++----- 1 file changed, 18 insertions(+), 5 deletions(-) diff --git a/tools/perf/util/cgroup.c b/tools/perf/util/cgroup.c index e99b41f9be45..cd978c240e0d 100644 --- a/tools/perf/util/cgroup.c +++ b/tools/perf/util/cgroup.c @@ -224,6 +224,19 @@ static int add_cgroup_name(const char *fpath, const struct stat *sb __maybe_unus return 0; } +static int check_and_add_cgroup_name(const char *fpath) +{ + struct cgroup_name *cn; + + list_for_each_entry(cn, &cgroup_list, list) { + if (!strcmp(cn->name, fpath)) + return 0; + } + + /* pretend if it's added by ftw() */ + return add_cgroup_name(fpath, NULL, FTW_D, NULL); +} + static void release_cgroup_list(void) { struct cgroup_name *cn; @@ -242,7 +255,7 @@ static int list_cgroups(const char *str) struct cgroup_name *cn; char *s; - /* use given name as is - for testing purpose */ + /* use given name as is when no regex is given */ for (;;) { p = strchr(str, ','); e = p ? p : eos; @@ -253,13 +266,13 @@ static int list_cgroups(const char *str) s = strndup(str, e - str); if (!s) return -1; - /* pretend if it's added by ftw() */ - ret = add_cgroup_name(s, NULL, FTW_D, NULL); + + ret = check_and_add_cgroup_name(s); free(s); - if (ret) + if (ret < 0) return -1; } else { - if (add_cgroup_name("", NULL, FTW_D, NULL) < 0) + if (check_and_add_cgroup_name("/") < 0) return -1; }