test_command_queue.h 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428
  1. /**************************************************************************/
  2. /* test_command_queue.h */
  3. /**************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /**************************************************************************/
  8. /* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
  9. /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
  10. /* */
  11. /* Permission is hereby granted, free of charge, to any person obtaining */
  12. /* a copy of this software and associated documentation files (the */
  13. /* "Software"), to deal in the Software without restriction, including */
  14. /* without limitation the rights to use, copy, modify, merge, publish, */
  15. /* distribute, sublicense, and/or sell copies of the Software, and to */
  16. /* permit persons to whom the Software is furnished to do so, subject to */
  17. /* the following conditions: */
  18. /* */
  19. /* The above copyright notice and this permission notice shall be */
  20. /* included in all copies or substantial portions of the Software. */
  21. /* */
  22. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  23. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  24. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
  25. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  26. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  27. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  28. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  29. /**************************************************************************/
  30. #ifndef TEST_COMMAND_QUEUE_H
  31. #define TEST_COMMAND_QUEUE_H
  32. #include "core/config/project_settings.h"
  33. #include "core/math/random_number_generator.h"
  34. #include "core/os/os.h"
  35. #include "core/os/thread.h"
  36. #include "core/templates/command_queue_mt.h"
  37. #include "tests/test_macros.h"
  38. namespace TestCommandQueue {
  39. class ThreadWork {
  40. Semaphore thread_sem;
  41. Semaphore main_sem;
  42. Mutex mut;
  43. int threading_errors = 0;
  44. enum State {
  45. MAIN_START,
  46. MAIN_DONE,
  47. THREAD_START,
  48. THREAD_DONE,
  49. } state;
  50. public:
  51. ThreadWork() {
  52. mut.lock();
  53. state = MAIN_START;
  54. }
  55. ~ThreadWork() {
  56. CHECK_MESSAGE(threading_errors == 0, "threads did not lock/unlock correctly");
  57. }
  58. void thread_wait_for_work() {
  59. thread_sem.wait();
  60. mut.lock();
  61. if (state != MAIN_DONE) {
  62. threading_errors++;
  63. }
  64. state = THREAD_START;
  65. }
  66. void thread_done_work() {
  67. if (state != THREAD_START) {
  68. threading_errors++;
  69. }
  70. state = THREAD_DONE;
  71. mut.unlock();
  72. main_sem.post();
  73. }
  74. void main_wait_for_done() {
  75. main_sem.wait();
  76. mut.lock();
  77. if (state != THREAD_DONE) {
  78. threading_errors++;
  79. }
  80. state = MAIN_START;
  81. }
  82. void main_start_work() {
  83. if (state != MAIN_START) {
  84. threading_errors++;
  85. }
  86. state = MAIN_DONE;
  87. mut.unlock();
  88. thread_sem.post();
  89. }
  90. };
  91. class SharedThreadState {
  92. public:
  93. ThreadWork reader_threadwork;
  94. ThreadWork writer_threadwork;
  95. CommandQueueMT command_queue = CommandQueueMT(true);
  96. enum TestMsgType {
  97. TEST_MSG_FUNC1_TRANSFORM,
  98. TEST_MSG_FUNC2_TRANSFORM_FLOAT,
  99. TEST_MSG_FUNC3_TRANSFORMx6,
  100. TEST_MSGSYNC_FUNC1_TRANSFORM,
  101. TEST_MSGSYNC_FUNC2_TRANSFORM_FLOAT,
  102. TEST_MSGRET_FUNC1_TRANSFORM,
  103. TEST_MSGRET_FUNC2_TRANSFORM_FLOAT,
  104. TEST_MSG_MAX
  105. };
  106. Vector<TestMsgType> message_types_to_write;
  107. bool during_writing = false;
  108. int message_count_to_read = 0;
  109. bool exit_threads = false;
  110. Thread reader_thread;
  111. Thread writer_thread;
  112. int func1_count = 0;
  113. void func1(Transform3D t) {
  114. func1_count++;
  115. }
  116. void func2(Transform3D t, float f) {
  117. func1_count++;
  118. }
  119. void func3(Transform3D t1, Transform3D t2, Transform3D t3, Transform3D t4, Transform3D t5, Transform3D t6) {
  120. func1_count++;
  121. }
  122. Transform3D func1r(Transform3D t) {
  123. func1_count++;
  124. return t;
  125. }
  126. Transform3D func2r(Transform3D t, float f) {
  127. func1_count++;
  128. return t;
  129. }
  130. void add_msg_to_write(TestMsgType type) {
  131. message_types_to_write.push_back(type);
  132. }
  133. void reader_thread_loop() {
  134. reader_threadwork.thread_wait_for_work();
  135. while (!exit_threads) {
  136. if (message_count_to_read < 0) {
  137. command_queue.flush_all();
  138. }
  139. for (int i = 0; i < message_count_to_read; i++) {
  140. command_queue.wait_and_flush();
  141. }
  142. message_count_to_read = 0;
  143. reader_threadwork.thread_done_work();
  144. reader_threadwork.thread_wait_for_work();
  145. }
  146. command_queue.flush_all();
  147. reader_threadwork.thread_done_work();
  148. }
  149. static void static_reader_thread_loop(void *stsvoid) {
  150. SharedThreadState *sts = static_cast<SharedThreadState *>(stsvoid);
  151. sts->reader_thread_loop();
  152. }
  153. void writer_thread_loop() {
  154. during_writing = false;
  155. writer_threadwork.thread_wait_for_work();
  156. while (!exit_threads) {
  157. Transform3D tr;
  158. Transform3D otr;
  159. float f = 1;
  160. during_writing = true;
  161. for (int i = 0; i < message_types_to_write.size(); i++) {
  162. TestMsgType msg_type = message_types_to_write[i];
  163. switch (msg_type) {
  164. case TEST_MSG_FUNC1_TRANSFORM:
  165. command_queue.push(this, &SharedThreadState::func1, tr);
  166. break;
  167. case TEST_MSG_FUNC2_TRANSFORM_FLOAT:
  168. command_queue.push(this, &SharedThreadState::func2, tr, f);
  169. break;
  170. case TEST_MSG_FUNC3_TRANSFORMx6:
  171. command_queue.push(this, &SharedThreadState::func3, tr, tr, tr, tr, tr, tr);
  172. break;
  173. case TEST_MSGSYNC_FUNC1_TRANSFORM:
  174. command_queue.push_and_sync(this, &SharedThreadState::func1, tr);
  175. break;
  176. case TEST_MSGSYNC_FUNC2_TRANSFORM_FLOAT:
  177. command_queue.push_and_sync(this, &SharedThreadState::func2, tr, f);
  178. break;
  179. case TEST_MSGRET_FUNC1_TRANSFORM:
  180. command_queue.push_and_ret(this, &SharedThreadState::func1r, tr, &otr);
  181. break;
  182. case TEST_MSGRET_FUNC2_TRANSFORM_FLOAT:
  183. command_queue.push_and_ret(this, &SharedThreadState::func2r, tr, f, &otr);
  184. break;
  185. default:
  186. break;
  187. }
  188. }
  189. message_types_to_write.clear();
  190. during_writing = false;
  191. writer_threadwork.thread_done_work();
  192. writer_threadwork.thread_wait_for_work();
  193. }
  194. writer_threadwork.thread_done_work();
  195. }
  196. static void static_writer_thread_loop(void *stsvoid) {
  197. SharedThreadState *sts = static_cast<SharedThreadState *>(stsvoid);
  198. sts->writer_thread_loop();
  199. }
  200. void init_threads() {
  201. reader_thread.start(&SharedThreadState::static_reader_thread_loop, this);
  202. writer_thread.start(&SharedThreadState::static_writer_thread_loop, this);
  203. }
  204. void destroy_threads() {
  205. exit_threads = true;
  206. reader_threadwork.main_start_work();
  207. writer_threadwork.main_start_work();
  208. reader_thread.wait_to_finish();
  209. writer_thread.wait_to_finish();
  210. }
  211. };
  212. TEST_CASE("[CommandQueue] Test Queue Basics") {
  213. const char *COMMAND_QUEUE_SETTING = "memory/limits/command_queue/multithreading_queue_size_kb";
  214. ProjectSettings::get_singleton()->set_setting(COMMAND_QUEUE_SETTING, 1);
  215. SharedThreadState sts;
  216. sts.init_threads();
  217. sts.add_msg_to_write(SharedThreadState::TEST_MSG_FUNC1_TRANSFORM);
  218. sts.writer_threadwork.main_start_work();
  219. sts.writer_threadwork.main_wait_for_done();
  220. CHECK_MESSAGE(sts.func1_count == 0,
  221. "Control: no messages read before reader has run.");
  222. sts.message_count_to_read = 1;
  223. sts.reader_threadwork.main_start_work();
  224. sts.reader_threadwork.main_wait_for_done();
  225. CHECK_MESSAGE(sts.func1_count == 1,
  226. "Reader should have read one message");
  227. sts.message_count_to_read = -1;
  228. sts.reader_threadwork.main_start_work();
  229. sts.reader_threadwork.main_wait_for_done();
  230. CHECK_MESSAGE(sts.func1_count == 1,
  231. "Reader should have read no additional messages from flush_all");
  232. sts.add_msg_to_write(SharedThreadState::TEST_MSG_FUNC1_TRANSFORM);
  233. sts.writer_threadwork.main_start_work();
  234. sts.writer_threadwork.main_wait_for_done();
  235. sts.message_count_to_read = -1;
  236. sts.reader_threadwork.main_start_work();
  237. sts.reader_threadwork.main_wait_for_done();
  238. CHECK_MESSAGE(sts.func1_count == 2,
  239. "Reader should have read one additional message from flush_all");
  240. sts.destroy_threads();
  241. CHECK_MESSAGE(sts.func1_count == 2,
  242. "Reader should have read no additional messages after join");
  243. ProjectSettings::get_singleton()->set_setting(COMMAND_QUEUE_SETTING,
  244. ProjectSettings::get_singleton()->property_get_revert(COMMAND_QUEUE_SETTING));
  245. }
  246. TEST_CASE("[CommandQueue] Test Queue Wrapping to same spot.") {
  247. const char *COMMAND_QUEUE_SETTING = "memory/limits/command_queue/multithreading_queue_size_kb";
  248. ProjectSettings::get_singleton()->set_setting(COMMAND_QUEUE_SETTING, 1);
  249. SharedThreadState sts;
  250. sts.init_threads();
  251. sts.add_msg_to_write(SharedThreadState::TEST_MSG_FUNC3_TRANSFORMx6);
  252. sts.add_msg_to_write(SharedThreadState::TEST_MSG_FUNC3_TRANSFORMx6);
  253. sts.add_msg_to_write(SharedThreadState::TEST_MSG_FUNC1_TRANSFORM);
  254. sts.writer_threadwork.main_start_work();
  255. sts.writer_threadwork.main_wait_for_done();
  256. sts.message_count_to_read = -1;
  257. sts.reader_threadwork.main_start_work();
  258. sts.reader_threadwork.main_wait_for_done();
  259. CHECK_MESSAGE(sts.func1_count == 3,
  260. "Reader should have read at least three messages");
  261. sts.add_msg_to_write(SharedThreadState::TEST_MSG_FUNC3_TRANSFORMx6);
  262. sts.writer_threadwork.main_start_work();
  263. sts.writer_threadwork.main_wait_for_done();
  264. sts.add_msg_to_write(SharedThreadState::TEST_MSG_FUNC1_TRANSFORM);
  265. sts.add_msg_to_write(SharedThreadState::TEST_MSG_FUNC3_TRANSFORMx6);
  266. sts.writer_threadwork.main_start_work();
  267. OS::get_singleton()->delay_usec(1000);
  268. sts.message_count_to_read = -1;
  269. sts.reader_threadwork.main_start_work();
  270. OS::get_singleton()->delay_usec(1000);
  271. sts.writer_threadwork.main_wait_for_done();
  272. sts.reader_threadwork.main_wait_for_done();
  273. CHECK_MESSAGE(sts.func1_count >= 3,
  274. "Reader should have read at least three messages");
  275. sts.message_count_to_read = 6 - sts.func1_count;
  276. sts.reader_threadwork.main_start_work();
  277. // The following will fail immediately.
  278. // The reason it hangs indefinitely in engine, is all subsequent calls to
  279. // CommandQueue.wait_and_flush_one will also fail.
  280. sts.reader_threadwork.main_wait_for_done();
  281. // Because looping around uses an extra message, easiest to consume all.
  282. sts.message_count_to_read = -1;
  283. sts.reader_threadwork.main_start_work();
  284. sts.reader_threadwork.main_wait_for_done();
  285. CHECK_MESSAGE(sts.func1_count == 6,
  286. "Reader should have read both message sets");
  287. sts.destroy_threads();
  288. CHECK_MESSAGE(sts.func1_count == 6,
  289. "Reader should have read no additional messages after join");
  290. ProjectSettings::get_singleton()->set_setting(COMMAND_QUEUE_SETTING,
  291. ProjectSettings::get_singleton()->property_get_revert(COMMAND_QUEUE_SETTING));
  292. }
  293. TEST_CASE("[CommandQueue] Test Queue Lapping") {
  294. const char *COMMAND_QUEUE_SETTING = "memory/limits/command_queue/multithreading_queue_size_kb";
  295. ProjectSettings::get_singleton()->set_setting(COMMAND_QUEUE_SETTING, 1);
  296. SharedThreadState sts;
  297. sts.init_threads();
  298. sts.add_msg_to_write(SharedThreadState::TEST_MSG_FUNC1_TRANSFORM);
  299. sts.add_msg_to_write(SharedThreadState::TEST_MSG_FUNC3_TRANSFORMx6);
  300. sts.add_msg_to_write(SharedThreadState::TEST_MSG_FUNC3_TRANSFORMx6);
  301. sts.writer_threadwork.main_start_work();
  302. sts.writer_threadwork.main_wait_for_done();
  303. // We need to read an extra message so that it triggers the dealloc logic once.
  304. // Otherwise, the queue will be considered full.
  305. sts.message_count_to_read = 3;
  306. sts.reader_threadwork.main_start_work();
  307. sts.reader_threadwork.main_wait_for_done();
  308. CHECK_MESSAGE(sts.func1_count == 3,
  309. "Reader should have read first set of messages");
  310. sts.add_msg_to_write(SharedThreadState::TEST_MSG_FUNC3_TRANSFORMx6);
  311. sts.add_msg_to_write(SharedThreadState::TEST_MSG_FUNC3_TRANSFORMx6);
  312. sts.writer_threadwork.main_start_work();
  313. // Don't wait for these, because the queue isn't big enough.
  314. sts.writer_threadwork.main_wait_for_done();
  315. sts.add_msg_to_write(SharedThreadState::TEST_MSG_FUNC2_TRANSFORM_FLOAT);
  316. sts.writer_threadwork.main_start_work();
  317. OS::get_singleton()->delay_usec(1000);
  318. sts.message_count_to_read = 3;
  319. sts.reader_threadwork.main_start_work();
  320. sts.reader_threadwork.main_wait_for_done();
  321. sts.writer_threadwork.main_wait_for_done();
  322. sts.message_count_to_read = -1;
  323. sts.reader_threadwork.main_start_work();
  324. sts.reader_threadwork.main_wait_for_done();
  325. CHECK_MESSAGE(sts.func1_count == 6,
  326. "Reader should have read rest of the messages after lapping writers.");
  327. sts.destroy_threads();
  328. CHECK_MESSAGE(sts.func1_count == 6,
  329. "Reader should have read no additional messages after join");
  330. ProjectSettings::get_singleton()->set_setting(COMMAND_QUEUE_SETTING,
  331. ProjectSettings::get_singleton()->property_get_revert(COMMAND_QUEUE_SETTING));
  332. }
  333. TEST_CASE("[Stress][CommandQueue] Stress test command queue") {
  334. const char *COMMAND_QUEUE_SETTING = "memory/limits/command_queue/multithreading_queue_size_kb";
  335. ProjectSettings::get_singleton()->set_setting(COMMAND_QUEUE_SETTING, 1);
  336. SharedThreadState sts;
  337. sts.init_threads();
  338. RandomNumberGenerator rng;
  339. rng.set_seed(1837267);
  340. int msgs_to_add = 2048;
  341. for (int i = 0; i < msgs_to_add; i++) {
  342. // randi_range is inclusive, so allow any enum value except MAX.
  343. sts.add_msg_to_write((SharedThreadState::TestMsgType)rng.randi_range(0, SharedThreadState::TEST_MSG_MAX - 1));
  344. }
  345. sts.writer_threadwork.main_start_work();
  346. int max_loop_iters = msgs_to_add * 2;
  347. int loop_iters = 0;
  348. while (sts.func1_count < msgs_to_add && loop_iters < max_loop_iters) {
  349. int remaining = (msgs_to_add - sts.func1_count);
  350. sts.message_count_to_read = rng.randi_range(1, remaining < 128 ? remaining : 128);
  351. if (loop_iters % 3 == 0) {
  352. sts.message_count_to_read = -1;
  353. }
  354. sts.reader_threadwork.main_start_work();
  355. sts.reader_threadwork.main_wait_for_done();
  356. loop_iters++;
  357. }
  358. CHECK_MESSAGE(loop_iters < max_loop_iters,
  359. "Reader needed too many iterations to read messages!");
  360. sts.writer_threadwork.main_wait_for_done();
  361. sts.destroy_threads();
  362. CHECK_MESSAGE(sts.func1_count == msgs_to_add,
  363. "Reader should have read no additional messages after join");
  364. ProjectSettings::get_singleton()->set_setting(COMMAND_QUEUE_SETTING,
  365. ProjectSettings::get_singleton()->property_get_revert(COMMAND_QUEUE_SETTING));
  366. }
  367. } // namespace TestCommandQueue
  368. #endif // TEST_COMMAND_QUEUE_H