]>
Commit | Line | Data |
---|---|---|
d354c7ec PB |
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 <[email protected]> | |
9 | * Paolo Bonzini <[email protected]> | |
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 | */ | |
d38ea87a | 17 | #include "qemu/osdep.h" |
1de7afc9 PB |
18 | #include "qemu/queue.h" |
19 | #include "qemu/thread.h" | |
10817bf0 | 20 | #include "qemu/coroutine.h" |
c2b38b27 | 21 | #include "trace.h" |
737e150e | 22 | #include "block/thread-pool.h" |
6a1751b7 | 23 | #include "qemu/main-loop.h" |
d354c7ec | 24 | |
b811203c | 25 | static void do_spawn_thread(ThreadPool *pool); |
d354c7ec PB |
26 | |
27 | typedef struct ThreadPoolElement ThreadPoolElement; | |
28 | ||
29 | enum ThreadState { | |
30 | THREAD_QUEUED, | |
31 | THREAD_ACTIVE, | |
32 | THREAD_DONE, | |
d354c7ec PB |
33 | }; |
34 | ||
35 | struct ThreadPoolElement { | |
7c84b1b8 | 36 | BlockAIOCB common; |
b811203c | 37 | ThreadPool *pool; |
d354c7ec PB |
38 | ThreadPoolFunc *func; |
39 | void *arg; | |
19d092cf PB |
40 | |
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 | */ | |
d354c7ec PB |
45 | 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 | ||
b811203c | 55 | struct ThreadPool { |
f7311ccc | 56 | AioContext *ctx; |
c2e50e3d | 57 | QEMUBH *completion_bh; |
b811203c | 58 | QemuMutex lock; |
f7311ccc | 59 | QemuCond worker_stopped; |
b811203c SH |
60 | QemuSemaphore sem; |
61 | int max_threads; | |
62 | QEMUBH *new_thread_bh; | |
63 | ||
64 | /* The following variables are only accessed from one AioContext. */ | |
65 | QLIST_HEAD(, ThreadPoolElement) head; | |
66 | ||
67 | /* 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 */ | |
f7311ccc | 73 | bool stopping; |
b811203c SH |
74 | }; |
75 | ||
b811203c | 76 | static void *worker_thread(void *opaque) |
d354c7ec | 77 | { |
b811203c SH |
78 | ThreadPool *pool = opaque; |
79 | ||
80 | qemu_mutex_lock(&pool->lock); | |
81 | pool->pending_threads--; | |
82 | do_spawn_thread(pool); | |
d354c7ec | 83 | |
f7311ccc | 84 | while (!pool->stopping) { |
d354c7ec PB |
85 | ThreadPoolElement *req; |
86 | int ret; | |
87 | ||
88 | do { | |
b811203c SH |
89 | 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)); | |
f7311ccc | 95 | if (ret == -1 || pool->stopping) { |
d354c7ec PB |
96 | break; |
97 | } | |
98 | ||
b811203c SH |
99 | req = QTAILQ_FIRST(&pool->request_list); |
100 | QTAILQ_REMOVE(&pool->request_list, req, reqs); | |
d354c7ec | 101 | req->state = THREAD_ACTIVE; |
b811203c | 102 | qemu_mutex_unlock(&pool->lock); |
d354c7ec PB |
103 | |
104 | ret = req->func(req->arg); | |
105 | ||
d354c7ec | 106 | req->ret = ret; |
19d092cf PB |
107 | /* Write ret before state. */ |
108 | smp_wmb(); | |
109 | req->state = THREAD_DONE; | |
110 | ||
b811203c | 111 | qemu_mutex_lock(&pool->lock); |
d354c7ec | 112 | |
c2e50e3d | 113 | qemu_bh_schedule(pool->completion_bh); |
d354c7ec PB |
114 | } |
115 | ||
b811203c | 116 | pool->cur_threads--; |
f7311ccc | 117 | qemu_cond_signal(&pool->worker_stopped); |
b811203c | 118 | qemu_mutex_unlock(&pool->lock); |
d354c7ec PB |
119 | return NULL; |
120 | } | |
121 | ||
b811203c | 122 | static void do_spawn_thread(ThreadPool *pool) |
d354c7ec PB |
123 | { |
124 | QemuThread t; | |
125 | ||
126 | /* Runs with lock taken. */ | |
b811203c | 127 | if (!pool->new_threads) { |
d354c7ec PB |
128 | return; |
129 | } | |
130 | ||
b811203c SH |
131 | pool->new_threads--; |
132 | pool->pending_threads++; | |
d354c7ec | 133 | |
4900116e | 134 | qemu_thread_create(&t, "worker", worker_thread, pool, QEMU_THREAD_DETACHED); |
d354c7ec PB |
135 | } |
136 | ||
137 | static void spawn_thread_bh_fn(void *opaque) | |
138 | { | |
b811203c SH |
139 | ThreadPool *pool = opaque; |
140 | ||
141 | qemu_mutex_lock(&pool->lock); | |
142 | do_spawn_thread(pool); | |
143 | qemu_mutex_unlock(&pool->lock); | |
d354c7ec PB |
144 | } |
145 | ||
b811203c | 146 | static void spawn_thread(ThreadPool *pool) |
d354c7ec | 147 | { |
b811203c SH |
148 | pool->cur_threads++; |
149 | pool->new_threads++; | |
d354c7ec PB |
150 | /* 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 | */ | |
b811203c SH |
157 | if (!pool->pending_threads) { |
158 | qemu_bh_schedule(pool->new_thread_bh); | |
d354c7ec PB |
159 | } |
160 | } | |
161 | ||
c2e50e3d | 162 | static void thread_pool_completion_bh(void *opaque) |
d354c7ec | 163 | { |
c2e50e3d | 164 | ThreadPool *pool = opaque; |
d354c7ec PB |
165 | ThreadPoolElement *elem, *next; |
166 | ||
1919631e | 167 | aio_context_acquire(pool->ctx); |
d354c7ec | 168 | restart: |
b811203c | 169 | QLIST_FOREACH_SAFE(elem, &pool->head, all, next) { |
3391f5e5 | 170 | if (elem->state != THREAD_DONE) { |
d354c7ec PB |
171 | continue; |
172 | } | |
1faa5bb7 SH |
173 | |
174 | trace_thread_pool_complete(pool, elem, elem->common.opaque, | |
175 | elem->ret); | |
176 | QLIST_REMOVE(elem, all); | |
177 | ||
178 | if (elem->common.cb) { | |
19d092cf PB |
179 | /* Read state before ret. */ |
180 | smp_rmb(); | |
3c80ca15 SH |
181 | |
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 | ||
b9e413dd | 187 | aio_context_release(pool->ctx); |
19d092cf | 188 | elem->common.cb(elem->common.opaque, elem->ret); |
b9e413dd | 189 | aio_context_acquire(pool->ctx); |
b7a745dc PL |
190 | |
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 | ||
8007429a | 197 | qemu_aio_unref(elem); |
d354c7ec PB |
198 | goto restart; |
199 | } else { | |
8007429a | 200 | qemu_aio_unref(elem); |
d354c7ec PB |
201 | } |
202 | } | |
1919631e | 203 | aio_context_release(pool->ctx); |
d354c7ec PB |
204 | } |
205 | ||
7c84b1b8 | 206 | static void thread_pool_cancel(BlockAIOCB *acb) |
d354c7ec PB |
207 | { |
208 | ThreadPoolElement *elem = (ThreadPoolElement *)acb; | |
b811203c | 209 | ThreadPool *pool = elem->pool; |
d354c7ec PB |
210 | |
211 | trace_thread_pool_cancel(elem, elem->common.opaque); | |
212 | ||
6e8a355d | 213 | QEMU_LOCK_GUARD(&pool->lock); |
d354c7ec PB |
214 | 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 | */ | |
b811203c SH |
220 | qemu_sem_timedwait(&pool->sem, 0) == 0) { |
221 | QTAILQ_REMOVE(&pool->request_list, elem, reqs); | |
c2e50e3d | 222 | qemu_bh_schedule(pool->completion_bh); |
3391f5e5 FZ |
223 | |
224 | elem->state = THREAD_DONE; | |
225 | elem->ret = -ECANCELED; | |
d354c7ec | 226 | } |
3391f5e5 | 227 | |
3391f5e5 FZ |
228 | } |
229 | ||
7c84b1b8 | 230 | static AioContext *thread_pool_get_aio_context(BlockAIOCB *acb) |
3391f5e5 FZ |
231 | { |
232 | ThreadPoolElement *elem = (ThreadPoolElement *)acb; | |
233 | ThreadPool *pool = elem->pool; | |
234 | return pool->ctx; | |
d354c7ec PB |
235 | } |
236 | ||
d7331bed | 237 | static const AIOCBInfo thread_pool_aiocb_info = { |
d354c7ec | 238 | .aiocb_size = sizeof(ThreadPoolElement), |
3391f5e5 FZ |
239 | .cancel_async = thread_pool_cancel, |
240 | .get_aio_context = thread_pool_get_aio_context, | |
d354c7ec PB |
241 | }; |
242 | ||
7c84b1b8 | 243 | BlockAIOCB *thread_pool_submit_aio(ThreadPool *pool, |
c4d9d196 | 244 | ThreadPoolFunc *func, void *arg, |
097310b5 | 245 | BlockCompletionFunc *cb, void *opaque) |
d354c7ec PB |
246 | { |
247 | ThreadPoolElement *req; | |
248 | ||
d7331bed | 249 | req = qemu_aio_get(&thread_pool_aiocb_info, NULL, cb, opaque); |
d354c7ec PB |
250 | req->func = func; |
251 | req->arg = arg; | |
252 | req->state = THREAD_QUEUED; | |
b811203c | 253 | req->pool = pool; |
d354c7ec | 254 | |
b811203c | 255 | QLIST_INSERT_HEAD(&pool->head, req, all); |
d354c7ec | 256 | |
b811203c | 257 | trace_thread_pool_submit(pool, req, arg); |
d354c7ec | 258 | |
b811203c SH |
259 | qemu_mutex_lock(&pool->lock); |
260 | if (pool->idle_threads == 0 && pool->cur_threads < pool->max_threads) { | |
261 | spawn_thread(pool); | |
d354c7ec | 262 | } |
b811203c SH |
263 | QTAILQ_INSERT_TAIL(&pool->request_list, req, reqs); |
264 | qemu_mutex_unlock(&pool->lock); | |
265 | qemu_sem_post(&pool->sem); | |
d354c7ec PB |
266 | return &req->common; |
267 | } | |
268 | ||
269 | typedef struct ThreadPoolCo { | |
270 | Coroutine *co; | |
271 | int ret; | |
272 | } ThreadPoolCo; | |
273 | ||
274 | static void thread_pool_co_cb(void *opaque, int ret) | |
275 | { | |
276 | ThreadPoolCo *co = opaque; | |
277 | ||
278 | co->ret = ret; | |
b9e413dd | 279 | aio_co_wake(co->co); |
d354c7ec PB |
280 | } |
281 | ||
c4d9d196 SH |
282 | int coroutine_fn thread_pool_submit_co(ThreadPool *pool, ThreadPoolFunc *func, |
283 | void *arg) | |
d354c7ec PB |
284 | { |
285 | ThreadPoolCo tpc = { .co = qemu_coroutine_self(), .ret = -EINPROGRESS }; | |
286 | assert(qemu_in_coroutine()); | |
c4d9d196 | 287 | thread_pool_submit_aio(pool, func, arg, thread_pool_co_cb, &tpc); |
d354c7ec PB |
288 | qemu_coroutine_yield(); |
289 | return tpc.ret; | |
290 | } | |
291 | ||
c4d9d196 | 292 | void thread_pool_submit(ThreadPool *pool, ThreadPoolFunc *func, void *arg) |
d354c7ec | 293 | { |
c4d9d196 | 294 | thread_pool_submit_aio(pool, func, arg, NULL, NULL); |
d354c7ec PB |
295 | } |
296 | ||
b811203c SH |
297 | static 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)); | |
f7311ccc | 304 | pool->ctx = ctx; |
c2e50e3d | 305 | pool->completion_bh = aio_bh_new(ctx, thread_pool_completion_bh, pool); |
b811203c | 306 | qemu_mutex_init(&pool->lock); |
f7311ccc | 307 | qemu_cond_init(&pool->worker_stopped); |
b811203c SH |
308 | 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); | |
b811203c SH |
314 | } |
315 | ||
f7311ccc SH |
316 | ThreadPool *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 | ||
323 | void 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 | ||
c2e50e3d | 347 | qemu_bh_delete(pool->completion_bh); |
f7311ccc | 348 | qemu_sem_destroy(&pool->sem); |
f7311ccc SH |
349 | qemu_cond_destroy(&pool->worker_stopped); |
350 | qemu_mutex_destroy(&pool->lock); | |
f7311ccc SH |
351 | g_free(pool); |
352 | } |