blob: dc91ee06a37ccf19cf65f519a928bfaae4b969b7 [file] [log] [blame]
Arnaldo Carvalho de Melo1c6a8002010-04-29 18:58:32 -03001/*
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"
Arnaldo Carvalho de Melode5fa3a2011-01-15 10:42:46 -020010#include "util/evlist.h"
Arnaldo Carvalho de Melo1c6a8002010-04-29 18:58:32 -030011#include "util/parse-options.h"
Arnaldo Carvalho de Melode5fa3a2011-01-15 10:42:46 -020012#include "util/parse-events.h"
Arnaldo Carvalho de Melo1c6a8002010-04-29 18:58:32 -030013#include "util/session.h"
14#include "util/symbol.h"
Arnaldo Carvalho de Melofd782602011-01-18 15:15:24 -020015#include "util/thread_map.h"
Arnaldo Carvalho de Melo1c6a8002010-04-29 18:58:32 -030016
17static long page_size;
18
19static int vmlinux_matches_kallsyms_filter(struct map *map __used, struct symbol *sym)
20{
21 bool *visited = symbol__priv(sym);
22 *visited = true;
23 return 0;
24}
25
26static int test__vmlinux_matches_kallsyms(void)
27{
28 int err = -1;
29 struct rb_node *nd;
30 struct symbol *sym;
31 struct map *kallsyms_map, *vmlinux_map;
32 struct machine kallsyms, vmlinux;
33 enum map_type type = MAP__FUNCTION;
34 struct ref_reloc_sym ref_reloc_sym = { .name = "_stext", };
35
36 /*
37 * Step 1:
38 *
39 * Init the machines that will hold kernel, modules obtained from
40 * both vmlinux + .ko files and from /proc/kallsyms split by modules.
41 */
42 machine__init(&kallsyms, "", HOST_KERNEL_ID);
43 machine__init(&vmlinux, "", HOST_KERNEL_ID);
44
45 /*
46 * Step 2:
47 *
48 * Create the kernel maps for kallsyms and the DSO where we will then
49 * load /proc/kallsyms. Also create the modules maps from /proc/modules
50 * and find the .ko files that match them in /lib/modules/`uname -r`/.
51 */
52 if (machine__create_kernel_maps(&kallsyms) < 0) {
53 pr_debug("machine__create_kernel_maps ");
54 return -1;
55 }
56
57 /*
58 * Step 3:
59 *
60 * Load and split /proc/kallsyms into multiple maps, one per module.
61 */
62 if (machine__load_kallsyms(&kallsyms, "/proc/kallsyms", type, NULL) <= 0) {
63 pr_debug("dso__load_kallsyms ");
64 goto out;
65 }
66
67 /*
68 * Step 4:
69 *
70 * kallsyms will be internally on demand sorted by name so that we can
71 * find the reference relocation * symbol, i.e. the symbol we will use
72 * to see if the running kernel was relocated by checking if it has the
73 * same value in the vmlinux file we load.
74 */
75 kallsyms_map = machine__kernel_map(&kallsyms, type);
76
77 sym = map__find_symbol_by_name(kallsyms_map, ref_reloc_sym.name, NULL);
78 if (sym == NULL) {
79 pr_debug("dso__find_symbol_by_name ");
80 goto out;
81 }
82
83 ref_reloc_sym.addr = sym->start;
84
85 /*
86 * Step 5:
87 *
88 * Now repeat step 2, this time for the vmlinux file we'll auto-locate.
89 */
90 if (machine__create_kernel_maps(&vmlinux) < 0) {
91 pr_debug("machine__create_kernel_maps ");
92 goto out;
93 }
94
95 vmlinux_map = machine__kernel_map(&vmlinux, type);
96 map__kmap(vmlinux_map)->ref_reloc_sym = &ref_reloc_sym;
97
98 /*
99 * Step 6:
100 *
101 * Locate a vmlinux file in the vmlinux path that has a buildid that
102 * matches the one of the running kernel.
103 *
104 * While doing that look if we find the ref reloc symbol, if we find it
105 * we'll have its ref_reloc_symbol.unrelocated_addr and then
106 * maps__reloc_vmlinux will notice and set proper ->[un]map_ip routines
107 * to fixup the symbols.
108 */
109 if (machine__load_vmlinux_path(&vmlinux, type,
110 vmlinux_matches_kallsyms_filter) <= 0) {
111 pr_debug("machine__load_vmlinux_path ");
112 goto out;
113 }
114
115 err = 0;
116 /*
117 * Step 7:
118 *
119 * Now look at the symbols in the vmlinux DSO and check if we find all of them
120 * in the kallsyms dso. For the ones that are in both, check its names and
121 * end addresses too.
122 */
123 for (nd = rb_first(&vmlinux_map->dso->symbols[type]); nd; nd = rb_next(nd)) {
Arnaldo Carvalho de Melod3678752010-12-21 23:38:37 -0200124 struct symbol *pair, *first_pair;
125 bool backwards = true;
Arnaldo Carvalho de Melo1c6a8002010-04-29 18:58:32 -0300126
127 sym = rb_entry(nd, struct symbol, rb_node);
Arnaldo Carvalho de Melod3678752010-12-21 23:38:37 -0200128
129 if (sym->start == sym->end)
130 continue;
131
132 first_pair = machine__find_kernel_symbol(&kallsyms, type, sym->start, NULL, NULL);
133 pair = first_pair;
Arnaldo Carvalho de Melo1c6a8002010-04-29 18:58:32 -0300134
135 if (pair && pair->start == sym->start) {
136next_pair:
137 if (strcmp(sym->name, pair->name) == 0) {
138 /*
139 * kallsyms don't have the symbol end, so we
140 * set that by using the next symbol start - 1,
141 * in some cases we get this up to a page
142 * wrong, trace_kmalloc when I was developing
143 * this code was one such example, 2106 bytes
144 * off the real size. More than that and we
145 * _really_ have a problem.
146 */
147 s64 skew = sym->end - pair->end;
148 if (llabs(skew) < page_size)
149 continue;
150
Arnaldo Carvalho de Melo9486aa32011-01-22 20:37:02 -0200151 pr_debug("%#" PRIx64 ": diff end addr for %s v: %#" PRIx64 " k: %#" PRIx64 "\n",
Arnaldo Carvalho de Melo1c6a8002010-04-29 18:58:32 -0300152 sym->start, sym->name, sym->end, pair->end);
153 } else {
Arnaldo Carvalho de Melod3678752010-12-21 23:38:37 -0200154 struct rb_node *nnd;
155detour:
156 nnd = backwards ? rb_prev(&pair->rb_node) :
157 rb_next(&pair->rb_node);
Arnaldo Carvalho de Melo1c6a8002010-04-29 18:58:32 -0300158 if (nnd) {
159 struct symbol *next = rb_entry(nnd, struct symbol, rb_node);
160
161 if (next->start == sym->start) {
162 pair = next;
163 goto next_pair;
164 }
165 }
Arnaldo Carvalho de Melod3678752010-12-21 23:38:37 -0200166
167 if (backwards) {
168 backwards = false;
169 pair = first_pair;
170 goto detour;
171 }
172
Arnaldo Carvalho de Melo9486aa32011-01-22 20:37:02 -0200173 pr_debug("%#" PRIx64 ": diff name v: %s k: %s\n",
Arnaldo Carvalho de Melo1c6a8002010-04-29 18:58:32 -0300174 sym->start, sym->name, pair->name);
175 }
176 } else
Arnaldo Carvalho de Melo9486aa32011-01-22 20:37:02 -0200177 pr_debug("%#" PRIx64 ": %s not on kallsyms\n", sym->start, sym->name);
Arnaldo Carvalho de Melo1c6a8002010-04-29 18:58:32 -0300178
179 err = -1;
180 }
181
182 if (!verbose)
183 goto out;
184
185 pr_info("Maps only in vmlinux:\n");
186
187 for (nd = rb_first(&vmlinux.kmaps.maps[type]); nd; nd = rb_next(nd)) {
188 struct map *pos = rb_entry(nd, struct map, rb_node), *pair;
189 /*
190 * If it is the kernel, kallsyms is always "[kernel.kallsyms]", while
191 * the kernel will have the path for the vmlinux file being used,
192 * so use the short name, less descriptive but the same ("[kernel]" in
193 * both cases.
194 */
195 pair = map_groups__find_by_name(&kallsyms.kmaps, type,
196 (pos->dso->kernel ?
197 pos->dso->short_name :
198 pos->dso->name));
199 if (pair)
200 pair->priv = 1;
201 else
202 map__fprintf(pos, stderr);
203 }
204
205 pr_info("Maps in vmlinux with a different name in kallsyms:\n");
206
207 for (nd = rb_first(&vmlinux.kmaps.maps[type]); nd; nd = rb_next(nd)) {
208 struct map *pos = rb_entry(nd, struct map, rb_node), *pair;
209
210 pair = map_groups__find(&kallsyms.kmaps, type, pos->start);
211 if (pair == NULL || pair->priv)
212 continue;
213
214 if (pair->start == pos->start) {
215 pair->priv = 1;
Arnaldo Carvalho de Melo9486aa32011-01-22 20:37:02 -0200216 pr_info(" %" PRIx64 "-%" PRIx64 " %" PRIx64 " %s in kallsyms as",
Arnaldo Carvalho de Melo1c6a8002010-04-29 18:58:32 -0300217 pos->start, pos->end, pos->pgoff, pos->dso->name);
218 if (pos->pgoff != pair->pgoff || pos->end != pair->end)
Arnaldo Carvalho de Melo9486aa32011-01-22 20:37:02 -0200219 pr_info(": \n*%" PRIx64 "-%" PRIx64 " %" PRIx64 "",
Arnaldo Carvalho de Melo1c6a8002010-04-29 18:58:32 -0300220 pair->start, pair->end, pair->pgoff);
221 pr_info(" %s\n", pair->dso->name);
222 pair->priv = 1;
223 }
224 }
225
226 pr_info("Maps only in kallsyms:\n");
227
228 for (nd = rb_first(&kallsyms.kmaps.maps[type]);
229 nd; nd = rb_next(nd)) {
230 struct map *pos = rb_entry(nd, struct map, rb_node);
231
232 if (!pos->priv)
233 map__fprintf(pos, stderr);
234 }
235out:
236 return err;
237}
238
Arnaldo Carvalho de Melo02522082011-01-04 11:55:27 -0200239#include "util/cpumap.h"
Arnaldo Carvalho de Melod8548612011-01-04 00:16:20 -0200240#include "util/evsel.h"
241#include <sys/types.h>
242
Arnaldo Carvalho de Melode5fa3a2011-01-15 10:42:46 -0200243static int trace_event__id(const char *evname)
Arnaldo Carvalho de Melod8548612011-01-04 00:16:20 -0200244{
245 char *filename;
246 int err = -1, fd;
247
248 if (asprintf(&filename,
249 "/sys/kernel/debug/tracing/events/syscalls/%s/id",
Arnaldo Carvalho de Melode5fa3a2011-01-15 10:42:46 -0200250 evname) < 0)
Arnaldo Carvalho de Melod8548612011-01-04 00:16:20 -0200251 return -1;
252
253 fd = open(filename, O_RDONLY);
254 if (fd >= 0) {
255 char id[16];
256 if (read(fd, id, sizeof(id)) > 0)
257 err = atoi(id);
258 close(fd);
259 }
260
261 free(filename);
262 return err;
263}
264
265static int test__open_syscall_event(void)
266{
267 int err = -1, fd;
268 struct thread_map *threads;
269 struct perf_evsel *evsel;
Lin Ming23a2f3a2011-01-07 11:11:09 +0800270 struct perf_event_attr attr;
Arnaldo Carvalho de Melod8548612011-01-04 00:16:20 -0200271 unsigned int nr_open_calls = 111, i;
272 int id = trace_event__id("sys_enter_open");
273
274 if (id < 0) {
Arnaldo Carvalho de Melo454a3bb2011-01-04 10:40:08 -0200275 pr_debug("is debugfs mounted on /sys/kernel/debug?\n");
Arnaldo Carvalho de Melod8548612011-01-04 00:16:20 -0200276 return -1;
277 }
278
279 threads = thread_map__new(-1, getpid());
280 if (threads == NULL) {
Arnaldo Carvalho de Melo454a3bb2011-01-04 10:40:08 -0200281 pr_debug("thread_map__new\n");
Arnaldo Carvalho de Melod8548612011-01-04 00:16:20 -0200282 return -1;
283 }
284
Lin Ming23a2f3a2011-01-07 11:11:09 +0800285 memset(&attr, 0, sizeof(attr));
286 attr.type = PERF_TYPE_TRACEPOINT;
287 attr.config = id;
288 evsel = perf_evsel__new(&attr, 0);
Arnaldo Carvalho de Melod8548612011-01-04 00:16:20 -0200289 if (evsel == NULL) {
Arnaldo Carvalho de Melo454a3bb2011-01-04 10:40:08 -0200290 pr_debug("perf_evsel__new\n");
Arnaldo Carvalho de Melod8548612011-01-04 00:16:20 -0200291 goto out_thread_map_delete;
292 }
293
Arnaldo Carvalho de Melo9d04f172011-01-12 00:08:18 -0200294 if (perf_evsel__open_per_thread(evsel, threads, false, false) < 0) {
Arnaldo Carvalho de Melo454a3bb2011-01-04 10:40:08 -0200295 pr_debug("failed to open counter: %s, "
296 "tweak /proc/sys/kernel/perf_event_paranoid?\n",
297 strerror(errno));
Arnaldo Carvalho de Melod8548612011-01-04 00:16:20 -0200298 goto out_evsel_delete;
299 }
300
301 for (i = 0; i < nr_open_calls; ++i) {
302 fd = open("/etc/passwd", O_RDONLY);
303 close(fd);
304 }
305
306 if (perf_evsel__read_on_cpu(evsel, 0, 0) < 0) {
Arnaldo Carvalho de Melo454a3bb2011-01-04 10:40:08 -0200307 pr_debug("perf_evsel__open_read_on_cpu\n");
Arnaldo Carvalho de Melod8548612011-01-04 00:16:20 -0200308 goto out_close_fd;
309 }
310
Arnaldo Carvalho de Melo454a3bb2011-01-04 10:40:08 -0200311 if (evsel->counts->cpu[0].val != nr_open_calls) {
Arnaldo Carvalho de Melo9486aa32011-01-22 20:37:02 -0200312 pr_debug("perf_evsel__read_on_cpu: expected to intercept %d calls, got %" PRIu64 "\n",
Arnaldo Carvalho de Melod8548612011-01-04 00:16:20 -0200313 nr_open_calls, evsel->counts->cpu[0].val);
Arnaldo Carvalho de Melo454a3bb2011-01-04 10:40:08 -0200314 goto out_close_fd;
315 }
Arnaldo Carvalho de Melod8548612011-01-04 00:16:20 -0200316
317 err = 0;
318out_close_fd:
319 perf_evsel__close_fd(evsel, 1, threads->nr);
320out_evsel_delete:
321 perf_evsel__delete(evsel);
322out_thread_map_delete:
323 thread_map__delete(threads);
324 return err;
325}
326
Arnaldo Carvalho de Melo02522082011-01-04 11:55:27 -0200327#include <sched.h>
328
329static int test__open_syscall_event_on_all_cpus(void)
330{
331 int err = -1, fd, cpu;
332 struct thread_map *threads;
333 struct cpu_map *cpus;
334 struct perf_evsel *evsel;
335 struct perf_event_attr attr;
336 unsigned int nr_open_calls = 111, i;
Arnaldo Carvalho de Melo57b84e52011-01-22 23:14:20 -0200337 cpu_set_t cpu_set;
Arnaldo Carvalho de Melo02522082011-01-04 11:55:27 -0200338 int id = trace_event__id("sys_enter_open");
339
340 if (id < 0) {
341 pr_debug("is debugfs mounted on /sys/kernel/debug?\n");
342 return -1;
343 }
344
345 threads = thread_map__new(-1, getpid());
346 if (threads == NULL) {
347 pr_debug("thread_map__new\n");
348 return -1;
349 }
350
351 cpus = cpu_map__new(NULL);
Han Pingtian98d77b72011-01-15 07:00:50 +0800352 if (cpus == NULL) {
353 pr_debug("cpu_map__new\n");
354 goto out_thread_map_delete;
Arnaldo Carvalho de Melo02522082011-01-04 11:55:27 -0200355 }
356
Arnaldo Carvalho de Melo02522082011-01-04 11:55:27 -0200357
Arnaldo Carvalho de Melo57b84e52011-01-22 23:14:20 -0200358 CPU_ZERO(&cpu_set);
Arnaldo Carvalho de Melo02522082011-01-04 11:55:27 -0200359
360 memset(&attr, 0, sizeof(attr));
361 attr.type = PERF_TYPE_TRACEPOINT;
362 attr.config = id;
363 evsel = perf_evsel__new(&attr, 0);
364 if (evsel == NULL) {
365 pr_debug("perf_evsel__new\n");
Arnaldo Carvalho de Melo57b84e52011-01-22 23:14:20 -0200366 goto out_thread_map_delete;
Arnaldo Carvalho de Melo02522082011-01-04 11:55:27 -0200367 }
368
Arnaldo Carvalho de Melo9d04f172011-01-12 00:08:18 -0200369 if (perf_evsel__open(evsel, cpus, threads, false, false) < 0) {
Arnaldo Carvalho de Melo02522082011-01-04 11:55:27 -0200370 pr_debug("failed to open counter: %s, "
371 "tweak /proc/sys/kernel/perf_event_paranoid?\n",
372 strerror(errno));
373 goto out_evsel_delete;
374 }
375
376 for (cpu = 0; cpu < cpus->nr; ++cpu) {
377 unsigned int ncalls = nr_open_calls + cpu;
Arnaldo Carvalho de Melo57b84e52011-01-22 23:14:20 -0200378 /*
379 * XXX eventually lift this restriction in a way that
380 * keeps perf building on older glibc installations
381 * without CPU_ALLOC. 1024 cpus in 2010 still seems
382 * a reasonable upper limit tho :-)
383 */
384 if (cpus->map[cpu] >= CPU_SETSIZE) {
385 pr_debug("Ignoring CPU %d\n", cpus->map[cpu]);
386 continue;
387 }
Arnaldo Carvalho de Melo02522082011-01-04 11:55:27 -0200388
Arnaldo Carvalho de Melo57b84e52011-01-22 23:14:20 -0200389 CPU_SET(cpus->map[cpu], &cpu_set);
390 if (sched_setaffinity(0, sizeof(cpu_set), &cpu_set) < 0) {
Han Pingtianffb5e0f2011-01-20 19:47:07 +0800391 pr_debug("sched_setaffinity() failed on CPU %d: %s ",
392 cpus->map[cpu],
393 strerror(errno));
394 goto out_close_fd;
395 }
Arnaldo Carvalho de Melo02522082011-01-04 11:55:27 -0200396 for (i = 0; i < ncalls; ++i) {
397 fd = open("/etc/passwd", O_RDONLY);
398 close(fd);
399 }
Arnaldo Carvalho de Melo57b84e52011-01-22 23:14:20 -0200400 CPU_CLR(cpus->map[cpu], &cpu_set);
Arnaldo Carvalho de Melo02522082011-01-04 11:55:27 -0200401 }
402
403 /*
404 * Here we need to explicitely preallocate the counts, as if
405 * we use the auto allocation it will allocate just for 1 cpu,
406 * as we start by cpu 0.
407 */
408 if (perf_evsel__alloc_counts(evsel, cpus->nr) < 0) {
409 pr_debug("perf_evsel__alloc_counts(ncpus=%d)\n", cpus->nr);
410 goto out_close_fd;
411 }
412
Arnaldo Carvalho de Melod2af9682011-01-14 16:24:49 -0200413 err = 0;
414
Arnaldo Carvalho de Melo02522082011-01-04 11:55:27 -0200415 for (cpu = 0; cpu < cpus->nr; ++cpu) {
416 unsigned int expected;
417
Arnaldo Carvalho de Melo57b84e52011-01-22 23:14:20 -0200418 if (cpus->map[cpu] >= CPU_SETSIZE)
419 continue;
420
Arnaldo Carvalho de Melo02522082011-01-04 11:55:27 -0200421 if (perf_evsel__read_on_cpu(evsel, cpu, 0) < 0) {
422 pr_debug("perf_evsel__open_read_on_cpu\n");
Arnaldo Carvalho de Melod2af9682011-01-14 16:24:49 -0200423 err = -1;
424 break;
Arnaldo Carvalho de Melo02522082011-01-04 11:55:27 -0200425 }
426
427 expected = nr_open_calls + cpu;
428 if (evsel->counts->cpu[cpu].val != expected) {
Arnaldo Carvalho de Melo9486aa32011-01-22 20:37:02 -0200429 pr_debug("perf_evsel__read_on_cpu: expected to intercept %d calls on cpu %d, got %" PRIu64 "\n",
Han Pingtianffb5e0f2011-01-20 19:47:07 +0800430 expected, cpus->map[cpu], evsel->counts->cpu[cpu].val);
Arnaldo Carvalho de Melod2af9682011-01-14 16:24:49 -0200431 err = -1;
Arnaldo Carvalho de Melo02522082011-01-04 11:55:27 -0200432 }
433 }
434
Arnaldo Carvalho de Melo02522082011-01-04 11:55:27 -0200435out_close_fd:
436 perf_evsel__close_fd(evsel, 1, threads->nr);
437out_evsel_delete:
438 perf_evsel__delete(evsel);
Arnaldo Carvalho de Melo02522082011-01-04 11:55:27 -0200439out_thread_map_delete:
440 thread_map__delete(threads);
441 return err;
442}
443
Arnaldo Carvalho de Melode5fa3a2011-01-15 10:42:46 -0200444/*
445 * This test will generate random numbers of calls to some getpid syscalls,
446 * then establish an mmap for a group of events that are created to monitor
447 * the syscalls.
448 *
449 * It will receive the events, using mmap, use its PERF_SAMPLE_ID generated
450 * sample.id field to map back to its respective perf_evsel instance.
451 *
452 * Then it checks if the number of syscalls reported as perf events by
453 * the kernel corresponds to the number of syscalls made.
454 */
455static int test__basic_mmap(void)
456{
457 int err = -1;
458 event_t *event;
459 struct thread_map *threads;
460 struct perf_session session;
461 struct cpu_map *cpus;
462 struct perf_evlist *evlist;
463 struct perf_event_attr attr = {
464 .type = PERF_TYPE_TRACEPOINT,
465 .read_format = PERF_FORMAT_ID,
466 .sample_type = PERF_SAMPLE_ID,
467 .watermark = 0,
468 };
469 cpu_set_t cpu_set;
470 const char *syscall_names[] = { "getsid", "getppid", "getpgrp",
471 "getpgid", };
472 pid_t (*syscalls[])(void) = { (void *)getsid, getppid, getpgrp,
473 (void*)getpgid };
474#define nsyscalls ARRAY_SIZE(syscall_names)
475 int ids[nsyscalls];
476 unsigned int nr_events[nsyscalls],
477 expected_nr_events[nsyscalls], i, j;
478 struct perf_evsel *evsels[nsyscalls], *evsel;
479
480 for (i = 0; i < nsyscalls; ++i) {
481 char name[64];
482
483 snprintf(name, sizeof(name), "sys_enter_%s", syscall_names[i]);
484 ids[i] = trace_event__id(name);
485 if (ids[i] < 0) {
486 pr_debug("Is debugfs mounted on /sys/kernel/debug?\n");
487 return -1;
488 }
489 nr_events[i] = 0;
490 expected_nr_events[i] = random() % 257;
491 }
492
493 threads = thread_map__new(-1, getpid());
494 if (threads == NULL) {
495 pr_debug("thread_map__new\n");
496 return -1;
497 }
498
499 cpus = cpu_map__new(NULL);
500 if (threads == NULL) {
501 pr_debug("thread_map__new\n");
502 goto out_free_threads;
503 }
504
505 CPU_ZERO(&cpu_set);
506 CPU_SET(cpus->map[0], &cpu_set);
507 sched_setaffinity(0, sizeof(cpu_set), &cpu_set);
508 if (sched_setaffinity(0, sizeof(cpu_set), &cpu_set) < 0) {
509 pr_debug("sched_setaffinity() failed on CPU %d: %s ",
510 cpus->map[0], strerror(errno));
511 goto out_free_cpus;
512 }
513
514 evlist = perf_evlist__new();
515 if (threads == NULL) {
516 pr_debug("perf_evlist__new\n");
517 goto out_free_cpus;
518 }
519
520 /* anonymous union fields, can't be initialized above */
521 attr.wakeup_events = 1;
522 attr.sample_period = 1;
523
524 /*
525 * FIXME: use evsel->attr.sample_type in event__parse_sample.
526 * This will nicely remove the requirement that we have
527 * all the events with the same sample_type.
528 */
529 session.sample_type = attr.sample_type;
530
531 for (i = 0; i < nsyscalls; ++i) {
532 attr.config = ids[i];
533 evsels[i] = perf_evsel__new(&attr, i);
534 if (evsels[i] == NULL) {
535 pr_debug("perf_evsel__new\n");
536 goto out_free_evlist;
537 }
538
539 perf_evlist__add(evlist, evsels[i]);
540
541 if (perf_evsel__open(evsels[i], cpus, threads, false, false) < 0) {
542 pr_debug("failed to open counter: %s, "
543 "tweak /proc/sys/kernel/perf_event_paranoid?\n",
544 strerror(errno));
545 goto out_close_fd;
546 }
547 }
548
549 if (perf_evlist__mmap(evlist, cpus, threads, 128, true) < 0) {
550 pr_debug("failed to mmap events: %d (%s)\n", errno,
551 strerror(errno));
552 goto out_close_fd;
553 }
554
555 for (i = 0; i < nsyscalls; ++i)
556 for (j = 0; j < expected_nr_events[i]; ++j) {
557 int foo = syscalls[i]();
558 ++foo;
559 }
560
561 while ((event = perf_evlist__read_on_cpu(evlist, 0)) != NULL) {
562 struct sample_data sample;
563
564 if (event->header.type != PERF_RECORD_SAMPLE) {
565 pr_debug("unexpected %s event\n",
566 event__get_event_name(event->header.type));
567 goto out_munmap;
568 }
569
570 event__parse_sample(event, &session, &sample);
571 evsel = perf_evlist__id2evsel(evlist, sample.id);
572 if (evsel == NULL) {
573 pr_debug("event with id %" PRIu64
574 " doesn't map to an evsel\n", sample.id);
575 goto out_munmap;
576 }
577 nr_events[evsel->idx]++;
578 }
579
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 event_name(evsel), nr_events[evsel->idx]);
585 goto out_munmap;
586 }
587 }
588
589 err = 0;
590out_munmap:
591 perf_evlist__munmap(evlist, 1);
592out_close_fd:
593 for (i = 0; i < nsyscalls; ++i)
594 perf_evsel__close_fd(evsels[i], 1, threads->nr);
595out_free_evlist:
596 perf_evlist__delete(evlist);
597out_free_cpus:
598 cpu_map__delete(cpus);
599out_free_threads:
600 thread_map__delete(threads);
601 return err;
602#undef nsyscalls
603}
604
Arnaldo Carvalho de Melo1c6a8002010-04-29 18:58:32 -0300605static struct test {
606 const char *desc;
607 int (*func)(void);
608} tests[] = {
609 {
610 .desc = "vmlinux symtab matches kallsyms",
611 .func = test__vmlinux_matches_kallsyms,
612 },
613 {
Arnaldo Carvalho de Melod8548612011-01-04 00:16:20 -0200614 .desc = "detect open syscall event",
615 .func = test__open_syscall_event,
616 },
617 {
Arnaldo Carvalho de Melo02522082011-01-04 11:55:27 -0200618 .desc = "detect open syscall event on all cpus",
619 .func = test__open_syscall_event_on_all_cpus,
620 },
621 {
Arnaldo Carvalho de Melode5fa3a2011-01-15 10:42:46 -0200622 .desc = "read samples using the mmap interface",
623 .func = test__basic_mmap,
624 },
625 {
Arnaldo Carvalho de Melo1c6a8002010-04-29 18:58:32 -0300626 .func = NULL,
627 },
628};
629
630static int __cmd_test(void)
631{
632 int i = 0;
633
634 page_size = sysconf(_SC_PAGE_SIZE);
635
636 while (tests[i].func) {
637 int err;
638 pr_info("%2d: %s:", i + 1, tests[i].desc);
639 pr_debug("\n--- start ---\n");
640 err = tests[i].func();
641 pr_debug("---- end ----\n%s:", tests[i].desc);
642 pr_info(" %s\n", err ? "FAILED!\n" : "Ok");
643 ++i;
644 }
645
646 return 0;
647}
648
649static const char * const test_usage[] = {
650 "perf test [<options>]",
651 NULL,
652};
653
654static const struct option test_options[] = {
Arnaldo Carvalho de Melo80354582010-05-17 15:51:10 -0300655 OPT_INTEGER('v', "verbose", &verbose,
Arnaldo Carvalho de Melo1c6a8002010-04-29 18:58:32 -0300656 "be more verbose (show symbol address, etc)"),
657 OPT_END()
658};
659
660int cmd_test(int argc, const char **argv, const char *prefix __used)
661{
662 argc = parse_options(argc, argv, test_options, test_usage, 0);
663 if (argc)
664 usage_with_options(test_usage, test_options);
665
666 symbol_conf.priv_size = sizeof(int);
667 symbol_conf.sort_by_name = true;
668 symbol_conf.try_vmlinux_path = true;
669
670 if (symbol__init() < 0)
671 return -1;
672
673 setup_pager();
674
675 return __cmd_test();
676}