[1/4] libperf evlist: Update group info in perf_evlist__remove()

Message ID 20240213075256.1983638-2-namhyung@kernel.org
State New
Headers
Series perf report: Omit dummy events in the output (v1) |

Commit Message

Namhyung Kim Feb. 13, 2024, 7:52 a.m. UTC
  When an event in a group is removed, it should update the group status
including the pointer to leader and number of member events.

Signed-off-by: Namhyung Kim <namhyung@kernel.org>
---
 tools/lib/perf/evlist.c | 21 +++++++++++++++++++++
 1 file changed, 21 insertions(+)
  

Comments

Ian Rogers Feb. 14, 2024, 11:50 p.m. UTC | #1
On Mon, Feb 12, 2024 at 11:52 PM Namhyung Kim <namhyung@kernel.org> wrote:
>
> When an event in a group is removed, it should update the group status
> including the pointer to leader and number of member events.
>
> Signed-off-by: Namhyung Kim <namhyung@kernel.org>

Should we worry about evlist's all_cpus that could also be stale now?

Thanks,
Ian

> ---
>  tools/lib/perf/evlist.c | 21 +++++++++++++++++++++
>  1 file changed, 21 insertions(+)
>
> diff --git a/tools/lib/perf/evlist.c b/tools/lib/perf/evlist.c
> index 058e3ff10f9b..befdb062fa1d 100644
> --- a/tools/lib/perf/evlist.c
> +++ b/tools/lib/perf/evlist.c
> @@ -102,8 +102,29 @@ void perf_evlist__add(struct perf_evlist *evlist,
>  void perf_evlist__remove(struct perf_evlist *evlist,
>                          struct perf_evsel *evsel)
>  {
> +       struct perf_evsel *leader = evsel->leader;
> +
>         list_del_init(&evsel->node);
>         evlist->nr_entries -= 1;
> +
> +       /* return stand-alone event */
> +       if (leader == evsel && leader->nr_members < 2)
> +               return;
> +
> +       if (leader == evsel) {
> +               struct perf_evsel *member;
> +
> +               /* select the next event as a new leader */
> +               leader = member = perf_evlist__next(evlist, evsel);
> +
> +               /* update members to see the new leader */
> +               while (member && member->leader == evsel) {
> +                       member->leader = leader;
> +                       member = perf_evlist__next(evlist, member);
> +               }
> +       }
> +
> +       leader->nr_members = evsel->leader->nr_members - 1;
>  }
>
>  struct perf_evlist *perf_evlist__new(void)
> --
> 2.43.0.687.g38aa6559b0-goog
>
  
Namhyung Kim Feb. 15, 2024, 5:16 a.m. UTC | #2
On Wed, Feb 14, 2024 at 3:50 PM Ian Rogers <irogers@google.com> wrote:
>
> On Mon, Feb 12, 2024 at 11:52 PM Namhyung Kim <namhyung@kernel.org> wrote:
> >
> > When an event in a group is removed, it should update the group status
> > including the pointer to leader and number of member events.
> >
> > Signed-off-by: Namhyung Kim <namhyung@kernel.org>
>
> Should we worry about evlist's all_cpus that could also be stale now?

Hmm.. right.  I will refresh the cpu list after removal.

Thanks,
Namhyung
  

Patch

diff --git a/tools/lib/perf/evlist.c b/tools/lib/perf/evlist.c
index 058e3ff10f9b..befdb062fa1d 100644
--- a/tools/lib/perf/evlist.c
+++ b/tools/lib/perf/evlist.c
@@ -102,8 +102,29 @@  void perf_evlist__add(struct perf_evlist *evlist,
 void perf_evlist__remove(struct perf_evlist *evlist,
 			 struct perf_evsel *evsel)
 {
+	struct perf_evsel *leader = evsel->leader;
+
 	list_del_init(&evsel->node);
 	evlist->nr_entries -= 1;
+
+	/* return stand-alone event */
+	if (leader == evsel && leader->nr_members < 2)
+		return;
+
+	if (leader == evsel) {
+		struct perf_evsel *member;
+
+		/* select the next event as a new leader */
+		leader = member = perf_evlist__next(evlist, evsel);
+
+		/* update members to see the new leader */
+		while (member && member->leader == evsel) {
+			member->leader = leader;
+			member = perf_evlist__next(evlist, member);
+		}
+	}
+
+	leader->nr_members = evsel->leader->nr_members - 1;
 }
 
 struct perf_evlist *perf_evlist__new(void)