perf test: fix a build error on builtin-test
[deliverable/linux.git] / tools / perf / tests / builtin-test.c
CommitLineData
1c6a800c
ACM
1/*
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"
9a8e85ad 9#include "util/color.h"
1c6a800c 10#include "util/debug.h"
ebf294bf 11#include "util/debugfs.h"
de5fa3a8 12#include "util/evlist.h"
69d2591a 13#include "util/machine.h"
1c6a800c 14#include "util/parse-options.h"
de5fa3a8 15#include "util/parse-events.h"
1c6a800c 16#include "util/symbol.h"
fd78260b 17#include "util/thread_map.h"
cd82a32e 18#include "util/pmu.h"
6a6cd11d 19#include "event-parse.h"
13b62567 20#include "../../include/linux/hw_breakpoint.h"
1c6a800c 21
08aa0d1f
PZ
22#include <sys/mman.h>
23
1d037ca1
IT
24static int vmlinux_matches_kallsyms_filter(struct map *map __maybe_unused,
25 struct symbol *sym)
1c6a800c
ACM
26{
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)) {
d3678758
ACM
130 struct symbol *pair, *first_pair;
131 bool backwards = true;
1c6a800c
ACM
132
133 sym = rb_entry(nd, struct symbol, rb_node);
d3678758
ACM
134
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;
1c6a800c
ACM
140
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
9486aa38 157 pr_debug("%#" PRIx64 ": diff end addr for %s v: %#" PRIx64 " k: %#" PRIx64 "\n",
1c6a800c
ACM
158 sym->start, sym->name, sym->end, pair->end);
159 } else {
d3678758
ACM
160 struct rb_node *nnd;
161detour:
162 nnd = backwards ? rb_prev(&pair->rb_node) :
163 rb_next(&pair->rb_node);
1c6a800c
ACM
164 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 }
d3678758
ACM
172
173 if (backwards) {
174 backwards = false;
175 pair = first_pair;
176 goto detour;
177 }
178
9486aa38 179 pr_debug("%#" PRIx64 ": diff name v: %s k: %s\n",
1c6a800c
ACM
180 sym->start, sym->name, pair->name);
181 }
182 } else
9486aa38 183 pr_debug("%#" PRIx64 ": %s not on kallsyms\n", sym->start, sym->name);
1c6a800c
ACM
184
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;
9486aa38 222 pr_info(" %" PRIx64 "-%" PRIx64 " %" PRIx64 " %s in kallsyms as",
1c6a800c
ACM
223 pos->start, pos->end, pos->pgoff, pos->dso->name);
224 if (pos->pgoff != pair->pgoff || pos->end != pair->end)
9486aa38 225 pr_info(": \n*%" PRIx64 "-%" PRIx64 " %" PRIx64 "",
1c6a800c
ACM
226 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
0252208e 245#include "util/cpumap.h"
d854861c
ACM
246#include "util/evsel.h"
247#include <sys/types.h>
248
de5fa3a8 249static int trace_event__id(const char *evname)
d854861c
ACM
250{
251 char *filename;
252 int err = -1, fd;
253
254 if (asprintf(&filename,
baf040a0 255 "%s/syscalls/%s/id",
ebf294bf 256 tracing_events_path, evname) < 0)
d854861c
ACM
257 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;
23a2f3ab 276 struct perf_event_attr attr;
d854861c
ACM
277 unsigned int nr_open_calls = 111, i;
278 int id = trace_event__id("sys_enter_open");
279
280 if (id < 0) {
454a3bbe 281 pr_debug("is debugfs mounted on /sys/kernel/debug?\n");
d854861c
ACM
282 return -1;
283 }
284
0d37aa34 285 threads = thread_map__new(-1, getpid(), UINT_MAX);
d854861c 286 if (threads == NULL) {
454a3bbe 287 pr_debug("thread_map__new\n");
d854861c
ACM
288 return -1;
289 }
290
23a2f3ab
LM
291 memset(&attr, 0, sizeof(attr));
292 attr.type = PERF_TYPE_TRACEPOINT;
293 attr.config = id;
294 evsel = perf_evsel__new(&attr, 0);
d854861c 295 if (evsel == NULL) {
454a3bbe 296 pr_debug("perf_evsel__new\n");
d854861c
ACM
297 goto out_thread_map_delete;
298 }
299
6a4bb04c 300 if (perf_evsel__open_per_thread(evsel, threads) < 0) {
454a3bbe
ACM
301 pr_debug("failed to open counter: %s, "
302 "tweak /proc/sys/kernel/perf_event_paranoid?\n",
303 strerror(errno));
d854861c
ACM
304 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) {
5d2cd909 313 pr_debug("perf_evsel__read_on_cpu\n");
d854861c
ACM
314 goto out_close_fd;
315 }
316
454a3bbe 317 if (evsel->counts->cpu[0].val != nr_open_calls) {
9486aa38 318 pr_debug("perf_evsel__read_on_cpu: expected to intercept %d calls, got %" PRIu64 "\n",
d854861c 319 nr_open_calls, evsel->counts->cpu[0].val);
454a3bbe
ACM
320 goto out_close_fd;
321 }
945aea22 322
d854861c
ACM
323 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
0252208e
ACM
333#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;
57b84e53 343 cpu_set_t cpu_set;
0252208e
ACM
344 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
0d37aa34 351 threads = thread_map__new(-1, getpid(), UINT_MAX);
0252208e
ACM
352 if (threads == NULL) {
353 pr_debug("thread_map__new\n");
354 return -1;
355 }
356
357 cpus = cpu_map__new(NULL);
98d77b78
HP
358 if (cpus == NULL) {
359 pr_debug("cpu_map__new\n");
360 goto out_thread_map_delete;
0252208e
ACM
361 }
362
0252208e 363
57b84e53 364 CPU_ZERO(&cpu_set);
0252208e
ACM
365
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");
57b84e53 372 goto out_thread_map_delete;
0252208e
ACM
373 }
374
6a4bb04c 375 if (perf_evsel__open(evsel, cpus, threads) < 0) {
0252208e
ACM
376 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;
57b84e53
ACM
384 /*
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 }
0252208e 394
57b84e53
ACM
395 CPU_SET(cpus->map[cpu], &cpu_set);
396 if (sched_setaffinity(0, sizeof(cpu_set), &cpu_set) < 0) {
ffb5e0fb
HP
397 pr_debug("sched_setaffinity() failed on CPU %d: %s ",
398 cpus->map[cpu],
399 strerror(errno));
400 goto out_close_fd;
401 }
0252208e
ACM
402 for (i = 0; i < ncalls; ++i) {
403 fd = open("/etc/passwd", O_RDONLY);
404 close(fd);
405 }
57b84e53 406 CPU_CLR(cpus->map[cpu], &cpu_set);
0252208e
ACM
407 }
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
d2af9687
ACM
419 err = 0;
420
0252208e
ACM
421 for (cpu = 0; cpu < cpus->nr; ++cpu) {
422 unsigned int expected;
423
57b84e53
ACM
424 if (cpus->map[cpu] >= CPU_SETSIZE)
425 continue;
426
0252208e 427 if (perf_evsel__read_on_cpu(evsel, cpu, 0) < 0) {
5d2cd909 428 pr_debug("perf_evsel__read_on_cpu\n");
d2af9687
ACM
429 err = -1;
430 break;
0252208e
ACM
431 }
432
433 expected = nr_open_calls + cpu;
434 if (evsel->counts->cpu[cpu].val != expected) {
9486aa38 435 pr_debug("perf_evsel__read_on_cpu: expected to intercept %d calls on cpu %d, got %" PRIu64 "\n",
ffb5e0fb 436 expected, cpus->map[cpu], evsel->counts->cpu[cpu].val);
d2af9687 437 err = -1;
0252208e
ACM
438 }
439 }
440
0252208e
ACM
441out_close_fd:
442 perf_evsel__close_fd(evsel, 1, threads->nr);
443out_evsel_delete:
444 perf_evsel__delete(evsel);
0252208e
ACM
445out_thread_map_delete:
446 thread_map__delete(threads);
447 return err;
448}
449
de5fa3a8
ACM
450/*
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;
8115d60c 464 union perf_event *event;
de5fa3a8 465 struct thread_map *threads;
de5fa3a8
ACM
466 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
0d37aa34 498 threads = thread_map__new(-1, getpid(), UINT_MAX);
de5fa3a8
ACM
499 if (threads == NULL) {
500 pr_debug("thread_map__new\n");
501 return -1;
502 }
503
504 cpus = cpu_map__new(NULL);
54489c18
HP
505 if (cpus == NULL) {
506 pr_debug("cpu_map__new\n");
de5fa3a8
ACM
507 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
7e2ed097 519 evlist = perf_evlist__new(cpus, threads);
54489c18 520 if (evlist == NULL) {
de5fa3a8
ACM
521 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
de5fa3a8
ACM
529 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
6a4bb04c 539 if (perf_evsel__open(evsels[i], cpus, threads) < 0) {
de5fa3a8
ACM
540 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
7e2ed097 547 if (perf_evlist__mmap(evlist, 128, true) < 0) {
de5fa3a8
ACM
548 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
aece948f 559 while ((event = perf_evlist__mmap_read(evlist, 0)) != NULL) {
8d50e5b4 560 struct perf_sample sample;
de5fa3a8
ACM
561
562 if (event->header.type != PERF_RECORD_SAMPLE) {
563 pr_debug("unexpected %s event\n",
8115d60c 564 perf_event__name(event->header.type));
de5fa3a8
ACM
565 goto out_munmap;
566 }
567
0807d2d8 568 err = perf_evlist__parse_sample(evlist, event, &sample);
5538beca
FW
569 if (err) {
570 pr_err("Can't parse sample, err = %d\n", err);
571 goto out_munmap;
572 }
573
de5fa3a8
ACM
574 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],
7289f83c 587 perf_evsel__name(evsel), nr_events[evsel->idx]);
de5fa3a8
ACM
588 goto out_munmap;
589 }
590 }
591
592 err = 0;
593out_munmap:
7e2ed097 594 perf_evlist__munmap(evlist);
de5fa3a8
ACM
595out_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
12f8f74b 608static int sched__get_first_possible_cpu(pid_t pid, cpu_set_t *maskp)
3e7c439a 609{
3e7c439a
ACM
610 int i, cpu = -1, nrcpus = 1024;
611realloc:
12f8f74b 612 CPU_ZERO(maskp);
3e7c439a 613
12f8f74b 614 if (sched_getaffinity(pid, sizeof(*maskp), maskp) == -1) {
3e7c439a
ACM
615 if (errno == EINVAL && nrcpus < (1024 << 8)) {
616 nrcpus = nrcpus << 2;
617 goto realloc;
618 }
619 perror("sched_getaffinity");
620 return -1;
621 }
622
623 for (i = 0; i < nrcpus; i++) {
12f8f74b
ZL
624 if (CPU_ISSET(i, maskp)) {
625 if (cpu == -1)
3e7c439a 626 cpu = i;
12f8f74b
ZL
627 else
628 CPU_CLR(i, maskp);
3e7c439a
ACM
629 }
630 }
631
3e7c439a
ACM
632 return cpu;
633}
634
635static int test__PERF_RECORD(void)
636{
637 struct perf_record_opts opts = {
b809ac10
NK
638 .target = {
639 .uid = UINT_MAX,
d1cb9fce 640 .uses_mmap = true,
b809ac10 641 },
3e7c439a
ACM
642 .no_delay = true,
643 .freq = 10,
644 .mmap_pages = 256,
3e7c439a 645 };
12f8f74b
ZL
646 cpu_set_t cpu_mask;
647 size_t cpu_mask_size = sizeof(cpu_mask);
3e7c439a
ACM
648 struct perf_evlist *evlist = perf_evlist__new(NULL, NULL);
649 struct perf_evsel *evsel;
650 struct perf_sample sample;
651 const char *cmd = "sleep";
652 const char *argv[] = { cmd, "1", NULL, };
653 char *bname;
7f3be652 654 u64 prev_time = 0;
3e7c439a
ACM
655 bool found_cmd_mmap = false,
656 found_libc_mmap = false,
657 found_vdso_mmap = false,
658 found_ld_mmap = false;
bde09467 659 int err = -1, errs = 0, i, wakeups = 0;
3e7c439a
ACM
660 u32 cpu;
661 int total_events = 0, nr_events[PERF_RECORD_MAX] = { 0, };
662
663 if (evlist == NULL || argv == NULL) {
664 pr_debug("Not enough memory to create evlist\n");
665 goto out;
666 }
667
668 /*
669 * We need at least one evsel in the evlist, use the default
670 * one: "cycles".
671 */
672 err = perf_evlist__add_default(evlist);
673 if (err < 0) {
674 pr_debug("Not enough memory to create evsel\n");
675 goto out_delete_evlist;
676 }
677
678 /*
679 * Create maps of threads and cpus to monitor. In this case
680 * we start with all threads and cpus (-1, -1) but then in
681 * perf_evlist__prepare_workload we'll fill in the only thread
682 * we're monitoring, the one forked there.
683 */
b809ac10 684 err = perf_evlist__create_maps(evlist, &opts.target);
3e7c439a
ACM
685 if (err < 0) {
686 pr_debug("Not enough memory to create thread/cpu maps\n");
687 goto out_delete_evlist;
688 }
689
690 /*
691 * Prepare the workload in argv[] to run, it'll fork it, and then wait
692 * for perf_evlist__start_workload() to exec it. This is done this way
693 * so that we have time to open the evlist (calling sys_perf_event_open
694 * on all the fds) and then mmap them.
695 */
696 err = perf_evlist__prepare_workload(evlist, &opts, argv);
697 if (err < 0) {
698 pr_debug("Couldn't run the workload!\n");
699 goto out_delete_evlist;
700 }
701
702 /*
703 * Config the evsels, setting attr->comm on the first one, etc.
704 */
0c21f736 705 evsel = perf_evlist__first(evlist);
3e7c439a
ACM
706 evsel->attr.sample_type |= PERF_SAMPLE_CPU;
707 evsel->attr.sample_type |= PERF_SAMPLE_TID;
708 evsel->attr.sample_type |= PERF_SAMPLE_TIME;
709 perf_evlist__config_attrs(evlist, &opts);
710
12f8f74b 711 err = sched__get_first_possible_cpu(evlist->workload.pid, &cpu_mask);
3e7c439a
ACM
712 if (err < 0) {
713 pr_debug("sched__get_first_possible_cpu: %s\n", strerror(errno));
714 goto out_delete_evlist;
715 }
716
717 cpu = err;
718
719 /*
720 * So that we can check perf_sample.cpu on all the samples.
721 */
12f8f74b 722 if (sched_setaffinity(evlist->workload.pid, cpu_mask_size, &cpu_mask) < 0) {
3e7c439a 723 pr_debug("sched_setaffinity: %s\n", strerror(errno));
12f8f74b 724 goto out_delete_evlist;
3e7c439a
ACM
725 }
726
727 /*
728 * Call sys_perf_event_open on all the fds on all the evsels,
729 * grouping them if asked to.
730 */
6a4bb04c 731 err = perf_evlist__open(evlist);
3e7c439a
ACM
732 if (err < 0) {
733 pr_debug("perf_evlist__open: %s\n", strerror(errno));
734 goto out_delete_evlist;
735 }
736
737 /*
738 * mmap the first fd on a given CPU and ask for events for the other
739 * fds in the same CPU to be injected in the same mmap ring buffer
740 * (using ioctl(PERF_EVENT_IOC_SET_OUTPUT)).
741 */
742 err = perf_evlist__mmap(evlist, opts.mmap_pages, false);
743 if (err < 0) {
744 pr_debug("perf_evlist__mmap: %s\n", strerror(errno));
745 goto out_delete_evlist;
746 }
747
3e7c439a
ACM
748 /*
749 * Now that all is properly set up, enable the events, they will
750 * count just on workload.pid, which will start...
751 */
752 perf_evlist__enable(evlist);
753
754 /*
755 * Now!
756 */
757 perf_evlist__start_workload(evlist);
758
3e7c439a
ACM
759 while (1) {
760 int before = total_events;
761
762 for (i = 0; i < evlist->nr_mmaps; i++) {
763 union perf_event *event;
764
765 while ((event = perf_evlist__mmap_read(evlist, i)) != NULL) {
766 const u32 type = event->header.type;
767 const char *name = perf_event__name(type);
768
769 ++total_events;
770 if (type < PERF_RECORD_MAX)
771 nr_events[type]++;
772
0807d2d8 773 err = perf_evlist__parse_sample(evlist, event, &sample);
f71c49e5 774 if (err < 0) {
3e7c439a
ACM
775 if (verbose)
776 perf_event__fprintf(event, stderr);
777 pr_debug("Couldn't parse sample\n");
778 goto out_err;
779 }
780
781 if (verbose) {
782 pr_info("%" PRIu64" %d ", sample.time, sample.cpu);
783 perf_event__fprintf(event, stderr);
784 }
785
786 if (prev_time > sample.time) {
787 pr_debug("%s going backwards in time, prev=%" PRIu64 ", curr=%" PRIu64 "\n",
788 name, prev_time, sample.time);
f71c49e5 789 ++errs;
3e7c439a
ACM
790 }
791
792 prev_time = sample.time;
793
794 if (sample.cpu != cpu) {
795 pr_debug("%s with unexpected cpu, expected %d, got %d\n",
796 name, cpu, sample.cpu);
f71c49e5 797 ++errs;
3e7c439a
ACM
798 }
799
800 if ((pid_t)sample.pid != evlist->workload.pid) {
801 pr_debug("%s with unexpected pid, expected %d, got %d\n",
802 name, evlist->workload.pid, sample.pid);
f71c49e5 803 ++errs;
3e7c439a
ACM
804 }
805
806 if ((pid_t)sample.tid != evlist->workload.pid) {
807 pr_debug("%s with unexpected tid, expected %d, got %d\n",
808 name, evlist->workload.pid, sample.tid);
f71c49e5 809 ++errs;
3e7c439a
ACM
810 }
811
812 if ((type == PERF_RECORD_COMM ||
813 type == PERF_RECORD_MMAP ||
814 type == PERF_RECORD_FORK ||
815 type == PERF_RECORD_EXIT) &&
816 (pid_t)event->comm.pid != evlist->workload.pid) {
817 pr_debug("%s with unexpected pid/tid\n", name);
f71c49e5 818 ++errs;
3e7c439a
ACM
819 }
820
821 if ((type == PERF_RECORD_COMM ||
822 type == PERF_RECORD_MMAP) &&
823 event->comm.pid != event->comm.tid) {
824 pr_debug("%s with different pid/tid!\n", name);
f71c49e5 825 ++errs;
3e7c439a
ACM
826 }
827
828 switch (type) {
829 case PERF_RECORD_COMM:
830 if (strcmp(event->comm.comm, cmd)) {
831 pr_debug("%s with unexpected comm!\n", name);
f71c49e5 832 ++errs;
3e7c439a
ACM
833 }
834 break;
835 case PERF_RECORD_EXIT:
836 goto found_exit;
837 case PERF_RECORD_MMAP:
838 bname = strrchr(event->mmap.filename, '/');
839 if (bname != NULL) {
840 if (!found_cmd_mmap)
841 found_cmd_mmap = !strcmp(bname + 1, cmd);
842 if (!found_libc_mmap)
843 found_libc_mmap = !strncmp(bname + 1, "libc", 4);
844 if (!found_ld_mmap)
845 found_ld_mmap = !strncmp(bname + 1, "ld", 2);
846 } else if (!found_vdso_mmap)
847 found_vdso_mmap = !strcmp(event->mmap.filename, "[vdso]");
848 break;
849
850 case PERF_RECORD_SAMPLE:
851 /* Just ignore samples for now */
852 break;
853 default:
854 pr_debug("Unexpected perf_event->header.type %d!\n",
855 type);
f71c49e5 856 ++errs;
3e7c439a
ACM
857 }
858 }
859 }
860
861 /*
862 * We don't use poll here because at least at 3.1 times the
863 * PERF_RECORD_{!SAMPLE} events don't honour
864 * perf_event_attr.wakeup_events, just PERF_EVENT_SAMPLE does.
865 */
866 if (total_events == before && false)
867 poll(evlist->pollfd, evlist->nr_fds, -1);
868
869 sleep(1);
870 if (++wakeups > 5) {
871 pr_debug("No PERF_RECORD_EXIT event!\n");
f71c49e5 872 break;
3e7c439a
ACM
873 }
874 }
875
876found_exit:
877 if (nr_events[PERF_RECORD_COMM] > 1) {
878 pr_debug("Excessive number of PERF_RECORD_COMM events!\n");
f71c49e5 879 ++errs;
3e7c439a
ACM
880 }
881
882 if (nr_events[PERF_RECORD_COMM] == 0) {
883 pr_debug("Missing PERF_RECORD_COMM for %s!\n", cmd);
f71c49e5 884 ++errs;
3e7c439a
ACM
885 }
886
887 if (!found_cmd_mmap) {
888 pr_debug("PERF_RECORD_MMAP for %s missing!\n", cmd);
f71c49e5 889 ++errs;
3e7c439a
ACM
890 }
891
892 if (!found_libc_mmap) {
893 pr_debug("PERF_RECORD_MMAP for %s missing!\n", "libc");
f71c49e5 894 ++errs;
3e7c439a
ACM
895 }
896
897 if (!found_ld_mmap) {
898 pr_debug("PERF_RECORD_MMAP for %s missing!\n", "ld");
f71c49e5 899 ++errs;
3e7c439a
ACM
900 }
901
902 if (!found_vdso_mmap) {
903 pr_debug("PERF_RECORD_MMAP for %s missing!\n", "[vdso]");
f71c49e5 904 ++errs;
3e7c439a 905 }
3e7c439a
ACM
906out_err:
907 perf_evlist__munmap(evlist);
3e7c439a
ACM
908out_delete_evlist:
909 perf_evlist__delete(evlist);
910out:
f71c49e5 911 return (err < 0 || errs > 0) ? -1 : 0;
3e7c439a
ACM
912}
913
08aa0d1f
PZ
914
915#if defined(__x86_64__) || defined(__i386__)
916
917#define barrier() asm volatile("" ::: "memory")
918
919static u64 rdpmc(unsigned int counter)
920{
921 unsigned int low, high;
922
923 asm volatile("rdpmc" : "=a" (low), "=d" (high) : "c" (counter));
924
925 return low | ((u64)high) << 32;
926}
927
928static u64 rdtsc(void)
929{
930 unsigned int low, high;
931
932 asm volatile("rdtsc" : "=a" (low), "=d" (high));
933
934 return low | ((u64)high) << 32;
935}
936
937static u64 mmap_read_self(void *addr)
938{
939 struct perf_event_mmap_page *pc = addr;
940 u32 seq, idx, time_mult = 0, time_shift = 0;
941 u64 count, cyc = 0, time_offset = 0, enabled, running, delta;
942
943 do {
944 seq = pc->lock;
945 barrier();
946
947 enabled = pc->time_enabled;
948 running = pc->time_running;
949
950 if (enabled != running) {
951 cyc = rdtsc();
952 time_mult = pc->time_mult;
953 time_shift = pc->time_shift;
954 time_offset = pc->time_offset;
955 }
956
957 idx = pc->index;
958 count = pc->offset;
959 if (idx)
960 count += rdpmc(idx - 1);
961
962 barrier();
963 } while (pc->lock != seq);
964
965 if (enabled != running) {
966 u64 quot, rem;
967
968 quot = (cyc >> time_shift);
969 rem = cyc & ((1 << time_shift) - 1);
970 delta = time_offset + quot * time_mult +
971 ((rem * time_mult) >> time_shift);
972
973 enabled += delta;
974 if (idx)
975 running += delta;
976
977 quot = count / running;
978 rem = count % running;
979 count = quot * enabled + (rem * enabled) / running;
980 }
981
982 return count;
983}
984
985/*
986 * If the RDPMC instruction faults then signal this back to the test parent task:
987 */
1d037ca1
IT
988static void segfault_handler(int sig __maybe_unused,
989 siginfo_t *info __maybe_unused,
990 void *uc __maybe_unused)
08aa0d1f
PZ
991{
992 exit(-1);
993}
994
995static int __test__rdpmc(void)
996{
08aa0d1f
PZ
997 volatile int tmp = 0;
998 u64 i, loops = 1000;
999 int n;
1000 int fd;
1001 void *addr;
1002 struct perf_event_attr attr = {
1003 .type = PERF_TYPE_HARDWARE,
1004 .config = PERF_COUNT_HW_INSTRUCTIONS,
1005 .exclude_kernel = 1,
1006 };
1007 u64 delta_sum = 0;
1008 struct sigaction sa;
1009
1010 sigfillset(&sa.sa_mask);
1011 sa.sa_sigaction = segfault_handler;
1012 sigaction(SIGSEGV, &sa, NULL);
1013
08aa0d1f
PZ
1014 fd = sys_perf_event_open(&attr, 0, -1, -1, 0);
1015 if (fd < 0) {
bb77ac3a
NK
1016 pr_err("Error: sys_perf_event_open() syscall returned "
1017 "with %d (%s)\n", fd, strerror(errno));
32c7f738 1018 return -1;
08aa0d1f
PZ
1019 }
1020
1021 addr = mmap(NULL, page_size, PROT_READ, MAP_SHARED, fd, 0);
1022 if (addr == (void *)(-1)) {
bb77ac3a
NK
1023 pr_err("Error: mmap() syscall returned with (%s)\n",
1024 strerror(errno));
32c7f738 1025 goto out_close;
08aa0d1f
PZ
1026 }
1027
1028 for (n = 0; n < 6; n++) {
1029 u64 stamp, now, delta;
1030
1031 stamp = mmap_read_self(addr);
1032
1033 for (i = 0; i < loops; i++)
1034 tmp++;
1035
1036 now = mmap_read_self(addr);
1037 loops *= 10;
1038
1039 delta = now - stamp;
23080e4c 1040 pr_debug("%14d: %14Lu\n", n, (long long)delta);
08aa0d1f
PZ
1041
1042 delta_sum += delta;
1043 }
1044
1045 munmap(addr, page_size);
23080e4c 1046 pr_debug(" ");
32c7f738
ACM
1047out_close:
1048 close(fd);
08aa0d1f
PZ
1049
1050 if (!delta_sum)
1051 return -1;
1052
1053 return 0;
1054}
1055
1056static int test__rdpmc(void)
1057{
1058 int status = 0;
1059 int wret = 0;
1060 int ret;
1061 int pid;
1062
1063 pid = fork();
1064 if (pid < 0)
1065 return -1;
1066
1067 if (!pid) {
1068 ret = __test__rdpmc();
1069
1070 exit(ret);
1071 }
1072
1073 wret = waitpid(pid, &status, 0);
1074 if (wret < 0 || status)
1075 return -1;
1076
1077 return 0;
1078}
1079
1080#endif
1081
cd82a32e
JO
1082static int test__perf_pmu(void)
1083{
1084 return perf_pmu__test();
1085}
1086
49f20d72
ACM
1087static int perf_evsel__roundtrip_cache_name_test(void)
1088{
1089 char name[128];
1090 int type, op, err = 0, ret = 0, i, idx;
1091 struct perf_evsel *evsel;
1092 struct perf_evlist *evlist = perf_evlist__new(NULL, NULL);
1093
1094 if (evlist == NULL)
1095 return -ENOMEM;
1096
1097 for (type = 0; type < PERF_COUNT_HW_CACHE_MAX; type++) {
1098 for (op = 0; op < PERF_COUNT_HW_CACHE_OP_MAX; op++) {
1099 /* skip invalid cache type */
1100 if (!perf_evsel__is_cache_op_valid(type, op))
1101 continue;
1102
1103 for (i = 0; i < PERF_COUNT_HW_CACHE_RESULT_MAX; i++) {
1104 __perf_evsel__hw_cache_type_op_res_name(type, op, i,
1105 name, sizeof(name));
1106 err = parse_events(evlist, name, 0);
1107 if (err)
1108 ret = err;
1109 }
1110 }
1111 }
1112
1113 idx = 0;
1114 evsel = perf_evlist__first(evlist);
1115
1116 for (type = 0; type < PERF_COUNT_HW_CACHE_MAX; type++) {
1117 for (op = 0; op < PERF_COUNT_HW_CACHE_OP_MAX; op++) {
1118 /* skip invalid cache type */
1119 if (!perf_evsel__is_cache_op_valid(type, op))
1120 continue;
1121
1122 for (i = 0; i < PERF_COUNT_HW_CACHE_RESULT_MAX; i++) {
1123 __perf_evsel__hw_cache_type_op_res_name(type, op, i,
1124 name, sizeof(name));
1125 if (evsel->idx != idx)
1126 continue;
1127
1128 ++idx;
1129
1130 if (strcmp(perf_evsel__name(evsel), name)) {
1131 pr_debug("%s != %s\n", perf_evsel__name(evsel), name);
1132 ret = -1;
1133 }
1134
1135 evsel = perf_evsel__next(evsel);
1136 }
1137 }
1138 }
1139
1140 perf_evlist__delete(evlist);
1141 return ret;
1142}
1143
8ad7013b
ACM
1144static int __perf_evsel__name_array_test(const char *names[], int nr_names)
1145{
1146 int i, err;
1147 struct perf_evsel *evsel;
1148 struct perf_evlist *evlist = perf_evlist__new(NULL, NULL);
1149
1150 if (evlist == NULL)
1151 return -ENOMEM;
1152
1153 for (i = 0; i < nr_names; ++i) {
1154 err = parse_events(evlist, names[i], 0);
1155 if (err) {
1156 pr_debug("failed to parse event '%s', err %d\n",
1157 names[i], err);
1158 goto out_delete_evlist;
1159 }
1160 }
1161
1162 err = 0;
1163 list_for_each_entry(evsel, &evlist->entries, node) {
1164 if (strcmp(perf_evsel__name(evsel), names[evsel->idx])) {
1165 --err;
1166 pr_debug("%s != %s\n", perf_evsel__name(evsel), names[evsel->idx]);
1167 }
1168 }
1169
1170out_delete_evlist:
1171 perf_evlist__delete(evlist);
1172 return err;
1173}
1174
1175#define perf_evsel__name_array_test(names) \
1176 __perf_evsel__name_array_test(names, ARRAY_SIZE(names))
1177
1178static int perf_evsel__roundtrip_name_test(void)
1179{
1180 int err = 0, ret = 0;
1181
1182 err = perf_evsel__name_array_test(perf_evsel__hw_names);
1183 if (err)
1184 ret = err;
1185
1186 err = perf_evsel__name_array_test(perf_evsel__sw_names);
1187 if (err)
1188 ret = err;
1189
49f20d72
ACM
1190 err = perf_evsel__roundtrip_cache_name_test();
1191 if (err)
1192 ret = err;
1193
8ad7013b
ACM
1194 return ret;
1195}
1196
6a6cd11d
ACM
1197static int perf_evsel__test_field(struct perf_evsel *evsel, const char *name,
1198 int size, bool should_be_signed)
1199{
1200 struct format_field *field = perf_evsel__field(evsel, name);
1201 int is_signed;
1202 int ret = 0;
1203
1204 if (field == NULL) {
1205 pr_debug("%s: \"%s\" field not found!\n", evsel->name, name);
1206 return -1;
1207 }
1208
1209 is_signed = !!(field->flags | FIELD_IS_SIGNED);
1210 if (should_be_signed && !is_signed) {
1211 pr_debug("%s: \"%s\" signedness(%d) is wrong, should be %d\n",
1212 evsel->name, name, is_signed, should_be_signed);
1213 ret = -1;
1214 }
1215
1216 if (field->size != size) {
1217 pr_debug("%s: \"%s\" size (%d) should be %d!\n",
1218 evsel->name, name, field->size, size);
1219 ret = -1;
1220 }
1221
af9da88f 1222 return ret;
6a6cd11d
ACM
1223}
1224
1225static int perf_evsel__tp_sched_test(void)
1226{
1227 struct perf_evsel *evsel = perf_evsel__newtp("sched", "sched_switch", 0);
1228 int ret = 0;
1229
1230 if (evsel == NULL) {
1231 pr_debug("perf_evsel__new\n");
1232 return -1;
1233 }
1234
1235 if (perf_evsel__test_field(evsel, "prev_comm", 16, true))
1236 ret = -1;
1237
1238 if (perf_evsel__test_field(evsel, "prev_pid", 4, true))
1239 ret = -1;
1240
1241 if (perf_evsel__test_field(evsel, "prev_prio", 4, true))
1242 ret = -1;
1243
1244 if (perf_evsel__test_field(evsel, "prev_state", 8, true))
1245 ret = -1;
1246
1247 if (perf_evsel__test_field(evsel, "next_comm", 16, true))
1248 ret = -1;
1249
1250 if (perf_evsel__test_field(evsel, "next_pid", 4, true))
1251 ret = -1;
1252
1253 if (perf_evsel__test_field(evsel, "next_prio", 4, true))
1254 ret = -1;
1255
1256 perf_evsel__delete(evsel);
1257
1258 evsel = perf_evsel__newtp("sched", "sched_wakeup", 0);
1259
1260 if (perf_evsel__test_field(evsel, "comm", 16, true))
1261 ret = -1;
1262
1263 if (perf_evsel__test_field(evsel, "pid", 4, true))
1264 ret = -1;
1265
1266 if (perf_evsel__test_field(evsel, "prio", 4, true))
1267 ret = -1;
1268
1269 if (perf_evsel__test_field(evsel, "success", 4, true))
1270 ret = -1;
1271
1272 if (perf_evsel__test_field(evsel, "target_cpu", 4, true))
1273 ret = -1;
1274
af9da88f 1275 return ret;
6a6cd11d
ACM
1276}
1277
eb2f2703
ACM
1278static int test__syscall_open_tp_fields(void)
1279{
1280 struct perf_record_opts opts = {
1281 .target = {
1282 .uid = UINT_MAX,
1283 .uses_mmap = true,
1284 },
1285 .no_delay = true,
1286 .freq = 1,
1287 .mmap_pages = 256,
1288 .raw_samples = true,
1289 };
1290 const char *filename = "/etc/passwd";
1291 int flags = O_RDONLY | O_DIRECTORY;
1292 struct perf_evlist *evlist = perf_evlist__new(NULL, NULL);
1293 struct perf_evsel *evsel;
1294 int err = -1, i, nr_events = 0, nr_polls = 0;
1295
1296 if (evlist == NULL) {
1297 pr_debug("%s: perf_evlist__new\n", __func__);
1298 goto out;
1299 }
1300
1301 evsel = perf_evsel__newtp("syscalls", "sys_enter_open", 0);
1302 if (evsel == NULL) {
1303 pr_debug("%s: perf_evsel__newtp\n", __func__);
1304 goto out_delete_evlist;
1305 }
1306
1307 perf_evlist__add(evlist, evsel);
1308
1309 err = perf_evlist__create_maps(evlist, &opts.target);
1310 if (err < 0) {
1311 pr_debug("%s: perf_evlist__create_maps\n", __func__);
1312 goto out_delete_evlist;
1313 }
1314
1315 perf_evsel__config(evsel, &opts, evsel);
1316
1317 evlist->threads->map[0] = getpid();
1318
1319 err = perf_evlist__open(evlist);
1320 if (err < 0) {
1321 pr_debug("perf_evlist__open: %s\n", strerror(errno));
1322 goto out_delete_evlist;
1323 }
1324
1325 err = perf_evlist__mmap(evlist, UINT_MAX, false);
1326 if (err < 0) {
1327 pr_debug("perf_evlist__mmap: %s\n", strerror(errno));
1328 goto out_delete_evlist;
1329 }
1330
1331 perf_evlist__enable(evlist);
1332
1333 /*
945aea22
JO
1334 * Generate the event:
1335 */
eb2f2703
ACM
1336 open(filename, flags);
1337
1338 while (1) {
1339 int before = nr_events;
1340
1341 for (i = 0; i < evlist->nr_mmaps; i++) {
1342 union perf_event *event;
1343
1344 while ((event = perf_evlist__mmap_read(evlist, i)) != NULL) {
1345 const u32 type = event->header.type;
1346 int tp_flags;
1347 struct perf_sample sample;
1348
1349 ++nr_events;
1350
1351 if (type != PERF_RECORD_SAMPLE)
1352 continue;
1353
1354 err = perf_evsel__parse_sample(evsel, event, &sample);
1355 if (err) {
1356 pr_err("Can't parse sample, err = %d\n", err);
1357 goto out_munmap;
1358 }
1359
1360 tp_flags = perf_evsel__intval(evsel, &sample, "flags");
1361
1362 if (flags != tp_flags) {
1363 pr_debug("%s: Expected flags=%#x, got %#x\n",
1364 __func__, flags, tp_flags);
1365 goto out_munmap;
1366 }
1367
1368 goto out_ok;
1369 }
1370 }
1371
1372 if (nr_events == before)
1373 poll(evlist->pollfd, evlist->nr_fds, 10);
1374
1375 if (++nr_polls > 5) {
1376 pr_debug("%s: no events!\n", __func__);
1377 goto out_munmap;
1378 }
1379 }
1380out_ok:
1381 err = 0;
1382out_munmap:
1383 perf_evlist__munmap(evlist);
1384out_delete_evlist:
1385 perf_evlist__delete(evlist);
1386out:
1387 return err;
1388}
1389
1c6a800c
ACM
1390static struct test {
1391 const char *desc;
1392 int (*func)(void);
1393} tests[] = {
1394 {
1395 .desc = "vmlinux symtab matches kallsyms",
1396 .func = test__vmlinux_matches_kallsyms,
1397 },
d854861c
ACM
1398 {
1399 .desc = "detect open syscall event",
1400 .func = test__open_syscall_event,
1401 },
0252208e
ACM
1402 {
1403 .desc = "detect open syscall event on all cpus",
1404 .func = test__open_syscall_event_on_all_cpus,
1405 },
de5fa3a8
ACM
1406 {
1407 .desc = "read samples using the mmap interface",
1408 .func = test__basic_mmap,
1409 },
13b62567
JO
1410 {
1411 .desc = "parse events tests",
f50246e2 1412 .func = parse_events__test,
13b62567 1413 },
08aa0d1f
PZ
1414#if defined(__x86_64__) || defined(__i386__)
1415 {
1416 .desc = "x86 rdpmc test",
1417 .func = test__rdpmc,
1418 },
1419#endif
3e7c439a
ACM
1420 {
1421 .desc = "Validate PERF_RECORD_* events & perf_sample fields",
1422 .func = test__PERF_RECORD,
1423 },
cd82a32e
JO
1424 {
1425 .desc = "Test perf pmu format parsing",
1426 .func = test__perf_pmu,
1427 },
f7add556
JO
1428 {
1429 .desc = "Test dso data interface",
1430 .func = dso__test_data,
1431 },
8ad7013b
ACM
1432 {
1433 .desc = "roundtrip evsel->name check",
1434 .func = perf_evsel__roundtrip_name_test,
1435 },
6a6cd11d
ACM
1436 {
1437 .desc = "Check parsing of sched tracepoints fields",
1438 .func = perf_evsel__tp_sched_test,
1439 },
eb2f2703
ACM
1440 {
1441 .desc = "Generate and check syscalls:sys_enter_open event fields",
1442 .func = test__syscall_open_tp_fields,
1443 },
d898b241
JO
1444 {
1445 .desc = "struct perf_event_attr setup",
1446 .func = test_attr__run,
1447 },
1c6a800c
ACM
1448 {
1449 .func = NULL,
1450 },
1451};
1452
e60770a0 1453static bool perf_test__matches(int curr, int argc, const char *argv[])
1c6a800c 1454{
e60770a0
ACM
1455 int i;
1456
1457 if (argc == 0)
1458 return true;
1459
1460 for (i = 0; i < argc; ++i) {
1461 char *end;
1462 long nr = strtoul(argv[i], &end, 10);
1463
1464 if (*end == '\0') {
1465 if (nr == curr + 1)
1466 return true;
1467 continue;
1468 }
1c6a800c 1469
e60770a0
ACM
1470 if (strstr(tests[curr].desc, argv[i]))
1471 return true;
1472 }
1473
1474 return false;
1475}
1476
1477static int __cmd_test(int argc, const char *argv[])
1478{
1479 int i = 0;
9a8e85ad 1480 int width = 0;
1c6a800c 1481
9a8e85ad
ACM
1482 while (tests[i].func) {
1483 int len = strlen(tests[i].desc);
1484
1485 if (width < len)
1486 width = len;
1487 ++i;
1488 }
945aea22 1489
9a8e85ad 1490 i = 0;
1c6a800c 1491 while (tests[i].func) {
e60770a0
ACM
1492 int curr = i++, err;
1493
1494 if (!perf_test__matches(curr, argc, argv))
1495 continue;
1496
9a8e85ad 1497 pr_info("%2d: %-*s:", i, width, tests[curr].desc);
1c6a800c 1498 pr_debug("\n--- start ---\n");
e60770a0
ACM
1499 err = tests[curr].func();
1500 pr_debug("---- end ----\n%s:", tests[curr].desc);
9a8e85ad
ACM
1501 if (err)
1502 color_fprintf(stderr, PERF_COLOR_RED, " FAILED!\n");
1503 else
1504 pr_info(" Ok\n");
1c6a800c
ACM
1505 }
1506
1507 return 0;
1508}
1509
e60770a0
ACM
1510static int perf_test__list(int argc, const char **argv)
1511{
1512 int i = 0;
1513
1514 while (tests[i].func) {
1515 int curr = i++;
1516
1517 if (argc > 1 && !strstr(tests[curr].desc, argv[1]))
1518 continue;
1519
1520 pr_info("%2d: %s\n", i, tests[curr].desc);
1521 }
1522
1523 return 0;
1524}
1c6a800c 1525
1d037ca1 1526int cmd_test(int argc, const char **argv, const char *prefix __maybe_unused)
e60770a0
ACM
1527{
1528 const char * const test_usage[] = {
1529 "perf test [<options>] [{list <test-name-fragment>|[<test-name-fragments>|<test-numbers>]}]",
1530 NULL,
1531 };
1532 const struct option test_options[] = {
c30ab8aa 1533 OPT_INCR('v', "verbose", &verbose,
1c6a800c
ACM
1534 "be more verbose (show symbol address, etc)"),
1535 OPT_END()
e60770a0 1536 };
1c6a800c 1537
1c6a800c 1538 argc = parse_options(argc, argv, test_options, test_usage, 0);
e60770a0
ACM
1539 if (argc >= 1 && !strcmp(argv[0], "list"))
1540 return perf_test__list(argc, argv);
1c6a800c
ACM
1541
1542 symbol_conf.priv_size = sizeof(int);
1543 symbol_conf.sort_by_name = true;
1544 symbol_conf.try_vmlinux_path = true;
1545
1546 if (symbol__init() < 0)
1547 return -1;
1548
e60770a0 1549 return __cmd_test(argc, argv);
1c6a800c 1550}
This page took 0.214836 seconds and 5 git commands to generate.