From bbcdfb324b473373d37c005e2e005f0870cc74ce Mon Sep 17 00:00:00 2001 From: Thiago Macieira Date: Fri, 20 Dec 2013 10:44:03 -0800 Subject: Fix file descriptor leak in the perf event counter We opened the counter at every start(), even if we had already started before. Some of the benchlib's options caused it to start() and stop() over and over again, like -minimumvalue and -minimumtotal, which could leak to fd exhaustion (EMFILE). Change-Id: Ifeb7cca65c758877be600dc22928bea74f499934 Reviewed-by: Robin Burchell Reviewed-by: Jason McDonald --- src/testlib/qbenchmarkperfevents.cpp | 20 +++++++++++--------- 1 file changed, 11 insertions(+), 9 deletions(-) (limited to 'src/testlib/qbenchmarkperfevents.cpp') diff --git a/src/testlib/qbenchmarkperfevents.cpp b/src/testlib/qbenchmarkperfevents.cpp index acc11b09ac..30a0d3ea9c 100644 --- a/src/testlib/qbenchmarkperfevents.cpp +++ b/src/testlib/qbenchmarkperfevents.cpp @@ -515,16 +515,18 @@ void QBenchmarkPerfEventsMeasurer::start() { initPerf(); - // pid == 0 -> attach to the current process - // cpu == -1 -> monitor on all CPUs - // group_fd == -1 -> this is the group leader - // flags == 0 -> reserved, must be zero - fd = perf_event_open(&attr, 0, -1, -1, 0); if (fd == -1) { - perror("QBenchmarkPerfEventsMeasurer::start: perf_event_open"); - exit(1); - } else { - ::fcntl(fd, F_SETFD, FD_CLOEXEC); + // pid == 0 -> attach to the current process + // cpu == -1 -> monitor on all CPUs + // group_fd == -1 -> this is the group leader + // flags == 0 -> reserved, must be zero + fd = perf_event_open(&attr, 0, -1, -1, 0); + if (fd == -1) { + perror("QBenchmarkPerfEventsMeasurer::start: perf_event_open"); + exit(1); + } else { + ::fcntl(fd, F_SETFD, FD_CLOEXEC); + } } // enable the counter -- cgit v1.2.3