blob: b5a544d1b381bf64b5a6c4b2059ef694e1315fe9 [file] [log] [blame]
Arnaldo Carvalho de Melo1c6a8002010-04-29 18:58:32 -03001/*
2 * builtin-test.c
3 *
4 * Builtin regression testing command: ever growing number of sanity tests
5 */
6#include "builtin.h"
7
8#include "util/cache.h"
Arnaldo Carvalho de Melo9a8e85a2012-10-24 15:44:41 -02009#include "util/color.h"
Arnaldo Carvalho de Melo1c6a8002010-04-29 18:58:32 -030010#include "util/debug.h"
Arnaldo Carvalho de Meloebf294b2011-11-16 14:03:07 -020011#include "util/debugfs.h"
Arnaldo Carvalho de Melode5fa3a2011-01-15 10:42:46 -020012#include "util/evlist.h"
Arnaldo Carvalho de Melo69d25912012-11-09 11:32:52 -030013#include "util/machine.h"
Arnaldo Carvalho de Melo1c6a8002010-04-29 18:58:32 -030014#include "util/parse-options.h"
Arnaldo Carvalho de Melode5fa3a2011-01-15 10:42:46 -020015#include "util/parse-events.h"
Arnaldo Carvalho de Melo1c6a8002010-04-29 18:58:32 -030016#include "util/symbol.h"
Arnaldo Carvalho de Melofd782602011-01-18 15:15:24 -020017#include "util/thread_map.h"
Jiri Olsacd82a322012-03-15 20:09:17 +010018#include "util/pmu.h"
Arnaldo Carvalho de Melo6a6cd112012-09-18 11:56:28 -030019#include "event-parse.h"
Jiri Olsa13b62562011-07-14 11:25:33 +020020#include "../../include/linux/hw_breakpoint.h"
Arnaldo Carvalho de Melo1c6a8002010-04-29 18:58:32 -030021
Peter Zijlstra08aa0d12011-11-21 14:42:47 +010022#include <sys/mman.h>
23
Irina Tirdea1d037ca2012-09-11 01:15:03 +030024static int vmlinux_matches_kallsyms_filter(struct map *map __maybe_unused,
25 struct symbol *sym)
Arnaldo Carvalho de Melo1c6a8002010-04-29 18:58:32 -030026{
27 bool *visited = symbol__priv(sym);
28 *visited = true;
29 return 0;
30}
31
32static int test__vmlinux_matches_kallsyms(void)
33{
34 int err = -1;
35 struct rb_node *nd;
36 struct symbol *sym;
37 struct map *kallsyms_map, *vmlinux_map;
38 struct machine kallsyms, vmlinux;
39 enum map_type type = MAP__FUNCTION;
40 struct ref_reloc_sym ref_reloc_sym = { .name = "_stext", };
41
42 /*
43 * Step 1:
44 *
45 * Init the machines that will hold kernel, modules obtained from
46 * both vmlinux + .ko files and from /proc/kallsyms split by modules.
47 */
48 machine__init(&kallsyms, "", HOST_KERNEL_ID);
49 machine__init(&vmlinux, "", HOST_KERNEL_ID);
50
51 /*
52 * Step 2:
53 *
54 * Create the kernel maps for kallsyms and the DSO where we will then
55 * load /proc/kallsyms. Also create the modules maps from /proc/modules
56 * and find the .ko files that match them in /lib/modules/`uname -r`/.
57 */
58 if (machine__create_kernel_maps(&kallsyms) < 0) {
59 pr_debug("machine__create_kernel_maps ");
60 return -1;
61 }
62
63 /*
64 * Step 3:
65 *
66 * Load and split /proc/kallsyms into multiple maps, one per module.
67 */
68 if (machine__load_kallsyms(&kallsyms, "/proc/kallsyms", type, NULL) <= 0) {
69 pr_debug("dso__load_kallsyms ");
70 goto out;
71 }
72
73 /*
74 * Step 4:
75 *
76 * kallsyms will be internally on demand sorted by name so that we can
77 * find the reference relocation * symbol, i.e. the symbol we will use
78 * to see if the running kernel was relocated by checking if it has the
79 * same value in the vmlinux file we load.
80 */
81 kallsyms_map = machine__kernel_map(&kallsyms, type);
82
83 sym = map__find_symbol_by_name(kallsyms_map, ref_reloc_sym.name, NULL);
84 if (sym == NULL) {
85 pr_debug("dso__find_symbol_by_name ");
86 goto out;
87 }
88
89 ref_reloc_sym.addr = sym->start;
90
91 /*
92 * Step 5:
93 *
94 * Now repeat step 2, this time for the vmlinux file we'll auto-locate.
95 */
96 if (machine__create_kernel_maps(&vmlinux) < 0) {
97 pr_debug("machine__create_kernel_maps ");
98 goto out;
99 }
100
101 vmlinux_map = machine__kernel_map(&vmlinux, type);
102 map__kmap(vmlinux_map)->ref_reloc_sym = &ref_reloc_sym;
103
104 /*
105 * Step 6:
106 *
107 * Locate a vmlinux file in the vmlinux path that has a buildid that
108 * matches the one of the running kernel.
109 *
110 * While doing that look if we find the ref reloc symbol, if we find it
111 * we'll have its ref_reloc_symbol.unrelocated_addr and then
112 * maps__reloc_vmlinux will notice and set proper ->[un]map_ip routines
113 * to fixup the symbols.
114 */
115 if (machine__load_vmlinux_path(&vmlinux, type,
116 vmlinux_matches_kallsyms_filter) <= 0) {
117 pr_debug("machine__load_vmlinux_path ");
118 goto out;
119 }
120
121 err = 0;
122 /*
123 * Step 7:
124 *
125 * Now look at the symbols in the vmlinux DSO and check if we find all of them
126 * in the kallsyms dso. For the ones that are in both, check its names and
127 * end addresses too.
128 */
129 for (nd = rb_first(&vmlinux_map->dso->symbols[type]); nd; nd = rb_next(nd)) {
Arnaldo Carvalho de Melod3678752010-12-21 23:38:37 -0200130 struct symbol *pair, *first_pair;
131 bool backwards = true;
Arnaldo Carvalho de Melo1c6a8002010-04-29 18:58:32 -0300132
133 sym = rb_entry(nd, struct symbol, rb_node);
Arnaldo Carvalho de Melod3678752010-12-21 23:38:37 -0200134
135 if (sym->start == sym->end)
136 continue;
137
138 first_pair = machine__find_kernel_symbol(&kallsyms, type, sym->start, NULL, NULL);
139 pair = first_pair;
Arnaldo Carvalho de Melo1c6a8002010-04-29 18:58:32 -0300140
141 if (pair && pair->start == sym->start) {
142next_pair:
143 if (strcmp(sym->name, pair->name) == 0) {
144 /*
145 * kallsyms don't have the symbol end, so we
146 * set that by using the next symbol start - 1,
147 * in some cases we get this up to a page
148 * wrong, trace_kmalloc when I was developing
149 * this code was one such example, 2106 bytes
150 * off the real size. More than that and we
151 * _really_ have a problem.
152 */
153 s64 skew = sym->end - pair->end;
154 if (llabs(skew) < page_size)
155 continue;
156
Arnaldo Carvalho de Melo9486aa32011-01-22 20:37:02 -0200157 pr_debug("%#" PRIx64 ": diff end addr for %s v: %#" PRIx64 " k: %#" PRIx64 "\n",
Arnaldo Carvalho de Melo1c6a8002010-04-29 18:58:32 -0300158 sym->start, sym->name, sym->end, pair->end);
159 } else {
Arnaldo Carvalho de Melod3678752010-12-21 23:38:37 -0200160 struct rb_node *nnd;
161detour:
162 nnd = backwards ? rb_prev(&pair->rb_node) :
163 rb_next(&pair->rb_node);
Arnaldo Carvalho de Melo1c6a8002010-04-29 18:58:32 -0300164 if (nnd) {
165 struct symbol *next = rb_entry(nnd, struct symbol, rb_node);
166
167 if (next->start == sym->start) {
168 pair = next;
169 goto next_pair;
170 }
171 }
Arnaldo Carvalho de Melod3678752010-12-21 23:38:37 -0200172
173 if (backwards) {
174 backwards = false;
175 pair = first_pair;
176 goto detour;
177 }
178
Arnaldo Carvalho de Melo9486aa32011-01-22 20:37:02 -0200179 pr_debug("%#" PRIx64 ": diff name v: %s k: %s\n",
Arnaldo Carvalho de Melo1c6a8002010-04-29 18:58:32 -0300180 sym->start, sym->name, pair->name);
181 }
182 } else
Arnaldo Carvalho de Melo9486aa32011-01-22 20:37:02 -0200183 pr_debug("%#" PRIx64 ": %s not on kallsyms\n", sym->start, sym->name);
Arnaldo Carvalho de Melo1c6a8002010-04-29 18:58:32 -0300184
185 err = -1;
186 }
187
188 if (!verbose)
189 goto out;
190
191 pr_info("Maps only in vmlinux:\n");
192
193 for (nd = rb_first(&vmlinux.kmaps.maps[type]); nd; nd = rb_next(nd)) {
194 struct map *pos = rb_entry(nd, struct map, rb_node), *pair;
195 /*
196 * If it is the kernel, kallsyms is always "[kernel.kallsyms]", while
197 * the kernel will have the path for the vmlinux file being used,
198 * so use the short name, less descriptive but the same ("[kernel]" in
199 * both cases.
200 */
201 pair = map_groups__find_by_name(&kallsyms.kmaps, type,
202 (pos->dso->kernel ?
203 pos->dso->short_name :
204 pos->dso->name));
205 if (pair)
206 pair->priv = 1;
207 else
208 map__fprintf(pos, stderr);
209 }
210
211 pr_info("Maps in vmlinux with a different name in kallsyms:\n");
212
213 for (nd = rb_first(&vmlinux.kmaps.maps[type]); nd; nd = rb_next(nd)) {
214 struct map *pos = rb_entry(nd, struct map, rb_node), *pair;
215
216 pair = map_groups__find(&kallsyms.kmaps, type, pos->start);
217 if (pair == NULL || pair->priv)
218 continue;
219
220 if (pair->start == pos->start) {
221 pair->priv = 1;
Arnaldo Carvalho de Melo9486aa32011-01-22 20:37:02 -0200222 pr_info(" %" PRIx64 "-%" PRIx64 " %" PRIx64 " %s in kallsyms as",
Arnaldo Carvalho de Melo1c6a8002010-04-29 18:58:32 -0300223 pos->start, pos->end, pos->pgoff, pos->dso->name);
224 if (pos->pgoff != pair->pgoff || pos->end != pair->end)
Arnaldo Carvalho de Melo9486aa32011-01-22 20:37:02 -0200225 pr_info(": \n*%" PRIx64 "-%" PRIx64 " %" PRIx64 "",
Arnaldo Carvalho de Melo1c6a8002010-04-29 18:58:32 -0300226 pair->start, pair->end, pair->pgoff);
227 pr_info(" %s\n", pair->dso->name);
228 pair->priv = 1;
229 }
230 }
231
232 pr_info("Maps only in kallsyms:\n");
233
234 for (nd = rb_first(&kallsyms.kmaps.maps[type]);
235 nd; nd = rb_next(nd)) {
236 struct map *pos = rb_entry(nd, struct map, rb_node);
237
238 if (!pos->priv)
239 map__fprintf(pos, stderr);
240 }
241out:
242 return err;
243}
244
Arnaldo Carvalho de Melo02522082011-01-04 11:55:27 -0200245#include "util/cpumap.h"
Arnaldo Carvalho de Melod8548612011-01-04 00:16:20 -0200246#include "util/evsel.h"
247#include <sys/types.h>
248
Arnaldo Carvalho de Melode5fa3a2011-01-15 10:42:46 -0200249static int trace_event__id(const char *evname)
Arnaldo Carvalho de Melod8548612011-01-04 00:16:20 -0200250{
251 char *filename;
252 int err = -1, fd;
253
254 if (asprintf(&filename,
Jiri Olsabaf040a2011-07-14 11:25:34 +0200255 "%s/syscalls/%s/id",
Arnaldo Carvalho de Meloebf294b2011-11-16 14:03:07 -0200256 tracing_events_path, evname) < 0)
Arnaldo Carvalho de Melod8548612011-01-04 00:16:20 -0200257 return -1;
258
259 fd = open(filename, O_RDONLY);
260 if (fd >= 0) {
261 char id[16];
262 if (read(fd, id, sizeof(id)) > 0)
263 err = atoi(id);
264 close(fd);
265 }
266
267 free(filename);
268 return err;
269}
270
271static int test__open_syscall_event(void)
272{
273 int err = -1, fd;
274 struct thread_map *threads;
275 struct perf_evsel *evsel;
Lin Ming23a2f3a2011-01-07 11:11:09 +0800276 struct perf_event_attr attr;
Arnaldo Carvalho de Melod8548612011-01-04 00:16:20 -0200277 unsigned int nr_open_calls = 111, i;
278 int id = trace_event__id("sys_enter_open");
279
280 if (id < 0) {
Arnaldo Carvalho de Melo454a3bb2011-01-04 10:40:08 -0200281 pr_debug("is debugfs mounted on /sys/kernel/debug?\n");
Arnaldo Carvalho de Melod8548612011-01-04 00:16:20 -0200282 return -1;
283 }
284
Arnaldo Carvalho de Melo0d37aa32012-01-19 14:08:15 -0200285 threads = thread_map__new(-1, getpid(), UINT_MAX);
Arnaldo Carvalho de Melod8548612011-01-04 00:16:20 -0200286 if (threads == NULL) {
Arnaldo Carvalho de Melo454a3bb2011-01-04 10:40:08 -0200287 pr_debug("thread_map__new\n");
Arnaldo Carvalho de Melod8548612011-01-04 00:16:20 -0200288 return -1;
289 }
290
Lin Ming23a2f3a2011-01-07 11:11:09 +0800291 memset(&attr, 0, sizeof(attr));
292 attr.type = PERF_TYPE_TRACEPOINT;
293 attr.config = id;
294 evsel = perf_evsel__new(&attr, 0);
Arnaldo Carvalho de Melod8548612011-01-04 00:16:20 -0200295 if (evsel == NULL) {
Arnaldo Carvalho de Melo454a3bb2011-01-04 10:40:08 -0200296 pr_debug("perf_evsel__new\n");
Arnaldo Carvalho de Melod8548612011-01-04 00:16:20 -0200297 goto out_thread_map_delete;
298 }
299
Jiri Olsa6a4bb042012-08-08 12:22:36 +0200300 if (perf_evsel__open_per_thread(evsel, threads) < 0) {
Arnaldo Carvalho de Melo454a3bb2011-01-04 10:40:08 -0200301 pr_debug("failed to open counter: %s, "
302 "tweak /proc/sys/kernel/perf_event_paranoid?\n",
303 strerror(errno));
Arnaldo Carvalho de Melod8548612011-01-04 00:16:20 -0200304 goto out_evsel_delete;
305 }
306
307 for (i = 0; i < nr_open_calls; ++i) {
308 fd = open("/etc/passwd", O_RDONLY);
309 close(fd);
310 }
311
312 if (perf_evsel__read_on_cpu(evsel, 0, 0) < 0) {
Arnaldo Carvalho de Melo5d2cd902011-04-14 11:20:14 -0300313 pr_debug("perf_evsel__read_on_cpu\n");
Arnaldo Carvalho de Melod8548612011-01-04 00:16:20 -0200314 goto out_close_fd;
315 }
316
Arnaldo Carvalho de Melo454a3bb2011-01-04 10:40:08 -0200317 if (evsel->counts->cpu[0].val != nr_open_calls) {
Arnaldo Carvalho de Melo9486aa32011-01-22 20:37:02 -0200318 pr_debug("perf_evsel__read_on_cpu: expected to intercept %d calls, got %" PRIu64 "\n",
Arnaldo Carvalho de Melod8548612011-01-04 00:16:20 -0200319 nr_open_calls, evsel->counts->cpu[0].val);
Arnaldo Carvalho de Melo454a3bb2011-01-04 10:40:08 -0200320 goto out_close_fd;
321 }
Jiri Olsa945aea22012-10-30 23:01:43 +0100322
Arnaldo Carvalho de Melod8548612011-01-04 00:16:20 -0200323 err = 0;
324out_close_fd:
325 perf_evsel__close_fd(evsel, 1, threads->nr);
326out_evsel_delete:
327 perf_evsel__delete(evsel);
328out_thread_map_delete:
329 thread_map__delete(threads);
330 return err;
331}
332
Arnaldo Carvalho de Melo02522082011-01-04 11:55:27 -0200333#include <sched.h>
334
335static int test__open_syscall_event_on_all_cpus(void)
336{
337 int err = -1, fd, cpu;
338 struct thread_map *threads;
339 struct cpu_map *cpus;
340 struct perf_evsel *evsel;
341 struct perf_event_attr attr;
342 unsigned int nr_open_calls = 111, i;
Arnaldo Carvalho de Melo57b84e52011-01-22 23:14:20 -0200343 cpu_set_t cpu_set;
Arnaldo Carvalho de Melo02522082011-01-04 11:55:27 -0200344 int id = trace_event__id("sys_enter_open");
345
346 if (id < 0) {
347 pr_debug("is debugfs mounted on /sys/kernel/debug?\n");
348 return -1;
349 }
350
Arnaldo Carvalho de Melo0d37aa32012-01-19 14:08:15 -0200351 threads = thread_map__new(-1, getpid(), UINT_MAX);
Arnaldo Carvalho de Melo02522082011-01-04 11:55:27 -0200352 if (threads == NULL) {
353 pr_debug("thread_map__new\n");
354 return -1;
355 }
356
357 cpus = cpu_map__new(NULL);
Han Pingtian98d77b72011-01-15 07:00:50 +0800358 if (cpus == NULL) {
359 pr_debug("cpu_map__new\n");
360 goto out_thread_map_delete;
Arnaldo Carvalho de Melo02522082011-01-04 11:55:27 -0200361 }
362
Arnaldo Carvalho de Melo02522082011-01-04 11:55:27 -0200363
Arnaldo Carvalho de Melo57b84e52011-01-22 23:14:20 -0200364 CPU_ZERO(&cpu_set);
Arnaldo Carvalho de Melo02522082011-01-04 11:55:27 -0200365
366 memset(&attr, 0, sizeof(attr));
367 attr.type = PERF_TYPE_TRACEPOINT;
368 attr.config = id;
369 evsel = perf_evsel__new(&attr, 0);
370 if (evsel == NULL) {
371 pr_debug("perf_evsel__new\n");
Arnaldo Carvalho de Melo57b84e52011-01-22 23:14:20 -0200372 goto out_thread_map_delete;
Arnaldo Carvalho de Melo02522082011-01-04 11:55:27 -0200373 }
374
Jiri Olsa6a4bb042012-08-08 12:22:36 +0200375 if (perf_evsel__open(evsel, cpus, threads) < 0) {
Arnaldo Carvalho de Melo02522082011-01-04 11:55:27 -0200376 pr_debug("failed to open counter: %s, "
377 "tweak /proc/sys/kernel/perf_event_paranoid?\n",
378 strerror(errno));
379 goto out_evsel_delete;
380 }
381
382 for (cpu = 0; cpu < cpus->nr; ++cpu) {
383 unsigned int ncalls = nr_open_calls + cpu;
Arnaldo Carvalho de Melo57b84e52011-01-22 23:14:20 -0200384 /*
385 * XXX eventually lift this restriction in a way that
386 * keeps perf building on older glibc installations
387 * without CPU_ALLOC. 1024 cpus in 2010 still seems
388 * a reasonable upper limit tho :-)
389 */
390 if (cpus->map[cpu] >= CPU_SETSIZE) {
391 pr_debug("Ignoring CPU %d\n", cpus->map[cpu]);
392 continue;
393 }
Arnaldo Carvalho de Melo02522082011-01-04 11:55:27 -0200394
Arnaldo Carvalho de Melo57b84e52011-01-22 23:14:20 -0200395 CPU_SET(cpus->map[cpu], &cpu_set);
396 if (sched_setaffinity(0, sizeof(cpu_set), &cpu_set) < 0) {
Han Pingtianffb5e0f2011-01-20 19:47:07 +0800397 pr_debug("sched_setaffinity() failed on CPU %d: %s ",
398 cpus->map[cpu],
399 strerror(errno));
400 goto out_close_fd;
401 }
Arnaldo Carvalho de Melo02522082011-01-04 11:55:27 -0200402 for (i = 0; i < ncalls; ++i) {
403 fd = open("/etc/passwd", O_RDONLY);
404 close(fd);
405 }
Arnaldo Carvalho de Melo57b84e52011-01-22 23:14:20 -0200406 CPU_CLR(cpus->map[cpu], &cpu_set);
Arnaldo Carvalho de Melo02522082011-01-04 11:55:27 -0200407 }
408
409 /*
410 * Here we need to explicitely preallocate the counts, as if
411 * we use the auto allocation it will allocate just for 1 cpu,
412 * as we start by cpu 0.
413 */
414 if (perf_evsel__alloc_counts(evsel, cpus->nr) < 0) {
415 pr_debug("perf_evsel__alloc_counts(ncpus=%d)\n", cpus->nr);
416 goto out_close_fd;
417 }
418
Arnaldo Carvalho de Melod2af9682011-01-14 16:24:49 -0200419 err = 0;
420
Arnaldo Carvalho de Melo02522082011-01-04 11:55:27 -0200421 for (cpu = 0; cpu < cpus->nr; ++cpu) {
422 unsigned int expected;
423
Arnaldo Carvalho de Melo57b84e52011-01-22 23:14:20 -0200424 if (cpus->map[cpu] >= CPU_SETSIZE)
425 continue;
426
Arnaldo Carvalho de Melo02522082011-01-04 11:55:27 -0200427 if (perf_evsel__read_on_cpu(evsel, cpu, 0) < 0) {
Arnaldo Carvalho de Melo5d2cd902011-04-14 11:20:14 -0300428 pr_debug("perf_evsel__read_on_cpu\n");
Arnaldo Carvalho de Melod2af9682011-01-14 16:24:49 -0200429 err = -1;
430 break;
Arnaldo Carvalho de Melo02522082011-01-04 11:55:27 -0200431 }
432
433 expected = nr_open_calls + cpu;
434 if (evsel->counts->cpu[cpu].val != expected) {
Arnaldo Carvalho de Melo9486aa32011-01-22 20:37:02 -0200435 pr_debug("perf_evsel__read_on_cpu: expected to intercept %d calls on cpu %d, got %" PRIu64 "\n",
Han Pingtianffb5e0f2011-01-20 19:47:07 +0800436 expected, cpus->map[cpu], evsel->counts->cpu[cpu].val);
Arnaldo Carvalho de Melod2af9682011-01-14 16:24:49 -0200437 err = -1;
Arnaldo Carvalho de Melo02522082011-01-04 11:55:27 -0200438 }
439 }
440
Arnaldo Carvalho de Melo02522082011-01-04 11:55:27 -0200441out_close_fd:
442 perf_evsel__close_fd(evsel, 1, threads->nr);
443out_evsel_delete:
444 perf_evsel__delete(evsel);
Arnaldo Carvalho de Melo02522082011-01-04 11:55:27 -0200445out_thread_map_delete:
446 thread_map__delete(threads);
447 return err;
448}
449
Arnaldo Carvalho de Melode5fa3a2011-01-15 10:42:46 -0200450/*
451 * This test will generate random numbers of calls to some getpid syscalls,
452 * then establish an mmap for a group of events that are created to monitor
453 * the syscalls.
454 *
455 * It will receive the events, using mmap, use its PERF_SAMPLE_ID generated
456 * sample.id field to map back to its respective perf_evsel instance.
457 *
458 * Then it checks if the number of syscalls reported as perf events by
459 * the kernel corresponds to the number of syscalls made.
460 */
461static int test__basic_mmap(void)
462{
463 int err = -1;
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -0200464 union perf_event *event;
Arnaldo Carvalho de Melode5fa3a2011-01-15 10:42:46 -0200465 struct thread_map *threads;
Arnaldo Carvalho de Melode5fa3a2011-01-15 10:42:46 -0200466 struct cpu_map *cpus;
467 struct perf_evlist *evlist;
468 struct perf_event_attr attr = {
469 .type = PERF_TYPE_TRACEPOINT,
470 .read_format = PERF_FORMAT_ID,
471 .sample_type = PERF_SAMPLE_ID,
472 .watermark = 0,
473 };
474 cpu_set_t cpu_set;
475 const char *syscall_names[] = { "getsid", "getppid", "getpgrp",
476 "getpgid", };
477 pid_t (*syscalls[])(void) = { (void *)getsid, getppid, getpgrp,
478 (void*)getpgid };
479#define nsyscalls ARRAY_SIZE(syscall_names)
480 int ids[nsyscalls];
481 unsigned int nr_events[nsyscalls],
482 expected_nr_events[nsyscalls], i, j;
483 struct perf_evsel *evsels[nsyscalls], *evsel;
484
485 for (i = 0; i < nsyscalls; ++i) {
486 char name[64];
487
488 snprintf(name, sizeof(name), "sys_enter_%s", syscall_names[i]);
489 ids[i] = trace_event__id(name);
490 if (ids[i] < 0) {
491 pr_debug("Is debugfs mounted on /sys/kernel/debug?\n");
492 return -1;
493 }
494 nr_events[i] = 0;
495 expected_nr_events[i] = random() % 257;
496 }
497
Arnaldo Carvalho de Melo0d37aa32012-01-19 14:08:15 -0200498 threads = thread_map__new(-1, getpid(), UINT_MAX);
Arnaldo Carvalho de Melode5fa3a2011-01-15 10:42:46 -0200499 if (threads == NULL) {
500 pr_debug("thread_map__new\n");
501 return -1;
502 }
503
504 cpus = cpu_map__new(NULL);
Han Pingtian54489c182011-01-25 07:39:00 +0800505 if (cpus == NULL) {
506 pr_debug("cpu_map__new\n");
Arnaldo Carvalho de Melode5fa3a2011-01-15 10:42:46 -0200507 goto out_free_threads;
508 }
509
510 CPU_ZERO(&cpu_set);
511 CPU_SET(cpus->map[0], &cpu_set);
512 sched_setaffinity(0, sizeof(cpu_set), &cpu_set);
513 if (sched_setaffinity(0, sizeof(cpu_set), &cpu_set) < 0) {
514 pr_debug("sched_setaffinity() failed on CPU %d: %s ",
515 cpus->map[0], strerror(errno));
516 goto out_free_cpus;
517 }
518
Arnaldo Carvalho de Melo7e2ed092011-01-30 11:59:43 -0200519 evlist = perf_evlist__new(cpus, threads);
Han Pingtian54489c182011-01-25 07:39:00 +0800520 if (evlist == NULL) {
Arnaldo Carvalho de Melode5fa3a2011-01-15 10:42:46 -0200521 pr_debug("perf_evlist__new\n");
522 goto out_free_cpus;
523 }
524
525 /* anonymous union fields, can't be initialized above */
526 attr.wakeup_events = 1;
527 attr.sample_period = 1;
528
Arnaldo Carvalho de Melode5fa3a2011-01-15 10:42:46 -0200529 for (i = 0; i < nsyscalls; ++i) {
530 attr.config = ids[i];
531 evsels[i] = perf_evsel__new(&attr, i);
532 if (evsels[i] == NULL) {
533 pr_debug("perf_evsel__new\n");
534 goto out_free_evlist;
535 }
536
537 perf_evlist__add(evlist, evsels[i]);
538
Jiri Olsa6a4bb042012-08-08 12:22:36 +0200539 if (perf_evsel__open(evsels[i], cpus, threads) < 0) {
Arnaldo Carvalho de Melode5fa3a2011-01-15 10:42:46 -0200540 pr_debug("failed to open counter: %s, "
541 "tweak /proc/sys/kernel/perf_event_paranoid?\n",
542 strerror(errno));
543 goto out_close_fd;
544 }
545 }
546
Arnaldo Carvalho de Melo7e2ed092011-01-30 11:59:43 -0200547 if (perf_evlist__mmap(evlist, 128, true) < 0) {
Arnaldo Carvalho de Melode5fa3a2011-01-15 10:42:46 -0200548 pr_debug("failed to mmap events: %d (%s)\n", errno,
549 strerror(errno));
550 goto out_close_fd;
551 }
552
553 for (i = 0; i < nsyscalls; ++i)
554 for (j = 0; j < expected_nr_events[i]; ++j) {
555 int foo = syscalls[i]();
556 ++foo;
557 }
558
Arnaldo Carvalho de Meloaece9482011-05-15 09:39:00 -0300559 while ((event = perf_evlist__mmap_read(evlist, 0)) != NULL) {
Arnaldo Carvalho de Melo8d50e5b2011-01-29 13:02:00 -0200560 struct perf_sample sample;
Arnaldo Carvalho de Melode5fa3a2011-01-15 10:42:46 -0200561
562 if (event->header.type != PERF_RECORD_SAMPLE) {
563 pr_debug("unexpected %s event\n",
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -0200564 perf_event__name(event->header.type));
Arnaldo Carvalho de Melode5fa3a2011-01-15 10:42:46 -0200565 goto out_munmap;
566 }
567
Arnaldo Carvalho de Melo0807d2d2012-09-26 12:48:18 -0300568 err = perf_evlist__parse_sample(evlist, event, &sample);
Frederic Weisbecker5538bec2011-05-22 02:17:22 +0200569 if (err) {
570 pr_err("Can't parse sample, err = %d\n", err);
571 goto out_munmap;
572 }
573
Arnaldo Carvalho de Melode5fa3a2011-01-15 10:42:46 -0200574 evsel = perf_evlist__id2evsel(evlist, sample.id);
575 if (evsel == NULL) {
576 pr_debug("event with id %" PRIu64
577 " doesn't map to an evsel\n", sample.id);
578 goto out_munmap;
579 }
580 nr_events[evsel->idx]++;
581 }
582
583 list_for_each_entry(evsel, &evlist->entries, node) {
584 if (nr_events[evsel->idx] != expected_nr_events[evsel->idx]) {
585 pr_debug("expected %d %s events, got %d\n",
586 expected_nr_events[evsel->idx],
Arnaldo Carvalho de Melo7289f832012-06-12 12:34:58 -0300587 perf_evsel__name(evsel), nr_events[evsel->idx]);
Arnaldo Carvalho de Melode5fa3a2011-01-15 10:42:46 -0200588 goto out_munmap;
589 }
590 }
591
592 err = 0;
593out_munmap:
Arnaldo Carvalho de Melo7e2ed092011-01-30 11:59:43 -0200594 perf_evlist__munmap(evlist);
Arnaldo Carvalho de Melode5fa3a2011-01-15 10:42:46 -0200595out_close_fd:
596 for (i = 0; i < nsyscalls; ++i)
597 perf_evsel__close_fd(evsels[i], 1, threads->nr);
598out_free_evlist:
599 perf_evlist__delete(evlist);
600out_free_cpus:
601 cpu_map__delete(cpus);
602out_free_threads:
603 thread_map__delete(threads);
604 return err;
605#undef nsyscalls
606}
607
Arnaldo Carvalho de Melo3e7c4392011-12-02 11:13:50 -0200608static int sched__get_first_possible_cpu(pid_t pid, cpu_set_t **maskp,
609 size_t *sizep)
610{
611 cpu_set_t *mask;
612 size_t size;
613 int i, cpu = -1, nrcpus = 1024;
614realloc:
615 mask = CPU_ALLOC(nrcpus);
616 size = CPU_ALLOC_SIZE(nrcpus);
617 CPU_ZERO_S(size, mask);
618
619 if (sched_getaffinity(pid, size, mask) == -1) {
620 CPU_FREE(mask);
621 if (errno == EINVAL && nrcpus < (1024 << 8)) {
622 nrcpus = nrcpus << 2;
623 goto realloc;
624 }
625 perror("sched_getaffinity");
626 return -1;
627 }
628
629 for (i = 0; i < nrcpus; i++) {
630 if (CPU_ISSET_S(i, size, mask)) {
631 if (cpu == -1) {
632 cpu = i;
633 *maskp = mask;
634 *sizep = size;
635 } else
636 CPU_CLR_S(i, size, mask);
637 }
638 }
639
640 if (cpu == -1)
641 CPU_FREE(mask);
642
643 return cpu;
644}
645
646static int test__PERF_RECORD(void)
647{
648 struct perf_record_opts opts = {
Namhyung Kimb809ac12012-04-26 14:15:19 +0900649 .target = {
650 .uid = UINT_MAX,
Namhyung Kimd1cb9fc2012-05-16 18:45:49 +0900651 .uses_mmap = true,
Namhyung Kimb809ac12012-04-26 14:15:19 +0900652 },
Arnaldo Carvalho de Melo3e7c4392011-12-02 11:13:50 -0200653 .no_delay = true,
654 .freq = 10,
655 .mmap_pages = 256,
Arnaldo Carvalho de Melo3e7c4392011-12-02 11:13:50 -0200656 };
657 cpu_set_t *cpu_mask = NULL;
658 size_t cpu_mask_size = 0;
659 struct perf_evlist *evlist = perf_evlist__new(NULL, NULL);
660 struct perf_evsel *evsel;
661 struct perf_sample sample;
662 const char *cmd = "sleep";
663 const char *argv[] = { cmd, "1", NULL, };
664 char *bname;
Arnaldo Carvalho de Melo7f3be652012-08-01 19:15:52 -0300665 u64 prev_time = 0;
Arnaldo Carvalho de Melo3e7c4392011-12-02 11:13:50 -0200666 bool found_cmd_mmap = false,
667 found_libc_mmap = false,
668 found_vdso_mmap = false,
669 found_ld_mmap = false;
Arnaldo Carvalho de Melobde09462012-08-01 18:53:11 -0300670 int err = -1, errs = 0, i, wakeups = 0;
Arnaldo Carvalho de Melo3e7c4392011-12-02 11:13:50 -0200671 u32 cpu;
672 int total_events = 0, nr_events[PERF_RECORD_MAX] = { 0, };
673
674 if (evlist == NULL || argv == NULL) {
675 pr_debug("Not enough memory to create evlist\n");
676 goto out;
677 }
678
679 /*
680 * We need at least one evsel in the evlist, use the default
681 * one: "cycles".
682 */
683 err = perf_evlist__add_default(evlist);
684 if (err < 0) {
685 pr_debug("Not enough memory to create evsel\n");
686 goto out_delete_evlist;
687 }
688
689 /*
690 * Create maps of threads and cpus to monitor. In this case
691 * we start with all threads and cpus (-1, -1) but then in
692 * perf_evlist__prepare_workload we'll fill in the only thread
693 * we're monitoring, the one forked there.
694 */
Namhyung Kimb809ac12012-04-26 14:15:19 +0900695 err = perf_evlist__create_maps(evlist, &opts.target);
Arnaldo Carvalho de Melo3e7c4392011-12-02 11:13:50 -0200696 if (err < 0) {
697 pr_debug("Not enough memory to create thread/cpu maps\n");
698 goto out_delete_evlist;
699 }
700
701 /*
702 * Prepare the workload in argv[] to run, it'll fork it, and then wait
703 * for perf_evlist__start_workload() to exec it. This is done this way
704 * so that we have time to open the evlist (calling sys_perf_event_open
705 * on all the fds) and then mmap them.
706 */
707 err = perf_evlist__prepare_workload(evlist, &opts, argv);
708 if (err < 0) {
709 pr_debug("Couldn't run the workload!\n");
710 goto out_delete_evlist;
711 }
712
713 /*
714 * Config the evsels, setting attr->comm on the first one, etc.
715 */
Arnaldo Carvalho de Melo0c21f732012-08-14 16:42:15 -0300716 evsel = perf_evlist__first(evlist);
Arnaldo Carvalho de Melo3e7c4392011-12-02 11:13:50 -0200717 evsel->attr.sample_type |= PERF_SAMPLE_CPU;
718 evsel->attr.sample_type |= PERF_SAMPLE_TID;
719 evsel->attr.sample_type |= PERF_SAMPLE_TIME;
720 perf_evlist__config_attrs(evlist, &opts);
721
722 err = sched__get_first_possible_cpu(evlist->workload.pid, &cpu_mask,
723 &cpu_mask_size);
724 if (err < 0) {
725 pr_debug("sched__get_first_possible_cpu: %s\n", strerror(errno));
726 goto out_delete_evlist;
727 }
728
729 cpu = err;
730
731 /*
732 * So that we can check perf_sample.cpu on all the samples.
733 */
734 if (sched_setaffinity(evlist->workload.pid, cpu_mask_size, cpu_mask) < 0) {
735 pr_debug("sched_setaffinity: %s\n", strerror(errno));
736 goto out_free_cpu_mask;
737 }
738
739 /*
740 * Call sys_perf_event_open on all the fds on all the evsels,
741 * grouping them if asked to.
742 */
Jiri Olsa6a4bb042012-08-08 12:22:36 +0200743 err = perf_evlist__open(evlist);
Arnaldo Carvalho de Melo3e7c4392011-12-02 11:13:50 -0200744 if (err < 0) {
745 pr_debug("perf_evlist__open: %s\n", strerror(errno));
746 goto out_delete_evlist;
747 }
748
749 /*
750 * mmap the first fd on a given CPU and ask for events for the other
751 * fds in the same CPU to be injected in the same mmap ring buffer
752 * (using ioctl(PERF_EVENT_IOC_SET_OUTPUT)).
753 */
754 err = perf_evlist__mmap(evlist, opts.mmap_pages, false);
755 if (err < 0) {
756 pr_debug("perf_evlist__mmap: %s\n", strerror(errno));
757 goto out_delete_evlist;
758 }
759
760 /*
Arnaldo Carvalho de Melo3e7c4392011-12-02 11:13:50 -0200761 * Now that all is properly set up, enable the events, they will
762 * count just on workload.pid, which will start...
763 */
764 perf_evlist__enable(evlist);
765
766 /*
767 * Now!
768 */
769 perf_evlist__start_workload(evlist);
770
Arnaldo Carvalho de Melo3e7c4392011-12-02 11:13:50 -0200771 while (1) {
772 int before = total_events;
773
774 for (i = 0; i < evlist->nr_mmaps; i++) {
775 union perf_event *event;
776
777 while ((event = perf_evlist__mmap_read(evlist, i)) != NULL) {
778 const u32 type = event->header.type;
779 const char *name = perf_event__name(type);
780
781 ++total_events;
782 if (type < PERF_RECORD_MAX)
783 nr_events[type]++;
784
Arnaldo Carvalho de Melo0807d2d2012-09-26 12:48:18 -0300785 err = perf_evlist__parse_sample(evlist, event, &sample);
Arnaldo Carvalho de Melof71c49e2011-12-02 13:53:04 -0200786 if (err < 0) {
Arnaldo Carvalho de Melo3e7c4392011-12-02 11:13:50 -0200787 if (verbose)
788 perf_event__fprintf(event, stderr);
789 pr_debug("Couldn't parse sample\n");
790 goto out_err;
791 }
792
793 if (verbose) {
794 pr_info("%" PRIu64" %d ", sample.time, sample.cpu);
795 perf_event__fprintf(event, stderr);
796 }
797
798 if (prev_time > sample.time) {
799 pr_debug("%s going backwards in time, prev=%" PRIu64 ", curr=%" PRIu64 "\n",
800 name, prev_time, sample.time);
Arnaldo Carvalho de Melof71c49e2011-12-02 13:53:04 -0200801 ++errs;
Arnaldo Carvalho de Melo3e7c4392011-12-02 11:13:50 -0200802 }
803
804 prev_time = sample.time;
805
806 if (sample.cpu != cpu) {
807 pr_debug("%s with unexpected cpu, expected %d, got %d\n",
808 name, cpu, sample.cpu);
Arnaldo Carvalho de Melof71c49e2011-12-02 13:53:04 -0200809 ++errs;
Arnaldo Carvalho de Melo3e7c4392011-12-02 11:13:50 -0200810 }
811
812 if ((pid_t)sample.pid != evlist->workload.pid) {
813 pr_debug("%s with unexpected pid, expected %d, got %d\n",
814 name, evlist->workload.pid, sample.pid);
Arnaldo Carvalho de Melof71c49e2011-12-02 13:53:04 -0200815 ++errs;
Arnaldo Carvalho de Melo3e7c4392011-12-02 11:13:50 -0200816 }
817
818 if ((pid_t)sample.tid != evlist->workload.pid) {
819 pr_debug("%s with unexpected tid, expected %d, got %d\n",
820 name, evlist->workload.pid, sample.tid);
Arnaldo Carvalho de Melof71c49e2011-12-02 13:53:04 -0200821 ++errs;
Arnaldo Carvalho de Melo3e7c4392011-12-02 11:13:50 -0200822 }
823
824 if ((type == PERF_RECORD_COMM ||
825 type == PERF_RECORD_MMAP ||
826 type == PERF_RECORD_FORK ||
827 type == PERF_RECORD_EXIT) &&
828 (pid_t)event->comm.pid != evlist->workload.pid) {
829 pr_debug("%s with unexpected pid/tid\n", name);
Arnaldo Carvalho de Melof71c49e2011-12-02 13:53:04 -0200830 ++errs;
Arnaldo Carvalho de Melo3e7c4392011-12-02 11:13:50 -0200831 }
832
833 if ((type == PERF_RECORD_COMM ||
834 type == PERF_RECORD_MMAP) &&
835 event->comm.pid != event->comm.tid) {
836 pr_debug("%s with different pid/tid!\n", name);
Arnaldo Carvalho de Melof71c49e2011-12-02 13:53:04 -0200837 ++errs;
Arnaldo Carvalho de Melo3e7c4392011-12-02 11:13:50 -0200838 }
839
840 switch (type) {
841 case PERF_RECORD_COMM:
842 if (strcmp(event->comm.comm, cmd)) {
843 pr_debug("%s with unexpected comm!\n", name);
Arnaldo Carvalho de Melof71c49e2011-12-02 13:53:04 -0200844 ++errs;
Arnaldo Carvalho de Melo3e7c4392011-12-02 11:13:50 -0200845 }
846 break;
847 case PERF_RECORD_EXIT:
848 goto found_exit;
849 case PERF_RECORD_MMAP:
850 bname = strrchr(event->mmap.filename, '/');
851 if (bname != NULL) {
852 if (!found_cmd_mmap)
853 found_cmd_mmap = !strcmp(bname + 1, cmd);
854 if (!found_libc_mmap)
855 found_libc_mmap = !strncmp(bname + 1, "libc", 4);
856 if (!found_ld_mmap)
857 found_ld_mmap = !strncmp(bname + 1, "ld", 2);
858 } else if (!found_vdso_mmap)
859 found_vdso_mmap = !strcmp(event->mmap.filename, "[vdso]");
860 break;
861
862 case PERF_RECORD_SAMPLE:
863 /* Just ignore samples for now */
864 break;
865 default:
866 pr_debug("Unexpected perf_event->header.type %d!\n",
867 type);
Arnaldo Carvalho de Melof71c49e2011-12-02 13:53:04 -0200868 ++errs;
Arnaldo Carvalho de Melo3e7c4392011-12-02 11:13:50 -0200869 }
870 }
871 }
872
873 /*
874 * We don't use poll here because at least at 3.1 times the
875 * PERF_RECORD_{!SAMPLE} events don't honour
876 * perf_event_attr.wakeup_events, just PERF_EVENT_SAMPLE does.
877 */
878 if (total_events == before && false)
879 poll(evlist->pollfd, evlist->nr_fds, -1);
880
881 sleep(1);
882 if (++wakeups > 5) {
883 pr_debug("No PERF_RECORD_EXIT event!\n");
Arnaldo Carvalho de Melof71c49e2011-12-02 13:53:04 -0200884 break;
Arnaldo Carvalho de Melo3e7c4392011-12-02 11:13:50 -0200885 }
886 }
887
888found_exit:
889 if (nr_events[PERF_RECORD_COMM] > 1) {
890 pr_debug("Excessive number of PERF_RECORD_COMM events!\n");
Arnaldo Carvalho de Melof71c49e2011-12-02 13:53:04 -0200891 ++errs;
Arnaldo Carvalho de Melo3e7c4392011-12-02 11:13:50 -0200892 }
893
894 if (nr_events[PERF_RECORD_COMM] == 0) {
895 pr_debug("Missing PERF_RECORD_COMM for %s!\n", cmd);
Arnaldo Carvalho de Melof71c49e2011-12-02 13:53:04 -0200896 ++errs;
Arnaldo Carvalho de Melo3e7c4392011-12-02 11:13:50 -0200897 }
898
899 if (!found_cmd_mmap) {
900 pr_debug("PERF_RECORD_MMAP for %s missing!\n", cmd);
Arnaldo Carvalho de Melof71c49e2011-12-02 13:53:04 -0200901 ++errs;
Arnaldo Carvalho de Melo3e7c4392011-12-02 11:13:50 -0200902 }
903
904 if (!found_libc_mmap) {
905 pr_debug("PERF_RECORD_MMAP for %s missing!\n", "libc");
Arnaldo Carvalho de Melof71c49e2011-12-02 13:53:04 -0200906 ++errs;
Arnaldo Carvalho de Melo3e7c4392011-12-02 11:13:50 -0200907 }
908
909 if (!found_ld_mmap) {
910 pr_debug("PERF_RECORD_MMAP for %s missing!\n", "ld");
Arnaldo Carvalho de Melof71c49e2011-12-02 13:53:04 -0200911 ++errs;
Arnaldo Carvalho de Melo3e7c4392011-12-02 11:13:50 -0200912 }
913
914 if (!found_vdso_mmap) {
915 pr_debug("PERF_RECORD_MMAP for %s missing!\n", "[vdso]");
Arnaldo Carvalho de Melof71c49e2011-12-02 13:53:04 -0200916 ++errs;
Arnaldo Carvalho de Melo3e7c4392011-12-02 11:13:50 -0200917 }
Arnaldo Carvalho de Melo3e7c4392011-12-02 11:13:50 -0200918out_err:
919 perf_evlist__munmap(evlist);
920out_free_cpu_mask:
921 CPU_FREE(cpu_mask);
922out_delete_evlist:
923 perf_evlist__delete(evlist);
924out:
Arnaldo Carvalho de Melof71c49e2011-12-02 13:53:04 -0200925 return (err < 0 || errs > 0) ? -1 : 0;
Arnaldo Carvalho de Melo3e7c4392011-12-02 11:13:50 -0200926}
927
Peter Zijlstra08aa0d12011-11-21 14:42:47 +0100928
929#if defined(__x86_64__) || defined(__i386__)
930
931#define barrier() asm volatile("" ::: "memory")
932
933static u64 rdpmc(unsigned int counter)
934{
935 unsigned int low, high;
936
937 asm volatile("rdpmc" : "=a" (low), "=d" (high) : "c" (counter));
938
939 return low | ((u64)high) << 32;
940}
941
942static u64 rdtsc(void)
943{
944 unsigned int low, high;
945
946 asm volatile("rdtsc" : "=a" (low), "=d" (high));
947
948 return low | ((u64)high) << 32;
949}
950
951static u64 mmap_read_self(void *addr)
952{
953 struct perf_event_mmap_page *pc = addr;
954 u32 seq, idx, time_mult = 0, time_shift = 0;
955 u64 count, cyc = 0, time_offset = 0, enabled, running, delta;
956
957 do {
958 seq = pc->lock;
959 barrier();
960
961 enabled = pc->time_enabled;
962 running = pc->time_running;
963
964 if (enabled != running) {
965 cyc = rdtsc();
966 time_mult = pc->time_mult;
967 time_shift = pc->time_shift;
968 time_offset = pc->time_offset;
969 }
970
971 idx = pc->index;
972 count = pc->offset;
973 if (idx)
974 count += rdpmc(idx - 1);
975
976 barrier();
977 } while (pc->lock != seq);
978
979 if (enabled != running) {
980 u64 quot, rem;
981
982 quot = (cyc >> time_shift);
983 rem = cyc & ((1 << time_shift) - 1);
984 delta = time_offset + quot * time_mult +
985 ((rem * time_mult) >> time_shift);
986
987 enabled += delta;
988 if (idx)
989 running += delta;
990
991 quot = count / running;
992 rem = count % running;
993 count = quot * enabled + (rem * enabled) / running;
994 }
995
996 return count;
997}
998
999/*
1000 * If the RDPMC instruction faults then signal this back to the test parent task:
1001 */
Irina Tirdea1d037ca2012-09-11 01:15:03 +03001002static void segfault_handler(int sig __maybe_unused,
1003 siginfo_t *info __maybe_unused,
1004 void *uc __maybe_unused)
Peter Zijlstra08aa0d12011-11-21 14:42:47 +01001005{
1006 exit(-1);
1007}
1008
1009static int __test__rdpmc(void)
1010{
Peter Zijlstra08aa0d12011-11-21 14:42:47 +01001011 volatile int tmp = 0;
1012 u64 i, loops = 1000;
1013 int n;
1014 int fd;
1015 void *addr;
1016 struct perf_event_attr attr = {
1017 .type = PERF_TYPE_HARDWARE,
1018 .config = PERF_COUNT_HW_INSTRUCTIONS,
1019 .exclude_kernel = 1,
1020 };
1021 u64 delta_sum = 0;
1022 struct sigaction sa;
1023
1024 sigfillset(&sa.sa_mask);
1025 sa.sa_sigaction = segfault_handler;
1026 sigaction(SIGSEGV, &sa, NULL);
1027
Peter Zijlstra08aa0d12011-11-21 14:42:47 +01001028 fd = sys_perf_event_open(&attr, 0, -1, -1, 0);
1029 if (fd < 0) {
Namhyung Kimbb77ac32012-09-12 11:11:05 +09001030 pr_err("Error: sys_perf_event_open() syscall returned "
1031 "with %d (%s)\n", fd, strerror(errno));
Arnaldo Carvalho de Melo32c7f732012-09-08 22:53:06 -03001032 return -1;
Peter Zijlstra08aa0d12011-11-21 14:42:47 +01001033 }
1034
1035 addr = mmap(NULL, page_size, PROT_READ, MAP_SHARED, fd, 0);
1036 if (addr == (void *)(-1)) {
Namhyung Kimbb77ac32012-09-12 11:11:05 +09001037 pr_err("Error: mmap() syscall returned with (%s)\n",
1038 strerror(errno));
Arnaldo Carvalho de Melo32c7f732012-09-08 22:53:06 -03001039 goto out_close;
Peter Zijlstra08aa0d12011-11-21 14:42:47 +01001040 }
1041
1042 for (n = 0; n < 6; n++) {
1043 u64 stamp, now, delta;
1044
1045 stamp = mmap_read_self(addr);
1046
1047 for (i = 0; i < loops; i++)
1048 tmp++;
1049
1050 now = mmap_read_self(addr);
1051 loops *= 10;
1052
1053 delta = now - stamp;
Arnaldo Carvalho de Melo23080e42012-04-26 16:22:09 -03001054 pr_debug("%14d: %14Lu\n", n, (long long)delta);
Peter Zijlstra08aa0d12011-11-21 14:42:47 +01001055
1056 delta_sum += delta;
1057 }
1058
1059 munmap(addr, page_size);
Arnaldo Carvalho de Melo23080e42012-04-26 16:22:09 -03001060 pr_debug(" ");
Arnaldo Carvalho de Melo32c7f732012-09-08 22:53:06 -03001061out_close:
1062 close(fd);
Peter Zijlstra08aa0d12011-11-21 14:42:47 +01001063
1064 if (!delta_sum)
1065 return -1;
1066
1067 return 0;
1068}
1069
1070static int test__rdpmc(void)
1071{
1072 int status = 0;
1073 int wret = 0;
1074 int ret;
1075 int pid;
1076
1077 pid = fork();
1078 if (pid < 0)
1079 return -1;
1080
1081 if (!pid) {
1082 ret = __test__rdpmc();
1083
1084 exit(ret);
1085 }
1086
1087 wret = waitpid(pid, &status, 0);
1088 if (wret < 0 || status)
1089 return -1;
1090
1091 return 0;
1092}
1093
1094#endif
1095
Jiri Olsacd82a322012-03-15 20:09:17 +01001096static int test__perf_pmu(void)
1097{
1098 return perf_pmu__test();
1099}
1100
Arnaldo Carvalho de Melo49f20d72012-09-06 14:55:44 -03001101static int perf_evsel__roundtrip_cache_name_test(void)
1102{
1103 char name[128];
1104 int type, op, err = 0, ret = 0, i, idx;
1105 struct perf_evsel *evsel;
1106 struct perf_evlist *evlist = perf_evlist__new(NULL, NULL);
1107
1108 if (evlist == NULL)
1109 return -ENOMEM;
1110
1111 for (type = 0; type < PERF_COUNT_HW_CACHE_MAX; type++) {
1112 for (op = 0; op < PERF_COUNT_HW_CACHE_OP_MAX; op++) {
1113 /* skip invalid cache type */
1114 if (!perf_evsel__is_cache_op_valid(type, op))
1115 continue;
1116
1117 for (i = 0; i < PERF_COUNT_HW_CACHE_RESULT_MAX; i++) {
1118 __perf_evsel__hw_cache_type_op_res_name(type, op, i,
1119 name, sizeof(name));
1120 err = parse_events(evlist, name, 0);
1121 if (err)
1122 ret = err;
1123 }
1124 }
1125 }
1126
1127 idx = 0;
1128 evsel = perf_evlist__first(evlist);
1129
1130 for (type = 0; type < PERF_COUNT_HW_CACHE_MAX; type++) {
1131 for (op = 0; op < PERF_COUNT_HW_CACHE_OP_MAX; op++) {
1132 /* skip invalid cache type */
1133 if (!perf_evsel__is_cache_op_valid(type, op))
1134 continue;
1135
1136 for (i = 0; i < PERF_COUNT_HW_CACHE_RESULT_MAX; i++) {
1137 __perf_evsel__hw_cache_type_op_res_name(type, op, i,
1138 name, sizeof(name));
1139 if (evsel->idx != idx)
1140 continue;
1141
1142 ++idx;
1143
1144 if (strcmp(perf_evsel__name(evsel), name)) {
1145 pr_debug("%s != %s\n", perf_evsel__name(evsel), name);
1146 ret = -1;
1147 }
1148
1149 evsel = perf_evsel__next(evsel);
1150 }
1151 }
1152 }
1153
1154 perf_evlist__delete(evlist);
1155 return ret;
1156}
1157
Arnaldo Carvalho de Melo8ad70132012-09-06 13:11:18 -03001158static int __perf_evsel__name_array_test(const char *names[], int nr_names)
1159{
1160 int i, err;
1161 struct perf_evsel *evsel;
1162 struct perf_evlist *evlist = perf_evlist__new(NULL, NULL);
1163
1164 if (evlist == NULL)
1165 return -ENOMEM;
1166
1167 for (i = 0; i < nr_names; ++i) {
1168 err = parse_events(evlist, names[i], 0);
1169 if (err) {
1170 pr_debug("failed to parse event '%s', err %d\n",
1171 names[i], err);
1172 goto out_delete_evlist;
1173 }
1174 }
1175
1176 err = 0;
1177 list_for_each_entry(evsel, &evlist->entries, node) {
1178 if (strcmp(perf_evsel__name(evsel), names[evsel->idx])) {
1179 --err;
1180 pr_debug("%s != %s\n", perf_evsel__name(evsel), names[evsel->idx]);
1181 }
1182 }
1183
1184out_delete_evlist:
1185 perf_evlist__delete(evlist);
1186 return err;
1187}
1188
1189#define perf_evsel__name_array_test(names) \
1190 __perf_evsel__name_array_test(names, ARRAY_SIZE(names))
1191
1192static int perf_evsel__roundtrip_name_test(void)
1193{
1194 int err = 0, ret = 0;
1195
1196 err = perf_evsel__name_array_test(perf_evsel__hw_names);
1197 if (err)
1198 ret = err;
1199
1200 err = perf_evsel__name_array_test(perf_evsel__sw_names);
1201 if (err)
1202 ret = err;
1203
Arnaldo Carvalho de Melo49f20d72012-09-06 14:55:44 -03001204 err = perf_evsel__roundtrip_cache_name_test();
1205 if (err)
1206 ret = err;
1207
Arnaldo Carvalho de Melo8ad70132012-09-06 13:11:18 -03001208 return ret;
1209}
1210
Arnaldo Carvalho de Melo6a6cd112012-09-18 11:56:28 -03001211static int perf_evsel__test_field(struct perf_evsel *evsel, const char *name,
1212 int size, bool should_be_signed)
1213{
1214 struct format_field *field = perf_evsel__field(evsel, name);
1215 int is_signed;
1216 int ret = 0;
1217
1218 if (field == NULL) {
1219 pr_debug("%s: \"%s\" field not found!\n", evsel->name, name);
1220 return -1;
1221 }
1222
1223 is_signed = !!(field->flags | FIELD_IS_SIGNED);
1224 if (should_be_signed && !is_signed) {
1225 pr_debug("%s: \"%s\" signedness(%d) is wrong, should be %d\n",
1226 evsel->name, name, is_signed, should_be_signed);
1227 ret = -1;
1228 }
1229
1230 if (field->size != size) {
1231 pr_debug("%s: \"%s\" size (%d) should be %d!\n",
1232 evsel->name, name, field->size, size);
1233 ret = -1;
1234 }
1235
Namhyung Kimaf9da882012-09-25 11:20:28 +09001236 return ret;
Arnaldo Carvalho de Melo6a6cd112012-09-18 11:56:28 -03001237}
1238
1239static int perf_evsel__tp_sched_test(void)
1240{
1241 struct perf_evsel *evsel = perf_evsel__newtp("sched", "sched_switch", 0);
1242 int ret = 0;
1243
1244 if (evsel == NULL) {
1245 pr_debug("perf_evsel__new\n");
1246 return -1;
1247 }
1248
1249 if (perf_evsel__test_field(evsel, "prev_comm", 16, true))
1250 ret = -1;
1251
1252 if (perf_evsel__test_field(evsel, "prev_pid", 4, true))
1253 ret = -1;
1254
1255 if (perf_evsel__test_field(evsel, "prev_prio", 4, true))
1256 ret = -1;
1257
1258 if (perf_evsel__test_field(evsel, "prev_state", 8, true))
1259 ret = -1;
1260
1261 if (perf_evsel__test_field(evsel, "next_comm", 16, true))
1262 ret = -1;
1263
1264 if (perf_evsel__test_field(evsel, "next_pid", 4, true))
1265 ret = -1;
1266
1267 if (perf_evsel__test_field(evsel, "next_prio", 4, true))
1268 ret = -1;
1269
1270 perf_evsel__delete(evsel);
1271
1272 evsel = perf_evsel__newtp("sched", "sched_wakeup", 0);
1273
1274 if (perf_evsel__test_field(evsel, "comm", 16, true))
1275 ret = -1;
1276
1277 if (perf_evsel__test_field(evsel, "pid", 4, true))
1278 ret = -1;
1279
1280 if (perf_evsel__test_field(evsel, "prio", 4, true))
1281 ret = -1;
1282
1283 if (perf_evsel__test_field(evsel, "success", 4, true))
1284 ret = -1;
1285
1286 if (perf_evsel__test_field(evsel, "target_cpu", 4, true))
1287 ret = -1;
1288
Namhyung Kimaf9da882012-09-25 11:20:28 +09001289 return ret;
Arnaldo Carvalho de Melo6a6cd112012-09-18 11:56:28 -03001290}
1291
Arnaldo Carvalho de Meloeb2f2702012-09-26 13:23:10 -03001292static int test__syscall_open_tp_fields(void)
1293{
1294 struct perf_record_opts opts = {
1295 .target = {
1296 .uid = UINT_MAX,
1297 .uses_mmap = true,
1298 },
1299 .no_delay = true,
1300 .freq = 1,
1301 .mmap_pages = 256,
1302 .raw_samples = true,
1303 };
1304 const char *filename = "/etc/passwd";
1305 int flags = O_RDONLY | O_DIRECTORY;
1306 struct perf_evlist *evlist = perf_evlist__new(NULL, NULL);
1307 struct perf_evsel *evsel;
1308 int err = -1, i, nr_events = 0, nr_polls = 0;
1309
1310 if (evlist == NULL) {
1311 pr_debug("%s: perf_evlist__new\n", __func__);
1312 goto out;
1313 }
1314
1315 evsel = perf_evsel__newtp("syscalls", "sys_enter_open", 0);
1316 if (evsel == NULL) {
1317 pr_debug("%s: perf_evsel__newtp\n", __func__);
1318 goto out_delete_evlist;
1319 }
1320
1321 perf_evlist__add(evlist, evsel);
1322
1323 err = perf_evlist__create_maps(evlist, &opts.target);
1324 if (err < 0) {
1325 pr_debug("%s: perf_evlist__create_maps\n", __func__);
1326 goto out_delete_evlist;
1327 }
1328
1329 perf_evsel__config(evsel, &opts, evsel);
1330
1331 evlist->threads->map[0] = getpid();
1332
1333 err = perf_evlist__open(evlist);
1334 if (err < 0) {
1335 pr_debug("perf_evlist__open: %s\n", strerror(errno));
1336 goto out_delete_evlist;
1337 }
1338
1339 err = perf_evlist__mmap(evlist, UINT_MAX, false);
1340 if (err < 0) {
1341 pr_debug("perf_evlist__mmap: %s\n", strerror(errno));
1342 goto out_delete_evlist;
1343 }
1344
1345 perf_evlist__enable(evlist);
1346
1347 /*
Jiri Olsa945aea22012-10-30 23:01:43 +01001348 * Generate the event:
1349 */
Arnaldo Carvalho de Meloeb2f2702012-09-26 13:23:10 -03001350 open(filename, flags);
1351
1352 while (1) {
1353 int before = nr_events;
1354
1355 for (i = 0; i < evlist->nr_mmaps; i++) {
1356 union perf_event *event;
1357
1358 while ((event = perf_evlist__mmap_read(evlist, i)) != NULL) {
1359 const u32 type = event->header.type;
1360 int tp_flags;
1361 struct perf_sample sample;
1362
1363 ++nr_events;
1364
1365 if (type != PERF_RECORD_SAMPLE)
1366 continue;
1367
1368 err = perf_evsel__parse_sample(evsel, event, &sample);
1369 if (err) {
1370 pr_err("Can't parse sample, err = %d\n", err);
1371 goto out_munmap;
1372 }
1373
1374 tp_flags = perf_evsel__intval(evsel, &sample, "flags");
1375
1376 if (flags != tp_flags) {
1377 pr_debug("%s: Expected flags=%#x, got %#x\n",
1378 __func__, flags, tp_flags);
1379 goto out_munmap;
1380 }
1381
1382 goto out_ok;
1383 }
1384 }
1385
1386 if (nr_events == before)
1387 poll(evlist->pollfd, evlist->nr_fds, 10);
1388
1389 if (++nr_polls > 5) {
1390 pr_debug("%s: no events!\n", __func__);
1391 goto out_munmap;
1392 }
1393 }
1394out_ok:
1395 err = 0;
1396out_munmap:
1397 perf_evlist__munmap(evlist);
1398out_delete_evlist:
1399 perf_evlist__delete(evlist);
1400out:
1401 return err;
1402}
1403
Arnaldo Carvalho de Melo1c6a8002010-04-29 18:58:32 -03001404static struct test {
1405 const char *desc;
1406 int (*func)(void);
1407} tests[] = {
1408 {
1409 .desc = "vmlinux symtab matches kallsyms",
1410 .func = test__vmlinux_matches_kallsyms,
1411 },
1412 {
Arnaldo Carvalho de Melod8548612011-01-04 00:16:20 -02001413 .desc = "detect open syscall event",
1414 .func = test__open_syscall_event,
1415 },
1416 {
Arnaldo Carvalho de Melo02522082011-01-04 11:55:27 -02001417 .desc = "detect open syscall event on all cpus",
1418 .func = test__open_syscall_event_on_all_cpus,
1419 },
1420 {
Arnaldo Carvalho de Melode5fa3a2011-01-15 10:42:46 -02001421 .desc = "read samples using the mmap interface",
1422 .func = test__basic_mmap,
1423 },
1424 {
Jiri Olsa13b62562011-07-14 11:25:33 +02001425 .desc = "parse events tests",
Jiri Olsaf50246e2012-05-21 09:12:49 +02001426 .func = parse_events__test,
Jiri Olsa13b62562011-07-14 11:25:33 +02001427 },
Peter Zijlstra08aa0d12011-11-21 14:42:47 +01001428#if defined(__x86_64__) || defined(__i386__)
1429 {
1430 .desc = "x86 rdpmc test",
1431 .func = test__rdpmc,
1432 },
1433#endif
Jiri Olsa13b62562011-07-14 11:25:33 +02001434 {
Arnaldo Carvalho de Melo3e7c4392011-12-02 11:13:50 -02001435 .desc = "Validate PERF_RECORD_* events & perf_sample fields",
1436 .func = test__PERF_RECORD,
1437 },
1438 {
Jiri Olsacd82a322012-03-15 20:09:17 +01001439 .desc = "Test perf pmu format parsing",
1440 .func = test__perf_pmu,
1441 },
1442 {
Jiri Olsaf7add552012-07-22 14:14:40 +02001443 .desc = "Test dso data interface",
1444 .func = dso__test_data,
1445 },
1446 {
Arnaldo Carvalho de Melo8ad70132012-09-06 13:11:18 -03001447 .desc = "roundtrip evsel->name check",
1448 .func = perf_evsel__roundtrip_name_test,
1449 },
1450 {
Arnaldo Carvalho de Melo6a6cd112012-09-18 11:56:28 -03001451 .desc = "Check parsing of sched tracepoints fields",
1452 .func = perf_evsel__tp_sched_test,
1453 },
1454 {
Arnaldo Carvalho de Meloeb2f2702012-09-26 13:23:10 -03001455 .desc = "Generate and check syscalls:sys_enter_open event fields",
1456 .func = test__syscall_open_tp_fields,
1457 },
1458 {
Jiri Olsad898b242012-10-30 23:02:05 +01001459 .desc = "struct perf_event_attr setup",
1460 .func = test_attr__run,
1461 },
1462 {
Arnaldo Carvalho de Melo1c6a8002010-04-29 18:58:32 -03001463 .func = NULL,
1464 },
1465};
1466
Arnaldo Carvalho de Meloe60770a2011-11-29 12:52:07 -02001467static bool perf_test__matches(int curr, int argc, const char *argv[])
1468{
1469 int i;
1470
1471 if (argc == 0)
1472 return true;
1473
1474 for (i = 0; i < argc; ++i) {
1475 char *end;
1476 long nr = strtoul(argv[i], &end, 10);
1477
1478 if (*end == '\0') {
1479 if (nr == curr + 1)
1480 return true;
1481 continue;
1482 }
1483
1484 if (strstr(tests[curr].desc, argv[i]))
1485 return true;
1486 }
1487
1488 return false;
1489}
1490
1491static int __cmd_test(int argc, const char *argv[])
Arnaldo Carvalho de Melo1c6a8002010-04-29 18:58:32 -03001492{
1493 int i = 0;
Arnaldo Carvalho de Melo9a8e85a2012-10-24 15:44:41 -02001494 int width = 0;
Arnaldo Carvalho de Melo1c6a8002010-04-29 18:58:32 -03001495
Arnaldo Carvalho de Melo1c6a8002010-04-29 18:58:32 -03001496 while (tests[i].func) {
Arnaldo Carvalho de Melo9a8e85a2012-10-24 15:44:41 -02001497 int len = strlen(tests[i].desc);
1498
1499 if (width < len)
1500 width = len;
1501 ++i;
1502 }
Jiri Olsa945aea22012-10-30 23:01:43 +01001503
Arnaldo Carvalho de Melo9a8e85a2012-10-24 15:44:41 -02001504 i = 0;
1505 while (tests[i].func) {
Arnaldo Carvalho de Meloe60770a2011-11-29 12:52:07 -02001506 int curr = i++, err;
1507
1508 if (!perf_test__matches(curr, argc, argv))
1509 continue;
1510
Arnaldo Carvalho de Melo9a8e85a2012-10-24 15:44:41 -02001511 pr_info("%2d: %-*s:", i, width, tests[curr].desc);
Arnaldo Carvalho de Melo1c6a8002010-04-29 18:58:32 -03001512 pr_debug("\n--- start ---\n");
Arnaldo Carvalho de Meloe60770a2011-11-29 12:52:07 -02001513 err = tests[curr].func();
1514 pr_debug("---- end ----\n%s:", tests[curr].desc);
Arnaldo Carvalho de Melo9a8e85a2012-10-24 15:44:41 -02001515 if (err)
1516 color_fprintf(stderr, PERF_COLOR_RED, " FAILED!\n");
1517 else
1518 pr_info(" Ok\n");
Arnaldo Carvalho de Melo1c6a8002010-04-29 18:58:32 -03001519 }
1520
1521 return 0;
1522}
1523
Arnaldo Carvalho de Meloe60770a2011-11-29 12:52:07 -02001524static int perf_test__list(int argc, const char **argv)
1525{
1526 int i = 0;
Arnaldo Carvalho de Melo1c6a8002010-04-29 18:58:32 -03001527
Arnaldo Carvalho de Meloe60770a2011-11-29 12:52:07 -02001528 while (tests[i].func) {
1529 int curr = i++;
1530
1531 if (argc > 1 && !strstr(tests[curr].desc, argv[1]))
1532 continue;
1533
1534 pr_info("%2d: %s\n", i, tests[curr].desc);
1535 }
1536
1537 return 0;
1538}
Arnaldo Carvalho de Melo1c6a8002010-04-29 18:58:32 -03001539
Irina Tirdea1d037ca2012-09-11 01:15:03 +03001540int cmd_test(int argc, const char **argv, const char *prefix __maybe_unused)
Arnaldo Carvalho de Melo1c6a8002010-04-29 18:58:32 -03001541{
Arnaldo Carvalho de Meloe60770a2011-11-29 12:52:07 -02001542 const char * const test_usage[] = {
1543 "perf test [<options>] [{list <test-name-fragment>|[<test-name-fragments>|<test-numbers>]}]",
1544 NULL,
1545 };
1546 const struct option test_options[] = {
Namhyung Kimc30ab8a2012-01-08 02:25:26 +09001547 OPT_INCR('v', "verbose", &verbose,
Arnaldo Carvalho de Meloe60770a2011-11-29 12:52:07 -02001548 "be more verbose (show symbol address, etc)"),
1549 OPT_END()
1550 };
1551
Arnaldo Carvalho de Melo1c6a8002010-04-29 18:58:32 -03001552 argc = parse_options(argc, argv, test_options, test_usage, 0);
Arnaldo Carvalho de Meloe60770a2011-11-29 12:52:07 -02001553 if (argc >= 1 && !strcmp(argv[0], "list"))
1554 return perf_test__list(argc, argv);
Arnaldo Carvalho de Melo1c6a8002010-04-29 18:58:32 -03001555
1556 symbol_conf.priv_size = sizeof(int);
1557 symbol_conf.sort_by_name = true;
1558 symbol_conf.try_vmlinux_path = true;
1559
1560 if (symbol__init() < 0)
1561 return -1;
1562
Arnaldo Carvalho de Meloe60770a2011-11-29 12:52:07 -02001563 return __cmd_test(argc, argv);
Arnaldo Carvalho de Melo1c6a8002010-04-29 18:58:32 -03001564}