Merge tag 'v9.0.0-rc3'
[qemu/ar7.git] / tests / unit / test-coroutine.c
blob49d4d9b2512142f4c861a3674048f540ca7d1c80
1 /*
2 * Coroutine tests
4 * Copyright IBM, Corp. 2011
6 * Authors:
7 * Stefan Hajnoczi <stefanha@linux.vnet.ibm.com>
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.
14 #include "qemu/osdep.h"
15 #include "qemu/coroutine_int.h"
18 * Check that qemu_in_coroutine() works
21 static void coroutine_fn verify_in_coroutine(void *opaque)
23 g_assert(qemu_in_coroutine());
26 static void test_in_coroutine(void)
28 Coroutine *coroutine;
30 g_assert(!qemu_in_coroutine());
32 coroutine = qemu_coroutine_create(verify_in_coroutine, NULL);
33 qemu_coroutine_enter(coroutine);
37 * Check that qemu_coroutine_self() works
40 static void coroutine_fn verify_self(void *opaque)
42 Coroutine **p_co = opaque;
43 g_assert(qemu_coroutine_self() == *p_co);
46 static void test_self(void)
48 Coroutine *coroutine;
50 coroutine = qemu_coroutine_create(verify_self, &coroutine);
51 qemu_coroutine_enter(coroutine);
55 * Check that qemu_coroutine_entered() works
58 static void coroutine_fn verify_entered_step_2(void *opaque)
60 Coroutine *caller = (Coroutine *)opaque;
62 g_assert(qemu_coroutine_entered(caller));
63 g_assert(qemu_coroutine_entered(qemu_coroutine_self()));
64 qemu_coroutine_yield();
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()));
71 static void coroutine_fn verify_entered_step_1(void *opaque)
73 Coroutine *self = qemu_coroutine_self();
74 Coroutine *coroutine;
76 g_assert(qemu_coroutine_entered(self));
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);
85 static void test_entered(void)
87 Coroutine *coroutine;
89 coroutine = qemu_coroutine_create(verify_entered_step_1, NULL);
90 g_assert(!qemu_coroutine_entered(coroutine));
91 qemu_coroutine_enter(coroutine);
95 * Check that coroutines may nest multiple levels
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;
104 static void coroutine_fn nest(void *opaque)
106 NestData *nd = opaque;
108 nd->n_enter++;
110 if (nd->n_enter < nd->max) {
111 Coroutine *child;
113 child = qemu_coroutine_create(nest, nd);
114 qemu_coroutine_enter(child);
117 nd->n_return++;
120 static void test_nesting(void)
122 Coroutine *root;
123 NestData nd = {
124 .n_enter = 0,
125 .n_return = 0,
126 .max = 128,
129 root = qemu_coroutine_create(nest, &nd);
130 qemu_coroutine_enter(root);
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);
138 * Check that yield/enter transfer control correctly
141 static void coroutine_fn yield_5_times(void *opaque)
143 bool *done = opaque;
144 int i;
146 for (i = 0; i < 5; i++) {
147 qemu_coroutine_yield();
149 *done = true;
152 static void test_yield(void)
154 Coroutine *coroutine;
155 bool done = false;
156 int i = -1; /* one extra time to return from coroutine */
158 coroutine = qemu_coroutine_create(yield_5_times, &done);
159 while (!done) {
160 qemu_coroutine_enter(coroutine);
161 i++;
163 g_assert_cmpint(i, ==, 5); /* coroutine must yield 5 times */
166 static void coroutine_fn c2_fn(void *opaque)
168 qemu_coroutine_yield();
171 static void coroutine_fn c1_fn(void *opaque)
173 Coroutine *c2 = opaque;
174 qemu_coroutine_enter(c2);
177 static void test_no_dangling_access(void)
179 Coroutine *c1;
180 Coroutine *c2;
181 Coroutine tmp;
183 c2 = qemu_coroutine_create(c2_fn, NULL);
184 c1 = qemu_coroutine_create(c1_fn, c2);
186 qemu_coroutine_enter(c1);
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);
193 /* Must restore the coroutine now to avoid corrupted pool */
194 *c1 = tmp;
197 static bool locked;
198 static int done_count;
200 static void coroutine_fn mutex_fn(void *opaque)
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++;
212 static void coroutine_fn lockable_fn(void *opaque)
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++;
224 static void do_test_co_mutex(CoroutineEntry *entry, void *opaque)
226 Coroutine *c1 = qemu_coroutine_create(entry, opaque);
227 Coroutine *c2 = qemu_coroutine_create(entry, opaque);
229 done_count = 0;
230 qemu_coroutine_enter(c1);
231 g_assert(locked);
232 qemu_coroutine_enter(c2);
234 /* Unlock queues c2. It is then started automatically when c1 yields or
235 * terminates.
237 qemu_coroutine_enter(c1);
238 g_assert_cmpint(done_count, ==, 1);
239 g_assert(locked);
241 qemu_coroutine_enter(c2);
242 g_assert_cmpint(done_count, ==, 2);
243 g_assert(!locked);
246 static void test_co_mutex(void)
248 CoMutex m;
250 qemu_co_mutex_init(&m);
251 do_test_co_mutex(mutex_fn, &m);
254 static void test_co_mutex_lockable(void)
256 CoMutex m;
257 CoMutex *null_pointer = NULL;
259 qemu_co_mutex_init(&m);
260 do_test_co_mutex(lockable_fn, QEMU_MAKE_LOCKABLE(&m));
262 g_assert(QEMU_MAKE_LOCKABLE(null_pointer) == NULL);
265 static CoRwlock rwlock;
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:
271 * | c1 | c2 |
272 * |--------------+------------+
273 * | rdlock | |
274 * | yield | |
275 * | | wrlock |
276 * | | <queued> |
277 * | upgrade | |
278 * | <queued> | <dequeued> |
279 * | | unlock |
280 * | <dequeued> | |
281 * | unlock | |
284 static void coroutine_fn rwlock_yield_upgrade(void *opaque)
286 qemu_co_rwlock_rdlock(&rwlock);
287 qemu_coroutine_yield();
289 qemu_co_rwlock_upgrade(&rwlock);
290 qemu_co_rwlock_unlock(&rwlock);
292 *(bool *)opaque = true;
295 static void coroutine_fn rwlock_wrlock_yield(void *opaque)
297 qemu_co_rwlock_wrlock(&rwlock);
298 qemu_coroutine_yield();
300 qemu_co_rwlock_unlock(&rwlock);
301 *(bool *)opaque = true;
304 static void test_co_rwlock_upgrade(void)
306 bool c1_done = false;
307 bool c2_done = false;
308 Coroutine *c1, *c2;
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);
314 qemu_coroutine_enter(c1);
315 qemu_coroutine_enter(c2);
317 /* c1 now should go to sleep. */
318 qemu_coroutine_enter(c1);
319 g_assert(!c1_done);
321 qemu_coroutine_enter(c2);
322 g_assert(c1_done);
323 g_assert(c2_done);
326 static void coroutine_fn rwlock_rdlock_yield(void *opaque)
328 qemu_co_rwlock_rdlock(&rwlock);
329 qemu_coroutine_yield();
331 qemu_co_rwlock_unlock(&rwlock);
332 qemu_coroutine_yield();
334 *(bool *)opaque = true;
337 static void coroutine_fn rwlock_wrlock_downgrade(void *opaque)
339 qemu_co_rwlock_wrlock(&rwlock);
341 qemu_co_rwlock_downgrade(&rwlock);
342 qemu_co_rwlock_unlock(&rwlock);
343 *(bool *)opaque = true;
346 static void coroutine_fn rwlock_rdlock(void *opaque)
348 qemu_co_rwlock_rdlock(&rwlock);
350 qemu_co_rwlock_unlock(&rwlock);
351 *(bool *)opaque = true;
354 static void coroutine_fn rwlock_wrlock(void *opaque)
356 qemu_co_rwlock_wrlock(&rwlock);
358 qemu_co_rwlock_unlock(&rwlock);
359 *(bool *)opaque = true;
363 * Check that downgrading a reader-writer lock does not cause a hang.
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.
369 * The correct sequence of operations we aim to provoke can be
370 * represented as:
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 |
393 static void test_co_rwlock_downgrade(void)
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;
401 qemu_co_rwlock_init(&rwlock);
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);
408 qemu_coroutine_enter(c1);
409 qemu_coroutine_enter(c2);
410 qemu_coroutine_enter(c3);
411 qemu_coroutine_enter(c4);
413 qemu_coroutine_enter(c1);
415 g_assert(c2_done);
416 g_assert(c3_done);
417 g_assert(c4_done);
419 qemu_coroutine_enter(c1);
421 g_assert(c1_done);
425 * Check that creation, enter, and return work
428 static void coroutine_fn set_and_exit(void *opaque)
430 bool *done = opaque;
432 *done = true;
435 static void test_lifecycle(void)
437 Coroutine *coroutine;
438 bool done = false;
440 /* Create, enter, and return from coroutine */
441 coroutine = qemu_coroutine_create(set_and_exit, &done);
442 qemu_coroutine_enter(coroutine);
443 g_assert(done); /* expect done to be true (first time) */
445 /* Repeat to check that no state affects this test */
446 done = false;
447 coroutine = qemu_coroutine_create(set_and_exit, &done);
448 qemu_coroutine_enter(coroutine);
449 g_assert(done); /* expect done to be true (second time) */
453 #define RECORD_SIZE 10 /* Leave some room for expansion */
454 struct coroutine_position {
455 int func;
456 int state;
458 static struct coroutine_position records[RECORD_SIZE];
459 static unsigned record_pos;
461 static void record_push(int func, int state)
463 struct coroutine_position *cp = &records[record_pos++];
464 g_assert_cmpint(record_pos, <, RECORD_SIZE);
465 cp->func = func;
466 cp->state = state;
469 static void coroutine_fn co_order_test(void *opaque)
471 record_push(2, 1);
472 g_assert(qemu_in_coroutine());
473 qemu_coroutine_yield();
474 record_push(2, 2);
475 g_assert(qemu_in_coroutine());
478 static void do_order_test(void)
480 Coroutine *co;
482 co = qemu_coroutine_create(co_order_test, NULL);
483 record_push(1, 1);
484 qemu_coroutine_enter(co);
485 record_push(1, 2);
486 g_assert(!qemu_in_coroutine());
487 qemu_coroutine_enter(co);
488 record_push(1, 3);
489 g_assert(!qemu_in_coroutine());
492 static void test_order(void)
494 int i;
495 const struct coroutine_position expected_pos[] = {
496 {1, 1,}, {2, 1}, {1, 2}, {2, 2}, {1, 3}
498 do_order_test();
499 g_assert_cmpint(record_pos, ==, 5);
500 for (i = 0; i < record_pos; i++) {
501 g_assert_cmpint(records[i].func , ==, expected_pos[i].func );
502 g_assert_cmpint(records[i].state, ==, expected_pos[i].state);
506 * Lifecycle benchmark
509 static void coroutine_fn empty_coroutine(void *opaque)
511 /* Do nothing */
514 static void perf_lifecycle(void)
516 Coroutine *coroutine;
517 unsigned int i, max;
518 double duration;
520 max = 1000000;
522 g_test_timer_start();
523 for (i = 0; i < max; i++) {
524 coroutine = qemu_coroutine_create(empty_coroutine, NULL);
525 qemu_coroutine_enter(coroutine);
527 duration = g_test_timer_elapsed();
529 g_test_message("Lifecycle %u iterations: %f s", max, duration);
532 static void perf_nesting(void)
534 unsigned int i, maxcycles, maxnesting;
535 double duration;
537 maxcycles = 10000;
538 maxnesting = 1000;
539 Coroutine *root;
541 g_test_timer_start();
542 for (i = 0; i < maxcycles; i++) {
543 NestData nd = {
544 .n_enter = 0,
545 .n_return = 0,
546 .max = maxnesting,
548 root = qemu_coroutine_create(nest, &nd);
549 qemu_coroutine_enter(root);
551 duration = g_test_timer_elapsed();
553 g_test_message("Nesting %u iterations of %u depth each: %f s",
554 maxcycles, maxnesting, duration);
558 * Yield benchmark
561 static void coroutine_fn yield_loop(void *opaque)
563 unsigned int *counter = opaque;
565 while ((*counter) > 0) {
566 (*counter)--;
567 qemu_coroutine_yield();
571 static void perf_yield(void)
573 unsigned int i, maxcycles;
574 double duration;
576 maxcycles = 100000000;
577 i = maxcycles;
578 Coroutine *coroutine = qemu_coroutine_create(yield_loop, &i);
580 g_test_timer_start();
581 while (i > 0) {
582 qemu_coroutine_enter(coroutine);
584 duration = g_test_timer_elapsed();
586 g_test_message("Yield %u iterations: %f s", maxcycles, duration);
589 static __attribute__((noinline)) void dummy(unsigned *i)
591 (*i)--;
594 static void perf_baseline(void)
596 unsigned int i, maxcycles;
597 double duration;
599 maxcycles = 100000000;
600 i = maxcycles;
602 g_test_timer_start();
603 while (i > 0) {
604 dummy(&i);
606 duration = g_test_timer_elapsed();
608 g_test_message("Function call %u iterations: %f s", maxcycles, duration);
611 static __attribute__((noinline)) void coroutine_fn perf_cost_func(void *opaque)
613 qemu_coroutine_yield();
616 static void perf_cost(void)
618 const unsigned long maxcycles = 40000000;
619 unsigned long i = 0;
620 double duration;
621 unsigned long ops;
622 Coroutine *co;
624 g_test_timer_start();
625 while (i++ < maxcycles) {
626 co = qemu_coroutine_create(perf_cost_func, &i);
627 qemu_coroutine_enter(co);
628 qemu_coroutine_enter(co);
630 duration = g_test_timer_elapsed();
631 ops = (long)(maxcycles / (duration * 1000));
633 g_test_message("Run operation %lu iterations %f s, %luK operations/s, "
634 "%luns per coroutine",
635 maxcycles,
636 duration, ops,
637 (unsigned long)(1000000000.0 * duration / maxcycles));
640 int main(int argc, char **argv)
642 g_test_init(&argc, &argv, NULL);
644 /* This test assumes there is a freelist and marks freed coroutine memory
645 * with a sentinel value. If there is no freelist this would legitimately
646 * crash, so skip it.
648 if (IS_ENABLED(CONFIG_COROUTINE_POOL)) {
649 g_test_add_func("/basic/no-dangling-access", test_no_dangling_access);
652 g_test_add_func("/basic/lifecycle", test_lifecycle);
653 g_test_add_func("/basic/yield", test_yield);
654 g_test_add_func("/basic/nesting", test_nesting);
655 g_test_add_func("/basic/self", test_self);
656 g_test_add_func("/basic/entered", test_entered);
657 g_test_add_func("/basic/in_coroutine", test_in_coroutine);
658 g_test_add_func("/basic/order", test_order);
659 g_test_add_func("/locking/co-mutex", test_co_mutex);
660 g_test_add_func("/locking/co-mutex/lockable", test_co_mutex_lockable);
661 g_test_add_func("/locking/co-rwlock/upgrade", test_co_rwlock_upgrade);
662 g_test_add_func("/locking/co-rwlock/downgrade", test_co_rwlock_downgrade);
663 if (g_test_perf()) {
664 g_test_add_func("/perf/lifecycle", perf_lifecycle);
665 g_test_add_func("/perf/nesting", perf_nesting);
666 g_test_add_func("/perf/yield", perf_yield);
667 g_test_add_func("/perf/function-call", perf_baseline);
668 g_test_add_func("/perf/cost", perf_cost);
670 return g_test_run();