Re: [PATCH 06/10] perf evlist: Allow growing pollfd on add method

From: Arnaldo Carvalho de Melo
Date: Mon Aug 25 2014 - 12:00:38 EST


Em Mon, Aug 25, 2014 at 11:47:47AM +0200, Jiri Olsa escreveu:
> On Fri, Aug 22, 2014 at 05:59:46PM -0300, Arnaldo Carvalho de Melo wrote:

> > -static int perf_evlist__alloc_pollfd(struct perf_evlist *evlist)
> > +static int perf_evlist__grow_pollfd(struct perf_evlist *evlist, int hint)
> > +{
> > + int nr_fds_alloc = evlist->nr_fds_alloc + hint;
> > + size_t size = sizeof(struct pollfd) * nr_fds_alloc;
> > + struct pollfd *pollfd = realloc(evlist->pollfd, size);
> > +
> > + if (pollfd == NULL)
> > + return -ENOMEM;
> > +
> > + evlist->nr_fds_alloc = nr_fds_alloc;
> > + evlist->pollfd = pollfd;
> > + return 0;
> > +}
> > +
> > +int perf_evlist__alloc_pollfd(struct perf_evlist *evlist)
> > {
> > int nr_cpus = cpu_map__nr(evlist->cpus);
> > int nr_threads = thread_map__nr(evlist->threads);
> > @@ -416,16 +430,28 @@ static int perf_evlist__alloc_pollfd(struct perf_evlist *evlist)
> > nfds += nr_cpus * nr_threads;
> > }
> >
> > - evlist->pollfd = malloc(sizeof(struct pollfd) * nfds);
> > - return evlist->pollfd != NULL ? 0 : -ENOMEM;
> > + if (evlist->nr_fds_alloc - evlist->nr_fds < nfds &&
> > + perf_evlist__grow_pollfd(evlist, nfds) < 0)
> > + return -ENOMEM;
>
> hum, so do we still need perf_evlist__alloc_pollfd?
> we grow any time we need inside perf_evlist__add_pollfd..

Humm, yeah, it could conceivably be removed and then we would rely
always on add_pollfd() autogrowing it as needed. But then, I thought
that code using this could optimize for the case where it knows in
advance how many entries it will need, just like perf_evlist does.

I.e. the first thing builtin-kvm.c should do would be to grow it by two
more entries, because it knows it wants to add just two more entries, in
advance, etc.

- Arnaldo
--
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in
the body of a message to majordomo@xxxxxxxxxxxxxxx
More majordomo info at http://vger.kernel.org/majordomo-info.html
Please read the FAQ at http://www.tux.org/lkml/