test-aio-multithread.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460
  1. /*
  2. * AioContext multithreading tests
  3. *
  4. * Copyright Red Hat, Inc. 2016
  5. *
  6. * Authors:
  7. * Paolo Bonzini <pbonzini@redhat.com>
  8. *
  9. * This work is licensed under the terms of the GNU LGPL, version 2 or later.
  10. * See the COPYING.LIB file in the top-level directory.
  11. */
  12. #include "qemu/osdep.h"
  13. #include "block/aio.h"
  14. #include "qemu/coroutine.h"
  15. #include "qemu/thread.h"
  16. #include "qemu/error-report.h"
  17. #include "iothread.h"
  18. /* AioContext management */
  19. #define NUM_CONTEXTS 5
  20. static IOThread *threads[NUM_CONTEXTS];
  21. static AioContext *ctx[NUM_CONTEXTS];
  22. static __thread int id = -1;
  23. static QemuEvent done_event;
  24. /* Run a function synchronously on a remote iothread. */
  25. typedef struct CtxRunData {
  26. QEMUBHFunc *cb;
  27. void *arg;
  28. } CtxRunData;
  29. static void ctx_run_bh_cb(void *opaque)
  30. {
  31. CtxRunData *data = opaque;
  32. data->cb(data->arg);
  33. qemu_event_set(&done_event);
  34. }
  35. static void ctx_run(int i, QEMUBHFunc *cb, void *opaque)
  36. {
  37. CtxRunData data = {
  38. .cb = cb,
  39. .arg = opaque
  40. };
  41. qemu_event_reset(&done_event);
  42. aio_bh_schedule_oneshot(ctx[i], ctx_run_bh_cb, &data);
  43. qemu_event_wait(&done_event);
  44. }
  45. /* Starting the iothreads. */
  46. static void set_id_cb(void *opaque)
  47. {
  48. int *i = opaque;
  49. id = *i;
  50. }
  51. static void create_aio_contexts(void)
  52. {
  53. int i;
  54. for (i = 0; i < NUM_CONTEXTS; i++) {
  55. threads[i] = iothread_new();
  56. ctx[i] = iothread_get_aio_context(threads[i]);
  57. }
  58. qemu_event_init(&done_event, false);
  59. for (i = 0; i < NUM_CONTEXTS; i++) {
  60. ctx_run(i, set_id_cb, &i);
  61. }
  62. }
  63. /* Stopping the iothreads. */
  64. static void join_aio_contexts(void)
  65. {
  66. int i;
  67. for (i = 0; i < NUM_CONTEXTS; i++) {
  68. aio_context_ref(ctx[i]);
  69. }
  70. for (i = 0; i < NUM_CONTEXTS; i++) {
  71. iothread_join(threads[i]);
  72. }
  73. for (i = 0; i < NUM_CONTEXTS; i++) {
  74. aio_context_unref(ctx[i]);
  75. }
  76. qemu_event_destroy(&done_event);
  77. }
  78. /* Basic test for the stuff above. */
  79. static void test_lifecycle(void)
  80. {
  81. create_aio_contexts();
  82. join_aio_contexts();
  83. }
  84. /* aio_co_schedule test. */
  85. static Coroutine *to_schedule[NUM_CONTEXTS];
  86. static bool now_stopping;
  87. static int count_retry;
  88. static int count_here;
  89. static int count_other;
  90. static bool schedule_next(int n)
  91. {
  92. Coroutine *co;
  93. co = atomic_xchg(&to_schedule[n], NULL);
  94. if (!co) {
  95. atomic_inc(&count_retry);
  96. return false;
  97. }
  98. if (n == id) {
  99. atomic_inc(&count_here);
  100. } else {
  101. atomic_inc(&count_other);
  102. }
  103. aio_co_schedule(ctx[n], co);
  104. return true;
  105. }
  106. static void finish_cb(void *opaque)
  107. {
  108. schedule_next(id);
  109. }
  110. static coroutine_fn void test_multi_co_schedule_entry(void *opaque)
  111. {
  112. g_assert(to_schedule[id] == NULL);
  113. while (!atomic_mb_read(&now_stopping)) {
  114. int n;
  115. n = g_test_rand_int_range(0, NUM_CONTEXTS);
  116. schedule_next(n);
  117. atomic_mb_set(&to_schedule[id], qemu_coroutine_self());
  118. qemu_coroutine_yield();
  119. g_assert(to_schedule[id] == NULL);
  120. }
  121. }
  122. static void test_multi_co_schedule(int seconds)
  123. {
  124. int i;
  125. count_here = count_other = count_retry = 0;
  126. now_stopping = false;
  127. create_aio_contexts();
  128. for (i = 0; i < NUM_CONTEXTS; i++) {
  129. Coroutine *co1 = qemu_coroutine_create(test_multi_co_schedule_entry, NULL);
  130. aio_co_schedule(ctx[i], co1);
  131. }
  132. g_usleep(seconds * 1000000);
  133. atomic_mb_set(&now_stopping, true);
  134. for (i = 0; i < NUM_CONTEXTS; i++) {
  135. ctx_run(i, finish_cb, NULL);
  136. to_schedule[i] = NULL;
  137. }
  138. join_aio_contexts();
  139. g_test_message("scheduled %d, queued %d, retry %d, total %d",
  140. count_other, count_here, count_retry,
  141. count_here + count_other + count_retry);
  142. }
  143. static void test_multi_co_schedule_1(void)
  144. {
  145. test_multi_co_schedule(1);
  146. }
  147. static void test_multi_co_schedule_10(void)
  148. {
  149. test_multi_co_schedule(10);
  150. }
  151. /* CoMutex thread-safety. */
  152. static uint32_t atomic_counter;
  153. static uint32_t running;
  154. static uint32_t counter;
  155. static CoMutex comutex;
  156. static void coroutine_fn test_multi_co_mutex_entry(void *opaque)
  157. {
  158. while (!atomic_mb_read(&now_stopping)) {
  159. qemu_co_mutex_lock(&comutex);
  160. counter++;
  161. qemu_co_mutex_unlock(&comutex);
  162. /* Increase atomic_counter *after* releasing the mutex. Otherwise
  163. * there is a chance (it happens about 1 in 3 runs) that the iothread
  164. * exits before the coroutine is woken up, causing a spurious
  165. * assertion failure.
  166. */
  167. atomic_inc(&atomic_counter);
  168. }
  169. atomic_dec(&running);
  170. }
  171. static void test_multi_co_mutex(int threads, int seconds)
  172. {
  173. int i;
  174. qemu_co_mutex_init(&comutex);
  175. counter = 0;
  176. atomic_counter = 0;
  177. now_stopping = false;
  178. create_aio_contexts();
  179. assert(threads <= NUM_CONTEXTS);
  180. running = threads;
  181. for (i = 0; i < threads; i++) {
  182. Coroutine *co1 = qemu_coroutine_create(test_multi_co_mutex_entry, NULL);
  183. aio_co_schedule(ctx[i], co1);
  184. }
  185. g_usleep(seconds * 1000000);
  186. atomic_mb_set(&now_stopping, true);
  187. while (running > 0) {
  188. g_usleep(100000);
  189. }
  190. join_aio_contexts();
  191. g_test_message("%d iterations/second", counter / seconds);
  192. g_assert_cmpint(counter, ==, atomic_counter);
  193. }
  194. /* Testing with NUM_CONTEXTS threads focuses on the queue. The mutex however
  195. * is too contended (and the threads spend too much time in aio_poll)
  196. * to actually stress the handoff protocol.
  197. */
  198. static void test_multi_co_mutex_1(void)
  199. {
  200. test_multi_co_mutex(NUM_CONTEXTS, 1);
  201. }
  202. static void test_multi_co_mutex_10(void)
  203. {
  204. test_multi_co_mutex(NUM_CONTEXTS, 10);
  205. }
  206. /* Testing with fewer threads stresses the handoff protocol too. Still, the
  207. * case where the locker _can_ pick up a handoff is very rare, happening
  208. * about 10 times in 1 million, so increase the runtime a bit compared to
  209. * other "quick" testcases that only run for 1 second.
  210. */
  211. static void test_multi_co_mutex_2_3(void)
  212. {
  213. test_multi_co_mutex(2, 3);
  214. }
  215. static void test_multi_co_mutex_2_30(void)
  216. {
  217. test_multi_co_mutex(2, 30);
  218. }
  219. /* Same test with fair mutexes, for performance comparison. */
  220. #ifdef CONFIG_LINUX
  221. #include "qemu/futex.h"
  222. /* The nodes for the mutex reside in this structure (on which we try to avoid
  223. * false sharing). The head of the mutex is in the "mutex_head" variable.
  224. */
  225. static struct {
  226. int next, locked;
  227. int padding[14];
  228. } nodes[NUM_CONTEXTS] __attribute__((__aligned__(64)));
  229. static int mutex_head = -1;
  230. static void mcs_mutex_lock(void)
  231. {
  232. int prev;
  233. nodes[id].next = -1;
  234. nodes[id].locked = 1;
  235. prev = atomic_xchg(&mutex_head, id);
  236. if (prev != -1) {
  237. atomic_set(&nodes[prev].next, id);
  238. qemu_futex_wait(&nodes[id].locked, 1);
  239. }
  240. }
  241. static void mcs_mutex_unlock(void)
  242. {
  243. int next;
  244. if (atomic_read(&nodes[id].next) == -1) {
  245. if (atomic_read(&mutex_head) == id &&
  246. atomic_cmpxchg(&mutex_head, id, -1) == id) {
  247. /* Last item in the list, exit. */
  248. return;
  249. }
  250. while (atomic_read(&nodes[id].next) == -1) {
  251. /* mcs_mutex_lock did the xchg, but has not updated
  252. * nodes[prev].next yet.
  253. */
  254. }
  255. }
  256. /* Wake up the next in line. */
  257. next = atomic_read(&nodes[id].next);
  258. nodes[next].locked = 0;
  259. qemu_futex_wake(&nodes[next].locked, 1);
  260. }
  261. static void test_multi_fair_mutex_entry(void *opaque)
  262. {
  263. while (!atomic_mb_read(&now_stopping)) {
  264. mcs_mutex_lock();
  265. counter++;
  266. mcs_mutex_unlock();
  267. atomic_inc(&atomic_counter);
  268. }
  269. atomic_dec(&running);
  270. }
  271. static void test_multi_fair_mutex(int threads, int seconds)
  272. {
  273. int i;
  274. assert(mutex_head == -1);
  275. counter = 0;
  276. atomic_counter = 0;
  277. now_stopping = false;
  278. create_aio_contexts();
  279. assert(threads <= NUM_CONTEXTS);
  280. running = threads;
  281. for (i = 0; i < threads; i++) {
  282. Coroutine *co1 = qemu_coroutine_create(test_multi_fair_mutex_entry, NULL);
  283. aio_co_schedule(ctx[i], co1);
  284. }
  285. g_usleep(seconds * 1000000);
  286. atomic_mb_set(&now_stopping, true);
  287. while (running > 0) {
  288. g_usleep(100000);
  289. }
  290. join_aio_contexts();
  291. g_test_message("%d iterations/second", counter / seconds);
  292. g_assert_cmpint(counter, ==, atomic_counter);
  293. }
  294. static void test_multi_fair_mutex_1(void)
  295. {
  296. test_multi_fair_mutex(NUM_CONTEXTS, 1);
  297. }
  298. static void test_multi_fair_mutex_10(void)
  299. {
  300. test_multi_fair_mutex(NUM_CONTEXTS, 10);
  301. }
  302. #endif
  303. /* Same test with pthread mutexes, for performance comparison and
  304. * portability. */
  305. static QemuMutex mutex;
  306. static void test_multi_mutex_entry(void *opaque)
  307. {
  308. while (!atomic_mb_read(&now_stopping)) {
  309. qemu_mutex_lock(&mutex);
  310. counter++;
  311. qemu_mutex_unlock(&mutex);
  312. atomic_inc(&atomic_counter);
  313. }
  314. atomic_dec(&running);
  315. }
  316. static void test_multi_mutex(int threads, int seconds)
  317. {
  318. int i;
  319. qemu_mutex_init(&mutex);
  320. counter = 0;
  321. atomic_counter = 0;
  322. now_stopping = false;
  323. create_aio_contexts();
  324. assert(threads <= NUM_CONTEXTS);
  325. running = threads;
  326. for (i = 0; i < threads; i++) {
  327. Coroutine *co1 = qemu_coroutine_create(test_multi_mutex_entry, NULL);
  328. aio_co_schedule(ctx[i], co1);
  329. }
  330. g_usleep(seconds * 1000000);
  331. atomic_mb_set(&now_stopping, true);
  332. while (running > 0) {
  333. g_usleep(100000);
  334. }
  335. join_aio_contexts();
  336. g_test_message("%d iterations/second", counter / seconds);
  337. g_assert_cmpint(counter, ==, atomic_counter);
  338. }
  339. static void test_multi_mutex_1(void)
  340. {
  341. test_multi_mutex(NUM_CONTEXTS, 1);
  342. }
  343. static void test_multi_mutex_10(void)
  344. {
  345. test_multi_mutex(NUM_CONTEXTS, 10);
  346. }
  347. /* End of tests. */
  348. int main(int argc, char **argv)
  349. {
  350. init_clocks(NULL);
  351. g_test_init(&argc, &argv, NULL);
  352. g_test_add_func("/aio/multi/lifecycle", test_lifecycle);
  353. if (g_test_quick()) {
  354. g_test_add_func("/aio/multi/schedule", test_multi_co_schedule_1);
  355. g_test_add_func("/aio/multi/mutex/contended", test_multi_co_mutex_1);
  356. g_test_add_func("/aio/multi/mutex/handoff", test_multi_co_mutex_2_3);
  357. #ifdef CONFIG_LINUX
  358. g_test_add_func("/aio/multi/mutex/mcs", test_multi_fair_mutex_1);
  359. #endif
  360. g_test_add_func("/aio/multi/mutex/pthread", test_multi_mutex_1);
  361. } else {
  362. g_test_add_func("/aio/multi/schedule", test_multi_co_schedule_10);
  363. g_test_add_func("/aio/multi/mutex/contended", test_multi_co_mutex_10);
  364. g_test_add_func("/aio/multi/mutex/handoff", test_multi_co_mutex_2_30);
  365. #ifdef CONFIG_LINUX
  366. g_test_add_func("/aio/multi/mutex/mcs", test_multi_fair_mutex_10);
  367. #endif
  368. g_test_add_func("/aio/multi/mutex/pthread", test_multi_mutex_10);
  369. }
  370. return g_test_run();
  371. }