perf tools: Include errno.h where needed
[linux-2.6-block.git] / tools / perf / tests / openat-syscall-all-cpus.c
CommitLineData
a43783ae 1#include <errno.h>
fd20e811 2#include <inttypes.h>
1fbe7df8
ACM
3/* For the CPU_* macros */
4#include <pthread.h>
5
4605eab3 6#include <api/fs/fs.h>
8dd2a131 7#include <linux/err.h>
bd90517b
JO
8#include "evsel.h"
9#include "tests.h"
10#include "thread_map.h"
11#include "cpumap.h"
12#include "debug.h"
a9a3a4d9 13#include "stat.h"
bd90517b 14
721a1f53 15int test__openat_syscall_event_on_all_cpus(int subtest __maybe_unused)
bd90517b
JO
16{
17 int err = -1, fd, cpu;
bd90517b
JO
18 struct cpu_map *cpus;
19 struct perf_evsel *evsel;
43f322b4 20 unsigned int nr_openat_calls = 111, i;
bd90517b 21 cpu_set_t cpu_set;
a60d7953 22 struct thread_map *threads = thread_map__new(-1, getpid(), UINT_MAX);
ba3dfff8 23 char sbuf[STRERR_BUFSIZE];
fbf99625 24 char errbuf[BUFSIZ];
bd90517b 25
bd90517b
JO
26 if (threads == NULL) {
27 pr_debug("thread_map__new\n");
28 return -1;
29 }
30
31 cpus = cpu_map__new(NULL);
32 if (cpus == NULL) {
33 pr_debug("cpu_map__new\n");
34 goto out_thread_map_delete;
35 }
36
bd90517b
JO
37 CPU_ZERO(&cpu_set);
38
43f322b4 39 evsel = perf_evsel__newtp("syscalls", "sys_enter_openat");
8dd2a131 40 if (IS_ERR(evsel)) {
fbf99625 41 tracing_path__strerror_open_tp(errno, errbuf, sizeof(errbuf), "syscalls", "sys_enter_openat");
87191383 42 pr_debug("%s\n", errbuf);
bd90517b
JO
43 goto out_thread_map_delete;
44 }
45
46 if (perf_evsel__open(evsel, cpus, threads) < 0) {
47 pr_debug("failed to open counter: %s, "
48 "tweak /proc/sys/kernel/perf_event_paranoid?\n",
c8b5f2c9 49 str_error_r(errno, sbuf, sizeof(sbuf)));
bd90517b
JO
50 goto out_evsel_delete;
51 }
52
53 for (cpu = 0; cpu < cpus->nr; ++cpu) {
43f322b4 54 unsigned int ncalls = nr_openat_calls + cpu;
bd90517b
JO
55 /*
56 * XXX eventually lift this restriction in a way that
57 * keeps perf building on older glibc installations
58 * without CPU_ALLOC. 1024 cpus in 2010 still seems
59 * a reasonable upper limit tho :-)
60 */
61 if (cpus->map[cpu] >= CPU_SETSIZE) {
62 pr_debug("Ignoring CPU %d\n", cpus->map[cpu]);
63 continue;
64 }
65
66 CPU_SET(cpus->map[cpu], &cpu_set);
67 if (sched_setaffinity(0, sizeof(cpu_set), &cpu_set) < 0) {
68 pr_debug("sched_setaffinity() failed on CPU %d: %s ",
69 cpus->map[cpu],
c8b5f2c9 70 str_error_r(errno, sbuf, sizeof(sbuf)));
bd90517b
JO
71 goto out_close_fd;
72 }
73 for (i = 0; i < ncalls; ++i) {
43f322b4 74 fd = openat(0, "/etc/passwd", O_RDONLY);
bd90517b
JO
75 close(fd);
76 }
77 CPU_CLR(cpus->map[cpu], &cpu_set);
78 }
79
80 /*
bd1a0be5 81 * Here we need to explicitly preallocate the counts, as if
bd90517b
JO
82 * we use the auto allocation it will allocate just for 1 cpu,
83 * as we start by cpu 0.
84 */
a6fa0038 85 if (perf_evsel__alloc_counts(evsel, cpus->nr, 1) < 0) {
bd90517b
JO
86 pr_debug("perf_evsel__alloc_counts(ncpus=%d)\n", cpus->nr);
87 goto out_close_fd;
88 }
89
90 err = 0;
91
92 for (cpu = 0; cpu < cpus->nr; ++cpu) {
93 unsigned int expected;
94
95 if (cpus->map[cpu] >= CPU_SETSIZE)
96 continue;
97
98 if (perf_evsel__read_on_cpu(evsel, cpu, 0) < 0) {
99 pr_debug("perf_evsel__read_on_cpu\n");
100 err = -1;
101 break;
102 }
103
43f322b4 104 expected = nr_openat_calls + cpu;
a6fa0038 105 if (perf_counts(evsel->counts, cpu, 0)->val != expected) {
bd90517b 106 pr_debug("perf_evsel__read_on_cpu: expected to intercept %d calls on cpu %d, got %" PRIu64 "\n",
a6fa0038 107 expected, cpus->map[cpu], perf_counts(evsel->counts, cpu, 0)->val);
bd90517b
JO
108 err = -1;
109 }
110 }
111
43f8e76e 112 perf_evsel__free_counts(evsel);
bd90517b
JO
113out_close_fd:
114 perf_evsel__close_fd(evsel, 1, threads->nr);
115out_evsel_delete:
116 perf_evsel__delete(evsel);
117out_thread_map_delete:
186fbb74 118 thread_map__put(threads);
bd90517b
JO
119 return err;
120}