@@ -1584,11 +1584,22 @@ static const char* bench_result_words3[][5] = {
15841584 static THREAD_LS_T int cycles = -1;
15851585 static THREAD_LS_T struct perf_event_attr atr;
15861586
1587+ /* Try with PERF_FLAG_FD_CLOEXEC first; on older kernels (< 3.14) this
1588+ * fails with EINVAL, so fall back to flags=0 and set FD_CLOEXEC via
1589+ * fcntl() as a best-effort. */
15871590 #define INIT_CYCLE_COUNTER do { \
15881591 atr.type = PERF_TYPE_HARDWARE; \
15891592 atr.config = PERF_COUNT_HW_CPU_CYCLES; \
1590- cycles = (int)syscall(__NR_perf_event_open, &atr, 0, -1, -1, \
1591- PERF_FLAG_FD_CLOEXEC); \
1593+ cycles = (int)syscall(__NR_perf_event_open, &atr, 0, -1, -1, \
1594+ PERF_FLAG_FD_CLOEXEC); \
1595+ if (cycles < 0 && errno == EINVAL) { \
1596+ cycles = (int)syscall(__NR_perf_event_open, &atr, 0, -1, -1, 0); \
1597+ if (cycles >= 0) { \
1598+ int _fdFlags = fcntl(cycles, F_GETFD); \
1599+ if (_fdFlags >= 0) \
1600+ (void)fcntl(cycles, F_SETFD, _fdFlags | FD_CLOEXEC); \
1601+ } \
1602+ } \
15921603 } while (0);
15931604
15941605 #define BEGIN_CYCLES read(cycles, &begin_cycles, sizeof(begin_cycles));
0 commit comments