Re: [PATCH v2 9/9] perf stat: Use affinity for enabling/disabling events

From: Andi Kleen
Date: Wed Oct 23 2019 - 09:07:16 EST


On Wed, Oct 23, 2019 at 12:30:48PM +0200, Jiri Olsa wrote:
> On Sun, Oct 20, 2019 at 10:52:02AM -0700, Andi Kleen wrote:
>
> SNIP
>
> >
> > void evlist__enable(struct evlist *evlist)
> > {
> > struct evsel *pos;
> > + struct affinity affinity;
> > + struct perf_cpu_map *cpus;
> > + int i;
> > +
> > + if (affinity__setup(&affinity) < 0)
> > + return;
> > +
> > + cpus = evlist__cpu_iter_start(evlist);
> > + for (i = 0; i < cpus->nr; i++) {
> > + int cpu = cpus->map[i];
> > + affinity__set(&affinity, cpu);
> >
> > + evlist__for_each_entry(evlist, pos) {
> > + if (evlist__cpu_iter_skip(pos, cpu))
> > + continue;
> > + if (!perf_evsel__is_group_leader(pos) || !pos->core.fd)
> > + continue;
>
> all the previous patches and this one have this code in common,
> could we make this a single function, that would call a callback
> that would have affinity set.. sort of like what we do in
> cpu_function_call in the kernel

I'm personally not a big friend of call backs. They usually make
the code harder to read and reason about.

Prefer to use callable libraries of common code.

Also the event open code has some more complex variants of this pattern
which would need multiple call backs.

I already factored the common code into the iterator.

I guess the for loop could be a macro, and affinity_set() could
perhaps accept the map and return the cpu. I'll add that in
the next version. This will reduce the common code by a few lines
more.

-Andi