Arnaldo Carvalho de Melo | a43783a | 2017-04-18 10:46:11 -0300 | [diff] [blame] | 1 | #include <errno.h> |
Arnaldo Carvalho de Melo | fd20e81 | 2017-04-17 15:23:08 -0300 | [diff] [blame] | 2 | #include <inttypes.h> |
Jiri Olsa | fbf9962 | 2015-09-02 09:56:45 +0200 | [diff] [blame] | 3 | #include <api/fs/tracing_path.h> |
Jiri Olsa | 8dd2a13 | 2015-09-07 10:38:06 +0200 | [diff] [blame] | 4 | #include <linux/err.h> |
Arnaldo Carvalho de Melo | 9a3993d | 2017-04-18 11:33:48 -0300 | [diff] [blame] | 5 | #include <sys/types.h> |
| 6 | #include <sys/stat.h> |
| 7 | #include <fcntl.h> |
Jiri Olsa | d3b59a3 | 2012-11-10 01:46:42 +0100 | [diff] [blame] | 8 | #include "thread_map.h" |
| 9 | #include "evsel.h" |
| 10 | #include "debug.h" |
| 11 | #include "tests.h" |
| 12 | |
Arnaldo Carvalho de Melo | 81f17c9 | 2017-08-03 15:16:31 -0300 | [diff] [blame] | 13 | int test__openat_syscall_event(struct test *test __maybe_unused, int subtest __maybe_unused) |
Jiri Olsa | d3b59a3 | 2012-11-10 01:46:42 +0100 | [diff] [blame] | 14 | { |
| 15 | int err = -1, fd; |
Jiri Olsa | d3b59a3 | 2012-11-10 01:46:42 +0100 | [diff] [blame] | 16 | struct perf_evsel *evsel; |
Riku Voipio | 43f322b | 2015-04-16 16:52:53 +0300 | [diff] [blame] | 17 | unsigned int nr_openat_calls = 111, i; |
Arnaldo Carvalho de Melo | a60d795 | 2012-12-10 15:11:43 -0300 | [diff] [blame] | 18 | struct thread_map *threads = thread_map__new(-1, getpid(), UINT_MAX); |
Masami Hiramatsu | ba3dfff | 2014-08-14 02:22:45 +0000 | [diff] [blame] | 19 | char sbuf[STRERR_BUFSIZE]; |
Jiri Olsa | fbf9962 | 2015-09-02 09:56:45 +0200 | [diff] [blame] | 20 | char errbuf[BUFSIZ]; |
Jiri Olsa | d3b59a3 | 2012-11-10 01:46:42 +0100 | [diff] [blame] | 21 | |
Jiri Olsa | d3b59a3 | 2012-11-10 01:46:42 +0100 | [diff] [blame] | 22 | if (threads == NULL) { |
| 23 | pr_debug("thread_map__new\n"); |
| 24 | return -1; |
| 25 | } |
| 26 | |
Riku Voipio | 43f322b | 2015-04-16 16:52:53 +0300 | [diff] [blame] | 27 | evsel = perf_evsel__newtp("syscalls", "sys_enter_openat"); |
Jiri Olsa | 8dd2a13 | 2015-09-07 10:38:06 +0200 | [diff] [blame] | 28 | if (IS_ERR(evsel)) { |
Jiri Olsa | fbf9962 | 2015-09-02 09:56:45 +0200 | [diff] [blame] | 29 | 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] | 30 | pr_debug("%s\n", errbuf); |
Jiri Olsa | d3b59a3 | 2012-11-10 01:46:42 +0100 | [diff] [blame] | 31 | goto out_thread_map_delete; |
| 32 | } |
| 33 | |
| 34 | if (perf_evsel__open_per_thread(evsel, threads) < 0) { |
| 35 | pr_debug("failed to open counter: %s, " |
| 36 | "tweak /proc/sys/kernel/perf_event_paranoid?\n", |
Arnaldo Carvalho de Melo | c8b5f2c | 2016-07-06 11:56:20 -0300 | [diff] [blame] | 37 | str_error_r(errno, sbuf, sizeof(sbuf))); |
Jiri Olsa | d3b59a3 | 2012-11-10 01:46:42 +0100 | [diff] [blame] | 38 | goto out_evsel_delete; |
| 39 | } |
| 40 | |
Riku Voipio | 43f322b | 2015-04-16 16:52:53 +0300 | [diff] [blame] | 41 | for (i = 0; i < nr_openat_calls; ++i) { |
| 42 | fd = openat(0, "/etc/passwd", O_RDONLY); |
Jiri Olsa | d3b59a3 | 2012-11-10 01:46:42 +0100 | [diff] [blame] | 43 | close(fd); |
| 44 | } |
| 45 | |
| 46 | if (perf_evsel__read_on_cpu(evsel, 0, 0) < 0) { |
| 47 | pr_debug("perf_evsel__read_on_cpu\n"); |
| 48 | goto out_close_fd; |
| 49 | } |
| 50 | |
Jiri Olsa | a6fa003 | 2015-06-26 11:29:11 +0200 | [diff] [blame] | 51 | if (perf_counts(evsel->counts, 0, 0)->val != nr_openat_calls) { |
Jiri Olsa | d3b59a3 | 2012-11-10 01:46:42 +0100 | [diff] [blame] | 52 | pr_debug("perf_evsel__read_on_cpu: expected to intercept %d calls, got %" PRIu64 "\n", |
Jiri Olsa | a6fa003 | 2015-06-26 11:29:11 +0200 | [diff] [blame] | 53 | nr_openat_calls, perf_counts(evsel->counts, 0, 0)->val); |
Jiri Olsa | d3b59a3 | 2012-11-10 01:46:42 +0100 | [diff] [blame] | 54 | goto out_close_fd; |
| 55 | } |
| 56 | |
| 57 | err = 0; |
| 58 | out_close_fd: |
Andi Kleen | 475fb53 | 2017-08-11 16:26:17 -0700 | [diff] [blame] | 59 | perf_evsel__close_fd(evsel); |
Jiri Olsa | d3b59a3 | 2012-11-10 01:46:42 +0100 | [diff] [blame] | 60 | out_evsel_delete: |
| 61 | perf_evsel__delete(evsel); |
| 62 | out_thread_map_delete: |
Jiri Olsa | 186fbb7 | 2015-06-23 00:36:05 +0200 | [diff] [blame] | 63 | thread_map__put(threads); |
Jiri Olsa | d3b59a3 | 2012-11-10 01:46:42 +0100 | [diff] [blame] | 64 | return err; |
| 65 | } |