From patchwork Wed Sep 25 13:55:22 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Tengda Wu X-Patchwork-Id: 13812034 Received: from dggsgout12.his.huawei.com (dggsgout12.his.huawei.com [45.249.212.56]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 5DBD21922C6; Wed, 25 Sep 2024 14:06:43 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=45.249.212.56 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1727273206; cv=none; b=cPEpvSm0onYQ5g+nf1pDZK2/MDqAxIwSqT508N4j7hzvZTdqMzwZpezNBk6xS74rdlPgkvGeiajj0phHNnjGmVbZ+0Zpre3HeGQ+8cxMtQMekSV+vgdfFk0IDQOGY6l3hD4e6OYmOkLrXYKIJMPHwhu+GH2BlOWUffTFnUzG9q4= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1727273206; c=relaxed/simple; bh=SzO8X16jZRvAUKwUC7XqsRzeE7wXfOxIoUul93xnJtY=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=OCiWKWDx7Wd/VgOQ3ypfTKDVT2Y517scKrUqTqCClr3fGBLSu8xkBAfAnxIxIQ+Bxb/L/hweSl20EhZ24GgyTBx4pKl/XIRFsto3nVbBQ24/75Z62Pfujkeq7O4ZZHcUy6LGedb+AhRYkmnUYqaTAxb5AvqkeOga6ArCZcAu9vc= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=huaweicloud.com; spf=pass smtp.mailfrom=huaweicloud.com; arc=none smtp.client-ip=45.249.212.56 Authentication-Results: smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=huaweicloud.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=huaweicloud.com Received: from mail.maildlp.com (unknown [172.19.163.235]) by dggsgout12.his.huawei.com (SkyGuard) with ESMTP id 4XDJRs6dLfz4f3jHn; Wed, 25 Sep 2024 22:06:17 +0800 (CST) Received: from mail02.huawei.com (unknown [10.116.40.128]) by mail.maildlp.com (Postfix) with ESMTP id 23F921A0568; Wed, 25 Sep 2024 22:06:34 +0800 (CST) Received: from huawei.com (unknown [10.67.174.45]) by APP4 (Coremail) with SMTP id gCh0CgBHfMjLGPRmMo9HCQ--.17785S3; Wed, 25 Sep 2024 22:06:32 +0800 (CST) From: Tengda Wu To: Peter Zijlstra Cc: song@kernel.org, Ingo Molnar , Arnaldo Carvalho de Melo , Namhyung Kim , Mark Rutland , Alexander Shishkin , Jiri Olsa , Ian Rogers , Adrian Hunter , kan.liang@linux.intel.com, linux-perf-users@vger.kernel.org, linux-kernel@vger.kernel.org, bpf@vger.kernel.org Subject: [PATCH -next 1/2] perf stat: Increase perf_attr_map entries Date: Wed, 25 Sep 2024 13:55:22 +0000 Message-Id: <20240925135523.367957-2-wutengda@huaweicloud.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20240925135523.367957-1-wutengda@huaweicloud.com> References: <20240925135523.367957-1-wutengda@huaweicloud.com> Precedence: bulk X-Mailing-List: bpf@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-CM-TRANSID: gCh0CgBHfMjLGPRmMo9HCQ--.17785S3 X-Coremail-Antispam: 1UD129KBjvJXoW7Kr4UCF1fJF47Kw15Jry3XFb_yoW8Ww1kpF 4DCrW7tr15Ww18Jw1DAwsxWas0g3y3uFW3Wr1fKw4FyFnaqwn3KayxWFyYqF15XrZ2934F qr1qgr4UuayUuw7anT9S1TB71UUUUU7qnTZGkaVYY2UrUUUUjbIjqfuFe4nvWSU5nxnvy2 9KBjDU0xBIdaVrnRJUUUPFb4IE77IF4wAFF20E14v26rWj6s0DM7CY07I20VC2zVCF04k2 6cxKx2IYs7xG6rWj6s0DM7CIcVAFz4kK6r1j6r18M28IrcIa0xkI8VA2jI8067AKxVWUGw A2048vs2IY020Ec7CjxVAFwI0_Gr0_Xr1l8cAvFVAK0II2c7xJM28CjxkF64kEwVA0rcxS w2x7M28EF7xvwVC0I7IYx2IY67AKxVW5JVW7JwA2z4x0Y4vE2Ix0cI8IcVCY1x0267AKxV W8Jr0_Cr1UM28EF7xvwVC2z280aVAFwI0_GcCE3s1l84ACjcxK6I8E87Iv6xkF7I0E14v2 6rxl6s0DM2AIxVAIcxkEcVAq07x20xvEncxIr21l5I8CrVACY4xI64kE6c02F40Ex7xfMc Ij6xIIjxv20xvE14v26r1j6r18McIj6I8E87Iv67AKxVWUJVW8JwAm72CE4IkC6x0Yz7v_ Jr0_Gr1lF7xvr2IYc2Ij64vIr41lFIxGxcIEc7CjxVA2Y2ka0xkIwI1lc7CjxVAaw2AFwI 0_Jw0_GFyl42xK82IYc2Ij64vIr41l4I8I3I0E4IkC6x0Yz7v_Jr0_Gr1lx2IqxVAqx4xG 67AKxVWUJVWUGwC20s026x8GjcxK67AKxVWUGVWUWwC2zVAF1VAY17CE14v26r1q6r43MI IYrxkI7VAKI48JMIIF0xvE2Ix0cI8IcVAFwI0_Jr0_JF4lIxAIcVC0I7IYx2IY6xkF7I0E 14v26r4j6F4UMIIF0xvE42xK8VAvwI8IcIk0rVWUJVWUCwCI42IY6I8E87Iv67AKxVWUJV W8JwCI42IY6I8E87Iv6xkF7I0E14v26r4j6r4UJbIYCTnIWIevJa73UjIFyTuYvjxU2HGQ DUUUU X-CM-SenderInfo: pzxwv0hjgdqx5xdzvxpfor3voofrz/ bperf restricts the size of perf_attr_map's entries to 16, which cannot hold all events in many scenarios. A typical example is when the user specifies `-a -ddd` ([0]). And in other cases such as top-down analysis, which often requires a set of more than 16 PMUs to be collected simultaneously. Fix this by increase perf_attr_map entries to 100, and an event number check has been introduced when bperf__load() to ensure that users receive a more friendly prompt when the event limit is reached. [0] https://lore.kernel.org/all/20230104064402.1551516-3-namhyung@kernel.org/ Fixes: 7fac83aaf2ee ("perf stat: Introduce 'bperf' to share hardware PMCs with BPF") Signed-off-by: Tengda Wu --- tools/perf/util/bpf_counter.c | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/tools/perf/util/bpf_counter.c b/tools/perf/util/bpf_counter.c index 7a8af60e0f51..3346129c20cf 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 100 static inline void *u64_to_ptr(__u64 ptr) { @@ -451,6 +451,12 @@ static int bperf__load(struct evsel *evsel, struct target *target) enum bperf_filter_type filter_type; __u32 filter_entry_cnt, i; + if (evsel->evlist->core.nr_entries > ATTR_MAP_SIZE) { + pr_err("Too many events, please limit to %d or less\n", + ATTR_MAP_SIZE); + return -1; + } + if (bperf_check_target(evsel, target, &filter_type, &filter_entry_cnt)) return -1; From patchwork Wed Sep 25 13:55:23 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Tengda Wu X-Patchwork-Id: 13812033 Received: from dggsgout11.his.huawei.com (dggsgout11.his.huawei.com [45.249.212.51]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 954511B3F3E; Wed, 25 Sep 2024 14:06:38 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=45.249.212.51 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1727273201; cv=none; b=IZkYfzVf365nxpTchjcnrF8yXbi4HK0Tnf5l5Kxj/WUPmcLXzC1FRzfwQoyVAOaMssApWwsbZpbIctRdSYqy+SuOuzcUpN2jV3mW5E8/ia31jxuvps6lS/GDOvJrHcBMUBx5xP8mTQKtmw+Xl6b6aEOIB3MzlJrz9/gWn6Cm60Y= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1727273201; c=relaxed/simple; bh=X2lPzjfCfv1KuQ2t48jGTVr/WPoj/wwdy2lWQNASttY=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=IKjo7JO5nczgsDrbmU++T/InK0O1v5ExOcHD2lLpp1dN0CWc6F5y6cE0Aya91mpzYgsSZg6tK9/ua090gB27NBndJPxPB7Znf9sG1oMPDNgaaJT+dnzaQzlKl2pT8siYTBH1GY6S9RMbnhcBrIKZlYHHyd3mAU3zxdwCAlC9osQ= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=huaweicloud.com; spf=pass smtp.mailfrom=huaweicloud.com; arc=none smtp.client-ip=45.249.212.51 Authentication-Results: smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=huaweicloud.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=huaweicloud.com Received: from mail.maildlp.com (unknown [172.19.163.235]) by dggsgout11.his.huawei.com (SkyGuard) with ESMTP id 4XDJRt11D0z4f3lV7; Wed, 25 Sep 2024 22:06:18 +0800 (CST) Received: from mail02.huawei.com (unknown [10.116.40.128]) by mail.maildlp.com (Postfix) with ESMTP id 148281A0568; Wed, 25 Sep 2024 22:06:35 +0800 (CST) Received: from huawei.com (unknown [10.67.174.45]) by APP4 (Coremail) with SMTP id gCh0CgBHfMjLGPRmMo9HCQ--.17785S4; Wed, 25 Sep 2024 22:06:34 +0800 (CST) From: Tengda Wu To: Peter Zijlstra Cc: song@kernel.org, Ingo Molnar , Arnaldo Carvalho de Melo , Namhyung Kim , Mark Rutland , Alexander Shishkin , Jiri Olsa , Ian Rogers , Adrian Hunter , kan.liang@linux.intel.com, linux-perf-users@vger.kernel.org, linux-kernel@vger.kernel.org, bpf@vger.kernel.org Subject: [PATCH -next 2/2] perf stat: Fix incorrect display of bperf when event count is 0 Date: Wed, 25 Sep 2024 13:55:23 +0000 Message-Id: <20240925135523.367957-3-wutengda@huaweicloud.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20240925135523.367957-1-wutengda@huaweicloud.com> References: <20240925135523.367957-1-wutengda@huaweicloud.com> Precedence: bulk X-Mailing-List: bpf@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-CM-TRANSID: gCh0CgBHfMjLGPRmMo9HCQ--.17785S4 X-Coremail-Antispam: 1UD129KBjvJXoWxAr4UGr1rJF1UXw13Cr4UJwb_yoW5KFyDpa n3Cry7KryrXrWDZwn8ZwsFgay09w1fW3yag3s5KrWrAFsxX3ZrXry8Ja12y345XrnYyFya qw4q9r47ArW3ZrJanT9S1TB71UUUUU7qnTZGkaVYY2UrUUUUjbIjqfuFe4nvWSU5nxnvy2 9KBjDU0xBIdaVrnRJUUUPFb4IE77IF4wAFF20E14v26rWj6s0DM7CY07I20VC2zVCF04k2 6cxKx2IYs7xG6rWj6s0DM7CIcVAFz4kK6r1j6r18M28IrcIa0xkI8VA2jI8067AKxVWUXw A2048vs2IY020Ec7CjxVAFwI0_Xr0E3s1l8cAvFVAK0II2c7xJM28CjxkF64kEwVA0rcxS w2x7M28EF7xvwVC0I7IYx2IY67AKxVW5JVW7JwA2z4x0Y4vE2Ix0cI8IcVCY1x0267AKxV W8Jr0_Cr1UM28EF7xvwVC2z280aVAFwI0_GcCE3s1l84ACjcxK6I8E87Iv6xkF7I0E14v2 6rxl6s0DM2AIxVAIcxkEcVAq07x20xvEncxIr21l5I8CrVACY4xI64kE6c02F40Ex7xfMc Ij6xIIjxv20xvE14v26r1j6r18McIj6I8E87Iv67AKxVWUJVW8JwAm72CE4IkC6x0Yz7v_ Jr0_Gr1lF7xvr2IYc2Ij64vIr41lFIxGxcIEc7CjxVA2Y2ka0xkIwI1lc7CjxVAaw2AFwI 0_Jw0_GFyl42xK82IYc2Ij64vIr41l4I8I3I0E4IkC6x0Yz7v_Jr0_Gr1lx2IqxVAqx4xG 67AKxVWUJVWUGwC20s026x8GjcxK67AKxVWUGVWUWwC2zVAF1VAY17CE14v26r1q6r43MI IYrxkI7VAKI48JMIIF0xvE2Ix0cI8IcVAFwI0_Jr0_JF4lIxAIcVC0I7IYx2IY6xkF7I0E 14v26r4j6F4UMIIF0xvE42xK8VAvwI8IcIk0rVWUJVWUCwCI42IY6I8E87Iv67AKxVWUJV W8JwCI42IY6I8E87Iv6xkF7I0E14v26r4j6r4UJbIYCTnIWIevJa73UjIFyTuYvjxUFSdy UUUUU X-CM-SenderInfo: pzxwv0hjgdqx5xdzvxpfor3voofrz/ There are 2 possible reasons for the event count being 0: not supported and not counted. Perf distinguishes between these two possibilities through `evsel->supported`, but in bperf, this value is always false. This is because bperf is prematurely break or continue in the evlist__for_each_cpu loop under __run_perf_stat(), skipping the `counter->supported` assignment, resulting in bperf incorrectly displaying when the count is 0. The most direct way to fix it is to do `evsel->supported` assignment when opening an event in bperf. However, since bperf only opens events when loading the leader, the followers are not aware of whether the event is supported or not. Therefore, we store the `evsel->supported` value in a common location, which is the `perf_event_attr_map`, to achieve synchronization of event support across perf sessions. Fixes: 7fac83aaf2ee ("perf stat: Introduce 'bperf' to share hardware PMCs with BPF") Signed-off-by: Tengda Wu --- tools/lib/perf/include/perf/bpf_perf.h | 1 + tools/perf/util/bpf_counter.c | 18 ++++++++++-------- 2 files changed, 11 insertions(+), 8 deletions(-) diff --git a/tools/lib/perf/include/perf/bpf_perf.h b/tools/lib/perf/include/perf/bpf_perf.h index e7cf6ba7b674..64c8d211726d 100644 --- a/tools/lib/perf/include/perf/bpf_perf.h +++ b/tools/lib/perf/include/perf/bpf_perf.h @@ -23,6 +23,7 @@ struct perf_event_attr_map_entry { __u32 link_id; __u32 diff_map_id; + __u8 supported; }; /* default attr_map name */ diff --git a/tools/perf/util/bpf_counter.c b/tools/perf/util/bpf_counter.c index 3346129c20cf..c04b274c3c45 100644 --- a/tools/perf/util/bpf_counter.c +++ b/tools/perf/util/bpf_counter.c @@ -425,18 +425,19 @@ static int bperf_reload_leader_program(struct evsel *evsel, int attr_map_fd, diff_map_fd = bpf_map__fd(skel->maps.diff_readings); entry->link_id = bpf_link_get_id(link_fd); entry->diff_map_id = bpf_map_get_id(diff_map_fd); - err = bpf_map_update_elem(attr_map_fd, &evsel->core.attr, entry, BPF_ANY); - assert(err == 0); - - evsel->bperf_leader_link_fd = bpf_link_get_fd_by_id(entry->link_id); - assert(evsel->bperf_leader_link_fd >= 0); - /* * save leader_skel for install_pe, which is called within * following evsel__open_per_cpu call */ evsel->leader_skel = skel; - evsel__open_per_cpu(evsel, all_cpu_map, -1); + if (!evsel__open_per_cpu(evsel, all_cpu_map, -1)) + entry->supported = true; + + err = bpf_map_update_elem(attr_map_fd, &evsel->core.attr, entry, BPF_ANY); + assert(err == 0); + + evsel->bperf_leader_link_fd = bpf_link_get_fd_by_id(entry->link_id); + assert(evsel->bperf_leader_link_fd >= 0); out: bperf_leader_bpf__destroy(skel); @@ -446,7 +447,7 @@ static int bperf_reload_leader_program(struct evsel *evsel, int attr_map_fd, static int bperf__load(struct evsel *evsel, struct target *target) { - struct perf_event_attr_map_entry entry = {0xffffffff, 0xffffffff}; + struct perf_event_attr_map_entry entry = {0xffffffff, 0xffffffff, false}; int attr_map_fd, diff_map_fd = -1, err; enum bperf_filter_type filter_type; __u32 filter_entry_cnt, i; @@ -494,6 +495,7 @@ static int bperf__load(struct evsel *evsel, struct target *target) err = -1; goto out; } + evsel->supported = entry.supported; /* * The bpf_link holds reference to the leader program, and the * leader program holds reference to the maps. Therefore, if