blob: cd3dd463783fd047b8ac7caa3719dd01a940e218 [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 Melo8520a982019-08-29 16:18:59 -030012#include <linux/string.h>
Arnaldo Carvalho de Melo9a3993d2017-04-18 11:33:48 -030013#include <api/fs/tracing_path.h>
Jiri Olsabd90517b2012-11-10 01:46:43 +010014#include "evsel.h"
15#include "tests.h"
16#include "thread_map.h"
Arnaldo Carvalho de Melo87ffb6c2019-09-10 16:29:02 +010017#include <perf/cpumap.h>
Jiri Olsabd90517b2012-11-10 01:46:43 +010018#include "debug.h"
Jiri Olsaa9a3a4d2015-06-14 10:19:26 +020019#include "stat.h"
Arnaldo Carvalho de Meloe4aec1b2019-08-21 14:01:24 -030020#include "util/counts.h"
Jiri Olsabd90517b2012-11-10 01:46:43 +010021
Ian Rogers33f44bf2021-11-03 23:41:51 -070022static int test__openat_syscall_event_on_all_cpus(struct test_suite *test __maybe_unused,
Ian Rogersd68f0362021-11-03 23:41:50 -070023 int subtest __maybe_unused)
Jiri Olsabd90517b2012-11-10 01:46:43 +010024{
25 int err = -1, fd, cpu;
Jiri Olsaf8548392019-07-21 13:23:49 +020026 struct perf_cpu_map *cpus;
Jiri Olsa32dcd022019-07-21 13:23:51 +020027 struct evsel *evsel;
Riku Voipio43f322b2015-04-16 16:52:53 +030028 unsigned int nr_openat_calls = 111, i;
Jiri Olsabd90517b2012-11-10 01:46:43 +010029 cpu_set_t cpu_set;
Jiri Olsa9749b902019-07-21 13:23:50 +020030 struct perf_thread_map *threads = thread_map__new(-1, getpid(), UINT_MAX);
Masami Hiramatsuba3dfff2014-08-14 02:22:45 +000031 char sbuf[STRERR_BUFSIZE];
Jiri Olsafbf99622015-09-02 09:56:45 +020032 char errbuf[BUFSIZ];
Jiri Olsabd90517b2012-11-10 01:46:43 +010033
Jiri Olsabd90517b2012-11-10 01:46:43 +010034 if (threads == NULL) {
35 pr_debug("thread_map__new\n");
36 return -1;
37 }
38
Jiri Olsa9c3516d2019-07-21 13:24:30 +020039 cpus = perf_cpu_map__new(NULL);
Jiri Olsabd90517b2012-11-10 01:46:43 +010040 if (cpus == NULL) {
Arnaldo Carvalho de Melo87ffb6c2019-09-10 16:29:02 +010041 pr_debug("perf_cpu_map__new\n");
Jiri Olsabd90517b2012-11-10 01:46:43 +010042 goto out_thread_map_delete;
43 }
44
Jiri Olsabd90517b2012-11-10 01:46:43 +010045 CPU_ZERO(&cpu_set);
46
Arnaldo Carvalho de Melo8f6725a2020-05-06 13:27:04 -030047 evsel = evsel__newtp("syscalls", "sys_enter_openat");
Jiri Olsa8dd2a132015-09-07 10:38:06 +020048 if (IS_ERR(evsel)) {
Jiri Olsafbf99622015-09-02 09:56:45 +020049 tracing_path__strerror_open_tp(errno, errbuf, sizeof(errbuf), "syscalls", "sys_enter_openat");
Namhyung Kim87191382015-10-20 00:23:48 +090050 pr_debug("%s\n", errbuf);
Changbin Du93faa522019-03-16 16:05:54 +080051 goto out_cpu_map_delete;
Jiri Olsabd90517b2012-11-10 01:46:43 +010052 }
53
Jiri Olsa5972d1e2019-07-21 13:24:01 +020054 if (evsel__open(evsel, cpus, threads) < 0) {
Jiri Olsabd90517b2012-11-10 01:46:43 +010055 pr_debug("failed to open counter: %s, "
56 "tweak /proc/sys/kernel/perf_event_paranoid?\n",
Arnaldo Carvalho de Meloc8b5f2c2016-07-06 11:56:20 -030057 str_error_r(errno, sbuf, sizeof(sbuf)));
Jiri Olsabd90517b2012-11-10 01:46:43 +010058 goto out_evsel_delete;
59 }
60
61 for (cpu = 0; cpu < cpus->nr; ++cpu) {
Riku Voipio43f322b2015-04-16 16:52:53 +030062 unsigned int ncalls = nr_openat_calls + cpu;
Jiri Olsabd90517b2012-11-10 01:46:43 +010063 /*
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 Meloc8b5f2c2016-07-06 11:56:20 -030078 str_error_r(errno, sbuf, sizeof(sbuf)));
Jiri Olsabd90517b2012-11-10 01:46:43 +010079 goto out_close_fd;
80 }
81 for (i = 0; i < ncalls; ++i) {
Riku Voipio43f322b2015-04-16 16:52:53 +030082 fd = openat(0, "/etc/passwd", O_RDONLY);
Jiri Olsabd90517b2012-11-10 01:46:43 +010083 close(fd);
84 }
85 CPU_CLR(cpus->map[cpu], &cpu_set);
86 }
87
88 /*
Adam Buchbinderbd1a0be52016-02-24 10:02:25 -080089 * Here we need to explicitly preallocate the counts, as if
Jiri Olsabd90517b2012-11-10 01:46:43 +010090 * we use the auto allocation it will allocate just for 1 cpu,
91 * as we start by cpu 0.
92 */
Arnaldo Carvalho de Melo7d1e2392020-05-06 13:38:26 -030093 if (evsel__alloc_counts(evsel, cpus->nr, 1) < 0) {
94 pr_debug("evsel__alloc_counts(ncpus=%d)\n", cpus->nr);
Jiri Olsabd90517b2012-11-10 01:46:43 +010095 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 Meloea089692020-04-30 11:00:53 -0300106 if (evsel__read_on_cpu(evsel, cpu, 0) < 0) {
107 pr_debug("evsel__read_on_cpu\n");
Jiri Olsabd90517b2012-11-10 01:46:43 +0100108 err = -1;
109 break;
110 }
111
Riku Voipio43f322b2015-04-16 16:52:53 +0300112 expected = nr_openat_calls + cpu;
Jiri Olsaa6fa0032015-06-26 11:29:11 +0200113 if (perf_counts(evsel->counts, cpu, 0)->val != expected) {
Arnaldo Carvalho de Meloea089692020-04-30 11:00:53 -0300114 pr_debug("evsel__read_on_cpu: expected to intercept %d calls on cpu %d, got %" PRIu64 "\n",
Jiri Olsaa6fa0032015-06-26 11:29:11 +0200115 expected, cpus->map[cpu], perf_counts(evsel->counts, cpu, 0)->val);
Jiri Olsabd90517b2012-11-10 01:46:43 +0100116 err = -1;
117 }
118 }
119
Arnaldo Carvalho de Melo7d1e2392020-05-06 13:38:26 -0300120 evsel__free_counts(evsel);
Jiri Olsabd90517b2012-11-10 01:46:43 +0100121out_close_fd:
Jiri Olsa88761fa2019-07-21 13:24:50 +0200122 perf_evsel__close_fd(&evsel->core);
Jiri Olsabd90517b2012-11-10 01:46:43 +0100123out_evsel_delete:
Jiri Olsa5eb2dd22019-07-21 13:23:57 +0200124 evsel__delete(evsel);
Changbin Du93faa522019-03-16 16:05:54 +0800125out_cpu_map_delete:
Jiri Olsa38f01d82019-07-21 13:24:17 +0200126 perf_cpu_map__put(cpus);
Jiri Olsabd90517b2012-11-10 01:46:43 +0100127out_thread_map_delete:
Jiri Olsa7836e522019-07-21 13:24:20 +0200128 perf_thread_map__put(threads);
Jiri Olsabd90517b2012-11-10 01:46:43 +0100129 return err;
130}
Ian Rogersd68f0362021-11-03 23:41:50 -0700131
132DEFINE_SUITE("Detect openat syscall event on all cpus", openat_syscall_event_on_all_cpus);