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/osdep.h"
18 #include "qemu/defer-call.h"
19 #include "qemu/queue.h"
20 #include "qemu/thread.h"
21 #include "qemu/coroutine.h"
22 #include "trace.h"
23 #include "block/thread-pool.h"
24 #include "qemu/main-loop.h"
25
26 static void do_spawn_thread(ThreadPoolAio *pool);
27
28 typedef struct ThreadPoolElementAio ThreadPoolElementAio;
29
30 enum ThreadState {
31 THREAD_QUEUED,
32 THREAD_ACTIVE,
33 THREAD_DONE,
34 };
35
36 struct ThreadPoolElementAio {
37 BlockAIOCB common;
38 ThreadPoolAio *pool;
39 ThreadPoolFunc *func;
40 void *arg;
41
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 */
46 enum ThreadState state;
47 int ret;
48
49 /* Access to this list is protected by lock. */
50 QTAILQ_ENTRY(ThreadPoolElementAio) reqs;
51
52 /* This list is only written by the thread pool's mother thread. */
53 QLIST_ENTRY(ThreadPoolElementAio) all;
54 };
55
56 struct ThreadPoolAio {
57 AioContext *ctx;
58 QEMUBH *completion_bh;
59 QemuMutex lock;
60 QemuCond worker_stopped;
61 QemuCond request_cond;
62 QEMUBH *new_thread_bh;
63
64 /* The following variables are only accessed from one AioContext. */
65 QLIST_HEAD(, ThreadPoolElementAio) head;
66
67 /* The following variables are protected by lock. */
68 QTAILQ_HEAD(, ThreadPoolElementAio) 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 */
73 int min_threads;
74 int max_threads;
75 };
76
worker_thread(void * opaque)77 static void *worker_thread(void *opaque)
78 {
79 ThreadPoolAio *pool = opaque;
80
81 qemu_mutex_lock(&pool->lock);
82 pool->pending_threads--;
83 do_spawn_thread(pool);
84
85 while (pool->cur_threads <= pool->max_threads) {
86 ThreadPoolElementAio *req;
87 int ret;
88
89 if (QTAILQ_EMPTY(&pool->request_list)) {
90 pool->idle_threads++;
91 ret = qemu_cond_timedwait(&pool->request_cond, &pool->lock, 10000);
92 pool->idle_threads--;
93 if (ret == 0 &&
94 QTAILQ_EMPTY(&pool->request_list) &&
95 pool->cur_threads > pool->min_threads) {
96 /* Timed out + no work to do + no need for warm threads = exit. */
97 break;
98 }
99 /*
100 * Even if there was some work to do, check if there aren't
101 * too many worker threads before picking it up.
102 */
103 continue;
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_bh_schedule(pool->completion_bh);
119 qemu_mutex_lock(&pool->lock);
120 }
121
122 pool->cur_threads--;
123 qemu_cond_signal(&pool->worker_stopped);
124
125 /*
126 * Wake up another thread, in case we got a wakeup but decided
127 * to exit due to pool->cur_threads > pool->max_threads.
128 */
129 qemu_cond_signal(&pool->request_cond);
130 qemu_mutex_unlock(&pool->lock);
131 return NULL;
132 }
133
do_spawn_thread(ThreadPoolAio * pool)134 static void do_spawn_thread(ThreadPoolAio *pool)
135 {
136 QemuThread t;
137
138 /* Runs with lock taken. */
139 if (!pool->new_threads) {
140 return;
141 }
142
143 pool->new_threads--;
144 pool->pending_threads++;
145
146 qemu_thread_create(&t, "worker", worker_thread, pool, QEMU_THREAD_DETACHED);
147 }
148
spawn_thread_bh_fn(void * opaque)149 static void spawn_thread_bh_fn(void *opaque)
150 {
151 ThreadPoolAio *pool = opaque;
152
153 qemu_mutex_lock(&pool->lock);
154 do_spawn_thread(pool);
155 qemu_mutex_unlock(&pool->lock);
156 }
157
spawn_thread(ThreadPoolAio * pool)158 static void spawn_thread(ThreadPoolAio *pool)
159 {
160 pool->cur_threads++;
161 pool->new_threads++;
162 /* If there are threads being created, they will spawn new workers, so
163 * we don't spend time creating many threads in a loop holding a mutex or
164 * starving the current vcpu.
165 *
166 * If there are no idle threads, ask the main thread to create one, so we
167 * inherit the correct affinity instead of the vcpu affinity.
168 */
169 if (!pool->pending_threads) {
170 qemu_bh_schedule(pool->new_thread_bh);
171 }
172 }
173
thread_pool_completion_bh(void * opaque)174 static void thread_pool_completion_bh(void *opaque)
175 {
176 ThreadPoolAio *pool = opaque;
177 ThreadPoolElementAio *elem, *next;
178
179 defer_call_begin(); /* cb() may use defer_call() to coalesce work */
180
181 restart:
182 QLIST_FOREACH_SAFE(elem, &pool->head, all, next) {
183 if (elem->state != THREAD_DONE) {
184 continue;
185 }
186
187 trace_thread_pool_complete_aio(pool, elem, elem->common.opaque,
188 elem->ret);
189 QLIST_REMOVE(elem, all);
190
191 if (elem->common.cb) {
192 /* Read state before ret. */
193 smp_rmb();
194
195 /* Schedule ourselves in case elem->common.cb() calls aio_poll() to
196 * wait for another request that completed at the same time.
197 */
198 qemu_bh_schedule(pool->completion_bh);
199
200 elem->common.cb(elem->common.opaque, elem->ret);
201
202 /* We can safely cancel the completion_bh here regardless of someone
203 * else having scheduled it meanwhile because we reenter the
204 * completion function anyway (goto restart).
205 */
206 qemu_bh_cancel(pool->completion_bh);
207
208 qemu_aio_unref(elem);
209 goto restart;
210 } else {
211 qemu_aio_unref(elem);
212 }
213 }
214
215 defer_call_end();
216 }
217
thread_pool_cancel(BlockAIOCB * acb)218 static void thread_pool_cancel(BlockAIOCB *acb)
219 {
220 ThreadPoolElementAio *elem = (ThreadPoolElementAio *)acb;
221 ThreadPoolAio *pool = elem->pool;
222
223 trace_thread_pool_cancel_aio(elem, elem->common.opaque);
224
225 QEMU_LOCK_GUARD(&pool->lock);
226 if (elem->state == THREAD_QUEUED) {
227 QTAILQ_REMOVE(&pool->request_list, elem, reqs);
228 qemu_bh_schedule(pool->completion_bh);
229
230 elem->state = THREAD_DONE;
231 elem->ret = -ECANCELED;
232 }
233
234 }
235
236 static const AIOCBInfo thread_pool_aiocb_info = {
237 .aiocb_size = sizeof(ThreadPoolElementAio),
238 .cancel_async = thread_pool_cancel,
239 };
240
thread_pool_submit_aio(ThreadPoolFunc * func,void * arg,BlockCompletionFunc * cb,void * opaque)241 BlockAIOCB *thread_pool_submit_aio(ThreadPoolFunc *func, void *arg,
242 BlockCompletionFunc *cb, void *opaque)
243 {
244 ThreadPoolElementAio *req;
245 AioContext *ctx = qemu_get_current_aio_context();
246 ThreadPoolAio *pool = aio_get_thread_pool(ctx);
247
248 /* Assert that the thread submitting work is the same running the pool */
249 assert(pool->ctx == qemu_get_current_aio_context());
250
251 req = qemu_aio_get(&thread_pool_aiocb_info, NULL, cb, opaque);
252 req->func = func;
253 req->arg = arg;
254 req->state = THREAD_QUEUED;
255 req->pool = pool;
256
257 QLIST_INSERT_HEAD(&pool->head, req, all);
258
259 trace_thread_pool_submit_aio(pool, req, arg);
260
261 qemu_mutex_lock(&pool->lock);
262 if (pool->idle_threads == 0 && pool->cur_threads < pool->max_threads) {
263 spawn_thread(pool);
264 }
265 QTAILQ_INSERT_TAIL(&pool->request_list, req, reqs);
266 qemu_mutex_unlock(&pool->lock);
267 qemu_cond_signal(&pool->request_cond);
268 return &req->common;
269 }
270
271 typedef struct ThreadPoolCo {
272 Coroutine *co;
273 int ret;
274 } ThreadPoolCo;
275
thread_pool_co_cb(void * opaque,int ret)276 static void thread_pool_co_cb(void *opaque, int ret)
277 {
278 ThreadPoolCo *co = opaque;
279
280 co->ret = ret;
281 aio_co_wake(co->co);
282 }
283
thread_pool_submit_co(ThreadPoolFunc * func,void * arg)284 int coroutine_fn thread_pool_submit_co(ThreadPoolFunc *func, void *arg)
285 {
286 ThreadPoolCo tpc = { .co = qemu_coroutine_self(), .ret = -EINPROGRESS };
287 assert(qemu_in_coroutine());
288 thread_pool_submit_aio(func, arg, thread_pool_co_cb, &tpc);
289 qemu_coroutine_yield();
290 return tpc.ret;
291 }
292
thread_pool_update_params(ThreadPoolAio * pool,AioContext * ctx)293 void thread_pool_update_params(ThreadPoolAio *pool, AioContext *ctx)
294 {
295 qemu_mutex_lock(&pool->lock);
296
297 pool->min_threads = ctx->thread_pool_min;
298 pool->max_threads = ctx->thread_pool_max;
299
300 /*
301 * We either have to:
302 * - Increase the number available of threads until over the min_threads
303 * threshold.
304 * - Bump the worker threads so that they exit, until under the max_threads
305 * threshold.
306 * - Do nothing. The current number of threads fall in between the min and
307 * max thresholds. We'll let the pool manage itself.
308 */
309 for (int i = pool->cur_threads; i < pool->min_threads; i++) {
310 spawn_thread(pool);
311 }
312
313 for (int i = pool->cur_threads; i > pool->max_threads; i--) {
314 qemu_cond_signal(&pool->request_cond);
315 }
316
317 qemu_mutex_unlock(&pool->lock);
318 }
319
thread_pool_init_one(ThreadPoolAio * pool,AioContext * ctx)320 static void thread_pool_init_one(ThreadPoolAio *pool, AioContext *ctx)
321 {
322 if (!ctx) {
323 ctx = qemu_get_aio_context();
324 }
325
326 memset(pool, 0, sizeof(*pool));
327 pool->ctx = ctx;
328 pool->completion_bh = aio_bh_new(ctx, thread_pool_completion_bh, pool);
329 qemu_mutex_init(&pool->lock);
330 qemu_cond_init(&pool->worker_stopped);
331 qemu_cond_init(&pool->request_cond);
332 pool->new_thread_bh = aio_bh_new(ctx, spawn_thread_bh_fn, pool);
333
334 QLIST_INIT(&pool->head);
335 QTAILQ_INIT(&pool->request_list);
336
337 thread_pool_update_params(pool, ctx);
338 }
339
thread_pool_new_aio(AioContext * ctx)340 ThreadPoolAio *thread_pool_new_aio(AioContext *ctx)
341 {
342 ThreadPoolAio *pool = g_new(ThreadPoolAio, 1);
343 thread_pool_init_one(pool, ctx);
344 return pool;
345 }
346
thread_pool_free_aio(ThreadPoolAio * pool)347 void thread_pool_free_aio(ThreadPoolAio *pool)
348 {
349 if (!pool) {
350 return;
351 }
352
353 assert(QLIST_EMPTY(&pool->head));
354
355 qemu_mutex_lock(&pool->lock);
356
357 /* Stop new threads from spawning */
358 qemu_bh_delete(pool->new_thread_bh);
359 pool->cur_threads -= pool->new_threads;
360 pool->new_threads = 0;
361
362 /* Wait for worker threads to terminate */
363 pool->max_threads = 0;
364 qemu_cond_broadcast(&pool->request_cond);
365 while (pool->cur_threads > 0) {
366 qemu_cond_wait(&pool->worker_stopped, &pool->lock);
367 }
368
369 qemu_mutex_unlock(&pool->lock);
370
371 qemu_bh_delete(pool->completion_bh);
372 qemu_cond_destroy(&pool->request_cond);
373 qemu_cond_destroy(&pool->worker_stopped);
374 qemu_mutex_destroy(&pool->lock);
375 g_free(pool);
376 }
377
378 struct ThreadPool {
379 GThreadPool *t;
380 size_t cur_work;
381 QemuMutex cur_work_lock;
382 QemuCond all_finished_cond;
383 };
384
385 typedef struct {
386 ThreadPoolFunc *func;
387 void *opaque;
388 GDestroyNotify opaque_destroy;
389 } ThreadPoolElement;
390
thread_pool_func(gpointer data,gpointer user_data)391 static void thread_pool_func(gpointer data, gpointer user_data)
392 {
393 ThreadPool *pool = user_data;
394 g_autofree ThreadPoolElement *el = data;
395
396 el->func(el->opaque);
397
398 if (el->opaque_destroy) {
399 el->opaque_destroy(el->opaque);
400 }
401
402 QEMU_LOCK_GUARD(&pool->cur_work_lock);
403
404 assert(pool->cur_work > 0);
405 pool->cur_work--;
406
407 if (pool->cur_work == 0) {
408 qemu_cond_signal(&pool->all_finished_cond);
409 }
410 }
411
thread_pool_new(void)412 ThreadPool *thread_pool_new(void)
413 {
414 ThreadPool *pool = g_new(ThreadPool, 1);
415
416 pool->cur_work = 0;
417 qemu_mutex_init(&pool->cur_work_lock);
418 qemu_cond_init(&pool->all_finished_cond);
419
420 pool->t = g_thread_pool_new(thread_pool_func, pool, 0, TRUE, NULL);
421 /*
422 * g_thread_pool_new() can only return errors if initial thread(s)
423 * creation fails but we ask for 0 initial threads above.
424 */
425 assert(pool->t);
426
427 return pool;
428 }
429
thread_pool_free(ThreadPool * pool)430 void thread_pool_free(ThreadPool *pool)
431 {
432 /*
433 * With _wait = TRUE this effectively waits for all
434 * previously submitted work to complete first.
435 */
436 g_thread_pool_free(pool->t, FALSE, TRUE);
437
438 qemu_cond_destroy(&pool->all_finished_cond);
439 qemu_mutex_destroy(&pool->cur_work_lock);
440
441 g_free(pool);
442 }
443
thread_pool_submit(ThreadPool * pool,ThreadPoolFunc * func,void * opaque,GDestroyNotify opaque_destroy)444 void thread_pool_submit(ThreadPool *pool, ThreadPoolFunc *func,
445 void *opaque, GDestroyNotify opaque_destroy)
446 {
447 ThreadPoolElement *el = g_new(ThreadPoolElement, 1);
448
449 el->func = func;
450 el->opaque = opaque;
451 el->opaque_destroy = opaque_destroy;
452
453 WITH_QEMU_LOCK_GUARD(&pool->cur_work_lock) {
454 pool->cur_work++;
455 }
456
457 /*
458 * Ignore the return value since this function can only return errors
459 * if creation of an additional thread fails but even in this case the
460 * provided work is still getting queued (just for the existing threads).
461 */
462 g_thread_pool_push(pool->t, el, NULL);
463 }
464
thread_pool_submit_immediate(ThreadPool * pool,ThreadPoolFunc * func,void * opaque,GDestroyNotify opaque_destroy)465 void thread_pool_submit_immediate(ThreadPool *pool, ThreadPoolFunc *func,
466 void *opaque, GDestroyNotify opaque_destroy)
467 {
468 thread_pool_submit(pool, func, opaque, opaque_destroy);
469 thread_pool_adjust_max_threads_to_work(pool);
470 }
471
thread_pool_wait(ThreadPool * pool)472 void thread_pool_wait(ThreadPool *pool)
473 {
474 QEMU_LOCK_GUARD(&pool->cur_work_lock);
475
476 while (pool->cur_work > 0) {
477 qemu_cond_wait(&pool->all_finished_cond,
478 &pool->cur_work_lock);
479 }
480 }
481
thread_pool_set_max_threads(ThreadPool * pool,int max_threads)482 bool thread_pool_set_max_threads(ThreadPool *pool,
483 int max_threads)
484 {
485 assert(max_threads > 0);
486
487 return g_thread_pool_set_max_threads(pool->t, max_threads, NULL);
488 }
489
thread_pool_adjust_max_threads_to_work(ThreadPool * pool)490 bool thread_pool_adjust_max_threads_to_work(ThreadPool *pool)
491 {
492 QEMU_LOCK_GUARD(&pool->cur_work_lock);
493
494 return thread_pool_set_max_threads(pool, pool->cur_work);
495 }
496