keep-tracking.c 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. // SPDX-License-Identifier: GPL-2.0
  2. #include <linux/types.h>
  3. #include <limits.h>
  4. #include <unistd.h>
  5. #include <sys/prctl.h>
  6. #include <perf/cpumap.h>
  7. #include <perf/evlist.h>
  8. #include <perf/mmap.h>
  9. #include "debug.h"
  10. #include "parse-events.h"
  11. #include "evlist.h"
  12. #include "evsel.h"
  13. #include "record.h"
  14. #include "thread_map.h"
  15. #include "tests.h"
  16. #include "util/mmap.h"
  17. #define CHECK__(x) { \
  18. while ((x) < 0) { \
  19. pr_debug(#x " failed!\n"); \
  20. goto out_err; \
  21. } \
  22. }
  23. #define CHECK_NOT_NULL__(x) { \
  24. while ((x) == NULL) { \
  25. pr_debug(#x " failed!\n"); \
  26. goto out_err; \
  27. } \
  28. }
  29. static int find_comm(struct evlist *evlist, const char *comm)
  30. {
  31. union perf_event *event;
  32. struct mmap *md;
  33. int i, found;
  34. found = 0;
  35. for (i = 0; i < evlist->core.nr_mmaps; i++) {
  36. md = &evlist->mmap[i];
  37. if (perf_mmap__read_init(&md->core) < 0)
  38. continue;
  39. while ((event = perf_mmap__read_event(&md->core)) != NULL) {
  40. if (event->header.type == PERF_RECORD_COMM &&
  41. (pid_t)event->comm.pid == getpid() &&
  42. (pid_t)event->comm.tid == getpid() &&
  43. strcmp(event->comm.comm, comm) == 0)
  44. found += 1;
  45. perf_mmap__consume(&md->core);
  46. }
  47. perf_mmap__read_done(&md->core);
  48. }
  49. return found;
  50. }
  51. /**
  52. * test__keep_tracking - test using a dummy software event to keep tracking.
  53. *
  54. * This function implements a test that checks that tracking events continue
  55. * when an event is disabled but a dummy software event is not disabled. If the
  56. * test passes %0 is returned, otherwise %-1 is returned.
  57. */
  58. int test__keep_tracking(struct test *test __maybe_unused, int subtest __maybe_unused)
  59. {
  60. struct record_opts opts = {
  61. .mmap_pages = UINT_MAX,
  62. .user_freq = UINT_MAX,
  63. .user_interval = ULLONG_MAX,
  64. .target = {
  65. .uses_mmap = true,
  66. },
  67. };
  68. struct perf_thread_map *threads = NULL;
  69. struct perf_cpu_map *cpus = NULL;
  70. struct evlist *evlist = NULL;
  71. struct evsel *evsel = NULL;
  72. int found, err = -1;
  73. const char *comm;
  74. threads = thread_map__new(-1, getpid(), UINT_MAX);
  75. CHECK_NOT_NULL__(threads);
  76. cpus = perf_cpu_map__new(NULL);
  77. CHECK_NOT_NULL__(cpus);
  78. evlist = evlist__new();
  79. CHECK_NOT_NULL__(evlist);
  80. perf_evlist__set_maps(&evlist->core, cpus, threads);
  81. CHECK__(parse_events(evlist, "dummy:u", NULL));
  82. CHECK__(parse_events(evlist, "cycles:u", NULL));
  83. perf_evlist__config(evlist, &opts, NULL);
  84. evsel = evlist__first(evlist);
  85. evsel->core.attr.comm = 1;
  86. evsel->core.attr.disabled = 1;
  87. evsel->core.attr.enable_on_exec = 0;
  88. if (evlist__open(evlist) < 0) {
  89. pr_debug("Unable to open dummy and cycles event\n");
  90. err = TEST_SKIP;
  91. goto out_err;
  92. }
  93. CHECK__(evlist__mmap(evlist, UINT_MAX));
  94. /*
  95. * First, test that a 'comm' event can be found when the event is
  96. * enabled.
  97. */
  98. evlist__enable(evlist);
  99. comm = "Test COMM 1";
  100. CHECK__(prctl(PR_SET_NAME, (unsigned long)comm, 0, 0, 0));
  101. evlist__disable(evlist);
  102. found = find_comm(evlist, comm);
  103. if (found != 1) {
  104. pr_debug("First time, failed to find tracking event.\n");
  105. goto out_err;
  106. }
  107. /*
  108. * Secondly, test that a 'comm' event can be found when the event is
  109. * disabled with the dummy event still enabled.
  110. */
  111. evlist__enable(evlist);
  112. evsel = evlist__last(evlist);
  113. CHECK__(evsel__disable(evsel));
  114. comm = "Test COMM 2";
  115. CHECK__(prctl(PR_SET_NAME, (unsigned long)comm, 0, 0, 0));
  116. evlist__disable(evlist);
  117. found = find_comm(evlist, comm);
  118. if (found != 1) {
  119. pr_debug("Second time, failed to find tracking event.\n");
  120. goto out_err;
  121. }
  122. err = 0;
  123. out_err:
  124. if (evlist) {
  125. evlist__disable(evlist);
  126. evlist__delete(evlist);
  127. } else {
  128. perf_cpu_map__put(cpus);
  129. perf_thread_map__put(threads);
  130. }
  131. return err;
  132. }