task-exit.c 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  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)
  10. {
  11. exited = 1;
  12. if (sig == SIGUSR1)
  13. nr_exit = -1;
  14. }
  15. /*
  16. * This test will start a workload that does nothing then it checks
  17. * if the number of exit event reported by the kernel is 1 or not
  18. * in order to check the kernel returns correct number of event.
  19. */
  20. int test__task_exit(void)
  21. {
  22. int err = -1;
  23. union perf_event *event;
  24. struct perf_evsel *evsel;
  25. struct perf_evlist *evlist;
  26. struct perf_target target = {
  27. .uid = UINT_MAX,
  28. .uses_mmap = true,
  29. };
  30. const char *argv[] = { "true", NULL };
  31. signal(SIGCHLD, sig_handler);
  32. signal(SIGUSR1, sig_handler);
  33. evlist = perf_evlist__new();
  34. if (evlist == NULL) {
  35. pr_debug("perf_evlist__new\n");
  36. return -1;
  37. }
  38. /*
  39. * We need at least one evsel in the evlist, use the default
  40. * one: "cycles".
  41. */
  42. err = perf_evlist__add_default(evlist);
  43. if (err < 0) {
  44. pr_debug("Not enough memory to create evsel\n");
  45. goto out_free_evlist;
  46. }
  47. /*
  48. * Create maps of threads and cpus to monitor. In this case
  49. * we start with all threads and cpus (-1, -1) but then in
  50. * perf_evlist__prepare_workload we'll fill in the only thread
  51. * we're monitoring, the one forked there.
  52. */
  53. evlist->cpus = cpu_map__dummy_new();
  54. evlist->threads = thread_map__new_by_tid(-1);
  55. if (!evlist->cpus || !evlist->threads) {
  56. err = -ENOMEM;
  57. pr_debug("Not enough memory to create thread/cpu maps\n");
  58. goto out_delete_maps;
  59. }
  60. err = perf_evlist__prepare_workload(evlist, &target, argv, false, true);
  61. if (err < 0) {
  62. pr_debug("Couldn't run the workload!\n");
  63. goto out_delete_maps;
  64. }
  65. evsel = perf_evlist__first(evlist);
  66. evsel->attr.task = 1;
  67. evsel->attr.sample_freq = 0;
  68. evsel->attr.inherit = 0;
  69. evsel->attr.watermark = 0;
  70. evsel->attr.wakeup_events = 1;
  71. evsel->attr.exclude_kernel = 1;
  72. err = perf_evlist__open(evlist);
  73. if (err < 0) {
  74. pr_debug("Couldn't open the evlist: %s\n", strerror(-err));
  75. goto out_delete_maps;
  76. }
  77. if (perf_evlist__mmap(evlist, 128, true) < 0) {
  78. pr_debug("failed to mmap events: %d (%s)\n", errno,
  79. strerror(errno));
  80. goto out_close_evlist;
  81. }
  82. perf_evlist__start_workload(evlist);
  83. retry:
  84. while ((event = perf_evlist__mmap_read(evlist, 0)) != NULL) {
  85. if (event->header.type == PERF_RECORD_EXIT)
  86. nr_exit++;
  87. perf_evlist__mmap_consume(evlist, 0);
  88. }
  89. if (!exited || !nr_exit) {
  90. poll(evlist->pollfd, evlist->nr_fds, -1);
  91. goto retry;
  92. }
  93. if (nr_exit != 1) {
  94. pr_debug("received %d EXIT records\n", nr_exit);
  95. err = -1;
  96. }
  97. perf_evlist__munmap(evlist);
  98. out_close_evlist:
  99. perf_evlist__close(evlist);
  100. out_delete_maps:
  101. perf_evlist__delete_maps(evlist);
  102. out_free_evlist:
  103. perf_evlist__delete(evlist);
  104. return err;
  105. }