virtio-blk: use blk_io_plug_call() instead of notification BH
Subsystems (2)
v2 cover virtio-blk: use blk_io_plug_call() instead of notification BH
v2:
- Rename blk_io_plug() to defer_call() and move it to util/ so the net subsystem can use it [Ilya]
- Add defer_call_begin()/end() to thread_pool_completion_bh() to match Linux AIO and io_uring completion batching
Replace the seldom-used virtio-blk notification BH mechanism with blk_io_plug(). This is part of an effort to enable the multi-queue block layer in virtio-blk. The notification BH was not multi-queue friendly.
The blk_io_plug() mechanism improves fio rw=randread bs=4k iodepth=64 numjobs=8 IOPS by ~9% with a single IOThread and 8 vCPUs (this is not even a multi-queue block layer configuration) compared to no completion batching. iodepth=1 decreases by ~1% but this could be noise. Benchmark details are available here:
https://gitlab.com/stefanha/virt-playbooks/-/tree/blk_io_plug-irqfd
Stefan Hajnoczi (4):
block: rename blk_io_plug_call() API to defer_call() util/defer-call: move defer_call() to util/
virtio: use defer_call() in virtio_irqfd_notify()
virtio-blk: remove batch notification BH
MAINTAINERS | 3 +- include/qemu/defer-call.h | 15 +++ include/sysemu/block-backend-io.h | 4 - block/blkio.c | 9 +- block/io_uring.c | 11 ++- block/linux-aio.c | 9 +- block/nvme.c | 5 +- block/plug.c | 159 ------------------------------ hw/block/dataplane/virtio-blk.c | 48 +-------- hw/block/dataplane/xen-block.c | 11 ++- hw/block/virtio-blk.c | 5 +- hw/scsi/virtio-scsi.c | 7 +- hw/virtio/virtio.c | 11 ++- util/defer-call.c | 156 +++++++++++++++++++++++++++++ util/thread-pool.c | 5 + block/meson.build | 1 - util/meson.build | 1 + 17 files changed, 227 insertions(+), 233 deletions(-) create mode 100644 include/qemu/defer-call.h delete mode 100644 block/plug.c create mode 100644 util/defer-call.c
Signature · 1 line
2.41.0
PATCH v2 1/4 block: rename blk_io_plug_call() API to defer_call()
Prepare to move the blk_io_plug_call() API out of the block layer so that other subsystems call use this deferred call mechanism. Rename it to defer_call() but leave the code in block/plug.c.
The next commit will move the code out of the block layer.
--- include/sysemu/block-backend-io.h | 6 +- block/blkio.c | 8 +-- block/io_uring.c | 4 +- block/linux-aio.c | 4 +- block/nvme.c | 4 +- block/plug.c | 109 +++++++++++++++--------------- hw/block/dataplane/xen-block.c | 10 +-- hw/block/virtio-blk.c | 4 +- hw/scsi/virtio-scsi.c | 6 +- 9 files changed, 76 insertions(+), 79 deletions(-)
include/sysemu/block-backend-io.h +3 -3
@@ -100,9 +100,9 @@ void blk_iostatus_set_err(BlockBackend *blk, int error);int blk_get_max_iov (BlockBackend *blk);int blk_get_max_hw_iov (BlockBackend *blk); -void blk_io_plug (void ); -void blk_io_unplug (void ); -void blk_io_plug_call (void (*fn)(void *),void *opaque); +void defer_call_begin (void ); +void defer_call_end (void ); +void defer_call (void (*fn)(void *),void *opaque);AioContext *blk_get_aio_context (BlockBackend *blk);BlockAcctStats *blk_get_stats (BlockBackend *blk);
block/blkio.c +4 -4
@@ -312,10 +312,10 @@ static void blkio_detach_aio_context(BlockDriverState *bs) }/* -* Called by blk_io_unplug() or immediately if not plugged. Called without -* blkio_lock. +* Called by defer_call_end() or immediately if not in a deferred section. +* Called without blkio_lock. */ -static void blkio_unplug_fn (void *opaque) +static void blkio_deferred_fn (void *opaque) {BDRVBlkioState *s = opaque;
@@ -332,7 +332,7 @@ static void blkio_submit_io(BlockDriverState *bs) {BDRVBlkioState *s = bs->opaque; -blk_io_plug_call (blkio_unplug_fn, s); +defer_call (blkio_deferred_fn, s); }static int coroutine_fn
block/io_uring.c +2 -2
@@ -306,7 +306,7 @@ static void ioq_init(LuringQueue *io_q) io_q->blocked = false; } -static void luring_unplug_fn (void *opaque) +static void luring_deferred_fn (void *opaque) {LuringState *s = opaque; trace_luring_unplug_fn(s, s->io_q.blocked, s->io_q.in_queue,
@@ -367,7 +367,7 @@ static int luring_do_submit(int fd, LuringAIOCB *luringcb, LuringState *s,return ret; } -blk_io_plug_call (luring_unplug_fn, s); +defer_call (luring_deferred_fn, s); }return 0 ; }
block/linux-aio.c +2 -2
@@ -353,7 +353,7 @@ static uint64_t laio_max_batch(LinuxAioState *s, uint64_t dev_max_batch)return max_batch; } -static void laio_unplug_fn (void *opaque) +static void laio_deferred_fn (void *opaque) {LinuxAioState *s = opaque;
@@ -393,7 +393,7 @@ static int laio_do_submit(int fd, struct qemu_laiocb *laiocb, off_t offset,if (s->io_q.in_queue >=laio_max_batch (s, dev_max_batch)) {ioq_submit (s); }else { -blk_io_plug_call (laio_unplug_fn, s); +defer_call (laio_deferred_fn, s); } }
block/nvme.c +2 -2
@@ -476,7 +476,7 @@ static void nvme_trace_command(const NvmeCmd *cmd) } } -static void nvme_unplug_fn (void *opaque) +static void nvme_deferred_fn (void *opaque) {NVMeQueuePair *q = opaque;
@@ -503,7 +503,7 @@ static void nvme_submit_command(NVMeQueuePair *q, NVMeRequest *req, q->need_kick++;qemu_mutex_unlock (&q->lock); -blk_io_plug_call (nvme_unplug_fn, q); +defer_call (nvme_deferred_fn, q); }static void nvme_admin_cmd_sync_cb (void *opaque,int ret)
block/plug.c +53 -56
189 lines not on this page · raw message
hw/block/dataplane/xen-block.c +5 -5
@@ -509,7 +509,7 @@ static int xen_block_get_request(XenBlockDataPlane *dataplane,/* * Threshold of in-flight requests above which we will start using -* blk_io_plug()/blk_io_unplug() to batch requests. +* defer_call_begin()/defer_call_end() to batch requests. */ #define IO_PLUG_THRESHOLD 1
@@ -537,7 +537,7 @@ static bool xen_block_handle_requests(XenBlockDataPlane *dataplane)* is below us. */ if (inflight_atstart >IO_PLUG_THRESHOLD ) { -blk_io_plug (); +defer_call_begin (); }while (rc != rp) {/* pull request from ring */
@@ -577,12 +577,12 @@ static bool xen_block_handle_requests(XenBlockDataPlane *dataplane)if (inflight_atstart >IO_PLUG_THRESHOLD && batched >= inflight_atstart) { -blk_io_unplug (); +defer_call_end (); }xen_block_do_aio (request);if (inflight_atstart >IO_PLUG_THRESHOLD ) {if (batched >= inflight_atstart) { -blk_io_plug (); +defer_call_begin (); batched =0 ; }else { batched++;
@@ -590,7 +590,7 @@ static bool xen_block_handle_requests(XenBlockDataPlane *dataplane) } }if (inflight_atstart >IO_PLUG_THRESHOLD ) { -blk_io_unplug (); +defer_call_end (); }return done_something;
hw/block/virtio-blk.c +2 -2
@@ -1134,7 +1134,7 @@ void virtio_blk_handle_vq(VirtIOBlock *s, VirtQueue *vq)bool suppress_notifications =virtio_queue_get_notification (vq);aio_context_acquire (blk_get_aio_context (s->blk)); -blk_io_plug (); +defer_call_begin (); do { if (suppress_notifications) {
@@ -1158,7 +1158,7 @@ void virtio_blk_handle_vq(VirtIOBlock *s, VirtQueue *vq)virtio_blk_submit_multireq (s, &mrb); } -blk_io_unplug (); +defer_call_end ();aio_context_release (blk_get_aio_context (s->blk)); }
hw/scsi/virtio-scsi.c +3 -3
@@ -799,7 +799,7 @@ static int virtio_scsi_handle_cmd_req_prepare(VirtIOSCSI *s, VirtIOSCSIReq *req)return -ENOBUFS ; }scsi_req_ref (req->sreq); -blk_io_plug (); +defer_call_begin ();object_unref (OBJECT (d));return 0 ; }
@@ -810,7 +810,7 @@ static void virtio_scsi_handle_cmd_req_submit(VirtIOSCSI *s, VirtIOSCSIReq *req)if (scsi_req_enqueue (sreq)) {scsi_req_continue (sreq); } -blk_io_unplug (); +defer_call_end ();scsi_req_unref (sreq); }
@@ -836,7 +836,7 @@ static void virtio_scsi_handle_cmd_vq(VirtIOSCSI *s, VirtQueue *vq)while (!QTAILQ_EMPTY (&reqs)) { req =QTAILQ_FIRST (&reqs);QTAILQ_REMOVE (&reqs, req, next); -blk_io_unplug (); +defer_call_end ();scsi_req_unref (req->sreq);virtqueue_detach_element (req->vq, &req->elem,0 );virtio_scsi_free_req (req);
Signature · 1 line
2.41.0
Re: [PATCH v2 1/4] block: rename blk_io_plug_call() API to defer_call()
Quoted 8 lines · Stefan Hajnoczi
Prepare to move the
blk_io_plug_call()API out of the block layer so that other subsystems call use this deferred call mechanism. Rename it todefer_call()but leave the code inblock/plug.c.The next commit will move the code out of the block layer.
Suggested-by Ilya Maximets <[email protected]>Signed-off-by Stefan Hajnoczi <[email protected]>
Quoted 11 lines
--- include/sysemu/block-backend-io.h | 6 +- block/blkio.c | 8 +-- block/io_uring.c | 4 +- block/linux-aio.c | 4 +- block/nvme.c | 4 +- block/plug.c | 109 +++++++++++++++--------------- hw/block/dataplane/xen-block.c | 10 +-- hw/block/virtio-blk.c | 4 +- hw/scsi/virtio-scsi.c | 6 +- 9 files changed, 76 insertions(+), 79 deletions(-)
Re: [PATCH v2 1/4] block: rename blk_io_plug_call() API to defer_call()
In reply to Stefan HajnocziQuoted 8 lines · Stefan Hajnoczi
Prepare to move the
blk_io_plug_call()API out of the block layer so that other subsystems call use this deferred call mechanism. Rename it todefer_call()but leave the code inblock/plug.c.The next commit will move the code out of the block layer.
Suggested-by Ilya Maximets <[email protected]>Signed-off-by Stefan Hajnoczi <[email protected]>
Quoted 12 lines
--- include/sysemu/block-backend-io.h | 6 +- block/blkio.c | 8 +-- block/io_uring.c | 4 +- block/linux-aio.c | 4 +- block/nvme.c | 4 +- block/plug.c | 109 +++++++++++++++--------------- hw/block/dataplane/xen-block.c | 10 +-- hw/block/virtio-blk.c | 4 +- hw/scsi/virtio-scsi.c | 6 +- 9 files changed, 76 insertions(+), 79 deletions(-)
PATCH v2 2/4 util/defer-call: move defer_call() to util/
In reply to Stefan HajnocziThe networking subsystem may wish to use defer_call(), so move the code to util/ where it can be reused.
As a reminder of what defer_call() does:
This API defers a function call within a defer_call_begin()/defer_call_end() section, allowing multiple calls to batch up. This is a performance optimization that is used in the block layer to submit several I/O requests at once instead of individually:
defer_call_begin(); <-- start of section
...
defer_call(my_func, my_obj); <-- deferred my_func(my_obj) call defer_call(my_func, my_obj); <-- another defer_call(my_func, my_obj); <-- another
...
defer_call_end(); <-- end of section, my_func(my_obj) is called once
--- MAINTAINERS | 3 ++- include/qemu/defer-call.h | 15 +++++++++++++++ include/sysemu/block-backend-io.h | 4 ---- block/blkio.c | 1 + block/io_uring.c | 1 + block/linux-aio.c | 1 + block/nvme.c | 1 + hw/block/dataplane/xen-block.c | 1 + hw/block/virtio-blk.c | 1 + hw/scsi/virtio-scsi.c | 1 + block/plug.c => util/defer-call.c | 2 +- block/meson.build | 1 - util/meson.build | 1 + 13 files changed, 26 insertions(+), 7 deletions(-) create mode 100644 include/qemu/defer-call.h rename block/plug.c => util/defer-call.c (99%)
MAINTAINERS +2 -1
@@ -2676,12 +2676,13 @@ S: Supported F: util/async.c F: util/aio-*.c F: util/aio-*.h +F: util/defer-call.c F: util/fdmon-*.c F: block/io.c -F: block/plug.c F: migration/block* F: include/block/aio.h F: include/block/aio-wait.h +F: include/qemu/defer-call.h F: scripts/qemugdb/aio.py F: tests/unit/test-fdmon-epoll.c T: git https://github.com/stefanha/qemu.git block
include/qemu/defer-call.h new file +15 -0
@@ -0,0 +1,15 @@ +/* SPDX-License-Identifier: GPL-2.0-or-later */ +/* +* Deferred calls +* +* Copyright Red Hat. +*/ + +#ifndef QEMU_DEFER_CALL_H +#define QEMU_DEFER_CALL_H + +void defer_call_begin (void ); +void defer_call_end (void ); +void defer_call (void (*fn)(void *),void *opaque); + +#endif /* QEMU_DEFER_CALL_H */
include/sysemu/block-backend-io.h +0 -4
@@ -100,10 +100,6 @@ void blk_iostatus_set_err(BlockBackend *blk, int error);int blk_get_max_iov (BlockBackend *blk);int blk_get_max_hw_iov (BlockBackend *blk); -void defer_call_begin (void ); -void defer_call_end (void ); -void defer_call (void (*fn)(void *),void *opaque); -AioContext *blk_get_aio_context (BlockBackend *blk);BlockAcctStats *blk_get_stats (BlockBackend *blk); void *blk_aio_get(const AIOCBInfo *aiocb_info, BlockBackend *blk,
block/blkio.c +1 -0
@@ -13,6 +13,7 @@#include "block/block_int.h" #include "exec/memory.h" #include "exec/cpu-common.h" /* for qemu_ram_get_fd() */ +#include "qemu/defer-call.h" #include "qapi/error.h" #include "qemu/error-report.h" #include "qapi/qmp/qdict.h"
block/io_uring.c +1 -0
@@ -15,6 +15,7 @@#include "block/block.h" #include "block/raw-aio.h" #include "qemu/coroutine.h" +#include "qemu/defer-call.h" #include "qapi/error.h" #include "sysemu/block-backend.h" #include "trace.h"
block/linux-aio.c +1 -0
@@ -14,6 +14,7 @@#include "block/raw-aio.h" #include "qemu/event_notifier.h" #include "qemu/coroutine.h" +#include "qemu/defer-call.h" #include "qapi/error.h" #include "sysemu/block-backend.h"
block/nvme.c +1 -0
@@ -16,6 +16,7 @@#include "qapi/error.h" #include "qapi/qmp/qdict.h" #include "qapi/qmp/qstring.h" +#include "qemu/defer-call.h" #include "qemu/error-report.h" #include "qemu/main-loop.h" #include "qemu/module.h"
hw/block/dataplane/xen-block.c +1 -0
@@ -19,6 +19,7 @@*/ #include "qemu/osdep.h" +#include "qemu/defer-call.h" #include "qemu/error-report.h" #include "qemu/main-loop.h" #include "qemu/memalign.h"
hw/block/virtio-blk.c +1 -0
@@ -12,6 +12,7 @@*/ #include "qemu/osdep.h" +#include "qemu/defer-call.h" #include "qapi/error.h" #include "qemu/iov.h" #include "qemu/module.h"
hw/scsi/virtio-scsi.c +1 -0
@@ -18,6 +18,7 @@#include "standard-headers/linux/virtio_ids.h" #include "hw/virtio/virtio-scsi.h" #include "migration/qemu-file-types.h" +#include "qemu/defer-call.h" #include "qemu/error-report.h" #include "qemu/iov.h" #include "qemu/module.h"
util/defer-call.c renamed from block/plug.c +1 -1
@@ -22,7 +22,7 @@#include "qemu/coroutine-tls.h" #include "qemu/notify.h" #include "qemu/thread.h" -#include "sysemu/block-backend.h" +#include "qemu/defer-call.h" /* A function call that has been deferred until defer_call_end() */ typedef struct {
block/meson.build +0 -1
@@ -23,7 +23,6 @@ block_ss.add(files( 'mirror.c', 'nbd.c', 'null.c', - 'plug.c', 'qapi.c', 'qcow2-bitmap.c', 'qcow2-cache.c',
util/meson.build +1 -0
@@ -29,6 +29,7 @@ util_ss.add(when: 'CONFIG_WIN32', if_true: pathcch) if glib_has_gslice util_ss.add(files('qtree.c')) endif +util_ss.add(files('defer-call.c')) util_ss.add(files('envlist.c', 'path.c', 'module.c')) util_ss.add(files('host-utils.c')) util_ss.add(files('bitmap.c', 'bitops.c'))
Signature · 1 line
2.41.0
Re: [PATCH v2 2/4] util/defer-call: move defer_call() to util/
Hi Stefan,
Quoted 43 lines · Stefan Hajnoczi
The networking subsystem may wish to use
defer_call(), so move the code to util/ where it can be reused.As a reminder of what
defer_call()does:This API defers a function call within a defer_call_begin()/defer_call_end() section, allowing multiple calls to batch up. This is a performance optimization that is used in the block layer to submit several I/O requests at once instead of individually:
defer_call_begin(); <-- start of section
...
defer_call(my_func, my_obj); <-- deferredmy_func(my_obj)calldefer_call(my_func, my_obj); <-- anotherdefer_call(my_func, my_obj); <-- another
...
defer_call_end(); <-- end of section,my_func(my_obj)is called onceSuggested-by Ilya Maximets <[email protected]>Signed-off-by Stefan Hajnoczi <[email protected]>--- MAINTAINERS | 3 ++- include/qemu/defer-call.h | 15 +++++++++++++++ include/sysemu/block-backend-io.h | 4 ---- block/blkio.c | 1 + block/io_uring.c | 1 + block/linux-aio.c | 1 + block/nvme.c | 1 + hw/block/dataplane/xen-block.c | 1 + hw/block/virtio-blk.c | 1 + hw/scsi/virtio-scsi.c | 1 + block/plug.c => util/defer-call.c | 2 +- block/meson.build | 1 - util/meson.build | 1 + 13 files changed, 26 insertions(+), 7 deletions(-)create mode 100644
include/qemu/defer-call.h
renameblock/plug.c=>util/defer-call.c(99%)MAINTAINERS +0
-0
MAINTAINERS S: Supported · line 2679
Quoted 4 lines
MAINTAINERS +1
-0@@ -2676,12 +2676,13 @@ S: Supported F: util/async.c F: util/aio-*.c F: util/aio-*.h +F: util/defer-call.c
If used by network/other backends, maybe worth adding a brand new section instead, rather than "Block I/O path".
Quoted 16 lines
MAINTAINERS +1
-1F: util/fdmon-*.c F: block/io.c -F: block/plug.c F: migration/block* F: include/block/aio.h F: include/block/aio-wait.h +F: include/qemu/defer-call.h F: scripts/qemugdb/aio.py F: tests/unit/test-fdmon-epoll.c T: git https://github.com/stefanha/qemu.git blockinclude/qemu/defer-call.h new file +0
-0
include/qemu/defer-call.h line 10
Quoted 10 lines
include/qemu/defer-call.h new file +10
-0@@ -0,0 +1,15 @@ +/* SPDX-License-Identifier: GPL-2.0-or-later */ +/* +* Deferred calls +* +* Copyright Red Hat. +*/ + +#ifndef QEMU_DEFER_CALL_H +#define QEMU_DEFER_CALL_H +
Please add smth like:
/* See documentation in util/defer-call.c */
include/qemu/defer-call.h line 15
Quoted 5 lines
include/qemu/defer-call.h new file +5
-0+void defer_call_begin (void ); +void defer_call_end (void ); +void defer_call (void (*fn)(void *),void *opaque); + +#endif /* QEMU_DEFER_CALL_H */
Re: [PATCH v2 2/4] util/defer-call: move defer_call() to util/
Quoted 41 lines · Philippe Mathieu-Daudé
Hi Stefan,
Quoted 38 lines · Stefan Hajnoczi
The networking subsystem may wish to use
defer_call(), so move the code to util/ where it can be reused.As a reminder of what
defer_call()does:This API defers a function call within a defer_call_begin()/defer_call_end() section, allowing multiple calls to batch up. This is a performance optimization that is used in the block layer to submit several I/O requests at once instead of individually:
defer_call_begin(); <-- start of section
...
defer_call(my_func, my_obj); <-- deferredmy_func(my_obj)calldefer_call(my_func, my_obj); <-- anotherdefer_call(my_func, my_obj); <-- another
...
defer_call_end(); <-- end of section,my_func(my_obj)is called onceSuggested-by Ilya Maximets <[email protected]>Signed-off-by Stefan Hajnoczi <[email protected]>--- MAINTAINERS | 3 ++- include/qemu/defer-call.h | 15 +++++++++++++++ include/sysemu/block-backend-io.h | 4 ---- block/blkio.c | 1 + block/io_uring.c | 1 + block/linux-aio.c | 1 + block/nvme.c | 1 + hw/block/dataplane/xen-block.c | 1 + hw/block/virtio-blk.c | 1 + hw/scsi/virtio-scsi.c | 1 + block/plug.c => util/defer-call.c | 2 +- block/meson.build | 1 - util/meson.build | 1 + 13 files changed, 26 insertions(+), 7 deletions(-)create mode 100644
include/qemu/defer-call.h
renameblock/plug.c=>util/defer-call.c(99%)
Quoted 12 lines
MAINTAINERS S: Supported · line 2679
Quoted 9 lines
MAINTAINERS +1
-0@@ -2676,12 +2676,13 @@ S: Supported F: util/async.c F: util/aio-*.c F: util/aio-*.h +F: util/defer-call.cIf used by network/other backends, maybe worth adding a brand new section instead, rather than "Block I/O path".
Changes to defer-call.c will go through my block tree. We don't split out the event loop (async.c, aio-*.c, etc) either even though it's shared by other subsystems. The important thing is that scripts/get_maintainer.pl identifies the maintainers.
I'd rather not create lots of micro-subsystems in MAINTAINERS that duplicate my email and block git repo URL.
Quoted 30 lines
Quoted 17 lines
MAINTAINERS +1
-1F: util/fdmon-*.c F: block/io.c -F: block/plug.c F: migration/block* F: include/block/aio.h F: include/block/aio-wait.h +F: include/qemu/defer-call.h F: scripts/qemugdb/aio.py F: tests/unit/test-fdmon-epoll.c T: git https://github.com/stefanha/qemu.git blockinclude/qemu/defer-call.h new file +0
-0include/qemu/defer-call.h line 10
Quoted 10 lines
include/qemu/defer-call.h new file +10
-0@@ -0,0 +1,15 @@ +/* SPDX-License-Identifier: GPL-2.0-or-later */ +/* +* Deferred calls +* +* Copyright Red Hat. +*/ + +#ifndef QEMU_DEFER_CALL_H +#define QEMU_DEFER_CALL_H +Please add smth like:
Quoted 1 line
/* See documentation in util/defer-call.c */
Sure, will fix.
Quoted 9 lines
include/qemu/defer-call.h line 15
Quoted 6 lines
include/qemu/defer-call.h new file +5
-0+void defer_call_begin (void ); +void defer_call_end (void ); +void defer_call (void (*fn)(void *),void *opaque); + +#endif /* QEMU_DEFER_CALL_H */ Reviewed-by Philippe Mathieu-Daudé <[email protected]>
PATCH v2 3/4 virtio: use defer_call() in virtio_irqfd_notify()
In reply to Stefan Hajnoczivirtio-blk and virtio-scsi invoke virtio_irqfd_notify() to send Used Buffer Notifications from an IOThread. This involves an eventfd write(2) syscall. Calling this repeatedly when completing multiple I/O requests in a row is wasteful.
Use the defer_call() API to batch together virtio_irqfd_notify() calls made during thread pool (aio=threads), Linux AIO (aio=native), and io_uring (aio=io_uring) completion processing.
Behavior is unchanged for emulated devices that do not use defer_call_begin()/defer_call_end() since defer_call() immediately invokes the callback when called outside a defer_call_begin()/defer_call_end() region.
fio rw=randread bs=4k iodepth=64 numjobs=8 IOPS increases by ~9% with a single IOThread and 8 vCPUs. iodepth=1 decreases by ~1% but this could be noise. Detailed performance data and configuration specifics are available here:
https://gitlab.com/stefanha/virt-playbooks/-/tree/blk_io_plug-irqfd
This duplicates the BH that virtio-blk uses for batching. The next commit will remove it.
--- block/io_uring.c | 6 ++++++ block/linux-aio.c | 4 ++++ hw/virtio/virtio.c | 11 ++++++++++- util/thread-pool.c | 5 +++++ 4 files changed, 25 insertions(+), 1 deletion(-)
block/io_uring.c +6 -0
@@ -125,6 +125,9 @@ static void luring_process_completions(LuringState *s) {struct io_uring_cqe *cqes;int total_bytes; + +defer_call_begin (); +/* * Request completion callbacks can run the nested event loop. * Schedule ourselves so the nested event loop will "see" remaining
@@ -217,7 +220,10 @@ end:aio_co_wake (luringcb->co); } } +qemu_bh_cancel (s->completion_bh); + +defer_call_end (); }static int ioq_submit (LuringState *s)
block/linux-aio.c +4 -0
@@ -205,6 +205,8 @@ static void qemu_laio_process_completions(LinuxAioState *s) {struct io_event *events; +defer_call_begin (); +/* Reschedule so nested event loops see currently pending completions */ qemu_bh_schedule (s->completion_bh);
@@ -231,6 +233,8 @@ static void qemu_laio_process_completions(LinuxAioState *s)* own `for` loop. If we are the last all counters droped to zero. */ s->event_max =0 ; s->event_idx =0 ; + +defer_call_end (); }static void qemu_laio_process_completions_and_submit (LinuxAioState *s)
hw/virtio/virtio.c +10 -1
@@ -15,6 +15,7 @@#include "qapi/error.h" #include "qapi/qapi-commands-virtio.h" #include "trace.h" +#include "qemu/defer-call.h" #include "qemu/error-report.h" #include "qemu/log.h" #include "qemu/main-loop.h"
@@ -28,6 +29,7 @@#include "hw/virtio/virtio-bus.h" #include "hw/qdev-properties.h" #include "hw/virtio/virtio-access.h" +#include "sysemu/block-backend.h" #include "sysemu/dma.h" #include "sysemu/runstate.h" #include "virtio-qmp.h"
@@ -2426,6 +2428,13 @@ static bool virtio_should_notify(VirtIODevice *vdev, VirtQueue *vq) } } +/* Batch irqs while inside a defer_call_begin()/defer_call_end() section */ +static void virtio_notify_irqfd_deferred_fn (void *opaque) +{ +EventNotifier *notifier = opaque; +event_notifier_set (notifier); +} +void virtio_notify_irqfd (VirtIODevice *vdev,VirtQueue *vq) { WITH_RCU_READ_LOCK_GUARD() {
@@ -2452,7 +2461,7 @@ void virtio_notify_irqfd(VirtIODevice *vdev, VirtQueue *vq)* to an atomic operation. */ virtio_set_isr (vq->vdev,0x1 ); -event_notifier_set (&vq->guest_notifier); +defer_call (virtio_notify_irqfd_deferred_fn, &vq->guest_notifier); }static void virtio_irq (VirtQueue *vq)
util/thread-pool.c +5 -0
@@ -15,6 +15,7 @@* GNU GPL, version 2 or (at your option) any later version. */ #include "qemu/osdep.h" +#include "qemu/defer-call.h" #include "qemu/queue.h" #include "qemu/thread.h" #include "qemu/coroutine.h"
@@ -175,6 +176,8 @@ static void thread_pool_completion_bh(void *opaque)ThreadPool *pool = opaque;ThreadPoolElement *elem, *next; +defer_call_begin ();/* cb() may use defer_call() to coalesce work */ + restart: QLIST_FOREACH_SAFE(elem, &pool->head, all, next) {if (elem->state !=THREAD_DONE ) {
@@ -208,6 +211,8 @@ restart:qemu_aio_unref (elem); } } + +defer_call_end (); }static void thread_pool_cancel (BlockAIOCB *acb)
Signature · 1 line
2.41.0
Re: [PATCH v2 3/4] virtio: use defer_call() in virtio_irqfd_notify()
Quoted 23 lines · Stefan Hajnoczi
virtio-blk and virtio-scsi invoke
virtio_irqfd_notify()to send Used Buffer Notifications from an IOThread. This involves an eventfd write(2) syscall. Calling this repeatedly when completing multiple I/O requests in a row is wasteful.Use the
defer_call()API to batch togethervirtio_irqfd_notify()calls made during thread pool (aio=threads), Linux AIO (aio=native), andio_uring(aio=io_uring) completion processing.Behavior is unchanged for emulated devices that do not use defer_call_begin()/defer_call_end() since
defer_call()immediately invokes the callback when called outside a defer_call_begin()/defer_call_end() region.fio rw=randread bs=4k iodepth=64 numjobs=8 IOPS increases by ~9% with a single IOThread and 8 vCPUs. iodepth=1 decreases by ~1% but this could be noise. Detailed performance data and configuration specifics are available here:
https://gitlab.com/stefanha/virt-playbooks/-/tree/blk_io_plug-irqfdThis duplicates the BH that virtio-blk uses for batching. The next commit will remove it.
Quoted 7 lines
Signed-off-by Stefan Hajnoczi <[email protected]>--- block/io_uring.c | 6 ++++++ block/linux-aio.c | 4 ++++ hw/virtio/virtio.c | 11 ++++++++++- util/thread-pool.c | 5 +++++ 4 files changed, 25 insertions(+), 1 deletion(-)
Signature · 3 lines
Eric Blake, Principal Software Engineer Red Hat, Inc. Virtualization: qemu.org | libguestfs.org
Re: [PATCH v2 3/4] virtio: use defer_call() in virtio_irqfd_notify()
In reply to Stefan HajnocziQuoted 91 lines · Stefan Hajnoczi
virtio-blk and virtio-scsi invoke
virtio_irqfd_notify()to send Used Buffer Notifications from an IOThread. This involves an eventfd write(2) syscall. Calling this repeatedly when completing multiple I/O requests in a row is wasteful.Use the
defer_call()API to batch togethervirtio_irqfd_notify()calls made during thread pool (aio=threads), Linux AIO (aio=native), andio_uring(aio=io_uring) completion processing.Behavior is unchanged for emulated devices that do not use defer_call_begin()/defer_call_end() since
defer_call()immediately invokes the callback when called outside a defer_call_begin()/defer_call_end() region.fio rw=randread bs=4k iodepth=64 numjobs=8 IOPS increases by ~9% with a single IOThread and 8 vCPUs. iodepth=1 decreases by ~1% but this could be noise. Detailed performance data and configuration specifics are available here:
https://gitlab.com/stefanha/virt-playbooks/-/tree/blk_io_plug-irqfdThis duplicates the BH that virtio-blk uses for batching. The next commit will remove it.
Signed-off-by Stefan Hajnoczi <[email protected]>--- block/io_uring.c | 6 ++++++ block/linux-aio.c | 4 ++++ hw/virtio/virtio.c | 11 ++++++++++- util/thread-pool.c | 5 +++++ 4 files changed, 25 insertions(+), 1 deletion(-)block/io_uring.c +6
-0@@ -125,6 +125,9 @@ static void luring_process_completions(LuringState *s) {struct io_uring_cqe *cqes;int total_bytes; + +defer_call_begin (); +/* * Request completion callbacks can run the nested event loop. * Schedule ourselves so the nested event loop will "see" remaining @@ -217,7 +220,10 @@ end:aio_co_wake (luringcb->co); } } +qemu_bh_cancel (s->completion_bh); + +defer_call_end (); }static int ioq_submit (LuringState *s)block/linux-aio.c +4
-0@@ -205,6 +205,8 @@ static void qemu_laio_process_completions(LinuxAioState *s) {struct io_event *events; +defer_call_begin (); +/* Reschedule so nested event loops see currently pending completions */ qemu_bh_schedule (s->completion_bh);@@ -231,6 +233,8 @@ static void qemu_laio_process_completions(LinuxAioState *s)* own `for` loop. If we are the last all counters droped to zero. */ s->event_max =0 ; s->event_idx =0 ; + +defer_call_end (); }static void qemu_laio_process_completions_and_submit (LinuxAioState *s)hw/virtio/virtio.c +1
-0@@ -15,6 +15,7 @@#include "qapi/error.h" #include "qapi/qapi-commands-virtio.h" #include "trace.h" +#include "qemu/defer-call.h" #include "qemu/error-report.h" #include "qemu/log.h" #include "qemu/main-loop.h"
hw/virtio/virtio.c line 32
Quoted 4 lines
hw/virtio/virtio.c +1
-0@@ -28,6 +29,7 @@#include "hw/virtio/virtio-bus.h" #include "hw/qdev-properties.h" #include "hw/virtio/virtio-access.h" +#include "sysemu/block-backend.h"
An artifact from the previous version.
Quoted 18 lines
hw/virtio/virtio.c +7
-0#include "sysemu/dma.h" #include "sysemu/runstate.h" #include "virtio-qmp.h" @@ -2426,6 +2428,13 @@ static bool virtio_should_notify(VirtIODevice *vdev, VirtQueue *vq) } } +/* Batch irqs while inside a defer_call_begin()/defer_call_end() section */ +static void virtio_notify_irqfd_deferred_fn (void *opaque) +{ +EventNotifier *notifier = opaque; +event_notifier_set (notifier); +} +void virtio_notify_irqfd (VirtIODevice *vdev,VirtQueue *vq) { WITH_RCU_READ_LOCK_GUARD() {
hw/virtio/virtio.c void virtio_notify_irqfd(VirtIODevice *vdev, VirtQueue *vq) · line 2464
Quoted 5 lines
hw/virtio/virtio.c +1
-1@@ -2452,7 +2461,7 @@ void virtio_notify_irqfd(VirtIODevice *vdev, VirtQueue *vq)* to an atomic operation. */ virtio_set_isr (vq->vdev,0x1 ); -event_notifier_set (&vq->guest_notifier); +defer_call (virtio_notify_irqfd_deferred_fn, &vq->guest_notifier);
Should we move the trace from this function to deferred one? Or maybe add a new trace?
Quoted 33 lines
hw/virtio/virtio.c +0
-0}static void virtio_irq (VirtQueue *vq)util/thread-pool.c +5
-0@@ -15,6 +15,7 @@* GNU GPL, version 2 or (at your option) any later version. */ #include "qemu/osdep.h" +#include "qemu/defer-call.h" #include "qemu/queue.h" #include "qemu/thread.h" #include "qemu/coroutine.h" @@ -175,6 +176,8 @@ static void thread_pool_completion_bh(void *opaque)ThreadPool *pool = opaque;ThreadPoolElement *elem, *next; +defer_call_begin ();/* cb() may use defer_call() to coalesce work */ + restart: QLIST_FOREACH_SAFE(elem, &pool->head, all, next) {if (elem->state !=THREAD_DONE ) {@@ -208,6 +211,8 @@ restart:qemu_aio_unref (elem); } } + +defer_call_end (); }static void thread_pool_cancel (BlockAIOCB *acb)
PATCH v2 4/4 virtio-blk: remove batch notification BH
In reply to Stefan HajnocziThere is a batching mechanism for virtio-blk Used Buffer Notifications that is no longer needed because the previous commit added batching to virtio_notify_irqfd().
Note that this mechanism was rarely used in practice because it is only enabled when EVENT_IDX is not negotiated by the driver. Modern drivers enable EVENT_IDX.
--- hw/block/dataplane/virtio-blk.c | 48 +-------------------------------- 1 file changed, 1 insertion(+), 47 deletions(-)
hw/block/dataplane/virtio-blk.c +1 -47
@@ -31,9 +31,6 @@ struct VirtIOBlockDataPlane {VirtIOBlkConf *conf;VirtIODevice *vdev; -QEMUBH *bh;/* bh for guest notification */ -unsigned long *batch_notify_vqs; -bool batch_notifications;/* Note that these EventNotifiers are assigned by value. This is * fine as long as you do not call event_notifier_cleanup on them
@@ -47,36 +44,7 @@ struct VirtIOBlockDataPlane {/* Raise an interrupt to signal guest, if necessary */ void virtio_blk_data_plane_notify (VirtIOBlockDataPlane *s,VirtQueue *vq) { -if (s->batch_notifications) { -set_bit (virtio_get_queue_index (vq), s->batch_notify_vqs); -qemu_bh_schedule (s->bh); - }else { -virtio_notify_irqfd (s->vdev, vq); - } -} - -static void notify_guest_bh (void *opaque) -{ -VirtIOBlockDataPlane *s = opaque; -unsigned nvqs = s->conf->num_queues; -unsigned long bitmap[BITS_TO_LONGS (nvqs)]; -unsigned j; - -memcpy (bitmap, s->batch_notify_vqs,sizeof (bitmap)); -memset (s->batch_notify_vqs,0 ,sizeof (bitmap)); - -for (j =0 ; j < nvqs; j +=BITS_PER_LONG ) { -unsigned long bits = bitmap[j /BITS_PER_LONG ]; - -while (bits !=0 ) { -unsigned i = j +ctzl (bits); -VirtQueue *vq =virtio_get_queue (s->vdev, i); - -virtio_notify_irqfd (s->vdev, vq); - - bits &= bits -1 ;/* clear right-most bit */ - } - } +virtio_notify_irqfd (s->vdev, vq); }/* Context: QEMU global mutex held */
@@ -126,9 +94,6 @@ bool virtio_blk_data_plane_create(VirtIODevice *vdev, VirtIOBlkConf *conf, }else { s->ctx =qemu_get_aio_context (); } - s->bh =aio_bh_new_guarded (s->ctx, notify_guest_bh, s, - &DEVICE (vdev)->mem_reentrancy_guard); - s->batch_notify_vqs =bitmap_new (conf->num_queues); *dataplane = s;
@@ -146,8 +111,6 @@ void virtio_blk_data_plane_destroy(VirtIOBlockDataPlane *s) vblk =VIRTIO_BLK (s->vdev);assert (!vblk->dataplane_started); -g_free (s->batch_notify_vqs); -qemu_bh_delete (s->bh);if (s->iothread) {object_unref (OBJECT (s->iothread)); }
@@ -173,12 +136,6 @@ int virtio_blk_data_plane_start(VirtIODevice *vdev) s->starting = true; -if (!virtio_vdev_has_feature (vdev,VIRTIO_RING_F_EVENT_IDX )) { - s->batch_notifications = true; - }else { - s->batch_notifications = false; - } -/* Set up guest notifier (irq) */ r = k->set_guest_notifiers (qbus->parent, nvqs, true);if (r !=0 ) {
@@ -370,9 +327,6 @@ void virtio_blk_data_plane_stop(VirtIODevice *vdev)aio_context_release (s->ctx); -qemu_bh_cancel (s->bh); -notify_guest_bh (s);/* final chance to notify guest */ -/* Clean up guest notifier (irq) */ k->set_guest_notifiers (qbus->parent, nvqs, false);
Signature · 1 line
2.41.0
Re: [PATCH v2 4/4] virtio-blk: remove batch notification BH
Quoted 4 lines · Stefan Hajnoczi
There is a batching mechanism for virtio-blk Used Buffer Notifications that is no longer needed because the previous commit added batching to
virtio_notify_irqfd().
Quoted 9 lines
Note that this mechanism was rarely used in practice because it is only enabled when
EVENT_IDXis not negotiated by the driver. Modern drivers enableEVENT_IDX.Signed-off-by Stefan Hajnoczi <[email protected]>--- hw/block/dataplane/virtio-blk.c | 48 +-------------------------------- 1 file changed, 1 insertion(+), 47 deletions(-)
Signature · 3 lines
Eric Blake, Principal Software Engineer Red Hat, Inc. Virtualization: qemu.org | libguestfs.org