]>
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 | */ | |
17 | #include "qemu-common.h" | |
1de7afc9 PB |
18 | #include "qemu/queue.h" |
19 | #include "qemu/thread.h" | |
20 | #include "qemu/osdep.h" | |
737e150e | 21 | #include "block/coroutine.h" |
d354c7ec | 22 | #include "trace.h" |
737e150e | 23 | #include "block/block_int.h" |
1de7afc9 | 24 | #include "qemu/event_notifier.h" |
737e150e | 25 | #include "block/thread-pool.h" |
6a1751b7 | 26 | #include "qemu/main-loop.h" |
d354c7ec | 27 | |
b811203c | 28 | static void do_spawn_thread(ThreadPool *pool); |
d354c7ec PB |
29 | |
30 | typedef struct ThreadPoolElement ThreadPoolElement; | |
31 | ||
32 | enum ThreadState { | |
33 | THREAD_QUEUED, | |
34 | THREAD_ACTIVE, | |
35 | THREAD_DONE, | |
36 | THREAD_CANCELED, | |
37 | }; | |
38 | ||
39 | struct ThreadPoolElement { | |
40 | BlockDriverAIOCB common; | |
b811203c | 41 | ThreadPool *pool; |
d354c7ec PB |
42 | ThreadPoolFunc *func; |
43 | void *arg; | |
19d092cf PB |
44 | |
45 | /* Moving state out of THREAD_QUEUED is protected by lock. After | |
46 | * that, only the worker thread can write to it. Reads and writes | |
47 | * of state and ret are ordered with memory barriers. | |
48 | */ | |
d354c7ec PB |
49 | enum ThreadState state; |
50 | int ret; | |
51 | ||
52 | /* Access to this list is protected by lock. */ | |
53 | QTAILQ_ENTRY(ThreadPoolElement) reqs; | |
54 | ||
55 | /* Access to this list is protected by the global mutex. */ | |
56 | QLIST_ENTRY(ThreadPoolElement) all; | |
57 | }; | |
58 | ||
b811203c SH |
59 | struct ThreadPool { |
60 | EventNotifier notifier; | |
f7311ccc | 61 | AioContext *ctx; |
b811203c SH |
62 | QemuMutex lock; |
63 | QemuCond check_cancel; | |
f7311ccc | 64 | QemuCond worker_stopped; |
b811203c SH |
65 | QemuSemaphore sem; |
66 | int max_threads; | |
67 | QEMUBH *new_thread_bh; | |
68 | ||
69 | /* The following variables are only accessed from one AioContext. */ | |
70 | QLIST_HEAD(, ThreadPoolElement) head; | |
71 | ||
72 | /* The following variables are protected by lock. */ | |
73 | QTAILQ_HEAD(, ThreadPoolElement) request_list; | |
74 | int cur_threads; | |
75 | int idle_threads; | |
76 | int new_threads; /* backlog of threads we need to create */ | |
77 | int pending_threads; /* threads created but not running yet */ | |
78 | int pending_cancellations; /* whether we need a cond_broadcast */ | |
f7311ccc | 79 | bool stopping; |
b811203c SH |
80 | }; |
81 | ||
b811203c | 82 | static void *worker_thread(void *opaque) |
d354c7ec | 83 | { |
b811203c SH |
84 | ThreadPool *pool = opaque; |
85 | ||
86 | qemu_mutex_lock(&pool->lock); | |
87 | pool->pending_threads--; | |
88 | do_spawn_thread(pool); | |
d354c7ec | 89 | |
f7311ccc | 90 | while (!pool->stopping) { |
d354c7ec PB |
91 | ThreadPoolElement *req; |
92 | int ret; | |
93 | ||
94 | do { | |
b811203c SH |
95 | pool->idle_threads++; |
96 | qemu_mutex_unlock(&pool->lock); | |
97 | ret = qemu_sem_timedwait(&pool->sem, 10000); | |
98 | qemu_mutex_lock(&pool->lock); | |
99 | pool->idle_threads--; | |
100 | } while (ret == -1 && !QTAILQ_EMPTY(&pool->request_list)); | |
f7311ccc | 101 | if (ret == -1 || pool->stopping) { |
d354c7ec PB |
102 | break; |
103 | } | |
104 | ||
b811203c SH |
105 | req = QTAILQ_FIRST(&pool->request_list); |
106 | QTAILQ_REMOVE(&pool->request_list, req, reqs); | |
d354c7ec | 107 | req->state = THREAD_ACTIVE; |
b811203c | 108 | qemu_mutex_unlock(&pool->lock); |
d354c7ec PB |
109 | |
110 | ret = req->func(req->arg); | |
111 | ||
d354c7ec | 112 | req->ret = ret; |
19d092cf PB |
113 | /* Write ret before state. */ |
114 | smp_wmb(); | |
115 | req->state = THREAD_DONE; | |
116 | ||
b811203c SH |
117 | qemu_mutex_lock(&pool->lock); |
118 | if (pool->pending_cancellations) { | |
119 | qemu_cond_broadcast(&pool->check_cancel); | |
d354c7ec PB |
120 | } |
121 | ||
b811203c | 122 | event_notifier_set(&pool->notifier); |
d354c7ec PB |
123 | } |
124 | ||
b811203c | 125 | pool->cur_threads--; |
f7311ccc | 126 | qemu_cond_signal(&pool->worker_stopped); |
b811203c | 127 | qemu_mutex_unlock(&pool->lock); |
d354c7ec PB |
128 | return NULL; |
129 | } | |
130 | ||
b811203c | 131 | static void do_spawn_thread(ThreadPool *pool) |
d354c7ec PB |
132 | { |
133 | QemuThread t; | |
134 | ||
135 | /* Runs with lock taken. */ | |
b811203c | 136 | if (!pool->new_threads) { |
d354c7ec PB |
137 | return; |
138 | } | |
139 | ||
b811203c SH |
140 | pool->new_threads--; |
141 | pool->pending_threads++; | |
d354c7ec | 142 | |
4900116e | 143 | qemu_thread_create(&t, "worker", worker_thread, pool, QEMU_THREAD_DETACHED); |
d354c7ec PB |
144 | } |
145 | ||
146 | static void spawn_thread_bh_fn(void *opaque) | |
147 | { | |
b811203c SH |
148 | ThreadPool *pool = opaque; |
149 | ||
150 | qemu_mutex_lock(&pool->lock); | |
151 | do_spawn_thread(pool); | |
152 | qemu_mutex_unlock(&pool->lock); | |
d354c7ec PB |
153 | } |
154 | ||
b811203c | 155 | static void spawn_thread(ThreadPool *pool) |
d354c7ec | 156 | { |
b811203c SH |
157 | pool->cur_threads++; |
158 | pool->new_threads++; | |
d354c7ec PB |
159 | /* If there are threads being created, they will spawn new workers, so |
160 | * we don't spend time creating many threads in a loop holding a mutex or | |
161 | * starving the current vcpu. | |
162 | * | |
163 | * If there are no idle threads, ask the main thread to create one, so we | |
164 | * inherit the correct affinity instead of the vcpu affinity. | |
165 | */ | |
b811203c SH |
166 | if (!pool->pending_threads) { |
167 | qemu_bh_schedule(pool->new_thread_bh); | |
d354c7ec PB |
168 | } |
169 | } | |
170 | ||
171 | static void event_notifier_ready(EventNotifier *notifier) | |
172 | { | |
b811203c | 173 | ThreadPool *pool = container_of(notifier, ThreadPool, notifier); |
d354c7ec PB |
174 | ThreadPoolElement *elem, *next; |
175 | ||
176 | event_notifier_test_and_clear(notifier); | |
177 | restart: | |
b811203c | 178 | QLIST_FOREACH_SAFE(elem, &pool->head, all, next) { |
d354c7ec PB |
179 | if (elem->state != THREAD_CANCELED && elem->state != THREAD_DONE) { |
180 | continue; | |
181 | } | |
182 | if (elem->state == THREAD_DONE) { | |
b811203c SH |
183 | trace_thread_pool_complete(pool, elem, elem->common.opaque, |
184 | elem->ret); | |
d354c7ec PB |
185 | } |
186 | if (elem->state == THREAD_DONE && elem->common.cb) { | |
d354c7ec | 187 | QLIST_REMOVE(elem, all); |
19d092cf PB |
188 | /* Read state before ret. */ |
189 | smp_rmb(); | |
190 | elem->common.cb(elem->common.opaque, elem->ret); | |
d354c7ec PB |
191 | qemu_aio_release(elem); |
192 | goto restart; | |
193 | } else { | |
194 | /* remove the request */ | |
195 | QLIST_REMOVE(elem, all); | |
196 | qemu_aio_release(elem); | |
197 | } | |
198 | } | |
199 | } | |
200 | ||
d354c7ec PB |
201 | static void thread_pool_cancel(BlockDriverAIOCB *acb) |
202 | { | |
203 | ThreadPoolElement *elem = (ThreadPoolElement *)acb; | |
b811203c | 204 | ThreadPool *pool = elem->pool; |
d354c7ec PB |
205 | |
206 | trace_thread_pool_cancel(elem, elem->common.opaque); | |
207 | ||
b811203c | 208 | qemu_mutex_lock(&pool->lock); |
d354c7ec PB |
209 | if (elem->state == THREAD_QUEUED && |
210 | /* No thread has yet started working on elem. we can try to "steal" | |
211 | * the item from the worker if we can get a signal from the | |
212 | * semaphore. Because this is non-blocking, we can do it with | |
213 | * the lock taken and ensure that elem will remain THREAD_QUEUED. | |
214 | */ | |
b811203c SH |
215 | qemu_sem_timedwait(&pool->sem, 0) == 0) { |
216 | QTAILQ_REMOVE(&pool->request_list, elem, reqs); | |
d354c7ec | 217 | elem->state = THREAD_CANCELED; |
b811203c | 218 | event_notifier_set(&pool->notifier); |
d354c7ec | 219 | } else { |
b811203c | 220 | pool->pending_cancellations++; |
d354c7ec | 221 | while (elem->state != THREAD_CANCELED && elem->state != THREAD_DONE) { |
b811203c | 222 | qemu_cond_wait(&pool->check_cancel, &pool->lock); |
d354c7ec | 223 | } |
b811203c | 224 | pool->pending_cancellations--; |
d354c7ec | 225 | } |
b811203c | 226 | qemu_mutex_unlock(&pool->lock); |
271c0f68 | 227 | event_notifier_ready(&pool->notifier); |
d354c7ec PB |
228 | } |
229 | ||
d7331bed | 230 | static const AIOCBInfo thread_pool_aiocb_info = { |
d354c7ec PB |
231 | .aiocb_size = sizeof(ThreadPoolElement), |
232 | .cancel = thread_pool_cancel, | |
233 | }; | |
234 | ||
c4d9d196 SH |
235 | BlockDriverAIOCB *thread_pool_submit_aio(ThreadPool *pool, |
236 | ThreadPoolFunc *func, void *arg, | |
d354c7ec PB |
237 | BlockDriverCompletionFunc *cb, void *opaque) |
238 | { | |
239 | ThreadPoolElement *req; | |
240 | ||
d7331bed | 241 | req = qemu_aio_get(&thread_pool_aiocb_info, NULL, cb, opaque); |
d354c7ec PB |
242 | req->func = func; |
243 | req->arg = arg; | |
244 | req->state = THREAD_QUEUED; | |
b811203c | 245 | req->pool = pool; |
d354c7ec | 246 | |
b811203c | 247 | QLIST_INSERT_HEAD(&pool->head, req, all); |
d354c7ec | 248 | |
b811203c | 249 | trace_thread_pool_submit(pool, req, arg); |
d354c7ec | 250 | |
b811203c SH |
251 | qemu_mutex_lock(&pool->lock); |
252 | if (pool->idle_threads == 0 && pool->cur_threads < pool->max_threads) { | |
253 | spawn_thread(pool); | |
d354c7ec | 254 | } |
b811203c SH |
255 | QTAILQ_INSERT_TAIL(&pool->request_list, req, reqs); |
256 | qemu_mutex_unlock(&pool->lock); | |
257 | qemu_sem_post(&pool->sem); | |
d354c7ec PB |
258 | return &req->common; |
259 | } | |
260 | ||
261 | typedef struct ThreadPoolCo { | |
262 | Coroutine *co; | |
263 | int ret; | |
264 | } ThreadPoolCo; | |
265 | ||
266 | static void thread_pool_co_cb(void *opaque, int ret) | |
267 | { | |
268 | ThreadPoolCo *co = opaque; | |
269 | ||
270 | co->ret = ret; | |
271 | qemu_coroutine_enter(co->co, NULL); | |
272 | } | |
273 | ||
c4d9d196 SH |
274 | int coroutine_fn thread_pool_submit_co(ThreadPool *pool, ThreadPoolFunc *func, |
275 | void *arg) | |
d354c7ec PB |
276 | { |
277 | ThreadPoolCo tpc = { .co = qemu_coroutine_self(), .ret = -EINPROGRESS }; | |
278 | assert(qemu_in_coroutine()); | |
c4d9d196 | 279 | thread_pool_submit_aio(pool, func, arg, thread_pool_co_cb, &tpc); |
d354c7ec PB |
280 | qemu_coroutine_yield(); |
281 | return tpc.ret; | |
282 | } | |
283 | ||
c4d9d196 | 284 | void thread_pool_submit(ThreadPool *pool, ThreadPoolFunc *func, void *arg) |
d354c7ec | 285 | { |
c4d9d196 | 286 | thread_pool_submit_aio(pool, func, arg, NULL, NULL); |
d354c7ec PB |
287 | } |
288 | ||
b811203c SH |
289 | static void thread_pool_init_one(ThreadPool *pool, AioContext *ctx) |
290 | { | |
291 | if (!ctx) { | |
292 | ctx = qemu_get_aio_context(); | |
293 | } | |
294 | ||
295 | memset(pool, 0, sizeof(*pool)); | |
296 | event_notifier_init(&pool->notifier, false); | |
f7311ccc | 297 | pool->ctx = ctx; |
b811203c SH |
298 | qemu_mutex_init(&pool->lock); |
299 | qemu_cond_init(&pool->check_cancel); | |
f7311ccc | 300 | qemu_cond_init(&pool->worker_stopped); |
b811203c SH |
301 | qemu_sem_init(&pool->sem, 0); |
302 | pool->max_threads = 64; | |
303 | pool->new_thread_bh = aio_bh_new(ctx, spawn_thread_bh_fn, pool); | |
304 | ||
305 | QLIST_INIT(&pool->head); | |
306 | QTAILQ_INIT(&pool->request_list); | |
307 | ||
f2e5dca4 | 308 | aio_set_event_notifier(ctx, &pool->notifier, event_notifier_ready); |
b811203c SH |
309 | } |
310 | ||
f7311ccc SH |
311 | ThreadPool *thread_pool_new(AioContext *ctx) |
312 | { | |
313 | ThreadPool *pool = g_new(ThreadPool, 1); | |
314 | thread_pool_init_one(pool, ctx); | |
315 | return pool; | |
316 | } | |
317 | ||
318 | void thread_pool_free(ThreadPool *pool) | |
319 | { | |
320 | if (!pool) { | |
321 | return; | |
322 | } | |
323 | ||
324 | assert(QLIST_EMPTY(&pool->head)); | |
325 | ||
326 | qemu_mutex_lock(&pool->lock); | |
327 | ||
328 | /* Stop new threads from spawning */ | |
329 | qemu_bh_delete(pool->new_thread_bh); | |
330 | pool->cur_threads -= pool->new_threads; | |
331 | pool->new_threads = 0; | |
332 | ||
333 | /* Wait for worker threads to terminate */ | |
334 | pool->stopping = true; | |
335 | while (pool->cur_threads > 0) { | |
336 | qemu_sem_post(&pool->sem); | |
337 | qemu_cond_wait(&pool->worker_stopped, &pool->lock); | |
338 | } | |
339 | ||
340 | qemu_mutex_unlock(&pool->lock); | |
341 | ||
f2e5dca4 | 342 | aio_set_event_notifier(pool->ctx, &pool->notifier, NULL); |
f7311ccc SH |
343 | qemu_sem_destroy(&pool->sem); |
344 | qemu_cond_destroy(&pool->check_cancel); | |
345 | qemu_cond_destroy(&pool->worker_stopped); | |
346 | qemu_mutex_destroy(&pool->lock); | |
347 | event_notifier_cleanup(&pool->notifier); | |
348 | g_free(pool); | |
349 | } |