Merge tag 'perf-core-for-mingo' of git://git.kernel.org/pub/scm/linux/kernel/git...
[~shefty/rdma-dev.git] / tools / perf / tests / mmap-basic.c
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;
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)
31         unsigned int nr_events[nsyscalls],
32                      expected_nr_events[nsyscalls], i, j;
33         struct perf_evsel *evsels[nsyscalls], *evsel;
34
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
56         evlist = perf_evlist__new(cpus, threads);
57         if (evlist == NULL) {
58                 pr_debug("perf_evlist__new\n");
59                 goto out_free_cpus;
60         }
61
62         for (i = 0; i < nsyscalls; ++i) {
63                 char name[64];
64
65                 snprintf(name, sizeof(name), "sys_enter_%s", syscall_names[i]);
66                 evsels[i] = perf_evsel__newtp("syscalls", name, i);
67                 if (evsels[i] == NULL) {
68                         pr_debug("perf_evsel__new\n");
69                         goto out_free_evlist;
70                 }
71
72                 evsels[i]->attr.wakeup_events = 1;
73                 perf_evsel__set_sample_id(evsels[i]);
74
75                 perf_evlist__add(evlist, evsels[i]);
76
77                 if (perf_evsel__open(evsels[i], cpus, threads) < 0) {
78                         pr_debug("failed to open counter: %s, "
79                                  "tweak /proc/sys/kernel/perf_event_paranoid?\n",
80                                  strerror(errno));
81                         goto out_close_fd;
82                 }
83
84                 nr_events[i] = 0;
85                 expected_nr_events[i] = 1 + rand() % 127;
86         }
87
88         if (perf_evlist__mmap(evlist, 128, true) < 0) {
89                 pr_debug("failed to mmap events: %d (%s)\n", errno,
90                          strerror(errno));
91                 goto out_close_fd;
92         }
93
94         for (i = 0; i < nsyscalls; ++i)
95                 for (j = 0; j < expected_nr_events[i]; ++j) {
96                         int foo = syscalls[i]();
97                         ++foo;
98                 }
99
100         while ((event = perf_evlist__mmap_read(evlist, 0)) != NULL) {
101                 struct perf_sample sample;
102
103                 if (event->header.type != PERF_RECORD_SAMPLE) {
104                         pr_debug("unexpected %s event\n",
105                                  perf_event__name(event->header.type));
106                         goto out_munmap;
107                 }
108
109                 err = perf_evlist__parse_sample(evlist, event, &sample);
110                 if (err) {
111                         pr_err("Can't parse sample, err = %d\n", err);
112                         goto out_munmap;
113                 }
114
115                 err = -1;
116                 evsel = perf_evlist__id2evsel(evlist, sample.id);
117                 if (evsel == NULL) {
118                         pr_debug("event with id %" PRIu64
119                                  " doesn't map to an evsel\n", sample.id);
120                         goto out_munmap;
121                 }
122                 nr_events[evsel->idx]++;
123         }
124
125         err = 0;
126         list_for_each_entry(evsel, &evlist->entries, node) {
127                 if (nr_events[evsel->idx] != expected_nr_events[evsel->idx]) {
128                         pr_debug("expected %d %s events, got %d\n",
129                                  expected_nr_events[evsel->idx],
130                                  perf_evsel__name(evsel), nr_events[evsel->idx]);
131                         err = -1;
132                         goto out_munmap;
133                 }
134         }
135
136 out_munmap:
137         perf_evlist__munmap(evlist);
138 out_close_fd:
139         for (i = 0; i < nsyscalls; ++i)
140                 perf_evsel__close_fd(evsels[i], 1, threads->nr);
141 out_free_evlist:
142         perf_evlist__delete(evlist);
143 out_free_cpus:
144         cpu_map__delete(cpus);
145 out_free_threads:
146         thread_map__delete(threads);
147         return err;
148 }