2
0

thread-pool.c 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353
  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/queue.h"
  19. #include "qemu/thread.h"
  20. #include "qemu/coroutine.h"
  21. #include "trace.h"
  22. #include "block/thread-pool.h"
  23. #include "qemu/main-loop.h"
  24. static void do_spawn_thread(ThreadPool *pool);
  25. typedef struct ThreadPoolElement ThreadPoolElement;
  26. enum ThreadState {
  27. THREAD_QUEUED,
  28. THREAD_ACTIVE,
  29. THREAD_DONE,
  30. };
  31. struct ThreadPoolElement {
  32. BlockAIOCB common;
  33. ThreadPool *pool;
  34. ThreadPoolFunc *func;
  35. void *arg;
  36. /* Moving state out of THREAD_QUEUED is protected by lock. After
  37. * that, only the worker thread can write to it. Reads and writes
  38. * of state and ret are ordered with memory barriers.
  39. */
  40. enum ThreadState state;
  41. int ret;
  42. /* Access to this list is protected by lock. */
  43. QTAILQ_ENTRY(ThreadPoolElement) reqs;
  44. /* Access to this list is protected by the global mutex. */
  45. QLIST_ENTRY(ThreadPoolElement) all;
  46. };
  47. struct ThreadPool {
  48. AioContext *ctx;
  49. QEMUBH *completion_bh;
  50. QemuMutex lock;
  51. QemuCond worker_stopped;
  52. QemuSemaphore sem;
  53. int max_threads;
  54. QEMUBH *new_thread_bh;
  55. /* The following variables are only accessed from one AioContext. */
  56. QLIST_HEAD(, ThreadPoolElement) head;
  57. /* The following variables are protected by lock. */
  58. QTAILQ_HEAD(, ThreadPoolElement) request_list;
  59. int cur_threads;
  60. int idle_threads;
  61. int new_threads; /* backlog of threads we need to create */
  62. int pending_threads; /* threads created but not running yet */
  63. bool stopping;
  64. };
  65. static void *worker_thread(void *opaque)
  66. {
  67. ThreadPool *pool = opaque;
  68. qemu_mutex_lock(&pool->lock);
  69. pool->pending_threads--;
  70. do_spawn_thread(pool);
  71. while (!pool->stopping) {
  72. ThreadPoolElement *req;
  73. int ret;
  74. do {
  75. pool->idle_threads++;
  76. qemu_mutex_unlock(&pool->lock);
  77. ret = qemu_sem_timedwait(&pool->sem, 10000);
  78. qemu_mutex_lock(&pool->lock);
  79. pool->idle_threads--;
  80. } while (ret == -1 && !QTAILQ_EMPTY(&pool->request_list));
  81. if (ret == -1 || pool->stopping) {
  82. break;
  83. }
  84. req = QTAILQ_FIRST(&pool->request_list);
  85. QTAILQ_REMOVE(&pool->request_list, req, reqs);
  86. req->state = THREAD_ACTIVE;
  87. qemu_mutex_unlock(&pool->lock);
  88. ret = req->func(req->arg);
  89. req->ret = ret;
  90. /* Write ret before state. */
  91. smp_wmb();
  92. req->state = THREAD_DONE;
  93. qemu_mutex_lock(&pool->lock);
  94. qemu_bh_schedule(pool->completion_bh);
  95. }
  96. pool->cur_threads--;
  97. qemu_cond_signal(&pool->worker_stopped);
  98. qemu_mutex_unlock(&pool->lock);
  99. return NULL;
  100. }
  101. static void do_spawn_thread(ThreadPool *pool)
  102. {
  103. QemuThread t;
  104. /* Runs with lock taken. */
  105. if (!pool->new_threads) {
  106. return;
  107. }
  108. pool->new_threads--;
  109. pool->pending_threads++;
  110. qemu_thread_create(&t, "worker", worker_thread, pool, QEMU_THREAD_DETACHED);
  111. }
  112. static void spawn_thread_bh_fn(void *opaque)
  113. {
  114. ThreadPool *pool = opaque;
  115. qemu_mutex_lock(&pool->lock);
  116. do_spawn_thread(pool);
  117. qemu_mutex_unlock(&pool->lock);
  118. }
  119. static void spawn_thread(ThreadPool *pool)
  120. {
  121. pool->cur_threads++;
  122. pool->new_threads++;
  123. /* If there are threads being created, they will spawn new workers, so
  124. * we don't spend time creating many threads in a loop holding a mutex or
  125. * starving the current vcpu.
  126. *
  127. * If there are no idle threads, ask the main thread to create one, so we
  128. * inherit the correct affinity instead of the vcpu affinity.
  129. */
  130. if (!pool->pending_threads) {
  131. qemu_bh_schedule(pool->new_thread_bh);
  132. }
  133. }
  134. static void thread_pool_completion_bh(void *opaque)
  135. {
  136. ThreadPool *pool = opaque;
  137. ThreadPoolElement *elem, *next;
  138. aio_context_acquire(pool->ctx);
  139. restart:
  140. QLIST_FOREACH_SAFE(elem, &pool->head, all, next) {
  141. if (elem->state != THREAD_DONE) {
  142. continue;
  143. }
  144. trace_thread_pool_complete(pool, elem, elem->common.opaque,
  145. elem->ret);
  146. QLIST_REMOVE(elem, all);
  147. if (elem->common.cb) {
  148. /* Read state before ret. */
  149. smp_rmb();
  150. /* Schedule ourselves in case elem->common.cb() calls aio_poll() to
  151. * wait for another request that completed at the same time.
  152. */
  153. qemu_bh_schedule(pool->completion_bh);
  154. aio_context_release(pool->ctx);
  155. elem->common.cb(elem->common.opaque, elem->ret);
  156. aio_context_acquire(pool->ctx);
  157. /* We can safely cancel the completion_bh here regardless of someone
  158. * else having scheduled it meanwhile because we reenter the
  159. * completion function anyway (goto restart).
  160. */
  161. qemu_bh_cancel(pool->completion_bh);
  162. qemu_aio_unref(elem);
  163. goto restart;
  164. } else {
  165. qemu_aio_unref(elem);
  166. }
  167. }
  168. aio_context_release(pool->ctx);
  169. }
  170. static void thread_pool_cancel(BlockAIOCB *acb)
  171. {
  172. ThreadPoolElement *elem = (ThreadPoolElement *)acb;
  173. ThreadPool *pool = elem->pool;
  174. trace_thread_pool_cancel(elem, elem->common.opaque);
  175. qemu_mutex_lock(&pool->lock);
  176. if (elem->state == THREAD_QUEUED &&
  177. /* No thread has yet started working on elem. we can try to "steal"
  178. * the item from the worker if we can get a signal from the
  179. * semaphore. Because this is non-blocking, we can do it with
  180. * the lock taken and ensure that elem will remain THREAD_QUEUED.
  181. */
  182. qemu_sem_timedwait(&pool->sem, 0) == 0) {
  183. QTAILQ_REMOVE(&pool->request_list, elem, reqs);
  184. qemu_bh_schedule(pool->completion_bh);
  185. elem->state = THREAD_DONE;
  186. elem->ret = -ECANCELED;
  187. }
  188. qemu_mutex_unlock(&pool->lock);
  189. }
  190. static AioContext *thread_pool_get_aio_context(BlockAIOCB *acb)
  191. {
  192. ThreadPoolElement *elem = (ThreadPoolElement *)acb;
  193. ThreadPool *pool = elem->pool;
  194. return pool->ctx;
  195. }
  196. static const AIOCBInfo thread_pool_aiocb_info = {
  197. .aiocb_size = sizeof(ThreadPoolElement),
  198. .cancel_async = thread_pool_cancel,
  199. .get_aio_context = thread_pool_get_aio_context,
  200. };
  201. BlockAIOCB *thread_pool_submit_aio(ThreadPool *pool,
  202. ThreadPoolFunc *func, void *arg,
  203. BlockCompletionFunc *cb, void *opaque)
  204. {
  205. ThreadPoolElement *req;
  206. req = qemu_aio_get(&thread_pool_aiocb_info, NULL, cb, opaque);
  207. req->func = func;
  208. req->arg = arg;
  209. req->state = THREAD_QUEUED;
  210. req->pool = pool;
  211. QLIST_INSERT_HEAD(&pool->head, req, all);
  212. trace_thread_pool_submit(pool, req, arg);
  213. qemu_mutex_lock(&pool->lock);
  214. if (pool->idle_threads == 0 && pool->cur_threads < pool->max_threads) {
  215. spawn_thread(pool);
  216. }
  217. QTAILQ_INSERT_TAIL(&pool->request_list, req, reqs);
  218. qemu_mutex_unlock(&pool->lock);
  219. qemu_sem_post(&pool->sem);
  220. return &req->common;
  221. }
  222. typedef struct ThreadPoolCo {
  223. Coroutine *co;
  224. int ret;
  225. } ThreadPoolCo;
  226. static void thread_pool_co_cb(void *opaque, int ret)
  227. {
  228. ThreadPoolCo *co = opaque;
  229. co->ret = ret;
  230. aio_co_wake(co->co);
  231. }
  232. int coroutine_fn thread_pool_submit_co(ThreadPool *pool, ThreadPoolFunc *func,
  233. void *arg)
  234. {
  235. ThreadPoolCo tpc = { .co = qemu_coroutine_self(), .ret = -EINPROGRESS };
  236. assert(qemu_in_coroutine());
  237. thread_pool_submit_aio(pool, func, arg, thread_pool_co_cb, &tpc);
  238. qemu_coroutine_yield();
  239. return tpc.ret;
  240. }
  241. void thread_pool_submit(ThreadPool *pool, ThreadPoolFunc *func, void *arg)
  242. {
  243. thread_pool_submit_aio(pool, func, arg, NULL, NULL);
  244. }
  245. static void thread_pool_init_one(ThreadPool *pool, AioContext *ctx)
  246. {
  247. if (!ctx) {
  248. ctx = qemu_get_aio_context();
  249. }
  250. memset(pool, 0, sizeof(*pool));
  251. pool->ctx = ctx;
  252. pool->completion_bh = aio_bh_new(ctx, thread_pool_completion_bh, pool);
  253. qemu_mutex_init(&pool->lock);
  254. qemu_cond_init(&pool->worker_stopped);
  255. qemu_sem_init(&pool->sem, 0);
  256. pool->max_threads = 64;
  257. pool->new_thread_bh = aio_bh_new(ctx, spawn_thread_bh_fn, pool);
  258. QLIST_INIT(&pool->head);
  259. QTAILQ_INIT(&pool->request_list);
  260. }
  261. ThreadPool *thread_pool_new(AioContext *ctx)
  262. {
  263. ThreadPool *pool = g_new(ThreadPool, 1);
  264. thread_pool_init_one(pool, ctx);
  265. return pool;
  266. }
  267. void thread_pool_free(ThreadPool *pool)
  268. {
  269. if (!pool) {
  270. return;
  271. }
  272. assert(QLIST_EMPTY(&pool->head));
  273. qemu_mutex_lock(&pool->lock);
  274. /* Stop new threads from spawning */
  275. qemu_bh_delete(pool->new_thread_bh);
  276. pool->cur_threads -= pool->new_threads;
  277. pool->new_threads = 0;
  278. /* Wait for worker threads to terminate */
  279. pool->stopping = true;
  280. while (pool->cur_threads > 0) {
  281. qemu_sem_post(&pool->sem);
  282. qemu_cond_wait(&pool->worker_stopped, &pool->lock);
  283. }
  284. qemu_mutex_unlock(&pool->lock);
  285. qemu_bh_delete(pool->completion_bh);
  286. qemu_sem_destroy(&pool->sem);
  287. qemu_cond_destroy(&pool->worker_stopped);
  288. qemu_mutex_destroy(&pool->lock);
  289. g_free(pool);
  290. }