blob: 1a74dd9fd06792001f111d3ca4b42ce484319704 [file] [log] [blame]
Arnaldo Carvalho de Meloa43783a2017-04-18 10:46:11 -03001#include <errno.h>
Arnaldo Carvalho de Melofd20e812017-04-17 15:23:08 -03002#include <inttypes.h>
Arnaldo Carvalho de Melo1fbe7df2016-07-06 12:19:19 -03003/* For the CPU_* macros */
4#include <pthread.h>
5
Arnaldo Carvalho de Melo9a3993d2017-04-18 11:33:48 -03006#include <sys/types.h>
7#include <sys/stat.h>
8#include <fcntl.h>
Jiri Olsa4605eab2015-09-02 09:56:43 +02009#include <api/fs/fs.h>
Jiri Olsa8dd2a132015-09-07 10:38:06 +020010#include <linux/err.h>
Arnaldo Carvalho de Melo9a3993d2017-04-18 11:33:48 -030011#include <api/fs/tracing_path.h>
Jiri Olsabd905172012-11-10 01:46:43 +010012#include "evsel.h"
13#include "tests.h"
14#include "thread_map.h"
15#include "cpumap.h"
16#include "debug.h"
Jiri Olsaa9a3a4d2015-06-14 10:19:26 +020017#include "stat.h"
Jiri Olsabd905172012-11-10 01:46:43 +010018
Arnaldo Carvalho de Melo721a1f52015-11-19 12:01:48 -030019int test__openat_syscall_event_on_all_cpus(int subtest __maybe_unused)
Jiri Olsabd905172012-11-10 01:46:43 +010020{
21 int err = -1, fd, cpu;
Jiri Olsabd905172012-11-10 01:46:43 +010022 struct cpu_map *cpus;
23 struct perf_evsel *evsel;
Riku Voipio43f322b2015-04-16 16:52:53 +030024 unsigned int nr_openat_calls = 111, i;
Jiri Olsabd905172012-11-10 01:46:43 +010025 cpu_set_t cpu_set;
Arnaldo Carvalho de Meloa60d7952012-12-10 15:11:43 -030026 struct thread_map *threads = thread_map__new(-1, getpid(), UINT_MAX);
Masami Hiramatsuba3dfff2014-08-14 02:22:45 +000027 char sbuf[STRERR_BUFSIZE];
Jiri Olsafbf99622015-09-02 09:56:45 +020028 char errbuf[BUFSIZ];
Jiri Olsabd905172012-11-10 01:46:43 +010029
Jiri Olsabd905172012-11-10 01:46:43 +010030 if (threads == NULL) {
31 pr_debug("thread_map__new\n");
32 return -1;
33 }
34
35 cpus = cpu_map__new(NULL);
36 if (cpus == NULL) {
37 pr_debug("cpu_map__new\n");
38 goto out_thread_map_delete;
39 }
40
Jiri Olsabd905172012-11-10 01:46:43 +010041 CPU_ZERO(&cpu_set);
42
Riku Voipio43f322b2015-04-16 16:52:53 +030043 evsel = perf_evsel__newtp("syscalls", "sys_enter_openat");
Jiri Olsa8dd2a132015-09-07 10:38:06 +020044 if (IS_ERR(evsel)) {
Jiri Olsafbf99622015-09-02 09:56:45 +020045 tracing_path__strerror_open_tp(errno, errbuf, sizeof(errbuf), "syscalls", "sys_enter_openat");
Namhyung Kim87191382015-10-20 00:23:48 +090046 pr_debug("%s\n", errbuf);
Jiri Olsabd905172012-11-10 01:46:43 +010047 goto out_thread_map_delete;
48 }
49
50 if (perf_evsel__open(evsel, cpus, threads) < 0) {
51 pr_debug("failed to open counter: %s, "
52 "tweak /proc/sys/kernel/perf_event_paranoid?\n",
Arnaldo Carvalho de Meloc8b5f2c2016-07-06 11:56:20 -030053 str_error_r(errno, sbuf, sizeof(sbuf)));
Jiri Olsabd905172012-11-10 01:46:43 +010054 goto out_evsel_delete;
55 }
56
57 for (cpu = 0; cpu < cpus->nr; ++cpu) {
Riku Voipio43f322b2015-04-16 16:52:53 +030058 unsigned int ncalls = nr_openat_calls + cpu;
Jiri Olsabd905172012-11-10 01:46:43 +010059 /*
60 * XXX eventually lift this restriction in a way that
61 * keeps perf building on older glibc installations
62 * without CPU_ALLOC. 1024 cpus in 2010 still seems
63 * a reasonable upper limit tho :-)
64 */
65 if (cpus->map[cpu] >= CPU_SETSIZE) {
66 pr_debug("Ignoring CPU %d\n", cpus->map[cpu]);
67 continue;
68 }
69
70 CPU_SET(cpus->map[cpu], &cpu_set);
71 if (sched_setaffinity(0, sizeof(cpu_set), &cpu_set) < 0) {
72 pr_debug("sched_setaffinity() failed on CPU %d: %s ",
73 cpus->map[cpu],
Arnaldo Carvalho de Meloc8b5f2c2016-07-06 11:56:20 -030074 str_error_r(errno, sbuf, sizeof(sbuf)));
Jiri Olsabd905172012-11-10 01:46:43 +010075 goto out_close_fd;
76 }
77 for (i = 0; i < ncalls; ++i) {
Riku Voipio43f322b2015-04-16 16:52:53 +030078 fd = openat(0, "/etc/passwd", O_RDONLY);
Jiri Olsabd905172012-11-10 01:46:43 +010079 close(fd);
80 }
81 CPU_CLR(cpus->map[cpu], &cpu_set);
82 }
83
84 /*
Adam Buchbinderbd1a0be2016-02-24 10:02:25 -080085 * Here we need to explicitly preallocate the counts, as if
Jiri Olsabd905172012-11-10 01:46:43 +010086 * we use the auto allocation it will allocate just for 1 cpu,
87 * as we start by cpu 0.
88 */
Jiri Olsaa6fa0032015-06-26 11:29:11 +020089 if (perf_evsel__alloc_counts(evsel, cpus->nr, 1) < 0) {
Jiri Olsabd905172012-11-10 01:46:43 +010090 pr_debug("perf_evsel__alloc_counts(ncpus=%d)\n", cpus->nr);
91 goto out_close_fd;
92 }
93
94 err = 0;
95
96 for (cpu = 0; cpu < cpus->nr; ++cpu) {
97 unsigned int expected;
98
99 if (cpus->map[cpu] >= CPU_SETSIZE)
100 continue;
101
102 if (perf_evsel__read_on_cpu(evsel, cpu, 0) < 0) {
103 pr_debug("perf_evsel__read_on_cpu\n");
104 err = -1;
105 break;
106 }
107
Riku Voipio43f322b2015-04-16 16:52:53 +0300108 expected = nr_openat_calls + cpu;
Jiri Olsaa6fa0032015-06-26 11:29:11 +0200109 if (perf_counts(evsel->counts, cpu, 0)->val != expected) {
Jiri Olsabd905172012-11-10 01:46:43 +0100110 pr_debug("perf_evsel__read_on_cpu: expected to intercept %d calls on cpu %d, got %" PRIu64 "\n",
Jiri Olsaa6fa0032015-06-26 11:29:11 +0200111 expected, cpus->map[cpu], perf_counts(evsel->counts, cpu, 0)->val);
Jiri Olsabd905172012-11-10 01:46:43 +0100112 err = -1;
113 }
114 }
115
Namhyung Kim43f8e762013-01-25 10:44:44 +0900116 perf_evsel__free_counts(evsel);
Jiri Olsabd905172012-11-10 01:46:43 +0100117out_close_fd:
118 perf_evsel__close_fd(evsel, 1, threads->nr);
119out_evsel_delete:
120 perf_evsel__delete(evsel);
121out_thread_map_delete:
Jiri Olsa186fbb72015-06-23 00:36:05 +0200122 thread_map__put(threads);
Jiri Olsabd905172012-11-10 01:46:43 +0100123 return err;
124}