]> git.proxmox.com Git - mirror_qemu.git/blob - tests/test-coroutine.c
test-coroutine: Fix coroutine pool corruption
[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 coroutines may nest multiple levels
57 */
58
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;
64
65 static 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
74 child = qemu_coroutine_create(nest, nd);
75 qemu_coroutine_enter(child);
76 }
77
78 nd->n_return++;
79 }
80
81 static void test_nesting(void)
82 {
83 Coroutine *root;
84 NestData nd = {
85 .n_enter = 0,
86 .n_return = 0,
87 .max = 128,
88 };
89
90 root = qemu_coroutine_create(nest, &nd);
91 qemu_coroutine_enter(root);
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
102 static 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
113 static void test_yield(void)
114 {
115 Coroutine *coroutine;
116 bool done = false;
117 int i = -1; /* one extra time to return from coroutine */
118
119 coroutine = qemu_coroutine_create(yield_5_times, &done);
120 while (!done) {
121 qemu_coroutine_enter(coroutine);
122 i++;
123 }
124 g_assert_cmpint(i, ==, 5); /* coroutine must yield 5 times */
125 }
126
127 static void coroutine_fn c2_fn(void *opaque)
128 {
129 qemu_coroutine_yield();
130 }
131
132 static void coroutine_fn c1_fn(void *opaque)
133 {
134 Coroutine *c2 = opaque;
135 qemu_coroutine_enter(c2);
136 }
137
138 static void test_co_queue(void)
139 {
140 Coroutine *c1;
141 Coroutine *c2;
142 Coroutine tmp;
143
144 c2 = qemu_coroutine_create(c2_fn, NULL);
145 c1 = qemu_coroutine_create(c1_fn, c2);
146
147 qemu_coroutine_enter(c1);
148
149 /* c1 shouldn't be used any more now; make sure we segfault if it is */
150 tmp = *c1;
151 memset(c1, 0xff, sizeof(Coroutine));
152 qemu_coroutine_enter(c2);
153
154 /* Must restore the coroutine now to avoid corrupted pool */
155 *c1 = tmp;
156 }
157
158 /*
159 * Check that creation, enter, and return work
160 */
161
162 static void coroutine_fn set_and_exit(void *opaque)
163 {
164 bool *done = opaque;
165
166 *done = true;
167 }
168
169 static void test_lifecycle(void)
170 {
171 Coroutine *coroutine;
172 bool done = false;
173
174 /* Create, enter, and return from coroutine */
175 coroutine = qemu_coroutine_create(set_and_exit, &done);
176 qemu_coroutine_enter(coroutine);
177 g_assert(done); /* expect done to be true (first time) */
178
179 /* Repeat to check that no state affects this test */
180 done = false;
181 coroutine = qemu_coroutine_create(set_and_exit, &done);
182 qemu_coroutine_enter(coroutine);
183 g_assert(done); /* expect done to be true (second time) */
184 }
185
186
187 #define RECORD_SIZE 10 /* Leave some room for expansion */
188 struct coroutine_position {
189 int func;
190 int state;
191 };
192 static struct coroutine_position records[RECORD_SIZE];
193 static unsigned record_pos;
194
195 static void record_push(int func, int state)
196 {
197 struct coroutine_position *cp = &records[record_pos++];
198 g_assert_cmpint(record_pos, <, RECORD_SIZE);
199 cp->func = func;
200 cp->state = state;
201 }
202
203 static void coroutine_fn co_order_test(void *opaque)
204 {
205 record_push(2, 1);
206 g_assert(qemu_in_coroutine());
207 qemu_coroutine_yield();
208 record_push(2, 2);
209 g_assert(qemu_in_coroutine());
210 }
211
212 static void do_order_test(void)
213 {
214 Coroutine *co;
215
216 co = qemu_coroutine_create(co_order_test, NULL);
217 record_push(1, 1);
218 qemu_coroutine_enter(co);
219 record_push(1, 2);
220 g_assert(!qemu_in_coroutine());
221 qemu_coroutine_enter(co);
222 record_push(1, 3);
223 g_assert(!qemu_in_coroutine());
224 }
225
226 static void test_order(void)
227 {
228 int i;
229 const struct coroutine_position expected_pos[] = {
230 {1, 1,}, {2, 1}, {1, 2}, {2, 2}, {1, 3}
231 };
232 do_order_test();
233 g_assert_cmpint(record_pos, ==, 5);
234 for (i = 0; i < record_pos; i++) {
235 g_assert_cmpint(records[i].func , ==, expected_pos[i].func );
236 g_assert_cmpint(records[i].state, ==, expected_pos[i].state);
237 }
238 }
239 /*
240 * Lifecycle benchmark
241 */
242
243 static void coroutine_fn empty_coroutine(void *opaque)
244 {
245 /* Do nothing */
246 }
247
248 static void perf_lifecycle(void)
249 {
250 Coroutine *coroutine;
251 unsigned int i, max;
252 double duration;
253
254 max = 1000000;
255
256 g_test_timer_start();
257 for (i = 0; i < max; i++) {
258 coroutine = qemu_coroutine_create(empty_coroutine, NULL);
259 qemu_coroutine_enter(coroutine);
260 }
261 duration = g_test_timer_elapsed();
262
263 g_test_message("Lifecycle %u iterations: %f s\n", max, duration);
264 }
265
266 static void perf_nesting(void)
267 {
268 unsigned int i, maxcycles, maxnesting;
269 double duration;
270
271 maxcycles = 10000;
272 maxnesting = 1000;
273 Coroutine *root;
274
275 g_test_timer_start();
276 for (i = 0; i < maxcycles; i++) {
277 NestData nd = {
278 .n_enter = 0,
279 .n_return = 0,
280 .max = maxnesting,
281 };
282 root = qemu_coroutine_create(nest, &nd);
283 qemu_coroutine_enter(root);
284 }
285 duration = g_test_timer_elapsed();
286
287 g_test_message("Nesting %u iterations of %u depth each: %f s\n",
288 maxcycles, maxnesting, duration);
289 }
290
291 /*
292 * Yield benchmark
293 */
294
295 static void coroutine_fn yield_loop(void *opaque)
296 {
297 unsigned int *counter = opaque;
298
299 while ((*counter) > 0) {
300 (*counter)--;
301 qemu_coroutine_yield();
302 }
303 }
304
305 static void perf_yield(void)
306 {
307 unsigned int i, maxcycles;
308 double duration;
309
310 maxcycles = 100000000;
311 i = maxcycles;
312 Coroutine *coroutine = qemu_coroutine_create(yield_loop, &i);
313
314 g_test_timer_start();
315 while (i > 0) {
316 qemu_coroutine_enter(coroutine);
317 }
318 duration = g_test_timer_elapsed();
319
320 g_test_message("Yield %u iterations: %f s\n",
321 maxcycles, duration);
322 }
323
324 static __attribute__((noinline)) void dummy(unsigned *i)
325 {
326 (*i)--;
327 }
328
329 static void perf_baseline(void)
330 {
331 unsigned int i, maxcycles;
332 double duration;
333
334 maxcycles = 100000000;
335 i = maxcycles;
336
337 g_test_timer_start();
338 while (i > 0) {
339 dummy(&i);
340 }
341 duration = g_test_timer_elapsed();
342
343 g_test_message("Function call %u iterations: %f s\n",
344 maxcycles, duration);
345 }
346
347 static __attribute__((noinline)) void perf_cost_func(void *opaque)
348 {
349 qemu_coroutine_yield();
350 }
351
352 static void perf_cost(void)
353 {
354 const unsigned long maxcycles = 40000000;
355 unsigned long i = 0;
356 double duration;
357 unsigned long ops;
358 Coroutine *co;
359
360 g_test_timer_start();
361 while (i++ < maxcycles) {
362 co = qemu_coroutine_create(perf_cost_func, &i);
363 qemu_coroutine_enter(co);
364 qemu_coroutine_enter(co);
365 }
366 duration = g_test_timer_elapsed();
367 ops = (long)(maxcycles / (duration * 1000));
368
369 g_test_message("Run operation %lu iterations %f s, %luK operations/s, "
370 "%luns per coroutine",
371 maxcycles,
372 duration, ops,
373 (unsigned long)(1000000000.0 * duration / maxcycles));
374 }
375
376 int main(int argc, char **argv)
377 {
378 g_test_init(&argc, &argv, NULL);
379
380 /* This test assumes there is a freelist and marks freed coroutine memory
381 * with a sentinel value. If there is no freelist this would legitimately
382 * crash, so skip it.
383 */
384 if (CONFIG_COROUTINE_POOL) {
385 g_test_add_func("/basic/co_queue", test_co_queue);
386 }
387
388 g_test_add_func("/basic/lifecycle", test_lifecycle);
389 g_test_add_func("/basic/yield", test_yield);
390 g_test_add_func("/basic/nesting", test_nesting);
391 g_test_add_func("/basic/self", test_self);
392 g_test_add_func("/basic/in_coroutine", test_in_coroutine);
393 g_test_add_func("/basic/order", test_order);
394 if (g_test_perf()) {
395 g_test_add_func("/perf/lifecycle", perf_lifecycle);
396 g_test_add_func("/perf/nesting", perf_nesting);
397 g_test_add_func("/perf/yield", perf_yield);
398 g_test_add_func("/perf/function-call", perf_baseline);
399 g_test_add_func("/perf/cost", perf_cost);
400 }
401 return g_test_run();
402 }