From nobody Wed Dec 17 07:26:55 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 518F11C6FE6 for ; Wed, 26 Mar 2025 23:51:20 +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=1743033082; cv=none; b=ioeNp74hMW+AV08g67LqaWLHcPtT+DsxnVhBDyAPYPQznfAyFIejGrvMR0Gsn0O/cGnbiBuCyTZ5ZiDL6xXv6B81nA1UmgvXbNXBgmwRsEWIZZ6uf+7xhrvYX5Mq44VQqy9PPnjpwo7uzwtb4/QLKKdmRPclBWhaNc6YiAQU1p0= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1743033082; c=relaxed/simple; bh=t2tWu2D0c3sfj5y4zl1G5sLxdKr+9guWjcJcmYZfRBU=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=YpHJDjNlkYcd7QjiUt32q6Yn4h+RoMfRURAe5snGIc49Lg1G6dCY2pGXA2Koh+LT0UotZCtsvEsqX76FErTV37lbcCOopnvwf9+gcqtPcmGAQ0ZmocIMsUIrlAQpViavKNmaERdC3+75HjZW+XSduLRxHL2FiNbaQ6rFerYIkso= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--ctshao.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=NXe6juLx; 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--ctshao.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="NXe6juLx" Received: by mail-pj1-f73.google.com with SMTP id 98e67ed59e1d1-2ff78dd28ecso688706a91.1 for ; Wed, 26 Mar 2025 16:51:20 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1743033079; x=1743637879; darn=vger.kernel.org; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=q30bX9+I8GcSOpo9Y0hBVw3mvYTN2XWDyBYM467aSpE=; b=NXe6juLx8s4LMC0UCrDsgxmj2LsJSqnn26ANdTEJMc6vItgjRy8/tw24C9yaZvY1Ac hLDRu2N2xJaKT9qx7gzn/5LRxtV/E6zC3e5EtnyxhY4TOnFkyS0FySVPBKWOdr2FpVy8 NW/urTKXYMM0xO5VRo2OT2rt2AcflJIwQ/RmcLZ+Z3Q0qt/SOm4SqQkaiX9RZY8GK7eP WrWs/2EB5qTXIszHYplrpxvNZhDdrSNqna9rJ9NzQYq52nwJdwtnCUQoAGKxTWGwVURJ 43cxyYmWOmBts8/+fI0T5NhKHPnccJBki5vQfecJU4o+h16mxObSFenuq5zENmk/oSgz yWcA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1743033079; x=1743637879; h=cc: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=q30bX9+I8GcSOpo9Y0hBVw3mvYTN2XWDyBYM467aSpE=; b=nTC5IK8zEkADLULFkx+SBeJod/MrOXZErW4LWXbGSu9CCd7oiDh9K1doQvTzD+hL7f CfaxAYFl36IN/vVdBuHUo7vrxLEg5IYGPTN2D5N1P5/MpXxpVMYF65j0/qeSf3OkuSRh aBoBZClzgX37ptvrPdxaGdOU+8A3Ap2pzWFto/GIkjn7QSjoj6oWYEyVnUWhnGh0HoTo FDRlRJE5rTvn6p2WC1WfUvZJnYj9C+JRyzEIcFAsnG4Spq5uZlwntIq9J4RFecuv+htP uA9+tu8YimccpoRKW+x9qWnVpOXKij/hKt4w80FHeXM4yGv7v+NanDAsOiZhHMjO0f5B pR6w== X-Gm-Message-State: AOJu0YycxSfnJ4vdeG4FFAe1rjNIr6dIPhC992P5IaLEgTPsi9gJuP6d vBJP+Uy0WB4ZwR6nYUlKedVfmLB/hYqakQVeaTmFpWaXZZPWSz87LQKbUzRUQFVFPJ78Rdcq6DL egKEvzsdMUlPdAq6igwxsRN+tE4hPQOSw7uADGbvkMt2ot9dGRNDm1mSv+doi6yGeBXrzk1/kz2 ds61+SrfDPbo8S0IKw+Izz0GMHS9I1buPbuYBLml+B X-Google-Smtp-Source: AGHT+IGfyPL45x0WkucqFEcKaMvnBC0INnO/xWDuB/tJf1TPWrD/Zzf3B+xVfs4YQ4rwfKtCPqDwABIiBE0= X-Received: from pjbnw9.prod.google.com ([2002:a17:90b:2549:b0:301:1bf5:2f07]) (user=ctshao job=prod-delivery.src-stubby-dispatcher) by 2002:a17:90b:2c84:b0:2ee:b2e6:4276 with SMTP id 98e67ed59e1d1-303a9171a7dmr2246014a91.27.1743033079593; Wed, 26 Mar 2025 16:51:19 -0700 (PDT) Date: Wed, 26 Mar 2025 16:42:35 -0700 In-Reply-To: <20250326234758.480431-1-ctshao@google.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20250326234758.480431-1-ctshao@google.com> X-Mailer: git-send-email 2.49.0.472.ge94155a9ec-goog Message-ID: <20250326234758.480431-2-ctshao@google.com> Subject: [PATCH v1 1/2] perf evlist: Make uniquifying counter names consistent From: Chun-Tse Shao To: linux-kernel@vger.kernel.org Cc: Ian Rogers , Chun-Tse Shao , peterz@infradead.org, mingo@redhat.com, acme@kernel.org, namhyung@kernel.org, mark.rutland@arm.com, alexander.shishkin@linux.intel.com, jolsa@kernel.org, adrian.hunter@intel.com, kan.liang@linux.intel.com, ak@linux.intel.com, howardchu95@gmail.com, asmadeus@codewreck.org, yeoreum.yun@arm.com, linux@treblig.org, james.clark@linaro.org, weilin.wang@intel.com, linux-perf-users@vger.kernel.org Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" From: Ian Rogers From: Ian Rogers perf stat has different uniquification logic to perf record and perf top. In the case of perf record and perf top all hybrid event names are uniquified. Perf stat is more disciplined respecting name config terms, libpfm4 events, etc. Perf stat will uniquify hybrid events and the non-core PMU cases shouldn't apply to perf record or perf top. For consistency, remove the uniquification for perf record and perf top and reuse the perf stat uniquification, making the code more globally visible for this. Fix the detection of cross-PMU for disabling uniquify by correctly setting last_pmu. When setting uniquify on an evsel, make sure the PMUs between the 2 considered events differ otherwise the uniquify isn't adding value. Signed-off-by: Ian Rogers Tested-by: Chun-Tse Shao --- tools/perf/builtin-record.c | 7 +- tools/perf/builtin-top.c | 7 +- tools/perf/util/evlist.c | 66 ++++++++++----- tools/perf/util/evlist.h | 3 +- tools/perf/util/evsel.c | 113 +++++++++++++++++++++++++ tools/perf/util/evsel.h | 4 + tools/perf/util/stat-display.c | 149 +-------------------------------- 7 files changed, 176 insertions(+), 173 deletions(-) diff --git a/tools/perf/builtin-record.c b/tools/perf/builtin-record.c index ba20bf7c011d..83f8f9728e12 100644 --- a/tools/perf/builtin-record.c +++ b/tools/perf/builtin-record.c @@ -26,6 +26,7 @@ #include "util/target.h" #include "util/session.h" #include "util/tool.h" +#include "util/stat.h" #include "util/symbol.h" #include "util/record.h" #include "util/cpumap.h" @@ -2483,7 +2484,11 @@ static int __cmd_record(struct record *rec, int argc= , const char **argv) pr_warning("WARNING: --timestamp-filename option is not available in pip= e mode.\n"); } =20 - evlist__uniquify_name(rec->evlist); + /* + * Use global stat_config that is zero meaning aggr_mode is AGGR_NONE + * and hybrid_merge is false. + */ + evlist__uniquify_evsel_names(rec->evlist, &stat_config); =20 evlist__config(rec->evlist, opts, &callchain_param); =20 diff --git a/tools/perf/builtin-top.c b/tools/perf/builtin-top.c index 1061f4eebc3f..b7c8ecafa2f2 100644 --- a/tools/perf/builtin-top.c +++ b/tools/perf/builtin-top.c @@ -35,6 +35,7 @@ #include "util/mmap.h" #include "util/session.h" #include "util/thread.h" +#include "util/stat.h" #include "util/symbol.h" #include "util/synthetic-events.h" #include "util/top.h" @@ -1309,7 +1310,11 @@ static int __cmd_top(struct perf_top *top) } } =20 - evlist__uniquify_name(top->evlist); + /* + * Use global stat_config that is zero meaning aggr_mode is AGGR_NONE + * and hybrid_merge is false. + */ + evlist__uniquify_evsel_names(top->evlist, &stat_config); ret =3D perf_top__start_counters(top); if (ret) return ret; diff --git a/tools/perf/util/evlist.c b/tools/perf/util/evlist.c index c1a04141aed0..a28ab52e7d85 100644 --- a/tools/perf/util/evlist.c +++ b/tools/perf/util/evlist.c @@ -2552,34 +2552,56 @@ void evlist__warn_user_requested_cpus(struct evlist= *evlist, const char *cpu_lis perf_cpu_map__put(user_requested_cpus); } =20 -void evlist__uniquify_name(struct evlist *evlist) +/* Should uniquify be disabled for the evlist? */ +static bool evlist__disable_uniquify(const struct evlist *evlist) { - char *new_name, empty_attributes[2] =3D ":", *attributes; - struct evsel *pos; + struct evsel *counter; + struct perf_pmu *last_pmu =3D NULL; + bool first =3D true; =20 - if (perf_pmus__num_core_pmus() =3D=3D 1) - return; + evlist__for_each_entry(evlist, counter) { + /* If PMUs vary then uniquify can be useful. */ + if (!first && counter->pmu !=3D last_pmu) + return false; + first =3D false; + if (counter->pmu) { + /* Allow uniquify for uncore PMUs. */ + if (!counter->pmu->is_core) + return false; + /* Keep hybrid event names uniquified for clarity. */ + if (perf_pmus__num_core_pmus() > 1) + return false; + } + last_pmu =3D counter->pmu; + } + return true; +} =20 - evlist__for_each_entry(evlist, pos) { - if (!evsel__is_hybrid(pos)) - continue; +static bool evlist__set_needs_uniquify(struct evlist *evlist, const struct= perf_stat_config *config) +{ + struct evsel *counter; + bool needs_uniquify =3D false; =20 - if (strchr(pos->name, '/')) - continue; + if (evlist__disable_uniquify(evlist)) { + evlist__for_each_entry(evlist, counter) + counter->uniquified_name =3D true; + return false; + } + + evlist__for_each_entry(evlist, counter) { + if (evsel__set_needs_uniquify(counter, config)) + needs_uniquify =3D true; + } + return needs_uniquify; +} =20 - attributes =3D strchr(pos->name, ':'); - if (attributes) - *attributes =3D '\0'; - else - attributes =3D empty_attributes; +void evlist__uniquify_evsel_names(struct evlist *evlist, const struct perf= _stat_config *config) +{ + if (evlist__set_needs_uniquify(evlist, config)) { + struct evsel *pos; =20 - if (asprintf(&new_name, "%s/%s/%s", pos->pmu ? pos->pmu->name : "", - pos->name, attributes + 1)) { - free(pos->name); - pos->name =3D new_name; - } else { - *attributes =3D ':'; - } + evlist__for_each_entry(evlist, pos) + evsel__uniquify_counter(pos); } } =20 diff --git a/tools/perf/util/evlist.h b/tools/perf/util/evlist.h index edcbf1c10e92..7cfdaf543214 100644 --- a/tools/perf/util/evlist.h +++ b/tools/perf/util/evlist.h @@ -19,6 +19,7 @@ struct pollfd; struct thread_map; struct perf_cpu_map; +struct perf_stat_config; struct record_opts; struct target; =20 @@ -433,7 +434,7 @@ struct evsel *evlist__find_evsel(struct evlist *evlist,= int idx); int evlist__scnprintf_evsels(struct evlist *evlist, size_t size, char *bf); void evlist__check_mem_load_aux(struct evlist *evlist); void evlist__warn_user_requested_cpus(struct evlist *evlist, const char *c= pu_list); -void evlist__uniquify_name(struct evlist *evlist); +void evlist__uniquify_evsel_names(struct evlist *evlist, const struct perf= _stat_config *config); bool evlist__has_bpf_output(struct evlist *evlist); bool evlist__needs_bpf_sb_event(struct evlist *evlist); =20 diff --git a/tools/perf/util/evsel.c b/tools/perf/util/evsel.c index 1974395492d7..f00cfff119aa 100644 --- a/tools/perf/util/evsel.c +++ b/tools/perf/util/evsel.c @@ -3939,3 +3939,116 @@ void evsel__remove_from_group(struct evsel *evsel, = struct evsel *leader) leader->core.nr_members--; } } + +bool evsel__set_needs_uniquify(struct evsel *counter, const struct perf_st= at_config *config) +{ + struct evsel *evsel; + + if (counter->needs_uniquify) { + /* Already set. */ + return true; + } + + if (counter->merged_stat) { + /* Counter won't be shown. */ + return false; + } + + if (counter->use_config_name || counter->is_libpfm_event) { + /* Original name will be used. */ + return false; + } + + if (!config->hybrid_merge && evsel__is_hybrid(counter)) { + /* Unique hybrid counters necessary. */ + counter->needs_uniquify =3D true; + return true; + } + + if (counter->core.attr.type < PERF_TYPE_MAX && counter->core.attr.type != =3D PERF_TYPE_RAW) { + /* Legacy event, don't uniquify. */ + return false; + } + + if (counter->pmu && counter->pmu->is_core && + counter->alternate_hw_config !=3D PERF_COUNT_HW_MAX) { + /* A sysfs or json event replacing a legacy event, don't uniquify. */ + return false; + } + + if (config->aggr_mode =3D=3D AGGR_NONE) { + /* Always unique with no aggregation. */ + counter->needs_uniquify =3D true; + return true; + } + + /* + * Do other non-merged events in the evlist have the same name? If so + * uniquify is necessary. + */ + evlist__for_each_entry(counter->evlist, evsel) { + if (evsel =3D=3D counter || evsel->merged_stat || evsel->pmu =3D=3D coun= ter->pmu) + continue; + + if (evsel__name_is(counter, evsel__name(evsel))) { + counter->needs_uniquify =3D true; + return true; + } + } + return false; +} + +void evsel__uniquify_counter(struct evsel *counter) +{ + const char *name, *pmu_name; + char *new_name, *config; + int ret; + + /* No uniquification necessary. */ + if (!counter->needs_uniquify) + return; + + /* The evsel was already uniquified. */ + if (counter->uniquified_name) + return; + + /* Avoid checking to uniquify twice. */ + counter->uniquified_name =3D true; + + name =3D evsel__name(counter); + pmu_name =3D counter->pmu->name; + /* Already prefixed by the PMU name. */ + if (!strncmp(name, pmu_name, strlen(pmu_name))) + return; + + config =3D strchr(name, '/'); + if (config) { + int len =3D config - name; + + if (config[1] =3D=3D '/') { + /* case: event// */ + ret =3D asprintf(&new_name, "%s/%.*s/%s", pmu_name, len, name, config += 2); + } else { + /* case: event/.../ */ + ret =3D asprintf(&new_name, "%s/%.*s,%s", pmu_name, len, name, config += 1); + } + } else { + config =3D strchr(name, ':'); + if (config) { + /* case: event:.. */ + int len =3D config - name; + + ret =3D asprintf(&new_name, "%s/%.*s/%s", pmu_name, len, name, config += 1); + } else { + /* case: event */ + ret =3D asprintf(&new_name, "%s/%s/", pmu_name, name); + } + } + if (ret > 0) { + free(counter->name); + counter->name =3D new_name; + } else { + /* ENOMEM from asprintf. */ + counter->uniquified_name =3D false; + } +} diff --git a/tools/perf/util/evsel.h b/tools/perf/util/evsel.h index aae431d63d64..76ccb7c7e8c2 100644 --- a/tools/perf/util/evsel.h +++ b/tools/perf/util/evsel.h @@ -16,6 +16,7 @@ struct bpf_object; struct cgroup; struct perf_counts; +struct perf_stat_config; struct perf_stat_evsel; union perf_event; struct bpf_counter_ops; @@ -542,6 +543,9 @@ void evsel__remove_from_group(struct evsel *evsel, stru= ct evsel *leader); =20 bool arch_evsel__must_be_in_group(const struct evsel *evsel); =20 +bool evsel__set_needs_uniquify(struct evsel *counter, const struct perf_st= at_config *config); +void evsel__uniquify_counter(struct evsel *counter); + /* * Macro to swap the bit-field postition and size. * Used when, diff --git a/tools/perf/util/stat-display.c b/tools/perf/util/stat-display.c index e852ac0d9847..d427e0ca98a1 100644 --- a/tools/perf/util/stat-display.c +++ b/tools/perf/util/stat-display.c @@ -929,61 +929,6 @@ static void printout(struct perf_stat_config *config, = struct outstate *os, } } =20 -static void evsel__uniquify_counter(struct evsel *counter) -{ - const char *name, *pmu_name; - char *new_name, *config; - int ret; - - /* No uniquification necessary. */ - if (!counter->needs_uniquify) - return; - - /* The evsel was already uniquified. */ - if (counter->uniquified_name) - return; - - /* Avoid checking to uniquify twice. */ - counter->uniquified_name =3D true; - - name =3D evsel__name(counter); - pmu_name =3D counter->pmu->name; - /* Already prefixed by the PMU name. */ - if (!strncmp(name, pmu_name, strlen(pmu_name))) - return; - - config =3D strchr(name, '/'); - if (config) { - int len =3D config - name; - - if (config[1] =3D=3D '/') { - /* case: event// */ - ret =3D asprintf(&new_name, "%s/%.*s/%s", pmu_name, len, name, config += 2); - } else { - /* case: event/.../ */ - ret =3D asprintf(&new_name, "%s/%.*s,%s", pmu_name, len, name, config += 1); - } - } else { - config =3D strchr(name, ':'); - if (config) { - /* case: event:.. */ - int len =3D config - name; - - ret =3D asprintf(&new_name, "%s/%.*s/%s", pmu_name, len, name, config += 1); - } else { - /* case: event */ - ret =3D asprintf(&new_name, "%s/%s/", pmu_name, name); - } - } - if (ret > 0) { - free(counter->name); - counter->name =3D new_name; - } else { - /* ENOMEM from asprintf. */ - counter->uniquified_name =3D false; - } -} - /** * should_skip_zero_count() - Check if the event should print 0 values. * @config: The perf stat configuration (including aggregation mode). @@ -1069,8 +1014,6 @@ static void print_counter_aggrdata(struct perf_stat_c= onfig *config, if (counter->merged_stat) return; =20 - evsel__uniquify_counter(counter); - val =3D aggr->counts.val; ena =3D aggr->counts.ena; run =3D aggr->counts.run; @@ -1650,96 +1593,6 @@ static void print_cgroup_counter(struct perf_stat_co= nfig *config, struct evlist print_metric_end(config, os); } =20 -/* Should uniquify be disabled for the evlist? */ -static bool evlist__disable_uniquify(const struct evlist *evlist) -{ - struct evsel *counter; - struct perf_pmu *last_pmu =3D NULL; - bool first =3D true; - - evlist__for_each_entry(evlist, counter) { - /* If PMUs vary then uniquify can be useful. */ - if (!first && counter->pmu !=3D last_pmu) - return false; - first =3D false; - if (counter->pmu) { - /* Allow uniquify for uncore PMUs. */ - if (!counter->pmu->is_core) - return false; - /* Keep hybrid event names uniquified for clarity. */ - if (perf_pmus__num_core_pmus() > 1) - return false; - } - } - return true; -} - -static void evsel__set_needs_uniquify(struct evsel *counter, const struct = perf_stat_config *config) -{ - struct evsel *evsel; - - if (counter->merged_stat) { - /* Counter won't be shown. */ - return; - } - - if (counter->use_config_name || counter->is_libpfm_event) { - /* Original name will be used. */ - return; - } - - if (!config->hybrid_merge && evsel__is_hybrid(counter)) { - /* Unique hybrid counters necessary. */ - counter->needs_uniquify =3D true; - return; - } - - if (counter->core.attr.type < PERF_TYPE_MAX && counter->core.attr.type != =3D PERF_TYPE_RAW) { - /* Legacy event, don't uniquify. */ - return; - } - - if (counter->pmu && counter->pmu->is_core && - counter->alternate_hw_config !=3D PERF_COUNT_HW_MAX) { - /* A sysfs or json event replacing a legacy event, don't uniquify. */ - return; - } - - if (config->aggr_mode =3D=3D AGGR_NONE) { - /* Always unique with no aggregation. */ - counter->needs_uniquify =3D true; - return; - } - - /* - * Do other non-merged events in the evlist have the same name? If so - * uniquify is necessary. - */ - evlist__for_each_entry(counter->evlist, evsel) { - if (evsel =3D=3D counter || evsel->merged_stat) - continue; - - if (evsel__name_is(counter, evsel__name(evsel))) { - counter->needs_uniquify =3D true; - return; - } - } -} - -static void evlist__set_needs_uniquify(struct evlist *evlist, const struct= perf_stat_config *config) -{ - struct evsel *counter; - - if (evlist__disable_uniquify(evlist)) { - evlist__for_each_entry(evlist, counter) - counter->uniquified_name =3D true; - return; - } - - evlist__for_each_entry(evlist, counter) - evsel__set_needs_uniquify(counter, config); -} - void evlist__print_counters(struct evlist *evlist, struct perf_stat_config= *config, struct target *_target, struct timespec *ts, int argc, const char **argv) @@ -1751,7 +1604,7 @@ void evlist__print_counters(struct evlist *evlist, st= ruct perf_stat_config *conf .first =3D true, }; =20 - evlist__set_needs_uniquify(evlist, config); + evlist__uniquify_evsel_names(evlist, config); =20 if (config->iostat_run) evlist->selected =3D evlist__first(evlist); --=20 2.49.0.472.ge94155a9ec-goog From nobody Wed Dec 17 07:26:55 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 1620518027 for ; Wed, 26 Mar 2025 23:52:42 +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=1743033164; cv=none; b=LZAhc8oe1jZitlVlywbiaYPxNdf1kJwcMpZDj8tXEQTNlCz3deHRyM6yw3L7cVpMN4dzD+TM0M1iO4QkGjUnRNjTWoDTiE6Wl/lUSjceJWHTjIYpwOhvyI/wKJMKCSFzwiIpuLUeBCfjXgYRXRAWPF/i6jsLFUCNzJotQTe1Egc= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1743033164; c=relaxed/simple; bh=xVp2QMfLt7PK7AmAqU2aS2o/Vv+VexUF9/S5FnYX/LM=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=Ss7Q+DQZcdk5/AMOw1CrvqbQCcdp2w6HVe5hRx2ho/Paw+4sZPo4u1ByAeIx8EJrxRzSvkvBOdOeyRy6inShXUE/MSu4G6V2Oy2hyws0xiOf6xDNs7oyZnjakKOkgLXKROGNj6Bn7ti9wlgr332WwBv482Abnfshh3/cLVpJqxE= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--ctshao.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=oUpqypyi; 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--ctshao.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="oUpqypyi" Received: by mail-pl1-f202.google.com with SMTP id d9443c01a7336-2254bdd4982so9161075ad.1 for ; Wed, 26 Mar 2025 16:52:42 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1743033162; x=1743637962; darn=vger.kernel.org; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=x90XvYEzFqcvfnyhuh/NjS2NoQXwJjkpKyy6gPyPFcE=; b=oUpqypyiplWGtGdCn03TTocyRV2GD3pVROePObteUVslQXF8t/GdWo33SwQWBVN30A d1bm2HwAHRIB+ADFMyQ/r6k5w9F6fiUKmwa+GO8wpspEnN2m1eIbxyGzDJXGdHV9XpKx ea5ftE6P2eiDgB0O18VVIdMYLXv4WVwvJgGRrAMbCF0Fvk5k+Kf8kgc3B3j7tdkZHi8I xLB+DCJG2brd3mVXfEj/PYcPEMOmS8Ecr3bcLoozVc40Dyt3YVU5Gom++7VQaADe8nTc 6nkRTZPQ6xpY7V+CiDYANMu3GWzKZjlDAb/EyBOeGLUzHUFROp41YJznsEDM7g68zDC/ NWjA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1743033162; x=1743637962; h=cc: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=x90XvYEzFqcvfnyhuh/NjS2NoQXwJjkpKyy6gPyPFcE=; b=Gakqa+IWUw/HbphxlUtNcWCQ2oDF8AG+yWnk2d225qUQ7pckRMG++lV9XzdFwwKgum dCHj56SVg+EAMWKxsqAt2pSnsNmFI5TNtZ3OvSL8EXqSStsBSwqLnbn2J/eigi4VmEb+ 6PtxOa0cfOudlDjHm5mC1XcVutrUYczhuN2zdkrOTwv6JTJmOjkdANbWs88l0l1tpyJ8 h/N5Z66OmM2kmW/daU3B1ZMgWxKbDbumOqkYYdIhrBiiYEFyrcdLYneBN0j8vSuWFJeY tJHxGZjMxDMtnqxdRFZCk1CFZit9x8j7/blOrZTR/UFQ2EiBAspDfgW7CzBau4W9PdqX lETg== X-Gm-Message-State: AOJu0Yyn4kvctM/LJnJi8x6emMREqzvL1HazKRu6goLnikxCY6iyC3ne 0zsNm8vqpK6YIncIEEwUmvqiCrDbPyeNH05BsgfpSrihDYxGHkA1pveu4AXozCEmhIhj7ttRdbl PeOyH6xb4ZT7M14EM6aqQb9E9rPhCSD7jarI+TavRfPfxvmVZqQjBZh+tCCnHtwTTxV8G/JwmoS 75H4GNdLbdz5N52X1s9G6zuqRdyLQStWxYcwfJp5De X-Google-Smtp-Source: AGHT+IHDXEo6LkpH675QoZfsVFOYEDRrNppU08Rfa+cGSrzioV6wNeqQmVyyBL+VZB8bDIo624XJIuSnL5Q= X-Received: from plqt12.prod.google.com ([2002:a17:902:a5cc:b0:21f:14cc:68b0]) (user=ctshao job=prod-delivery.src-stubby-dispatcher) by 2002:a17:902:f548:b0:223:6455:8752 with SMTP id d9443c01a7336-2280498703dmr18329665ad.43.1743033162148; Wed, 26 Mar 2025 16:52:42 -0700 (PDT) Date: Wed, 26 Mar 2025 16:42:36 -0700 In-Reply-To: <20250326234758.480431-1-ctshao@google.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20250326234758.480431-1-ctshao@google.com> X-Mailer: git-send-email 2.49.0.472.ge94155a9ec-goog Message-ID: <20250326234758.480431-3-ctshao@google.com> Subject: [PATCH v1 2/2] perf parse-events: Use wildcard processing to set an event to merge into From: Chun-Tse Shao To: linux-kernel@vger.kernel.org Cc: Ian Rogers , Chun-Tse Shao , peterz@infradead.org, mingo@redhat.com, acme@kernel.org, namhyung@kernel.org, mark.rutland@arm.com, alexander.shishkin@linux.intel.com, jolsa@kernel.org, adrian.hunter@intel.com, kan.liang@linux.intel.com, james.clark@linaro.org, howardchu95@gmail.com, weilin.wang@intel.com, yeoreum.yun@arm.com, linux@treblig.org, ak@linux.intel.com, asmadeus@codewreck.org, linux-perf-users@vger.kernel.org Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" From: Ian Rogers From: Ian Rogers The merge stat code fails for uncore events if they are repeated twice, for example `perf stat -e clockticks,clockticks -I 1000` as the counts of the second set of uncore events will be merged into the first counter. Reimplement the logic to have a first_wildcard_match so that merged later events correctly merge into the first wildcard event that they will be aggregated into. Signed-off-by: Ian Rogers Tested-by: Chun-Tse Shao --- tools/perf/util/evsel.c | 15 ++++++------ tools/perf/util/evsel.h | 7 ++++-- tools/perf/util/parse-events.c | 45 ++++++++++++++++++++-------------- tools/perf/util/stat-display.c | 2 +- tools/perf/util/stat.c | 40 +++--------------------------- 5 files changed, 44 insertions(+), 65 deletions(-) diff --git a/tools/perf/util/evsel.c b/tools/perf/util/evsel.c index f00cfff119aa..84e56691c49f 100644 --- a/tools/perf/util/evsel.c +++ b/tools/perf/util/evsel.c @@ -552,11 +552,11 @@ struct evsel *evsel__clone(struct evsel *dest, struct= evsel *orig) =20 evsel->exclude_GH =3D orig->exclude_GH; evsel->sample_read =3D orig->sample_read; - evsel->auto_merge_stats =3D orig->auto_merge_stats; evsel->collect_stat =3D orig->collect_stat; evsel->weak_group =3D orig->weak_group; evsel->use_config_name =3D orig->use_config_name; evsel->pmu =3D orig->pmu; + evsel->first_wildcard_match =3D orig->first_wildcard_match; =20 if (evsel__copy_config_terms(evsel, orig) < 0) goto out_err; @@ -3949,11 +3949,6 @@ bool evsel__set_needs_uniquify(struct evsel *counter= , const struct perf_stat_con return true; } =20 - if (counter->merged_stat) { - /* Counter won't be shown. */ - return false; - } - if (counter->use_config_name || counter->is_libpfm_event) { /* Original name will be used. */ return false; @@ -3980,6 +3975,12 @@ bool evsel__set_needs_uniquify(struct evsel *counter= , const struct perf_stat_con /* Always unique with no aggregation. */ counter->needs_uniquify =3D true; return true; + } else if (counter->first_wildcard_match !=3D NULL) { + /* + * It is under aggregation mode and the counter won't be shown (the coun= ts should be + * aggreated to the `first_wildcard_match` evsel) + */ + return false; } =20 /* @@ -3987,7 +3988,7 @@ bool evsel__set_needs_uniquify(struct evsel *counter,= const struct perf_stat_con * uniquify is necessary. */ evlist__for_each_entry(counter->evlist, evsel) { - if (evsel =3D=3D counter || evsel->merged_stat || evsel->pmu =3D=3D coun= ter->pmu) + if (evsel =3D=3D counter || evsel->first_wildcard_match || evsel->pmu = =3D=3D counter->pmu) continue; =20 if (evsel__name_is(counter, evsel__name(evsel))) { diff --git a/tools/perf/util/evsel.h b/tools/perf/util/evsel.h index 76ccb7c7e8c2..2cc046894e30 100644 --- a/tools/perf/util/evsel.h +++ b/tools/perf/util/evsel.h @@ -70,6 +70,11 @@ struct evsel { const char *unit; struct cgroup *cgrp; const char *metric_id; + /* + * This point to the first evsel with the same name, intended to store t= he + * aggregated counts in aggregation mode. + */ + struct evsel *first_wildcard_match; /* parse modifier helper */ int exclude_GH; int sample_read; @@ -78,7 +83,6 @@ struct evsel { bool percore; bool precise_max; bool is_libpfm_event; - bool auto_merge_stats; bool collect_stat; bool weak_group; bool bpf_counter; @@ -115,7 +119,6 @@ struct evsel { bool ignore_missing_thread; bool forced_leader; bool cmdline_group_boundary; - bool merged_stat; bool reset_group; bool errored; bool needs_auxtrace_mmap; diff --git a/tools/perf/util/parse-events.c b/tools/perf/util/parse-events.c index 5152fd5a6ead..0985fea17a26 100644 --- a/tools/perf/util/parse-events.c +++ b/tools/perf/util/parse-events.c @@ -228,7 +228,7 @@ __add_event(struct list_head *list, int *idx, struct perf_event_attr *attr, bool init_attr, const char *name, const char *metric_id, struct perf_pmu *pmu, - struct list_head *config_terms, bool auto_merge_stats, + struct list_head *config_terms, struct evsel *first_wildcard_match, struct perf_cpu_map *cpu_list, u64 alternate_hw_config) { struct evsel *evsel; @@ -262,9 +262,9 @@ __add_event(struct list_head *list, int *idx, evsel->core.own_cpus =3D perf_cpu_map__get(cpus); evsel->core.requires_cpu =3D pmu ? pmu->is_uncore : false; evsel->core.is_pmu_core =3D pmu ? pmu->is_core : false; - evsel->auto_merge_stats =3D auto_merge_stats; 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); @@ -423,7 +423,7 @@ bool parse_events__filter_pmu(const struct parse_events= _state *parse_state, static int parse_events_add_pmu(struct parse_events_state *parse_state, struct list_head *list, struct perf_pmu *pmu, const struct parse_events_terms *const_parsed_terms, - bool auto_merge_stats, u64 alternate_hw_config); + struct evsel *first_wildcard_match, u64 alternate_hw_config); =20 int parse_events_add_cache(struct list_head *list, int *idx, const char *n= ame, struct parse_events_state *parse_state, @@ -433,6 +433,7 @@ int parse_events_add_cache(struct list_head *list, int = *idx, const char *name, bool found_supported =3D false; const char *config_name =3D get_config_name(parsed_terms); const char *metric_id =3D get_config_metric_id(parsed_terms); + struct evsel *first_wildcard_match =3D NULL; =20 while ((pmu =3D perf_pmus__scan(pmu)) !=3D NULL) { LIST_HEAD(config_terms); @@ -449,10 +450,14 @@ int parse_events_add_cache(struct list_head *list, in= t *idx, const char *name, */ ret =3D parse_events_add_pmu(parse_state, list, pmu, parsed_terms, - perf_pmu__auto_merge_stats(pmu), + first_wildcard_match, /*alternate_hw_config=3D*/PERF_COUNT_HW_MAX); if (ret) return ret; + + if (first_wildcard_match =3D=3D NULL) + first_wildcard_match =3D + container_of(list->prev, struct evsel, core.node); continue; } =20 @@ -480,11 +485,13 @@ int parse_events_add_cache(struct list_head *list, in= t *idx, const char *name, } =20 if (__add_event(list, idx, &attr, /*init_attr*/true, config_name ?: name, - metric_id, pmu, &config_terms, /*auto_merge_stats=3D*/false, + metric_id, pmu, &config_terms, first_wildcard_match, /*cpu_list=3D*/NULL, /*alternate_hw_config=3D*/PERF_COUNT_HW_MAX) =3D=3D NULL) return -ENOMEM; =20 + if (first_wildcard_match =3D=3D NULL) + first_wildcard_match =3D container_of(list->prev, struct evsel, core.no= de); free_config_terms(&config_terms); } return found_supported ? 0 : -EINVAL; @@ -1415,7 +1422,7 @@ static bool config_term_percore(struct list_head *con= fig_terms) static int parse_events_add_pmu(struct parse_events_state *parse_state, struct list_head *list, struct perf_pmu *pmu, const struct parse_events_terms *const_parsed_terms, - bool auto_merge_stats, u64 alternate_hw_config) + struct evsel *first_wildcard_match, u64 alternate_hw_config) { struct perf_event_attr attr; struct perf_pmu_info info; @@ -1451,7 +1458,7 @@ static int parse_events_add_pmu(struct parse_events_s= tate *parse_state, evsel =3D __add_event(list, &parse_state->idx, &attr, /*init_attr=3D*/true, /*name=3D*/NULL, /*metric_id=3D*/NULL, pmu, - /*config_terms=3D*/NULL, auto_merge_stats, + /*config_terms=3D*/NULL, first_wildcard_match, /*cpu_list=3D*/NULL, alternate_hw_config); return evsel ? 0 : -ENOMEM; } @@ -1521,7 +1528,7 @@ static int parse_events_add_pmu(struct parse_events_s= tate *parse_state, evsel =3D __add_event(list, &parse_state->idx, &attr, /*init_attr=3D*/tru= e, get_config_name(&parsed_terms), get_config_metric_id(&parsed_terms), pmu, - &config_terms, auto_merge_stats, /*cpu_list=3D*/NULL, + &config_terms, first_wildcard_match, /*cpu_list=3D*/NULL, alternate_hw_config); if (!evsel) { parse_events_terms__exit(&parsed_terms); @@ -1554,6 +1561,7 @@ int parse_events_multi_pmu_add(struct parse_events_st= ate *parse_state, int ok =3D 0; const char *config; struct parse_events_terms parsed_terms; + struct evsel *first_wildcard_match =3D NULL; =20 *listp =3D NULL; =20 @@ -1586,17 +1594,14 @@ int parse_events_multi_pmu_add(struct parse_events_= state *parse_state, INIT_LIST_HEAD(list); =20 while ((pmu =3D perf_pmus__scan(pmu)) !=3D NULL) { - bool auto_merge_stats; - if (parse_events__filter_pmu(parse_state, pmu)) continue; =20 if (!perf_pmu__have_event(pmu, event_name)) continue; =20 - auto_merge_stats =3D perf_pmu__auto_merge_stats(pmu); if (!parse_events_add_pmu(parse_state, list, pmu, - &parsed_terms, auto_merge_stats, hw_config)) { + &parsed_terms, first_wildcard_match, hw_config)) { struct strbuf sb; =20 strbuf_init(&sb, /*hint=3D*/ 0); @@ -1605,11 +1610,13 @@ int parse_events_multi_pmu_add(struct parse_events_= state *parse_state, strbuf_release(&sb); ok++; } + if (first_wildcard_match =3D=3D NULL) + first_wildcard_match =3D container_of(list->prev, struct evsel, core.no= de); } =20 if (parse_state->fake_pmu) { if (!parse_events_add_pmu(parse_state, list, perf_pmus__fake_pmu(), &par= sed_terms, - /*auto_merge_stats=3D*/true, hw_config)) { + first_wildcard_match, hw_config)) { struct strbuf sb; =20 strbuf_init(&sb, /*hint=3D*/ 0); @@ -1640,6 +1647,7 @@ int parse_events_multi_pmu_add_or_add_pmu(struct pars= e_events_state *parse_state struct perf_pmu *pmu; int ok =3D 0; char *help; + struct evsel *first_wildcard_match =3D NULL; =20 *listp =3D malloc(sizeof(**listp)); if (!*listp) @@ -1650,14 +1658,14 @@ int parse_events_multi_pmu_add_or_add_pmu(struct pa= rse_events_state *parse_state /* Attempt to add to list assuming event_or_pmu is a PMU name. */ pmu =3D perf_pmus__find(event_or_pmu); if (pmu && !parse_events_add_pmu(parse_state, *listp, pmu, const_parsed_t= erms, - /*auto_merge_stats=3D*/false, + first_wildcard_match, /*alternate_hw_config=3D*/PERF_COUNT_HW_MAX)) return 0; =20 if (parse_state->fake_pmu) { if (!parse_events_add_pmu(parse_state, *listp, perf_pmus__fake_pmu(), const_parsed_terms, - /*auto_merge_stats=3D*/false, + first_wildcard_match, /*alternate_hw_config=3D*/PERF_COUNT_HW_MAX)) return 0; } @@ -1667,15 +1675,16 @@ int parse_events_multi_pmu_add_or_add_pmu(struct pa= rse_events_state *parse_state while ((pmu =3D perf_pmus__scan(pmu)) !=3D NULL) { if (!parse_events__filter_pmu(parse_state, pmu) && perf_pmu__wildcard_match(pmu, event_or_pmu)) { - bool auto_merge_stats =3D perf_pmu__auto_merge_stats(pmu); - if (!parse_events_add_pmu(parse_state, *listp, pmu, const_parsed_terms, - auto_merge_stats, + first_wildcard_match, /*alternate_hw_config=3D*/PERF_COUNT_HW_MAX)) { ok++; parse_state->wild_card_pmus =3D true; } + if (first_wildcard_match =3D=3D NULL) + first_wildcard_match =3D + container_of((*listp)->prev, struct evsel, core.node); } } if (ok) diff --git a/tools/perf/util/stat-display.c b/tools/perf/util/stat-display.c index d427e0ca98a1..7e2ec33e8c99 100644 --- a/tools/perf/util/stat-display.c +++ b/tools/perf/util/stat-display.c @@ -1011,7 +1011,7 @@ static void print_counter_aggrdata(struct perf_stat_c= onfig *config, os->evsel =3D counter; =20 /* Skip already merged uncore/hybrid events */ - if (counter->merged_stat) + if (config->aggr_mode !=3D AGGR_NONE && counter->first_wildcard_match) return; =20 val =3D aggr->counts.val; diff --git a/tools/perf/util/stat.c b/tools/perf/util/stat.c index 1f7abd8754c7..355a7d5c8ab8 100644 --- a/tools/perf/util/stat.c +++ b/tools/perf/util/stat.c @@ -535,35 +535,6 @@ static int evsel__merge_aggr_counters(struct evsel *ev= sel, struct evsel *alias) =20 return 0; } -/* - * Events should have the same name, scale, unit, cgroup but on different = core - * PMUs or on different but matching uncore PMUs. - */ -static bool evsel__is_alias(struct evsel *evsel_a, struct evsel *evsel_b) -{ - if (strcmp(evsel__name(evsel_a), evsel__name(evsel_b))) - return false; - - if (evsel_a->scale !=3D evsel_b->scale) - return false; - - if (evsel_a->cgrp !=3D evsel_b->cgrp) - return false; - - if (strcmp(evsel_a->unit, evsel_b->unit)) - return false; - - if (evsel__is_clock(evsel_a) !=3D evsel__is_clock(evsel_b)) - return false; - - if (evsel_a->pmu =3D=3D evsel_b->pmu || evsel_a->pmu =3D=3D NULL || evsel= _b->pmu =3D=3D NULL) - return false; - - if (evsel_a->pmu->is_core) - return evsel_b->pmu->is_core; - - return perf_pmu__name_no_suffix_match(evsel_a->pmu, evsel_b->pmu->name); -} =20 static void evsel__merge_aliases(struct evsel *evsel) { @@ -572,10 +543,9 @@ static void evsel__merge_aliases(struct evsel *evsel) =20 alias =3D list_prepare_entry(evsel, &(evlist->core.entries), core.node); list_for_each_entry_continue(alias, &evlist->core.entries, core.node) { - /* Merge the same events on different PMUs. */ - if (evsel__is_alias(evsel, alias)) { + if (alias->first_wildcard_match =3D=3D evsel) { + /* Merge the same events on different PMUs. */ evsel__merge_aggr_counters(evsel, alias); - alias->merged_stat =3D true; } } } @@ -588,11 +558,7 @@ static bool evsel__should_merge_hybrid(const struct ev= sel *evsel, =20 static void evsel__merge_stats(struct evsel *evsel, struct perf_stat_confi= g *config) { - /* this evsel is already merged */ - if (evsel->merged_stat) - return; - - if (evsel->auto_merge_stats || evsel__should_merge_hybrid(evsel, config)) + if (!evsel->pmu || !evsel->pmu->is_core || evsel__should_merge_hybrid(evs= el, config)) evsel__merge_aliases(evsel); } =20 --=20 2.49.0.472.ge94155a9ec-goog