xref: /openbmc/linux/tools/perf/tests/openat-syscall.c (revision b24413180f5600bcb3bb70fbed5cf186b60864bd)
1*b2441318SGreg Kroah-Hartman // SPDX-License-Identifier: GPL-2.0
2a43783aeSArnaldo Carvalho de Melo #include <errno.h>
3fd20e811SArnaldo Carvalho de Melo #include <inttypes.h>
4fbf99625SJiri Olsa #include <api/fs/tracing_path.h>
58dd2a131SJiri Olsa #include <linux/err.h>
69a3993d4SArnaldo Carvalho de Melo #include <sys/types.h>
79a3993d4SArnaldo Carvalho de Melo #include <sys/stat.h>
89a3993d4SArnaldo Carvalho de Melo #include <fcntl.h>
9fbb2df23SRiku Voipio #include "thread_map.h"
10fbb2df23SRiku Voipio #include "evsel.h"
11fbb2df23SRiku Voipio #include "debug.h"
12fbb2df23SRiku Voipio #include "tests.h"
13fbb2df23SRiku Voipio 
1481f17c90SArnaldo Carvalho de Melo int test__openat_syscall_event(struct test *test __maybe_unused, int subtest __maybe_unused)
15fbb2df23SRiku Voipio {
16fbb2df23SRiku Voipio 	int err = -1, fd;
17fbb2df23SRiku Voipio 	struct perf_evsel *evsel;
18fbb2df23SRiku Voipio 	unsigned int nr_openat_calls = 111, i;
19fbb2df23SRiku Voipio 	struct thread_map *threads = thread_map__new(-1, getpid(), UINT_MAX);
20fbb2df23SRiku Voipio 	char sbuf[STRERR_BUFSIZE];
21fbf99625SJiri Olsa 	char errbuf[BUFSIZ];
22fbb2df23SRiku Voipio 
23fbb2df23SRiku Voipio 	if (threads == NULL) {
24fbb2df23SRiku Voipio 		pr_debug("thread_map__new\n");
25fbb2df23SRiku Voipio 		return -1;
26fbb2df23SRiku Voipio 	}
27fbb2df23SRiku Voipio 
28fbb2df23SRiku Voipio 	evsel = perf_evsel__newtp("syscalls", "sys_enter_openat");
298dd2a131SJiri Olsa 	if (IS_ERR(evsel)) {
30fbf99625SJiri Olsa 		tracing_path__strerror_open_tp(errno, errbuf, sizeof(errbuf), "syscalls", "sys_enter_openat");
3187191383SNamhyung Kim 		pr_debug("%s\n", errbuf);
32fbb2df23SRiku Voipio 		goto out_thread_map_delete;
33fbb2df23SRiku Voipio 	}
34fbb2df23SRiku Voipio 
35fbb2df23SRiku Voipio 	if (perf_evsel__open_per_thread(evsel, threads) < 0) {
36fbb2df23SRiku Voipio 		pr_debug("failed to open counter: %s, "
37fbb2df23SRiku Voipio 			 "tweak /proc/sys/kernel/perf_event_paranoid?\n",
38c8b5f2c9SArnaldo Carvalho de Melo 			 str_error_r(errno, sbuf, sizeof(sbuf)));
39fbb2df23SRiku Voipio 		goto out_evsel_delete;
40fbb2df23SRiku Voipio 	}
41fbb2df23SRiku Voipio 
42fbb2df23SRiku Voipio 	for (i = 0; i < nr_openat_calls; ++i) {
43fbb2df23SRiku Voipio 		fd = openat(0, "/etc/passwd", O_RDONLY);
44fbb2df23SRiku Voipio 		close(fd);
45fbb2df23SRiku Voipio 	}
46fbb2df23SRiku Voipio 
47fbb2df23SRiku Voipio 	if (perf_evsel__read_on_cpu(evsel, 0, 0) < 0) {
48fbb2df23SRiku Voipio 		pr_debug("perf_evsel__read_on_cpu\n");
49fbb2df23SRiku Voipio 		goto out_close_fd;
50fbb2df23SRiku Voipio 	}
51fbb2df23SRiku Voipio 
52a6fa0038SJiri Olsa 	if (perf_counts(evsel->counts, 0, 0)->val != nr_openat_calls) {
53fbb2df23SRiku Voipio 		pr_debug("perf_evsel__read_on_cpu: expected to intercept %d calls, got %" PRIu64 "\n",
54a6fa0038SJiri Olsa 			 nr_openat_calls, perf_counts(evsel->counts, 0, 0)->val);
55fbb2df23SRiku Voipio 		goto out_close_fd;
56fbb2df23SRiku Voipio 	}
57fbb2df23SRiku Voipio 
58fbb2df23SRiku Voipio 	err = 0;
59fbb2df23SRiku Voipio out_close_fd:
60475fb533SAndi Kleen 	perf_evsel__close_fd(evsel);
61fbb2df23SRiku Voipio out_evsel_delete:
62fbb2df23SRiku Voipio 	perf_evsel__delete(evsel);
63fbb2df23SRiku Voipio out_thread_map_delete:
64186fbb74SJiri Olsa 	thread_map__put(threads);
65fbb2df23SRiku Voipio 	return err;
66fbb2df23SRiku Voipio }
67