]> rtime.felk.cvut.cz Git - lisovros/qemu_apohw.git/blob - thread-pool.c
Merge remote-tracking branch 'remotes/cohuck/tags/s390x-20150330' into staging
[lisovros/qemu_apohw.git] / thread-pool.c
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-common.h"
18 #include "qemu/queue.h"
19 #include "qemu/thread.h"
20 #include "qemu/osdep.h"
21 #include "block/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(ThreadPool *pool);
27
28 typedef struct ThreadPoolElement ThreadPoolElement;
29
30 enum ThreadState {
31     THREAD_QUEUED,
32     THREAD_ACTIVE,
33     THREAD_DONE,
34 };
35
36 struct ThreadPoolElement {
37     BlockAIOCB common;
38     ThreadPool *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(ThreadPoolElement) reqs;
51
52     /* Access to this list is protected by the global mutex.  */
53     QLIST_ENTRY(ThreadPoolElement) all;
54 };
55
56 struct ThreadPool {
57     AioContext *ctx;
58     QEMUBH *completion_bh;
59     QemuMutex lock;
60     QemuCond worker_stopped;
61     QemuSemaphore sem;
62     int max_threads;
63     QEMUBH *new_thread_bh;
64
65     /* The following variables are only accessed from one AioContext. */
66     QLIST_HEAD(, ThreadPoolElement) head;
67
68     /* 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 */
74     bool stopping;
75 };
76
77 static void *worker_thread(void *opaque)
78 {
79     ThreadPool *pool = opaque;
80
81     qemu_mutex_lock(&pool->lock);
82     pool->pending_threads--;
83     do_spawn_thread(pool);
84
85     while (!pool->stopping) {
86         ThreadPoolElement *req;
87         int ret;
88
89         do {
90             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));
96         if (ret == -1 || pool->stopping) {
97             break;
98         }
99
100         req = QTAILQ_FIRST(&pool->request_list);
101         QTAILQ_REMOVE(&pool->request_list, req, reqs);
102         req->state = THREAD_ACTIVE;
103         qemu_mutex_unlock(&pool->lock);
104
105         ret = req->func(req->arg);
106
107         req->ret = ret;
108         /* Write ret before state.  */
109         smp_wmb();
110         req->state = THREAD_DONE;
111
112         qemu_mutex_lock(&pool->lock);
113
114         qemu_bh_schedule(pool->completion_bh);
115     }
116
117     pool->cur_threads--;
118     qemu_cond_signal(&pool->worker_stopped);
119     qemu_mutex_unlock(&pool->lock);
120     return NULL;
121 }
122
123 static void do_spawn_thread(ThreadPool *pool)
124 {
125     QemuThread t;
126
127     /* Runs with lock taken.  */
128     if (!pool->new_threads) {
129         return;
130     }
131
132     pool->new_threads--;
133     pool->pending_threads++;
134
135     qemu_thread_create(&t, "worker", worker_thread, pool, QEMU_THREAD_DETACHED);
136 }
137
138 static void spawn_thread_bh_fn(void *opaque)
139 {
140     ThreadPool *pool = opaque;
141
142     qemu_mutex_lock(&pool->lock);
143     do_spawn_thread(pool);
144     qemu_mutex_unlock(&pool->lock);
145 }
146
147 static void spawn_thread(ThreadPool *pool)
148 {
149     pool->cur_threads++;
150     pool->new_threads++;
151     /* 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      */
158     if (!pool->pending_threads) {
159         qemu_bh_schedule(pool->new_thread_bh);
160     }
161 }
162
163 static void thread_pool_completion_bh(void *opaque)
164 {
165     ThreadPool *pool = opaque;
166     ThreadPoolElement *elem, *next;
167
168 restart:
169     QLIST_FOREACH_SAFE(elem, &pool->head, all, next) {
170         if (elem->state != THREAD_DONE) {
171             continue;
172         }
173         if (elem->state == THREAD_DONE) {
174             trace_thread_pool_complete(pool, elem, elem->common.opaque,
175                                        elem->ret);
176         }
177         if (elem->state == THREAD_DONE && elem->common.cb) {
178             QLIST_REMOVE(elem, all);
179             /* Read state before ret.  */
180             smp_rmb();
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
187             elem->common.cb(elem->common.opaque, elem->ret);
188             qemu_aio_unref(elem);
189             goto restart;
190         } else {
191             /* remove the request */
192             QLIST_REMOVE(elem, all);
193             qemu_aio_unref(elem);
194         }
195     }
196 }
197
198 static void thread_pool_cancel(BlockAIOCB *acb)
199 {
200     ThreadPoolElement *elem = (ThreadPoolElement *)acb;
201     ThreadPool *pool = elem->pool;
202
203     trace_thread_pool_cancel(elem, elem->common.opaque);
204
205     qemu_mutex_lock(&pool->lock);
206     if (elem->state == THREAD_QUEUED &&
207         /* No thread has yet started working on elem. we can try to "steal"
208          * the item from the worker if we can get a signal from the
209          * semaphore.  Because this is non-blocking, we can do it with
210          * the lock taken and ensure that elem will remain THREAD_QUEUED.
211          */
212         qemu_sem_timedwait(&pool->sem, 0) == 0) {
213         QTAILQ_REMOVE(&pool->request_list, elem, reqs);
214         qemu_bh_schedule(pool->completion_bh);
215
216         elem->state = THREAD_DONE;
217         elem->ret = -ECANCELED;
218     }
219
220     qemu_mutex_unlock(&pool->lock);
221 }
222
223 static AioContext *thread_pool_get_aio_context(BlockAIOCB *acb)
224 {
225     ThreadPoolElement *elem = (ThreadPoolElement *)acb;
226     ThreadPool *pool = elem->pool;
227     return pool->ctx;
228 }
229
230 static const AIOCBInfo thread_pool_aiocb_info = {
231     .aiocb_size         = sizeof(ThreadPoolElement),
232     .cancel_async       = thread_pool_cancel,
233     .get_aio_context    = thread_pool_get_aio_context,
234 };
235
236 BlockAIOCB *thread_pool_submit_aio(ThreadPool *pool,
237         ThreadPoolFunc *func, void *arg,
238         BlockCompletionFunc *cb, void *opaque)
239 {
240     ThreadPoolElement *req;
241
242     req = qemu_aio_get(&thread_pool_aiocb_info, NULL, cb, opaque);
243     req->func = func;
244     req->arg = arg;
245     req->state = THREAD_QUEUED;
246     req->pool = pool;
247
248     QLIST_INSERT_HEAD(&pool->head, req, all);
249
250     trace_thread_pool_submit(pool, req, arg);
251
252     qemu_mutex_lock(&pool->lock);
253     if (pool->idle_threads == 0 && pool->cur_threads < pool->max_threads) {
254         spawn_thread(pool);
255     }
256     QTAILQ_INSERT_TAIL(&pool->request_list, req, reqs);
257     qemu_mutex_unlock(&pool->lock);
258     qemu_sem_post(&pool->sem);
259     return &req->common;
260 }
261
262 typedef struct ThreadPoolCo {
263     Coroutine *co;
264     int ret;
265 } ThreadPoolCo;
266
267 static void thread_pool_co_cb(void *opaque, int ret)
268 {
269     ThreadPoolCo *co = opaque;
270
271     co->ret = ret;
272     qemu_coroutine_enter(co->co, NULL);
273 }
274
275 int coroutine_fn thread_pool_submit_co(ThreadPool *pool, ThreadPoolFunc *func,
276                                        void *arg)
277 {
278     ThreadPoolCo tpc = { .co = qemu_coroutine_self(), .ret = -EINPROGRESS };
279     assert(qemu_in_coroutine());
280     thread_pool_submit_aio(pool, func, arg, thread_pool_co_cb, &tpc);
281     qemu_coroutine_yield();
282     return tpc.ret;
283 }
284
285 void thread_pool_submit(ThreadPool *pool, ThreadPoolFunc *func, void *arg)
286 {
287     thread_pool_submit_aio(pool, func, arg, NULL, NULL);
288 }
289
290 static void thread_pool_init_one(ThreadPool *pool, AioContext *ctx)
291 {
292     if (!ctx) {
293         ctx = qemu_get_aio_context();
294     }
295
296     memset(pool, 0, sizeof(*pool));
297     pool->ctx = ctx;
298     pool->completion_bh = aio_bh_new(ctx, thread_pool_completion_bh, pool);
299     qemu_mutex_init(&pool->lock);
300     qemu_cond_init(&pool->worker_stopped);
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 }
308
309 ThreadPool *thread_pool_new(AioContext *ctx)
310 {
311     ThreadPool *pool = g_new(ThreadPool, 1);
312     thread_pool_init_one(pool, ctx);
313     return pool;
314 }
315
316 void thread_pool_free(ThreadPool *pool)
317 {
318     if (!pool) {
319         return;
320     }
321
322     assert(QLIST_EMPTY(&pool->head));
323
324     qemu_mutex_lock(&pool->lock);
325
326     /* Stop new threads from spawning */
327     qemu_bh_delete(pool->new_thread_bh);
328     pool->cur_threads -= pool->new_threads;
329     pool->new_threads = 0;
330
331     /* Wait for worker threads to terminate */
332     pool->stopping = true;
333     while (pool->cur_threads > 0) {
334         qemu_sem_post(&pool->sem);
335         qemu_cond_wait(&pool->worker_stopped, &pool->lock);
336     }
337
338     qemu_mutex_unlock(&pool->lock);
339
340     qemu_bh_delete(pool->completion_bh);
341     qemu_sem_destroy(&pool->sem);
342     qemu_cond_destroy(&pool->worker_stopped);
343     qemu_mutex_destroy(&pool->lock);
344     g_free(pool);
345 }