test-thread-pool.c 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  1. #include "qemu/osdep.h"
  2. #include "qemu-common.h"
  3. #include "block/aio.h"
  4. #include "block/thread-pool.h"
  5. #include "block/block.h"
  6. #include "qapi/error.h"
  7. #include "qemu/timer.h"
  8. #include "qemu/error-report.h"
  9. #include "qemu/main-loop.h"
  10. static AioContext *ctx;
  11. static ThreadPool *pool;
  12. static int active;
  13. typedef struct {
  14. BlockAIOCB *aiocb;
  15. int n;
  16. int ret;
  17. } WorkerTestData;
  18. static int worker_cb(void *opaque)
  19. {
  20. WorkerTestData *data = opaque;
  21. return atomic_fetch_inc(&data->n);
  22. }
  23. static int long_cb(void *opaque)
  24. {
  25. WorkerTestData *data = opaque;
  26. if (atomic_cmpxchg(&data->n, 0, 1) == 0) {
  27. g_usleep(2000000);
  28. atomic_or(&data->n, 2);
  29. }
  30. return 0;
  31. }
  32. static void done_cb(void *opaque, int ret)
  33. {
  34. WorkerTestData *data = opaque;
  35. g_assert(data->ret == -EINPROGRESS || data->ret == -ECANCELED);
  36. data->ret = ret;
  37. data->aiocb = NULL;
  38. /* Callbacks are serialized, so no need to use atomic ops. */
  39. active--;
  40. }
  41. static void test_submit(void)
  42. {
  43. WorkerTestData data = { .n = 0 };
  44. thread_pool_submit(pool, worker_cb, &data);
  45. while (data.n == 0) {
  46. aio_poll(ctx, true);
  47. }
  48. g_assert_cmpint(data.n, ==, 1);
  49. }
  50. static void test_submit_aio(void)
  51. {
  52. WorkerTestData data = { .n = 0, .ret = -EINPROGRESS };
  53. data.aiocb = thread_pool_submit_aio(pool, worker_cb, &data,
  54. done_cb, &data);
  55. /* The callbacks are not called until after the first wait. */
  56. active = 1;
  57. g_assert_cmpint(data.ret, ==, -EINPROGRESS);
  58. while (data.ret == -EINPROGRESS) {
  59. aio_poll(ctx, true);
  60. }
  61. g_assert_cmpint(active, ==, 0);
  62. g_assert_cmpint(data.n, ==, 1);
  63. g_assert_cmpint(data.ret, ==, 0);
  64. }
  65. static void co_test_cb(void *opaque)
  66. {
  67. WorkerTestData *data = opaque;
  68. active = 1;
  69. data->n = 0;
  70. data->ret = -EINPROGRESS;
  71. thread_pool_submit_co(pool, worker_cb, data);
  72. /* The test continues in test_submit_co, after qemu_coroutine_enter... */
  73. g_assert_cmpint(data->n, ==, 1);
  74. data->ret = 0;
  75. active--;
  76. /* The test continues in test_submit_co, after aio_poll... */
  77. }
  78. static void test_submit_co(void)
  79. {
  80. WorkerTestData data;
  81. Coroutine *co = qemu_coroutine_create(co_test_cb, &data);
  82. qemu_coroutine_enter(co);
  83. /* Back here once the worker has started. */
  84. g_assert_cmpint(active, ==, 1);
  85. g_assert_cmpint(data.ret, ==, -EINPROGRESS);
  86. /* aio_poll will execute the rest of the coroutine. */
  87. while (data.ret == -EINPROGRESS) {
  88. aio_poll(ctx, true);
  89. }
  90. /* Back here after the coroutine has finished. */
  91. g_assert_cmpint(active, ==, 0);
  92. g_assert_cmpint(data.ret, ==, 0);
  93. }
  94. static void test_submit_many(void)
  95. {
  96. WorkerTestData data[100];
  97. int i;
  98. /* Start more work items than there will be threads. */
  99. for (i = 0; i < 100; i++) {
  100. data[i].n = 0;
  101. data[i].ret = -EINPROGRESS;
  102. thread_pool_submit_aio(pool, worker_cb, &data[i], done_cb, &data[i]);
  103. }
  104. active = 100;
  105. while (active > 0) {
  106. aio_poll(ctx, true);
  107. }
  108. for (i = 0; i < 100; i++) {
  109. g_assert_cmpint(data[i].n, ==, 1);
  110. g_assert_cmpint(data[i].ret, ==, 0);
  111. }
  112. }
  113. static void do_test_cancel(bool sync)
  114. {
  115. WorkerTestData data[100];
  116. int num_canceled;
  117. int i;
  118. /* Start more work items than there will be threads, to ensure
  119. * the pool is full.
  120. */
  121. test_submit_many();
  122. /* Start long running jobs, to ensure we can cancel some. */
  123. for (i = 0; i < 100; i++) {
  124. data[i].n = 0;
  125. data[i].ret = -EINPROGRESS;
  126. data[i].aiocb = thread_pool_submit_aio(pool, long_cb, &data[i],
  127. done_cb, &data[i]);
  128. }
  129. /* Starting the threads may be left to a bottom half. Let it
  130. * run, but do not waste too much time...
  131. */
  132. active = 100;
  133. aio_notify(ctx);
  134. aio_poll(ctx, false);
  135. /* Wait some time for the threads to start, with some sanity
  136. * testing on the behavior of the scheduler...
  137. */
  138. g_assert_cmpint(active, ==, 100);
  139. g_usleep(1000000);
  140. g_assert_cmpint(active, >, 50);
  141. /* Cancel the jobs that haven't been started yet. */
  142. num_canceled = 0;
  143. for (i = 0; i < 100; i++) {
  144. if (atomic_cmpxchg(&data[i].n, 0, 4) == 0) {
  145. data[i].ret = -ECANCELED;
  146. if (sync) {
  147. bdrv_aio_cancel(data[i].aiocb);
  148. } else {
  149. bdrv_aio_cancel_async(data[i].aiocb);
  150. }
  151. num_canceled++;
  152. }
  153. }
  154. g_assert_cmpint(active, >, 0);
  155. g_assert_cmpint(num_canceled, <, 100);
  156. for (i = 0; i < 100; i++) {
  157. if (data[i].aiocb && atomic_read(&data[i].n) < 4) {
  158. if (sync) {
  159. /* Canceling the others will be a blocking operation. */
  160. bdrv_aio_cancel(data[i].aiocb);
  161. } else {
  162. bdrv_aio_cancel_async(data[i].aiocb);
  163. }
  164. }
  165. }
  166. /* Finish execution and execute any remaining callbacks. */
  167. while (active > 0) {
  168. aio_poll(ctx, true);
  169. }
  170. g_assert_cmpint(active, ==, 0);
  171. for (i = 0; i < 100; i++) {
  172. g_assert(data[i].aiocb == NULL);
  173. switch (data[i].n) {
  174. case 0:
  175. fprintf(stderr, "Callback not canceled but never started?\n");
  176. abort();
  177. case 3:
  178. /* Couldn't be canceled asynchronously, must have completed. */
  179. g_assert_cmpint(data[i].ret, ==, 0);
  180. break;
  181. case 4:
  182. /* Could be canceled asynchronously, never started. */
  183. g_assert_cmpint(data[i].ret, ==, -ECANCELED);
  184. break;
  185. default:
  186. fprintf(stderr, "Callback aborted while running?\n");
  187. abort();
  188. }
  189. }
  190. }
  191. static void test_cancel(void)
  192. {
  193. do_test_cancel(true);
  194. }
  195. static void test_cancel_async(void)
  196. {
  197. do_test_cancel(false);
  198. }
  199. int main(int argc, char **argv)
  200. {
  201. qemu_init_main_loop(&error_abort);
  202. ctx = qemu_get_current_aio_context();
  203. pool = aio_get_thread_pool(ctx);
  204. g_test_init(&argc, &argv, NULL);
  205. g_test_add_func("/thread-pool/submit", test_submit);
  206. g_test_add_func("/thread-pool/submit-aio", test_submit_aio);
  207. g_test_add_func("/thread-pool/submit-co", test_submit_co);
  208. g_test_add_func("/thread-pool/submit-many", test_submit_many);
  209. g_test_add_func("/thread-pool/cancel", test_cancel);
  210. g_test_add_func("/thread-pool/cancel-async", test_cancel_async);
  211. return g_test_run();
  212. }