]>
Commit | Line | Data |
---|---|---|
1c6a800c ACM |
1 | /* |
2 | * builtin-test.c | |
3 | * | |
4 | * Builtin regression testing command: ever growing number of sanity tests | |
5 | */ | |
6 | #include "builtin.h" | |
7 | ||
8 | #include "util/cache.h" | |
9 | #include "util/debug.h" | |
ebf294bf | 10 | #include "util/debugfs.h" |
de5fa3a8 | 11 | #include "util/evlist.h" |
1c6a800c | 12 | #include "util/parse-options.h" |
de5fa3a8 | 13 | #include "util/parse-events.h" |
1c6a800c | 14 | #include "util/symbol.h" |
fd78260b | 15 | #include "util/thread_map.h" |
cd82a32e | 16 | #include "util/pmu.h" |
13b62567 | 17 | #include "../../include/linux/hw_breakpoint.h" |
1c6a800c | 18 | |
08aa0d1f PZ |
19 | #include <sys/mman.h> |
20 | ||
1c6a800c ACM |
21 | static int vmlinux_matches_kallsyms_filter(struct map *map __used, struct symbol *sym) |
22 | { | |
23 | bool *visited = symbol__priv(sym); | |
24 | *visited = true; | |
25 | return 0; | |
26 | } | |
27 | ||
28 | static int test__vmlinux_matches_kallsyms(void) | |
29 | { | |
30 | int err = -1; | |
31 | struct rb_node *nd; | |
32 | struct symbol *sym; | |
33 | struct map *kallsyms_map, *vmlinux_map; | |
34 | struct machine kallsyms, vmlinux; | |
35 | enum map_type type = MAP__FUNCTION; | |
e60770a0 | 36 | long page_size = sysconf(_SC_PAGE_SIZE); |
1c6a800c ACM |
37 | struct ref_reloc_sym ref_reloc_sym = { .name = "_stext", }; |
38 | ||
39 | /* | |
40 | * Step 1: | |
41 | * | |
42 | * Init the machines that will hold kernel, modules obtained from | |
43 | * both vmlinux + .ko files and from /proc/kallsyms split by modules. | |
44 | */ | |
45 | machine__init(&kallsyms, "", HOST_KERNEL_ID); | |
46 | machine__init(&vmlinux, "", HOST_KERNEL_ID); | |
47 | ||
48 | /* | |
49 | * Step 2: | |
50 | * | |
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`/. | |
54 | */ | |
55 | if (machine__create_kernel_maps(&kallsyms) < 0) { | |
56 | pr_debug("machine__create_kernel_maps "); | |
57 | return -1; | |
58 | } | |
59 | ||
60 | /* | |
61 | * Step 3: | |
62 | * | |
63 | * Load and split /proc/kallsyms into multiple maps, one per module. | |
64 | */ | |
65 | if (machine__load_kallsyms(&kallsyms, "/proc/kallsyms", type, NULL) <= 0) { | |
66 | pr_debug("dso__load_kallsyms "); | |
67 | goto out; | |
68 | } | |
69 | ||
70 | /* | |
71 | * Step 4: | |
72 | * | |
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. | |
77 | */ | |
78 | kallsyms_map = machine__kernel_map(&kallsyms, type); | |
79 | ||
80 | sym = map__find_symbol_by_name(kallsyms_map, ref_reloc_sym.name, NULL); | |
81 | if (sym == NULL) { | |
82 | pr_debug("dso__find_symbol_by_name "); | |
83 | goto out; | |
84 | } | |
85 | ||
86 | ref_reloc_sym.addr = sym->start; | |
87 | ||
88 | /* | |
89 | * Step 5: | |
90 | * | |
91 | * Now repeat step 2, this time for the vmlinux file we'll auto-locate. | |
92 | */ | |
93 | if (machine__create_kernel_maps(&vmlinux) < 0) { | |
94 | pr_debug("machine__create_kernel_maps "); | |
95 | goto out; | |
96 | } | |
97 | ||
98 | vmlinux_map = machine__kernel_map(&vmlinux, type); | |
99 | map__kmap(vmlinux_map)->ref_reloc_sym = &ref_reloc_sym; | |
100 | ||
101 | /* | |
102 | * Step 6: | |
103 | * | |
104 | * Locate a vmlinux file in the vmlinux path that has a buildid that | |
105 | * matches the one of the running kernel. | |
106 | * | |
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. | |
111 | */ | |
112 | if (machine__load_vmlinux_path(&vmlinux, type, | |
113 | vmlinux_matches_kallsyms_filter) <= 0) { | |
114 | pr_debug("machine__load_vmlinux_path "); | |
115 | goto out; | |
116 | } | |
117 | ||
118 | err = 0; | |
119 | /* | |
120 | * Step 7: | |
121 | * | |
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 | |
124 | * end addresses too. | |
125 | */ | |
126 | for (nd = rb_first(&vmlinux_map->dso->symbols[type]); nd; nd = rb_next(nd)) { | |
d3678758 ACM |
127 | struct symbol *pair, *first_pair; |
128 | bool backwards = true; | |
1c6a800c ACM |
129 | |
130 | sym = rb_entry(nd, struct symbol, rb_node); | |
d3678758 ACM |
131 | |
132 | if (sym->start == sym->end) | |
133 | continue; | |
134 | ||
135 | first_pair = machine__find_kernel_symbol(&kallsyms, type, sym->start, NULL, NULL); | |
136 | pair = first_pair; | |
1c6a800c ACM |
137 | |
138 | if (pair && pair->start == sym->start) { | |
139 | next_pair: | |
140 | if (strcmp(sym->name, pair->name) == 0) { | |
141 | /* | |
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. | |
149 | */ | |
150 | s64 skew = sym->end - pair->end; | |
151 | if (llabs(skew) < page_size) | |
152 | continue; | |
153 | ||
9486aa38 | 154 | pr_debug("%#" PRIx64 ": diff end addr for %s v: %#" PRIx64 " k: %#" PRIx64 "\n", |
1c6a800c ACM |
155 | sym->start, sym->name, sym->end, pair->end); |
156 | } else { | |
d3678758 ACM |
157 | struct rb_node *nnd; |
158 | detour: | |
159 | nnd = backwards ? rb_prev(&pair->rb_node) : | |
160 | rb_next(&pair->rb_node); | |
1c6a800c ACM |
161 | if (nnd) { |
162 | struct symbol *next = rb_entry(nnd, struct symbol, rb_node); | |
163 | ||
164 | if (next->start == sym->start) { | |
165 | pair = next; | |
166 | goto next_pair; | |
167 | } | |
168 | } | |
d3678758 ACM |
169 | |
170 | if (backwards) { | |
171 | backwards = false; | |
172 | pair = first_pair; | |
173 | goto detour; | |
174 | } | |
175 | ||
9486aa38 | 176 | pr_debug("%#" PRIx64 ": diff name v: %s k: %s\n", |
1c6a800c ACM |
177 | sym->start, sym->name, pair->name); |
178 | } | |
179 | } else | |
9486aa38 | 180 | pr_debug("%#" PRIx64 ": %s not on kallsyms\n", sym->start, sym->name); |
1c6a800c ACM |
181 | |
182 | err = -1; | |
183 | } | |
184 | ||
185 | if (!verbose) | |
186 | goto out; | |
187 | ||
188 | pr_info("Maps only in vmlinux:\n"); | |
189 | ||
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; | |
192 | /* | |
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 | |
196 | * both cases. | |
197 | */ | |
198 | pair = map_groups__find_by_name(&kallsyms.kmaps, type, | |
199 | (pos->dso->kernel ? | |
200 | pos->dso->short_name : | |
201 | pos->dso->name)); | |
202 | if (pair) | |
203 | pair->priv = 1; | |
204 | else | |
205 | map__fprintf(pos, stderr); | |
206 | } | |
207 | ||
208 | pr_info("Maps in vmlinux with a different name in kallsyms:\n"); | |
209 | ||
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; | |
212 | ||
213 | pair = map_groups__find(&kallsyms.kmaps, type, pos->start); | |
214 | if (pair == NULL || pair->priv) | |
215 | continue; | |
216 | ||
217 | if (pair->start == pos->start) { | |
218 | pair->priv = 1; | |
9486aa38 | 219 | pr_info(" %" PRIx64 "-%" PRIx64 " %" PRIx64 " %s in kallsyms as", |
1c6a800c ACM |
220 | pos->start, pos->end, pos->pgoff, pos->dso->name); |
221 | if (pos->pgoff != pair->pgoff || pos->end != pair->end) | |
9486aa38 | 222 | pr_info(": \n*%" PRIx64 "-%" PRIx64 " %" PRIx64 "", |
1c6a800c ACM |
223 | pair->start, pair->end, pair->pgoff); |
224 | pr_info(" %s\n", pair->dso->name); | |
225 | pair->priv = 1; | |
226 | } | |
227 | } | |
228 | ||
229 | pr_info("Maps only in kallsyms:\n"); | |
230 | ||
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); | |
234 | ||
235 | if (!pos->priv) | |
236 | map__fprintf(pos, stderr); | |
237 | } | |
238 | out: | |
239 | return err; | |
240 | } | |
241 | ||
0252208e | 242 | #include "util/cpumap.h" |
d854861c ACM |
243 | #include "util/evsel.h" |
244 | #include <sys/types.h> | |
245 | ||
de5fa3a8 | 246 | static int trace_event__id(const char *evname) |
d854861c ACM |
247 | { |
248 | char *filename; | |
249 | int err = -1, fd; | |
250 | ||
251 | if (asprintf(&filename, | |
baf040a0 | 252 | "%s/syscalls/%s/id", |
ebf294bf | 253 | tracing_events_path, evname) < 0) |
d854861c ACM |
254 | return -1; |
255 | ||
256 | fd = open(filename, O_RDONLY); | |
257 | if (fd >= 0) { | |
258 | char id[16]; | |
259 | if (read(fd, id, sizeof(id)) > 0) | |
260 | err = atoi(id); | |
261 | close(fd); | |
262 | } | |
263 | ||
264 | free(filename); | |
265 | return err; | |
266 | } | |
267 | ||
268 | static int test__open_syscall_event(void) | |
269 | { | |
270 | int err = -1, fd; | |
271 | struct thread_map *threads; | |
272 | struct perf_evsel *evsel; | |
23a2f3ab | 273 | struct perf_event_attr attr; |
d854861c ACM |
274 | unsigned int nr_open_calls = 111, i; |
275 | int id = trace_event__id("sys_enter_open"); | |
276 | ||
277 | if (id < 0) { | |
454a3bbe | 278 | pr_debug("is debugfs mounted on /sys/kernel/debug?\n"); |
d854861c ACM |
279 | return -1; |
280 | } | |
281 | ||
0d37aa34 | 282 | threads = thread_map__new(-1, getpid(), UINT_MAX); |
d854861c | 283 | if (threads == NULL) { |
454a3bbe | 284 | pr_debug("thread_map__new\n"); |
d854861c ACM |
285 | return -1; |
286 | } | |
287 | ||
23a2f3ab LM |
288 | memset(&attr, 0, sizeof(attr)); |
289 | attr.type = PERF_TYPE_TRACEPOINT; | |
290 | attr.config = id; | |
291 | evsel = perf_evsel__new(&attr, 0); | |
d854861c | 292 | if (evsel == NULL) { |
454a3bbe | 293 | pr_debug("perf_evsel__new\n"); |
d854861c ACM |
294 | goto out_thread_map_delete; |
295 | } | |
296 | ||
727ab04e | 297 | if (perf_evsel__open_per_thread(evsel, threads, false, NULL) < 0) { |
454a3bbe ACM |
298 | pr_debug("failed to open counter: %s, " |
299 | "tweak /proc/sys/kernel/perf_event_paranoid?\n", | |
300 | strerror(errno)); | |
d854861c ACM |
301 | goto out_evsel_delete; |
302 | } | |
303 | ||
304 | for (i = 0; i < nr_open_calls; ++i) { | |
305 | fd = open("/etc/passwd", O_RDONLY); | |
306 | close(fd); | |
307 | } | |
308 | ||
309 | if (perf_evsel__read_on_cpu(evsel, 0, 0) < 0) { | |
5d2cd909 | 310 | pr_debug("perf_evsel__read_on_cpu\n"); |
d854861c ACM |
311 | goto out_close_fd; |
312 | } | |
313 | ||
454a3bbe | 314 | if (evsel->counts->cpu[0].val != nr_open_calls) { |
9486aa38 | 315 | pr_debug("perf_evsel__read_on_cpu: expected to intercept %d calls, got %" PRIu64 "\n", |
d854861c | 316 | nr_open_calls, evsel->counts->cpu[0].val); |
454a3bbe ACM |
317 | goto out_close_fd; |
318 | } | |
d854861c ACM |
319 | |
320 | err = 0; | |
321 | out_close_fd: | |
322 | perf_evsel__close_fd(evsel, 1, threads->nr); | |
323 | out_evsel_delete: | |
324 | perf_evsel__delete(evsel); | |
325 | out_thread_map_delete: | |
326 | thread_map__delete(threads); | |
327 | return err; | |
328 | } | |
329 | ||
0252208e ACM |
330 | #include <sched.h> |
331 | ||
332 | static int test__open_syscall_event_on_all_cpus(void) | |
333 | { | |
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; | |
57b84e53 | 340 | cpu_set_t cpu_set; |
0252208e ACM |
341 | int id = trace_event__id("sys_enter_open"); |
342 | ||
343 | if (id < 0) { | |
344 | pr_debug("is debugfs mounted on /sys/kernel/debug?\n"); | |
345 | return -1; | |
346 | } | |
347 | ||
0d37aa34 | 348 | threads = thread_map__new(-1, getpid(), UINT_MAX); |
0252208e ACM |
349 | if (threads == NULL) { |
350 | pr_debug("thread_map__new\n"); | |
351 | return -1; | |
352 | } | |
353 | ||
354 | cpus = cpu_map__new(NULL); | |
98d77b78 HP |
355 | if (cpus == NULL) { |
356 | pr_debug("cpu_map__new\n"); | |
357 | goto out_thread_map_delete; | |
0252208e ACM |
358 | } |
359 | ||
0252208e | 360 | |
57b84e53 | 361 | CPU_ZERO(&cpu_set); |
0252208e ACM |
362 | |
363 | memset(&attr, 0, sizeof(attr)); | |
364 | attr.type = PERF_TYPE_TRACEPOINT; | |
365 | attr.config = id; | |
366 | evsel = perf_evsel__new(&attr, 0); | |
367 | if (evsel == NULL) { | |
368 | pr_debug("perf_evsel__new\n"); | |
57b84e53 | 369 | goto out_thread_map_delete; |
0252208e ACM |
370 | } |
371 | ||
727ab04e | 372 | if (perf_evsel__open(evsel, cpus, threads, false, NULL) < 0) { |
0252208e ACM |
373 | pr_debug("failed to open counter: %s, " |
374 | "tweak /proc/sys/kernel/perf_event_paranoid?\n", | |
375 | strerror(errno)); | |
376 | goto out_evsel_delete; | |
377 | } | |
378 | ||
379 | for (cpu = 0; cpu < cpus->nr; ++cpu) { | |
380 | unsigned int ncalls = nr_open_calls + cpu; | |
57b84e53 ACM |
381 | /* |
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 :-) | |
386 | */ | |
387 | if (cpus->map[cpu] >= CPU_SETSIZE) { | |
388 | pr_debug("Ignoring CPU %d\n", cpus->map[cpu]); | |
389 | continue; | |
390 | } | |
0252208e | 391 | |
57b84e53 ACM |
392 | CPU_SET(cpus->map[cpu], &cpu_set); |
393 | if (sched_setaffinity(0, sizeof(cpu_set), &cpu_set) < 0) { | |
ffb5e0fb HP |
394 | pr_debug("sched_setaffinity() failed on CPU %d: %s ", |
395 | cpus->map[cpu], | |
396 | strerror(errno)); | |
397 | goto out_close_fd; | |
398 | } | |
0252208e ACM |
399 | for (i = 0; i < ncalls; ++i) { |
400 | fd = open("/etc/passwd", O_RDONLY); | |
401 | close(fd); | |
402 | } | |
57b84e53 | 403 | CPU_CLR(cpus->map[cpu], &cpu_set); |
0252208e ACM |
404 | } |
405 | ||
406 | /* | |
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. | |
410 | */ | |
411 | if (perf_evsel__alloc_counts(evsel, cpus->nr) < 0) { | |
412 | pr_debug("perf_evsel__alloc_counts(ncpus=%d)\n", cpus->nr); | |
413 | goto out_close_fd; | |
414 | } | |
415 | ||
d2af9687 ACM |
416 | err = 0; |
417 | ||
0252208e ACM |
418 | for (cpu = 0; cpu < cpus->nr; ++cpu) { |
419 | unsigned int expected; | |
420 | ||
57b84e53 ACM |
421 | if (cpus->map[cpu] >= CPU_SETSIZE) |
422 | continue; | |
423 | ||
0252208e | 424 | if (perf_evsel__read_on_cpu(evsel, cpu, 0) < 0) { |
5d2cd909 | 425 | pr_debug("perf_evsel__read_on_cpu\n"); |
d2af9687 ACM |
426 | err = -1; |
427 | break; | |
0252208e ACM |
428 | } |
429 | ||
430 | expected = nr_open_calls + cpu; | |
431 | if (evsel->counts->cpu[cpu].val != expected) { | |
9486aa38 | 432 | pr_debug("perf_evsel__read_on_cpu: expected to intercept %d calls on cpu %d, got %" PRIu64 "\n", |
ffb5e0fb | 433 | expected, cpus->map[cpu], evsel->counts->cpu[cpu].val); |
d2af9687 | 434 | err = -1; |
0252208e ACM |
435 | } |
436 | } | |
437 | ||
0252208e ACM |
438 | out_close_fd: |
439 | perf_evsel__close_fd(evsel, 1, threads->nr); | |
440 | out_evsel_delete: | |
441 | perf_evsel__delete(evsel); | |
0252208e ACM |
442 | out_thread_map_delete: |
443 | thread_map__delete(threads); | |
444 | return err; | |
445 | } | |
446 | ||
de5fa3a8 ACM |
447 | /* |
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 | |
450 | * the syscalls. | |
451 | * | |
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. | |
454 | * | |
455 | * Then it checks if the number of syscalls reported as perf events by | |
456 | * the kernel corresponds to the number of syscalls made. | |
457 | */ | |
458 | static int test__basic_mmap(void) | |
459 | { | |
460 | int err = -1; | |
8115d60c | 461 | union perf_event *event; |
de5fa3a8 | 462 | struct thread_map *threads; |
de5fa3a8 ACM |
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, | |
469 | .watermark = 0, | |
470 | }; | |
471 | cpu_set_t cpu_set; | |
472 | const char *syscall_names[] = { "getsid", "getppid", "getpgrp", | |
473 | "getpgid", }; | |
474 | pid_t (*syscalls[])(void) = { (void *)getsid, getppid, getpgrp, | |
475 | (void*)getpgid }; | |
476 | #define nsyscalls ARRAY_SIZE(syscall_names) | |
477 | int ids[nsyscalls]; | |
478 | unsigned int nr_events[nsyscalls], | |
479 | expected_nr_events[nsyscalls], i, j; | |
480 | struct perf_evsel *evsels[nsyscalls], *evsel; | |
c2a70653 | 481 | int sample_size = __perf_evsel__sample_size(attr.sample_type); |
de5fa3a8 ACM |
482 | |
483 | for (i = 0; i < nsyscalls; ++i) { | |
484 | char name[64]; | |
485 | ||
486 | snprintf(name, sizeof(name), "sys_enter_%s", syscall_names[i]); | |
487 | ids[i] = trace_event__id(name); | |
488 | if (ids[i] < 0) { | |
489 | pr_debug("Is debugfs mounted on /sys/kernel/debug?\n"); | |
490 | return -1; | |
491 | } | |
492 | nr_events[i] = 0; | |
493 | expected_nr_events[i] = random() % 257; | |
494 | } | |
495 | ||
0d37aa34 | 496 | threads = thread_map__new(-1, getpid(), UINT_MAX); |
de5fa3a8 ACM |
497 | if (threads == NULL) { |
498 | pr_debug("thread_map__new\n"); | |
499 | return -1; | |
500 | } | |
501 | ||
502 | cpus = cpu_map__new(NULL); | |
54489c18 HP |
503 | if (cpus == NULL) { |
504 | pr_debug("cpu_map__new\n"); | |
de5fa3a8 ACM |
505 | goto out_free_threads; |
506 | } | |
507 | ||
508 | CPU_ZERO(&cpu_set); | |
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)); | |
514 | goto out_free_cpus; | |
515 | } | |
516 | ||
7e2ed097 | 517 | evlist = perf_evlist__new(cpus, threads); |
54489c18 | 518 | if (evlist == NULL) { |
de5fa3a8 ACM |
519 | pr_debug("perf_evlist__new\n"); |
520 | goto out_free_cpus; | |
521 | } | |
522 | ||
523 | /* anonymous union fields, can't be initialized above */ | |
524 | attr.wakeup_events = 1; | |
525 | attr.sample_period = 1; | |
526 | ||
de5fa3a8 ACM |
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; | |
533 | } | |
534 | ||
535 | perf_evlist__add(evlist, evsels[i]); | |
536 | ||
727ab04e | 537 | if (perf_evsel__open(evsels[i], cpus, threads, false, NULL) < 0) { |
de5fa3a8 ACM |
538 | pr_debug("failed to open counter: %s, " |
539 | "tweak /proc/sys/kernel/perf_event_paranoid?\n", | |
540 | strerror(errno)); | |
541 | goto out_close_fd; | |
542 | } | |
543 | } | |
544 | ||
7e2ed097 | 545 | if (perf_evlist__mmap(evlist, 128, true) < 0) { |
de5fa3a8 ACM |
546 | pr_debug("failed to mmap events: %d (%s)\n", errno, |
547 | strerror(errno)); | |
548 | goto out_close_fd; | |
549 | } | |
550 | ||
551 | for (i = 0; i < nsyscalls; ++i) | |
552 | for (j = 0; j < expected_nr_events[i]; ++j) { | |
553 | int foo = syscalls[i](); | |
554 | ++foo; | |
555 | } | |
556 | ||
aece948f | 557 | while ((event = perf_evlist__mmap_read(evlist, 0)) != NULL) { |
8d50e5b4 | 558 | struct perf_sample sample; |
de5fa3a8 ACM |
559 | |
560 | if (event->header.type != PERF_RECORD_SAMPLE) { | |
561 | pr_debug("unexpected %s event\n", | |
8115d60c | 562 | perf_event__name(event->header.type)); |
de5fa3a8 ACM |
563 | goto out_munmap; |
564 | } | |
565 | ||
5538beca | 566 | err = perf_event__parse_sample(event, attr.sample_type, sample_size, |
936be503 | 567 | false, &sample, false); |
5538beca FW |
568 | if (err) { |
569 | pr_err("Can't parse sample, err = %d\n", err); | |
570 | goto out_munmap; | |
571 | } | |
572 | ||
de5fa3a8 ACM |
573 | evsel = perf_evlist__id2evsel(evlist, sample.id); |
574 | if (evsel == NULL) { | |
575 | pr_debug("event with id %" PRIu64 | |
576 | " doesn't map to an evsel\n", sample.id); | |
577 | goto out_munmap; | |
578 | } | |
579 | nr_events[evsel->idx]++; | |
580 | } | |
581 | ||
582 | list_for_each_entry(evsel, &evlist->entries, node) { | |
583 | if (nr_events[evsel->idx] != expected_nr_events[evsel->idx]) { | |
584 | pr_debug("expected %d %s events, got %d\n", | |
585 | expected_nr_events[evsel->idx], | |
586 | event_name(evsel), nr_events[evsel->idx]); | |
587 | goto out_munmap; | |
588 | } | |
589 | } | |
590 | ||
591 | err = 0; | |
592 | out_munmap: | |
7e2ed097 | 593 | perf_evlist__munmap(evlist); |
de5fa3a8 ACM |
594 | out_close_fd: |
595 | for (i = 0; i < nsyscalls; ++i) | |
596 | perf_evsel__close_fd(evsels[i], 1, threads->nr); | |
597 | out_free_evlist: | |
598 | perf_evlist__delete(evlist); | |
599 | out_free_cpus: | |
600 | cpu_map__delete(cpus); | |
601 | out_free_threads: | |
602 | thread_map__delete(threads); | |
603 | return err; | |
604 | #undef nsyscalls | |
605 | } | |
606 | ||
13b62567 JO |
607 | #define TEST_ASSERT_VAL(text, cond) \ |
608 | do { \ | |
65c1e045 | 609 | if (!(cond)) { \ |
13b62567 JO |
610 | pr_debug("FAILED %s:%d %s\n", __FILE__, __LINE__, text); \ |
611 | return -1; \ | |
612 | } \ | |
613 | } while (0) | |
614 | ||
615 | static int test__checkevent_tracepoint(struct perf_evlist *evlist) | |
616 | { | |
617 | struct perf_evsel *evsel = list_entry(evlist->entries.next, | |
618 | struct perf_evsel, node); | |
619 | ||
620 | TEST_ASSERT_VAL("wrong number of entries", 1 == evlist->nr_entries); | |
621 | TEST_ASSERT_VAL("wrong type", PERF_TYPE_TRACEPOINT == evsel->attr.type); | |
622 | TEST_ASSERT_VAL("wrong sample_type", | |
623 | (PERF_SAMPLE_RAW | PERF_SAMPLE_TIME | PERF_SAMPLE_CPU) == | |
624 | evsel->attr.sample_type); | |
625 | TEST_ASSERT_VAL("wrong sample_period", 1 == evsel->attr.sample_period); | |
626 | return 0; | |
627 | } | |
628 | ||
629 | static int test__checkevent_tracepoint_multi(struct perf_evlist *evlist) | |
630 | { | |
631 | struct perf_evsel *evsel; | |
632 | ||
633 | TEST_ASSERT_VAL("wrong number of entries", evlist->nr_entries > 1); | |
634 | ||
635 | list_for_each_entry(evsel, &evlist->entries, node) { | |
636 | TEST_ASSERT_VAL("wrong type", | |
637 | PERF_TYPE_TRACEPOINT == evsel->attr.type); | |
638 | TEST_ASSERT_VAL("wrong sample_type", | |
639 | (PERF_SAMPLE_RAW | PERF_SAMPLE_TIME | PERF_SAMPLE_CPU) | |
640 | == evsel->attr.sample_type); | |
641 | TEST_ASSERT_VAL("wrong sample_period", | |
642 | 1 == evsel->attr.sample_period); | |
643 | } | |
644 | return 0; | |
645 | } | |
646 | ||
647 | static int test__checkevent_raw(struct perf_evlist *evlist) | |
648 | { | |
649 | struct perf_evsel *evsel = list_entry(evlist->entries.next, | |
650 | struct perf_evsel, node); | |
651 | ||
652 | TEST_ASSERT_VAL("wrong number of entries", 1 == evlist->nr_entries); | |
653 | TEST_ASSERT_VAL("wrong type", PERF_TYPE_RAW == evsel->attr.type); | |
89812fc8 | 654 | TEST_ASSERT_VAL("wrong config", 0x1a == evsel->attr.config); |
13b62567 JO |
655 | return 0; |
656 | } | |
657 | ||
658 | static int test__checkevent_numeric(struct perf_evlist *evlist) | |
659 | { | |
660 | struct perf_evsel *evsel = list_entry(evlist->entries.next, | |
661 | struct perf_evsel, node); | |
662 | ||
663 | TEST_ASSERT_VAL("wrong number of entries", 1 == evlist->nr_entries); | |
664 | TEST_ASSERT_VAL("wrong type", 1 == evsel->attr.type); | |
665 | TEST_ASSERT_VAL("wrong config", 1 == evsel->attr.config); | |
666 | return 0; | |
667 | } | |
668 | ||
669 | static int test__checkevent_symbolic_name(struct perf_evlist *evlist) | |
670 | { | |
671 | struct perf_evsel *evsel = list_entry(evlist->entries.next, | |
672 | struct perf_evsel, node); | |
673 | ||
674 | TEST_ASSERT_VAL("wrong number of entries", 1 == evlist->nr_entries); | |
675 | TEST_ASSERT_VAL("wrong type", PERF_TYPE_HARDWARE == evsel->attr.type); | |
676 | TEST_ASSERT_VAL("wrong config", | |
677 | PERF_COUNT_HW_INSTRUCTIONS == evsel->attr.config); | |
678 | return 0; | |
679 | } | |
680 | ||
8f707d84 JO |
681 | static int test__checkevent_symbolic_name_config(struct perf_evlist *evlist) |
682 | { | |
683 | struct perf_evsel *evsel = list_entry(evlist->entries.next, | |
684 | struct perf_evsel, node); | |
685 | ||
686 | TEST_ASSERT_VAL("wrong number of entries", 1 == evlist->nr_entries); | |
687 | TEST_ASSERT_VAL("wrong type", PERF_TYPE_HARDWARE == evsel->attr.type); | |
688 | TEST_ASSERT_VAL("wrong config", | |
689 | PERF_COUNT_HW_CPU_CYCLES == evsel->attr.config); | |
690 | TEST_ASSERT_VAL("wrong period", | |
691 | 100000 == evsel->attr.sample_period); | |
692 | TEST_ASSERT_VAL("wrong config1", | |
693 | 0 == evsel->attr.config1); | |
694 | TEST_ASSERT_VAL("wrong config2", | |
695 | 1 == evsel->attr.config2); | |
696 | return 0; | |
697 | } | |
698 | ||
13b62567 JO |
699 | static int test__checkevent_symbolic_alias(struct perf_evlist *evlist) |
700 | { | |
701 | struct perf_evsel *evsel = list_entry(evlist->entries.next, | |
702 | struct perf_evsel, node); | |
703 | ||
704 | TEST_ASSERT_VAL("wrong number of entries", 1 == evlist->nr_entries); | |
705 | TEST_ASSERT_VAL("wrong type", PERF_TYPE_SOFTWARE == evsel->attr.type); | |
706 | TEST_ASSERT_VAL("wrong config", | |
707 | PERF_COUNT_SW_PAGE_FAULTS == evsel->attr.config); | |
708 | return 0; | |
709 | } | |
710 | ||
711 | static int test__checkevent_genhw(struct perf_evlist *evlist) | |
712 | { | |
713 | struct perf_evsel *evsel = list_entry(evlist->entries.next, | |
714 | struct perf_evsel, node); | |
715 | ||
716 | TEST_ASSERT_VAL("wrong number of entries", 1 == evlist->nr_entries); | |
717 | TEST_ASSERT_VAL("wrong type", PERF_TYPE_HW_CACHE == evsel->attr.type); | |
718 | TEST_ASSERT_VAL("wrong config", (1 << 16) == evsel->attr.config); | |
719 | return 0; | |
720 | } | |
721 | ||
722 | static int test__checkevent_breakpoint(struct perf_evlist *evlist) | |
723 | { | |
724 | struct perf_evsel *evsel = list_entry(evlist->entries.next, | |
725 | struct perf_evsel, node); | |
726 | ||
727 | TEST_ASSERT_VAL("wrong number of entries", 1 == evlist->nr_entries); | |
728 | TEST_ASSERT_VAL("wrong type", PERF_TYPE_BREAKPOINT == evsel->attr.type); | |
729 | TEST_ASSERT_VAL("wrong config", 0 == evsel->attr.config); | |
730 | TEST_ASSERT_VAL("wrong bp_type", (HW_BREAKPOINT_R | HW_BREAKPOINT_W) == | |
731 | evsel->attr.bp_type); | |
732 | TEST_ASSERT_VAL("wrong bp_len", HW_BREAKPOINT_LEN_4 == | |
733 | evsel->attr.bp_len); | |
734 | return 0; | |
735 | } | |
736 | ||
737 | static int test__checkevent_breakpoint_x(struct perf_evlist *evlist) | |
738 | { | |
739 | struct perf_evsel *evsel = list_entry(evlist->entries.next, | |
740 | struct perf_evsel, node); | |
741 | ||
742 | TEST_ASSERT_VAL("wrong number of entries", 1 == evlist->nr_entries); | |
743 | TEST_ASSERT_VAL("wrong type", PERF_TYPE_BREAKPOINT == evsel->attr.type); | |
744 | TEST_ASSERT_VAL("wrong config", 0 == evsel->attr.config); | |
745 | TEST_ASSERT_VAL("wrong bp_type", | |
746 | HW_BREAKPOINT_X == evsel->attr.bp_type); | |
747 | TEST_ASSERT_VAL("wrong bp_len", sizeof(long) == evsel->attr.bp_len); | |
748 | return 0; | |
749 | } | |
750 | ||
751 | static int test__checkevent_breakpoint_r(struct perf_evlist *evlist) | |
752 | { | |
753 | struct perf_evsel *evsel = list_entry(evlist->entries.next, | |
754 | struct perf_evsel, node); | |
755 | ||
756 | TEST_ASSERT_VAL("wrong number of entries", 1 == evlist->nr_entries); | |
757 | TEST_ASSERT_VAL("wrong type", | |
758 | PERF_TYPE_BREAKPOINT == evsel->attr.type); | |
759 | TEST_ASSERT_VAL("wrong config", 0 == evsel->attr.config); | |
760 | TEST_ASSERT_VAL("wrong bp_type", | |
761 | HW_BREAKPOINT_R == evsel->attr.bp_type); | |
762 | TEST_ASSERT_VAL("wrong bp_len", | |
763 | HW_BREAKPOINT_LEN_4 == evsel->attr.bp_len); | |
764 | return 0; | |
765 | } | |
766 | ||
767 | static int test__checkevent_breakpoint_w(struct perf_evlist *evlist) | |
768 | { | |
769 | struct perf_evsel *evsel = list_entry(evlist->entries.next, | |
770 | struct perf_evsel, node); | |
771 | ||
772 | TEST_ASSERT_VAL("wrong number of entries", 1 == evlist->nr_entries); | |
773 | TEST_ASSERT_VAL("wrong type", | |
774 | PERF_TYPE_BREAKPOINT == evsel->attr.type); | |
775 | TEST_ASSERT_VAL("wrong config", 0 == evsel->attr.config); | |
776 | TEST_ASSERT_VAL("wrong bp_type", | |
777 | HW_BREAKPOINT_W == evsel->attr.bp_type); | |
778 | TEST_ASSERT_VAL("wrong bp_len", | |
779 | HW_BREAKPOINT_LEN_4 == evsel->attr.bp_len); | |
780 | return 0; | |
781 | } | |
782 | ||
65c1e045 JO |
783 | static int test__checkevent_tracepoint_modifier(struct perf_evlist *evlist) |
784 | { | |
785 | struct perf_evsel *evsel = list_entry(evlist->entries.next, | |
786 | struct perf_evsel, node); | |
787 | ||
788 | TEST_ASSERT_VAL("wrong exclude_user", evsel->attr.exclude_user); | |
789 | TEST_ASSERT_VAL("wrong exclude_kernel", !evsel->attr.exclude_kernel); | |
790 | TEST_ASSERT_VAL("wrong exclude_hv", evsel->attr.exclude_hv); | |
791 | TEST_ASSERT_VAL("wrong precise_ip", !evsel->attr.precise_ip); | |
792 | ||
793 | return test__checkevent_tracepoint(evlist); | |
794 | } | |
795 | ||
796 | static int | |
797 | test__checkevent_tracepoint_multi_modifier(struct perf_evlist *evlist) | |
798 | { | |
799 | struct perf_evsel *evsel; | |
800 | ||
801 | TEST_ASSERT_VAL("wrong number of entries", evlist->nr_entries > 1); | |
802 | ||
803 | list_for_each_entry(evsel, &evlist->entries, node) { | |
804 | TEST_ASSERT_VAL("wrong exclude_user", | |
805 | !evsel->attr.exclude_user); | |
806 | TEST_ASSERT_VAL("wrong exclude_kernel", | |
807 | evsel->attr.exclude_kernel); | |
808 | TEST_ASSERT_VAL("wrong exclude_hv", evsel->attr.exclude_hv); | |
809 | TEST_ASSERT_VAL("wrong precise_ip", !evsel->attr.precise_ip); | |
810 | } | |
811 | ||
812 | return test__checkevent_tracepoint_multi(evlist); | |
813 | } | |
814 | ||
815 | static int test__checkevent_raw_modifier(struct perf_evlist *evlist) | |
816 | { | |
817 | struct perf_evsel *evsel = list_entry(evlist->entries.next, | |
818 | struct perf_evsel, node); | |
819 | ||
820 | TEST_ASSERT_VAL("wrong exclude_user", evsel->attr.exclude_user); | |
821 | TEST_ASSERT_VAL("wrong exclude_kernel", !evsel->attr.exclude_kernel); | |
822 | TEST_ASSERT_VAL("wrong exclude_hv", evsel->attr.exclude_hv); | |
823 | TEST_ASSERT_VAL("wrong precise_ip", evsel->attr.precise_ip); | |
824 | ||
825 | return test__checkevent_raw(evlist); | |
826 | } | |
827 | ||
828 | static int test__checkevent_numeric_modifier(struct perf_evlist *evlist) | |
829 | { | |
830 | struct perf_evsel *evsel = list_entry(evlist->entries.next, | |
831 | struct perf_evsel, node); | |
832 | ||
833 | TEST_ASSERT_VAL("wrong exclude_user", evsel->attr.exclude_user); | |
834 | TEST_ASSERT_VAL("wrong exclude_kernel", evsel->attr.exclude_kernel); | |
835 | TEST_ASSERT_VAL("wrong exclude_hv", !evsel->attr.exclude_hv); | |
836 | TEST_ASSERT_VAL("wrong precise_ip", evsel->attr.precise_ip); | |
837 | ||
838 | return test__checkevent_numeric(evlist); | |
839 | } | |
840 | ||
841 | static int test__checkevent_symbolic_name_modifier(struct perf_evlist *evlist) | |
842 | { | |
843 | struct perf_evsel *evsel = list_entry(evlist->entries.next, | |
844 | struct perf_evsel, node); | |
845 | ||
846 | TEST_ASSERT_VAL("wrong exclude_user", evsel->attr.exclude_user); | |
847 | TEST_ASSERT_VAL("wrong exclude_kernel", evsel->attr.exclude_kernel); | |
848 | TEST_ASSERT_VAL("wrong exclude_hv", !evsel->attr.exclude_hv); | |
849 | TEST_ASSERT_VAL("wrong precise_ip", !evsel->attr.precise_ip); | |
850 | ||
851 | return test__checkevent_symbolic_name(evlist); | |
852 | } | |
853 | ||
854 | static int test__checkevent_symbolic_alias_modifier(struct perf_evlist *evlist) | |
855 | { | |
856 | struct perf_evsel *evsel = list_entry(evlist->entries.next, | |
857 | struct perf_evsel, node); | |
858 | ||
859 | TEST_ASSERT_VAL("wrong exclude_user", !evsel->attr.exclude_user); | |
860 | TEST_ASSERT_VAL("wrong exclude_kernel", evsel->attr.exclude_kernel); | |
861 | TEST_ASSERT_VAL("wrong exclude_hv", evsel->attr.exclude_hv); | |
862 | TEST_ASSERT_VAL("wrong precise_ip", !evsel->attr.precise_ip); | |
863 | ||
864 | return test__checkevent_symbolic_alias(evlist); | |
865 | } | |
866 | ||
867 | static int test__checkevent_genhw_modifier(struct perf_evlist *evlist) | |
868 | { | |
869 | struct perf_evsel *evsel = list_entry(evlist->entries.next, | |
870 | struct perf_evsel, node); | |
871 | ||
872 | TEST_ASSERT_VAL("wrong exclude_user", evsel->attr.exclude_user); | |
873 | TEST_ASSERT_VAL("wrong exclude_kernel", !evsel->attr.exclude_kernel); | |
874 | TEST_ASSERT_VAL("wrong exclude_hv", evsel->attr.exclude_hv); | |
875 | TEST_ASSERT_VAL("wrong precise_ip", evsel->attr.precise_ip); | |
876 | ||
877 | return test__checkevent_genhw(evlist); | |
878 | } | |
879 | ||
5d7be90e JO |
880 | static int test__checkevent_breakpoint_modifier(struct perf_evlist *evlist) |
881 | { | |
882 | struct perf_evsel *evsel = list_entry(evlist->entries.next, | |
883 | struct perf_evsel, node); | |
884 | ||
885 | TEST_ASSERT_VAL("wrong exclude_user", !evsel->attr.exclude_user); | |
886 | TEST_ASSERT_VAL("wrong exclude_kernel", evsel->attr.exclude_kernel); | |
887 | TEST_ASSERT_VAL("wrong exclude_hv", evsel->attr.exclude_hv); | |
888 | TEST_ASSERT_VAL("wrong precise_ip", !evsel->attr.precise_ip); | |
889 | ||
890 | return test__checkevent_breakpoint(evlist); | |
891 | } | |
892 | ||
893 | static int test__checkevent_breakpoint_x_modifier(struct perf_evlist *evlist) | |
894 | { | |
895 | struct perf_evsel *evsel = list_entry(evlist->entries.next, | |
896 | struct perf_evsel, node); | |
897 | ||
898 | TEST_ASSERT_VAL("wrong exclude_user", evsel->attr.exclude_user); | |
899 | TEST_ASSERT_VAL("wrong exclude_kernel", !evsel->attr.exclude_kernel); | |
900 | TEST_ASSERT_VAL("wrong exclude_hv", evsel->attr.exclude_hv); | |
901 | TEST_ASSERT_VAL("wrong precise_ip", !evsel->attr.precise_ip); | |
902 | ||
903 | return test__checkevent_breakpoint_x(evlist); | |
904 | } | |
905 | ||
906 | static int test__checkevent_breakpoint_r_modifier(struct perf_evlist *evlist) | |
907 | { | |
908 | struct perf_evsel *evsel = list_entry(evlist->entries.next, | |
909 | struct perf_evsel, node); | |
910 | ||
911 | TEST_ASSERT_VAL("wrong exclude_user", evsel->attr.exclude_user); | |
912 | TEST_ASSERT_VAL("wrong exclude_kernel", evsel->attr.exclude_kernel); | |
913 | TEST_ASSERT_VAL("wrong exclude_hv", !evsel->attr.exclude_hv); | |
914 | TEST_ASSERT_VAL("wrong precise_ip", evsel->attr.precise_ip); | |
915 | ||
916 | return test__checkevent_breakpoint_r(evlist); | |
917 | } | |
918 | ||
919 | static int test__checkevent_breakpoint_w_modifier(struct perf_evlist *evlist) | |
920 | { | |
921 | struct perf_evsel *evsel = list_entry(evlist->entries.next, | |
922 | struct perf_evsel, node); | |
923 | ||
924 | TEST_ASSERT_VAL("wrong exclude_user", !evsel->attr.exclude_user); | |
925 | TEST_ASSERT_VAL("wrong exclude_kernel", evsel->attr.exclude_kernel); | |
926 | TEST_ASSERT_VAL("wrong exclude_hv", evsel->attr.exclude_hv); | |
927 | TEST_ASSERT_VAL("wrong precise_ip", evsel->attr.precise_ip); | |
928 | ||
929 | return test__checkevent_breakpoint_w(evlist); | |
930 | } | |
931 | ||
5f537a26 JO |
932 | static int test__checkevent_pmu(struct perf_evlist *evlist) |
933 | { | |
934 | ||
935 | struct perf_evsel *evsel = list_entry(evlist->entries.next, | |
936 | struct perf_evsel, node); | |
937 | ||
938 | TEST_ASSERT_VAL("wrong number of entries", 1 == evlist->nr_entries); | |
939 | TEST_ASSERT_VAL("wrong type", PERF_TYPE_RAW == evsel->attr.type); | |
940 | TEST_ASSERT_VAL("wrong config", 10 == evsel->attr.config); | |
941 | TEST_ASSERT_VAL("wrong config1", 1 == evsel->attr.config1); | |
942 | TEST_ASSERT_VAL("wrong config2", 3 == evsel->attr.config2); | |
943 | TEST_ASSERT_VAL("wrong period", 1000 == evsel->attr.sample_period); | |
944 | ||
945 | return 0; | |
946 | } | |
947 | ||
5d7be90e JO |
948 | static int test__checkevent_list(struct perf_evlist *evlist) |
949 | { | |
950 | struct perf_evsel *evsel; | |
951 | ||
952 | TEST_ASSERT_VAL("wrong number of entries", 3 == evlist->nr_entries); | |
953 | ||
954 | /* r1 */ | |
955 | evsel = list_entry(evlist->entries.next, struct perf_evsel, node); | |
956 | TEST_ASSERT_VAL("wrong type", PERF_TYPE_RAW == evsel->attr.type); | |
957 | TEST_ASSERT_VAL("wrong config", 1 == evsel->attr.config); | |
958 | TEST_ASSERT_VAL("wrong config1", 0 == evsel->attr.config1); | |
959 | TEST_ASSERT_VAL("wrong config2", 0 == evsel->attr.config2); | |
960 | TEST_ASSERT_VAL("wrong exclude_user", !evsel->attr.exclude_user); | |
961 | TEST_ASSERT_VAL("wrong exclude_kernel", !evsel->attr.exclude_kernel); | |
962 | TEST_ASSERT_VAL("wrong exclude_hv", !evsel->attr.exclude_hv); | |
963 | TEST_ASSERT_VAL("wrong precise_ip", !evsel->attr.precise_ip); | |
964 | ||
965 | /* syscalls:sys_enter_open:k */ | |
966 | evsel = list_entry(evsel->node.next, struct perf_evsel, node); | |
967 | TEST_ASSERT_VAL("wrong type", PERF_TYPE_TRACEPOINT == evsel->attr.type); | |
968 | TEST_ASSERT_VAL("wrong sample_type", | |
969 | (PERF_SAMPLE_RAW | PERF_SAMPLE_TIME | PERF_SAMPLE_CPU) == | |
970 | evsel->attr.sample_type); | |
971 | TEST_ASSERT_VAL("wrong sample_period", 1 == evsel->attr.sample_period); | |
972 | TEST_ASSERT_VAL("wrong exclude_user", evsel->attr.exclude_user); | |
973 | TEST_ASSERT_VAL("wrong exclude_kernel", !evsel->attr.exclude_kernel); | |
974 | TEST_ASSERT_VAL("wrong exclude_hv", evsel->attr.exclude_hv); | |
975 | TEST_ASSERT_VAL("wrong precise_ip", !evsel->attr.precise_ip); | |
976 | ||
977 | /* 1:1:hp */ | |
978 | evsel = list_entry(evsel->node.next, struct perf_evsel, node); | |
979 | TEST_ASSERT_VAL("wrong type", 1 == evsel->attr.type); | |
980 | TEST_ASSERT_VAL("wrong config", 1 == evsel->attr.config); | |
981 | TEST_ASSERT_VAL("wrong exclude_user", evsel->attr.exclude_user); | |
982 | TEST_ASSERT_VAL("wrong exclude_kernel", evsel->attr.exclude_kernel); | |
983 | TEST_ASSERT_VAL("wrong exclude_hv", !evsel->attr.exclude_hv); | |
984 | TEST_ASSERT_VAL("wrong precise_ip", evsel->attr.precise_ip); | |
985 | ||
986 | return 0; | |
987 | } | |
988 | ||
13b62567 JO |
989 | static struct test__event_st { |
990 | const char *name; | |
991 | __u32 type; | |
992 | int (*check)(struct perf_evlist *evlist); | |
993 | } test__events[] = { | |
994 | { | |
995 | .name = "syscalls:sys_enter_open", | |
996 | .check = test__checkevent_tracepoint, | |
997 | }, | |
998 | { | |
999 | .name = "syscalls:*", | |
1000 | .check = test__checkevent_tracepoint_multi, | |
1001 | }, | |
1002 | { | |
89812fc8 | 1003 | .name = "r1a", |
13b62567 JO |
1004 | .check = test__checkevent_raw, |
1005 | }, | |
1006 | { | |
1007 | .name = "1:1", | |
1008 | .check = test__checkevent_numeric, | |
1009 | }, | |
1010 | { | |
1011 | .name = "instructions", | |
1012 | .check = test__checkevent_symbolic_name, | |
1013 | }, | |
8f707d84 JO |
1014 | { |
1015 | .name = "cycles/period=100000,config2/", | |
1016 | .check = test__checkevent_symbolic_name_config, | |
1017 | }, | |
13b62567 JO |
1018 | { |
1019 | .name = "faults", | |
1020 | .check = test__checkevent_symbolic_alias, | |
1021 | }, | |
1022 | { | |
1023 | .name = "L1-dcache-load-miss", | |
1024 | .check = test__checkevent_genhw, | |
1025 | }, | |
1026 | { | |
1027 | .name = "mem:0", | |
1028 | .check = test__checkevent_breakpoint, | |
1029 | }, | |
1030 | { | |
1031 | .name = "mem:0:x", | |
1032 | .check = test__checkevent_breakpoint_x, | |
1033 | }, | |
1034 | { | |
1035 | .name = "mem:0:r", | |
1036 | .check = test__checkevent_breakpoint_r, | |
1037 | }, | |
1038 | { | |
1039 | .name = "mem:0:w", | |
1040 | .check = test__checkevent_breakpoint_w, | |
1041 | }, | |
65c1e045 JO |
1042 | { |
1043 | .name = "syscalls:sys_enter_open:k", | |
1044 | .check = test__checkevent_tracepoint_modifier, | |
1045 | }, | |
1046 | { | |
1047 | .name = "syscalls:*:u", | |
1048 | .check = test__checkevent_tracepoint_multi_modifier, | |
1049 | }, | |
1050 | { | |
89812fc8 | 1051 | .name = "r1a:kp", |
65c1e045 JO |
1052 | .check = test__checkevent_raw_modifier, |
1053 | }, | |
1054 | { | |
1055 | .name = "1:1:hp", | |
1056 | .check = test__checkevent_numeric_modifier, | |
1057 | }, | |
1058 | { | |
1059 | .name = "instructions:h", | |
1060 | .check = test__checkevent_symbolic_name_modifier, | |
1061 | }, | |
1062 | { | |
1063 | .name = "faults:u", | |
1064 | .check = test__checkevent_symbolic_alias_modifier, | |
1065 | }, | |
1066 | { | |
1067 | .name = "L1-dcache-load-miss:kp", | |
1068 | .check = test__checkevent_genhw_modifier, | |
1069 | }, | |
5d7be90e JO |
1070 | { |
1071 | .name = "mem:0:u", | |
1072 | .check = test__checkevent_breakpoint_modifier, | |
1073 | }, | |
1074 | { | |
1075 | .name = "mem:0:x:k", | |
1076 | .check = test__checkevent_breakpoint_x_modifier, | |
1077 | }, | |
1078 | { | |
1079 | .name = "mem:0:r:hp", | |
1080 | .check = test__checkevent_breakpoint_r_modifier, | |
1081 | }, | |
1082 | { | |
1083 | .name = "mem:0:w:up", | |
1084 | .check = test__checkevent_breakpoint_w_modifier, | |
1085 | }, | |
5f537a26 JO |
1086 | { |
1087 | .name = "cpu/config=10,config1,config2=3,period=1000/u", | |
1088 | .check = test__checkevent_pmu, | |
1089 | }, | |
5d7be90e JO |
1090 | { |
1091 | .name = "r1,syscalls:sys_enter_open:k,1:1:hp", | |
1092 | .check = test__checkevent_list, | |
1093 | }, | |
13b62567 JO |
1094 | }; |
1095 | ||
1096 | #define TEST__EVENTS_CNT (sizeof(test__events) / sizeof(struct test__event_st)) | |
1097 | ||
1098 | static int test__parse_events(void) | |
1099 | { | |
1100 | struct perf_evlist *evlist; | |
1101 | u_int i; | |
1102 | int ret = 0; | |
1103 | ||
1104 | for (i = 0; i < TEST__EVENTS_CNT; i++) { | |
1105 | struct test__event_st *e = &test__events[i]; | |
1106 | ||
1107 | evlist = perf_evlist__new(NULL, NULL); | |
1108 | if (evlist == NULL) | |
1109 | break; | |
1110 | ||
1111 | ret = parse_events(evlist, e->name, 0); | |
1112 | if (ret) { | |
1113 | pr_debug("failed to parse event '%s', err %d\n", | |
1114 | e->name, ret); | |
1115 | break; | |
1116 | } | |
1117 | ||
1118 | ret = e->check(evlist); | |
5d7be90e | 1119 | perf_evlist__delete(evlist); |
13b62567 JO |
1120 | if (ret) |
1121 | break; | |
13b62567 JO |
1122 | } |
1123 | ||
1124 | return ret; | |
1125 | } | |
3e7c439a ACM |
1126 | |
1127 | static int sched__get_first_possible_cpu(pid_t pid, cpu_set_t **maskp, | |
1128 | size_t *sizep) | |
1129 | { | |
1130 | cpu_set_t *mask; | |
1131 | size_t size; | |
1132 | int i, cpu = -1, nrcpus = 1024; | |
1133 | realloc: | |
1134 | mask = CPU_ALLOC(nrcpus); | |
1135 | size = CPU_ALLOC_SIZE(nrcpus); | |
1136 | CPU_ZERO_S(size, mask); | |
1137 | ||
1138 | if (sched_getaffinity(pid, size, mask) == -1) { | |
1139 | CPU_FREE(mask); | |
1140 | if (errno == EINVAL && nrcpus < (1024 << 8)) { | |
1141 | nrcpus = nrcpus << 2; | |
1142 | goto realloc; | |
1143 | } | |
1144 | perror("sched_getaffinity"); | |
1145 | return -1; | |
1146 | } | |
1147 | ||
1148 | for (i = 0; i < nrcpus; i++) { | |
1149 | if (CPU_ISSET_S(i, size, mask)) { | |
1150 | if (cpu == -1) { | |
1151 | cpu = i; | |
1152 | *maskp = mask; | |
1153 | *sizep = size; | |
1154 | } else | |
1155 | CPU_CLR_S(i, size, mask); | |
1156 | } | |
1157 | } | |
1158 | ||
1159 | if (cpu == -1) | |
1160 | CPU_FREE(mask); | |
1161 | ||
1162 | return cpu; | |
1163 | } | |
1164 | ||
1165 | static int test__PERF_RECORD(void) | |
1166 | { | |
1167 | struct perf_record_opts opts = { | |
b809ac10 NK |
1168 | .target = { |
1169 | .uid = UINT_MAX, | |
d1cb9fce | 1170 | .uses_mmap = true, |
b809ac10 | 1171 | }, |
3e7c439a ACM |
1172 | .no_delay = true, |
1173 | .freq = 10, | |
1174 | .mmap_pages = 256, | |
3e7c439a ACM |
1175 | }; |
1176 | cpu_set_t *cpu_mask = NULL; | |
1177 | size_t cpu_mask_size = 0; | |
1178 | struct perf_evlist *evlist = perf_evlist__new(NULL, NULL); | |
1179 | struct perf_evsel *evsel; | |
1180 | struct perf_sample sample; | |
1181 | const char *cmd = "sleep"; | |
1182 | const char *argv[] = { cmd, "1", NULL, }; | |
1183 | char *bname; | |
1184 | u64 sample_type, prev_time = 0; | |
1185 | bool found_cmd_mmap = false, | |
1186 | found_libc_mmap = false, | |
1187 | found_vdso_mmap = false, | |
1188 | found_ld_mmap = false; | |
f71c49e5 | 1189 | int err = -1, errs = 0, i, wakeups = 0, sample_size; |
3e7c439a ACM |
1190 | u32 cpu; |
1191 | int total_events = 0, nr_events[PERF_RECORD_MAX] = { 0, }; | |
1192 | ||
1193 | if (evlist == NULL || argv == NULL) { | |
1194 | pr_debug("Not enough memory to create evlist\n"); | |
1195 | goto out; | |
1196 | } | |
1197 | ||
1198 | /* | |
1199 | * We need at least one evsel in the evlist, use the default | |
1200 | * one: "cycles". | |
1201 | */ | |
1202 | err = perf_evlist__add_default(evlist); | |
1203 | if (err < 0) { | |
1204 | pr_debug("Not enough memory to create evsel\n"); | |
1205 | goto out_delete_evlist; | |
1206 | } | |
1207 | ||
1208 | /* | |
1209 | * Create maps of threads and cpus to monitor. In this case | |
1210 | * we start with all threads and cpus (-1, -1) but then in | |
1211 | * perf_evlist__prepare_workload we'll fill in the only thread | |
1212 | * we're monitoring, the one forked there. | |
1213 | */ | |
b809ac10 | 1214 | err = perf_evlist__create_maps(evlist, &opts.target); |
3e7c439a ACM |
1215 | if (err < 0) { |
1216 | pr_debug("Not enough memory to create thread/cpu maps\n"); | |
1217 | goto out_delete_evlist; | |
1218 | } | |
1219 | ||
1220 | /* | |
1221 | * Prepare the workload in argv[] to run, it'll fork it, and then wait | |
1222 | * for perf_evlist__start_workload() to exec it. This is done this way | |
1223 | * so that we have time to open the evlist (calling sys_perf_event_open | |
1224 | * on all the fds) and then mmap them. | |
1225 | */ | |
1226 | err = perf_evlist__prepare_workload(evlist, &opts, argv); | |
1227 | if (err < 0) { | |
1228 | pr_debug("Couldn't run the workload!\n"); | |
1229 | goto out_delete_evlist; | |
1230 | } | |
1231 | ||
1232 | /* | |
1233 | * Config the evsels, setting attr->comm on the first one, etc. | |
1234 | */ | |
1235 | evsel = list_entry(evlist->entries.next, struct perf_evsel, node); | |
1236 | evsel->attr.sample_type |= PERF_SAMPLE_CPU; | |
1237 | evsel->attr.sample_type |= PERF_SAMPLE_TID; | |
1238 | evsel->attr.sample_type |= PERF_SAMPLE_TIME; | |
1239 | perf_evlist__config_attrs(evlist, &opts); | |
1240 | ||
1241 | err = sched__get_first_possible_cpu(evlist->workload.pid, &cpu_mask, | |
1242 | &cpu_mask_size); | |
1243 | if (err < 0) { | |
1244 | pr_debug("sched__get_first_possible_cpu: %s\n", strerror(errno)); | |
1245 | goto out_delete_evlist; | |
1246 | } | |
1247 | ||
1248 | cpu = err; | |
1249 | ||
1250 | /* | |
1251 | * So that we can check perf_sample.cpu on all the samples. | |
1252 | */ | |
1253 | if (sched_setaffinity(evlist->workload.pid, cpu_mask_size, cpu_mask) < 0) { | |
1254 | pr_debug("sched_setaffinity: %s\n", strerror(errno)); | |
1255 | goto out_free_cpu_mask; | |
1256 | } | |
1257 | ||
1258 | /* | |
1259 | * Call sys_perf_event_open on all the fds on all the evsels, | |
1260 | * grouping them if asked to. | |
1261 | */ | |
1262 | err = perf_evlist__open(evlist, opts.group); | |
1263 | if (err < 0) { | |
1264 | pr_debug("perf_evlist__open: %s\n", strerror(errno)); | |
1265 | goto out_delete_evlist; | |
1266 | } | |
1267 | ||
1268 | /* | |
1269 | * mmap the first fd on a given CPU and ask for events for the other | |
1270 | * fds in the same CPU to be injected in the same mmap ring buffer | |
1271 | * (using ioctl(PERF_EVENT_IOC_SET_OUTPUT)). | |
1272 | */ | |
1273 | err = perf_evlist__mmap(evlist, opts.mmap_pages, false); | |
1274 | if (err < 0) { | |
1275 | pr_debug("perf_evlist__mmap: %s\n", strerror(errno)); | |
1276 | goto out_delete_evlist; | |
1277 | } | |
1278 | ||
1279 | /* | |
1280 | * We'll need these two to parse the PERF_SAMPLE_* fields in each | |
1281 | * event. | |
1282 | */ | |
1283 | sample_type = perf_evlist__sample_type(evlist); | |
1284 | sample_size = __perf_evsel__sample_size(sample_type); | |
1285 | ||
1286 | /* | |
1287 | * Now that all is properly set up, enable the events, they will | |
1288 | * count just on workload.pid, which will start... | |
1289 | */ | |
1290 | perf_evlist__enable(evlist); | |
1291 | ||
1292 | /* | |
1293 | * Now! | |
1294 | */ | |
1295 | perf_evlist__start_workload(evlist); | |
1296 | ||
3e7c439a ACM |
1297 | while (1) { |
1298 | int before = total_events; | |
1299 | ||
1300 | for (i = 0; i < evlist->nr_mmaps; i++) { | |
1301 | union perf_event *event; | |
1302 | ||
1303 | while ((event = perf_evlist__mmap_read(evlist, i)) != NULL) { | |
1304 | const u32 type = event->header.type; | |
1305 | const char *name = perf_event__name(type); | |
1306 | ||
1307 | ++total_events; | |
1308 | if (type < PERF_RECORD_MAX) | |
1309 | nr_events[type]++; | |
1310 | ||
f71c49e5 ACM |
1311 | err = perf_event__parse_sample(event, sample_type, |
1312 | sample_size, true, | |
1313 | &sample, false); | |
1314 | if (err < 0) { | |
3e7c439a ACM |
1315 | if (verbose) |
1316 | perf_event__fprintf(event, stderr); | |
1317 | pr_debug("Couldn't parse sample\n"); | |
1318 | goto out_err; | |
1319 | } | |
1320 | ||
1321 | if (verbose) { | |
1322 | pr_info("%" PRIu64" %d ", sample.time, sample.cpu); | |
1323 | perf_event__fprintf(event, stderr); | |
1324 | } | |
1325 | ||
1326 | if (prev_time > sample.time) { | |
1327 | pr_debug("%s going backwards in time, prev=%" PRIu64 ", curr=%" PRIu64 "\n", | |
1328 | name, prev_time, sample.time); | |
f71c49e5 | 1329 | ++errs; |
3e7c439a ACM |
1330 | } |
1331 | ||
1332 | prev_time = sample.time; | |
1333 | ||
1334 | if (sample.cpu != cpu) { | |
1335 | pr_debug("%s with unexpected cpu, expected %d, got %d\n", | |
1336 | name, cpu, sample.cpu); | |
f71c49e5 | 1337 | ++errs; |
3e7c439a ACM |
1338 | } |
1339 | ||
1340 | if ((pid_t)sample.pid != evlist->workload.pid) { | |
1341 | pr_debug("%s with unexpected pid, expected %d, got %d\n", | |
1342 | name, evlist->workload.pid, sample.pid); | |
f71c49e5 | 1343 | ++errs; |
3e7c439a ACM |
1344 | } |
1345 | ||
1346 | if ((pid_t)sample.tid != evlist->workload.pid) { | |
1347 | pr_debug("%s with unexpected tid, expected %d, got %d\n", | |
1348 | name, evlist->workload.pid, sample.tid); | |
f71c49e5 | 1349 | ++errs; |
3e7c439a ACM |
1350 | } |
1351 | ||
1352 | if ((type == PERF_RECORD_COMM || | |
1353 | type == PERF_RECORD_MMAP || | |
1354 | type == PERF_RECORD_FORK || | |
1355 | type == PERF_RECORD_EXIT) && | |
1356 | (pid_t)event->comm.pid != evlist->workload.pid) { | |
1357 | pr_debug("%s with unexpected pid/tid\n", name); | |
f71c49e5 | 1358 | ++errs; |
3e7c439a ACM |
1359 | } |
1360 | ||
1361 | if ((type == PERF_RECORD_COMM || | |
1362 | type == PERF_RECORD_MMAP) && | |
1363 | event->comm.pid != event->comm.tid) { | |
1364 | pr_debug("%s with different pid/tid!\n", name); | |
f71c49e5 | 1365 | ++errs; |
3e7c439a ACM |
1366 | } |
1367 | ||
1368 | switch (type) { | |
1369 | case PERF_RECORD_COMM: | |
1370 | if (strcmp(event->comm.comm, cmd)) { | |
1371 | pr_debug("%s with unexpected comm!\n", name); | |
f71c49e5 | 1372 | ++errs; |
3e7c439a ACM |
1373 | } |
1374 | break; | |
1375 | case PERF_RECORD_EXIT: | |
1376 | goto found_exit; | |
1377 | case PERF_RECORD_MMAP: | |
1378 | bname = strrchr(event->mmap.filename, '/'); | |
1379 | if (bname != NULL) { | |
1380 | if (!found_cmd_mmap) | |
1381 | found_cmd_mmap = !strcmp(bname + 1, cmd); | |
1382 | if (!found_libc_mmap) | |
1383 | found_libc_mmap = !strncmp(bname + 1, "libc", 4); | |
1384 | if (!found_ld_mmap) | |
1385 | found_ld_mmap = !strncmp(bname + 1, "ld", 2); | |
1386 | } else if (!found_vdso_mmap) | |
1387 | found_vdso_mmap = !strcmp(event->mmap.filename, "[vdso]"); | |
1388 | break; | |
1389 | ||
1390 | case PERF_RECORD_SAMPLE: | |
1391 | /* Just ignore samples for now */ | |
1392 | break; | |
1393 | default: | |
1394 | pr_debug("Unexpected perf_event->header.type %d!\n", | |
1395 | type); | |
f71c49e5 | 1396 | ++errs; |
3e7c439a ACM |
1397 | } |
1398 | } | |
1399 | } | |
1400 | ||
1401 | /* | |
1402 | * We don't use poll here because at least at 3.1 times the | |
1403 | * PERF_RECORD_{!SAMPLE} events don't honour | |
1404 | * perf_event_attr.wakeup_events, just PERF_EVENT_SAMPLE does. | |
1405 | */ | |
1406 | if (total_events == before && false) | |
1407 | poll(evlist->pollfd, evlist->nr_fds, -1); | |
1408 | ||
1409 | sleep(1); | |
1410 | if (++wakeups > 5) { | |
1411 | pr_debug("No PERF_RECORD_EXIT event!\n"); | |
f71c49e5 | 1412 | break; |
3e7c439a ACM |
1413 | } |
1414 | } | |
1415 | ||
1416 | found_exit: | |
1417 | if (nr_events[PERF_RECORD_COMM] > 1) { | |
1418 | pr_debug("Excessive number of PERF_RECORD_COMM events!\n"); | |
f71c49e5 | 1419 | ++errs; |
3e7c439a ACM |
1420 | } |
1421 | ||
1422 | if (nr_events[PERF_RECORD_COMM] == 0) { | |
1423 | pr_debug("Missing PERF_RECORD_COMM for %s!\n", cmd); | |
f71c49e5 | 1424 | ++errs; |
3e7c439a ACM |
1425 | } |
1426 | ||
1427 | if (!found_cmd_mmap) { | |
1428 | pr_debug("PERF_RECORD_MMAP for %s missing!\n", cmd); | |
f71c49e5 | 1429 | ++errs; |
3e7c439a ACM |
1430 | } |
1431 | ||
1432 | if (!found_libc_mmap) { | |
1433 | pr_debug("PERF_RECORD_MMAP for %s missing!\n", "libc"); | |
f71c49e5 | 1434 | ++errs; |
3e7c439a ACM |
1435 | } |
1436 | ||
1437 | if (!found_ld_mmap) { | |
1438 | pr_debug("PERF_RECORD_MMAP for %s missing!\n", "ld"); | |
f71c49e5 | 1439 | ++errs; |
3e7c439a ACM |
1440 | } |
1441 | ||
1442 | if (!found_vdso_mmap) { | |
1443 | pr_debug("PERF_RECORD_MMAP for %s missing!\n", "[vdso]"); | |
f71c49e5 | 1444 | ++errs; |
3e7c439a | 1445 | } |
3e7c439a ACM |
1446 | out_err: |
1447 | perf_evlist__munmap(evlist); | |
1448 | out_free_cpu_mask: | |
1449 | CPU_FREE(cpu_mask); | |
1450 | out_delete_evlist: | |
1451 | perf_evlist__delete(evlist); | |
1452 | out: | |
f71c49e5 | 1453 | return (err < 0 || errs > 0) ? -1 : 0; |
3e7c439a ACM |
1454 | } |
1455 | ||
08aa0d1f PZ |
1456 | |
1457 | #if defined(__x86_64__) || defined(__i386__) | |
1458 | ||
1459 | #define barrier() asm volatile("" ::: "memory") | |
1460 | ||
1461 | static u64 rdpmc(unsigned int counter) | |
1462 | { | |
1463 | unsigned int low, high; | |
1464 | ||
1465 | asm volatile("rdpmc" : "=a" (low), "=d" (high) : "c" (counter)); | |
1466 | ||
1467 | return low | ((u64)high) << 32; | |
1468 | } | |
1469 | ||
1470 | static u64 rdtsc(void) | |
1471 | { | |
1472 | unsigned int low, high; | |
1473 | ||
1474 | asm volatile("rdtsc" : "=a" (low), "=d" (high)); | |
1475 | ||
1476 | return low | ((u64)high) << 32; | |
1477 | } | |
1478 | ||
1479 | static u64 mmap_read_self(void *addr) | |
1480 | { | |
1481 | struct perf_event_mmap_page *pc = addr; | |
1482 | u32 seq, idx, time_mult = 0, time_shift = 0; | |
1483 | u64 count, cyc = 0, time_offset = 0, enabled, running, delta; | |
1484 | ||
1485 | do { | |
1486 | seq = pc->lock; | |
1487 | barrier(); | |
1488 | ||
1489 | enabled = pc->time_enabled; | |
1490 | running = pc->time_running; | |
1491 | ||
1492 | if (enabled != running) { | |
1493 | cyc = rdtsc(); | |
1494 | time_mult = pc->time_mult; | |
1495 | time_shift = pc->time_shift; | |
1496 | time_offset = pc->time_offset; | |
1497 | } | |
1498 | ||
1499 | idx = pc->index; | |
1500 | count = pc->offset; | |
1501 | if (idx) | |
1502 | count += rdpmc(idx - 1); | |
1503 | ||
1504 | barrier(); | |
1505 | } while (pc->lock != seq); | |
1506 | ||
1507 | if (enabled != running) { | |
1508 | u64 quot, rem; | |
1509 | ||
1510 | quot = (cyc >> time_shift); | |
1511 | rem = cyc & ((1 << time_shift) - 1); | |
1512 | delta = time_offset + quot * time_mult + | |
1513 | ((rem * time_mult) >> time_shift); | |
1514 | ||
1515 | enabled += delta; | |
1516 | if (idx) | |
1517 | running += delta; | |
1518 | ||
1519 | quot = count / running; | |
1520 | rem = count % running; | |
1521 | count = quot * enabled + (rem * enabled) / running; | |
1522 | } | |
1523 | ||
1524 | return count; | |
1525 | } | |
1526 | ||
1527 | /* | |
1528 | * If the RDPMC instruction faults then signal this back to the test parent task: | |
1529 | */ | |
1530 | static void segfault_handler(int sig __used, siginfo_t *info __used, void *uc __used) | |
1531 | { | |
1532 | exit(-1); | |
1533 | } | |
1534 | ||
1535 | static int __test__rdpmc(void) | |
1536 | { | |
1537 | long page_size = sysconf(_SC_PAGE_SIZE); | |
1538 | volatile int tmp = 0; | |
1539 | u64 i, loops = 1000; | |
1540 | int n; | |
1541 | int fd; | |
1542 | void *addr; | |
1543 | struct perf_event_attr attr = { | |
1544 | .type = PERF_TYPE_HARDWARE, | |
1545 | .config = PERF_COUNT_HW_INSTRUCTIONS, | |
1546 | .exclude_kernel = 1, | |
1547 | }; | |
1548 | u64 delta_sum = 0; | |
1549 | struct sigaction sa; | |
1550 | ||
1551 | sigfillset(&sa.sa_mask); | |
1552 | sa.sa_sigaction = segfault_handler; | |
1553 | sigaction(SIGSEGV, &sa, NULL); | |
1554 | ||
08aa0d1f PZ |
1555 | fd = sys_perf_event_open(&attr, 0, -1, -1, 0); |
1556 | if (fd < 0) { | |
1557 | die("Error: sys_perf_event_open() syscall returned " | |
1558 | "with %d (%s)\n", fd, strerror(errno)); | |
1559 | } | |
1560 | ||
1561 | addr = mmap(NULL, page_size, PROT_READ, MAP_SHARED, fd, 0); | |
1562 | if (addr == (void *)(-1)) { | |
1563 | die("Error: mmap() syscall returned " | |
1564 | "with (%s)\n", strerror(errno)); | |
1565 | } | |
1566 | ||
1567 | for (n = 0; n < 6; n++) { | |
1568 | u64 stamp, now, delta; | |
1569 | ||
1570 | stamp = mmap_read_self(addr); | |
1571 | ||
1572 | for (i = 0; i < loops; i++) | |
1573 | tmp++; | |
1574 | ||
1575 | now = mmap_read_self(addr); | |
1576 | loops *= 10; | |
1577 | ||
1578 | delta = now - stamp; | |
23080e4c | 1579 | pr_debug("%14d: %14Lu\n", n, (long long)delta); |
08aa0d1f PZ |
1580 | |
1581 | delta_sum += delta; | |
1582 | } | |
1583 | ||
1584 | munmap(addr, page_size); | |
1585 | close(fd); | |
1586 | ||
23080e4c | 1587 | pr_debug(" "); |
08aa0d1f PZ |
1588 | |
1589 | if (!delta_sum) | |
1590 | return -1; | |
1591 | ||
1592 | return 0; | |
1593 | } | |
1594 | ||
1595 | static int test__rdpmc(void) | |
1596 | { | |
1597 | int status = 0; | |
1598 | int wret = 0; | |
1599 | int ret; | |
1600 | int pid; | |
1601 | ||
1602 | pid = fork(); | |
1603 | if (pid < 0) | |
1604 | return -1; | |
1605 | ||
1606 | if (!pid) { | |
1607 | ret = __test__rdpmc(); | |
1608 | ||
1609 | exit(ret); | |
1610 | } | |
1611 | ||
1612 | wret = waitpid(pid, &status, 0); | |
1613 | if (wret < 0 || status) | |
1614 | return -1; | |
1615 | ||
1616 | return 0; | |
1617 | } | |
1618 | ||
1619 | #endif | |
1620 | ||
cd82a32e JO |
1621 | static int test__perf_pmu(void) |
1622 | { | |
1623 | return perf_pmu__test(); | |
1624 | } | |
1625 | ||
1c6a800c ACM |
1626 | static struct test { |
1627 | const char *desc; | |
1628 | int (*func)(void); | |
1629 | } tests[] = { | |
1630 | { | |
1631 | .desc = "vmlinux symtab matches kallsyms", | |
1632 | .func = test__vmlinux_matches_kallsyms, | |
1633 | }, | |
d854861c ACM |
1634 | { |
1635 | .desc = "detect open syscall event", | |
1636 | .func = test__open_syscall_event, | |
1637 | }, | |
0252208e ACM |
1638 | { |
1639 | .desc = "detect open syscall event on all cpus", | |
1640 | .func = test__open_syscall_event_on_all_cpus, | |
1641 | }, | |
de5fa3a8 ACM |
1642 | { |
1643 | .desc = "read samples using the mmap interface", | |
1644 | .func = test__basic_mmap, | |
1645 | }, | |
13b62567 JO |
1646 | { |
1647 | .desc = "parse events tests", | |
1648 | .func = test__parse_events, | |
1649 | }, | |
08aa0d1f PZ |
1650 | #if defined(__x86_64__) || defined(__i386__) |
1651 | { | |
1652 | .desc = "x86 rdpmc test", | |
1653 | .func = test__rdpmc, | |
1654 | }, | |
1655 | #endif | |
3e7c439a ACM |
1656 | { |
1657 | .desc = "Validate PERF_RECORD_* events & perf_sample fields", | |
1658 | .func = test__PERF_RECORD, | |
1659 | }, | |
cd82a32e JO |
1660 | { |
1661 | .desc = "Test perf pmu format parsing", | |
1662 | .func = test__perf_pmu, | |
1663 | }, | |
1c6a800c ACM |
1664 | { |
1665 | .func = NULL, | |
1666 | }, | |
1667 | }; | |
1668 | ||
e60770a0 | 1669 | static bool perf_test__matches(int curr, int argc, const char *argv[]) |
1c6a800c | 1670 | { |
e60770a0 ACM |
1671 | int i; |
1672 | ||
1673 | if (argc == 0) | |
1674 | return true; | |
1675 | ||
1676 | for (i = 0; i < argc; ++i) { | |
1677 | char *end; | |
1678 | long nr = strtoul(argv[i], &end, 10); | |
1679 | ||
1680 | if (*end == '\0') { | |
1681 | if (nr == curr + 1) | |
1682 | return true; | |
1683 | continue; | |
1684 | } | |
1c6a800c | 1685 | |
e60770a0 ACM |
1686 | if (strstr(tests[curr].desc, argv[i])) |
1687 | return true; | |
1688 | } | |
1689 | ||
1690 | return false; | |
1691 | } | |
1692 | ||
1693 | static int __cmd_test(int argc, const char *argv[]) | |
1694 | { | |
1695 | int i = 0; | |
1c6a800c ACM |
1696 | |
1697 | while (tests[i].func) { | |
e60770a0 ACM |
1698 | int curr = i++, err; |
1699 | ||
1700 | if (!perf_test__matches(curr, argc, argv)) | |
1701 | continue; | |
1702 | ||
1703 | pr_info("%2d: %s:", i, tests[curr].desc); | |
1c6a800c | 1704 | pr_debug("\n--- start ---\n"); |
e60770a0 ACM |
1705 | err = tests[curr].func(); |
1706 | pr_debug("---- end ----\n%s:", tests[curr].desc); | |
1c6a800c | 1707 | pr_info(" %s\n", err ? "FAILED!\n" : "Ok"); |
1c6a800c ACM |
1708 | } |
1709 | ||
1710 | return 0; | |
1711 | } | |
1712 | ||
e60770a0 ACM |
1713 | static int perf_test__list(int argc, const char **argv) |
1714 | { | |
1715 | int i = 0; | |
1716 | ||
1717 | while (tests[i].func) { | |
1718 | int curr = i++; | |
1719 | ||
1720 | if (argc > 1 && !strstr(tests[curr].desc, argv[1])) | |
1721 | continue; | |
1722 | ||
1723 | pr_info("%2d: %s\n", i, tests[curr].desc); | |
1724 | } | |
1725 | ||
1726 | return 0; | |
1727 | } | |
1c6a800c | 1728 | |
e60770a0 ACM |
1729 | int cmd_test(int argc, const char **argv, const char *prefix __used) |
1730 | { | |
1731 | const char * const test_usage[] = { | |
1732 | "perf test [<options>] [{list <test-name-fragment>|[<test-name-fragments>|<test-numbers>]}]", | |
1733 | NULL, | |
1734 | }; | |
1735 | const struct option test_options[] = { | |
c30ab8aa | 1736 | OPT_INCR('v', "verbose", &verbose, |
1c6a800c ACM |
1737 | "be more verbose (show symbol address, etc)"), |
1738 | OPT_END() | |
e60770a0 | 1739 | }; |
1c6a800c | 1740 | |
1c6a800c | 1741 | argc = parse_options(argc, argv, test_options, test_usage, 0); |
e60770a0 ACM |
1742 | if (argc >= 1 && !strcmp(argv[0], "list")) |
1743 | return perf_test__list(argc, argv); | |
1c6a800c ACM |
1744 | |
1745 | symbol_conf.priv_size = sizeof(int); | |
1746 | symbol_conf.sort_by_name = true; | |
1747 | symbol_conf.try_vmlinux_path = true; | |
1748 | ||
1749 | if (symbol__init() < 0) | |
1750 | return -1; | |
1751 | ||
e60770a0 | 1752 | return __cmd_test(argc, argv); |
1c6a800c | 1753 | } |