4 * Copyright IBM, Corp. 2011
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.
15 #include "qemu-coroutine.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)
30 g_assert(!qemu_in_coroutine());
32 coroutine = qemu_coroutine_create(verify_in_coroutine);
33 qemu_coroutine_enter(coroutine, NULL);
37 * Check that qemu_coroutine_self() works
40 static void coroutine_fn verify_self(void *opaque)
42 g_assert(qemu_coroutine_self() == opaque);
45 static void test_self(void)
49 coroutine = qemu_coroutine_create(verify_self);
50 qemu_coroutine_enter(coroutine, coroutine);
54 * Check that coroutines may nest multiple levels
58 unsigned int n_enter; /* num coroutines entered */
59 unsigned int n_return; /* num coroutines returned */
60 unsigned int max; /* maximum level of nesting */
63 static void coroutine_fn nest(void *opaque)
65 NestData *nd = opaque;
69 if (nd->n_enter < nd->max) {
72 child = qemu_coroutine_create(nest);
73 qemu_coroutine_enter(child, nd);
79 static void test_nesting(void)
88 root = qemu_coroutine_create(nest);
89 qemu_coroutine_enter(root, &nd);
91 /* Must enter and return from max nesting level */
92 g_assert_cmpint(nd.n_enter, ==, nd.max);
93 g_assert_cmpint(nd.n_return, ==, nd.max);
97 * Check that yield/enter transfer control correctly
100 static void coroutine_fn yield_5_times(void *opaque)
105 for (i = 0; i < 5; i++) {
106 qemu_coroutine_yield();
111 static void test_yield(void)
113 Coroutine *coroutine;
115 int i = -1; /* one extra time to return from coroutine */
117 coroutine = qemu_coroutine_create(yield_5_times);
119 qemu_coroutine_enter(coroutine, &done);
122 g_assert_cmpint(i, ==, 5); /* coroutine must yield 5 times */
126 * Check that creation, enter, and return work
129 static void coroutine_fn set_and_exit(void *opaque)
136 static void test_lifecycle(void)
138 Coroutine *coroutine;
141 /* Create, enter, and return from coroutine */
142 coroutine = qemu_coroutine_create(set_and_exit);
143 qemu_coroutine_enter(coroutine, &done);
144 g_assert(done); /* expect done to be true (first time) */
146 /* Repeat to check that no state affects this test */
148 coroutine = qemu_coroutine_create(set_and_exit);
149 qemu_coroutine_enter(coroutine, &done);
150 g_assert(done); /* expect done to be true (second time) */
154 * Lifecycle benchmark
157 static void coroutine_fn empty_coroutine(void *opaque)
162 static void perf_lifecycle(void)
164 Coroutine *coroutine;
170 g_test_timer_start();
171 for (i = 0; i < max; i++) {
172 coroutine = qemu_coroutine_create(empty_coroutine);
173 qemu_coroutine_enter(coroutine, NULL);
175 duration = g_test_timer_elapsed();
177 g_test_message("Lifecycle %u iterations: %f s\n", max, duration);
180 static void perf_nesting(void)
182 unsigned int i, maxcycles, maxnesting;
185 maxcycles = 100000000;
194 g_test_timer_start();
195 for (i = 0; i < maxcycles; i++) {
196 root = qemu_coroutine_create(nest);
197 qemu_coroutine_enter(root, &nd);
199 duration = g_test_timer_elapsed();
201 g_test_message("Nesting %u iterations of %u depth each: %f s\n",
202 maxcycles, maxnesting, duration);
206 int main(int argc, char **argv)
208 g_test_init(&argc, &argv, NULL);
209 g_test_add_func("/basic/lifecycle", test_lifecycle);
210 g_test_add_func("/basic/yield", test_yield);
211 g_test_add_func("/basic/nesting", test_nesting);
212 g_test_add_func("/basic/self", test_self);
213 g_test_add_func("/basic/in_coroutine", test_in_coroutine);
215 g_test_add_func("/perf/lifecycle", perf_lifecycle);
216 g_test_add_func("/perf/nesting", perf_nesting);