task-exit.c 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. // SPDX-License-Identifier: GPL-2.0
  2. #include "debug.h"
  3. #include "evlist.h"
  4. #include "evsel.h"
  5. #include "target.h"
  6. #include "thread_map.h"
  7. #include "tests.h"
  8. #include "util/mmap.h"
  9. #include <errno.h>
  10. #include <signal.h>
  11. #include <linux/string.h>
  12. #include <perf/cpumap.h>
  13. #include <perf/evlist.h>
  14. #include <perf/mmap.h>
  15. static int exited;
  16. static int nr_exit;
  17. static void sig_handler(int sig __maybe_unused)
  18. {
  19. exited = 1;
  20. }
  21. /*
  22. * evlist__prepare_workload will send a SIGUSR1 if the fork fails, since
  23. * we asked by setting its exec_error to this handler.
  24. */
  25. static void workload_exec_failed_signal(int signo __maybe_unused,
  26. siginfo_t *info __maybe_unused,
  27. void *ucontext __maybe_unused)
  28. {
  29. exited = 1;
  30. nr_exit = -1;
  31. }
  32. /*
  33. * This test will start a workload that does nothing then it checks
  34. * if the number of exit event reported by the kernel is 1 or not
  35. * in order to check the kernel returns correct number of event.
  36. */
  37. static int test__task_exit(struct test_suite *test __maybe_unused, int subtest __maybe_unused)
  38. {
  39. int err = -1;
  40. union perf_event *event;
  41. struct evsel *evsel;
  42. struct evlist *evlist;
  43. struct target target = {
  44. .uses_mmap = true,
  45. };
  46. const char *argv[] = { "true", NULL };
  47. char sbuf[STRERR_BUFSIZE];
  48. struct perf_cpu_map *cpus;
  49. struct perf_thread_map *threads;
  50. struct mmap *md;
  51. int retry_count = 0;
  52. signal(SIGCHLD, sig_handler);
  53. evlist = evlist__new_dummy();
  54. if (evlist == NULL) {
  55. pr_debug("evlist__new_dummy\n");
  56. return -1;
  57. }
  58. /*
  59. * Create maps of threads and cpus to monitor. In this case
  60. * we start with all threads and cpus (-1, -1) but then in
  61. * evlist__prepare_workload we'll fill in the only thread
  62. * we're monitoring, the one forked there.
  63. */
  64. cpus = perf_cpu_map__new_any_cpu();
  65. threads = thread_map__new_by_tid(-1);
  66. if (!cpus || !threads) {
  67. err = -ENOMEM;
  68. pr_debug("Not enough memory to create thread/cpu maps\n");
  69. goto out_delete_evlist;
  70. }
  71. perf_evlist__set_maps(&evlist->core, cpus, threads);
  72. err = evlist__prepare_workload(evlist, &target, argv, false, workload_exec_failed_signal);
  73. if (err < 0) {
  74. pr_debug("Couldn't run the workload!\n");
  75. goto out_delete_evlist;
  76. }
  77. evsel = evlist__first(evlist);
  78. evsel->core.attr.task = 1;
  79. #ifdef __s390x__
  80. evsel->core.attr.sample_freq = 1000000;
  81. #else
  82. evsel->core.attr.sample_freq = 1;
  83. #endif
  84. evsel->core.attr.inherit = 0;
  85. evsel->core.attr.watermark = 0;
  86. evsel->core.attr.wakeup_events = 1;
  87. evsel->core.attr.exclude_kernel = 1;
  88. err = evlist__open(evlist);
  89. if (err < 0) {
  90. pr_debug("Couldn't open the evlist: %s\n",
  91. str_error_r(-err, sbuf, sizeof(sbuf)));
  92. goto out_delete_evlist;
  93. }
  94. if (evlist__mmap(evlist, 128) < 0) {
  95. pr_debug("failed to mmap events: %d (%s)\n", errno,
  96. str_error_r(errno, sbuf, sizeof(sbuf)));
  97. err = -1;
  98. goto out_delete_evlist;
  99. }
  100. evlist__start_workload(evlist);
  101. retry:
  102. md = &evlist->mmap[0];
  103. if (perf_mmap__read_init(&md->core) < 0)
  104. goto out_init;
  105. while ((event = perf_mmap__read_event(&md->core)) != NULL) {
  106. if (event->header.type == PERF_RECORD_EXIT)
  107. nr_exit++;
  108. perf_mmap__consume(&md->core);
  109. }
  110. perf_mmap__read_done(&md->core);
  111. out_init:
  112. if (!exited || !nr_exit) {
  113. evlist__poll(evlist, -1);
  114. if (retry_count++ > 1000) {
  115. pr_debug("Failed after retrying 1000 times\n");
  116. err = -1;
  117. goto out_delete_evlist;
  118. }
  119. goto retry;
  120. }
  121. if (nr_exit != 1) {
  122. pr_debug("received %d EXIT records\n", nr_exit);
  123. err = -1;
  124. }
  125. out_delete_evlist:
  126. perf_cpu_map__put(cpus);
  127. perf_thread_map__put(threads);
  128. evlist__delete(evlist);
  129. return err;
  130. }
  131. struct test_case tests__task_exit[] = {
  132. TEST_CASE_EXCLUSIVE("Number of exit events of a simple workload", task_exit),
  133. { .name = NULL, }
  134. };
  135. struct test_suite suite__task_exit = {
  136. .desc = "Number of exit events of a simple workload",
  137. .test_cases = tests__task_exit,
  138. };