blob: 610646d1314ed6f174587805d3674ffdc7535db5 [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 */
Peter Maydelld38ea872016-01-29 17:50:05 +000017#include "qemu/osdep.h"
Paolo Bonzinid354c7e2012-02-23 13:23:34 +010018#include "qemu-common.h"
Paolo Bonzini1de7afc2012-12-17 18:20:00 +010019#include "qemu/queue.h"
20#include "qemu/thread.h"
Daniel P. Berrange10817bf2015-09-01 14:48:02 +010021#include "qemu/coroutine.h"
Paolo Bonzinic2b38b22017-02-13 14:52:18 +010022#include "trace.h"
Paolo Bonzini737e1502012-12-17 18:19:44 +010023#include "block/thread-pool.h"
Alex Bligh6a1751b2013-08-21 16:02:47 +010024#include "qemu/main-loop.h"
Paolo Bonzinid354c7e2012-02-23 13:23:34 +010025
Stefan Hajnoczib8112032013-03-07 13:41:45 +010026static void do_spawn_thread(ThreadPool *pool);
Paolo Bonzinid354c7e2012-02-23 13:23:34 +010027
28typedef struct ThreadPoolElement ThreadPoolElement;
29
30enum ThreadState {
31 THREAD_QUEUED,
32 THREAD_ACTIVE,
33 THREAD_DONE,
Paolo Bonzinid354c7e2012-02-23 13:23:34 +010034};
35
36struct ThreadPoolElement {
Markus Armbruster7c84b1b2014-10-07 13:59:14 +020037 BlockAIOCB common;
Stefan Hajnoczib8112032013-03-07 13:41:45 +010038 ThreadPool *pool;
Paolo Bonzinid354c7e2012-02-23 13:23:34 +010039 ThreadPoolFunc *func;
40 void *arg;
Paolo Bonzini19d092c2012-10-31 10:09:11 +010041
42 /* Moving state out of THREAD_QUEUED is protected by lock. After
43 * that, only the worker thread can write to it. Reads and writes
44 * of state and ret are ordered with memory barriers.
45 */
Paolo Bonzinid354c7e2012-02-23 13:23:34 +010046 enum ThreadState state;
47 int ret;
48
49 /* Access to this list is protected by lock. */
50 QTAILQ_ENTRY(ThreadPoolElement) reqs;
51
52 /* Access to this list is protected by the global mutex. */
53 QLIST_ENTRY(ThreadPoolElement) all;
54};
55
Stefan Hajnoczib8112032013-03-07 13:41:45 +010056struct ThreadPool {
Stefan Hajnoczif7311cc2013-03-07 13:41:46 +010057 AioContext *ctx;
Stefan Hajnoczic2e50e32014-07-15 16:44:25 +020058 QEMUBH *completion_bh;
Stefan Hajnoczib8112032013-03-07 13:41:45 +010059 QemuMutex lock;
Stefan Hajnoczif7311cc2013-03-07 13:41:46 +010060 QemuCond worker_stopped;
Stefan Hajnoczib8112032013-03-07 13:41:45 +010061 QemuSemaphore sem;
62 int max_threads;
63 QEMUBH *new_thread_bh;
Paolo Bonzinid354c7e2012-02-23 13:23:34 +010064
Stefan Hajnoczib8112032013-03-07 13:41:45 +010065 /* The following variables are only accessed from one AioContext. */
66 QLIST_HEAD(, ThreadPoolElement) head;
Paolo Bonzinid354c7e2012-02-23 13:23:34 +010067
Stefan Hajnoczib8112032013-03-07 13:41:45 +010068 /* The following variables are protected by lock. */
69 QTAILQ_HEAD(, ThreadPoolElement) request_list;
70 int cur_threads;
71 int idle_threads;
72 int new_threads; /* backlog of threads we need to create */
73 int pending_threads; /* threads created but not running yet */
Stefan Hajnoczif7311cc2013-03-07 13:41:46 +010074 bool stopping;
Stefan Hajnoczib8112032013-03-07 13:41:45 +010075};
Paolo Bonzinid354c7e2012-02-23 13:23:34 +010076
Stefan Hajnoczib8112032013-03-07 13:41:45 +010077static void *worker_thread(void *opaque)
Paolo Bonzinid354c7e2012-02-23 13:23:34 +010078{
Stefan Hajnoczib8112032013-03-07 13:41:45 +010079 ThreadPool *pool = opaque;
80
81 qemu_mutex_lock(&pool->lock);
82 pool->pending_threads--;
83 do_spawn_thread(pool);
Paolo Bonzinid354c7e2012-02-23 13:23:34 +010084
Stefan Hajnoczif7311cc2013-03-07 13:41:46 +010085 while (!pool->stopping) {
Paolo Bonzinid354c7e2012-02-23 13:23:34 +010086 ThreadPoolElement *req;
87 int ret;
88
89 do {
Stefan Hajnoczib8112032013-03-07 13:41:45 +010090 pool->idle_threads++;
91 qemu_mutex_unlock(&pool->lock);
92 ret = qemu_sem_timedwait(&pool->sem, 10000);
93 qemu_mutex_lock(&pool->lock);
94 pool->idle_threads--;
95 } while (ret == -1 && !QTAILQ_EMPTY(&pool->request_list));
Stefan Hajnoczif7311cc2013-03-07 13:41:46 +010096 if (ret == -1 || pool->stopping) {
Paolo Bonzinid354c7e2012-02-23 13:23:34 +010097 break;
98 }
99
Stefan Hajnoczib8112032013-03-07 13:41:45 +0100100 req = QTAILQ_FIRST(&pool->request_list);
101 QTAILQ_REMOVE(&pool->request_list, req, reqs);
Paolo Bonzinid354c7e2012-02-23 13:23:34 +0100102 req->state = THREAD_ACTIVE;
Stefan Hajnoczib8112032013-03-07 13:41:45 +0100103 qemu_mutex_unlock(&pool->lock);
Paolo Bonzinid354c7e2012-02-23 13:23:34 +0100104
105 ret = req->func(req->arg);
106
Paolo Bonzinid354c7e2012-02-23 13:23:34 +0100107 req->ret = ret;
Paolo Bonzini19d092c2012-10-31 10:09:11 +0100108 /* Write ret before state. */
109 smp_wmb();
110 req->state = THREAD_DONE;
111
Stefan Hajnoczib8112032013-03-07 13:41:45 +0100112 qemu_mutex_lock(&pool->lock);
Paolo Bonzinid354c7e2012-02-23 13:23:34 +0100113
Stefan Hajnoczic2e50e32014-07-15 16:44:25 +0200114 qemu_bh_schedule(pool->completion_bh);
Paolo Bonzinid354c7e2012-02-23 13:23:34 +0100115 }
116
Stefan Hajnoczib8112032013-03-07 13:41:45 +0100117 pool->cur_threads--;
Stefan Hajnoczif7311cc2013-03-07 13:41:46 +0100118 qemu_cond_signal(&pool->worker_stopped);
Stefan Hajnoczib8112032013-03-07 13:41:45 +0100119 qemu_mutex_unlock(&pool->lock);
Paolo Bonzinid354c7e2012-02-23 13:23:34 +0100120 return NULL;
121}
122
Stefan Hajnoczib8112032013-03-07 13:41:45 +0100123static void do_spawn_thread(ThreadPool *pool)
Paolo Bonzinid354c7e2012-02-23 13:23:34 +0100124{
125 QemuThread t;
126
127 /* Runs with lock taken. */
Stefan Hajnoczib8112032013-03-07 13:41:45 +0100128 if (!pool->new_threads) {
Paolo Bonzinid354c7e2012-02-23 13:23:34 +0100129 return;
130 }
131
Stefan Hajnoczib8112032013-03-07 13:41:45 +0100132 pool->new_threads--;
133 pool->pending_threads++;
Paolo Bonzinid354c7e2012-02-23 13:23:34 +0100134
Dr. David Alan Gilbert49001162014-01-30 10:20:32 +0000135 qemu_thread_create(&t, "worker", worker_thread, pool, QEMU_THREAD_DETACHED);
Paolo Bonzinid354c7e2012-02-23 13:23:34 +0100136}
137
138static void spawn_thread_bh_fn(void *opaque)
139{
Stefan Hajnoczib8112032013-03-07 13:41:45 +0100140 ThreadPool *pool = opaque;
141
142 qemu_mutex_lock(&pool->lock);
143 do_spawn_thread(pool);
144 qemu_mutex_unlock(&pool->lock);
Paolo Bonzinid354c7e2012-02-23 13:23:34 +0100145}
146
Stefan Hajnoczib8112032013-03-07 13:41:45 +0100147static void spawn_thread(ThreadPool *pool)
Paolo Bonzinid354c7e2012-02-23 13:23:34 +0100148{
Stefan Hajnoczib8112032013-03-07 13:41:45 +0100149 pool->cur_threads++;
150 pool->new_threads++;
Paolo Bonzinid354c7e2012-02-23 13:23:34 +0100151 /* If there are threads being created, they will spawn new workers, so
152 * we don't spend time creating many threads in a loop holding a mutex or
153 * starving the current vcpu.
154 *
155 * If there are no idle threads, ask the main thread to create one, so we
156 * inherit the correct affinity instead of the vcpu affinity.
157 */
Stefan Hajnoczib8112032013-03-07 13:41:45 +0100158 if (!pool->pending_threads) {
159 qemu_bh_schedule(pool->new_thread_bh);
Paolo Bonzinid354c7e2012-02-23 13:23:34 +0100160 }
161}
162
Stefan Hajnoczic2e50e32014-07-15 16:44:25 +0200163static void thread_pool_completion_bh(void *opaque)
Paolo Bonzinid354c7e2012-02-23 13:23:34 +0100164{
Stefan Hajnoczic2e50e32014-07-15 16:44:25 +0200165 ThreadPool *pool = opaque;
Paolo Bonzinid354c7e2012-02-23 13:23:34 +0100166 ThreadPoolElement *elem, *next;
167
Paolo Bonzini19196312017-02-13 14:52:31 +0100168 aio_context_acquire(pool->ctx);
Paolo Bonzinid354c7e2012-02-23 13:23:34 +0100169restart:
Stefan Hajnoczib8112032013-03-07 13:41:45 +0100170 QLIST_FOREACH_SAFE(elem, &pool->head, all, next) {
Fam Zheng3391f5e2014-09-11 13:41:12 +0800171 if (elem->state != THREAD_DONE) {
Paolo Bonzinid354c7e2012-02-23 13:23:34 +0100172 continue;
173 }
Stefan Hajnoczi1faa5bb2015-04-02 17:39:22 +0100174
175 trace_thread_pool_complete(pool, elem, elem->common.opaque,
176 elem->ret);
177 QLIST_REMOVE(elem, all);
178
179 if (elem->common.cb) {
Paolo Bonzini19d092c2012-10-31 10:09:11 +0100180 /* Read state before ret. */
181 smp_rmb();
Stefan Hajnoczi3c80ca12014-07-15 16:44:26 +0200182
183 /* Schedule ourselves in case elem->common.cb() calls aio_poll() to
184 * wait for another request that completed at the same time.
185 */
186 qemu_bh_schedule(pool->completion_bh);
187
Paolo Bonzinib9e413d2017-02-13 14:52:32 +0100188 aio_context_release(pool->ctx);
Paolo Bonzini19d092c2012-10-31 10:09:11 +0100189 elem->common.cb(elem->common.opaque, elem->ret);
Paolo Bonzinib9e413d2017-02-13 14:52:32 +0100190 aio_context_acquire(pool->ctx);
Peter Lievenb7a745d2017-03-16 17:02:49 +0100191
192 /* We can safely cancel the completion_bh here regardless of someone
193 * else having scheduled it meanwhile because we reenter the
194 * completion function anyway (goto restart).
195 */
196 qemu_bh_cancel(pool->completion_bh);
197
Fam Zheng80074292014-09-11 13:41:28 +0800198 qemu_aio_unref(elem);
Paolo Bonzinid354c7e2012-02-23 13:23:34 +0100199 goto restart;
200 } else {
Fam Zheng80074292014-09-11 13:41:28 +0800201 qemu_aio_unref(elem);
Paolo Bonzinid354c7e2012-02-23 13:23:34 +0100202 }
203 }
Paolo Bonzini19196312017-02-13 14:52:31 +0100204 aio_context_release(pool->ctx);
Paolo Bonzinid354c7e2012-02-23 13:23:34 +0100205}
206
Markus Armbruster7c84b1b2014-10-07 13:59:14 +0200207static void thread_pool_cancel(BlockAIOCB *acb)
Paolo Bonzinid354c7e2012-02-23 13:23:34 +0100208{
209 ThreadPoolElement *elem = (ThreadPoolElement *)acb;
Stefan Hajnoczib8112032013-03-07 13:41:45 +0100210 ThreadPool *pool = elem->pool;
Paolo Bonzinid354c7e2012-02-23 13:23:34 +0100211
212 trace_thread_pool_cancel(elem, elem->common.opaque);
213
Stefan Hajnoczib8112032013-03-07 13:41:45 +0100214 qemu_mutex_lock(&pool->lock);
Paolo Bonzinid354c7e2012-02-23 13:23:34 +0100215 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 */
Stefan Hajnoczib8112032013-03-07 13:41:45 +0100221 qemu_sem_timedwait(&pool->sem, 0) == 0) {
222 QTAILQ_REMOVE(&pool->request_list, elem, reqs);
Stefan Hajnoczic2e50e32014-07-15 16:44:25 +0200223 qemu_bh_schedule(pool->completion_bh);
Fam Zheng3391f5e2014-09-11 13:41:12 +0800224
225 elem->state = THREAD_DONE;
226 elem->ret = -ECANCELED;
Paolo Bonzinid354c7e2012-02-23 13:23:34 +0100227 }
Fam Zheng3391f5e2014-09-11 13:41:12 +0800228
Stefan Hajnoczib8112032013-03-07 13:41:45 +0100229 qemu_mutex_unlock(&pool->lock);
Fam Zheng3391f5e2014-09-11 13:41:12 +0800230}
231
Markus Armbruster7c84b1b2014-10-07 13:59:14 +0200232static AioContext *thread_pool_get_aio_context(BlockAIOCB *acb)
Fam Zheng3391f5e2014-09-11 13:41:12 +0800233{
234 ThreadPoolElement *elem = (ThreadPoolElement *)acb;
235 ThreadPool *pool = elem->pool;
236 return pool->ctx;
Paolo Bonzinid354c7e2012-02-23 13:23:34 +0100237}
238
Stefan Hajnoczid7331be2012-10-31 16:34:37 +0100239static const AIOCBInfo thread_pool_aiocb_info = {
Paolo Bonzinid354c7e2012-02-23 13:23:34 +0100240 .aiocb_size = sizeof(ThreadPoolElement),
Fam Zheng3391f5e2014-09-11 13:41:12 +0800241 .cancel_async = thread_pool_cancel,
242 .get_aio_context = thread_pool_get_aio_context,
Paolo Bonzinid354c7e2012-02-23 13:23:34 +0100243};
244
Markus Armbruster7c84b1b2014-10-07 13:59:14 +0200245BlockAIOCB *thread_pool_submit_aio(ThreadPool *pool,
Stefan Hajnoczic4d9d192013-03-07 13:41:49 +0100246 ThreadPoolFunc *func, void *arg,
Markus Armbruster097310b2014-10-07 13:59:15 +0200247 BlockCompletionFunc *cb, void *opaque)
Paolo Bonzinid354c7e2012-02-23 13:23:34 +0100248{
249 ThreadPoolElement *req;
250
Stefan Hajnoczid7331be2012-10-31 16:34:37 +0100251 req = qemu_aio_get(&thread_pool_aiocb_info, NULL, cb, opaque);
Paolo Bonzinid354c7e2012-02-23 13:23:34 +0100252 req->func = func;
253 req->arg = arg;
254 req->state = THREAD_QUEUED;
Stefan Hajnoczib8112032013-03-07 13:41:45 +0100255 req->pool = pool;
Paolo Bonzinid354c7e2012-02-23 13:23:34 +0100256
Stefan Hajnoczib8112032013-03-07 13:41:45 +0100257 QLIST_INSERT_HEAD(&pool->head, req, all);
Paolo Bonzinid354c7e2012-02-23 13:23:34 +0100258
Stefan Hajnoczib8112032013-03-07 13:41:45 +0100259 trace_thread_pool_submit(pool, req, arg);
Paolo Bonzinid354c7e2012-02-23 13:23:34 +0100260
Stefan Hajnoczib8112032013-03-07 13:41:45 +0100261 qemu_mutex_lock(&pool->lock);
262 if (pool->idle_threads == 0 && pool->cur_threads < pool->max_threads) {
263 spawn_thread(pool);
Paolo Bonzinid354c7e2012-02-23 13:23:34 +0100264 }
Stefan Hajnoczib8112032013-03-07 13:41:45 +0100265 QTAILQ_INSERT_TAIL(&pool->request_list, req, reqs);
266 qemu_mutex_unlock(&pool->lock);
267 qemu_sem_post(&pool->sem);
Paolo Bonzinid354c7e2012-02-23 13:23:34 +0100268 return &req->common;
269}
270
271typedef struct ThreadPoolCo {
272 Coroutine *co;
273 int ret;
274} ThreadPoolCo;
275
276static void thread_pool_co_cb(void *opaque, int ret)
277{
278 ThreadPoolCo *co = opaque;
279
280 co->ret = ret;
Paolo Bonzinib9e413d2017-02-13 14:52:32 +0100281 aio_co_wake(co->co);
Paolo Bonzinid354c7e2012-02-23 13:23:34 +0100282}
283
Stefan Hajnoczic4d9d192013-03-07 13:41:49 +0100284int coroutine_fn thread_pool_submit_co(ThreadPool *pool, ThreadPoolFunc *func,
285 void *arg)
Paolo Bonzinid354c7e2012-02-23 13:23:34 +0100286{
287 ThreadPoolCo tpc = { .co = qemu_coroutine_self(), .ret = -EINPROGRESS };
288 assert(qemu_in_coroutine());
Stefan Hajnoczic4d9d192013-03-07 13:41:49 +0100289 thread_pool_submit_aio(pool, func, arg, thread_pool_co_cb, &tpc);
Paolo Bonzinid354c7e2012-02-23 13:23:34 +0100290 qemu_coroutine_yield();
291 return tpc.ret;
292}
293
Stefan Hajnoczic4d9d192013-03-07 13:41:49 +0100294void thread_pool_submit(ThreadPool *pool, ThreadPoolFunc *func, void *arg)
Paolo Bonzinid354c7e2012-02-23 13:23:34 +0100295{
Stefan Hajnoczic4d9d192013-03-07 13:41:49 +0100296 thread_pool_submit_aio(pool, func, arg, NULL, NULL);
Paolo Bonzinid354c7e2012-02-23 13:23:34 +0100297}
298
Stefan Hajnoczib8112032013-03-07 13:41:45 +0100299static void thread_pool_init_one(ThreadPool *pool, AioContext *ctx)
300{
301 if (!ctx) {
302 ctx = qemu_get_aio_context();
303 }
304
305 memset(pool, 0, sizeof(*pool));
Stefan Hajnoczif7311cc2013-03-07 13:41:46 +0100306 pool->ctx = ctx;
Stefan Hajnoczic2e50e32014-07-15 16:44:25 +0200307 pool->completion_bh = aio_bh_new(ctx, thread_pool_completion_bh, pool);
Stefan Hajnoczib8112032013-03-07 13:41:45 +0100308 qemu_mutex_init(&pool->lock);
Stefan Hajnoczif7311cc2013-03-07 13:41:46 +0100309 qemu_cond_init(&pool->worker_stopped);
Stefan Hajnoczib8112032013-03-07 13:41:45 +0100310 qemu_sem_init(&pool->sem, 0);
311 pool->max_threads = 64;
312 pool->new_thread_bh = aio_bh_new(ctx, spawn_thread_bh_fn, pool);
313
314 QLIST_INIT(&pool->head);
315 QTAILQ_INIT(&pool->request_list);
Stefan Hajnoczib8112032013-03-07 13:41:45 +0100316}
317
Stefan Hajnoczif7311cc2013-03-07 13:41:46 +0100318ThreadPool *thread_pool_new(AioContext *ctx)
319{
320 ThreadPool *pool = g_new(ThreadPool, 1);
321 thread_pool_init_one(pool, ctx);
322 return pool;
323}
324
325void thread_pool_free(ThreadPool *pool)
326{
327 if (!pool) {
328 return;
329 }
330
331 assert(QLIST_EMPTY(&pool->head));
332
333 qemu_mutex_lock(&pool->lock);
334
335 /* Stop new threads from spawning */
336 qemu_bh_delete(pool->new_thread_bh);
337 pool->cur_threads -= pool->new_threads;
338 pool->new_threads = 0;
339
340 /* Wait for worker threads to terminate */
341 pool->stopping = true;
342 while (pool->cur_threads > 0) {
343 qemu_sem_post(&pool->sem);
344 qemu_cond_wait(&pool->worker_stopped, &pool->lock);
345 }
346
347 qemu_mutex_unlock(&pool->lock);
348
Stefan Hajnoczic2e50e32014-07-15 16:44:25 +0200349 qemu_bh_delete(pool->completion_bh);
Stefan Hajnoczif7311cc2013-03-07 13:41:46 +0100350 qemu_sem_destroy(&pool->sem);
Stefan Hajnoczif7311cc2013-03-07 13:41:46 +0100351 qemu_cond_destroy(&pool->worker_stopped);
352 qemu_mutex_destroy(&pool->lock);
Stefan Hajnoczif7311cc2013-03-07 13:41:46 +0100353 g_free(pool);
354}