Skip to content

Commit da23133

Browse files
akmistryacmel
authored andcommitted
perf record: Use an eventfd to wakeup when done
The setting and checking of 'done' contains a rare race where the signal handler setting 'done' is run after checking to break the loop, but before waiting in evlist__poll(). In this case, the main loop won't wake up until either another signal is sent, or the perf data fd causes a wake up. The following simple script can trigger this condition (but you might need to run it for several hours): for ((i = 0; i >= 0; i++)) ; do echo "Loop $i" delay=$(echo "scale=4; 0.1 * $RANDOM/32768" | bc) ./perf record -- sleep 30000000 >/dev/null& pid=$! sleep $delay kill -TERM $pid echo "PID $pid" wait $pid done At some point, the loop will stall. Adding logging, even though perf has received the SIGTERM and set 'done = 1', perf will remain sleeping until a second signal is sent. Committer notes: Make this dependent on HAVE_EVENTFD_SUPPORT, so that we continue building on older systems without the eventfd syscall. Signed-off-by: Anand K Mistry <amistry@google.com> Acked-by: Jiri Olsa <jolsa@redhat.com> Cc: Alexander Shishkin <alexander.shishkin@linux.intel.com> Cc: Mark Rutland <mark.rutland@arm.com> Cc: Namhyung Kim <namhyung@kernel.org> Cc: Peter Zijlstra <peterz@infradead.org> Link: http://lore.kernel.org/lkml/20200513122012.v3.1.I4d7421c6bbb1f83ea58419082481082e19097841@changeid Signed-off-by: Arnaldo Carvalho de Melo <acme@redhat.com>
1 parent ba35fe9 commit da23133

1 file changed

Lines changed: 39 additions & 0 deletions

File tree

‎tools/perf/builtin-record.c‎

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,9 @@
5656
#include <unistd.h>
5757
#include <sched.h>
5858
#include <signal.h>
59+
#ifdef HAVE_EVENTFD_SUPPORT
60+
#include <sys/eventfd.h>
61+
#endif
5962
#include <sys/mman.h>
6063
#include <sys/wait.h>
6164
#include <sys/types.h>
@@ -538,6 +541,9 @@ static int record__pushfn(struct mmap *map, void *to, void *bf, size_t size)
538541

539542
static volatile int signr = -1;
540543
static volatile int child_finished;
544+
#ifdef HAVE_EVENTFD_SUPPORT
545+
static int done_fd = -1;
546+
#endif
541547

542548
static void sig_handler(int sig)
543549
{
@@ -547,6 +553,21 @@ static void sig_handler(int sig)
547553
signr = sig;
548554

549555
done = 1;
556+
#ifdef HAVE_EVENTFD_SUPPORT
557+
{
558+
u64 tmp = 1;
559+
/*
560+
* It is possible for this signal handler to run after done is checked
561+
* in the main loop, but before the perf counter fds are polled. If this
562+
* happens, the poll() will continue to wait even though done is set,
563+
* and will only break out if either another signal is received, or the
564+
* counters are ready for read. To ensure the poll() doesn't sleep when
565+
* done is set, use an eventfd (done_fd) to wake up the poll().
566+
*/
567+
if (write(done_fd, &tmp, sizeof(tmp)) < 0)
568+
pr_err("failed to signal wakeup fd, error: %m\n");
569+
}
570+
#endif // HAVE_EVENTFD_SUPPORT
550571
}
551572

552573
static void sigsegv_handler(int sig)
@@ -1547,6 +1568,20 @@ static int __cmd_record(struct record *rec, int argc, const char **argv)
15471568
pr_err("Compression initialization failed.\n");
15481569
return -1;
15491570
}
1571+
#ifdef HAVE_EVENTFD_SUPPORT
1572+
done_fd = eventfd(0, EFD_NONBLOCK);
1573+
if (done_fd < 0) {
1574+
pr_err("Failed to create wakeup eventfd, error: %m\n");
1575+
status = -1;
1576+
goto out_delete_session;
1577+
}
1578+
err = evlist__add_pollfd(rec->evlist, done_fd);
1579+
if (err < 0) {
1580+
pr_err("Failed to add wakeup eventfd to poll list\n");
1581+
status = err;
1582+
goto out_delete_session;
1583+
}
1584+
#endif // HAVE_EVENTFD_SUPPORT
15501585

15511586
session->header.env.comp_type = PERF_COMP_ZSTD;
15521587
session->header.env.comp_level = rec->opts.comp_level;
@@ -1905,6 +1940,10 @@ static int __cmd_record(struct record *rec, int argc, const char **argv)
19051940
}
19061941

19071942
out_delete_session:
1943+
#ifdef HAVE_EVENTFD_SUPPORT
1944+
if (done_fd >= 0)
1945+
close(done_fd);
1946+
#endif
19081947
zstd_fini(&session->zstd_data);
19091948
perf_session__delete(session);
19101949

0 commit comments

Comments
 (0)