From nobody Mon Oct 6 17:06:46 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 F288621CA13 for ; Sat, 19 Jul 2025 03:05:38 +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=1752894340; cv=none; b=Yok/W7fu1gxfNkaDbI2nJHP7Lx3PbBs3SxWzG5kezVXuq6BjXlGkHgyP/nHwwy6yy1XR0ffQf+fXeEjQeD2wT9QJwk5VqZcUNRYdzcgahTgwdu8bEpTHrrX/SN/ALatYVllB+/F/Jh6RWApZwhsvGR63t6FonLwnAmlH247MEps= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1752894340; c=relaxed/simple; bh=3iqPS74Vm3U8TVu2LqxzGlONC7F3fEEx0C3HxO9oOX8=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Content-Type; b=kQX/LxOOyzu6qxQR8uehC6ux+CySsBuahIWTFm5ct+boPNs0fiRSt0y1Ppn5CCaDaLSAskRHAeP1jgXCo33xk0IIWqACqMi7tvbISN5gmPId7Bg+8iB98AlDPyV7SkOFi1Kz0y8djsrFp4oF4IG5/gYbRHl23sa41OKBMvdOUPY= 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=06DuwR5X; 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="06DuwR5X" Received: by mail-oa1-f74.google.com with SMTP id 586e51a60fabf-301ede1998aso851478fac.3 for ; Fri, 18 Jul 2025 20:05:38 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1752894338; x=1753499138; 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=06DuwR5XH1SBSbVSYiDmmqGlh1MJODmI/qpFmHuYV68fisCSLyCLHgwQ4g5w3dTxZ6 cBoBvTY7S+EhEcpwS2/memz69kCMNiaHfj4giRVRMt6GAl06EX+gZxnfeSPFUILNWfBw 2HXZV4lbRb/PhiKVsq+/TbNq7kGbFMqR+orxlGw6M/Ng9EZIniGfWbfE8K+gzJTOF3R0 Urt8fEYe+Zb3u9KHEaDQmSikcVUMxAFJOCBT9BKXQEejAjyMudyVXnpS+2kmY2KfsDFh JgLvsGIYfZZJNLoEZz4vgBmN894wRvyCN5xSSdgrePyOUUWcUiMOoqpPnvBch5eHqpkI 1QbA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1752894338; x=1753499138; 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=vnGDWl8tPLwyhwcwdNArMQIXQ79fkrXujTqIeqjIKzdqrviw1d7YBBajkmKNLsLyHm hemmYOQxX952scU4bhe1tSmElVQgVEudnqVHb8Ulbxmx26LcEROTeV40kvw3icpTZQRj puO9sDkd2B/Kkq1fB72yPSLU7CGgTaejnZVIkrgu1iaE/ND/kqxx60jBfiPys4oPPd3O ZjbwYtqGBzSs62sgrZXQS1yIh/8/8TJdnMnjxAxxNUVjfjZGn7aHR7xzStzpHKqCrEIk 6rGAeIMvlT5TI4SV1ZG47g2YU2lJNkd9YG6f8SCGn6M9G4SiFiCb/x5qs4K1aGQ3jp66 cffw== X-Forwarded-Encrypted: i=1; AJvYcCUt3om1xvSft5mIf8muZJuu9b1PVes4X35i/AIRt6BdMhR3pTyyQYWHctqUKhcIRKjCW/jqXh9zMqygI4c=@vger.kernel.org X-Gm-Message-State: AOJu0YzKWT81R8vTWEAPeORQ2SqY0O7f1G9oEQTSmJLAkkCz9cOZIBxn TGooCeO7jZbhcRjW9TrFffgc3JvJoGWPPnUCY/KNRzNpoo0tM6YXgYoLGGk8+wZQkxZ2dNTdfVb hVczEGrlo8A== X-Google-Smtp-Source: AGHT+IHtAUqaMjqyTAyw5V9bMWgfhc/EuGmlDpdtDKfXE6b2gJq6gjfWRWQouDvDJvNd/3krYdZnxhu9NN3z X-Received: from oabsa2.prod.google.com ([2002:a05:6871:2102:b0:2bc:65b9:2ace]) (user=irogers job=prod-delivery.src-stubby-dispatcher) by 2002:a05:6871:7244:b0:2ff:9224:b1c8 with SMTP id 586e51a60fabf-2ffaf5a4bc4mr10499853fac.36.1752894338080; Fri, 18 Jul 2025 20:05:38 -0700 (PDT) Date: Fri, 18 Jul 2025 20:05:06 -0700 In-Reply-To: <20250719030517.1990983-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: <20250719030517.1990983-1-irogers@google.com> X-Mailer: git-send-email 2.50.0.727.gbf7dc18ff4-goog Message-ID: <20250719030517.1990983-5-irogers@google.com> Subject: [PATCH v3 04/15] 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 , "Liang, Kan" , Ravi Bangoria , James Clark , Dapeng Mi , Weilin Wang , Andi Kleen , linux-kernel@vger.kernel.org, linux-perf-users@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