iothread.c 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311
  1. /*
  2. * Event loop thread
  3. *
  4. * Copyright Red Hat Inc., 2013
  5. *
  6. * Authors:
  7. * Stefan Hajnoczi <stefanha@redhat.com>
  8. *
  9. * This work is licensed under the terms of the GNU GPL, version 2 or later.
  10. * See the COPYING file in the top-level directory.
  11. *
  12. */
  13. #include "qemu/osdep.h"
  14. #include "qom/object.h"
  15. #include "qom/object_interfaces.h"
  16. #include "qemu/module.h"
  17. #include "block/aio.h"
  18. #include "block/block.h"
  19. #include "sysemu/iothread.h"
  20. #include "qmp-commands.h"
  21. #include "qemu/error-report.h"
  22. #include "qemu/rcu.h"
  23. #include "qemu/main-loop.h"
  24. typedef ObjectClass IOThreadClass;
  25. #define IOTHREAD_GET_CLASS(obj) \
  26. OBJECT_GET_CLASS(IOThreadClass, obj, TYPE_IOTHREAD)
  27. #define IOTHREAD_CLASS(klass) \
  28. OBJECT_CLASS_CHECK(IOThreadClass, klass, TYPE_IOTHREAD)
  29. /* Benchmark results from 2016 on NVMe SSD drives show max polling times around
  30. * 16-32 microseconds yield IOPS improvements for both iodepth=1 and iodepth=32
  31. * workloads.
  32. */
  33. #define IOTHREAD_POLL_MAX_NS_DEFAULT 32768ULL
  34. static __thread IOThread *my_iothread;
  35. AioContext *qemu_get_current_aio_context(void)
  36. {
  37. return my_iothread ? my_iothread->ctx : qemu_get_aio_context();
  38. }
  39. static void *iothread_run(void *opaque)
  40. {
  41. IOThread *iothread = opaque;
  42. rcu_register_thread();
  43. my_iothread = iothread;
  44. qemu_mutex_lock(&iothread->init_done_lock);
  45. iothread->thread_id = qemu_get_thread_id();
  46. qemu_cond_signal(&iothread->init_done_cond);
  47. qemu_mutex_unlock(&iothread->init_done_lock);
  48. while (!atomic_read(&iothread->stopping)) {
  49. aio_poll(iothread->ctx, true);
  50. }
  51. rcu_unregister_thread();
  52. return NULL;
  53. }
  54. static int iothread_stop(Object *object, void *opaque)
  55. {
  56. IOThread *iothread;
  57. iothread = (IOThread *)object_dynamic_cast(object, TYPE_IOTHREAD);
  58. if (!iothread || !iothread->ctx) {
  59. return 0;
  60. }
  61. iothread->stopping = true;
  62. aio_notify(iothread->ctx);
  63. qemu_thread_join(&iothread->thread);
  64. return 0;
  65. }
  66. static void iothread_instance_init(Object *obj)
  67. {
  68. IOThread *iothread = IOTHREAD(obj);
  69. iothread->poll_max_ns = IOTHREAD_POLL_MAX_NS_DEFAULT;
  70. }
  71. static void iothread_instance_finalize(Object *obj)
  72. {
  73. IOThread *iothread = IOTHREAD(obj);
  74. iothread_stop(obj, NULL);
  75. qemu_cond_destroy(&iothread->init_done_cond);
  76. qemu_mutex_destroy(&iothread->init_done_lock);
  77. if (!iothread->ctx) {
  78. return;
  79. }
  80. aio_context_unref(iothread->ctx);
  81. }
  82. static void iothread_complete(UserCreatable *obj, Error **errp)
  83. {
  84. Error *local_error = NULL;
  85. IOThread *iothread = IOTHREAD(obj);
  86. char *name, *thread_name;
  87. iothread->stopping = false;
  88. iothread->thread_id = -1;
  89. iothread->ctx = aio_context_new(&local_error);
  90. if (!iothread->ctx) {
  91. error_propagate(errp, local_error);
  92. return;
  93. }
  94. aio_context_set_poll_params(iothread->ctx,
  95. iothread->poll_max_ns,
  96. iothread->poll_grow,
  97. iothread->poll_shrink,
  98. &local_error);
  99. if (local_error) {
  100. error_propagate(errp, local_error);
  101. aio_context_unref(iothread->ctx);
  102. iothread->ctx = NULL;
  103. return;
  104. }
  105. qemu_mutex_init(&iothread->init_done_lock);
  106. qemu_cond_init(&iothread->init_done_cond);
  107. /* This assumes we are called from a thread with useful CPU affinity for us
  108. * to inherit.
  109. */
  110. name = object_get_canonical_path_component(OBJECT(obj));
  111. thread_name = g_strdup_printf("IO %s", name);
  112. qemu_thread_create(&iothread->thread, thread_name, iothread_run,
  113. iothread, QEMU_THREAD_JOINABLE);
  114. g_free(thread_name);
  115. g_free(name);
  116. /* Wait for initialization to complete */
  117. qemu_mutex_lock(&iothread->init_done_lock);
  118. while (iothread->thread_id == -1) {
  119. qemu_cond_wait(&iothread->init_done_cond,
  120. &iothread->init_done_lock);
  121. }
  122. qemu_mutex_unlock(&iothread->init_done_lock);
  123. }
  124. typedef struct {
  125. const char *name;
  126. ptrdiff_t offset; /* field's byte offset in IOThread struct */
  127. } PollParamInfo;
  128. static PollParamInfo poll_max_ns_info = {
  129. "poll-max-ns", offsetof(IOThread, poll_max_ns),
  130. };
  131. static PollParamInfo poll_grow_info = {
  132. "poll-grow", offsetof(IOThread, poll_grow),
  133. };
  134. static PollParamInfo poll_shrink_info = {
  135. "poll-shrink", offsetof(IOThread, poll_shrink),
  136. };
  137. static void iothread_get_poll_param(Object *obj, Visitor *v,
  138. const char *name, void *opaque, Error **errp)
  139. {
  140. IOThread *iothread = IOTHREAD(obj);
  141. PollParamInfo *info = opaque;
  142. int64_t *field = (void *)iothread + info->offset;
  143. visit_type_int64(v, name, field, errp);
  144. }
  145. static void iothread_set_poll_param(Object *obj, Visitor *v,
  146. const char *name, void *opaque, Error **errp)
  147. {
  148. IOThread *iothread = IOTHREAD(obj);
  149. PollParamInfo *info = opaque;
  150. int64_t *field = (void *)iothread + info->offset;
  151. Error *local_err = NULL;
  152. int64_t value;
  153. visit_type_int64(v, name, &value, &local_err);
  154. if (local_err) {
  155. goto out;
  156. }
  157. if (value < 0) {
  158. error_setg(&local_err, "%s value must be in range [0, %"PRId64"]",
  159. info->name, INT64_MAX);
  160. goto out;
  161. }
  162. *field = value;
  163. if (iothread->ctx) {
  164. aio_context_set_poll_params(iothread->ctx,
  165. iothread->poll_max_ns,
  166. iothread->poll_grow,
  167. iothread->poll_shrink,
  168. &local_err);
  169. }
  170. out:
  171. error_propagate(errp, local_err);
  172. }
  173. static void iothread_class_init(ObjectClass *klass, void *class_data)
  174. {
  175. UserCreatableClass *ucc = USER_CREATABLE_CLASS(klass);
  176. ucc->complete = iothread_complete;
  177. object_class_property_add(klass, "poll-max-ns", "int",
  178. iothread_get_poll_param,
  179. iothread_set_poll_param,
  180. NULL, &poll_max_ns_info, &error_abort);
  181. object_class_property_add(klass, "poll-grow", "int",
  182. iothread_get_poll_param,
  183. iothread_set_poll_param,
  184. NULL, &poll_grow_info, &error_abort);
  185. object_class_property_add(klass, "poll-shrink", "int",
  186. iothread_get_poll_param,
  187. iothread_set_poll_param,
  188. NULL, &poll_shrink_info, &error_abort);
  189. }
  190. static const TypeInfo iothread_info = {
  191. .name = TYPE_IOTHREAD,
  192. .parent = TYPE_OBJECT,
  193. .class_init = iothread_class_init,
  194. .instance_size = sizeof(IOThread),
  195. .instance_init = iothread_instance_init,
  196. .instance_finalize = iothread_instance_finalize,
  197. .interfaces = (InterfaceInfo[]) {
  198. {TYPE_USER_CREATABLE},
  199. {}
  200. },
  201. };
  202. static void iothread_register_types(void)
  203. {
  204. type_register_static(&iothread_info);
  205. }
  206. type_init(iothread_register_types)
  207. char *iothread_get_id(IOThread *iothread)
  208. {
  209. return object_get_canonical_path_component(OBJECT(iothread));
  210. }
  211. AioContext *iothread_get_aio_context(IOThread *iothread)
  212. {
  213. return iothread->ctx;
  214. }
  215. static int query_one_iothread(Object *object, void *opaque)
  216. {
  217. IOThreadInfoList ***prev = opaque;
  218. IOThreadInfoList *elem;
  219. IOThreadInfo *info;
  220. IOThread *iothread;
  221. iothread = (IOThread *)object_dynamic_cast(object, TYPE_IOTHREAD);
  222. if (!iothread) {
  223. return 0;
  224. }
  225. info = g_new0(IOThreadInfo, 1);
  226. info->id = iothread_get_id(iothread);
  227. info->thread_id = iothread->thread_id;
  228. info->poll_max_ns = iothread->poll_max_ns;
  229. info->poll_grow = iothread->poll_grow;
  230. info->poll_shrink = iothread->poll_shrink;
  231. elem = g_new0(IOThreadInfoList, 1);
  232. elem->value = info;
  233. elem->next = NULL;
  234. **prev = elem;
  235. *prev = &elem->next;
  236. return 0;
  237. }
  238. IOThreadInfoList *qmp_query_iothreads(Error **errp)
  239. {
  240. IOThreadInfoList *head = NULL;
  241. IOThreadInfoList **prev = &head;
  242. Object *container = object_get_objects_root();
  243. object_child_foreach(container, query_one_iothread, &prev);
  244. return head;
  245. }
  246. void iothread_stop_all(void)
  247. {
  248. Object *container = object_get_objects_root();
  249. BlockDriverState *bs;
  250. BdrvNextIterator it;
  251. for (bs = bdrv_first(&it); bs; bs = bdrv_next(&it)) {
  252. AioContext *ctx = bdrv_get_aio_context(bs);
  253. if (ctx == qemu_get_aio_context()) {
  254. continue;
  255. }
  256. aio_context_acquire(ctx);
  257. bdrv_set_aio_context(bs, qemu_get_aio_context());
  258. aio_context_release(ctx);
  259. }
  260. object_child_foreach(container, iothread_stop, NULL);
  261. }