2
0

thread-pool.c 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345
  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. 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. if (elem->state == THREAD_DONE) {
  145. trace_thread_pool_complete(pool, elem, elem->common.opaque,
  146. elem->ret);
  147. }
  148. if (elem->state == THREAD_DONE && elem->common.cb) {
  149. QLIST_REMOVE(elem, all);
  150. /* Read state before ret. */
  151. smp_rmb();
  152. /* Schedule ourselves in case elem->common.cb() calls aio_poll() to
  153. * wait for another request that completed at the same time.
  154. */
  155. qemu_bh_schedule(pool->completion_bh);
  156. elem->common.cb(elem->common.opaque, elem->ret);
  157. qemu_aio_unref(elem);
  158. goto restart;
  159. } else {
  160. /* remove the request */
  161. QLIST_REMOVE(elem, all);
  162. qemu_aio_unref(elem);
  163. }
  164. }
  165. }
  166. static void thread_pool_cancel(BlockAIOCB *acb)
  167. {
  168. ThreadPoolElement *elem = (ThreadPoolElement *)acb;
  169. ThreadPool *pool = elem->pool;
  170. trace_thread_pool_cancel(elem, elem->common.opaque);
  171. qemu_mutex_lock(&pool->lock);
  172. if (elem->state == THREAD_QUEUED &&
  173. /* No thread has yet started working on elem. we can try to "steal"
  174. * the item from the worker if we can get a signal from the
  175. * semaphore. Because this is non-blocking, we can do it with
  176. * the lock taken and ensure that elem will remain THREAD_QUEUED.
  177. */
  178. qemu_sem_timedwait(&pool->sem, 0) == 0) {
  179. QTAILQ_REMOVE(&pool->request_list, elem, reqs);
  180. qemu_bh_schedule(pool->completion_bh);
  181. elem->state = THREAD_DONE;
  182. elem->ret = -ECANCELED;
  183. }
  184. qemu_mutex_unlock(&pool->lock);
  185. }
  186. static AioContext *thread_pool_get_aio_context(BlockAIOCB *acb)
  187. {
  188. ThreadPoolElement *elem = (ThreadPoolElement *)acb;
  189. ThreadPool *pool = elem->pool;
  190. return pool->ctx;
  191. }
  192. static const AIOCBInfo thread_pool_aiocb_info = {
  193. .aiocb_size = sizeof(ThreadPoolElement),
  194. .cancel_async = thread_pool_cancel,
  195. .get_aio_context = thread_pool_get_aio_context,
  196. };
  197. BlockAIOCB *thread_pool_submit_aio(ThreadPool *pool,
  198. ThreadPoolFunc *func, void *arg,
  199. BlockCompletionFunc *cb, void *opaque)
  200. {
  201. ThreadPoolElement *req;
  202. req = qemu_aio_get(&thread_pool_aiocb_info, NULL, cb, opaque);
  203. req->func = func;
  204. req->arg = arg;
  205. req->state = THREAD_QUEUED;
  206. req->pool = pool;
  207. QLIST_INSERT_HEAD(&pool->head, req, all);
  208. trace_thread_pool_submit(pool, req, arg);
  209. qemu_mutex_lock(&pool->lock);
  210. if (pool->idle_threads == 0 && pool->cur_threads < pool->max_threads) {
  211. spawn_thread(pool);
  212. }
  213. QTAILQ_INSERT_TAIL(&pool->request_list, req, reqs);
  214. qemu_mutex_unlock(&pool->lock);
  215. qemu_sem_post(&pool->sem);
  216. return &req->common;
  217. }
  218. typedef struct ThreadPoolCo {
  219. Coroutine *co;
  220. int ret;
  221. } ThreadPoolCo;
  222. static void thread_pool_co_cb(void *opaque, int ret)
  223. {
  224. ThreadPoolCo *co = opaque;
  225. co->ret = ret;
  226. qemu_coroutine_enter(co->co, NULL);
  227. }
  228. int coroutine_fn thread_pool_submit_co(ThreadPool *pool, ThreadPoolFunc *func,
  229. void *arg)
  230. {
  231. ThreadPoolCo tpc = { .co = qemu_coroutine_self(), .ret = -EINPROGRESS };
  232. assert(qemu_in_coroutine());
  233. thread_pool_submit_aio(pool, func, arg, thread_pool_co_cb, &tpc);
  234. qemu_coroutine_yield();
  235. return tpc.ret;
  236. }
  237. void thread_pool_submit(ThreadPool *pool, ThreadPoolFunc *func, void *arg)
  238. {
  239. thread_pool_submit_aio(pool, func, arg, NULL, NULL);
  240. }
  241. static void thread_pool_init_one(ThreadPool *pool, AioContext *ctx)
  242. {
  243. if (!ctx) {
  244. ctx = qemu_get_aio_context();
  245. }
  246. memset(pool, 0, sizeof(*pool));
  247. pool->ctx = ctx;
  248. pool->completion_bh = aio_bh_new(ctx, thread_pool_completion_bh, pool);
  249. qemu_mutex_init(&pool->lock);
  250. qemu_cond_init(&pool->worker_stopped);
  251. qemu_sem_init(&pool->sem, 0);
  252. pool->max_threads = 64;
  253. pool->new_thread_bh = aio_bh_new(ctx, spawn_thread_bh_fn, pool);
  254. QLIST_INIT(&pool->head);
  255. QTAILQ_INIT(&pool->request_list);
  256. }
  257. ThreadPool *thread_pool_new(AioContext *ctx)
  258. {
  259. ThreadPool *pool = g_new(ThreadPool, 1);
  260. thread_pool_init_one(pool, ctx);
  261. return pool;
  262. }
  263. void thread_pool_free(ThreadPool *pool)
  264. {
  265. if (!pool) {
  266. return;
  267. }
  268. assert(QLIST_EMPTY(&pool->head));
  269. qemu_mutex_lock(&pool->lock);
  270. /* Stop new threads from spawning */
  271. qemu_bh_delete(pool->new_thread_bh);
  272. pool->cur_threads -= pool->new_threads;
  273. pool->new_threads = 0;
  274. /* Wait for worker threads to terminate */
  275. pool->stopping = true;
  276. while (pool->cur_threads > 0) {
  277. qemu_sem_post(&pool->sem);
  278. qemu_cond_wait(&pool->worker_stopped, &pool->lock);
  279. }
  280. qemu_mutex_unlock(&pool->lock);
  281. qemu_bh_delete(pool->completion_bh);
  282. qemu_sem_destroy(&pool->sem);
  283. qemu_cond_destroy(&pool->worker_stopped);
  284. qemu_mutex_destroy(&pool->lock);
  285. g_free(pool);
  286. }