diff options
Diffstat (limited to 'tools/perf/builtin-test.c')
-rw-r--r-- | tools/perf/builtin-test.c | 184 |
1 files changed, 172 insertions, 12 deletions
diff --git a/tools/perf/builtin-test.c b/tools/perf/builtin-test.c index 5dcdba653d70..1b2106c58f66 100644 --- a/tools/perf/builtin-test.c +++ b/tools/perf/builtin-test.c | |||
@@ -7,10 +7,11 @@ | |||
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" |
11 | #include "util/session.h" | 12 | #include "util/parse-events.h" |
12 | #include "util/symbol.h" | 13 | #include "util/symbol.h" |
13 | #include "util/thread.h" | 14 | #include "util/thread_map.h" |
14 | 15 | ||
15 | static long page_size; | 16 | static long page_size; |
16 | 17 | ||
@@ -238,14 +239,14 @@ out: | |||
238 | #include "util/evsel.h" | 239 | #include "util/evsel.h" |
239 | #include <sys/types.h> | 240 | #include <sys/types.h> |
240 | 241 | ||
241 | static int trace_event__id(const char *event_name) | 242 | static int trace_event__id(const char *evname) |
242 | { | 243 | { |
243 | char *filename; | 244 | char *filename; |
244 | int err = -1, fd; | 245 | int err = -1, fd; |
245 | 246 | ||
246 | if (asprintf(&filename, | 247 | if (asprintf(&filename, |
247 | "/sys/kernel/debug/tracing/events/syscalls/%s/id", | 248 | "/sys/kernel/debug/tracing/events/syscalls/%s/id", |
248 | event_name) < 0) | 249 | evname) < 0) |
249 | return -1; | 250 | return -1; |
250 | 251 | ||
251 | fd = open(filename, O_RDONLY); | 252 | fd = open(filename, O_RDONLY); |
@@ -289,7 +290,7 @@ static int test__open_syscall_event(void) | |||
289 | goto out_thread_map_delete; | 290 | goto out_thread_map_delete; |
290 | } | 291 | } |
291 | 292 | ||
292 | if (perf_evsel__open_per_thread(evsel, threads) < 0) { | 293 | if (perf_evsel__open_per_thread(evsel, threads, false, false) < 0) { |
293 | pr_debug("failed to open counter: %s, " | 294 | pr_debug("failed to open counter: %s, " |
294 | "tweak /proc/sys/kernel/perf_event_paranoid?\n", | 295 | "tweak /proc/sys/kernel/perf_event_paranoid?\n", |
295 | strerror(errno)); | 296 | strerror(errno)); |
@@ -347,9 +348,9 @@ static int test__open_syscall_event_on_all_cpus(void) | |||
347 | } | 348 | } |
348 | 349 | ||
349 | cpus = cpu_map__new(NULL); | 350 | cpus = cpu_map__new(NULL); |
350 | if (threads == NULL) { | 351 | if (cpus == NULL) { |
351 | pr_debug("thread_map__new\n"); | 352 | pr_debug("cpu_map__new\n"); |
352 | return -1; | 353 | goto out_thread_map_delete; |
353 | } | 354 | } |
354 | 355 | ||
355 | 356 | ||
@@ -364,7 +365,7 @@ static int test__open_syscall_event_on_all_cpus(void) | |||
364 | goto out_thread_map_delete; | 365 | goto out_thread_map_delete; |
365 | } | 366 | } |
366 | 367 | ||
367 | if (perf_evsel__open(evsel, cpus, threads) < 0) { | 368 | if (perf_evsel__open(evsel, cpus, threads, false, false) < 0) { |
368 | pr_debug("failed to open counter: %s, " | 369 | pr_debug("failed to open counter: %s, " |
369 | "tweak /proc/sys/kernel/perf_event_paranoid?\n", | 370 | "tweak /proc/sys/kernel/perf_event_paranoid?\n", |
370 | strerror(errno)); | 371 | strerror(errno)); |
@@ -408,6 +409,8 @@ static int test__open_syscall_event_on_all_cpus(void) | |||
408 | goto out_close_fd; | 409 | goto out_close_fd; |
409 | } | 410 | } |
410 | 411 | ||
412 | err = 0; | ||
413 | |||
411 | for (cpu = 0; cpu < cpus->nr; ++cpu) { | 414 | for (cpu = 0; cpu < cpus->nr; ++cpu) { |
412 | unsigned int expected; | 415 | unsigned int expected; |
413 | 416 | ||
@@ -416,18 +419,18 @@ static int test__open_syscall_event_on_all_cpus(void) | |||
416 | 419 | ||
417 | if (perf_evsel__read_on_cpu(evsel, cpu, 0) < 0) { | 420 | if (perf_evsel__read_on_cpu(evsel, cpu, 0) < 0) { |
418 | pr_debug("perf_evsel__open_read_on_cpu\n"); | 421 | pr_debug("perf_evsel__open_read_on_cpu\n"); |
419 | goto out_close_fd; | 422 | err = -1; |
423 | break; | ||
420 | } | 424 | } |
421 | 425 | ||
422 | expected = nr_open_calls + cpu; | 426 | expected = nr_open_calls + cpu; |
423 | if (evsel->counts->cpu[cpu].val != expected) { | 427 | if (evsel->counts->cpu[cpu].val != expected) { |
424 | pr_debug("perf_evsel__read_on_cpu: expected to intercept %d calls on cpu %d, got %" PRIu64 "\n", | 428 | pr_debug("perf_evsel__read_on_cpu: expected to intercept %d calls on cpu %d, got %" PRIu64 "\n", |
425 | expected, cpus->map[cpu], evsel->counts->cpu[cpu].val); | 429 | expected, cpus->map[cpu], evsel->counts->cpu[cpu].val); |
426 | goto out_close_fd; | 430 | err = -1; |
427 | } | 431 | } |
428 | } | 432 | } |
429 | 433 | ||
430 | err = 0; | ||
431 | out_close_fd: | 434 | out_close_fd: |
432 | perf_evsel__close_fd(evsel, 1, threads->nr); | 435 | perf_evsel__close_fd(evsel, 1, threads->nr); |
433 | out_evsel_delete: | 436 | out_evsel_delete: |
@@ -437,6 +440,159 @@ out_thread_map_delete: | |||
437 | return err; | 440 | return err; |
438 | } | 441 | } |
439 | 442 | ||
443 | /* | ||
444 | * This test will generate random numbers of calls to some getpid syscalls, | ||
445 | * then establish an mmap for a group of events that are created to monitor | ||
446 | * the syscalls. | ||
447 | * | ||
448 | * It will receive the events, using mmap, use its PERF_SAMPLE_ID generated | ||
449 | * sample.id field to map back to its respective perf_evsel instance. | ||
450 | * | ||
451 | * Then it checks if the number of syscalls reported as perf events by | ||
452 | * the kernel corresponds to the number of syscalls made. | ||
453 | */ | ||
454 | static int test__basic_mmap(void) | ||
455 | { | ||
456 | int err = -1; | ||
457 | union perf_event *event; | ||
458 | struct thread_map *threads; | ||
459 | struct cpu_map *cpus; | ||
460 | struct perf_evlist *evlist; | ||
461 | struct perf_event_attr attr = { | ||
462 | .type = PERF_TYPE_TRACEPOINT, | ||
463 | .read_format = PERF_FORMAT_ID, | ||
464 | .sample_type = PERF_SAMPLE_ID, | ||
465 | .watermark = 0, | ||
466 | }; | ||
467 | cpu_set_t cpu_set; | ||
468 | const char *syscall_names[] = { "getsid", "getppid", "getpgrp", | ||
469 | "getpgid", }; | ||
470 | pid_t (*syscalls[])(void) = { (void *)getsid, getppid, getpgrp, | ||
471 | (void*)getpgid }; | ||
472 | #define nsyscalls ARRAY_SIZE(syscall_names) | ||
473 | int ids[nsyscalls]; | ||
474 | unsigned int nr_events[nsyscalls], | ||
475 | expected_nr_events[nsyscalls], i, j; | ||
476 | struct perf_evsel *evsels[nsyscalls], *evsel; | ||
477 | |||
478 | for (i = 0; i < nsyscalls; ++i) { | ||
479 | char name[64]; | ||
480 | |||
481 | snprintf(name, sizeof(name), "sys_enter_%s", syscall_names[i]); | ||
482 | ids[i] = trace_event__id(name); | ||
483 | if (ids[i] < 0) { | ||
484 | pr_debug("Is debugfs mounted on /sys/kernel/debug?\n"); | ||
485 | return -1; | ||
486 | } | ||
487 | nr_events[i] = 0; | ||
488 | expected_nr_events[i] = random() % 257; | ||
489 | } | ||
490 | |||
491 | threads = thread_map__new(-1, getpid()); | ||
492 | if (threads == NULL) { | ||
493 | pr_debug("thread_map__new\n"); | ||
494 | return -1; | ||
495 | } | ||
496 | |||
497 | cpus = cpu_map__new(NULL); | ||
498 | if (cpus == NULL) { | ||
499 | pr_debug("cpu_map__new\n"); | ||
500 | goto out_free_threads; | ||
501 | } | ||
502 | |||
503 | CPU_ZERO(&cpu_set); | ||
504 | CPU_SET(cpus->map[0], &cpu_set); | ||
505 | sched_setaffinity(0, sizeof(cpu_set), &cpu_set); | ||
506 | if (sched_setaffinity(0, sizeof(cpu_set), &cpu_set) < 0) { | ||
507 | pr_debug("sched_setaffinity() failed on CPU %d: %s ", | ||
508 | cpus->map[0], strerror(errno)); | ||
509 | goto out_free_cpus; | ||
510 | } | ||
511 | |||
512 | evlist = perf_evlist__new(cpus, threads); | ||
513 | if (evlist == NULL) { | ||
514 | pr_debug("perf_evlist__new\n"); | ||
515 | goto out_free_cpus; | ||
516 | } | ||
517 | |||
518 | /* anonymous union fields, can't be initialized above */ | ||
519 | attr.wakeup_events = 1; | ||
520 | attr.sample_period = 1; | ||
521 | |||
522 | for (i = 0; i < nsyscalls; ++i) { | ||
523 | attr.config = ids[i]; | ||
524 | evsels[i] = perf_evsel__new(&attr, i); | ||
525 | if (evsels[i] == NULL) { | ||
526 | pr_debug("perf_evsel__new\n"); | ||
527 | goto out_free_evlist; | ||
528 | } | ||
529 | |||
530 | perf_evlist__add(evlist, evsels[i]); | ||
531 | |||
532 | if (perf_evsel__open(evsels[i], cpus, threads, false, false) < 0) { | ||
533 | pr_debug("failed to open counter: %s, " | ||
534 | "tweak /proc/sys/kernel/perf_event_paranoid?\n", | ||
535 | strerror(errno)); | ||
536 | goto out_close_fd; | ||
537 | } | ||
538 | } | ||
539 | |||
540 | if (perf_evlist__mmap(evlist, 128, true) < 0) { | ||
541 | pr_debug("failed to mmap events: %d (%s)\n", errno, | ||
542 | strerror(errno)); | ||
543 | goto out_close_fd; | ||
544 | } | ||
545 | |||
546 | for (i = 0; i < nsyscalls; ++i) | ||
547 | for (j = 0; j < expected_nr_events[i]; ++j) { | ||
548 | int foo = syscalls[i](); | ||
549 | ++foo; | ||
550 | } | ||
551 | |||
552 | while ((event = perf_evlist__read_on_cpu(evlist, 0)) != NULL) { | ||
553 | struct perf_sample sample; | ||
554 | |||
555 | if (event->header.type != PERF_RECORD_SAMPLE) { | ||
556 | pr_debug("unexpected %s event\n", | ||
557 | perf_event__name(event->header.type)); | ||
558 | goto out_munmap; | ||
559 | } | ||
560 | |||
561 | perf_event__parse_sample(event, attr.sample_type, false, &sample); | ||
562 | evsel = perf_evlist__id2evsel(evlist, sample.id); | ||
563 | if (evsel == NULL) { | ||
564 | pr_debug("event with id %" PRIu64 | ||
565 | " doesn't map to an evsel\n", sample.id); | ||
566 | goto out_munmap; | ||
567 | } | ||
568 | nr_events[evsel->idx]++; | ||
569 | } | ||
570 | |||
571 | list_for_each_entry(evsel, &evlist->entries, node) { | ||
572 | if (nr_events[evsel->idx] != expected_nr_events[evsel->idx]) { | ||
573 | pr_debug("expected %d %s events, got %d\n", | ||
574 | expected_nr_events[evsel->idx], | ||
575 | event_name(evsel), nr_events[evsel->idx]); | ||
576 | goto out_munmap; | ||
577 | } | ||
578 | } | ||
579 | |||
580 | err = 0; | ||
581 | out_munmap: | ||
582 | perf_evlist__munmap(evlist); | ||
583 | out_close_fd: | ||
584 | for (i = 0; i < nsyscalls; ++i) | ||
585 | perf_evsel__close_fd(evsels[i], 1, threads->nr); | ||
586 | out_free_evlist: | ||
587 | perf_evlist__delete(evlist); | ||
588 | out_free_cpus: | ||
589 | cpu_map__delete(cpus); | ||
590 | out_free_threads: | ||
591 | thread_map__delete(threads); | ||
592 | return err; | ||
593 | #undef nsyscalls | ||
594 | } | ||
595 | |||
440 | static struct test { | 596 | static struct test { |
441 | const char *desc; | 597 | const char *desc; |
442 | int (*func)(void); | 598 | int (*func)(void); |
@@ -454,6 +610,10 @@ static struct test { | |||
454 | .func = test__open_syscall_event_on_all_cpus, | 610 | .func = test__open_syscall_event_on_all_cpus, |
455 | }, | 611 | }, |
456 | { | 612 | { |
613 | .desc = "read samples using the mmap interface", | ||
614 | .func = test__basic_mmap, | ||
615 | }, | ||
616 | { | ||
457 | .func = NULL, | 617 | .func = NULL, |
458 | }, | 618 | }, |
459 | }; | 619 | }; |