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