mmap-basic.c 4.2 KB

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