test-thread-pool.c 6.2 KB
Newer Older
P
Peter Maydell 已提交
1
#include "qemu/osdep.h"
P
Paolo Bonzini 已提交
2
#include "qemu-common.h"
3 4 5
#include "block/aio.h"
#include "block/thread-pool.h"
#include "block/block.h"
6
#include "qapi/error.h"
7
#include "qemu/timer.h"
8
#include "qemu/error-report.h"
P
Paolo Bonzini 已提交
9

10 11
static AioContext *ctx;
static ThreadPool *pool;
P
Paolo Bonzini 已提交
12 13 14
static int active;

typedef struct {
15
    BlockAIOCB *aiocb;
P
Paolo Bonzini 已提交
16 17 18 19 20 21 22
    int n;
    int ret;
} WorkerTestData;

static int worker_cb(void *opaque)
{
    WorkerTestData *data = opaque;
23
    return atomic_fetch_inc(&data->n);
P
Paolo Bonzini 已提交
24 25 26 27 28
}

static int long_cb(void *opaque)
{
    WorkerTestData *data = opaque;
29
    atomic_inc(&data->n);
P
Paolo Bonzini 已提交
30
    g_usleep(2000000);
31
    atomic_inc(&data->n);
P
Paolo Bonzini 已提交
32 33 34 35 36 37
    return 0;
}

static void done_cb(void *opaque, int ret)
{
    WorkerTestData *data = opaque;
38
    g_assert(data->ret == -EINPROGRESS || data->ret == -ECANCELED);
P
Paolo Bonzini 已提交
39 40 41 42 43 44 45 46 47 48
    data->ret = ret;
    data->aiocb = NULL;

    /* Callbacks are serialized, so no need to use atomic ops.  */
    active--;
}

static void test_submit(void)
{
    WorkerTestData data = { .n = 0 };
49
    thread_pool_submit(pool, worker_cb, &data);
50 51 52
    while (data.n == 0) {
        aio_poll(ctx, true);
    }
P
Paolo Bonzini 已提交
53 54 55 56 57 58
    g_assert_cmpint(data.n, ==, 1);
}

static void test_submit_aio(void)
{
    WorkerTestData data = { .n = 0, .ret = -EINPROGRESS };
59 60
    data.aiocb = thread_pool_submit_aio(pool, worker_cb, &data,
                                        done_cb, &data);
P
Paolo Bonzini 已提交
61 62 63 64

    /* The callbacks are not called until after the first wait.  */
    active = 1;
    g_assert_cmpint(data.ret, ==, -EINPROGRESS);
65 66 67
    while (data.ret == -EINPROGRESS) {
        aio_poll(ctx, true);
    }
P
Paolo Bonzini 已提交
68 69 70 71 72 73 74 75 76 77 78 79
    g_assert_cmpint(active, ==, 0);
    g_assert_cmpint(data.n, ==, 1);
    g_assert_cmpint(data.ret, ==, 0);
}

static void co_test_cb(void *opaque)
{
    WorkerTestData *data = opaque;

    active = 1;
    data->n = 0;
    data->ret = -EINPROGRESS;
80
    thread_pool_submit_co(pool, worker_cb, data);
P
Paolo Bonzini 已提交
81 82 83 84 85 86 87

    /* The test continues in test_submit_co, after qemu_coroutine_enter... */

    g_assert_cmpint(data->n, ==, 1);
    data->ret = 0;
    active--;

88
    /* The test continues in test_submit_co, after aio_poll... */
P
Paolo Bonzini 已提交
89 90 91 92 93 94 95 96 97 98 99 100 101 102
}

static void test_submit_co(void)
{
    WorkerTestData data;
    Coroutine *co = qemu_coroutine_create(co_test_cb);

    qemu_coroutine_enter(co, &data);

    /* Back here once the worker has started.  */

    g_assert_cmpint(active, ==, 1);
    g_assert_cmpint(data.ret, ==, -EINPROGRESS);

103
    /* aio_poll will execute the rest of the coroutine.  */
P
Paolo Bonzini 已提交
104

105 106 107
    while (data.ret == -EINPROGRESS) {
        aio_poll(ctx, true);
    }
P
Paolo Bonzini 已提交
108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123

    /* Back here after the coroutine has finished.  */

    g_assert_cmpint(active, ==, 0);
    g_assert_cmpint(data.ret, ==, 0);
}

static void test_submit_many(void)
{
    WorkerTestData data[100];
    int i;

    /* Start more work items than there will be threads.  */
    for (i = 0; i < 100; i++) {
        data[i].n = 0;
        data[i].ret = -EINPROGRESS;
124
        thread_pool_submit_aio(pool, worker_cb, &data[i], done_cb, &data[i]);
P
Paolo Bonzini 已提交
125 126 127 128
    }

    active = 100;
    while (active > 0) {
129
        aio_poll(ctx, true);
P
Paolo Bonzini 已提交
130 131 132 133 134 135 136
    }
    for (i = 0; i < 100; i++) {
        g_assert_cmpint(data[i].n, ==, 1);
        g_assert_cmpint(data[i].ret, ==, 0);
    }
}

137
static void do_test_cancel(bool sync)
P
Paolo Bonzini 已提交
138 139
{
    WorkerTestData data[100];
140
    int num_canceled;
P
Paolo Bonzini 已提交
141 142 143 144 145 146 147 148 149 150 151
    int i;

    /* Start more work items than there will be threads, to ensure
     * the pool is full.
     */
    test_submit_many();

    /* Start long running jobs, to ensure we can cancel some.  */
    for (i = 0; i < 100; i++) {
        data[i].n = 0;
        data[i].ret = -EINPROGRESS;
152
        data[i].aiocb = thread_pool_submit_aio(pool, long_cb, &data[i],
P
Paolo Bonzini 已提交
153 154 155 156 157 158 159
                                               done_cb, &data[i]);
    }

    /* Starting the threads may be left to a bottom half.  Let it
     * run, but do not waste too much time...
     */
    active = 100;
160 161
    aio_notify(ctx);
    aio_poll(ctx, false);
P
Paolo Bonzini 已提交
162 163 164 165 166 167 168 169 170

    /* Wait some time for the threads to start, with some sanity
     * testing on the behavior of the scheduler...
     */
    g_assert_cmpint(active, ==, 100);
    g_usleep(1000000);
    g_assert_cmpint(active, >, 50);

    /* Cancel the jobs that haven't been started yet.  */
171
    num_canceled = 0;
P
Paolo Bonzini 已提交
172
    for (i = 0; i < 100; i++) {
173
        if (atomic_cmpxchg(&data[i].n, 0, 3) == 0) {
P
Paolo Bonzini 已提交
174
            data[i].ret = -ECANCELED;
175 176 177 178 179
            if (sync) {
                bdrv_aio_cancel(data[i].aiocb);
            } else {
                bdrv_aio_cancel_async(data[i].aiocb);
            }
180
            num_canceled++;
P
Paolo Bonzini 已提交
181 182
        }
    }
183 184
    g_assert_cmpint(active, >, 0);
    g_assert_cmpint(num_canceled, <, 100);
P
Paolo Bonzini 已提交
185 186

    for (i = 0; i < 100; i++) {
187
        if (data[i].aiocb && data[i].n != 3) {
188 189 190 191 192 193
            if (sync) {
                /* Canceling the others will be a blocking operation.  */
                bdrv_aio_cancel(data[i].aiocb);
            } else {
                bdrv_aio_cancel_async(data[i].aiocb);
            }
P
Paolo Bonzini 已提交
194 195 196 197
        }
    }

    /* Finish execution and execute any remaining callbacks.  */
198 199 200
    while (active > 0) {
        aio_poll(ctx, true);
    }
P
Paolo Bonzini 已提交
201 202 203 204
    g_assert_cmpint(active, ==, 0);
    for (i = 0; i < 100; i++) {
        if (data[i].n == 3) {
            g_assert_cmpint(data[i].ret, ==, -ECANCELED);
205
            g_assert(data[i].aiocb == NULL);
P
Paolo Bonzini 已提交
206 207
        } else {
            g_assert_cmpint(data[i].n, ==, 2);
208
            g_assert(data[i].ret == 0 || data[i].ret == -ECANCELED);
P
Paolo Bonzini 已提交
209 210 211 212 213
            g_assert(data[i].aiocb == NULL);
        }
    }
}

214 215 216 217 218 219 220 221 222 223
static void test_cancel(void)
{
    do_test_cancel(true);
}

static void test_cancel_async(void)
{
    do_test_cancel(false);
}

P
Paolo Bonzini 已提交
224 225
int main(int argc, char **argv)
{
226
    int ret;
227
    Error *local_error = NULL;
228

229 230
    init_clocks();

231 232
    ctx = aio_context_new(&local_error);
    if (!ctx) {
233
        error_reportf_err(local_error, "Failed to create AIO Context: ");
234 235
        exit(1);
    }
236
    pool = aio_get_thread_pool(ctx);
P
Paolo Bonzini 已提交
237 238 239 240 241 242 243

    g_test_init(&argc, &argv, NULL);
    g_test_add_func("/thread-pool/submit", test_submit);
    g_test_add_func("/thread-pool/submit-aio", test_submit_aio);
    g_test_add_func("/thread-pool/submit-co", test_submit_co);
    g_test_add_func("/thread-pool/submit-many", test_submit_many);
    g_test_add_func("/thread-pool/cancel", test_cancel);
244
    g_test_add_func("/thread-pool/cancel-async", test_cancel_async);
245 246 247 248 249

    ret = g_test_run();

    aio_context_unref(ctx);
    return ret;
P
Paolo Bonzini 已提交
250
}