2 #include "qemu-common.h"
4 #include "block/thread-pool.h"
5 #include "block/block.h"
6 #include "qemu/timer.h"
8 static AioContext *ctx;
9 static ThreadPool *pool;
13 BlockDriverAIOCB *aiocb;
18 static int worker_cb(void *opaque)
20 WorkerTestData *data = opaque;
21 return atomic_fetch_inc(&data->n);
24 static int long_cb(void *opaque)
26 WorkerTestData *data = opaque;
33 static void done_cb(void *opaque, int ret)
35 WorkerTestData *data = opaque;
36 g_assert_cmpint(data->ret, ==, -EINPROGRESS);
40 /* Callbacks are serialized, so no need to use atomic ops. */
44 static void test_submit(void)
46 WorkerTestData data = { .n = 0 };
47 thread_pool_submit(pool, worker_cb, &data);
51 g_assert_cmpint(data.n, ==, 1);
54 static void test_submit_aio(void)
56 WorkerTestData data = { .n = 0, .ret = -EINPROGRESS };
57 data.aiocb = thread_pool_submit_aio(pool, worker_cb, &data,
60 /* The callbacks are not called until after the first wait. */
62 g_assert_cmpint(data.ret, ==, -EINPROGRESS);
63 while (data.ret == -EINPROGRESS) {
66 g_assert_cmpint(active, ==, 0);
67 g_assert_cmpint(data.n, ==, 1);
68 g_assert_cmpint(data.ret, ==, 0);
71 static void co_test_cb(void *opaque)
73 WorkerTestData *data = opaque;
77 data->ret = -EINPROGRESS;
78 thread_pool_submit_co(pool, worker_cb, data);
80 /* The test continues in test_submit_co, after qemu_coroutine_enter... */
82 g_assert_cmpint(data->n, ==, 1);
86 /* The test continues in test_submit_co, after qemu_aio_wait_all... */
89 static void test_submit_co(void)
92 Coroutine *co = qemu_coroutine_create(co_test_cb);
94 qemu_coroutine_enter(co, &data);
96 /* Back here once the worker has started. */
98 g_assert_cmpint(active, ==, 1);
99 g_assert_cmpint(data.ret, ==, -EINPROGRESS);
101 /* qemu_aio_wait_all will execute the rest of the coroutine. */
103 while (data.ret == -EINPROGRESS) {
107 /* Back here after the coroutine has finished. */
109 g_assert_cmpint(active, ==, 0);
110 g_assert_cmpint(data.ret, ==, 0);
113 static void test_submit_many(void)
115 WorkerTestData data[100];
118 /* Start more work items than there will be threads. */
119 for (i = 0; i < 100; i++) {
121 data[i].ret = -EINPROGRESS;
122 thread_pool_submit_aio(pool, worker_cb, &data[i], done_cb, &data[i]);
129 for (i = 0; i < 100; i++) {
130 g_assert_cmpint(data[i].n, ==, 1);
131 g_assert_cmpint(data[i].ret, ==, 0);
135 static void test_cancel(void)
137 WorkerTestData data[100];
141 /* Start more work items than there will be threads, to ensure
146 /* Start long running jobs, to ensure we can cancel some. */
147 for (i = 0; i < 100; i++) {
149 data[i].ret = -EINPROGRESS;
150 data[i].aiocb = thread_pool_submit_aio(pool, long_cb, &data[i],
154 /* Starting the threads may be left to a bottom half. Let it
155 * run, but do not waste too much time...
159 aio_poll(ctx, false);
161 /* Wait some time for the threads to start, with some sanity
162 * testing on the behavior of the scheduler...
164 g_assert_cmpint(active, ==, 100);
166 g_assert_cmpint(active, >, 50);
168 /* Cancel the jobs that haven't been started yet. */
170 for (i = 0; i < 100; i++) {
171 if (atomic_cmpxchg(&data[i].n, 0, 3) == 0) {
172 data[i].ret = -ECANCELED;
173 bdrv_aio_cancel(data[i].aiocb);
178 g_assert_cmpint(active, >, 0);
179 g_assert_cmpint(num_canceled, <, 100);
181 /* Canceling the others will be a blocking operation. */
182 for (i = 0; i < 100; i++) {
183 if (data[i].n != 3) {
184 bdrv_aio_cancel(data[i].aiocb);
188 /* Finish execution and execute any remaining callbacks. */
192 g_assert_cmpint(active, ==, 0);
193 for (i = 0; i < 100; i++) {
194 if (data[i].n == 3) {
195 g_assert_cmpint(data[i].ret, ==, -ECANCELED);
196 g_assert(data[i].aiocb != NULL);
198 g_assert_cmpint(data[i].n, ==, 2);
199 g_assert_cmpint(data[i].ret, ==, 0);
200 g_assert(data[i].aiocb == NULL);
205 int main(int argc, char **argv)
211 ctx = aio_context_new();
212 pool = aio_get_thread_pool(ctx);
214 g_test_init(&argc, &argv, NULL);
215 g_test_add_func("/thread-pool/submit", test_submit);
216 g_test_add_func("/thread-pool/submit-aio", test_submit_aio);
217 g_test_add_func("/thread-pool/submit-co", test_submit_co);
218 g_test_add_func("/thread-pool/submit-many", test_submit_many);
219 g_test_add_func("/thread-pool/cancel", test_cancel);
223 aio_context_unref(ctx);