linux-perf-users.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Ian Rogers <irogers@google.com>
To: James Clark <james.clark@linaro.org>
Cc: linux-perf-users@vger.kernel.org, namhyung@kernel.org,
	 thomas.falcon@intel.com, Peter Zijlstra <peterz@infradead.org>,
	 Ingo Molnar <mingo@redhat.com>,
	Arnaldo Carvalho de Melo <acme@kernel.org>,
	Mark Rutland <mark.rutland@arm.com>,
	 Alexander Shishkin <alexander.shishkin@linux.intel.com>,
	Jiri Olsa <jolsa@kernel.org>,
	 Adrian Hunter <adrian.hunter@intel.com>,
	"Liang, Kan" <kan.liang@linux.intel.com>,
	 linux-kernel@vger.kernel.org
Subject: Re: [PATCH v2 2/2] libperf: evlist: Keep evsel idx contiguous on removal
Date: Thu, 14 Nov 2024 15:35:35 -0800	[thread overview]
Message-ID: <CAP-5=fVdNov38srPj-ax-PHtnMBhGT+y+fp+NJHGFS0dxRchew@mail.gmail.com> (raw)
In-Reply-To: <20241114160450.295844-3-james.clark@linaro.org>

On Thu, Nov 14, 2024 at 8:07 AM James Clark <james.clark@linaro.org> wrote:
>
> The only two places where evsels are removed end up fixing up the index.
> Move it into libperf so that it's always done.
>
> Signed-off-by: James Clark <james.clark@linaro.org>

Reviewed-by: Ian Rogers <irogers@google.com>

but I was also reminded that the evsel idx is just a meaningless int
and we should stop trying to make it special, so I sent out some clean
up:
https://lore.kernel.org/lkml/20241114230713.330701-1-irogers@google.com/

You may get asked by the maintainers to separate the test from the
main logic to make patches with fewer lines of code ¯\_(ツ)_/¯

Thanks,
Ian

> ---
>  tools/lib/perf/evlist.c            | 17 +++++++------
>  tools/lib/perf/tests/test-evlist.c | 41 ++++++++++++++++++++++++++++++
>  tools/perf/builtin-record.c        | 18 +++----------
>  3 files changed, 53 insertions(+), 23 deletions(-)
>
> diff --git a/tools/lib/perf/evlist.c b/tools/lib/perf/evlist.c
> index 83c43dc13313..ffa1a28fb14f 100644
> --- a/tools/lib/perf/evlist.c
> +++ b/tools/lib/perf/evlist.c
> @@ -52,15 +52,8 @@ static void __perf_evlist__propagate_maps(struct perf_evlist *evlist,
>                  * Empty cpu lists would eventually get opened as "any" so remove
>                  * genuinely empty ones before they're opened in the wrong place.
>                  */
> -               if (perf_cpu_map__is_empty(evsel->cpus)) {
> -                       struct perf_evsel *next = perf_evlist__next(evlist, evsel);
> -
> +               if (perf_cpu_map__is_empty(evsel->cpus))
>                         perf_evlist__remove(evlist, evsel);
> -                       /* Keep idx contiguous */
> -                       if (next)
> -                               list_for_each_entry_from(next, &evlist->entries, node)
> -                                       next->idx--;
> -               }
>         } else if (!evsel->own_cpus || evlist->has_user_cpus ||
>                 (!evsel->requires_cpu && perf_cpu_map__has_any_cpu(evlist->user_requested_cpus))) {
>                 /*
> @@ -116,8 +109,16 @@ void perf_evlist__add(struct perf_evlist *evlist,
>  void perf_evlist__remove(struct perf_evlist *evlist,
>                          struct perf_evsel *evsel)
>  {
> +       struct perf_evsel *next = perf_evlist__next(evlist, evsel);
> +
>         list_del_init(&evsel->node);
>         evlist->nr_entries -= 1;
> +
> +       /* Keep idx contiguous */
> +       if (!next)
> +               return;
> +       list_for_each_entry_from(next, &evlist->entries, node)
> +               next->idx--;
>  }
>
>  struct perf_evlist *perf_evlist__new(void)
> diff --git a/tools/lib/perf/tests/test-evlist.c b/tools/lib/perf/tests/test-evlist.c
> index 10f70cb41ff1..06153820f408 100644
> --- a/tools/lib/perf/tests/test-evlist.c
> +++ b/tools/lib/perf/tests/test-evlist.c
> @@ -571,6 +571,46 @@ static int test_stat_multiplexing(void)
>         return 0;
>  }
>
> +static int test_list_remove(void)
> +{
> +       struct perf_evlist *evlist;
> +       struct perf_evsel *evsel, *evsel0, *evsel1, *evsel2;
> +       struct perf_event_attr attr = {};
> +       int idx = 0;
> +
> +       evlist = perf_evlist__new();
> +       __T("failed to create evlist", evlist);
> +
> +       evsel0 = perf_evsel__new(&attr);
> +       __T("failed to create evsel", evsel0);
> +       evsel1 = perf_evsel__new(&attr);
> +       __T("failed to create evsel", evsel1);
> +       evsel2 = perf_evsel__new(&attr);
> +       __T("failed to create evsel", evsel2);
> +
> +       perf_evlist__add(evlist, evsel0);
> +       perf_evlist__add(evlist, evsel1);
> +       perf_evlist__add(evlist, evsel2);
> +
> +       idx = 0;
> +       perf_evlist__for_each_evsel(evlist, evsel)
> +               __T("idx isn't contiguous", evsel->idx == idx++);
> +
> +       /* Test removing middle */
> +       perf_evlist__remove(evlist, evsel1);
> +       idx = 0;
> +       perf_evlist__for_each_evsel(evlist, evsel)
> +               __T("idx isn't contiguous", evsel->idx == idx++);
> +
> +       /* Test removing end */
> +       perf_evlist__remove(evlist, evsel2);
> +       idx = 0;
> +       perf_evlist__for_each_evsel(evlist, evsel)
> +               __T("idx isn't contiguous", evsel->idx == idx++);
> +
> +       return 0;
> +}
> +
>  int test_evlist(int argc, char **argv)
>  {
>         __T_START;
> @@ -583,6 +623,7 @@ int test_evlist(int argc, char **argv)
>         test_mmap_thread();
>         test_mmap_cpus();
>         test_stat_multiplexing();
> +       test_list_remove();
>
>         __T_END;
>         return tests_failed == 0 ? 0 : -1;
> diff --git a/tools/perf/builtin-record.c b/tools/perf/builtin-record.c
> index 7e99743f7e42..2ebbb0fd0064 100644
> --- a/tools/perf/builtin-record.c
> +++ b/tools/perf/builtin-record.c
> @@ -1359,14 +1359,13 @@ static int record__mmap(struct record *rec)
>  static int record__open(struct record *rec)
>  {
>         char msg[BUFSIZ];
> -       struct evsel *pos;
> +       struct evsel *pos, *tmp;
>         struct evlist *evlist = rec->evlist;
>         struct perf_session *session = rec->session;
>         struct record_opts *opts = &rec->opts;
>         int rc = 0;
> -       bool skipped = false;
>
> -       evlist__for_each_entry(evlist, pos) {
> +       evlist__for_each_entry_safe(evlist, tmp, pos) {
>  try_again:
>                 if (evsel__open(pos, pos->core.cpus, pos->core.threads) < 0) {
>                         if (evsel__fallback(pos, &opts->target, errno, msg, sizeof(msg))) {
> @@ -1383,23 +1382,12 @@ static int record__open(struct record *rec)
>                         evsel__open_strerror(pos, &opts->target, errno, msg, sizeof(msg));
>                         ui__error("Failure to open event '%s' on PMU '%s' which will be removed.\n%s\n",
>                                   evsel__name(pos), evsel__pmu_name(pos), msg);
> -                       pos->skippable = true;
> -                       skipped = true;
> +                       evlist__remove(evlist, pos);
>                 } else {
>                         pos->supported = true;
>                 }
>         }
>
> -       if (skipped) {
> -               struct evsel *tmp;
> -               int idx = 0;
> -
> -               evlist__for_each_entry_safe(evlist, tmp, pos) {
> -                       if (pos->skippable)
> -                               evlist__remove(evlist, pos);
> -                       pos->core.idx = idx++;
> -               }
> -       }
>         if (symbol_conf.kptr_restrict && !evlist__exclude_kernel(evlist)) {
>                 pr_warning(
>  "WARNING: Kernel address maps (/proc/{kallsyms,modules}) are restricted,\n"
> --
> 2.34.1
>

      reply	other threads:[~2024-11-14 23:35 UTC|newest]

Thread overview: 9+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-11-14 16:04 [PATCH v2 0/2] libperf: evlist: Fix --cpu argument on hybrid platform James Clark
2024-11-14 16:04 ` [PATCH v2 1/2] " James Clark
2024-12-10 13:51   ` Arnaldo Carvalho de Melo
2024-12-10 13:56     ` James Clark
2024-12-10 14:07       ` Arnaldo Carvalho de Melo
2024-12-10 18:10         ` Namhyung Kim
2024-12-12  7:10           ` Namhyung Kim
2024-11-14 16:04 ` [PATCH v2 2/2] libperf: evlist: Keep evsel idx contiguous on removal James Clark
2024-11-14 23:35   ` Ian Rogers [this message]

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to='CAP-5=fVdNov38srPj-ax-PHtnMBhGT+y+fp+NJHGFS0dxRchew@mail.gmail.com' \
    --to=irogers@google.com \
    --cc=acme@kernel.org \
    --cc=adrian.hunter@intel.com \
    --cc=alexander.shishkin@linux.intel.com \
    --cc=james.clark@linaro.org \
    --cc=jolsa@kernel.org \
    --cc=kan.liang@linux.intel.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-perf-users@vger.kernel.org \
    --cc=mark.rutland@arm.com \
    --cc=mingo@redhat.com \
    --cc=namhyung@kernel.org \
    --cc=peterz@infradead.org \
    --cc=thomas.falcon@intel.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).