[v2,05/10] perf evsel: Allow const evsel for certain accesses

Message ID 20230302212531.1043318-6-irogers@google.com
State New
Headers
Series Better fixes for grouping of events |

Commit Message

Ian Rogers March 2, 2023, 9:25 p.m. UTC
  List sorting, added later to evlist, passes const elements requiring
helper functions to also be const. Make the argument to
evsel__find_pmu, evsel__is_aux_event and evsel__leader const.

Signed-off-by: Ian Rogers <irogers@google.com>
---
 tools/perf/util/evsel.c  | 2 +-
 tools/perf/util/evsel.h  | 6 +++---
 tools/perf/util/pmu.c    | 6 +++---
 tools/perf/util/python.c | 2 +-
 4 files changed, 8 insertions(+), 8 deletions(-)
  

Comments

Namhyung Kim March 3, 2023, 12:14 a.m. UTC | #1
Hi Ian,

On Thu, Mar 2, 2023 at 1:26 PM Ian Rogers <irogers@google.com> wrote:
>
> List sorting, added later to evlist, passes const elements requiring
> helper functions to also be const. Make the argument to
> evsel__find_pmu, evsel__is_aux_event and evsel__leader const.
>
> Signed-off-by: Ian Rogers <irogers@google.com>
> ---
>  tools/perf/util/evsel.c  | 2 +-
>  tools/perf/util/evsel.h  | 6 +++---
>  tools/perf/util/pmu.c    | 6 +++---
>  tools/perf/util/python.c | 2 +-
>  4 files changed, 8 insertions(+), 8 deletions(-)
>
> diff --git a/tools/perf/util/evsel.c b/tools/perf/util/evsel.c
> index 51e8ce6edddc..2dc2c24252bb 100644
> --- a/tools/perf/util/evsel.c
> +++ b/tools/perf/util/evsel.c
> @@ -3139,7 +3139,7 @@ bool evsel__is_hybrid(const struct evsel *evsel)
>         return evsel->pmu_name && perf_pmu__is_hybrid(evsel->pmu_name);
>  }
>
> -struct evsel *evsel__leader(struct evsel *evsel)
> +struct evsel *evsel__leader(const struct evsel *evsel)

It seems like it should return const struct evsel *, right?

>  {
>         return container_of(evsel->core.leader, struct evsel, core);
>  }
> diff --git a/tools/perf/util/evsel.h b/tools/perf/util/evsel.h
> index 814a49ebb7e3..676c499323e9 100644
> --- a/tools/perf/util/evsel.h
> +++ b/tools/perf/util/evsel.h
> @@ -212,8 +212,8 @@ int evsel__object_config(size_t object_size,
>                          int (*init)(struct evsel *evsel),
>                          void (*fini)(struct evsel *evsel));
>
> -struct perf_pmu *evsel__find_pmu(struct evsel *evsel);
> -bool evsel__is_aux_event(struct evsel *evsel);
> +struct perf_pmu *evsel__find_pmu(const struct evsel *evsel);
> +bool evsel__is_aux_event(const struct evsel *evsel);
>
>  struct evsel *evsel__new_idx(struct perf_event_attr *attr, int idx);
>
> @@ -505,7 +505,7 @@ int evsel__store_ids(struct evsel *evsel, struct evlist *evlist);
>
>  void evsel__zero_per_pkg(struct evsel *evsel);
>  bool evsel__is_hybrid(const struct evsel *evsel);
> -struct evsel *evsel__leader(struct evsel *evsel);
> +struct evsel *evsel__leader(const struct evsel *evsel);
>  bool evsel__has_leader(struct evsel *evsel, struct evsel *leader);
>  bool evsel__is_leader(struct evsel *evsel);
>  void evsel__set_leader(struct evsel *evsel, struct evsel *leader);
> diff --git a/tools/perf/util/pmu.c b/tools/perf/util/pmu.c
> index 43b6182d96b7..45d9b8e28e16 100644
> --- a/tools/perf/util/pmu.c
> +++ b/tools/perf/util/pmu.c
> @@ -988,7 +988,7 @@ struct perf_pmu *perf_pmu__scan(struct perf_pmu *pmu)
>         return NULL;
>  }
>
> -struct perf_pmu *evsel__find_pmu(struct evsel *evsel)
> +struct perf_pmu *evsel__find_pmu(const struct evsel *evsel)
>  {
>         struct perf_pmu *pmu = NULL;
>
> @@ -1000,11 +1000,11 @@ struct perf_pmu *evsel__find_pmu(struct evsel *evsel)
>                         break;
>         }
>
> -       evsel->pmu = pmu;
> +       ((struct evsel *)evsel)->pmu = pmu;

Maybe we can move it out of the function and make callers set it.

Thanks,
Namhyung


>         return pmu;
>  }
>
> -bool evsel__is_aux_event(struct evsel *evsel)
> +bool evsel__is_aux_event(const struct evsel *evsel)
>  {
>         struct perf_pmu *pmu = evsel__find_pmu(evsel);
>
> diff --git a/tools/perf/util/python.c b/tools/perf/util/python.c
> index 42e8b813d010..ab48ffbb6448 100644
> --- a/tools/perf/util/python.c
> +++ b/tools/perf/util/python.c
> @@ -83,7 +83,7 @@ const char *perf_env__arch(struct perf_env *env __maybe_unused)
>   * far, for the perf python binding known usecases, revisit if this become
>   * necessary.
>   */
> -struct perf_pmu *evsel__find_pmu(struct evsel *evsel __maybe_unused)
> +struct perf_pmu *evsel__find_pmu(const struct evsel *evsel __maybe_unused)
>  {
>         return NULL;
>  }
> --
> 2.40.0.rc0.216.gc4246ad0f0-goog
>
  
Ian Rogers March 3, 2023, 1:40 a.m. UTC | #2
On Thu, Mar 2, 2023 at 4:14 PM Namhyung Kim <namhyung@kernel.org> wrote:
>
> Hi Ian,
>
> On Thu, Mar 2, 2023 at 1:26 PM Ian Rogers <irogers@google.com> wrote:
> >
> > List sorting, added later to evlist, passes const elements requiring
> > helper functions to also be const. Make the argument to
> > evsel__find_pmu, evsel__is_aux_event and evsel__leader const.
> >
> > Signed-off-by: Ian Rogers <irogers@google.com>
> > ---
> >  tools/perf/util/evsel.c  | 2 +-
> >  tools/perf/util/evsel.h  | 6 +++---
> >  tools/perf/util/pmu.c    | 6 +++---
> >  tools/perf/util/python.c | 2 +-
> >  4 files changed, 8 insertions(+), 8 deletions(-)
> >
> > diff --git a/tools/perf/util/evsel.c b/tools/perf/util/evsel.c
> > index 51e8ce6edddc..2dc2c24252bb 100644
> > --- a/tools/perf/util/evsel.c
> > +++ b/tools/perf/util/evsel.c
> > @@ -3139,7 +3139,7 @@ bool evsel__is_hybrid(const struct evsel *evsel)
> >         return evsel->pmu_name && perf_pmu__is_hybrid(evsel->pmu_name);
> >  }
> >
> > -struct evsel *evsel__leader(struct evsel *evsel)
> > +struct evsel *evsel__leader(const struct evsel *evsel)
>
> It seems like it should return const struct evsel *, right?

Yes, but that breaks things :-)

> >  {
> >         return container_of(evsel->core.leader, struct evsel, core);
> >  }
> > diff --git a/tools/perf/util/evsel.h b/tools/perf/util/evsel.h
> > index 814a49ebb7e3..676c499323e9 100644
> > --- a/tools/perf/util/evsel.h
> > +++ b/tools/perf/util/evsel.h
> > @@ -212,8 +212,8 @@ int evsel__object_config(size_t object_size,
> >                          int (*init)(struct evsel *evsel),
> >                          void (*fini)(struct evsel *evsel));
> >
> > -struct perf_pmu *evsel__find_pmu(struct evsel *evsel);
> > -bool evsel__is_aux_event(struct evsel *evsel);
> > +struct perf_pmu *evsel__find_pmu(const struct evsel *evsel);
> > +bool evsel__is_aux_event(const struct evsel *evsel);
> >
> >  struct evsel *evsel__new_idx(struct perf_event_attr *attr, int idx);
> >
> > @@ -505,7 +505,7 @@ int evsel__store_ids(struct evsel *evsel, struct evlist *evlist);
> >
> >  void evsel__zero_per_pkg(struct evsel *evsel);
> >  bool evsel__is_hybrid(const struct evsel *evsel);
> > -struct evsel *evsel__leader(struct evsel *evsel);
> > +struct evsel *evsel__leader(const struct evsel *evsel);
> >  bool evsel__has_leader(struct evsel *evsel, struct evsel *leader);
> >  bool evsel__is_leader(struct evsel *evsel);
> >  void evsel__set_leader(struct evsel *evsel, struct evsel *leader);
> > diff --git a/tools/perf/util/pmu.c b/tools/perf/util/pmu.c
> > index 43b6182d96b7..45d9b8e28e16 100644
> > --- a/tools/perf/util/pmu.c
> > +++ b/tools/perf/util/pmu.c
> > @@ -988,7 +988,7 @@ struct perf_pmu *perf_pmu__scan(struct perf_pmu *pmu)
> >         return NULL;
> >  }
> >
> > -struct perf_pmu *evsel__find_pmu(struct evsel *evsel)
> > +struct perf_pmu *evsel__find_pmu(const struct evsel *evsel)
> >  {
> >         struct perf_pmu *pmu = NULL;
> >
> > @@ -1000,11 +1000,11 @@ struct perf_pmu *evsel__find_pmu(struct evsel *evsel)
> >                         break;
> >         }
> >
> > -       evsel->pmu = pmu;
> > +       ((struct evsel *)evsel)->pmu = pmu;
>
> Maybe we can move it out of the function and make callers set it.

Right, I considered this a case where in C++ we'd use mutable so
casting away the const wasn't the worst offense.

Thanks,
Ian

> Thanks,
> Namhyung
>
>
> >         return pmu;
> >  }
> >
> > -bool evsel__is_aux_event(struct evsel *evsel)
> > +bool evsel__is_aux_event(const struct evsel *evsel)
> >  {
> >         struct perf_pmu *pmu = evsel__find_pmu(evsel);
> >
> > diff --git a/tools/perf/util/python.c b/tools/perf/util/python.c
> > index 42e8b813d010..ab48ffbb6448 100644
> > --- a/tools/perf/util/python.c
> > +++ b/tools/perf/util/python.c
> > @@ -83,7 +83,7 @@ const char *perf_env__arch(struct perf_env *env __maybe_unused)
> >   * far, for the perf python binding known usecases, revisit if this become
> >   * necessary.
> >   */
> > -struct perf_pmu *evsel__find_pmu(struct evsel *evsel __maybe_unused)
> > +struct perf_pmu *evsel__find_pmu(const struct evsel *evsel __maybe_unused)
> >  {
> >         return NULL;
> >  }
> > --
> > 2.40.0.rc0.216.gc4246ad0f0-goog
> >
  

Patch

diff --git a/tools/perf/util/evsel.c b/tools/perf/util/evsel.c
index 51e8ce6edddc..2dc2c24252bb 100644
--- a/tools/perf/util/evsel.c
+++ b/tools/perf/util/evsel.c
@@ -3139,7 +3139,7 @@  bool evsel__is_hybrid(const struct evsel *evsel)
 	return evsel->pmu_name && perf_pmu__is_hybrid(evsel->pmu_name);
 }
 
-struct evsel *evsel__leader(struct evsel *evsel)
+struct evsel *evsel__leader(const struct evsel *evsel)
 {
 	return container_of(evsel->core.leader, struct evsel, core);
 }
diff --git a/tools/perf/util/evsel.h b/tools/perf/util/evsel.h
index 814a49ebb7e3..676c499323e9 100644
--- a/tools/perf/util/evsel.h
+++ b/tools/perf/util/evsel.h
@@ -212,8 +212,8 @@  int evsel__object_config(size_t object_size,
 			 int (*init)(struct evsel *evsel),
 			 void (*fini)(struct evsel *evsel));
 
-struct perf_pmu *evsel__find_pmu(struct evsel *evsel);
-bool evsel__is_aux_event(struct evsel *evsel);
+struct perf_pmu *evsel__find_pmu(const struct evsel *evsel);
+bool evsel__is_aux_event(const struct evsel *evsel);
 
 struct evsel *evsel__new_idx(struct perf_event_attr *attr, int idx);
 
@@ -505,7 +505,7 @@  int evsel__store_ids(struct evsel *evsel, struct evlist *evlist);
 
 void evsel__zero_per_pkg(struct evsel *evsel);
 bool evsel__is_hybrid(const struct evsel *evsel);
-struct evsel *evsel__leader(struct evsel *evsel);
+struct evsel *evsel__leader(const struct evsel *evsel);
 bool evsel__has_leader(struct evsel *evsel, struct evsel *leader);
 bool evsel__is_leader(struct evsel *evsel);
 void evsel__set_leader(struct evsel *evsel, struct evsel *leader);
diff --git a/tools/perf/util/pmu.c b/tools/perf/util/pmu.c
index 43b6182d96b7..45d9b8e28e16 100644
--- a/tools/perf/util/pmu.c
+++ b/tools/perf/util/pmu.c
@@ -988,7 +988,7 @@  struct perf_pmu *perf_pmu__scan(struct perf_pmu *pmu)
 	return NULL;
 }
 
-struct perf_pmu *evsel__find_pmu(struct evsel *evsel)
+struct perf_pmu *evsel__find_pmu(const struct evsel *evsel)
 {
 	struct perf_pmu *pmu = NULL;
 
@@ -1000,11 +1000,11 @@  struct perf_pmu *evsel__find_pmu(struct evsel *evsel)
 			break;
 	}
 
-	evsel->pmu = pmu;
+	((struct evsel *)evsel)->pmu = pmu;
 	return pmu;
 }
 
-bool evsel__is_aux_event(struct evsel *evsel)
+bool evsel__is_aux_event(const struct evsel *evsel)
 {
 	struct perf_pmu *pmu = evsel__find_pmu(evsel);
 
diff --git a/tools/perf/util/python.c b/tools/perf/util/python.c
index 42e8b813d010..ab48ffbb6448 100644
--- a/tools/perf/util/python.c
+++ b/tools/perf/util/python.c
@@ -83,7 +83,7 @@  const char *perf_env__arch(struct perf_env *env __maybe_unused)
  * far, for the perf python binding known usecases, revisit if this become
  * necessary.
  */
-struct perf_pmu *evsel__find_pmu(struct evsel *evsel __maybe_unused)
+struct perf_pmu *evsel__find_pmu(const struct evsel *evsel __maybe_unused)
 {
 	return NULL;
 }