Greg Kroah-Hartman | b244131 | 2017-11-01 15:07:57 +0100 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0 |
Arnaldo Carvalho de Melo | a43783a | 2017-04-18 10:46:11 -0300 | [diff] [blame] | 2 | #include <errno.h> |
Arnaldo Carvalho de Melo | fd20e81 | 2017-04-17 15:23:08 -0300 | [diff] [blame] | 3 | #include <inttypes.h> |
Arnaldo Carvalho de Melo | 1fbe7df | 2016-07-06 12:19:19 -0300 | [diff] [blame] | 4 | /* For the CPU_* macros */ |
| 5 | #include <pthread.h> |
| 6 | |
Arnaldo Carvalho de Melo | 9a3993d | 2017-04-18 11:33:48 -0300 | [diff] [blame] | 7 | #include <sys/types.h> |
| 8 | #include <sys/stat.h> |
| 9 | #include <fcntl.h> |
Jiri Olsa | 4605eab | 2015-09-02 09:56:43 +0200 | [diff] [blame] | 10 | #include <api/fs/fs.h> |
Jiri Olsa | 8dd2a13 | 2015-09-07 10:38:06 +0200 | [diff] [blame] | 11 | #include <linux/err.h> |
Arnaldo Carvalho de Melo | 8520a98 | 2019-08-29 16:18:59 -0300 | [diff] [blame] | 12 | #include <linux/string.h> |
Arnaldo Carvalho de Melo | 9a3993d | 2017-04-18 11:33:48 -0300 | [diff] [blame] | 13 | #include <api/fs/tracing_path.h> |
Jiri Olsa | bd90517b | 2012-11-10 01:46:43 +0100 | [diff] [blame] | 14 | #include "evsel.h" |
| 15 | #include "tests.h" |
| 16 | #include "thread_map.h" |
Arnaldo Carvalho de Melo | 87ffb6c | 2019-09-10 16:29:02 +0100 | [diff] [blame] | 17 | #include <perf/cpumap.h> |
Jiri Olsa | bd90517b | 2012-11-10 01:46:43 +0100 | [diff] [blame] | 18 | #include "debug.h" |
Jiri Olsa | a9a3a4d | 2015-06-14 10:19:26 +0200 | [diff] [blame] | 19 | #include "stat.h" |
Arnaldo Carvalho de Melo | e4aec1b | 2019-08-21 14:01:24 -0300 | [diff] [blame] | 20 | #include "util/counts.h" |
Jiri Olsa | bd90517b | 2012-11-10 01:46:43 +0100 | [diff] [blame] | 21 | |
Ian Rogers | 33f44bf | 2021-11-03 23:41:51 -0700 | [diff] [blame] | 22 | static int test__openat_syscall_event_on_all_cpus(struct test_suite *test __maybe_unused, |
Ian Rogers | d68f036 | 2021-11-03 23:41:50 -0700 | [diff] [blame] | 23 | int subtest __maybe_unused) |
Jiri Olsa | bd90517b | 2012-11-10 01:46:43 +0100 | [diff] [blame] | 24 | { |
| 25 | int err = -1, fd, cpu; |
Jiri Olsa | f854839 | 2019-07-21 13:23:49 +0200 | [diff] [blame] | 26 | struct perf_cpu_map *cpus; |
Jiri Olsa | 32dcd02 | 2019-07-21 13:23:51 +0200 | [diff] [blame] | 27 | struct evsel *evsel; |
Riku Voipio | 43f322b | 2015-04-16 16:52:53 +0300 | [diff] [blame] | 28 | unsigned int nr_openat_calls = 111, i; |
Jiri Olsa | bd90517b | 2012-11-10 01:46:43 +0100 | [diff] [blame] | 29 | cpu_set_t cpu_set; |
Jiri Olsa | 9749b90 | 2019-07-21 13:23:50 +0200 | [diff] [blame] | 30 | struct perf_thread_map *threads = thread_map__new(-1, getpid(), UINT_MAX); |
Masami Hiramatsu | ba3dfff | 2014-08-14 02:22:45 +0000 | [diff] [blame] | 31 | char sbuf[STRERR_BUFSIZE]; |
Jiri Olsa | fbf9962 | 2015-09-02 09:56:45 +0200 | [diff] [blame] | 32 | char errbuf[BUFSIZ]; |
Jiri Olsa | bd90517b | 2012-11-10 01:46:43 +0100 | [diff] [blame] | 33 | |
Jiri Olsa | bd90517b | 2012-11-10 01:46:43 +0100 | [diff] [blame] | 34 | if (threads == NULL) { |
| 35 | pr_debug("thread_map__new\n"); |
| 36 | return -1; |
| 37 | } |
| 38 | |
Jiri Olsa | 9c3516d | 2019-07-21 13:24:30 +0200 | [diff] [blame] | 39 | cpus = perf_cpu_map__new(NULL); |
Jiri Olsa | bd90517b | 2012-11-10 01:46:43 +0100 | [diff] [blame] | 40 | if (cpus == NULL) { |
Arnaldo Carvalho de Melo | 87ffb6c | 2019-09-10 16:29:02 +0100 | [diff] [blame] | 41 | pr_debug("perf_cpu_map__new\n"); |
Jiri Olsa | bd90517b | 2012-11-10 01:46:43 +0100 | [diff] [blame] | 42 | goto out_thread_map_delete; |
| 43 | } |
| 44 | |
Jiri Olsa | bd90517b | 2012-11-10 01:46:43 +0100 | [diff] [blame] | 45 | CPU_ZERO(&cpu_set); |
| 46 | |
Arnaldo Carvalho de Melo | 8f6725a | 2020-05-06 13:27:04 -0300 | [diff] [blame] | 47 | evsel = evsel__newtp("syscalls", "sys_enter_openat"); |
Jiri Olsa | 8dd2a13 | 2015-09-07 10:38:06 +0200 | [diff] [blame] | 48 | if (IS_ERR(evsel)) { |
Jiri Olsa | fbf9962 | 2015-09-02 09:56:45 +0200 | [diff] [blame] | 49 | tracing_path__strerror_open_tp(errno, errbuf, sizeof(errbuf), "syscalls", "sys_enter_openat"); |
Namhyung Kim | 8719138 | 2015-10-20 00:23:48 +0900 | [diff] [blame] | 50 | pr_debug("%s\n", errbuf); |
Changbin Du | 93faa52 | 2019-03-16 16:05:54 +0800 | [diff] [blame] | 51 | goto out_cpu_map_delete; |
Jiri Olsa | bd90517b | 2012-11-10 01:46:43 +0100 | [diff] [blame] | 52 | } |
| 53 | |
Jiri Olsa | 5972d1e | 2019-07-21 13:24:01 +0200 | [diff] [blame] | 54 | if (evsel__open(evsel, cpus, threads) < 0) { |
Jiri Olsa | bd90517b | 2012-11-10 01:46:43 +0100 | [diff] [blame] | 55 | pr_debug("failed to open counter: %s, " |
| 56 | "tweak /proc/sys/kernel/perf_event_paranoid?\n", |
Arnaldo Carvalho de Melo | c8b5f2c | 2016-07-06 11:56:20 -0300 | [diff] [blame] | 57 | str_error_r(errno, sbuf, sizeof(sbuf))); |
Jiri Olsa | bd90517b | 2012-11-10 01:46:43 +0100 | [diff] [blame] | 58 | goto out_evsel_delete; |
| 59 | } |
| 60 | |
| 61 | for (cpu = 0; cpu < cpus->nr; ++cpu) { |
Riku Voipio | 43f322b | 2015-04-16 16:52:53 +0300 | [diff] [blame] | 62 | unsigned int ncalls = nr_openat_calls + cpu; |
Jiri Olsa | bd90517b | 2012-11-10 01:46:43 +0100 | [diff] [blame] | 63 | /* |
| 64 | * XXX eventually lift this restriction in a way that |
| 65 | * keeps perf building on older glibc installations |
| 66 | * without CPU_ALLOC. 1024 cpus in 2010 still seems |
| 67 | * a reasonable upper limit tho :-) |
| 68 | */ |
| 69 | if (cpus->map[cpu] >= CPU_SETSIZE) { |
| 70 | pr_debug("Ignoring CPU %d\n", cpus->map[cpu]); |
| 71 | continue; |
| 72 | } |
| 73 | |
| 74 | CPU_SET(cpus->map[cpu], &cpu_set); |
| 75 | if (sched_setaffinity(0, sizeof(cpu_set), &cpu_set) < 0) { |
| 76 | pr_debug("sched_setaffinity() failed on CPU %d: %s ", |
| 77 | cpus->map[cpu], |
Arnaldo Carvalho de Melo | c8b5f2c | 2016-07-06 11:56:20 -0300 | [diff] [blame] | 78 | str_error_r(errno, sbuf, sizeof(sbuf))); |
Jiri Olsa | bd90517b | 2012-11-10 01:46:43 +0100 | [diff] [blame] | 79 | goto out_close_fd; |
| 80 | } |
| 81 | for (i = 0; i < ncalls; ++i) { |
Riku Voipio | 43f322b | 2015-04-16 16:52:53 +0300 | [diff] [blame] | 82 | fd = openat(0, "/etc/passwd", O_RDONLY); |
Jiri Olsa | bd90517b | 2012-11-10 01:46:43 +0100 | [diff] [blame] | 83 | close(fd); |
| 84 | } |
| 85 | CPU_CLR(cpus->map[cpu], &cpu_set); |
| 86 | } |
| 87 | |
| 88 | /* |
Adam Buchbinder | bd1a0be5 | 2016-02-24 10:02:25 -0800 | [diff] [blame] | 89 | * Here we need to explicitly preallocate the counts, as if |
Jiri Olsa | bd90517b | 2012-11-10 01:46:43 +0100 | [diff] [blame] | 90 | * we use the auto allocation it will allocate just for 1 cpu, |
| 91 | * as we start by cpu 0. |
| 92 | */ |
Arnaldo Carvalho de Melo | 7d1e239 | 2020-05-06 13:38:26 -0300 | [diff] [blame] | 93 | if (evsel__alloc_counts(evsel, cpus->nr, 1) < 0) { |
| 94 | pr_debug("evsel__alloc_counts(ncpus=%d)\n", cpus->nr); |
Jiri Olsa | bd90517b | 2012-11-10 01:46:43 +0100 | [diff] [blame] | 95 | goto out_close_fd; |
| 96 | } |
| 97 | |
| 98 | err = 0; |
| 99 | |
| 100 | for (cpu = 0; cpu < cpus->nr; ++cpu) { |
| 101 | unsigned int expected; |
| 102 | |
| 103 | if (cpus->map[cpu] >= CPU_SETSIZE) |
| 104 | continue; |
| 105 | |
Arnaldo Carvalho de Melo | ea08969 | 2020-04-30 11:00:53 -0300 | [diff] [blame] | 106 | if (evsel__read_on_cpu(evsel, cpu, 0) < 0) { |
| 107 | pr_debug("evsel__read_on_cpu\n"); |
Jiri Olsa | bd90517b | 2012-11-10 01:46:43 +0100 | [diff] [blame] | 108 | err = -1; |
| 109 | break; |
| 110 | } |
| 111 | |
Riku Voipio | 43f322b | 2015-04-16 16:52:53 +0300 | [diff] [blame] | 112 | expected = nr_openat_calls + cpu; |
Jiri Olsa | a6fa003 | 2015-06-26 11:29:11 +0200 | [diff] [blame] | 113 | if (perf_counts(evsel->counts, cpu, 0)->val != expected) { |
Arnaldo Carvalho de Melo | ea08969 | 2020-04-30 11:00:53 -0300 | [diff] [blame] | 114 | pr_debug("evsel__read_on_cpu: expected to intercept %d calls on cpu %d, got %" PRIu64 "\n", |
Jiri Olsa | a6fa003 | 2015-06-26 11:29:11 +0200 | [diff] [blame] | 115 | expected, cpus->map[cpu], perf_counts(evsel->counts, cpu, 0)->val); |
Jiri Olsa | bd90517b | 2012-11-10 01:46:43 +0100 | [diff] [blame] | 116 | err = -1; |
| 117 | } |
| 118 | } |
| 119 | |
Arnaldo Carvalho de Melo | 7d1e239 | 2020-05-06 13:38:26 -0300 | [diff] [blame] | 120 | evsel__free_counts(evsel); |
Jiri Olsa | bd90517b | 2012-11-10 01:46:43 +0100 | [diff] [blame] | 121 | out_close_fd: |
Jiri Olsa | 88761fa | 2019-07-21 13:24:50 +0200 | [diff] [blame] | 122 | perf_evsel__close_fd(&evsel->core); |
Jiri Olsa | bd90517b | 2012-11-10 01:46:43 +0100 | [diff] [blame] | 123 | out_evsel_delete: |
Jiri Olsa | 5eb2dd2 | 2019-07-21 13:23:57 +0200 | [diff] [blame] | 124 | evsel__delete(evsel); |
Changbin Du | 93faa52 | 2019-03-16 16:05:54 +0800 | [diff] [blame] | 125 | out_cpu_map_delete: |
Jiri Olsa | 38f01d8 | 2019-07-21 13:24:17 +0200 | [diff] [blame] | 126 | perf_cpu_map__put(cpus); |
Jiri Olsa | bd90517b | 2012-11-10 01:46:43 +0100 | [diff] [blame] | 127 | out_thread_map_delete: |
Jiri Olsa | 7836e52 | 2019-07-21 13:24:20 +0200 | [diff] [blame] | 128 | perf_thread_map__put(threads); |
Jiri Olsa | bd90517b | 2012-11-10 01:46:43 +0100 | [diff] [blame] | 129 | return err; |
| 130 | } |
Ian Rogers | d68f036 | 2021-11-03 23:41:50 -0700 | [diff] [blame] | 131 | |
| 132 | DEFINE_SUITE("Detect openat syscall event on all cpus", openat_syscall_event_on_all_cpus); |