blob: c531e6deb104799d733f47fbee405b5dca4b861c [file] [log] [blame]
Greg Kroah-Hartmanb2441312017-11-01 15:07:57 +01001// SPDX-License-Identifier: GPL-2.0
Arnaldo Carvalho de Meloa43783a2017-04-18 10:46:11 -03002#include <errno.h>
Arnaldo Carvalho de Melofd20e812017-04-17 15:23:08 -03003#include <inttypes.h>
Arnaldo Carvalho de Melo1fbe7df2016-07-06 12:19:19 -03004/* For the CPU_* macros */
5#include <pthread.h>
6
Arnaldo Carvalho de Melo9a3993d2017-04-18 11:33:48 -03007#include <sys/types.h>
8#include <sys/stat.h>
9#include <fcntl.h>
Jiri Olsa4605eab2015-09-02 09:56:43 +020010#include <api/fs/fs.h>
Jiri Olsa8dd2a132015-09-07 10:38:06 +020011#include <linux/err.h>
Arnaldo Carvalho de Melo9a3993d2017-04-18 11:33:48 -030012#include <api/fs/tracing_path.h>
Jiri Olsabd90517b2012-11-10 01:46:43 +010013#include "evsel.h"
14#include "tests.h"
15#include "thread_map.h"
16#include "cpumap.h"
17#include "debug.h"
Jiri Olsaa9a3a4d2015-06-14 10:19:26 +020018#include "stat.h"
Jiri Olsabd90517b2012-11-10 01:46:43 +010019
Arnaldo Carvalho de Melo81f17c92017-08-03 15:16:31 -030020int test__openat_syscall_event_on_all_cpus(struct test *test __maybe_unused, int subtest __maybe_unused)
Jiri Olsabd90517b2012-11-10 01:46:43 +010021{
22 int err = -1, fd, cpu;
Jiri Olsabd90517b2012-11-10 01:46:43 +010023 struct cpu_map *cpus;
24 struct perf_evsel *evsel;
Riku Voipio43f322b2015-04-16 16:52:53 +030025 unsigned int nr_openat_calls = 111, i;
Jiri Olsabd90517b2012-11-10 01:46:43 +010026 cpu_set_t cpu_set;
Arnaldo Carvalho de Meloa60d7952012-12-10 15:11:43 -030027 struct thread_map *threads = thread_map__new(-1, getpid(), UINT_MAX);
Masami Hiramatsuba3dfff2014-08-14 02:22:45 +000028 char sbuf[STRERR_BUFSIZE];
Jiri Olsafbf99622015-09-02 09:56:45 +020029 char errbuf[BUFSIZ];
Jiri Olsabd90517b2012-11-10 01:46:43 +010030
Jiri Olsabd90517b2012-11-10 01:46:43 +010031 if (threads == NULL) {
32 pr_debug("thread_map__new\n");
33 return -1;
34 }
35
36 cpus = cpu_map__new(NULL);
37 if (cpus == NULL) {
38 pr_debug("cpu_map__new\n");
39 goto out_thread_map_delete;
40 }
41
Jiri Olsabd90517b2012-11-10 01:46:43 +010042 CPU_ZERO(&cpu_set);
43
Riku Voipio43f322b2015-04-16 16:52:53 +030044 evsel = perf_evsel__newtp("syscalls", "sys_enter_openat");
Jiri Olsa8dd2a132015-09-07 10:38:06 +020045 if (IS_ERR(evsel)) {
Jiri Olsafbf99622015-09-02 09:56:45 +020046 tracing_path__strerror_open_tp(errno, errbuf, sizeof(errbuf), "syscalls", "sys_enter_openat");
Namhyung Kim87191382015-10-20 00:23:48 +090047 pr_debug("%s\n", errbuf);
Jiri Olsabd90517b2012-11-10 01:46:43 +010048 goto out_thread_map_delete;
49 }
50
51 if (perf_evsel__open(evsel, cpus, threads) < 0) {
52 pr_debug("failed to open counter: %s, "
53 "tweak /proc/sys/kernel/perf_event_paranoid?\n",
Arnaldo Carvalho de Meloc8b5f2c2016-07-06 11:56:20 -030054 str_error_r(errno, sbuf, sizeof(sbuf)));
Jiri Olsabd90517b2012-11-10 01:46:43 +010055 goto out_evsel_delete;
56 }
57
58 for (cpu = 0; cpu < cpus->nr; ++cpu) {
Riku Voipio43f322b2015-04-16 16:52:53 +030059 unsigned int ncalls = nr_openat_calls + cpu;
Jiri Olsabd90517b2012-11-10 01:46:43 +010060 /*
61 * XXX eventually lift this restriction in a way that
62 * keeps perf building on older glibc installations
63 * without CPU_ALLOC. 1024 cpus in 2010 still seems
64 * a reasonable upper limit tho :-)
65 */
66 if (cpus->map[cpu] >= CPU_SETSIZE) {
67 pr_debug("Ignoring CPU %d\n", cpus->map[cpu]);
68 continue;
69 }
70
71 CPU_SET(cpus->map[cpu], &cpu_set);
72 if (sched_setaffinity(0, sizeof(cpu_set), &cpu_set) < 0) {
73 pr_debug("sched_setaffinity() failed on CPU %d: %s ",
74 cpus->map[cpu],
Arnaldo Carvalho de Meloc8b5f2c2016-07-06 11:56:20 -030075 str_error_r(errno, sbuf, sizeof(sbuf)));
Jiri Olsabd90517b2012-11-10 01:46:43 +010076 goto out_close_fd;
77 }
78 for (i = 0; i < ncalls; ++i) {
Riku Voipio43f322b2015-04-16 16:52:53 +030079 fd = openat(0, "/etc/passwd", O_RDONLY);
Jiri Olsabd90517b2012-11-10 01:46:43 +010080 close(fd);
81 }
82 CPU_CLR(cpus->map[cpu], &cpu_set);
83 }
84
85 /*
Adam Buchbinderbd1a0be52016-02-24 10:02:25 -080086 * Here we need to explicitly preallocate the counts, as if
Jiri Olsabd90517b2012-11-10 01:46:43 +010087 * we use the auto allocation it will allocate just for 1 cpu,
88 * as we start by cpu 0.
89 */
Jiri Olsaa6fa0032015-06-26 11:29:11 +020090 if (perf_evsel__alloc_counts(evsel, cpus->nr, 1) < 0) {
Jiri Olsabd90517b2012-11-10 01:46:43 +010091 pr_debug("perf_evsel__alloc_counts(ncpus=%d)\n", cpus->nr);
92 goto out_close_fd;
93 }
94
95 err = 0;
96
97 for (cpu = 0; cpu < cpus->nr; ++cpu) {
98 unsigned int expected;
99
100 if (cpus->map[cpu] >= CPU_SETSIZE)
101 continue;
102
103 if (perf_evsel__read_on_cpu(evsel, cpu, 0) < 0) {
104 pr_debug("perf_evsel__read_on_cpu\n");
105 err = -1;
106 break;
107 }
108
Riku Voipio43f322b2015-04-16 16:52:53 +0300109 expected = nr_openat_calls + cpu;
Jiri Olsaa6fa0032015-06-26 11:29:11 +0200110 if (perf_counts(evsel->counts, cpu, 0)->val != expected) {
Jiri Olsabd90517b2012-11-10 01:46:43 +0100111 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 +0200112 expected, cpus->map[cpu], perf_counts(evsel->counts, cpu, 0)->val);
Jiri Olsabd90517b2012-11-10 01:46:43 +0100113 err = -1;
114 }
115 }
116
Namhyung Kim43f8e762013-01-25 10:44:44 +0900117 perf_evsel__free_counts(evsel);
Jiri Olsabd90517b2012-11-10 01:46:43 +0100118out_close_fd:
Andi Kleen475fb532017-08-11 16:26:17 -0700119 perf_evsel__close_fd(evsel);
Jiri Olsabd90517b2012-11-10 01:46:43 +0100120out_evsel_delete:
121 perf_evsel__delete(evsel);
122out_thread_map_delete:
Jiri Olsa186fbb72015-06-23 00:36:05 +0200123 thread_map__put(threads);
Jiri Olsabd90517b2012-11-10 01:46:43 +0100124 return err;
125}