]> git.proxmox.com Git - mirror_qemu.git/blob - tests/test-coroutine.c
Merge remote-tracking branch 'remotes/xtensa/tags/20180122-xtensa' into staging
[mirror_qemu.git] / tests / test-coroutine.c
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
14 #include "qemu/osdep.h"
15 #include "qemu/coroutine.h"
16 #include "qemu/coroutine_int.h"
17
18 /*
19 * Check that qemu_in_coroutine() works
20 */
21
22 static void coroutine_fn verify_in_coroutine(void *opaque)
23 {
24 g_assert(qemu_in_coroutine());
25 }
26
27 static void test_in_coroutine(void)
28 {
29 Coroutine *coroutine;
30
31 g_assert(!qemu_in_coroutine());
32
33 coroutine = qemu_coroutine_create(verify_in_coroutine, NULL);
34 qemu_coroutine_enter(coroutine);
35 }
36
37 /*
38 * Check that qemu_coroutine_self() works
39 */
40
41 static void coroutine_fn verify_self(void *opaque)
42 {
43 Coroutine **p_co = opaque;
44 g_assert(qemu_coroutine_self() == *p_co);
45 }
46
47 static void test_self(void)
48 {
49 Coroutine *coroutine;
50
51 coroutine = qemu_coroutine_create(verify_self, &coroutine);
52 qemu_coroutine_enter(coroutine);
53 }
54
55 /*
56 * Check that qemu_coroutine_entered() works
57 */
58
59 static void coroutine_fn verify_entered_step_2(void *opaque)
60 {
61 Coroutine *caller = (Coroutine *)opaque;
62
63 g_assert(qemu_coroutine_entered(caller));
64 g_assert(qemu_coroutine_entered(qemu_coroutine_self()));
65 qemu_coroutine_yield();
66
67 /* Once more to check it still works after yielding */
68 g_assert(qemu_coroutine_entered(caller));
69 g_assert(qemu_coroutine_entered(qemu_coroutine_self()));
70 }
71
72 static void coroutine_fn verify_entered_step_1(void *opaque)
73 {
74 Coroutine *self = qemu_coroutine_self();
75 Coroutine *coroutine;
76
77 g_assert(qemu_coroutine_entered(self));
78
79 coroutine = qemu_coroutine_create(verify_entered_step_2, self);
80 g_assert(!qemu_coroutine_entered(coroutine));
81 qemu_coroutine_enter(coroutine);
82 g_assert(!qemu_coroutine_entered(coroutine));
83 qemu_coroutine_enter(coroutine);
84 }
85
86 static void test_entered(void)
87 {
88 Coroutine *coroutine;
89
90 coroutine = qemu_coroutine_create(verify_entered_step_1, NULL);
91 g_assert(!qemu_coroutine_entered(coroutine));
92 qemu_coroutine_enter(coroutine);
93 }
94
95 /*
96 * Check that coroutines may nest multiple levels
97 */
98
99 typedef struct {
100 unsigned int n_enter; /* num coroutines entered */
101 unsigned int n_return; /* num coroutines returned */
102 unsigned int max; /* maximum level of nesting */
103 } NestData;
104
105 static void coroutine_fn nest(void *opaque)
106 {
107 NestData *nd = opaque;
108
109 nd->n_enter++;
110
111 if (nd->n_enter < nd->max) {
112 Coroutine *child;
113
114 child = qemu_coroutine_create(nest, nd);
115 qemu_coroutine_enter(child);
116 }
117
118 nd->n_return++;
119 }
120
121 static void test_nesting(void)
122 {
123 Coroutine *root;
124 NestData nd = {
125 .n_enter = 0,
126 .n_return = 0,
127 .max = 128,
128 };
129
130 root = qemu_coroutine_create(nest, &nd);
131 qemu_coroutine_enter(root);
132
133 /* Must enter and return from max nesting level */
134 g_assert_cmpint(nd.n_enter, ==, nd.max);
135 g_assert_cmpint(nd.n_return, ==, nd.max);
136 }
137
138 /*
139 * Check that yield/enter transfer control correctly
140 */
141
142 static void coroutine_fn yield_5_times(void *opaque)
143 {
144 bool *done = opaque;
145 int i;
146
147 for (i = 0; i < 5; i++) {
148 qemu_coroutine_yield();
149 }
150 *done = true;
151 }
152
153 static void test_yield(void)
154 {
155 Coroutine *coroutine;
156 bool done = false;
157 int i = -1; /* one extra time to return from coroutine */
158
159 coroutine = qemu_coroutine_create(yield_5_times, &done);
160 while (!done) {
161 qemu_coroutine_enter(coroutine);
162 i++;
163 }
164 g_assert_cmpint(i, ==, 5); /* coroutine must yield 5 times */
165 }
166
167 static void coroutine_fn c2_fn(void *opaque)
168 {
169 qemu_coroutine_yield();
170 }
171
172 static void coroutine_fn c1_fn(void *opaque)
173 {
174 Coroutine *c2 = opaque;
175 qemu_coroutine_enter(c2);
176 }
177
178 static void test_co_queue(void)
179 {
180 Coroutine *c1;
181 Coroutine *c2;
182 Coroutine tmp;
183
184 c2 = qemu_coroutine_create(c2_fn, NULL);
185 c1 = qemu_coroutine_create(c1_fn, c2);
186
187 qemu_coroutine_enter(c1);
188
189 /* c1 shouldn't be used any more now; make sure we segfault if it is */
190 tmp = *c1;
191 memset(c1, 0xff, sizeof(Coroutine));
192 qemu_coroutine_enter(c2);
193
194 /* Must restore the coroutine now to avoid corrupted pool */
195 *c1 = tmp;
196 }
197
198 /*
199 * Check that creation, enter, and return work
200 */
201
202 static void coroutine_fn set_and_exit(void *opaque)
203 {
204 bool *done = opaque;
205
206 *done = true;
207 }
208
209 static void test_lifecycle(void)
210 {
211 Coroutine *coroutine;
212 bool done = false;
213
214 /* Create, enter, and return from coroutine */
215 coroutine = qemu_coroutine_create(set_and_exit, &done);
216 qemu_coroutine_enter(coroutine);
217 g_assert(done); /* expect done to be true (first time) */
218
219 /* Repeat to check that no state affects this test */
220 done = false;
221 coroutine = qemu_coroutine_create(set_and_exit, &done);
222 qemu_coroutine_enter(coroutine);
223 g_assert(done); /* expect done to be true (second time) */
224 }
225
226
227 #define RECORD_SIZE 10 /* Leave some room for expansion */
228 struct coroutine_position {
229 int func;
230 int state;
231 };
232 static struct coroutine_position records[RECORD_SIZE];
233 static unsigned record_pos;
234
235 static void record_push(int func, int state)
236 {
237 struct coroutine_position *cp = &records[record_pos++];
238 g_assert_cmpint(record_pos, <, RECORD_SIZE);
239 cp->func = func;
240 cp->state = state;
241 }
242
243 static void coroutine_fn co_order_test(void *opaque)
244 {
245 record_push(2, 1);
246 g_assert(qemu_in_coroutine());
247 qemu_coroutine_yield();
248 record_push(2, 2);
249 g_assert(qemu_in_coroutine());
250 }
251
252 static void do_order_test(void)
253 {
254 Coroutine *co;
255
256 co = qemu_coroutine_create(co_order_test, NULL);
257 record_push(1, 1);
258 qemu_coroutine_enter(co);
259 record_push(1, 2);
260 g_assert(!qemu_in_coroutine());
261 qemu_coroutine_enter(co);
262 record_push(1, 3);
263 g_assert(!qemu_in_coroutine());
264 }
265
266 static void test_order(void)
267 {
268 int i;
269 const struct coroutine_position expected_pos[] = {
270 {1, 1,}, {2, 1}, {1, 2}, {2, 2}, {1, 3}
271 };
272 do_order_test();
273 g_assert_cmpint(record_pos, ==, 5);
274 for (i = 0; i < record_pos; i++) {
275 g_assert_cmpint(records[i].func , ==, expected_pos[i].func );
276 g_assert_cmpint(records[i].state, ==, expected_pos[i].state);
277 }
278 }
279 /*
280 * Lifecycle benchmark
281 */
282
283 static void coroutine_fn empty_coroutine(void *opaque)
284 {
285 /* Do nothing */
286 }
287
288 static void perf_lifecycle(void)
289 {
290 Coroutine *coroutine;
291 unsigned int i, max;
292 double duration;
293
294 max = 1000000;
295
296 g_test_timer_start();
297 for (i = 0; i < max; i++) {
298 coroutine = qemu_coroutine_create(empty_coroutine, NULL);
299 qemu_coroutine_enter(coroutine);
300 }
301 duration = g_test_timer_elapsed();
302
303 g_test_message("Lifecycle %u iterations: %f s\n", max, duration);
304 }
305
306 static void perf_nesting(void)
307 {
308 unsigned int i, maxcycles, maxnesting;
309 double duration;
310
311 maxcycles = 10000;
312 maxnesting = 1000;
313 Coroutine *root;
314
315 g_test_timer_start();
316 for (i = 0; i < maxcycles; i++) {
317 NestData nd = {
318 .n_enter = 0,
319 .n_return = 0,
320 .max = maxnesting,
321 };
322 root = qemu_coroutine_create(nest, &nd);
323 qemu_coroutine_enter(root);
324 }
325 duration = g_test_timer_elapsed();
326
327 g_test_message("Nesting %u iterations of %u depth each: %f s\n",
328 maxcycles, maxnesting, duration);
329 }
330
331 /*
332 * Yield benchmark
333 */
334
335 static void coroutine_fn yield_loop(void *opaque)
336 {
337 unsigned int *counter = opaque;
338
339 while ((*counter) > 0) {
340 (*counter)--;
341 qemu_coroutine_yield();
342 }
343 }
344
345 static void perf_yield(void)
346 {
347 unsigned int i, maxcycles;
348 double duration;
349
350 maxcycles = 100000000;
351 i = maxcycles;
352 Coroutine *coroutine = qemu_coroutine_create(yield_loop, &i);
353
354 g_test_timer_start();
355 while (i > 0) {
356 qemu_coroutine_enter(coroutine);
357 }
358 duration = g_test_timer_elapsed();
359
360 g_test_message("Yield %u iterations: %f s\n",
361 maxcycles, duration);
362 }
363
364 static __attribute__((noinline)) void dummy(unsigned *i)
365 {
366 (*i)--;
367 }
368
369 static void perf_baseline(void)
370 {
371 unsigned int i, maxcycles;
372 double duration;
373
374 maxcycles = 100000000;
375 i = maxcycles;
376
377 g_test_timer_start();
378 while (i > 0) {
379 dummy(&i);
380 }
381 duration = g_test_timer_elapsed();
382
383 g_test_message("Function call %u iterations: %f s\n",
384 maxcycles, duration);
385 }
386
387 static __attribute__((noinline)) void perf_cost_func(void *opaque)
388 {
389 qemu_coroutine_yield();
390 }
391
392 static void perf_cost(void)
393 {
394 const unsigned long maxcycles = 40000000;
395 unsigned long i = 0;
396 double duration;
397 unsigned long ops;
398 Coroutine *co;
399
400 g_test_timer_start();
401 while (i++ < maxcycles) {
402 co = qemu_coroutine_create(perf_cost_func, &i);
403 qemu_coroutine_enter(co);
404 qemu_coroutine_enter(co);
405 }
406 duration = g_test_timer_elapsed();
407 ops = (long)(maxcycles / (duration * 1000));
408
409 g_test_message("Run operation %lu iterations %f s, %luK operations/s, "
410 "%luns per coroutine",
411 maxcycles,
412 duration, ops,
413 (unsigned long)(1000000000.0 * duration / maxcycles));
414 }
415
416 int main(int argc, char **argv)
417 {
418 g_test_init(&argc, &argv, NULL);
419
420 /* This test assumes there is a freelist and marks freed coroutine memory
421 * with a sentinel value. If there is no freelist this would legitimately
422 * crash, so skip it.
423 */
424 if (CONFIG_COROUTINE_POOL) {
425 g_test_add_func("/basic/co_queue", test_co_queue);
426 }
427
428 g_test_add_func("/basic/lifecycle", test_lifecycle);
429 g_test_add_func("/basic/yield", test_yield);
430 g_test_add_func("/basic/nesting", test_nesting);
431 g_test_add_func("/basic/self", test_self);
432 g_test_add_func("/basic/entered", test_entered);
433 g_test_add_func("/basic/in_coroutine", test_in_coroutine);
434 g_test_add_func("/basic/order", test_order);
435 if (g_test_perf()) {
436 g_test_add_func("/perf/lifecycle", perf_lifecycle);
437 g_test_add_func("/perf/nesting", perf_nesting);
438 g_test_add_func("/perf/yield", perf_yield);
439 g_test_add_func("/perf/function-call", perf_baseline);
440 g_test_add_func("/perf/cost", perf_cost);
441 }
442 return g_test_run();
443 }