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
);
34 qemu_coroutine_enter(coroutine
, NULL
);
38 * Check that qemu_coroutine_self() works
41 static void coroutine_fn
verify_self(void *opaque
)
43 g_assert(qemu_coroutine_self() == opaque
);
46 static void test_self(void)
50 coroutine
= qemu_coroutine_create(verify_self
);
51 qemu_coroutine_enter(coroutine
, coroutine
);
55 * Check that coroutines may nest multiple levels
59 unsigned int n_enter
; /* num coroutines entered */
60 unsigned int n_return
; /* num coroutines returned */
61 unsigned int max
; /* maximum level of nesting */
64 static void coroutine_fn
nest(void *opaque
)
66 NestData
*nd
= opaque
;
70 if (nd
->n_enter
< nd
->max
) {
73 child
= qemu_coroutine_create(nest
);
74 qemu_coroutine_enter(child
, nd
);
80 static void test_nesting(void)
89 root
= qemu_coroutine_create(nest
);
90 qemu_coroutine_enter(root
, &nd
);
92 /* Must enter and return from max nesting level */
93 g_assert_cmpint(nd
.n_enter
, ==, nd
.max
);
94 g_assert_cmpint(nd
.n_return
, ==, nd
.max
);
98 * Check that yield/enter transfer control correctly
101 static void coroutine_fn
yield_5_times(void *opaque
)
106 for (i
= 0; i
< 5; i
++) {
107 qemu_coroutine_yield();
112 static void test_yield(void)
114 Coroutine
*coroutine
;
116 int i
= -1; /* one extra time to return from coroutine */
118 coroutine
= qemu_coroutine_create(yield_5_times
);
120 qemu_coroutine_enter(coroutine
, &done
);
123 g_assert_cmpint(i
, ==, 5); /* coroutine must yield 5 times */
126 static void coroutine_fn
c2_fn(void *opaque
)
128 qemu_coroutine_yield();
131 static void coroutine_fn
c1_fn(void *opaque
)
133 Coroutine
*c2
= opaque
;
134 qemu_coroutine_enter(c2
, NULL
);
137 static void test_co_queue(void)
142 c1
= qemu_coroutine_create(c1_fn
);
143 c2
= qemu_coroutine_create(c2_fn
);
145 qemu_coroutine_enter(c1
, c2
);
146 memset(c1
, 0xff, sizeof(Coroutine
));
147 qemu_coroutine_enter(c2
, NULL
);
151 * Check that creation, enter, and return work
154 static void coroutine_fn
set_and_exit(void *opaque
)
161 static void test_lifecycle(void)
163 Coroutine
*coroutine
;
166 /* Create, enter, and return from coroutine */
167 coroutine
= qemu_coroutine_create(set_and_exit
);
168 qemu_coroutine_enter(coroutine
, &done
);
169 g_assert(done
); /* expect done to be true (first time) */
171 /* Repeat to check that no state affects this test */
173 coroutine
= qemu_coroutine_create(set_and_exit
);
174 qemu_coroutine_enter(coroutine
, &done
);
175 g_assert(done
); /* expect done to be true (second time) */
179 #define RECORD_SIZE 10 /* Leave some room for expansion */
180 struct coroutine_position
{
184 static struct coroutine_position records
[RECORD_SIZE
];
185 static unsigned record_pos
;
187 static void record_push(int func
, int state
)
189 struct coroutine_position
*cp
= &records
[record_pos
++];
190 g_assert_cmpint(record_pos
, <, RECORD_SIZE
);
195 static void coroutine_fn
co_order_test(void *opaque
)
198 g_assert(qemu_in_coroutine());
199 qemu_coroutine_yield();
201 g_assert(qemu_in_coroutine());
204 static void do_order_test(void)
208 co
= qemu_coroutine_create(co_order_test
);
210 qemu_coroutine_enter(co
, NULL
);
212 g_assert(!qemu_in_coroutine());
213 qemu_coroutine_enter(co
, NULL
);
215 g_assert(!qemu_in_coroutine());
218 static void test_order(void)
221 const struct coroutine_position expected_pos
[] = {
222 {1, 1,}, {2, 1}, {1, 2}, {2, 2}, {1, 3}
225 g_assert_cmpint(record_pos
, ==, 5);
226 for (i
= 0; i
< record_pos
; i
++) {
227 g_assert_cmpint(records
[i
].func
, ==, expected_pos
[i
].func
);
228 g_assert_cmpint(records
[i
].state
, ==, expected_pos
[i
].state
);
232 * Lifecycle benchmark
235 static void coroutine_fn
empty_coroutine(void *opaque
)
240 static void perf_lifecycle(void)
242 Coroutine
*coroutine
;
248 g_test_timer_start();
249 for (i
= 0; i
< max
; i
++) {
250 coroutine
= qemu_coroutine_create(empty_coroutine
);
251 qemu_coroutine_enter(coroutine
, NULL
);
253 duration
= g_test_timer_elapsed();
255 g_test_message("Lifecycle %u iterations: %f s\n", max
, duration
);
258 static void perf_nesting(void)
260 unsigned int i
, maxcycles
, maxnesting
;
267 g_test_timer_start();
268 for (i
= 0; i
< maxcycles
; i
++) {
274 root
= qemu_coroutine_create(nest
);
275 qemu_coroutine_enter(root
, &nd
);
277 duration
= g_test_timer_elapsed();
279 g_test_message("Nesting %u iterations of %u depth each: %f s\n",
280 maxcycles
, maxnesting
, duration
);
287 static void coroutine_fn
yield_loop(void *opaque
)
289 unsigned int *counter
= opaque
;
291 while ((*counter
) > 0) {
293 qemu_coroutine_yield();
297 static void perf_yield(void)
299 unsigned int i
, maxcycles
;
302 maxcycles
= 100000000;
304 Coroutine
*coroutine
= qemu_coroutine_create(yield_loop
);
306 g_test_timer_start();
308 qemu_coroutine_enter(coroutine
, &i
);
310 duration
= g_test_timer_elapsed();
312 g_test_message("Yield %u iterations: %f s\n",
313 maxcycles
, duration
);
316 static __attribute__((noinline
)) void dummy(unsigned *i
)
321 static void perf_baseline(void)
323 unsigned int i
, maxcycles
;
326 maxcycles
= 100000000;
329 g_test_timer_start();
333 duration
= g_test_timer_elapsed();
335 g_test_message("Function call %u iterations: %f s\n",
336 maxcycles
, duration
);
339 static __attribute__((noinline
)) void perf_cost_func(void *opaque
)
341 qemu_coroutine_yield();
344 static void perf_cost(void)
346 const unsigned long maxcycles
= 40000000;
352 g_test_timer_start();
353 while (i
++ < maxcycles
) {
354 co
= qemu_coroutine_create(perf_cost_func
);
355 qemu_coroutine_enter(co
, &i
);
356 qemu_coroutine_enter(co
, NULL
);
358 duration
= g_test_timer_elapsed();
359 ops
= (long)(maxcycles
/ (duration
* 1000));
361 g_test_message("Run operation %lu iterations %f s, %luK operations/s, "
362 "%luns per coroutine",
365 (unsigned long)(1000000000.0 * duration
/ maxcycles
));
368 int main(int argc
, char **argv
)
370 g_test_init(&argc
, &argv
, NULL
);
372 /* This test assumes there is a freelist and marks freed coroutine memory
373 * with a sentinel value. If there is no freelist this would legitimately
376 if (CONFIG_COROUTINE_POOL
) {
377 g_test_add_func("/basic/co_queue", test_co_queue
);
380 g_test_add_func("/basic/lifecycle", test_lifecycle
);
381 g_test_add_func("/basic/yield", test_yield
);
382 g_test_add_func("/basic/nesting", test_nesting
);
383 g_test_add_func("/basic/self", test_self
);
384 g_test_add_func("/basic/in_coroutine", test_in_coroutine
);
385 g_test_add_func("/basic/order", test_order
);
387 g_test_add_func("/perf/lifecycle", perf_lifecycle
);
388 g_test_add_func("/perf/nesting", perf_nesting
);
389 g_test_add_func("/perf/yield", perf_yield
);
390 g_test_add_func("/perf/function-call", perf_baseline
);
391 g_test_add_func("/perf/cost", perf_cost
);