ppc: Fix CFAR updates
[qemu/kevin.git] / tests / test-coroutine.c
blob6431dd6d7cb3a575b3dc428dc308b98393847d1a
1 /*
2 * Coroutine tests
4 * Copyright IBM, Corp. 2011
6 * Authors:
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)
29 Coroutine *coroutine;
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)
49 Coroutine *coroutine;
51 coroutine = qemu_coroutine_create(verify_self, &coroutine);
52 qemu_coroutine_enter(coroutine);
56 * Check that coroutines may nest multiple levels
59 typedef struct {
60 unsigned int n_enter; /* num coroutines entered */
61 unsigned int n_return; /* num coroutines returned */
62 unsigned int max; /* maximum level of nesting */
63 } NestData;
65 static void coroutine_fn nest(void *opaque)
67 NestData *nd = opaque;
69 nd->n_enter++;
71 if (nd->n_enter < nd->max) {
72 Coroutine *child;
74 child = qemu_coroutine_create(nest, nd);
75 qemu_coroutine_enter(child);
78 nd->n_return++;
81 static void test_nesting(void)
83 Coroutine *root;
84 NestData nd = {
85 .n_enter = 0,
86 .n_return = 0,
87 .max = 128,
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)
104 bool *done = opaque;
105 int i;
107 for (i = 0; i < 5; i++) {
108 qemu_coroutine_yield();
110 *done = true;
113 static void test_yield(void)
115 Coroutine *coroutine;
116 bool done = false;
117 int i = -1; /* one extra time to return from coroutine */
119 coroutine = qemu_coroutine_create(yield_5_times, &done);
120 while (!done) {
121 qemu_coroutine_enter(coroutine);
122 i++;
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)
140 Coroutine *c1;
141 Coroutine *c2;
142 Coroutine tmp;
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 */
150 tmp = *c1;
151 memset(c1, 0xff, sizeof(Coroutine));
152 qemu_coroutine_enter(c2);
154 /* Must restore the coroutine now to avoid corrupted pool */
155 *c1 = tmp;
159 * Check that creation, enter, and return work
162 static void coroutine_fn set_and_exit(void *opaque)
164 bool *done = opaque;
166 *done = true;
169 static void test_lifecycle(void)
171 Coroutine *coroutine;
172 bool done = false;
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 */
180 done = false;
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 {
189 int func;
190 int state;
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);
199 cp->func = func;
200 cp->state = state;
203 static void coroutine_fn co_order_test(void *opaque)
205 record_push(2, 1);
206 g_assert(qemu_in_coroutine());
207 qemu_coroutine_yield();
208 record_push(2, 2);
209 g_assert(qemu_in_coroutine());
212 static void do_order_test(void)
214 Coroutine *co;
216 co = qemu_coroutine_create(co_order_test, NULL);
217 record_push(1, 1);
218 qemu_coroutine_enter(co);
219 record_push(1, 2);
220 g_assert(!qemu_in_coroutine());
221 qemu_coroutine_enter(co);
222 record_push(1, 3);
223 g_assert(!qemu_in_coroutine());
226 static void test_order(void)
228 int i;
229 const struct coroutine_position expected_pos[] = {
230 {1, 1,}, {2, 1}, {1, 2}, {2, 2}, {1, 3}
232 do_order_test();
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)
245 /* Do nothing */
248 static void perf_lifecycle(void)
250 Coroutine *coroutine;
251 unsigned int i, max;
252 double duration;
254 max = 1000000;
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;
269 double duration;
271 maxcycles = 10000;
272 maxnesting = 1000;
273 Coroutine *root;
275 g_test_timer_start();
276 for (i = 0; i < maxcycles; i++) {
277 NestData nd = {
278 .n_enter = 0,
279 .n_return = 0,
280 .max = maxnesting,
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);
292 * Yield benchmark
295 static void coroutine_fn yield_loop(void *opaque)
297 unsigned int *counter = opaque;
299 while ((*counter) > 0) {
300 (*counter)--;
301 qemu_coroutine_yield();
305 static void perf_yield(void)
307 unsigned int i, maxcycles;
308 double duration;
310 maxcycles = 100000000;
311 i = maxcycles;
312 Coroutine *coroutine = qemu_coroutine_create(yield_loop, &i);
314 g_test_timer_start();
315 while (i > 0) {
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)
326 (*i)--;
329 static void perf_baseline(void)
331 unsigned int i, maxcycles;
332 double duration;
334 maxcycles = 100000000;
335 i = maxcycles;
337 g_test_timer_start();
338 while (i > 0) {
339 dummy(&i);
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;
355 unsigned long i = 0;
356 double duration;
357 unsigned long ops;
358 Coroutine *co;
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",
371 maxcycles,
372 duration, ops,
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
382 * crash, so skip it.
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);
394 if (g_test_perf()) {
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);
401 return g_test_run();