2 #include "qemu-common.h"
4 #include "thread-pool.h"
10 BlockDriverAIOCB
*aiocb
;
15 static int worker_cb(void *opaque
)
17 WorkerTestData
*data
= opaque
;
18 return __sync_fetch_and_add(&data
->n
, 1);
21 static int long_cb(void *opaque
)
23 WorkerTestData
*data
= opaque
;
24 __sync_fetch_and_add(&data
->n
, 1);
26 __sync_fetch_and_add(&data
->n
, 1);
30 static void done_cb(void *opaque
, int ret
)
32 WorkerTestData
*data
= opaque
;
33 g_assert_cmpint(data
->ret
, ==, -EINPROGRESS
);
37 /* Callbacks are serialized, so no need to use atomic ops. */
41 /* A non-blocking poll of the main AIO context (we cannot use aio_poll
42 * because we do not know the AioContext).
44 static void qemu_aio_wait_nonblocking(void)
50 static void test_submit(void)
52 WorkerTestData data
= { .n
= 0 };
53 thread_pool_submit(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(worker_cb
, &data
, done_cb
, &data
);
63 /* The callbacks are not called until after the first wait. */
65 g_assert_cmpint(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(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 qemu_aio_flush... */
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 /* qemu_aio_flush will execute the rest of the coroutine. */
106 /* Back here after the coroutine has finished. */
108 g_assert_cmpint(active
, ==, 0);
109 g_assert_cmpint(data
.ret
, ==, 0);
112 static void test_submit_many(void)
114 WorkerTestData data
[100];
117 /* Start more work items than there will be threads. */
118 for (i
= 0; i
< 100; i
++) {
120 data
[i
].ret
= -EINPROGRESS
;
121 thread_pool_submit_aio(worker_cb
, &data
[i
], done_cb
, &data
[i
]);
128 for (i
= 0; i
< 100; i
++) {
129 g_assert_cmpint(data
[i
].n
, ==, 1);
130 g_assert_cmpint(data
[i
].ret
, ==, 0);
134 static void test_cancel(void)
136 WorkerTestData data
[100];
140 /* Start more work items than there will be threads, to ensure
145 /* Start long running jobs, to ensure we can cancel some. */
146 for (i
= 0; i
< 100; i
++) {
148 data
[i
].ret
= -EINPROGRESS
;
149 data
[i
].aiocb
= thread_pool_submit_aio(long_cb
, &data
[i
],
153 /* Starting the threads may be left to a bottom half. Let it
154 * run, but do not waste too much time...
157 qemu_aio_wait_nonblocking();
159 /* Wait some time for the threads to start, with some sanity
160 * testing on the behavior of the scheduler...
162 g_assert_cmpint(active
, ==, 100);
164 g_assert_cmpint(active
, >, 50);
166 /* Cancel the jobs that haven't been started yet. */
168 for (i
= 0; i
< 100; i
++) {
169 if (__sync_val_compare_and_swap(&data
[i
].n
, 0, 3) == 0) {
170 data
[i
].ret
= -ECANCELED
;
171 bdrv_aio_cancel(data
[i
].aiocb
);
176 g_assert_cmpint(active
, >, 0);
177 g_assert_cmpint(num_canceled
, <, 100);
179 /* Canceling the others will be a blocking operation. */
180 for (i
= 0; i
< 100; i
++) {
181 if (data
[i
].n
!= 3) {
182 bdrv_aio_cancel(data
[i
].aiocb
);
186 /* Finish execution and execute any remaining callbacks. */
188 g_assert_cmpint(active
, ==, 0);
189 for (i
= 0; i
< 100; i
++) {
190 if (data
[i
].n
== 3) {
191 g_assert_cmpint(data
[i
].ret
, ==, -ECANCELED
);
192 g_assert(data
[i
].aiocb
!= NULL
);
194 g_assert_cmpint(data
[i
].n
, ==, 2);
195 g_assert_cmpint(data
[i
].ret
, ==, 0);
196 g_assert(data
[i
].aiocb
== NULL
);
201 int main(int argc
, char **argv
)
203 /* These should be removed once each AioContext has its thread pool.
204 * The test should create its own AioContext.
206 qemu_init_main_loop();
209 g_test_init(&argc
, &argv
, NULL
);
210 g_test_add_func("/thread-pool/submit", test_submit
);
211 g_test_add_func("/thread-pool/submit-aio", test_submit_aio
);
212 g_test_add_func("/thread-pool/submit-co", test_submit_co
);
213 g_test_add_func("/thread-pool/submit-many", test_submit_many
);
214 g_test_add_func("/thread-pool/cancel", test_cancel
);