test_perfmon_cpu.c 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  1. /*
  2. * Copyright (c) 2014-2018 Remy Noel.
  3. * Copyright (c) 2014-2018 Richard Braun.
  4. *
  5. * This program is free software: you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation, either version 3 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  17. *
  18. *
  19. * This test checks the behavior of performance monitoring on a CPU.
  20. * It creates a group with two events, cycle and instruction, and attaches
  21. * that group to CPU1, where a thread is bound and runs a tight loop to
  22. * make sure the target CPU is never idle. After some time, the measurement
  23. * stops and values are reported.
  24. */
  25. #include <errno.h>
  26. #include <stdbool.h>
  27. #include <stddef.h>
  28. #include <stdint.h>
  29. #include <string.h>
  30. #include <kern/atomic.h>
  31. #include <kern/clock.h>
  32. #include <kern/cpumap.h>
  33. #include <kern/error.h>
  34. #include <kern/list.h>
  35. #include <kern/log.h>
  36. #include <kern/panic.h>
  37. #include <kern/perfmon.h>
  38. #include <kern/thread.h>
  39. #include <machine/cpu.h>
  40. #include <test/test.h>
  41. #define TEST_WAIT_DELAY_MS 1000
  42. /*
  43. * Using another CPU than the BSP as the monitored CPU checks that PMUs are
  44. * correctly initialized on APs.
  45. */
  46. #define TEST_CONTROL_CPU 0
  47. #define TEST_MONITORED_CPU (TEST_CONTROL_CPU + 1)
  48. #define TEST_MIN_CPUS (TEST_MONITORED_CPU + 1)
  49. #define TEST_EVENT_NAME_MAX_SIZE 32
  50. struct test_event
  51. {
  52. struct list node;
  53. struct perfmon_event pm_event;
  54. char name[TEST_EVENT_NAME_MAX_SIZE];
  55. };
  56. struct test_group
  57. {
  58. struct list events;
  59. };
  60. static uint32_t test_run_stop;
  61. static void
  62. test_wait (void)
  63. {
  64. thread_delay (clock_ticks_from_ms (TEST_WAIT_DELAY_MS), false);
  65. }
  66. static void
  67. test_event_init (struct test_event *event, uint32_t id, const char *name)
  68. {
  69. int error = perfmon_event_init (&event->pm_event, id, PERFMON_EF_KERN);
  70. error_check (error, "perfmon_event_init");
  71. strlcpy (event->name, name, sizeof (event->name));
  72. }
  73. static void
  74. test_event_report (struct test_event *event)
  75. {
  76. uint64_t count = perfmon_event_read (&event->pm_event);
  77. int error = count ? 0 : EINVAL;
  78. error_check (error, __func__);
  79. log_info ("test: %s: %llu", event->name, count);
  80. }
  81. static void
  82. test_event_attach_cpu (struct test_event *event, uint32_t cpu)
  83. {
  84. int error = perfmon_event_attach_cpu (&event->pm_event, cpu);
  85. error_check (error, "perfmon_event_attach_cpu");
  86. }
  87. static void
  88. test_event_detach (struct test_event *event)
  89. {
  90. int error = perfmon_event_detach (&event->pm_event);
  91. error_check (error, "perfmon_event_detach");
  92. }
  93. static void
  94. test_group_init (struct test_group *group)
  95. {
  96. list_init (&group->events);
  97. }
  98. static void
  99. test_group_add (struct test_group *group, struct test_event *event)
  100. {
  101. list_insert_tail (&group->events, &event->node);
  102. }
  103. static void
  104. test_group_attach_cpu (struct test_group *group, uint32_t cpu)
  105. {
  106. struct test_event *event;
  107. list_for_each_entry (&group->events, event, node)
  108. test_event_attach_cpu (event, cpu);
  109. }
  110. static void
  111. test_group_detach (struct test_group *group)
  112. {
  113. struct test_event *event;
  114. list_for_each_entry (&group->events, event, node)
  115. test_event_detach (event);
  116. }
  117. static void
  118. test_group_report (struct test_group *group)
  119. {
  120. struct test_event *event;
  121. list_for_each_entry (&group->events, event, node)
  122. test_event_report (event);
  123. }
  124. static void
  125. test_run (void *arg __unused)
  126. {
  127. while (!atomic_load_rlx (&test_run_stop))
  128. cpu_pause ();
  129. }
  130. static void
  131. test_control (void *arg)
  132. {
  133. struct thread *thread = arg;
  134. struct test_event cycle, instruction;
  135. struct test_group group;
  136. test_event_init (&cycle, PERFMON_EV_CYCLE, "cycle");
  137. test_event_init (&instruction, PERFMON_EV_INSTRUCTION, "instruction");
  138. test_group_init (&group);
  139. test_group_add (&group, &cycle);
  140. test_group_add (&group, &instruction);
  141. test_group_attach_cpu (&group, TEST_MONITORED_CPU);
  142. test_wait ();
  143. test_group_report (&group);
  144. test_wait ();
  145. test_group_detach (&group);
  146. test_group_report (&group);
  147. atomic_store_rlx (&test_run_stop, 1);
  148. thread_join (thread);
  149. log_info ("test (perfmon_cpu): OK");
  150. }
  151. TEST_INLINE (perfmon_cpu)
  152. {
  153. if (cpu_count () < TEST_MIN_CPUS)
  154. {
  155. log_err ("test: %u processors required", TEST_MIN_CPUS);
  156. return (TEST_SKIPPED);
  157. }
  158. struct cpumap *cpumap;
  159. int error = cpumap_create (&cpumap);
  160. error_check (error, "cpumap_create");
  161. struct thread_attr attr;
  162. thread_attr_init (&attr, THREAD_KERNEL_PREFIX "test_run");
  163. cpumap_zero (cpumap);
  164. cpumap_set (cpumap, TEST_MONITORED_CPU);
  165. thread_attr_set_cpumap (&attr, cpumap);
  166. struct thread *thread;
  167. error = thread_create (&thread, &attr, test_run, NULL);
  168. error_check (error, "thread_create");
  169. thread_attr_init (&attr, THREAD_KERNEL_PREFIX "test_control");
  170. thread_attr_set_detached (&attr);
  171. cpumap_zero (cpumap);
  172. cpumap_set (cpumap, TEST_CONTROL_CPU);
  173. thread_attr_set_cpumap (&attr, cpumap);
  174. error = thread_create (NULL, &attr, test_control, thread);
  175. error_check (error, "thread_create");
  176. cpumap_destroy (cpumap);
  177. return (TEST_RUNNING);
  178. }