qemu-log: Improve the "exec" TB execution logging
[qemu/ar7.git] / tests / test-coroutine.c
blobdd4ced946c2f9f4bd5d1c11b7402a6b6ee5bc632
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 <glib.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)
30 Coroutine *coroutine;
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)
49 Coroutine *coroutine;
51 coroutine = qemu_coroutine_create(verify_self);
52 qemu_coroutine_enter(coroutine, 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);
75 qemu_coroutine_enter(child, nd);
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);
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)
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);
120 while (!done) {
121 qemu_coroutine_enter(coroutine, &done);
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, NULL);
138 static void test_co_queue(void)
140 Coroutine *c1;
141 Coroutine *c2;
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)
157 bool *done = opaque;
159 *done = true;
162 static void test_lifecycle(void)
164 Coroutine *coroutine;
165 bool done = false;
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 */
173 done = false;
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 {
182 int func;
183 int state;
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);
192 cp->func = func;
193 cp->state = state;
196 static void coroutine_fn co_order_test(void *opaque)
198 record_push(2, 1);
199 g_assert(qemu_in_coroutine());
200 qemu_coroutine_yield();
201 record_push(2, 2);
202 g_assert(qemu_in_coroutine());
205 static void do_order_test(void)
207 Coroutine *co;
209 co = qemu_coroutine_create(co_order_test);
210 record_push(1, 1);
211 qemu_coroutine_enter(co, NULL);
212 record_push(1, 2);
213 g_assert(!qemu_in_coroutine());
214 qemu_coroutine_enter(co, NULL);
215 record_push(1, 3);
216 g_assert(!qemu_in_coroutine());
219 static void test_order(void)
221 int i;
222 const struct coroutine_position expected_pos[] = {
223 {1, 1,}, {2, 1}, {1, 2}, {2, 2}, {1, 3}
225 do_order_test();
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)
238 /* Do nothing */
241 static void perf_lifecycle(void)
243 Coroutine *coroutine;
244 unsigned int i, max;
245 double duration;
247 max = 1000000;
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;
262 double duration;
264 maxcycles = 10000;
265 maxnesting = 1000;
266 Coroutine *root;
268 g_test_timer_start();
269 for (i = 0; i < maxcycles; i++) {
270 NestData nd = {
271 .n_enter = 0,
272 .n_return = 0,
273 .max = maxnesting,
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);
285 * Yield benchmark
288 static void coroutine_fn yield_loop(void *opaque)
290 unsigned int *counter = opaque;
292 while ((*counter) > 0) {
293 (*counter)--;
294 qemu_coroutine_yield();
298 static void perf_yield(void)
300 unsigned int i, maxcycles;
301 double duration;
303 maxcycles = 100000000;
304 i = maxcycles;
305 Coroutine *coroutine = qemu_coroutine_create(yield_loop);
307 g_test_timer_start();
308 while (i > 0) {
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)
319 (*i)--;
322 static void perf_baseline(void)
324 unsigned int i, maxcycles;
325 double duration;
327 maxcycles = 100000000;
328 i = maxcycles;
330 g_test_timer_start();
331 while (i > 0) {
332 dummy(&i);
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;
348 unsigned long i = 0;
349 double duration;
350 unsigned long ops;
351 Coroutine *co;
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",
364 maxcycles,
365 duration, ops,
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);
379 if (g_test_perf()) {
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);
386 return g_test_run();