net: check fragment length during fragmentation
[qemu/ar7.git] / tests / test-coroutine.c
blobee5e06d3273e32ac43f5013974e680fa34cc5eb3
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;
143 c2 = qemu_coroutine_create(c2_fn, NULL);
144 c1 = qemu_coroutine_create(c1_fn, c2);
146 qemu_coroutine_enter(c1);
147 memset(c1, 0xff, sizeof(Coroutine));
148 qemu_coroutine_enter(c2);
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, &done);
169 qemu_coroutine_enter(coroutine);
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, &done);
175 qemu_coroutine_enter(coroutine);
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, NULL);
210 record_push(1, 1);
211 qemu_coroutine_enter(co);
212 record_push(1, 2);
213 g_assert(!qemu_in_coroutine());
214 qemu_coroutine_enter(co);
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, NULL);
252 qemu_coroutine_enter(coroutine);
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, &nd);
276 qemu_coroutine_enter(root);
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, &i);
307 g_test_timer_start();
308 while (i > 0) {
309 qemu_coroutine_enter(coroutine);
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, &i);
356 qemu_coroutine_enter(co);
357 qemu_coroutine_enter(co);
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);
373 /* This test assumes there is a freelist and marks freed coroutine memory
374 * with a sentinel value. If there is no freelist this would legitimately
375 * crash, so skip it.
377 if (CONFIG_COROUTINE_POOL) {
378 g_test_add_func("/basic/co_queue", test_co_queue);
381 g_test_add_func("/basic/lifecycle", test_lifecycle);
382 g_test_add_func("/basic/yield", test_yield);
383 g_test_add_func("/basic/nesting", test_nesting);
384 g_test_add_func("/basic/self", test_self);
385 g_test_add_func("/basic/in_coroutine", test_in_coroutine);
386 g_test_add_func("/basic/order", test_order);
387 if (g_test_perf()) {
388 g_test_add_func("/perf/lifecycle", perf_lifecycle);
389 g_test_add_func("/perf/nesting", perf_nesting);
390 g_test_add_func("/perf/yield", perf_yield);
391 g_test_add_func("/perf/function-call", perf_baseline);
392 g_test_add_func("/perf/cost", perf_cost);
394 return g_test_run();