test-thread-pool.c 5.5 KB
Newer Older
P
Paolo Bonzini 已提交
1 2
#include <glib.h>
#include "qemu-common.h"
3 4 5
#include "block/aio.h"
#include "block/thread-pool.h"
#include "block/block.h"
P
Paolo Bonzini 已提交
6

7 8
static AioContext *ctx;
static ThreadPool *pool;
P
Paolo Bonzini 已提交
9 10 11 12 13 14 15 16 17 18 19
static int active;

typedef struct {
    BlockDriverAIOCB *aiocb;
    int n;
    int ret;
} WorkerTestData;

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

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

static void done_cb(void *opaque, int ret)
{
    WorkerTestData *data = opaque;
    g_assert_cmpint(data->ret, ==, -EINPROGRESS);
    data->ret = ret;
    data->aiocb = NULL;

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

43 44 45
/* Wait until all aio and bh activity has finished */
static void qemu_aio_wait_all(void)
{
46
    while (aio_poll(ctx, true)) {
47 48 49 50
        /* Do nothing */
    }
}

P
Paolo Bonzini 已提交
51 52 53
static void test_submit(void)
{
    WorkerTestData data = { .n = 0 };
54
    thread_pool_submit(pool, worker_cb, &data);
55
    qemu_aio_wait_all();
P
Paolo Bonzini 已提交
56 57 58 59 60 61
    g_assert_cmpint(data.n, ==, 1);
}

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

    /* The callbacks are not called until after the first wait.  */
    active = 1;
    g_assert_cmpint(data.ret, ==, -EINPROGRESS);
68
    qemu_aio_wait_all();
P
Paolo Bonzini 已提交
69 70 71 72 73 74 75 76 77 78 79 80
    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;
81
    thread_pool_submit_co(pool, worker_cb, data);
P
Paolo Bonzini 已提交
82 83 84 85 86 87 88

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

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

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

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);

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

106
    qemu_aio_wait_all();
P
Paolo Bonzini 已提交
107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122

    /* 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;
123
        thread_pool_submit_aio(pool, worker_cb, &data[i], done_cb, &data[i]);
P
Paolo Bonzini 已提交
124 125 126 127
    }

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

static void test_cancel(void)
{
    WorkerTestData data[100];
139
    int num_canceled;
P
Paolo Bonzini 已提交
140 141 142 143 144 145 146 147 148 149 150
    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;
151
        data[i].aiocb = thread_pool_submit_aio(pool, long_cb, &data[i],
P
Paolo Bonzini 已提交
152 153 154 155 156 157 158
                                               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;
159 160
    aio_notify(ctx);
    aio_poll(ctx, false);
P
Paolo Bonzini 已提交
161 162 163 164 165 166 167 168 169

    /* 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.  */
170
    num_canceled = 0;
P
Paolo Bonzini 已提交
171
    for (i = 0; i < 100; i++) {
172
        if (atomic_cmpxchg(&data[i].n, 0, 3) == 0) {
P
Paolo Bonzini 已提交
173 174 175
            data[i].ret = -ECANCELED;
            bdrv_aio_cancel(data[i].aiocb);
            active--;
176
            num_canceled++;
P
Paolo Bonzini 已提交
177 178
        }
    }
179 180
    g_assert_cmpint(active, >, 0);
    g_assert_cmpint(num_canceled, <, 100);
P
Paolo Bonzini 已提交
181 182 183 184 185 186 187 188 189

    /* Canceling the others will be a blocking operation.  */
    for (i = 0; i < 100; i++) {
        if (data[i].n != 3) {
            bdrv_aio_cancel(data[i].aiocb);
        }
    }

    /* Finish execution and execute any remaining callbacks.  */
190
    qemu_aio_wait_all();
P
Paolo Bonzini 已提交
191 192 193 194 195 196 197 198 199 200 201 202 203 204 205
    g_assert_cmpint(active, ==, 0);
    for (i = 0; i < 100; i++) {
        if (data[i].n == 3) {
            g_assert_cmpint(data[i].ret, ==, -ECANCELED);
            g_assert(data[i].aiocb != NULL);
        } else {
            g_assert_cmpint(data[i].n, ==, 2);
            g_assert_cmpint(data[i].ret, ==, 0);
            g_assert(data[i].aiocb == NULL);
        }
    }
}

int main(int argc, char **argv)
{
206 207 208 209
    int ret;

    ctx = aio_context_new();
    pool = aio_get_thread_pool(ctx);
P
Paolo Bonzini 已提交
210 211 212 213 214 215 216

    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);
217 218 219 220 221

    ret = g_test_run();

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