1 #include "qemu/osdep.h"
2 #include "qemu-common.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"
11 static AioContext
*ctx
;
12 static ThreadPool
*pool
;
21 static int worker_cb(void *opaque
)
23 WorkerTestData
*data
= opaque
;
24 return atomic_fetch_inc(&data
->n
);
27 static int long_cb(void *opaque
)
29 WorkerTestData
*data
= opaque
;
30 if (atomic_cmpxchg(&data
->n
, 0, 1) == 0) {
32 atomic_or(&data
->n
, 2);
37 static void done_cb(void *opaque
, int ret
)
39 WorkerTestData
*data
= opaque
;
40 g_assert(data
->ret
== -EINPROGRESS
|| data
->ret
== -ECANCELED
);
44 /* Callbacks are serialized, so no need to use atomic ops. */
48 static void test_submit(void)
50 WorkerTestData data
= { .n
= 0 };
51 thread_pool_submit(pool
, worker_cb
, &data
);
55 g_assert_cmpint(data
.n
, ==, 1);
58 static void test_submit_aio(void)
60 WorkerTestData data
= { .n
= 0, .ret
= -EINPROGRESS
};
61 data
.aiocb
= thread_pool_submit_aio(pool
, worker_cb
, &data
,
64 /* The callbacks are not called until after the first wait. */
66 g_assert_cmpint(data
.ret
, ==, -EINPROGRESS
);
67 while (data
.ret
== -EINPROGRESS
) {
70 g_assert_cmpint(active
, ==, 0);
71 g_assert_cmpint(data
.n
, ==, 1);
72 g_assert_cmpint(data
.ret
, ==, 0);
75 static void co_test_cb(void *opaque
)
77 WorkerTestData
*data
= opaque
;
81 data
->ret
= -EINPROGRESS
;
82 thread_pool_submit_co(pool
, worker_cb
, data
);
84 /* The test continues in test_submit_co, after qemu_coroutine_enter... */
86 g_assert_cmpint(data
->n
, ==, 1);
90 /* The test continues in test_submit_co, after aio_poll... */
93 static void test_submit_co(void)
96 Coroutine
*co
= qemu_coroutine_create(co_test_cb
, &data
);
98 qemu_coroutine_enter(co
);
100 /* Back here once the worker has started. */
102 g_assert_cmpint(active
, ==, 1);
103 g_assert_cmpint(data
.ret
, ==, -EINPROGRESS
);
105 /* aio_poll will execute the rest of the coroutine. */
107 while (data
.ret
== -EINPROGRESS
) {
111 /* Back here after the coroutine has finished. */
113 g_assert_cmpint(active
, ==, 0);
114 g_assert_cmpint(data
.ret
, ==, 0);
117 static void test_submit_many(void)
119 WorkerTestData data
[100];
122 /* Start more work items than there will be threads. */
123 for (i
= 0; i
< 100; i
++) {
125 data
[i
].ret
= -EINPROGRESS
;
126 thread_pool_submit_aio(pool
, worker_cb
, &data
[i
], done_cb
, &data
[i
]);
133 for (i
= 0; i
< 100; i
++) {
134 g_assert_cmpint(data
[i
].n
, ==, 1);
135 g_assert_cmpint(data
[i
].ret
, ==, 0);
139 static void do_test_cancel(bool sync
)
141 WorkerTestData data
[100];
145 /* Start more work items than there will be threads, to ensure
150 /* Start long running jobs, to ensure we can cancel some. */
151 for (i
= 0; i
< 100; i
++) {
153 data
[i
].ret
= -EINPROGRESS
;
154 data
[i
].aiocb
= thread_pool_submit_aio(pool
, long_cb
, &data
[i
],
158 /* Starting the threads may be left to a bottom half. Let it
159 * run, but do not waste too much time...
163 aio_poll(ctx
, false);
165 /* Wait some time for the threads to start, with some sanity
166 * testing on the behavior of the scheduler...
168 g_assert_cmpint(active
, ==, 100);
170 g_assert_cmpint(active
, >, 50);
172 /* Cancel the jobs that haven't been started yet. */
174 for (i
= 0; i
< 100; i
++) {
175 if (atomic_cmpxchg(&data
[i
].n
, 0, 4) == 0) {
176 data
[i
].ret
= -ECANCELED
;
178 bdrv_aio_cancel(data
[i
].aiocb
);
180 bdrv_aio_cancel_async(data
[i
].aiocb
);
185 g_assert_cmpint(active
, >, 0);
186 g_assert_cmpint(num_canceled
, <, 100);
188 for (i
= 0; i
< 100; i
++) {
189 if (data
[i
].aiocb
&& atomic_read(&data
[i
].n
) < 4) {
191 /* Canceling the others will be a blocking operation. */
192 bdrv_aio_cancel(data
[i
].aiocb
);
194 bdrv_aio_cancel_async(data
[i
].aiocb
);
199 /* Finish execution and execute any remaining callbacks. */
203 g_assert_cmpint(active
, ==, 0);
204 for (i
= 0; i
< 100; i
++) {
205 g_assert(data
[i
].aiocb
== NULL
);
208 fprintf(stderr
, "Callback not canceled but never started?\n");
211 /* Couldn't be canceled asynchronously, must have completed. */
212 g_assert_cmpint(data
[i
].ret
, ==, 0);
215 /* Could be canceled asynchronously, never started. */
216 g_assert_cmpint(data
[i
].ret
, ==, -ECANCELED
);
219 fprintf(stderr
, "Callback aborted while running?\n");
225 static void test_cancel(void)
227 do_test_cancel(true);
230 static void test_cancel_async(void)
232 do_test_cancel(false);
235 int main(int argc
, char **argv
)
237 qemu_init_main_loop(&error_abort
);
238 ctx
= qemu_get_current_aio_context();
239 pool
= aio_get_thread_pool(ctx
);
241 g_test_init(&argc
, &argv
, NULL
);
242 g_test_add_func("/thread-pool/submit", test_submit
);
243 g_test_add_func("/thread-pool/submit-aio", test_submit_aio
);
244 g_test_add_func("/thread-pool/submit-co", test_submit_co
);
245 g_test_add_func("/thread-pool/submit-many", test_submit_many
);
246 g_test_add_func("/thread-pool/cancel", test_cancel
);
247 g_test_add_func("/thread-pool/cancel-async", test_cancel_async
);