Message ID | 20240213075256.1983638-4-namhyung@kernel.org |
---|---|
State | New |
Headers |
Return-Path: <linux-kernel+bounces-63064-ouuuleilei=gmail.com@vger.kernel.org> Delivered-To: ouuuleilei@gmail.com Received: by 2002:a05:7300:bc8a:b0:106:860b:bbdd with SMTP id dn10csp383902dyb; Mon, 12 Feb 2024 23:54:27 -0800 (PST) X-Google-Smtp-Source: AGHT+IEIBkZfjfOSC+4a0Edl23dIUdfpFiWtI2EoUORBLONy8fQNV3lBkxXPq14q/KRwYSJbm6U8 X-Received: by 2002:a17:906:b208:b0:a3c:bb37:a676 with SMTP id p8-20020a170906b20800b00a3cbb37a676mr1585066ejz.9.1707810866891; Mon, 12 Feb 2024 23:54:26 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1707810866; cv=pass; d=google.com; s=arc-20160816; b=URVv0KbmGrEeTAteRwvYeFwJhhxdGXDjLu08Nyrmd3+XM3YDLeNC0SsDAODVdFshAR XTx4oaVZm8kzffk9lts8AN/cQbMhgQ1jb1XZrU1SkSz0iEwmRNgFvCs8z8DhmlKWyuiS OFVAZ4PlAkq9+C04fYyLkJOIT/YdO4rsy0+6Wpop7+LR+aGcxz0AazAnFhohrVteDiF5 t5T8H7ufbKpEyWWhAqN1Swojys2qVJ7H/3NkUUvzUBP1E7HCZ42H9oTV9olj3TCAzjDQ i8GN4w34i1pjPSNPgMNa2w4S5QEX1+AqI4PvMmcev2yXGiV+BxJr0W0XBxE6ouRwvLud EudA== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=content-transfer-encoding:mime-version:list-unsubscribe :list-subscribe:list-id:precedence:references:in-reply-to:message-id :date:subject:cc:to:from:dkim-signature; bh=9SEbukrbA/HS3Yv+8R7Y5N1FpordbRB0wYABigTmuEE=; fh=g3Rr4Au5chWCIlZGOxuxly969VQqHHxllh9FJ0fxSLQ=; b=r2JLskwbrDZ63Ql5u+GwBo+CSaJWaeVkbeK8OZnpQtiTCUJq59KFNIO7eL6yLlUCmL rELwd0bemNjMYKwHOtooTMizWj0ZMb6T7GtKiaSxfH2TLBVenrkSkLn+V74w7ZzdOpKG dz3yoFv3FwMO5S3i++2cyX9AueThxuBNwyO78LGIHGyrCnXEViR3YqNQkKUjpm6jokxU oq/NF3cJSMsW0OArJX0OmjhZOOGGWtDP9X9p+snBbNDntdtpbqfGI6+0Z3XIsxGcdBl3 2lqyB8ar1elHVMqyayQkECwSm18IK/XVyULC6nE91TEqwBD4MCz1To7brjHuOU650tSK 8JdA==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=nnufl34i; arc=pass (i=1 dkim=pass dkdomain=kernel.org); spf=pass (google.com: domain of linux-kernel+bounces-63064-ouuuleilei=gmail.com@vger.kernel.org designates 147.75.80.249 as permitted sender) smtp.mailfrom="linux-kernel+bounces-63064-ouuuleilei=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org X-Forwarded-Encrypted: i=2; AJvYcCVsbcOfKLR8I9yJnIBmWrNL6ugNGHYe7bF4Ob+5XwXIZtb7ybeQFvf0Xm0xxGBEz90/pyovR2GsKt95UtXrEPG5ZvwzUA== Received: from am.mirrors.kernel.org (am.mirrors.kernel.org. [147.75.80.249]) by mx.google.com with ESMTPS id g26-20020a170906199a00b00a3cf4e76e06si575158ejd.446.2024.02.12.23.54.26 for <ouuuleilei@gmail.com> (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 12 Feb 2024 23:54:26 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-63064-ouuuleilei=gmail.com@vger.kernel.org designates 147.75.80.249 as permitted sender) client-ip=147.75.80.249; Authentication-Results: mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=nnufl34i; arc=pass (i=1 dkim=pass dkdomain=kernel.org); spf=pass (google.com: domain of linux-kernel+bounces-63064-ouuuleilei=gmail.com@vger.kernel.org designates 147.75.80.249 as permitted sender) smtp.mailfrom="linux-kernel+bounces-63064-ouuuleilei=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Received: from smtp.subspace.kernel.org (wormhole.subspace.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by am.mirrors.kernel.org (Postfix) with ESMTPS id 81FBC1F22957 for <ouuuleilei@gmail.com>; Tue, 13 Feb 2024 07:54:26 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id E9E5D224D6; Tue, 13 Feb 2024 07:53:01 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="nnufl34i" Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 4427618C1A; Tue, 13 Feb 2024 07:52:58 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1707810779; cv=none; b=KD5FNiac0OF0mScWSFCXr8pU8yLyGmsoD1vDJaOwUz/4LaDv0EGviS+O5vJpMbnngvQOULG7d9kha9YfzL6/oVjDnt48nlDbz/+UKuQbXK8SHetnK9sBRVl6sWaWUly3uT+Ymc3nYaxumvqAPW2lgt0pdDzanx4dqlKzkhifaVA= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1707810779; c=relaxed/simple; bh=/cAFvtSoMi2WFvvFOaWCdmF3lcgANJyG3cErhVuBZ3Q=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=G8ul/i2vhAcX4hpxsQuAnRBTsa8VipPNyHguDrC3noTZB5ppejSJ+0Sm/jVOvtCUL4ZY4Xyx9aRwg55vhGU1P+R0LjHtlZonhz+3LBNZOkZz+1lJRuwSYg9xFhubBahXL+RbYfP2m0cfxTGIOjnj4MM+CjbuIh5qtyMU0QCgO8w= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=nnufl34i; arc=none smtp.client-ip=10.30.226.201 Received: by smtp.kernel.org (Postfix) with ESMTPSA id 67A32C433C7; Tue, 13 Feb 2024 07:52:58 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1707810778; bh=/cAFvtSoMi2WFvvFOaWCdmF3lcgANJyG3cErhVuBZ3Q=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=nnufl34iNC3PJN5Ds6rRUdfaW5U9p2ah3WGrMWoIOVvx6meTPJyvRYbKJ2VaWyEpW JwENBqr0ykiQo0dWSIrefm/0Q3hCBelXViKWu6WnXs1/D+eQxTjCDrspxoXrf5EhhM hkT0uEhvSSnwieywqmxuphXVUYOrios9WcUb1q/hxbZx5KmVcJf1FVVuBL7NihNQp6 fYsmE51MaZ5UWNh3X63jbf74uJzpBmpXClyoZPS6grNXRnBwGCjhjqkr5zPfys4DoT ns/e68RUr2CY0OTxJ/F0Ospavzi4qyYapfM7i+OnslVuD/yU1rBP9yPRbnI3Oreubc snyzjTj+UcECQ== From: Namhyung Kim <namhyung@kernel.org> To: Arnaldo Carvalho de Melo <acme@kernel.org>, Ian Rogers <irogers@google.com> Cc: Jiri Olsa <jolsa@kernel.org>, Adrian Hunter <adrian.hunter@intel.com>, Peter Zijlstra <peterz@infradead.org>, Ingo Molnar <mingo@kernel.org>, LKML <linux-kernel@vger.kernel.org>, linux-perf-users@vger.kernel.org Subject: [PATCH 3/4] perf hist: Do not use event index in hpp__fmt() Date: Mon, 12 Feb 2024 23:52:55 -0800 Message-ID: <20240213075256.1983638-4-namhyung@kernel.org> X-Mailer: git-send-email 2.43.0.687.g38aa6559b0-goog In-Reply-To: <20240213075256.1983638-1-namhyung@kernel.org> References: <20240213075256.1983638-1-namhyung@kernel.org> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: <linux-kernel.vger.kernel.org> List-Subscribe: <mailto:linux-kernel+subscribe@vger.kernel.org> List-Unsubscribe: <mailto:linux-kernel+unsubscribe@vger.kernel.org> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1790769487809479703 X-GMAIL-MSGID: 1790769487809479703 |
Series |
perf report: Omit dummy events in the output (v1)
|
|
Commit Message
Namhyung Kim
Feb. 13, 2024, 7:52 a.m. UTC
The __hpp__fmt() is to print period values in a hist entry. It handles
event groups using linked pair entries. Until now, it used event index
to print values of group members. But we want to make it more robust
and support groups even if some members in the group were removed.
Let's use an index table from evsel to value array so that we can skip
dummy events in the output later.
Signed-off-by: Namhyung Kim <namhyung@kernel.org>
---
tools/perf/ui/hist.c | 34 ++++++++++++++++++++++++++++------
1 file changed, 28 insertions(+), 6 deletions(-)
Comments
On Mon, Feb 12, 2024 at 11:52 PM Namhyung Kim <namhyung@kernel.org> wrote: > > The __hpp__fmt() is to print period values in a hist entry. It handles > event groups using linked pair entries. Until now, it used event index > to print values of group members. But we want to make it more robust > and support groups even if some members in the group were removed. I'm unclear how it breaks currently. The evsel idx is set the evlist nr_entries on creation and not updated by a remove. A remove may change a groups leader, should the remove also make the next entry's index idx that of the previous group leader? > Let's use an index table from evsel to value array so that we can skip > dummy events in the output later. > > Signed-off-by: Namhyung Kim <namhyung@kernel.org> > --- > tools/perf/ui/hist.c | 34 ++++++++++++++++++++++++++++------ > 1 file changed, 28 insertions(+), 6 deletions(-) > > diff --git a/tools/perf/ui/hist.c b/tools/perf/ui/hist.c > index 5f4c110d840f..9c4c738edde1 100644 > --- a/tools/perf/ui/hist.c > +++ b/tools/perf/ui/hist.c > @@ -48,15 +48,30 @@ static int __hpp__fmt(struct perf_hpp *hpp, struct hist_entry *he, > if (evsel__is_group_event(evsel)) { > int idx; > struct hist_entry *pair; > - int nr_members = evsel->core.nr_members; > + int nr_members = evsel->core.nr_members - 1; A comment on the -1 would be useful. Thanks, Ian > union { > u64 period; > double percent; > } *val; > + struct evsel *member; > + struct evsel **idx_table; > > val = calloc(nr_members, sizeof(*val)); > if (val == NULL) > - return 0; > + goto out; > + > + idx_table = calloc(nr_members, sizeof(*idx_table)); > + if (idx_table == NULL) > + goto out; > + > + /* > + * Build an index table for each evsel to the val array. > + * It cannot use evsel->core.idx because removed events might > + * create a hole so the index is not consecutive anymore. > + */ > + idx = 0; > + for_each_group_member(member, evsel) > + idx_table[idx++] = member; > > /* collect values in the group members */ > list_for_each_entry(pair, &he->pairs.head, pairs.node) { > @@ -66,8 +81,15 @@ static int __hpp__fmt(struct perf_hpp *hpp, struct hist_entry *he, > if (!total) > continue; > > - evsel = hists_to_evsel(pair->hists); > - idx = evsel__group_idx(evsel); > + member = hists_to_evsel(pair->hists); > + for (idx = 0; idx < nr_members; idx++) { > + if (idx_table[idx] == member) > + break; > + } > + > + /* this should not happen */ > + if (idx == nr_members) > + continue; > > if (fmt_percent) > val[idx].percent = 100.0 * period / total; > @@ -75,8 +97,7 @@ static int __hpp__fmt(struct perf_hpp *hpp, struct hist_entry *he, > val[idx].period = period; > } > > - /* idx starts from 1 to skip the leader event */ > - for (idx = 1; idx < nr_members; idx++) { > + for (idx = 0; idx < nr_members; idx++) { > if (fmt_percent) { > ret += hpp__call_print_fn(hpp, print_fn, > fmt, len, val[idx].percent); > @@ -89,6 +110,7 @@ static int __hpp__fmt(struct perf_hpp *hpp, struct hist_entry *he, > free(val); > } > > +out: > /* > * Restore original buf and size as it's where caller expects > * the result will be saved. > -- > 2.43.0.687.g38aa6559b0-goog >
On Wed, Feb 14, 2024 at 4:08 PM Ian Rogers <irogers@google.com> wrote: > > On Mon, Feb 12, 2024 at 11:52 PM Namhyung Kim <namhyung@kernel.org> wrote: > > > > The __hpp__fmt() is to print period values in a hist entry. It handles > > event groups using linked pair entries. Until now, it used event index > > to print values of group members. But we want to make it more robust > > and support groups even if some members in the group were removed. > > I'm unclear how it breaks currently. The evsel idx is set the evlist > nr_entries on creation and not updated by a remove. A remove may > change a groups leader, should the remove also make the next entry's > index idx that of the previous group leader? The evsel__group_idx() returns evsel->idx - leader->idx. If it has a group event {A, B, C} then the index would be 0, 1, 2. If it removes B, the group would be {A, C} with index 0 and 2. The nr_members is 2 now so it cannot use index 2 for C. Note that we cannot change the index of C because some information like annotation histogram relies on the index. > > > Let's use an index table from evsel to value array so that we can skip > > dummy events in the output later. > > > > Signed-off-by: Namhyung Kim <namhyung@kernel.org> > > --- > > tools/perf/ui/hist.c | 34 ++++++++++++++++++++++++++++------ > > 1 file changed, 28 insertions(+), 6 deletions(-) > > > > diff --git a/tools/perf/ui/hist.c b/tools/perf/ui/hist.c > > index 5f4c110d840f..9c4c738edde1 100644 > > --- a/tools/perf/ui/hist.c > > +++ b/tools/perf/ui/hist.c > > @@ -48,15 +48,30 @@ static int __hpp__fmt(struct perf_hpp *hpp, struct hist_entry *he, > > if (evsel__is_group_event(evsel)) { > > int idx; > > struct hist_entry *pair; > > - int nr_members = evsel->core.nr_members; > > + int nr_members = evsel->core.nr_members - 1; > > A comment on the -1 would be useful. The 'nr_members' includes the leader which is already printed. In this code, we want to print member events only, hence -1. I'll add it to the comment. Thanks, Namhyung > > Thanks, > Ian > > > > union { > > u64 period; > > double percent; > > } *val; > > + struct evsel *member; > > + struct evsel **idx_table; > > > > val = calloc(nr_members, sizeof(*val)); > > if (val == NULL) > > - return 0; > > + goto out; > > + > > + idx_table = calloc(nr_members, sizeof(*idx_table)); > > + if (idx_table == NULL) > > + goto out; > > + > > + /* > > + * Build an index table for each evsel to the val array. > > + * It cannot use evsel->core.idx because removed events might > > + * create a hole so the index is not consecutive anymore. > > + */ > > + idx = 0; > > + for_each_group_member(member, evsel) > > + idx_table[idx++] = member; > > > > /* collect values in the group members */ > > list_for_each_entry(pair, &he->pairs.head, pairs.node) { > > @@ -66,8 +81,15 @@ static int __hpp__fmt(struct perf_hpp *hpp, struct hist_entry *he, > > if (!total) > > continue; > > > > - evsel = hists_to_evsel(pair->hists); > > - idx = evsel__group_idx(evsel); > > + member = hists_to_evsel(pair->hists); > > + for (idx = 0; idx < nr_members; idx++) { > > + if (idx_table[idx] == member) > > + break; > > + } > > + > > + /* this should not happen */ > > + if (idx == nr_members) > > + continue; > > > > if (fmt_percent) > > val[idx].percent = 100.0 * period / total; > > @@ -75,8 +97,7 @@ static int __hpp__fmt(struct perf_hpp *hpp, struct hist_entry *he, > > val[idx].period = period; > > } > > > > - /* idx starts from 1 to skip the leader event */ > > - for (idx = 1; idx < nr_members; idx++) { > > + for (idx = 0; idx < nr_members; idx++) { > > if (fmt_percent) { > > ret += hpp__call_print_fn(hpp, print_fn, > > fmt, len, val[idx].percent); > > @@ -89,6 +110,7 @@ static int __hpp__fmt(struct perf_hpp *hpp, struct hist_entry *he, > > free(val); > > } > > > > +out: > > /* > > * Restore original buf and size as it's where caller expects > > * the result will be saved. > > -- > > 2.43.0.687.g38aa6559b0-goog > >
On Wed, Feb 14, 2024 at 9:26 PM Namhyung Kim <namhyung@kernel.org> wrote: > > On Wed, Feb 14, 2024 at 4:08 PM Ian Rogers <irogers@google.com> wrote: > > > > On Mon, Feb 12, 2024 at 11:52 PM Namhyung Kim <namhyung@kernel.org> wrote: > > > > > > The __hpp__fmt() is to print period values in a hist entry. It handles > > > event groups using linked pair entries. Until now, it used event index > > > to print values of group members. But we want to make it more robust > > > and support groups even if some members in the group were removed. > > > > I'm unclear how it breaks currently. The evsel idx is set the evlist > > nr_entries on creation and not updated by a remove. A remove may > > change a groups leader, should the remove also make the next entry's > > index idx that of the previous group leader? > > The evsel__group_idx() returns evsel->idx - leader->idx. > If it has a group event {A, B, C} then the index would be 0, 1, 2. > If it removes B, the group would be {A, C} with index 0 and 2. > The nr_members is 2 now so it cannot use index 2 for C. > > Note that we cannot change the index of C because some information > like annotation histogram relies on the index. Ugh, the whole index thing is just messy - perhaps these days we could have a hashmap with the evsel as a key instead. I remember that I also forced the idx here: https://git.kernel.org/pub/scm/linux/kernel/git/perf/perf-tools-next.git/tree/tools/perf/util/parse-events.c?h=perf-tools-next#n2049 If it were invariant that the idx were always the position of an event in the evlist then I think life would be easier, but that won't help for the arrays of counters that need the index to be constant. I guess this is why the previous work to do this skipped evsels rather than removed them. Thanks, Ian > > > > > Let's use an index table from evsel to value array so that we can skip > > > dummy events in the output later. > > > > > > Signed-off-by: Namhyung Kim <namhyung@kernel.org> > > > --- > > > tools/perf/ui/hist.c | 34 ++++++++++++++++++++++++++++------ > > > 1 file changed, 28 insertions(+), 6 deletions(-) > > > > > > diff --git a/tools/perf/ui/hist.c b/tools/perf/ui/hist.c > > > index 5f4c110d840f..9c4c738edde1 100644 > > > --- a/tools/perf/ui/hist.c > > > +++ b/tools/perf/ui/hist.c > > > @@ -48,15 +48,30 @@ static int __hpp__fmt(struct perf_hpp *hpp, struct hist_entry *he, > > > if (evsel__is_group_event(evsel)) { > > > int idx; > > > struct hist_entry *pair; > > > - int nr_members = evsel->core.nr_members; > > > + int nr_members = evsel->core.nr_members - 1; > > > > A comment on the -1 would be useful. > > The 'nr_members' includes the leader which is already printed. > In this code, we want to print member events only, hence -1. > I'll add it to the comment. > > Thanks, > Namhyung > > > > > Thanks, > > Ian > > > > > > > union { > > > u64 period; > > > double percent; > > > } *val; > > > + struct evsel *member; > > > + struct evsel **idx_table; > > > > > > val = calloc(nr_members, sizeof(*val)); > > > if (val == NULL) > > > - return 0; > > > + goto out; > > > + > > > + idx_table = calloc(nr_members, sizeof(*idx_table)); > > > + if (idx_table == NULL) > > > + goto out; > > > + > > > + /* > > > + * Build an index table for each evsel to the val array. > > > + * It cannot use evsel->core.idx because removed events might > > > + * create a hole so the index is not consecutive anymore. > > > + */ > > > + idx = 0; > > > + for_each_group_member(member, evsel) > > > + idx_table[idx++] = member; > > > > > > /* collect values in the group members */ > > > list_for_each_entry(pair, &he->pairs.head, pairs.node) { > > > @@ -66,8 +81,15 @@ static int __hpp__fmt(struct perf_hpp *hpp, struct hist_entry *he, > > > if (!total) > > > continue; > > > > > > - evsel = hists_to_evsel(pair->hists); > > > - idx = evsel__group_idx(evsel); > > > + member = hists_to_evsel(pair->hists); > > > + for (idx = 0; idx < nr_members; idx++) { > > > + if (idx_table[idx] == member) > > > + break; > > > + } > > > + > > > + /* this should not happen */ > > > + if (idx == nr_members) > > > + continue; > > > > > > if (fmt_percent) > > > val[idx].percent = 100.0 * period / total; > > > @@ -75,8 +97,7 @@ static int __hpp__fmt(struct perf_hpp *hpp, struct hist_entry *he, > > > val[idx].period = period; > > > } > > > > > > - /* idx starts from 1 to skip the leader event */ > > > - for (idx = 1; idx < nr_members; idx++) { > > > + for (idx = 0; idx < nr_members; idx++) { > > > if (fmt_percent) { > > > ret += hpp__call_print_fn(hpp, print_fn, > > > fmt, len, val[idx].percent); > > > @@ -89,6 +110,7 @@ static int __hpp__fmt(struct perf_hpp *hpp, struct hist_entry *he, > > > free(val); > > > } > > > > > > +out: > > > /* > > > * Restore original buf and size as it's where caller expects > > > * the result will be saved. > > > -- > > > 2.43.0.687.g38aa6559b0-goog > > >
On Wed, Feb 14, 2024 at 11:54 PM Ian Rogers <irogers@google.com> wrote: > > On Wed, Feb 14, 2024 at 9:26 PM Namhyung Kim <namhyung@kernel.org> wrote: > > > > On Wed, Feb 14, 2024 at 4:08 PM Ian Rogers <irogers@google.com> wrote: > > > > > > On Mon, Feb 12, 2024 at 11:52 PM Namhyung Kim <namhyung@kernel.org> wrote: > > > > > > > > The __hpp__fmt() is to print period values in a hist entry. It handles > > > > event groups using linked pair entries. Until now, it used event index > > > > to print values of group members. But we want to make it more robust > > > > and support groups even if some members in the group were removed. > > > > > > I'm unclear how it breaks currently. The evsel idx is set the evlist > > > nr_entries on creation and not updated by a remove. A remove may > > > change a groups leader, should the remove also make the next entry's > > > index idx that of the previous group leader? > > > > The evsel__group_idx() returns evsel->idx - leader->idx. > > If it has a group event {A, B, C} then the index would be 0, 1, 2. > > If it removes B, the group would be {A, C} with index 0 and 2. > > The nr_members is 2 now so it cannot use index 2 for C. > > > > Note that we cannot change the index of C because some information > > like annotation histogram relies on the index. > > Ugh, the whole index thing is just messy - perhaps these days we could > have a hashmap with the evsel as a key instead. I remember that I also > forced the idx here: > https://git.kernel.org/pub/scm/linux/kernel/git/perf/perf-tools-next.git/tree/tools/perf/util/parse-events.c?h=perf-tools-next#n2049 > If it were invariant that the idx were always the position of an event > in the evlist then I think life would be easier, but that won't help > for the arrays of counters that need the index to be constant. I guess > this is why the previous work to do this skipped evsels rather than > removed them. Actually I have a patch series to convert the annotation histogram to a hash map. It'd reduce memory usage as well. Will post. I think removing evsel is not a common operation and should be done with care. In this patchset, it removed (dummy) events after processing all samples. I can make the code to skip those event when printing the result but it'd be much easier if it can remove the unnecessary events. Thanks, Namhyung
diff --git a/tools/perf/ui/hist.c b/tools/perf/ui/hist.c index 5f4c110d840f..9c4c738edde1 100644 --- a/tools/perf/ui/hist.c +++ b/tools/perf/ui/hist.c @@ -48,15 +48,30 @@ static int __hpp__fmt(struct perf_hpp *hpp, struct hist_entry *he, if (evsel__is_group_event(evsel)) { int idx; struct hist_entry *pair; - int nr_members = evsel->core.nr_members; + int nr_members = evsel->core.nr_members - 1; union { u64 period; double percent; } *val; + struct evsel *member; + struct evsel **idx_table; val = calloc(nr_members, sizeof(*val)); if (val == NULL) - return 0; + goto out; + + idx_table = calloc(nr_members, sizeof(*idx_table)); + if (idx_table == NULL) + goto out; + + /* + * Build an index table for each evsel to the val array. + * It cannot use evsel->core.idx because removed events might + * create a hole so the index is not consecutive anymore. + */ + idx = 0; + for_each_group_member(member, evsel) + idx_table[idx++] = member; /* collect values in the group members */ list_for_each_entry(pair, &he->pairs.head, pairs.node) { @@ -66,8 +81,15 @@ static int __hpp__fmt(struct perf_hpp *hpp, struct hist_entry *he, if (!total) continue; - evsel = hists_to_evsel(pair->hists); - idx = evsel__group_idx(evsel); + member = hists_to_evsel(pair->hists); + for (idx = 0; idx < nr_members; idx++) { + if (idx_table[idx] == member) + break; + } + + /* this should not happen */ + if (idx == nr_members) + continue; if (fmt_percent) val[idx].percent = 100.0 * period / total; @@ -75,8 +97,7 @@ static int __hpp__fmt(struct perf_hpp *hpp, struct hist_entry *he, val[idx].period = period; } - /* idx starts from 1 to skip the leader event */ - for (idx = 1; idx < nr_members; idx++) { + for (idx = 0; idx < nr_members; idx++) { if (fmt_percent) { ret += hpp__call_print_fn(hpp, print_fn, fmt, len, val[idx].percent); @@ -89,6 +110,7 @@ static int __hpp__fmt(struct perf_hpp *hpp, struct hist_entry *he, free(val); } +out: /* * Restore original buf and size as it's where caller expects * the result will be saved.