blob: d763cea505b68575af8e1b39cd95d02caa73801a [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 Bonzini1de7afc2012-12-17 18:20:00 +010018#include "qemu/queue.h"
19#include "qemu/thread.h"
Daniel P. Berrange10817bf2015-09-01 14:48:02 +010020#include "qemu/coroutine.h"
Paolo Bonzinic2b38b22017-02-13 14:52:18 +010021#include "trace.h"
Paolo Bonzini737e1502012-12-17 18:19:44 +010022#include "block/thread-pool.h"
Alex Bligh6a1751b2013-08-21 16:02:47 +010023#include "qemu/main-loop.h"
Paolo Bonzinid354c7e2012-02-23 13:23:34 +010024
Stefan Hajnoczib8112032013-03-07 13:41:45 +010025static void do_spawn_thread(ThreadPool *pool);
Paolo Bonzinid354c7e2012-02-23 13:23:34 +010026
27typedef struct ThreadPoolElement ThreadPoolElement;
28
29enum ThreadState {
30 THREAD_QUEUED,
31 THREAD_ACTIVE,
32 THREAD_DONE,
Paolo Bonzinid354c7e2012-02-23 13:23:34 +010033};
34
35struct ThreadPoolElement {
Markus Armbruster7c84b1b2014-10-07 13:59:14 +020036 BlockAIOCB common;
Stefan Hajnoczib8112032013-03-07 13:41:45 +010037 ThreadPool *pool;
Paolo Bonzinid354c7e2012-02-23 13:23:34 +010038 ThreadPoolFunc *func;
39 void *arg;
Paolo Bonzini19d092c2012-10-31 10:09:11 +010040
41 /* Moving state out of THREAD_QUEUED is protected by lock. After
42 * that, only the worker thread can write to it. Reads and writes
43 * of state and ret are ordered with memory barriers.
44 */
Paolo Bonzinid354c7e2012-02-23 13:23:34 +010045 enum ThreadState state;
46 int ret;
47
48 /* Access to this list is protected by lock. */
49 QTAILQ_ENTRY(ThreadPoolElement) reqs;
50
51 /* Access to this list is protected by the global mutex. */
52 QLIST_ENTRY(ThreadPoolElement) all;
53};
54
Stefan Hajnoczib8112032013-03-07 13:41:45 +010055struct ThreadPool {
Stefan Hajnoczif7311cc2013-03-07 13:41:46 +010056 AioContext *ctx;
Stefan Hajnoczic2e50e32014-07-15 16:44:25 +020057 QEMUBH *completion_bh;
Stefan Hajnoczib8112032013-03-07 13:41:45 +010058 QemuMutex lock;
Stefan Hajnoczif7311cc2013-03-07 13:41:46 +010059 QemuCond worker_stopped;
Stefan Hajnoczib8112032013-03-07 13:41:45 +010060 QemuSemaphore sem;
61 int max_threads;
62 QEMUBH *new_thread_bh;
Paolo Bonzinid354c7e2012-02-23 13:23:34 +010063
Stefan Hajnoczib8112032013-03-07 13:41:45 +010064 /* The following variables are only accessed from one AioContext. */
65 QLIST_HEAD(, ThreadPoolElement) head;
Paolo Bonzinid354c7e2012-02-23 13:23:34 +010066
Stefan Hajnoczib8112032013-03-07 13:41:45 +010067 /* The following variables are protected by lock. */
68 QTAILQ_HEAD(, ThreadPoolElement) request_list;
69 int cur_threads;
70 int idle_threads;
71 int new_threads; /* backlog of threads we need to create */
72 int pending_threads; /* threads created but not running yet */
Stefan Hajnoczif7311cc2013-03-07 13:41:46 +010073 bool stopping;
Stefan Hajnoczib8112032013-03-07 13:41:45 +010074};
Paolo Bonzinid354c7e2012-02-23 13:23:34 +010075
Stefan Hajnoczib8112032013-03-07 13:41:45 +010076static void *worker_thread(void *opaque)
Paolo Bonzinid354c7e2012-02-23 13:23:34 +010077{
Stefan Hajnoczib8112032013-03-07 13:41:45 +010078 ThreadPool *pool = opaque;
79
80 qemu_mutex_lock(&pool->lock);
81 pool->pending_threads--;
82 do_spawn_thread(pool);
Paolo Bonzinid354c7e2012-02-23 13:23:34 +010083
Stefan Hajnoczif7311cc2013-03-07 13:41:46 +010084 while (!pool->stopping) {
Paolo Bonzinid354c7e2012-02-23 13:23:34 +010085 ThreadPoolElement *req;
86 int ret;
87
88 do {
Stefan Hajnoczib8112032013-03-07 13:41:45 +010089 pool->idle_threads++;
90 qemu_mutex_unlock(&pool->lock);
91 ret = qemu_sem_timedwait(&pool->sem, 10000);
92 qemu_mutex_lock(&pool->lock);
93 pool->idle_threads--;
94 } while (ret == -1 && !QTAILQ_EMPTY(&pool->request_list));
Stefan Hajnoczif7311cc2013-03-07 13:41:46 +010095 if (ret == -1 || pool->stopping) {
Paolo Bonzinid354c7e2012-02-23 13:23:34 +010096 break;
97 }
98
Stefan Hajnoczib8112032013-03-07 13:41:45 +010099 req = QTAILQ_FIRST(&pool->request_list);
100 QTAILQ_REMOVE(&pool->request_list, req, reqs);
Paolo Bonzinid354c7e2012-02-23 13:23:34 +0100101 req->state = THREAD_ACTIVE;
Stefan Hajnoczib8112032013-03-07 13:41:45 +0100102 qemu_mutex_unlock(&pool->lock);
Paolo Bonzinid354c7e2012-02-23 13:23:34 +0100103
104 ret = req->func(req->arg);
105
Paolo Bonzinid354c7e2012-02-23 13:23:34 +0100106 req->ret = ret;
Paolo Bonzini19d092c2012-10-31 10:09:11 +0100107 /* Write ret before state. */
108 smp_wmb();
109 req->state = THREAD_DONE;
110
Stefan Hajnoczib8112032013-03-07 13:41:45 +0100111 qemu_mutex_lock(&pool->lock);
Paolo Bonzinid354c7e2012-02-23 13:23:34 +0100112
Stefan Hajnoczic2e50e32014-07-15 16:44:25 +0200113 qemu_bh_schedule(pool->completion_bh);
Paolo Bonzinid354c7e2012-02-23 13:23:34 +0100114 }
115
Stefan Hajnoczib8112032013-03-07 13:41:45 +0100116 pool->cur_threads--;
Stefan Hajnoczif7311cc2013-03-07 13:41:46 +0100117 qemu_cond_signal(&pool->worker_stopped);
Stefan Hajnoczib8112032013-03-07 13:41:45 +0100118 qemu_mutex_unlock(&pool->lock);
Paolo Bonzinid354c7e2012-02-23 13:23:34 +0100119 return NULL;
120}
121
Stefan Hajnoczib8112032013-03-07 13:41:45 +0100122static void do_spawn_thread(ThreadPool *pool)
Paolo Bonzinid354c7e2012-02-23 13:23:34 +0100123{
124 QemuThread t;
125
126 /* Runs with lock taken. */
Stefan Hajnoczib8112032013-03-07 13:41:45 +0100127 if (!pool->new_threads) {
Paolo Bonzinid354c7e2012-02-23 13:23:34 +0100128 return;
129 }
130
Stefan Hajnoczib8112032013-03-07 13:41:45 +0100131 pool->new_threads--;
132 pool->pending_threads++;
Paolo Bonzinid354c7e2012-02-23 13:23:34 +0100133
Dr. David Alan Gilbert49001162014-01-30 10:20:32 +0000134 qemu_thread_create(&t, "worker", worker_thread, pool, QEMU_THREAD_DETACHED);
Paolo Bonzinid354c7e2012-02-23 13:23:34 +0100135}
136
137static void spawn_thread_bh_fn(void *opaque)
138{
Stefan Hajnoczib8112032013-03-07 13:41:45 +0100139 ThreadPool *pool = opaque;
140
141 qemu_mutex_lock(&pool->lock);
142 do_spawn_thread(pool);
143 qemu_mutex_unlock(&pool->lock);
Paolo Bonzinid354c7e2012-02-23 13:23:34 +0100144}
145
Stefan Hajnoczib8112032013-03-07 13:41:45 +0100146static void spawn_thread(ThreadPool *pool)
Paolo Bonzinid354c7e2012-02-23 13:23:34 +0100147{
Stefan Hajnoczib8112032013-03-07 13:41:45 +0100148 pool->cur_threads++;
149 pool->new_threads++;
Paolo Bonzinid354c7e2012-02-23 13:23:34 +0100150 /* If there are threads being created, they will spawn new workers, so
151 * we don't spend time creating many threads in a loop holding a mutex or
152 * starving the current vcpu.
153 *
154 * If there are no idle threads, ask the main thread to create one, so we
155 * inherit the correct affinity instead of the vcpu affinity.
156 */
Stefan Hajnoczib8112032013-03-07 13:41:45 +0100157 if (!pool->pending_threads) {
158 qemu_bh_schedule(pool->new_thread_bh);
Paolo Bonzinid354c7e2012-02-23 13:23:34 +0100159 }
160}
161
Stefan Hajnoczic2e50e32014-07-15 16:44:25 +0200162static void thread_pool_completion_bh(void *opaque)
Paolo Bonzinid354c7e2012-02-23 13:23:34 +0100163{
Stefan Hajnoczic2e50e32014-07-15 16:44:25 +0200164 ThreadPool *pool = opaque;
Paolo Bonzinid354c7e2012-02-23 13:23:34 +0100165 ThreadPoolElement *elem, *next;
166
Paolo Bonzini19196312017-02-13 14:52:31 +0100167 aio_context_acquire(pool->ctx);
Paolo Bonzinid354c7e2012-02-23 13:23:34 +0100168restart:
Stefan Hajnoczib8112032013-03-07 13:41:45 +0100169 QLIST_FOREACH_SAFE(elem, &pool->head, all, next) {
Fam Zheng3391f5e2014-09-11 13:41:12 +0800170 if (elem->state != THREAD_DONE) {
Paolo Bonzinid354c7e2012-02-23 13:23:34 +0100171 continue;
172 }
Stefan Hajnoczi1faa5bb2015-04-02 17:39:22 +0100173
174 trace_thread_pool_complete(pool, elem, elem->common.opaque,
175 elem->ret);
176 QLIST_REMOVE(elem, all);
177
178 if (elem->common.cb) {
Paolo Bonzini19d092c2012-10-31 10:09:11 +0100179 /* Read state before ret. */
180 smp_rmb();
Stefan Hajnoczi3c80ca12014-07-15 16:44:26 +0200181
182 /* Schedule ourselves in case elem->common.cb() calls aio_poll() to
183 * wait for another request that completed at the same time.
184 */
185 qemu_bh_schedule(pool->completion_bh);
186
Paolo Bonzinib9e413d2017-02-13 14:52:32 +0100187 aio_context_release(pool->ctx);
Paolo Bonzini19d092c2012-10-31 10:09:11 +0100188 elem->common.cb(elem->common.opaque, elem->ret);
Paolo Bonzinib9e413d2017-02-13 14:52:32 +0100189 aio_context_acquire(pool->ctx);
Peter Lievenb7a745d2017-03-16 17:02:49 +0100190
191 /* We can safely cancel the completion_bh here regardless of someone
192 * else having scheduled it meanwhile because we reenter the
193 * completion function anyway (goto restart).
194 */
195 qemu_bh_cancel(pool->completion_bh);
196
Fam Zheng80074292014-09-11 13:41:28 +0800197 qemu_aio_unref(elem);
Paolo Bonzinid354c7e2012-02-23 13:23:34 +0100198 goto restart;
199 } else {
Fam Zheng80074292014-09-11 13:41:28 +0800200 qemu_aio_unref(elem);
Paolo Bonzinid354c7e2012-02-23 13:23:34 +0100201 }
202 }
Paolo Bonzini19196312017-02-13 14:52:31 +0100203 aio_context_release(pool->ctx);
Paolo Bonzinid354c7e2012-02-23 13:23:34 +0100204}
205
Markus Armbruster7c84b1b2014-10-07 13:59:14 +0200206static void thread_pool_cancel(BlockAIOCB *acb)
Paolo Bonzinid354c7e2012-02-23 13:23:34 +0100207{
208 ThreadPoolElement *elem = (ThreadPoolElement *)acb;
Stefan Hajnoczib8112032013-03-07 13:41:45 +0100209 ThreadPool *pool = elem->pool;
Paolo Bonzinid354c7e2012-02-23 13:23:34 +0100210
211 trace_thread_pool_cancel(elem, elem->common.opaque);
212
Daniel Brodsky6e8a3552020-04-03 21:21:08 -0700213 QEMU_LOCK_GUARD(&pool->lock);
Paolo Bonzinid354c7e2012-02-23 13:23:34 +0100214 if (elem->state == THREAD_QUEUED &&
215 /* No thread has yet started working on elem. we can try to "steal"
216 * the item from the worker if we can get a signal from the
217 * semaphore. Because this is non-blocking, we can do it with
218 * the lock taken and ensure that elem will remain THREAD_QUEUED.
219 */
Stefan Hajnoczib8112032013-03-07 13:41:45 +0100220 qemu_sem_timedwait(&pool->sem, 0) == 0) {
221 QTAILQ_REMOVE(&pool->request_list, elem, reqs);
Stefan Hajnoczic2e50e32014-07-15 16:44:25 +0200222 qemu_bh_schedule(pool->completion_bh);
Fam Zheng3391f5e2014-09-11 13:41:12 +0800223
224 elem->state = THREAD_DONE;
225 elem->ret = -ECANCELED;
Paolo Bonzinid354c7e2012-02-23 13:23:34 +0100226 }
Fam Zheng3391f5e2014-09-11 13:41:12 +0800227
Fam Zheng3391f5e2014-09-11 13:41:12 +0800228}
229
Markus Armbruster7c84b1b2014-10-07 13:59:14 +0200230static AioContext *thread_pool_get_aio_context(BlockAIOCB *acb)
Fam Zheng3391f5e2014-09-11 13:41:12 +0800231{
232 ThreadPoolElement *elem = (ThreadPoolElement *)acb;
233 ThreadPool *pool = elem->pool;
234 return pool->ctx;
Paolo Bonzinid354c7e2012-02-23 13:23:34 +0100235}
236
Stefan Hajnoczid7331be2012-10-31 16:34:37 +0100237static const AIOCBInfo thread_pool_aiocb_info = {
Paolo Bonzinid354c7e2012-02-23 13:23:34 +0100238 .aiocb_size = sizeof(ThreadPoolElement),
Fam Zheng3391f5e2014-09-11 13:41:12 +0800239 .cancel_async = thread_pool_cancel,
240 .get_aio_context = thread_pool_get_aio_context,
Paolo Bonzinid354c7e2012-02-23 13:23:34 +0100241};
242
Markus Armbruster7c84b1b2014-10-07 13:59:14 +0200243BlockAIOCB *thread_pool_submit_aio(ThreadPool *pool,
Stefan Hajnoczic4d9d192013-03-07 13:41:49 +0100244 ThreadPoolFunc *func, void *arg,
Markus Armbruster097310b2014-10-07 13:59:15 +0200245 BlockCompletionFunc *cb, void *opaque)
Paolo Bonzinid354c7e2012-02-23 13:23:34 +0100246{
247 ThreadPoolElement *req;
248
Stefan Hajnoczid7331be2012-10-31 16:34:37 +0100249 req = qemu_aio_get(&thread_pool_aiocb_info, NULL, cb, opaque);
Paolo Bonzinid354c7e2012-02-23 13:23:34 +0100250 req->func = func;
251 req->arg = arg;
252 req->state = THREAD_QUEUED;
Stefan Hajnoczib8112032013-03-07 13:41:45 +0100253 req->pool = pool;
Paolo Bonzinid354c7e2012-02-23 13:23:34 +0100254
Stefan Hajnoczib8112032013-03-07 13:41:45 +0100255 QLIST_INSERT_HEAD(&pool->head, req, all);
Paolo Bonzinid354c7e2012-02-23 13:23:34 +0100256
Stefan Hajnoczib8112032013-03-07 13:41:45 +0100257 trace_thread_pool_submit(pool, req, arg);
Paolo Bonzinid354c7e2012-02-23 13:23:34 +0100258
Stefan Hajnoczib8112032013-03-07 13:41:45 +0100259 qemu_mutex_lock(&pool->lock);
260 if (pool->idle_threads == 0 && pool->cur_threads < pool->max_threads) {
261 spawn_thread(pool);
Paolo Bonzinid354c7e2012-02-23 13:23:34 +0100262 }
Stefan Hajnoczib8112032013-03-07 13:41:45 +0100263 QTAILQ_INSERT_TAIL(&pool->request_list, req, reqs);
264 qemu_mutex_unlock(&pool->lock);
265 qemu_sem_post(&pool->sem);
Paolo Bonzinid354c7e2012-02-23 13:23:34 +0100266 return &req->common;
267}
268
269typedef struct ThreadPoolCo {
270 Coroutine *co;
271 int ret;
272} ThreadPoolCo;
273
274static void thread_pool_co_cb(void *opaque, int ret)
275{
276 ThreadPoolCo *co = opaque;
277
278 co->ret = ret;
Paolo Bonzinib9e413d2017-02-13 14:52:32 +0100279 aio_co_wake(co->co);
Paolo Bonzinid354c7e2012-02-23 13:23:34 +0100280}
281
Stefan Hajnoczic4d9d192013-03-07 13:41:49 +0100282int coroutine_fn thread_pool_submit_co(ThreadPool *pool, ThreadPoolFunc *func,
283 void *arg)
Paolo Bonzinid354c7e2012-02-23 13:23:34 +0100284{
285 ThreadPoolCo tpc = { .co = qemu_coroutine_self(), .ret = -EINPROGRESS };
286 assert(qemu_in_coroutine());
Stefan Hajnoczic4d9d192013-03-07 13:41:49 +0100287 thread_pool_submit_aio(pool, func, arg, thread_pool_co_cb, &tpc);
Paolo Bonzinid354c7e2012-02-23 13:23:34 +0100288 qemu_coroutine_yield();
289 return tpc.ret;
290}
291
Stefan Hajnoczic4d9d192013-03-07 13:41:49 +0100292void thread_pool_submit(ThreadPool *pool, ThreadPoolFunc *func, void *arg)
Paolo Bonzinid354c7e2012-02-23 13:23:34 +0100293{
Stefan Hajnoczic4d9d192013-03-07 13:41:49 +0100294 thread_pool_submit_aio(pool, func, arg, NULL, NULL);
Paolo Bonzinid354c7e2012-02-23 13:23:34 +0100295}
296
Stefan Hajnoczib8112032013-03-07 13:41:45 +0100297static void thread_pool_init_one(ThreadPool *pool, AioContext *ctx)
298{
299 if (!ctx) {
300 ctx = qemu_get_aio_context();
301 }
302
303 memset(pool, 0, sizeof(*pool));
Stefan Hajnoczif7311cc2013-03-07 13:41:46 +0100304 pool->ctx = ctx;
Stefan Hajnoczic2e50e32014-07-15 16:44:25 +0200305 pool->completion_bh = aio_bh_new(ctx, thread_pool_completion_bh, pool);
Stefan Hajnoczib8112032013-03-07 13:41:45 +0100306 qemu_mutex_init(&pool->lock);
Stefan Hajnoczif7311cc2013-03-07 13:41:46 +0100307 qemu_cond_init(&pool->worker_stopped);
Stefan Hajnoczib8112032013-03-07 13:41:45 +0100308 qemu_sem_init(&pool->sem, 0);
309 pool->max_threads = 64;
310 pool->new_thread_bh = aio_bh_new(ctx, spawn_thread_bh_fn, pool);
311
312 QLIST_INIT(&pool->head);
313 QTAILQ_INIT(&pool->request_list);
Stefan Hajnoczib8112032013-03-07 13:41:45 +0100314}
315
Stefan Hajnoczif7311cc2013-03-07 13:41:46 +0100316ThreadPool *thread_pool_new(AioContext *ctx)
317{
318 ThreadPool *pool = g_new(ThreadPool, 1);
319 thread_pool_init_one(pool, ctx);
320 return pool;
321}
322
323void thread_pool_free(ThreadPool *pool)
324{
325 if (!pool) {
326 return;
327 }
328
329 assert(QLIST_EMPTY(&pool->head));
330
331 qemu_mutex_lock(&pool->lock);
332
333 /* Stop new threads from spawning */
334 qemu_bh_delete(pool->new_thread_bh);
335 pool->cur_threads -= pool->new_threads;
336 pool->new_threads = 0;
337
338 /* Wait for worker threads to terminate */
339 pool->stopping = true;
340 while (pool->cur_threads > 0) {
341 qemu_sem_post(&pool->sem);
342 qemu_cond_wait(&pool->worker_stopped, &pool->lock);
343 }
344
345 qemu_mutex_unlock(&pool->lock);
346
Stefan Hajnoczic2e50e32014-07-15 16:44:25 +0200347 qemu_bh_delete(pool->completion_bh);
Stefan Hajnoczif7311cc2013-03-07 13:41:46 +0100348 qemu_sem_destroy(&pool->sem);
Stefan Hajnoczif7311cc2013-03-07 13:41:46 +0100349 qemu_cond_destroy(&pool->worker_stopped);
350 qemu_mutex_destroy(&pool->lock);
Stefan Hajnoczif7311cc2013-03-07 13:41:46 +0100351 g_free(pool);
352}