2
0

thread-pool.c 9.6 KB

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