Arnaldo Carvalho de Melo | 1fbe7df | 2016-07-06 12:19:19 -0300 | [diff] [blame] | 1 | /* For the CLR_() macros */ |
| 2 | #include <pthread.h> |
| 3 | |
Jiri Olsa | a65b9c6 | 2012-11-10 01:46:44 +0100 | [diff] [blame] | 4 | #include "evlist.h" |
| 5 | #include "evsel.h" |
| 6 | #include "thread_map.h" |
| 7 | #include "cpumap.h" |
| 8 | #include "tests.h" |
Jiri Olsa | 8dd2a13 | 2015-09-07 10:38:06 +0200 | [diff] [blame] | 9 | #include <linux/err.h> |
Jiri Olsa | a65b9c6 | 2012-11-10 01:46:44 +0100 | [diff] [blame] | 10 | |
| 11 | /* |
| 12 | * This test will generate random numbers of calls to some getpid syscalls, |
| 13 | * then establish an mmap for a group of events that are created to monitor |
| 14 | * the syscalls. |
| 15 | * |
| 16 | * It will receive the events, using mmap, use its PERF_SAMPLE_ID generated |
| 17 | * sample.id field to map back to its respective perf_evsel instance. |
| 18 | * |
| 19 | * Then it checks if the number of syscalls reported as perf events by |
| 20 | * the kernel corresponds to the number of syscalls made. |
| 21 | */ |
Arnaldo Carvalho de Melo | 721a1f5 | 2015-11-19 12:01:48 -0300 | [diff] [blame] | 22 | int test__basic_mmap(int subtest __maybe_unused) |
Jiri Olsa | a65b9c6 | 2012-11-10 01:46:44 +0100 | [diff] [blame] | 23 | { |
| 24 | int err = -1; |
| 25 | union perf_event *event; |
| 26 | struct thread_map *threads; |
| 27 | struct cpu_map *cpus; |
| 28 | struct perf_evlist *evlist; |
Jiri Olsa | a65b9c6 | 2012-11-10 01:46:44 +0100 | [diff] [blame] | 29 | cpu_set_t cpu_set; |
Riku Voipio | cec8393 | 2015-04-16 16:52:55 +0300 | [diff] [blame] | 30 | const char *syscall_names[] = { "getsid", "getppid", "getpgid", }; |
| 31 | pid_t (*syscalls[])(void) = { (void *)getsid, getppid, (void*)getpgid }; |
Jiri Olsa | a65b9c6 | 2012-11-10 01:46:44 +0100 | [diff] [blame] | 32 | #define nsyscalls ARRAY_SIZE(syscall_names) |
Jiri Olsa | a65b9c6 | 2012-11-10 01:46:44 +0100 | [diff] [blame] | 33 | unsigned int nr_events[nsyscalls], |
| 34 | expected_nr_events[nsyscalls], i, j; |
| 35 | struct perf_evsel *evsels[nsyscalls], *evsel; |
Masami Hiramatsu | ba3dfff | 2014-08-14 02:22:45 +0000 | [diff] [blame] | 36 | char sbuf[STRERR_BUFSIZE]; |
Jiri Olsa | a65b9c6 | 2012-11-10 01:46:44 +0100 | [diff] [blame] | 37 | |
Jiri Olsa | a65b9c6 | 2012-11-10 01:46:44 +0100 | [diff] [blame] | 38 | threads = thread_map__new(-1, getpid(), UINT_MAX); |
| 39 | if (threads == NULL) { |
| 40 | pr_debug("thread_map__new\n"); |
| 41 | return -1; |
| 42 | } |
| 43 | |
| 44 | cpus = cpu_map__new(NULL); |
| 45 | if (cpus == NULL) { |
| 46 | pr_debug("cpu_map__new\n"); |
| 47 | goto out_free_threads; |
| 48 | } |
| 49 | |
| 50 | CPU_ZERO(&cpu_set); |
| 51 | CPU_SET(cpus->map[0], &cpu_set); |
| 52 | sched_setaffinity(0, sizeof(cpu_set), &cpu_set); |
| 53 | if (sched_setaffinity(0, sizeof(cpu_set), &cpu_set) < 0) { |
| 54 | pr_debug("sched_setaffinity() failed on CPU %d: %s ", |
Arnaldo Carvalho de Melo | c8b5f2c | 2016-07-06 11:56:20 -0300 | [diff] [blame] | 55 | cpus->map[0], str_error_r(errno, sbuf, sizeof(sbuf))); |
Jiri Olsa | a65b9c6 | 2012-11-10 01:46:44 +0100 | [diff] [blame] | 56 | goto out_free_cpus; |
| 57 | } |
| 58 | |
Namhyung Kim | 334fe7a | 2013-03-11 16:43:12 +0900 | [diff] [blame] | 59 | evlist = perf_evlist__new(); |
Jiri Olsa | a65b9c6 | 2012-11-10 01:46:44 +0100 | [diff] [blame] | 60 | if (evlist == NULL) { |
| 61 | pr_debug("perf_evlist__new\n"); |
| 62 | goto out_free_cpus; |
| 63 | } |
| 64 | |
Namhyung Kim | 334fe7a | 2013-03-11 16:43:12 +0900 | [diff] [blame] | 65 | perf_evlist__set_maps(evlist, cpus, threads); |
| 66 | |
Jiri Olsa | a65b9c6 | 2012-11-10 01:46:44 +0100 | [diff] [blame] | 67 | for (i = 0; i < nsyscalls; ++i) { |
Arnaldo Carvalho de Melo | a60d795 | 2012-12-10 15:11:43 -0300 | [diff] [blame] | 68 | char name[64]; |
| 69 | |
| 70 | snprintf(name, sizeof(name), "sys_enter_%s", syscall_names[i]); |
Arnaldo Carvalho de Melo | ef50383 | 2013-11-07 16:41:19 -0300 | [diff] [blame] | 71 | evsels[i] = perf_evsel__newtp("syscalls", name); |
Jiri Olsa | 8dd2a13 | 2015-09-07 10:38:06 +0200 | [diff] [blame] | 72 | if (IS_ERR(evsels[i])) { |
Jiri Olsa | a65b9c6 | 2012-11-10 01:46:44 +0100 | [diff] [blame] | 73 | pr_debug("perf_evsel__new\n"); |
Arnaldo Carvalho de Melo | f26e1c7 | 2014-01-03 16:54:12 -0300 | [diff] [blame] | 74 | goto out_delete_evlist; |
Jiri Olsa | a65b9c6 | 2012-11-10 01:46:44 +0100 | [diff] [blame] | 75 | } |
| 76 | |
Arnaldo Carvalho de Melo | a60d795 | 2012-12-10 15:11:43 -0300 | [diff] [blame] | 77 | evsels[i]->attr.wakeup_events = 1; |
Adrian Hunter | 7556257 | 2013-08-27 11:23:09 +0300 | [diff] [blame] | 78 | perf_evsel__set_sample_id(evsels[i], false); |
Arnaldo Carvalho de Melo | a60d795 | 2012-12-10 15:11:43 -0300 | [diff] [blame] | 79 | |
Jiri Olsa | a65b9c6 | 2012-11-10 01:46:44 +0100 | [diff] [blame] | 80 | perf_evlist__add(evlist, evsels[i]); |
| 81 | |
| 82 | if (perf_evsel__open(evsels[i], cpus, threads) < 0) { |
| 83 | pr_debug("failed to open counter: %s, " |
| 84 | "tweak /proc/sys/kernel/perf_event_paranoid?\n", |
Arnaldo Carvalho de Melo | c8b5f2c | 2016-07-06 11:56:20 -0300 | [diff] [blame] | 85 | str_error_r(errno, sbuf, sizeof(sbuf))); |
Arnaldo Carvalho de Melo | f26e1c7 | 2014-01-03 16:54:12 -0300 | [diff] [blame] | 86 | goto out_delete_evlist; |
Jiri Olsa | a65b9c6 | 2012-11-10 01:46:44 +0100 | [diff] [blame] | 87 | } |
Arnaldo Carvalho de Melo | a60d795 | 2012-12-10 15:11:43 -0300 | [diff] [blame] | 88 | |
| 89 | nr_events[i] = 0; |
| 90 | expected_nr_events[i] = 1 + rand() % 127; |
Jiri Olsa | a65b9c6 | 2012-11-10 01:46:44 +0100 | [diff] [blame] | 91 | } |
| 92 | |
| 93 | if (perf_evlist__mmap(evlist, 128, true) < 0) { |
| 94 | pr_debug("failed to mmap events: %d (%s)\n", errno, |
Arnaldo Carvalho de Melo | c8b5f2c | 2016-07-06 11:56:20 -0300 | [diff] [blame] | 95 | str_error_r(errno, sbuf, sizeof(sbuf))); |
Arnaldo Carvalho de Melo | f26e1c7 | 2014-01-03 16:54:12 -0300 | [diff] [blame] | 96 | goto out_delete_evlist; |
Jiri Olsa | a65b9c6 | 2012-11-10 01:46:44 +0100 | [diff] [blame] | 97 | } |
| 98 | |
| 99 | for (i = 0; i < nsyscalls; ++i) |
| 100 | for (j = 0; j < expected_nr_events[i]; ++j) { |
| 101 | int foo = syscalls[i](); |
| 102 | ++foo; |
| 103 | } |
| 104 | |
| 105 | while ((event = perf_evlist__mmap_read(evlist, 0)) != NULL) { |
| 106 | struct perf_sample sample; |
| 107 | |
| 108 | if (event->header.type != PERF_RECORD_SAMPLE) { |
| 109 | pr_debug("unexpected %s event\n", |
| 110 | perf_event__name(event->header.type)); |
Arnaldo Carvalho de Melo | 983874d | 2014-01-03 17:25:49 -0300 | [diff] [blame] | 111 | goto out_delete_evlist; |
Jiri Olsa | a65b9c6 | 2012-11-10 01:46:44 +0100 | [diff] [blame] | 112 | } |
| 113 | |
| 114 | err = perf_evlist__parse_sample(evlist, event, &sample); |
| 115 | if (err) { |
| 116 | pr_err("Can't parse sample, err = %d\n", err); |
Arnaldo Carvalho de Melo | 983874d | 2014-01-03 17:25:49 -0300 | [diff] [blame] | 117 | goto out_delete_evlist; |
Jiri Olsa | a65b9c6 | 2012-11-10 01:46:44 +0100 | [diff] [blame] | 118 | } |
| 119 | |
Arnaldo Carvalho de Melo | c5d3d50d | 2012-12-10 14:58:42 -0300 | [diff] [blame] | 120 | err = -1; |
Jiri Olsa | a65b9c6 | 2012-11-10 01:46:44 +0100 | [diff] [blame] | 121 | evsel = perf_evlist__id2evsel(evlist, sample.id); |
| 122 | if (evsel == NULL) { |
| 123 | pr_debug("event with id %" PRIu64 |
| 124 | " doesn't map to an evsel\n", sample.id); |
Arnaldo Carvalho de Melo | 983874d | 2014-01-03 17:25:49 -0300 | [diff] [blame] | 125 | goto out_delete_evlist; |
Jiri Olsa | a65b9c6 | 2012-11-10 01:46:44 +0100 | [diff] [blame] | 126 | } |
| 127 | nr_events[evsel->idx]++; |
Zhouyi Zhou | 8e50d38 | 2013-10-24 15:43:33 +0800 | [diff] [blame] | 128 | perf_evlist__mmap_consume(evlist, 0); |
Jiri Olsa | a65b9c6 | 2012-11-10 01:46:44 +0100 | [diff] [blame] | 129 | } |
| 130 | |
Arnaldo Carvalho de Melo | c5d3d50d | 2012-12-10 14:58:42 -0300 | [diff] [blame] | 131 | err = 0; |
Arnaldo Carvalho de Melo | e5cadb9 | 2016-06-23 11:26:15 -0300 | [diff] [blame] | 132 | evlist__for_each_entry(evlist, evsel) { |
Jiri Olsa | a65b9c6 | 2012-11-10 01:46:44 +0100 | [diff] [blame] | 133 | if (nr_events[evsel->idx] != expected_nr_events[evsel->idx]) { |
| 134 | pr_debug("expected %d %s events, got %d\n", |
| 135 | expected_nr_events[evsel->idx], |
| 136 | perf_evsel__name(evsel), nr_events[evsel->idx]); |
Arnaldo Carvalho de Melo | c5d3d50d | 2012-12-10 14:58:42 -0300 | [diff] [blame] | 137 | err = -1; |
Arnaldo Carvalho de Melo | 983874d | 2014-01-03 17:25:49 -0300 | [diff] [blame] | 138 | goto out_delete_evlist; |
Jiri Olsa | a65b9c6 | 2012-11-10 01:46:44 +0100 | [diff] [blame] | 139 | } |
| 140 | } |
| 141 | |
Arnaldo Carvalho de Melo | f26e1c7 | 2014-01-03 16:54:12 -0300 | [diff] [blame] | 142 | out_delete_evlist: |
Jiri Olsa | a65b9c6 | 2012-11-10 01:46:44 +0100 | [diff] [blame] | 143 | perf_evlist__delete(evlist); |
Arnaldo Carvalho de Melo | 03ad974 | 2014-01-03 15:56:06 -0300 | [diff] [blame] | 144 | cpus = NULL; |
| 145 | threads = NULL; |
Jiri Olsa | a65b9c6 | 2012-11-10 01:46:44 +0100 | [diff] [blame] | 146 | out_free_cpus: |
Jiri Olsa | f30a79b | 2015-06-23 00:36:04 +0200 | [diff] [blame] | 147 | cpu_map__put(cpus); |
Jiri Olsa | a65b9c6 | 2012-11-10 01:46:44 +0100 | [diff] [blame] | 148 | out_free_threads: |
Jiri Olsa | 186fbb7 | 2015-06-23 00:36:05 +0200 | [diff] [blame] | 149 | thread_map__put(threads); |
Jiri Olsa | a65b9c6 | 2012-11-10 01:46:44 +0100 | [diff] [blame] | 150 | return err; |
| 151 | } |