blob: f0dcd0ee0afaa0505470ca382a9d597a5e88fb3a [file] [log] [blame]
Adrian Hunterf6edb532014-08-12 18:04:31 +03001#include <sched.h>
Yann Droneaud57480d22014-06-30 22:28:47 +02002#include "util.h"
3#include "../perf.h"
4#include "cloexec.h"
5#include "asm/bug.h"
Masami Hiramatsu6e81c742014-08-14 02:22:36 +00006#include "debug.h"
Arnaldo Carvalho de Meloc7007e92016-07-12 10:29:31 -03007#include <unistd.h>
8#include <asm/unistd.h>
9#include <sys/syscall.h>
Yann Droneaud57480d22014-06-30 22:28:47 +020010
11static unsigned long flag = PERF_FLAG_FD_CLOEXEC;
12
Vinson Leee1e455f2015-03-23 12:09:16 -070013int __weak sched_getcpu(void)
14{
Arnaldo Carvalho de Meloc7007e92016-07-12 10:29:31 -030015#ifdef __NR_getcpu
16 unsigned cpu;
17 int err = syscall(__NR_getcpu, &cpu, NULL, NULL);
18 if (!err)
19 return cpu;
20#else
Vinson Leee1e455f2015-03-23 12:09:16 -070021 errno = ENOSYS;
Arnaldo Carvalho de Meloc7007e92016-07-12 10:29:31 -030022#endif
Vinson Leee1e455f2015-03-23 12:09:16 -070023 return -1;
24}
25
Yann Droneaud57480d22014-06-30 22:28:47 +020026static int perf_flag_probe(void)
27{
28 /* use 'safest' configuration as used in perf_evsel__fallback() */
29 struct perf_event_attr attr = {
Jiri Olsa038fa0b92014-08-03 14:10:36 +020030 .type = PERF_TYPE_SOFTWARE,
Yann Droneaud57480d22014-06-30 22:28:47 +020031 .config = PERF_COUNT_SW_CPU_CLOCK,
Adrian Huntera5b01532014-08-12 18:04:28 +030032 .exclude_kernel = 1,
Yann Droneaud57480d22014-06-30 22:28:47 +020033 };
34 int fd;
35 int err;
Adrian Hunterf6edb532014-08-12 18:04:31 +030036 int cpu;
37 pid_t pid = -1;
Masami Hiramatsu6e81c742014-08-14 02:22:36 +000038 char sbuf[STRERR_BUFSIZE];
Yann Droneaud57480d22014-06-30 22:28:47 +020039
Adrian Hunterf6edb532014-08-12 18:04:31 +030040 cpu = sched_getcpu();
41 if (cpu < 0)
42 cpu = 0;
43
Adrian Hunter48536c912015-02-24 13:20:59 +020044 /*
45 * Using -1 for the pid is a workaround to avoid gratuitous jump label
46 * changes.
47 */
Adrian Hunterf6edb532014-08-12 18:04:31 +030048 while (1) {
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) {
53 pid = 0;
54 continue;
55 }
56 break;
57 }
Yann Droneaud57480d22014-06-30 22:28:47 +020058 err = errno;
59
60 if (fd >= 0) {
61 close(fd);
62 return 1;
63 }
64
Jiri Olsa63914ac2014-08-01 17:46:54 +020065 WARN_ONCE(err != EINVAL && err != EBUSY,
Yann Droneaud57480d22014-06-30 22:28:47 +020066 "perf_event_open(..., PERF_FLAG_FD_CLOEXEC) failed with unexpected error %d (%s)\n",
Arnaldo Carvalho de Meloc8b5f2c2016-07-06 11:56:20 -030067 err, str_error_r(err, sbuf, sizeof(sbuf)));
Yann Droneaud57480d22014-06-30 22:28:47 +020068
69 /* not supported, confirm error related to PERF_FLAG_FD_CLOEXEC */
Adrian Hunter48536c912015-02-24 13:20:59 +020070 while (1) {
71 fd = sys_perf_event_open(&attr, pid, cpu, -1, 0);
72 if (fd < 0 && pid == -1 && errno == EACCES) {
73 pid = 0;
74 continue;
75 }
76 break;
77 }
Yann Droneaud57480d22014-06-30 22:28:47 +020078 err = errno;
79
Adrian Hunter48536c912015-02-24 13:20:59 +020080 if (fd >= 0)
81 close(fd);
82
Jiri Olsa63914ac2014-08-01 17:46:54 +020083 if (WARN_ONCE(fd < 0 && err != EBUSY,
Yann Droneaud57480d22014-06-30 22:28:47 +020084 "perf_event_open(..., 0) failed unexpectedly with error %d (%s)\n",
Arnaldo Carvalho de Meloc8b5f2c2016-07-06 11:56:20 -030085 err, str_error_r(err, sbuf, sizeof(sbuf))))
Yann Droneaud57480d22014-06-30 22:28:47 +020086 return -1;
87
Yann Droneaud57480d22014-06-30 22:28:47 +020088 return 0;
89}
90
91unsigned long perf_event_open_cloexec_flag(void)
92{
93 static bool probed;
94
95 if (!probed) {
96 if (perf_flag_probe() <= 0)
97 flag = 0;
98 probed = true;
99 }
100
101 return flag;
102}