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 "../../include/linux/hw_breakpoint.h"
21 static int vmlinux_matches_kallsyms_filter(struct map *map __used, struct symbol *sym)
23 bool *visited = symbol__priv(sym);
28 static int test__vmlinux_matches_kallsyms(void)
33 struct map *kallsyms_map, *vmlinux_map;
34 struct machine kallsyms, vmlinux;
35 enum map_type type = MAP__FUNCTION;
36 long page_size = sysconf(_SC_PAGE_SIZE);
37 struct ref_reloc_sym ref_reloc_sym = { .name = "_stext", };
42 * Init the machines that will hold kernel, modules obtained from
43 * both vmlinux + .ko files and from /proc/kallsyms split by modules.
45 machine__init(&kallsyms, "", HOST_KERNEL_ID);
46 machine__init(&vmlinux, "", HOST_KERNEL_ID);
51 * Create the kernel maps for kallsyms and the DSO where we will then
52 * load /proc/kallsyms. Also create the modules maps from /proc/modules
53 * and find the .ko files that match them in /lib/modules/`uname -r`/.
55 if (machine__create_kernel_maps(&kallsyms) < 0) {
56 pr_debug("machine__create_kernel_maps ");
63 * Load and split /proc/kallsyms into multiple maps, one per module.
65 if (machine__load_kallsyms(&kallsyms, "/proc/kallsyms", type, NULL) <= 0) {
66 pr_debug("dso__load_kallsyms ");
73 * kallsyms will be internally on demand sorted by name so that we can
74 * find the reference relocation * symbol, i.e. the symbol we will use
75 * to see if the running kernel was relocated by checking if it has the
76 * same value in the vmlinux file we load.
78 kallsyms_map = machine__kernel_map(&kallsyms, type);
80 sym = map__find_symbol_by_name(kallsyms_map, ref_reloc_sym.name, NULL);
82 pr_debug("dso__find_symbol_by_name ");
86 ref_reloc_sym.addr = sym->start;
91 * Now repeat step 2, this time for the vmlinux file we'll auto-locate.
93 if (machine__create_kernel_maps(&vmlinux) < 0) {
94 pr_debug("machine__create_kernel_maps ");
98 vmlinux_map = machine__kernel_map(&vmlinux, type);
99 map__kmap(vmlinux_map)->ref_reloc_sym = &ref_reloc_sym;
104 * Locate a vmlinux file in the vmlinux path that has a buildid that
105 * matches the one of the running kernel.
107 * While doing that look if we find the ref reloc symbol, if we find it
108 * we'll have its ref_reloc_symbol.unrelocated_addr and then
109 * maps__reloc_vmlinux will notice and set proper ->[un]map_ip routines
110 * to fixup the symbols.
112 if (machine__load_vmlinux_path(&vmlinux, type,
113 vmlinux_matches_kallsyms_filter) <= 0) {
114 pr_debug("machine__load_vmlinux_path ");
122 * Now look at the symbols in the vmlinux DSO and check if we find all of them
123 * in the kallsyms dso. For the ones that are in both, check its names and
126 for (nd = rb_first(&vmlinux_map->dso->symbols[type]); nd; nd = rb_next(nd)) {
127 struct symbol *pair, *first_pair;
128 bool backwards = true;
130 sym = rb_entry(nd, struct symbol, rb_node);
132 if (sym->start == sym->end)
135 first_pair = machine__find_kernel_symbol(&kallsyms, type, sym->start, NULL, NULL);
138 if (pair && pair->start == sym->start) {
140 if (strcmp(sym->name, pair->name) == 0) {
142 * kallsyms don't have the symbol end, so we
143 * set that by using the next symbol start - 1,
144 * in some cases we get this up to a page
145 * wrong, trace_kmalloc when I was developing
146 * this code was one such example, 2106 bytes
147 * off the real size. More than that and we
148 * _really_ have a problem.
150 s64 skew = sym->end - pair->end;
151 if (llabs(skew) < page_size)
154 pr_debug("%#" PRIx64 ": diff end addr for %s v: %#" PRIx64 " k: %#" PRIx64 "\n",
155 sym->start, sym->name, sym->end, pair->end);
159 nnd = backwards ? rb_prev(&pair->rb_node) :
160 rb_next(&pair->rb_node);
162 struct symbol *next = rb_entry(nnd, struct symbol, rb_node);
164 if (next->start == sym->start) {
176 pr_debug("%#" PRIx64 ": diff name v: %s k: %s\n",
177 sym->start, sym->name, pair->name);
180 pr_debug("%#" PRIx64 ": %s not on kallsyms\n", sym->start, sym->name);
188 pr_info("Maps only in vmlinux:\n");
190 for (nd = rb_first(&vmlinux.kmaps.maps[type]); nd; nd = rb_next(nd)) {
191 struct map *pos = rb_entry(nd, struct map, rb_node), *pair;
193 * If it is the kernel, kallsyms is always "[kernel.kallsyms]", while
194 * the kernel will have the path for the vmlinux file being used,
195 * so use the short name, less descriptive but the same ("[kernel]" in
198 pair = map_groups__find_by_name(&kallsyms.kmaps, type,
200 pos->dso->short_name :
205 map__fprintf(pos, stderr);
208 pr_info("Maps in vmlinux with a different name in kallsyms:\n");
210 for (nd = rb_first(&vmlinux.kmaps.maps[type]); nd; nd = rb_next(nd)) {
211 struct map *pos = rb_entry(nd, struct map, rb_node), *pair;
213 pair = map_groups__find(&kallsyms.kmaps, type, pos->start);
214 if (pair == NULL || pair->priv)
217 if (pair->start == pos->start) {
219 pr_info(" %" PRIx64 "-%" PRIx64 " %" PRIx64 " %s in kallsyms as",
220 pos->start, pos->end, pos->pgoff, pos->dso->name);
221 if (pos->pgoff != pair->pgoff || pos->end != pair->end)
222 pr_info(": \n*%" PRIx64 "-%" PRIx64 " %" PRIx64 "",
223 pair->start, pair->end, pair->pgoff);
224 pr_info(" %s\n", pair->dso->name);
229 pr_info("Maps only in kallsyms:\n");
231 for (nd = rb_first(&kallsyms.kmaps.maps[type]);
232 nd; nd = rb_next(nd)) {
233 struct map *pos = rb_entry(nd, struct map, rb_node);
236 map__fprintf(pos, stderr);
242 #include "util/cpumap.h"
243 #include "util/evsel.h"
244 #include <sys/types.h>
246 static int trace_event__id(const char *evname)
251 if (asprintf(&filename,
253 tracing_events_path, evname) < 0)
256 fd = open(filename, O_RDONLY);
259 if (read(fd, id, sizeof(id)) > 0)
268 static int test__open_syscall_event(void)
271 struct thread_map *threads;
272 struct perf_evsel *evsel;
273 struct perf_event_attr attr;
274 unsigned int nr_open_calls = 111, i;
275 int id = trace_event__id("sys_enter_open");
278 pr_debug("is debugfs mounted on /sys/kernel/debug?\n");
282 threads = thread_map__new(-1, getpid(), UINT_MAX);
283 if (threads == NULL) {
284 pr_debug("thread_map__new\n");
288 memset(&attr, 0, sizeof(attr));
289 attr.type = PERF_TYPE_TRACEPOINT;
291 evsel = perf_evsel__new(&attr, 0);
293 pr_debug("perf_evsel__new\n");
294 goto out_thread_map_delete;
297 if (perf_evsel__open_per_thread(evsel, threads, false, NULL) < 0) {
298 pr_debug("failed to open counter: %s, "
299 "tweak /proc/sys/kernel/perf_event_paranoid?\n",
301 goto out_evsel_delete;
304 for (i = 0; i < nr_open_calls; ++i) {
305 fd = open("/etc/passwd", O_RDONLY);
309 if (perf_evsel__read_on_cpu(evsel, 0, 0) < 0) {
310 pr_debug("perf_evsel__read_on_cpu\n");
314 if (evsel->counts->cpu[0].val != nr_open_calls) {
315 pr_debug("perf_evsel__read_on_cpu: expected to intercept %d calls, got %" PRIu64 "\n",
316 nr_open_calls, evsel->counts->cpu[0].val);
322 perf_evsel__close_fd(evsel, 1, threads->nr);
324 perf_evsel__delete(evsel);
325 out_thread_map_delete:
326 thread_map__delete(threads);
332 static int test__open_syscall_event_on_all_cpus(void)
334 int err = -1, fd, cpu;
335 struct thread_map *threads;
336 struct cpu_map *cpus;
337 struct perf_evsel *evsel;
338 struct perf_event_attr attr;
339 unsigned int nr_open_calls = 111, i;
341 int id = trace_event__id("sys_enter_open");
344 pr_debug("is debugfs mounted on /sys/kernel/debug?\n");
348 threads = thread_map__new(-1, getpid(), UINT_MAX);
349 if (threads == NULL) {
350 pr_debug("thread_map__new\n");
354 cpus = cpu_map__new(NULL);
356 pr_debug("cpu_map__new\n");
357 goto out_thread_map_delete;
363 memset(&attr, 0, sizeof(attr));
364 attr.type = PERF_TYPE_TRACEPOINT;
366 evsel = perf_evsel__new(&attr, 0);
368 pr_debug("perf_evsel__new\n");
369 goto out_thread_map_delete;
372 if (perf_evsel__open(evsel, cpus, threads, false, NULL) < 0) {
373 pr_debug("failed to open counter: %s, "
374 "tweak /proc/sys/kernel/perf_event_paranoid?\n",
376 goto out_evsel_delete;
379 for (cpu = 0; cpu < cpus->nr; ++cpu) {
380 unsigned int ncalls = nr_open_calls + cpu;
382 * XXX eventually lift this restriction in a way that
383 * keeps perf building on older glibc installations
384 * without CPU_ALLOC. 1024 cpus in 2010 still seems
385 * a reasonable upper limit tho :-)
387 if (cpus->map[cpu] >= CPU_SETSIZE) {
388 pr_debug("Ignoring CPU %d\n", cpus->map[cpu]);
392 CPU_SET(cpus->map[cpu], &cpu_set);
393 if (sched_setaffinity(0, sizeof(cpu_set), &cpu_set) < 0) {
394 pr_debug("sched_setaffinity() failed on CPU %d: %s ",
399 for (i = 0; i < ncalls; ++i) {
400 fd = open("/etc/passwd", O_RDONLY);
403 CPU_CLR(cpus->map[cpu], &cpu_set);
407 * Here we need to explicitely preallocate the counts, as if
408 * we use the auto allocation it will allocate just for 1 cpu,
409 * as we start by cpu 0.
411 if (perf_evsel__alloc_counts(evsel, cpus->nr) < 0) {
412 pr_debug("perf_evsel__alloc_counts(ncpus=%d)\n", cpus->nr);
418 for (cpu = 0; cpu < cpus->nr; ++cpu) {
419 unsigned int expected;
421 if (cpus->map[cpu] >= CPU_SETSIZE)
424 if (perf_evsel__read_on_cpu(evsel, cpu, 0) < 0) {
425 pr_debug("perf_evsel__read_on_cpu\n");
430 expected = nr_open_calls + cpu;
431 if (evsel->counts->cpu[cpu].val != expected) {
432 pr_debug("perf_evsel__read_on_cpu: expected to intercept %d calls on cpu %d, got %" PRIu64 "\n",
433 expected, cpus->map[cpu], evsel->counts->cpu[cpu].val);
439 perf_evsel__close_fd(evsel, 1, threads->nr);
441 perf_evsel__delete(evsel);
442 out_thread_map_delete:
443 thread_map__delete(threads);
448 * This test will generate random numbers of calls to some getpid syscalls,
449 * then establish an mmap for a group of events that are created to monitor
452 * It will receive the events, using mmap, use its PERF_SAMPLE_ID generated
453 * sample.id field to map back to its respective perf_evsel instance.
455 * Then it checks if the number of syscalls reported as perf events by
456 * the kernel corresponds to the number of syscalls made.
458 static int test__basic_mmap(void)
461 union perf_event *event;
462 struct thread_map *threads;
463 struct cpu_map *cpus;
464 struct perf_evlist *evlist;
465 struct perf_event_attr attr = {
466 .type = PERF_TYPE_TRACEPOINT,
467 .read_format = PERF_FORMAT_ID,
468 .sample_type = PERF_SAMPLE_ID,
472 const char *syscall_names[] = { "getsid", "getppid", "getpgrp",
474 pid_t (*syscalls[])(void) = { (void *)getsid, getppid, getpgrp,
476 #define nsyscalls ARRAY_SIZE(syscall_names)
478 unsigned int nr_events[nsyscalls],
479 expected_nr_events[nsyscalls], i, j;
480 struct perf_evsel *evsels[nsyscalls], *evsel;
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_evlist__parse_sample(evlist, event, &sample, false);
567 pr_err("Can't parse sample, err = %d\n", err);
571 evsel = perf_evlist__id2evsel(evlist, sample.id);
573 pr_debug("event with id %" PRIu64
574 " doesn't map to an evsel\n", sample.id);
577 nr_events[evsel->idx]++;
580 list_for_each_entry(evsel, &evlist->entries, node) {
581 if (nr_events[evsel->idx] != expected_nr_events[evsel->idx]) {
582 pr_debug("expected %d %s events, got %d\n",
583 expected_nr_events[evsel->idx],
584 perf_evsel__name(evsel), nr_events[evsel->idx]);
591 perf_evlist__munmap(evlist);
593 for (i = 0; i < nsyscalls; ++i)
594 perf_evsel__close_fd(evsels[i], 1, threads->nr);
596 perf_evlist__delete(evlist);
598 cpu_map__delete(cpus);
600 thread_map__delete(threads);
605 static int sched__get_first_possible_cpu(pid_t pid, cpu_set_t **maskp,
610 int i, cpu = -1, nrcpus = 1024;
612 mask = CPU_ALLOC(nrcpus);
613 size = CPU_ALLOC_SIZE(nrcpus);
614 CPU_ZERO_S(size, mask);
616 if (sched_getaffinity(pid, size, mask) == -1) {
618 if (errno == EINVAL && nrcpus < (1024 << 8)) {
619 nrcpus = nrcpus << 2;
622 perror("sched_getaffinity");
626 for (i = 0; i < nrcpus; i++) {
627 if (CPU_ISSET_S(i, size, mask)) {
633 CPU_CLR_S(i, size, mask);
643 static int test__PERF_RECORD(void)
645 struct perf_record_opts opts = {
654 cpu_set_t *cpu_mask = NULL;
655 size_t cpu_mask_size = 0;
656 struct perf_evlist *evlist = perf_evlist__new(NULL, NULL);
657 struct perf_evsel *evsel;
658 struct perf_sample sample;
659 const char *cmd = "sleep";
660 const char *argv[] = { cmd, "1", NULL, };
663 bool found_cmd_mmap = false,
664 found_libc_mmap = false,
665 found_vdso_mmap = false,
666 found_ld_mmap = false;
667 int err = -1, errs = 0, i, wakeups = 0;
669 int total_events = 0, nr_events[PERF_RECORD_MAX] = { 0, };
671 if (evlist == NULL || argv == NULL) {
672 pr_debug("Not enough memory to create evlist\n");
677 * We need at least one evsel in the evlist, use the default
680 err = perf_evlist__add_default(evlist);
682 pr_debug("Not enough memory to create evsel\n");
683 goto out_delete_evlist;
687 * Create maps of threads and cpus to monitor. In this case
688 * we start with all threads and cpus (-1, -1) but then in
689 * perf_evlist__prepare_workload we'll fill in the only thread
690 * we're monitoring, the one forked there.
692 err = perf_evlist__create_maps(evlist, &opts.target);
694 pr_debug("Not enough memory to create thread/cpu maps\n");
695 goto out_delete_evlist;
699 * Prepare the workload in argv[] to run, it'll fork it, and then wait
700 * for perf_evlist__start_workload() to exec it. This is done this way
701 * so that we have time to open the evlist (calling sys_perf_event_open
702 * on all the fds) and then mmap them.
704 err = perf_evlist__prepare_workload(evlist, &opts, argv);
706 pr_debug("Couldn't run the workload!\n");
707 goto out_delete_evlist;
711 * Config the evsels, setting attr->comm on the first one, etc.
713 evsel = list_entry(evlist->entries.next, struct perf_evsel, node);
714 evsel->attr.sample_type |= PERF_SAMPLE_CPU;
715 evsel->attr.sample_type |= PERF_SAMPLE_TID;
716 evsel->attr.sample_type |= PERF_SAMPLE_TIME;
717 perf_evlist__config_attrs(evlist, &opts);
719 err = sched__get_first_possible_cpu(evlist->workload.pid, &cpu_mask,
722 pr_debug("sched__get_first_possible_cpu: %s\n", strerror(errno));
723 goto out_delete_evlist;
729 * So that we can check perf_sample.cpu on all the samples.
731 if (sched_setaffinity(evlist->workload.pid, cpu_mask_size, cpu_mask) < 0) {
732 pr_debug("sched_setaffinity: %s\n", strerror(errno));
733 goto out_free_cpu_mask;
737 * Call sys_perf_event_open on all the fds on all the evsels,
738 * grouping them if asked to.
740 err = perf_evlist__open(evlist, opts.group);
742 pr_debug("perf_evlist__open: %s\n", strerror(errno));
743 goto out_delete_evlist;
747 * mmap the first fd on a given CPU and ask for events for the other
748 * fds in the same CPU to be injected in the same mmap ring buffer
749 * (using ioctl(PERF_EVENT_IOC_SET_OUTPUT)).
751 err = perf_evlist__mmap(evlist, opts.mmap_pages, false);
753 pr_debug("perf_evlist__mmap: %s\n", strerror(errno));
754 goto out_delete_evlist;
758 * Now that all is properly set up, enable the events, they will
759 * count just on workload.pid, which will start...
761 perf_evlist__enable(evlist);
766 perf_evlist__start_workload(evlist);
769 int before = total_events;
771 for (i = 0; i < evlist->nr_mmaps; i++) {
772 union perf_event *event;
774 while ((event = perf_evlist__mmap_read(evlist, i)) != NULL) {
775 const u32 type = event->header.type;
776 const char *name = perf_event__name(type);
779 if (type < PERF_RECORD_MAX)
782 err = perf_evlist__parse_sample(evlist, event, &sample, false);
785 perf_event__fprintf(event, stderr);
786 pr_debug("Couldn't parse sample\n");
791 pr_info("%" PRIu64" %d ", sample.time, sample.cpu);
792 perf_event__fprintf(event, stderr);
795 if (prev_time > sample.time) {
796 pr_debug("%s going backwards in time, prev=%" PRIu64 ", curr=%" PRIu64 "\n",
797 name, prev_time, sample.time);
801 prev_time = sample.time;
803 if (sample.cpu != cpu) {
804 pr_debug("%s with unexpected cpu, expected %d, got %d\n",
805 name, cpu, sample.cpu);
809 if ((pid_t)sample.pid != evlist->workload.pid) {
810 pr_debug("%s with unexpected pid, expected %d, got %d\n",
811 name, evlist->workload.pid, sample.pid);
815 if ((pid_t)sample.tid != evlist->workload.pid) {
816 pr_debug("%s with unexpected tid, expected %d, got %d\n",
817 name, evlist->workload.pid, sample.tid);
821 if ((type == PERF_RECORD_COMM ||
822 type == PERF_RECORD_MMAP ||
823 type == PERF_RECORD_FORK ||
824 type == PERF_RECORD_EXIT) &&
825 (pid_t)event->comm.pid != evlist->workload.pid) {
826 pr_debug("%s with unexpected pid/tid\n", name);
830 if ((type == PERF_RECORD_COMM ||
831 type == PERF_RECORD_MMAP) &&
832 event->comm.pid != event->comm.tid) {
833 pr_debug("%s with different pid/tid!\n", name);
838 case PERF_RECORD_COMM:
839 if (strcmp(event->comm.comm, cmd)) {
840 pr_debug("%s with unexpected comm!\n", name);
844 case PERF_RECORD_EXIT:
846 case PERF_RECORD_MMAP:
847 bname = strrchr(event->mmap.filename, '/');
850 found_cmd_mmap = !strcmp(bname + 1, cmd);
851 if (!found_libc_mmap)
852 found_libc_mmap = !strncmp(bname + 1, "libc", 4);
854 found_ld_mmap = !strncmp(bname + 1, "ld", 2);
855 } else if (!found_vdso_mmap)
856 found_vdso_mmap = !strcmp(event->mmap.filename, "[vdso]");
859 case PERF_RECORD_SAMPLE:
860 /* Just ignore samples for now */
863 pr_debug("Unexpected perf_event->header.type %d!\n",
871 * We don't use poll here because at least at 3.1 times the
872 * PERF_RECORD_{!SAMPLE} events don't honour
873 * perf_event_attr.wakeup_events, just PERF_EVENT_SAMPLE does.
875 if (total_events == before && false)
876 poll(evlist->pollfd, evlist->nr_fds, -1);
880 pr_debug("No PERF_RECORD_EXIT event!\n");
886 if (nr_events[PERF_RECORD_COMM] > 1) {
887 pr_debug("Excessive number of PERF_RECORD_COMM events!\n");
891 if (nr_events[PERF_RECORD_COMM] == 0) {
892 pr_debug("Missing PERF_RECORD_COMM for %s!\n", cmd);
896 if (!found_cmd_mmap) {
897 pr_debug("PERF_RECORD_MMAP for %s missing!\n", cmd);
901 if (!found_libc_mmap) {
902 pr_debug("PERF_RECORD_MMAP for %s missing!\n", "libc");
906 if (!found_ld_mmap) {
907 pr_debug("PERF_RECORD_MMAP for %s missing!\n", "ld");
911 if (!found_vdso_mmap) {
912 pr_debug("PERF_RECORD_MMAP for %s missing!\n", "[vdso]");
916 perf_evlist__munmap(evlist);
920 perf_evlist__delete(evlist);
922 return (err < 0 || errs > 0) ? -1 : 0;
926 #if defined(__x86_64__) || defined(__i386__)
928 #define barrier() asm volatile("" ::: "memory")
930 static u64 rdpmc(unsigned int counter)
932 unsigned int low, high;
934 asm volatile("rdpmc" : "=a" (low), "=d" (high) : "c" (counter));
936 return low | ((u64)high) << 32;
939 static u64 rdtsc(void)
941 unsigned int low, high;
943 asm volatile("rdtsc" : "=a" (low), "=d" (high));
945 return low | ((u64)high) << 32;
948 static u64 mmap_read_self(void *addr)
950 struct perf_event_mmap_page *pc = addr;
951 u32 seq, idx, time_mult = 0, time_shift = 0;
952 u64 count, cyc = 0, time_offset = 0, enabled, running, delta;
958 enabled = pc->time_enabled;
959 running = pc->time_running;
961 if (enabled != running) {
963 time_mult = pc->time_mult;
964 time_shift = pc->time_shift;
965 time_offset = pc->time_offset;
971 count += rdpmc(idx - 1);
974 } while (pc->lock != seq);
976 if (enabled != running) {
979 quot = (cyc >> time_shift);
980 rem = cyc & ((1 << time_shift) - 1);
981 delta = time_offset + quot * time_mult +
982 ((rem * time_mult) >> time_shift);
988 quot = count / running;
989 rem = count % running;
990 count = quot * enabled + (rem * enabled) / running;
997 * If the RDPMC instruction faults then signal this back to the test parent task:
999 static void segfault_handler(int sig __used, siginfo_t *info __used, void *uc __used)
1004 static int __test__rdpmc(void)
1006 long page_size = sysconf(_SC_PAGE_SIZE);
1007 volatile int tmp = 0;
1008 u64 i, loops = 1000;
1012 struct perf_event_attr attr = {
1013 .type = PERF_TYPE_HARDWARE,
1014 .config = PERF_COUNT_HW_INSTRUCTIONS,
1015 .exclude_kernel = 1,
1018 struct sigaction sa;
1020 sigfillset(&sa.sa_mask);
1021 sa.sa_sigaction = segfault_handler;
1022 sigaction(SIGSEGV, &sa, NULL);
1024 fd = sys_perf_event_open(&attr, 0, -1, -1, 0);
1026 die("Error: sys_perf_event_open() syscall returned "
1027 "with %d (%s)\n", fd, strerror(errno));
1030 addr = mmap(NULL, page_size, PROT_READ, MAP_SHARED, fd, 0);
1031 if (addr == (void *)(-1)) {
1032 die("Error: mmap() syscall returned "
1033 "with (%s)\n", strerror(errno));
1036 for (n = 0; n < 6; n++) {
1037 u64 stamp, now, delta;
1039 stamp = mmap_read_self(addr);
1041 for (i = 0; i < loops; i++)
1044 now = mmap_read_self(addr);
1047 delta = now - stamp;
1048 pr_debug("%14d: %14Lu\n", n, (long long)delta);
1053 munmap(addr, page_size);
1064 static int test__rdpmc(void)
1076 ret = __test__rdpmc();
1081 wret = waitpid(pid, &status, 0);
1082 if (wret < 0 || status)
1090 static int test__perf_pmu(void)
1092 return perf_pmu__test();
1095 static struct test {
1100 .desc = "vmlinux symtab matches kallsyms",
1101 .func = test__vmlinux_matches_kallsyms,
1104 .desc = "detect open syscall event",
1105 .func = test__open_syscall_event,
1108 .desc = "detect open syscall event on all cpus",
1109 .func = test__open_syscall_event_on_all_cpus,
1112 .desc = "read samples using the mmap interface",
1113 .func = test__basic_mmap,
1116 .desc = "parse events tests",
1117 .func = parse_events__test,
1119 #if defined(__x86_64__) || defined(__i386__)
1121 .desc = "x86 rdpmc test",
1122 .func = test__rdpmc,
1126 .desc = "Validate PERF_RECORD_* events & perf_sample fields",
1127 .func = test__PERF_RECORD,
1130 .desc = "Test perf pmu format parsing",
1131 .func = test__perf_pmu,
1134 .desc = "Test dso data interface",
1135 .func = dso__test_data,
1142 static bool perf_test__matches(int curr, int argc, const char *argv[])
1149 for (i = 0; i < argc; ++i) {
1151 long nr = strtoul(argv[i], &end, 10);
1159 if (strstr(tests[curr].desc, argv[i]))
1166 static int __cmd_test(int argc, const char *argv[])
1170 while (tests[i].func) {
1171 int curr = i++, err;
1173 if (!perf_test__matches(curr, argc, argv))
1176 pr_info("%2d: %s:", i, tests[curr].desc);
1177 pr_debug("\n--- start ---\n");
1178 err = tests[curr].func();
1179 pr_debug("---- end ----\n%s:", tests[curr].desc);
1180 pr_info(" %s\n", err ? "FAILED!\n" : "Ok");
1186 static int perf_test__list(int argc, const char **argv)
1190 while (tests[i].func) {
1193 if (argc > 1 && !strstr(tests[curr].desc, argv[1]))
1196 pr_info("%2d: %s\n", i, tests[curr].desc);
1202 int cmd_test(int argc, const char **argv, const char *prefix __used)
1204 const char * const test_usage[] = {
1205 "perf test [<options>] [{list <test-name-fragment>|[<test-name-fragments>|<test-numbers>]}]",
1208 const struct option test_options[] = {
1209 OPT_INCR('v', "verbose", &verbose,
1210 "be more verbose (show symbol address, etc)"),
1214 argc = parse_options(argc, argv, test_options, test_usage, 0);
1215 if (argc >= 1 && !strcmp(argv[0], "list"))
1216 return perf_test__list(argc, argv);
1218 symbol_conf.priv_size = sizeof(int);
1219 symbol_conf.sort_by_name = true;
1220 symbol_conf.try_vmlinux_path = true;
1222 if (symbol__init() < 0)
1225 return __cmd_test(argc, argv);