thread-pool.c 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289
  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 "qemu/event_notifier.h"
  25. #include "block/thread-pool.h"
  26. static void do_spawn_thread(void);
  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. ThreadPoolFunc *func;
  37. void *arg;
  38. /* Moving state out of THREAD_QUEUED is protected by lock. After
  39. * that, only the worker thread can write to it. Reads and writes
  40. * of state and ret are ordered with memory barriers.
  41. */
  42. enum ThreadState state;
  43. int ret;
  44. /* Access to this list is protected by lock. */
  45. QTAILQ_ENTRY(ThreadPoolElement) reqs;
  46. /* Access to this list is protected by the global mutex. */
  47. QLIST_ENTRY(ThreadPoolElement) all;
  48. };
  49. static EventNotifier notifier;
  50. static QemuMutex lock;
  51. static QemuCond check_cancel;
  52. static QemuSemaphore sem;
  53. static int max_threads = 64;
  54. static QEMUBH *new_thread_bh;
  55. /* The following variables are protected by the global mutex. */
  56. static QLIST_HEAD(, ThreadPoolElement) head;
  57. /* The following variables are protected by lock. */
  58. static QTAILQ_HEAD(, ThreadPoolElement) request_list;
  59. static int cur_threads;
  60. static int idle_threads;
  61. static int new_threads; /* backlog of threads we need to create */
  62. static int pending_threads; /* threads created but not running yet */
  63. static int pending_cancellations; /* whether we need a cond_broadcast */
  64. static void *worker_thread(void *unused)
  65. {
  66. qemu_mutex_lock(&lock);
  67. pending_threads--;
  68. do_spawn_thread();
  69. while (1) {
  70. ThreadPoolElement *req;
  71. int ret;
  72. do {
  73. idle_threads++;
  74. qemu_mutex_unlock(&lock);
  75. ret = qemu_sem_timedwait(&sem, 10000);
  76. qemu_mutex_lock(&lock);
  77. idle_threads--;
  78. } while (ret == -1 && !QTAILQ_EMPTY(&request_list));
  79. if (ret == -1) {
  80. break;
  81. }
  82. req = QTAILQ_FIRST(&request_list);
  83. QTAILQ_REMOVE(&request_list, req, reqs);
  84. req->state = THREAD_ACTIVE;
  85. qemu_mutex_unlock(&lock);
  86. ret = req->func(req->arg);
  87. req->ret = ret;
  88. /* Write ret before state. */
  89. smp_wmb();
  90. req->state = THREAD_DONE;
  91. qemu_mutex_lock(&lock);
  92. if (pending_cancellations) {
  93. qemu_cond_broadcast(&check_cancel);
  94. }
  95. event_notifier_set(&notifier);
  96. }
  97. cur_threads--;
  98. qemu_mutex_unlock(&lock);
  99. return NULL;
  100. }
  101. static void do_spawn_thread(void)
  102. {
  103. QemuThread t;
  104. /* Runs with lock taken. */
  105. if (!new_threads) {
  106. return;
  107. }
  108. new_threads--;
  109. pending_threads++;
  110. qemu_thread_create(&t, worker_thread, NULL, QEMU_THREAD_DETACHED);
  111. }
  112. static void spawn_thread_bh_fn(void *opaque)
  113. {
  114. qemu_mutex_lock(&lock);
  115. do_spawn_thread();
  116. qemu_mutex_unlock(&lock);
  117. }
  118. static void spawn_thread(void)
  119. {
  120. cur_threads++;
  121. new_threads++;
  122. /* If there are threads being created, they will spawn new workers, so
  123. * we don't spend time creating many threads in a loop holding a mutex or
  124. * starving the current vcpu.
  125. *
  126. * If there are no idle threads, ask the main thread to create one, so we
  127. * inherit the correct affinity instead of the vcpu affinity.
  128. */
  129. if (!pending_threads) {
  130. qemu_bh_schedule(new_thread_bh);
  131. }
  132. }
  133. static void event_notifier_ready(EventNotifier *notifier)
  134. {
  135. ThreadPoolElement *elem, *next;
  136. event_notifier_test_and_clear(notifier);
  137. restart:
  138. QLIST_FOREACH_SAFE(elem, &head, all, next) {
  139. if (elem->state != THREAD_CANCELED && elem->state != THREAD_DONE) {
  140. continue;
  141. }
  142. if (elem->state == THREAD_DONE) {
  143. trace_thread_pool_complete(elem, elem->common.opaque, elem->ret);
  144. }
  145. if (elem->state == THREAD_DONE && elem->common.cb) {
  146. QLIST_REMOVE(elem, all);
  147. /* Read state before ret. */
  148. smp_rmb();
  149. elem->common.cb(elem->common.opaque, elem->ret);
  150. qemu_aio_release(elem);
  151. goto restart;
  152. } else {
  153. /* remove the request */
  154. QLIST_REMOVE(elem, all);
  155. qemu_aio_release(elem);
  156. }
  157. }
  158. }
  159. static int thread_pool_active(EventNotifier *notifier)
  160. {
  161. return !QLIST_EMPTY(&head);
  162. }
  163. static void thread_pool_cancel(BlockDriverAIOCB *acb)
  164. {
  165. ThreadPoolElement *elem = (ThreadPoolElement *)acb;
  166. trace_thread_pool_cancel(elem, elem->common.opaque);
  167. qemu_mutex_lock(&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(&sem, 0) == 0) {
  175. QTAILQ_REMOVE(&request_list, elem, reqs);
  176. elem->state = THREAD_CANCELED;
  177. event_notifier_set(&notifier);
  178. } else {
  179. pending_cancellations++;
  180. while (elem->state != THREAD_CANCELED && elem->state != THREAD_DONE) {
  181. qemu_cond_wait(&check_cancel, &lock);
  182. }
  183. pending_cancellations--;
  184. }
  185. qemu_mutex_unlock(&lock);
  186. }
  187. static const AIOCBInfo thread_pool_aiocb_info = {
  188. .aiocb_size = sizeof(ThreadPoolElement),
  189. .cancel = thread_pool_cancel,
  190. };
  191. BlockDriverAIOCB *thread_pool_submit_aio(ThreadPoolFunc *func, void *arg,
  192. BlockDriverCompletionFunc *cb, void *opaque)
  193. {
  194. ThreadPoolElement *req;
  195. req = qemu_aio_get(&thread_pool_aiocb_info, NULL, cb, opaque);
  196. req->func = func;
  197. req->arg = arg;
  198. req->state = THREAD_QUEUED;
  199. QLIST_INSERT_HEAD(&head, req, all);
  200. trace_thread_pool_submit(req, arg);
  201. qemu_mutex_lock(&lock);
  202. if (idle_threads == 0 && cur_threads < max_threads) {
  203. spawn_thread();
  204. }
  205. QTAILQ_INSERT_TAIL(&request_list, req, reqs);
  206. qemu_mutex_unlock(&lock);
  207. qemu_sem_post(&sem);
  208. return &req->common;
  209. }
  210. typedef struct ThreadPoolCo {
  211. Coroutine *co;
  212. int ret;
  213. } ThreadPoolCo;
  214. static void thread_pool_co_cb(void *opaque, int ret)
  215. {
  216. ThreadPoolCo *co = opaque;
  217. co->ret = ret;
  218. qemu_coroutine_enter(co->co, NULL);
  219. }
  220. int coroutine_fn thread_pool_submit_co(ThreadPoolFunc *func, void *arg)
  221. {
  222. ThreadPoolCo tpc = { .co = qemu_coroutine_self(), .ret = -EINPROGRESS };
  223. assert(qemu_in_coroutine());
  224. thread_pool_submit_aio(func, arg, thread_pool_co_cb, &tpc);
  225. qemu_coroutine_yield();
  226. return tpc.ret;
  227. }
  228. void thread_pool_submit(ThreadPoolFunc *func, void *arg)
  229. {
  230. thread_pool_submit_aio(func, arg, NULL, NULL);
  231. }
  232. static void thread_pool_init(void)
  233. {
  234. QLIST_INIT(&head);
  235. event_notifier_init(&notifier, false);
  236. qemu_mutex_init(&lock);
  237. qemu_cond_init(&check_cancel);
  238. qemu_sem_init(&sem, 0);
  239. qemu_aio_set_event_notifier(&notifier, event_notifier_ready,
  240. thread_pool_active);
  241. QTAILQ_INIT(&request_list);
  242. new_thread_bh = qemu_bh_new(spawn_thread_bh_fn, NULL);
  243. }
  244. block_init(thread_pool_init)