From nobody Sun Feb 8 01:32:58 2026 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 7D13DC0015E for ; Wed, 19 Jul 2023 00:18:51 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229813AbjGSASu (ORCPT ); Tue, 18 Jul 2023 20:18:50 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:40342 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229450AbjGSASo (ORCPT ); Tue, 18 Jul 2023 20:18:44 -0400 Received: from mail-yw1-x114a.google.com (mail-yw1-x114a.google.com [IPv6:2607:f8b0:4864:20::114a]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 56B9E134 for ; Tue, 18 Jul 2023 17:18:43 -0700 (PDT) Received: by mail-yw1-x114a.google.com with SMTP id 00721157ae682-5618857518dso42513537b3.2 for ; Tue, 18 Jul 2023 17:18:43 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20221208; t=1689725922; x=1692317922; h=to:from:subject:references:mime-version:message-id:in-reply-to:date :from:to:cc:subject:date:message-id:reply-to; bh=EzZfmowfpaOAxfw4qEjYh3Dd4yzwOwjsuGJmX6Wkr1Y=; b=JlGx8OjDED/9mGbeDU0purblcp19YsAhhm1yfmMZ5AxoB5/YnBIVJ/U+xIe03wLKmZ KF045vV6VIxDYj67PwbuF6ceSylQbxVCDPbUn9W+wXvL/fdkQPPQNXt6qOzJd0aioqru LL55ddw9h6A+ybAxScavuNT3dw4seXKoJ4LOIjLjVxHs6TAw7NpQbdssGguG+n60DshU iUD522FEt7WLwEErStpyo+Dp1IgZFHnwau4a9j4FI7Wc/CzQ0HVo9GevlpRV6tFjPdxQ aT4XxWx40ZtmI31Koarzs2xCPGSXSj6nUoHSxwvI630sTTaZpZ5CJn3n+/PSAx0wXvxH 7ZWA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1689725922; x=1692317922; h=to:from:subject:references:mime-version:message-id:in-reply-to:date :x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=EzZfmowfpaOAxfw4qEjYh3Dd4yzwOwjsuGJmX6Wkr1Y=; b=cbSfsxps48XklisU9Xdg6eUO51pgApaLr5EGoND0WbgcB+qi/2zLEEoGVyGOhlBzJL oiv4NcTauw1dWlLDcuQe6NywqVBNFDVy/kxF8W01UacYHz98qXEzCVD0Oop5dXsx5p5a cRBp6e0je91SD7ysp0BoJSHsauRnBKAcCSJ1Do2uUJp2rrenZI4BA8t711ItB9xxmCp9 Y5gTzDQMYSPZInTpWs2ZMMEa7p/uxztzT5fj8nPyicSccIC32k4IzKgKmi362Udq/Yvp tTRIlWXOxRN62dqGo/g25L0csgwZY+bkBQgldH+ciwPjuk+euIlqBYS3D0lq8GwVlUX+ bTyg== X-Gm-Message-State: ABy/qLbltfb6IDJQzblssnpwRU7biCG5CpZIDzMeggl9e0nXsz9fLDmI dN9lzyClPu15x5sHLPztU3hlKDv7scp2 X-Google-Smtp-Source: APBJJlGoqw5slG37ZamIc0FtM+3St0BI0iyNqMpsLcXhIrObXzSVzC4tb8ifO4A5x/NN2TV3wfKqESDC4+Gt X-Received: from irogers.svl.corp.google.com ([2620:15c:2a3:200:c587:348f:a079:d876]) (user=irogers job=sendgmr) by 2002:a81:8d51:0:b0:579:fa4c:1f22 with SMTP id w17-20020a818d51000000b00579fa4c1f22mr191864ywj.6.1689725922615; Tue, 18 Jul 2023 17:18:42 -0700 (PDT) Date: Tue, 18 Jul 2023 17:18:34 -0700 In-Reply-To: <20230719001836.198363-1-irogers@google.com> Message-Id: <20230719001836.198363-2-irogers@google.com> Mime-Version: 1.0 References: <20230719001836.198363-1-irogers@google.com> X-Mailer: git-send-email 2.41.0.487.g6d72f3e995-goog Subject: [PATCH v1 1/3] perf parse-events: Extra care around force grouped events From: Ian Rogers To: Andi Kleen , Peter Zijlstra , Ingo Molnar , Arnaldo Carvalho de Melo , Mark Rutland , Alexander Shishkin , Jiri Olsa , Namhyung Kim , Ian Rogers , Adrian Hunter , Kan Liang , Zhengjun Xing , linux-perf-users@vger.kernel.org, linux-kernel@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Perf metric (topdown) events on Intel Icelake+ machines require a group, however, they may be next to events that don't require a group. Consider: cycles,slots,topdown-fe-bound The cycles event needn't be grouped but slots and topdown-fe-bound need grouping. Prior to this change, as slots and topdown-fe-bound need a group forcing and all events share the same PMU, slots and topdown-fe-bound would be forced into a group with cycles. This is a bug on two fronts, cycles wasn't supposed to be grouped and cycles can't be a group leader with a perf metric event. This change adds recognition that cycles isn't force grouped and so it shouldn't be force grouped with slots and topdown-fe-bound. Fixes: a90cc5a9eeab ("perf evsel: Don't let evsel__group_pmu_name() travers= e unsorted group") Signed-off-by: Ian Rogers Tested-by: Andi Kleen --- tools/perf/util/parse-events.c | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/tools/perf/util/parse-events.c b/tools/perf/util/parse-events.c index 5dcfbf316bf6..f10760ac1781 100644 --- a/tools/perf/util/parse-events.c +++ b/tools/perf/util/parse-events.c @@ -2141,7 +2141,7 @@ static int parse_events__sort_events_and_fix_groups(s= truct list_head *list) int idx =3D 0, unsorted_idx =3D -1; struct evsel *pos, *cur_leader =3D NULL; struct perf_evsel *cur_leaders_grp =3D NULL; - bool idx_changed =3D false; + bool idx_changed =3D false, cur_leader_force_grouped =3D false; int orig_num_leaders =3D 0, num_leaders =3D 0; int ret; =20 @@ -2182,7 +2182,7 @@ static int parse_events__sort_events_and_fix_groups(s= truct list_head *list) const struct evsel *pos_leader =3D evsel__leader(pos); const char *pos_pmu_name =3D pos->group_pmu_name; const char *cur_leader_pmu_name, *pos_leader_pmu_name; - bool force_grouped =3D arch_evsel__must_be_in_group(pos); + bool pos_force_grouped =3D arch_evsel__must_be_in_group(pos); =20 /* Reset index and nr_members. */ if (pos->core.idx !=3D idx) @@ -2198,7 +2198,8 @@ static int parse_events__sort_events_and_fix_groups(s= truct list_head *list) cur_leader =3D pos; =20 cur_leader_pmu_name =3D cur_leader->group_pmu_name; - if ((cur_leaders_grp !=3D pos->core.leader && !force_grouped) || + if ((cur_leaders_grp !=3D pos->core.leader && + (!pos_force_grouped || !cur_leader_force_grouped)) || strcmp(cur_leader_pmu_name, pos_pmu_name)) { /* Event is for a different group/PMU than last. */ cur_leader =3D pos; @@ -2208,9 +2209,14 @@ static int parse_events__sort_events_and_fix_groups(= struct list_head *list) * group. */ cur_leaders_grp =3D pos->core.leader; + /* + * Avoid forcing events into groups with events that + * don't need to be in the group. + */ + cur_leader_force_grouped =3D pos_force_grouped; } pos_leader_pmu_name =3D pos_leader->group_pmu_name; - if (strcmp(pos_leader_pmu_name, pos_pmu_name) || force_grouped) { + if (strcmp(pos_leader_pmu_name, pos_pmu_name) || pos_force_grouped) { /* * Event's PMU differs from its leader's. Groups can't * span PMUs, so update leader from the group/PMU --=20 2.41.0.487.g6d72f3e995-goog From nobody Sun Feb 8 01:32:58 2026 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 19F9FC0015E for ; Wed, 19 Jul 2023 00:19:00 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230126AbjGSAS7 (ORCPT ); Tue, 18 Jul 2023 20:18:59 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:40580 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229919AbjGSAS4 (ORCPT ); Tue, 18 Jul 2023 20:18:56 -0400 Received: from mail-yb1-xb49.google.com (mail-yb1-xb49.google.com [IPv6:2607:f8b0:4864:20::b49]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id C36B8196 for ; Tue, 18 Jul 2023 17:18:45 -0700 (PDT) Received: by mail-yb1-xb49.google.com with SMTP id 3f1490d57ef6-c0d62f4487cso5493806276.0 for ; Tue, 18 Jul 2023 17:18:45 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20221208; t=1689725925; x=1692317925; h=content-transfer-encoding:to:from:subject:references:mime-version :message-id:in-reply-to:date:from:to:cc:subject:date:message-id :reply-to; bh=bQhjJCwCizVjI6zsBo644k5ejMqtTn3YGv7O61mhVz0=; b=i0TbVRn1nj8cogXXq+MgECyaLUZs/5vqRr0koKL2JFSQx9nXyET7zyPSab/r+ayTcH wNl82VZToSCfotazOs3Mzo9A5Ems8dkVSKLbLJQ51US38cPe1P09b9kNGpEtHXMn+zRz 4NGv8BaH0+eJ2C9S2y4NTlSE4cXzkuxnenePKBH7Ew+Ad6/b7u3r12p4/YfQ79eHzXWs NqVMDOSP6e+tAgiK9PO61GPXhMTH0golvvVXIA0QuVXwoO4zL6X3+K4mM1s7Gd/oawaV jmzAmLabPVNa7vRUrUvPstW3q4vkhdc342QOEpulBzzK9MWlI0ROrdph3Kdb/uWuzNTS cmHQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1689725925; x=1692317925; h=content-transfer-encoding:to:from:subject:references:mime-version :message-id:in-reply-to:date:x-gm-message-state:from:to:cc:subject :date:message-id:reply-to; bh=bQhjJCwCizVjI6zsBo644k5ejMqtTn3YGv7O61mhVz0=; b=fNM1Sm4Q+fxzGDO/lA6NnZkpiPvonqZfAk53O4YGY4lRY+itALJpTP54FQTJdYxCrO L3+cfuwKlQT8Pmn8AbMAmjM195E6CCmiVzTSiz7SoBejpkXdROEOJtjuKl4GhajQQbG5 PPQIXsdjL5DSju+IlAaJy9na5RmF3aZ1Y5C5SkEogAVR5DWG9OhzMdmA4hBN9fj197gx wfb8WGbMJL7wcySIB+K7EphyBoaqw2xyza0vOsDJSrukDU3c3ogoHNhKnzv8a4L+VEJr f3pnoUB6wEByEVBD3qyWvsFb+KwSvLrtvpTwpdnIUDqZACTmD8FyyBt4cYcQMGDd1cNI lfGw== X-Gm-Message-State: ABy/qLYe/NVi43+3kVfb3UXc/yTqsZjEE+56T6aniJIxdR8+O8dDRSzv aOT740Zcpk2gUFBYkC1U5KaVDNeTj4UO X-Google-Smtp-Source: APBJJlEg+bVWv7rHg8zTymSrEwYQ1XWBqOZtko3019L1wwD8uUHvQFedbBKmp/mm+sJJ9ko5JNlSv0dOzV6T X-Received: from irogers.svl.corp.google.com ([2620:15c:2a3:200:c587:348f:a079:d876]) (user=irogers job=sendgmr) by 2002:a5b:a12:0:b0:c72:2571:bd9a with SMTP id k18-20020a5b0a12000000b00c722571bd9amr11695ybq.6.1689725924923; Tue, 18 Jul 2023 17:18:44 -0700 (PDT) Date: Tue, 18 Jul 2023 17:18:35 -0700 In-Reply-To: <20230719001836.198363-1-irogers@google.com> Message-Id: <20230719001836.198363-3-irogers@google.com> Mime-Version: 1.0 References: <20230719001836.198363-1-irogers@google.com> X-Mailer: git-send-email 2.41.0.487.g6d72f3e995-goog Subject: [PATCH v1 2/3] perf parse-events: When fixing group leaders always set the leader From: Ian Rogers To: Andi Kleen , Peter Zijlstra , Ingo Molnar , Arnaldo Carvalho de Melo , Mark Rutland , Alexander Shishkin , Jiri Olsa , Namhyung Kim , Ian Rogers , Adrian Hunter , Kan Liang , Zhengjun Xing , linux-perf-users@vger.kernel.org, linux-kernel@vger.kernel.org Content-Transfer-Encoding: quoted-printable Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Type: text/plain; charset="utf-8" The evsel grouping fix iterates over evsels tracking the leader group and the current position's group, updating the current position's leader if an evsel is being forced into a group or groups changed. However, groups changing isn't a sufficient condition as sorting may have reordered events and the leader may no longer come first. For this reason update all leaders whenever they disagree. This change breaks certain Icelake+ metrics due to bugs in the kernel. For example, tma_l3_bound with threshold enabled tries to program the events: {topdown-retiring,slots,CYCLE_ACTIVITY.STALLS_L2_MISS,topdown-fe-bound,EXE_= ACTIVITY.BOUND_ON_STORES,EXE_ACTIVITY.1_PORTS_UTIL,topdown-be-bound,cpu/INT= _MISC.RECOVERY_CYCLES,cmask=3D1,edge/,CYCLE_ACTIVITY.STALLS_L3_MISS,CPU_CLK= _UNHALTED.THREAD,CYCLE_ACTIVITY.STALLS_MEM_ANY,EXE_ACTIVITY.2_PORTS_UTIL,CY= CLE_ACTIVITY.STALLS_TOTAL,topdown-bad-spec}:W fixing the perf metric event order gives: {slots,topdown-retiring,topdown-fe-bound,topdown-be-bound,topdown-bad-spec,= CYCLE_ACTIVITY.STALLS_L2_MISS,EXE_ACTIVITY.BOUND_ON_STORES,EXE_ACTIVITY.1_P= ORTS_UTIL,cpu/INT_MISC.RECOVERY_CYCLES,cmask=3D1,edge/,CYCLE_ACTIVITY.STALL= S_L3_MISS,CPU_CLK_UNHALTED.THREAD,CYCLE_ACTIVITY.STALLS_MEM_ANY,EXE_ACTIVIT= Y.2_PORTS_UTIL,CYCLE_ACTIVITY.STALLS_TOTAL}:W Both of these return "" for all events, whilst they work with the group removed respecting that the perf metric events must still be grouped. A vendor events update will need to add METRIC_NO_GROUP to these metrics to workaround the kernel PMU driver issue. Fixes: a90cc5a9eeab ("perf evsel: Don't let evsel__group_pmu_name() travers= e unsorted group") Signed-off-by: Ian Rogers Tested-by: Andi Kleen --- tools/perf/util/parse-events.c | 11 +++-------- 1 file changed, 3 insertions(+), 8 deletions(-) diff --git a/tools/perf/util/parse-events.c b/tools/perf/util/parse-events.c index f10760ac1781..4a36ce60c7dd 100644 --- a/tools/perf/util/parse-events.c +++ b/tools/perf/util/parse-events.c @@ -2181,7 +2181,7 @@ static int parse_events__sort_events_and_fix_groups(s= truct list_head *list) list_for_each_entry(pos, list, core.node) { const struct evsel *pos_leader =3D evsel__leader(pos); const char *pos_pmu_name =3D pos->group_pmu_name; - const char *cur_leader_pmu_name, *pos_leader_pmu_name; + const char *cur_leader_pmu_name; bool pos_force_grouped =3D arch_evsel__must_be_in_group(pos); =20 /* Reset index and nr_members. */ @@ -2215,13 +2215,8 @@ static int parse_events__sort_events_and_fix_groups(= struct list_head *list) */ cur_leader_force_grouped =3D pos_force_grouped; } - pos_leader_pmu_name =3D pos_leader->group_pmu_name; - if (strcmp(pos_leader_pmu_name, pos_pmu_name) || pos_force_grouped) { - /* - * Event's PMU differs from its leader's. Groups can't - * span PMUs, so update leader from the group/PMU - * tracker. - */ + if (pos_leader !=3D cur_leader) { + /* The leader changed so update it. */ evsel__set_leader(pos, cur_leader); } } --=20 2.41.0.487.g6d72f3e995-goog From nobody Sun Feb 8 01:32:58 2026 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id A8556EB64DC for ; Wed, 19 Jul 2023 00:19:03 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229826AbjGSATC (ORCPT ); Tue, 18 Jul 2023 20:19:02 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:40616 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229851AbjGSAS4 (ORCPT ); Tue, 18 Jul 2023 20:18:56 -0400 Received: from mail-yw1-x1149.google.com (mail-yw1-x1149.google.com [IPv6:2607:f8b0:4864:20::1149]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id D90611715 for ; Tue, 18 Jul 2023 17:18:47 -0700 (PDT) Received: by mail-yw1-x1149.google.com with SMTP id 00721157ae682-576d63dfc1dso57087077b3.3 for ; Tue, 18 Jul 2023 17:18:47 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20221208; t=1689725927; x=1692317927; h=to:from:subject:references:mime-version:message-id:in-reply-to:date :from:to:cc:subject:date:message-id:reply-to; bh=89xDhKy5l7vxo2+qhhUkXKLJEl4EKrwY/Qi9H8xXUCI=; b=gIdel0GCvcaHSKraw2i4Ivn7O4LlQ53nZfo/62RYSLb9hMhmWLLBmY5vgEMRZ1W84V GDUp6U2HyeWbRMzIzEIuPP1dgdIUtGMFxHLUSCZ9MnsaA8aPsyVFeKUQIIIHLJ8nibbF 97J9BjhQV0UOl2G8j1YHlOuAHAaUYvzW/240+X+easGP1/F9W28wH0OHj3LbUU5dOckM tZ/aABv4+IsNlyymFk7Nsb4bLSZaYCii5N9Pdae9s642k9L6XmhCZExo8rSeeuK/o2U7 uXdecNsbnNwl9mOBJhcgrbARAm6cOL6/TSxiRPKcKQ0A4wakbnmd2tYNyl/NIPwWXLxl PB4g== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1689725927; x=1692317927; h=to:from:subject:references:mime-version:message-id:in-reply-to:date :x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=89xDhKy5l7vxo2+qhhUkXKLJEl4EKrwY/Qi9H8xXUCI=; b=IdchERoJs1duICyXRqRnLFkDBWMpi4SIFaMFMWmCZQjOzU+vJNbte/ByDVx6aAAOYa ANYyMSgeHcr8CXDUG8k+lV8nPNz4uTGP2L8dkozQWms0DBigI7YY0UYRssLtlzgsCxG+ /qzUfzE88YumxaPMdDgbYieio4TbW2CvVebJwbGja0DQAL6SbRTcKpG4BKmWQu1nfaMp x3TtbY0PnsBGzTr04YJ3dUSqtutRmXdlMgvDQJP3ogLlUFCpypkQA0z4PCmPXynSR+AM oTcy4/SFPjDttwK30/uvoypcnvdpqUSW78vXMU9Ok7bTdr7dq2Dy/OfWiZsCT+5W6vH2 CMgQ== X-Gm-Message-State: ABy/qLYAR6aWRg7ST6gxG6Eza06rlNuYb4b2non9d2r63GObPsAhFlk/ hsPL2ZEn+UxYYUMxbgr55eaGLBVMUmo3 X-Google-Smtp-Source: APBJJlEZo5YGpbD1/T9izHzifiGUeb7YSYDppowvL6xFHVvRkp2RNkGHGeZXSc1tFNcU61fSVlU1AAMB1GK1 X-Received: from irogers.svl.corp.google.com ([2620:15c:2a3:200:c587:348f:a079:d876]) (user=irogers job=sendgmr) by 2002:a81:d447:0:b0:577:3b0c:5b85 with SMTP id g7-20020a81d447000000b005773b0c5b85mr19547ywl.0.1689725927086; Tue, 18 Jul 2023 17:18:47 -0700 (PDT) Date: Tue, 18 Jul 2023 17:18:36 -0700 In-Reply-To: <20230719001836.198363-1-irogers@google.com> Message-Id: <20230719001836.198363-4-irogers@google.com> Mime-Version: 1.0 References: <20230719001836.198363-1-irogers@google.com> X-Mailer: git-send-email 2.41.0.487.g6d72f3e995-goog Subject: [PATCH v1 3/3] perf parse-events: Only move force grouped evsels when sorting From: Ian Rogers To: Andi Kleen , Peter Zijlstra , Ingo Molnar , Arnaldo Carvalho de Melo , Mark Rutland , Alexander Shishkin , Jiri Olsa , Namhyung Kim , Ian Rogers , Adrian Hunter , Kan Liang , Zhengjun Xing , linux-perf-users@vger.kernel.org, linux-kernel@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Prior to this change, events without a group would be sorted as if they were from the location of the first event without a group. For example instructions and cycles are without a group: instructions,{imc_free_running/data_read/,imc_free_running/data_write/},cyc= les parse events would create an eventual evlist like: instructions,cycles,{uncore_imc_free_running_0/data_read/,uncore_imc_free_r= unning_1/data_read/,uncore_imc_free_running_0/data_write/,uncore_imc_free_r= unning_1/data_write/} This is done so that perf metric events, that must always be in a group, will be adjacent and so can be forced into a group. This change modifies the sorting so that only force grouped events, like perf metrics, are sorted and all other events keep their position with respect to groups in the evlist. The location of the force grouped event is chosen to match the first force grouped event. For architectures without force grouped events, ie anything not Intel Icelake or newer, this should mean sorting and fixing doesn't modify the event positions except when fixing the grouping for PMUs of things like uncore events. Reported-by: Andi Kleen Fixes: 347c2f0a0988 ("perf parse-events: Sort and group parsed events") Signed-off-by: Ian Rogers Tested-by: Andi Kleen --- tools/perf/util/parse-events.c | 39 ++++++++++++++++++++++------------ 1 file changed, 26 insertions(+), 13 deletions(-) diff --git a/tools/perf/util/parse-events.c b/tools/perf/util/parse-events.c index 4a36ce60c7dd..e63fc40efea5 100644 --- a/tools/perf/util/parse-events.c +++ b/tools/perf/util/parse-events.c @@ -2092,16 +2092,16 @@ __weak int arch_evlist__cmp(const struct evsel *lhs= , const struct evsel *rhs) return lhs->core.idx - rhs->core.idx; } =20 -static int evlist__cmp(void *state, const struct list_head *l, const struc= t list_head *r) +static int evlist__cmp(void *_fg_idx, const struct list_head *l, const str= uct list_head *r) { const struct perf_evsel *lhs_core =3D container_of(l, struct perf_evsel, = node); const struct evsel *lhs =3D container_of(lhs_core, struct evsel, core); const struct perf_evsel *rhs_core =3D container_of(r, struct perf_evsel, = node); const struct evsel *rhs =3D container_of(rhs_core, struct evsel, core); - int *leader_idx =3D state; - int lhs_leader_idx =3D *leader_idx, rhs_leader_idx =3D *leader_idx, ret; + int *force_grouped_idx =3D _fg_idx; + int lhs_sort_idx, rhs_sort_idx, ret; const char *lhs_pmu_name, *rhs_pmu_name; - bool lhs_has_group =3D false, rhs_has_group =3D false; + bool lhs_has_group, rhs_has_group; =20 /* * First sort by grouping/leader. Read the leader idx only if the evsel @@ -2113,15 +2113,25 @@ static int evlist__cmp(void *state, const struct li= st_head *l, const struct list */ if (lhs_core->leader !=3D lhs_core || lhs_core->nr_members > 1) { lhs_has_group =3D true; - lhs_leader_idx =3D lhs_core->leader->idx; + lhs_sort_idx =3D lhs_core->leader->idx; + } else { + lhs_has_group =3D false; + lhs_sort_idx =3D *force_grouped_idx !=3D -1 && arch_evsel__must_be_in_gr= oup(lhs) + ? *force_grouped_idx + : lhs_core->idx; } if (rhs_core->leader !=3D rhs_core || rhs_core->nr_members > 1) { rhs_has_group =3D true; - rhs_leader_idx =3D rhs_core->leader->idx; + rhs_sort_idx =3D rhs_core->leader->idx; + } else { + rhs_has_group =3D false; + rhs_sort_idx =3D *force_grouped_idx !=3D -1 && arch_evsel__must_be_in_gr= oup(rhs) + ? *force_grouped_idx + : rhs_core->idx; } =20 - if (lhs_leader_idx !=3D rhs_leader_idx) - return lhs_leader_idx - rhs_leader_idx; + if (lhs_sort_idx !=3D rhs_sort_idx) + return lhs_sort_idx - rhs_sort_idx; =20 /* Group by PMU if there is a group. Groups can't span PMUs. */ if (lhs_has_group && rhs_has_group) { @@ -2138,7 +2148,7 @@ static int evlist__cmp(void *state, const struct list= _head *l, const struct list =20 static int parse_events__sort_events_and_fix_groups(struct list_head *list) { - int idx =3D 0, unsorted_idx =3D -1; + int idx =3D 0, force_grouped_idx =3D -1; struct evsel *pos, *cur_leader =3D NULL; struct perf_evsel *cur_leaders_grp =3D NULL; bool idx_changed =3D false, cur_leader_force_grouped =3D false; @@ -2166,12 +2176,14 @@ static int parse_events__sort_events_and_fix_groups= (struct list_head *list) */ pos->core.idx =3D idx++; =20 - if (unsorted_idx =3D=3D -1 && pos =3D=3D pos_leader && pos->core.nr_memb= ers < 2) - unsorted_idx =3D pos->core.idx; + /* Remember an index to sort all forced grouped events together to. */ + if (force_grouped_idx =3D=3D -1 && pos =3D=3D pos_leader && pos->core.nr= _members < 2 && + arch_evsel__must_be_in_group(pos)) + force_grouped_idx =3D pos->core.idx; } =20 /* Sort events. */ - list_sort(&unsorted_idx, list, evlist__cmp); + list_sort(&force_grouped_idx, list, evlist__cmp); =20 /* * Recompute groups, splitting for PMUs and adding groups for events @@ -2182,7 +2194,8 @@ static int parse_events__sort_events_and_fix_groups(s= truct list_head *list) const struct evsel *pos_leader =3D evsel__leader(pos); const char *pos_pmu_name =3D pos->group_pmu_name; const char *cur_leader_pmu_name; - bool pos_force_grouped =3D arch_evsel__must_be_in_group(pos); + bool pos_force_grouped =3D force_grouped_idx !=3D -1 && + arch_evsel__must_be_in_group(pos); =20 /* Reset index and nr_members. */ if (pos->core.idx !=3D idx) --=20 2.41.0.487.g6d72f3e995-goog