Re: [PATCH v1 50/51] perf stat: Use counts rather than saved_value

From: Namhyung Kim
Date: Fri Feb 24 2023 - 17:48:17 EST


On Sun, Feb 19, 2023 at 01:28:47AM -0800, Ian Rogers wrote:
> Switch the hard coded metrics to use the aggregate value rather than
> from saved_value. When computing a metric like IPC the aggregate count
> comes from instructions then cycles is looked up and if present IPC
> computed. Rather than lookup from the saved_value rbtree, search the
> counter's evlist for the desired counter.
>
> A new helper evsel__stat_type is used to both quickly find a metric
> function and to identify when a counter is the one being sought. So
> that both total and miss counts can be sought, the stat_type enum is
> expanded. The ratio functions are rewritten to share a common helper
> with the ratios being directly passed rather than computed from an
> enum value.
>
> Signed-off-by: Ian Rogers <irogers@xxxxxxxxxx>
> ---
[SNIP]
> -static double runtime_stat_avg(enum stat_type type, int aggr_idx,
> - struct runtime_stat_data *rsd)
> +static double find_stat(const struct evsel *evsel, int aggr_idx, enum stat_type type)
> {
> - struct saved_value *v;
> -
> - v = saved_value_lookup(NULL, aggr_idx, false, type, rsd->ctx, rsd->cgrp);
> - if (!v)
> - return 0.0;
> -
> - return avg_stats(&v->stats);
> + const struct evsel *cur;
> + int evsel_ctx = evsel_context(evsel);
> +
> + evlist__for_each_entry(evsel->evlist, cur) {
> + struct perf_stat_aggr *aggr;
> +
> + /* Ignore the evsel that is being searched from. */
> + if (evsel == cur)
> + continue;
> +
> + /* Ignore evsels that are part of different groups. */
> + if (evsel->core.leader->nr_members &&
> + evsel->core.leader != cur->core.leader)

The evsel->nr_members is somewhat confusing in that it counts itself
as a member. I'm not sure it resets the nr_members to 0 for standalone
events. You'd better checking nr_members greater than 1 for group
events.

Thanks,
Namhyung


> + continue;
> + /* Ignore evsels with mismatched modifiers. */
> + if (evsel_ctx != evsel_context(cur))
> + continue;
> + /* Ignore if not the cgroup we're looking for. */
> + if (evsel->cgrp != cur->cgrp)
> + continue;
> + /* Ignore if not the stat we're looking for. */
> + if (type != evsel__stat_type(cur))
> + continue;
> +
> + aggr = &cur->stats->aggr[aggr_idx];
> + if (type == STAT_NSECS)
> + return aggr->counts.val;
> + return aggr->counts.val * cur->scale;
> + }
> + return 0.0;
> }