virt-acpi-build: add always-on property for timer
[qemu/ar7.git] / tests / test-thread-pool.c
blobccdee3999309ca87a9be120724db9316eb143dd2
1 #include <glib.h>
2 #include "qemu-common.h"
3 #include "block/aio.h"
4 #include "block/thread-pool.h"
5 #include "block/block.h"
6 #include "qemu/timer.h"
7 #include "qemu/error-report.h"
9 static AioContext *ctx;
10 static ThreadPool *pool;
11 static int active;
13 typedef struct {
14 BlockAIOCB *aiocb;
15 int n;
16 int ret;
17 } WorkerTestData;
19 static int worker_cb(void *opaque)
21 WorkerTestData *data = opaque;
22 return atomic_fetch_inc(&data->n);
25 static int long_cb(void *opaque)
27 WorkerTestData *data = opaque;
28 atomic_inc(&data->n);
29 g_usleep(2000000);
30 atomic_inc(&data->n);
31 return 0;
34 static void done_cb(void *opaque, int ret)
36 WorkerTestData *data = opaque;
37 g_assert(data->ret == -EINPROGRESS || data->ret == -ECANCELED);
38 data->ret = ret;
39 data->aiocb = NULL;
41 /* Callbacks are serialized, so no need to use atomic ops. */
42 active--;
45 static void test_submit(void)
47 WorkerTestData data = { .n = 0 };
48 thread_pool_submit(pool, worker_cb, &data);
49 while (data.n == 0) {
50 aio_poll(ctx, true);
52 g_assert_cmpint(data.n, ==, 1);
55 static void test_submit_aio(void)
57 WorkerTestData data = { .n = 0, .ret = -EINPROGRESS };
58 data.aiocb = thread_pool_submit_aio(pool, worker_cb, &data,
59 done_cb, &data);
61 /* The callbacks are not called until after the first wait. */
62 active = 1;
63 g_assert_cmpint(data.ret, ==, -EINPROGRESS);
64 while (data.ret == -EINPROGRESS) {
65 aio_poll(ctx, true);
67 g_assert_cmpint(active, ==, 0);
68 g_assert_cmpint(data.n, ==, 1);
69 g_assert_cmpint(data.ret, ==, 0);
72 static void co_test_cb(void *opaque)
74 WorkerTestData *data = opaque;
76 active = 1;
77 data->n = 0;
78 data->ret = -EINPROGRESS;
79 thread_pool_submit_co(pool, worker_cb, data);
81 /* The test continues in test_submit_co, after qemu_coroutine_enter... */
83 g_assert_cmpint(data->n, ==, 1);
84 data->ret = 0;
85 active--;
87 /* The test continues in test_submit_co, after aio_poll... */
90 static void test_submit_co(void)
92 WorkerTestData data;
93 Coroutine *co = qemu_coroutine_create(co_test_cb);
95 qemu_coroutine_enter(co, &data);
97 /* Back here once the worker has started. */
99 g_assert_cmpint(active, ==, 1);
100 g_assert_cmpint(data.ret, ==, -EINPROGRESS);
102 /* aio_poll will execute the rest of the coroutine. */
104 while (data.ret == -EINPROGRESS) {
105 aio_poll(ctx, true);
108 /* Back here after the coroutine has finished. */
110 g_assert_cmpint(active, ==, 0);
111 g_assert_cmpint(data.ret, ==, 0);
114 static void test_submit_many(void)
116 WorkerTestData data[100];
117 int i;
119 /* Start more work items than there will be threads. */
120 for (i = 0; i < 100; i++) {
121 data[i].n = 0;
122 data[i].ret = -EINPROGRESS;
123 thread_pool_submit_aio(pool, worker_cb, &data[i], done_cb, &data[i]);
126 active = 100;
127 while (active > 0) {
128 aio_poll(ctx, true);
130 for (i = 0; i < 100; i++) {
131 g_assert_cmpint(data[i].n, ==, 1);
132 g_assert_cmpint(data[i].ret, ==, 0);
136 static void do_test_cancel(bool sync)
138 WorkerTestData data[100];
139 int num_canceled;
140 int i;
142 /* Start more work items than there will be threads, to ensure
143 * the pool is full.
145 test_submit_many();
147 /* Start long running jobs, to ensure we can cancel some. */
148 for (i = 0; i < 100; i++) {
149 data[i].n = 0;
150 data[i].ret = -EINPROGRESS;
151 data[i].aiocb = thread_pool_submit_aio(pool, long_cb, &data[i],
152 done_cb, &data[i]);
155 /* Starting the threads may be left to a bottom half. Let it
156 * run, but do not waste too much time...
158 active = 100;
159 aio_notify(ctx);
160 aio_poll(ctx, false);
162 /* Wait some time for the threads to start, with some sanity
163 * testing on the behavior of the scheduler...
165 g_assert_cmpint(active, ==, 100);
166 g_usleep(1000000);
167 g_assert_cmpint(active, >, 50);
169 /* Cancel the jobs that haven't been started yet. */
170 num_canceled = 0;
171 for (i = 0; i < 100; i++) {
172 if (atomic_cmpxchg(&data[i].n, 0, 3) == 0) {
173 data[i].ret = -ECANCELED;
174 if (sync) {
175 bdrv_aio_cancel(data[i].aiocb);
176 } else {
177 bdrv_aio_cancel_async(data[i].aiocb);
179 num_canceled++;
182 g_assert_cmpint(active, >, 0);
183 g_assert_cmpint(num_canceled, <, 100);
185 for (i = 0; i < 100; i++) {
186 if (data[i].aiocb && data[i].n != 3) {
187 if (sync) {
188 /* Canceling the others will be a blocking operation. */
189 bdrv_aio_cancel(data[i].aiocb);
190 } else {
191 bdrv_aio_cancel_async(data[i].aiocb);
196 /* Finish execution and execute any remaining callbacks. */
197 while (active > 0) {
198 aio_poll(ctx, true);
200 g_assert_cmpint(active, ==, 0);
201 for (i = 0; i < 100; i++) {
202 if (data[i].n == 3) {
203 g_assert_cmpint(data[i].ret, ==, -ECANCELED);
204 g_assert(data[i].aiocb == NULL);
205 } else {
206 g_assert_cmpint(data[i].n, ==, 2);
207 g_assert(data[i].ret == 0 || data[i].ret == -ECANCELED);
208 g_assert(data[i].aiocb == NULL);
213 static void test_cancel(void)
215 do_test_cancel(true);
218 static void test_cancel_async(void)
220 do_test_cancel(false);
223 int main(int argc, char **argv)
225 int ret;
226 Error *local_error = NULL;
228 init_clocks();
230 ctx = aio_context_new(&local_error);
231 if (!ctx) {
232 error_reportf_err(local_error, "Failed to create AIO Context: ");
233 exit(1);
235 pool = aio_get_thread_pool(ctx);
237 g_test_init(&argc, &argv, NULL);
238 g_test_add_func("/thread-pool/submit", test_submit);
239 g_test_add_func("/thread-pool/submit-aio", test_submit_aio);
240 g_test_add_func("/thread-pool/submit-co", test_submit_co);
241 g_test_add_func("/thread-pool/submit-many", test_submit_many);
242 g_test_add_func("/thread-pool/cancel", test_cancel);
243 g_test_add_func("/thread-pool/cancel-async", test_cancel_async);
245 ret = g_test_run();
247 aio_context_unref(ctx);
248 return ret;