2 #include "qemu-common.h"
4 #include "block/thread-pool.h"
5 #include "block/block.h"
6 #include "qemu/timer.h"
7 #include "qemu/error-report.h"
9 static AioContext
*ctx
;
10 static ThreadPool
*pool
;
19 static int worker_cb(void *opaque
)
21 WorkerTestData
*data
= opaque
;
22 return atomic_fetch_inc(&data
->n
);
25 static int long_cb(void *opaque
)
27 WorkerTestData
*data
= opaque
;
34 static void done_cb(void *opaque
, int ret
)
36 WorkerTestData
*data
= opaque
;
37 g_assert(data
->ret
== -EINPROGRESS
|| data
->ret
== -ECANCELED
);
41 /* Callbacks are serialized, so no need to use atomic ops. */
45 static void test_submit(void)
47 WorkerTestData data
= { .n
= 0 };
48 thread_pool_submit(pool
, worker_cb
, &data
);
52 g_assert_cmpint(data
.n
, ==, 1);
55 static void test_submit_aio(void)
57 WorkerTestData data
= { .n
= 0, .ret
= -EINPROGRESS
};
58 data
.aiocb
= thread_pool_submit_aio(pool
, worker_cb
, &data
,
61 /* The callbacks are not called until after the first wait. */
63 g_assert_cmpint(data
.ret
, ==, -EINPROGRESS
);
64 while (data
.ret
== -EINPROGRESS
) {
67 g_assert_cmpint(active
, ==, 0);
68 g_assert_cmpint(data
.n
, ==, 1);
69 g_assert_cmpint(data
.ret
, ==, 0);
72 static void co_test_cb(void *opaque
)
74 WorkerTestData
*data
= opaque
;
78 data
->ret
= -EINPROGRESS
;
79 thread_pool_submit_co(pool
, worker_cb
, data
);
81 /* The test continues in test_submit_co, after qemu_coroutine_enter... */
83 g_assert_cmpint(data
->n
, ==, 1);
87 /* The test continues in test_submit_co, after aio_poll... */
90 static void test_submit_co(void)
93 Coroutine
*co
= qemu_coroutine_create(co_test_cb
);
95 qemu_coroutine_enter(co
, &data
);
97 /* Back here once the worker has started. */
99 g_assert_cmpint(active
, ==, 1);
100 g_assert_cmpint(data
.ret
, ==, -EINPROGRESS
);
102 /* aio_poll will execute the rest of the coroutine. */
104 while (data
.ret
== -EINPROGRESS
) {
108 /* Back here after the coroutine has finished. */
110 g_assert_cmpint(active
, ==, 0);
111 g_assert_cmpint(data
.ret
, ==, 0);
114 static void test_submit_many(void)
116 WorkerTestData data
[100];
119 /* Start more work items than there will be threads. */
120 for (i
= 0; i
< 100; i
++) {
122 data
[i
].ret
= -EINPROGRESS
;
123 thread_pool_submit_aio(pool
, worker_cb
, &data
[i
], done_cb
, &data
[i
]);
130 for (i
= 0; i
< 100; i
++) {
131 g_assert_cmpint(data
[i
].n
, ==, 1);
132 g_assert_cmpint(data
[i
].ret
, ==, 0);
136 static void do_test_cancel(bool sync
)
138 WorkerTestData data
[100];
142 /* Start more work items than there will be threads, to ensure
147 /* Start long running jobs, to ensure we can cancel some. */
148 for (i
= 0; i
< 100; i
++) {
150 data
[i
].ret
= -EINPROGRESS
;
151 data
[i
].aiocb
= thread_pool_submit_aio(pool
, long_cb
, &data
[i
],
155 /* Starting the threads may be left to a bottom half. Let it
156 * run, but do not waste too much time...
160 aio_poll(ctx
, false);
162 /* Wait some time for the threads to start, with some sanity
163 * testing on the behavior of the scheduler...
165 g_assert_cmpint(active
, ==, 100);
167 g_assert_cmpint(active
, >, 50);
169 /* Cancel the jobs that haven't been started yet. */
171 for (i
= 0; i
< 100; i
++) {
172 if (atomic_cmpxchg(&data
[i
].n
, 0, 3) == 0) {
173 data
[i
].ret
= -ECANCELED
;
175 bdrv_aio_cancel(data
[i
].aiocb
);
177 bdrv_aio_cancel_async(data
[i
].aiocb
);
182 g_assert_cmpint(active
, >, 0);
183 g_assert_cmpint(num_canceled
, <, 100);
185 for (i
= 0; i
< 100; i
++) {
186 if (data
[i
].aiocb
&& data
[i
].n
!= 3) {
188 /* Canceling the others will be a blocking operation. */
189 bdrv_aio_cancel(data
[i
].aiocb
);
191 bdrv_aio_cancel_async(data
[i
].aiocb
);
196 /* Finish execution and execute any remaining callbacks. */
200 g_assert_cmpint(active
, ==, 0);
201 for (i
= 0; i
< 100; i
++) {
202 if (data
[i
].n
== 3) {
203 g_assert_cmpint(data
[i
].ret
, ==, -ECANCELED
);
204 g_assert(data
[i
].aiocb
== NULL
);
206 g_assert_cmpint(data
[i
].n
, ==, 2);
207 g_assert(data
[i
].ret
== 0 || data
[i
].ret
== -ECANCELED
);
208 g_assert(data
[i
].aiocb
== NULL
);
213 static void test_cancel(void)
215 do_test_cancel(true);
218 static void test_cancel_async(void)
220 do_test_cancel(false);
223 int main(int argc
, char **argv
)
226 Error
*local_error
= NULL
;
230 ctx
= aio_context_new(&local_error
);
232 error_report("Failed to create AIO Context: '%s'",
233 error_get_pretty(local_error
));
234 error_free(local_error
);
237 pool
= aio_get_thread_pool(ctx
);
239 g_test_init(&argc
, &argv
, NULL
);
240 g_test_add_func("/thread-pool/submit", test_submit
);
241 g_test_add_func("/thread-pool/submit-aio", test_submit_aio
);
242 g_test_add_func("/thread-pool/submit-co", test_submit_co
);
243 g_test_add_func("/thread-pool/submit-many", test_submit_many
);
244 g_test_add_func("/thread-pool/cancel", test_cancel
);
245 g_test_add_func("/thread-pool/cancel-async", test_cancel_async
);
249 aio_context_unref(ctx
);