2
0

multiple-iothreads.rst 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. Using Multiple ``IOThread``\ s
  2. ==============================
  3. ..
  4. Copyright (c) 2014-2017 Red Hat Inc.
  5. This work is licensed under the terms of the GNU GPL, version 2 or later. See
  6. the COPYING file in the top-level directory.
  7. This document explains the ``IOThread`` feature and how to write code that runs
  8. outside the BQL.
  9. The main loop and ``IOThread``\ s
  10. ---------------------------------
  11. QEMU is an event-driven program that can do several things at once using an
  12. event loop. The VNC server and the QMP monitor are both processed from the
  13. same event loop, which monitors their file descriptors until they become
  14. readable and then invokes a callback.
  15. The default event loop is called the main loop (see ``main-loop.c``). It is
  16. possible to create additional event loop threads using
  17. ``-object iothread,id=my-iothread``.
  18. Side note: The main loop and ``IOThread`` are both event loops but their code is
  19. not shared completely. Sometimes it is useful to remember that although they
  20. are conceptually similar they are currently not interchangeable.
  21. Why ``IOThread``\ s are useful
  22. ------------------------------
  23. ``IOThread``\ s allow the user to control the placement of work. The main loop is a
  24. scalability bottleneck on hosts with many CPUs. Work can be spread across
  25. several ``IOThread``\ s instead of just one main loop. When set up correctly this
  26. can improve I/O latency and reduce jitter seen by the guest.
  27. The main loop is also deeply associated with the BQL, which is a
  28. scalability bottleneck in itself. vCPU threads and the main loop use the BQL
  29. to serialize execution of QEMU code. This mutex is necessary because a lot of
  30. QEMU's code historically was not thread-safe.
  31. The fact that all I/O processing is done in a single main loop and that the
  32. BQL is contended by all vCPU threads and the main loop explain
  33. why it is desirable to place work into ``IOThread``\ s.
  34. The experimental ``virtio-blk`` data-plane implementation has been benchmarked and
  35. shows these effects:
  36. ftp://public.dhe.ibm.com/linux/pdfs/KVM_Virtualized_IO_Performance_Paper.pdf
  37. .. _how-to-program:
  38. How to program for ``IOThread``\ s
  39. ----------------------------------
  40. The main difference between legacy code and new code that can run in an
  41. ``IOThread`` is dealing explicitly with the event loop object, ``AioContext``
  42. (see ``include/block/aio.h``). Code that only works in the main loop
  43. implicitly uses the main loop's ``AioContext``. Code that supports running
  44. in ``IOThread``\ s must be aware of its ``AioContext``.
  45. AioContext supports the following services:
  46. * File descriptor monitoring (read/write/error on POSIX hosts)
  47. * Event notifiers (inter-thread signalling)
  48. * Timers
  49. * Bottom Halves (BH) deferred callbacks
  50. There are several old APIs that use the main loop AioContext:
  51. * LEGACY ``qemu_aio_set_fd_handler()`` - monitor a file descriptor
  52. * LEGACY ``qemu_aio_set_event_notifier()`` - monitor an event notifier
  53. * LEGACY ``timer_new_ms()`` - create a timer
  54. * LEGACY ``qemu_bh_new()`` - create a BH
  55. * LEGACY ``qemu_bh_new_guarded()`` - create a BH with a device re-entrancy guard
  56. * LEGACY ``qemu_aio_wait()`` - run an event loop iteration
  57. Since they implicitly work on the main loop they cannot be used in code that
  58. runs in an ``IOThread``. They might cause a crash or deadlock if called from an
  59. ``IOThread`` since the BQL is not held.
  60. Instead, use the ``AioContext`` functions directly (see ``include/block/aio.h``):
  61. * ``aio_set_fd_handler()`` - monitor a file descriptor
  62. * ``aio_set_event_notifier()`` - monitor an event notifier
  63. * ``aio_timer_new()`` - create a timer
  64. * ``aio_bh_new()`` - create a BH
  65. * ``aio_bh_new_guarded()`` - create a BH with a device re-entrancy guard
  66. * ``aio_poll()`` - run an event loop iteration
  67. The ``qemu_bh_new_guarded``/``aio_bh_new_guarded`` APIs accept a
  68. ``MemReentrancyGuard``
  69. argument, which is used to check for and prevent re-entrancy problems. For
  70. BHs associated with devices, the reentrancy-guard is contained in the
  71. corresponding ``DeviceState`` and named ``mem_reentrancy_guard``.
  72. The ``AioContext`` can be obtained from the ``IOThread`` using
  73. ``iothread_get_aio_context()`` or for the main loop using
  74. ``qemu_get_aio_context()``. Code that takes an ``AioContext`` argument
  75. works both in ``IOThread``\ s or the main loop, depending on which ``AioContext``
  76. instance the caller passes in.
  77. How to synchronize with an ``IOThread``
  78. ---------------------------------------
  79. Variables that can be accessed by multiple threads require some form of
  80. synchronization such as ``qemu_mutex_lock()``, ``rcu_read_lock()``, etc.
  81. ``AioContext`` functions like ``aio_set_fd_handler()``,
  82. ``aio_set_event_notifier()``, ``aio_bh_new()``, and ``aio_timer_new()``
  83. are thread-safe. They can be used to trigger activity in an ``IOThread``.
  84. Side note: the best way to schedule a function call across threads is to call
  85. ``aio_bh_schedule_oneshot()``.
  86. The main loop thread can wait synchronously for a condition using
  87. ``AIO_WAIT_WHILE()``.
  88. ``AioContext`` and the block layer
  89. ----------------------------------
  90. The ``AioContext`` originates from the QEMU block layer, even though nowadays
  91. ``AioContext`` is a generic event loop that can be used by any QEMU subsystem.
  92. The block layer has support for ``AioContext`` integrated. Each
  93. ``BlockDriverState`` is associated with an ``AioContext`` using
  94. ``bdrv_try_change_aio_context()`` and ``bdrv_get_aio_context()``.
  95. This allows block layer code to process I/O inside the
  96. right ``AioContext``. Other subsystems may wish to follow a similar approach.
  97. Block layer code must therefore expect to run in an ``IOThread`` and avoid using
  98. old APIs that implicitly use the main loop. See
  99. `How to program for IOThreads`_ for information on how to do that.
  100. Code running in the monitor typically needs to ensure that past
  101. requests from the guest are completed. When a block device is running
  102. in an ``IOThread``, the ``IOThread`` can also process requests from the guest
  103. (via ioeventfd). To achieve both objects, wrap the code between
  104. ``bdrv_drained_begin()`` and ``bdrv_drained_end()``, thus creating a "drained
  105. section".
  106. Long-running jobs (usually in the form of coroutines) are often scheduled in
  107. the ``BlockDriverState``'s ``AioContext``. The functions
  108. ``bdrv_add``/``remove_aio_context_notifier``, or alternatively
  109. ``blk_add``/``remove_aio_context_notifier`` if you use ``BlockBackends``,
  110. can be used to get a notification whenever ``bdrv_try_change_aio_context()``
  111. moves a ``BlockDriverState`` to a different ``AioContext``.