master
c 724 lines 17.8 KB
Raw
1 /*
2 * Coroutine tests
3 *
4 * Copyright IBM, Corp. 2011
5 *
6 * Authors:
7 * Stefan Hajnoczi <stefanha@linux.vnet.ibm.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
14 #include "qemu/osdep.h"
15 #include "qemu/coroutine_int.h"
16
17 /*
18 * Check that qemu_in_coroutine() works
19 */
20
21 static void coroutine_fn verify_in_coroutine(void *opaque)
22 {
23 g_assert(qemu_in_coroutine());
24 }
25
26 static void test_in_coroutine(void)
27 {
28 Coroutine *coroutine;
29
30 g_assert(!qemu_in_coroutine());
31
32 coroutine = qemu_coroutine_create(verify_in_coroutine, NULL);
33 qemu_coroutine_enter(coroutine);
34 }
35
36 /*
37 * Check that qemu_coroutine_self() works
38 */
39
40 static void coroutine_fn verify_self(void *opaque)
41 {
42 Coroutine **p_co = opaque;
43 g_assert(qemu_coroutine_self() == *p_co);
44 }
45
46 static void test_self(void)
47 {
48 Coroutine *coroutine;
49
50 coroutine = qemu_coroutine_create(verify_self, &coroutine);
51 qemu_coroutine_enter(coroutine);
52 }
53
54 /*
55 * Check that qemu_coroutine_entered() works
56 */
57
58 static void coroutine_fn verify_entered_step_2(void *opaque)
59 {
60 Coroutine *caller = (Coroutine *)opaque;
61
62 g_assert(qemu_coroutine_entered(caller));
63 g_assert(qemu_coroutine_entered(qemu_coroutine_self()));
64 qemu_coroutine_yield();
65
66 /* Once more to check it still works after yielding */
67 g_assert(qemu_coroutine_entered(caller));
68 g_assert(qemu_coroutine_entered(qemu_coroutine_self()));
69 }
70
71 static void coroutine_fn verify_entered_step_1(void *opaque)
72 {
73 Coroutine *self = qemu_coroutine_self();
74 Coroutine *coroutine;
75
76 g_assert(qemu_coroutine_entered(self));
77
78 coroutine = qemu_coroutine_create(verify_entered_step_2, self);
79 g_assert(!qemu_coroutine_entered(coroutine));
80 qemu_coroutine_enter(coroutine);
81 g_assert(!qemu_coroutine_entered(coroutine));
82 qemu_coroutine_enter(coroutine);
83 }
84
85 static void test_entered(void)
86 {
87 Coroutine *coroutine;
88
89 coroutine = qemu_coroutine_create(verify_entered_step_1, NULL);
90 g_assert(!qemu_coroutine_entered(coroutine));
91 qemu_coroutine_enter(coroutine);
92 }
93
94 /*
95 * Check that coroutines may nest multiple levels
96 */
97
98 typedef struct {
99 unsigned int n_enter; /* num coroutines entered */
100 unsigned int n_return; /* num coroutines returned */
101 unsigned int max; /* maximum level of nesting */
102 } NestData;
103
104 static void coroutine_fn nest(void *opaque)
105 {
106 NestData *nd = opaque;
107
108 nd->n_enter++;
109
110 if (nd->n_enter < nd->max) {
111 Coroutine *child;
112
113 child = qemu_coroutine_create(nest, nd);
114 qemu_coroutine_enter(child);
115 }
116
117 nd->n_return++;
118 }
119
120 static void test_nesting(void)
121 {
122 Coroutine *root;
123 NestData nd = {
124 .n_enter = 0,
125 .n_return = 0,
126 .max = 128,
127 };
128
129 root = qemu_coroutine_create(nest, &nd);
130 qemu_coroutine_enter(root);
131
132 /* Must enter and return from max nesting level */
133 g_assert_cmpint(nd.n_enter, ==, nd.max);
134 g_assert_cmpint(nd.n_return, ==, nd.max);
135 }
136
137 /*
138 * Check that yield/enter transfer control correctly
139 */
140
141 static void coroutine_fn yield_5_times(void *opaque)
142 {
143 bool *done = opaque;
144 int i;
145
146 for (i = 0; i < 5; i++) {
147 qemu_coroutine_yield();
148 }
149 *done = true;
150 }
151
152 static void test_yield(void)
153 {
154 Coroutine *coroutine;
155 bool done = false;
156 int i = -1; /* one extra time to return from coroutine */
157
158 coroutine = qemu_coroutine_create(yield_5_times, &done);
159 while (!done) {
160 qemu_coroutine_enter(coroutine);
161 i++;
162 }
163 g_assert_cmpint(i, ==, 5); /* coroutine must yield 5 times */
164 }
165
166 static void coroutine_fn c2_fn(void *opaque)
167 {
168 qemu_coroutine_yield();
169 }
170
171 static void coroutine_fn c1_fn(void *opaque)
172 {
173 Coroutine *c2 = opaque;
174 qemu_coroutine_enter(c2);
175 }
176
177 static void test_no_dangling_access(void)
178 {
179 Coroutine *c1;
180 Coroutine *c2;
181 Coroutine tmp;
182
183 c2 = qemu_coroutine_create(c2_fn, NULL);
184 c1 = qemu_coroutine_create(c1_fn, c2);
185
186 qemu_coroutine_enter(c1);
187
188 /* c1 shouldn't be used any more now; make sure we segfault if it is */
189 tmp = *c1;
190 memset(c1, 0xff, sizeof(Coroutine));
191 qemu_coroutine_enter(c2);
192
193 /* Must restore the coroutine now to avoid corrupted pool */
194 *c1 = tmp;
195 }
196
197 static bool locked;
198 static int done_count;
199
200 static void coroutine_fn mutex_fn(void *opaque)
201 {
202 CoMutex *m = opaque;
203 qemu_co_mutex_lock(m);
204 assert(!locked);
205 locked = true;
206 qemu_coroutine_yield();
207 locked = false;
208 qemu_co_mutex_unlock(m);
209 done_count++;
210 }
211
212 static void coroutine_fn lockable_fn(void *opaque)
213 {
214 QemuLockable *x = opaque;
215 qemu_lockable_lock(x);
216 assert(!locked);
217 locked = true;
218 qemu_coroutine_yield();
219 locked = false;
220 qemu_lockable_unlock(x);
221 done_count++;
222 }
223
224 static void do_test_co_mutex(CoroutineEntry *entry, void *opaque)
225 {
226 Coroutine *c1 = qemu_coroutine_create(entry, opaque);
227 Coroutine *c2 = qemu_coroutine_create(entry, opaque);
228
229 done_count = 0;
230 qemu_coroutine_enter(c1);
231 g_assert(locked);
232 qemu_coroutine_enter(c2);
233
234 /* Unlock queues c2. It is then started automatically when c1 yields or
235 * terminates.
236 */
237 qemu_coroutine_enter(c1);
238 g_assert_cmpint(done_count, ==, 1);
239 g_assert(locked);
240
241 qemu_coroutine_enter(c2);
242 g_assert_cmpint(done_count, ==, 2);
243 g_assert(!locked);
244 }
245
246 static void test_co_mutex(void)
247 {
248 CoMutex m;
249
250 qemu_co_mutex_init(&m);
251 do_test_co_mutex(mutex_fn, &m);
252 }
253
254 static void test_co_mutex_lockable(void)
255 {
256 CoMutex m;
257 CoMutex *null_pointer = NULL;
258
259 qemu_co_mutex_init(&m);
260 do_test_co_mutex(lockable_fn, QEMU_MAKE_LOCKABLE(&m));
261
262 g_assert(QEMU_MAKE_LOCKABLE(null_pointer) == NULL);
263 }
264
265 static CoRwlock rwlock;
266
267 /* Test that readers are properly sent back to the queue when upgrading,
268 * even if they are the sole readers. The test scenario is as follows:
269 *
270 *
271 * | c1 | c2 |
272 * |--------------+------------+
273 * | rdlock | |
274 * | yield | |
275 * | | wrlock |
276 * | | <queued> |
277 * | upgrade | |
278 * | <queued> | <dequeued> |
279 * | | unlock |
280 * | <dequeued> | |
281 * | unlock | |
282 */
283
284 static void coroutine_fn rwlock_yield_upgrade(void *opaque)
285 {
286 qemu_co_rwlock_rdlock(&rwlock);
287 qemu_coroutine_yield();
288
289 qemu_co_rwlock_upgrade(&rwlock);
290 qemu_co_rwlock_unlock(&rwlock);
291
292 *(bool *)opaque = true;
293 }
294
295 static void coroutine_fn rwlock_wrlock_yield(void *opaque)
296 {
297 qemu_co_rwlock_wrlock(&rwlock);
298 qemu_coroutine_yield();
299
300 qemu_co_rwlock_unlock(&rwlock);
301 *(bool *)opaque = true;
302 }
303
304 static void test_co_rwlock_upgrade(void)
305 {
306 bool c1_done = false;
307 bool c2_done = false;
308 Coroutine *c1, *c2;
309
310 qemu_co_rwlock_init(&rwlock);
311 c1 = qemu_coroutine_create(rwlock_yield_upgrade, &c1_done);
312 c2 = qemu_coroutine_create(rwlock_wrlock_yield, &c2_done);
313
314 qemu_coroutine_enter(c1);
315 qemu_coroutine_enter(c2);
316
317 /* c1 now should go to sleep. */
318 qemu_coroutine_enter(c1);
319 g_assert(!c1_done);
320
321 qemu_coroutine_enter(c2);
322 g_assert(c1_done);
323 g_assert(c2_done);
324 }
325
326 static void coroutine_fn rwlock_rdlock_yield(void *opaque)
327 {
328 qemu_co_rwlock_rdlock(&rwlock);
329 qemu_coroutine_yield();
330
331 qemu_co_rwlock_unlock(&rwlock);
332 qemu_coroutine_yield();
333
334 *(bool *)opaque = true;
335 }
336
337 static void coroutine_fn rwlock_wrlock_downgrade(void *opaque)
338 {
339 qemu_co_rwlock_wrlock(&rwlock);
340
341 qemu_co_rwlock_downgrade(&rwlock);
342 qemu_co_rwlock_unlock(&rwlock);
343 *(bool *)opaque = true;
344 }
345
346 static void coroutine_fn rwlock_rdlock(void *opaque)
347 {
348 qemu_co_rwlock_rdlock(&rwlock);
349
350 qemu_co_rwlock_unlock(&rwlock);
351 *(bool *)opaque = true;
352 }
353
354 static void coroutine_fn rwlock_wrlock(void *opaque)
355 {
356 qemu_co_rwlock_wrlock(&rwlock);
357
358 qemu_co_rwlock_unlock(&rwlock);
359 *(bool *)opaque = true;
360 }
361
362 /*
363 * Check that downgrading a reader-writer lock does not cause a hang.
364 *
365 * Four coroutines are used to produce a situation where there are
366 * both reader and writer hopefuls waiting to acquire an rwlock that
367 * is held by a reader.
368 *
369 * The correct sequence of operations we aim to provoke can be
370 * represented as:
371 *
372 * | c1 | c2 | c3 | c4 |
373 * |--------+------------+------------+------------|
374 * | rdlock | | | |
375 * | yield | | | |
376 * | | wrlock | | |
377 * | | <queued> | | |
378 * | | | rdlock | |
379 * | | | <queued> | |
380 * | | | | wrlock |
381 * | | | | <queued> |
382 * | unlock | | | |
383 * | yield | | | |
384 * | | <dequeued> | | |
385 * | | downgrade | | |
386 * | | | <dequeued> | |
387 * | | | unlock | |
388 * | | ... | | |
389 * | | unlock | | |
390 * | | | | <dequeued> |
391 * | | | | unlock |
392 */
393 static void test_co_rwlock_downgrade(void)
394 {
395 bool c1_done = false;
396 bool c2_done = false;
397 bool c3_done = false;
398 bool c4_done = false;
399 Coroutine *c1, *c2, *c3, *c4;
400
401 qemu_co_rwlock_init(&rwlock);
402
403 c1 = qemu_coroutine_create(rwlock_rdlock_yield, &c1_done);
404 c2 = qemu_coroutine_create(rwlock_wrlock_downgrade, &c2_done);
405 c3 = qemu_coroutine_create(rwlock_rdlock, &c3_done);
406 c4 = qemu_coroutine_create(rwlock_wrlock, &c4_done);
407
408 qemu_coroutine_enter(c1);
409 qemu_coroutine_enter(c2);
410 qemu_coroutine_enter(c3);
411 qemu_coroutine_enter(c4);
412
413 qemu_coroutine_enter(c1);
414
415 g_assert(c2_done);
416 g_assert(c3_done);
417 g_assert(c4_done);
418
419 qemu_coroutine_enter(c1);
420
421 g_assert(c1_done);
422 }
423
424 /*
425 * Check that a wake delivered before the sleeper parks is not lost.
426 *
427 * qemu_co_sleep_wake() is fire-and-forget: a caller cancelling a
428 * sleep/work loop may call it in the window after the sleeper has
429 * decided to sleep but before it has published itself inside
430 * qemu_co_sleep(). The wake must be sticky and shorten the next sleep
431 * rather than being dropped (which would block until the full sleep
432 * duration expired).
433 *
434 * No threads, timers or AioContext are needed: coroutines are
435 * cooperative, so ordering the wake before the sleep deterministically
436 * reproduces the state the racing waker would otherwise produce.
437 */
438
439 typedef struct {
440 QemuCoSleep w;
441 bool completed;
442 } CoSleepWakeData;
443
444 static void coroutine_fn co_sleep_wake_entry(void *opaque)
445 {
446 CoSleepWakeData *d = opaque;
447
448 /*
449 * The wake was already delivered before we got here. qemu_co_sleep()
450 * must consume it and return without yielding.
451 */
452 qemu_co_sleep(&d->w);
453 d->completed = true;
454 }
455
456 static void test_co_sleep_wake_before_sleep(void)
457 {
458 CoSleepWakeData d = { .w = { 0 }, .completed = false };
459 Coroutine *co = qemu_coroutine_create(co_sleep_wake_entry, &d);
460
461 /* Waker runs first, while no sleeper is parked on w. */
462 qemu_co_sleep_wake(&d.w);
463
464 /*
465 * Entering runs qemu_co_sleep(), which consumes the pending wake and
466 * returns without yielding, so the coroutine runs straight to
467 * completion in this single enter. With the pre-fix primitive the wake
468 * is dropped, qemu_co_sleep() parks, and completed stays false.
469 */
470 qemu_coroutine_enter(co);
471
472 g_assert(d.completed);
473 }
474
475 /*
476 * Check that creation, enter, and return work
477 */
478
479 static void coroutine_fn set_and_exit(void *opaque)
480 {
481 bool *done = opaque;
482
483 *done = true;
484 }
485
486 static void test_lifecycle(void)
487 {
488 Coroutine *coroutine;
489 bool done = false;
490
491 /* Create, enter, and return from coroutine */
492 coroutine = qemu_coroutine_create(set_and_exit, &done);
493 qemu_coroutine_enter(coroutine);
494 g_assert(done); /* expect done to be true (first time) */
495
496 /* Repeat to check that no state affects this test */
497 done = false;
498 coroutine = qemu_coroutine_create(set_and_exit, &done);
499 qemu_coroutine_enter(coroutine);
500 g_assert(done); /* expect done to be true (second time) */
501 }
502
503
504 #define RECORD_SIZE 10 /* Leave some room for expansion */
505 struct coroutine_position {
506 int func;
507 int state;
508 };
509 static struct coroutine_position records[RECORD_SIZE];
510 static unsigned record_pos;
511
512 static void record_push(int func, int state)
513 {
514 struct coroutine_position *cp = &records[record_pos++];
515 g_assert_cmpint(record_pos, <, RECORD_SIZE);
516 cp->func = func;
517 cp->state = state;
518 }
519
520 static void coroutine_fn co_order_test(void *opaque)
521 {
522 record_push(2, 1);
523 g_assert(qemu_in_coroutine());
524 qemu_coroutine_yield();
525 record_push(2, 2);
526 g_assert(qemu_in_coroutine());
527 }
528
529 static void do_order_test(void)
530 {
531 Coroutine *co;
532
533 co = qemu_coroutine_create(co_order_test, NULL);
534 record_push(1, 1);
535 qemu_coroutine_enter(co);
536 record_push(1, 2);
537 g_assert(!qemu_in_coroutine());
538 qemu_coroutine_enter(co);
539 record_push(1, 3);
540 g_assert(!qemu_in_coroutine());
541 }
542
543 static void test_order(void)
544 {
545 int i;
546 const struct coroutine_position expected_pos[] = {
547 {1, 1,}, {2, 1}, {1, 2}, {2, 2}, {1, 3}
548 };
549 do_order_test();
550 g_assert_cmpint(record_pos, ==, 5);
551 for (i = 0; i < record_pos; i++) {
552 g_assert_cmpint(records[i].func , ==, expected_pos[i].func );
553 g_assert_cmpint(records[i].state, ==, expected_pos[i].state);
554 }
555 }
556 /*
557 * Lifecycle benchmark
558 */
559
560 static void coroutine_fn empty_coroutine(void *opaque)
561 {
562 /* Do nothing */
563 }
564
565 static void perf_lifecycle(void)
566 {
567 Coroutine *coroutine;
568 unsigned int i, max;
569 double duration;
570
571 max = 1000000;
572
573 g_test_timer_start();
574 for (i = 0; i < max; i++) {
575 coroutine = qemu_coroutine_create(empty_coroutine, NULL);
576 qemu_coroutine_enter(coroutine);
577 }
578 duration = g_test_timer_elapsed();
579
580 g_test_message("Lifecycle %u iterations: %f s", max, duration);
581 }
582
583 static void perf_nesting(void)
584 {
585 unsigned int i, maxcycles, maxnesting;
586 double duration;
587
588 maxcycles = 10000;
589 maxnesting = 1000;
590 Coroutine *root;
591
592 g_test_timer_start();
593 for (i = 0; i < maxcycles; i++) {
594 NestData nd = {
595 .n_enter = 0,
596 .n_return = 0,
597 .max = maxnesting,
598 };
599 root = qemu_coroutine_create(nest, &nd);
600 qemu_coroutine_enter(root);
601 }
602 duration = g_test_timer_elapsed();
603
604 g_test_message("Nesting %u iterations of %u depth each: %f s",
605 maxcycles, maxnesting, duration);
606 }
607
608 /*
609 * Yield benchmark
610 */
611
612 static void coroutine_fn yield_loop(void *opaque)
613 {
614 unsigned int *counter = opaque;
615
616 while ((*counter) > 0) {
617 (*counter)--;
618 qemu_coroutine_yield();
619 }
620 }
621
622 static void perf_yield(void)
623 {
624 unsigned int i, maxcycles;
625 double duration;
626
627 maxcycles = 100000000;
628 i = maxcycles;
629 Coroutine *coroutine = qemu_coroutine_create(yield_loop, &i);
630
631 g_test_timer_start();
632 while (i > 0) {
633 qemu_coroutine_enter(coroutine);
634 }
635 duration = g_test_timer_elapsed();
636
637 g_test_message("Yield %u iterations: %f s", maxcycles, duration);
638 }
639
640 static __attribute__((noinline)) void dummy(unsigned *i)
641 {
642 (*i)--;
643 }
644
645 static void perf_baseline(void)
646 {
647 unsigned int i, maxcycles;
648 double duration;
649
650 maxcycles = 100000000;
651 i = maxcycles;
652
653 g_test_timer_start();
654 while (i > 0) {
655 dummy(&i);
656 }
657 duration = g_test_timer_elapsed();
658
659 g_test_message("Function call %u iterations: %f s", maxcycles, duration);
660 }
661
662 static __attribute__((noinline)) void coroutine_fn perf_cost_func(void *opaque)
663 {
664 qemu_coroutine_yield();
665 }
666
667 static void perf_cost(void)
668 {
669 const unsigned long maxcycles = 40000000;
670 unsigned long i = 0;
671 double duration;
672 unsigned long ops;
673 Coroutine *co;
674
675 g_test_timer_start();
676 while (i++ < maxcycles) {
677 co = qemu_coroutine_create(perf_cost_func, &i);
678 qemu_coroutine_enter(co);
679 qemu_coroutine_enter(co);
680 }
681 duration = g_test_timer_elapsed();
682 ops = (long)(maxcycles / (duration * 1000));
683
684 g_test_message("Run operation %lu iterations %f s, %luK operations/s, "
685 "%luns per coroutine",
686 maxcycles,
687 duration, ops,
688 (unsigned long)(1000000000.0 * duration / maxcycles));
689 }
690
691 int main(int argc, char **argv)
692 {
693 g_test_init(&argc, &argv, NULL);
694
695 /* This test assumes there is a freelist and marks freed coroutine memory
696 * with a sentinel value. If there is no freelist this would legitimately
697 * crash, so skip it.
698 */
699 if (IS_ENABLED(CONFIG_COROUTINE_POOL)) {
700 g_test_add_func("/basic/no-dangling-access", test_no_dangling_access);
701 }
702
703 g_test_add_func("/basic/lifecycle", test_lifecycle);
704 g_test_add_func("/basic/yield", test_yield);
705 g_test_add_func("/basic/nesting", test_nesting);
706 g_test_add_func("/basic/self", test_self);
707 g_test_add_func("/basic/entered", test_entered);
708 g_test_add_func("/basic/in_coroutine", test_in_coroutine);
709 g_test_add_func("/basic/order", test_order);
710 g_test_add_func("/locking/co-mutex", test_co_mutex);
711 g_test_add_func("/locking/co-mutex/lockable", test_co_mutex_lockable);
712 g_test_add_func("/locking/co-rwlock/upgrade", test_co_rwlock_upgrade);
713 g_test_add_func("/locking/co-rwlock/downgrade", test_co_rwlock_downgrade);
714 g_test_add_func("/locking/co-sleep/wake-before-sleep",
715 test_co_sleep_wake_before_sleep);
716 if (g_test_perf()) {
717 g_test_add_func("/perf/lifecycle", perf_lifecycle);
718 g_test_add_func("/perf/nesting", perf_nesting);
719 g_test_add_func("/perf/yield", perf_yield);
720 g_test_add_func("/perf/function-call", perf_baseline);
721 g_test_add_func("/perf/cost", perf_cost);
722 }
723 return g_test_run();
724 }