From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=74.125.82.198 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796364754; b=b/xVtpFtSwcS3VYEVljmz1zzcpEfY5Gna/h9qDxlOqJvxVlYSwKIT7qeU6Hg10CyFZ7Y 6+x9s7uROQiH7JOmqTaFYp8GuvsEPF0M/qexat+x4/kFPEQ7q/OyR/QdOEC/a7ic8cTup 8UY3jPKH1SZQZo7LxLcQP46QfBgrjWW3PJMDcDl6sNGwOatXRSBfyLjs20+9I07rSRGfb VmXhQ4YnhU/MtwWm5LpsPrQEmaaltiVOtqGd2zztpY81beYYsaYk1JBvJSfJ6MSJ8+u4Z hBImAGMOyOqR1DvC9j5ySHSU6sTBeC4WKlFVGe8Rq12/zUVbHAs4XkHa+EID4BAayHw== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796364754; h=DMARC-Filter:Received:DKIM-Signature:X-Google-DKIM-Signature: X-Forwarded-Encrypted:X-Gm-Message-State:X-Received:Date:Mime-Version: X-Mailer:Message-ID:Subject:From:To:Cc:Content-Type; bh=Nf83BKvv+ifqjWryAIJyALDbQAvLYQcGPoMh8E4thTs=; b=SpKWbEXKphGJ971rO1dQD2AC2oyhaLuUuj2wo5lU12pCkyZSasc4JAf17/uCXg0wR5Xp 5vLUI1Gec3hE0XKWkavxXW/xuNGyxLbhhKWSNseInrSw+RPjyTBmx05YsfHLadHNWgxX6 5YtDpzsvMsRNALEy/kCVGoO9ZcK+INvqdn0F5XPHvSxDLaWGBRFwqlG9IyO7Jk8c2AYYc Xgd8Ao8kvc3GFreuhLpiuvRMVcDEhoYv3PwLzM/v7ybqKlh7HBGsknNGrPn8IgWc0golL gI2sbhxFcOwyAFDtDWqIkL1FCwl8qTC+G+q1CHk5CI/uHsJRox4ZHb65elfbWxZbZrg== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass header.from=google.com; spf=pass smtp.mailfrom=flex--irogers.bounces.google.com; dkim=pass header.d=google.com header.i=@google.com header.a=rsa-sha256 header.s=20260707 header.b=gkahHv1U; arc=none smtp.remote-ip=74.125.82.198 Received: from mail-dy1-f198.google.com (mail-dy1-f198.google.com [74.125.82.198]) (using TLSv1.3 with cipher TLS_AES_128_GCM_SHA256 (128/128 bits) key-exchange X25519 server-signature ECDSA (prime256v1) server-digest SHA256) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 61259606A322 for ; Fri, 4 Dec 2026 06:12:33 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org 61259606A322 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.a=rsa-sha256 header.s=20260707 header.b=gkahHv1U Received: by mail-dy1-f198.google.com with SMTP id 5a478bee46e88-3846d5f54c1so799604eec.1 for ; Thu, 03 Dec 2026 22:12:33 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20260707; t=1796364752; x=1796969552; darn=vger.kernel.org; h=content-type:cc:to:from:subject:message-id:mime-version:date:from :to:cc:subject:date:message-id:reply-to:content-type; bh=Nf83BKvv+ifqjWryAIJyALDbQAvLYQcGPoMh8E4thTs=; b=gkahHv1U0qRRtKLU314gdFbuveZw2uAVbq6EugXIPHxNSNLDI8/gzoQ3d6OhbrBmYF 2dC/O4+6KbbEFfE03DUhUcuAECLQG8LaUzFu7EvKHh7m9SCM2VK8GK37jCWqxaMenBxE lvw3uxzt9IsyTxPIiQHR3OBVhrwGrzXcQvtsZKOYu6CHXXoey2mn+duGAziZYUnjqYc/ 2YbezFWdLpzXt5mp9Q0TBVsiCmP6y5417lxziiZUzXDsz7qF9nklNDrcxtvA3qqQFRRE Zobz50YGy26K0/y4uwgx5Sx1ZSeGPXRkXiUBe5uc7re5fEHXkIV8+qKGPDuNdzazj2ts IRFQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20260707; t=1796364752; x=1796969552; h=content-type:cc:to:from:subject:message-id:mime-version:date :x-gm-message-state:from:to:cc:subject:date:message-id:reply-to :content-type; bh=Nf83BKvv+ifqjWryAIJyALDbQAvLYQcGPoMh8E4thTs=; b=eNLVrtsmJVMF8fty4YLj8p5wkHCI2egVLYghPZ+00BVy9DQDcS5sPGc+t2/ZWaLkpQ KJ4cloF/IRZcr7CnWbYUlpAqzI0X+MFhdMR6nmC5Z/j/HzkETA4wQpXlE2hgnonI/O2Q jVibzPwKIHYXV7U/Kb5ZS6ZhY0ciCE8tnq1oVKPTd1PVo/MRZ+T8w6dxH1nMwBOBizZQ QrO89BmMd1q1HZyKEaHU/uNFwVY8vPQ9KMV8VNNXtCauZdVEholM8FYHc+n7T2kHt39i Cjc22FjRFEiuFrZMeHAd45/umv5h5ODiWxRZD2e0MWs7V4VCvuEN372jIZ53HPYykJNN zimQ== X-Forwarded-Encrypted: i=1; AL+hErFP4EyWVF1Op2t45nRO7SDcfOMOEH9kiMc6ylPnQo4YKEsyfs7JhYLNrQ1eyxINIeVkVqPVVjlrqfZtiprII5jq@vger.kernel.org X-Gm-Message-State: APuU4Z8J9gT0tgmuIdQFu0kpX2WgRrKnuPzQnJo0Z+XTx2J0vhrqPWXM iX6ukebpyCpf9d/fQlo0V/l4qOyYVnqzosYm+Ke4su1kTGEFA4tIqc4syRa2UtyTFm8iVu+3M5r LE/zJMxGPWA== X-Received: from dybma23.prod.google.com ([2002:a05:7301:1697:b0:386:ce6a:b261]) (user=irogers job=prod-delivery.src-stubby-dispatcher) by 2002:a05:7300:dc14:b0:386:d917:8d8a with SMTP id 5a478bee46e88-3875219719emr1554624eec.27.1796364752099; Thu, 03 Dec 2026 22:12:32 -0800 (PST) Date: Thu, 3 Dec 2026 22:12:26 -0800 X-Mailing-List: linux-perf-users@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list Mime-Version: 1.0 X-Mailer: git-send-email 2.56.1.915.g69d5ec7298-goog Message-ID: <20261204061226.2483613-1-irogers@google.com> Subject: [PATCH v2] perf evlist: Reparent first_wildcard_match when evsels are removed From: Ian Rogers To: Peter Zijlstra , Ingo Molnar , Arnaldo Carvalho de Melo , Namhyung Kim Cc: Jiri Olsa , Adrian Hunter , James Clark , linux-perf-users@vger.kernel.org, linux-kernel@vger.kernel.org, Ian Rogers Content-Type: text/plain; charset="UTF-8" When a wildcard event expands across multiple PMUs (such as hybrid core PMUs cpu_atom/cpu_core or multiple uncore PMU instances), the later evsels point their first_wildcard_match field to the first matched evsel. During stat aggregation and display, evsel__merge_aliases() merges aliases into their first_wildcard_match and print_counter_aggrdata() skips any evsel with a non-NULL first_wildcard_match (for uncore events and hybrid events under --hybrid-merge). If that first matched evsel is subsequently removed from the evlist -- either by __perf_evlist__propagate_maps() during perf_evlist__set_maps() or perf_evlist__add() when its CPU map intersection with -C or a per-event cpu= term is empty, or via evlist__remove() when opening fails -- the remaining aliases keep a stale first_wildcard_match pointing to the removed evsel. As a result, they are neither merged nor displayed (and evlist__expand_cgroup() copies a NULL priv pointer from the removed evsel). For example, on a hybrid x86 machine (cpu_core on CPUs 0-15, cpu_atom on CPUs 16-27): $ perf stat --hybrid-merge -C 0 -e 'cycles,duration_time' -- true cpu_atom/cycles/ is removed due to an empty CPU intersection with -C 0, while cpu_core/cycles/ keeps first_wildcard_match pointing to the removed cpu_atom/cycles/ and is silently omitted from the output. Add evlist__reparent_wildcard_match() to promote the first remaining alias in the evlist to be the new primary (first_wildcard_match = NULL) and reparent any subsequent aliases to it without dereferencing the removed evsel pointer. Call it from evlist__remove(), and call evlist__fix_removed_wildcard_matches() (checking live list membership by pointer equality via evlist__contains()) whenever map propagation removes an evsel in evlist__splice_list_tail() or via the new evlist__set_maps() helper used by evlist__create_maps() and evlist__create_syswide_maps(). Add unit tests in tools/perf/tests/hybrid-merge.c covering direct evlist__remove(), empty-CPU map propagation via evlist__create_maps(), and empty-CPU map propagation during evlist__splice_list_tail(). Fixes: 137359b7895f ("perf parse-events: Use wildcard processing to set an event to merge into") Signed-off-by: Ian Rogers --- Changes in v2: - Also call evlist__fix_removed_wildcard_matches() at the end of evlist__splice_list_tail() when map propagation in evlist__add() removes an empty-CPU evsel, covering per-event cpu= terms before evlist__create_maps() (such as evlist__expand_cgroup()) and callers like pyrf__parse_events() (Sashiko). - Gate evlist__fix_removed_wildcard_matches() calls on an actual entry count drop in evlist__splice_list_tail() and a new evlist__set_maps() helper so the common case stays O(1). - Add a unit test in tools/perf/tests/hybrid-merge.c covering evlist__splice_list_tail() empty-CPU removal. - v1: https://lore.kernel.org/linux-perf-users/20261204011846.1959123-1-irogers@google.com/ tools/perf/tests/hybrid-merge.c | 177 +++++++++++++++++++++++++++++++- tools/perf/util/evlist.c | 60 ++++++++++- 2 files changed, 233 insertions(+), 4 deletions(-) diff --git a/tools/perf/tests/hybrid-merge.c b/tools/perf/tests/hybrid-merge.c index 426b1d09618e..e0df37d4bdef 100644 --- a/tools/perf/tests/hybrid-merge.c +++ b/tools/perf/tests/hybrid-merge.c @@ -1,11 +1,14 @@ // SPDX-License-Identifier: GPL-2.0 #include #include +#include #include +#include #include "debug.h" #include "env.h" #include "evlist.h" #include "evsel.h" +#include "target.h" #include "tests.h" /* A hybrid machine with 2 core PMUs, as read from a perf.data file. */ @@ -21,7 +24,7 @@ static struct hybrid_node three_core_pmus[] = { { .pmu_name = (char *)"cpu_lowpower", .cpus = (char *)"8", }, }; -static struct evsel *test_evsel__new(struct evlist *evlist, const char *name) +static struct evsel *test_evsel__new_unlisted(const char *name) { struct perf_event_attr attr = { .type = PERF_TYPE_RAW, @@ -38,7 +41,15 @@ static struct evsel *test_evsel__new(struct evlist *evlist, const char *name) evsel__put(evsel); return NULL; } - evlist__add(evlist, evsel); + return evsel; +} + +static struct evsel *test_evsel__new(struct evlist *evlist, const char *name) +{ + struct evsel *evsel = test_evsel__new_unlisted(name); + + if (evsel) + evlist__add(evlist, evsel); return evsel; } @@ -193,10 +204,172 @@ static int test__hybrid_merge_unmergeable(struct test_suite *test __maybe_unused return ret; } +static int check_reparent_wildcard_remove(struct evlist *evlist) +{ + struct evsel *atom_cycles, *core_cycles, *lowpower_cycles; + + atom_cycles = test_evsel__new(evlist, "cpu_atom/cycles/"); + core_cycles = test_evsel__new(evlist, "cpu_core/cycles/"); + lowpower_cycles = test_evsel__new(evlist, "cpu_lowpower/cycles/"); + TEST_ASSERT_VAL("failed to allocate evsels", + atom_cycles && core_cycles && lowpower_cycles); + + core_cycles->first_wildcard_match = atom_cycles; + lowpower_cycles->first_wildcard_match = atom_cycles; + + evlist__remove(evlist, atom_cycles); + evsel__put(atom_cycles); + TEST_ASSERT_VAL("second PMU not promoted to first wildcard match", + !core_cycles->first_wildcard_match); + TEST_ASSERT_VAL("third PMU not reparented to second PMU", + lowpower_cycles->first_wildcard_match == core_cycles); + + evlist__remove(evlist, core_cycles); + evsel__put(core_cycles); + TEST_ASSERT_VAL("third PMU not promoted after second removal", + !lowpower_cycles->first_wildcard_match); + + return TEST_OK; +} + +static int check_reparent_wildcard_create_maps(struct evlist *evlist) +{ + struct evsel *atom_cycles, *core_cycles, *lowpower_cycles; + struct target target = { + .cpu_list = "0", + }; + + atom_cycles = test_evsel__new(evlist, "cpu_atom/cycles/"); + core_cycles = test_evsel__new(evlist, "cpu_core/cycles/"); + lowpower_cycles = test_evsel__new(evlist, "cpu_lowpower/cycles/"); + TEST_ASSERT_VAL("failed to allocate evsels", + atom_cycles && core_cycles && lowpower_cycles); + + perf_cpu_map__put(atom_cycles->core.pmu_cpus); + perf_cpu_map__put(core_cycles->core.pmu_cpus); + perf_cpu_map__put(lowpower_cycles->core.pmu_cpus); + atom_cycles->core.pmu_cpus = perf_cpu_map__new("1"); + core_cycles->core.pmu_cpus = perf_cpu_map__new("0"); + lowpower_cycles->core.pmu_cpus = perf_cpu_map__new("0"); + TEST_ASSERT_VAL("failed to allocate cpu maps", + atom_cycles->core.pmu_cpus && + core_cycles->core.pmu_cpus && + lowpower_cycles->core.pmu_cpus); + + core_cycles->first_wildcard_match = atom_cycles; + lowpower_cycles->first_wildcard_match = atom_cycles; + + TEST_ASSERT_VAL("evlist__create_maps failed", + evlist__create_maps(evlist, &target) == 0); + TEST_ASSERT_VAL("empty-CPU evsel not removed", + evlist__nr_entries(evlist) == 2 && + list_empty(&atom_cycles->core.node)); + evlist__put(atom_cycles->evlist); + atom_cycles->evlist = NULL; + evsel__put(atom_cycles); + + TEST_ASSERT_VAL("second PMU not promoted after map propagation", + !core_cycles->first_wildcard_match); + TEST_ASSERT_VAL("third PMU not reparented after map propagation", + lowpower_cycles->first_wildcard_match == core_cycles); + return TEST_OK; +} + +static int check_reparent_wildcard_splice(struct evlist *evlist) +{ + struct evsel *atom_cycles, *core_cycles, *lowpower_cycles, *pos, *tmp; + LIST_HEAD(list); + int ret = TEST_FAIL; + + atom_cycles = test_evsel__new_unlisted("cpu_atom/cycles/"); + core_cycles = test_evsel__new_unlisted("cpu_core/cycles/"); + lowpower_cycles = test_evsel__new_unlisted("cpu_lowpower/cycles/"); + if (atom_cycles) + list_add_tail(&atom_cycles->core.node, &list); + if (core_cycles) + list_add_tail(&core_cycles->core.node, &list); + if (lowpower_cycles) + list_add_tail(&lowpower_cycles->core.node, &list); + if (!atom_cycles || !core_cycles || !lowpower_cycles) { + pr_debug("failed to allocate evsels\n"); + goto out; + } + + atom_cycles->core.pmu_cpus = perf_cpu_map__new("1"); + atom_cycles->core.cpus = perf_cpu_map__new("0"); + core_cycles->core.pmu_cpus = perf_cpu_map__new("0"); + core_cycles->core.cpus = perf_cpu_map__new("0"); + lowpower_cycles->core.pmu_cpus = perf_cpu_map__new("0"); + lowpower_cycles->core.cpus = perf_cpu_map__new("0"); + if (!atom_cycles->core.pmu_cpus || !atom_cycles->core.cpus || + !core_cycles->core.pmu_cpus || !core_cycles->core.cpus || + !lowpower_cycles->core.pmu_cpus || !lowpower_cycles->core.cpus) { + pr_debug("failed to allocate cpu maps\n"); + goto out; + } + + core_cycles->first_wildcard_match = atom_cycles; + lowpower_cycles->first_wildcard_match = atom_cycles; + + evlist__splice_list_tail(evlist, &list); + if (list_empty(&atom_cycles->core.node)) { + evlist__put(atom_cycles->evlist); + atom_cycles->evlist = NULL; + evsel__put(atom_cycles); + } else { + pr_debug("empty-CPU evsel not removed during splice\n"); + goto out; + } + TEST_ASSERT_VAL("unexpected evlist entry count after splice", + evlist__nr_entries(evlist) == 2); + TEST_ASSERT_VAL("second PMU not promoted after splice", + !core_cycles->first_wildcard_match); + TEST_ASSERT_VAL("third PMU not reparented after splice", + lowpower_cycles->first_wildcard_match == core_cycles); + ret = TEST_OK; +out: + list_for_each_entry_safe(pos, tmp, &list, core.node) { + list_del_init(&pos->core.node); + evsel__put(pos); + } + return ret; +} + +static int test__hybrid_merge_reparent_removed(struct test_suite *test __maybe_unused, + int subtest __maybe_unused) +{ + struct evlist *evlist = evlist__new(); + int ret; + + TEST_ASSERT_VAL("failed to allocate evlist", evlist); + + ret = check_reparent_wildcard_remove(evlist); + evlist__put(evlist); + if (ret != TEST_OK) + return ret; + + evlist = evlist__new(); + TEST_ASSERT_VAL("failed to allocate evlist", evlist); + + ret = check_reparent_wildcard_create_maps(evlist); + evlist__put(evlist); + if (ret != TEST_OK) + return ret; + + evlist = evlist__new(); + TEST_ASSERT_VAL("failed to allocate evlist", evlist); + + ret = check_reparent_wildcard_splice(evlist); + evlist__put(evlist); + return ret; +} + static struct test_case tests__hybrid_merge[] = { TEST_CASE("Merge events of 2 core PMUs", hybrid_merge_events), TEST_CASE("Merge events of 3 core PMUs", hybrid_merge_3_core_pmus), TEST_CASE("Events that shouldn't merge", hybrid_merge_unmergeable), + TEST_CASE("Reparent first_wildcard_match on evsel removal", + hybrid_merge_reparent_removed), { .name = NULL, } }; diff --git a/tools/perf/util/evlist.c b/tools/perf/util/evlist.c index c2402e4791b6..424b01449c39 100644 --- a/tools/perf/util/evlist.c +++ b/tools/perf/util/evlist.c @@ -458,21 +458,73 @@ void evlist__add(struct evlist *evlist, struct evsel *entry) evlist__set_id_pos(evlist); } +static bool evlist__contains(struct evlist *evlist, const struct evsel *evsel) +{ + struct evsel *pos; + + evlist__for_each_entry(evlist, pos) { + if (pos == evsel) + return true; + } + return false; +} + +static void evlist__reparent_wildcard_match(struct evlist *evlist, + const struct evsel *removed) +{ + struct evsel *pos, *new_first = NULL; + + evlist__for_each_entry(evlist, pos) { + if (pos->first_wildcard_match == removed) { + pos->first_wildcard_match = new_first; + if (!new_first) + new_first = pos; + } + } +} + +static void evlist__fix_removed_wildcard_matches(struct evlist *evlist) +{ + struct evsel *pos; + + evlist__for_each_entry(evlist, pos) { + if (pos->first_wildcard_match && + !evlist__contains(evlist, pos->first_wildcard_match)) + evlist__reparent_wildcard_match(evlist, pos->first_wildcard_match); + } +} + +static void evlist__set_maps(struct evlist *evlist, + struct perf_cpu_map *cpus, + struct perf_thread_map *threads) +{ + int nr_entries = evlist__nr_entries(evlist); + + perf_evlist__set_maps(evlist__core(evlist), cpus, threads); + if (evlist__nr_entries(evlist) < nr_entries) + evlist__fix_removed_wildcard_matches(evlist); +} + void evlist__remove(struct evlist *evlist, struct evsel *evsel) { perf_evlist__remove(evlist__core(evlist), &evsel->core); + evlist__reparent_wildcard_match(evlist, evsel); + evsel->first_wildcard_match = NULL; evlist__put(evsel->evlist); evsel->evlist = NULL; } void evlist__splice_list_tail(struct evlist *evlist, struct list_head *list) { + int expected = evlist__nr_entries(evlist); + while (!list_empty(list)) { struct evsel *evsel, *temp, *leader = NULL; __evlist__for_each_entry_safe(list, temp, evsel) { list_del_init(&evsel->core.node); evlist__add(evlist, evsel); + expected++; leader = evsel; break; } @@ -481,9 +533,13 @@ void evlist__splice_list_tail(struct evlist *evlist, struct list_head *list) if (evsel__has_leader(evsel, leader)) { list_del_init(&evsel->core.node); evlist__add(evlist, evsel); + expected++; } } } + + if (evlist__nr_entries(evlist) < expected) + evlist__fix_removed_wildcard_matches(evlist); } int __evlist__set_tracepoints_handlers(struct evlist *evlist, @@ -1382,7 +1438,7 @@ int evlist__create_maps(struct evlist *evlist, struct target *target) evlist__core(evlist)->has_user_cpus = !!target->cpu_list; - perf_evlist__set_maps(evlist__core(evlist), cpus, threads); + evlist__set_maps(evlist, cpus, threads); /* as evlist now has references, put count here */ perf_cpu_map__put(cpus); @@ -1730,7 +1786,7 @@ static int evlist__create_syswide_maps(struct evlist *evlist) return -ENOMEM; } - perf_evlist__set_maps(evlist__core(evlist), cpus, threads); + evlist__set_maps(evlist, cpus, threads); perf_thread_map__put(threads); perf_cpu_map__put(cpus); return 0; -- 2.56.1.915.g69d5ec7298-goog