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"
18 static int vmlinux_matches_kallsyms_filter(struct map *map __used, struct symbol *sym)
20 bool *visited = symbol__priv(sym);
25 static int test__vmlinux_matches_kallsyms(void)
30 struct map *kallsyms_map, *vmlinux_map;
31 struct machine kallsyms, vmlinux;
32 enum map_type type = MAP__FUNCTION;
33 long page_size = sysconf(_SC_PAGE_SIZE);
34 struct ref_reloc_sym ref_reloc_sym = { .name = "_stext", };
39 * Init the machines that will hold kernel, modules obtained from
40 * both vmlinux + .ko files and from /proc/kallsyms split by modules.
42 machine__init(&kallsyms, "", HOST_KERNEL_ID);
43 machine__init(&vmlinux, "", HOST_KERNEL_ID);
48 * Create the kernel maps for kallsyms and the DSO where we will then
49 * load /proc/kallsyms. Also create the modules maps from /proc/modules
50 * and find the .ko files that match them in /lib/modules/`uname -r`/.
52 if (machine__create_kernel_maps(&kallsyms) < 0) {
53 pr_debug("machine__create_kernel_maps ");
60 * Load and split /proc/kallsyms into multiple maps, one per module.
62 if (machine__load_kallsyms(&kallsyms, "/proc/kallsyms", type, NULL) <= 0) {
63 pr_debug("dso__load_kallsyms ");
70 * kallsyms will be internally on demand sorted by name so that we can
71 * find the reference relocation * symbol, i.e. the symbol we will use
72 * to see if the running kernel was relocated by checking if it has the
73 * same value in the vmlinux file we load.
75 kallsyms_map = machine__kernel_map(&kallsyms, type);
77 sym = map__find_symbol_by_name(kallsyms_map, ref_reloc_sym.name, NULL);
79 pr_debug("dso__find_symbol_by_name ");
83 ref_reloc_sym.addr = sym->start;
88 * Now repeat step 2, this time for the vmlinux file we'll auto-locate.
90 if (machine__create_kernel_maps(&vmlinux) < 0) {
91 pr_debug("machine__create_kernel_maps ");
95 vmlinux_map = machine__kernel_map(&vmlinux, type);
96 map__kmap(vmlinux_map)->ref_reloc_sym = &ref_reloc_sym;
101 * Locate a vmlinux file in the vmlinux path that has a buildid that
102 * matches the one of the running kernel.
104 * While doing that look if we find the ref reloc symbol, if we find it
105 * we'll have its ref_reloc_symbol.unrelocated_addr and then
106 * maps__reloc_vmlinux will notice and set proper ->[un]map_ip routines
107 * to fixup the symbols.
109 if (machine__load_vmlinux_path(&vmlinux, type,
110 vmlinux_matches_kallsyms_filter) <= 0) {
111 pr_debug("machine__load_vmlinux_path ");
119 * Now look at the symbols in the vmlinux DSO and check if we find all of them
120 * in the kallsyms dso. For the ones that are in both, check its names and
123 for (nd = rb_first(&vmlinux_map->dso->symbols[type]); nd; nd = rb_next(nd)) {
124 struct symbol *pair, *first_pair;
125 bool backwards = true;
127 sym = rb_entry(nd, struct symbol, rb_node);
129 if (sym->start == sym->end)
132 first_pair = machine__find_kernel_symbol(&kallsyms, type, sym->start, NULL, NULL);
135 if (pair && pair->start == sym->start) {
137 if (strcmp(sym->name, pair->name) == 0) {
139 * kallsyms don't have the symbol end, so we
140 * set that by using the next symbol start - 1,
141 * in some cases we get this up to a page
142 * wrong, trace_kmalloc when I was developing
143 * this code was one such example, 2106 bytes
144 * off the real size. More than that and we
145 * _really_ have a problem.
147 s64 skew = sym->end - pair->end;
148 if (llabs(skew) < page_size)
151 pr_debug("%#" PRIx64 ": diff end addr for %s v: %#" PRIx64 " k: %#" PRIx64 "\n",
152 sym->start, sym->name, sym->end, pair->end);
156 nnd = backwards ? rb_prev(&pair->rb_node) :
157 rb_next(&pair->rb_node);
159 struct symbol *next = rb_entry(nnd, struct symbol, rb_node);
161 if (next->start == sym->start) {
173 pr_debug("%#" PRIx64 ": diff name v: %s k: %s\n",
174 sym->start, sym->name, pair->name);
177 pr_debug("%#" PRIx64 ": %s not on kallsyms\n", sym->start, sym->name);
185 pr_info("Maps only in vmlinux:\n");
187 for (nd = rb_first(&vmlinux.kmaps.maps[type]); nd; nd = rb_next(nd)) {
188 struct map *pos = rb_entry(nd, struct map, rb_node), *pair;
190 * If it is the kernel, kallsyms is always "[kernel.kallsyms]", while
191 * the kernel will have the path for the vmlinux file being used,
192 * so use the short name, less descriptive but the same ("[kernel]" in
195 pair = map_groups__find_by_name(&kallsyms.kmaps, type,
197 pos->dso->short_name :
202 map__fprintf(pos, stderr);
205 pr_info("Maps in vmlinux with a different name in kallsyms:\n");
207 for (nd = rb_first(&vmlinux.kmaps.maps[type]); nd; nd = rb_next(nd)) {
208 struct map *pos = rb_entry(nd, struct map, rb_node), *pair;
210 pair = map_groups__find(&kallsyms.kmaps, type, pos->start);
211 if (pair == NULL || pair->priv)
214 if (pair->start == pos->start) {
216 pr_info(" %" PRIx64 "-%" PRIx64 " %" PRIx64 " %s in kallsyms as",
217 pos->start, pos->end, pos->pgoff, pos->dso->name);
218 if (pos->pgoff != pair->pgoff || pos->end != pair->end)
219 pr_info(": \n*%" PRIx64 "-%" PRIx64 " %" PRIx64 "",
220 pair->start, pair->end, pair->pgoff);
221 pr_info(" %s\n", pair->dso->name);
226 pr_info("Maps only in kallsyms:\n");
228 for (nd = rb_first(&kallsyms.kmaps.maps[type]);
229 nd; nd = rb_next(nd)) {
230 struct map *pos = rb_entry(nd, struct map, rb_node);
233 map__fprintf(pos, stderr);
239 #include "util/cpumap.h"
240 #include "util/evsel.h"
241 #include <sys/types.h>
243 static int trace_event__id(const char *evname)
248 if (asprintf(&filename,
250 tracing_events_path, evname) < 0)
253 fd = open(filename, O_RDONLY);
256 if (read(fd, id, sizeof(id)) > 0)
265 static int test__open_syscall_event(void)
268 struct thread_map *threads;
269 struct perf_evsel *evsel;
270 struct perf_event_attr attr;
271 unsigned int nr_open_calls = 111, i;
272 int id = trace_event__id("sys_enter_open");
275 pr_debug("is debugfs mounted on /sys/kernel/debug?\n");
279 threads = thread_map__new(-1, getpid());
280 if (threads == NULL) {
281 pr_debug("thread_map__new\n");
285 memset(&attr, 0, sizeof(attr));
286 attr.type = PERF_TYPE_TRACEPOINT;
288 evsel = perf_evsel__new(&attr, 0);
290 pr_debug("perf_evsel__new\n");
291 goto out_thread_map_delete;
294 if (perf_evsel__open_per_thread(evsel, threads, false, NULL) < 0) {
295 pr_debug("failed to open counter: %s, "
296 "tweak /proc/sys/kernel/perf_event_paranoid?\n",
298 goto out_evsel_delete;
301 for (i = 0; i < nr_open_calls; ++i) {
302 fd = open("/etc/passwd", O_RDONLY);
306 if (perf_evsel__read_on_cpu(evsel, 0, 0) < 0) {
307 pr_debug("perf_evsel__read_on_cpu\n");
311 if (evsel->counts->cpu[0].val != nr_open_calls) {
312 pr_debug("perf_evsel__read_on_cpu: expected to intercept %d calls, got %" PRIu64 "\n",
313 nr_open_calls, evsel->counts->cpu[0].val);
319 perf_evsel__close_fd(evsel, 1, threads->nr);
321 perf_evsel__delete(evsel);
322 out_thread_map_delete:
323 thread_map__delete(threads);
329 static int test__open_syscall_event_on_all_cpus(void)
331 int err = -1, fd, cpu;
332 struct thread_map *threads;
333 struct cpu_map *cpus;
334 struct perf_evsel *evsel;
335 struct perf_event_attr attr;
336 unsigned int nr_open_calls = 111, i;
338 int id = trace_event__id("sys_enter_open");
341 pr_debug("is debugfs mounted on /sys/kernel/debug?\n");
345 threads = thread_map__new(-1, getpid());
346 if (threads == NULL) {
347 pr_debug("thread_map__new\n");
351 cpus = cpu_map__new(NULL);
353 pr_debug("cpu_map__new\n");
354 goto out_thread_map_delete;
360 memset(&attr, 0, sizeof(attr));
361 attr.type = PERF_TYPE_TRACEPOINT;
363 evsel = perf_evsel__new(&attr, 0);
365 pr_debug("perf_evsel__new\n");
366 goto out_thread_map_delete;
369 if (perf_evsel__open(evsel, cpus, threads, false, NULL) < 0) {
370 pr_debug("failed to open counter: %s, "
371 "tweak /proc/sys/kernel/perf_event_paranoid?\n",
373 goto out_evsel_delete;
376 for (cpu = 0; cpu < cpus->nr; ++cpu) {
377 unsigned int ncalls = nr_open_calls + cpu;
379 * XXX eventually lift this restriction in a way that
380 * keeps perf building on older glibc installations
381 * without CPU_ALLOC. 1024 cpus in 2010 still seems
382 * a reasonable upper limit tho :-)
384 if (cpus->map[cpu] >= CPU_SETSIZE) {
385 pr_debug("Ignoring CPU %d\n", cpus->map[cpu]);
389 CPU_SET(cpus->map[cpu], &cpu_set);
390 if (sched_setaffinity(0, sizeof(cpu_set), &cpu_set) < 0) {
391 pr_debug("sched_setaffinity() failed on CPU %d: %s ",
396 for (i = 0; i < ncalls; ++i) {
397 fd = open("/etc/passwd", O_RDONLY);
400 CPU_CLR(cpus->map[cpu], &cpu_set);
404 * Here we need to explicitely preallocate the counts, as if
405 * we use the auto allocation it will allocate just for 1 cpu,
406 * as we start by cpu 0.
408 if (perf_evsel__alloc_counts(evsel, cpus->nr) < 0) {
409 pr_debug("perf_evsel__alloc_counts(ncpus=%d)\n", cpus->nr);
415 for (cpu = 0; cpu < cpus->nr; ++cpu) {
416 unsigned int expected;
418 if (cpus->map[cpu] >= CPU_SETSIZE)
421 if (perf_evsel__read_on_cpu(evsel, cpu, 0) < 0) {
422 pr_debug("perf_evsel__read_on_cpu\n");
427 expected = nr_open_calls + cpu;
428 if (evsel->counts->cpu[cpu].val != expected) {
429 pr_debug("perf_evsel__read_on_cpu: expected to intercept %d calls on cpu %d, got %" PRIu64 "\n",
430 expected, cpus->map[cpu], evsel->counts->cpu[cpu].val);
436 perf_evsel__close_fd(evsel, 1, threads->nr);
438 perf_evsel__delete(evsel);
439 out_thread_map_delete:
440 thread_map__delete(threads);
445 * This test will generate random numbers of calls to some getpid syscalls,
446 * then establish an mmap for a group of events that are created to monitor
449 * It will receive the events, using mmap, use its PERF_SAMPLE_ID generated
450 * sample.id field to map back to its respective perf_evsel instance.
452 * Then it checks if the number of syscalls reported as perf events by
453 * the kernel corresponds to the number of syscalls made.
455 static int test__basic_mmap(void)
458 union perf_event *event;
459 struct thread_map *threads;
460 struct cpu_map *cpus;
461 struct perf_evlist *evlist;
462 struct perf_event_attr attr = {
463 .type = PERF_TYPE_TRACEPOINT,
464 .read_format = PERF_FORMAT_ID,
465 .sample_type = PERF_SAMPLE_ID,
469 const char *syscall_names[] = { "getsid", "getppid", "getpgrp",
471 pid_t (*syscalls[])(void) = { (void *)getsid, getppid, getpgrp,
473 #define nsyscalls ARRAY_SIZE(syscall_names)
475 unsigned int nr_events[nsyscalls],
476 expected_nr_events[nsyscalls], i, j;
477 struct perf_evsel *evsels[nsyscalls], *evsel;
478 int sample_size = __perf_evsel__sample_size(attr.sample_type);
480 for (i = 0; i < nsyscalls; ++i) {
483 snprintf(name, sizeof(name), "sys_enter_%s", syscall_names[i]);
484 ids[i] = trace_event__id(name);
486 pr_debug("Is debugfs mounted on /sys/kernel/debug?\n");
490 expected_nr_events[i] = random() % 257;
493 threads = thread_map__new(-1, getpid());
494 if (threads == NULL) {
495 pr_debug("thread_map__new\n");
499 cpus = cpu_map__new(NULL);
501 pr_debug("cpu_map__new\n");
502 goto out_free_threads;
506 CPU_SET(cpus->map[0], &cpu_set);
507 sched_setaffinity(0, sizeof(cpu_set), &cpu_set);
508 if (sched_setaffinity(0, sizeof(cpu_set), &cpu_set) < 0) {
509 pr_debug("sched_setaffinity() failed on CPU %d: %s ",
510 cpus->map[0], strerror(errno));
514 evlist = perf_evlist__new(cpus, threads);
515 if (evlist == NULL) {
516 pr_debug("perf_evlist__new\n");
520 /* anonymous union fields, can't be initialized above */
521 attr.wakeup_events = 1;
522 attr.sample_period = 1;
524 for (i = 0; i < nsyscalls; ++i) {
525 attr.config = ids[i];
526 evsels[i] = perf_evsel__new(&attr, i);
527 if (evsels[i] == NULL) {
528 pr_debug("perf_evsel__new\n");
529 goto out_free_evlist;
532 perf_evlist__add(evlist, evsels[i]);
534 if (perf_evsel__open(evsels[i], cpus, threads, false, NULL) < 0) {
535 pr_debug("failed to open counter: %s, "
536 "tweak /proc/sys/kernel/perf_event_paranoid?\n",
542 if (perf_evlist__mmap(evlist, 128, true) < 0) {
543 pr_debug("failed to mmap events: %d (%s)\n", errno,
548 for (i = 0; i < nsyscalls; ++i)
549 for (j = 0; j < expected_nr_events[i]; ++j) {
550 int foo = syscalls[i]();
554 while ((event = perf_evlist__mmap_read(evlist, 0)) != NULL) {
555 struct perf_sample sample;
557 if (event->header.type != PERF_RECORD_SAMPLE) {
558 pr_debug("unexpected %s event\n",
559 perf_event__name(event->header.type));
563 err = perf_event__parse_sample(event, attr.sample_type, sample_size,
564 false, &sample, false);
566 pr_err("Can't parse sample, err = %d\n", err);
570 evsel = perf_evlist__id2evsel(evlist, sample.id);
572 pr_debug("event with id %" PRIu64
573 " doesn't map to an evsel\n", sample.id);
576 nr_events[evsel->idx]++;
579 list_for_each_entry(evsel, &evlist->entries, node) {
580 if (nr_events[evsel->idx] != expected_nr_events[evsel->idx]) {
581 pr_debug("expected %d %s events, got %d\n",
582 expected_nr_events[evsel->idx],
583 event_name(evsel), nr_events[evsel->idx]);
590 perf_evlist__munmap(evlist);
592 for (i = 0; i < nsyscalls; ++i)
593 perf_evsel__close_fd(evsels[i], 1, threads->nr);
595 perf_evlist__delete(evlist);
597 cpu_map__delete(cpus);
599 thread_map__delete(threads);
604 #define TEST_ASSERT_VAL(text, cond) \
607 pr_debug("FAILED %s:%d %s\n", __FILE__, __LINE__, text); \
612 static int test__checkevent_tracepoint(struct perf_evlist *evlist)
614 struct perf_evsel *evsel = list_entry(evlist->entries.next,
615 struct perf_evsel, node);
617 TEST_ASSERT_VAL("wrong number of entries", 1 == evlist->nr_entries);
618 TEST_ASSERT_VAL("wrong type", PERF_TYPE_TRACEPOINT == evsel->attr.type);
619 TEST_ASSERT_VAL("wrong sample_type",
620 (PERF_SAMPLE_RAW | PERF_SAMPLE_TIME | PERF_SAMPLE_CPU) ==
621 evsel->attr.sample_type);
622 TEST_ASSERT_VAL("wrong sample_period", 1 == evsel->attr.sample_period);
626 static int test__checkevent_tracepoint_multi(struct perf_evlist *evlist)
628 struct perf_evsel *evsel;
630 TEST_ASSERT_VAL("wrong number of entries", evlist->nr_entries > 1);
632 list_for_each_entry(evsel, &evlist->entries, node) {
633 TEST_ASSERT_VAL("wrong type",
634 PERF_TYPE_TRACEPOINT == evsel->attr.type);
635 TEST_ASSERT_VAL("wrong sample_type",
636 (PERF_SAMPLE_RAW | PERF_SAMPLE_TIME | PERF_SAMPLE_CPU)
637 == evsel->attr.sample_type);
638 TEST_ASSERT_VAL("wrong sample_period",
639 1 == evsel->attr.sample_period);
644 static int test__checkevent_raw(struct perf_evlist *evlist)
646 struct perf_evsel *evsel = list_entry(evlist->entries.next,
647 struct perf_evsel, node);
649 TEST_ASSERT_VAL("wrong number of entries", 1 == evlist->nr_entries);
650 TEST_ASSERT_VAL("wrong type", PERF_TYPE_RAW == evsel->attr.type);
651 TEST_ASSERT_VAL("wrong config", 1 == evsel->attr.config);
655 static int test__checkevent_numeric(struct perf_evlist *evlist)
657 struct perf_evsel *evsel = list_entry(evlist->entries.next,
658 struct perf_evsel, node);
660 TEST_ASSERT_VAL("wrong number of entries", 1 == evlist->nr_entries);
661 TEST_ASSERT_VAL("wrong type", 1 == evsel->attr.type);
662 TEST_ASSERT_VAL("wrong config", 1 == evsel->attr.config);
666 static int test__checkevent_symbolic_name(struct perf_evlist *evlist)
668 struct perf_evsel *evsel = list_entry(evlist->entries.next,
669 struct perf_evsel, node);
671 TEST_ASSERT_VAL("wrong number of entries", 1 == evlist->nr_entries);
672 TEST_ASSERT_VAL("wrong type", PERF_TYPE_HARDWARE == evsel->attr.type);
673 TEST_ASSERT_VAL("wrong config",
674 PERF_COUNT_HW_INSTRUCTIONS == evsel->attr.config);
678 static int test__checkevent_symbolic_alias(struct perf_evlist *evlist)
680 struct perf_evsel *evsel = list_entry(evlist->entries.next,
681 struct perf_evsel, node);
683 TEST_ASSERT_VAL("wrong number of entries", 1 == evlist->nr_entries);
684 TEST_ASSERT_VAL("wrong type", PERF_TYPE_SOFTWARE == evsel->attr.type);
685 TEST_ASSERT_VAL("wrong config",
686 PERF_COUNT_SW_PAGE_FAULTS == evsel->attr.config);
690 static int test__checkevent_genhw(struct perf_evlist *evlist)
692 struct perf_evsel *evsel = list_entry(evlist->entries.next,
693 struct perf_evsel, node);
695 TEST_ASSERT_VAL("wrong number of entries", 1 == evlist->nr_entries);
696 TEST_ASSERT_VAL("wrong type", PERF_TYPE_HW_CACHE == evsel->attr.type);
697 TEST_ASSERT_VAL("wrong config", (1 << 16) == evsel->attr.config);
701 static int test__checkevent_breakpoint(struct perf_evlist *evlist)
703 struct perf_evsel *evsel = list_entry(evlist->entries.next,
704 struct perf_evsel, node);
706 TEST_ASSERT_VAL("wrong number of entries", 1 == evlist->nr_entries);
707 TEST_ASSERT_VAL("wrong type", PERF_TYPE_BREAKPOINT == evsel->attr.type);
708 TEST_ASSERT_VAL("wrong config", 0 == evsel->attr.config);
709 TEST_ASSERT_VAL("wrong bp_type", (HW_BREAKPOINT_R | HW_BREAKPOINT_W) ==
710 evsel->attr.bp_type);
711 TEST_ASSERT_VAL("wrong bp_len", HW_BREAKPOINT_LEN_4 ==
716 static int test__checkevent_breakpoint_x(struct perf_evlist *evlist)
718 struct perf_evsel *evsel = list_entry(evlist->entries.next,
719 struct perf_evsel, node);
721 TEST_ASSERT_VAL("wrong number of entries", 1 == evlist->nr_entries);
722 TEST_ASSERT_VAL("wrong type", PERF_TYPE_BREAKPOINT == evsel->attr.type);
723 TEST_ASSERT_VAL("wrong config", 0 == evsel->attr.config);
724 TEST_ASSERT_VAL("wrong bp_type",
725 HW_BREAKPOINT_X == evsel->attr.bp_type);
726 TEST_ASSERT_VAL("wrong bp_len", sizeof(long) == evsel->attr.bp_len);
730 static int test__checkevent_breakpoint_r(struct perf_evlist *evlist)
732 struct perf_evsel *evsel = list_entry(evlist->entries.next,
733 struct perf_evsel, node);
735 TEST_ASSERT_VAL("wrong number of entries", 1 == evlist->nr_entries);
736 TEST_ASSERT_VAL("wrong type",
737 PERF_TYPE_BREAKPOINT == evsel->attr.type);
738 TEST_ASSERT_VAL("wrong config", 0 == evsel->attr.config);
739 TEST_ASSERT_VAL("wrong bp_type",
740 HW_BREAKPOINT_R == evsel->attr.bp_type);
741 TEST_ASSERT_VAL("wrong bp_len",
742 HW_BREAKPOINT_LEN_4 == evsel->attr.bp_len);
746 static int test__checkevent_breakpoint_w(struct perf_evlist *evlist)
748 struct perf_evsel *evsel = list_entry(evlist->entries.next,
749 struct perf_evsel, node);
751 TEST_ASSERT_VAL("wrong number of entries", 1 == evlist->nr_entries);
752 TEST_ASSERT_VAL("wrong type",
753 PERF_TYPE_BREAKPOINT == evsel->attr.type);
754 TEST_ASSERT_VAL("wrong config", 0 == evsel->attr.config);
755 TEST_ASSERT_VAL("wrong bp_type",
756 HW_BREAKPOINT_W == evsel->attr.bp_type);
757 TEST_ASSERT_VAL("wrong bp_len",
758 HW_BREAKPOINT_LEN_4 == evsel->attr.bp_len);
762 static struct test__event_st {
765 int (*check)(struct perf_evlist *evlist);
768 .name = "syscalls:sys_enter_open",
769 .check = test__checkevent_tracepoint,
772 .name = "syscalls:*",
773 .check = test__checkevent_tracepoint_multi,
777 .check = test__checkevent_raw,
781 .check = test__checkevent_numeric,
784 .name = "instructions",
785 .check = test__checkevent_symbolic_name,
789 .check = test__checkevent_symbolic_alias,
792 .name = "L1-dcache-load-miss",
793 .check = test__checkevent_genhw,
797 .check = test__checkevent_breakpoint,
801 .check = test__checkevent_breakpoint_x,
805 .check = test__checkevent_breakpoint_r,
809 .check = test__checkevent_breakpoint_w,
813 #define TEST__EVENTS_CNT (sizeof(test__events) / sizeof(struct test__event_st))
815 static int test__parse_events(void)
817 struct perf_evlist *evlist;
821 for (i = 0; i < TEST__EVENTS_CNT; i++) {
822 struct test__event_st *e = &test__events[i];
824 evlist = perf_evlist__new(NULL, NULL);
828 ret = parse_events(evlist, e->name, 0);
830 pr_debug("failed to parse event '%s', err %d\n",
835 ret = e->check(evlist);
839 perf_evlist__delete(evlist);
845 static int sched__get_first_possible_cpu(pid_t pid, cpu_set_t **maskp,
850 int i, cpu = -1, nrcpus = 1024;
852 mask = CPU_ALLOC(nrcpus);
853 size = CPU_ALLOC_SIZE(nrcpus);
854 CPU_ZERO_S(size, mask);
856 if (sched_getaffinity(pid, size, mask) == -1) {
858 if (errno == EINVAL && nrcpus < (1024 << 8)) {
859 nrcpus = nrcpus << 2;
862 perror("sched_getaffinity");
866 for (i = 0; i < nrcpus; i++) {
867 if (CPU_ISSET_S(i, size, mask)) {
873 CPU_CLR_S(i, size, mask);
883 static int test__PERF_RECORD(void)
885 struct perf_record_opts opts = {
891 .sample_id_all_avail = true,
893 cpu_set_t *cpu_mask = NULL;
894 size_t cpu_mask_size = 0;
895 struct perf_evlist *evlist = perf_evlist__new(NULL, NULL);
896 struct perf_evsel *evsel;
897 struct perf_sample sample;
898 const char *cmd = "sleep";
899 const char *argv[] = { cmd, "1", NULL, };
901 u64 sample_type, prev_time = 0;
902 bool found_cmd_mmap = false,
903 found_libc_mmap = false,
904 found_vdso_mmap = false,
905 found_ld_mmap = false;
906 int err = -1, errs = 0, i, wakeups = 0, sample_size;
908 int total_events = 0, nr_events[PERF_RECORD_MAX] = { 0, };
910 if (evlist == NULL || argv == NULL) {
911 pr_debug("Not enough memory to create evlist\n");
916 * We need at least one evsel in the evlist, use the default
919 err = perf_evlist__add_default(evlist);
921 pr_debug("Not enough memory to create evsel\n");
922 goto out_delete_evlist;
926 * Create maps of threads and cpus to monitor. In this case
927 * we start with all threads and cpus (-1, -1) but then in
928 * perf_evlist__prepare_workload we'll fill in the only thread
929 * we're monitoring, the one forked there.
931 err = perf_evlist__create_maps(evlist, opts.target_pid,
932 opts.target_tid, opts.cpu_list);
934 pr_debug("Not enough memory to create thread/cpu maps\n");
935 goto out_delete_evlist;
939 * Prepare the workload in argv[] to run, it'll fork it, and then wait
940 * for perf_evlist__start_workload() to exec it. This is done this way
941 * so that we have time to open the evlist (calling sys_perf_event_open
942 * on all the fds) and then mmap them.
944 err = perf_evlist__prepare_workload(evlist, &opts, argv);
946 pr_debug("Couldn't run the workload!\n");
947 goto out_delete_evlist;
951 * Config the evsels, setting attr->comm on the first one, etc.
953 evsel = list_entry(evlist->entries.next, struct perf_evsel, node);
954 evsel->attr.sample_type |= PERF_SAMPLE_CPU;
955 evsel->attr.sample_type |= PERF_SAMPLE_TID;
956 evsel->attr.sample_type |= PERF_SAMPLE_TIME;
957 perf_evlist__config_attrs(evlist, &opts);
959 err = sched__get_first_possible_cpu(evlist->workload.pid, &cpu_mask,
962 pr_debug("sched__get_first_possible_cpu: %s\n", strerror(errno));
963 goto out_delete_evlist;
969 * So that we can check perf_sample.cpu on all the samples.
971 if (sched_setaffinity(evlist->workload.pid, cpu_mask_size, cpu_mask) < 0) {
972 pr_debug("sched_setaffinity: %s\n", strerror(errno));
973 goto out_free_cpu_mask;
977 * Call sys_perf_event_open on all the fds on all the evsels,
978 * grouping them if asked to.
980 err = perf_evlist__open(evlist, opts.group);
982 pr_debug("perf_evlist__open: %s\n", strerror(errno));
983 goto out_delete_evlist;
987 * mmap the first fd on a given CPU and ask for events for the other
988 * fds in the same CPU to be injected in the same mmap ring buffer
989 * (using ioctl(PERF_EVENT_IOC_SET_OUTPUT)).
991 err = perf_evlist__mmap(evlist, opts.mmap_pages, false);
993 pr_debug("perf_evlist__mmap: %s\n", strerror(errno));
994 goto out_delete_evlist;
998 * We'll need these two to parse the PERF_SAMPLE_* fields in each
1001 sample_type = perf_evlist__sample_type(evlist);
1002 sample_size = __perf_evsel__sample_size(sample_type);
1005 * Now that all is properly set up, enable the events, they will
1006 * count just on workload.pid, which will start...
1008 perf_evlist__enable(evlist);
1013 perf_evlist__start_workload(evlist);
1016 int before = total_events;
1018 for (i = 0; i < evlist->nr_mmaps; i++) {
1019 union perf_event *event;
1021 while ((event = perf_evlist__mmap_read(evlist, i)) != NULL) {
1022 const u32 type = event->header.type;
1023 const char *name = perf_event__name(type);
1026 if (type < PERF_RECORD_MAX)
1029 err = perf_event__parse_sample(event, sample_type,
1034 perf_event__fprintf(event, stderr);
1035 pr_debug("Couldn't parse sample\n");
1040 pr_info("%" PRIu64" %d ", sample.time, sample.cpu);
1041 perf_event__fprintf(event, stderr);
1044 if (prev_time > sample.time) {
1045 pr_debug("%s going backwards in time, prev=%" PRIu64 ", curr=%" PRIu64 "\n",
1046 name, prev_time, sample.time);
1050 prev_time = sample.time;
1052 if (sample.cpu != cpu) {
1053 pr_debug("%s with unexpected cpu, expected %d, got %d\n",
1054 name, cpu, sample.cpu);
1058 if ((pid_t)sample.pid != evlist->workload.pid) {
1059 pr_debug("%s with unexpected pid, expected %d, got %d\n",
1060 name, evlist->workload.pid, sample.pid);
1064 if ((pid_t)sample.tid != evlist->workload.pid) {
1065 pr_debug("%s with unexpected tid, expected %d, got %d\n",
1066 name, evlist->workload.pid, sample.tid);
1070 if ((type == PERF_RECORD_COMM ||
1071 type == PERF_RECORD_MMAP ||
1072 type == PERF_RECORD_FORK ||
1073 type == PERF_RECORD_EXIT) &&
1074 (pid_t)event->comm.pid != evlist->workload.pid) {
1075 pr_debug("%s with unexpected pid/tid\n", name);
1079 if ((type == PERF_RECORD_COMM ||
1080 type == PERF_RECORD_MMAP) &&
1081 event->comm.pid != event->comm.tid) {
1082 pr_debug("%s with different pid/tid!\n", name);
1087 case PERF_RECORD_COMM:
1088 if (strcmp(event->comm.comm, cmd)) {
1089 pr_debug("%s with unexpected comm!\n", name);
1093 case PERF_RECORD_EXIT:
1095 case PERF_RECORD_MMAP:
1096 bname = strrchr(event->mmap.filename, '/');
1097 if (bname != NULL) {
1098 if (!found_cmd_mmap)
1099 found_cmd_mmap = !strcmp(bname + 1, cmd);
1100 if (!found_libc_mmap)
1101 found_libc_mmap = !strncmp(bname + 1, "libc", 4);
1103 found_ld_mmap = !strncmp(bname + 1, "ld", 2);
1104 } else if (!found_vdso_mmap)
1105 found_vdso_mmap = !strcmp(event->mmap.filename, "[vdso]");
1108 case PERF_RECORD_SAMPLE:
1109 /* Just ignore samples for now */
1112 pr_debug("Unexpected perf_event->header.type %d!\n",
1120 * We don't use poll here because at least at 3.1 times the
1121 * PERF_RECORD_{!SAMPLE} events don't honour
1122 * perf_event_attr.wakeup_events, just PERF_EVENT_SAMPLE does.
1124 if (total_events == before && false)
1125 poll(evlist->pollfd, evlist->nr_fds, -1);
1128 if (++wakeups > 5) {
1129 pr_debug("No PERF_RECORD_EXIT event!\n");
1135 if (nr_events[PERF_RECORD_COMM] > 1) {
1136 pr_debug("Excessive number of PERF_RECORD_COMM events!\n");
1140 if (nr_events[PERF_RECORD_COMM] == 0) {
1141 pr_debug("Missing PERF_RECORD_COMM for %s!\n", cmd);
1145 if (!found_cmd_mmap) {
1146 pr_debug("PERF_RECORD_MMAP for %s missing!\n", cmd);
1150 if (!found_libc_mmap) {
1151 pr_debug("PERF_RECORD_MMAP for %s missing!\n", "libc");
1155 if (!found_ld_mmap) {
1156 pr_debug("PERF_RECORD_MMAP for %s missing!\n", "ld");
1160 if (!found_vdso_mmap) {
1161 pr_debug("PERF_RECORD_MMAP for %s missing!\n", "[vdso]");
1165 perf_evlist__munmap(evlist);
1169 perf_evlist__delete(evlist);
1171 return (err < 0 || errs > 0) ? -1 : 0;
1174 static struct test {
1179 .desc = "vmlinux symtab matches kallsyms",
1180 .func = test__vmlinux_matches_kallsyms,
1183 .desc = "detect open syscall event",
1184 .func = test__open_syscall_event,
1187 .desc = "detect open syscall event on all cpus",
1188 .func = test__open_syscall_event_on_all_cpus,
1191 .desc = "read samples using the mmap interface",
1192 .func = test__basic_mmap,
1195 .desc = "parse events tests",
1196 .func = test__parse_events,
1199 .desc = "Validate PERF_RECORD_* events & perf_sample fields",
1200 .func = test__PERF_RECORD,
1207 static bool perf_test__matches(int curr, int argc, const char *argv[])
1214 for (i = 0; i < argc; ++i) {
1216 long nr = strtoul(argv[i], &end, 10);
1224 if (strstr(tests[curr].desc, argv[i]))
1231 static int __cmd_test(int argc, const char *argv[])
1235 while (tests[i].func) {
1236 int curr = i++, err;
1238 if (!perf_test__matches(curr, argc, argv))
1241 pr_info("%2d: %s:", i, tests[curr].desc);
1242 pr_debug("\n--- start ---\n");
1243 err = tests[curr].func();
1244 pr_debug("---- end ----\n%s:", tests[curr].desc);
1245 pr_info(" %s\n", err ? "FAILED!\n" : "Ok");
1251 static int perf_test__list(int argc, const char **argv)
1255 while (tests[i].func) {
1258 if (argc > 1 && !strstr(tests[curr].desc, argv[1]))
1261 pr_info("%2d: %s\n", i, tests[curr].desc);
1267 int cmd_test(int argc, const char **argv, const char *prefix __used)
1269 const char * const test_usage[] = {
1270 "perf test [<options>] [{list <test-name-fragment>|[<test-name-fragments>|<test-numbers>]}]",
1273 const struct option test_options[] = {
1274 OPT_INTEGER('v', "verbose", &verbose,
1275 "be more verbose (show symbol address, etc)"),
1279 argc = parse_options(argc, argv, test_options, test_usage, 0);
1280 if (argc >= 1 && !strcmp(argv[0], "list"))
1281 return perf_test__list(argc, argv);
1283 symbol_conf.priv_size = sizeof(int);
1284 symbol_conf.sort_by_name = true;
1285 symbol_conf.try_vmlinux_path = true;
1287 if (symbol__init() < 0)
1292 return __cmd_test(argc, argv);