1 #include "qemu/osdep.h"
3 #include "block/thread-pool.h"
4 #include "block/block.h"
5 #include "qapi/error.h"
6 #include "qemu/timer.h"
7 #include "qemu/error-report.h"
8 #include "qemu/main-loop.h"
10 static AioContext *ctx;
19 static int worker_cb(void *opaque)
21 WorkerTestData *data = opaque;
22 return qatomic_fetch_inc(&data->n);
25 static int long_cb(void *opaque)
27 WorkerTestData *data = opaque;
28 if (qatomic_cmpxchg(&data->n, 0, 1) == 0) {
30 qatomic_or(&data->n, 2);
35 static void done_cb(void *opaque, int ret)
37 WorkerTestData *data = opaque;
38 g_assert(data->ret == -EINPROGRESS || data->ret == -ECANCELED);
42 /* Callbacks are serialized, so no need to use atomic ops. */
46 static void test_submit(void)
48 WorkerTestData data = { .n = 0 };
49 thread_pool_submit(worker_cb, &data);
53 g_assert_cmpint(data.n, ==, 1);
56 static void test_submit_aio(void)
58 WorkerTestData data = { .n = 0, .ret = -EINPROGRESS };
59 data.aiocb = thread_pool_submit_aio(worker_cb, &data,
62 /* The callbacks are not called until after the first wait. */
64 g_assert_cmpint(data.ret, ==, -EINPROGRESS);
65 while (data.ret == -EINPROGRESS) {
68 g_assert_cmpint(active, ==, 0);
69 g_assert_cmpint(data.n, ==, 1);
70 g_assert_cmpint(data.ret, ==, 0);
73 static void coroutine_fn co_test_cb(void *opaque)
75 WorkerTestData *data = opaque;
79 data->ret = -EINPROGRESS;
80 thread_pool_submit_co(worker_cb, data);
82 /* The test continues in test_submit_co, after qemu_coroutine_enter... */
84 g_assert_cmpint(data->n, ==, 1);
88 /* The test continues in test_submit_co, after aio_poll... */
91 static void test_submit_co(void)
94 Coroutine *co = qemu_coroutine_create(co_test_cb, &data);
96 qemu_coroutine_enter(co);
98 /* Back here once the worker has started. */
100 g_assert_cmpint(active, ==, 1);
101 g_assert_cmpint(data.ret, ==, -EINPROGRESS);
103 /* aio_poll will execute the rest of the coroutine. */
105 while (data.ret == -EINPROGRESS) {
109 /* Back here after the coroutine has finished. */
111 g_assert_cmpint(active, ==, 0);
112 g_assert_cmpint(data.ret, ==, 0);
115 static void test_submit_many(void)
117 WorkerTestData data[100];
120 /* Start more work items than there will be threads. */
121 for (i = 0; i < 100; i++) {
123 data[i].ret = -EINPROGRESS;
124 thread_pool_submit_aio(worker_cb, &data[i], done_cb, &data[i]);
131 for (i = 0; i < 100; i++) {
132 g_assert_cmpint(data[i].n, ==, 1);
133 g_assert_cmpint(data[i].ret, ==, 0);
137 static void do_test_cancel(bool sync)
139 WorkerTestData data[100];
143 /* Start more work items than there will be threads, to ensure
148 /* Start long running jobs, to ensure we can cancel some. */
149 for (i = 0; i < 100; i++) {
151 data[i].ret = -EINPROGRESS;
152 data[i].aiocb = thread_pool_submit_aio(long_cb, &data[i],
156 /* Starting the threads may be left to a bottom half. Let it
157 * run, but do not waste too much time...
161 aio_poll(ctx, false);
163 /* Wait some time for the threads to start, with some sanity
164 * testing on the behavior of the scheduler...
166 g_assert_cmpint(active, ==, 100);
168 g_assert_cmpint(active, >, 50);
170 /* Cancel the jobs that haven't been started yet. */
172 for (i = 0; i < 100; i++) {
173 if (qatomic_cmpxchg(&data[i].n, 0, 4) == 0) {
174 data[i].ret = -ECANCELED;
176 bdrv_aio_cancel(data[i].aiocb);
178 bdrv_aio_cancel_async(data[i].aiocb);
183 g_assert_cmpint(active, >, 0);
184 g_assert_cmpint(num_canceled, <, 100);
186 for (i = 0; i < 100; i++) {
187 if (data[i].aiocb && qatomic_read(&data[i].n) < 4) {
189 /* Canceling the others will be a blocking operation. */
190 bdrv_aio_cancel(data[i].aiocb);
192 bdrv_aio_cancel_async(data[i].aiocb);
197 /* Finish execution and execute any remaining callbacks. */
201 g_assert_cmpint(active, ==, 0);
202 for (i = 0; i < 100; i++) {
203 g_assert(data[i].aiocb == NULL);
206 fprintf(stderr, "Callback not canceled but never started?\n");
209 /* Couldn't be canceled asynchronously, must have completed. */
210 g_assert_cmpint(data[i].ret, ==, 0);
213 /* Could be canceled asynchronously, never started. */
214 g_assert_cmpint(data[i].ret, ==, -ECANCELED);
217 fprintf(stderr, "Callback aborted while running?\n");
223 static void test_cancel(void)
225 do_test_cancel(true);
228 static void test_cancel_async(void)
230 do_test_cancel(false);
233 int main(int argc, char **argv)
235 qemu_init_main_loop(&error_abort);
236 ctx = qemu_get_current_aio_context();
238 g_test_init(&argc, &argv, NULL);
239 g_test_add_func("/thread-pool/submit", test_submit);
240 g_test_add_func("/thread-pool/submit-aio", test_submit_aio);
241 g_test_add_func("/thread-pool/submit-co", test_submit_co);
242 g_test_add_func("/thread-pool/submit-many", test_submit_many);
243 g_test_add_func("/thread-pool/cancel", test_cancel);
244 g_test_add_func("/thread-pool/cancel-async", test_cancel_async);