mmap-basic.c 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. /* For the CLR_() macros */
  2. #include <pthread.h>
  3. #include "evlist.h"
  4. #include "evsel.h"
  5. #include "thread_map.h"
  6. #include "cpumap.h"
  7. #include "tests.h"
  8. #include <linux/err.h>
  9. /*
  10. * This test will generate random numbers of calls to some getpid syscalls,
  11. * then establish an mmap for a group of events that are created to monitor
  12. * the syscalls.
  13. *
  14. * It will receive the events, using mmap, use its PERF_SAMPLE_ID generated
  15. * sample.id field to map back to its respective perf_evsel instance.
  16. *
  17. * Then it checks if the number of syscalls reported as perf events by
  18. * the kernel corresponds to the number of syscalls made.
  19. */
  20. int test__basic_mmap(int subtest __maybe_unused)
  21. {
  22. int err = -1;
  23. union perf_event *event;
  24. struct thread_map *threads;
  25. struct cpu_map *cpus;
  26. struct perf_evlist *evlist;
  27. cpu_set_t cpu_set;
  28. const char *syscall_names[] = { "getsid", "getppid", "getpgid", };
  29. pid_t (*syscalls[])(void) = { (void *)getsid, getppid, (void*)getpgid };
  30. #define nsyscalls ARRAY_SIZE(syscall_names)
  31. unsigned int nr_events[nsyscalls],
  32. expected_nr_events[nsyscalls], i, j;
  33. struct perf_evsel *evsels[nsyscalls], *evsel;
  34. char sbuf[STRERR_BUFSIZE];
  35. threads = thread_map__new(-1, getpid(), UINT_MAX);
  36. if (threads == NULL) {
  37. pr_debug("thread_map__new\n");
  38. return -1;
  39. }
  40. cpus = cpu_map__new(NULL);
  41. if (cpus == NULL) {
  42. pr_debug("cpu_map__new\n");
  43. goto out_free_threads;
  44. }
  45. CPU_ZERO(&cpu_set);
  46. CPU_SET(cpus->map[0], &cpu_set);
  47. sched_setaffinity(0, sizeof(cpu_set), &cpu_set);
  48. if (sched_setaffinity(0, sizeof(cpu_set), &cpu_set) < 0) {
  49. pr_debug("sched_setaffinity() failed on CPU %d: %s ",
  50. cpus->map[0], str_error_r(errno, sbuf, sizeof(sbuf)));
  51. goto out_free_cpus;
  52. }
  53. evlist = perf_evlist__new();
  54. if (evlist == NULL) {
  55. pr_debug("perf_evlist__new\n");
  56. goto out_free_cpus;
  57. }
  58. perf_evlist__set_maps(evlist, cpus, threads);
  59. for (i = 0; i < nsyscalls; ++i) {
  60. char name[64];
  61. snprintf(name, sizeof(name), "sys_enter_%s", syscall_names[i]);
  62. evsels[i] = perf_evsel__newtp("syscalls", name);
  63. if (IS_ERR(evsels[i])) {
  64. pr_debug("perf_evsel__new\n");
  65. goto out_delete_evlist;
  66. }
  67. evsels[i]->attr.wakeup_events = 1;
  68. perf_evsel__set_sample_id(evsels[i], false);
  69. perf_evlist__add(evlist, evsels[i]);
  70. if (perf_evsel__open(evsels[i], cpus, threads) < 0) {
  71. pr_debug("failed to open counter: %s, "
  72. "tweak /proc/sys/kernel/perf_event_paranoid?\n",
  73. str_error_r(errno, sbuf, sizeof(sbuf)));
  74. goto out_delete_evlist;
  75. }
  76. nr_events[i] = 0;
  77. expected_nr_events[i] = 1 + rand() % 127;
  78. }
  79. if (perf_evlist__mmap(evlist, 128, true) < 0) {
  80. pr_debug("failed to mmap events: %d (%s)\n", errno,
  81. str_error_r(errno, sbuf, sizeof(sbuf)));
  82. goto out_delete_evlist;
  83. }
  84. for (i = 0; i < nsyscalls; ++i)
  85. for (j = 0; j < expected_nr_events[i]; ++j) {
  86. int foo = syscalls[i]();
  87. ++foo;
  88. }
  89. while ((event = perf_evlist__mmap_read(evlist, 0)) != NULL) {
  90. struct perf_sample sample;
  91. if (event->header.type != PERF_RECORD_SAMPLE) {
  92. pr_debug("unexpected %s event\n",
  93. perf_event__name(event->header.type));
  94. goto out_delete_evlist;
  95. }
  96. err = perf_evlist__parse_sample(evlist, event, &sample);
  97. if (err) {
  98. pr_err("Can't parse sample, err = %d\n", err);
  99. goto out_delete_evlist;
  100. }
  101. err = -1;
  102. evsel = perf_evlist__id2evsel(evlist, sample.id);
  103. if (evsel == NULL) {
  104. pr_debug("event with id %" PRIu64
  105. " doesn't map to an evsel\n", sample.id);
  106. goto out_delete_evlist;
  107. }
  108. nr_events[evsel->idx]++;
  109. perf_evlist__mmap_consume(evlist, 0);
  110. }
  111. err = 0;
  112. evlist__for_each_entry(evlist, evsel) {
  113. if (nr_events[evsel->idx] != expected_nr_events[evsel->idx]) {
  114. pr_debug("expected %d %s events, got %d\n",
  115. expected_nr_events[evsel->idx],
  116. perf_evsel__name(evsel), nr_events[evsel->idx]);
  117. err = -1;
  118. goto out_delete_evlist;
  119. }
  120. }
  121. out_delete_evlist:
  122. perf_evlist__delete(evlist);
  123. cpus = NULL;
  124. threads = NULL;
  125. out_free_cpus:
  126. cpu_map__put(cpus);
  127. out_free_threads:
  128. thread_map__put(threads);
  129. return err;
  130. }