Statistics
| Branch: | Revision:

root / thread-pool.c @ b811203c

History | View | Annotate | Download (8.7 kB)

1
/*
2
 * QEMU block layer thread pool
3
 *
4
 * Copyright IBM, Corp. 2008
5
 * Copyright Red Hat, Inc. 2012
6
 *
7
 * Authors:
8
 *  Anthony Liguori   <aliguori@us.ibm.com>
9
 *  Paolo Bonzini     <pbonzini@redhat.com>
10
 *
11
 * This work is licensed under the terms of the GNU GPL, version 2.  See
12
 * the COPYING file in the top-level directory.
13
 *
14
 * Contributions after 2012-01-13 are licensed under the terms of the
15
 * GNU GPL, version 2 or (at your option) any later version.
16
 */
17
#include "qemu-common.h"
18
#include "qemu/queue.h"
19
#include "qemu/thread.h"
20
#include "qemu/osdep.h"
21
#include "block/coroutine.h"
22
#include "trace.h"
23
#include "block/block_int.h"
24
#include "qemu/event_notifier.h"
25
#include "block/thread-pool.h"
26

    
27
typedef struct ThreadPool ThreadPool;
28

    
29
static void do_spawn_thread(ThreadPool *pool);
30

    
31
typedef struct ThreadPoolElement ThreadPoolElement;
32

    
33
enum ThreadState {
34
    THREAD_QUEUED,
35
    THREAD_ACTIVE,
36
    THREAD_DONE,
37
    THREAD_CANCELED,
38
};
39

    
40
struct ThreadPoolElement {
41
    BlockDriverAIOCB common;
42
    ThreadPool *pool;
43
    ThreadPoolFunc *func;
44
    void *arg;
45

    
46
    /* Moving state out of THREAD_QUEUED is protected by lock.  After
47
     * that, only the worker thread can write to it.  Reads and writes
48
     * of state and ret are ordered with memory barriers.
49
     */
50
    enum ThreadState state;
51
    int ret;
52

    
53
    /* Access to this list is protected by lock.  */
54
    QTAILQ_ENTRY(ThreadPoolElement) reqs;
55

    
56
    /* Access to this list is protected by the global mutex.  */
57
    QLIST_ENTRY(ThreadPoolElement) all;
58
};
59

    
60
struct ThreadPool {
61
    EventNotifier notifier;
62
    QemuMutex lock;
63
    QemuCond check_cancel;
64
    QemuSemaphore sem;
65
    int max_threads;
66
    QEMUBH *new_thread_bh;
67

    
68
    /* The following variables are only accessed from one AioContext. */
69
    QLIST_HEAD(, ThreadPoolElement) head;
70

    
71
    /* The following variables are protected by lock.  */
72
    QTAILQ_HEAD(, ThreadPoolElement) request_list;
73
    int cur_threads;
74
    int idle_threads;
75
    int new_threads;     /* backlog of threads we need to create */
76
    int pending_threads; /* threads created but not running yet */
77
    int pending_cancellations; /* whether we need a cond_broadcast */
78
};
79

    
80
/* Currently there is only one thread pool instance. */
81
static ThreadPool global_pool;
82

    
83
static void *worker_thread(void *opaque)
84
{
85
    ThreadPool *pool = opaque;
86

    
87
    qemu_mutex_lock(&pool->lock);
88
    pool->pending_threads--;
89
    do_spawn_thread(pool);
90

    
91
    while (1) {
92
        ThreadPoolElement *req;
93
        int ret;
94

    
95
        do {
96
            pool->idle_threads++;
97
            qemu_mutex_unlock(&pool->lock);
98
            ret = qemu_sem_timedwait(&pool->sem, 10000);
99
            qemu_mutex_lock(&pool->lock);
100
            pool->idle_threads--;
101
        } while (ret == -1 && !QTAILQ_EMPTY(&pool->request_list));
102
        if (ret == -1) {
103
            break;
104
        }
105

    
106
        req = QTAILQ_FIRST(&pool->request_list);
107
        QTAILQ_REMOVE(&pool->request_list, req, reqs);
108
        req->state = THREAD_ACTIVE;
109
        qemu_mutex_unlock(&pool->lock);
110

    
111
        ret = req->func(req->arg);
112

    
113
        req->ret = ret;
114
        /* Write ret before state.  */
115
        smp_wmb();
116
        req->state = THREAD_DONE;
117

    
118
        qemu_mutex_lock(&pool->lock);
119
        if (pool->pending_cancellations) {
120
            qemu_cond_broadcast(&pool->check_cancel);
121
        }
122

    
123
        event_notifier_set(&pool->notifier);
124
    }
125

    
126
    pool->cur_threads--;
127
    qemu_mutex_unlock(&pool->lock);
128
    return NULL;
129
}
130

    
131
static void do_spawn_thread(ThreadPool *pool)
132
{
133
    QemuThread t;
134

    
135
    /* Runs with lock taken.  */
136
    if (!pool->new_threads) {
137
        return;
138
    }
139

    
140
    pool->new_threads--;
141
    pool->pending_threads++;
142

    
143
    qemu_thread_create(&t, worker_thread, pool, QEMU_THREAD_DETACHED);
144
}
145

    
146
static void spawn_thread_bh_fn(void *opaque)
147
{
148
    ThreadPool *pool = opaque;
149

    
150
    qemu_mutex_lock(&pool->lock);
151
    do_spawn_thread(pool);
152
    qemu_mutex_unlock(&pool->lock);
153
}
154

    
155
static void spawn_thread(ThreadPool *pool)
156
{
157
    pool->cur_threads++;
158
    pool->new_threads++;
159
    /* If there are threads being created, they will spawn new workers, so
160
     * we don't spend time creating many threads in a loop holding a mutex or
161
     * starving the current vcpu.
162
     *
163
     * If there are no idle threads, ask the main thread to create one, so we
164
     * inherit the correct affinity instead of the vcpu affinity.
165
     */
166
    if (!pool->pending_threads) {
167
        qemu_bh_schedule(pool->new_thread_bh);
168
    }
169
}
170

    
171
static void event_notifier_ready(EventNotifier *notifier)
172
{
173
    ThreadPool *pool = container_of(notifier, ThreadPool, notifier);
174
    ThreadPoolElement *elem, *next;
175

    
176
    event_notifier_test_and_clear(notifier);
177
restart:
178
    QLIST_FOREACH_SAFE(elem, &pool->head, all, next) {
179
        if (elem->state != THREAD_CANCELED && elem->state != THREAD_DONE) {
180
            continue;
181
        }
182
        if (elem->state == THREAD_DONE) {
183
            trace_thread_pool_complete(pool, elem, elem->common.opaque,
184
                                       elem->ret);
185
        }
186
        if (elem->state == THREAD_DONE && elem->common.cb) {
187
            QLIST_REMOVE(elem, all);
188
            /* Read state before ret.  */
189
            smp_rmb();
190
            elem->common.cb(elem->common.opaque, elem->ret);
191
            qemu_aio_release(elem);
192
            goto restart;
193
        } else {
194
            /* remove the request */
195
            QLIST_REMOVE(elem, all);
196
            qemu_aio_release(elem);
197
        }
198
    }
199
}
200

    
201
static int thread_pool_active(EventNotifier *notifier)
202
{
203
    ThreadPool *pool = container_of(notifier, ThreadPool, notifier);
204
    return !QLIST_EMPTY(&pool->head);
205
}
206

    
207
static void thread_pool_cancel(BlockDriverAIOCB *acb)
208
{
209
    ThreadPoolElement *elem = (ThreadPoolElement *)acb;
210
    ThreadPool *pool = elem->pool;
211

    
212
    trace_thread_pool_cancel(elem, elem->common.opaque);
213

    
214
    qemu_mutex_lock(&pool->lock);
215
    if (elem->state == THREAD_QUEUED &&
216
        /* No thread has yet started working on elem. we can try to "steal"
217
         * the item from the worker if we can get a signal from the
218
         * semaphore.  Because this is non-blocking, we can do it with
219
         * the lock taken and ensure that elem will remain THREAD_QUEUED.
220
         */
221
        qemu_sem_timedwait(&pool->sem, 0) == 0) {
222
        QTAILQ_REMOVE(&pool->request_list, elem, reqs);
223
        elem->state = THREAD_CANCELED;
224
        event_notifier_set(&pool->notifier);
225
    } else {
226
        pool->pending_cancellations++;
227
        while (elem->state != THREAD_CANCELED && elem->state != THREAD_DONE) {
228
            qemu_cond_wait(&pool->check_cancel, &pool->lock);
229
        }
230
        pool->pending_cancellations--;
231
    }
232
    qemu_mutex_unlock(&pool->lock);
233
}
234

    
235
static const AIOCBInfo thread_pool_aiocb_info = {
236
    .aiocb_size         = sizeof(ThreadPoolElement),
237
    .cancel             = thread_pool_cancel,
238
};
239

    
240
BlockDriverAIOCB *thread_pool_submit_aio(ThreadPoolFunc *func, void *arg,
241
        BlockDriverCompletionFunc *cb, void *opaque)
242
{
243
    ThreadPool *pool = &global_pool;
244
    ThreadPoolElement *req;
245

    
246
    req = qemu_aio_get(&thread_pool_aiocb_info, NULL, cb, opaque);
247
    req->func = func;
248
    req->arg = arg;
249
    req->state = THREAD_QUEUED;
250
    req->pool = pool;
251

    
252
    QLIST_INSERT_HEAD(&pool->head, req, all);
253

    
254
    trace_thread_pool_submit(pool, req, arg);
255

    
256
    qemu_mutex_lock(&pool->lock);
257
    if (pool->idle_threads == 0 && pool->cur_threads < pool->max_threads) {
258
        spawn_thread(pool);
259
    }
260
    QTAILQ_INSERT_TAIL(&pool->request_list, req, reqs);
261
    qemu_mutex_unlock(&pool->lock);
262
    qemu_sem_post(&pool->sem);
263
    return &req->common;
264
}
265

    
266
typedef struct ThreadPoolCo {
267
    Coroutine *co;
268
    int ret;
269
} ThreadPoolCo;
270

    
271
static void thread_pool_co_cb(void *opaque, int ret)
272
{
273
    ThreadPoolCo *co = opaque;
274

    
275
    co->ret = ret;
276
    qemu_coroutine_enter(co->co, NULL);
277
}
278

    
279
int coroutine_fn thread_pool_submit_co(ThreadPoolFunc *func, void *arg)
280
{
281
    ThreadPoolCo tpc = { .co = qemu_coroutine_self(), .ret = -EINPROGRESS };
282
    assert(qemu_in_coroutine());
283
    thread_pool_submit_aio(func, arg, thread_pool_co_cb, &tpc);
284
    qemu_coroutine_yield();
285
    return tpc.ret;
286
}
287

    
288
void thread_pool_submit(ThreadPoolFunc *func, void *arg)
289
{
290
    thread_pool_submit_aio(func, arg, NULL, NULL);
291
}
292

    
293
static void thread_pool_init_one(ThreadPool *pool, AioContext *ctx)
294
{
295
    if (!ctx) {
296
        ctx = qemu_get_aio_context();
297
    }
298

    
299
    memset(pool, 0, sizeof(*pool));
300
    event_notifier_init(&pool->notifier, false);
301
    qemu_mutex_init(&pool->lock);
302
    qemu_cond_init(&pool->check_cancel);
303
    qemu_sem_init(&pool->sem, 0);
304
    pool->max_threads = 64;
305
    pool->new_thread_bh = aio_bh_new(ctx, spawn_thread_bh_fn, pool);
306

    
307
    QLIST_INIT(&pool->head);
308
    QTAILQ_INIT(&pool->request_list);
309

    
310
    aio_set_event_notifier(ctx, &pool->notifier, event_notifier_ready,
311
                           thread_pool_active);
312
}
313

    
314
static void thread_pool_init(void)
315
{
316
    thread_pool_init_one(&global_pool, NULL);
317
}
318

    
319
block_init(thread_pool_init)