8 #include <asm/unistd.h>
9 #include <sys/syscall.h>
11 static unsigned long flag = PERF_FLAG_FD_CLOEXEC;
13 int __weak sched_getcpu(void)
17 int err = syscall(__NR_getcpu, &cpu, NULL, NULL);
26 static int perf_flag_probe(void)
28 /* use 'safest' configuration as used in perf_evsel__fallback() */
29 struct perf_event_attr attr = {
30 .type = PERF_TYPE_SOFTWARE,
31 .config = PERF_COUNT_SW_CPU_CLOCK,
38 char sbuf[STRERR_BUFSIZE];
45 * Using -1 for the pid is a workaround to avoid gratuitous jump label
49 /* check cloexec flag */
50 fd = sys_perf_event_open(&attr, pid, cpu, -1,
51 PERF_FLAG_FD_CLOEXEC);
52 if (fd < 0 && pid == -1 && errno == EACCES) {
65 WARN_ONCE(err != EINVAL && err != EBUSY,
66 "perf_event_open(..., PERF_FLAG_FD_CLOEXEC) failed with unexpected error %d (%s)\n",
67 err, str_error_r(err, sbuf, sizeof(sbuf)));
69 /* not supported, confirm error related to PERF_FLAG_FD_CLOEXEC */
71 fd = sys_perf_event_open(&attr, pid, cpu, -1, 0);
72 if (fd < 0 && pid == -1 && errno == EACCES) {
83 if (WARN_ONCE(fd < 0 && err != EBUSY,
84 "perf_event_open(..., 0) failed unexpectedly with error %d (%s)\n",
85 err, str_error_r(err, sbuf, sizeof(sbuf))))
91 unsigned long perf_event_open_cloexec_flag(void)
96 if (perf_flag_probe() <= 0)