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"
17 #include "event-parse.h"
18 #include "../../include/linux/hw_breakpoint.h"
22 static int vmlinux_matches_kallsyms_filter(struct map *map __maybe_unused,
25 bool *visited = symbol__priv(sym);
30 static int test__vmlinux_matches_kallsyms(void)
35 struct map *kallsyms_map, *vmlinux_map;
36 struct machine kallsyms, vmlinux;
37 enum map_type type = MAP__FUNCTION;
38 struct ref_reloc_sym ref_reloc_sym = { .name = "_stext", };
43 * Init the machines that will hold kernel, modules obtained from
44 * both vmlinux + .ko files and from /proc/kallsyms split by modules.
46 machine__init(&kallsyms, "", HOST_KERNEL_ID);
47 machine__init(&vmlinux, "", HOST_KERNEL_ID);
52 * Create the kernel maps for kallsyms and the DSO where we will then
53 * load /proc/kallsyms. Also create the modules maps from /proc/modules
54 * and find the .ko files that match them in /lib/modules/`uname -r`/.
56 if (machine__create_kernel_maps(&kallsyms) < 0) {
57 pr_debug("machine__create_kernel_maps ");
64 * Load and split /proc/kallsyms into multiple maps, one per module.
66 if (machine__load_kallsyms(&kallsyms, "/proc/kallsyms", type, NULL) <= 0) {
67 pr_debug("dso__load_kallsyms ");
74 * kallsyms will be internally on demand sorted by name so that we can
75 * find the reference relocation * symbol, i.e. the symbol we will use
76 * to see if the running kernel was relocated by checking if it has the
77 * same value in the vmlinux file we load.
79 kallsyms_map = machine__kernel_map(&kallsyms, type);
81 sym = map__find_symbol_by_name(kallsyms_map, ref_reloc_sym.name, NULL);
83 pr_debug("dso__find_symbol_by_name ");
87 ref_reloc_sym.addr = sym->start;
92 * Now repeat step 2, this time for the vmlinux file we'll auto-locate.
94 if (machine__create_kernel_maps(&vmlinux) < 0) {
95 pr_debug("machine__create_kernel_maps ");
99 vmlinux_map = machine__kernel_map(&vmlinux, type);
100 map__kmap(vmlinux_map)->ref_reloc_sym = &ref_reloc_sym;
105 * Locate a vmlinux file in the vmlinux path that has a buildid that
106 * matches the one of the running kernel.
108 * While doing that look if we find the ref reloc symbol, if we find it
109 * we'll have its ref_reloc_symbol.unrelocated_addr and then
110 * maps__reloc_vmlinux will notice and set proper ->[un]map_ip routines
111 * to fixup the symbols.
113 if (machine__load_vmlinux_path(&vmlinux, type,
114 vmlinux_matches_kallsyms_filter) <= 0) {
115 pr_debug("machine__load_vmlinux_path ");
123 * Now look at the symbols in the vmlinux DSO and check if we find all of them
124 * in the kallsyms dso. For the ones that are in both, check its names and
127 for (nd = rb_first(&vmlinux_map->dso->symbols[type]); nd; nd = rb_next(nd)) {
128 struct symbol *pair, *first_pair;
129 bool backwards = true;
131 sym = rb_entry(nd, struct symbol, rb_node);
133 if (sym->start == sym->end)
136 first_pair = machine__find_kernel_symbol(&kallsyms, type, sym->start, NULL, NULL);
139 if (pair && pair->start == sym->start) {
141 if (strcmp(sym->name, pair->name) == 0) {
143 * kallsyms don't have the symbol end, so we
144 * set that by using the next symbol start - 1,
145 * in some cases we get this up to a page
146 * wrong, trace_kmalloc when I was developing
147 * this code was one such example, 2106 bytes
148 * off the real size. More than that and we
149 * _really_ have a problem.
151 s64 skew = sym->end - pair->end;
152 if (llabs(skew) < page_size)
155 pr_debug("%#" PRIx64 ": diff end addr for %s v: %#" PRIx64 " k: %#" PRIx64 "\n",
156 sym->start, sym->name, sym->end, pair->end);
160 nnd = backwards ? rb_prev(&pair->rb_node) :
161 rb_next(&pair->rb_node);
163 struct symbol *next = rb_entry(nnd, struct symbol, rb_node);
165 if (next->start == sym->start) {
177 pr_debug("%#" PRIx64 ": diff name v: %s k: %s\n",
178 sym->start, sym->name, pair->name);
181 pr_debug("%#" PRIx64 ": %s not on kallsyms\n", sym->start, sym->name);
189 pr_info("Maps only in vmlinux:\n");
191 for (nd = rb_first(&vmlinux.kmaps.maps[type]); nd; nd = rb_next(nd)) {
192 struct map *pos = rb_entry(nd, struct map, rb_node), *pair;
194 * If it is the kernel, kallsyms is always "[kernel.kallsyms]", while
195 * the kernel will have the path for the vmlinux file being used,
196 * so use the short name, less descriptive but the same ("[kernel]" in
199 pair = map_groups__find_by_name(&kallsyms.kmaps, type,
201 pos->dso->short_name :
206 map__fprintf(pos, stderr);
209 pr_info("Maps in vmlinux with a different name in kallsyms:\n");
211 for (nd = rb_first(&vmlinux.kmaps.maps[type]); nd; nd = rb_next(nd)) {
212 struct map *pos = rb_entry(nd, struct map, rb_node), *pair;
214 pair = map_groups__find(&kallsyms.kmaps, type, pos->start);
215 if (pair == NULL || pair->priv)
218 if (pair->start == pos->start) {
220 pr_info(" %" PRIx64 "-%" PRIx64 " %" PRIx64 " %s in kallsyms as",
221 pos->start, pos->end, pos->pgoff, pos->dso->name);
222 if (pos->pgoff != pair->pgoff || pos->end != pair->end)
223 pr_info(": \n*%" PRIx64 "-%" PRIx64 " %" PRIx64 "",
224 pair->start, pair->end, pair->pgoff);
225 pr_info(" %s\n", pair->dso->name);
230 pr_info("Maps only in kallsyms:\n");
232 for (nd = rb_first(&kallsyms.kmaps.maps[type]);
233 nd; nd = rb_next(nd)) {
234 struct map *pos = rb_entry(nd, struct map, rb_node);
237 map__fprintf(pos, stderr);
243 #include "util/cpumap.h"
244 #include "util/evsel.h"
245 #include <sys/types.h>
247 static int trace_event__id(const char *evname)
252 if (asprintf(&filename,
254 tracing_events_path, evname) < 0)
257 fd = open(filename, O_RDONLY);
260 if (read(fd, id, sizeof(id)) > 0)
269 static int test__open_syscall_event(void)
272 struct thread_map *threads;
273 struct perf_evsel *evsel;
274 struct perf_event_attr attr;
275 unsigned int nr_open_calls = 111, i;
276 int id = trace_event__id("sys_enter_open");
279 pr_debug("is debugfs mounted on /sys/kernel/debug?\n");
283 threads = thread_map__new(-1, getpid(), UINT_MAX);
284 if (threads == NULL) {
285 pr_debug("thread_map__new\n");
289 memset(&attr, 0, sizeof(attr));
290 attr.type = PERF_TYPE_TRACEPOINT;
292 evsel = perf_evsel__new(&attr, 0);
294 pr_debug("perf_evsel__new\n");
295 goto out_thread_map_delete;
298 if (perf_evsel__open_per_thread(evsel, threads) < 0) {
299 pr_debug("failed to open counter: %s, "
300 "tweak /proc/sys/kernel/perf_event_paranoid?\n",
302 goto out_evsel_delete;
305 for (i = 0; i < nr_open_calls; ++i) {
306 fd = open("/etc/passwd", O_RDONLY);
310 if (perf_evsel__read_on_cpu(evsel, 0, 0) < 0) {
311 pr_debug("perf_evsel__read_on_cpu\n");
315 if (evsel->counts->cpu[0].val != nr_open_calls) {
316 pr_debug("perf_evsel__read_on_cpu: expected to intercept %d calls, got %" PRIu64 "\n",
317 nr_open_calls, evsel->counts->cpu[0].val);
323 perf_evsel__close_fd(evsel, 1, threads->nr);
325 perf_evsel__delete(evsel);
326 out_thread_map_delete:
327 thread_map__delete(threads);
333 static int test__open_syscall_event_on_all_cpus(void)
335 int err = -1, fd, cpu;
336 struct thread_map *threads;
337 struct cpu_map *cpus;
338 struct perf_evsel *evsel;
339 struct perf_event_attr attr;
340 unsigned int nr_open_calls = 111, i;
342 int id = trace_event__id("sys_enter_open");
345 pr_debug("is debugfs mounted on /sys/kernel/debug?\n");
349 threads = thread_map__new(-1, getpid(), UINT_MAX);
350 if (threads == NULL) {
351 pr_debug("thread_map__new\n");
355 cpus = cpu_map__new(NULL);
357 pr_debug("cpu_map__new\n");
358 goto out_thread_map_delete;
364 memset(&attr, 0, sizeof(attr));
365 attr.type = PERF_TYPE_TRACEPOINT;
367 evsel = perf_evsel__new(&attr, 0);
369 pr_debug("perf_evsel__new\n");
370 goto out_thread_map_delete;
373 if (perf_evsel__open(evsel, cpus, threads) < 0) {
374 pr_debug("failed to open counter: %s, "
375 "tweak /proc/sys/kernel/perf_event_paranoid?\n",
377 goto out_evsel_delete;
380 for (cpu = 0; cpu < cpus->nr; ++cpu) {
381 unsigned int ncalls = nr_open_calls + cpu;
383 * XXX eventually lift this restriction in a way that
384 * keeps perf building on older glibc installations
385 * without CPU_ALLOC. 1024 cpus in 2010 still seems
386 * a reasonable upper limit tho :-)
388 if (cpus->map[cpu] >= CPU_SETSIZE) {
389 pr_debug("Ignoring CPU %d\n", cpus->map[cpu]);
393 CPU_SET(cpus->map[cpu], &cpu_set);
394 if (sched_setaffinity(0, sizeof(cpu_set), &cpu_set) < 0) {
395 pr_debug("sched_setaffinity() failed on CPU %d: %s ",
400 for (i = 0; i < ncalls; ++i) {
401 fd = open("/etc/passwd", O_RDONLY);
404 CPU_CLR(cpus->map[cpu], &cpu_set);
408 * Here we need to explicitely preallocate the counts, as if
409 * we use the auto allocation it will allocate just for 1 cpu,
410 * as we start by cpu 0.
412 if (perf_evsel__alloc_counts(evsel, cpus->nr) < 0) {
413 pr_debug("perf_evsel__alloc_counts(ncpus=%d)\n", cpus->nr);
419 for (cpu = 0; cpu < cpus->nr; ++cpu) {
420 unsigned int expected;
422 if (cpus->map[cpu] >= CPU_SETSIZE)
425 if (perf_evsel__read_on_cpu(evsel, cpu, 0) < 0) {
426 pr_debug("perf_evsel__read_on_cpu\n");
431 expected = nr_open_calls + cpu;
432 if (evsel->counts->cpu[cpu].val != expected) {
433 pr_debug("perf_evsel__read_on_cpu: expected to intercept %d calls on cpu %d, got %" PRIu64 "\n",
434 expected, cpus->map[cpu], evsel->counts->cpu[cpu].val);
440 perf_evsel__close_fd(evsel, 1, threads->nr);
442 perf_evsel__delete(evsel);
443 out_thread_map_delete:
444 thread_map__delete(threads);
449 * This test will generate random numbers of calls to some getpid syscalls,
450 * then establish an mmap for a group of events that are created to monitor
453 * It will receive the events, using mmap, use its PERF_SAMPLE_ID generated
454 * sample.id field to map back to its respective perf_evsel instance.
456 * Then it checks if the number of syscalls reported as perf events by
457 * the kernel corresponds to the number of syscalls made.
459 static int test__basic_mmap(void)
462 union perf_event *event;
463 struct thread_map *threads;
464 struct cpu_map *cpus;
465 struct perf_evlist *evlist;
466 struct perf_event_attr attr = {
467 .type = PERF_TYPE_TRACEPOINT,
468 .read_format = PERF_FORMAT_ID,
469 .sample_type = PERF_SAMPLE_ID,
473 const char *syscall_names[] = { "getsid", "getppid", "getpgrp",
475 pid_t (*syscalls[])(void) = { (void *)getsid, getppid, getpgrp,
477 #define nsyscalls ARRAY_SIZE(syscall_names)
479 unsigned int nr_events[nsyscalls],
480 expected_nr_events[nsyscalls], i, j;
481 struct perf_evsel *evsels[nsyscalls], *evsel;
483 for (i = 0; i < nsyscalls; ++i) {
486 snprintf(name, sizeof(name), "sys_enter_%s", syscall_names[i]);
487 ids[i] = trace_event__id(name);
489 pr_debug("Is debugfs mounted on /sys/kernel/debug?\n");
493 expected_nr_events[i] = random() % 257;
496 threads = thread_map__new(-1, getpid(), UINT_MAX);
497 if (threads == NULL) {
498 pr_debug("thread_map__new\n");
502 cpus = cpu_map__new(NULL);
504 pr_debug("cpu_map__new\n");
505 goto out_free_threads;
509 CPU_SET(cpus->map[0], &cpu_set);
510 sched_setaffinity(0, sizeof(cpu_set), &cpu_set);
511 if (sched_setaffinity(0, sizeof(cpu_set), &cpu_set) < 0) {
512 pr_debug("sched_setaffinity() failed on CPU %d: %s ",
513 cpus->map[0], strerror(errno));
517 evlist = perf_evlist__new(cpus, threads);
518 if (evlist == NULL) {
519 pr_debug("perf_evlist__new\n");
523 /* anonymous union fields, can't be initialized above */
524 attr.wakeup_events = 1;
525 attr.sample_period = 1;
527 for (i = 0; i < nsyscalls; ++i) {
528 attr.config = ids[i];
529 evsels[i] = perf_evsel__new(&attr, i);
530 if (evsels[i] == NULL) {
531 pr_debug("perf_evsel__new\n");
532 goto out_free_evlist;
535 perf_evlist__add(evlist, evsels[i]);
537 if (perf_evsel__open(evsels[i], cpus, threads) < 0) {
538 pr_debug("failed to open counter: %s, "
539 "tweak /proc/sys/kernel/perf_event_paranoid?\n",
545 if (perf_evlist__mmap(evlist, 128, true) < 0) {
546 pr_debug("failed to mmap events: %d (%s)\n", errno,
551 for (i = 0; i < nsyscalls; ++i)
552 for (j = 0; j < expected_nr_events[i]; ++j) {
553 int foo = syscalls[i]();
557 while ((event = perf_evlist__mmap_read(evlist, 0)) != NULL) {
558 struct perf_sample sample;
560 if (event->header.type != PERF_RECORD_SAMPLE) {
561 pr_debug("unexpected %s event\n",
562 perf_event__name(event->header.type));
566 err = perf_evlist__parse_sample(evlist, event, &sample);
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 perf_evsel__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 static int sched__get_first_possible_cpu(pid_t pid, cpu_set_t **maskp,
611 int i, cpu = -1, nrcpus = 1024;
613 mask = CPU_ALLOC(nrcpus);
614 size = CPU_ALLOC_SIZE(nrcpus);
615 CPU_ZERO_S(size, mask);
617 if (sched_getaffinity(pid, size, mask) == -1) {
619 if (errno == EINVAL && nrcpus < (1024 << 8)) {
620 nrcpus = nrcpus << 2;
623 perror("sched_getaffinity");
627 for (i = 0; i < nrcpus; i++) {
628 if (CPU_ISSET_S(i, size, mask)) {
634 CPU_CLR_S(i, size, mask);
644 static int test__PERF_RECORD(void)
646 struct perf_record_opts opts = {
655 cpu_set_t *cpu_mask = NULL;
656 size_t cpu_mask_size = 0;
657 struct perf_evlist *evlist = perf_evlist__new(NULL, NULL);
658 struct perf_evsel *evsel;
659 struct perf_sample sample;
660 const char *cmd = "sleep";
661 const char *argv[] = { cmd, "1", NULL, };
664 bool found_cmd_mmap = false,
665 found_libc_mmap = false,
666 found_vdso_mmap = false,
667 found_ld_mmap = false;
668 int err = -1, errs = 0, i, wakeups = 0;
670 int total_events = 0, nr_events[PERF_RECORD_MAX] = { 0, };
672 if (evlist == NULL || argv == NULL) {
673 pr_debug("Not enough memory to create evlist\n");
678 * We need at least one evsel in the evlist, use the default
681 err = perf_evlist__add_default(evlist);
683 pr_debug("Not enough memory to create evsel\n");
684 goto out_delete_evlist;
688 * Create maps of threads and cpus to monitor. In this case
689 * we start with all threads and cpus (-1, -1) but then in
690 * perf_evlist__prepare_workload we'll fill in the only thread
691 * we're monitoring, the one forked there.
693 err = perf_evlist__create_maps(evlist, &opts.target);
695 pr_debug("Not enough memory to create thread/cpu maps\n");
696 goto out_delete_evlist;
700 * Prepare the workload in argv[] to run, it'll fork it, and then wait
701 * for perf_evlist__start_workload() to exec it. This is done this way
702 * so that we have time to open the evlist (calling sys_perf_event_open
703 * on all the fds) and then mmap them.
705 err = perf_evlist__prepare_workload(evlist, &opts, argv);
707 pr_debug("Couldn't run the workload!\n");
708 goto out_delete_evlist;
712 * Config the evsels, setting attr->comm on the first one, etc.
714 evsel = perf_evlist__first(evlist);
715 evsel->attr.sample_type |= PERF_SAMPLE_CPU;
716 evsel->attr.sample_type |= PERF_SAMPLE_TID;
717 evsel->attr.sample_type |= PERF_SAMPLE_TIME;
718 perf_evlist__config_attrs(evlist, &opts);
720 err = sched__get_first_possible_cpu(evlist->workload.pid, &cpu_mask,
723 pr_debug("sched__get_first_possible_cpu: %s\n", strerror(errno));
724 goto out_delete_evlist;
730 * So that we can check perf_sample.cpu on all the samples.
732 if (sched_setaffinity(evlist->workload.pid, cpu_mask_size, cpu_mask) < 0) {
733 pr_debug("sched_setaffinity: %s\n", strerror(errno));
734 goto out_free_cpu_mask;
738 * Call sys_perf_event_open on all the fds on all the evsels,
739 * grouping them if asked to.
741 err = perf_evlist__open(evlist);
743 pr_debug("perf_evlist__open: %s\n", strerror(errno));
744 goto out_delete_evlist;
748 * mmap the first fd on a given CPU and ask for events for the other
749 * fds in the same CPU to be injected in the same mmap ring buffer
750 * (using ioctl(PERF_EVENT_IOC_SET_OUTPUT)).
752 err = perf_evlist__mmap(evlist, opts.mmap_pages, false);
754 pr_debug("perf_evlist__mmap: %s\n", strerror(errno));
755 goto out_delete_evlist;
759 * Now that all is properly set up, enable the events, they will
760 * count just on workload.pid, which will start...
762 perf_evlist__enable(evlist);
767 perf_evlist__start_workload(evlist);
770 int before = total_events;
772 for (i = 0; i < evlist->nr_mmaps; i++) {
773 union perf_event *event;
775 while ((event = perf_evlist__mmap_read(evlist, i)) != NULL) {
776 const u32 type = event->header.type;
777 const char *name = perf_event__name(type);
780 if (type < PERF_RECORD_MAX)
783 err = perf_evlist__parse_sample(evlist, event, &sample);
786 perf_event__fprintf(event, stderr);
787 pr_debug("Couldn't parse sample\n");
792 pr_info("%" PRIu64" %d ", sample.time, sample.cpu);
793 perf_event__fprintf(event, stderr);
796 if (prev_time > sample.time) {
797 pr_debug("%s going backwards in time, prev=%" PRIu64 ", curr=%" PRIu64 "\n",
798 name, prev_time, sample.time);
802 prev_time = sample.time;
804 if (sample.cpu != cpu) {
805 pr_debug("%s with unexpected cpu, expected %d, got %d\n",
806 name, cpu, sample.cpu);
810 if ((pid_t)sample.pid != evlist->workload.pid) {
811 pr_debug("%s with unexpected pid, expected %d, got %d\n",
812 name, evlist->workload.pid, sample.pid);
816 if ((pid_t)sample.tid != evlist->workload.pid) {
817 pr_debug("%s with unexpected tid, expected %d, got %d\n",
818 name, evlist->workload.pid, sample.tid);
822 if ((type == PERF_RECORD_COMM ||
823 type == PERF_RECORD_MMAP ||
824 type == PERF_RECORD_FORK ||
825 type == PERF_RECORD_EXIT) &&
826 (pid_t)event->comm.pid != evlist->workload.pid) {
827 pr_debug("%s with unexpected pid/tid\n", name);
831 if ((type == PERF_RECORD_COMM ||
832 type == PERF_RECORD_MMAP) &&
833 event->comm.pid != event->comm.tid) {
834 pr_debug("%s with different pid/tid!\n", name);
839 case PERF_RECORD_COMM:
840 if (strcmp(event->comm.comm, cmd)) {
841 pr_debug("%s with unexpected comm!\n", name);
845 case PERF_RECORD_EXIT:
847 case PERF_RECORD_MMAP:
848 bname = strrchr(event->mmap.filename, '/');
851 found_cmd_mmap = !strcmp(bname + 1, cmd);
852 if (!found_libc_mmap)
853 found_libc_mmap = !strncmp(bname + 1, "libc", 4);
855 found_ld_mmap = !strncmp(bname + 1, "ld", 2);
856 } else if (!found_vdso_mmap)
857 found_vdso_mmap = !strcmp(event->mmap.filename, "[vdso]");
860 case PERF_RECORD_SAMPLE:
861 /* Just ignore samples for now */
864 pr_debug("Unexpected perf_event->header.type %d!\n",
872 * We don't use poll here because at least at 3.1 times the
873 * PERF_RECORD_{!SAMPLE} events don't honour
874 * perf_event_attr.wakeup_events, just PERF_EVENT_SAMPLE does.
876 if (total_events == before && false)
877 poll(evlist->pollfd, evlist->nr_fds, -1);
881 pr_debug("No PERF_RECORD_EXIT event!\n");
887 if (nr_events[PERF_RECORD_COMM] > 1) {
888 pr_debug("Excessive number of PERF_RECORD_COMM events!\n");
892 if (nr_events[PERF_RECORD_COMM] == 0) {
893 pr_debug("Missing PERF_RECORD_COMM for %s!\n", cmd);
897 if (!found_cmd_mmap) {
898 pr_debug("PERF_RECORD_MMAP for %s missing!\n", cmd);
902 if (!found_libc_mmap) {
903 pr_debug("PERF_RECORD_MMAP for %s missing!\n", "libc");
907 if (!found_ld_mmap) {
908 pr_debug("PERF_RECORD_MMAP for %s missing!\n", "ld");
912 if (!found_vdso_mmap) {
913 pr_debug("PERF_RECORD_MMAP for %s missing!\n", "[vdso]");
917 perf_evlist__munmap(evlist);
921 perf_evlist__delete(evlist);
923 return (err < 0 || errs > 0) ? -1 : 0;
927 #if defined(__x86_64__) || defined(__i386__)
929 #define barrier() asm volatile("" ::: "memory")
931 static u64 rdpmc(unsigned int counter)
933 unsigned int low, high;
935 asm volatile("rdpmc" : "=a" (low), "=d" (high) : "c" (counter));
937 return low | ((u64)high) << 32;
940 static u64 rdtsc(void)
942 unsigned int low, high;
944 asm volatile("rdtsc" : "=a" (low), "=d" (high));
946 return low | ((u64)high) << 32;
949 static u64 mmap_read_self(void *addr)
951 struct perf_event_mmap_page *pc = addr;
952 u32 seq, idx, time_mult = 0, time_shift = 0;
953 u64 count, cyc = 0, time_offset = 0, enabled, running, delta;
959 enabled = pc->time_enabled;
960 running = pc->time_running;
962 if (enabled != running) {
964 time_mult = pc->time_mult;
965 time_shift = pc->time_shift;
966 time_offset = pc->time_offset;
972 count += rdpmc(idx - 1);
975 } while (pc->lock != seq);
977 if (enabled != running) {
980 quot = (cyc >> time_shift);
981 rem = cyc & ((1 << time_shift) - 1);
982 delta = time_offset + quot * time_mult +
983 ((rem * time_mult) >> time_shift);
989 quot = count / running;
990 rem = count % running;
991 count = quot * enabled + (rem * enabled) / running;
998 * If the RDPMC instruction faults then signal this back to the test parent task:
1000 static void segfault_handler(int sig __maybe_unused,
1001 siginfo_t *info __maybe_unused,
1002 void *uc __maybe_unused)
1007 static int __test__rdpmc(void)
1009 volatile int tmp = 0;
1010 u64 i, loops = 1000;
1014 struct perf_event_attr attr = {
1015 .type = PERF_TYPE_HARDWARE,
1016 .config = PERF_COUNT_HW_INSTRUCTIONS,
1017 .exclude_kernel = 1,
1020 struct sigaction sa;
1022 sigfillset(&sa.sa_mask);
1023 sa.sa_sigaction = segfault_handler;
1024 sigaction(SIGSEGV, &sa, NULL);
1026 fd = sys_perf_event_open(&attr, 0, -1, -1, 0);
1028 pr_err("Error: sys_perf_event_open() syscall returned "
1029 "with %d (%s)\n", fd, strerror(errno));
1033 addr = mmap(NULL, page_size, PROT_READ, MAP_SHARED, fd, 0);
1034 if (addr == (void *)(-1)) {
1035 pr_err("Error: mmap() syscall returned with (%s)\n",
1040 for (n = 0; n < 6; n++) {
1041 u64 stamp, now, delta;
1043 stamp = mmap_read_self(addr);
1045 for (i = 0; i < loops; i++)
1048 now = mmap_read_self(addr);
1051 delta = now - stamp;
1052 pr_debug("%14d: %14Lu\n", n, (long long)delta);
1057 munmap(addr, page_size);
1068 static int test__rdpmc(void)
1080 ret = __test__rdpmc();
1085 wret = waitpid(pid, &status, 0);
1086 if (wret < 0 || status)
1094 static int test__perf_pmu(void)
1096 return perf_pmu__test();
1099 static int perf_evsel__roundtrip_cache_name_test(void)
1102 int type, op, err = 0, ret = 0, i, idx;
1103 struct perf_evsel *evsel;
1104 struct perf_evlist *evlist = perf_evlist__new(NULL, NULL);
1109 for (type = 0; type < PERF_COUNT_HW_CACHE_MAX; type++) {
1110 for (op = 0; op < PERF_COUNT_HW_CACHE_OP_MAX; op++) {
1111 /* skip invalid cache type */
1112 if (!perf_evsel__is_cache_op_valid(type, op))
1115 for (i = 0; i < PERF_COUNT_HW_CACHE_RESULT_MAX; i++) {
1116 __perf_evsel__hw_cache_type_op_res_name(type, op, i,
1117 name, sizeof(name));
1118 err = parse_events(evlist, name, 0);
1126 evsel = perf_evlist__first(evlist);
1128 for (type = 0; type < PERF_COUNT_HW_CACHE_MAX; type++) {
1129 for (op = 0; op < PERF_COUNT_HW_CACHE_OP_MAX; op++) {
1130 /* skip invalid cache type */
1131 if (!perf_evsel__is_cache_op_valid(type, op))
1134 for (i = 0; i < PERF_COUNT_HW_CACHE_RESULT_MAX; i++) {
1135 __perf_evsel__hw_cache_type_op_res_name(type, op, i,
1136 name, sizeof(name));
1137 if (evsel->idx != idx)
1142 if (strcmp(perf_evsel__name(evsel), name)) {
1143 pr_debug("%s != %s\n", perf_evsel__name(evsel), name);
1147 evsel = perf_evsel__next(evsel);
1152 perf_evlist__delete(evlist);
1156 static int __perf_evsel__name_array_test(const char *names[], int nr_names)
1159 struct perf_evsel *evsel;
1160 struct perf_evlist *evlist = perf_evlist__new(NULL, NULL);
1165 for (i = 0; i < nr_names; ++i) {
1166 err = parse_events(evlist, names[i], 0);
1168 pr_debug("failed to parse event '%s', err %d\n",
1170 goto out_delete_evlist;
1175 list_for_each_entry(evsel, &evlist->entries, node) {
1176 if (strcmp(perf_evsel__name(evsel), names[evsel->idx])) {
1178 pr_debug("%s != %s\n", perf_evsel__name(evsel), names[evsel->idx]);
1183 perf_evlist__delete(evlist);
1187 #define perf_evsel__name_array_test(names) \
1188 __perf_evsel__name_array_test(names, ARRAY_SIZE(names))
1190 static int perf_evsel__roundtrip_name_test(void)
1192 int err = 0, ret = 0;
1194 err = perf_evsel__name_array_test(perf_evsel__hw_names);
1198 err = perf_evsel__name_array_test(perf_evsel__sw_names);
1202 err = perf_evsel__roundtrip_cache_name_test();
1209 static int perf_evsel__test_field(struct perf_evsel *evsel, const char *name,
1210 int size, bool should_be_signed)
1212 struct format_field *field = perf_evsel__field(evsel, name);
1216 if (field == NULL) {
1217 pr_debug("%s: \"%s\" field not found!\n", evsel->name, name);
1221 is_signed = !!(field->flags | FIELD_IS_SIGNED);
1222 if (should_be_signed && !is_signed) {
1223 pr_debug("%s: \"%s\" signedness(%d) is wrong, should be %d\n",
1224 evsel->name, name, is_signed, should_be_signed);
1228 if (field->size != size) {
1229 pr_debug("%s: \"%s\" size (%d) should be %d!\n",
1230 evsel->name, name, field->size, size);
1237 static int perf_evsel__tp_sched_test(void)
1239 struct perf_evsel *evsel = perf_evsel__newtp("sched", "sched_switch", 0);
1242 if (evsel == NULL) {
1243 pr_debug("perf_evsel__new\n");
1247 if (perf_evsel__test_field(evsel, "prev_comm", 16, true))
1250 if (perf_evsel__test_field(evsel, "prev_pid", 4, true))
1253 if (perf_evsel__test_field(evsel, "prev_prio", 4, true))
1256 if (perf_evsel__test_field(evsel, "prev_state", 8, true))
1259 if (perf_evsel__test_field(evsel, "next_comm", 16, true))
1262 if (perf_evsel__test_field(evsel, "next_pid", 4, true))
1265 if (perf_evsel__test_field(evsel, "next_prio", 4, true))
1268 perf_evsel__delete(evsel);
1270 evsel = perf_evsel__newtp("sched", "sched_wakeup", 0);
1272 if (perf_evsel__test_field(evsel, "comm", 16, true))
1275 if (perf_evsel__test_field(evsel, "pid", 4, true))
1278 if (perf_evsel__test_field(evsel, "prio", 4, true))
1281 if (perf_evsel__test_field(evsel, "success", 4, true))
1284 if (perf_evsel__test_field(evsel, "target_cpu", 4, true))
1290 static int test__syscall_open_tp_fields(void)
1292 struct perf_record_opts opts = {
1300 .raw_samples = true,
1302 const char *filename = "/etc/passwd";
1303 int flags = O_RDONLY | O_DIRECTORY;
1304 struct perf_evlist *evlist = perf_evlist__new(NULL, NULL);
1305 struct perf_evsel *evsel;
1306 int err = -1, i, nr_events = 0, nr_polls = 0;
1308 if (evlist == NULL) {
1309 pr_debug("%s: perf_evlist__new\n", __func__);
1313 evsel = perf_evsel__newtp("syscalls", "sys_enter_open", 0);
1314 if (evsel == NULL) {
1315 pr_debug("%s: perf_evsel__newtp\n", __func__);
1316 goto out_delete_evlist;
1319 perf_evlist__add(evlist, evsel);
1321 err = perf_evlist__create_maps(evlist, &opts.target);
1323 pr_debug("%s: perf_evlist__create_maps\n", __func__);
1324 goto out_delete_evlist;
1327 perf_evsel__config(evsel, &opts, evsel);
1329 evlist->threads->map[0] = getpid();
1331 err = perf_evlist__open(evlist);
1333 pr_debug("perf_evlist__open: %s\n", strerror(errno));
1334 goto out_delete_evlist;
1337 err = perf_evlist__mmap(evlist, UINT_MAX, false);
1339 pr_debug("perf_evlist__mmap: %s\n", strerror(errno));
1340 goto out_delete_evlist;
1343 perf_evlist__enable(evlist);
1346 * Generate the event:
1348 open(filename, flags);
1351 int before = nr_events;
1353 for (i = 0; i < evlist->nr_mmaps; i++) {
1354 union perf_event *event;
1356 while ((event = perf_evlist__mmap_read(evlist, i)) != NULL) {
1357 const u32 type = event->header.type;
1359 struct perf_sample sample;
1363 if (type != PERF_RECORD_SAMPLE)
1366 err = perf_evsel__parse_sample(evsel, event, &sample);
1368 pr_err("Can't parse sample, err = %d\n", err);
1372 tp_flags = perf_evsel__intval(evsel, &sample, "flags");
1374 if (flags != tp_flags) {
1375 pr_debug("%s: Expected flags=%#x, got %#x\n",
1376 __func__, flags, tp_flags);
1384 if (nr_events == before)
1385 poll(evlist->pollfd, evlist->nr_fds, 10);
1387 if (++nr_polls > 5) {
1388 pr_debug("%s: no events!\n", __func__);
1395 perf_evlist__munmap(evlist);
1397 perf_evlist__delete(evlist);
1402 static struct test {
1407 .desc = "vmlinux symtab matches kallsyms",
1408 .func = test__vmlinux_matches_kallsyms,
1411 .desc = "detect open syscall event",
1412 .func = test__open_syscall_event,
1415 .desc = "detect open syscall event on all cpus",
1416 .func = test__open_syscall_event_on_all_cpus,
1419 .desc = "read samples using the mmap interface",
1420 .func = test__basic_mmap,
1423 .desc = "parse events tests",
1424 .func = parse_events__test,
1426 #if defined(__x86_64__) || defined(__i386__)
1428 .desc = "x86 rdpmc test",
1429 .func = test__rdpmc,
1433 .desc = "Validate PERF_RECORD_* events & perf_sample fields",
1434 .func = test__PERF_RECORD,
1437 .desc = "Test perf pmu format parsing",
1438 .func = test__perf_pmu,
1441 .desc = "Test dso data interface",
1442 .func = dso__test_data,
1445 .desc = "roundtrip evsel->name check",
1446 .func = perf_evsel__roundtrip_name_test,
1449 .desc = "Check parsing of sched tracepoints fields",
1450 .func = perf_evsel__tp_sched_test,
1453 .desc = "Generate and check syscalls:sys_enter_open event fields",
1454 .func = test__syscall_open_tp_fields,
1461 static bool perf_test__matches(int curr, int argc, const char *argv[])
1468 for (i = 0; i < argc; ++i) {
1470 long nr = strtoul(argv[i], &end, 10);
1478 if (strstr(tests[curr].desc, argv[i]))
1485 static int __cmd_test(int argc, const char *argv[])
1489 while (tests[i].func) {
1490 int curr = i++, err;
1492 if (!perf_test__matches(curr, argc, argv))
1495 pr_info("%2d: %s:", i, tests[curr].desc);
1496 pr_debug("\n--- start ---\n");
1497 err = tests[curr].func();
1498 pr_debug("---- end ----\n%s:", tests[curr].desc);
1499 pr_info(" %s\n", err ? "FAILED!\n" : "Ok");
1505 static int perf_test__list(int argc, const char **argv)
1509 while (tests[i].func) {
1512 if (argc > 1 && !strstr(tests[curr].desc, argv[1]))
1515 pr_info("%2d: %s\n", i, tests[curr].desc);
1521 int cmd_test(int argc, const char **argv, const char *prefix __maybe_unused)
1523 const char * const test_usage[] = {
1524 "perf test [<options>] [{list <test-name-fragment>|[<test-name-fragments>|<test-numbers>]}]",
1527 const struct option test_options[] = {
1528 OPT_INCR('v', "verbose", &verbose,
1529 "be more verbose (show symbol address, etc)"),
1533 argc = parse_options(argc, argv, test_options, test_usage, 0);
1534 if (argc >= 1 && !strcmp(argv[0], "list"))
1535 return perf_test__list(argc, argv);
1537 symbol_conf.priv_size = sizeof(int);
1538 symbol_conf.sort_by_name = true;
1539 symbol_conf.try_vmlinux_path = true;
1541 if (symbol__init() < 0)
1544 return __cmd_test(argc, argv);