]>
git.proxmox.com Git - qemu.git/blob - tests/test-thread-pool.c
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];
139 /* Start more work items than there will be threads, to ensure
144 /* Start long running jobs, to ensure we can cancel some. */
145 for (i
= 0; i
< 100; i
++) {
147 data
[i
].ret
= -EINPROGRESS
;
148 data
[i
].aiocb
= thread_pool_submit_aio(long_cb
, &data
[i
],
152 /* Starting the threads may be left to a bottom half. Let it
153 * run, but do not waste too much time...
156 qemu_aio_wait_nonblocking();
158 /* Wait some time for the threads to start, with some sanity
159 * testing on the behavior of the scheduler...
161 g_assert_cmpint(active
, ==, 100);
163 g_assert_cmpint(active
, >, 50);
165 /* Cancel the jobs that haven't been started yet. */
166 for (i
= 0; i
< 100; i
++) {
167 if (__sync_val_compare_and_swap(&data
[i
].n
, 0, 3) == 0) {
168 data
[i
].ret
= -ECANCELED
;
169 bdrv_aio_cancel(data
[i
].aiocb
);
173 g_assert_cmpint(active
, >, 5);
174 g_assert_cmpint(active
, <, 95);
176 /* Canceling the others will be a blocking operation. */
177 for (i
= 0; i
< 100; i
++) {
178 if (data
[i
].n
!= 3) {
179 bdrv_aio_cancel(data
[i
].aiocb
);
183 /* Finish execution and execute any remaining callbacks. */
185 g_assert_cmpint(active
, ==, 0);
186 for (i
= 0; i
< 100; i
++) {
187 if (data
[i
].n
== 3) {
188 g_assert_cmpint(data
[i
].ret
, ==, -ECANCELED
);
189 g_assert(data
[i
].aiocb
!= NULL
);
191 g_assert_cmpint(data
[i
].n
, ==, 2);
192 g_assert_cmpint(data
[i
].ret
, ==, 0);
193 g_assert(data
[i
].aiocb
== NULL
);
198 int main(int argc
, char **argv
)
200 /* These should be removed once each AioContext has its thread pool.
201 * The test should create its own AioContext.
203 qemu_init_main_loop();
206 g_test_init(&argc
, &argv
, NULL
);
207 g_test_add_func("/thread-pool/submit", test_submit
);
208 g_test_add_func("/thread-pool/submit-aio", test_submit_aio
);
209 g_test_add_func("/thread-pool/submit-co", test_submit_co
);
210 g_test_add_func("/thread-pool/submit-many", test_submit_many
);
211 g_test_add_func("/thread-pool/cancel", test_cancel
);