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
;
36 static void done_cb(void *opaque
, int ret
)
38 WorkerTestData
*data
= opaque
;
39 g_assert(data
->ret
== -EINPROGRESS
|| data
->ret
== -ECANCELED
);
43 /* Callbacks are serialized, so no need to use atomic ops. */
47 static void test_submit(void)
49 WorkerTestData data
= { .n
= 0 };
50 thread_pool_submit(pool
, worker_cb
, &data
);
54 g_assert_cmpint(data
.n
, ==, 1);
57 static void test_submit_aio(void)
59 WorkerTestData data
= { .n
= 0, .ret
= -EINPROGRESS
};
60 data
.aiocb
= thread_pool_submit_aio(pool
, worker_cb
, &data
,
63 /* The callbacks are not called until after the first wait. */
65 g_assert_cmpint(data
.ret
, ==, -EINPROGRESS
);
66 while (data
.ret
== -EINPROGRESS
) {
69 g_assert_cmpint(active
, ==, 0);
70 g_assert_cmpint(data
.n
, ==, 1);
71 g_assert_cmpint(data
.ret
, ==, 0);
74 static void co_test_cb(void *opaque
)
76 WorkerTestData
*data
= opaque
;
80 data
->ret
= -EINPROGRESS
;
81 thread_pool_submit_co(pool
, worker_cb
, data
);
83 /* The test continues in test_submit_co, after qemu_coroutine_enter... */
85 g_assert_cmpint(data
->n
, ==, 1);
89 /* The test continues in test_submit_co, after aio_poll... */
92 static void test_submit_co(void)
95 Coroutine
*co
= qemu_coroutine_create(co_test_cb
, &data
);
97 qemu_coroutine_enter(co
);
99 /* Back here once the worker has started. */
101 g_assert_cmpint(active
, ==, 1);
102 g_assert_cmpint(data
.ret
, ==, -EINPROGRESS
);
104 /* aio_poll will execute the rest of the coroutine. */
106 while (data
.ret
== -EINPROGRESS
) {
110 /* Back here after the coroutine has finished. */
112 g_assert_cmpint(active
, ==, 0);
113 g_assert_cmpint(data
.ret
, ==, 0);
116 static void test_submit_many(void)
118 WorkerTestData data
[100];
121 /* Start more work items than there will be threads. */
122 for (i
= 0; i
< 100; i
++) {
124 data
[i
].ret
= -EINPROGRESS
;
125 thread_pool_submit_aio(pool
, worker_cb
, &data
[i
], done_cb
, &data
[i
]);
132 for (i
= 0; i
< 100; i
++) {
133 g_assert_cmpint(data
[i
].n
, ==, 1);
134 g_assert_cmpint(data
[i
].ret
, ==, 0);
138 static void do_test_cancel(bool sync
)
140 WorkerTestData data
[100];
144 /* Start more work items than there will be threads, to ensure
149 /* Start long running jobs, to ensure we can cancel some. */
150 for (i
= 0; i
< 100; i
++) {
152 data
[i
].ret
= -EINPROGRESS
;
153 data
[i
].aiocb
= thread_pool_submit_aio(pool
, long_cb
, &data
[i
],
157 /* Starting the threads may be left to a bottom half. Let it
158 * run, but do not waste too much time...
162 aio_poll(ctx
, false);
164 /* Wait some time for the threads to start, with some sanity
165 * testing on the behavior of the scheduler...
167 g_assert_cmpint(active
, ==, 100);
169 g_assert_cmpint(active
, >, 50);
171 /* Cancel the jobs that haven't been started yet. */
173 for (i
= 0; i
< 100; i
++) {
174 if (atomic_cmpxchg(&data
[i
].n
, 0, 3) == 0) {
175 data
[i
].ret
= -ECANCELED
;
177 bdrv_aio_cancel(data
[i
].aiocb
);
179 bdrv_aio_cancel_async(data
[i
].aiocb
);
184 g_assert_cmpint(active
, >, 0);
185 g_assert_cmpint(num_canceled
, <, 100);
187 for (i
= 0; i
< 100; i
++) {
188 if (data
[i
].aiocb
&& data
[i
].n
!= 3) {
190 /* Canceling the others will be a blocking operation. */
191 bdrv_aio_cancel(data
[i
].aiocb
);
193 bdrv_aio_cancel_async(data
[i
].aiocb
);
198 /* Finish execution and execute any remaining callbacks. */
202 g_assert_cmpint(active
, ==, 0);
203 for (i
= 0; i
< 100; i
++) {
204 if (data
[i
].n
== 3) {
205 g_assert_cmpint(data
[i
].ret
, ==, -ECANCELED
);
206 g_assert(data
[i
].aiocb
== NULL
);
208 g_assert_cmpint(data
[i
].n
, ==, 2);
209 g_assert(data
[i
].ret
== 0 || data
[i
].ret
== -ECANCELED
);
210 g_assert(data
[i
].aiocb
== NULL
);
215 static void test_cancel(void)
217 do_test_cancel(true);
220 static void test_cancel_async(void)
222 do_test_cancel(false);
225 int main(int argc
, char **argv
)
227 qemu_init_main_loop(&error_abort
);
228 ctx
= qemu_get_current_aio_context();
229 pool
= aio_get_thread_pool(ctx
);
231 g_test_init(&argc
, &argv
, NULL
);
232 g_test_add_func("/thread-pool/submit", test_submit
);
233 g_test_add_func("/thread-pool/submit-aio", test_submit_aio
);
234 g_test_add_func("/thread-pool/submit-co", test_submit_co
);
235 g_test_add_func("/thread-pool/submit-many", test_submit_many
);
236 g_test_add_func("/thread-pool/cancel", test_cancel
);
237 g_test_add_func("/thread-pool/cancel-async", test_cancel_async
);