Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0 |
| 2 | #include <errno.h> |
| 3 | #include <inttypes.h> |
| 4 | /* For the CLR_() macros */ |
| 5 | #include <pthread.h> |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 6 | #include <stdlib.h> |
| 7 | #include <perf/cpumap.h> |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 8 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 9 | #include "debug.h" |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 10 | #include "evlist.h" |
| 11 | #include "evsel.h" |
| 12 | #include "thread_map.h" |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 13 | #include "tests.h" |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 14 | #include "util/mmap.h" |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 15 | #include <linux/err.h> |
| 16 | #include <linux/kernel.h> |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 17 | #include <linux/string.h> |
| 18 | #include <perf/evlist.h> |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 19 | #include <perf/mmap.h> |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 20 | |
| 21 | /* |
| 22 | * This test will generate random numbers of calls to some getpid syscalls, |
| 23 | * then establish an mmap for a group of events that are created to monitor |
| 24 | * the syscalls. |
| 25 | * |
| 26 | * It will receive the events, using mmap, use its PERF_SAMPLE_ID generated |
| 27 | * sample.id field to map back to its respective perf_evsel instance. |
| 28 | * |
| 29 | * Then it checks if the number of syscalls reported as perf events by |
| 30 | * the kernel corresponds to the number of syscalls made. |
| 31 | */ |
| 32 | int test__basic_mmap(struct test *test __maybe_unused, int subtest __maybe_unused) |
| 33 | { |
| 34 | int err = -1; |
| 35 | union perf_event *event; |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 36 | struct perf_thread_map *threads; |
| 37 | struct perf_cpu_map *cpus; |
| 38 | struct evlist *evlist; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 39 | cpu_set_t cpu_set; |
| 40 | const char *syscall_names[] = { "getsid", "getppid", "getpgid", }; |
| 41 | pid_t (*syscalls[])(void) = { (void *)getsid, getppid, (void*)getpgid }; |
| 42 | #define nsyscalls ARRAY_SIZE(syscall_names) |
| 43 | unsigned int nr_events[nsyscalls], |
| 44 | expected_nr_events[nsyscalls], i, j; |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 45 | struct evsel *evsels[nsyscalls], *evsel; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 46 | char sbuf[STRERR_BUFSIZE]; |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 47 | struct mmap *md; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 48 | |
| 49 | threads = thread_map__new(-1, getpid(), UINT_MAX); |
| 50 | if (threads == NULL) { |
| 51 | pr_debug("thread_map__new\n"); |
| 52 | return -1; |
| 53 | } |
| 54 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 55 | cpus = perf_cpu_map__new(NULL); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 56 | if (cpus == NULL) { |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 57 | pr_debug("perf_cpu_map__new\n"); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 58 | goto out_free_threads; |
| 59 | } |
| 60 | |
| 61 | CPU_ZERO(&cpu_set); |
| 62 | CPU_SET(cpus->map[0], &cpu_set); |
| 63 | sched_setaffinity(0, sizeof(cpu_set), &cpu_set); |
| 64 | if (sched_setaffinity(0, sizeof(cpu_set), &cpu_set) < 0) { |
| 65 | pr_debug("sched_setaffinity() failed on CPU %d: %s ", |
| 66 | cpus->map[0], str_error_r(errno, sbuf, sizeof(sbuf))); |
| 67 | goto out_free_cpus; |
| 68 | } |
| 69 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 70 | evlist = evlist__new(); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 71 | if (evlist == NULL) { |
| 72 | pr_debug("perf_evlist__new\n"); |
| 73 | goto out_free_cpus; |
| 74 | } |
| 75 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 76 | perf_evlist__set_maps(&evlist->core, cpus, threads); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 77 | |
| 78 | for (i = 0; i < nsyscalls; ++i) { |
| 79 | char name[64]; |
| 80 | |
| 81 | snprintf(name, sizeof(name), "sys_enter_%s", syscall_names[i]); |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 82 | evsels[i] = evsel__newtp("syscalls", name); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 83 | if (IS_ERR(evsels[i])) { |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 84 | pr_debug("evsel__new(%s)\n", name); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 85 | goto out_delete_evlist; |
| 86 | } |
| 87 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 88 | evsels[i]->core.attr.wakeup_events = 1; |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 89 | evsel__set_sample_id(evsels[i], false); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 90 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 91 | evlist__add(evlist, evsels[i]); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 92 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 93 | if (evsel__open(evsels[i], cpus, threads) < 0) { |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 94 | pr_debug("failed to open counter: %s, " |
| 95 | "tweak /proc/sys/kernel/perf_event_paranoid?\n", |
| 96 | str_error_r(errno, sbuf, sizeof(sbuf))); |
| 97 | goto out_delete_evlist; |
| 98 | } |
| 99 | |
| 100 | nr_events[i] = 0; |
| 101 | expected_nr_events[i] = 1 + rand() % 127; |
| 102 | } |
| 103 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 104 | if (evlist__mmap(evlist, 128) < 0) { |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 105 | pr_debug("failed to mmap events: %d (%s)\n", errno, |
| 106 | str_error_r(errno, sbuf, sizeof(sbuf))); |
| 107 | goto out_delete_evlist; |
| 108 | } |
| 109 | |
| 110 | for (i = 0; i < nsyscalls; ++i) |
| 111 | for (j = 0; j < expected_nr_events[i]; ++j) { |
| 112 | int foo = syscalls[i](); |
| 113 | ++foo; |
| 114 | } |
| 115 | |
| 116 | md = &evlist->mmap[0]; |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 117 | if (perf_mmap__read_init(&md->core) < 0) |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 118 | goto out_init; |
| 119 | |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 120 | while ((event = perf_mmap__read_event(&md->core)) != NULL) { |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 121 | struct perf_sample sample; |
| 122 | |
| 123 | if (event->header.type != PERF_RECORD_SAMPLE) { |
| 124 | pr_debug("unexpected %s event\n", |
| 125 | perf_event__name(event->header.type)); |
| 126 | goto out_delete_evlist; |
| 127 | } |
| 128 | |
| 129 | err = perf_evlist__parse_sample(evlist, event, &sample); |
| 130 | if (err) { |
| 131 | pr_err("Can't parse sample, err = %d\n", err); |
| 132 | goto out_delete_evlist; |
| 133 | } |
| 134 | |
| 135 | err = -1; |
| 136 | evsel = perf_evlist__id2evsel(evlist, sample.id); |
| 137 | if (evsel == NULL) { |
| 138 | pr_debug("event with id %" PRIu64 |
| 139 | " doesn't map to an evsel\n", sample.id); |
| 140 | goto out_delete_evlist; |
| 141 | } |
| 142 | nr_events[evsel->idx]++; |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 143 | perf_mmap__consume(&md->core); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 144 | } |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 145 | perf_mmap__read_done(&md->core); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 146 | |
| 147 | out_init: |
| 148 | err = 0; |
| 149 | evlist__for_each_entry(evlist, evsel) { |
| 150 | if (nr_events[evsel->idx] != expected_nr_events[evsel->idx]) { |
| 151 | pr_debug("expected %d %s events, got %d\n", |
| 152 | expected_nr_events[evsel->idx], |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 153 | evsel__name(evsel), nr_events[evsel->idx]); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 154 | err = -1; |
| 155 | goto out_delete_evlist; |
| 156 | } |
| 157 | } |
| 158 | |
| 159 | out_delete_evlist: |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 160 | evlist__delete(evlist); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 161 | cpus = NULL; |
| 162 | threads = NULL; |
| 163 | out_free_cpus: |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 164 | perf_cpu_map__put(cpus); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 165 | out_free_threads: |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 166 | perf_thread_map__put(threads); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 167 | return err; |
| 168 | } |