]>
Commit | Line | Data |
---|---|---|
b2ea25d7 PB |
1 | /* |
2 | * AioContext tests | |
3 | * | |
4 | * Copyright Red Hat, Inc. 2012 | |
5 | * | |
6 | * Authors: | |
7 | * Paolo Bonzini <pbonzini@redhat.com> | |
8 | * | |
9 | * This work is licensed under the terms of the GNU LGPL, version 2 or later. | |
10 | * See the COPYING.LIB file in the top-level directory. | |
11 | */ | |
12 | ||
13 | #include <glib.h> | |
737e150e | 14 | #include "block/aio.h" |
dae21b98 | 15 | #include "qemu/timer.h" |
a94a3fac | 16 | #include "qemu/sockets.h" |
2f78e491 | 17 | #include "qemu/error-report.h" |
b2ea25d7 | 18 | |
748bfb4e | 19 | static AioContext *ctx; |
b2ea25d7 | 20 | |
24d1a6d9 SH |
21 | typedef struct { |
22 | EventNotifier e; | |
23 | int n; | |
24 | int active; | |
25 | bool auto_set; | |
26 | } EventNotifierTestData; | |
27 | ||
24d1a6d9 SH |
28 | /* Wait until event notifier becomes inactive */ |
29 | static void wait_until_inactive(EventNotifierTestData *data) | |
30 | { | |
31 | while (data->active > 0) { | |
32 | aio_poll(ctx, true); | |
33 | } | |
34 | } | |
35 | ||
b2ea25d7 PB |
36 | /* Simple callbacks for testing. */ |
37 | ||
38 | typedef struct { | |
39 | QEMUBH *bh; | |
40 | int n; | |
41 | int max; | |
42 | } BHTestData; | |
43 | ||
b53edf97 AB |
44 | typedef struct { |
45 | QEMUTimer timer; | |
46 | QEMUClockType clock_type; | |
47 | int n; | |
48 | int max; | |
49 | int64_t ns; | |
50 | AioContext *ctx; | |
51 | } TimerTestData; | |
52 | ||
b2ea25d7 PB |
53 | static void bh_test_cb(void *opaque) |
54 | { | |
55 | BHTestData *data = opaque; | |
56 | if (++data->n < data->max) { | |
57 | qemu_bh_schedule(data->bh); | |
58 | } | |
59 | } | |
60 | ||
b53edf97 AB |
61 | static void timer_test_cb(void *opaque) |
62 | { | |
63 | TimerTestData *data = opaque; | |
64 | if (++data->n < data->max) { | |
65 | timer_mod(&data->timer, | |
66 | qemu_clock_get_ns(data->clock_type) + data->ns); | |
67 | } | |
68 | } | |
69 | ||
363285d4 | 70 | static void dummy_io_handler_read(EventNotifier *e) |
b53edf97 AB |
71 | { |
72 | } | |
73 | ||
b2ea25d7 PB |
74 | static void bh_delete_cb(void *opaque) |
75 | { | |
76 | BHTestData *data = opaque; | |
77 | if (++data->n < data->max) { | |
78 | qemu_bh_schedule(data->bh); | |
79 | } else { | |
80 | qemu_bh_delete(data->bh); | |
81 | data->bh = NULL; | |
82 | } | |
83 | } | |
84 | ||
b2ea25d7 PB |
85 | static void event_ready_cb(EventNotifier *e) |
86 | { | |
87 | EventNotifierTestData *data = container_of(e, EventNotifierTestData, e); | |
88 | g_assert(event_notifier_test_and_clear(e)); | |
89 | data->n++; | |
90 | if (data->active > 0) { | |
91 | data->active--; | |
92 | } | |
93 | if (data->auto_set && data->active) { | |
94 | event_notifier_set(e); | |
95 | } | |
96 | } | |
97 | ||
98 | /* Tests using aio_*. */ | |
99 | ||
100 | static void test_notify(void) | |
101 | { | |
102 | g_assert(!aio_poll(ctx, false)); | |
103 | aio_notify(ctx); | |
104 | g_assert(!aio_poll(ctx, true)); | |
105 | g_assert(!aio_poll(ctx, false)); | |
106 | } | |
107 | ||
98563fc3 SH |
108 | typedef struct { |
109 | QemuMutex start_lock; | |
a0710f79 | 110 | EventNotifier notifier; |
98563fc3 SH |
111 | bool thread_acquired; |
112 | } AcquireTestData; | |
113 | ||
114 | static void *test_acquire_thread(void *opaque) | |
115 | { | |
116 | AcquireTestData *data = opaque; | |
117 | ||
118 | /* Wait for other thread to let us start */ | |
119 | qemu_mutex_lock(&data->start_lock); | |
120 | qemu_mutex_unlock(&data->start_lock); | |
121 | ||
a0710f79 PB |
122 | g_usleep(500000); |
123 | event_notifier_set(&data->notifier); | |
98563fc3 SH |
124 | aio_context_acquire(ctx); |
125 | aio_context_release(ctx); | |
126 | ||
127 | data->thread_acquired = true; /* success, we got here */ | |
128 | ||
129 | return NULL; | |
130 | } | |
131 | ||
a0710f79 | 132 | static void dummy_notifier_read(EventNotifier *n) |
98563fc3 | 133 | { |
a0710f79 | 134 | event_notifier_test_and_clear(n); |
98563fc3 SH |
135 | } |
136 | ||
137 | static void test_acquire(void) | |
138 | { | |
139 | QemuThread thread; | |
98563fc3 SH |
140 | AcquireTestData data; |
141 | ||
142 | /* Dummy event notifier ensures aio_poll() will block */ | |
a0710f79 PB |
143 | event_notifier_init(&data.notifier, false); |
144 | aio_set_event_notifier(ctx, &data.notifier, dummy_notifier_read); | |
98563fc3 SH |
145 | g_assert(!aio_poll(ctx, false)); /* consume aio_notify() */ |
146 | ||
147 | qemu_mutex_init(&data.start_lock); | |
148 | qemu_mutex_lock(&data.start_lock); | |
149 | data.thread_acquired = false; | |
150 | ||
151 | qemu_thread_create(&thread, "test_acquire_thread", | |
152 | test_acquire_thread, | |
153 | &data, QEMU_THREAD_JOINABLE); | |
154 | ||
155 | /* Block in aio_poll(), let other thread kick us and acquire context */ | |
156 | aio_context_acquire(ctx); | |
157 | qemu_mutex_unlock(&data.start_lock); /* let the thread run */ | |
a0710f79 PB |
158 | g_assert(aio_poll(ctx, true)); |
159 | g_assert(!data.thread_acquired); | |
98563fc3 SH |
160 | aio_context_release(ctx); |
161 | ||
162 | qemu_thread_join(&thread); | |
a0710f79 PB |
163 | aio_set_event_notifier(ctx, &data.notifier, NULL); |
164 | event_notifier_cleanup(&data.notifier); | |
98563fc3 SH |
165 | |
166 | g_assert(data.thread_acquired); | |
167 | } | |
168 | ||
b2ea25d7 PB |
169 | static void test_bh_schedule(void) |
170 | { | |
171 | BHTestData data = { .n = 0 }; | |
172 | data.bh = aio_bh_new(ctx, bh_test_cb, &data); | |
173 | ||
174 | qemu_bh_schedule(data.bh); | |
175 | g_assert_cmpint(data.n, ==, 0); | |
176 | ||
177 | g_assert(aio_poll(ctx, true)); | |
178 | g_assert_cmpint(data.n, ==, 1); | |
179 | ||
180 | g_assert(!aio_poll(ctx, false)); | |
181 | g_assert_cmpint(data.n, ==, 1); | |
182 | qemu_bh_delete(data.bh); | |
183 | } | |
184 | ||
185 | static void test_bh_schedule10(void) | |
186 | { | |
187 | BHTestData data = { .n = 0, .max = 10 }; | |
188 | data.bh = aio_bh_new(ctx, bh_test_cb, &data); | |
189 | ||
190 | qemu_bh_schedule(data.bh); | |
191 | g_assert_cmpint(data.n, ==, 0); | |
192 | ||
193 | g_assert(aio_poll(ctx, false)); | |
194 | g_assert_cmpint(data.n, ==, 1); | |
195 | ||
196 | g_assert(aio_poll(ctx, true)); | |
197 | g_assert_cmpint(data.n, ==, 2); | |
198 | ||
acfb23ad PB |
199 | while (data.n < 10) { |
200 | aio_poll(ctx, true); | |
201 | } | |
b2ea25d7 PB |
202 | g_assert_cmpint(data.n, ==, 10); |
203 | ||
204 | g_assert(!aio_poll(ctx, false)); | |
205 | g_assert_cmpint(data.n, ==, 10); | |
206 | qemu_bh_delete(data.bh); | |
207 | } | |
208 | ||
209 | static void test_bh_cancel(void) | |
210 | { | |
211 | BHTestData data = { .n = 0 }; | |
212 | data.bh = aio_bh_new(ctx, bh_test_cb, &data); | |
213 | ||
214 | qemu_bh_schedule(data.bh); | |
215 | g_assert_cmpint(data.n, ==, 0); | |
216 | ||
217 | qemu_bh_cancel(data.bh); | |
218 | g_assert_cmpint(data.n, ==, 0); | |
219 | ||
220 | g_assert(!aio_poll(ctx, false)); | |
221 | g_assert_cmpint(data.n, ==, 0); | |
222 | qemu_bh_delete(data.bh); | |
223 | } | |
224 | ||
225 | static void test_bh_delete(void) | |
226 | { | |
227 | BHTestData data = { .n = 0 }; | |
228 | data.bh = aio_bh_new(ctx, bh_test_cb, &data); | |
229 | ||
230 | qemu_bh_schedule(data.bh); | |
231 | g_assert_cmpint(data.n, ==, 0); | |
232 | ||
233 | qemu_bh_delete(data.bh); | |
234 | g_assert_cmpint(data.n, ==, 0); | |
235 | ||
236 | g_assert(!aio_poll(ctx, false)); | |
237 | g_assert_cmpint(data.n, ==, 0); | |
238 | } | |
239 | ||
240 | static void test_bh_delete_from_cb(void) | |
241 | { | |
242 | BHTestData data1 = { .n = 0, .max = 1 }; | |
243 | ||
244 | data1.bh = aio_bh_new(ctx, bh_delete_cb, &data1); | |
245 | ||
246 | qemu_bh_schedule(data1.bh); | |
247 | g_assert_cmpint(data1.n, ==, 0); | |
248 | ||
acfb23ad PB |
249 | while (data1.n < data1.max) { |
250 | aio_poll(ctx, true); | |
251 | } | |
b2ea25d7 PB |
252 | g_assert_cmpint(data1.n, ==, data1.max); |
253 | g_assert(data1.bh == NULL); | |
254 | ||
255 | g_assert(!aio_poll(ctx, false)); | |
b2ea25d7 PB |
256 | } |
257 | ||
258 | static void test_bh_delete_from_cb_many(void) | |
259 | { | |
260 | BHTestData data1 = { .n = 0, .max = 1 }; | |
261 | BHTestData data2 = { .n = 0, .max = 3 }; | |
262 | BHTestData data3 = { .n = 0, .max = 2 }; | |
263 | BHTestData data4 = { .n = 0, .max = 4 }; | |
264 | ||
265 | data1.bh = aio_bh_new(ctx, bh_delete_cb, &data1); | |
266 | data2.bh = aio_bh_new(ctx, bh_delete_cb, &data2); | |
267 | data3.bh = aio_bh_new(ctx, bh_delete_cb, &data3); | |
268 | data4.bh = aio_bh_new(ctx, bh_delete_cb, &data4); | |
269 | ||
270 | qemu_bh_schedule(data1.bh); | |
271 | qemu_bh_schedule(data2.bh); | |
272 | qemu_bh_schedule(data3.bh); | |
273 | qemu_bh_schedule(data4.bh); | |
274 | g_assert_cmpint(data1.n, ==, 0); | |
275 | g_assert_cmpint(data2.n, ==, 0); | |
276 | g_assert_cmpint(data3.n, ==, 0); | |
277 | g_assert_cmpint(data4.n, ==, 0); | |
278 | ||
279 | g_assert(aio_poll(ctx, false)); | |
280 | g_assert_cmpint(data1.n, ==, 1); | |
281 | g_assert_cmpint(data2.n, ==, 1); | |
282 | g_assert_cmpint(data3.n, ==, 1); | |
283 | g_assert_cmpint(data4.n, ==, 1); | |
284 | g_assert(data1.bh == NULL); | |
285 | ||
acfb23ad PB |
286 | while (data1.n < data1.max || |
287 | data2.n < data2.max || | |
288 | data3.n < data3.max || | |
289 | data4.n < data4.max) { | |
290 | aio_poll(ctx, true); | |
291 | } | |
b2ea25d7 PB |
292 | g_assert_cmpint(data1.n, ==, data1.max); |
293 | g_assert_cmpint(data2.n, ==, data2.max); | |
294 | g_assert_cmpint(data3.n, ==, data3.max); | |
295 | g_assert_cmpint(data4.n, ==, data4.max); | |
296 | g_assert(data1.bh == NULL); | |
297 | g_assert(data2.bh == NULL); | |
298 | g_assert(data3.bh == NULL); | |
299 | g_assert(data4.bh == NULL); | |
300 | } | |
301 | ||
302 | static void test_bh_flush(void) | |
303 | { | |
304 | BHTestData data = { .n = 0 }; | |
305 | data.bh = aio_bh_new(ctx, bh_test_cb, &data); | |
306 | ||
307 | qemu_bh_schedule(data.bh); | |
308 | g_assert_cmpint(data.n, ==, 0); | |
309 | ||
acfb23ad | 310 | g_assert(aio_poll(ctx, true)); |
b2ea25d7 PB |
311 | g_assert_cmpint(data.n, ==, 1); |
312 | ||
313 | g_assert(!aio_poll(ctx, false)); | |
314 | g_assert_cmpint(data.n, ==, 1); | |
315 | qemu_bh_delete(data.bh); | |
316 | } | |
317 | ||
318 | static void test_set_event_notifier(void) | |
319 | { | |
320 | EventNotifierTestData data = { .n = 0, .active = 0 }; | |
321 | event_notifier_init(&data.e, false); | |
f2e5dca4 | 322 | aio_set_event_notifier(ctx, &data.e, event_ready_cb); |
b2ea25d7 PB |
323 | g_assert(!aio_poll(ctx, false)); |
324 | g_assert_cmpint(data.n, ==, 0); | |
325 | ||
f2e5dca4 | 326 | aio_set_event_notifier(ctx, &data.e, NULL); |
b2ea25d7 PB |
327 | g_assert(!aio_poll(ctx, false)); |
328 | g_assert_cmpint(data.n, ==, 0); | |
329 | event_notifier_cleanup(&data.e); | |
330 | } | |
331 | ||
332 | static void test_wait_event_notifier(void) | |
333 | { | |
334 | EventNotifierTestData data = { .n = 0, .active = 1 }; | |
335 | event_notifier_init(&data.e, false); | |
f2e5dca4 | 336 | aio_set_event_notifier(ctx, &data.e, event_ready_cb); |
164a101f | 337 | g_assert(!aio_poll(ctx, false)); |
b2ea25d7 PB |
338 | g_assert_cmpint(data.n, ==, 0); |
339 | g_assert_cmpint(data.active, ==, 1); | |
340 | ||
341 | event_notifier_set(&data.e); | |
342 | g_assert(aio_poll(ctx, false)); | |
343 | g_assert_cmpint(data.n, ==, 1); | |
344 | g_assert_cmpint(data.active, ==, 0); | |
345 | ||
346 | g_assert(!aio_poll(ctx, false)); | |
347 | g_assert_cmpint(data.n, ==, 1); | |
348 | g_assert_cmpint(data.active, ==, 0); | |
349 | ||
f2e5dca4 | 350 | aio_set_event_notifier(ctx, &data.e, NULL); |
b2ea25d7 PB |
351 | g_assert(!aio_poll(ctx, false)); |
352 | g_assert_cmpint(data.n, ==, 1); | |
353 | ||
354 | event_notifier_cleanup(&data.e); | |
355 | } | |
356 | ||
357 | static void test_flush_event_notifier(void) | |
358 | { | |
359 | EventNotifierTestData data = { .n = 0, .active = 10, .auto_set = true }; | |
360 | event_notifier_init(&data.e, false); | |
f2e5dca4 | 361 | aio_set_event_notifier(ctx, &data.e, event_ready_cb); |
164a101f | 362 | g_assert(!aio_poll(ctx, false)); |
b2ea25d7 PB |
363 | g_assert_cmpint(data.n, ==, 0); |
364 | g_assert_cmpint(data.active, ==, 10); | |
365 | ||
366 | event_notifier_set(&data.e); | |
367 | g_assert(aio_poll(ctx, false)); | |
368 | g_assert_cmpint(data.n, ==, 1); | |
369 | g_assert_cmpint(data.active, ==, 9); | |
370 | g_assert(aio_poll(ctx, false)); | |
371 | ||
24d1a6d9 | 372 | wait_until_inactive(&data); |
b2ea25d7 PB |
373 | g_assert_cmpint(data.n, ==, 10); |
374 | g_assert_cmpint(data.active, ==, 0); | |
375 | g_assert(!aio_poll(ctx, false)); | |
376 | ||
f2e5dca4 | 377 | aio_set_event_notifier(ctx, &data.e, NULL); |
b2ea25d7 PB |
378 | g_assert(!aio_poll(ctx, false)); |
379 | event_notifier_cleanup(&data.e); | |
380 | } | |
381 | ||
382 | static void test_wait_event_notifier_noflush(void) | |
383 | { | |
384 | EventNotifierTestData data = { .n = 0 }; | |
385 | EventNotifierTestData dummy = { .n = 0, .active = 1 }; | |
386 | ||
387 | event_notifier_init(&data.e, false); | |
f2e5dca4 | 388 | aio_set_event_notifier(ctx, &data.e, event_ready_cb); |
b2ea25d7 PB |
389 | |
390 | g_assert(!aio_poll(ctx, false)); | |
391 | g_assert_cmpint(data.n, ==, 0); | |
392 | ||
393 | /* Until there is an active descriptor, aio_poll may or may not call | |
394 | * event_ready_cb. Still, it must not block. */ | |
395 | event_notifier_set(&data.e); | |
164a101f | 396 | g_assert(aio_poll(ctx, true)); |
b2ea25d7 PB |
397 | data.n = 0; |
398 | ||
399 | /* An active event notifier forces aio_poll to look at EventNotifiers. */ | |
400 | event_notifier_init(&dummy.e, false); | |
f2e5dca4 | 401 | aio_set_event_notifier(ctx, &dummy.e, event_ready_cb); |
b2ea25d7 PB |
402 | |
403 | event_notifier_set(&data.e); | |
404 | g_assert(aio_poll(ctx, false)); | |
405 | g_assert_cmpint(data.n, ==, 1); | |
164a101f | 406 | g_assert(!aio_poll(ctx, false)); |
b2ea25d7 PB |
407 | g_assert_cmpint(data.n, ==, 1); |
408 | ||
409 | event_notifier_set(&data.e); | |
410 | g_assert(aio_poll(ctx, false)); | |
411 | g_assert_cmpint(data.n, ==, 2); | |
164a101f | 412 | g_assert(!aio_poll(ctx, false)); |
b2ea25d7 PB |
413 | g_assert_cmpint(data.n, ==, 2); |
414 | ||
415 | event_notifier_set(&dummy.e); | |
24d1a6d9 | 416 | wait_until_inactive(&dummy); |
b2ea25d7 PB |
417 | g_assert_cmpint(data.n, ==, 2); |
418 | g_assert_cmpint(dummy.n, ==, 1); | |
419 | g_assert_cmpint(dummy.active, ==, 0); | |
420 | ||
f2e5dca4 | 421 | aio_set_event_notifier(ctx, &dummy.e, NULL); |
b2ea25d7 PB |
422 | event_notifier_cleanup(&dummy.e); |
423 | ||
f2e5dca4 | 424 | aio_set_event_notifier(ctx, &data.e, NULL); |
b2ea25d7 PB |
425 | g_assert(!aio_poll(ctx, false)); |
426 | g_assert_cmpint(data.n, ==, 2); | |
427 | ||
428 | event_notifier_cleanup(&data.e); | |
429 | } | |
430 | ||
b53edf97 AB |
431 | static void test_timer_schedule(void) |
432 | { | |
433 | TimerTestData data = { .n = 0, .ctx = ctx, .ns = SCALE_MS * 750LL, | |
434 | .max = 2, | |
435 | .clock_type = QEMU_CLOCK_VIRTUAL }; | |
363285d4 | 436 | EventNotifier e; |
b53edf97 AB |
437 | |
438 | /* aio_poll will not block to wait for timers to complete unless it has | |
439 | * an fd to wait on. Fixing this breaks other tests. So create a dummy one. | |
440 | */ | |
363285d4 PB |
441 | event_notifier_init(&e, false); |
442 | aio_set_event_notifier(ctx, &e, dummy_io_handler_read); | |
b53edf97 AB |
443 | aio_poll(ctx, false); |
444 | ||
445 | aio_timer_init(ctx, &data.timer, data.clock_type, | |
446 | SCALE_NS, timer_test_cb, &data); | |
447 | timer_mod(&data.timer, | |
448 | qemu_clock_get_ns(data.clock_type) + | |
449 | data.ns); | |
450 | ||
451 | g_assert_cmpint(data.n, ==, 0); | |
452 | ||
453 | /* timer_mod may well cause an event notifer to have gone off, | |
454 | * so clear that | |
455 | */ | |
456 | do {} while (aio_poll(ctx, false)); | |
457 | ||
458 | g_assert(!aio_poll(ctx, false)); | |
459 | g_assert_cmpint(data.n, ==, 0); | |
460 | ||
fcdda211 | 461 | g_usleep(1 * G_USEC_PER_SEC); |
b53edf97 AB |
462 | g_assert_cmpint(data.n, ==, 0); |
463 | ||
464 | g_assert(aio_poll(ctx, false)); | |
465 | g_assert_cmpint(data.n, ==, 1); | |
466 | ||
467 | /* timer_mod called by our callback */ | |
468 | do {} while (aio_poll(ctx, false)); | |
469 | ||
470 | g_assert(!aio_poll(ctx, false)); | |
471 | g_assert_cmpint(data.n, ==, 1); | |
472 | ||
473 | g_assert(aio_poll(ctx, true)); | |
474 | g_assert_cmpint(data.n, ==, 2); | |
475 | ||
476 | /* As max is now 2, an event notifier should not have gone off */ | |
477 | ||
478 | g_assert(!aio_poll(ctx, false)); | |
479 | g_assert_cmpint(data.n, ==, 2); | |
480 | ||
363285d4 PB |
481 | aio_set_event_notifier(ctx, &e, NULL); |
482 | event_notifier_cleanup(&e); | |
b53edf97 AB |
483 | |
484 | timer_del(&data.timer); | |
485 | } | |
486 | ||
b2ea25d7 PB |
487 | /* Now the same tests, using the context as a GSource. They are |
488 | * very similar to the ones above, with g_main_context_iteration | |
489 | * replacing aio_poll. However: | |
490 | * - sometimes both the AioContext and the glib main loop wake | |
491 | * themselves up. Hence, some "g_assert(!aio_poll(ctx, false));" | |
492 | * are replaced by "while (g_main_context_iteration(NULL, false));". | |
9fe3781f | 493 | * - there is no exact replacement for a blocking wait. |
b2ea25d7 PB |
494 | * "while (g_main_context_iteration(NULL, true)" seems to work, |
495 | * but it is not documented _why_ it works. For these tests a | |
496 | * non-blocking loop like "while (g_main_context_iteration(NULL, false)" | |
497 | * works well, and that's what I am using. | |
498 | */ | |
499 | ||
500 | static void test_source_notify(void) | |
501 | { | |
502 | while (g_main_context_iteration(NULL, false)); | |
503 | aio_notify(ctx); | |
504 | g_assert(g_main_context_iteration(NULL, true)); | |
505 | g_assert(!g_main_context_iteration(NULL, false)); | |
506 | } | |
507 | ||
508 | static void test_source_flush(void) | |
509 | { | |
510 | g_assert(!g_main_context_iteration(NULL, false)); | |
511 | aio_notify(ctx); | |
512 | while (g_main_context_iteration(NULL, false)); | |
513 | g_assert(!g_main_context_iteration(NULL, false)); | |
514 | } | |
515 | ||
516 | static void test_source_bh_schedule(void) | |
517 | { | |
518 | BHTestData data = { .n = 0 }; | |
519 | data.bh = aio_bh_new(ctx, bh_test_cb, &data); | |
520 | ||
521 | qemu_bh_schedule(data.bh); | |
522 | g_assert_cmpint(data.n, ==, 0); | |
523 | ||
524 | g_assert(g_main_context_iteration(NULL, true)); | |
525 | g_assert_cmpint(data.n, ==, 1); | |
526 | ||
527 | g_assert(!g_main_context_iteration(NULL, false)); | |
528 | g_assert_cmpint(data.n, ==, 1); | |
529 | qemu_bh_delete(data.bh); | |
530 | } | |
531 | ||
532 | static void test_source_bh_schedule10(void) | |
533 | { | |
534 | BHTestData data = { .n = 0, .max = 10 }; | |
535 | data.bh = aio_bh_new(ctx, bh_test_cb, &data); | |
536 | ||
537 | qemu_bh_schedule(data.bh); | |
538 | g_assert_cmpint(data.n, ==, 0); | |
539 | ||
540 | g_assert(g_main_context_iteration(NULL, false)); | |
541 | g_assert_cmpint(data.n, ==, 1); | |
542 | ||
543 | g_assert(g_main_context_iteration(NULL, true)); | |
544 | g_assert_cmpint(data.n, ==, 2); | |
545 | ||
546 | while (g_main_context_iteration(NULL, false)); | |
547 | g_assert_cmpint(data.n, ==, 10); | |
548 | ||
549 | g_assert(!g_main_context_iteration(NULL, false)); | |
550 | g_assert_cmpint(data.n, ==, 10); | |
551 | qemu_bh_delete(data.bh); | |
552 | } | |
553 | ||
554 | static void test_source_bh_cancel(void) | |
555 | { | |
556 | BHTestData data = { .n = 0 }; | |
557 | data.bh = aio_bh_new(ctx, bh_test_cb, &data); | |
558 | ||
559 | qemu_bh_schedule(data.bh); | |
560 | g_assert_cmpint(data.n, ==, 0); | |
561 | ||
562 | qemu_bh_cancel(data.bh); | |
563 | g_assert_cmpint(data.n, ==, 0); | |
564 | ||
565 | while (g_main_context_iteration(NULL, false)); | |
566 | g_assert_cmpint(data.n, ==, 0); | |
567 | qemu_bh_delete(data.bh); | |
568 | } | |
569 | ||
570 | static void test_source_bh_delete(void) | |
571 | { | |
572 | BHTestData data = { .n = 0 }; | |
573 | data.bh = aio_bh_new(ctx, bh_test_cb, &data); | |
574 | ||
575 | qemu_bh_schedule(data.bh); | |
576 | g_assert_cmpint(data.n, ==, 0); | |
577 | ||
578 | qemu_bh_delete(data.bh); | |
579 | g_assert_cmpint(data.n, ==, 0); | |
580 | ||
581 | while (g_main_context_iteration(NULL, false)); | |
582 | g_assert_cmpint(data.n, ==, 0); | |
583 | } | |
584 | ||
585 | static void test_source_bh_delete_from_cb(void) | |
586 | { | |
587 | BHTestData data1 = { .n = 0, .max = 1 }; | |
588 | ||
589 | data1.bh = aio_bh_new(ctx, bh_delete_cb, &data1); | |
590 | ||
591 | qemu_bh_schedule(data1.bh); | |
592 | g_assert_cmpint(data1.n, ==, 0); | |
593 | ||
594 | g_main_context_iteration(NULL, true); | |
595 | g_assert_cmpint(data1.n, ==, data1.max); | |
596 | g_assert(data1.bh == NULL); | |
597 | ||
598 | g_assert(!g_main_context_iteration(NULL, false)); | |
599 | } | |
600 | ||
601 | static void test_source_bh_delete_from_cb_many(void) | |
602 | { | |
603 | BHTestData data1 = { .n = 0, .max = 1 }; | |
604 | BHTestData data2 = { .n = 0, .max = 3 }; | |
605 | BHTestData data3 = { .n = 0, .max = 2 }; | |
606 | BHTestData data4 = { .n = 0, .max = 4 }; | |
607 | ||
608 | data1.bh = aio_bh_new(ctx, bh_delete_cb, &data1); | |
609 | data2.bh = aio_bh_new(ctx, bh_delete_cb, &data2); | |
610 | data3.bh = aio_bh_new(ctx, bh_delete_cb, &data3); | |
611 | data4.bh = aio_bh_new(ctx, bh_delete_cb, &data4); | |
612 | ||
613 | qemu_bh_schedule(data1.bh); | |
614 | qemu_bh_schedule(data2.bh); | |
615 | qemu_bh_schedule(data3.bh); | |
616 | qemu_bh_schedule(data4.bh); | |
617 | g_assert_cmpint(data1.n, ==, 0); | |
618 | g_assert_cmpint(data2.n, ==, 0); | |
619 | g_assert_cmpint(data3.n, ==, 0); | |
620 | g_assert_cmpint(data4.n, ==, 0); | |
621 | ||
622 | g_assert(g_main_context_iteration(NULL, false)); | |
623 | g_assert_cmpint(data1.n, ==, 1); | |
624 | g_assert_cmpint(data2.n, ==, 1); | |
625 | g_assert_cmpint(data3.n, ==, 1); | |
626 | g_assert_cmpint(data4.n, ==, 1); | |
627 | g_assert(data1.bh == NULL); | |
628 | ||
629 | while (g_main_context_iteration(NULL, false)); | |
630 | g_assert_cmpint(data1.n, ==, data1.max); | |
631 | g_assert_cmpint(data2.n, ==, data2.max); | |
632 | g_assert_cmpint(data3.n, ==, data3.max); | |
633 | g_assert_cmpint(data4.n, ==, data4.max); | |
634 | g_assert(data1.bh == NULL); | |
635 | g_assert(data2.bh == NULL); | |
636 | g_assert(data3.bh == NULL); | |
637 | g_assert(data4.bh == NULL); | |
638 | } | |
639 | ||
640 | static void test_source_bh_flush(void) | |
641 | { | |
642 | BHTestData data = { .n = 0 }; | |
643 | data.bh = aio_bh_new(ctx, bh_test_cb, &data); | |
644 | ||
645 | qemu_bh_schedule(data.bh); | |
646 | g_assert_cmpint(data.n, ==, 0); | |
647 | ||
648 | g_assert(g_main_context_iteration(NULL, true)); | |
649 | g_assert_cmpint(data.n, ==, 1); | |
650 | ||
651 | g_assert(!g_main_context_iteration(NULL, false)); | |
652 | g_assert_cmpint(data.n, ==, 1); | |
653 | qemu_bh_delete(data.bh); | |
654 | } | |
655 | ||
656 | static void test_source_set_event_notifier(void) | |
657 | { | |
658 | EventNotifierTestData data = { .n = 0, .active = 0 }; | |
659 | event_notifier_init(&data.e, false); | |
f2e5dca4 | 660 | aio_set_event_notifier(ctx, &data.e, event_ready_cb); |
b2ea25d7 PB |
661 | while (g_main_context_iteration(NULL, false)); |
662 | g_assert_cmpint(data.n, ==, 0); | |
663 | ||
f2e5dca4 | 664 | aio_set_event_notifier(ctx, &data.e, NULL); |
b2ea25d7 PB |
665 | while (g_main_context_iteration(NULL, false)); |
666 | g_assert_cmpint(data.n, ==, 0); | |
667 | event_notifier_cleanup(&data.e); | |
668 | } | |
669 | ||
670 | static void test_source_wait_event_notifier(void) | |
671 | { | |
672 | EventNotifierTestData data = { .n = 0, .active = 1 }; | |
673 | event_notifier_init(&data.e, false); | |
f2e5dca4 | 674 | aio_set_event_notifier(ctx, &data.e, event_ready_cb); |
b2ea25d7 PB |
675 | g_assert(g_main_context_iteration(NULL, false)); |
676 | g_assert_cmpint(data.n, ==, 0); | |
677 | g_assert_cmpint(data.active, ==, 1); | |
678 | ||
679 | event_notifier_set(&data.e); | |
680 | g_assert(g_main_context_iteration(NULL, false)); | |
681 | g_assert_cmpint(data.n, ==, 1); | |
682 | g_assert_cmpint(data.active, ==, 0); | |
683 | ||
684 | while (g_main_context_iteration(NULL, false)); | |
685 | g_assert_cmpint(data.n, ==, 1); | |
686 | g_assert_cmpint(data.active, ==, 0); | |
687 | ||
f2e5dca4 | 688 | aio_set_event_notifier(ctx, &data.e, NULL); |
b2ea25d7 PB |
689 | while (g_main_context_iteration(NULL, false)); |
690 | g_assert_cmpint(data.n, ==, 1); | |
691 | ||
692 | event_notifier_cleanup(&data.e); | |
693 | } | |
694 | ||
695 | static void test_source_flush_event_notifier(void) | |
696 | { | |
697 | EventNotifierTestData data = { .n = 0, .active = 10, .auto_set = true }; | |
698 | event_notifier_init(&data.e, false); | |
f2e5dca4 | 699 | aio_set_event_notifier(ctx, &data.e, event_ready_cb); |
b2ea25d7 PB |
700 | g_assert(g_main_context_iteration(NULL, false)); |
701 | g_assert_cmpint(data.n, ==, 0); | |
702 | g_assert_cmpint(data.active, ==, 10); | |
703 | ||
704 | event_notifier_set(&data.e); | |
705 | g_assert(g_main_context_iteration(NULL, false)); | |
706 | g_assert_cmpint(data.n, ==, 1); | |
707 | g_assert_cmpint(data.active, ==, 9); | |
708 | g_assert(g_main_context_iteration(NULL, false)); | |
709 | ||
710 | while (g_main_context_iteration(NULL, false)); | |
711 | g_assert_cmpint(data.n, ==, 10); | |
712 | g_assert_cmpint(data.active, ==, 0); | |
713 | g_assert(!g_main_context_iteration(NULL, false)); | |
714 | ||
f2e5dca4 | 715 | aio_set_event_notifier(ctx, &data.e, NULL); |
b2ea25d7 PB |
716 | while (g_main_context_iteration(NULL, false)); |
717 | event_notifier_cleanup(&data.e); | |
718 | } | |
719 | ||
720 | static void test_source_wait_event_notifier_noflush(void) | |
721 | { | |
722 | EventNotifierTestData data = { .n = 0 }; | |
723 | EventNotifierTestData dummy = { .n = 0, .active = 1 }; | |
724 | ||
725 | event_notifier_init(&data.e, false); | |
f2e5dca4 | 726 | aio_set_event_notifier(ctx, &data.e, event_ready_cb); |
b2ea25d7 PB |
727 | |
728 | while (g_main_context_iteration(NULL, false)); | |
729 | g_assert_cmpint(data.n, ==, 0); | |
730 | ||
731 | /* Until there is an active descriptor, glib may or may not call | |
732 | * event_ready_cb. Still, it must not block. */ | |
733 | event_notifier_set(&data.e); | |
734 | g_main_context_iteration(NULL, true); | |
735 | data.n = 0; | |
736 | ||
737 | /* An active event notifier forces aio_poll to look at EventNotifiers. */ | |
738 | event_notifier_init(&dummy.e, false); | |
f2e5dca4 | 739 | aio_set_event_notifier(ctx, &dummy.e, event_ready_cb); |
b2ea25d7 PB |
740 | |
741 | event_notifier_set(&data.e); | |
742 | g_assert(g_main_context_iteration(NULL, false)); | |
743 | g_assert_cmpint(data.n, ==, 1); | |
744 | g_assert(!g_main_context_iteration(NULL, false)); | |
745 | g_assert_cmpint(data.n, ==, 1); | |
746 | ||
747 | event_notifier_set(&data.e); | |
748 | g_assert(g_main_context_iteration(NULL, false)); | |
749 | g_assert_cmpint(data.n, ==, 2); | |
750 | g_assert(!g_main_context_iteration(NULL, false)); | |
751 | g_assert_cmpint(data.n, ==, 2); | |
752 | ||
753 | event_notifier_set(&dummy.e); | |
754 | while (g_main_context_iteration(NULL, false)); | |
755 | g_assert_cmpint(data.n, ==, 2); | |
756 | g_assert_cmpint(dummy.n, ==, 1); | |
757 | g_assert_cmpint(dummy.active, ==, 0); | |
758 | ||
f2e5dca4 | 759 | aio_set_event_notifier(ctx, &dummy.e, NULL); |
b2ea25d7 PB |
760 | event_notifier_cleanup(&dummy.e); |
761 | ||
f2e5dca4 | 762 | aio_set_event_notifier(ctx, &data.e, NULL); |
b2ea25d7 PB |
763 | while (g_main_context_iteration(NULL, false)); |
764 | g_assert_cmpint(data.n, ==, 2); | |
765 | ||
766 | event_notifier_cleanup(&data.e); | |
767 | } | |
768 | ||
b53edf97 AB |
769 | static void test_source_timer_schedule(void) |
770 | { | |
771 | TimerTestData data = { .n = 0, .ctx = ctx, .ns = SCALE_MS * 750LL, | |
772 | .max = 2, | |
773 | .clock_type = QEMU_CLOCK_VIRTUAL }; | |
363285d4 | 774 | EventNotifier e; |
b53edf97 AB |
775 | int64_t expiry; |
776 | ||
777 | /* aio_poll will not block to wait for timers to complete unless it has | |
778 | * an fd to wait on. Fixing this breaks other tests. So create a dummy one. | |
779 | */ | |
363285d4 PB |
780 | event_notifier_init(&e, false); |
781 | aio_set_event_notifier(ctx, &e, dummy_io_handler_read); | |
b53edf97 AB |
782 | do {} while (g_main_context_iteration(NULL, false)); |
783 | ||
784 | aio_timer_init(ctx, &data.timer, data.clock_type, | |
785 | SCALE_NS, timer_test_cb, &data); | |
786 | expiry = qemu_clock_get_ns(data.clock_type) + | |
787 | data.ns; | |
788 | timer_mod(&data.timer, expiry); | |
789 | ||
790 | g_assert_cmpint(data.n, ==, 0); | |
791 | ||
fcdda211 | 792 | g_usleep(1 * G_USEC_PER_SEC); |
b53edf97 AB |
793 | g_assert_cmpint(data.n, ==, 0); |
794 | ||
ef508f42 | 795 | g_assert(g_main_context_iteration(NULL, true)); |
b53edf97 | 796 | g_assert_cmpint(data.n, ==, 1); |
ef508f42 | 797 | expiry += data.ns; |
b53edf97 | 798 | |
ef508f42 PB |
799 | while (data.n < 2) { |
800 | g_main_context_iteration(NULL, true); | |
801 | } | |
b53edf97 AB |
802 | |
803 | g_assert_cmpint(data.n, ==, 2); | |
ef508f42 | 804 | g_assert(qemu_clock_get_ns(data.clock_type) > expiry); |
b53edf97 | 805 | |
363285d4 PB |
806 | aio_set_event_notifier(ctx, &e, NULL); |
807 | event_notifier_cleanup(&e); | |
b53edf97 AB |
808 | |
809 | timer_del(&data.timer); | |
810 | } | |
811 | ||
812 | ||
b2ea25d7 PB |
813 | /* End of tests. */ |
814 | ||
815 | int main(int argc, char **argv) | |
816 | { | |
2f78e491 | 817 | Error *local_error = NULL; |
b2ea25d7 PB |
818 | GSource *src; |
819 | ||
dae21b98 AB |
820 | init_clocks(); |
821 | ||
2f78e491 CN |
822 | ctx = aio_context_new(&local_error); |
823 | if (!ctx) { | |
824 | error_report("Failed to create AIO Context: '%s'", | |
825 | error_get_pretty(local_error)); | |
826 | error_free(local_error); | |
827 | exit(1); | |
828 | } | |
b2ea25d7 PB |
829 | src = aio_get_g_source(ctx); |
830 | g_source_attach(src, NULL); | |
831 | g_source_unref(src); | |
832 | ||
833 | while (g_main_context_iteration(NULL, false)); | |
834 | ||
835 | g_test_init(&argc, &argv, NULL); | |
836 | g_test_add_func("/aio/notify", test_notify); | |
98563fc3 | 837 | g_test_add_func("/aio/acquire", test_acquire); |
b2ea25d7 PB |
838 | g_test_add_func("/aio/bh/schedule", test_bh_schedule); |
839 | g_test_add_func("/aio/bh/schedule10", test_bh_schedule10); | |
840 | g_test_add_func("/aio/bh/cancel", test_bh_cancel); | |
841 | g_test_add_func("/aio/bh/delete", test_bh_delete); | |
842 | g_test_add_func("/aio/bh/callback-delete/one", test_bh_delete_from_cb); | |
843 | g_test_add_func("/aio/bh/callback-delete/many", test_bh_delete_from_cb_many); | |
844 | g_test_add_func("/aio/bh/flush", test_bh_flush); | |
845 | g_test_add_func("/aio/event/add-remove", test_set_event_notifier); | |
846 | g_test_add_func("/aio/event/wait", test_wait_event_notifier); | |
847 | g_test_add_func("/aio/event/wait/no-flush-cb", test_wait_event_notifier_noflush); | |
848 | g_test_add_func("/aio/event/flush", test_flush_event_notifier); | |
b53edf97 | 849 | g_test_add_func("/aio/timer/schedule", test_timer_schedule); |
b2ea25d7 PB |
850 | |
851 | g_test_add_func("/aio-gsource/notify", test_source_notify); | |
852 | g_test_add_func("/aio-gsource/flush", test_source_flush); | |
853 | g_test_add_func("/aio-gsource/bh/schedule", test_source_bh_schedule); | |
854 | g_test_add_func("/aio-gsource/bh/schedule10", test_source_bh_schedule10); | |
855 | g_test_add_func("/aio-gsource/bh/cancel", test_source_bh_cancel); | |
856 | g_test_add_func("/aio-gsource/bh/delete", test_source_bh_delete); | |
857 | g_test_add_func("/aio-gsource/bh/callback-delete/one", test_source_bh_delete_from_cb); | |
858 | g_test_add_func("/aio-gsource/bh/callback-delete/many", test_source_bh_delete_from_cb_many); | |
859 | g_test_add_func("/aio-gsource/bh/flush", test_source_bh_flush); | |
860 | g_test_add_func("/aio-gsource/event/add-remove", test_source_set_event_notifier); | |
861 | g_test_add_func("/aio-gsource/event/wait", test_source_wait_event_notifier); | |
862 | g_test_add_func("/aio-gsource/event/wait/no-flush-cb", test_source_wait_event_notifier_noflush); | |
863 | g_test_add_func("/aio-gsource/event/flush", test_source_flush_event_notifier); | |
b53edf97 | 864 | g_test_add_func("/aio-gsource/timer/schedule", test_source_timer_schedule); |
b2ea25d7 PB |
865 | return g_test_run(); |
866 | } |