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