hw/ppc/spapr: Add some missing hcall function set strings
[qemu.git] / tests / test-coroutine.c
blob215b92e636e31fb8968de4ddb9e28ad03bf93fc8
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);
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)
48 Coroutine *coroutine;
50 coroutine = qemu_coroutine_create(verify_self);
51 qemu_coroutine_enter(coroutine, coroutine);
55 * Check that coroutines may nest multiple levels
58 typedef struct {
59 unsigned int n_enter; /* num coroutines entered */
60 unsigned int n_return; /* num coroutines returned */
61 unsigned int max; /* maximum level of nesting */
62 } NestData;
64 static void coroutine_fn nest(void *opaque)
66 NestData *nd = opaque;
68 nd->n_enter++;
70 if (nd->n_enter < nd->max) {
71 Coroutine *child;
73 child = qemu_coroutine_create(nest);
74 qemu_coroutine_enter(child, nd);
77 nd->n_return++;
80 static void test_nesting(void)
82 Coroutine *root;
83 NestData nd = {
84 .n_enter = 0,
85 .n_return = 0,
86 .max = 128,
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)
103 bool *done = opaque;
104 int i;
106 for (i = 0; i < 5; i++) {
107 qemu_coroutine_yield();
109 *done = true;
112 static void test_yield(void)
114 Coroutine *coroutine;
115 bool done = false;
116 int i = -1; /* one extra time to return from coroutine */
118 coroutine = qemu_coroutine_create(yield_5_times);
119 while (!done) {
120 qemu_coroutine_enter(coroutine, &done);
121 i++;
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)
139 Coroutine *c1;
140 Coroutine *c2;
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)
156 bool *done = opaque;
158 *done = true;
161 static void test_lifecycle(void)
163 Coroutine *coroutine;
164 bool done = false;
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 */
172 done = false;
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 {
181 int func;
182 int state;
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);
191 cp->func = func;
192 cp->state = state;
195 static void coroutine_fn co_order_test(void *opaque)
197 record_push(2, 1);
198 g_assert(qemu_in_coroutine());
199 qemu_coroutine_yield();
200 record_push(2, 2);
201 g_assert(qemu_in_coroutine());
204 static void do_order_test(void)
206 Coroutine *co;
208 co = qemu_coroutine_create(co_order_test);
209 record_push(1, 1);
210 qemu_coroutine_enter(co, NULL);
211 record_push(1, 2);
212 g_assert(!qemu_in_coroutine());
213 qemu_coroutine_enter(co, NULL);
214 record_push(1, 3);
215 g_assert(!qemu_in_coroutine());
218 static void test_order(void)
220 int i;
221 const struct coroutine_position expected_pos[] = {
222 {1, 1,}, {2, 1}, {1, 2}, {2, 2}, {1, 3}
224 do_order_test();
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)
237 /* Do nothing */
240 static void perf_lifecycle(void)
242 Coroutine *coroutine;
243 unsigned int i, max;
244 double duration;
246 max = 1000000;
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;
261 double duration;
263 maxcycles = 10000;
264 maxnesting = 1000;
265 Coroutine *root;
267 g_test_timer_start();
268 for (i = 0; i < maxcycles; i++) {
269 NestData nd = {
270 .n_enter = 0,
271 .n_return = 0,
272 .max = maxnesting,
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);
284 * Yield benchmark
287 static void coroutine_fn yield_loop(void *opaque)
289 unsigned int *counter = opaque;
291 while ((*counter) > 0) {
292 (*counter)--;
293 qemu_coroutine_yield();
297 static void perf_yield(void)
299 unsigned int i, maxcycles;
300 double duration;
302 maxcycles = 100000000;
303 i = maxcycles;
304 Coroutine *coroutine = qemu_coroutine_create(yield_loop);
306 g_test_timer_start();
307 while (i > 0) {
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)
318 (*i)--;
321 static void perf_baseline(void)
323 unsigned int i, maxcycles;
324 double duration;
326 maxcycles = 100000000;
327 i = maxcycles;
329 g_test_timer_start();
330 while (i > 0) {
331 dummy(&i);
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;
347 unsigned long i = 0;
348 double duration;
349 unsigned long ops;
350 Coroutine *co;
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",
363 maxcycles,
364 duration, ops,
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
374 * crash, so skip it.
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);
386 if (g_test_perf()) {
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);
393 return g_test_run();