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 coroutines may nest multiple levels
60 unsigned int n_enter
; /* num coroutines entered */
61 unsigned int n_return
; /* num coroutines returned */
62 unsigned int max
; /* maximum level of nesting */
65 static void coroutine_fn
nest(void *opaque
)
67 NestData
*nd
= opaque
;
71 if (nd
->n_enter
< nd
->max
) {
74 child
= qemu_coroutine_create(nest
, nd
);
75 qemu_coroutine_enter(child
);
81 static void test_nesting(void)
90 root
= qemu_coroutine_create(nest
, &nd
);
91 qemu_coroutine_enter(root
);
93 /* Must enter and return from max nesting level */
94 g_assert_cmpint(nd
.n_enter
, ==, nd
.max
);
95 g_assert_cmpint(nd
.n_return
, ==, nd
.max
);
99 * Check that yield/enter transfer control correctly
102 static void coroutine_fn
yield_5_times(void *opaque
)
107 for (i
= 0; i
< 5; i
++) {
108 qemu_coroutine_yield();
113 static void test_yield(void)
115 Coroutine
*coroutine
;
117 int i
= -1; /* one extra time to return from coroutine */
119 coroutine
= qemu_coroutine_create(yield_5_times
, &done
);
121 qemu_coroutine_enter(coroutine
);
124 g_assert_cmpint(i
, ==, 5); /* coroutine must yield 5 times */
127 static void coroutine_fn
c2_fn(void *opaque
)
129 qemu_coroutine_yield();
132 static void coroutine_fn
c1_fn(void *opaque
)
134 Coroutine
*c2
= opaque
;
135 qemu_coroutine_enter(c2
);
138 static void test_co_queue(void)
144 c2
= qemu_coroutine_create(c2_fn
, NULL
);
145 c1
= qemu_coroutine_create(c1_fn
, c2
);
147 qemu_coroutine_enter(c1
);
149 /* c1 shouldn't be used any more now; make sure we segfault if it is */
151 memset(c1
, 0xff, sizeof(Coroutine
));
152 qemu_coroutine_enter(c2
);
154 /* Must restore the coroutine now to avoid corrupted pool */
159 * Check that creation, enter, and return work
162 static void coroutine_fn
set_and_exit(void *opaque
)
169 static void test_lifecycle(void)
171 Coroutine
*coroutine
;
174 /* Create, enter, and return from coroutine */
175 coroutine
= qemu_coroutine_create(set_and_exit
, &done
);
176 qemu_coroutine_enter(coroutine
);
177 g_assert(done
); /* expect done to be true (first time) */
179 /* Repeat to check that no state affects this test */
181 coroutine
= qemu_coroutine_create(set_and_exit
, &done
);
182 qemu_coroutine_enter(coroutine
);
183 g_assert(done
); /* expect done to be true (second time) */
187 #define RECORD_SIZE 10 /* Leave some room for expansion */
188 struct coroutine_position
{
192 static struct coroutine_position records
[RECORD_SIZE
];
193 static unsigned record_pos
;
195 static void record_push(int func
, int state
)
197 struct coroutine_position
*cp
= &records
[record_pos
++];
198 g_assert_cmpint(record_pos
, <, RECORD_SIZE
);
203 static void coroutine_fn
co_order_test(void *opaque
)
206 g_assert(qemu_in_coroutine());
207 qemu_coroutine_yield();
209 g_assert(qemu_in_coroutine());
212 static void do_order_test(void)
216 co
= qemu_coroutine_create(co_order_test
, NULL
);
218 qemu_coroutine_enter(co
);
220 g_assert(!qemu_in_coroutine());
221 qemu_coroutine_enter(co
);
223 g_assert(!qemu_in_coroutine());
226 static void test_order(void)
229 const struct coroutine_position expected_pos
[] = {
230 {1, 1,}, {2, 1}, {1, 2}, {2, 2}, {1, 3}
233 g_assert_cmpint(record_pos
, ==, 5);
234 for (i
= 0; i
< record_pos
; i
++) {
235 g_assert_cmpint(records
[i
].func
, ==, expected_pos
[i
].func
);
236 g_assert_cmpint(records
[i
].state
, ==, expected_pos
[i
].state
);
240 * Lifecycle benchmark
243 static void coroutine_fn
empty_coroutine(void *opaque
)
248 static void perf_lifecycle(void)
250 Coroutine
*coroutine
;
256 g_test_timer_start();
257 for (i
= 0; i
< max
; i
++) {
258 coroutine
= qemu_coroutine_create(empty_coroutine
, NULL
);
259 qemu_coroutine_enter(coroutine
);
261 duration
= g_test_timer_elapsed();
263 g_test_message("Lifecycle %u iterations: %f s\n", max
, duration
);
266 static void perf_nesting(void)
268 unsigned int i
, maxcycles
, maxnesting
;
275 g_test_timer_start();
276 for (i
= 0; i
< maxcycles
; i
++) {
282 root
= qemu_coroutine_create(nest
, &nd
);
283 qemu_coroutine_enter(root
);
285 duration
= g_test_timer_elapsed();
287 g_test_message("Nesting %u iterations of %u depth each: %f s\n",
288 maxcycles
, maxnesting
, duration
);
295 static void coroutine_fn
yield_loop(void *opaque
)
297 unsigned int *counter
= opaque
;
299 while ((*counter
) > 0) {
301 qemu_coroutine_yield();
305 static void perf_yield(void)
307 unsigned int i
, maxcycles
;
310 maxcycles
= 100000000;
312 Coroutine
*coroutine
= qemu_coroutine_create(yield_loop
, &i
);
314 g_test_timer_start();
316 qemu_coroutine_enter(coroutine
);
318 duration
= g_test_timer_elapsed();
320 g_test_message("Yield %u iterations: %f s\n",
321 maxcycles
, duration
);
324 static __attribute__((noinline
)) void dummy(unsigned *i
)
329 static void perf_baseline(void)
331 unsigned int i
, maxcycles
;
334 maxcycles
= 100000000;
337 g_test_timer_start();
341 duration
= g_test_timer_elapsed();
343 g_test_message("Function call %u iterations: %f s\n",
344 maxcycles
, duration
);
347 static __attribute__((noinline
)) void perf_cost_func(void *opaque
)
349 qemu_coroutine_yield();
352 static void perf_cost(void)
354 const unsigned long maxcycles
= 40000000;
360 g_test_timer_start();
361 while (i
++ < maxcycles
) {
362 co
= qemu_coroutine_create(perf_cost_func
, &i
);
363 qemu_coroutine_enter(co
);
364 qemu_coroutine_enter(co
);
366 duration
= g_test_timer_elapsed();
367 ops
= (long)(maxcycles
/ (duration
* 1000));
369 g_test_message("Run operation %lu iterations %f s, %luK operations/s, "
370 "%luns per coroutine",
373 (unsigned long)(1000000000.0 * duration
/ maxcycles
));
376 int main(int argc
, char **argv
)
378 g_test_init(&argc
, &argv
, NULL
);
380 /* This test assumes there is a freelist and marks freed coroutine memory
381 * with a sentinel value. If there is no freelist this would legitimately
384 if (CONFIG_COROUTINE_POOL
) {
385 g_test_add_func("/basic/co_queue", test_co_queue
);
388 g_test_add_func("/basic/lifecycle", test_lifecycle
);
389 g_test_add_func("/basic/yield", test_yield
);
390 g_test_add_func("/basic/nesting", test_nesting
);
391 g_test_add_func("/basic/self", test_self
);
392 g_test_add_func("/basic/in_coroutine", test_in_coroutine
);
393 g_test_add_func("/basic/order", test_order
);
395 g_test_add_func("/perf/lifecycle", perf_lifecycle
);
396 g_test_add_func("/perf/nesting", perf_nesting
);
397 g_test_add_func("/perf/yield", perf_yield
);
398 g_test_add_func("/perf/function-call", perf_baseline
);
399 g_test_add_func("/perf/cost", perf_cost
);