xref: /openbmc/linux/tools/perf/tests/openat-syscall-all-cpus.c (revision 4f2c0a4acffbec01079c28f839422e64ddeff004)
1b2441318SGreg Kroah-Hartman // SPDX-License-Identifier: GPL-2.0
2a43783aeSArnaldo Carvalho de Melo #include <errno.h>
3fd20e811SArnaldo Carvalho de Melo #include <inttypes.h>
41fbe7df8SArnaldo Carvalho de Melo /* For the CPU_* macros */
5*130f267aSIan Rogers #include <sched.h>
61fbe7df8SArnaldo Carvalho de Melo 
79a3993d4SArnaldo Carvalho de Melo #include <sys/types.h>
89a3993d4SArnaldo Carvalho de Melo #include <sys/stat.h>
99a3993d4SArnaldo Carvalho de Melo #include <fcntl.h>
104605eab3SJiri Olsa #include <api/fs/fs.h>
118dd2a131SJiri Olsa #include <linux/err.h>
128520a98dSArnaldo Carvalho de Melo #include <linux/string.h>
139a3993d4SArnaldo Carvalho de Melo #include <api/fs/tracing_path.h>
14fbb2df23SRiku Voipio #include "evsel.h"
15fbb2df23SRiku Voipio #include "tests.h"
16fbb2df23SRiku Voipio #include "thread_map.h"
1787ffb6c6SArnaldo Carvalho de Melo #include <perf/cpumap.h>
18fbb2df23SRiku Voipio #include "debug.h"
19a9a3a4d9SJiri Olsa #include "stat.h"
20e4aec1b1SArnaldo Carvalho de Melo #include "util/counts.h"
21fbb2df23SRiku Voipio 
test__openat_syscall_event_on_all_cpus(struct test_suite * test __maybe_unused,int subtest __maybe_unused)2233f44bfdSIan Rogers static int test__openat_syscall_event_on_all_cpus(struct test_suite *test __maybe_unused,
23d68f0365SIan Rogers 						  int subtest __maybe_unused)
24fbb2df23SRiku Voipio {
25f9b10c82SIan Rogers 	int err = TEST_FAIL, fd, idx;
266d18804bSIan Rogers 	struct perf_cpu cpu;
27f854839bSJiri Olsa 	struct perf_cpu_map *cpus;
2832dcd021SJiri Olsa 	struct evsel *evsel;
29fbb2df23SRiku Voipio 	unsigned int nr_openat_calls = 111, i;
30fbb2df23SRiku Voipio 	cpu_set_t cpu_set;
319749b90eSJiri Olsa 	struct perf_thread_map *threads = thread_map__new(-1, getpid(), UINT_MAX);
32fbb2df23SRiku Voipio 	char sbuf[STRERR_BUFSIZE];
33fbf99625SJiri Olsa 	char errbuf[BUFSIZ];
34fbb2df23SRiku Voipio 
35fbb2df23SRiku Voipio 	if (threads == NULL) {
36fbb2df23SRiku Voipio 		pr_debug("thread_map__new\n");
37fbb2df23SRiku Voipio 		return -1;
38fbb2df23SRiku Voipio 	}
39fbb2df23SRiku Voipio 
409c3516d1SJiri Olsa 	cpus = perf_cpu_map__new(NULL);
41fbb2df23SRiku Voipio 	if (cpus == NULL) {
4287ffb6c6SArnaldo Carvalho de Melo 		pr_debug("perf_cpu_map__new\n");
43fbb2df23SRiku Voipio 		goto out_thread_map_delete;
44fbb2df23SRiku Voipio 	}
45fbb2df23SRiku Voipio 
46fbb2df23SRiku Voipio 	CPU_ZERO(&cpu_set);
47fbb2df23SRiku Voipio 
488f6725a2SArnaldo Carvalho de Melo 	evsel = evsel__newtp("syscalls", "sys_enter_openat");
498dd2a131SJiri Olsa 	if (IS_ERR(evsel)) {
50fbf99625SJiri Olsa 		tracing_path__strerror_open_tp(errno, errbuf, sizeof(errbuf), "syscalls", "sys_enter_openat");
5187191383SNamhyung Kim 		pr_debug("%s\n", errbuf);
52f9b10c82SIan Rogers 		err = TEST_SKIP;
5393faa52eSChangbin Du 		goto out_cpu_map_delete;
54fbb2df23SRiku Voipio 	}
55fbb2df23SRiku Voipio 
565972d1e0SJiri Olsa 	if (evsel__open(evsel, cpus, threads) < 0) {
57fbb2df23SRiku Voipio 		pr_debug("failed to open counter: %s, "
58fbb2df23SRiku Voipio 			 "tweak /proc/sys/kernel/perf_event_paranoid?\n",
59c8b5f2c9SArnaldo Carvalho de Melo 			 str_error_r(errno, sbuf, sizeof(sbuf)));
60f9b10c82SIan Rogers 		err = TEST_SKIP;
61fbb2df23SRiku Voipio 		goto out_evsel_delete;
62fbb2df23SRiku Voipio 	}
63fbb2df23SRiku Voipio 
64aa11e55aSIan Rogers 	perf_cpu_map__for_each_cpu(cpu, idx, cpus) {
65aa11e55aSIan Rogers 		unsigned int ncalls = nr_openat_calls + idx;
66fbb2df23SRiku Voipio 		/*
67fbb2df23SRiku Voipio 		 * XXX eventually lift this restriction in a way that
68fbb2df23SRiku Voipio 		 * keeps perf building on older glibc installations
69fbb2df23SRiku Voipio 		 * without CPU_ALLOC. 1024 cpus in 2010 still seems
70fbb2df23SRiku Voipio 		 * a reasonable upper limit tho :-)
71fbb2df23SRiku Voipio 		 */
726d18804bSIan Rogers 		if (cpu.cpu >= CPU_SETSIZE) {
736d18804bSIan Rogers 			pr_debug("Ignoring CPU %d\n", cpu.cpu);
74fbb2df23SRiku Voipio 			continue;
75fbb2df23SRiku Voipio 		}
76fbb2df23SRiku Voipio 
776d18804bSIan Rogers 		CPU_SET(cpu.cpu, &cpu_set);
78fbb2df23SRiku Voipio 		if (sched_setaffinity(0, sizeof(cpu_set), &cpu_set) < 0) {
79fbb2df23SRiku Voipio 			pr_debug("sched_setaffinity() failed on CPU %d: %s ",
806d18804bSIan Rogers 				 cpu.cpu,
81c8b5f2c9SArnaldo Carvalho de Melo 				 str_error_r(errno, sbuf, sizeof(sbuf)));
82fbb2df23SRiku Voipio 			goto out_close_fd;
83fbb2df23SRiku Voipio 		}
84fbb2df23SRiku Voipio 		for (i = 0; i < ncalls; ++i) {
85fbb2df23SRiku Voipio 			fd = openat(0, "/etc/passwd", O_RDONLY);
86fbb2df23SRiku Voipio 			close(fd);
87fbb2df23SRiku Voipio 		}
886d18804bSIan Rogers 		CPU_CLR(cpu.cpu, &cpu_set);
89fbb2df23SRiku Voipio 	}
90fbb2df23SRiku Voipio 
912ca0a371SIan Rogers 	evsel->core.cpus = perf_cpu_map__get(cpus);
92fbb2df23SRiku Voipio 
93f9b10c82SIan Rogers 	err = TEST_OK;
94fbb2df23SRiku Voipio 
95aa11e55aSIan Rogers 	perf_cpu_map__for_each_cpu(cpu, idx, cpus) {
96fbb2df23SRiku Voipio 		unsigned int expected;
97fbb2df23SRiku Voipio 
986d18804bSIan Rogers 		if (cpu.cpu >= CPU_SETSIZE)
99fbb2df23SRiku Voipio 			continue;
100fbb2df23SRiku Voipio 
101aa11e55aSIan Rogers 		if (evsel__read_on_cpu(evsel, idx, 0) < 0) {
102ea089692SArnaldo Carvalho de Melo 			pr_debug("evsel__read_on_cpu\n");
103f9b10c82SIan Rogers 			err = TEST_FAIL;
104fbb2df23SRiku Voipio 			break;
105fbb2df23SRiku Voipio 		}
106fbb2df23SRiku Voipio 
107aa11e55aSIan Rogers 		expected = nr_openat_calls + idx;
108aa11e55aSIan Rogers 		if (perf_counts(evsel->counts, idx, 0)->val != expected) {
109ea089692SArnaldo Carvalho de Melo 			pr_debug("evsel__read_on_cpu: expected to intercept %d calls on cpu %d, got %" PRIu64 "\n",
1106d18804bSIan Rogers 				 expected, cpu.cpu, perf_counts(evsel->counts, idx, 0)->val);
111f9b10c82SIan Rogers 			err = TEST_FAIL;
112fbb2df23SRiku Voipio 		}
113fbb2df23SRiku Voipio 	}
114fbb2df23SRiku Voipio 
1157d1e239eSArnaldo Carvalho de Melo 	evsel__free_counts(evsel);
116fbb2df23SRiku Voipio out_close_fd:
11788761fa1SJiri Olsa 	perf_evsel__close_fd(&evsel->core);
118fbb2df23SRiku Voipio out_evsel_delete:
1195eb2dd2aSJiri Olsa 	evsel__delete(evsel);
12093faa52eSChangbin Du out_cpu_map_delete:
12138f01d8dSJiri Olsa 	perf_cpu_map__put(cpus);
122fbb2df23SRiku Voipio out_thread_map_delete:
1237836e52eSJiri Olsa 	perf_thread_map__put(threads);
124fbb2df23SRiku Voipio 	return err;
125fbb2df23SRiku Voipio }
126d68f0365SIan Rogers 
127f9b10c82SIan Rogers 
128f9b10c82SIan Rogers static struct test_case tests__openat_syscall_event_on_all_cpus[] = {
129f9b10c82SIan Rogers 	TEST_CASE_REASON("Detect openat syscall event on all cpus",
130f9b10c82SIan Rogers 			 openat_syscall_event_on_all_cpus,
131f9b10c82SIan Rogers 			 "permissions"),
132f9b10c82SIan Rogers 	{	.name = NULL, }
133f9b10c82SIan Rogers };
134f9b10c82SIan Rogers 
135f9b10c82SIan Rogers struct test_suite suite__openat_syscall_event_on_all_cpus = {
136f9b10c82SIan Rogers 	.desc = "Detect openat syscall event on all cpus",
137f9b10c82SIan Rogers 	.test_cases = tests__openat_syscall_event_on_all_cpus,
138f9b10c82SIan Rogers };
139