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(int subtest __maybe_unused)
  47. {
  48. struct record_opts opts = {
  49. .mmap_pages = UINT_MAX,
  50. .user_freq = UINT_MAX,
  51. .user_interval = ULLONG_MAX,
  52. .target = {
  53. .uses_mmap = true,
  54. },
  55. };
  56. struct thread_map *threads = NULL;
  57. struct cpu_map *cpus = NULL;
  58. struct perf_evlist *evlist = NULL;
  59. struct perf_evsel *evsel = NULL;
  60. int found, err = -1;
  61. const char *comm;
  62. threads = thread_map__new(-1, getpid(), UINT_MAX);
  63. CHECK_NOT_NULL__(threads);
  64. cpus = cpu_map__new(NULL);
  65. CHECK_NOT_NULL__(cpus);
  66. evlist = perf_evlist__new();
  67. CHECK_NOT_NULL__(evlist);
  68. perf_evlist__set_maps(evlist, cpus, threads);
  69. CHECK__(parse_events(evlist, "dummy:u", NULL));
  70. CHECK__(parse_events(evlist, "cycles:u", NULL));
  71. perf_evlist__config(evlist, &opts, NULL);
  72. evsel = perf_evlist__first(evlist);
  73. evsel->attr.comm = 1;
  74. evsel->attr.disabled = 1;
  75. evsel->attr.enable_on_exec = 0;
  76. if (perf_evlist__open(evlist) < 0) {
  77. pr_debug("Unable to open dummy and cycles event\n");
  78. err = TEST_SKIP;
  79. goto out_err;
  80. }
  81. CHECK__(perf_evlist__mmap(evlist, UINT_MAX, false));
  82. /*
  83. * First, test that a 'comm' event can be found when the event is
  84. * enabled.
  85. */
  86. perf_evlist__enable(evlist);
  87. comm = "Test COMM 1";
  88. CHECK__(prctl(PR_SET_NAME, (unsigned long)comm, 0, 0, 0));
  89. perf_evlist__disable(evlist);
  90. found = find_comm(evlist, comm);
  91. if (found != 1) {
  92. pr_debug("First time, failed to find tracking event.\n");
  93. goto out_err;
  94. }
  95. /*
  96. * Secondly, test that a 'comm' event can be found when the event is
  97. * disabled with the dummy event still enabled.
  98. */
  99. perf_evlist__enable(evlist);
  100. evsel = perf_evlist__last(evlist);
  101. CHECK__(perf_evsel__disable(evsel));
  102. comm = "Test COMM 2";
  103. CHECK__(prctl(PR_SET_NAME, (unsigned long)comm, 0, 0, 0));
  104. perf_evlist__disable(evlist);
  105. found = find_comm(evlist, comm);
  106. if (found != 1) {
  107. pr_debug("Seconf time, failed to find tracking event.\n");
  108. goto out_err;
  109. }
  110. err = 0;
  111. out_err:
  112. if (evlist) {
  113. perf_evlist__disable(evlist);
  114. perf_evlist__delete(evlist);
  115. } else {
  116. cpu_map__put(cpus);
  117. thread_map__put(threads);
  118. }
  119. return err;
  120. }