2
0

thread-pool.c 9.6 KB

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