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.
14 #include "qemu/osdep.h"
15 #include "qemu/coroutine.h"
16 #include "qemu/coroutine_int.h"
19 * Check that qemu_in_coroutine() works
22 static void coroutine_fn
verify_in_coroutine(void *opaque
)
24 g_assert(qemu_in_coroutine());
27 static void test_in_coroutine(void)
31 g_assert(!qemu_in_coroutine());
33 coroutine
= qemu_coroutine_create(verify_in_coroutine
, NULL
);
34 qemu_coroutine_enter(coroutine
);
38 * Check that qemu_coroutine_self() works
41 static void coroutine_fn
verify_self(void *opaque
)
43 Coroutine
**p_co
= opaque
;
44 g_assert(qemu_coroutine_self() == *p_co
);
47 static void test_self(void)
51 coroutine
= qemu_coroutine_create(verify_self
, &coroutine
);
52 qemu_coroutine_enter(coroutine
);
56 * Check that qemu_coroutine_entered() works
59 static void coroutine_fn
verify_entered_step_2(void *opaque
)
61 Coroutine
*caller
= (Coroutine
*)opaque
;
63 g_assert(qemu_coroutine_entered(caller
));
64 g_assert(qemu_coroutine_entered(qemu_coroutine_self()));
65 qemu_coroutine_yield();
67 /* Once more to check it still works after yielding */
68 g_assert(qemu_coroutine_entered(caller
));
69 g_assert(qemu_coroutine_entered(qemu_coroutine_self()));
72 static void coroutine_fn
verify_entered_step_1(void *opaque
)
74 Coroutine
*self
= qemu_coroutine_self();
77 g_assert(qemu_coroutine_entered(self
));
79 coroutine
= qemu_coroutine_create(verify_entered_step_2
, self
);
80 g_assert(!qemu_coroutine_entered(coroutine
));
81 qemu_coroutine_enter(coroutine
);
82 g_assert(!qemu_coroutine_entered(coroutine
));
83 qemu_coroutine_enter(coroutine
);
86 static void test_entered(void)
90 coroutine
= qemu_coroutine_create(verify_entered_step_1
, NULL
);
91 g_assert(!qemu_coroutine_entered(coroutine
));
92 qemu_coroutine_enter(coroutine
);
96 * Check that coroutines may nest multiple levels
100 unsigned int n_enter
; /* num coroutines entered */
101 unsigned int n_return
; /* num coroutines returned */
102 unsigned int max
; /* maximum level of nesting */
105 static void coroutine_fn
nest(void *opaque
)
107 NestData
*nd
= opaque
;
111 if (nd
->n_enter
< nd
->max
) {
114 child
= qemu_coroutine_create(nest
, nd
);
115 qemu_coroutine_enter(child
);
121 static void test_nesting(void)
130 root
= qemu_coroutine_create(nest
, &nd
);
131 qemu_coroutine_enter(root
);
133 /* Must enter and return from max nesting level */
134 g_assert_cmpint(nd
.n_enter
, ==, nd
.max
);
135 g_assert_cmpint(nd
.n_return
, ==, nd
.max
);
139 * Check that yield/enter transfer control correctly
142 static void coroutine_fn
yield_5_times(void *opaque
)
147 for (i
= 0; i
< 5; i
++) {
148 qemu_coroutine_yield();
153 static void test_yield(void)
155 Coroutine
*coroutine
;
157 int i
= -1; /* one extra time to return from coroutine */
159 coroutine
= qemu_coroutine_create(yield_5_times
, &done
);
161 qemu_coroutine_enter(coroutine
);
164 g_assert_cmpint(i
, ==, 5); /* coroutine must yield 5 times */
167 static void coroutine_fn
c2_fn(void *opaque
)
169 qemu_coroutine_yield();
172 static void coroutine_fn
c1_fn(void *opaque
)
174 Coroutine
*c2
= opaque
;
175 qemu_coroutine_enter(c2
);
178 static void test_co_queue(void)
184 c2
= qemu_coroutine_create(c2_fn
, NULL
);
185 c1
= qemu_coroutine_create(c1_fn
, c2
);
187 qemu_coroutine_enter(c1
);
189 /* c1 shouldn't be used any more now; make sure we segfault if it is */
191 memset(c1
, 0xff, sizeof(Coroutine
));
192 qemu_coroutine_enter(c2
);
194 /* Must restore the coroutine now to avoid corrupted pool */
199 * Check that creation, enter, and return work
202 static void coroutine_fn
set_and_exit(void *opaque
)
209 static void test_lifecycle(void)
211 Coroutine
*coroutine
;
214 /* Create, enter, and return from coroutine */
215 coroutine
= qemu_coroutine_create(set_and_exit
, &done
);
216 qemu_coroutine_enter(coroutine
);
217 g_assert(done
); /* expect done to be true (first time) */
219 /* Repeat to check that no state affects this test */
221 coroutine
= qemu_coroutine_create(set_and_exit
, &done
);
222 qemu_coroutine_enter(coroutine
);
223 g_assert(done
); /* expect done to be true (second time) */
227 #define RECORD_SIZE 10 /* Leave some room for expansion */
228 struct coroutine_position
{
232 static struct coroutine_position records
[RECORD_SIZE
];
233 static unsigned record_pos
;
235 static void record_push(int func
, int state
)
237 struct coroutine_position
*cp
= &records
[record_pos
++];
238 g_assert_cmpint(record_pos
, <, RECORD_SIZE
);
243 static void coroutine_fn
co_order_test(void *opaque
)
246 g_assert(qemu_in_coroutine());
247 qemu_coroutine_yield();
249 g_assert(qemu_in_coroutine());
252 static void do_order_test(void)
256 co
= qemu_coroutine_create(co_order_test
, NULL
);
258 qemu_coroutine_enter(co
);
260 g_assert(!qemu_in_coroutine());
261 qemu_coroutine_enter(co
);
263 g_assert(!qemu_in_coroutine());
266 static void test_order(void)
269 const struct coroutine_position expected_pos
[] = {
270 {1, 1,}, {2, 1}, {1, 2}, {2, 2}, {1, 3}
273 g_assert_cmpint(record_pos
, ==, 5);
274 for (i
= 0; i
< record_pos
; i
++) {
275 g_assert_cmpint(records
[i
].func
, ==, expected_pos
[i
].func
);
276 g_assert_cmpint(records
[i
].state
, ==, expected_pos
[i
].state
);
280 * Lifecycle benchmark
283 static void coroutine_fn
empty_coroutine(void *opaque
)
288 static void perf_lifecycle(void)
290 Coroutine
*coroutine
;
296 g_test_timer_start();
297 for (i
= 0; i
< max
; i
++) {
298 coroutine
= qemu_coroutine_create(empty_coroutine
, NULL
);
299 qemu_coroutine_enter(coroutine
);
301 duration
= g_test_timer_elapsed();
303 g_test_message("Lifecycle %u iterations: %f s\n", max
, duration
);
306 static void perf_nesting(void)
308 unsigned int i
, maxcycles
, maxnesting
;
315 g_test_timer_start();
316 for (i
= 0; i
< maxcycles
; i
++) {
322 root
= qemu_coroutine_create(nest
, &nd
);
323 qemu_coroutine_enter(root
);
325 duration
= g_test_timer_elapsed();
327 g_test_message("Nesting %u iterations of %u depth each: %f s\n",
328 maxcycles
, maxnesting
, duration
);
335 static void coroutine_fn
yield_loop(void *opaque
)
337 unsigned int *counter
= opaque
;
339 while ((*counter
) > 0) {
341 qemu_coroutine_yield();
345 static void perf_yield(void)
347 unsigned int i
, maxcycles
;
350 maxcycles
= 100000000;
352 Coroutine
*coroutine
= qemu_coroutine_create(yield_loop
, &i
);
354 g_test_timer_start();
356 qemu_coroutine_enter(coroutine
);
358 duration
= g_test_timer_elapsed();
360 g_test_message("Yield %u iterations: %f s\n",
361 maxcycles
, duration
);
364 static __attribute__((noinline
)) void dummy(unsigned *i
)
369 static void perf_baseline(void)
371 unsigned int i
, maxcycles
;
374 maxcycles
= 100000000;
377 g_test_timer_start();
381 duration
= g_test_timer_elapsed();
383 g_test_message("Function call %u iterations: %f s\n",
384 maxcycles
, duration
);
387 static __attribute__((noinline
)) void perf_cost_func(void *opaque
)
389 qemu_coroutine_yield();
392 static void perf_cost(void)
394 const unsigned long maxcycles
= 40000000;
400 g_test_timer_start();
401 while (i
++ < maxcycles
) {
402 co
= qemu_coroutine_create(perf_cost_func
, &i
);
403 qemu_coroutine_enter(co
);
404 qemu_coroutine_enter(co
);
406 duration
= g_test_timer_elapsed();
407 ops
= (long)(maxcycles
/ (duration
* 1000));
409 g_test_message("Run operation %lu iterations %f s, %luK operations/s, "
410 "%luns per coroutine",
413 (unsigned long)(1000000000.0 * duration
/ maxcycles
));
416 int main(int argc
, char **argv
)
418 g_test_init(&argc
, &argv
, NULL
);
420 /* This test assumes there is a freelist and marks freed coroutine memory
421 * with a sentinel value. If there is no freelist this would legitimately
424 if (CONFIG_COROUTINE_POOL
) {
425 g_test_add_func("/basic/co_queue", test_co_queue
);
428 g_test_add_func("/basic/lifecycle", test_lifecycle
);
429 g_test_add_func("/basic/yield", test_yield
);
430 g_test_add_func("/basic/nesting", test_nesting
);
431 g_test_add_func("/basic/self", test_self
);
432 g_test_add_func("/basic/entered", test_entered
);
433 g_test_add_func("/basic/in_coroutine", test_in_coroutine
);
434 g_test_add_func("/basic/order", test_order
);
436 g_test_add_func("/perf/lifecycle", perf_lifecycle
);
437 g_test_add_func("/perf/nesting", perf_nesting
);
438 g_test_add_func("/perf/yield", perf_yield
);
439 g_test_add_func("/perf/function-call", perf_baseline
);
440 g_test_add_func("/perf/cost", perf_cost
);