1 // SPDX-License-Identifier: GPL-2.0
4 #include "util.h" // for sched_getcpu()
5 #include "../perf-sys.h"
11 #include <sys/syscall.h>
12 #include <linux/string.h>
14 static unsigned long flag = PERF_FLAG_FD_CLOEXEC;
16 int __weak sched_getcpu(void)
20 int err = syscall(__NR_getcpu, &cpu, NULL, NULL);
29 static int perf_flag_probe(void)
31 /* use 'safest' configuration as used in evsel__fallback() */
32 struct perf_event_attr attr = {
33 .type = PERF_TYPE_SOFTWARE,
34 .config = PERF_COUNT_SW_CPU_CLOCK,
41 char sbuf[STRERR_BUFSIZE];
48 * Using -1 for the pid is a workaround to avoid gratuitous jump label
52 /* check cloexec flag */
53 fd = sys_perf_event_open(&attr, pid, cpu, -1,
54 PERF_FLAG_FD_CLOEXEC);
55 if (fd < 0 && pid == -1 && errno == EACCES) {
68 WARN_ONCE(err != EINVAL && err != EBUSY && err != EACCES,
69 "perf_event_open(..., PERF_FLAG_FD_CLOEXEC) failed with unexpected error %d (%s)\n",
70 err, str_error_r(err, sbuf, sizeof(sbuf)));
72 /* not supported, confirm error related to PERF_FLAG_FD_CLOEXEC */
74 fd = sys_perf_event_open(&attr, pid, cpu, -1, 0);
75 if (fd < 0 && pid == -1 && errno == EACCES) {
86 if (WARN_ONCE(fd < 0 && err != EBUSY && err != EACCES,
87 "perf_event_open(..., 0) failed unexpectedly with error %d (%s)\n",
88 err, str_error_r(err, sbuf, sizeof(sbuf))))
94 unsigned long perf_event_open_cloexec_flag(void)
99 if (perf_flag_probe() <= 0)