task-exit.c 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  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. .uid = UINT_MAX,
  45. .uses_mmap = true,
  46. };
  47. const char *argv[] = { "true", NULL };
  48. char sbuf[STRERR_BUFSIZE];
  49. struct perf_cpu_map *cpus;
  50. struct perf_thread_map *threads;
  51. struct mmap *md;
  52. int retry_count = 0;
  53. signal(SIGCHLD, sig_handler);
  54. evlist = evlist__new_dummy();
  55. if (evlist == NULL) {
  56. pr_debug("evlist__new_dummy\n");
  57. return -1;
  58. }
  59. /*
  60. * Create maps of threads and cpus to monitor. In this case
  61. * we start with all threads and cpus (-1, -1) but then in
  62. * evlist__prepare_workload we'll fill in the only thread
  63. * we're monitoring, the one forked there.
  64. */
  65. cpus = perf_cpu_map__new_any_cpu();
  66. threads = thread_map__new_by_tid(-1);
  67. if (!cpus || !threads) {
  68. err = -ENOMEM;
  69. pr_debug("Not enough memory to create thread/cpu maps\n");
  70. goto out_delete_evlist;
  71. }
  72. perf_evlist__set_maps(&evlist->core, cpus, threads);
  73. err = evlist__prepare_workload(evlist, &target, argv, false, workload_exec_failed_signal);
  74. if (err < 0) {
  75. pr_debug("Couldn't run the workload!\n");
  76. goto out_delete_evlist;
  77. }
  78. evsel = evlist__first(evlist);
  79. evsel->core.attr.task = 1;
  80. #ifdef __s390x__
  81. evsel->core.attr.sample_freq = 1000000;
  82. #else
  83. evsel->core.attr.sample_freq = 1;
  84. #endif
  85. evsel->core.attr.inherit = 0;
  86. evsel->core.attr.watermark = 0;
  87. evsel->core.attr.wakeup_events = 1;
  88. evsel->core.attr.exclude_kernel = 1;
  89. err = evlist__open(evlist);
  90. if (err < 0) {
  91. pr_debug("Couldn't open the evlist: %s\n",
  92. str_error_r(-err, sbuf, sizeof(sbuf)));
  93. goto out_delete_evlist;
  94. }
  95. if (evlist__mmap(evlist, 128) < 0) {
  96. pr_debug("failed to mmap events: %d (%s)\n", errno,
  97. str_error_r(errno, sbuf, sizeof(sbuf)));
  98. err = -1;
  99. goto out_delete_evlist;
  100. }
  101. evlist__start_workload(evlist);
  102. retry:
  103. md = &evlist->mmap[0];
  104. if (perf_mmap__read_init(&md->core) < 0)
  105. goto out_init;
  106. while ((event = perf_mmap__read_event(&md->core)) != NULL) {
  107. if (event->header.type == PERF_RECORD_EXIT)
  108. nr_exit++;
  109. perf_mmap__consume(&md->core);
  110. }
  111. perf_mmap__read_done(&md->core);
  112. out_init:
  113. if (!exited || !nr_exit) {
  114. evlist__poll(evlist, -1);
  115. if (retry_count++ > 1000) {
  116. pr_debug("Failed after retrying 1000 times\n");
  117. err = -1;
  118. goto out_delete_evlist;
  119. }
  120. goto retry;
  121. }
  122. if (nr_exit != 1) {
  123. pr_debug("received %d EXIT records\n", nr_exit);
  124. err = -1;
  125. }
  126. out_delete_evlist:
  127. perf_cpu_map__put(cpus);
  128. perf_thread_map__put(threads);
  129. evlist__delete(evlist);
  130. return err;
  131. }
  132. DEFINE_SUITE("Number of exit events of a simple workload", task_exit);