perf tools polling fixes
Arnaldo Carvalho de Melo
maintainerHi Ingo,
Please consider pulling into tip/perf/core.
Best Regards,
- Arnaldo
The following changes since commit 521e8bac67a71a6544274f39d5c61473e0e54ac0:
perf/x86/intel/uncore: Update support for client uncore IMC PMU (2014-09-24 14:48:25 +0200)
are available in the git repository at:
git://git.kernel.org/pub/scm/linux/kernel/git/acme/linux.git tags/perf-fdarray-for-mingo
for you to fetch changes up to 46fb3c21d20415dd2693570c33d0ea6eb8745e04:
perf trace: Filter out POLLHUP'ed file descriptors (2014-09-25 16:46:56 -0300)
----------------------------------------------------------------
Infrastructure:
We were not handling POLLHUP notifications for event file descriptors.
Fix it by filtering entries in the events file descriptor array after poll returns, refcounting mmaps so that when the last fd pointing to a perf mmap goes away we do the unmap.
User visible:
Now 'record' and 'trace' properly exit when a target thread exits.
Arnaldo Carvalho de Melo (14):
perf evlist: Introduce perf_evlist__filter_pollfd method
perf tests: Add test for perf_evlist__filter_pollfd()
perf evlist: Monitor POLLERR and POLLHUP events too
perf evlist: We need to poll all event file descriptors
perf evlist: Allow growing pollfd on add method
perf tests: Add pollfd growing test
perf kvm stat live: Use perf_evlist__add_pollfd() instead of local equivalent perf evlist: Introduce poll method for common code idiom
tools lib api: Adopt fdarray class from perf's evlist
perf evlist: Refcount mmaps
tools lib fd array: Allow associating an integer cookie with each entry perf evlist: Unmap when all refcounts to fd are gone and events drained perf record: Filter out POLLHUP'ed file descriptors
perf trace: Filter out POLLHUP'ed file descriptors
----------------------------------------------------------------
Arnaldo Carvalho de Melo (14):
perf evlist: Introduce perf_evlist__filter_pollfd method
perf tests: Add test for perf_evlist__filter_pollfd()
perf evlist: Monitor POLLERR and POLLHUP events too
perf evlist: We need to poll all event file descriptors
perf evlist: Allow growing pollfd on add method
perf tests: Add pollfd growing test
perf kvm stat live: Use perf_evlist__add_pollfd() instead of local equivalent perf evlist: Introduce poll method for common code idiom
tools lib api: Adopt fdarray class from perf's evlist
perf evlist: Refcount mmaps
tools lib fd array: Allow associating an integer cookie with each entry perf evlist: Unmap when all refcounts to fd are gone and events drained perf record: Filter out POLLHUP'ed file descriptors
perf trace: Filter out POLLHUP'ed file descriptors
tools/lib/api/Makefile | 7 +- tools/lib/api/fd/array.c | 127 ++++++++++++++++++++++ tools/lib/api/fd/array.h | 46 ++++++++ tools/perf/Makefile.perf | 3 +- tools/perf/builtin-kvm.c | 24 ++--- tools/perf/builtin-record.c | 9 +- tools/perf/builtin-top.c | 4 +- tools/perf/builtin-trace.c | 7 +- tools/perf/tests/builtin-test.c | 8 ++ tools/perf/tests/fdarray.c | 174 ++++++++++++++++++++++++++++++ tools/perf/tests/open-syscall-tp-fields.c | 2 +- tools/perf/tests/perf-record.c | 2 +- tools/perf/tests/task-exit.c | 2 +- tools/perf/tests/tests.h | 2 + tools/perf/util/evlist.c | 105 +++++++++++++++--- tools/perf/util/evlist.h | 16 ++- tools/perf/util/python.c | 6 +- 17 files changed, 501 insertions(+), 43 deletions(-) create mode 100644 tools/lib/api/fd/array.c create mode 100644 tools/lib/api/fd/array.h create mode 100644 tools/perf/tests/fdarray.c
Arnaldo Carvalho de Melo
maintainerPATCH 1/14 perf evlist: Introduce perf_evlist__filter_pollfd method
From: Arnaldo Carvalho de Melo <[email protected]>
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.
--- tools/perf/util/evlist.c | 21 +++++++++++++++++++++ tools/perf/util/evlist.h | 2 ++ 2 files changed, 23 insertions(+)
tools/perf/util/evlist.c +21 -0
@@ -428,6 +428,27 @@ 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, nr_fds =0 ; + +if (evlist->nr_fds ==0 ) +return 0 ; + +for (fd =0 ; fd < evlist->nr_fds; ++fd) { +if (evlist->pollfd[fd].revents & revents_and_mask) +continue ; + +if (fd != nr_fds) + evlist->pollfd[nr_fds] = evlist->pollfd[fd]; + + ++nr_fds; + } + + 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)
tools/perf/util/evlist.h +2 -0
@@ -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);
Signature · 1 line
1.9.3
Arnaldo Carvalho de Melo
maintainerPATCH 2/14 perf tests: Add test for perf_evlist__filter_pollfd()
In reply to Arnaldo Carvalho de MeloFrom: Arnaldo Carvalho de Melo <[email protected]>
That will use a synthetic evlist with just what is touched by this new method to check that it works as expected.
Output in verbose mode:
$ perf test -v pollfd 33: Filter fds with revents mask in a pollfd array : --- start --- filtering all but pollfd[2]: before: 5 [ 5, 4, 3, 2, 1 ] after: 1 [ 3 ] filtering all but (pollfd[0], pollfd[3]): before: 5 [ 5, 4, 3, 2, 1 ] after: 2 [ 5, 2 ] test child finished with 0 ---- end ---- Filter fds with revents mask in a pollfd array: Ok $
--- tools/perf/Makefile.perf | 1 + tools/perf/tests/builtin-test.c | 4 ++ tools/perf/tests/evlist.c | 103 ++++++++++++++++++++++++++++++++++++++++ tools/perf/tests/tests.h | 1 + 4 files changed, 109 insertions(+) create mode 100644 tools/perf/tests/evlist.c
tools/perf/Makefile.perf +1 -0
@@ -400,6 +400,7 @@ LIB_OBJS += $(OUTPUT)tests/open-syscall-tp-fields.o LIB_OBJS += $(OUTPUT)tests/mmap-basic.o LIB_OBJS += $(OUTPUT)tests/perf-record.o LIB_OBJS += $(OUTPUT)tests/rdpmc.o +LIB_OBJS += $(OUTPUT)tests/evlist.o LIB_OBJS += $(OUTPUT)tests/evsel-roundtrip-name.o LIB_OBJS += $(OUTPUT)tests/evsel-tp-sched.o LIB_OBJS += $(OUTPUT)tests/pmu.o
tools/perf/tests/builtin-test.c +4 -0
@@ -158,6 +158,10 @@ static struct test { .func = test__switch_tracking, }, { + .desc = "Filter fds with revents mask in a pollfd array", + .func = test__perf_evlist__filter_pollfd, + }, + { .func = NULL, }, };
tools/perf/tests/evlist.c new file +103 -0
103 lines not on this page · raw message
tools/perf/tests/tests.h +1 -0
@@ -49,6 +49,7 @@ int test__thread_mg_share(void);int test__hists_output (void );int test__hists_cumulate (void );int test__switch_tracking (void ); +int test__perf_evlist__filter_pollfd (void );#if defined(__x86_64__) || defined(__i386__) || defined(__arm__)#ifdef HAVE_DWARF_UNWIND_SUPPORT
Signature · 1 line
1.9.3
Arnaldo Carvalho de Melo
maintainerPATCH 3/14 perf evlist: Monitor POLLERR and POLLHUP events too
In reply to Arnaldo Carvalho de MeloFrom: Arnaldo Carvalho de Melo <[email protected]>
We want to know when the fd went away, like when a monitored thread exits.
If we do not monitor such events, then the tools will wait forever on events from a vanished thread, like when running:
$ sleep 5s & $ perf record -p `pidof sleep`
This builds upon the kernel patch by Jiri Olsa that actually makes a poll on those file descriptors to return POLLHUP.
It is also needed to change the tools to use perf_evlist__filter_pollfd() to check if there are remainings fds to monitor or if all are gone, in which case they will exit the poll/mmap/read loop.
--- tools/perf/util/evlist.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
tools/perf/util/evlist.c +1 -1
@@ -424,7 +424,7 @@ void perf_evlist__add_pollfd(struct perf_evlist *evlist, int fd) {fcntl (fd,F_SETFL ,O_NONBLOCK ); evlist->pollfd[evlist->nr_fds].fd = fd; - evlist->pollfd[evlist->nr_fds].events =POLLIN ; + evlist->pollfd[evlist->nr_fds].events =POLLIN |POLLERR |POLLHUP ; evlist->nr_fds++; }
Signature · 1 line
1.9.3
Arnaldo Carvalho de Melo
maintainerPATCH 4/14 perf evlist: We need to poll all event file descriptors
In reply to Arnaldo Carvalho de MeloFrom: Arnaldo Carvalho de Melo <[email protected]>
Because we want to notice when they get POLLHUP'ed, so that we can figure out when all threads exited in a workload being monitored.
We can't just monitor the fds that were mmaped, we need to notice when all the fds that were PERF_EVENT_IOC_SET_OUTPUT'ed too, because the mmap stays even after the fd that originally was used to do the mmap call went away, its only when all the set-output fds for a mmap are gone that the mmap is.
--- tools/perf/util/evlist.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)
tools/perf/util/evlist.c +2 -2
@@ -717,8 +717,6 @@ static int __perf_evlist__mmap(struct perf_evlist *evlist, int idx, evlist->mmap[idx].base =NULL ;return -1 ; } - -perf_evlist__add_pollfd (evlist, fd);return 0 ; }
@@ -745,6 +743,8 @@ static int perf_evlist__mmap_per_evsel(struct perf_evlist *evlist, int idx,return -1 ; } +perf_evlist__add_pollfd (evlist, fd); +if ((evsel->attr.read_format &PERF_FORMAT_ID ) &&perf_evlist__id_add_fd (evlist, evsel, cpu, thread, fd) <0 )return -1 ;
Signature · 1 line
1.9.3
Arnaldo Carvalho de Melo
maintainerPATCH 5/14 perf evlist: Allow growing pollfd on add method
In reply to Arnaldo Carvalho de MeloFrom: Arnaldo Carvalho de Melo <[email protected]>
This way we will be able to add more file descriptors to be polled, like stdin or some timer fd.
At this point we might as well yank the pollfd class from evlist so that it can be used in other places.
--- tools/perf/util/evlist.c | 38 +++++++++++++++++++++++++++++++++----- tools/perf/util/evlist.h | 5 +++-- 2 files changed, 36 insertions(+), 7 deletions(-)
tools/perf/util/evlist.c +33 -5
@@ -402,7 +402,21 @@ int perf_evlist__enable_event_idx(struct perf_evlist *evlist,return perf_evlist__enable_event_thread (evlist, evsel, idx); } -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 ; + +return 0 ; } -void perf_evlist__add_pollfd (struct perf_evlist *evlist,int fd) +int perf_evlist__add_pollfd (struct perf_evlist *evlist,int fd) { +/* +* XXX: 64 is arbitrary, just not to call realloc at each fd. +* Find a better autogrowing heuristic +*/ +if (evlist->nr_fds == evlist->nr_fds_alloc && +perf_evlist__grow_pollfd (evlist,64 ) <0 ) +return -ENOMEM ; +fcntl (fd,F_SETFL ,O_NONBLOCK ); evlist->pollfd[evlist->nr_fds].fd = fd; evlist->pollfd[evlist->nr_fds].events =POLLIN |POLLERR |POLLHUP ; evlist->nr_fds++; +return 0 ; }int perf_evlist__filter_pollfd (struct perf_evlist *evlist,short revents_and_mask)
@@ -717,6 +743,7 @@ static int __perf_evlist__mmap(struct perf_evlist *evlist, int idx, evlist->mmap[idx].base =NULL ;return -1 ; } +return 0 ; }
@@ -743,7 +770,8 @@ static int perf_evlist__mmap_per_evsel(struct perf_evlist *evlist, int idx,return -1 ; } -perf_evlist__add_pollfd (evlist, fd); +if (perf_evlist__add_pollfd (evlist, fd) <0 ) +return -1 ;if ((evsel->attr.read_format &PERF_FORMAT_ID ) &&perf_evlist__id_add_fd (evlist, evsel, cpu, thread, fd) <0 )
tools/perf/util/evlist.h +3 -2
@@ -30,6 +30,7 @@ struct perf_evlist {int nr_entries;int nr_groups;int nr_fds; +int nr_fds_alloc;int nr_mmaps;size_t mmap_len;int id_pos;
@@ -82,8 +83,8 @@ perf_evlist__find_tracepoint_by_name(struct perf_evlist *evlist,void perf_evlist__id_add (struct perf_evlist *evlist,struct perf_evsel *evsel,int cpu,int thread,u64 id); -void perf_evlist__add_pollfd (struct perf_evlist *evlist,int fd); - +int perf_evlist__add_pollfd (struct perf_evlist *evlist,int fd); +int perf_evlist__alloc_pollfd (struct perf_evlist *evlist);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);
Signature · 1 line
1.9.3
Arnaldo Carvalho de Melo
maintainerPATCH 6/14 perf tests: Add pollfd growing test
In reply to Arnaldo Carvalho de MeloFrom: Arnaldo Carvalho de Melo <[email protected]>
[acme@ssdandy linux]$ perf test "Add fd"
34: Add fd to pollfd array, making it autogrow : Ok
[acme@ssdandy linux]$ perf test -v "Add fd"
34: Add fd to pollfd array, making it autogrow :
--- start ---
test child forked, pid 19817
before growing array: 2 [ 1, 2 ] after 3rd add_pollfd: 3 [ 1, 2, 35 ] after 4th add_pollfd: 4 [ 1, 2, 35, 88 ]
test child finished with 0
---- end ----
Add fd to pollfd array, making it autogrow: Ok [acme@ssdandy linux]$
--- tools/perf/tests/builtin-test.c | 4 ++ tools/perf/tests/evlist.c | 111 ++++++++++++++++++++++++++++++++++++++++ tools/perf/tests/tests.h | 1 + 3 files changed, 116 insertions(+)
tools/perf/tests/builtin-test.c +4 -0
@@ -162,6 +162,10 @@ static struct test { .func = test__perf_evlist__filter_pollfd, }, { + .desc = "Add fd to pollfd array, making it autogrow", + .func = test__perf_evlist__add_pollfd, + }, + { .func = NULL, }, };
tools/perf/tests/evlist.c +111 -0
119 lines not on this page · raw message
tools/perf/tests/tests.h +1 -0
@@ -50,6 +50,7 @@ int test__hists_output(void);int test__hists_cumulate (void );int test__switch_tracking (void );int test__perf_evlist__filter_pollfd (void ); +int test__perf_evlist__add_pollfd (void );#if defined(__x86_64__) || defined(__i386__) || defined(__arm__)#ifdef HAVE_DWARF_UNWIND_SUPPORT
Signature · 1 line
1.9.3
Arnaldo Carvalho de Melo
maintainerPATCH 7/14 perf kvm stat live: Use perf_evlist__add_pollfd() instead of local equivalent
In reply to Arnaldo Carvalho de MeloFrom: Arnaldo Carvalho de Melo <[email protected]>
Since we can add file descriptors to the evlist pollfd and it will autogrow, no need to copy all events to a local pollfd array, just add the timer and stdin file descriptors.
--- tools/perf/builtin-kvm.c | 22 +++++++++------------- 1 file changed, 9 insertions(+), 13 deletions(-)
tools/perf/builtin-kvm.c +9 -13
@@ -919,15 +919,8 @@ static int kvm_events_live_report(struct perf_kvm_stat *kvm)signal (SIGINT , sig_handler);signal (SIGTERM , sig_handler); -/* copy pollfds -- need to add timerfd and stdin */ +/* use pollfds -- need to add timerfd and stdin */ nr_fds = kvm->evlist->nr_fds; - pollfds =zalloc (sizeof (struct pollfd ) * (nr_fds +2 )); -if (!pollfds) { - err = -ENOMEM ; - goto out; - } -memcpy (pollfds, kvm->evlist->pollfd, -sizeof (struct pollfd ) * kvm->evlist->nr_fds);/* add timer fd */ if (perf_kvm__timerfd_create (kvm) <0 ) {
@@ -935,17 +928,21 @@ static int kvm_events_live_report(struct perf_kvm_stat *kvm) goto out; } - pollfds[nr_fds].fd = kvm->timerfd; - pollfds[nr_fds].events =POLLIN ; +if (perf_evlist__add_pollfd (kvm->evlist, kvm->timerfd)) + goto out; + nr_fds++; - pollfds[nr_fds].fd =fileno (stdin); - pollfds[nr_fds].events =POLLIN ; +if (perf_evlist__add_pollfd (kvm->evlist,fileno (stdin))) + goto out; + nr_stdin = nr_fds; nr_fds++;if (fd_set_nonblock (fileno (stdin)) !=0 ) goto out; + pollfds = kvm->evlist->pollfd; +/* everything is good - enable the events and process */ perf_evlist__enable (kvm->evlist);
@@ -979,7 +976,6 @@ out:close (kvm->timerfd);tcsetattr (0 ,TCSAFLUSH , &save); -free (pollfds);return err; }
Signature · 1 line
1.9.3
Arnaldo Carvalho de Melo
maintainerPATCH 8/14 perf evlist: Introduce poll method for common code idiom
In reply to Arnaldo Carvalho de MeloFrom: Arnaldo Carvalho de Melo <[email protected]>
Since we have access two evlist members in all these poll calls, provide a helper.
This will also help to make the patch introducing the pollfd class more clear, as the evlist specific uses will be hiden away perf_evlist__poll().
--- tools/perf/builtin-record.c | 2 +- tools/perf/builtin-top.c | 4 ++-- tools/perf/builtin-trace.c | 2 +- tools/perf/tests/open-syscall-tp-fields.c | 2 +- tools/perf/tests/perf-record.c | 2 +- tools/perf/tests/task-exit.c | 2 +- tools/perf/util/evlist.c | 5 +++++ tools/perf/util/evlist.h | 2 ++ tools/perf/util/python.c | 2 +- 9 files changed, 15 insertions(+), 8 deletions(-)
tools/perf/builtin-record.c +1 -1
@@ -459,7 +459,7 @@ static int __cmd_record(struct record *rec, int argc, const char **argv)if (hits == rec->samples) {if (done)break ; - err =poll (rec->evlist->pollfd, rec->evlist->nr_fds,-1 ); + err =perf_evlist__poll (rec->evlist,-1 );/* * Propagate error, only if there's any. Ignore positive * number of returned events and interrupt error.
tools/perf/builtin-top.c +2 -2
@@ -964,7 +964,7 @@ static int __cmd_top(struct perf_top *top)perf_evlist__enable (top->evlist);/* Wait for a minimal set of events before starting the snapshot */ -poll (top->evlist->pollfd, top->evlist->nr_fds,100 ); +perf_evlist__poll (top->evlist,100 );perf_top__mmap_read (top);
@@ -991,7 +991,7 @@ static int __cmd_top(struct perf_top *top)perf_top__mmap_read (top);if (hits == top->samples) - ret =poll (top->evlist->pollfd, top->evlist->nr_fds,100 ); + ret =perf_evlist__poll (top->evlist,100 ); } ret =0 ;
tools/perf/builtin-trace.c +1 -1
@@ -2171,7 +2171,7 @@ next_event:if (trace->nr_events == before) {int timeout = done ?100 :-1 ; -if (poll (evlist->pollfd, evlist->nr_fds, timeout) >0 ) +if (perf_evlist__poll (evlist, timeout) >0 ) goto again; }else { goto again;
tools/perf/tests/open-syscall-tp-fields.c +1 -1
@@ -105,7 +105,7 @@ int test__syscall_open_tp_fields(void) }if (nr_events == before) -poll (evlist->pollfd, evlist->nr_fds,10 ); +perf_evlist__poll (evlist,10 );if (++nr_polls >5 ) {pr_debug ("%s: no events!\n" , __func__);
tools/perf/tests/perf-record.c +1 -1
@@ -268,7 +268,7 @@ int test__PERF_RECORD(void)* perf_event_attr.wakeup_events, just PERF_EVENT_SAMPLE does. */ if (total_events == before && false) -poll (evlist->pollfd, evlist->nr_fds,-1 ); +perf_evlist__poll (evlist,-1 );sleep (1 );if (++wakeups >5 ) {
tools/perf/tests/task-exit.c +1 -1
@@ -105,7 +105,7 @@ retry: }if (!exited || !nr_exit) { -poll (evlist->pollfd, evlist->nr_fds,-1 ); +perf_evlist__poll (evlist,-1 ); goto retry; }
tools/perf/util/evlist.c +5 -0
@@ -475,6 +475,11 @@ int perf_evlist__filter_pollfd(struct perf_evlist *evlist, short revents_and_masreturn nr_fds; } +int perf_evlist__poll (struct perf_evlist *evlist,int timeout) +{ +return poll (evlist->pollfd, evlist->nr_fds, timeout); +} +static void perf_evlist__id_hash (struct perf_evlist *evlist,struct perf_evsel *evsel,int cpu,int thread,u64 id)
tools/perf/util/evlist.h +2 -0
@@ -87,6 +87,8 @@ int perf_evlist__add_pollfd(struct perf_evlist *evlist, int fd);int perf_evlist__alloc_pollfd (struct perf_evlist *evlist);int perf_evlist__filter_pollfd (struct perf_evlist *evlist,short revents_and_mask); +int perf_evlist__poll (struct perf_evlist *evlist,int timeout); +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);
tools/perf/util/python.c +1 -1
@@ -736,7 +736,7 @@ static PyObject *pyrf_evlist__poll(struct pyrf_evlist *pevlist,if (!PyArg_ParseTupleAndKeywords (args, kwargs,"|i" , kwlist, &timeout))return NULL ; - n =poll (evlist->pollfd, evlist->nr_fds, timeout); + n =perf_evlist__poll (evlist, timeout);if (n <0 ) {PyErr_SetFromErrno (PyExc_OSError);return NULL ;
Signature · 1 line
1.9.3
Arnaldo Carvalho de Melo
maintainerPATCH 9/14 tools lib api: Adopt fdarray class from perf's evlist
In reply to Arnaldo Carvalho de MeloFrom: Arnaldo Carvalho de Melo <[email protected]>
The extensible file description array that grew in the perf_evlist class can be useful for other tools, as it is not something that only evlists need, so move it to tools/lib/api/fd to ease sharing it.
v2: Don't use {} like in:
libapi_dirs:
$(QUIET_MKDIR)mkdir -p $(OUTPUT){fs,fd}/in Makefiles, as it will not work in some systems, as in ubuntu13.10.
v3: Add fd/*.[ch] to LIBAPIKFS_SOURCES (Fix from Jiri Olsa)
v4: Leave the fcntl(fd, O_NONBLOCK) in the evlist layer, remains to
be checked if it is really needed there, but has no place in the fdarray class (Fix from Jiri Olsa)
v5: Remove evlist details from fdarray grow/filter tests. Improve it a
bit doing more tests about expected internal state.
--- tools/lib/api/Makefile | 7 +- tools/lib/api/fd/array.c | 107 ++++++++++++++++++++ tools/lib/api/fd/array.h | 32 ++++++ tools/perf/Makefile.perf | 4 +- tools/perf/builtin-kvm.c | 4 +- tools/perf/tests/builtin-test.c | 8 +- tools/perf/tests/evlist.c | 214 ---------------------------------------- tools/perf/tests/fdarray.c | 174 ++++++++++++++++++++++++++++++++ tools/perf/tests/tests.h | 4 +- tools/perf/util/evlist.c | 57 ++--------- tools/perf/util/evlist.h | 5 +- tools/perf/util/python.c | 4 +- 12 files changed, 342 insertions(+), 278 deletions(-) create mode 100644 tools/lib/api/fd/array.c create mode 100644 tools/lib/api/fd/array.h delete mode 100644 tools/perf/tests/evlist.c create mode 100644 tools/perf/tests/fdarray.c
tools/lib/api/Makefile +6 -1
@@ -10,9 +10,14 @@ LIB_OBJS= LIB_H += fs/debugfs.h LIB_H += fs/fs.h +# See comment below about piggybacking... +LIB_H += fd/array.h LIB_OBJS += $(OUTPUT)fs/debugfs.o LIB_OBJS += $(OUTPUT)fs/fs.o +# XXX piggybacking here, need to introduce libapikfd, or rename this +# to plain libapik.a and make it have it all api goodies +LIB_OBJS += $(OUTPUT)fd/array.o LIBFILE = libapikfs.a
@@ -29,7 +34,7 @@ $(LIBFILE): $(LIB_OBJS) $(LIB_OBJS): $(LIB_H) libapi_dirs: - $(QUIET_MKDIR)mkdir -p $(OUTPUT)fs/ + $(QUIET_MKDIR)mkdir -p $(OUTPUT)fd $(OUTPUT)fs $(OUTPUT)%.o: %.c libapi_dirs $(QUIET_CC)$(CC) -o $@ -c $(ALL_CFLAGS) $<
tools/lib/api/fd/array.c new file +107 -0
107 lines not on this page · raw message
tools/lib/api/fd/array.h new file +32 -0
@@ -0,0 +1,32 @@ +#ifndef __API_FD_ARRAY__ +#define __API_FD_ARRAY__ + +#include <stdio.h> + +struct pollfd ; + +struct fdarray { +int nr; +int nr_alloc; +int nr_autogrow; +struct pollfd *entries; +}; + +void fdarray__init (struct fdarray *fda,int nr_autogrow); +void fdarray__exit (struct fdarray *fda); + +struct fdarray *fdarray__new (int nr_alloc,int nr_autogrow); +void fdarray__delete (struct fdarray *fda); + +int fdarray__add (struct fdarray *fda,int fd,short revents); +int fdarray__poll (struct fdarray *fda,int timeout); +int fdarray__filter (struct fdarray *fda,short revents); +int fdarray__grow (struct fdarray *fda,int extra); +int fdarray__fprintf (struct fdarray *fda,FILE *fp); + +static inline int fdarray__available_entries (struct fdarray *fda) +{ +return fda->nr_alloc - fda->nr; +} + +#endif /* __API_FD_ARRAY__ */
tools/perf/Makefile.perf +2 -2
@@ -400,9 +400,9 @@ LIB_OBJS += $(OUTPUT)tests/open-syscall-tp-fields.o LIB_OBJS += $(OUTPUT)tests/mmap-basic.o LIB_OBJS += $(OUTPUT)tests/perf-record.o LIB_OBJS += $(OUTPUT)tests/rdpmc.o -LIB_OBJS += $(OUTPUT)tests/evlist.o LIB_OBJS += $(OUTPUT)tests/evsel-roundtrip-name.o LIB_OBJS += $(OUTPUT)tests/evsel-tp-sched.o +LIB_OBJS += $(OUTPUT)tests/fdarray.o LIB_OBJS += $(OUTPUT)tests/pmu.o LIB_OBJS += $(OUTPUT)tests/hists_common.o LIB_OBJS += $(OUTPUT)tests/hists_link.o
@@ -770,7 +770,7 @@ $(LIBTRACEEVENT)-clean: install-traceevent-plugins: $(LIBTRACEEVENT) $(QUIET_SUBDIR0)$(TRACE_EVENT_DIR) $(LIBTRACEEVENT_FLAGS) install_plugins -LIBAPIKFS_SOURCES = $(wildcard $(LIB_PATH)fs/*.[ch]) +LIBAPIKFS_SOURCES = $(wildcard $(LIB_PATH)fs/*.[ch] $(LIB_PATH)fd/*.[ch]) # if subdir is set, we've been called from above so target has been built # already
tools/perf/builtin-kvm.c +2 -2
@@ -920,7 +920,7 @@ static int kvm_events_live_report(struct perf_kvm_stat *kvm)signal (SIGTERM , sig_handler);/* use pollfds -- need to add timerfd and stdin */ - nr_fds = kvm->evlist->nr_fds; + nr_fds = kvm->evlist->pollfd.nr;/* add timer fd */ if (perf_kvm__timerfd_create (kvm) <0 ) {
@@ -941,7 +941,7 @@ static int kvm_events_live_report(struct perf_kvm_stat *kvm)if (fd_set_nonblock (fileno (stdin)) !=0 ) goto out; - pollfds = kvm->evlist->pollfd; + pollfds = kvm->evlist->pollfd.entries;/* everything is good - enable the events and process */ perf_evlist__enable (kvm->evlist);
tools/perf/tests/builtin-test.c +4 -4
@@ -158,12 +158,12 @@ static struct test { .func = test__switch_tracking, }, { - .desc = "Filter fds with revents mask in a pollfd array", - .func = test__perf_evlist__filter_pollfd, + .desc = "Filter fds with revents mask in a fdarray", + .func = test__fdarray__filter, }, { - .desc = "Add fd to pollfd array, making it autogrow", - .func = test__perf_evlist__add_pollfd, + .desc = "Add fd to a fdarray, making it autogrow", + .func = test__fdarray__add, }, { .func = NULL,
tools/perf/tests/evlist.c deleted +0 -214
214 lines not on this page · raw message
tools/perf/tests/fdarray.c new file +174 -0
174 lines not on this page · raw message
tools/perf/tests/tests.h +2 -2
@@ -49,8 +49,8 @@ int test__thread_mg_share(void);int test__hists_output (void );int test__hists_cumulate (void );int test__switch_tracking (void ); -int test__perf_evlist__filter_pollfd (void ); -int test__perf_evlist__add_pollfd (void ); +int test__fdarray__filter (void ); +int test__fdarray__add (void );#if defined(__x86_64__) || defined(__i386__) || defined(__arm__)#ifdef HAVE_DWARF_UNWIND_SUPPORT
tools/perf/util/evlist.c +9 -48
102 lines not on this page · raw message
tools/perf/util/evlist.h +2 -3
@@ -2,6 +2,7 @@#define __PERF_EVLIST_H 1#include <linux/list.h> +#include <api/fd/array.h> #include <stdio.h> #include "../perf.h" #include "event.h"
@@ -29,8 +30,6 @@ struct perf_evlist {struct hlist_head heads[PERF_EVLIST__HLIST_SIZE ];int nr_entries;int nr_groups; -int nr_fds; -int nr_fds_alloc;int nr_mmaps;size_t mmap_len;int id_pos;
@@ -41,8 +40,8 @@ struct perf_evlist {pid_t pid; } workload;bool overwrite; +struct fdarray pollfd;struct perf_mmap *mmap; -struct pollfd *pollfd;struct thread_map *threads;struct cpu_map *cpus;struct perf_evsel *selected;
tools/perf/util/python.c +2 -2
@@ -753,9 +753,9 @@ static PyObject *pyrf_evlist__get_pollfd(struct pyrf_evlist *pevlist,PyObject *list =PyList_New (0 );int i; -for (i =0 ; i < evlist->nr_fds; ++i) { +for (i =0 ; i < evlist->pollfd.nr; ++i) {PyObject *file; -FILE *fp =fdopen (evlist->pollfd[i].fd,"r" ); +FILE *fp =fdopen (evlist->pollfd.entries[i].fd,"r" );if (fp ==NULL ) goto free_list;
Signature · 1 line
1.9.3
Arnaldo Carvalho de Melo
maintainerPATCH 10/14 perf evlist: Refcount mmaps
In reply to Arnaldo Carvalho de MeloFrom: Arnaldo Carvalho de Melo <[email protected]>
We need to know how many fds are using a perf mmap via PERF_EVENT_IOC_SET_OUTPUT, so that we can know when to ditch an mmap, refcount it.
v2: Automatically unmap it when the refcount hits one, which will happen when all fds are filtered by perf_evlist__filter_pollfd(), in later patches.
--- tools/perf/util/evlist.c | 47 +++++++++++++++++++++++++++++++++++++++++++++-- tools/perf/util/evlist.h | 6 ++++++ 2 files changed, 51 insertions(+), 2 deletions(-)
tools/perf/util/evlist.c +45 -2
@@ -28,6 +28,8 @@#define FD (e, x, y) (*(int *)xyarray__entry(e->fd, x, y))#define SID (e, x, y) xyarray__entry(e->sample_id, x, y) +static void __perf_evlist__munmap (struct perf_evlist *evlist,int idx); +void perf_evlist__init (struct perf_evlist *evlist,struct cpu_map *cpus,struct thread_map *threads) {
@@ -651,14 +653,36 @@ union perf_event *perf_evlist__mmap_read(struct perf_evlist *evlist, int idx)return event; } +static bool perf_mmap__empty (struct perf_mmap *md) +{ +return perf_mmap__read_head (md) != md->prev; +} + +static void perf_evlist__mmap_get (struct perf_evlist *evlist,int idx) +{ + ++evlist->mmap[idx].refcnt; +} + +static void perf_evlist__mmap_put (struct perf_evlist *evlist,int idx) +{ +BUG_ON (evlist->mmap[idx].refcnt ==0 ); + +if (--evlist->mmap[idx].refcnt ==0 ) +__perf_evlist__munmap (evlist, idx); +} +void perf_evlist__mmap_consume (struct perf_evlist *evlist,int idx) { +struct perf_mmap *md = &evlist->mmap[idx]; +if (!evlist->overwrite) { -struct perf_mmap *md = &evlist->mmap[idx];unsigned int old = md->prev;perf_mmap__write_tail (md, old); } + +if (md->refcnt ==1 &&perf_mmap__empty (md)) +perf_evlist__mmap_put (evlist, idx); }static void __perf_evlist__munmap (struct perf_evlist *evlist,int idx)
@@ -666,6 +690,7 @@ static void __perf_evlist__munmap(struct perf_evlist *evlist, int idx)if (evlist->mmap[idx].base !=NULL ) {munmap (evlist->mmap[idx].base, evlist->mmap_len); evlist->mmap[idx].base =NULL ; + evlist->mmap[idx].refcnt =0 ; } }
@@ -699,6 +724,20 @@ struct mmap_params {static int __perf_evlist__mmap (struct perf_evlist *evlist,int idx,struct mmap_params *mp,int fd) { +/* +* The last one will be done at perf_evlist__mmap_consume(), so that we +* make sure we don't prevent tools from consuming every last event in +* the ring buffer. +* +* I.e. we can get the POLLHUP meaning that the fd doesn't exist +* anymore, but the last events for it are still in the ring buffer, +* waiting to be consumed. +* +* Tools can chose to ignore this at their own discretion, but the +* evlist layer can't just drop it when filtering events in +* perf_evlist__filter_pollfd(). +*/ + evlist->mmap[idx].refcnt =2 ; evlist->mmap[idx].prev =0 ; evlist->mmap[idx].mask = mp->mask; evlist->mmap[idx].base = mmap(NULL, evlist->mmap_len, mp->prot,
@@ -734,10 +773,14 @@ static int perf_evlist__mmap_per_evsel(struct perf_evlist *evlist, int idx, }else {if (ioctl (fd,PERF_EVENT_IOC_SET_OUTPUT , *output) !=0 )return -1 ; + +perf_evlist__mmap_get (evlist, idx); } -if (perf_evlist__add_pollfd (evlist, fd) <0 ) +if (perf_evlist__add_pollfd (evlist, fd) <0 ) { +perf_evlist__mmap_put (evlist, idx);return -1 ; + }if ((evsel->attr.read_format &PERF_FORMAT_ID ) &&perf_evlist__id_add_fd (evlist, evsel, cpu, thread, fd) <0 )
tools/perf/util/evlist.h +6 -0
@@ -18,9 +18,15 @@ struct record_opts;#define PERF_EVLIST__HLIST_BITS 8#define PERF_EVLIST__HLIST_SIZE (1 << PERF_EVLIST__HLIST_BITS) +/** +* struct perf_mmap - perf's ring buffer mmap details +* +* @refcnt - e.g. code using PERF_EVENT_IOC_SET_OUTPUT to share this +*/ struct perf_mmap {void *base;int mask; +int refcnt;unsigned int prev;char event_copy[PERF_SAMPLE_MAX_SIZE ]; };
Signature · 1 line
1.9.3
Arnaldo Carvalho de Melo
maintainerPATCH 11/14 tools lib fd array: Allow associating an integer cookie with each entry
In reply to Arnaldo Carvalho de MeloFrom: Arnaldo Carvalho de Melo <[email protected]>
We will use this in perf's evlist class so that it can, at fdarray__filter() time, to unmap the associated ring buffer.
We may need to have further info associated with each fdarray entry, in that case we'll make that int array a 'union fdarray_priv' one and put a pointer there so that users can stash whatever they want there. For now, an int is enough tho.
v2: Add clarification to the per array entry priv area, as well as make
it a union, which makes usage a bit longer, but if/when we make it use more space by allowing per entry pointers existing users source code will not have to be changed, just rebuilt.
--- tools/lib/api/fd/array.c | 28 ++++++++++++++++++++++++---- tools/lib/api/fd/array.h | 16 +++++++++++++++- tools/perf/tests/fdarray.c | 8 ++++---- tools/perf/util/evlist.c | 2 +- 4 files changed, 44 insertions(+), 10 deletions(-)
tools/lib/api/fd/array.c +24 -4
@@ -13,21 +13,31 @@void fdarray__init (struct fdarray *fda,int nr_autogrow) { fda->entries =NULL ; + fda->priv =NULL ; fda->nr = fda->nr_alloc =0 ; fda->nr_autogrow = nr_autogrow; }int fdarray__grow (struct fdarray *fda,int nr) { +void *priv;int nr_alloc = fda->nr_alloc + nr; +size_t psize =sizeof (fda->priv[0 ]) * nr_alloc;size_t size =sizeof (struct pollfd ) * nr_alloc;struct pollfd *entries =realloc (fda->entries, size);if (entries ==NULL )return -ENOMEM ; + priv =realloc (fda->priv, psize); +if (priv ==NULL ) { +free (entries); +return -ENOMEM ; + } + fda->nr_alloc = nr_alloc; fda->entries = entries; + fda->priv = priv;return 0 ; }
@@ -50,6 +60,7 @@ struct fdarray *fdarray__new(int nr_alloc, int nr_autogrow)void fdarray__exit (struct fdarray *fda) {free (fda->entries); +free (fda->priv);fdarray__init (fda,0 ); }
@@ -61,6 +72,8 @@ void fdarray__delete(struct fdarray *fda)int fdarray__add (struct fdarray *fda,int fd,short revents) { +int pos = fda->nr; +if (fda->nr == fda->nr_alloc &&fdarray__grow (fda, fda->nr_autogrow) <0 )return -ENOMEM ;
@@ -68,10 +81,11 @@ int fdarray__add(struct fdarray *fda, int fd, short revents) fda->entries[fda->nr].fd = fd; fda->entries[fda->nr].events = revents; fda->nr++; -return 0 ; +return pos; } -int fdarray__filter (struct fdarray *fda,short revents) +int fdarray__filter (struct fdarray *fda,short revents, +void (*entry_destructor)(struct fdarray *fda,int fd)) {int fd, nr =0 ;
@@ -79,11 +93,17 @@ int fdarray__filter(struct fdarray *fda, short revents)return 0 ;for (fd =0 ; fd < fda->nr; ++fd) { -if (fda->entries[fd].revents & revents) +if (fda->entries[fd].revents & revents) { +if (entry_destructor) +entry_destructor (fda, fd); +continue ; + } -if (fd != nr) +if (fd != nr) { fda->entries[nr] = fda->entries[fd]; + fda->priv[nr] = fda->priv[fd]; + } ++nr; }
tools/lib/api/fd/array.h +15 -1
@@ -5,11 +5,24 @@struct pollfd ; +/** +* struct fdarray: Array of file descriptors +* +* @priv: Per array entry priv area, users should access just its contents, +* not set it to anything, as it is kept in synch with @entries, being +* realloc'ed, * for instance, in fdarray__{grow,filter}. +* +* I.e. using 'fda->priv[N].idx = * value' where N < fda->nr is ok, +* but doing 'fda->priv = malloc(M)' is not allowed. +*/ struct fdarray {int nr;int nr_alloc;int nr_autogrow;struct pollfd *entries; +union { +int idx; + } *priv; };void fdarray__init (struct fdarray *fda,int nr_autogrow);
@@ -20,7 +33,8 @@ void fdarray__delete(struct fdarray *fda);int fdarray__add (struct fdarray *fda,int fd,short revents);int fdarray__poll (struct fdarray *fda,int timeout); -int fdarray__filter (struct fdarray *fda,short revents); +int fdarray__filter (struct fdarray *fda,short revents, +void (*entry_destructor)(struct fdarray *fda,int fd));int fdarray__grow (struct fdarray *fda,int extra);int fdarray__fprintf (struct fdarray *fda,FILE *fp);
tools/perf/tests/fdarray.c +4 -4
@@ -36,7 +36,7 @@ int test__fdarray__filter(void) }fdarray__init_revents (fda,POLLIN ); - nr_fds =fdarray__filter (fda,POLLHUP ); + nr_fds =fdarray__filter (fda,POLLHUP ,NULL );if (nr_fds != fda->nr_alloc) {pr_debug ("\nfdarray__filter()=%d != %d shouldn't have filtered anything" , nr_fds, fda->nr_alloc);
@@ -44,7 +44,7 @@ int test__fdarray__filter(void) }fdarray__init_revents (fda,POLLHUP ); - nr_fds =fdarray__filter (fda,POLLHUP ); + nr_fds =fdarray__filter (fda,POLLHUP ,NULL );if (nr_fds !=0 ) {pr_debug ("\nfdarray__filter()=%d != %d, should have filtered all fds" , nr_fds, fda->nr_alloc);
@@ -57,7 +57,7 @@ int test__fdarray__filter(void)pr_debug ("\nfiltering all but fda->entries[2]:" );fdarray__fprintf_prefix (fda,"before" , stderr); - nr_fds =fdarray__filter (fda,POLLHUP ); + nr_fds =fdarray__filter (fda,POLLHUP ,NULL );fdarray__fprintf_prefix (fda," after" , stderr);if (nr_fds !=1 ) {pr_debug ("\nfdarray__filter()=%d != 1, should have left just one event" , nr_fds);
@@ -78,7 +78,7 @@ int test__fdarray__filter(void)pr_debug ("\nfiltering all but (fda->entries[0], fda->entries[3]):" );fdarray__fprintf_prefix (fda,"before" , stderr); - nr_fds =fdarray__filter (fda,POLLHUP ); + nr_fds =fdarray__filter (fda,POLLHUP ,NULL );fdarray__fprintf_prefix (fda," after" , stderr);if (nr_fds !=2 ) { pr_debug("\nfdarray__filter()=%d != 2, should have left just two events",
tools/perf/util/evlist.c +1 -1
@@ -435,7 +435,7 @@ int perf_evlist__add_pollfd(struct perf_evlist *evlist, int fd)int perf_evlist__filter_pollfd (struct perf_evlist *evlist,short revents_and_mask) { -return fdarray__filter (&evlist->pollfd, revents_and_mask); +return fdarray__filter (&evlist->pollfd, revents_and_mask,NULL ); }int perf_evlist__poll (struct perf_evlist *evlist,int timeout)
Signature · 1 line
1.9.3
Arnaldo Carvalho de Melo
maintainerPATCH 12/14 perf evlist: Unmap when all refcounts to fd are gone and events drained
In reply to Arnaldo Carvalho de MeloFrom: Arnaldo Carvalho de Melo <[email protected]>
As noticed by receiving a POLLHUP for all its pollfd entries.
That will remove the refcount taken in perf_evlist__mmap_per_evsel(), and when all events are consumed via perf_evlist__mmap_read() + perf_evlist__mmap_consume(), the ring buffer will be unmap'ed.
Thanks to Jiri Olsa for pointing out that we must wait till all events are consumed, not being ok to unmmap just when receiving all the POLLHUPs.
--- tools/perf/util/evlist.c | 35 +++++++++++++++++++++++++++++------ 1 file changed, 29 insertions(+), 6 deletions(-)
tools/perf/util/evlist.c +29 -6
@@ -25,11 +25,12 @@#include <linux/bitops.h> #include <linux/hash.h> +static void perf_evlist__mmap_put (struct perf_evlist *evlist,int idx); +static void __perf_evlist__munmap (struct perf_evlist *evlist,int idx); +#define FD (e, x, y) (*(int *)xyarray__entry(e->fd, x, y))#define SID (e, x, y) xyarray__entry(e->sample_id, x, y) -static void __perf_evlist__munmap (struct perf_evlist *evlist,int idx); -void perf_evlist__init (struct perf_evlist *evlist,struct cpu_map *cpus,struct thread_map *threads) {
@@ -426,16 +427,38 @@ int perf_evlist__alloc_pollfd(struct perf_evlist *evlist)return 0 ; } +static int __perf_evlist__add_pollfd (struct perf_evlist *evlist,int fd,int idx) +{ +int pos =fdarray__add (&evlist->pollfd, fd,POLLIN |POLLERR |POLLHUP ); +/* +* Save the idx so that when we filter out fds POLLHUP'ed we can +* close the associated evlist->mmap[] entry. +*/ +if (pos >=0 ) { + evlist->pollfd.priv[pos].idx = idx; + +fcntl (fd,F_SETFL ,O_NONBLOCK ); + } + +return pos; +} +int perf_evlist__add_pollfd (struct perf_evlist *evlist,int fd) { -fcntl (fd,F_SETFL ,O_NONBLOCK ); +return __perf_evlist__add_pollfd (evlist, fd,-1 ); +} + +static void perf_evlist__munmap_filtered (struct fdarray *fda,int fd) +{ + struct perf_evlist *evlist = container_of(fda, struct perf_evlist, pollfd); -return fdarray__add (&evlist->pollfd, fd,POLLIN |POLLERR |POLLHUP ); +perf_evlist__mmap_put (evlist, fda->priv[fd].idx); }int perf_evlist__filter_pollfd (struct perf_evlist *evlist,short revents_and_mask) { -return fdarray__filter (&evlist->pollfd, revents_and_mask,NULL ); +return fdarray__filter (&evlist->pollfd, revents_and_mask, + perf_evlist__munmap_filtered); }int perf_evlist__poll (struct perf_evlist *evlist,int timeout)
@@ -777,7 +800,7 @@ static int perf_evlist__mmap_per_evsel(struct perf_evlist *evlist, int idx,perf_evlist__mmap_get (evlist, idx); } -if (perf_evlist__add_pollfd (evlist, fd) <0 ) { +if (__perf_evlist__add_pollfd (evlist, fd, idx) <0 ) {perf_evlist__mmap_put (evlist, idx);return -1 ; }
Signature · 1 line
1.9.3
Arnaldo Carvalho de Melo
maintainerPATCH 13/14 perf record: Filter out POLLHUP'ed file descriptors
In reply to Arnaldo Carvalho de MeloFrom: Arnaldo Carvalho de Melo <[email protected]>
So that we don't continue polling on vanished file descriptors, i.e. file descriptors for events monitoring threads that exited.
I.e. the following 'perf record' command now exits as expected, instead of staying in an eternal loop:
$ sleep 5s & $ perf record -p `pidof sleep`
--- tools/perf/builtin-record.c | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-)
tools/perf/builtin-record.c +5 -2
@@ -308,7 +308,7 @@ static int __cmd_record(struct record *rec, int argc, const char **argv)struct record_opts *opts = &rec->opts;struct perf_data_file *file = &rec->file;struct perf_session *session; -bool disabled = false; +bool disabled = false, draining = false; rec->progname = argv[0 ];
@@ -457,7 +457,7 @@ static int __cmd_record(struct record *rec, int argc, const char **argv) }if (hits == rec->samples) { -if (done) +if (done || draining)break ; err =perf_evlist__poll (rec->evlist,-1 );/*
@@ -467,6 +467,9 @@ static int __cmd_record(struct record *rec, int argc, const char **argv)if (err >0 || (err <0 && errno ==EINTR )) err =0 ; waking++; + +if (perf_evlist__filter_pollfd (rec->evlist,POLLERR |POLLHUP ) ==0 ) + draining = true; }/*
Signature · 1 line
1.9.3
Arnaldo Carvalho de Melo
maintainerPATCH 14/14 perf trace: Filter out POLLHUP'ed file descriptors
In reply to Arnaldo Carvalho de MeloFrom: Arnaldo Carvalho de Melo <[email protected]>
So that we don't continue polling on vanished file descriptors, i.e. file descriptors for events monitoring threads that exited.
I.e. the following 'trace' command now exits as expected, instead of staying in an eternal loop:
$ sleep 5s &
$ trace -p `pidof sleep`--- tools/perf/builtin-trace.c | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-)
tools/perf/builtin-trace.c +6 -1
@@ -2044,6 +2044,7 @@ static int trace__run(struct trace *trace, int argc, const char **argv)int err =-1 , i;unsigned long before;const bool forks = argc >0 ; +bool draining = false;char sbuf[STRERR_BUFSIZE ]; trace->live = true;
@@ -2171,8 +2172,12 @@ next_event:if (trace->nr_events == before) {int timeout = done ?100 :-1 ; -if (perf_evlist__poll (evlist, timeout) >0 ) +if (!draining &&perf_evlist__poll (evlist, timeout) >0 ) { +if (perf_evlist__filter_pollfd (evlist,POLLERR |POLLHUP ) ==0 ) + draining = true; + goto again; + } }else { goto again; }
Signature · 1 line
1.9.3
Quoted 81 lines · Arnaldo Carvalho de Melo
Hi Ingo,
Please consider pulling into tip/perf/core.
Best Regards,
- Arnaldo
The following changes since commit
521e8bac67a71a6544274f39d5c61473e0e54ac0:perf/x86/intel/uncore: Update support for client uncore IMC PMU (2014-09-24 14:48:25 +0200)
are available in the git repository at:
git://git.kernel.org/pub/scm/linux/kernel/git/acme/linux.gittags/perf-fdarray-for-mingofor you to fetch changes up to
46fb3c21d20415dd2693570c33d0ea6eb8745e04:perf trace: Filter out POLLHUP'ed file descriptors (2014-09-25 16:46:56 -0300)
----------------------------------------------------------------
Infrastructure:We were not handling POLLHUP notifications for event file descriptors.
Fix it by filtering entries in the events file descriptor array after poll returns, refcounting mmaps so that when the last fd pointing to a perf mmap goes away we do the unmap.
User visible:
Now 'record' and 'trace' properly exit when a target thread exits.
Arnaldo Carvalho de Melo (14):
perf evlist: Introduceperf_evlist__filter_pollfdmethod
perf tests: Add test forperf_evlist__filter_pollfd()
perf evlist: Monitor POLLERR and POLLHUP events too
perf evlist: We need to poll all event file descriptors
perf evlist: Allow growing pollfd on add method
perf tests: Add pollfd growing test
perf kvm stat live: Useperf_evlist__add_pollfd()instead of local equivalent perf evlist: Introduce poll method for common code idiom
tools lib api: Adopt fdarray class from perf's evlist
perf evlist: Refcount mmaps
tools lib fd array: Allow associating an integer cookie with each entry perf evlist: Unmap when all refcounts to fd are gone and events drained perf record: Filter out POLLHUP'ed file descriptors
perf trace: Filter out POLLHUP'ed file descriptorsSigned-off-by Arnaldo Carvalho de Melo <[email protected]>----------------------------------------------------------------
Arnaldo Carvalho de Melo (14):
perf evlist: Introduceperf_evlist__filter_pollfdmethod
perf tests: Add test forperf_evlist__filter_pollfd()
perf evlist: Monitor POLLERR and POLLHUP events too
perf evlist: We need to poll all event file descriptors
perf evlist: Allow growing pollfd on add method
perf tests: Add pollfd growing test
perf kvm stat live: Useperf_evlist__add_pollfd()instead of local equivalent perf evlist: Introduce poll method for common code idiom
tools lib api: Adopt fdarray class from perf's evlist
perf evlist: Refcount mmaps
tools lib fd array: Allow associating an integer cookie with each entry perf evlist: Unmap when all refcounts to fd are gone and events drained perf record: Filter out POLLHUP'ed file descriptors
perf trace: Filter out POLLHUP'ed file descriptorstools/lib/api/Makefile | 7 +- tools/lib/api/fd/array.c | 127 ++++++++++++++++++++++ tools/lib/api/fd/array.h | 46 ++++++++ tools/perf/Makefile.perf | 3 +- tools/perf/builtin-kvm.c | 24 ++--- tools/perf/builtin-record.c | 9 +- tools/perf/builtin-top.c | 4 +- tools/perf/builtin-trace.c | 7 +- tools/perf/tests/builtin-test.c | 8 ++ tools/perf/tests/fdarray.c | 174 ++++++++++++++++++++++++++++++ tools/perf/tests/open-syscall-tp-fields.c | 2 +- tools/perf/tests/perf-record.c | 2 +- tools/perf/tests/task-exit.c | 2 +-
Quoted 8 lines
tools/perf/tests/tests.h | 2 + tools/perf/util/evlist.c | 105 +++++++++++++++--- tools/perf/util/evlist.h | 16 ++- tools/perf/util/python.c | 6 +- 17 files changed, 501 insertions(+), 43 deletions(-) create mode 100644 tools/lib/api/fd/array.c create mode 100644 tools/lib/api/fd/array.h create mode 100644 tools/perf/tests/fdarray.c
Pulled into tip:perf/core, thanks a lot Arnaldo!
Ingo