blob: 23888dcfc4d74d9c9343462271dd15cb6eaa926d [file] [log] [blame]
Paolo Bonzinid354c7e2012-02-23 13:23:34 +01001/*
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"
Paolo Bonzini1de7afc2012-12-17 18:20:00 +010018#include "qemu/queue.h"
19#include "qemu/thread.h"
20#include "qemu/osdep.h"
Paolo Bonzini737e1502012-12-17 18:19:44 +010021#include "block/coroutine.h"
Paolo Bonzinid354c7e2012-02-23 13:23:34 +010022#include "trace.h"
Paolo Bonzini737e1502012-12-17 18:19:44 +010023#include "block/block_int.h"
Paolo Bonzini737e1502012-12-17 18:19:44 +010024#include "block/thread-pool.h"
Alex Bligh6a1751b2013-08-21 16:02:47 +010025#include "qemu/main-loop.h"
Paolo Bonzinid354c7e2012-02-23 13:23:34 +010026
Stefan Hajnoczib8112032013-03-07 13:41:45 +010027static void do_spawn_thread(ThreadPool *pool);
Paolo Bonzinid354c7e2012-02-23 13:23:34 +010028
29typedef struct ThreadPoolElement ThreadPoolElement;
30
31enum ThreadState {
32 THREAD_QUEUED,
33 THREAD_ACTIVE,
34 THREAD_DONE,
35 THREAD_CANCELED,
36};
37
38struct ThreadPoolElement {
39 BlockDriverAIOCB common;
Stefan Hajnoczib8112032013-03-07 13:41:45 +010040 ThreadPool *pool;
Paolo Bonzinid354c7e2012-02-23 13:23:34 +010041 ThreadPoolFunc *func;
42 void *arg;
Paolo Bonzini19d092c2012-10-31 10:09:11 +010043
44 /* Moving state out of THREAD_QUEUED is protected by lock. After
45 * that, only the worker thread can write to it. Reads and writes
46 * of state and ret are ordered with memory barriers.
47 */
Paolo Bonzinid354c7e2012-02-23 13:23:34 +010048 enum ThreadState state;
49 int ret;
50
51 /* Access to this list is protected by lock. */
52 QTAILQ_ENTRY(ThreadPoolElement) reqs;
53
54 /* Access to this list is protected by the global mutex. */
55 QLIST_ENTRY(ThreadPoolElement) all;
56};
57
Stefan Hajnoczib8112032013-03-07 13:41:45 +010058struct ThreadPool {
Stefan Hajnoczif7311cc2013-03-07 13:41:46 +010059 AioContext *ctx;
Stefan Hajnoczic2e50e32014-07-15 16:44:25 +020060 QEMUBH *completion_bh;
Stefan Hajnoczib8112032013-03-07 13:41:45 +010061 QemuMutex lock;
62 QemuCond check_cancel;
Stefan Hajnoczif7311cc2013-03-07 13:41:46 +010063 QemuCond worker_stopped;
Stefan Hajnoczib8112032013-03-07 13:41:45 +010064 QemuSemaphore sem;
65 int max_threads;
66 QEMUBH *new_thread_bh;
Paolo Bonzinid354c7e2012-02-23 13:23:34 +010067
Stefan Hajnoczib8112032013-03-07 13:41:45 +010068 /* The following variables are only accessed from one AioContext. */
69 QLIST_HEAD(, ThreadPoolElement) head;
Paolo Bonzinid354c7e2012-02-23 13:23:34 +010070
Stefan Hajnoczib8112032013-03-07 13:41:45 +010071 /* 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 */
Stefan Hajnoczif7311cc2013-03-07 13:41:46 +010078 bool stopping;
Stefan Hajnoczib8112032013-03-07 13:41:45 +010079};
Paolo Bonzinid354c7e2012-02-23 13:23:34 +010080
Stefan Hajnoczib8112032013-03-07 13:41:45 +010081static void *worker_thread(void *opaque)
Paolo Bonzinid354c7e2012-02-23 13:23:34 +010082{
Stefan Hajnoczib8112032013-03-07 13:41:45 +010083 ThreadPool *pool = opaque;
84
85 qemu_mutex_lock(&pool->lock);
86 pool->pending_threads--;
87 do_spawn_thread(pool);
Paolo Bonzinid354c7e2012-02-23 13:23:34 +010088
Stefan Hajnoczif7311cc2013-03-07 13:41:46 +010089 while (!pool->stopping) {
Paolo Bonzinid354c7e2012-02-23 13:23:34 +010090 ThreadPoolElement *req;
91 int ret;
92
93 do {
Stefan Hajnoczib8112032013-03-07 13:41:45 +010094 pool->idle_threads++;
95 qemu_mutex_unlock(&pool->lock);
96 ret = qemu_sem_timedwait(&pool->sem, 10000);
97 qemu_mutex_lock(&pool->lock);
98 pool->idle_threads--;
99 } while (ret == -1 && !QTAILQ_EMPTY(&pool->request_list));
Stefan Hajnoczif7311cc2013-03-07 13:41:46 +0100100 if (ret == -1 || pool->stopping) {
Paolo Bonzinid354c7e2012-02-23 13:23:34 +0100101 break;
102 }
103
Stefan Hajnoczib8112032013-03-07 13:41:45 +0100104 req = QTAILQ_FIRST(&pool->request_list);
105 QTAILQ_REMOVE(&pool->request_list, req, reqs);
Paolo Bonzinid354c7e2012-02-23 13:23:34 +0100106 req->state = THREAD_ACTIVE;
Stefan Hajnoczib8112032013-03-07 13:41:45 +0100107 qemu_mutex_unlock(&pool->lock);
Paolo Bonzinid354c7e2012-02-23 13:23:34 +0100108
109 ret = req->func(req->arg);
110
Paolo Bonzinid354c7e2012-02-23 13:23:34 +0100111 req->ret = ret;
Paolo Bonzini19d092c2012-10-31 10:09:11 +0100112 /* Write ret before state. */
113 smp_wmb();
114 req->state = THREAD_DONE;
115
Stefan Hajnoczib8112032013-03-07 13:41:45 +0100116 qemu_mutex_lock(&pool->lock);
117 if (pool->pending_cancellations) {
118 qemu_cond_broadcast(&pool->check_cancel);
Paolo Bonzinid354c7e2012-02-23 13:23:34 +0100119 }
120
Stefan Hajnoczic2e50e32014-07-15 16:44:25 +0200121 qemu_bh_schedule(pool->completion_bh);
Paolo Bonzinid354c7e2012-02-23 13:23:34 +0100122 }
123
Stefan Hajnoczib8112032013-03-07 13:41:45 +0100124 pool->cur_threads--;
Stefan Hajnoczif7311cc2013-03-07 13:41:46 +0100125 qemu_cond_signal(&pool->worker_stopped);
Stefan Hajnoczib8112032013-03-07 13:41:45 +0100126 qemu_mutex_unlock(&pool->lock);
Paolo Bonzinid354c7e2012-02-23 13:23:34 +0100127 return NULL;
128}
129
Stefan Hajnoczib8112032013-03-07 13:41:45 +0100130static void do_spawn_thread(ThreadPool *pool)
Paolo Bonzinid354c7e2012-02-23 13:23:34 +0100131{
132 QemuThread t;
133
134 /* Runs with lock taken. */
Stefan Hajnoczib8112032013-03-07 13:41:45 +0100135 if (!pool->new_threads) {
Paolo Bonzinid354c7e2012-02-23 13:23:34 +0100136 return;
137 }
138
Stefan Hajnoczib8112032013-03-07 13:41:45 +0100139 pool->new_threads--;
140 pool->pending_threads++;
Paolo Bonzinid354c7e2012-02-23 13:23:34 +0100141
Dr. David Alan Gilbert49001162014-01-30 10:20:32 +0000142 qemu_thread_create(&t, "worker", worker_thread, pool, QEMU_THREAD_DETACHED);
Paolo Bonzinid354c7e2012-02-23 13:23:34 +0100143}
144
145static void spawn_thread_bh_fn(void *opaque)
146{
Stefan Hajnoczib8112032013-03-07 13:41:45 +0100147 ThreadPool *pool = opaque;
148
149 qemu_mutex_lock(&pool->lock);
150 do_spawn_thread(pool);
151 qemu_mutex_unlock(&pool->lock);
Paolo Bonzinid354c7e2012-02-23 13:23:34 +0100152}
153
Stefan Hajnoczib8112032013-03-07 13:41:45 +0100154static void spawn_thread(ThreadPool *pool)
Paolo Bonzinid354c7e2012-02-23 13:23:34 +0100155{
Stefan Hajnoczib8112032013-03-07 13:41:45 +0100156 pool->cur_threads++;
157 pool->new_threads++;
Paolo Bonzinid354c7e2012-02-23 13:23:34 +0100158 /* If there are threads being created, they will spawn new workers, so
159 * we don't spend time creating many threads in a loop holding a mutex or
160 * starving the current vcpu.
161 *
162 * If there are no idle threads, ask the main thread to create one, so we
163 * inherit the correct affinity instead of the vcpu affinity.
164 */
Stefan Hajnoczib8112032013-03-07 13:41:45 +0100165 if (!pool->pending_threads) {
166 qemu_bh_schedule(pool->new_thread_bh);
Paolo Bonzinid354c7e2012-02-23 13:23:34 +0100167 }
168}
169
Stefan Hajnoczic2e50e32014-07-15 16:44:25 +0200170static void thread_pool_completion_bh(void *opaque)
Paolo Bonzinid354c7e2012-02-23 13:23:34 +0100171{
Stefan Hajnoczic2e50e32014-07-15 16:44:25 +0200172 ThreadPool *pool = opaque;
Paolo Bonzinid354c7e2012-02-23 13:23:34 +0100173 ThreadPoolElement *elem, *next;
174
Paolo Bonzinid354c7e2012-02-23 13:23:34 +0100175restart:
Stefan Hajnoczib8112032013-03-07 13:41:45 +0100176 QLIST_FOREACH_SAFE(elem, &pool->head, all, next) {
Paolo Bonzinid354c7e2012-02-23 13:23:34 +0100177 if (elem->state != THREAD_CANCELED && elem->state != THREAD_DONE) {
178 continue;
179 }
180 if (elem->state == THREAD_DONE) {
Stefan Hajnoczib8112032013-03-07 13:41:45 +0100181 trace_thread_pool_complete(pool, elem, elem->common.opaque,
182 elem->ret);
Paolo Bonzinid354c7e2012-02-23 13:23:34 +0100183 }
184 if (elem->state == THREAD_DONE && elem->common.cb) {
Paolo Bonzinid354c7e2012-02-23 13:23:34 +0100185 QLIST_REMOVE(elem, all);
Paolo Bonzini19d092c2012-10-31 10:09:11 +0100186 /* Read state before ret. */
187 smp_rmb();
Stefan Hajnoczi3c80ca12014-07-15 16:44:26 +0200188
189 /* Schedule ourselves in case elem->common.cb() calls aio_poll() to
190 * wait for another request that completed at the same time.
191 */
192 qemu_bh_schedule(pool->completion_bh);
193
Paolo Bonzini19d092c2012-10-31 10:09:11 +0100194 elem->common.cb(elem->common.opaque, elem->ret);
Paolo Bonzinid354c7e2012-02-23 13:23:34 +0100195 qemu_aio_release(elem);
196 goto restart;
197 } else {
198 /* remove the request */
199 QLIST_REMOVE(elem, all);
200 qemu_aio_release(elem);
201 }
202 }
203}
204
Paolo Bonzinid354c7e2012-02-23 13:23:34 +0100205static void thread_pool_cancel(BlockDriverAIOCB *acb)
206{
207 ThreadPoolElement *elem = (ThreadPoolElement *)acb;
Stefan Hajnoczib8112032013-03-07 13:41:45 +0100208 ThreadPool *pool = elem->pool;
Paolo Bonzinid354c7e2012-02-23 13:23:34 +0100209
210 trace_thread_pool_cancel(elem, elem->common.opaque);
211
Stefan Hajnoczib8112032013-03-07 13:41:45 +0100212 qemu_mutex_lock(&pool->lock);
Paolo Bonzinid354c7e2012-02-23 13:23:34 +0100213 if (elem->state == THREAD_QUEUED &&
214 /* No thread has yet started working on elem. we can try to "steal"
215 * the item from the worker if we can get a signal from the
216 * semaphore. Because this is non-blocking, we can do it with
217 * the lock taken and ensure that elem will remain THREAD_QUEUED.
218 */
Stefan Hajnoczib8112032013-03-07 13:41:45 +0100219 qemu_sem_timedwait(&pool->sem, 0) == 0) {
220 QTAILQ_REMOVE(&pool->request_list, elem, reqs);
Paolo Bonzinid354c7e2012-02-23 13:23:34 +0100221 elem->state = THREAD_CANCELED;
Stefan Hajnoczic2e50e32014-07-15 16:44:25 +0200222 qemu_bh_schedule(pool->completion_bh);
Paolo Bonzinid354c7e2012-02-23 13:23:34 +0100223 } else {
Stefan Hajnoczib8112032013-03-07 13:41:45 +0100224 pool->pending_cancellations++;
Paolo Bonzinid354c7e2012-02-23 13:23:34 +0100225 while (elem->state != THREAD_CANCELED && elem->state != THREAD_DONE) {
Stefan Hajnoczib8112032013-03-07 13:41:45 +0100226 qemu_cond_wait(&pool->check_cancel, &pool->lock);
Paolo Bonzinid354c7e2012-02-23 13:23:34 +0100227 }
Stefan Hajnoczib8112032013-03-07 13:41:45 +0100228 pool->pending_cancellations--;
Paolo Bonzinid354c7e2012-02-23 13:23:34 +0100229 }
Stefan Hajnoczib8112032013-03-07 13:41:45 +0100230 qemu_mutex_unlock(&pool->lock);
Stefan Hajnoczic2e50e32014-07-15 16:44:25 +0200231 thread_pool_completion_bh(pool);
Paolo Bonzinid354c7e2012-02-23 13:23:34 +0100232}
233
Stefan Hajnoczid7331be2012-10-31 16:34:37 +0100234static const AIOCBInfo thread_pool_aiocb_info = {
Paolo Bonzinid354c7e2012-02-23 13:23:34 +0100235 .aiocb_size = sizeof(ThreadPoolElement),
236 .cancel = thread_pool_cancel,
237};
238
Stefan Hajnoczic4d9d192013-03-07 13:41:49 +0100239BlockDriverAIOCB *thread_pool_submit_aio(ThreadPool *pool,
240 ThreadPoolFunc *func, void *arg,
Paolo Bonzinid354c7e2012-02-23 13:23:34 +0100241 BlockDriverCompletionFunc *cb, void *opaque)
242{
243 ThreadPoolElement *req;
244
Stefan Hajnoczid7331be2012-10-31 16:34:37 +0100245 req = qemu_aio_get(&thread_pool_aiocb_info, NULL, cb, opaque);
Paolo Bonzinid354c7e2012-02-23 13:23:34 +0100246 req->func = func;
247 req->arg = arg;
248 req->state = THREAD_QUEUED;
Stefan Hajnoczib8112032013-03-07 13:41:45 +0100249 req->pool = pool;
Paolo Bonzinid354c7e2012-02-23 13:23:34 +0100250
Stefan Hajnoczib8112032013-03-07 13:41:45 +0100251 QLIST_INSERT_HEAD(&pool->head, req, all);
Paolo Bonzinid354c7e2012-02-23 13:23:34 +0100252
Stefan Hajnoczib8112032013-03-07 13:41:45 +0100253 trace_thread_pool_submit(pool, req, arg);
Paolo Bonzinid354c7e2012-02-23 13:23:34 +0100254
Stefan Hajnoczib8112032013-03-07 13:41:45 +0100255 qemu_mutex_lock(&pool->lock);
256 if (pool->idle_threads == 0 && pool->cur_threads < pool->max_threads) {
257 spawn_thread(pool);
Paolo Bonzinid354c7e2012-02-23 13:23:34 +0100258 }
Stefan Hajnoczib8112032013-03-07 13:41:45 +0100259 QTAILQ_INSERT_TAIL(&pool->request_list, req, reqs);
260 qemu_mutex_unlock(&pool->lock);
261 qemu_sem_post(&pool->sem);
Paolo Bonzinid354c7e2012-02-23 13:23:34 +0100262 return &req->common;
263}
264
265typedef struct ThreadPoolCo {
266 Coroutine *co;
267 int ret;
268} ThreadPoolCo;
269
270static void thread_pool_co_cb(void *opaque, int ret)
271{
272 ThreadPoolCo *co = opaque;
273
274 co->ret = ret;
275 qemu_coroutine_enter(co->co, NULL);
276}
277
Stefan Hajnoczic4d9d192013-03-07 13:41:49 +0100278int coroutine_fn thread_pool_submit_co(ThreadPool *pool, ThreadPoolFunc *func,
279 void *arg)
Paolo Bonzinid354c7e2012-02-23 13:23:34 +0100280{
281 ThreadPoolCo tpc = { .co = qemu_coroutine_self(), .ret = -EINPROGRESS };
282 assert(qemu_in_coroutine());
Stefan Hajnoczic4d9d192013-03-07 13:41:49 +0100283 thread_pool_submit_aio(pool, func, arg, thread_pool_co_cb, &tpc);
Paolo Bonzinid354c7e2012-02-23 13:23:34 +0100284 qemu_coroutine_yield();
285 return tpc.ret;
286}
287
Stefan Hajnoczic4d9d192013-03-07 13:41:49 +0100288void thread_pool_submit(ThreadPool *pool, ThreadPoolFunc *func, void *arg)
Paolo Bonzinid354c7e2012-02-23 13:23:34 +0100289{
Stefan Hajnoczic4d9d192013-03-07 13:41:49 +0100290 thread_pool_submit_aio(pool, func, arg, NULL, NULL);
Paolo Bonzinid354c7e2012-02-23 13:23:34 +0100291}
292
Stefan Hajnoczib8112032013-03-07 13:41:45 +0100293static 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));
Stefan Hajnoczif7311cc2013-03-07 13:41:46 +0100300 pool->ctx = ctx;
Stefan Hajnoczic2e50e32014-07-15 16:44:25 +0200301 pool->completion_bh = aio_bh_new(ctx, thread_pool_completion_bh, pool);
Stefan Hajnoczib8112032013-03-07 13:41:45 +0100302 qemu_mutex_init(&pool->lock);
303 qemu_cond_init(&pool->check_cancel);
Stefan Hajnoczif7311cc2013-03-07 13:41:46 +0100304 qemu_cond_init(&pool->worker_stopped);
Stefan Hajnoczib8112032013-03-07 13:41:45 +0100305 qemu_sem_init(&pool->sem, 0);
306 pool->max_threads = 64;
307 pool->new_thread_bh = aio_bh_new(ctx, spawn_thread_bh_fn, pool);
308
309 QLIST_INIT(&pool->head);
310 QTAILQ_INIT(&pool->request_list);
Stefan Hajnoczib8112032013-03-07 13:41:45 +0100311}
312
Stefan Hajnoczif7311cc2013-03-07 13:41:46 +0100313ThreadPool *thread_pool_new(AioContext *ctx)
314{
315 ThreadPool *pool = g_new(ThreadPool, 1);
316 thread_pool_init_one(pool, ctx);
317 return pool;
318}
319
320void thread_pool_free(ThreadPool *pool)
321{
322 if (!pool) {
323 return;
324 }
325
326 assert(QLIST_EMPTY(&pool->head));
327
328 qemu_mutex_lock(&pool->lock);
329
330 /* Stop new threads from spawning */
331 qemu_bh_delete(pool->new_thread_bh);
332 pool->cur_threads -= pool->new_threads;
333 pool->new_threads = 0;
334
335 /* Wait for worker threads to terminate */
336 pool->stopping = true;
337 while (pool->cur_threads > 0) {
338 qemu_sem_post(&pool->sem);
339 qemu_cond_wait(&pool->worker_stopped, &pool->lock);
340 }
341
342 qemu_mutex_unlock(&pool->lock);
343
Stefan Hajnoczic2e50e32014-07-15 16:44:25 +0200344 qemu_bh_delete(pool->completion_bh);
Stefan Hajnoczif7311cc2013-03-07 13:41:46 +0100345 qemu_sem_destroy(&pool->sem);
346 qemu_cond_destroy(&pool->check_cancel);
347 qemu_cond_destroy(&pool->worker_stopped);
348 qemu_mutex_destroy(&pool->lock);
Stefan Hajnoczif7311cc2013-03-07 13:41:46 +0100349 g_free(pool);
350}