From nobody Mon Oct 6 19:09:18 2025 Received: from mail-qv1-f74.google.com (mail-qv1-f74.google.com [209.85.219.74]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 3688F253B64 for ; Thu, 17 Jul 2025 21:02:59 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.219.74 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1752786182; cv=none; b=t6XNRWiOe/q+9UUdiAWULM346JAnTZXxUdolnmeCA4bS2lfX/yf+Iywg3hsPr9cpuZqYDr4K0q+HVlmdiDYmWF3CGc3ft90HW+CVVwSuvPrlgzLcXlwepkmkC3YPjlPgPkv3CYYt3ZmEzubjQfRe5cSmFkppQQvkFwFOymSn8N8= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1752786182; c=relaxed/simple; bh=IZ1dP4fsOOPT7GVDaxQeOs0A2G/SQ78gec0XJFeHBGU=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Content-Type; b=gZ5kfw8Nfj9rqEoCA48IcDKI+7nV6MCiWetJjO2fCZR7zn8Fo9b1OA2OnW6UJFCuRuoS167m3elRhnlvZUKjEb0j9x6Kc6cfxZwWBDR+XTNS/2spzdKhyHnnYYp3Rf9522YqKr+9WxnUQSG9n+1ylC1J0YEekP/jqH4Y40RE8FM= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--irogers.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=WDpT/0+f; arc=none smtp.client-ip=209.85.219.74 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--irogers.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="WDpT/0+f" Received: by mail-qv1-f74.google.com with SMTP id 6a1803df08f44-6fb5f70b93bso23724326d6.3 for ; Thu, 17 Jul 2025 14:02:59 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1752786179; x=1753390979; darn=vger.kernel.org; h=to:from:subject:message-id:references:mime-version:in-reply-to:date :from:to:cc:subject:date:message-id:reply-to; bh=NJyT4HLCqXHRxMmnshYz2066eN/wkiwo71q1NVZg9fs=; b=WDpT/0+fNkaA7es+A2c1XaGH6sqIi+MryPrgx2qAdi8tq96GQFOrC0J2AUsG9h8EqG 5YWEBPrkvP0vt4jTWcolGBER/A7BPzbNQXfkxJUSdxGGaBnyV+4A78TjrqNf5Rdl+GPd lY0R7p2n2iwtXpykRfVMaClThlT7XSOFLGOR0FRCKIPDNkvmkaC6GUHvQ3WBfWg3Qx0H sggBi2x5WUvvI8S+stZmYI2waLdZrCTU3AuzoAohhjVvixJA2yObR2Hn8vUjwEn7VYaw V3MI4zYqVo+iRFKi2t9yIZEji9G7m3qBRfnkMfqi7kiI25v5+hcee8k5o9FV86WOC3hm VOFw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1752786179; x=1753390979; h=to:from:subject:message-id:references:mime-version:in-reply-to:date :x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=NJyT4HLCqXHRxMmnshYz2066eN/wkiwo71q1NVZg9fs=; b=JJ8162iUKSZGq9KOA0QZu9SChZ25AozZWT6fZb3A/OUNtyjExyQmIdzjnLGyYmXPDx qL/RvFiU8qkiBPiYtKLHaCQn7VyB5Jyf269pJnntGgRU3JcCt+a2XxVQ9CVJlQmdLigD a/IJCbGIfi9SOkicLbOPCA36uLEGYY59fg0exQ2FFh9aWO284ev6CQN1tXPZ98NKRUEH FaqIJ5KfdEbA+7O7bUK3bMdsNv2xrJjz0kV6f0vkTGgxNIQRLgAa+C8khbLvrIqZGefW SjsZ34NWhdcW5v8mr2xTInnnLFel0ZJvrzjdfR7grrPnyP7fDFIRWKLC8wjxZOQLXj07 lV3Q== X-Forwarded-Encrypted: i=1; AJvYcCXYNZAXFI/TQGdAQfwfcVKRR7G/5Y5kbFyDWJHoWmPeTiTIfXxrfdPS77TqQeOSFgOzE5KJGMJiXzoy3qs=@vger.kernel.org X-Gm-Message-State: AOJu0YyCG5eDMN2AESsn0OG6hArwkkUpQEsR3PlamKwxFFIl8+YKpSwL Sp+FfD3lFie0Ite3YstfXptAe/Vkv8sh3FwCokz31ZEzArybQ8sI3GEB9N1C2IyW6T26SB5RrGd oIiObpYupOA== X-Google-Smtp-Source: AGHT+IFvJUdN2LoRlCp7zjevHjV8W0jDJbhWqJ+H1qjSiTAF99L0cjVkdvAxGZJH83D2GJXVolUxKi7VVswZ X-Received: from pjbst15.prod.google.com ([2002:a17:90b:1fcf:b0:313:221f:6571]) (user=irogers job=prod-delivery.src-stubby-dispatcher) by 2002:a17:90b:1f87:b0:311:abba:53d2 with SMTP id 98e67ed59e1d1-31caf8db642mr6789448a91.17.1752786159527; Thu, 17 Jul 2025 14:02:39 -0700 (PDT) Date: Thu, 17 Jul 2025 14:02:22 -0700 In-Reply-To: <20250717210233.1143622-1-irogers@google.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20250717210233.1143622-1-irogers@google.com> X-Mailer: git-send-email 2.50.0.727.gbf7dc18ff4-goog Message-ID: <20250717210233.1143622-2-irogers@google.com> Subject: [PATCH v2 01/12] perf parse-events: Warn if a cpu term is unsupported by a CPU From: Ian Rogers To: Thomas Falcon , Peter Zijlstra , Ingo Molnar , Arnaldo Carvalho de Melo , Namhyung Kim , Mark Rutland , Alexander Shishkin , Jiri Olsa , Ian Rogers , Adrian Hunter , Kan Liang , Ben Gainey , James Clark , Howard Chu , Weilin Wang , Levi Yun , "Dr. David Alan Gilbert" , Zhongqiu Han , Blake Jones , Yicong Yang , Anubhav Shelat , Thomas Richter , Jean-Philippe Romain , Song Liu , linux-perf-users@vger.kernel.org, linux-kernel@vger.kernel.org Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Factor requested CPU warning out of evlist and into evsel. At the end of adding an event, perform the warning check. To avoid repeatedly testing if the cpu_list is empty, add a local variable. ``` $ perf stat -e cpu_atom/cycles,cpu=3D1/ -a true WARNING: A requested CPU in '1' is not supported by PMU 'cpu_atom' (CPUs 16= -27) for event 'cpu_atom/cycles/' Performance counter stats for 'system wide': cpu_atom/cycles/ 0.000781511 seconds time elapsed ``` Reviewed-by: Thomas Falcon Signed-off-by: Ian Rogers --- tools/perf/util/evlist.c | 15 +-------------- tools/perf/util/evsel.c | 24 ++++++++++++++++++++++++ tools/perf/util/evsel.h | 2 ++ tools/perf/util/parse-events.c | 12 ++++++++---- 4 files changed, 35 insertions(+), 18 deletions(-) diff --git a/tools/perf/util/evlist.c b/tools/perf/util/evlist.c index 995ad5f654d0..80d8387e6b97 100644 --- a/tools/perf/util/evlist.c +++ b/tools/perf/util/evlist.c @@ -2549,20 +2549,7 @@ void evlist__warn_user_requested_cpus(struct evlist = *evlist, const char *cpu_lis return; =20 evlist__for_each_entry(evlist, pos) { - struct perf_cpu_map *intersect, *to_test, *online =3D cpu_map__online(); - const struct perf_pmu *pmu =3D evsel__find_pmu(pos); - - to_test =3D pmu && pmu->is_core ? pmu->cpus : online; - intersect =3D perf_cpu_map__intersect(to_test, user_requested_cpus); - if (!perf_cpu_map__equal(intersect, user_requested_cpus)) { - char buf[128]; - - cpu_map__snprint(to_test, buf, sizeof(buf)); - pr_warning("WARNING: A requested CPU in '%s' is not supported by PMU '%= s' (CPUs %s) for event '%s'\n", - cpu_list, pmu ? pmu->name : "cpu", buf, evsel__name(pos)); - } - perf_cpu_map__put(intersect); - perf_cpu_map__put(online); + evsel__warn_user_requested_cpus(pos, user_requested_cpus); } perf_cpu_map__put(user_requested_cpus); } diff --git a/tools/perf/util/evsel.c b/tools/perf/util/evsel.c index 3896a04d90af..d9b6bf78d67b 100644 --- a/tools/perf/util/evsel.c +++ b/tools/perf/util/evsel.c @@ -4091,3 +4091,27 @@ void evsel__uniquify_counter(struct evsel *counter) counter->uniquified_name =3D false; } } + +void evsel__warn_user_requested_cpus(struct evsel *evsel, struct perf_cpu_= map *user_requested_cpus) +{ + struct perf_cpu_map *intersect, *online =3D NULL; + const struct perf_pmu *pmu =3D evsel__find_pmu(evsel); + + if (pmu && pmu->is_core) { + intersect =3D perf_cpu_map__intersect(pmu->cpus, user_requested_cpus); + } else { + online =3D cpu_map__online(); + intersect =3D perf_cpu_map__intersect(online, user_requested_cpus); + } + if (!perf_cpu_map__equal(intersect, user_requested_cpus)) { + char buf1[128]; + char buf2[128]; + + cpu_map__snprint(user_requested_cpus, buf1, sizeof(buf1)); + cpu_map__snprint(online ?: pmu->cpus, buf2, sizeof(buf2)); + pr_warning("WARNING: A requested CPU in '%s' is not supported by PMU '%s= ' (CPUs %s) for event '%s'\n", + buf1, pmu ? pmu->name : "cpu", buf2, evsel__name(evsel)); + } + perf_cpu_map__put(intersect); + perf_cpu_map__put(online); +} diff --git a/tools/perf/util/evsel.h b/tools/perf/util/evsel.h index b84ee274602d..cefa8e64c0d5 100644 --- a/tools/perf/util/evsel.h +++ b/tools/perf/util/evsel.h @@ -574,4 +574,6 @@ void evsel__set_config_if_unset(struct perf_pmu *pmu, s= truct evsel *evsel, =20 bool evsel__is_offcpu_event(struct evsel *evsel); =20 +void evsel__warn_user_requested_cpus(struct evsel *evsel, struct perf_cpu_= map *user_requested_cpus); + #endif /* __PERF_EVSEL_H */ diff --git a/tools/perf/util/parse-events.c b/tools/perf/util/parse-events.c index a59ae5ca0f89..3fd6cc0c2794 100644 --- a/tools/perf/util/parse-events.c +++ b/tools/perf/util/parse-events.c @@ -252,6 +252,7 @@ __add_event(struct list_head *list, int *idx, struct evsel *evsel; bool is_pmu_core; struct perf_cpu_map *cpus; + bool has_cpu_list =3D !perf_cpu_map__is_empty(cpu_list); =20 /* * Ensure the first_wildcard_match's PMU matches that of the new event @@ -276,7 +277,7 @@ __add_event(struct list_head *list, int *idx, =20 if (pmu) { is_pmu_core =3D pmu->is_core; - cpus =3D perf_cpu_map__get(perf_cpu_map__is_empty(cpu_list) ? pmu->cpus = : cpu_list); + cpus =3D perf_cpu_map__get(has_cpu_list ? cpu_list : pmu->cpus); perf_pmu__warn_invalid_formats(pmu); if (attr->type =3D=3D PERF_TYPE_RAW || attr->type >=3D PERF_TYPE_MAX) { perf_pmu__warn_invalid_config(pmu, attr->config, name, @@ -291,10 +292,10 @@ __add_event(struct list_head *list, int *idx, } else { is_pmu_core =3D (attr->type =3D=3D PERF_TYPE_HARDWARE || attr->type =3D=3D PERF_TYPE_HW_CACHE); - if (perf_cpu_map__is_empty(cpu_list)) - cpus =3D is_pmu_core ? perf_cpu_map__new_online_cpus() : NULL; - else + if (has_cpu_list) cpus =3D perf_cpu_map__get(cpu_list); + else + cpus =3D is_pmu_core ? cpu_map__online() : NULL; } if (init_attr) event_attr_init(attr); @@ -326,6 +327,9 @@ __add_event(struct list_head *list, int *idx, if (list) list_add_tail(&evsel->core.node, list); =20 + if (has_cpu_list) + evsel__warn_user_requested_cpus(evsel, cpu_list); + return evsel; } =20 --=20 2.50.0.727.gbf7dc18ff4-goog From nobody Mon Oct 6 19:09:18 2025 Received: from mail-oa1-f74.google.com (mail-oa1-f74.google.com [209.85.160.74]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id D0E4D22FAC3 for ; Thu, 17 Jul 2025 21:02:42 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.160.74 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1752786164; cv=none; b=XD1muddReltT3kMxCFQHL4SbPxQYchXd4yu/b618vTWbXYKNM+9yEtHHdw2pOn2WvWGMVIWNV+0LfdIK2kfb+xXGbJInTRduChifLi/eh+wcO0HapHDW7jkjsCLeBlKSYxcG6nD+QjtzEDyaz3nUBzDUPzOeGJGsf2JSElTTDWQ= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1752786164; c=relaxed/simple; bh=zejmlYNwgS/OQpMHacoEnWFRqhftHB8onU2Qvy6vpsU=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Content-Type; b=qTjSqSiQZbOPut/LSrAxxkAWB3ughdgsqMfwiDdLoeAPnDwb12oBv1f/v+tl7HiPXpGncF/HaD9xHqmRIteKeQMqAMqel33GcuKQa3GgiAsKX/dSZq/q2XcZ5BW9AziBmF5ZzA7ZccNQuEwZ4FC4j/Ewdww+ynPGGYRqgsXJDOA= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--irogers.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=j9ilSaMP; arc=none smtp.client-ip=209.85.160.74 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--irogers.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="j9ilSaMP" Received: by mail-oa1-f74.google.com with SMTP id 586e51a60fabf-2eb887a2109so1427256fac.2 for ; Thu, 17 Jul 2025 14:02:42 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1752786162; x=1753390962; darn=vger.kernel.org; h=to:from:subject:message-id:references:mime-version:in-reply-to:date :from:to:cc:subject:date:message-id:reply-to; bh=5dVZGHVi7sQRXRzVKkfTNIiUhn/WdB0GHZb62bnQe1c=; b=j9ilSaMPRHB+DZzYb/SMuDIF0UULb6BkWSNUdI+EdT8bPPRKWBXvNbWvzPTCava9C4 QNWUtVeR7De9Ve/ymmBjvyicp6SLLYSidVcz4SH+HtBAFxO8fK9ubvn3n0b7bZ5D70oU o6d0dw6jYk9xsQ4AcBWeoMx9cXuILOulqrARA/3K/krD8eNDFhxwaKd0bzmCnUjbK82u Q+SmIvGfWkXi85+SPqnQBjykb+7m4xm9NXwScZ4XdIcQwX3BZb16J4ZoSheQnY2ahtbR uNdm5UeRXMkjhM6wuzngNJ1A451VztnwfAe0w0GJt0kYSNZAGd+Qxl6zvON+d4vVT9ob sQ2w== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1752786162; x=1753390962; h=to:from:subject:message-id:references:mime-version:in-reply-to:date :x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=5dVZGHVi7sQRXRzVKkfTNIiUhn/WdB0GHZb62bnQe1c=; b=AYLoK3py9u3kiNbHPoDvNEZwUsi7ck76cAS3MMLWrWGOyTkCwo2mdISVJy/B465+uW prkJeEX25wPQ2Awk6arwp2QfbzDNOvmauIH6l79k/z+LjbH67/RKju/Tq3guNxwytwzH Se5GtkmxodPIavVgX8IIivGxIHU52wEamAdT5gje67fEsA8TragHU/YoWcrTtafk0zvF +jo0IRDYGDRLkRVzzR1McYh0VF+34v0KlMQtF2PGf8hLWsFGOjs0i5u9r0EkgkbkQDPP 0/bX6lUFhjW6Wp0xmH76tq5WmoATgPhVhwl00dil3f4t7ibcEKKVSDj63tUtPXIfKPt3 5o6g== X-Forwarded-Encrypted: i=1; AJvYcCUCB0WiS1Q+zjuMId8Fb+G94dFwhC2tdUSuHoI3DcuLp1a8nchrs3Co00ErWboYWyIJNjWU6ZcUiEmcmSw=@vger.kernel.org X-Gm-Message-State: AOJu0Yw5t+Rviwxj+rI11/bpPRfRYcjmZvPH4hZuSYAWbTNzxqSxhFv0 BrRXGc9YPwmU+tZg/WIPCDxJl2Ro67HEi5FbZlPabRMTn3jTy28MA/9Vs0qKu13AeHHlryZQNcY cBvabd+uo2Q== X-Google-Smtp-Source: AGHT+IEf3wLginiuFgPMj/KhLeA3Fl3A4TQMyoFRYUZgHRewuT2Nl/mXUfU8dUFzGBWTIush9hwAag7yKgmI X-Received: from oabna16.prod.google.com ([2002:a05:6870:6c10:b0:2b8:e735:4798]) (user=irogers job=prod-delivery.src-stubby-dispatcher) by 2002:a05:6870:3048:b0:2d5:2955:aa58 with SMTP id 586e51a60fabf-2ffb1eafd4amr5810811fac.0.1752786161596; Thu, 17 Jul 2025 14:02:41 -0700 (PDT) Date: Thu, 17 Jul 2025 14:02:23 -0700 In-Reply-To: <20250717210233.1143622-1-irogers@google.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20250717210233.1143622-1-irogers@google.com> X-Mailer: git-send-email 2.50.0.727.gbf7dc18ff4-goog Message-ID: <20250717210233.1143622-3-irogers@google.com> Subject: [PATCH v2 02/12] perf stat: Avoid buffer overflow to the aggregation map From: Ian Rogers To: Thomas Falcon , Peter Zijlstra , Ingo Molnar , Arnaldo Carvalho de Melo , Namhyung Kim , Mark Rutland , Alexander Shishkin , Jiri Olsa , Ian Rogers , Adrian Hunter , Kan Liang , Ben Gainey , James Clark , Howard Chu , Weilin Wang , Levi Yun , "Dr. David Alan Gilbert" , Zhongqiu Han , Blake Jones , Yicong Yang , Anubhav Shelat , Thomas Richter , Jean-Philippe Romain , Song Liu , linux-perf-users@vger.kernel.org, linux-kernel@vger.kernel.org Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" CPUs may be created and passed to perf_stat__get_aggr (via config->aggr_get_id), such as in the stat display should_skip_zero_counter. There may be no such aggr_id, for example, if running with a thread. Add a missing bound check and just create IDs for these cases. Reviewed-by: Thomas Falcon Signed-off-by: Ian Rogers --- tools/perf/builtin-stat.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tools/perf/builtin-stat.c b/tools/perf/builtin-stat.c index 77e2248fa7fc..73b4521ab8af 100644 --- a/tools/perf/builtin-stat.c +++ b/tools/perf/builtin-stat.c @@ -1365,7 +1365,7 @@ static struct aggr_cpu_id perf_stat__get_aggr(struct = perf_stat_config *config, struct aggr_cpu_id id; =20 /* per-process mode - should use global aggr mode */ - if (cpu.cpu =3D=3D -1) + if (cpu.cpu =3D=3D -1 || cpu.cpu >=3D config->cpus_aggr_map->nr) return get_id(config, cpu); =20 if (aggr_cpu_id__is_empty(&config->cpus_aggr_map->map[cpu.cpu])) --=20 2.50.0.727.gbf7dc18ff4-goog From nobody Mon Oct 6 19:09:18 2025 Received: from mail-pj1-f74.google.com (mail-pj1-f74.google.com [209.85.216.74]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 50A08231839 for ; Thu, 17 Jul 2025 21:02:44 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.216.74 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1752786165; cv=none; b=UnQSWDNZ0R2x+dM7wJR1OfzW6rsv1PWq0MWYOJYpz8JDB1HBloxZxooG2VUWvIamMwHvQllSNfyV+GjMoIfxnqzrAhJDcgNneM9PEQ9SAV8NfDVmbbJdsmTzD9Zrhfub8/UrmDdzprrJsxCMgJATrgYTwm/U/hoYc2ELtUQBIXs= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1752786165; c=relaxed/simple; bh=rxECd/orHJ+FjXD+vxZ9UCxbrPbeZ0Ymr36fQ+vUgvE=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Content-Type; b=rGMhWwpW8dr8yKwq9BcXh1Ma4GgxfP9NeJskUjv/QOipk4XRZxPIjpgY2Yend9wMSm5tl2UTDqEDG2KTD6D3gAT6vucgky8SM/MPHHqGlwmnn3J5/82xQhBxPc1n5DbPObL+pzjG6UwaCOrMx1+IuPsNmfn0SkMgTTBvyTTneoE= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--irogers.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=kL/aq8pY; arc=none smtp.client-ip=209.85.216.74 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--irogers.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="kL/aq8pY" Received: by mail-pj1-f74.google.com with SMTP id 98e67ed59e1d1-3141f9ce4e2so1955353a91.1 for ; Thu, 17 Jul 2025 14:02:44 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1752786163; x=1753390963; darn=vger.kernel.org; h=to:from:subject:message-id:references:mime-version:in-reply-to:date :from:to:cc:subject:date:message-id:reply-to; bh=8LzGDRK9pbbZy05/7NVo0qy9CZXcleNoSvz0ippsT70=; b=kL/aq8pY0Mc7uRz9P2uhSIaSgGveJw8RXWFzuUU+nef1tEVlGlXrK+dZrPRjYE9BKg UhQk5ev6pjagq9wF+JiM6PvJsdIRraiqTOVphMTJfQzWFyeIiyHcmJRKHiZATwMbjaDQ TZ6s7QzvjiiiBILxWe6yY2NVH88odwJHuC2jrsBGYQO8gDgAg+eJVhv8p0mTswJeEkBl e6lw5CThFaMh4nVddf5Hl1aaVIXX2yy9oOutErAC73TQyreHXs7IZNP09ghL0wD/zjit Yt4zMV0N2/LtULRRotHIW2OUnG4FCq0AM7dC37yfH7+7robP+PLKsli/4ZM7zcnc6DFe 5AWA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1752786163; x=1753390963; h=to:from:subject:message-id:references:mime-version:in-reply-to:date :x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=8LzGDRK9pbbZy05/7NVo0qy9CZXcleNoSvz0ippsT70=; b=Q8v3b0lN4d18cAuVYFgrKxjEUKoQgxPi3/PT8Toc8QXifeTNrNsyNsbLLLYkpl10Gw EQsiISbmj/P4kMZOkSgCJrSw9bXpVjRLrYcVxUMqKwAW045++A0SaJ/Iby7E2d8sP/Be BowElCZUYADp6sFaM64p8/HjMtDf25GvEwyYzWZPo4zrAh2jzR9K5XcPETIePVUhJNYt 1WaKEZVWgVES/c62eABP/sGe5x62dy2KQQ/NmCiUgstFDgubGdSNlfLvT4BLyeYeTrRI AmAXl6yaWKXFA2gC+EJ80ykwzSqBKmfTNpdNOCFW8PQT6CEfrGgeemvHt06VfPumylWr YNzg== X-Forwarded-Encrypted: i=1; AJvYcCVnWbdbdaVzfjVtoKsAN+VgmFgqb+bwwL4hDx+xxOp3VBw2xdlGX+nmd7LXVpEYcTHMdxmVdZh4gZB2A08=@vger.kernel.org X-Gm-Message-State: AOJu0Yy64LnCl0+XCctd1Bpmrv+S4NMbajTE9LXkBtmk0tvRP8eYRKpB 9ApvXs1QPkcUlRrs9rnxmUggm95hrOlymXWiFYdLfg2JMq9gLPrKq0U5bg6ehJv1a5VZ0MuTxb+ x/YtCkk48yw== X-Google-Smtp-Source: AGHT+IGsliyvhCftHU8djUYilaALOZXqY6bbgwjC12dls6LI/EgUoGa/R5hu086zF4Ygat+4qBbb9lXx2O9T X-Received: from pjb7.prod.google.com ([2002:a17:90b:2f07:b0:31c:160d:e3be]) (user=irogers job=prod-delivery.src-stubby-dispatcher) by 2002:a17:90b:270b:b0:312:e8ed:763 with SMTP id 98e67ed59e1d1-31c9f42412cmr9768817a91.22.1752786163575; Thu, 17 Jul 2025 14:02:43 -0700 (PDT) Date: Thu, 17 Jul 2025 14:02:24 -0700 In-Reply-To: <20250717210233.1143622-1-irogers@google.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20250717210233.1143622-1-irogers@google.com> X-Mailer: git-send-email 2.50.0.727.gbf7dc18ff4-goog Message-ID: <20250717210233.1143622-4-irogers@google.com> Subject: [PATCH v2 03/12] perf stat: Don't size aggregation ids from user_requested_cpus From: Ian Rogers To: Thomas Falcon , Peter Zijlstra , Ingo Molnar , Arnaldo Carvalho de Melo , Namhyung Kim , Mark Rutland , Alexander Shishkin , Jiri Olsa , Ian Rogers , Adrian Hunter , Kan Liang , Ben Gainey , James Clark , Howard Chu , Weilin Wang , Levi Yun , "Dr. David Alan Gilbert" , Zhongqiu Han , Blake Jones , Yicong Yang , Anubhav Shelat , Thomas Richter , Jean-Philippe Romain , Song Liu , linux-perf-users@vger.kernel.org, linux-kernel@vger.kernel.org Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" As evsels may have additional CPU terms, the user_requested_cpus may not reflect all the CPUs requested. Use evlist->all_cpus to size the array as that reflects all the CPUs potentially needed by the evlist. Reviewed-by: Thomas Falcon Signed-off-by: Ian Rogers --- tools/perf/builtin-stat.c | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-) diff --git a/tools/perf/builtin-stat.c b/tools/perf/builtin-stat.c index 73b4521ab8af..00fce828cd5e 100644 --- a/tools/perf/builtin-stat.c +++ b/tools/perf/builtin-stat.c @@ -1513,11 +1513,8 @@ static int perf_stat_init_aggr_mode(void) * taking the highest cpu number to be the size of * the aggregation translate cpumap. */ - if (!perf_cpu_map__is_any_cpu_or_is_empty(evsel_list->core.user_requested= _cpus)) - nr =3D perf_cpu_map__max(evsel_list->core.user_requested_cpus).cpu; - else - nr =3D 0; - stat_config.cpus_aggr_map =3D cpu_aggr_map__empty_new(nr + 1); + nr =3D perf_cpu_map__max(evsel_list->core.all_cpus).cpu + 1; + stat_config.cpus_aggr_map =3D cpu_aggr_map__empty_new(nr); return stat_config.cpus_aggr_map ? 0 : -ENOMEM; } =20 --=20 2.50.0.727.gbf7dc18ff4-goog From nobody Mon Oct 6 19:09:18 2025 Received: from mail-pl1-f201.google.com (mail-pl1-f201.google.com [209.85.214.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 123802367A9 for ; Thu, 17 Jul 2025 21:02:45 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.214.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1752786167; cv=none; b=bvIMaE5+uFNzmzKo6qK9ykwtoZwcP+DbzbQenZhViIkTkyDenMVisdFx/DGB3Lfx9Y2QOh6W+QePEzu7pXIYc0kWtC9iSZhqXg/F6PFqFO003A5VysH2+lQhJCFDGfZm03yqYwGAiwZ4SpX9mvdaeRxPtaBL0lGAG9d4vCihquY= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1752786167; c=relaxed/simple; bh=3iqPS74Vm3U8TVu2LqxzGlONC7F3fEEx0C3HxO9oOX8=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Content-Type; b=gJ1uu1Vn5o8/Dot6iziRUqDfVb+g3A5o96qB9VSwvk58vx8Kykcmp0q11mZWyjRD3DHLBxTDfNjfS41GnswTL7pOY6COHCg1BZLCUVc2AvhyrBIJXTD068zThm9qB/ClhYnFQMxLPkaKe+BCDjjxYoQ4L15fl/G0Ng1Mzw/NAjo= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--irogers.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=iLmY1Eg5; arc=none smtp.client-ip=209.85.214.201 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--irogers.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="iLmY1Eg5" Received: by mail-pl1-f201.google.com with SMTP id d9443c01a7336-236725af87fso19718715ad.3 for ; Thu, 17 Jul 2025 14:02:45 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1752786165; x=1753390965; darn=vger.kernel.org; h=to:from:subject:message-id:references:mime-version:in-reply-to:date :from:to:cc:subject:date:message-id:reply-to; bh=TDJzHa5Jjh3EgodtOKHDgfY+ccZxVF0sXpFhOe3DuRk=; b=iLmY1Eg59iQ404M3s+t6O8sv7Pehl8V4yM24wXCEf0Gn+rYKic9f3HWwHkOXHKTJ4l VS4Ps/4EQ03OSuPpECW9QadFob8RIU8+O542c3T54ZtP6rgTD5G3wZQwfwg59vDYB4FQ X1jhFY5LbPrHYxfTv8J7OqThUROz/+xTy68yFxhkZbVTLwbI8+bVbUZ5UacIhNyExLe7 WqxD0KUVQQSwT7LumFaGLH90D+oJKAxXcESmCFMFOhFpdILRBbKueriTNB8epQKppwxa i5fgOxIxSotQuO3BB2YFVzQfaOpi1eL61lPj44iICzL4zdjftzFsF8Jmy6OEnxw2Q5zg D6XA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1752786165; x=1753390965; h=to:from:subject:message-id:references:mime-version:in-reply-to:date :x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=TDJzHa5Jjh3EgodtOKHDgfY+ccZxVF0sXpFhOe3DuRk=; b=fPgqB3YAH91za0gMDP0qKXigIt7gdzdNTB1IRhE7rHmTSBo66dRrY75UVQMNeF2iK2 7VWa1xTA7Zp0tBtxeBxDVgrgEm6rn/livZx9XKbHoUmMi5iy+AgPyHczBOraPMlkRorO O25PdsEdcG8iIOz+i7hwkEc4WDY+a41lBIre7kRkF0CmhzNGZwhBT7qAhGf5hVILrReC rkTXjwB0pbkav8d1YnLxcXnNbHWtWjQxFWnVrYEzF1ZrANiPd0cEU1vjUS9CBwlVYTpD 3y3plEgrvIorYUQ7C+Xrp1SNTGmmirC4ylg0cZUCF5XlsPPqz5UhkPmUcuXq7wf3f7xt thAw== X-Forwarded-Encrypted: i=1; AJvYcCULB8k6ZepsEzTMivJ5NROt+SdghysTI8gkh7BcqOUQ1C3CUj52MkPpptwPLM/nuPBnXnC7kBSWJtfN5/g=@vger.kernel.org X-Gm-Message-State: AOJu0YzBLQJw9v77EnSCGPdX9hOqgyegiksvpG9nylxiN3Alyb36AZwB JlFkt6WYFUeaKirIoK4iaV6D1ysc8VAMohbkVa3m96g/7YGOhTRFNXAQmsxq00F+GXQ+UKKeGii emvlluJokxQ== X-Google-Smtp-Source: AGHT+IFT0swixNXm9qdjS7EOMnt4hM5uFE2a5kg8QJcjgZGnBz12S1+iF2upqqz1KJ2dctapi//tVG7rYgTV X-Received: from plgy14.prod.google.com ([2002:a17:903:22ce:b0:237:ca39:8d92]) (user=irogers job=prod-delivery.src-stubby-dispatcher) by 2002:a17:903:3c2c:b0:234:c8f6:1b17 with SMTP id d9443c01a7336-23e3035f29bmr77170915ad.38.1752786165430; Thu, 17 Jul 2025 14:02:45 -0700 (PDT) Date: Thu, 17 Jul 2025 14:02:25 -0700 In-Reply-To: <20250717210233.1143622-1-irogers@google.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20250717210233.1143622-1-irogers@google.com> X-Mailer: git-send-email 2.50.0.727.gbf7dc18ff4-goog Message-ID: <20250717210233.1143622-5-irogers@google.com> Subject: [PATCH v2 04/12] perf parse-events: Allow the cpu term to be a PMU or CPU range From: Ian Rogers To: Thomas Falcon , Peter Zijlstra , Ingo Molnar , Arnaldo Carvalho de Melo , Namhyung Kim , Mark Rutland , Alexander Shishkin , Jiri Olsa , Ian Rogers , Adrian Hunter , Kan Liang , Ben Gainey , James Clark , Howard Chu , Weilin Wang , Levi Yun , "Dr. David Alan Gilbert" , Zhongqiu Han , Blake Jones , Yicong Yang , Anubhav Shelat , Thomas Richter , Jean-Philippe Romain , Song Liu , linux-perf-users@vger.kernel.org, linux-kernel@vger.kernel.org Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" On hybrid systems, events like msr/tsc/ will aggregate counts across all CPUs. Often metrics only want a value like msr/tsc/ for the cores on which the metric is being computed. Listing each CPU with terms cpu=3D0,cpu=3D1.. is laborious and would need to be encoded for all variations of a CPU model. Allow the cpumask from a PMU to be an argument to the cpu term. For example in the following the cpumask of the cstate_pkg PMU selects the CPUs to count msr/tsc/ counter upon: ``` $ cat /sys/bus/event_source/devices/cstate_pkg/cpumask 0 $ perf stat -A -e 'msr/tsc,cpu=3Dcstate_pkg/' -a sleep 0.1 Performance counter stats for 'system wide': CPU0 252,621,253 msr/tsc,cpu=3Dcstate_pkg/ 0.101184092 seconds time elapsed ``` As the cpu term is now also allowed to be a string, allow it to encode a range of CPUs (a list can't be supported as ',' is already a special token). The "event qualifiers" section of the `perf list` man page is updated to detail the additional behavior. The man page formatting is tidied up in this section, as it was incorrectly appearing within the "parameterized events" section. Reviewed-by: Thomas Falcon Signed-off-by: Ian Rogers --- tools/perf/Documentation/perf-list.txt | 25 +++++++++----- tools/perf/util/parse-events.c | 47 +++++++++++++++++++++----- 2 files changed, 54 insertions(+), 18 deletions(-) diff --git a/tools/perf/Documentation/perf-list.txt b/tools/perf/Documentat= ion/perf-list.txt index ce0735021473..28215306a78a 100644 --- a/tools/perf/Documentation/perf-list.txt +++ b/tools/perf/Documentation/perf-list.txt @@ -278,26 +278,33 @@ also be supplied. For example: =20 perf stat -C 0 -e 'hv_gpci/dtbp_ptitc,phys_processor_idx=3D0x2/' ... =20 -EVENT QUALIFIERS: +EVENT QUALIFIERS +---------------- =20 It is also possible to add extra qualifiers to an event: =20 percore: =20 -Sums up the event counts for all hardware threads in a core, e.g.: - - - perf stat -e cpu/event=3D0,umask=3D0x3,percore=3D1/ + Sums up the event counts for all hardware threads in a core, e.g.: + perf stat -e cpu/event=3D0,umask=3D0x3,percore=3D1/ =20 cpu: =20 -Specifies the CPU to open the event upon. The value may be repeated to -specify opening the event on multiple CPUs: + Specifies a CPU or a range of CPUs to open the event upon. It may + also reference a PMU to copy the CPU mask from. The value may be + repeated to specify opening the event on multiple CPUs. =20 + Example 1: to open the instructions event on CPUs 0 and 2, the + cycles event on CPUs 1 and 2: + perf stat -e instructions/cpu=3D0,cpu=3D2/,cycles/cpu=3D1-2/ -a sleep 1 =20 - perf stat -e instructions/cpu=3D0,cpu=3D2/,cycles/cpu=3D1,cpu=3D2/ -a sl= eep 1 - perf stat -e data_read/cpu=3D0/,data_write/cpu=3D1/ -a sleep 1 + Example 2: to open the data_read uncore event on CPU 0 and the + data_write uncore event on CPU 1: + perf stat -e data_read/cpu=3D0/,data_write/cpu=3D1/ -a sleep 1 =20 + Example 3: to open the software msr/tsc/ event only on the CPUs + matching those from the cpu_core PMU: + perf stat -e msr/tsc,cpu=3Dcpu_core/ -a sleep 1 =20 EVENT GROUPS ------------ diff --git a/tools/perf/util/parse-events.c b/tools/perf/util/parse-events.c index 3fd6cc0c2794..a337e4d22ff2 100644 --- a/tools/perf/util/parse-events.c +++ b/tools/perf/util/parse-events.c @@ -187,10 +187,22 @@ static struct perf_cpu_map *get_config_cpu(const stru= ct parse_events_terms *head =20 list_for_each_entry(term, &head_terms->terms, list) { if (term->type_term =3D=3D PARSE_EVENTS__TERM_TYPE_CPU) { - struct perf_cpu_map *cpu =3D perf_cpu_map__new_int(term->val.num); - - perf_cpu_map__merge(&cpus, cpu); - perf_cpu_map__put(cpu); + struct perf_cpu_map *term_cpus; + + if (term->type_val =3D=3D PARSE_EVENTS__TERM_TYPE_NUM) { + term_cpus =3D perf_cpu_map__new_int(term->val.num); + } else { + struct perf_pmu *pmu =3D perf_pmus__find(term->val.str); + + if (pmu && perf_cpu_map__is_empty(pmu->cpus)) + term_cpus =3D pmu->is_core ? cpu_map__online() : NULL; + else if (pmu) + term_cpus =3D perf_cpu_map__get(pmu->cpus); + else + term_cpus =3D perf_cpu_map__new(term->val.str); + } + perf_cpu_map__merge(&cpus, term_cpus); + perf_cpu_map__put(term_cpus); } } =20 @@ -1048,15 +1060,32 @@ do { \ return -EINVAL; } break; - case PARSE_EVENTS__TERM_TYPE_CPU: - CHECK_TYPE_VAL(NUM); - if (term->val.num >=3D (u64)cpu__max_present_cpu().cpu) { + case PARSE_EVENTS__TERM_TYPE_CPU: { + struct perf_cpu_map *map; + + if (term->type_val =3D=3D PARSE_EVENTS__TERM_TYPE_NUM) { + if (term->val.num >=3D (u64)cpu__max_present_cpu().cpu) { + parse_events_error__handle(err, term->err_val, + strdup("too big"), + /*help=3D*/NULL); + return -EINVAL; + } + break; + } + assert(term->type_val =3D=3D PARSE_EVENTS__TERM_TYPE_STR); + if (perf_pmus__find(term->val.str) !=3D NULL) + break; + + map =3D perf_cpu_map__new(term->val.str); + if (!map) { parse_events_error__handle(err, term->err_val, - strdup("too big"), - NULL); + strdup("not a valid PMU or CPU number"), + /*help=3D*/NULL); return -EINVAL; } + perf_cpu_map__put(map); break; + } case PARSE_EVENTS__TERM_TYPE_DRV_CFG: case PARSE_EVENTS__TERM_TYPE_USER: case PARSE_EVENTS__TERM_TYPE_LEGACY_CACHE: --=20 2.50.0.727.gbf7dc18ff4-goog From nobody Mon Oct 6 19:09:18 2025 Received: from mail-pj1-f74.google.com (mail-pj1-f74.google.com [209.85.216.74]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 614CA21931C for ; Thu, 17 Jul 2025 21:02:48 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.216.74 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1752786169; cv=none; b=nQraJNajP9H+ofSevADDEw6W9ztYUZGbZwFe/2BCFACrvzLKk521ilrmVrnt0xUIawo/ryHklPLVMt+HkCOdDxIvF8RVkuyBP1CcUEWQKgYNzQSLxlX/mNKIk4KXDfHnHIafCoNaJDQy9zm6jNwIudKQl94aDvLiVCTI+tZhrkk= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1752786169; c=relaxed/simple; bh=LV19GYynUUF0CjbElgTqMTM8C4erDLHMoiN6xloIXdY=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Content-Type; b=OL1QcgZV8sKSGKX5ntgmJcLShUz+YHNiuroW8vsrf5fz49d/0NGcSq+YmkFSBQY6cVqG6NO3cuc3IDvlGai3hmFuwsTYIRujeFOqSES1Negv+iCbDUMRLrTlON98NMqCRDjmjRwQ+V+NnaROUhCiu4xPzG+bqenj4WikTN+qPUg= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--irogers.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=sWIPQ7gk; arc=none smtp.client-ip=209.85.216.74 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--irogers.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="sWIPQ7gk" Received: by mail-pj1-f74.google.com with SMTP id 98e67ed59e1d1-31332dc2b59so1194108a91.0 for ; Thu, 17 Jul 2025 14:02:48 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1752786168; x=1753390968; darn=vger.kernel.org; h=to:from:subject:message-id:references:mime-version:in-reply-to:date :from:to:cc:subject:date:message-id:reply-to; bh=/sNed9QLILs+6YAkcENeXJYowIz3bMiJ6NyLJB9+4y0=; b=sWIPQ7gkTrBq4eyQZt/quXUJcdK421s0oa34qw38yLmR+B1fqKO/YWgMvrO4n1NU72 r4eOjmTuzYSFDlt3W0gggVpg+s4xrBKnXO74eVLBNvjbGoN4SoVZ9n4b0IQQpr9lp8s+ xZqsDhhXzkxKztcMLroVxiY++znjeRZcICqChyvcBjk7zPHK2B8FXfVI8K3qwcZi+2f5 Ot/+pBJ8VTcRJM1ZfAffwb8JJ76rxxMzULpOdo6/yRyvHflK0haLVrv3yf+q9vVyMq7F DQ9XVoutP6QtJxrB+D7AH/MDdRkmJwc3bV7Hw4qrjqxnwP3/7JMAmJsQDh7iM3lXcthc up7A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1752786168; x=1753390968; h=to:from:subject:message-id:references:mime-version:in-reply-to:date :x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=/sNed9QLILs+6YAkcENeXJYowIz3bMiJ6NyLJB9+4y0=; b=R88wd1Smb93D9lZTP30nG0Urd4rzkxMzDpGDPvWs3xROt28Z3HLiQp69HDb8LR1/JL Zx+Po2rQVoNWx5Qbu9+WcywGqPLAqkfZYo43RaqJ7ME9XHOS8f2nIpgDMw+Br9fbYf9S HbyAOCzw1vBVcifgUPcX9xVg5vIhJZquMH7g8rfCV7HTinX5+3zp60ED3PYo0Pxva2PV vv+XBVu2ax3S1I7gGU6MUi9lZJ5q88S0S7yqjqM3ueOpY5N5FjeGDKIBjbSG0kogAWX3 GfJr5YAKf6ScC4JhcXyNCeeqy0uz3qNjdQFNrCmndn3dMrudot02cpD7rFfg6Ax0N/Eh 19fg== X-Forwarded-Encrypted: i=1; AJvYcCVXZ9ZXDKLWFPUYosvCNhV+REe2aVk/yuT6+SPeiukDEyujW39bAhO0dvSAAAJHiBGRVnv5OANGFyoHVUg=@vger.kernel.org X-Gm-Message-State: AOJu0YytgEk+gzn0WeyHhY21RzDJvVR8uSUd/61B+XKgngFeBzdWxXyj FAYV8JFFVOpFitPA2vucSI3vDx3Y9MO1ZFjmDEjjrvnkkE5swDA0YgFG6FPeVbv5BA0uXyVb+tV AxMhwoNnY0g== X-Google-Smtp-Source: AGHT+IF1N5bP0NH1x3UUsnmmORw1kMloHDLxHBityI1C1Ml7b5Hy2MMBow/vt/WFaEmPoxG4b7rht9xNYEnM X-Received: from pji6.prod.google.com ([2002:a17:90b:3fc6:b0:313:2213:1f54]) (user=irogers job=prod-delivery.src-stubby-dispatcher) by 2002:a17:90b:3c09:b0:311:ffe8:20ee with SMTP id 98e67ed59e1d1-31c9e6f7349mr12023057a91.11.1752786167666; Thu, 17 Jul 2025 14:02:47 -0700 (PDT) Date: Thu, 17 Jul 2025 14:02:26 -0700 In-Reply-To: <20250717210233.1143622-1-irogers@google.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20250717210233.1143622-1-irogers@google.com> X-Mailer: git-send-email 2.50.0.727.gbf7dc18ff4-goog Message-ID: <20250717210233.1143622-6-irogers@google.com> Subject: [PATCH v2 05/12] perf tool_pmu: Allow num_cpus(_online) to be specific to a cpumask From: Ian Rogers To: Thomas Falcon , Peter Zijlstra , Ingo Molnar , Arnaldo Carvalho de Melo , Namhyung Kim , Mark Rutland , Alexander Shishkin , Jiri Olsa , Ian Rogers , Adrian Hunter , Kan Liang , Ben Gainey , James Clark , Howard Chu , Weilin Wang , Levi Yun , "Dr. David Alan Gilbert" , Zhongqiu Han , Blake Jones , Yicong Yang , Anubhav Shelat , Thomas Richter , Jean-Philippe Romain , Song Liu , linux-perf-users@vger.kernel.org, linux-kernel@vger.kernel.org Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" For hybrid metrics it is useful to know the number of p-core or e-core CPUs. If a cpumask is specified for the num_cpus or num_cpus_online tool events, compute the value relative to the given mask rather than for the full system. ``` $ sudo /tmp/perf/perf stat -e 'tool/num_cpus/,tool/num_cpus,cpu=3Dcpu_core/, tool/num_cpus,cpu=3Dcpu_atom/,tool/num_cpus_online/,tool/num_cpus_online, cpu=3Dcpu_core/,tool/num_cpus_online,cpu=3Dcpu_atom/' true Performance counter stats for 'true': 28 tool/num_cpus/ 16 tool/num_cpus,cpu=3Dcpu_core/ 12 tool/num_cpus,cpu=3Dcpu_atom/ 28 tool/num_cpus_online/ 16 tool/num_cpus_online,cpu=3Dcpu_core/ 12 tool/num_cpus_online,cpu=3Dcpu_atom/ 0.000767205 seconds time elapsed 0.000938000 seconds user 0.000000000 seconds sys ``` Reviewed-by: Thomas Falcon Signed-off-by: Ian Rogers --- tools/perf/util/expr.c | 2 +- tools/perf/util/tool_pmu.c | 56 +++++++++++++++++++++++++++++++++----- tools/perf/util/tool_pmu.h | 2 +- 3 files changed, 51 insertions(+), 9 deletions(-) diff --git a/tools/perf/util/expr.c b/tools/perf/util/expr.c index ca70a14c7cdf..7fda0ff89c16 100644 --- a/tools/perf/util/expr.c +++ b/tools/perf/util/expr.c @@ -401,7 +401,7 @@ double expr__get_literal(const char *literal, const str= uct expr_scanner_ctx *ctx if (ev !=3D TOOL_PMU__EVENT_NONE) { u64 count; =20 - if (tool_pmu__read_event(ev, &count)) + if (tool_pmu__read_event(ev, /*evsel=3D*/NULL, &count)) result =3D count; else pr_err("Failure to read '%s'", literal); diff --git a/tools/perf/util/tool_pmu.c b/tools/perf/util/tool_pmu.c index 4630b8cc8e52..7aa4f315b0ac 100644 --- a/tools/perf/util/tool_pmu.c +++ b/tools/perf/util/tool_pmu.c @@ -332,7 +332,7 @@ static bool has_pmem(void) return has_pmem; } =20 -bool tool_pmu__read_event(enum tool_pmu_event ev, u64 *result) +bool tool_pmu__read_event(enum tool_pmu_event ev, struct evsel *evsel, u64= *result) { const struct cpu_topology *topology; =20 @@ -347,18 +347,60 @@ bool tool_pmu__read_event(enum tool_pmu_event ev, u64= *result) return true; =20 case TOOL_PMU__EVENT_NUM_CPUS: - *result =3D cpu__max_present_cpu().cpu; + if (!evsel || perf_cpu_map__is_empty(evsel->core.cpus)) { + /* No evsel to be specific to. */ + *result =3D cpu__max_present_cpu().cpu; + } else if (!perf_cpu_map__has_any_cpu(evsel->core.cpus)) { + /* Evsel just has specific CPUs. */ + *result =3D perf_cpu_map__nr(evsel->core.cpus); + } else { + /* + * "Any CPU" event that can be scheduled on any CPU in + * the PMU's cpumask. The PMU cpumask should be saved in + * own_cpus. If not present fall back to max. + */ + if (!perf_cpu_map__is_empty(evsel->core.own_cpus)) + *result =3D perf_cpu_map__nr(evsel->core.own_cpus); + else + *result =3D cpu__max_present_cpu().cpu; + } return true; =20 case TOOL_PMU__EVENT_NUM_CPUS_ONLINE: { struct perf_cpu_map *online =3D cpu_map__online(); =20 - if (online) { + if (!online) + return false; + + if (!evsel || perf_cpu_map__is_empty(evsel->core.cpus)) { + /* No evsel to be specific to. */ *result =3D perf_cpu_map__nr(online); - perf_cpu_map__put(online); - return true; + } else if (!perf_cpu_map__has_any_cpu(evsel->core.cpus)) { + /* Evsel just has specific CPUs. */ + struct perf_cpu_map *tmp =3D + perf_cpu_map__intersect(online, evsel->core.cpus); + + *result =3D perf_cpu_map__nr(tmp); + perf_cpu_map__put(tmp); + } else { + /* + * "Any CPU" event that can be scheduled on any CPU in + * the PMU's cpumask. The PMU cpumask should be saved in + * own_cpus, if not present then just the online cpu + * mask. + */ + if (!perf_cpu_map__is_empty(evsel->core.own_cpus)) { + struct perf_cpu_map *tmp =3D + perf_cpu_map__intersect(online, evsel->core.own_cpus); + + *result =3D perf_cpu_map__nr(tmp); + perf_cpu_map__put(tmp); + } else { + *result =3D perf_cpu_map__nr(online); + } } - return false; + perf_cpu_map__put(online); + return true; } case TOOL_PMU__EVENT_NUM_DIES: topology =3D online_topology(); @@ -417,7 +459,7 @@ int evsel__tool_pmu_read(struct evsel *evsel, int cpu_m= ap_idx, int thread) old_count =3D perf_counts(evsel->prev_raw_counts, cpu_map_idx, thread); val =3D 0; if (cpu_map_idx =3D=3D 0 && thread =3D=3D 0) { - if (!tool_pmu__read_event(ev, &val)) { + if (!tool_pmu__read_event(ev, evsel, &val)) { count->lost++; val =3D 0; } diff --git a/tools/perf/util/tool_pmu.h b/tools/perf/util/tool_pmu.h index c6ad1dd90a56..d642e7d73910 100644 --- a/tools/perf/util/tool_pmu.h +++ b/tools/perf/util/tool_pmu.h @@ -34,7 +34,7 @@ enum tool_pmu_event tool_pmu__str_to_event(const char *st= r); bool tool_pmu__skip_event(const char *name); int tool_pmu__num_skip_events(void); =20 -bool tool_pmu__read_event(enum tool_pmu_event ev, u64 *result); +bool tool_pmu__read_event(enum tool_pmu_event ev, struct evsel *evsel, u64= *result); =20 u64 tool_pmu__cpu_slots_per_cycle(void); =20 --=20 2.50.0.727.gbf7dc18ff4-goog From nobody Mon Oct 6 19:09:19 2025 Received: from mail-pf1-f202.google.com (mail-pf1-f202.google.com [209.85.210.202]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id ECF5023CEF8 for ; Thu, 17 Jul 2025 21:02:50 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.210.202 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1752786172; cv=none; b=esGGXXtfnc5KRihnXvVYOPdTZkuz7ZzD0Qv3bL+BsoTn/AYdOYgH3WI0lmKZ9dWuH2Xv4uLv1mbetACgScdl/aeETzrUAg7B4X4JIGpq6VjaZInXzhITRxYjiHKohntQWCjrPHTQd/W8jMFOrO/eIS2y4k/UCMsSubs1XH+fGdQ= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1752786172; c=relaxed/simple; bh=5J3GWGp3X0qKm/PVDdnh3hIcgm2jxUOFfVXtpeT61dk=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Content-Type; b=AgNpywd+zkC2O5hR/GNS9vRiMc/S5AeKS+w9OWU4vuVcTBIv04CJGiI/MnKRrULc7g6n06RL6JH4Bc9xu/XQ39YVkzDk9us6WSTvEUddi3RE9u3cSjGMD95g+6N9kZEVxsT7AkT/GYW98ElPorA+cnXi1kEAsfGbGmCxphug1N8= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--irogers.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=sRVv5RzC; arc=none smtp.client-ip=209.85.210.202 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--irogers.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="sRVv5RzC" Received: by mail-pf1-f202.google.com with SMTP id d2e1a72fcca58-747ddba7c90so1210351b3a.0 for ; Thu, 17 Jul 2025 14:02:50 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1752786170; x=1753390970; darn=vger.kernel.org; h=to:from:subject:message-id:references:mime-version:in-reply-to:date :from:to:cc:subject:date:message-id:reply-to; bh=3cEtMLLOSHTjrlHrzBNeeaYGWDamKeHqpltFXvpLlqk=; b=sRVv5RzCkUHBICoK1Pot95sB6dqCXvkoXVT4hEvEERmRDElwRjybjgYcH8o4ccZpMP sfUSyN4TsPcCqj4/jGyH9bklws3HWvhZJ+LmWIlCXmsxW4Ulaw/ilyXGuNLsIZgZi69P v7TJ1zge5kl6DJ7Epyko3fmqRuVCWgoIN0BhyyPwT/foK8O+6AbCnUEaRX21FY+nMjEh 3HxJ9GQXfQGCzqeCe3151eTCUdXKYSPbw/4Lw2iCLFYcF8bUTBIBu3EaOV295KXH/rwW E3qFp+9b6gCyN8E9fB5eTtVLo1z2DiW5SNx+6zQ7qh3ErTO5xlxWn5Tc/MZJOrl5rdAb P2ZQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1752786170; x=1753390970; h=to:from:subject:message-id:references:mime-version:in-reply-to:date :x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=3cEtMLLOSHTjrlHrzBNeeaYGWDamKeHqpltFXvpLlqk=; b=f5wdKveovBXQdiL8Aocjqu6vNWRS9p0lPVSYol5eMtWk+5555KQMJM1/lbE2eGRtVq Wu1S4rHOMOjXttHKHp/ezwTkPwWytHxrH9QcZ9QVns/TEvyNs5rXToihQs3BCqLfRXCS Wotdcz66g5MR+zM5b2rzH8cbKwxcjY3Y/XOvwYRJ8mD4N67r09LRFmw8y4DSgXb39paG KxukLjr2y3BmuIR4AfOsATH2dkFVeZieeRJ+9uUQ2Q4FokunQzL1x8BlEWNZu/qJAd4k iiN8q3a+VlStD6rt0EttziuFJB9XOenkS1vvoHmHWv2wV2sxGTRJAJt8H/PUOLYPLH94 XYEQ== X-Forwarded-Encrypted: i=1; AJvYcCUtWXg1ImMCdm47HIrmsYFqyOQ+UhxHRRYgHN0Uaag8KjBDd1Xzbtq23WA0iw80x1thHi6ndWpnxbhgKIQ=@vger.kernel.org X-Gm-Message-State: AOJu0YxlBELszeF2bo1En4lXE3+gJlTDt0I9We0HTlqx4c0ndEq+w4tB 6Njq38UMnJHjt+wBmXpzTlwDJilc8UgM3oJtKw7yhUlgX2BlMPpDg8t6iY5ATeWC5CCGdqjrhCP eBIIzmmSJQg== X-Google-Smtp-Source: AGHT+IEMNILVPqbTTcQQfUzM0VBg4pa6EBAxiY3VpesCWCLEgrb+MP1b407NfUsQ8yyKQfHuTaAVlYA//qlr X-Received: from pgww7.prod.google.com ([2002:a05:6a02:2c87:b0:b3f:bb3:6f59]) (user=irogers job=prod-delivery.src-stubby-dispatcher) by 2002:a05:6a21:328d:b0:21f:86f1:e2dd with SMTP id adf61e73a8af0-23810c59df1mr13097697637.11.1752786169626; Thu, 17 Jul 2025 14:02:49 -0700 (PDT) Date: Thu, 17 Jul 2025 14:02:27 -0700 In-Reply-To: <20250717210233.1143622-1-irogers@google.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20250717210233.1143622-1-irogers@google.com> X-Mailer: git-send-email 2.50.0.727.gbf7dc18ff4-goog Message-ID: <20250717210233.1143622-7-irogers@google.com> Subject: [PATCH v2 06/12] libperf evsel: Rename own_cpus to pmu_cpus From: Ian Rogers To: Thomas Falcon , Peter Zijlstra , Ingo Molnar , Arnaldo Carvalho de Melo , Namhyung Kim , Mark Rutland , Alexander Shishkin , Jiri Olsa , Ian Rogers , Adrian Hunter , Kan Liang , Ben Gainey , James Clark , Howard Chu , Weilin Wang , Levi Yun , "Dr. David Alan Gilbert" , Zhongqiu Han , Blake Jones , Yicong Yang , Anubhav Shelat , Thomas Richter , Jean-Philippe Romain , Song Liu , linux-perf-users@vger.kernel.org, linux-kernel@vger.kernel.org Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" own_cpus is generally the cpumask from the PMU. Rename to pmu_cpus to try to make this clearer. Variable rename with no other changes. Reviewed-by: Thomas Falcon Signed-off-by: Ian Rogers --- tools/lib/perf/evlist.c | 8 ++++---- tools/lib/perf/evsel.c | 2 +- tools/lib/perf/include/internal/evsel.h | 2 +- tools/perf/tests/event_update.c | 4 ++-- tools/perf/util/evsel.c | 6 +++--- tools/perf/util/header.c | 4 ++-- tools/perf/util/parse-events.c | 2 +- tools/perf/util/synthetic-events.c | 4 ++-- tools/perf/util/tool_pmu.c | 12 ++++++------ 9 files changed, 22 insertions(+), 22 deletions(-) diff --git a/tools/lib/perf/evlist.c b/tools/lib/perf/evlist.c index b1f4c8176b32..9d9dec21f510 100644 --- a/tools/lib/perf/evlist.c +++ b/tools/lib/perf/evlist.c @@ -46,7 +46,7 @@ static void __perf_evlist__propagate_maps(struct perf_evl= ist *evlist, * are valid by intersecting with those of the PMU. */ perf_cpu_map__put(evsel->cpus); - evsel->cpus =3D perf_cpu_map__intersect(evlist->user_requested_cpus, evs= el->own_cpus); + evsel->cpus =3D perf_cpu_map__intersect(evlist->user_requested_cpus, evs= el->pmu_cpus); =20 /* * Empty cpu lists would eventually get opened as "any" so remove @@ -61,7 +61,7 @@ static void __perf_evlist__propagate_maps(struct perf_evl= ist *evlist, list_for_each_entry_from(next, &evlist->entries, node) next->idx--; } - } else if (!evsel->own_cpus || evlist->has_user_cpus || + } else if (!evsel->pmu_cpus || evlist->has_user_cpus || (!evsel->requires_cpu && perf_cpu_map__has_any_cpu(evlist->user_requeste= d_cpus))) { /* * The PMU didn't specify a default cpu map, this isn't a core @@ -72,13 +72,13 @@ static void __perf_evlist__propagate_maps(struct perf_e= vlist *evlist, */ perf_cpu_map__put(evsel->cpus); evsel->cpus =3D perf_cpu_map__get(evlist->user_requested_cpus); - } else if (evsel->cpus !=3D evsel->own_cpus) { + } else if (evsel->cpus !=3D evsel->pmu_cpus) { /* * No user requested cpu map but the PMU cpu map doesn't match * the evsel's. Reset it back to the PMU cpu map. */ perf_cpu_map__put(evsel->cpus); - evsel->cpus =3D perf_cpu_map__get(evsel->own_cpus); + evsel->cpus =3D perf_cpu_map__get(evsel->pmu_cpus); } =20 if (evsel->system_wide) { diff --git a/tools/lib/perf/evsel.c b/tools/lib/perf/evsel.c index 2a85e0bfee1e..127abe7df63d 100644 --- a/tools/lib/perf/evsel.c +++ b/tools/lib/perf/evsel.c @@ -46,7 +46,7 @@ void perf_evsel__delete(struct perf_evsel *evsel) assert(evsel->mmap =3D=3D NULL); /* If not munmap wasn't called. */ assert(evsel->sample_id =3D=3D NULL); /* If not free_id wasn't called. */ perf_cpu_map__put(evsel->cpus); - perf_cpu_map__put(evsel->own_cpus); + perf_cpu_map__put(evsel->pmu_cpus); perf_thread_map__put(evsel->threads); free(evsel); } diff --git a/tools/lib/perf/include/internal/evsel.h b/tools/lib/perf/inclu= de/internal/evsel.h index ea78defa77d0..b97dc8c92882 100644 --- a/tools/lib/perf/include/internal/evsel.h +++ b/tools/lib/perf/include/internal/evsel.h @@ -99,7 +99,7 @@ struct perf_evsel { * cpu map for opening the event on, for example, the first CPU on a * socket for an uncore event. */ - struct perf_cpu_map *own_cpus; + struct perf_cpu_map *pmu_cpus; struct perf_thread_map *threads; struct xyarray *fd; struct xyarray *mmap; diff --git a/tools/perf/tests/event_update.c b/tools/perf/tests/event_updat= e.c index 9301fde11366..cb9e6de2e033 100644 --- a/tools/perf/tests/event_update.c +++ b/tools/perf/tests/event_update.c @@ -109,8 +109,8 @@ static int test__event_update(struct test_suite *test _= _maybe_unused, int subtes TEST_ASSERT_VAL("failed to synthesize attr update name", !perf_event__synthesize_event_update_name(&tmp.tool, evsel, process_eve= nt_name)); =20 - perf_cpu_map__put(evsel->core.own_cpus); - evsel->core.own_cpus =3D perf_cpu_map__new("1,2,3"); + perf_cpu_map__put(evsel->core.pmu_cpus); + evsel->core.pmu_cpus =3D perf_cpu_map__new("1,2,3"); =20 TEST_ASSERT_VAL("failed to synthesize attr update cpus", !perf_event__synthesize_event_update_cpus(&tmp.tool, evsel, process_eve= nt_cpus)); diff --git a/tools/perf/util/evsel.c b/tools/perf/util/evsel.c index d9b6bf78d67b..ba0c9799928b 100644 --- a/tools/perf/util/evsel.c +++ b/tools/perf/util/evsel.c @@ -488,7 +488,7 @@ struct evsel *evsel__clone(struct evsel *dest, struct e= vsel *orig) return NULL; =20 evsel->core.cpus =3D perf_cpu_map__get(orig->core.cpus); - evsel->core.own_cpus =3D perf_cpu_map__get(orig->core.own_cpus); + evsel->core.pmu_cpus =3D perf_cpu_map__get(orig->core.pmu_cpus); evsel->core.threads =3D perf_thread_map__get(orig->core.threads); evsel->core.nr_members =3D orig->core.nr_members; evsel->core.system_wide =3D orig->core.system_wide; @@ -1527,7 +1527,7 @@ void evsel__config(struct evsel *evsel, struct record= _opts *opts, attr->exclude_user =3D 1; } =20 - if (evsel->core.own_cpus || evsel->unit) + if (evsel->core.pmu_cpus || evsel->unit) evsel->core.attr.read_format |=3D PERF_FORMAT_ID; =20 /* @@ -1680,7 +1680,7 @@ void evsel__exit(struct evsel *evsel) evsel__free_config_terms(evsel); cgroup__put(evsel->cgrp); perf_cpu_map__put(evsel->core.cpus); - perf_cpu_map__put(evsel->core.own_cpus); + perf_cpu_map__put(evsel->core.pmu_cpus); perf_thread_map__put(evsel->core.threads); zfree(&evsel->group_name); zfree(&evsel->name); diff --git a/tools/perf/util/header.c b/tools/perf/util/header.c index 53d54fbda10d..d941d7aa0f49 100644 --- a/tools/perf/util/header.c +++ b/tools/perf/util/header.c @@ -4507,8 +4507,8 @@ int perf_event__process_event_update(const struct per= f_tool *tool __maybe_unused case PERF_EVENT_UPDATE__CPUS: map =3D cpu_map__new_data(&ev->cpus.cpus); if (map) { - perf_cpu_map__put(evsel->core.own_cpus); - evsel->core.own_cpus =3D map; + perf_cpu_map__put(evsel->core.pmu_cpus); + evsel->core.pmu_cpus =3D map; } else pr_err("failed to get event_update cpus\n"); default: diff --git a/tools/perf/util/parse-events.c b/tools/perf/util/parse-events.c index a337e4d22ff2..d506f9943506 100644 --- a/tools/perf/util/parse-events.c +++ b/tools/perf/util/parse-events.c @@ -320,7 +320,7 @@ __add_event(struct list_head *list, int *idx, =20 (*idx)++; evsel->core.cpus =3D cpus; - evsel->core.own_cpus =3D perf_cpu_map__get(cpus); + evsel->core.pmu_cpus =3D perf_cpu_map__get(cpus); evsel->core.requires_cpu =3D pmu ? pmu->is_uncore : false; evsel->core.is_pmu_core =3D is_pmu_core; evsel->pmu =3D pmu; diff --git a/tools/perf/util/synthetic-events.c b/tools/perf/util/synthetic= -events.c index 2fc4d0537840..7c00b09e3a93 100644 --- a/tools/perf/util/synthetic-events.c +++ b/tools/perf/util/synthetic-events.c @@ -2045,7 +2045,7 @@ int perf_event__synthesize_event_update_name(const st= ruct perf_tool *tool, struc int perf_event__synthesize_event_update_cpus(const struct perf_tool *tool,= struct evsel *evsel, perf_event__handler_t process) { - struct synthesize_cpu_map_data syn_data =3D { .map =3D evsel->core.own_cp= us }; + struct synthesize_cpu_map_data syn_data =3D { .map =3D evsel->core.pmu_cp= us }; struct perf_record_event_update *ev; int err; =20 @@ -2126,7 +2126,7 @@ int perf_event__synthesize_extra_attr(const struct pe= rf_tool *tool, struct evlis } } =20 - if (evsel->core.own_cpus) { + if (evsel->core.pmu_cpus) { err =3D perf_event__synthesize_event_update_cpus(tool, evsel, process); if (err < 0) { pr_err("Couldn't synthesize evsel cpus.\n"); diff --git a/tools/perf/util/tool_pmu.c b/tools/perf/util/tool_pmu.c index 7aa4f315b0ac..d99e699e646d 100644 --- a/tools/perf/util/tool_pmu.c +++ b/tools/perf/util/tool_pmu.c @@ -357,10 +357,10 @@ bool tool_pmu__read_event(enum tool_pmu_event ev, str= uct evsel *evsel, u64 *resu /* * "Any CPU" event that can be scheduled on any CPU in * the PMU's cpumask. The PMU cpumask should be saved in - * own_cpus. If not present fall back to max. + * pmu_cpus. If not present fall back to max. */ - if (!perf_cpu_map__is_empty(evsel->core.own_cpus)) - *result =3D perf_cpu_map__nr(evsel->core.own_cpus); + if (!perf_cpu_map__is_empty(evsel->core.pmu_cpus)) + *result =3D perf_cpu_map__nr(evsel->core.pmu_cpus); else *result =3D cpu__max_present_cpu().cpu; } @@ -386,12 +386,12 @@ bool tool_pmu__read_event(enum tool_pmu_event ev, str= uct evsel *evsel, u64 *resu /* * "Any CPU" event that can be scheduled on any CPU in * the PMU's cpumask. The PMU cpumask should be saved in - * own_cpus, if not present then just the online cpu + * pmu_cpus, if not present then just the online cpu * mask. */ - if (!perf_cpu_map__is_empty(evsel->core.own_cpus)) { + if (!perf_cpu_map__is_empty(evsel->core.pmu_cpus)) { struct perf_cpu_map *tmp =3D - perf_cpu_map__intersect(online, evsel->core.own_cpus); + perf_cpu_map__intersect(online, evsel->core.pmu_cpus); =20 *result =3D perf_cpu_map__nr(tmp); perf_cpu_map__put(tmp); --=20 2.50.0.727.gbf7dc18ff4-goog From nobody Mon Oct 6 19:09:19 2025 Received: from mail-oa1-f73.google.com (mail-oa1-f73.google.com [209.85.160.73]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 9735B23ED6A for ; Thu, 17 Jul 2025 21:02:52 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.160.73 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1752786174; cv=none; b=XP4QHWWLihSf/LRKUhB+5NfOzmnYxT89ZVv3LMiPKQLRIpDkfoF1TUbQobRVajYYyXTXFoiacstZOmRGNOzimEYAhmQO8YcRXvIjSwytIm6NIE5fLcPx2r2YqKYwGwV+Qu785EvqGohgcVsZPNScF/rkyDV7JNbX1RcQEddvopY= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1752786174; c=relaxed/simple; bh=q5gclP8enVIDgmFTy0uPjEoUXrIKoG7WxEpD69DREJQ=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Content-Type; b=po7oybsHD3f7UCHJCqaYammR/IQNNOyVaWY6qbIGwc+bQWFvJrvfN/fzTgxckR3khzS3WgCRARMZGuhRBeQmVjNR1Uig/UIOMSBKO6ijwDOnENVM2R79JCuulG7GYmr05oEquhZCw0zrENHo/hQ8i3RLpkY1KiHLMGhZM6ycNks= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--irogers.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=1pJDrG3e; arc=none smtp.client-ip=209.85.160.73 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--irogers.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="1pJDrG3e" Received: by mail-oa1-f73.google.com with SMTP id 586e51a60fabf-2f3b9f042a6so1430241fac.3 for ; Thu, 17 Jul 2025 14:02:52 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1752786171; x=1753390971; darn=vger.kernel.org; h=to:from:subject:message-id:references:mime-version:in-reply-to:date :from:to:cc:subject:date:message-id:reply-to; bh=9k2/D5iQ4L1OZOaKDLGd73SeKK5tMn2kJdyY+h5+RPA=; b=1pJDrG3enDpQ+hK4ypwhrXVLne8nRXF9kopAQD4eyXAMvl4KZiYnZXtgO2wCCvKoS7 R8f2n6GnlscYha0CWgBpBLhY+DZdwvAnOhvmsji+BX3c87y9ZtLSTHq29NHBb0yFYFQS aQIll6JwdUhJ9qrLbEWBrk+APwHP7Iseg1p3RcbuDpbKyUD0u0xeNLyf986xeSqoS8+7 LzGQOugF3XjqvV/4VRSjCZFLTN/ucWhoYG6N4abiKuOLSdAckXFyIjnHzvqYWL/cRb1E ywi2A47bVnWgNZr0x46qYdCBxMuWYXbo11bsnRnGzo9d9XOxUdeTJabImJq8PkcPIgRL iRMg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1752786171; x=1753390971; h=to:from:subject:message-id:references:mime-version:in-reply-to:date :x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=9k2/D5iQ4L1OZOaKDLGd73SeKK5tMn2kJdyY+h5+RPA=; b=ZZ0t9TpS5q/n6A2gqZKFv+6CZuTM8h78mUwruY+DaUngkIU+CO86o+tVuPrmfurtRD RImGzSS+L3cq0rvETUZjJ/3JlYYo96pVqJRqA/Xl7qQl+jKAgElFRsaDGgfvR/HnmrJH f3X47OyU0acG3B9ZrtkvGaWsfkphOVsYS9W9prDFVFpcDYBwUxhksvxU8ecUgSJ8WNxu UsR+hUCULO0OO/RPwQT+g63fEjMXRxgTE9WQ2R2ZzY8wDGXx0stSCPgVUACER/OWDQ9h JvoxugkcUN3bVQXXi4obMd6Yb1UbiuLXuq2z5jTBn7EAJU4nVQJceMldZ5FruDALx5u7 DuVA== X-Forwarded-Encrypted: i=1; AJvYcCV1ghDp5BO0UHsKZ3/Uk4j+SU15OVfKoRmmHDqYU07QhFPN28K4xqGYK8KjOuIP4G2gmEFjfH0LrKJ37Lk=@vger.kernel.org X-Gm-Message-State: AOJu0YwedFpexreQw8+vt14bVdWkHuvCg+LXUrjecHeusnAJxE1DRCo8 es6g1zhcvBSLFf+nTtlOvf4N6H4F3HVqLmJ4UvqTeeTJ2/IIUjC6oLpNZYn4JYn9TkmjY/dTULr 6jakbpfOBgQ== X-Google-Smtp-Source: AGHT+IHC9ZZCEmJyp8ykZmSgFWVo+Qs9NWZAEYYccPZ1/jBQqB5hEbwCXwo2B6OdDesRj9peeNspk9nfYpZI X-Received: from oabla12.prod.google.com ([2002:a05:6871:410c:b0:2bc:757d:e145]) (user=irogers job=prod-delivery.src-stubby-dispatcher) by 2002:a05:6870:3b83:b0:2ff:8ad9:f12 with SMTP id 586e51a60fabf-2ffb23eee80mr5406266fac.22.1752786171572; Thu, 17 Jul 2025 14:02:51 -0700 (PDT) Date: Thu, 17 Jul 2025 14:02:28 -0700 In-Reply-To: <20250717210233.1143622-1-irogers@google.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20250717210233.1143622-1-irogers@google.com> X-Mailer: git-send-email 2.50.0.727.gbf7dc18ff4-goog Message-ID: <20250717210233.1143622-8-irogers@google.com> Subject: [PATCH v2 07/12] libperf evsel: Factor perf_evsel__exit out of perf_evsel__delete From: Ian Rogers To: Thomas Falcon , Peter Zijlstra , Ingo Molnar , Arnaldo Carvalho de Melo , Namhyung Kim , Mark Rutland , Alexander Shishkin , Jiri Olsa , Ian Rogers , Adrian Hunter , Kan Liang , Ben Gainey , James Clark , Howard Chu , Weilin Wang , Levi Yun , "Dr. David Alan Gilbert" , Zhongqiu Han , Blake Jones , Yicong Yang , Anubhav Shelat , Thomas Richter , Jean-Philippe Romain , Song Liu , linux-perf-users@vger.kernel.org, linux-kernel@vger.kernel.org Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" This allows the perf_evsel__exit to be called when the struct perf_evsel is embedded inside another struct, such as struct evsel in perf. Reviewed-by: Thomas Falcon Signed-off-by: Ian Rogers --- tools/lib/perf/evsel.c | 7 ++++++- tools/lib/perf/include/internal/evsel.h | 1 + 2 files changed, 7 insertions(+), 1 deletion(-) diff --git a/tools/lib/perf/evsel.c b/tools/lib/perf/evsel.c index 127abe7df63d..13a307fc75ae 100644 --- a/tools/lib/perf/evsel.c +++ b/tools/lib/perf/evsel.c @@ -40,7 +40,7 @@ struct perf_evsel *perf_evsel__new(struct perf_event_attr= *attr) return evsel; } =20 -void perf_evsel__delete(struct perf_evsel *evsel) +void perf_evsel__exit(struct perf_evsel *evsel) { assert(evsel->fd =3D=3D NULL); /* If not fds were not closed. */ assert(evsel->mmap =3D=3D NULL); /* If not munmap wasn't called. */ @@ -48,6 +48,11 @@ void perf_evsel__delete(struct perf_evsel *evsel) perf_cpu_map__put(evsel->cpus); perf_cpu_map__put(evsel->pmu_cpus); perf_thread_map__put(evsel->threads); +} + +void perf_evsel__delete(struct perf_evsel *evsel) +{ + perf_evsel__exit(evsel); free(evsel); } =20 diff --git a/tools/lib/perf/include/internal/evsel.h b/tools/lib/perf/inclu= de/internal/evsel.h index b97dc8c92882..fefe64ba5e26 100644 --- a/tools/lib/perf/include/internal/evsel.h +++ b/tools/lib/perf/include/internal/evsel.h @@ -133,6 +133,7 @@ struct perf_evsel { =20 void perf_evsel__init(struct perf_evsel *evsel, struct perf_event_attr *at= tr, int idx); +void perf_evsel__exit(struct perf_evsel *evsel); int perf_evsel__alloc_fd(struct perf_evsel *evsel, int ncpus, int nthreads= ); void perf_evsel__close_fd(struct perf_evsel *evsel); void perf_evsel__free_fd(struct perf_evsel *evsel); --=20 2.50.0.727.gbf7dc18ff4-goog From nobody Mon Oct 6 19:09:19 2025 Received: from mail-pj1-f73.google.com (mail-pj1-f73.google.com [209.85.216.73]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 0223A22D9E9 for ; Thu, 17 Jul 2025 21:02:54 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.216.73 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1752786175; cv=none; b=BMqFGG7PpmYYEFVLG8pHPysTHKTowZxDWt5JH79wGjNEieo/77xM5C3HzRbgPCjHPUyX8U3pUXm6MywqW5A2B6uWyXhLLqcjFKGoNnnxI+x6ig3tiM1IjBV9rj2jJNY1hfAm2BEqbI5+QRtJR4gcxEGRdniaeQsF8NHvac02bBQ= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1752786175; c=relaxed/simple; bh=wXa1SCmjfrA/iYrTIx3xkr7OdsM/urYyoLsdv3r5x70=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Content-Type; b=A262ihNtI4oPXY2EVd5YCKazVNO6Cqo7jMpEWXYiaKjgapZXqUJfi+g2ALjHUVzO4mBxVKdY2p1J/2qNDLRUIwa7VTxbTx8KrZ6lcF+X7yuMh7VEwNarvFDYfYqru5EVi0WL2zbONf8dgOvFgjziwEySaqrdHQBIES66A0C0/20= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--irogers.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=ZHe7fu0z; arc=none smtp.client-ip=209.85.216.73 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--irogers.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="ZHe7fu0z" Received: by mail-pj1-f73.google.com with SMTP id 98e67ed59e1d1-3138f5e8ff5so1477155a91.3 for ; Thu, 17 Jul 2025 14:02:53 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1752786173; x=1753390973; darn=vger.kernel.org; h=to:from:subject:message-id:references:mime-version:in-reply-to:date :from:to:cc:subject:date:message-id:reply-to; bh=/ZM3pCSwYMC0xtzhmcoaS56jTJWwNHuIZbVeellQWzo=; b=ZHe7fu0z586wTm8v48ZV9Iqw6nHOctzMHHrN4I5o/0/ed8VM3RiPIiBcnB6rtrLRjt sW7J/p/KZ+Ympi9Lkhtv7r9IM3GBIpnbiCVnqCVVHAzS6L5u3+u3jHh003nijm0i5SL/ /X7rgahy0ICkTPV5W+PF6XVWUce2/CVs3YCoHTrBRfJxfh+igFbWY0XcmXkX5wnjL+NY Q3T4taDmea3oSZd4OFY1JuCvmAXW7eXORs7GSr+RhwHdy4LEO45xa5YxZlIeSK4WpCHx dW3SHvhE+q2hxDCtlQLq78SsTY5L/YoMQkXyo08Ho+TLXPx88za+d8RFtk+R6us2EfnY BvVQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1752786173; x=1753390973; h=to:from:subject:message-id:references:mime-version:in-reply-to:date :x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=/ZM3pCSwYMC0xtzhmcoaS56jTJWwNHuIZbVeellQWzo=; b=w7dcC6kM2e00UtzYqoXZ0ZiJfMpQw/alzZsRqlh0Y2qGDTIWQi+OwGB8pvv2HJ2WBY M7iekQ8YYHiz9QF34ATAbDZdhsrM9wAGeBxMg3BdPhOL7hF96qzXlFOicacYGyiy33E9 K2bsKK08qc7dE03yjOB6ah5rJRV064TxhZ/ISj3h2CymaLaKgOGknESZ1WRG1rL3pmJC W0v/2ebc1C90zStUc2RKWbvLnAtzo/5tpb4RRZC39H/eAgyoTa0q5zFj3NXWFZlGAD1V imtCrzcoRQeeGOIoQ3P8sJkLz8mWKa9juiukN8kOIU2xvXVWN/oTqDgQP8sxUmsQqJtj zBRg== X-Forwarded-Encrypted: i=1; AJvYcCVDBbFmaN0/6cPbcNWx75iP58hU/vXY/qwfQutxcH9IEvPugGVhlaF04Fmrsw8H8ZBgyyAhsLL8ITh0lik=@vger.kernel.org X-Gm-Message-State: AOJu0Yw5JBoDe9n4unAtqeBPrkQfXQbCzxhwUbPgJNMBFNIJ8GNCqNBP yARlagpe7+nReScHC/iQmavZg+QyDgd6vmaaDZa/J9okv4+BsSV5C0/l2PAg3UoiLaQ93Ry4VGj TbtUiRioL+Q== X-Google-Smtp-Source: AGHT+IHUu3zgtgmAaVp6ifFETkP1mzQNAhWfaE9t/IlHqPOCXxgmX4IzE1WxtYzCaCEmo6bbXbyj/Om1yeyA X-Received: from pjbst15.prod.google.com ([2002:a17:90b:1fcf:b0:313:221f:6571]) (user=irogers job=prod-delivery.src-stubby-dispatcher) by 2002:a17:90b:58ec:b0:311:c5d9:2c79 with SMTP id 98e67ed59e1d1-31c9f42e84dmr12055444a91.21.1752786173521; Thu, 17 Jul 2025 14:02:53 -0700 (PDT) Date: Thu, 17 Jul 2025 14:02:29 -0700 In-Reply-To: <20250717210233.1143622-1-irogers@google.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20250717210233.1143622-1-irogers@google.com> X-Mailer: git-send-email 2.50.0.727.gbf7dc18ff4-goog Message-ID: <20250717210233.1143622-9-irogers@google.com> Subject: [PATCH v2 08/12] perf evsel: Use libperf perf_evsel__exit From: Ian Rogers To: Thomas Falcon , Peter Zijlstra , Ingo Molnar , Arnaldo Carvalho de Melo , Namhyung Kim , Mark Rutland , Alexander Shishkin , Jiri Olsa , Ian Rogers , Adrian Hunter , Kan Liang , Ben Gainey , James Clark , Howard Chu , Weilin Wang , Levi Yun , "Dr. David Alan Gilbert" , Zhongqiu Han , Blake Jones , Yicong Yang , Anubhav Shelat , Thomas Richter , Jean-Philippe Romain , Song Liu , linux-perf-users@vger.kernel.org, linux-kernel@vger.kernel.org Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Avoid the duplicated code and better enable perf_evsel to change. Reviewed-by: Thomas Falcon Signed-off-by: Ian Rogers --- tools/perf/util/evsel.c | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/tools/perf/util/evsel.c b/tools/perf/util/evsel.c index ba0c9799928b..af2b26c6456a 100644 --- a/tools/perf/util/evsel.c +++ b/tools/perf/util/evsel.c @@ -1679,9 +1679,7 @@ void evsel__exit(struct evsel *evsel) perf_evsel__free_id(&evsel->core); evsel__free_config_terms(evsel); cgroup__put(evsel->cgrp); - perf_cpu_map__put(evsel->core.cpus); - perf_cpu_map__put(evsel->core.pmu_cpus); - perf_thread_map__put(evsel->core.threads); + perf_evsel__exit(&evsel->core); zfree(&evsel->group_name); zfree(&evsel->name); #ifdef HAVE_LIBTRACEEVENT --=20 2.50.0.727.gbf7dc18ff4-goog From nobody Mon Oct 6 19:09:19 2025 Received: from mail-oa1-f73.google.com (mail-oa1-f73.google.com [209.85.160.73]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 3445824469E for ; Thu, 17 Jul 2025 21:02:55 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.160.73 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1752786177; cv=none; b=SeSJXK1oauEvwxhOo12K3QH8jI3I43/u+z3sPfSSz++ptgTvPFNBGvsDFH1RBy1zQ0ftdTKZGfyJWWQL07wvrPyFgOHdiVzMmD8gk3yHs41EJNq4txH5reJZaxcvD63ZPJGuBY/RG55u399i2o6UPsOScNiSWJNXZp9+9bq1a+g= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1752786177; c=relaxed/simple; bh=9zUQeTccp3718gUxjT/0ylX/P1DJl6kmckROyxaibeY=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Content-Type; b=K8Hm3jtSLGETY+sZxvXlSEAaHxhZQFo5dkCuALocCHVMmubewa4oqgFyBsm1wAWFpM28gfqDigvOYeaZ2wePr7YmF3Wt4v3JvH7F04onqkDMK6Xnn69hyJiF0GVzNyyHdKYQ2uNP4dqrnTaEK0KdBg0LjY2wvFZvTprT7yuZNAw= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--irogers.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=S/R0KDpt; arc=none smtp.client-ip=209.85.160.73 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--irogers.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="S/R0KDpt" Received: by mail-oa1-f73.google.com with SMTP id 586e51a60fabf-2d9e7fbfedaso1609047fac.3 for ; Thu, 17 Jul 2025 14:02:55 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1752786175; x=1753390975; darn=vger.kernel.org; h=to:from:subject:message-id:references:mime-version:in-reply-to:date :from:to:cc:subject:date:message-id:reply-to; bh=gJ/d8adwiFNGcK/vA7cpk18Ivr6shkjwDMIS9Lqkdas=; b=S/R0KDptUsOmZ7WfpcmNg6SG639Zq1aIYEjSdATDuM80IIHgOez39KCoXFmSQ98u8T wZ7xqcgJ7LOUSYqEHTR+yL3hF25ZC0LXI+YvdAiCDLKtshY/iIyot/WwsTRjSF1U/T7+ xbLYYGaHG/WWjD+fEOzesZYmWlWstuIuZrLK4AfVt5J6D8+gsvhC0m/NC8XTvvslDdfH qGMuv3zho/RUAOeAM94xz73LBJLrs/hKp9J/xzhMjRCtY3WVf0tUPBrQQRaODjLqCsxn qlsu46Lq1Dc5Dd30UKc7b7rDC+dAYqJziDbyb18JEse8kSrsWpb+4DxwMWzd0W0W+a01 +J9A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1752786175; x=1753390975; h=to:from:subject:message-id:references:mime-version:in-reply-to:date :x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=gJ/d8adwiFNGcK/vA7cpk18Ivr6shkjwDMIS9Lqkdas=; b=X/CjFzGh3MaX9UI+Zj01F6SeV1nZk/byjtxcCDJGOSy3QUzI+7qhDv+XF62n/CGwWo 0ip4D4x2RNhLCF5f/eFBStG8lUk1D/qlLi57mz9UGWdp5nU0AuZzD5MvbBKERf+4oVzT ZTdTPos6PrdJLR7PJk1S6tBITuzmEOGfhweuPAbSfX8P5O96dVID3wp9bJhpRj4a8KA3 QxVz2Si4Wl++WQDzATN72A/rwJsXp2fl/xwX6/5gwoZOhYPS7nyFwGWpPTR9R/m13Nqk HKDqPNA/ZX0skpi8aRB+gxKO7OjEr3gckYKIPFI2054of63VFyZxFeD6MVfq2MjRpiVZ Orpg== X-Forwarded-Encrypted: i=1; AJvYcCV3dhHJokv1yKzXbEBNldMwYA0ITP6y7HEgtGuHXqISiq02Vb+a4Fbw8JfISlhOhQe4FlerJyZo5Vio0Kw=@vger.kernel.org X-Gm-Message-State: AOJu0YyEd1ABpHY5SQnhrV3sftWpE3GpzzpaZBzSCA9qKq4GqPWE8rVn 4eGxxbB7QVHXq6j6uSq9rC8S+LGtEoMQ9qn1yPS1ffTvanKWTnjNxDa1BMsZXBn/ytIINBmxHn5 SdE8daUV9Ig== X-Google-Smtp-Source: AGHT+IFQrdkpOXC1FDEwFmziGHAaQ0FWIYgGoq1nF194P7haGyxFG4Dt7+N/y2IceE5nPHxNl0Pw1Ki0bHrG X-Received: from oabdu25.prod.google.com ([2002:a05:6870:3a19:b0:2ff:b181:6d0c]) (user=irogers job=prod-delivery.src-stubby-dispatcher) by 2002:a05:6871:828:b0:2da:843d:e530 with SMTP id 586e51a60fabf-2ffb22019a1mr6167320fac.2.1752786175170; Thu, 17 Jul 2025 14:02:55 -0700 (PDT) Date: Thu, 17 Jul 2025 14:02:30 -0700 In-Reply-To: <20250717210233.1143622-1-irogers@google.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20250717210233.1143622-1-irogers@google.com> X-Mailer: git-send-email 2.50.0.727.gbf7dc18ff4-goog Message-ID: <20250717210233.1143622-10-irogers@google.com> Subject: [PATCH v2 09/12] perf pmus: Factor perf_pmus__find_by_attr out of evsel__find_pmu From: Ian Rogers To: Thomas Falcon , Peter Zijlstra , Ingo Molnar , Arnaldo Carvalho de Melo , Namhyung Kim , Mark Rutland , Alexander Shishkin , Jiri Olsa , Ian Rogers , Adrian Hunter , Kan Liang , Ben Gainey , James Clark , Howard Chu , Weilin Wang , Levi Yun , "Dr. David Alan Gilbert" , Zhongqiu Han , Blake Jones , Yicong Yang , Anubhav Shelat , Thomas Richter , Jean-Philippe Romain , Song Liu , linux-perf-users@vger.kernel.org, linux-kernel@vger.kernel.org Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Allow a PMU to be found by a perf_event_attr, useful when creating evsels. Reviewed-by: Thomas Falcon Signed-off-by: Ian Rogers --- tools/perf/util/pmus.c | 29 +++++++++++++++++------------ tools/perf/util/pmus.h | 2 ++ 2 files changed, 19 insertions(+), 12 deletions(-) diff --git a/tools/perf/util/pmus.c b/tools/perf/util/pmus.c index 409b909cfa02..9137bb9036ed 100644 --- a/tools/perf/util/pmus.c +++ b/tools/perf/util/pmus.c @@ -814,24 +814,18 @@ bool perf_pmus__supports_extended_type(void) return perf_pmus__do_support_extended_type; } =20 -struct perf_pmu *evsel__find_pmu(const struct evsel *evsel) +struct perf_pmu *perf_pmus__find_by_attr(const struct perf_event_attr *att= r) { - struct perf_pmu *pmu =3D evsel->pmu; - bool legacy_core_type; - - if (pmu) - return pmu; + struct perf_pmu *pmu =3D perf_pmus__find_by_type(attr->type); + u32 type =3D attr->type; + bool legacy_core_type =3D type =3D=3D PERF_TYPE_HARDWARE || type =3D=3D P= ERF_TYPE_HW_CACHE; =20 - pmu =3D perf_pmus__find_by_type(evsel->core.attr.type); - legacy_core_type =3D - evsel->core.attr.type =3D=3D PERF_TYPE_HARDWARE || - evsel->core.attr.type =3D=3D PERF_TYPE_HW_CACHE; if (!pmu && legacy_core_type && perf_pmus__supports_extended_type()) { - u32 type =3D evsel->core.attr.config >> PERF_PMU_TYPE_SHIFT; + type =3D attr->config >> PERF_PMU_TYPE_SHIFT; =20 pmu =3D perf_pmus__find_by_type(type); } - if (!pmu && (legacy_core_type || evsel->core.attr.type =3D=3D PERF_TYPE_R= AW)) { + if (!pmu && (legacy_core_type || type =3D=3D PERF_TYPE_RAW)) { /* * For legacy events, if there was no extended type info then * assume the PMU is the first core PMU. @@ -842,6 +836,17 @@ struct perf_pmu *evsel__find_pmu(const struct evsel *e= vsel) */ pmu =3D perf_pmus__find_core_pmu(); } + return pmu; +} + +struct perf_pmu *evsel__find_pmu(const struct evsel *evsel) +{ + struct perf_pmu *pmu =3D evsel->pmu; + + if (pmu) + return pmu; + + pmu =3D perf_pmus__find_by_attr(&evsel->core.attr); ((struct evsel *)evsel)->pmu =3D pmu; return pmu; } diff --git a/tools/perf/util/pmus.h b/tools/perf/util/pmus.h index 86842ee5f539..7cb36863711a 100644 --- a/tools/perf/util/pmus.h +++ b/tools/perf/util/pmus.h @@ -5,6 +5,7 @@ #include #include =20 +struct perf_event_attr; struct perf_pmu; struct print_callbacks; =20 @@ -16,6 +17,7 @@ void perf_pmus__destroy(void); =20 struct perf_pmu *perf_pmus__find(const char *name); struct perf_pmu *perf_pmus__find_by_type(unsigned int type); +struct perf_pmu *perf_pmus__find_by_attr(const struct perf_event_attr *att= r); =20 struct perf_pmu *perf_pmus__scan(struct perf_pmu *pmu); struct perf_pmu *perf_pmus__scan_core(struct perf_pmu *pmu); --=20 2.50.0.727.gbf7dc18ff4-goog From nobody Mon Oct 6 19:09:19 2025 Received: from mail-pl1-f202.google.com (mail-pl1-f202.google.com [209.85.214.202]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id C9B2C248871 for ; Thu, 17 Jul 2025 21:02:57 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.214.202 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1752786179; cv=none; b=hQezbbfpVnYyxmWd32L0jWDFbC5nDujmgcP4ctLdD+41yGtN6Ulc7T0qsDYMYAbFPm+DiEbiIy3JyDP37p5tnnXUGOdJSYVqVKMJoKp60v0sxTofA+kKpLy/hBQd9LQnDrSexveX/5bV4EA2z89/P2UfkbLJ+pAtuaa7VVlSTjs= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1752786179; c=relaxed/simple; bh=jEgvDggwtQddGz/4ZXMmANkYODrHYVg1OL8Si0VZ5M0=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Content-Type; b=dHWsN+5BD3wTb3laZTSRNDZWhBI65g675DmObFutUb200CfjmfN7x3fmAItzXA9fYt0Q/MQLfvm4kn1yJoYdoAflUSt1vCAxUcxtvRxwF6ta72PrwH7UlhTmFBweBiR6X+4eFZ/5BR6raHD/1riyLDJoyZxioID8YDR5tO8IeeM= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--irogers.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=psJkS3Wr; arc=none smtp.client-ip=209.85.214.202 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--irogers.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="psJkS3Wr" Received: by mail-pl1-f202.google.com with SMTP id d9443c01a7336-23507382e64so13167685ad.2 for ; Thu, 17 Jul 2025 14:02:57 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1752786177; x=1753390977; darn=vger.kernel.org; h=to:from:subject:message-id:references:mime-version:in-reply-to:date :from:to:cc:subject:date:message-id:reply-to; bh=FbO2FaLlRAtP3MPRgVDa/ZaeF16Ze9Vrmjj4VaK1jxk=; b=psJkS3Wryl1ErenAxSbplOvQsC6lh3SH6/5AN20Y4Y0IICjvc9qn3om11IoVP8gkyb JIP82BHZac/C7OFpLUQ+ExYbhGqLUYHmtuu7MrleH45UIDlGJwTt2Rw510haqijBcnGD vHapfkTGn0lRgJMfStxO3jqodaRJQQW14qgbLkn4PWSKJLUYMWC/kOVoQTV3+2ot1a2C gsCEpm49mb+N/fTQfbeA1IzjUKv+4eeDejaAYVnpsOvdqT2JS9jR8+oQlioQbpCE1NSE oRwYWNNOFSiEAAuS9JkyrxGY+J6PKmu49dXrWPa3zwMrTk5YuYZYQvJEvC3JZnqltngS 23IA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1752786177; x=1753390977; h=to:from:subject:message-id:references:mime-version:in-reply-to:date :x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=FbO2FaLlRAtP3MPRgVDa/ZaeF16Ze9Vrmjj4VaK1jxk=; b=Ln3ngZW8ynpg3WLVE0C2DtFhlHn2cEWEWXj3EDnoBrs3JweE3DNXj3rIkquYwiyH5j qLZkNf1FVZiy/jmLh1/PcJJT0IMIQRvfRtnMzSwUdiBbMAnbw9GZi0rAY9H4ntl0omED 79vnDogPtMJRX3Y5+NpkAEAVgaIfJdPnVeayIYZsX50EqigzAOFmQxO/B4FUw0LER7ep QL0VCBhjw8CC/5phTz/F02dlhdox2x71yrIqATb9Ba6XLQbnNwbx0VvTAEAZhNrRZj4W WMjPuZaKLTDGzWV7ZxPKPqbGuV7Nx+IbjN5Ll5qWxJwZWO0FqCprqTFDUZggLWl+FeaA tckQ== X-Forwarded-Encrypted: i=1; AJvYcCXfn0wTRwJSGZfrkQpA6c/VNA+S/eUxxbu1gsJWr3Yp29YveANxx7y2MPb0e8MQ/j6FYYSDg18Ott5lLdQ=@vger.kernel.org X-Gm-Message-State: AOJu0Yx9sCYeh1Rktnz4dsvbGXkyRk33ThEOxFIxndj4gEOOdYhDym+E 7ZtR7tjEILV2P3WPk0A1S+MMYfyRV9LIChSdkNB2FPSC4iFbf41bS2pQvswqeXwUZfuEWusju04 f/qn61I33UA== X-Google-Smtp-Source: AGHT+IHFQ4p2NE4UN0zqFuYQHDWTlKps6FGN31gpRHaNHLnqvCbh3RD+oVb8CVeMEb78YINYoKWK4Hl9ss6o X-Received: from pjbqd16.prod.google.com ([2002:a17:90b:3cd0:b0:31a:aa02:8c50]) (user=irogers job=prod-delivery.src-stubby-dispatcher) by 2002:a17:903:985:b0:231:c9bb:60fd with SMTP id d9443c01a7336-23e2572aademr99943495ad.33.1752786177110; Thu, 17 Jul 2025 14:02:57 -0700 (PDT) Date: Thu, 17 Jul 2025 14:02:31 -0700 In-Reply-To: <20250717210233.1143622-1-irogers@google.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20250717210233.1143622-1-irogers@google.com> X-Mailer: git-send-email 2.50.0.727.gbf7dc18ff4-goog Message-ID: <20250717210233.1143622-11-irogers@google.com> Subject: [PATCH v2 10/12] perf parse-events: Minor __add_event refactoring From: Ian Rogers To: Thomas Falcon , Peter Zijlstra , Ingo Molnar , Arnaldo Carvalho de Melo , Namhyung Kim , Mark Rutland , Alexander Shishkin , Jiri Olsa , Ian Rogers , Adrian Hunter , Kan Liang , Ben Gainey , James Clark , Howard Chu , Weilin Wang , Levi Yun , "Dr. David Alan Gilbert" , Zhongqiu Han , Blake Jones , Yicong Yang , Anubhav Shelat , Thomas Richter , Jean-Philippe Romain , Song Liu , linux-perf-users@vger.kernel.org, linux-kernel@vger.kernel.org Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Rename cpu_list to user_cpus. If a PMU isn't given, find it early from the perf_event_attr. Make the pmu_cpus more explicitly a copy from the PMU (except when user_cpus are given). Derive the cpus from pmu_cpus and user_cpus as appropriate. Handle strdup errors on name and metric_id. Reviewed-by: Thomas Falcon Signed-off-by: Ian Rogers --- tools/perf/util/parse-events.c | 69 +++++++++++++++++++++++----------- 1 file changed, 48 insertions(+), 21 deletions(-) diff --git a/tools/perf/util/parse-events.c b/tools/perf/util/parse-events.c index d506f9943506..bd2d831d5123 100644 --- a/tools/perf/util/parse-events.c +++ b/tools/perf/util/parse-events.c @@ -259,12 +259,12 @@ __add_event(struct list_head *list, int *idx, bool init_attr, const char *name, const char *metric_id, struct perf_pmu *pmu, struct list_head *config_terms, struct evsel *first_wildcard_match, - struct perf_cpu_map *cpu_list, u64 alternate_hw_config) + struct perf_cpu_map *user_cpus, u64 alternate_hw_config) { struct evsel *evsel; bool is_pmu_core; - struct perf_cpu_map *cpus; - bool has_cpu_list =3D !perf_cpu_map__is_empty(cpu_list); + struct perf_cpu_map *cpus, *pmu_cpus; + bool has_user_cpus =3D !perf_cpu_map__is_empty(user_cpus); =20 /* * Ensure the first_wildcard_match's PMU matches that of the new event @@ -288,8 +288,6 @@ __add_event(struct list_head *list, int *idx, } =20 if (pmu) { - is_pmu_core =3D pmu->is_core; - cpus =3D perf_cpu_map__get(has_cpu_list ? cpu_list : pmu->cpus); perf_pmu__warn_invalid_formats(pmu); if (attr->type =3D=3D PERF_TYPE_RAW || attr->type >=3D PERF_TYPE_MAX) { perf_pmu__warn_invalid_config(pmu, attr->config, name, @@ -301,48 +299,77 @@ __add_event(struct list_head *list, int *idx, perf_pmu__warn_invalid_config(pmu, attr->config3, name, PERF_PMU_FORMAT_VALUE_CONFIG3, "config3"); } + } + /* + * If a PMU wasn't given, such as for legacy events, find now that + * warnings won't be generated. + */ + if (!pmu) + pmu =3D perf_pmus__find_by_attr(attr); + + if (pmu) { + is_pmu_core =3D pmu->is_core; + pmu_cpus =3D perf_cpu_map__get(pmu->cpus); } else { is_pmu_core =3D (attr->type =3D=3D PERF_TYPE_HARDWARE || attr->type =3D=3D PERF_TYPE_HW_CACHE); - if (has_cpu_list) - cpus =3D perf_cpu_map__get(cpu_list); - else - cpus =3D is_pmu_core ? cpu_map__online() : NULL; + pmu_cpus =3D is_pmu_core ? cpu_map__online() : NULL; + } + + if (has_user_cpus) { + cpus =3D perf_cpu_map__get(user_cpus); + /* Existing behavior that pmu_cpus matches the given user ones. */ + perf_cpu_map__put(pmu_cpus); + pmu_cpus =3D perf_cpu_map__get(user_cpus); + } else { + cpus =3D perf_cpu_map__get(pmu_cpus); } + if (init_attr) event_attr_init(attr); =20 evsel =3D evsel__new_idx(attr, *idx); - if (!evsel) { - perf_cpu_map__put(cpus); - return NULL; + if (!evsel) + goto out_err; + + if (name) { + evsel->name =3D strdup(name); + if (!evsel->name) + goto out_err; + } + + if (metric_id) { + evsel->metric_id =3D strdup(metric_id); + if (!evsel->metric_id) + goto out_err; } =20 (*idx)++; evsel->core.cpus =3D cpus; - evsel->core.pmu_cpus =3D perf_cpu_map__get(cpus); + evsel->core.pmu_cpus =3D pmu_cpus; evsel->core.requires_cpu =3D pmu ? pmu->is_uncore : false; evsel->core.is_pmu_core =3D is_pmu_core; evsel->pmu =3D pmu; evsel->alternate_hw_config =3D alternate_hw_config; evsel->first_wildcard_match =3D first_wildcard_match; =20 - if (name) - evsel->name =3D strdup(name); - - if (metric_id) - evsel->metric_id =3D strdup(metric_id); - if (config_terms) list_splice_init(config_terms, &evsel->config_terms); =20 if (list) list_add_tail(&evsel->core.node, list); =20 - if (has_cpu_list) - evsel__warn_user_requested_cpus(evsel, cpu_list); + if (has_user_cpus) + evsel__warn_user_requested_cpus(evsel, user_cpus); =20 return evsel; +out_err: + perf_cpu_map__put(cpus); + perf_cpu_map__put(pmu_cpus); + zfree(&evsel->name); + zfree(&evsel->metric_id); + free(evsel); + return NULL; } =20 struct evsel *parse_events__add_event(int idx, struct perf_event_attr *att= r, --=20 2.50.0.727.gbf7dc18ff4-goog From nobody Mon Oct 6 19:09:19 2025 Received: from mail-oa1-f74.google.com (mail-oa1-f74.google.com [209.85.160.74]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 3CC7E253F21 for ; Thu, 17 Jul 2025 21:03:00 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.160.74 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1752786182; cv=none; b=R2r+gBvvhmWta5gx5vvmqDabjKh5YnV5DECW7bJIdM1JODxTm1k8gfm9aBbxwaTzjsfQVmmNYy9wq0VK0bOG9hEgA3pFGhiL64f8e7jYDjjWTHBS6MmjMuIYrxsJrDGSinFe9pz7z5XpWvBBExAXTpCQGSY0Bvj8NpIq4IwK4Xk= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1752786182; c=relaxed/simple; bh=7V57mrxclma+L6i7A+s/RUZqSr9To26yFbxOIRG9Ic8=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Content-Type; b=SZvsUnJwIOXl3ASjhG7iQKFneMIZMsOJ4a5tEmvQdi1qGejM+ecx+4U/OfKmA1EuYJRCuDmL9FKIr8rJI4JPSgvvxrjfTzfqs+KZ8/OzoBsBpH0A15mR7I9qII+BN3MyClQ7DtLCDka97zu14KjXp2U/TM/CmKu6npjUlmehxjY= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--irogers.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=stxozxnO; arc=none smtp.client-ip=209.85.160.74 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--irogers.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="stxozxnO" Received: by mail-oa1-f74.google.com with SMTP id 586e51a60fabf-2ff9bf5ce1aso2367417fac.0 for ; Thu, 17 Jul 2025 14:03:00 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1752786179; x=1753390979; darn=vger.kernel.org; h=to:from:subject:message-id:references:mime-version:in-reply-to:date :from:to:cc:subject:date:message-id:reply-to; bh=lxK32ZV3/TwHJ48GSqnT12vEqhtnY89NH3NlvQCfpOQ=; b=stxozxnOpPXY3REDH7IRmXb8E6OC389QWhcniKGNZoOak1f21aOv8gOnib6xXpkhQn i4u/BMIwtwJfXiNpo1ghtOWwIBzCIGLjk8BMs60i1Dhb/uC85AFREuyZIhrHTJBmM3I6 paueMsMaB/tUANxC3O1+EGoV2J0li8eyrNzIUpbuPwsv9vxgDQcXL/P+5dXudoW+EPAZ 2tIJcpk8X+2yAS5Y32YWSKRHg423BJXIILe1wJ/MeVxdcDVh7QZfPcCo/Mjs40jO6ehk gkdo7ZrTdVIgCiWQfo0PrCRFyvhe4og2EvnCDtKc16xjVPC4eX9NHudem8PO/DI8Yimn TOTA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1752786179; x=1753390979; h=to:from:subject:message-id:references:mime-version:in-reply-to:date :x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=lxK32ZV3/TwHJ48GSqnT12vEqhtnY89NH3NlvQCfpOQ=; b=GjiXpM/uCb6O5I7kwBoTv0GR8Voin1pZRkmwFPP6lQnmHnR4/qqDGLYf6aPbq+PZwv gWXcn1P/Fro5jyu8WII/lbzAi9kQJeTg6pYpFF7XLzoYYyiKdQS3nwJCoPMjQcRhDbZC 0f3aSVhhZXlmsR54LRfNFj6/WdATiy2yAqHawVVCPq9Ro20ZgigUSHdNylXvia8/DenM 5qkVBk1eaRVdn+ltTt32OoSFSONlX8Oe8nTLb66VIYx90xDENS3eiQsrM5v1faXP63aK MaVujXk9tY8lyhx+fl/JDyRBblOPUq3ajTXnZWQ3V1szVkCJYThp+PP22/Q88iVoIFXf C+PA== X-Forwarded-Encrypted: i=1; AJvYcCXWd4DnGCT7TFxCkiu2EJbv1wbOsBZY382BCYAxNi3DZAr4sB1HkevxerJyA/f5ndChg0McdOkq9T3cFlQ=@vger.kernel.org X-Gm-Message-State: AOJu0YzL5tzUV5DfS6KVnLj3c4Ck9eYSlqc8ZD5yWuR/uGEjM5EPtjJl sfKfyuFjklTy2zuP7yufu6krNentkinzkv0sOAOIv07kCJVGuYy4bNupqkQOkdljzno2Zg1+S4c zWFq8ff1MEA== X-Google-Smtp-Source: AGHT+IHcIFzwFnn+SjPmcIqyplDWuJDsGtXSmp5dZf0+tUKV7Fw7+u0UQJPZHt4hNF01UTe3QJIM+mWzCi3d X-Received: from oacdz3.prod.google.com ([2002:a05:6871:ac03:b0:2bc:69a2:7ea5]) (user=irogers job=prod-delivery.src-stubby-dispatcher) by 2002:a05:687c:2f22:b0:2ff:8e7d:1d9f with SMTP id 586e51a60fabf-2ffb2285801mr7054112fac.13.1752786179445; Thu, 17 Jul 2025 14:02:59 -0700 (PDT) Date: Thu, 17 Jul 2025 14:02:32 -0700 In-Reply-To: <20250717210233.1143622-1-irogers@google.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20250717210233.1143622-1-irogers@google.com> X-Mailer: git-send-email 2.50.0.727.gbf7dc18ff4-goog Message-ID: <20250717210233.1143622-12-irogers@google.com> Subject: [PATCH v2 11/12] perf evsel: Add evsel__open_per_cpu_and_thread From: Ian Rogers To: Thomas Falcon , Peter Zijlstra , Ingo Molnar , Arnaldo Carvalho de Melo , Namhyung Kim , Mark Rutland , Alexander Shishkin , Jiri Olsa , Ian Rogers , Adrian Hunter , Kan Liang , Ben Gainey , James Clark , Howard Chu , Weilin Wang , Levi Yun , "Dr. David Alan Gilbert" , Zhongqiu Han , Blake Jones , Yicong Yang , Anubhav Shelat , Thomas Richter , Jean-Philippe Romain , Song Liu , linux-perf-users@vger.kernel.org, linux-kernel@vger.kernel.org Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Add evsel__open_per_cpu_and_thread that combines the operation of evsel__open_per_cpu and evsel__open_per_thread so that an event without the "any" cpumask can be opened with its cpumask and with threads it specifies. Change the implementation of evsel__open_per_cpu and evsel__open_per_thread to use evsel__open_per_cpu_and_thread to make the implementation of those functions clearer. Reviewed-by: Thomas Falcon Signed-off-by: Ian Rogers --- tools/perf/util/evsel.c | 23 +++++++++++++++++++---- tools/perf/util/evsel.h | 3 +++ 2 files changed, 22 insertions(+), 4 deletions(-) diff --git a/tools/perf/util/evsel.c b/tools/perf/util/evsel.c index af2b26c6456a..ae11df1e7902 100644 --- a/tools/perf/util/evsel.c +++ b/tools/perf/util/evsel.c @@ -2761,17 +2761,32 @@ void evsel__close(struct evsel *evsel) perf_evsel__free_id(&evsel->core); } =20 -int evsel__open_per_cpu(struct evsel *evsel, struct perf_cpu_map *cpus, in= t cpu_map_idx) +int evsel__open_per_cpu_and_thread(struct evsel *evsel, + struct perf_cpu_map *cpus, int cpu_map_idx, + struct perf_thread_map *threads) { if (cpu_map_idx =3D=3D -1) - return evsel__open_cpu(evsel, cpus, NULL, 0, perf_cpu_map__nr(cpus)); + return evsel__open_cpu(evsel, cpus, threads, 0, perf_cpu_map__nr(cpus)); =20 - return evsel__open_cpu(evsel, cpus, NULL, cpu_map_idx, cpu_map_idx + 1); + return evsel__open_cpu(evsel, cpus, threads, cpu_map_idx, cpu_map_idx + 1= ); +} + +int evsel__open_per_cpu(struct evsel *evsel, struct perf_cpu_map *cpus, in= t cpu_map_idx) +{ + struct perf_thread_map *threads =3D thread_map__new_by_tid(-1); + int ret =3D evsel__open_per_cpu_and_thread(evsel, cpus, cpu_map_idx, thre= ads); + + perf_thread_map__put(threads); + return ret; } =20 int evsel__open_per_thread(struct evsel *evsel, struct perf_thread_map *th= reads) { - return evsel__open(evsel, NULL, threads); + struct perf_cpu_map *cpus =3D perf_cpu_map__new_any_cpu(); + int ret =3D evsel__open_per_cpu_and_thread(evsel, cpus, -1, threads); + + perf_cpu_map__put(cpus); + return ret; } =20 static int perf_evsel__parse_id_sample(const struct evsel *evsel, diff --git a/tools/perf/util/evsel.h b/tools/perf/util/evsel.h index cefa8e64c0d5..8e79eb6d41b3 100644 --- a/tools/perf/util/evsel.h +++ b/tools/perf/util/evsel.h @@ -351,6 +351,9 @@ int evsel__enable(struct evsel *evsel); int evsel__disable(struct evsel *evsel); int evsel__disable_cpu(struct evsel *evsel, int cpu_map_idx); =20 +int evsel__open_per_cpu_and_thread(struct evsel *evsel, + struct perf_cpu_map *cpus, int cpu_map_idx, + struct perf_thread_map *threads); int evsel__open_per_cpu(struct evsel *evsel, struct perf_cpu_map *cpus, in= t cpu_map_idx); int evsel__open_per_thread(struct evsel *evsel, struct perf_thread_map *th= reads); int evsel__open(struct evsel *evsel, struct perf_cpu_map *cpus, --=20 2.50.0.727.gbf7dc18ff4-goog From nobody Mon Oct 6 19:09:19 2025 Received: from mail-oa1-f73.google.com (mail-oa1-f73.google.com [209.85.160.73]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 3EBA22343BE for ; Thu, 17 Jul 2025 21:03:02 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.160.73 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1752786184; cv=none; b=CmtyHlQXa94X0MziH0eL38h2Vn5FmUcPL7v9ZwQbRKS5XKPOB14FXlzc6HuKOOPzroUa12kmnysnr/95wK1KCmIvJaV84g2TSvKKqG/iceU8rJ9ZnNC2mFFwbf4+eIp/99dcdA/4K//x1S8MEY9Ik9HRe3o+Kx2WGoVx35Sw2gk= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1752786184; c=relaxed/simple; bh=ILKxxdhVKBL3H3T+k9tCVz/84PR5Y8ZVsZPuSrjtHFc=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Content-Type; b=hnEVQwijPi5CFejUGLpnxzcgfGck9gZXP8TIEHVp1WZLX+NkZGJ6/TleOAnsjSCpOcFMBULOpLeMR2yfWrC6hQJLMHvnn1EOtO0x/itLNokECKxLlUTRPSclGekzJXNUJyMPkRujTr6gY11ErKus3a61v/TGs9ngeHJjqh+axfE= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--irogers.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=POjGQDLr; arc=none smtp.client-ip=209.85.160.73 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--irogers.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="POjGQDLr" Received: by mail-oa1-f73.google.com with SMTP id 586e51a60fabf-2e90e7f170eso1324527fac.3 for ; Thu, 17 Jul 2025 14:03:02 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1752786181; x=1753390981; darn=vger.kernel.org; h=to:from:subject:message-id:references:mime-version:in-reply-to:date :from:to:cc:subject:date:message-id:reply-to; bh=HU89/GZC9oprC8rhKi3nOVfsuM3lRbUuTtm8KmujE+I=; b=POjGQDLrL0x4MJMu6yyzNob/C2dPMptPTib52VWDLh0C6lISoT9Tq4EogWhZu2tFWg NDinC10s7cGz8vb+NYZvC8rR1RxlgNShWbUjOVITyvxp2I6ePH0X0Bo+iIb8THKrH+V1 aSUKh6yetGiT6bTTf8CgwfEljG+LS0TeEQ8N9XHJ9dP+WQKZ7kMiqwklMAwsMpBzEHvn sKXcskbSVpBsX6cg4d5gz+19eecUrCDgknEblP1YIxP3t2Q/oGqJvF3XF2GLq4ek8riH Fo/GFrWn903+kXAj3NnTKSIcmX/iVwpAgE/jZ6Tfi1Gr4DKySkvGUhmyHG8uNIuoGnV2 7YTg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1752786181; x=1753390981; h=to:from:subject:message-id:references:mime-version:in-reply-to:date :x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=HU89/GZC9oprC8rhKi3nOVfsuM3lRbUuTtm8KmujE+I=; b=tL7/HvtSkb6p1iSOI9G6s8IhOJUWqY+K4DUWsf1T1HjspFcAA7dTMpRVeH0yE78VLj nAGqsKU6tuAb6xfr8NMtlIRgiW67bPlRiH9oHixmOQaZ5choYdhDlmdnAkYVwZKxB31Z tIsbPmyD45cZsxxsKH/qHi3VvM2WNkMQ8Ge+bfH7R3Ej4jE1s/qUs2W7YUCz6Orp/yC5 NYzLVvaeZQEWrIOUbbJQFBnPC23Lvuai+mNoRX/BVf8lAkxGDhkvt47XTWvCcLOLdbjx yBM0kc7lvCBMG9Qptg+7RxI7bCBDtAeNM2xoN6dU2kWPEnEJwFx5x6BaqkcwrX2aslCJ ZUoQ== X-Forwarded-Encrypted: i=1; AJvYcCXAZmG+qg5lH7DPH931c0uoJr1AjuKPD0Eli9r5aqqGERaT+deW50PYmDFLEtigTkazK6LpCNXKGdsf2/g=@vger.kernel.org X-Gm-Message-State: AOJu0YxePbSNRY68Dr2NcO8rrYHsnRUKBN/Vbl2oXWELlmBJ7Ayagcv8 RXgoZmCsCTEJ7O2GwWDh6WS7f0In6k0B2DJp8F+nAoKqQAa2QyPoM8sRzY2S4l5UmOsy0cjDcdP f93sSqZZCSA== X-Google-Smtp-Source: AGHT+IHigQdoOyZEy7c9tzzUz/1uR4HwaqALfA65QRdvTfANkkr3uJO1yLb6/cTnCmoAffWT+V9K2E+kwQyC X-Received: from oabrk3.prod.google.com ([2002:a05:6871:64c3:b0:2f7:9168:24fd]) (user=irogers job=prod-delivery.src-stubby-dispatcher) by 2002:a05:6870:d207:b0:2ea:7574:e243 with SMTP id 586e51a60fabf-2ffaf2bf7aemr6970652fac.9.1752786181359; Thu, 17 Jul 2025 14:03:01 -0700 (PDT) Date: Thu, 17 Jul 2025 14:02:33 -0700 In-Reply-To: <20250717210233.1143622-1-irogers@google.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20250717210233.1143622-1-irogers@google.com> X-Mailer: git-send-email 2.50.0.727.gbf7dc18ff4-goog Message-ID: <20250717210233.1143622-13-irogers@google.com> Subject: [PATCH v2 12/12] perf parse-events: Support user CPUs mixed with threads/processes From: Ian Rogers To: Thomas Falcon , Peter Zijlstra , Ingo Molnar , Arnaldo Carvalho de Melo , Namhyung Kim , Mark Rutland , Alexander Shishkin , Jiri Olsa , Ian Rogers , Adrian Hunter , Kan Liang , Ben Gainey , James Clark , Howard Chu , Weilin Wang , Levi Yun , "Dr. David Alan Gilbert" , Zhongqiu Han , Blake Jones , Yicong Yang , Anubhav Shelat , Thomas Richter , Jean-Philippe Romain , Song Liu , linux-perf-users@vger.kernel.org, linux-kernel@vger.kernel.org Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Counting events system-wide with a specified CPU prior to this change worked: ``` $ perf stat -e 'msr/tsc/,msr/tsc,cpu=3Dcpu_core/,msr/tsc,cpu=3Dcpu_atom/' -= a sleep 1 Performance counter stats for 'system wide': 59,393,419,099 msr/tsc/ 33,927,965,927 msr/tsc,cpu=3Dcpu_core/ 25,465,608,044 msr/tsc,cpu=3Dcpu_atom/ ``` However, when counting with process the counts became system wide: ``` $ perf stat -e 'msr/tsc/,msr/tsc,cpu=3Dcpu_core/,msr/tsc,cpu=3Dcpu_atom/' p= erf test -F 10 10.1: Basic parsing test : Ok 10.2: Parsing without PMU name : Ok 10.3: Parsing with PMU name : Ok Performance counter stats for 'perf test -F 10': 59,233,549 msr/tsc/ 59,227,556 msr/tsc,cpu=3Dcpu_core/ 59,224,053 msr/tsc,cpu=3Dcpu_atom/ ``` Make the handling of CPU maps with event parsing clearer. When an event is parsed creating an evsel the cpus should be either the PMU's cpumask or user specified CPUs. Update perf_evlist__propagate_maps so that it doesn't clobber the user specified CPUs. Try to make the behavior clearer, firstly fix up missing cpumasks. Next, perform sanity checks and adjustments from the global evlist CPU requests and for the PMU including simplifying to the "any CPU"(-1) value. Finally remove the event if the cpumask is empty. So that events are opened with a CPU and a thread change stat's create_perf_stat_counter to give both. With the change things are fixed: ``` $ perf stat --no-scale -e 'msr/tsc/,msr/tsc,cpu=3Dcpu_core/,msr/tsc,cpu=3Dc= pu_atom/' perf test -F 10 10.1: Basic parsing test : Ok 10.2: Parsing without PMU name : Ok 10.3: Parsing with PMU name : Ok Performance counter stats for 'perf test -F 10': 63,704,975 msr/tsc/ 47,060,704 msr/tsc,cpu=3Dcpu_core/ (4.6= 2%) 16,640,591 msr/tsc,cpu=3Dcpu_atom/ (2.1= 8%) ``` However, note the "--no-scale" option is used. This is necessary as the running time for the event on the counter isn't the same as the enabled time because the thread doesn't necessarily run on the CPUs specified for the counter. All counter values are scaled with: scaled_value =3D value * time_enabled / time_running and so without --no-scale the scaled_value becomes very large. This problem already exists on hybrid systems for the same reason. Here are 2 runs of the same code with an instructions event that counts the same on both types of core, there is no real multiplexing happening on the event: ``` $ perf stat -e instructions perf test -F 10 ... Performance counter stats for 'perf test -F 10': 87,896,447 cpu_atom/instructions/ (14.37= %) 98,171,964 cpu_core/instructions/ (85.63= %) ... $ perf stat --no-scale -e instructions perf test -F 10 ... Performance counter stats for 'perf test -F 10': 13,069,890 cpu_atom/instructions/ (19.32= %) 83,460,274 cpu_core/instructions/ (80.68= %) ... ``` The scaling has inflated per-PMU instruction counts and the overall count by 2x. To fix this the kernel needs changing when a task+CPU event (or just task event on hybrid) is scheduled out. A fix could be that the state isn't inactive but off for such events, so that time_enabled counts don't accumulate on them. Reviewed-by: Thomas Falcon Signed-off-by: Ian Rogers --- tools/lib/perf/evlist.c | 118 ++++++++++++++++++++++----------- tools/perf/util/parse-events.c | 10 ++- tools/perf/util/stat.c | 6 +- 3 files changed, 86 insertions(+), 48 deletions(-) diff --git a/tools/lib/perf/evlist.c b/tools/lib/perf/evlist.c index 9d9dec21f510..2d2236400220 100644 --- a/tools/lib/perf/evlist.c +++ b/tools/lib/perf/evlist.c @@ -36,49 +36,87 @@ void perf_evlist__init(struct perf_evlist *evlist) static void __perf_evlist__propagate_maps(struct perf_evlist *evlist, struct perf_evsel *evsel) { - if (evsel->system_wide) { - /* System wide: set the cpu map of the evsel to all online CPUs. */ - perf_cpu_map__put(evsel->cpus); - evsel->cpus =3D perf_cpu_map__new_online_cpus(); - } else if (evlist->has_user_cpus && evsel->is_pmu_core) { - /* - * User requested CPUs on a core PMU, ensure the requested CPUs - * are valid by intersecting with those of the PMU. - */ + if (perf_cpu_map__is_empty(evsel->cpus)) { + if (perf_cpu_map__is_empty(evsel->pmu_cpus)) { + /* + * Assume the unset PMU cpus were for a system-wide + * event, like a software or tracepoint. + */ + evsel->pmu_cpus =3D perf_cpu_map__new_online_cpus(); + } + if (evlist->has_user_cpus && !evsel->system_wide) { + /* + * Use the user CPUs unless the evsel is set to be + * system wide, such as the dummy event. + */ + evsel->cpus =3D perf_cpu_map__get(evlist->user_requested_cpus); + } else { + /* + * System wide and other modes, assume the cpu map + * should be set to all PMU CPUs. + */ + evsel->cpus =3D perf_cpu_map__get(evsel->pmu_cpus); + } + } + /* + * Avoid "any CPU"(-1) for uncore and PMUs that require a CPU, even if + * requested. + */ + if (evsel->requires_cpu && perf_cpu_map__has_any_cpu(evsel->cpus)) { perf_cpu_map__put(evsel->cpus); - evsel->cpus =3D perf_cpu_map__intersect(evlist->user_requested_cpus, evs= el->pmu_cpus); + evsel->cpus =3D perf_cpu_map__get(evsel->pmu_cpus); + } =20 - /* - * Empty cpu lists would eventually get opened as "any" so remove - * genuinely empty ones before they're opened in the wrong place. - */ - if (perf_cpu_map__is_empty(evsel->cpus)) { - struct perf_evsel *next =3D perf_evlist__next(evlist, evsel); - - perf_evlist__remove(evlist, evsel); - /* Keep idx contiguous */ - if (next) - list_for_each_entry_from(next, &evlist->entries, node) - next->idx--; + /* + * Globally requested CPUs replace user requested unless the evsel is + * set to be system wide. + */ + if (evlist->has_user_cpus && !evsel->system_wide) { + assert(!perf_cpu_map__has_any_cpu(evlist->user_requested_cpus)); + if (!perf_cpu_map__equal(evsel->cpus, evlist->user_requested_cpus)) { + perf_cpu_map__put(evsel->cpus); + evsel->cpus =3D perf_cpu_map__get(evlist->user_requested_cpus); } - } else if (!evsel->pmu_cpus || evlist->has_user_cpus || - (!evsel->requires_cpu && perf_cpu_map__has_any_cpu(evlist->user_requeste= d_cpus))) { - /* - * The PMU didn't specify a default cpu map, this isn't a core - * event and the user requested CPUs or the evlist user - * requested CPUs have the "any CPU" (aka dummy) CPU value. In - * which case use the user requested CPUs rather than the PMU - * ones. - */ + } + + /* Ensure cpus only references valid PMU CPUs. */ + if (!perf_cpu_map__has_any_cpu(evsel->cpus) && + !perf_cpu_map__is_subset(evsel->pmu_cpus, evsel->cpus)) { + struct perf_cpu_map *tmp =3D perf_cpu_map__intersect(evsel->pmu_cpus, ev= sel->cpus); + perf_cpu_map__put(evsel->cpus); - evsel->cpus =3D perf_cpu_map__get(evlist->user_requested_cpus); - } else if (evsel->cpus !=3D evsel->pmu_cpus) { - /* - * No user requested cpu map but the PMU cpu map doesn't match - * the evsel's. Reset it back to the PMU cpu map. - */ + evsel->cpus =3D tmp; + } + + /* + * Was event requested on all the PMU's CPUs but the user requested is + * any CPU (-1)? If so switch to using any CPU (-1) to reduce the number + * of events. + */ + if (!evsel->system_wide && + perf_cpu_map__equal(evsel->cpus, evsel->pmu_cpus) && + perf_cpu_map__has_any_cpu(evlist->user_requested_cpus)) { perf_cpu_map__put(evsel->cpus); - evsel->cpus =3D perf_cpu_map__get(evsel->pmu_cpus); + evsel->cpus =3D perf_cpu_map__get(evlist->user_requested_cpus); + } + + /* Sanity check assert before the evsel is potentially removed. */ + assert(!evsel->requires_cpu || !perf_cpu_map__has_any_cpu(evsel->cpus)); + + /* + * Empty cpu lists would eventually get opened as "any" so remove + * genuinely empty ones before they're opened in the wrong place. + */ + if (perf_cpu_map__is_empty(evsel->cpus)) { + struct perf_evsel *next =3D perf_evlist__next(evlist, evsel); + + perf_evlist__remove(evlist, evsel); + /* Keep idx contiguous */ + if (next) + list_for_each_entry_from(next, &evlist->entries, node) + next->idx--; + + return; } =20 if (evsel->system_wide) { @@ -98,6 +136,10 @@ static void perf_evlist__propagate_maps(struct perf_evl= ist *evlist) =20 evlist->needs_map_propagation =3D true; =20 + /* Clear the all_cpus set which will be merged into during propagation. */ + perf_cpu_map__put(evlist->all_cpus); + evlist->all_cpus =3D NULL; + list_for_each_entry_safe(evsel, n, &evlist->entries, node) __perf_evlist__propagate_maps(evlist, evsel); } diff --git a/tools/perf/util/parse-events.c b/tools/perf/util/parse-events.c index bd2d831d5123..fe2073c6b549 100644 --- a/tools/perf/util/parse-events.c +++ b/tools/perf/util/parse-events.c @@ -310,20 +310,18 @@ __add_event(struct list_head *list, int *idx, if (pmu) { is_pmu_core =3D pmu->is_core; pmu_cpus =3D perf_cpu_map__get(pmu->cpus); + if (perf_cpu_map__is_empty(pmu_cpus)) + pmu_cpus =3D cpu_map__online(); } else { is_pmu_core =3D (attr->type =3D=3D PERF_TYPE_HARDWARE || attr->type =3D=3D PERF_TYPE_HW_CACHE); pmu_cpus =3D is_pmu_core ? cpu_map__online() : NULL; } =20 - if (has_user_cpus) { + if (has_user_cpus) cpus =3D perf_cpu_map__get(user_cpus); - /* Existing behavior that pmu_cpus matches the given user ones. */ - perf_cpu_map__put(pmu_cpus); - pmu_cpus =3D perf_cpu_map__get(user_cpus); - } else { + else cpus =3D perf_cpu_map__get(pmu_cpus); - } =20 if (init_attr) event_attr_init(attr); diff --git a/tools/perf/util/stat.c b/tools/perf/util/stat.c index b0205e99a4c9..50b1a92d16df 100644 --- a/tools/perf/util/stat.c +++ b/tools/perf/util/stat.c @@ -769,8 +769,6 @@ int create_perf_stat_counter(struct evsel *evsel, attr->enable_on_exec =3D 1; } =20 - if (target__has_cpu(target) && !target__has_per_thread(target)) - return evsel__open_per_cpu(evsel, evsel__cpus(evsel), cpu_map_idx); - - return evsel__open_per_thread(evsel, evsel->core.threads); + return evsel__open_per_cpu_and_thread(evsel, evsel__cpus(evsel), cpu_map_= idx, + evsel->core.threads); } --=20 2.50.0.727.gbf7dc18ff4-goog