4 * Builtin regression testing command: ever growing number of sanity tests
8 #include "util/cache.h"
9 #include "util/debug.h"
10 #include "util/debugfs.h"
11 #include "util/evlist.h"
12 #include "util/parse-options.h"
13 #include "util/parse-events.h"
14 #include "util/symbol.h"
15 #include "util/thread_map.h"
16 #include "../../include/linux/hw_breakpoint.h"
20 static int vmlinux_matches_kallsyms_filter(struct map *map __used, struct symbol *sym)
22 bool *visited = symbol__priv(sym);
27 static int test__vmlinux_matches_kallsyms(void)
32 struct map *kallsyms_map, *vmlinux_map;
33 struct machine kallsyms, vmlinux;
34 enum map_type type = MAP__FUNCTION;
35 long page_size = sysconf(_SC_PAGE_SIZE);
36 struct ref_reloc_sym ref_reloc_sym = { .name = "_stext", };
41 * Init the machines that will hold kernel, modules obtained from
42 * both vmlinux + .ko files and from /proc/kallsyms split by modules.
44 machine__init(&kallsyms, "", HOST_KERNEL_ID);
45 machine__init(&vmlinux, "", HOST_KERNEL_ID);
50 * Create the kernel maps for kallsyms and the DSO where we will then
51 * load /proc/kallsyms. Also create the modules maps from /proc/modules
52 * and find the .ko files that match them in /lib/modules/`uname -r`/.
54 if (machine__create_kernel_maps(&kallsyms) < 0) {
55 pr_debug("machine__create_kernel_maps ");
62 * Load and split /proc/kallsyms into multiple maps, one per module.
64 if (machine__load_kallsyms(&kallsyms, "/proc/kallsyms", type, NULL) <= 0) {
65 pr_debug("dso__load_kallsyms ");
72 * kallsyms will be internally on demand sorted by name so that we can
73 * find the reference relocation * symbol, i.e. the symbol we will use
74 * to see if the running kernel was relocated by checking if it has the
75 * same value in the vmlinux file we load.
77 kallsyms_map = machine__kernel_map(&kallsyms, type);
79 sym = map__find_symbol_by_name(kallsyms_map, ref_reloc_sym.name, NULL);
81 pr_debug("dso__find_symbol_by_name ");
85 ref_reloc_sym.addr = sym->start;
90 * Now repeat step 2, this time for the vmlinux file we'll auto-locate.
92 if (machine__create_kernel_maps(&vmlinux) < 0) {
93 pr_debug("machine__create_kernel_maps ");
97 vmlinux_map = machine__kernel_map(&vmlinux, type);
98 map__kmap(vmlinux_map)->ref_reloc_sym = &ref_reloc_sym;
103 * Locate a vmlinux file in the vmlinux path that has a buildid that
104 * matches the one of the running kernel.
106 * While doing that look if we find the ref reloc symbol, if we find it
107 * we'll have its ref_reloc_symbol.unrelocated_addr and then
108 * maps__reloc_vmlinux will notice and set proper ->[un]map_ip routines
109 * to fixup the symbols.
111 if (machine__load_vmlinux_path(&vmlinux, type,
112 vmlinux_matches_kallsyms_filter) <= 0) {
113 pr_debug("machine__load_vmlinux_path ");
121 * Now look at the symbols in the vmlinux DSO and check if we find all of them
122 * in the kallsyms dso. For the ones that are in both, check its names and
125 for (nd = rb_first(&vmlinux_map->dso->symbols[type]); nd; nd = rb_next(nd)) {
126 struct symbol *pair, *first_pair;
127 bool backwards = true;
129 sym = rb_entry(nd, struct symbol, rb_node);
131 if (sym->start == sym->end)
134 first_pair = machine__find_kernel_symbol(&kallsyms, type, sym->start, NULL, NULL);
137 if (pair && pair->start == sym->start) {
139 if (strcmp(sym->name, pair->name) == 0) {
141 * kallsyms don't have the symbol end, so we
142 * set that by using the next symbol start - 1,
143 * in some cases we get this up to a page
144 * wrong, trace_kmalloc when I was developing
145 * this code was one such example, 2106 bytes
146 * off the real size. More than that and we
147 * _really_ have a problem.
149 s64 skew = sym->end - pair->end;
150 if (llabs(skew) < page_size)
153 pr_debug("%#" PRIx64 ": diff end addr for %s v: %#" PRIx64 " k: %#" PRIx64 "\n",
154 sym->start, sym->name, sym->end, pair->end);
158 nnd = backwards ? rb_prev(&pair->rb_node) :
159 rb_next(&pair->rb_node);
161 struct symbol *next = rb_entry(nnd, struct symbol, rb_node);
163 if (next->start == sym->start) {
175 pr_debug("%#" PRIx64 ": diff name v: %s k: %s\n",
176 sym->start, sym->name, pair->name);
179 pr_debug("%#" PRIx64 ": %s not on kallsyms\n", sym->start, sym->name);
187 pr_info("Maps only in vmlinux:\n");
189 for (nd = rb_first(&vmlinux.kmaps.maps[type]); nd; nd = rb_next(nd)) {
190 struct map *pos = rb_entry(nd, struct map, rb_node), *pair;
192 * If it is the kernel, kallsyms is always "[kernel.kallsyms]", while
193 * the kernel will have the path for the vmlinux file being used,
194 * so use the short name, less descriptive but the same ("[kernel]" in
197 pair = map_groups__find_by_name(&kallsyms.kmaps, type,
199 pos->dso->short_name :
204 map__fprintf(pos, stderr);
207 pr_info("Maps in vmlinux with a different name in kallsyms:\n");
209 for (nd = rb_first(&vmlinux.kmaps.maps[type]); nd; nd = rb_next(nd)) {
210 struct map *pos = rb_entry(nd, struct map, rb_node), *pair;
212 pair = map_groups__find(&kallsyms.kmaps, type, pos->start);
213 if (pair == NULL || pair->priv)
216 if (pair->start == pos->start) {
218 pr_info(" %" PRIx64 "-%" PRIx64 " %" PRIx64 " %s in kallsyms as",
219 pos->start, pos->end, pos->pgoff, pos->dso->name);
220 if (pos->pgoff != pair->pgoff || pos->end != pair->end)
221 pr_info(": \n*%" PRIx64 "-%" PRIx64 " %" PRIx64 "",
222 pair->start, pair->end, pair->pgoff);
223 pr_info(" %s\n", pair->dso->name);
228 pr_info("Maps only in kallsyms:\n");
230 for (nd = rb_first(&kallsyms.kmaps.maps[type]);
231 nd; nd = rb_next(nd)) {
232 struct map *pos = rb_entry(nd, struct map, rb_node);
235 map__fprintf(pos, stderr);
241 #include "util/cpumap.h"
242 #include "util/evsel.h"
243 #include <sys/types.h>
245 static int trace_event__id(const char *evname)
250 if (asprintf(&filename,
252 tracing_events_path, evname) < 0)
255 fd = open(filename, O_RDONLY);
258 if (read(fd, id, sizeof(id)) > 0)
267 static int test__open_syscall_event(void)
270 struct thread_map *threads;
271 struct perf_evsel *evsel;
272 struct perf_event_attr attr;
273 unsigned int nr_open_calls = 111, i;
274 int id = trace_event__id("sys_enter_open");
277 pr_debug("is debugfs mounted on /sys/kernel/debug?\n");
281 threads = thread_map__new(-1, getpid(), UINT_MAX);
282 if (threads == NULL) {
283 pr_debug("thread_map__new\n");
287 memset(&attr, 0, sizeof(attr));
288 attr.type = PERF_TYPE_TRACEPOINT;
290 evsel = perf_evsel__new(&attr, 0);
292 pr_debug("perf_evsel__new\n");
293 goto out_thread_map_delete;
296 if (perf_evsel__open_per_thread(evsel, threads, false, NULL) < 0) {
297 pr_debug("failed to open counter: %s, "
298 "tweak /proc/sys/kernel/perf_event_paranoid?\n",
300 goto out_evsel_delete;
303 for (i = 0; i < nr_open_calls; ++i) {
304 fd = open("/etc/passwd", O_RDONLY);
308 if (perf_evsel__read_on_cpu(evsel, 0, 0) < 0) {
309 pr_debug("perf_evsel__read_on_cpu\n");
313 if (evsel->counts->cpu[0].val != nr_open_calls) {
314 pr_debug("perf_evsel__read_on_cpu: expected to intercept %d calls, got %" PRIu64 "\n",
315 nr_open_calls, evsel->counts->cpu[0].val);
321 perf_evsel__close_fd(evsel, 1, threads->nr);
323 perf_evsel__delete(evsel);
324 out_thread_map_delete:
325 thread_map__delete(threads);
331 static int test__open_syscall_event_on_all_cpus(void)
333 int err = -1, fd, cpu;
334 struct thread_map *threads;
335 struct cpu_map *cpus;
336 struct perf_evsel *evsel;
337 struct perf_event_attr attr;
338 unsigned int nr_open_calls = 111, i;
340 int id = trace_event__id("sys_enter_open");
343 pr_debug("is debugfs mounted on /sys/kernel/debug?\n");
347 threads = thread_map__new(-1, getpid(), UINT_MAX);
348 if (threads == NULL) {
349 pr_debug("thread_map__new\n");
353 cpus = cpu_map__new(NULL);
355 pr_debug("cpu_map__new\n");
356 goto out_thread_map_delete;
362 memset(&attr, 0, sizeof(attr));
363 attr.type = PERF_TYPE_TRACEPOINT;
365 evsel = perf_evsel__new(&attr, 0);
367 pr_debug("perf_evsel__new\n");
368 goto out_thread_map_delete;
371 if (perf_evsel__open(evsel, cpus, threads, false, NULL) < 0) {
372 pr_debug("failed to open counter: %s, "
373 "tweak /proc/sys/kernel/perf_event_paranoid?\n",
375 goto out_evsel_delete;
378 for (cpu = 0; cpu < cpus->nr; ++cpu) {
379 unsigned int ncalls = nr_open_calls + cpu;
381 * XXX eventually lift this restriction in a way that
382 * keeps perf building on older glibc installations
383 * without CPU_ALLOC. 1024 cpus in 2010 still seems
384 * a reasonable upper limit tho :-)
386 if (cpus->map[cpu] >= CPU_SETSIZE) {
387 pr_debug("Ignoring CPU %d\n", cpus->map[cpu]);
391 CPU_SET(cpus->map[cpu], &cpu_set);
392 if (sched_setaffinity(0, sizeof(cpu_set), &cpu_set) < 0) {
393 pr_debug("sched_setaffinity() failed on CPU %d: %s ",
398 for (i = 0; i < ncalls; ++i) {
399 fd = open("/etc/passwd", O_RDONLY);
402 CPU_CLR(cpus->map[cpu], &cpu_set);
406 * Here we need to explicitely preallocate the counts, as if
407 * we use the auto allocation it will allocate just for 1 cpu,
408 * as we start by cpu 0.
410 if (perf_evsel__alloc_counts(evsel, cpus->nr) < 0) {
411 pr_debug("perf_evsel__alloc_counts(ncpus=%d)\n", cpus->nr);
417 for (cpu = 0; cpu < cpus->nr; ++cpu) {
418 unsigned int expected;
420 if (cpus->map[cpu] >= CPU_SETSIZE)
423 if (perf_evsel__read_on_cpu(evsel, cpu, 0) < 0) {
424 pr_debug("perf_evsel__read_on_cpu\n");
429 expected = nr_open_calls + cpu;
430 if (evsel->counts->cpu[cpu].val != expected) {
431 pr_debug("perf_evsel__read_on_cpu: expected to intercept %d calls on cpu %d, got %" PRIu64 "\n",
432 expected, cpus->map[cpu], evsel->counts->cpu[cpu].val);
438 perf_evsel__close_fd(evsel, 1, threads->nr);
440 perf_evsel__delete(evsel);
441 out_thread_map_delete:
442 thread_map__delete(threads);
447 * This test will generate random numbers of calls to some getpid syscalls,
448 * then establish an mmap for a group of events that are created to monitor
451 * It will receive the events, using mmap, use its PERF_SAMPLE_ID generated
452 * sample.id field to map back to its respective perf_evsel instance.
454 * Then it checks if the number of syscalls reported as perf events by
455 * the kernel corresponds to the number of syscalls made.
457 static int test__basic_mmap(void)
460 union perf_event *event;
461 struct thread_map *threads;
462 struct cpu_map *cpus;
463 struct perf_evlist *evlist;
464 struct perf_event_attr attr = {
465 .type = PERF_TYPE_TRACEPOINT,
466 .read_format = PERF_FORMAT_ID,
467 .sample_type = PERF_SAMPLE_ID,
471 const char *syscall_names[] = { "getsid", "getppid", "getpgrp",
473 pid_t (*syscalls[])(void) = { (void *)getsid, getppid, getpgrp,
475 #define nsyscalls ARRAY_SIZE(syscall_names)
477 unsigned int nr_events[nsyscalls],
478 expected_nr_events[nsyscalls], i, j;
479 struct perf_evsel *evsels[nsyscalls], *evsel;
480 int sample_size = __perf_evsel__sample_size(attr.sample_type);
482 for (i = 0; i < nsyscalls; ++i) {
485 snprintf(name, sizeof(name), "sys_enter_%s", syscall_names[i]);
486 ids[i] = trace_event__id(name);
488 pr_debug("Is debugfs mounted on /sys/kernel/debug?\n");
492 expected_nr_events[i] = random() % 257;
495 threads = thread_map__new(-1, getpid(), UINT_MAX);
496 if (threads == NULL) {
497 pr_debug("thread_map__new\n");
501 cpus = cpu_map__new(NULL);
503 pr_debug("cpu_map__new\n");
504 goto out_free_threads;
508 CPU_SET(cpus->map[0], &cpu_set);
509 sched_setaffinity(0, sizeof(cpu_set), &cpu_set);
510 if (sched_setaffinity(0, sizeof(cpu_set), &cpu_set) < 0) {
511 pr_debug("sched_setaffinity() failed on CPU %d: %s ",
512 cpus->map[0], strerror(errno));
516 evlist = perf_evlist__new(cpus, threads);
517 if (evlist == NULL) {
518 pr_debug("perf_evlist__new\n");
522 /* anonymous union fields, can't be initialized above */
523 attr.wakeup_events = 1;
524 attr.sample_period = 1;
526 for (i = 0; i < nsyscalls; ++i) {
527 attr.config = ids[i];
528 evsels[i] = perf_evsel__new(&attr, i);
529 if (evsels[i] == NULL) {
530 pr_debug("perf_evsel__new\n");
531 goto out_free_evlist;
534 perf_evlist__add(evlist, evsels[i]);
536 if (perf_evsel__open(evsels[i], cpus, threads, false, NULL) < 0) {
537 pr_debug("failed to open counter: %s, "
538 "tweak /proc/sys/kernel/perf_event_paranoid?\n",
544 if (perf_evlist__mmap(evlist, 128, true) < 0) {
545 pr_debug("failed to mmap events: %d (%s)\n", errno,
550 for (i = 0; i < nsyscalls; ++i)
551 for (j = 0; j < expected_nr_events[i]; ++j) {
552 int foo = syscalls[i]();
556 while ((event = perf_evlist__mmap_read(evlist, 0)) != NULL) {
557 struct perf_sample sample;
559 if (event->header.type != PERF_RECORD_SAMPLE) {
560 pr_debug("unexpected %s event\n",
561 perf_event__name(event->header.type));
565 err = perf_event__parse_sample(event, attr.sample_type, sample_size,
566 false, &sample, false);
568 pr_err("Can't parse sample, err = %d\n", err);
572 evsel = perf_evlist__id2evsel(evlist, sample.id);
574 pr_debug("event with id %" PRIu64
575 " doesn't map to an evsel\n", sample.id);
578 nr_events[evsel->idx]++;
581 list_for_each_entry(evsel, &evlist->entries, node) {
582 if (nr_events[evsel->idx] != expected_nr_events[evsel->idx]) {
583 pr_debug("expected %d %s events, got %d\n",
584 expected_nr_events[evsel->idx],
585 event_name(evsel), nr_events[evsel->idx]);
592 perf_evlist__munmap(evlist);
594 for (i = 0; i < nsyscalls; ++i)
595 perf_evsel__close_fd(evsels[i], 1, threads->nr);
597 perf_evlist__delete(evlist);
599 cpu_map__delete(cpus);
601 thread_map__delete(threads);
606 #define TEST_ASSERT_VAL(text, cond) \
609 pr_debug("FAILED %s:%d %s\n", __FILE__, __LINE__, text); \
614 static int test__checkevent_tracepoint(struct perf_evlist *evlist)
616 struct perf_evsel *evsel = list_entry(evlist->entries.next,
617 struct perf_evsel, node);
619 TEST_ASSERT_VAL("wrong number of entries", 1 == evlist->nr_entries);
620 TEST_ASSERT_VAL("wrong type", PERF_TYPE_TRACEPOINT == evsel->attr.type);
621 TEST_ASSERT_VAL("wrong sample_type",
622 (PERF_SAMPLE_RAW | PERF_SAMPLE_TIME | PERF_SAMPLE_CPU) ==
623 evsel->attr.sample_type);
624 TEST_ASSERT_VAL("wrong sample_period", 1 == evsel->attr.sample_period);
628 static int test__checkevent_tracepoint_multi(struct perf_evlist *evlist)
630 struct perf_evsel *evsel;
632 TEST_ASSERT_VAL("wrong number of entries", evlist->nr_entries > 1);
634 list_for_each_entry(evsel, &evlist->entries, node) {
635 TEST_ASSERT_VAL("wrong type",
636 PERF_TYPE_TRACEPOINT == evsel->attr.type);
637 TEST_ASSERT_VAL("wrong sample_type",
638 (PERF_SAMPLE_RAW | PERF_SAMPLE_TIME | PERF_SAMPLE_CPU)
639 == evsel->attr.sample_type);
640 TEST_ASSERT_VAL("wrong sample_period",
641 1 == evsel->attr.sample_period);
646 static int test__checkevent_raw(struct perf_evlist *evlist)
648 struct perf_evsel *evsel = list_entry(evlist->entries.next,
649 struct perf_evsel, node);
651 TEST_ASSERT_VAL("wrong number of entries", 1 == evlist->nr_entries);
652 TEST_ASSERT_VAL("wrong type", PERF_TYPE_RAW == evsel->attr.type);
653 TEST_ASSERT_VAL("wrong config", 1 == evsel->attr.config);
657 static int test__checkevent_numeric(struct perf_evlist *evlist)
659 struct perf_evsel *evsel = list_entry(evlist->entries.next,
660 struct perf_evsel, node);
662 TEST_ASSERT_VAL("wrong number of entries", 1 == evlist->nr_entries);
663 TEST_ASSERT_VAL("wrong type", 1 == evsel->attr.type);
664 TEST_ASSERT_VAL("wrong config", 1 == evsel->attr.config);
668 static int test__checkevent_symbolic_name(struct perf_evlist *evlist)
670 struct perf_evsel *evsel = list_entry(evlist->entries.next,
671 struct perf_evsel, node);
673 TEST_ASSERT_VAL("wrong number of entries", 1 == evlist->nr_entries);
674 TEST_ASSERT_VAL("wrong type", PERF_TYPE_HARDWARE == evsel->attr.type);
675 TEST_ASSERT_VAL("wrong config",
676 PERF_COUNT_HW_INSTRUCTIONS == evsel->attr.config);
680 static int test__checkevent_symbolic_alias(struct perf_evlist *evlist)
682 struct perf_evsel *evsel = list_entry(evlist->entries.next,
683 struct perf_evsel, node);
685 TEST_ASSERT_VAL("wrong number of entries", 1 == evlist->nr_entries);
686 TEST_ASSERT_VAL("wrong type", PERF_TYPE_SOFTWARE == evsel->attr.type);
687 TEST_ASSERT_VAL("wrong config",
688 PERF_COUNT_SW_PAGE_FAULTS == evsel->attr.config);
692 static int test__checkevent_genhw(struct perf_evlist *evlist)
694 struct perf_evsel *evsel = list_entry(evlist->entries.next,
695 struct perf_evsel, node);
697 TEST_ASSERT_VAL("wrong number of entries", 1 == evlist->nr_entries);
698 TEST_ASSERT_VAL("wrong type", PERF_TYPE_HW_CACHE == evsel->attr.type);
699 TEST_ASSERT_VAL("wrong config", (1 << 16) == evsel->attr.config);
703 static int test__checkevent_breakpoint(struct perf_evlist *evlist)
705 struct perf_evsel *evsel = list_entry(evlist->entries.next,
706 struct perf_evsel, node);
708 TEST_ASSERT_VAL("wrong number of entries", 1 == evlist->nr_entries);
709 TEST_ASSERT_VAL("wrong type", PERF_TYPE_BREAKPOINT == evsel->attr.type);
710 TEST_ASSERT_VAL("wrong config", 0 == evsel->attr.config);
711 TEST_ASSERT_VAL("wrong bp_type", (HW_BREAKPOINT_R | HW_BREAKPOINT_W) ==
712 evsel->attr.bp_type);
713 TEST_ASSERT_VAL("wrong bp_len", HW_BREAKPOINT_LEN_4 ==
718 static int test__checkevent_breakpoint_x(struct perf_evlist *evlist)
720 struct perf_evsel *evsel = list_entry(evlist->entries.next,
721 struct perf_evsel, node);
723 TEST_ASSERT_VAL("wrong number of entries", 1 == evlist->nr_entries);
724 TEST_ASSERT_VAL("wrong type", PERF_TYPE_BREAKPOINT == evsel->attr.type);
725 TEST_ASSERT_VAL("wrong config", 0 == evsel->attr.config);
726 TEST_ASSERT_VAL("wrong bp_type",
727 HW_BREAKPOINT_X == evsel->attr.bp_type);
728 TEST_ASSERT_VAL("wrong bp_len", sizeof(long) == evsel->attr.bp_len);
732 static int test__checkevent_breakpoint_r(struct perf_evlist *evlist)
734 struct perf_evsel *evsel = list_entry(evlist->entries.next,
735 struct perf_evsel, node);
737 TEST_ASSERT_VAL("wrong number of entries", 1 == evlist->nr_entries);
738 TEST_ASSERT_VAL("wrong type",
739 PERF_TYPE_BREAKPOINT == evsel->attr.type);
740 TEST_ASSERT_VAL("wrong config", 0 == evsel->attr.config);
741 TEST_ASSERT_VAL("wrong bp_type",
742 HW_BREAKPOINT_R == evsel->attr.bp_type);
743 TEST_ASSERT_VAL("wrong bp_len",
744 HW_BREAKPOINT_LEN_4 == evsel->attr.bp_len);
748 static int test__checkevent_breakpoint_w(struct perf_evlist *evlist)
750 struct perf_evsel *evsel = list_entry(evlist->entries.next,
751 struct perf_evsel, node);
753 TEST_ASSERT_VAL("wrong number of entries", 1 == evlist->nr_entries);
754 TEST_ASSERT_VAL("wrong type",
755 PERF_TYPE_BREAKPOINT == evsel->attr.type);
756 TEST_ASSERT_VAL("wrong config", 0 == evsel->attr.config);
757 TEST_ASSERT_VAL("wrong bp_type",
758 HW_BREAKPOINT_W == evsel->attr.bp_type);
759 TEST_ASSERT_VAL("wrong bp_len",
760 HW_BREAKPOINT_LEN_4 == evsel->attr.bp_len);
764 static int test__checkevent_tracepoint_modifier(struct perf_evlist *evlist)
766 struct perf_evsel *evsel = list_entry(evlist->entries.next,
767 struct perf_evsel, node);
769 TEST_ASSERT_VAL("wrong exclude_user", evsel->attr.exclude_user);
770 TEST_ASSERT_VAL("wrong exclude_kernel", !evsel->attr.exclude_kernel);
771 TEST_ASSERT_VAL("wrong exclude_hv", evsel->attr.exclude_hv);
772 TEST_ASSERT_VAL("wrong precise_ip", !evsel->attr.precise_ip);
774 return test__checkevent_tracepoint(evlist);
778 test__checkevent_tracepoint_multi_modifier(struct perf_evlist *evlist)
780 struct perf_evsel *evsel;
782 TEST_ASSERT_VAL("wrong number of entries", evlist->nr_entries > 1);
784 list_for_each_entry(evsel, &evlist->entries, node) {
785 TEST_ASSERT_VAL("wrong exclude_user",
786 !evsel->attr.exclude_user);
787 TEST_ASSERT_VAL("wrong exclude_kernel",
788 evsel->attr.exclude_kernel);
789 TEST_ASSERT_VAL("wrong exclude_hv", evsel->attr.exclude_hv);
790 TEST_ASSERT_VAL("wrong precise_ip", !evsel->attr.precise_ip);
793 return test__checkevent_tracepoint_multi(evlist);
796 static int test__checkevent_raw_modifier(struct perf_evlist *evlist)
798 struct perf_evsel *evsel = list_entry(evlist->entries.next,
799 struct perf_evsel, node);
801 TEST_ASSERT_VAL("wrong exclude_user", evsel->attr.exclude_user);
802 TEST_ASSERT_VAL("wrong exclude_kernel", !evsel->attr.exclude_kernel);
803 TEST_ASSERT_VAL("wrong exclude_hv", evsel->attr.exclude_hv);
804 TEST_ASSERT_VAL("wrong precise_ip", evsel->attr.precise_ip);
806 return test__checkevent_raw(evlist);
809 static int test__checkevent_numeric_modifier(struct perf_evlist *evlist)
811 struct perf_evsel *evsel = list_entry(evlist->entries.next,
812 struct perf_evsel, node);
814 TEST_ASSERT_VAL("wrong exclude_user", evsel->attr.exclude_user);
815 TEST_ASSERT_VAL("wrong exclude_kernel", evsel->attr.exclude_kernel);
816 TEST_ASSERT_VAL("wrong exclude_hv", !evsel->attr.exclude_hv);
817 TEST_ASSERT_VAL("wrong precise_ip", evsel->attr.precise_ip);
819 return test__checkevent_numeric(evlist);
822 static int test__checkevent_symbolic_name_modifier(struct perf_evlist *evlist)
824 struct perf_evsel *evsel = list_entry(evlist->entries.next,
825 struct perf_evsel, node);
827 TEST_ASSERT_VAL("wrong exclude_user", evsel->attr.exclude_user);
828 TEST_ASSERT_VAL("wrong exclude_kernel", evsel->attr.exclude_kernel);
829 TEST_ASSERT_VAL("wrong exclude_hv", !evsel->attr.exclude_hv);
830 TEST_ASSERT_VAL("wrong precise_ip", !evsel->attr.precise_ip);
832 return test__checkevent_symbolic_name(evlist);
835 static int test__checkevent_symbolic_alias_modifier(struct perf_evlist *evlist)
837 struct perf_evsel *evsel = list_entry(evlist->entries.next,
838 struct perf_evsel, node);
840 TEST_ASSERT_VAL("wrong exclude_user", !evsel->attr.exclude_user);
841 TEST_ASSERT_VAL("wrong exclude_kernel", evsel->attr.exclude_kernel);
842 TEST_ASSERT_VAL("wrong exclude_hv", evsel->attr.exclude_hv);
843 TEST_ASSERT_VAL("wrong precise_ip", !evsel->attr.precise_ip);
845 return test__checkevent_symbolic_alias(evlist);
848 static int test__checkevent_genhw_modifier(struct perf_evlist *evlist)
850 struct perf_evsel *evsel = list_entry(evlist->entries.next,
851 struct perf_evsel, node);
853 TEST_ASSERT_VAL("wrong exclude_user", evsel->attr.exclude_user);
854 TEST_ASSERT_VAL("wrong exclude_kernel", !evsel->attr.exclude_kernel);
855 TEST_ASSERT_VAL("wrong exclude_hv", evsel->attr.exclude_hv);
856 TEST_ASSERT_VAL("wrong precise_ip", evsel->attr.precise_ip);
858 return test__checkevent_genhw(evlist);
861 static struct test__event_st {
864 int (*check)(struct perf_evlist *evlist);
867 .name = "syscalls:sys_enter_open",
868 .check = test__checkevent_tracepoint,
871 .name = "syscalls:*",
872 .check = test__checkevent_tracepoint_multi,
876 .check = test__checkevent_raw,
880 .check = test__checkevent_numeric,
883 .name = "instructions",
884 .check = test__checkevent_symbolic_name,
888 .check = test__checkevent_symbolic_alias,
891 .name = "L1-dcache-load-miss",
892 .check = test__checkevent_genhw,
896 .check = test__checkevent_breakpoint,
900 .check = test__checkevent_breakpoint_x,
904 .check = test__checkevent_breakpoint_r,
908 .check = test__checkevent_breakpoint_w,
911 .name = "syscalls:sys_enter_open:k",
912 .check = test__checkevent_tracepoint_modifier,
915 .name = "syscalls:*:u",
916 .check = test__checkevent_tracepoint_multi_modifier,
920 .check = test__checkevent_raw_modifier,
924 .check = test__checkevent_numeric_modifier,
927 .name = "instructions:h",
928 .check = test__checkevent_symbolic_name_modifier,
932 .check = test__checkevent_symbolic_alias_modifier,
935 .name = "L1-dcache-load-miss:kp",
936 .check = test__checkevent_genhw_modifier,
940 #define TEST__EVENTS_CNT (sizeof(test__events) / sizeof(struct test__event_st))
942 static int test__parse_events(void)
944 struct perf_evlist *evlist;
948 for (i = 0; i < TEST__EVENTS_CNT; i++) {
949 struct test__event_st *e = &test__events[i];
951 evlist = perf_evlist__new(NULL, NULL);
955 ret = parse_events(evlist, e->name, 0);
957 pr_debug("failed to parse event '%s', err %d\n",
962 ret = e->check(evlist);
966 perf_evlist__delete(evlist);
972 static int sched__get_first_possible_cpu(pid_t pid, cpu_set_t **maskp,
977 int i, cpu = -1, nrcpus = 1024;
979 mask = CPU_ALLOC(nrcpus);
980 size = CPU_ALLOC_SIZE(nrcpus);
981 CPU_ZERO_S(size, mask);
983 if (sched_getaffinity(pid, size, mask) == -1) {
985 if (errno == EINVAL && nrcpus < (1024 << 8)) {
986 nrcpus = nrcpus << 2;
989 perror("sched_getaffinity");
993 for (i = 0; i < nrcpus; i++) {
994 if (CPU_ISSET_S(i, size, mask)) {
1000 CPU_CLR_S(i, size, mask);
1010 static int test__PERF_RECORD(void)
1012 struct perf_record_opts opts = {
1018 .sample_id_all_avail = true,
1020 cpu_set_t *cpu_mask = NULL;
1021 size_t cpu_mask_size = 0;
1022 struct perf_evlist *evlist = perf_evlist__new(NULL, NULL);
1023 struct perf_evsel *evsel;
1024 struct perf_sample sample;
1025 const char *cmd = "sleep";
1026 const char *argv[] = { cmd, "1", NULL, };
1028 u64 sample_type, prev_time = 0;
1029 bool found_cmd_mmap = false,
1030 found_libc_mmap = false,
1031 found_vdso_mmap = false,
1032 found_ld_mmap = false;
1033 int err = -1, errs = 0, i, wakeups = 0, sample_size;
1035 int total_events = 0, nr_events[PERF_RECORD_MAX] = { 0, };
1037 if (evlist == NULL || argv == NULL) {
1038 pr_debug("Not enough memory to create evlist\n");
1043 * We need at least one evsel in the evlist, use the default
1046 err = perf_evlist__add_default(evlist);
1048 pr_debug("Not enough memory to create evsel\n");
1049 goto out_delete_evlist;
1053 * Create maps of threads and cpus to monitor. In this case
1054 * we start with all threads and cpus (-1, -1) but then in
1055 * perf_evlist__prepare_workload we'll fill in the only thread
1056 * we're monitoring, the one forked there.
1058 err = perf_evlist__create_maps(evlist, opts.target_pid,
1059 opts.target_tid, UINT_MAX, opts.cpu_list);
1061 pr_debug("Not enough memory to create thread/cpu maps\n");
1062 goto out_delete_evlist;
1066 * Prepare the workload in argv[] to run, it'll fork it, and then wait
1067 * for perf_evlist__start_workload() to exec it. This is done this way
1068 * so that we have time to open the evlist (calling sys_perf_event_open
1069 * on all the fds) and then mmap them.
1071 err = perf_evlist__prepare_workload(evlist, &opts, argv);
1073 pr_debug("Couldn't run the workload!\n");
1074 goto out_delete_evlist;
1078 * Config the evsels, setting attr->comm on the first one, etc.
1080 evsel = list_entry(evlist->entries.next, struct perf_evsel, node);
1081 evsel->attr.sample_type |= PERF_SAMPLE_CPU;
1082 evsel->attr.sample_type |= PERF_SAMPLE_TID;
1083 evsel->attr.sample_type |= PERF_SAMPLE_TIME;
1084 perf_evlist__config_attrs(evlist, &opts);
1086 err = sched__get_first_possible_cpu(evlist->workload.pid, &cpu_mask,
1089 pr_debug("sched__get_first_possible_cpu: %s\n", strerror(errno));
1090 goto out_delete_evlist;
1096 * So that we can check perf_sample.cpu on all the samples.
1098 if (sched_setaffinity(evlist->workload.pid, cpu_mask_size, cpu_mask) < 0) {
1099 pr_debug("sched_setaffinity: %s\n", strerror(errno));
1100 goto out_free_cpu_mask;
1104 * Call sys_perf_event_open on all the fds on all the evsels,
1105 * grouping them if asked to.
1107 err = perf_evlist__open(evlist, opts.group);
1109 pr_debug("perf_evlist__open: %s\n", strerror(errno));
1110 goto out_delete_evlist;
1114 * mmap the first fd on a given CPU and ask for events for the other
1115 * fds in the same CPU to be injected in the same mmap ring buffer
1116 * (using ioctl(PERF_EVENT_IOC_SET_OUTPUT)).
1118 err = perf_evlist__mmap(evlist, opts.mmap_pages, false);
1120 pr_debug("perf_evlist__mmap: %s\n", strerror(errno));
1121 goto out_delete_evlist;
1125 * We'll need these two to parse the PERF_SAMPLE_* fields in each
1128 sample_type = perf_evlist__sample_type(evlist);
1129 sample_size = __perf_evsel__sample_size(sample_type);
1132 * Now that all is properly set up, enable the events, they will
1133 * count just on workload.pid, which will start...
1135 perf_evlist__enable(evlist);
1140 perf_evlist__start_workload(evlist);
1143 int before = total_events;
1145 for (i = 0; i < evlist->nr_mmaps; i++) {
1146 union perf_event *event;
1148 while ((event = perf_evlist__mmap_read(evlist, i)) != NULL) {
1149 const u32 type = event->header.type;
1150 const char *name = perf_event__name(type);
1153 if (type < PERF_RECORD_MAX)
1156 err = perf_event__parse_sample(event, sample_type,
1161 perf_event__fprintf(event, stderr);
1162 pr_debug("Couldn't parse sample\n");
1167 pr_info("%" PRIu64" %d ", sample.time, sample.cpu);
1168 perf_event__fprintf(event, stderr);
1171 if (prev_time > sample.time) {
1172 pr_debug("%s going backwards in time, prev=%" PRIu64 ", curr=%" PRIu64 "\n",
1173 name, prev_time, sample.time);
1177 prev_time = sample.time;
1179 if (sample.cpu != cpu) {
1180 pr_debug("%s with unexpected cpu, expected %d, got %d\n",
1181 name, cpu, sample.cpu);
1185 if ((pid_t)sample.pid != evlist->workload.pid) {
1186 pr_debug("%s with unexpected pid, expected %d, got %d\n",
1187 name, evlist->workload.pid, sample.pid);
1191 if ((pid_t)sample.tid != evlist->workload.pid) {
1192 pr_debug("%s with unexpected tid, expected %d, got %d\n",
1193 name, evlist->workload.pid, sample.tid);
1197 if ((type == PERF_RECORD_COMM ||
1198 type == PERF_RECORD_MMAP ||
1199 type == PERF_RECORD_FORK ||
1200 type == PERF_RECORD_EXIT) &&
1201 (pid_t)event->comm.pid != evlist->workload.pid) {
1202 pr_debug("%s with unexpected pid/tid\n", name);
1206 if ((type == PERF_RECORD_COMM ||
1207 type == PERF_RECORD_MMAP) &&
1208 event->comm.pid != event->comm.tid) {
1209 pr_debug("%s with different pid/tid!\n", name);
1214 case PERF_RECORD_COMM:
1215 if (strcmp(event->comm.comm, cmd)) {
1216 pr_debug("%s with unexpected comm!\n", name);
1220 case PERF_RECORD_EXIT:
1222 case PERF_RECORD_MMAP:
1223 bname = strrchr(event->mmap.filename, '/');
1224 if (bname != NULL) {
1225 if (!found_cmd_mmap)
1226 found_cmd_mmap = !strcmp(bname + 1, cmd);
1227 if (!found_libc_mmap)
1228 found_libc_mmap = !strncmp(bname + 1, "libc", 4);
1230 found_ld_mmap = !strncmp(bname + 1, "ld", 2);
1231 } else if (!found_vdso_mmap)
1232 found_vdso_mmap = !strcmp(event->mmap.filename, "[vdso]");
1235 case PERF_RECORD_SAMPLE:
1236 /* Just ignore samples for now */
1239 pr_debug("Unexpected perf_event->header.type %d!\n",
1247 * We don't use poll here because at least at 3.1 times the
1248 * PERF_RECORD_{!SAMPLE} events don't honour
1249 * perf_event_attr.wakeup_events, just PERF_EVENT_SAMPLE does.
1251 if (total_events == before && false)
1252 poll(evlist->pollfd, evlist->nr_fds, -1);
1255 if (++wakeups > 5) {
1256 pr_debug("No PERF_RECORD_EXIT event!\n");
1262 if (nr_events[PERF_RECORD_COMM] > 1) {
1263 pr_debug("Excessive number of PERF_RECORD_COMM events!\n");
1267 if (nr_events[PERF_RECORD_COMM] == 0) {
1268 pr_debug("Missing PERF_RECORD_COMM for %s!\n", cmd);
1272 if (!found_cmd_mmap) {
1273 pr_debug("PERF_RECORD_MMAP for %s missing!\n", cmd);
1277 if (!found_libc_mmap) {
1278 pr_debug("PERF_RECORD_MMAP for %s missing!\n", "libc");
1282 if (!found_ld_mmap) {
1283 pr_debug("PERF_RECORD_MMAP for %s missing!\n", "ld");
1287 if (!found_vdso_mmap) {
1288 pr_debug("PERF_RECORD_MMAP for %s missing!\n", "[vdso]");
1292 perf_evlist__munmap(evlist);
1296 perf_evlist__delete(evlist);
1298 return (err < 0 || errs > 0) ? -1 : 0;
1302 #if defined(__x86_64__) || defined(__i386__)
1304 #define barrier() asm volatile("" ::: "memory")
1306 static u64 rdpmc(unsigned int counter)
1308 unsigned int low, high;
1310 asm volatile("rdpmc" : "=a" (low), "=d" (high) : "c" (counter));
1312 return low | ((u64)high) << 32;
1315 static u64 rdtsc(void)
1317 unsigned int low, high;
1319 asm volatile("rdtsc" : "=a" (low), "=d" (high));
1321 return low | ((u64)high) << 32;
1324 static u64 mmap_read_self(void *addr)
1326 struct perf_event_mmap_page *pc = addr;
1327 u32 seq, idx, time_mult = 0, time_shift = 0;
1328 u64 count, cyc = 0, time_offset = 0, enabled, running, delta;
1334 enabled = pc->time_enabled;
1335 running = pc->time_running;
1337 if (enabled != running) {
1339 time_mult = pc->time_mult;
1340 time_shift = pc->time_shift;
1341 time_offset = pc->time_offset;
1347 count += rdpmc(idx - 1);
1350 } while (pc->lock != seq);
1352 if (enabled != running) {
1355 quot = (cyc >> time_shift);
1356 rem = cyc & ((1 << time_shift) - 1);
1357 delta = time_offset + quot * time_mult +
1358 ((rem * time_mult) >> time_shift);
1364 quot = count / running;
1365 rem = count % running;
1366 count = quot * enabled + (rem * enabled) / running;
1373 * If the RDPMC instruction faults then signal this back to the test parent task:
1375 static void segfault_handler(int sig __used, siginfo_t *info __used, void *uc __used)
1380 static int __test__rdpmc(void)
1382 long page_size = sysconf(_SC_PAGE_SIZE);
1383 volatile int tmp = 0;
1384 u64 i, loops = 1000;
1388 struct perf_event_attr attr = {
1389 .type = PERF_TYPE_HARDWARE,
1390 .config = PERF_COUNT_HW_INSTRUCTIONS,
1391 .exclude_kernel = 1,
1394 struct sigaction sa;
1396 sigfillset(&sa.sa_mask);
1397 sa.sa_sigaction = segfault_handler;
1398 sigaction(SIGSEGV, &sa, NULL);
1400 fprintf(stderr, "\n\n");
1402 fd = sys_perf_event_open(&attr, 0, -1, -1, 0);
1404 die("Error: sys_perf_event_open() syscall returned "
1405 "with %d (%s)\n", fd, strerror(errno));
1408 addr = mmap(NULL, page_size, PROT_READ, MAP_SHARED, fd, 0);
1409 if (addr == (void *)(-1)) {
1410 die("Error: mmap() syscall returned "
1411 "with (%s)\n", strerror(errno));
1414 for (n = 0; n < 6; n++) {
1415 u64 stamp, now, delta;
1417 stamp = mmap_read_self(addr);
1419 for (i = 0; i < loops; i++)
1422 now = mmap_read_self(addr);
1425 delta = now - stamp;
1426 fprintf(stderr, "%14d: %14Lu\n", n, (long long)delta);
1431 munmap(addr, page_size);
1434 fprintf(stderr, " ");
1442 static int test__rdpmc(void)
1454 ret = __test__rdpmc();
1459 wret = waitpid(pid, &status, 0);
1460 if (wret < 0 || status)
1468 static struct test {
1473 .desc = "vmlinux symtab matches kallsyms",
1474 .func = test__vmlinux_matches_kallsyms,
1477 .desc = "detect open syscall event",
1478 .func = test__open_syscall_event,
1481 .desc = "detect open syscall event on all cpus",
1482 .func = test__open_syscall_event_on_all_cpus,
1485 .desc = "read samples using the mmap interface",
1486 .func = test__basic_mmap,
1489 .desc = "parse events tests",
1490 .func = test__parse_events,
1492 #if defined(__x86_64__) || defined(__i386__)
1494 .desc = "x86 rdpmc test",
1495 .func = test__rdpmc,
1499 .desc = "Validate PERF_RECORD_* events & perf_sample fields",
1500 .func = test__PERF_RECORD,
1507 static bool perf_test__matches(int curr, int argc, const char *argv[])
1514 for (i = 0; i < argc; ++i) {
1516 long nr = strtoul(argv[i], &end, 10);
1524 if (strstr(tests[curr].desc, argv[i]))
1531 static int __cmd_test(int argc, const char *argv[])
1535 while (tests[i].func) {
1536 int curr = i++, err;
1538 if (!perf_test__matches(curr, argc, argv))
1541 pr_info("%2d: %s:", i, tests[curr].desc);
1542 pr_debug("\n--- start ---\n");
1543 err = tests[curr].func();
1544 pr_debug("---- end ----\n%s:", tests[curr].desc);
1545 pr_info(" %s\n", err ? "FAILED!\n" : "Ok");
1551 static int perf_test__list(int argc, const char **argv)
1555 while (tests[i].func) {
1558 if (argc > 1 && !strstr(tests[curr].desc, argv[1]))
1561 pr_info("%2d: %s\n", i, tests[curr].desc);
1567 int cmd_test(int argc, const char **argv, const char *prefix __used)
1569 const char * const test_usage[] = {
1570 "perf test [<options>] [{list <test-name-fragment>|[<test-name-fragments>|<test-numbers>]}]",
1573 const struct option test_options[] = {
1574 OPT_INCR('v', "verbose", &verbose,
1575 "be more verbose (show symbol address, etc)"),
1579 argc = parse_options(argc, argv, test_options, test_usage, 0);
1580 if (argc >= 1 && !strcmp(argv[0], "list"))
1581 return perf_test__list(argc, argv);
1583 symbol_conf.priv_size = sizeof(int);
1584 symbol_conf.sort_by_name = true;
1585 symbol_conf.try_vmlinux_path = true;
1587 if (symbol__init() < 0)
1590 return __cmd_test(argc, argv);