]>
git.proxmox.com Git - mirror_qemu.git/blob - tests/test-thread-pool.c
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 atomic_fetch_inc(&data
->n
);
23 static int long_cb(void *opaque
)
25 WorkerTestData
*data
= opaque
;
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 static void test_submit(void)
45 WorkerTestData data
= { .n
= 0 };
46 thread_pool_submit(pool
, worker_cb
, &data
);
50 g_assert_cmpint(data
.n
, ==, 1);
53 static void test_submit_aio(void)
55 WorkerTestData data
= { .n
= 0, .ret
= -EINPROGRESS
};
56 data
.aiocb
= thread_pool_submit_aio(pool
, worker_cb
, &data
,
59 /* The callbacks are not called until after the first wait. */
61 g_assert_cmpint(data
.ret
, ==, -EINPROGRESS
);
62 while (data
.ret
== -EINPROGRESS
) {
65 g_assert_cmpint(active
, ==, 0);
66 g_assert_cmpint(data
.n
, ==, 1);
67 g_assert_cmpint(data
.ret
, ==, 0);
70 static void co_test_cb(void *opaque
)
72 WorkerTestData
*data
= opaque
;
76 data
->ret
= -EINPROGRESS
;
77 thread_pool_submit_co(pool
, worker_cb
, data
);
79 /* The test continues in test_submit_co, after qemu_coroutine_enter... */
81 g_assert_cmpint(data
->n
, ==, 1);
85 /* The test continues in test_submit_co, after qemu_aio_wait_all... */
88 static void test_submit_co(void)
91 Coroutine
*co
= qemu_coroutine_create(co_test_cb
);
93 qemu_coroutine_enter(co
, &data
);
95 /* Back here once the worker has started. */
97 g_assert_cmpint(active
, ==, 1);
98 g_assert_cmpint(data
.ret
, ==, -EINPROGRESS
);
100 /* qemu_aio_wait_all will execute the rest of the coroutine. */
102 while (data
.ret
== -EINPROGRESS
) {
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(pool
, 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(pool
, 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...
158 aio_poll(ctx
, false);
160 /* Wait some time for the threads to start, with some sanity
161 * testing on the behavior of the scheduler...
163 g_assert_cmpint(active
, ==, 100);
165 g_assert_cmpint(active
, >, 50);
167 /* Cancel the jobs that haven't been started yet. */
169 for (i
= 0; i
< 100; i
++) {
170 if (atomic_cmpxchg(&data
[i
].n
, 0, 3) == 0) {
171 data
[i
].ret
= -ECANCELED
;
172 bdrv_aio_cancel(data
[i
].aiocb
);
177 g_assert_cmpint(active
, >, 0);
178 g_assert_cmpint(num_canceled
, <, 100);
180 /* Canceling the others will be a blocking operation. */
181 for (i
= 0; i
< 100; i
++) {
182 if (data
[i
].n
!= 3) {
183 bdrv_aio_cancel(data
[i
].aiocb
);
187 /* 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
);