2 #include "qemu-common.h"
4 #include "thread-pool.h"
10 BlockDriverAIOCB
*aiocb
;
15 static int worker_cb(void *opaque
)
17 WorkerTestData
*data
= opaque
;
18 return __sync_fetch_and_add(&data
->n
, 1);
21 static int long_cb(void *opaque
)
23 WorkerTestData
*data
= opaque
;
24 __sync_fetch_and_add(&data
->n
, 1);
26 __sync_fetch_and_add(&data
->n
, 1);
30 static void done_cb(void *opaque
, int ret
)
32 WorkerTestData
*data
= opaque
;
33 g_assert_cmpint(data
->ret
, ==, -EINPROGRESS
);
37 /* Callbacks are serialized, so no need to use atomic ops. */
41 /* A non-blocking poll of the main AIO context (we cannot use aio_poll
42 * because we do not know the AioContext).
44 static void qemu_aio_wait_nonblocking(void)
50 /* Wait until all aio and bh activity has finished */
51 static void qemu_aio_wait_all(void)
53 while (qemu_aio_wait()) {
58 static void test_submit(void)
60 WorkerTestData data
= { .n
= 0 };
61 thread_pool_submit(worker_cb
, &data
);
63 g_assert_cmpint(data
.n
, ==, 1);
66 static void test_submit_aio(void)
68 WorkerTestData data
= { .n
= 0, .ret
= -EINPROGRESS
};
69 data
.aiocb
= thread_pool_submit_aio(worker_cb
, &data
, done_cb
, &data
);
71 /* The callbacks are not called until after the first wait. */
73 g_assert_cmpint(data
.ret
, ==, -EINPROGRESS
);
75 g_assert_cmpint(active
, ==, 0);
76 g_assert_cmpint(data
.n
, ==, 1);
77 g_assert_cmpint(data
.ret
, ==, 0);
80 static void co_test_cb(void *opaque
)
82 WorkerTestData
*data
= opaque
;
86 data
->ret
= -EINPROGRESS
;
87 thread_pool_submit_co(worker_cb
, data
);
89 /* The test continues in test_submit_co, after qemu_coroutine_enter... */
91 g_assert_cmpint(data
->n
, ==, 1);
95 /* The test continues in test_submit_co, after qemu_aio_wait_all... */
98 static void test_submit_co(void)
101 Coroutine
*co
= qemu_coroutine_create(co_test_cb
);
103 qemu_coroutine_enter(co
, &data
);
105 /* Back here once the worker has started. */
107 g_assert_cmpint(active
, ==, 1);
108 g_assert_cmpint(data
.ret
, ==, -EINPROGRESS
);
110 /* qemu_aio_wait_all will execute the rest of the coroutine. */
114 /* Back here after the coroutine has finished. */
116 g_assert_cmpint(active
, ==, 0);
117 g_assert_cmpint(data
.ret
, ==, 0);
120 static void test_submit_many(void)
122 WorkerTestData data
[100];
125 /* Start more work items than there will be threads. */
126 for (i
= 0; i
< 100; i
++) {
128 data
[i
].ret
= -EINPROGRESS
;
129 thread_pool_submit_aio(worker_cb
, &data
[i
], done_cb
, &data
[i
]);
136 for (i
= 0; i
< 100; i
++) {
137 g_assert_cmpint(data
[i
].n
, ==, 1);
138 g_assert_cmpint(data
[i
].ret
, ==, 0);
142 static void test_cancel(void)
144 WorkerTestData data
[100];
148 /* Start more work items than there will be threads, to ensure
153 /* Start long running jobs, to ensure we can cancel some. */
154 for (i
= 0; i
< 100; i
++) {
156 data
[i
].ret
= -EINPROGRESS
;
157 data
[i
].aiocb
= thread_pool_submit_aio(long_cb
, &data
[i
],
161 /* Starting the threads may be left to a bottom half. Let it
162 * run, but do not waste too much time...
165 qemu_aio_wait_nonblocking();
167 /* Wait some time for the threads to start, with some sanity
168 * testing on the behavior of the scheduler...
170 g_assert_cmpint(active
, ==, 100);
172 g_assert_cmpint(active
, >, 50);
174 /* Cancel the jobs that haven't been started yet. */
176 for (i
= 0; i
< 100; i
++) {
177 if (__sync_val_compare_and_swap(&data
[i
].n
, 0, 3) == 0) {
178 data
[i
].ret
= -ECANCELED
;
179 bdrv_aio_cancel(data
[i
].aiocb
);
184 g_assert_cmpint(active
, >, 0);
185 g_assert_cmpint(num_canceled
, <, 100);
187 /* Canceling the others will be a blocking operation. */
188 for (i
= 0; i
< 100; i
++) {
189 if (data
[i
].n
!= 3) {
190 bdrv_aio_cancel(data
[i
].aiocb
);
194 /* Finish execution and execute any remaining callbacks. */
196 g_assert_cmpint(active
, ==, 0);
197 for (i
= 0; i
< 100; i
++) {
198 if (data
[i
].n
== 3) {
199 g_assert_cmpint(data
[i
].ret
, ==, -ECANCELED
);
200 g_assert(data
[i
].aiocb
!= NULL
);
202 g_assert_cmpint(data
[i
].n
, ==, 2);
203 g_assert_cmpint(data
[i
].ret
, ==, 0);
204 g_assert(data
[i
].aiocb
== NULL
);
209 int main(int argc
, char **argv
)
211 /* These should be removed once each AioContext has its thread pool.
212 * The test should create its own AioContext.
214 qemu_init_main_loop();
217 g_test_init(&argc
, &argv
, NULL
);
218 g_test_add_func("/thread-pool/submit", test_submit
);
219 g_test_add_func("/thread-pool/submit-aio", test_submit_aio
);
220 g_test_add_func("/thread-pool/submit-co", test_submit_co
);
221 g_test_add_func("/thread-pool/submit-many", test_submit_many
);
222 g_test_add_func("/thread-pool/cancel", test_cancel
);