From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=74.125.82.197 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796347132; b=Fn7DZfRIRv1GmNnD5JST9fQPGoeafj1S/VnsGatQcfL727lbrSa59LT3tNeZQvIMaKsP /gdtSdkQ21of/gwwox9Z6FGTSsIvPUUFwgETkXUkaaCJ3hLhYpks5mDOltKZCwEQb112N s2Q1PHtlQR1V6BI540ejqO3P/ujnw3XDRPEPqE94wMLpj/Ei1OmAl9EVBONsgj5eHwqks QaHtNxDZr7oJrmYkgx1I9/961pVasDX1z2gcDuxydG/Kpq989omunhkHnE8RZzzOgfWYu sSmuOSd01Dq3WhGPelJO4twfhsZQRp3hpVSdL2pv/uWKaKRkrDdZvtiFtE9+wR+1Xlg== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796347132; 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=LeKqksVHUuQhefkTi0zY8GYocI5WRH7qctWQbXP6NnE=; b=ze9KYcI8TbAGN81kCVfKp7+i3fXP8r7E7ngGZHSgoEaDvs9ayB055WOpEtUbL3KoFThV Fgt5sb8QfAWb4pziPAQWlhFW6gm4QzdIcaVbLl0w2LIUm5YJiAc3lYFHGbSsOSSLWSRCw Ua7OoFijMwCJsXJNJ+dJE9HGkBjTtLVtReBAL7sDmRlr/UUWhNcNY7vlSSXWf9GRBInsn 2BnD2hDOZAXUELDv0N85R9BNrabn5ziwqh2QLy5v8nkNkSXp/AyLcngx8ndi6aWNnzF2r /OJ7SuT0ifnVfd6+un4OCQtfTqwT0jYYdKZJtKO7RgXDn9j79taI+mCxAR+C4zUtnpg== 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=k9Ue4d+L; arc=none smtp.remote-ip=74.125.82.197 Received: from mail-dy1-f197.google.com (mail-dy1-f197.google.com [74.125.82.197]) (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 8C474606A322 for ; Fri, 4 Dec 2026 01:18:52 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org 8C474606A322 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=k9Ue4d+L Received: by mail-dy1-f197.google.com with SMTP id 5a478bee46e88-33713e5e6daso741711eec.0 for ; Thu, 03 Dec 2026 17:18:52 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20260707; t=1796347132; x=1796951932; 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=LeKqksVHUuQhefkTi0zY8GYocI5WRH7qctWQbXP6NnE=; b=k9Ue4d+LpJ/GbdFlXOn35cGLp56gig1XzhyXYLg68AaKTNlUojnH8XZLxwjO497i3p kknGPQYnjtOCkyX+oeXtXP3cqWamkKk7De1LNp2pyOFTyfQ/LsAytGBqr/p1B/APMO8u v6lplx2KSadWn3mAiBVrIQ63OCuPJo40+ewzDpl9K1c74nEtC8Fn2rGXrIqFhA3bNtfY mrvP/bw1il5MljvSuYyko15DfHDTZKsZDpAK1dyup8joNotxN8ePJtqH45r+Qro8KpJR J2CwvU8kPUrqxNq9R019uRgwv+qmpfKQd1iran6g/sEtGKQPlD0RlHc+OQmpVFB6mOYB Ti5w== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20260707; t=1796347132; x=1796951932; 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=LeKqksVHUuQhefkTi0zY8GYocI5WRH7qctWQbXP6NnE=; b=k3ywPEBzHMGfTIJI9lq04J/PPKNv64z4IkliOoBNx4umvXlshc9IBPH9hl4xjG+bqc xyRQiELpRywskRXx7YYizOYuA2YalkJLcuaHS2NXtDJJN54zKRLFfyFqZHeAWVliDted u0p77pQTWJ20osqnpcuHCjH8+xgtyCproC3k3LSN9cEGetqsGFG4aezGWSGGhUD0FXmw GA/OD2tB9TLCgs5UoBrUTwlFy7qwLgi2xajv/XQGLU0tI/WkFd04vxgjEwNX75OTXh2d T/eTB3s3i4ck2zUIdZJxjtiR0b/c30XnpxQpFWqyMUJ28sODzUtlyTa5kIILu4b+PzYt ygvQ== X-Forwarded-Encrypted: i=1; AL+hErFnHTGqiAQa44UrR4aSOQGdOpNjJTxstAdcUQ1g8yrWhOCmLRwDbxMiwhl/0o40hZr6pUJ/OKZCBmHcpks=@vger.kernel.org X-Gm-Message-State: APuU4Z+1vYZXQ2E6sGlddm0HBegjt5P3IAQcQQD4k42GGtQIi8JJb/RT zw+Gy7FCmoL+gCB097VI6IlBk/47lP1R6p8SUp8hi0RI5kzjhVYnn0TSvCWFItcI8sa83A1u9oj JHy1q3r7ClA== X-Received: from dyah4-n1.prod.google.com ([2002:a05:693c:83c4:10b0:386:9be5:2d95]) (user=irogers job=prod-delivery.src-stubby-dispatcher) by 2002:a05:7300:599f:b0:368:7c2d:a612 with SMTP id 5a478bee46e88-387521552e5mr553055eec.10.1796347131337; Thu, 03 Dec 2026 17:18:51 -0800 (PST) Date: Thu, 3 Dec 2026 17:18:46 -0800 X-Mailing-List: linux-kernel@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: <20261204011846.1959123-1-irogers@google.com> Subject: [PATCH] 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__set_maps() / __perf_evlist__propagate_maps() when its CPU map intersection with -C 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. 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 via evlist__fix_removed_wildcard_matches() (checking live list membership by pointer equality via evlist__contains()) after perf_evlist__set_maps() in evlist__create_maps() and evlist__create_syswide_maps(). Add unit tests in tools/perf/tests/hybrid-merge.c covering both direct evlist__remove() and empty-CPU map propagation via evlist__create_maps(). Fixes: 137359b7895f ("perf parse-events: Use wildcard processing to set an event to merge into") Signed-off-by: Ian Rogers --- tools/perf/tests/hybrid-merge.c | 96 +++++++++++++++++++++++++++++++++ tools/perf/util/evlist.c | 40 ++++++++++++++ 2 files changed, 136 insertions(+) diff --git a/tools/perf/tests/hybrid-merge.c b/tools/perf/tests/hybrid-merge.c index 426b1d09618e..7a25eb8f34ee 100644 --- a/tools/perf/tests/hybrid-merge.c +++ b/tools/perf/tests/hybrid-merge.c @@ -2,10 +2,12 @@ #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. */ @@ -193,10 +195,104 @@ 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 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); + 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..4b8cba6410c4 100644 --- a/tools/perf/util/evlist.c +++ b/tools/perf/util/evlist.c @@ -458,9 +458,47 @@ 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); + } +} + 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; } @@ -1383,6 +1421,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__fix_removed_wildcard_matches(evlist); /* as evlist now has references, put count here */ perf_cpu_map__put(cpus); @@ -1731,6 +1770,7 @@ static int evlist__create_syswide_maps(struct evlist *evlist) } perf_evlist__set_maps(evlist__core(evlist), cpus, threads); + evlist__fix_removed_wildcard_matches(evlist); perf_thread_map__put(threads); perf_cpu_map__put(cpus); return 0; -- 2.56.1.915.g69d5ec7298-goog