task-exit.c 2.9 KB

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