event-times.c 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  1. // SPDX-License-Identifier: GPL-2.0
  2. #include <linux/compiler.h>
  3. #include <linux/string.h>
  4. #include <errno.h>
  5. #include <inttypes.h>
  6. #include <string.h>
  7. #include <sys/wait.h>
  8. #include <perf/cpumap.h>
  9. #include "tests.h"
  10. #include "evlist.h"
  11. #include "evsel.h"
  12. #include "debug.h"
  13. #include "parse-events.h"
  14. #include "thread_map.h"
  15. #include "target.h"
  16. static int attach__enable_on_exec(struct evlist *evlist)
  17. {
  18. struct evsel *evsel = evlist__last(evlist);
  19. struct target target = {};
  20. const char *argv[] = { "true", NULL, };
  21. char sbuf[STRERR_BUFSIZE];
  22. int err;
  23. pr_debug("attaching to spawned child, enable on exec\n");
  24. err = evlist__create_maps(evlist, &target);
  25. if (err < 0) {
  26. pr_debug("Not enough memory to create thread/cpu maps\n");
  27. return err;
  28. }
  29. err = evlist__prepare_workload(evlist, &target, argv, false, NULL);
  30. if (err < 0) {
  31. pr_debug("Couldn't run the workload!\n");
  32. return err;
  33. }
  34. evsel->core.attr.enable_on_exec = 1;
  35. err = evlist__open(evlist);
  36. if (err < 0) {
  37. pr_debug("perf_evlist__open: %s\n",
  38. str_error_r(errno, sbuf, sizeof(sbuf)));
  39. return err;
  40. }
  41. return evlist__start_workload(evlist) == 1 ? TEST_OK : TEST_FAIL;
  42. }
  43. static int detach__enable_on_exec(struct evlist *evlist)
  44. {
  45. waitpid(evlist->workload.pid, NULL, 0);
  46. return 0;
  47. }
  48. static int attach__current_disabled(struct evlist *evlist)
  49. {
  50. struct evsel *evsel = evlist__last(evlist);
  51. struct perf_thread_map *threads;
  52. int err;
  53. pr_debug("attaching to current thread as disabled\n");
  54. threads = thread_map__new_by_tid(getpid());
  55. if (threads == NULL) {
  56. pr_debug("thread_map__new\n");
  57. return -1;
  58. }
  59. evsel->core.attr.disabled = 1;
  60. err = evsel__open_per_thread(evsel, threads);
  61. if (err) {
  62. pr_debug("Failed to open event cpu-clock:u\n");
  63. return err;
  64. }
  65. perf_thread_map__put(threads);
  66. return evsel__enable(evsel) == 0 ? TEST_OK : TEST_FAIL;
  67. }
  68. static int attach__current_enabled(struct evlist *evlist)
  69. {
  70. struct evsel *evsel = evlist__last(evlist);
  71. struct perf_thread_map *threads;
  72. int err;
  73. pr_debug("attaching to current thread as enabled\n");
  74. threads = thread_map__new_by_tid(getpid());
  75. if (threads == NULL) {
  76. pr_debug("failed to call thread_map__new\n");
  77. return -1;
  78. }
  79. err = evsel__open_per_thread(evsel, threads);
  80. perf_thread_map__put(threads);
  81. return err == 0 ? TEST_OK : TEST_FAIL;
  82. }
  83. static int detach__disable(struct evlist *evlist)
  84. {
  85. struct evsel *evsel = evlist__last(evlist);
  86. return evsel__enable(evsel);
  87. }
  88. static int attach__cpu_disabled(struct evlist *evlist)
  89. {
  90. struct evsel *evsel = evlist__last(evlist);
  91. struct perf_cpu_map *cpus;
  92. int err;
  93. pr_debug("attaching to CPU 0 as enabled\n");
  94. cpus = perf_cpu_map__new("0");
  95. if (cpus == NULL) {
  96. pr_debug("failed to call perf_cpu_map__new\n");
  97. return -1;
  98. }
  99. evsel->core.attr.disabled = 1;
  100. err = evsel__open_per_cpu(evsel, cpus, -1);
  101. perf_cpu_map__put(cpus);
  102. if (err) {
  103. if (err == -EACCES)
  104. return TEST_SKIP;
  105. pr_debug("Failed to open event cpu-clock:u\n");
  106. return err;
  107. }
  108. return evsel__enable(evsel);
  109. }
  110. static int attach__cpu_enabled(struct evlist *evlist)
  111. {
  112. struct evsel *evsel = evlist__last(evlist);
  113. struct perf_cpu_map *cpus;
  114. int err;
  115. pr_debug("attaching to CPU 0 as enabled\n");
  116. cpus = perf_cpu_map__new("0");
  117. if (cpus == NULL) {
  118. pr_debug("failed to call perf_cpu_map__new\n");
  119. return -1;
  120. }
  121. err = evsel__open_per_cpu(evsel, cpus, -1);
  122. perf_cpu_map__put(cpus);
  123. if (err == -EACCES)
  124. return TEST_SKIP;
  125. return err ? TEST_FAIL : TEST_OK;
  126. }
  127. static int test_times(int (attach)(struct evlist *),
  128. int (detach)(struct evlist *))
  129. {
  130. struct perf_counts_values count;
  131. struct evlist *evlist = NULL;
  132. struct evsel *evsel;
  133. int err = -1, i;
  134. evlist = evlist__new();
  135. if (!evlist) {
  136. pr_debug("failed to create event list\n");
  137. goto out_err;
  138. }
  139. err = parse_event(evlist, "cpu-clock:u");
  140. if (err) {
  141. pr_debug("failed to parse event cpu-clock:u\n");
  142. goto out_err;
  143. }
  144. evsel = evlist__last(evlist);
  145. evsel->core.attr.read_format |=
  146. PERF_FORMAT_TOTAL_TIME_ENABLED |
  147. PERF_FORMAT_TOTAL_TIME_RUNNING;
  148. err = attach(evlist);
  149. if (err == TEST_SKIP) {
  150. pr_debug(" SKIP : not enough rights\n");
  151. evlist__delete(evlist);
  152. return err;
  153. }
  154. TEST_ASSERT_VAL("failed to attach", !err);
  155. for (i = 0; i < 100000000; i++) { }
  156. TEST_ASSERT_VAL("failed to detach", !detach(evlist));
  157. perf_evsel__read(&evsel->core, 0, 0, &count);
  158. err = !(count.ena == count.run);
  159. pr_debug(" %s: ena %" PRIu64", run %" PRIu64"\n",
  160. !err ? "OK " : "FAILED",
  161. count.ena, count.run);
  162. out_err:
  163. evlist__delete(evlist);
  164. return !err ? TEST_OK : TEST_FAIL;
  165. }
  166. /*
  167. * This test creates software event 'cpu-clock'
  168. * attaches it in several ways (explained below)
  169. * and checks that enabled and running times
  170. * match.
  171. */
  172. static int test__event_times(struct test_suite *test __maybe_unused, int subtest __maybe_unused)
  173. {
  174. int err, ret = 0;
  175. #define _T(attach, detach) \
  176. err = test_times(attach, detach); \
  177. if (err && (ret == TEST_OK || ret == TEST_SKIP)) \
  178. ret = err;
  179. /* attach on newly spawned process after exec */
  180. _T(attach__enable_on_exec, detach__enable_on_exec)
  181. /* attach on current process as enabled */
  182. _T(attach__current_enabled, detach__disable)
  183. /* attach on current process as disabled */
  184. _T(attach__current_disabled, detach__disable)
  185. /* attach on cpu as disabled */
  186. _T(attach__cpu_disabled, detach__disable)
  187. /* attach on cpu as enabled */
  188. _T(attach__cpu_enabled, detach__disable)
  189. #undef _T
  190. return ret;
  191. }
  192. DEFINE_SUITE("Event times", event_times);