]> Git Repo - qemu.git/blob - tests/test-thread-pool.c
tests: make threadpool cancellation test looser
[qemu.git] / tests / test-thread-pool.c
1 #include <glib.h>
2 #include "qemu-common.h"
3 #include "qemu-aio.h"
4 #include "thread-pool.h"
5 #include "block.h"
6
7 static int active;
8
9 typedef struct {
10     BlockDriverAIOCB *aiocb;
11     int n;
12     int ret;
13 } WorkerTestData;
14
15 static int worker_cb(void *opaque)
16 {
17     WorkerTestData *data = opaque;
18     return __sync_fetch_and_add(&data->n, 1);
19 }
20
21 static int long_cb(void *opaque)
22 {
23     WorkerTestData *data = opaque;
24     __sync_fetch_and_add(&data->n, 1);
25     g_usleep(2000000);
26     __sync_fetch_and_add(&data->n, 1);
27     return 0;
28 }
29
30 static void done_cb(void *opaque, int ret)
31 {
32     WorkerTestData *data = opaque;
33     g_assert_cmpint(data->ret, ==, -EINPROGRESS);
34     data->ret = ret;
35     data->aiocb = NULL;
36
37     /* Callbacks are serialized, so no need to use atomic ops.  */
38     active--;
39 }
40
41 /* A non-blocking poll of the main AIO context (we cannot use aio_poll
42  * because we do not know the AioContext).
43  */
44 static void qemu_aio_wait_nonblocking(void)
45 {
46     qemu_notify_event();
47     qemu_aio_wait();
48 }
49
50 static void test_submit(void)
51 {
52     WorkerTestData data = { .n = 0 };
53     thread_pool_submit(worker_cb, &data);
54     qemu_aio_flush();
55     g_assert_cmpint(data.n, ==, 1);
56 }
57
58 static void test_submit_aio(void)
59 {
60     WorkerTestData data = { .n = 0, .ret = -EINPROGRESS };
61     data.aiocb = thread_pool_submit_aio(worker_cb, &data, done_cb, &data);
62
63     /* The callbacks are not called until after the first wait.  */
64     active = 1;
65     g_assert_cmpint(data.ret, ==, -EINPROGRESS);
66     qemu_aio_flush();
67     g_assert_cmpint(active, ==, 0);
68     g_assert_cmpint(data.n, ==, 1);
69     g_assert_cmpint(data.ret, ==, 0);
70 }
71
72 static void co_test_cb(void *opaque)
73 {
74     WorkerTestData *data = opaque;
75
76     active = 1;
77     data->n = 0;
78     data->ret = -EINPROGRESS;
79     thread_pool_submit_co(worker_cb, data);
80
81     /* The test continues in test_submit_co, after qemu_coroutine_enter... */
82
83     g_assert_cmpint(data->n, ==, 1);
84     data->ret = 0;
85     active--;
86
87     /* The test continues in test_submit_co, after qemu_aio_flush... */
88 }
89
90 static void test_submit_co(void)
91 {
92     WorkerTestData data;
93     Coroutine *co = qemu_coroutine_create(co_test_cb);
94
95     qemu_coroutine_enter(co, &data);
96
97     /* Back here once the worker has started.  */
98
99     g_assert_cmpint(active, ==, 1);
100     g_assert_cmpint(data.ret, ==, -EINPROGRESS);
101
102     /* qemu_aio_flush will execute the rest of the coroutine.  */
103
104     qemu_aio_flush();
105
106     /* Back here after the coroutine has finished.  */
107
108     g_assert_cmpint(active, ==, 0);
109     g_assert_cmpint(data.ret, ==, 0);
110 }
111
112 static void test_submit_many(void)
113 {
114     WorkerTestData data[100];
115     int i;
116
117     /* Start more work items than there will be threads.  */
118     for (i = 0; i < 100; i++) {
119         data[i].n = 0;
120         data[i].ret = -EINPROGRESS;
121         thread_pool_submit_aio(worker_cb, &data[i], done_cb, &data[i]);
122     }
123
124     active = 100;
125     while (active > 0) {
126         qemu_aio_wait();
127     }
128     for (i = 0; i < 100; i++) {
129         g_assert_cmpint(data[i].n, ==, 1);
130         g_assert_cmpint(data[i].ret, ==, 0);
131     }
132 }
133
134 static void test_cancel(void)
135 {
136     WorkerTestData data[100];
137     int num_canceled;
138     int i;
139
140     /* Start more work items than there will be threads, to ensure
141      * the pool is full.
142      */
143     test_submit_many();
144
145     /* Start long running jobs, to ensure we can cancel some.  */
146     for (i = 0; i < 100; i++) {
147         data[i].n = 0;
148         data[i].ret = -EINPROGRESS;
149         data[i].aiocb = thread_pool_submit_aio(long_cb, &data[i],
150                                                done_cb, &data[i]);
151     }
152
153     /* Starting the threads may be left to a bottom half.  Let it
154      * run, but do not waste too much time...
155      */
156     active = 100;
157     qemu_aio_wait_nonblocking();
158
159     /* Wait some time for the threads to start, with some sanity
160      * testing on the behavior of the scheduler...
161      */
162     g_assert_cmpint(active, ==, 100);
163     g_usleep(1000000);
164     g_assert_cmpint(active, >, 50);
165
166     /* Cancel the jobs that haven't been started yet.  */
167     num_canceled = 0;
168     for (i = 0; i < 100; i++) {
169         if (__sync_val_compare_and_swap(&data[i].n, 0, 3) == 0) {
170             data[i].ret = -ECANCELED;
171             bdrv_aio_cancel(data[i].aiocb);
172             active--;
173             num_canceled++;
174         }
175     }
176     g_assert_cmpint(active, >, 0);
177     g_assert_cmpint(num_canceled, <, 100);
178
179     /* Canceling the others will be a blocking operation.  */
180     for (i = 0; i < 100; i++) {
181         if (data[i].n != 3) {
182             bdrv_aio_cancel(data[i].aiocb);
183         }
184     }
185
186     /* Finish execution and execute any remaining callbacks.  */
187     qemu_aio_flush();
188     g_assert_cmpint(active, ==, 0);
189     for (i = 0; i < 100; i++) {
190         if (data[i].n == 3) {
191             g_assert_cmpint(data[i].ret, ==, -ECANCELED);
192             g_assert(data[i].aiocb != NULL);
193         } else {
194             g_assert_cmpint(data[i].n, ==, 2);
195             g_assert_cmpint(data[i].ret, ==, 0);
196             g_assert(data[i].aiocb == NULL);
197         }
198     }
199 }
200
201 int main(int argc, char **argv)
202 {
203     /* These should be removed once each AioContext has its thread pool.
204      * The test should create its own AioContext.
205      */
206     qemu_init_main_loop();
207     bdrv_init();
208
209     g_test_init(&argc, &argv, NULL);
210     g_test_add_func("/thread-pool/submit", test_submit);
211     g_test_add_func("/thread-pool/submit-aio", test_submit_aio);
212     g_test_add_func("/thread-pool/submit-co", test_submit_co);
213     g_test_add_func("/thread-pool/submit-many", test_submit_many);
214     g_test_add_func("/thread-pool/cancel", test_cancel);
215     return g_test_run();
216 }
This page took 0.034009 seconds and 4 git commands to generate.