2 #include "qemu-common.h"
4 #include "block/thread-pool.h"
5 #include "block/block.h"
7 static AioContext
*ctx
;
8 static ThreadPool
*pool
;
12 BlockDriverAIOCB
*aiocb
;
17 static int worker_cb(void *opaque
)
19 WorkerTestData
*data
= opaque
;
20 return __sync_fetch_and_add(&data
->n
, 1);
23 static int long_cb(void *opaque
)
25 WorkerTestData
*data
= opaque
;
26 __sync_fetch_and_add(&data
->n
, 1);
28 __sync_fetch_and_add(&data
->n
, 1);
32 static void done_cb(void *opaque
, int ret
)
34 WorkerTestData
*data
= opaque
;
35 g_assert_cmpint(data
->ret
, ==, -EINPROGRESS
);
39 /* Callbacks are serialized, so no need to use atomic ops. */
43 /* Wait until all aio and bh activity has finished */
44 static void qemu_aio_wait_all(void)
46 while (aio_poll(ctx
, true)) {
51 static void test_submit(void)
53 WorkerTestData data
= { .n
= 0 };
54 thread_pool_submit(pool
, worker_cb
, &data
);
56 g_assert_cmpint(data
.n
, ==, 1);
59 static void test_submit_aio(void)
61 WorkerTestData data
= { .n
= 0, .ret
= -EINPROGRESS
};
62 data
.aiocb
= thread_pool_submit_aio(pool
, worker_cb
, &data
,
65 /* The callbacks are not called until after the first wait. */
67 g_assert_cmpint(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 qemu_aio_wait_all... */
92 static void test_submit_co(void)
95 Coroutine
*co
= qemu_coroutine_create(co_test_cb
);
97 qemu_coroutine_enter(co
, &data
);
99 /* Back here once the worker has started. */
101 g_assert_cmpint(active
, ==, 1);
102 g_assert_cmpint(data
.ret
, ==, -EINPROGRESS
);
104 /* qemu_aio_wait_all will execute the rest of the coroutine. */
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 test_cancel(void)
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 (__sync_val_compare_and_swap(&data
[i
].n
, 0, 3) == 0) {
173 data
[i
].ret
= -ECANCELED
;
174 bdrv_aio_cancel(data
[i
].aiocb
);
179 g_assert_cmpint(active
, >, 0);
180 g_assert_cmpint(num_canceled
, <, 100);
182 /* Canceling the others will be a blocking operation. */
183 for (i
= 0; i
< 100; i
++) {
184 if (data
[i
].n
!= 3) {
185 bdrv_aio_cancel(data
[i
].aiocb
);
189 /* Finish execution and execute any remaining callbacks. */
191 g_assert_cmpint(active
, ==, 0);
192 for (i
= 0; i
< 100; i
++) {
193 if (data
[i
].n
== 3) {
194 g_assert_cmpint(data
[i
].ret
, ==, -ECANCELED
);
195 g_assert(data
[i
].aiocb
!= NULL
);
197 g_assert_cmpint(data
[i
].n
, ==, 2);
198 g_assert_cmpint(data
[i
].ret
, ==, 0);
199 g_assert(data
[i
].aiocb
== NULL
);
204 int main(int argc
, char **argv
)
208 ctx
= aio_context_new();
209 pool
= aio_get_thread_pool(ctx
);
211 g_test_init(&argc
, &argv
, NULL
);
212 g_test_add_func("/thread-pool/submit", test_submit
);
213 g_test_add_func("/thread-pool/submit-aio", test_submit_aio
);
214 g_test_add_func("/thread-pool/submit-co", test_submit_co
);
215 g_test_add_func("/thread-pool/submit-many", test_submit_many
);
216 g_test_add_func("/thread-pool/cancel", test_cancel
);
220 aio_context_unref(ctx
);