Statistics
| Branch: | Revision:

root / tests / test-coroutine.c @ 5d12aa63

History | View | Annotate | Download (6.7 kB)

1 aa7ee42e Stefan Hajnoczi
/*
2 aa7ee42e Stefan Hajnoczi
 * Coroutine tests
3 aa7ee42e Stefan Hajnoczi
 *
4 aa7ee42e Stefan Hajnoczi
 * Copyright IBM, Corp. 2011
5 aa7ee42e Stefan Hajnoczi
 *
6 aa7ee42e Stefan Hajnoczi
 * Authors:
7 aa7ee42e Stefan Hajnoczi
 *  Stefan Hajnoczi    <stefanha@linux.vnet.ibm.com>
8 aa7ee42e Stefan Hajnoczi
 *
9 aa7ee42e Stefan Hajnoczi
 * This work is licensed under the terms of the GNU LGPL, version 2 or later.
10 aa7ee42e Stefan Hajnoczi
 * See the COPYING.LIB file in the top-level directory.
11 aa7ee42e Stefan Hajnoczi
 *
12 aa7ee42e Stefan Hajnoczi
 */
13 aa7ee42e Stefan Hajnoczi
14 aa7ee42e Stefan Hajnoczi
#include <glib.h>
15 737e150e Paolo Bonzini
#include "block/coroutine.h"
16 aa7ee42e Stefan Hajnoczi
17 aa7ee42e Stefan Hajnoczi
/*
18 aa7ee42e Stefan Hajnoczi
 * Check that qemu_in_coroutine() works
19 aa7ee42e Stefan Hajnoczi
 */
20 aa7ee42e Stefan Hajnoczi
21 aa7ee42e Stefan Hajnoczi
static void coroutine_fn verify_in_coroutine(void *opaque)
22 aa7ee42e Stefan Hajnoczi
{
23 aa7ee42e Stefan Hajnoczi
    g_assert(qemu_in_coroutine());
24 aa7ee42e Stefan Hajnoczi
}
25 aa7ee42e Stefan Hajnoczi
26 aa7ee42e Stefan Hajnoczi
static void test_in_coroutine(void)
27 aa7ee42e Stefan Hajnoczi
{
28 aa7ee42e Stefan Hajnoczi
    Coroutine *coroutine;
29 aa7ee42e Stefan Hajnoczi
30 aa7ee42e Stefan Hajnoczi
    g_assert(!qemu_in_coroutine());
31 aa7ee42e Stefan Hajnoczi
32 aa7ee42e Stefan Hajnoczi
    coroutine = qemu_coroutine_create(verify_in_coroutine);
33 aa7ee42e Stefan Hajnoczi
    qemu_coroutine_enter(coroutine, NULL);
34 aa7ee42e Stefan Hajnoczi
}
35 aa7ee42e Stefan Hajnoczi
36 aa7ee42e Stefan Hajnoczi
/*
37 aa7ee42e Stefan Hajnoczi
 * Check that qemu_coroutine_self() works
38 aa7ee42e Stefan Hajnoczi
 */
39 aa7ee42e Stefan Hajnoczi
40 aa7ee42e Stefan Hajnoczi
static void coroutine_fn verify_self(void *opaque)
41 aa7ee42e Stefan Hajnoczi
{
42 aa7ee42e Stefan Hajnoczi
    g_assert(qemu_coroutine_self() == opaque);
43 aa7ee42e Stefan Hajnoczi
}
44 aa7ee42e Stefan Hajnoczi
45 aa7ee42e Stefan Hajnoczi
static void test_self(void)
46 aa7ee42e Stefan Hajnoczi
{
47 aa7ee42e Stefan Hajnoczi
    Coroutine *coroutine;
48 aa7ee42e Stefan Hajnoczi
49 aa7ee42e Stefan Hajnoczi
    coroutine = qemu_coroutine_create(verify_self);
50 aa7ee42e Stefan Hajnoczi
    qemu_coroutine_enter(coroutine, coroutine);
51 aa7ee42e Stefan Hajnoczi
}
52 aa7ee42e Stefan Hajnoczi
53 aa7ee42e Stefan Hajnoczi
/*
54 aa7ee42e Stefan Hajnoczi
 * Check that coroutines may nest multiple levels
55 aa7ee42e Stefan Hajnoczi
 */
56 aa7ee42e Stefan Hajnoczi
57 aa7ee42e Stefan Hajnoczi
typedef struct {
58 aa7ee42e Stefan Hajnoczi
    unsigned int n_enter;   /* num coroutines entered */
59 aa7ee42e Stefan Hajnoczi
    unsigned int n_return;  /* num coroutines returned */
60 aa7ee42e Stefan Hajnoczi
    unsigned int max;       /* maximum level of nesting */
61 aa7ee42e Stefan Hajnoczi
} NestData;
62 aa7ee42e Stefan Hajnoczi
63 aa7ee42e Stefan Hajnoczi
static void coroutine_fn nest(void *opaque)
64 aa7ee42e Stefan Hajnoczi
{
65 aa7ee42e Stefan Hajnoczi
    NestData *nd = opaque;
66 aa7ee42e Stefan Hajnoczi
67 aa7ee42e Stefan Hajnoczi
    nd->n_enter++;
68 aa7ee42e Stefan Hajnoczi
69 aa7ee42e Stefan Hajnoczi
    if (nd->n_enter < nd->max) {
70 aa7ee42e Stefan Hajnoczi
        Coroutine *child;
71 aa7ee42e Stefan Hajnoczi
72 aa7ee42e Stefan Hajnoczi
        child = qemu_coroutine_create(nest);
73 aa7ee42e Stefan Hajnoczi
        qemu_coroutine_enter(child, nd);
74 aa7ee42e Stefan Hajnoczi
    }
75 aa7ee42e Stefan Hajnoczi
76 aa7ee42e Stefan Hajnoczi
    nd->n_return++;
77 aa7ee42e Stefan Hajnoczi
}
78 aa7ee42e Stefan Hajnoczi
79 aa7ee42e Stefan Hajnoczi
static void test_nesting(void)
80 aa7ee42e Stefan Hajnoczi
{
81 aa7ee42e Stefan Hajnoczi
    Coroutine *root;
82 aa7ee42e Stefan Hajnoczi
    NestData nd = {
83 aa7ee42e Stefan Hajnoczi
        .n_enter  = 0,
84 aa7ee42e Stefan Hajnoczi
        .n_return = 0,
85 aa7ee42e Stefan Hajnoczi
        .max      = 128,
86 aa7ee42e Stefan Hajnoczi
    };
87 aa7ee42e Stefan Hajnoczi
88 aa7ee42e Stefan Hajnoczi
    root = qemu_coroutine_create(nest);
89 aa7ee42e Stefan Hajnoczi
    qemu_coroutine_enter(root, &nd);
90 aa7ee42e Stefan Hajnoczi
91 aa7ee42e Stefan Hajnoczi
    /* Must enter and return from max nesting level */
92 aa7ee42e Stefan Hajnoczi
    g_assert_cmpint(nd.n_enter, ==, nd.max);
93 aa7ee42e Stefan Hajnoczi
    g_assert_cmpint(nd.n_return, ==, nd.max);
94 aa7ee42e Stefan Hajnoczi
}
95 aa7ee42e Stefan Hajnoczi
96 aa7ee42e Stefan Hajnoczi
/*
97 aa7ee42e Stefan Hajnoczi
 * Check that yield/enter transfer control correctly
98 aa7ee42e Stefan Hajnoczi
 */
99 aa7ee42e Stefan Hajnoczi
100 aa7ee42e Stefan Hajnoczi
static void coroutine_fn yield_5_times(void *opaque)
101 aa7ee42e Stefan Hajnoczi
{
102 aa7ee42e Stefan Hajnoczi
    bool *done = opaque;
103 aa7ee42e Stefan Hajnoczi
    int i;
104 aa7ee42e Stefan Hajnoczi
105 aa7ee42e Stefan Hajnoczi
    for (i = 0; i < 5; i++) {
106 aa7ee42e Stefan Hajnoczi
        qemu_coroutine_yield();
107 aa7ee42e Stefan Hajnoczi
    }
108 aa7ee42e Stefan Hajnoczi
    *done = true;
109 aa7ee42e Stefan Hajnoczi
}
110 aa7ee42e Stefan Hajnoczi
111 aa7ee42e Stefan Hajnoczi
static void test_yield(void)
112 aa7ee42e Stefan Hajnoczi
{
113 aa7ee42e Stefan Hajnoczi
    Coroutine *coroutine;
114 aa7ee42e Stefan Hajnoczi
    bool done = false;
115 aa7ee42e Stefan Hajnoczi
    int i = -1; /* one extra time to return from coroutine */
116 aa7ee42e Stefan Hajnoczi
117 aa7ee42e Stefan Hajnoczi
    coroutine = qemu_coroutine_create(yield_5_times);
118 aa7ee42e Stefan Hajnoczi
    while (!done) {
119 aa7ee42e Stefan Hajnoczi
        qemu_coroutine_enter(coroutine, &done);
120 aa7ee42e Stefan Hajnoczi
        i++;
121 aa7ee42e Stefan Hajnoczi
    }
122 aa7ee42e Stefan Hajnoczi
    g_assert_cmpint(i, ==, 5); /* coroutine must yield 5 times */
123 aa7ee42e Stefan Hajnoczi
}
124 aa7ee42e Stefan Hajnoczi
125 aa7ee42e Stefan Hajnoczi
/*
126 aa7ee42e Stefan Hajnoczi
 * Check that creation, enter, and return work
127 aa7ee42e Stefan Hajnoczi
 */
128 aa7ee42e Stefan Hajnoczi
129 aa7ee42e Stefan Hajnoczi
static void coroutine_fn set_and_exit(void *opaque)
130 aa7ee42e Stefan Hajnoczi
{
131 aa7ee42e Stefan Hajnoczi
    bool *done = opaque;
132 aa7ee42e Stefan Hajnoczi
133 aa7ee42e Stefan Hajnoczi
    *done = true;
134 aa7ee42e Stefan Hajnoczi
}
135 aa7ee42e Stefan Hajnoczi
136 aa7ee42e Stefan Hajnoczi
static void test_lifecycle(void)
137 aa7ee42e Stefan Hajnoczi
{
138 aa7ee42e Stefan Hajnoczi
    Coroutine *coroutine;
139 aa7ee42e Stefan Hajnoczi
    bool done = false;
140 aa7ee42e Stefan Hajnoczi
141 aa7ee42e Stefan Hajnoczi
    /* Create, enter, and return from coroutine */
142 aa7ee42e Stefan Hajnoczi
    coroutine = qemu_coroutine_create(set_and_exit);
143 aa7ee42e Stefan Hajnoczi
    qemu_coroutine_enter(coroutine, &done);
144 aa7ee42e Stefan Hajnoczi
    g_assert(done); /* expect done to be true (first time) */
145 aa7ee42e Stefan Hajnoczi
146 aa7ee42e Stefan Hajnoczi
    /* Repeat to check that no state affects this test */
147 aa7ee42e Stefan Hajnoczi
    done = false;
148 aa7ee42e Stefan Hajnoczi
    coroutine = qemu_coroutine_create(set_and_exit);
149 aa7ee42e Stefan Hajnoczi
    qemu_coroutine_enter(coroutine, &done);
150 aa7ee42e Stefan Hajnoczi
    g_assert(done); /* expect done to be true (second time) */
151 aa7ee42e Stefan Hajnoczi
}
152 aa7ee42e Stefan Hajnoczi
153 f8d1daea Charlie Shepherd
154 f8d1daea Charlie Shepherd
#define RECORD_SIZE 10 /* Leave some room for expansion */
155 f8d1daea Charlie Shepherd
struct coroutine_position {
156 f8d1daea Charlie Shepherd
    int func;
157 f8d1daea Charlie Shepherd
    int state;
158 f8d1daea Charlie Shepherd
};
159 f8d1daea Charlie Shepherd
static struct coroutine_position records[RECORD_SIZE];
160 f8d1daea Charlie Shepherd
static unsigned record_pos;
161 f8d1daea Charlie Shepherd
162 f8d1daea Charlie Shepherd
static void record_push(int func, int state)
163 f8d1daea Charlie Shepherd
{
164 f8d1daea Charlie Shepherd
    struct coroutine_position *cp = &records[record_pos++];
165 f8d1daea Charlie Shepherd
    g_assert_cmpint(record_pos, <, RECORD_SIZE);
166 f8d1daea Charlie Shepherd
    cp->func = func;
167 f8d1daea Charlie Shepherd
    cp->state = state;
168 f8d1daea Charlie Shepherd
}
169 f8d1daea Charlie Shepherd
170 f8d1daea Charlie Shepherd
static void coroutine_fn co_order_test(void *opaque)
171 f8d1daea Charlie Shepherd
{
172 f8d1daea Charlie Shepherd
    record_push(2, 1);
173 f8d1daea Charlie Shepherd
    g_assert(qemu_in_coroutine());
174 f8d1daea Charlie Shepherd
    qemu_coroutine_yield();
175 f8d1daea Charlie Shepherd
    record_push(2, 2);
176 f8d1daea Charlie Shepherd
    g_assert(qemu_in_coroutine());
177 f8d1daea Charlie Shepherd
}
178 f8d1daea Charlie Shepherd
179 f8d1daea Charlie Shepherd
static void do_order_test(void)
180 f8d1daea Charlie Shepherd
{
181 f8d1daea Charlie Shepherd
    Coroutine *co;
182 f8d1daea Charlie Shepherd
183 f8d1daea Charlie Shepherd
    co = qemu_coroutine_create(co_order_test);
184 f8d1daea Charlie Shepherd
    record_push(1, 1);
185 f8d1daea Charlie Shepherd
    qemu_coroutine_enter(co, NULL);
186 f8d1daea Charlie Shepherd
    record_push(1, 2);
187 f8d1daea Charlie Shepherd
    g_assert(!qemu_in_coroutine());
188 f8d1daea Charlie Shepherd
    qemu_coroutine_enter(co, NULL);
189 f8d1daea Charlie Shepherd
    record_push(1, 3);
190 f8d1daea Charlie Shepherd
    g_assert(!qemu_in_coroutine());
191 f8d1daea Charlie Shepherd
}
192 f8d1daea Charlie Shepherd
193 f8d1daea Charlie Shepherd
static void test_order(void)
194 f8d1daea Charlie Shepherd
{
195 f8d1daea Charlie Shepherd
    int i;
196 f8d1daea Charlie Shepherd
    const struct coroutine_position expected_pos[] = {
197 f8d1daea Charlie Shepherd
        {1, 1,}, {2, 1}, {1, 2}, {2, 2}, {1, 3}
198 f8d1daea Charlie Shepherd
    };
199 f8d1daea Charlie Shepherd
    do_order_test();
200 f8d1daea Charlie Shepherd
    g_assert_cmpint(record_pos, ==, 5);
201 f8d1daea Charlie Shepherd
    for (i = 0; i < record_pos; i++) {
202 f8d1daea Charlie Shepherd
        g_assert_cmpint(records[i].func , ==, expected_pos[i].func );
203 f8d1daea Charlie Shepherd
        g_assert_cmpint(records[i].state, ==, expected_pos[i].state);
204 f8d1daea Charlie Shepherd
    }
205 f8d1daea Charlie Shepherd
}
206 5e3840ce Stefan Hajnoczi
/*
207 5e3840ce Stefan Hajnoczi
 * Lifecycle benchmark
208 5e3840ce Stefan Hajnoczi
 */
209 5e3840ce Stefan Hajnoczi
210 5e3840ce Stefan Hajnoczi
static void coroutine_fn empty_coroutine(void *opaque)
211 5e3840ce Stefan Hajnoczi
{
212 5e3840ce Stefan Hajnoczi
    /* Do nothing */
213 5e3840ce Stefan Hajnoczi
}
214 5e3840ce Stefan Hajnoczi
215 5e3840ce Stefan Hajnoczi
static void perf_lifecycle(void)
216 5e3840ce Stefan Hajnoczi
{
217 5e3840ce Stefan Hajnoczi
    Coroutine *coroutine;
218 5e3840ce Stefan Hajnoczi
    unsigned int i, max;
219 5e3840ce Stefan Hajnoczi
    double duration;
220 5e3840ce Stefan Hajnoczi
221 5e3840ce Stefan Hajnoczi
    max = 1000000;
222 5e3840ce Stefan Hajnoczi
223 5e3840ce Stefan Hajnoczi
    g_test_timer_start();
224 5e3840ce Stefan Hajnoczi
    for (i = 0; i < max; i++) {
225 5e3840ce Stefan Hajnoczi
        coroutine = qemu_coroutine_create(empty_coroutine);
226 5e3840ce Stefan Hajnoczi
        qemu_coroutine_enter(coroutine, NULL);
227 5e3840ce Stefan Hajnoczi
    }
228 5e3840ce Stefan Hajnoczi
    duration = g_test_timer_elapsed();
229 5e3840ce Stefan Hajnoczi
230 5e3840ce Stefan Hajnoczi
    g_test_message("Lifecycle %u iterations: %f s\n", max, duration);
231 5e3840ce Stefan Hajnoczi
}
232 5e3840ce Stefan Hajnoczi
233 7e849a99 Alex Barcelo
static void perf_nesting(void)
234 7e849a99 Alex Barcelo
{
235 7e849a99 Alex Barcelo
    unsigned int i, maxcycles, maxnesting;
236 7e849a99 Alex Barcelo
    double duration;
237 7e849a99 Alex Barcelo
238 a9031675 Gabriel Kerneis
    maxcycles = 10000;
239 02700315 Paolo Bonzini
    maxnesting = 1000;
240 7e849a99 Alex Barcelo
    Coroutine *root;
241 7e849a99 Alex Barcelo
242 7e849a99 Alex Barcelo
    g_test_timer_start();
243 7e849a99 Alex Barcelo
    for (i = 0; i < maxcycles; i++) {
244 a9031675 Gabriel Kerneis
        NestData nd = {
245 a9031675 Gabriel Kerneis
            .n_enter  = 0,
246 a9031675 Gabriel Kerneis
            .n_return = 0,
247 a9031675 Gabriel Kerneis
            .max      = maxnesting,
248 a9031675 Gabriel Kerneis
        };
249 7e849a99 Alex Barcelo
        root = qemu_coroutine_create(nest);
250 7e849a99 Alex Barcelo
        qemu_coroutine_enter(root, &nd);
251 7e849a99 Alex Barcelo
    }
252 7e849a99 Alex Barcelo
    duration = g_test_timer_elapsed();
253 7e849a99 Alex Barcelo
254 7e849a99 Alex Barcelo
    g_test_message("Nesting %u iterations of %u depth each: %f s\n",
255 7e849a99 Alex Barcelo
        maxcycles, maxnesting, duration);
256 7e849a99 Alex Barcelo
}
257 7e849a99 Alex Barcelo
258 2fcd15ea Gabriel Kerneis
/*
259 2fcd15ea Gabriel Kerneis
 * Yield benchmark
260 2fcd15ea Gabriel Kerneis
 */
261 2fcd15ea Gabriel Kerneis
262 2fcd15ea Gabriel Kerneis
static void coroutine_fn yield_loop(void *opaque)
263 2fcd15ea Gabriel Kerneis
{
264 2fcd15ea Gabriel Kerneis
    unsigned int *counter = opaque;
265 2fcd15ea Gabriel Kerneis
266 2fcd15ea Gabriel Kerneis
    while ((*counter) > 0) {
267 2fcd15ea Gabriel Kerneis
        (*counter)--;
268 2fcd15ea Gabriel Kerneis
        qemu_coroutine_yield();
269 2fcd15ea Gabriel Kerneis
    }
270 2fcd15ea Gabriel Kerneis
}
271 2fcd15ea Gabriel Kerneis
272 2fcd15ea Gabriel Kerneis
static void perf_yield(void)
273 2fcd15ea Gabriel Kerneis
{
274 2fcd15ea Gabriel Kerneis
    unsigned int i, maxcycles;
275 2fcd15ea Gabriel Kerneis
    double duration;
276 2fcd15ea Gabriel Kerneis
277 2fcd15ea Gabriel Kerneis
    maxcycles = 100000000;
278 2fcd15ea Gabriel Kerneis
    i = maxcycles;
279 2fcd15ea Gabriel Kerneis
    Coroutine *coroutine = qemu_coroutine_create(yield_loop);
280 2fcd15ea Gabriel Kerneis
281 2fcd15ea Gabriel Kerneis
    g_test_timer_start();
282 2fcd15ea Gabriel Kerneis
    while (i > 0) {
283 2fcd15ea Gabriel Kerneis
        qemu_coroutine_enter(coroutine, &i);
284 2fcd15ea Gabriel Kerneis
    }
285 2fcd15ea Gabriel Kerneis
    duration = g_test_timer_elapsed();
286 2fcd15ea Gabriel Kerneis
287 2fcd15ea Gabriel Kerneis
    g_test_message("Yield %u iterations: %f s\n",
288 2fcd15ea Gabriel Kerneis
        maxcycles, duration);
289 2fcd15ea Gabriel Kerneis
}
290 7e849a99 Alex Barcelo
291 aa7ee42e Stefan Hajnoczi
int main(int argc, char **argv)
292 aa7ee42e Stefan Hajnoczi
{
293 aa7ee42e Stefan Hajnoczi
    g_test_init(&argc, &argv, NULL);
294 aa7ee42e Stefan Hajnoczi
    g_test_add_func("/basic/lifecycle", test_lifecycle);
295 aa7ee42e Stefan Hajnoczi
    g_test_add_func("/basic/yield", test_yield);
296 aa7ee42e Stefan Hajnoczi
    g_test_add_func("/basic/nesting", test_nesting);
297 aa7ee42e Stefan Hajnoczi
    g_test_add_func("/basic/self", test_self);
298 aa7ee42e Stefan Hajnoczi
    g_test_add_func("/basic/in_coroutine", test_in_coroutine);
299 f8d1daea Charlie Shepherd
    g_test_add_func("/basic/order", test_order);
300 5e3840ce Stefan Hajnoczi
    if (g_test_perf()) {
301 5e3840ce Stefan Hajnoczi
        g_test_add_func("/perf/lifecycle", perf_lifecycle);
302 7e849a99 Alex Barcelo
        g_test_add_func("/perf/nesting", perf_nesting);
303 2fcd15ea Gabriel Kerneis
        g_test_add_func("/perf/yield", perf_yield);
304 5e3840ce Stefan Hajnoczi
    }
305 aa7ee42e Stefan Hajnoczi
    return g_test_run();
306 aa7ee42e Stefan Hajnoczi
}