openpic: unify memory api subregions
[qemu/agraf.git] / tests / test-thread-pool.c
blobfea0445fb47c9591e96dddb81a8003a3b8dc387c
1 #include <glib.h>
2 #include "qemu-common.h"
3 #include "qemu-aio.h"
4 #include "thread-pool.h"
5 #include "block.h"
7 static int active;
9 typedef struct {
10 BlockDriverAIOCB *aiocb;
11 int n;
12 int ret;
13 } WorkerTestData;
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);
25 g_usleep(2000000);
26 __sync_fetch_and_add(&data->n, 1);
27 return 0;
30 static void done_cb(void *opaque, int ret)
32 WorkerTestData *data = opaque;
33 g_assert_cmpint(data->ret, ==, -EINPROGRESS);
34 data->ret = ret;
35 data->aiocb = NULL;
37 /* Callbacks are serialized, so no need to use atomic ops. */
38 active--;
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)
46 qemu_notify_event();
47 qemu_aio_wait();
50 static void test_submit(void)
52 WorkerTestData data = { .n = 0 };
53 thread_pool_submit(worker_cb, &data);
54 qemu_aio_flush();
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. */
64 active = 1;
65 g_assert_cmpint(data.ret, ==, -EINPROGRESS);
66 qemu_aio_flush();
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;
76 active = 1;
77 data->n = 0;
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);
84 data->ret = 0;
85 active--;
87 /* The test continues in test_submit_co, after qemu_aio_flush... */
90 static void test_submit_co(void)
92 WorkerTestData data;
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. */
104 qemu_aio_flush();
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];
115 int i;
117 /* Start more work items than there will be threads. */
118 for (i = 0; i < 100; i++) {
119 data[i].n = 0;
120 data[i].ret = -EINPROGRESS;
121 thread_pool_submit_aio(worker_cb, &data[i], done_cb, &data[i]);
124 active = 100;
125 while (active > 0) {
126 qemu_aio_wait();
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];
137 int num_canceled;
138 int i;
140 /* Start more work items than there will be threads, to ensure
141 * the pool is full.
143 test_submit_many();
145 /* Start long running jobs, to ensure we can cancel some. */
146 for (i = 0; i < 100; i++) {
147 data[i].n = 0;
148 data[i].ret = -EINPROGRESS;
149 data[i].aiocb = thread_pool_submit_aio(long_cb, &data[i],
150 done_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...
156 active = 100;
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);
163 g_usleep(1000000);
164 g_assert_cmpint(active, >, 50);
166 /* Cancel the jobs that haven't been started yet. */
167 num_canceled = 0;
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);
172 active--;
173 num_canceled++;
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. */
187 qemu_aio_flush();
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);
193 } else {
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();
207 bdrv_init();
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);
215 return g_test_run();