thread-pool.c 9.0 KB

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