Re: [PATCH 11/19] perf stat: Allocate aggr counts for recorded data

From: Ian Rogers
Date: Mon Oct 10 2022 - 19:19:05 EST


On Sun, Oct 9, 2022 at 10:36 PM Namhyung Kim <namhyung@xxxxxxxxxx> wrote:
>
> In the process_stat_config_event() it sets the aggr_mode that means the
> earlier evlist__alloc_stats() cannot allocate the aggr counts due to the
> missing aggr_mode.
>
> Do it after setting the aggr_map using evlist__alloc_aggr_stats().
>
> Signed-off-by: Namhyung Kim <namhyung@xxxxxxxxxx>

Acked-by: Ian Rogers <irogers@xxxxxxxxxx>

Thanks,
Ian

> ---
> tools/perf/builtin-stat.c | 8 ++++++++
> tools/perf/util/stat.c | 39 +++++++++++++++++++++++++++++++--------
> tools/perf/util/stat.h | 2 ++
> 3 files changed, 41 insertions(+), 8 deletions(-)
>
> diff --git a/tools/perf/builtin-stat.c b/tools/perf/builtin-stat.c
> index c76240cfc635..983f38cd4caa 100644
> --- a/tools/perf/builtin-stat.c
> +++ b/tools/perf/builtin-stat.c
> @@ -2139,6 +2139,14 @@ int process_stat_config_event(struct perf_session *session,
> else
> perf_stat_init_aggr_mode_file(st);
>
> + if (stat_config.aggr_map) {
> + int nr_aggr = stat_config.aggr_map->nr;
> +
> + if (evlist__alloc_aggr_stats(session->evlist, nr_aggr) < 0) {
> + pr_err("cannot allocate aggr counts\n");
> + return -1;
> + }
> + }
> return 0;
> }
>
> diff --git a/tools/perf/util/stat.c b/tools/perf/util/stat.c
> index 013dbe1c5d28..279aa4ea342d 100644
> --- a/tools/perf/util/stat.c
> +++ b/tools/perf/util/stat.c
> @@ -141,6 +141,31 @@ static void evsel__reset_stat_priv(struct evsel *evsel)
> memset(aggr, 0, sizeof(*aggr) * ps->nr_aggr);
> }
>
> +static int evsel__alloc_aggr_stats(struct evsel *evsel, int nr_aggr)
> +{
> + struct perf_stat_evsel *ps = evsel->stats;
> +
> + if (ps == NULL)
> + return 0;
> +
> + ps->nr_aggr = nr_aggr;
> + ps->aggr = calloc(nr_aggr, sizeof(*ps->aggr));
> + if (ps->aggr == NULL)
> + return -ENOMEM;
> +
> + return 0;
> +}
> +
> +int evlist__alloc_aggr_stats(struct evlist *evlist, int nr_aggr)
> +{
> + struct evsel *evsel;
> +
> + evlist__for_each_entry(evlist, evsel) {
> + if (evsel__alloc_aggr_stats(evsel, nr_aggr) < 0)
> + return -1;
> + }
> + return 0;
> +}
>
> static int evsel__alloc_stat_priv(struct evsel *evsel, int nr_aggr)
> {
> @@ -150,16 +175,14 @@ static int evsel__alloc_stat_priv(struct evsel *evsel, int nr_aggr)
> if (ps == NULL)
> return -ENOMEM;
>
> - if (nr_aggr) {
> - ps->nr_aggr = nr_aggr;
> - ps->aggr = calloc(nr_aggr, sizeof(*ps->aggr));
> - if (ps->aggr == NULL) {
> - free(ps);
> - return -ENOMEM;
> - }
> + evsel->stats = ps;
> +
> + if (nr_aggr && evsel__alloc_aggr_stats(evsel, nr_aggr) < 0) {
> + evsel->stats = NULL;
> + free(ps);
> + return -ENOMEM;
> }
>
> - evsel->stats = ps;
> perf_stat_evsel_id_init(evsel);
> evsel__reset_stat_priv(evsel);
> return 0;
> diff --git a/tools/perf/util/stat.h b/tools/perf/util/stat.h
> index 74bd51a3cb36..936c0709ce0d 100644
> --- a/tools/perf/util/stat.h
> +++ b/tools/perf/util/stat.h
> @@ -265,6 +265,8 @@ void evlist__reset_prev_raw_counts(struct evlist *evlist);
> void evlist__copy_prev_raw_counts(struct evlist *evlist);
> void evlist__save_aggr_prev_raw_counts(struct evlist *evlist);
>
> +int evlist__alloc_aggr_stats(struct evlist *evlist, int nr_aggr);
> +
> int perf_stat_process_counter(struct perf_stat_config *config,
> struct evsel *counter);
> struct perf_tool;
> --
> 2.38.0.rc1.362.ged0d419d3c-goog
>