task-exit.c 3.2 KB

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