keep-tracking.c 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. #include <linux/types.h>
  2. #include <unistd.h>
  3. #include <sys/prctl.h>
  4. #include "parse-events.h"
  5. #include "evlist.h"
  6. #include "evsel.h"
  7. #include "thread_map.h"
  8. #include "cpumap.h"
  9. #include "tests.h"
  10. #define CHECK__(x) { \
  11. while ((x) < 0) { \
  12. pr_debug(#x " failed!\n"); \
  13. goto out_err; \
  14. } \
  15. }
  16. #define CHECK_NOT_NULL__(x) { \
  17. while ((x) == NULL) { \
  18. pr_debug(#x " failed!\n"); \
  19. goto out_err; \
  20. } \
  21. }
  22. static int find_comm(struct perf_evlist *evlist, const char *comm)
  23. {
  24. union perf_event *event;
  25. int i, found;
  26. found = 0;
  27. for (i = 0; i < evlist->nr_mmaps; i++) {
  28. while ((event = perf_evlist__mmap_read(evlist, i)) != NULL) {
  29. if (event->header.type == PERF_RECORD_COMM &&
  30. (pid_t)event->comm.pid == getpid() &&
  31. (pid_t)event->comm.tid == getpid() &&
  32. strcmp(event->comm.comm, comm) == 0)
  33. found += 1;
  34. perf_evlist__mmap_consume(evlist, i);
  35. }
  36. }
  37. return found;
  38. }
  39. /**
  40. * test__keep_tracking - test using a dummy software event to keep tracking.
  41. *
  42. * This function implements a test that checks that tracking events continue
  43. * when an event is disabled but a dummy software event is not disabled. If the
  44. * test passes %0 is returned, otherwise %-1 is returned.
  45. */
  46. int test__keep_tracking(void)
  47. {
  48. struct record_opts opts = {
  49. .mmap_pages = UINT_MAX,
  50. .user_freq = UINT_MAX,
  51. .user_interval = ULLONG_MAX,
  52. .freq = 4000,
  53. .target = {
  54. .uses_mmap = true,
  55. },
  56. };
  57. struct thread_map *threads = NULL;
  58. struct cpu_map *cpus = NULL;
  59. struct perf_evlist *evlist = NULL;
  60. struct perf_evsel *evsel = NULL;
  61. int found, err = -1;
  62. const char *comm;
  63. threads = thread_map__new(-1, getpid(), UINT_MAX);
  64. CHECK_NOT_NULL__(threads);
  65. cpus = cpu_map__new(NULL);
  66. CHECK_NOT_NULL__(cpus);
  67. evlist = perf_evlist__new();
  68. CHECK_NOT_NULL__(evlist);
  69. perf_evlist__set_maps(evlist, cpus, threads);
  70. CHECK__(parse_events(evlist, "dummy:u", NULL));
  71. CHECK__(parse_events(evlist, "cycles:u", NULL));
  72. perf_evlist__config(evlist, &opts);
  73. evsel = perf_evlist__first(evlist);
  74. evsel->attr.comm = 1;
  75. evsel->attr.disabled = 1;
  76. evsel->attr.enable_on_exec = 0;
  77. if (perf_evlist__open(evlist) < 0) {
  78. pr_debug("Unable to open dummy and cycles event\n");
  79. err = TEST_SKIP;
  80. goto out_err;
  81. }
  82. CHECK__(perf_evlist__mmap(evlist, UINT_MAX, false));
  83. /*
  84. * First, test that a 'comm' event can be found when the event is
  85. * enabled.
  86. */
  87. perf_evlist__enable(evlist);
  88. comm = "Test COMM 1";
  89. CHECK__(prctl(PR_SET_NAME, (unsigned long)comm, 0, 0, 0));
  90. perf_evlist__disable(evlist);
  91. found = find_comm(evlist, comm);
  92. if (found != 1) {
  93. pr_debug("First time, failed to find tracking event.\n");
  94. goto out_err;
  95. }
  96. /*
  97. * Secondly, test that a 'comm' event can be found when the event is
  98. * disabled with the dummy event still enabled.
  99. */
  100. perf_evlist__enable(evlist);
  101. evsel = perf_evlist__last(evlist);
  102. CHECK__(perf_evlist__disable_event(evlist, evsel));
  103. comm = "Test COMM 2";
  104. CHECK__(prctl(PR_SET_NAME, (unsigned long)comm, 0, 0, 0));
  105. perf_evlist__disable(evlist);
  106. found = find_comm(evlist, comm);
  107. if (found != 1) {
  108. pr_debug("Seconf time, failed to find tracking event.\n");
  109. goto out_err;
  110. }
  111. err = 0;
  112. out_err:
  113. if (evlist) {
  114. perf_evlist__disable(evlist);
  115. perf_evlist__delete(evlist);
  116. } else {
  117. cpu_map__put(cpus);
  118. thread_map__put(threads);
  119. }
  120. return err;
  121. }