test_perfmon_torture.c 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321
  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 module is a stress test, expected to never terminate, of the
  20. * performance monitoring module. It creates a control thread which
  21. * maintains a couple of test threads running while toggling performance
  22. * monitoring on them, attempting to produce many regular and corner
  23. * cases. In particular, the thread pool is randomly resized by destroying
  24. * and creating the underlying kernel threads.
  25. *
  26. * The control thread regularly prints some stats about the thread pool
  27. * and the associated performance monitoring events to report that it's
  28. * making progress.
  29. */
  30. #include <assert.h>
  31. #include <stdbool.h>
  32. #include <stddef.h>
  33. #include <stdio.h>
  34. #include <kern/atomic.h>
  35. #include <kern/clock.h>
  36. #include <kern/kmem.h>
  37. #include <kern/log.h>
  38. #include <kern/panic.h>
  39. #include <kern/perfmon.h>
  40. #include <kern/thread.h>
  41. #include <test/test.h>
  42. struct test_thread
  43. {
  44. uint32_t id;
  45. struct thread *thread;
  46. struct perfmon_event event;
  47. uint32_t must_stop;
  48. bool monitored;
  49. uint64_t count;
  50. };
  51. struct test_controller
  52. {
  53. struct test_thread **threads;
  54. uint32_t nr_threads;
  55. uint32_t monitoring_lid;
  56. uint32_t state_lid;
  57. uint64_t nr_current_events;
  58. uint64_t nr_total_events;
  59. uint64_t nr_current_threads;
  60. uint64_t nr_total_threads;
  61. };
  62. #define TEST_WAIT_DELAY_MS 100
  63. #define TEST_LOOPS_PER_PRINT 20
  64. #define TEST_MONITORING_SEED 12345
  65. #define TEST_STATE_SEED 23456
  66. static void
  67. test_wait (void)
  68. {
  69. thread_delay (clock_ticks_from_ms (TEST_WAIT_DELAY_MS), false);
  70. }
  71. static uint32_t
  72. test_rand (uint32_t x)
  73. {
  74. // Basic 32-bit xorshift PRNG.
  75. x ^= x << 13;
  76. x ^= x >> 17;
  77. x ^= x << 5;
  78. return (x);
  79. }
  80. static bool
  81. test_thread_monitored (const struct test_thread *thread)
  82. {
  83. return (thread->monitored);
  84. }
  85. static void
  86. test_thread_start_monitoring (struct test_thread *thread)
  87. {
  88. int error = perfmon_event_attach (&thread->event, thread->thread);
  89. test_assert_zero (error);
  90. thread->monitored = true;
  91. }
  92. static void
  93. test_thread_stop_monitoring (struct test_thread *thread)
  94. {
  95. thread->count += perfmon_event_read (&thread->event);
  96. int error = perfmon_event_detach (&thread->event);
  97. test_assert_zero (error);
  98. thread->monitored = false;
  99. }
  100. static void
  101. test_thread_report (const struct test_thread *thread)
  102. {
  103. log_info ("test: thread:%u count:%llu", thread->id, thread->count);
  104. }
  105. static void
  106. test_run (void *arg)
  107. {
  108. struct test_thread *thread = arg;
  109. while (1)
  110. if (atomic_load_rlx (&thread->must_stop))
  111. break;
  112. }
  113. static bool
  114. test_thread_started (const struct test_thread *thread)
  115. {
  116. return (thread->thread);
  117. }
  118. static void
  119. test_thread_start (struct test_thread *thread)
  120. {
  121. assert (!thread->monitored);
  122. if (test_thread_started (thread))
  123. return;
  124. thread->must_stop = 0;
  125. char name[THREAD_NAME_SIZE];
  126. snprintf (name, sizeof (name),
  127. THREAD_KERNEL_PREFIX "test_perfom_torture:%u", thread->id);
  128. struct thread_attr attr;
  129. thread_attr_init (&attr, name);
  130. int error = thread_create (&thread->thread, &attr, test_run, thread);
  131. test_assert_zero (error);
  132. }
  133. static void
  134. test_thread_request_stop (struct test_thread *thread)
  135. {
  136. atomic_store_rlx (&thread->must_stop, 1);
  137. }
  138. static void
  139. test_thread_join (struct test_thread *thread)
  140. {
  141. assert (test_thread_started (thread));
  142. assert (!test_thread_monitored (thread));
  143. thread_join (thread->thread);
  144. thread->thread = NULL;
  145. }
  146. static struct test_thread*
  147. test_thread_create (uint32_t id)
  148. {
  149. struct test_thread *thread = kmem_alloc (sizeof (*thread));
  150. if (! thread)
  151. panic ("thread allocation failed");
  152. thread->id = id;
  153. thread->thread = NULL;
  154. thread->must_stop = 0;
  155. thread->monitored = false;
  156. thread->count = 0;
  157. perfmon_event_init (&thread->event, PERFMON_EV_CYCLE, PERFMON_EF_KERN);
  158. test_thread_start (thread);
  159. return (thread);
  160. }
  161. static struct test_thread*
  162. test_controller_get (struct test_controller *controller, uint32_t id)
  163. {
  164. assert (id < controller->nr_threads);
  165. return (controller->threads[id]);
  166. }
  167. static struct test_thread*
  168. test_controller_get_by_lid (struct test_controller *controller, uint32_t lid)
  169. {
  170. return (test_controller_get (controller, lid % controller->nr_threads));
  171. }
  172. static void
  173. test_toggle_monitoring (struct test_controller *controller,
  174. struct test_thread *thread)
  175. {
  176. if (!test_thread_started (thread))
  177. return;
  178. else if (thread->monitored)
  179. {
  180. test_thread_stop_monitoring (thread);
  181. --controller->nr_current_events;
  182. }
  183. else
  184. {
  185. test_thread_start_monitoring (thread);
  186. ++controller->nr_total_events;
  187. ++controller->nr_current_events;
  188. }
  189. }
  190. static void
  191. test_toggle_state (struct test_controller *controller,
  192. struct test_thread *thread)
  193. {
  194. if (test_thread_started (thread))
  195. {
  196. /*
  197. * Make the thread stop asynchronously with monitoring to test
  198. * thread referencing.
  199. */
  200. test_thread_request_stop (thread);
  201. if (test_thread_monitored (thread))
  202. {
  203. test_thread_stop_monitoring (thread);
  204. --controller->nr_current_events;
  205. }
  206. test_thread_join (thread);
  207. --controller->nr_current_threads;
  208. }
  209. else
  210. {
  211. test_thread_start (thread);
  212. ++controller->nr_total_threads;
  213. ++controller->nr_current_threads;
  214. }
  215. }
  216. static void
  217. test_controller_report (struct test_controller *controller)
  218. {
  219. log_info ("test: events:%lu total:%lu threads:%lu total:%lu",
  220. controller->nr_current_events, controller->nr_total_events,
  221. controller->nr_current_threads, controller->nr_total_threads);
  222. for (uint32_t i = 0; i < controller->nr_threads; i++)
  223. test_thread_report (test_controller_get (controller, i));
  224. }
  225. static void
  226. test_control (void *arg)
  227. {
  228. struct test_controller *controller = arg;
  229. log_info ("test: %u threads", controller->nr_threads);
  230. for (unsigned long nr_loops = 1; /* no condition */; nr_loops++)
  231. {
  232. controller->monitoring_lid = test_rand (controller->monitoring_lid);
  233. struct test_thread *thread =
  234. test_controller_get_by_lid (controller, controller->monitoring_lid);
  235. test_toggle_monitoring (controller, thread);
  236. controller->state_lid = test_rand (controller->state_lid);
  237. thread = test_controller_get_by_lid (controller,
  238. controller->state_lid);
  239. test_toggle_state (controller, thread);
  240. test_wait ();
  241. if ((nr_loops % TEST_LOOPS_PER_PRINT) == 0)
  242. test_controller_report (controller);
  243. }
  244. }
  245. TEST_DEFERRED (perfmon_torture)
  246. {
  247. struct test_controller *controller = kmem_alloc (sizeof (*controller));
  248. if (! controller)
  249. panic ("test: unable to create controller");
  250. /*
  251. * At least two threads are required by the monitoring/state toggling
  252. * operations, otherwise they always apply to the same thread, severely
  253. * restricting their usefulness.
  254. */
  255. controller->nr_threads = MAX (cpu_count () - 1, 2);
  256. controller->threads = kmem_alloc (controller->nr_threads *
  257. sizeof (*controller->threads));
  258. if (!controller->threads)
  259. panic ("test: unable to allocate thread array");
  260. for (uint32_t i = 0; i < controller->nr_threads; i++)
  261. controller->threads[i] = test_thread_create (i);
  262. controller->monitoring_lid = TEST_MONITORING_SEED;
  263. controller->state_lid = TEST_STATE_SEED;
  264. controller->nr_current_events = 0;
  265. controller->nr_total_events = 0;
  266. controller->nr_current_threads = controller->nr_threads;
  267. controller->nr_total_threads = controller->nr_threads;
  268. struct thread_attr attr;
  269. thread_attr_init (&attr, THREAD_KERNEL_PREFIX "test_perfmon_control");
  270. thread_attr_set_detached (&attr);
  271. int error = thread_create (NULL, &attr, test_control, controller);
  272. test_assert_zero (error);
  273. return (TEST_RUNNING);
  274. }