Re: [PATCH 01/10] perf evlist: Introduce perf_evlist__filter_pollfd method

From: Adrian Hunter
Date: Mon Aug 25 2014 - 02:59:27 EST


On 08/22/2014 11:59 PM, Arnaldo Carvalho de Melo wrote:
> From: Arnaldo Carvalho de Melo <acme@xxxxxxxxxx>
>
> To remove all entries in evlist->pollfd[] that have revents matching at
> least one of the bits in the specified mask.
>
> It'll adjust evlist->nr_fds to the number of unfiltered fds and will
> return this value, as a convenience and to avoid requiring direct access
> to internal state of perf_evlist objects.
>
> This will be used after polling the evlist fds so that we remove fds
> that were closed by the kernel.
>
> Cc: Adrian Hunter <adrian.hunter@xxxxxxxxx>
> Cc: David Ahern <dsahern@xxxxxxxxx>
> Cc: Don Zickus <dzickus@xxxxxxxxxx>
> Cc: Frederic Weisbecker <fweisbec@xxxxxxxxx>
> Cc: Ingo Molnar <mingo@xxxxxxxxxx>
> Cc: Jiri Olsa <jolsa@xxxxxxxxxx>
> Cc: Mike Galbraith <efault@xxxxxx>
> Cc: Namhyung Kim <namhyung@xxxxxxxxxx>
> Cc: Paul Mackerras <paulus@xxxxxxxxx>
> Cc: Peter Zijlstra <peterz@xxxxxxxxxxxxx>
> Cc: Stephane Eranian <eranian@xxxxxxxxxx>
> Link: http://lkml.kernel.org/n/tip-ki3oqbfjg84si3f9amhyqvzb@xxxxxxxxxxxxxx
> Signed-off-by: Arnaldo Carvalho de Melo <acme@xxxxxxxxxx>
> ---
> tools/perf/util/evlist.c | 16 ++++++++++++++++
> tools/perf/util/evlist.h | 2 ++
> 2 files changed, 18 insertions(+)
>
> diff --git a/tools/perf/util/evlist.c b/tools/perf/util/evlist.c
> index a3e28b49128a..bd7896073d10 100644
> --- a/tools/perf/util/evlist.c
> +++ b/tools/perf/util/evlist.c
> @@ -428,6 +428,22 @@ void perf_evlist__add_pollfd(struct perf_evlist *evlist, int fd)
> evlist->nr_fds++;
> }
>
> +int perf_evlist__filter_pollfd(struct perf_evlist *evlist, short revents_and_mask)
> +{
> + int fd = 0, nr_fds = 0;
> +
> + while (fd < evlist->nr_fds) {

A for loop is clearer e.g.

for (fd = 0; fd < evlist->nr_fds; fd++) {


> + if ((evlist->pollfd[fd].revents & revents_and_mask) == 0)
> + ++nr_fds;
> +
> + if (++fd != nr_fds)
> + evlist->pollfd[nr_fds] = evlist->pollfd[fd];

That looks like it will go off the end of the array. Shouldn't it be:

if ((evlist->pollfd[fd].revents & revents_and_mask) == 0) {
if (fd != nr_fds)
evlist->pollfd[nr_fds] = evlist->pollfd[fd];
nr_fds += 1;
}

> + }
> +
> + evlist->nr_fds = nr_fds;
> + return nr_fds;
> +}
> +
> static void perf_evlist__id_hash(struct perf_evlist *evlist,
> struct perf_evsel *evsel,
> int cpu, int thread, u64 id)
> diff --git a/tools/perf/util/evlist.h b/tools/perf/util/evlist.h
> index 106de53a6a74..1082420951f9 100644
> --- a/tools/perf/util/evlist.h
> +++ b/tools/perf/util/evlist.h
> @@ -84,6 +84,8 @@ void perf_evlist__id_add(struct perf_evlist *evlist, struct perf_evsel *evsel,
>
> void perf_evlist__add_pollfd(struct perf_evlist *evlist, int fd);
>
> +int perf_evlist__filter_pollfd(struct perf_evlist *evlist, short revents_and_mask);
> +
> struct perf_evsel *perf_evlist__id2evsel(struct perf_evlist *evlist, u64 id);
>
> struct perf_sample_id *perf_evlist__id2sid(struct perf_evlist *evlist, u64 id);
>

--
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/