From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mail-dl1-f73.google.com (mail-dl1-f73.google.com [74.125.82.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 8FF66342C9E for ; Tue, 5 May 2026 21:20:58 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=74.125.82.73 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1778016061; cv=none; b=EZ3pVzeKxyZ9wzW9Plh4pR3WY7LINEXNMUmnmA7h5+xtm/+1BvymU6MlCwK/v77rvc/KoBmb5Wy/UCyPGy3AFXbZUPTJJ6JAAFzmTd5j9NnwGNx86jAed9UOiBGvugi9XSKjgG4Qr3BoZiygNy1nhjHihW9fphSwHMsnjC2OhSE= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1778016061; c=relaxed/simple; bh=d5Of11+55O6Fqy/aE6U5IITFEFsMWVZvzZsA9FBfaDc=; h=Date:Mime-Version:Message-ID:Subject:From:To:Cc:Content-Type; b=ee3TJlU3WYegJesAnH+fHjtvScrSKxUYCj0V/1tb39v6SSP9iTv6Ny0R6mRY69uTapWirh591enKW5Hhtnx/TrKHovGTb7XIjlqtmHQJpEE8ldllq6CSB2xMq5+98IJ8prl27MVczlQWornRZenXEwcdvP+erIWwrmql9nHZVqI= 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=KM1CJHAr; arc=none smtp.client-ip=74.125.82.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="KM1CJHAr" Received: by mail-dl1-f73.google.com with SMTP id a92af1059eb24-12c8ccc7593so7379974c88.1 for ; Tue, 05 May 2026 14:20:58 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20251104; t=1778016058; x=1778620858; darn=vger.kernel.org; h=cc:to:from:subject:message-id:mime-version:date:from:to:cc:subject :date:message-id:reply-to; bh=5A9IEHvBeBYnONRUgAiif7JcNDoW2hLKpQtkAXRwhaw=; b=KM1CJHAr7a6lRNvNpCq4Jdg5DA+w9F7AQj3701QrZEMBGVFsIAY8XYxdEeDOvLOur5 /5NaHIeOg1Lih3wIrFHZnCyH4NN4KsDiRxOPhOmi9UGrUZN3BBgnzLq2kC1fik/LIW/a Vpi6K5R4bq2DLAURaBluwp/WYfjbb6u+cZyh6staLHaIYa/95PncjZyjedP6Ne04OSMQ +AUQA0lJFe6y/evOuhj4aPTtoxYu5lKgXAu9uZrTMCnMqktZ+Tk/C5I/M7/ZR/tH9PqH bV+CwcyTys6wYI0Pk4pC5IyUA4aSjwZuNmMXVNmtVG+4Z5/y7Dka/UOoWK4Il8zmF3NV zIXQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1778016058; x=1778620858; h=cc:to:from:subject:message-id:mime-version:date:x-gm-message-state :from:to:cc:subject:date:message-id:reply-to; bh=5A9IEHvBeBYnONRUgAiif7JcNDoW2hLKpQtkAXRwhaw=; b=KE7IFPrNOlH5O2YdulujRnQiNFdGYL1qgdgXxhTgM8F4Gu08S2EPthLN2BUn0K1J1Q dKFVtqlDAWK3xtpVEXN6QbHYpHYxbodf7lCgi7S8C8tS3ikXC6jflnz6mR4It5E2zMSk 4v75augv5fRXgzGgE9evUtgFweq7BF3yY+yVGhMcNWHvs2GE96rM7gvPD66Mls3QMHgn 0QtpOiH6ToRgpLhOTTj/p+naVVYBLuZH/+B/zpx/r3dQXnaYBviO007Nv6e5xsAgVqNX a/BZRl4L37nkiNyLTxM6NhhX9hBxX7FO3uQDOJMP6nFbDGX48JvulF3fEvMhbpXtaE5f fzRA== X-Gm-Message-State: AOJu0Yw1uMVWOYGcF/NoOGe/lGsJ0BbOA4S/vZz+dU+B7mtOIJs40IrS S2uusto+iCOdB/PY3zCjcXBxqXXPAdmullE3Qs0VJOL05nzXk41+Q+T8tq5N/oHrSoClczPW2pl YzKcqGkamPhQXU3gyXQf3AeenZKvPNZjTqZbLGtN653569Ye4UqN5NmkZUIMMZKpJlKkw/faUOe BfgGs6KMR/L9RyzzDy1+J1+SjbntDZb1heZ2A9g9ryumZ8 X-Received: from dlbpv3.prod.google.com ([2002:a05:7023:903:b0:12c:9097:2891]) (user=ctshao job=prod-delivery.src-stubby-dispatcher) by 2002:a05:7022:2222:b0:128:d219:e169 with SMTP id a92af1059eb24-131853dc968mr524516c88.10.1778016057368; Tue, 05 May 2026 14:20:57 -0700 (PDT) Date: Tue, 5 May 2026 14:19:00 -0700 Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 X-Mailer: git-send-email 2.54.0.545.g6539524ca2-goog Message-ID: <20260505211924.1961056-2-ctshao@google.com> Subject: [RESEND PATCH v2] perf tools: Ensure event leader stays at head of evlist after sorting 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, james.clark@linaro.org, thomas.falcon@intel.com, tmricht@linux.ibm.com, linux-perf-users@vger.kernel.org Content-Type: text/plain; charset="UTF-8" From: Ian Rogers For evlist of a certain event/metric, the HEAD should be the event leader. In some scenarios where uncore_xxx_0 does not exist, the event leader is not the first element after sorting. For example, on my test machine uncore_iio_0 does not exist, the event leader is uncore_iio_2. However, in `evlist__cmp`, it was reordered based on the PMU name, which makes uncore_iio_1 the HEAD of evlist, breaking the following merge logic in `evsel__merge_aliases`. The patch adds a loop at the end of `parse_events__sort_events_and_fix_groups` to make sure the first wildcard match is the earliest entry in the list, updating pointers accordingly without breaking reordering detection. Tested on device lacks uncore_iio_0, and `perf test` looks good. Signed-off-by: Ian Rogers Signed-off-by: Chun-Tse Shao --- v2: Adding missing Signed-off-by from Ian Rogers. Renaming variable `l` to `orig_leader`. v1: lore.kernel.org/20260501043532.790711-1-ctshao@google.com tools/perf/util/parse-events.c | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) diff --git a/tools/perf/util/parse-events.c b/tools/perf/util/parse-events.c index 1497e1f2a08c..943569e82b82 100644 --- a/tools/perf/util/parse-events.c +++ b/tools/perf/util/parse-events.c @@ -2251,6 +2251,33 @@ static int parse_events__sort_events_and_fix_groups(struct list_head *list) } last_event_was_forced_leader = (force_grouped_leader == pos); } + + /* + * Make sure the first wildcard match is the earliest entry in the list. + * Since list_sort might have reordered the aliases, the original leader + * might not be at the head of the list anymore. We find the first + * alias in the sorted list and make it the new leader, and redirect + * all other aliases to it. + */ + list_for_each_entry(pos, list, core.node) { + struct evsel *orig_leader = pos->first_wildcard_match; + + if (!orig_leader) + continue; + + if (orig_leader->first_wildcard_match) { + /* Original leader was redirected to a new leader */ + pos->first_wildcard_match = orig_leader->first_wildcard_match; + } else if (pos->core.idx < orig_leader->core.idx) { + /* + * We are earlier than the original leader in sorted order, + * and no earlier alias has claimed leadership yet. + */ + orig_leader->first_wildcard_match = pos; + pos->first_wildcard_match = NULL; + } + } + list_for_each_entry(pos, list, core.node) { struct evsel *pos_leader = evsel__leader(pos); -- 2.54.0.545.g6539524ca2-goog