]>
Commit | Line | Data |
---|---|---|
aa7ee42e SH |
1 | /* |
2 | * Coroutine tests | |
3 | * | |
4 | * Copyright IBM, Corp. 2011 | |
5 | * | |
6 | * Authors: | |
7 | * Stefan Hajnoczi <[email protected]> | |
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> | |
737e150e | 15 | #include "block/coroutine.h" |
aa7ee42e SH |
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 | ||
f8d1daea CS |
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 | } | |
5e3840ce SH |
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 | ||
7e849a99 AB |
233 | static void perf_nesting(void) |
234 | { | |
235 | unsigned int i, maxcycles, maxnesting; | |
236 | double duration; | |
237 | ||
a9031675 | 238 | maxcycles = 10000; |
02700315 | 239 | maxnesting = 1000; |
7e849a99 | 240 | Coroutine *root; |
7e849a99 AB |
241 | |
242 | g_test_timer_start(); | |
243 | for (i = 0; i < maxcycles; i++) { | |
a9031675 GK |
244 | NestData nd = { |
245 | .n_enter = 0, | |
246 | .n_return = 0, | |
247 | .max = maxnesting, | |
248 | }; | |
7e849a99 AB |
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 | ||
2fcd15ea GK |
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 | } | |
7e849a99 | 290 | |
aa7ee42e SH |
291 | int main(int argc, char **argv) |
292 | { | |
293 | g_test_init(&argc, &argv, NULL); | |
294 | g_test_add_func("/basic/lifecycle", test_lifecycle); | |
295 | g_test_add_func("/basic/yield", test_yield); | |
296 | g_test_add_func("/basic/nesting", test_nesting); | |
297 | g_test_add_func("/basic/self", test_self); | |
298 | g_test_add_func("/basic/in_coroutine", test_in_coroutine); | |
f8d1daea | 299 | g_test_add_func("/basic/order", test_order); |
5e3840ce SH |
300 | if (g_test_perf()) { |
301 | g_test_add_func("/perf/lifecycle", perf_lifecycle); | |
7e849a99 | 302 | g_test_add_func("/perf/nesting", perf_nesting); |
2fcd15ea | 303 | g_test_add_func("/perf/yield", perf_yield); |
5e3840ce | 304 | } |
aa7ee42e SH |
305 | return g_test_run(); |
306 | } |