]> git.proxmox.com Git - mirror_qemu.git/blame - tests/test-coroutine.c
qemu-iotests: add vmdk for test backup compression in 055
[mirror_qemu.git] / tests / test-coroutine.c
CommitLineData
aa7ee42e
SH
1/*
2 * Coroutine tests
3 *
4 * Copyright IBM, Corp. 2011
5 *
6 * Authors:
7 * Stefan Hajnoczi <stefanha@linux.vnet.ibm.com>
8 *
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.
11 *
12 */
13
681c28a3 14#include "qemu/osdep.h"
10817bf0
DB
15#include "qemu/coroutine.h"
16#include "qemu/coroutine_int.h"
aa7ee42e
SH
17
18/*
19 * Check that qemu_in_coroutine() works
20 */
21
22static void coroutine_fn verify_in_coroutine(void *opaque)
23{
24 g_assert(qemu_in_coroutine());
25}
26
27static void test_in_coroutine(void)
28{
29 Coroutine *coroutine;
30
31 g_assert(!qemu_in_coroutine());
32
0b8b8753
PB
33 coroutine = qemu_coroutine_create(verify_in_coroutine, NULL);
34 qemu_coroutine_enter(coroutine);
aa7ee42e
SH
35}
36
37/*
38 * Check that qemu_coroutine_self() works
39 */
40
41static void coroutine_fn verify_self(void *opaque)
42{
7e70cdba
PB
43 Coroutine **p_co = opaque;
44 g_assert(qemu_coroutine_self() == *p_co);
aa7ee42e
SH
45}
46
47static void test_self(void)
48{
49 Coroutine *coroutine;
50
0b8b8753
PB
51 coroutine = qemu_coroutine_create(verify_self, &coroutine);
52 qemu_coroutine_enter(coroutine);
aa7ee42e
SH
53}
54
55/*
56 * Check that coroutines may nest multiple levels
57 */
58
59typedef 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;
64
65static void coroutine_fn nest(void *opaque)
66{
67 NestData *nd = opaque;
68
69 nd->n_enter++;
70
71 if (nd->n_enter < nd->max) {
72 Coroutine *child;
73
0b8b8753
PB
74 child = qemu_coroutine_create(nest, nd);
75 qemu_coroutine_enter(child);
aa7ee42e
SH
76 }
77
78 nd->n_return++;
79}
80
81static void test_nesting(void)
82{
83 Coroutine *root;
84 NestData nd = {
85 .n_enter = 0,
86 .n_return = 0,
87 .max = 128,
88 };
89
0b8b8753
PB
90 root = qemu_coroutine_create(nest, &nd);
91 qemu_coroutine_enter(root);
aa7ee42e
SH
92
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);
96}
97
98/*
99 * Check that yield/enter transfer control correctly
100 */
101
102static void coroutine_fn yield_5_times(void *opaque)
103{
104 bool *done = opaque;
105 int i;
106
107 for (i = 0; i < 5; i++) {
108 qemu_coroutine_yield();
109 }
110 *done = true;
111}
112
113static void test_yield(void)
114{
115 Coroutine *coroutine;
116 bool done = false;
117 int i = -1; /* one extra time to return from coroutine */
118
0b8b8753 119 coroutine = qemu_coroutine_create(yield_5_times, &done);
aa7ee42e 120 while (!done) {
0b8b8753 121 qemu_coroutine_enter(coroutine);
aa7ee42e
SH
122 i++;
123 }
124 g_assert_cmpint(i, ==, 5); /* coroutine must yield 5 times */
125}
126
7c2eed3e
SH
127static void coroutine_fn c2_fn(void *opaque)
128{
129 qemu_coroutine_yield();
130}
131
132static void coroutine_fn c1_fn(void *opaque)
133{
134 Coroutine *c2 = opaque;
0b8b8753 135 qemu_coroutine_enter(c2);
7c2eed3e
SH
136}
137
138static void test_co_queue(void)
139{
140 Coroutine *c1;
141 Coroutine *c2;
142
0b8b8753
PB
143 c2 = qemu_coroutine_create(c2_fn, NULL);
144 c1 = qemu_coroutine_create(c1_fn, c2);
7c2eed3e 145
0b8b8753 146 qemu_coroutine_enter(c1);
7c2eed3e 147 memset(c1, 0xff, sizeof(Coroutine));
0b8b8753 148 qemu_coroutine_enter(c2);
7c2eed3e
SH
149}
150
aa7ee42e
SH
151/*
152 * Check that creation, enter, and return work
153 */
154
155static void coroutine_fn set_and_exit(void *opaque)
156{
157 bool *done = opaque;
158
159 *done = true;
160}
161
162static void test_lifecycle(void)
163{
164 Coroutine *coroutine;
165 bool done = false;
166
167 /* Create, enter, and return from coroutine */
0b8b8753
PB
168 coroutine = qemu_coroutine_create(set_and_exit, &done);
169 qemu_coroutine_enter(coroutine);
aa7ee42e
SH
170 g_assert(done); /* expect done to be true (first time) */
171
172 /* Repeat to check that no state affects this test */
173 done = false;
0b8b8753
PB
174 coroutine = qemu_coroutine_create(set_and_exit, &done);
175 qemu_coroutine_enter(coroutine);
aa7ee42e
SH
176 g_assert(done); /* expect done to be true (second time) */
177}
178
f8d1daea
CS
179
180#define RECORD_SIZE 10 /* Leave some room for expansion */
181struct coroutine_position {
182 int func;
183 int state;
184};
185static struct coroutine_position records[RECORD_SIZE];
186static unsigned record_pos;
187
188static void record_push(int func, int state)
189{
190 struct coroutine_position *cp = &records[record_pos++];
191 g_assert_cmpint(record_pos, <, RECORD_SIZE);
192 cp->func = func;
193 cp->state = state;
194}
195
196static void coroutine_fn co_order_test(void *opaque)
197{
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());
203}
204
205static void do_order_test(void)
206{
207 Coroutine *co;
208
0b8b8753 209 co = qemu_coroutine_create(co_order_test, NULL);
f8d1daea 210 record_push(1, 1);
0b8b8753 211 qemu_coroutine_enter(co);
f8d1daea
CS
212 record_push(1, 2);
213 g_assert(!qemu_in_coroutine());
0b8b8753 214 qemu_coroutine_enter(co);
f8d1daea
CS
215 record_push(1, 3);
216 g_assert(!qemu_in_coroutine());
217}
218
219static void test_order(void)
220{
221 int i;
222 const struct coroutine_position expected_pos[] = {
223 {1, 1,}, {2, 1}, {1, 2}, {2, 2}, {1, 3}
224 };
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);
230 }
231}
5e3840ce
SH
232/*
233 * Lifecycle benchmark
234 */
235
236static void coroutine_fn empty_coroutine(void *opaque)
237{
238 /* Do nothing */
239}
240
241static void perf_lifecycle(void)
242{
243 Coroutine *coroutine;
244 unsigned int i, max;
245 double duration;
246
247 max = 1000000;
248
249 g_test_timer_start();
250 for (i = 0; i < max; i++) {
0b8b8753
PB
251 coroutine = qemu_coroutine_create(empty_coroutine, NULL);
252 qemu_coroutine_enter(coroutine);
5e3840ce
SH
253 }
254 duration = g_test_timer_elapsed();
255
256 g_test_message("Lifecycle %u iterations: %f s\n", max, duration);
257}
258
7e849a99
AB
259static void perf_nesting(void)
260{
261 unsigned int i, maxcycles, maxnesting;
262 double duration;
263
a9031675 264 maxcycles = 10000;
02700315 265 maxnesting = 1000;
7e849a99 266 Coroutine *root;
7e849a99
AB
267
268 g_test_timer_start();
269 for (i = 0; i < maxcycles; i++) {
a9031675
GK
270 NestData nd = {
271 .n_enter = 0,
272 .n_return = 0,
273 .max = maxnesting,
274 };
0b8b8753
PB
275 root = qemu_coroutine_create(nest, &nd);
276 qemu_coroutine_enter(root);
7e849a99
AB
277 }
278 duration = g_test_timer_elapsed();
279
280 g_test_message("Nesting %u iterations of %u depth each: %f s\n",
281 maxcycles, maxnesting, duration);
282}
283
2fcd15ea
GK
284/*
285 * Yield benchmark
286 */
287
288static void coroutine_fn yield_loop(void *opaque)
289{
290 unsigned int *counter = opaque;
291
292 while ((*counter) > 0) {
293 (*counter)--;
294 qemu_coroutine_yield();
295 }
296}
297
298static void perf_yield(void)
299{
300 unsigned int i, maxcycles;
301 double duration;
302
303 maxcycles = 100000000;
304 i = maxcycles;
0b8b8753 305 Coroutine *coroutine = qemu_coroutine_create(yield_loop, &i);
2fcd15ea
GK
306
307 g_test_timer_start();
308 while (i > 0) {
0b8b8753 309 qemu_coroutine_enter(coroutine);
2fcd15ea
GK
310 }
311 duration = g_test_timer_elapsed();
312
313 g_test_message("Yield %u iterations: %f s\n",
314 maxcycles, duration);
315}
7e849a99 316
58803ce7
PB
317static __attribute__((noinline)) void dummy(unsigned *i)
318{
319 (*i)--;
320}
321
322static void perf_baseline(void)
323{
324 unsigned int i, maxcycles;
325 double duration;
326
327 maxcycles = 100000000;
328 i = maxcycles;
329
330 g_test_timer_start();
331 while (i > 0) {
332 dummy(&i);
333 }
334 duration = g_test_timer_elapsed();
335
336 g_test_message("Function call %u iterations: %f s\n",
337 maxcycles, duration);
338}
339
61ff8cfb
ML
340static __attribute__((noinline)) void perf_cost_func(void *opaque)
341{
342 qemu_coroutine_yield();
343}
344
345static void perf_cost(void)
346{
347 const unsigned long maxcycles = 40000000;
348 unsigned long i = 0;
349 double duration;
350 unsigned long ops;
351 Coroutine *co;
352
353 g_test_timer_start();
354 while (i++ < maxcycles) {
0b8b8753
PB
355 co = qemu_coroutine_create(perf_cost_func, &i);
356 qemu_coroutine_enter(co);
357 qemu_coroutine_enter(co);
61ff8cfb
ML
358 }
359 duration = g_test_timer_elapsed();
360 ops = (long)(maxcycles / (duration * 1000));
361
362 g_test_message("Run operation %lu iterations %f s, %luK operations/s, "
363 "%luns per coroutine",
364 maxcycles,
365 duration, ops,
6d86ae08 366 (unsigned long)(1000000000.0 * duration / maxcycles));
61ff8cfb
ML
367}
368
aa7ee42e
SH
369int main(int argc, char **argv)
370{
371 g_test_init(&argc, &argv, NULL);
271b385e
SH
372
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.
376 */
377 if (CONFIG_COROUTINE_POOL) {
378 g_test_add_func("/basic/co_queue", test_co_queue);
379 }
380
aa7ee42e
SH
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);
f8d1daea 386 g_test_add_func("/basic/order", test_order);
5e3840ce
SH
387 if (g_test_perf()) {
388 g_test_add_func("/perf/lifecycle", perf_lifecycle);
7e849a99 389 g_test_add_func("/perf/nesting", perf_nesting);
2fcd15ea 390 g_test_add_func("/perf/yield", perf_yield);
58803ce7 391 g_test_add_func("/perf/function-call", perf_baseline);
61ff8cfb 392 g_test_add_func("/perf/cost", perf_cost);
5e3840ce 393 }
aa7ee42e
SH
394 return g_test_run();
395}