Re: [PATCH V1 02/23] libperf evsel: Add perf_evsel__enable_thread()

From: Ian Rogers
Date: Thu May 05 2022 - 18:49:09 EST


On Thu, May 5, 2022 at 9:56 AM Adrian Hunter <adrian.hunter@xxxxxxxxx> wrote:
>
> Add perf_evsel__enable_thread() as a counterpart to
> perf_evsel__enable_cpu(), to enable all events for a thread.
>
> Signed-off-by: Adrian Hunter <adrian.hunter@xxxxxxxxx>
> ---
> tools/lib/perf/evsel.c | 10 ++++++++++
> tools/lib/perf/include/perf/evsel.h | 1 +
> 2 files changed, 11 insertions(+)
>
> diff --git a/tools/lib/perf/evsel.c b/tools/lib/perf/evsel.c
> index 20ae9f5f8b30..dcc928dd25f8 100644
> --- a/tools/lib/perf/evsel.c
> +++ b/tools/lib/perf/evsel.c
> @@ -360,6 +360,16 @@ int perf_evsel__enable_cpu(struct perf_evsel *evsel, int cpu_map_idx)
> return perf_evsel__run_ioctl(evsel, PERF_EVENT_IOC_ENABLE, NULL, cpu_map_idx);
> }
>
> +int perf_evsel__enable_thread(struct perf_evsel *evsel, int thread)
> +{
> + int err = 0;
> + int idx;
> +
> + for (idx = 0; idx < xyarray__max_x(evsel->fd) && !err; idx++)
> + err = perf_evsel__ioctl(evsel, PERF_EVENT_IOC_ENABLE, NULL, idx, thread);

For perf_cpu_map code like this has been replaced with
perf_cpu_map__for_each_cpu, it would be nice here to use a
perf_thread_map__for_each_thread. I understand we don't have that at
the moment. It would be easier to find places to add this if there is
a call to perf_thread_map__nr. Adding an assert of:

assert(perf_thread_map__nr(evsel->thread_map) == xyarray__max_x(evsel->fd));

Would help, or we could add the loop.

Thanks,
Ian

> + return err;
> +}
> +
> int perf_evsel__enable(struct perf_evsel *evsel)
> {
> int i;
> diff --git a/tools/lib/perf/include/perf/evsel.h b/tools/lib/perf/include/perf/evsel.h
> index 2a9516b42d15..699c0ed97d34 100644
> --- a/tools/lib/perf/include/perf/evsel.h
> +++ b/tools/lib/perf/include/perf/evsel.h
> @@ -36,6 +36,7 @@ LIBPERF_API int perf_evsel__read(struct perf_evsel *evsel, int cpu_map_idx, int
> struct perf_counts_values *count);
> LIBPERF_API int perf_evsel__enable(struct perf_evsel *evsel);
> LIBPERF_API int perf_evsel__enable_cpu(struct perf_evsel *evsel, int cpu_map_idx);
> +LIBPERF_API int perf_evsel__enable_thread(struct perf_evsel *evsel, int thread);
> LIBPERF_API int perf_evsel__disable(struct perf_evsel *evsel);
> LIBPERF_API int perf_evsel__disable_cpu(struct perf_evsel *evsel, int cpu_map_idx);
> LIBPERF_API struct perf_cpu_map *perf_evsel__cpus(struct perf_evsel *evsel);
> --
> 2.25.1
>