diff options
author | Arnaldo Carvalho de Melo <acme@redhat.com> | 2011-01-15 07:42:46 -0500 |
---|---|---|
committer | Arnaldo Carvalho de Melo <acme@redhat.com> | 2011-01-22 16:56:31 -0500 |
commit | de5fa3a8a05cd60f59622e88cfeb90416760d78e (patch) | |
tree | aeafb466ef056e17c8cf043e396e9e5fcf348a7e /tools/perf/builtin-test.c | |
parent | 04391debc3e1195222a4dbb162ace6542dd89c1c (diff) |
perf test: Add test for the evlist mmap routines
This test will generate random numbers of calls to some getpid syscalls,
then establish an mmap for a group of events that are created to monitor
these syscalls.
It will receive the events, using mmap, use its PERF_SAMPLE_ID generated
sample.id field to map back to its respective perf_evsel instance.
Then it checks if the number of syscalls reported as perf events by the
kernel corresponds to the number of syscalls made.
Cc: Frederic Weisbecker <fweisbec@gmail.com>
Cc: Ingo Molnar <mingo@elte.hu>
Cc: Mike Galbraith <efault@gmx.de>
Cc: Paul Mackerras <paulus@samba.org>
Cc: Peter Zijlstra <peterz@infradead.org>
Cc: Stephane Eranian <eranian@google.com>
Cc: Tom Zanussi <tzanussi@gmail.com>
LKML-Reference: <new-submission>
Signed-off-by: Arnaldo Carvalho de Melo <acme@redhat.com>
Diffstat (limited to 'tools/perf/builtin-test.c')
-rw-r--r-- | tools/perf/builtin-test.c | 171 |
1 files changed, 169 insertions, 2 deletions
diff --git a/tools/perf/builtin-test.c b/tools/perf/builtin-test.c index 5a50e4755e6c..4fd34537c01d 100644 --- a/tools/perf/builtin-test.c +++ b/tools/perf/builtin-test.c | |||
@@ -7,7 +7,9 @@ | |||
7 | 7 | ||
8 | #include "util/cache.h" | 8 | #include "util/cache.h" |
9 | #include "util/debug.h" | 9 | #include "util/debug.h" |
10 | #include "util/evlist.h" | ||
10 | #include "util/parse-options.h" | 11 | #include "util/parse-options.h" |
12 | #include "util/parse-events.h" | ||
11 | #include "util/session.h" | 13 | #include "util/session.h" |
12 | #include "util/symbol.h" | 14 | #include "util/symbol.h" |
13 | #include "util/thread.h" | 15 | #include "util/thread.h" |
@@ -238,14 +240,14 @@ out: | |||
238 | #include "util/evsel.h" | 240 | #include "util/evsel.h" |
239 | #include <sys/types.h> | 241 | #include <sys/types.h> |
240 | 242 | ||
241 | static int trace_event__id(const char *event_name) | 243 | static int trace_event__id(const char *evname) |
242 | { | 244 | { |
243 | char *filename; | 245 | char *filename; |
244 | int err = -1, fd; | 246 | int err = -1, fd; |
245 | 247 | ||
246 | if (asprintf(&filename, | 248 | if (asprintf(&filename, |
247 | "/sys/kernel/debug/tracing/events/syscalls/%s/id", | 249 | "/sys/kernel/debug/tracing/events/syscalls/%s/id", |
248 | event_name) < 0) | 250 | evname) < 0) |
249 | return -1; | 251 | return -1; |
250 | 252 | ||
251 | fd = open(filename, O_RDONLY); | 253 | fd = open(filename, O_RDONLY); |
@@ -439,6 +441,167 @@ out_thread_map_delete: | |||
439 | return err; | 441 | return err; |
440 | } | 442 | } |
441 | 443 | ||
444 | /* | ||
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 | */ | ||
455 | static 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; | ||
590 | out_munmap: | ||
591 | perf_evlist__munmap(evlist, 1); | ||
592 | out_close_fd: | ||
593 | for (i = 0; i < nsyscalls; ++i) | ||
594 | perf_evsel__close_fd(evsels[i], 1, threads->nr); | ||
595 | out_free_evlist: | ||
596 | perf_evlist__delete(evlist); | ||
597 | out_free_cpus: | ||
598 | cpu_map__delete(cpus); | ||
599 | out_free_threads: | ||
600 | thread_map__delete(threads); | ||
601 | return err; | ||
602 | #undef nsyscalls | ||
603 | } | ||
604 | |||
442 | static struct test { | 605 | static struct test { |
443 | const char *desc; | 606 | const char *desc; |
444 | int (*func)(void); | 607 | int (*func)(void); |
@@ -456,6 +619,10 @@ static struct test { | |||
456 | .func = test__open_syscall_event_on_all_cpus, | 619 | .func = test__open_syscall_event_on_all_cpus, |
457 | }, | 620 | }, |
458 | { | 621 | { |
622 | .desc = "read samples using the mmap interface", | ||
623 | .func = test__basic_mmap, | ||
624 | }, | ||
625 | { | ||
459 | .func = NULL, | 626 | .func = NULL, |
460 | }, | 627 | }, |
461 | }; | 628 | }; |