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"
16 #include "qemu/coroutine.h"
17 #include "qemu/coroutine_int.h"
20 * Check that qemu_in_coroutine() works
23 static void coroutine_fn
verify_in_coroutine(void *opaque
)
25 g_assert(qemu_in_coroutine());
28 static void test_in_coroutine(void)
32 g_assert(!qemu_in_coroutine());
34 coroutine
= qemu_coroutine_create(verify_in_coroutine
);
35 qemu_coroutine_enter(coroutine
, NULL
);
39 * Check that qemu_coroutine_self() works
42 static void coroutine_fn
verify_self(void *opaque
)
44 g_assert(qemu_coroutine_self() == opaque
);
47 static void test_self(void)
51 coroutine
= qemu_coroutine_create(verify_self
);
52 qemu_coroutine_enter(coroutine
, 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
);
75 qemu_coroutine_enter(child
, nd
);
81 static void test_nesting(void)
90 root
= qemu_coroutine_create(nest
);
91 qemu_coroutine_enter(root
, &nd
);
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
);
121 qemu_coroutine_enter(coroutine
, &done
);
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
, NULL
);
138 static void test_co_queue(void)
143 c1
= qemu_coroutine_create(c1_fn
);
144 c2
= qemu_coroutine_create(c2_fn
);
146 qemu_coroutine_enter(c1
, c2
);
147 memset(c1
, 0xff, sizeof(Coroutine
));
148 qemu_coroutine_enter(c2
, NULL
);
152 * Check that creation, enter, and return work
155 static void coroutine_fn
set_and_exit(void *opaque
)
162 static void test_lifecycle(void)
164 Coroutine
*coroutine
;
167 /* Create, enter, and return from coroutine */
168 coroutine
= qemu_coroutine_create(set_and_exit
);
169 qemu_coroutine_enter(coroutine
, &done
);
170 g_assert(done
); /* expect done to be true (first time) */
172 /* Repeat to check that no state affects this test */
174 coroutine
= qemu_coroutine_create(set_and_exit
);
175 qemu_coroutine_enter(coroutine
, &done
);
176 g_assert(done
); /* expect done to be true (second time) */
180 #define RECORD_SIZE 10 /* Leave some room for expansion */
181 struct coroutine_position
{
185 static struct coroutine_position records
[RECORD_SIZE
];
186 static unsigned record_pos
;
188 static void record_push(int func
, int state
)
190 struct coroutine_position
*cp
= &records
[record_pos
++];
191 g_assert_cmpint(record_pos
, <, RECORD_SIZE
);
196 static void coroutine_fn
co_order_test(void *opaque
)
199 g_assert(qemu_in_coroutine());
200 qemu_coroutine_yield();
202 g_assert(qemu_in_coroutine());
205 static void do_order_test(void)
209 co
= qemu_coroutine_create(co_order_test
);
211 qemu_coroutine_enter(co
, NULL
);
213 g_assert(!qemu_in_coroutine());
214 qemu_coroutine_enter(co
, NULL
);
216 g_assert(!qemu_in_coroutine());
219 static void test_order(void)
222 const struct coroutine_position expected_pos
[] = {
223 {1, 1,}, {2, 1}, {1, 2}, {2, 2}, {1, 3}
226 g_assert_cmpint(record_pos
, ==, 5);
227 for (i
= 0; i
< record_pos
; i
++) {
228 g_assert_cmpint(records
[i
].func
, ==, expected_pos
[i
].func
);
229 g_assert_cmpint(records
[i
].state
, ==, expected_pos
[i
].state
);
233 * Lifecycle benchmark
236 static void coroutine_fn
empty_coroutine(void *opaque
)
241 static void perf_lifecycle(void)
243 Coroutine
*coroutine
;
249 g_test_timer_start();
250 for (i
= 0; i
< max
; i
++) {
251 coroutine
= qemu_coroutine_create(empty_coroutine
);
252 qemu_coroutine_enter(coroutine
, NULL
);
254 duration
= g_test_timer_elapsed();
256 g_test_message("Lifecycle %u iterations: %f s\n", max
, duration
);
259 static void perf_nesting(void)
261 unsigned int i
, maxcycles
, maxnesting
;
268 g_test_timer_start();
269 for (i
= 0; i
< maxcycles
; i
++) {
275 root
= qemu_coroutine_create(nest
);
276 qemu_coroutine_enter(root
, &nd
);
278 duration
= g_test_timer_elapsed();
280 g_test_message("Nesting %u iterations of %u depth each: %f s\n",
281 maxcycles
, maxnesting
, duration
);
288 static void coroutine_fn
yield_loop(void *opaque
)
290 unsigned int *counter
= opaque
;
292 while ((*counter
) > 0) {
294 qemu_coroutine_yield();
298 static void perf_yield(void)
300 unsigned int i
, maxcycles
;
303 maxcycles
= 100000000;
305 Coroutine
*coroutine
= qemu_coroutine_create(yield_loop
);
307 g_test_timer_start();
309 qemu_coroutine_enter(coroutine
, &i
);
311 duration
= g_test_timer_elapsed();
313 g_test_message("Yield %u iterations: %f s\n",
314 maxcycles
, duration
);
317 static __attribute__((noinline
)) void dummy(unsigned *i
)
322 static void perf_baseline(void)
324 unsigned int i
, maxcycles
;
327 maxcycles
= 100000000;
330 g_test_timer_start();
334 duration
= g_test_timer_elapsed();
336 g_test_message("Function call %u iterations: %f s\n",
337 maxcycles
, duration
);
340 static __attribute__((noinline
)) void perf_cost_func(void *opaque
)
342 qemu_coroutine_yield();
345 static void perf_cost(void)
347 const unsigned long maxcycles
= 40000000;
353 g_test_timer_start();
354 while (i
++ < maxcycles
) {
355 co
= qemu_coroutine_create(perf_cost_func
);
356 qemu_coroutine_enter(co
, &i
);
357 qemu_coroutine_enter(co
, NULL
);
359 duration
= g_test_timer_elapsed();
360 ops
= (long)(maxcycles
/ (duration
* 1000));
362 g_test_message("Run operation %lu iterations %f s, %luK operations/s, "
363 "%luns per coroutine",
366 (unsigned long)(1000000000.0 * duration
/ maxcycles
));
369 int main(int argc
, char **argv
)
371 g_test_init(&argc
, &argv
, NULL
);
372 g_test_add_func("/basic/co_queue", test_co_queue
);
373 g_test_add_func("/basic/lifecycle", test_lifecycle
);
374 g_test_add_func("/basic/yield", test_yield
);
375 g_test_add_func("/basic/nesting", test_nesting
);
376 g_test_add_func("/basic/self", test_self
);
377 g_test_add_func("/basic/in_coroutine", test_in_coroutine
);
378 g_test_add_func("/basic/order", test_order
);
380 g_test_add_func("/perf/lifecycle", perf_lifecycle
);
381 g_test_add_func("/perf/nesting", perf_nesting
);
382 g_test_add_func("/perf/yield", perf_yield
);
383 g_test_add_func("/perf/function-call", perf_baseline
);
384 g_test_add_func("/perf/cost", perf_cost
);