test_perfmon_torture.c 8.9 KB

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