]> git.proxmox.com Git - qemu.git/blob - tests/test-thread-pool.c
tests: add thread pool unit tests
[qemu.git] / tests / test-thread-pool.c
1 #include <glib.h>
2 #include "qemu-common.h"
3 #include "qemu-aio.h"
4 #include "thread-pool.h"
5 #include "block.h"
6
7 static int active;
8
9 typedef struct {
10 BlockDriverAIOCB *aiocb;
11 int n;
12 int ret;
13 } WorkerTestData;
14
15 static int worker_cb(void *opaque)
16 {
17 WorkerTestData *data = opaque;
18 return __sync_fetch_and_add(&data->n, 1);
19 }
20
21 static int long_cb(void *opaque)
22 {
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;
28 }
29
30 static void done_cb(void *opaque, int ret)
31 {
32 WorkerTestData *data = opaque;
33 g_assert_cmpint(data->ret, ==, -EINPROGRESS);
34 data->ret = ret;
35 data->aiocb = NULL;
36
37 /* Callbacks are serialized, so no need to use atomic ops. */
38 active--;
39 }
40
41 /* A non-blocking poll of the main AIO context (we cannot use aio_poll
42 * because we do not know the AioContext).
43 */
44 static void qemu_aio_wait_nonblocking(void)
45 {
46 qemu_notify_event();
47 qemu_aio_wait();
48 }
49
50 static void test_submit(void)
51 {
52 WorkerTestData data = { .n = 0 };
53 thread_pool_submit(worker_cb, &data);
54 qemu_aio_flush();
55 g_assert_cmpint(data.n, ==, 1);
56 }
57
58 static void test_submit_aio(void)
59 {
60 WorkerTestData data = { .n = 0, .ret = -EINPROGRESS };
61 data.aiocb = thread_pool_submit_aio(worker_cb, &data, done_cb, &data);
62
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);
70 }
71
72 static void co_test_cb(void *opaque)
73 {
74 WorkerTestData *data = opaque;
75
76 active = 1;
77 data->n = 0;
78 data->ret = -EINPROGRESS;
79 thread_pool_submit_co(worker_cb, data);
80
81 /* The test continues in test_submit_co, after qemu_coroutine_enter... */
82
83 g_assert_cmpint(data->n, ==, 1);
84 data->ret = 0;
85 active--;
86
87 /* The test continues in test_submit_co, after qemu_aio_flush... */
88 }
89
90 static void test_submit_co(void)
91 {
92 WorkerTestData data;
93 Coroutine *co = qemu_coroutine_create(co_test_cb);
94
95 qemu_coroutine_enter(co, &data);
96
97 /* Back here once the worker has started. */
98
99 g_assert_cmpint(active, ==, 1);
100 g_assert_cmpint(data.ret, ==, -EINPROGRESS);
101
102 /* qemu_aio_flush will execute the rest of the coroutine. */
103
104 qemu_aio_flush();
105
106 /* Back here after the coroutine has finished. */
107
108 g_assert_cmpint(active, ==, 0);
109 g_assert_cmpint(data.ret, ==, 0);
110 }
111
112 static void test_submit_many(void)
113 {
114 WorkerTestData data[100];
115 int i;
116
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]);
122 }
123
124 active = 100;
125 while (active > 0) {
126 qemu_aio_wait();
127 }
128 for (i = 0; i < 100; i++) {
129 g_assert_cmpint(data[i].n, ==, 1);
130 g_assert_cmpint(data[i].ret, ==, 0);
131 }
132 }
133
134 static void test_cancel(void)
135 {
136 WorkerTestData data[100];
137 int i;
138
139 /* Start more work items than there will be threads, to ensure
140 * the pool is full.
141 */
142 test_submit_many();
143
144 /* Start long running jobs, to ensure we can cancel some. */
145 for (i = 0; i < 100; i++) {
146 data[i].n = 0;
147 data[i].ret = -EINPROGRESS;
148 data[i].aiocb = thread_pool_submit_aio(long_cb, &data[i],
149 done_cb, &data[i]);
150 }
151
152 /* Starting the threads may be left to a bottom half. Let it
153 * run, but do not waste too much time...
154 */
155 active = 100;
156 qemu_aio_wait_nonblocking();
157
158 /* Wait some time for the threads to start, with some sanity
159 * testing on the behavior of the scheduler...
160 */
161 g_assert_cmpint(active, ==, 100);
162 g_usleep(1000000);
163 g_assert_cmpint(active, >, 50);
164
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);
170 active--;
171 }
172 }
173 g_assert_cmpint(active, >, 5);
174 g_assert_cmpint(active, <, 95);
175
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);
180 }
181 }
182
183 /* Finish execution and execute any remaining callbacks. */
184 qemu_aio_flush();
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);
190 } else {
191 g_assert_cmpint(data[i].n, ==, 2);
192 g_assert_cmpint(data[i].ret, ==, 0);
193 g_assert(data[i].aiocb == NULL);
194 }
195 }
196 }
197
198 int main(int argc, char **argv)
199 {
200 /* These should be removed once each AioContext has its thread pool.
201 * The test should create its own AioContext.
202 */
203 qemu_init_main_loop();
204 bdrv_init();
205
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);
212 return g_test_run();
213 }