Skip to contentSkip to conversation

linux-aio: introduce submit I/O as a batch

PATCH Applied Ming Lei 4 participants 9 messages Jul 4, 2014 – Aug 26, 2014 Block layer

lore
Share & tools
lore

Ming Lei

author
#0 lore raw

v6 cover linux-aio: introduce submit I/O as a batch

Hi,

The commit 580b6b2aa2(dataplane: use the QEMU block layer for I/O) introduces ~40% throughput regression on virtio-blk dataplane, and one of causes is that submitting I/O as a batch is removed.

This patchset trys to introduce this mechanism on block, at least, linux-aio can benefit from that.

With these patches, it is observed that thoughout on virtio-blk dataplane can be improved a lot, see data in commit log of patch 3/3.

It should be possible to apply the batch mechanism to other devices (such as virtio-scsi) too.

TODO:
- support queuing I/O to multi files for scsi devies, which
need some changes to linux-aio

V6:
- fix requests leak if part of them arn't submitted successfully, pointed by Stefan
- linux-aio.c coding style fix

V5:
- rebase on v2.1.0-rc0 of qemu.git/master
- block/linux-aio.c code style fix
- don't flush io queue before flush, pointed by Paolo

V4:
- support other non-raw formats with under-optimized performance
- use reference counter for plug & unplug
- flush io queue before sending flush command

V3:
- only support submitting I/O as a batch for raw format, pointed by
Kevin

V2:
- define return value of bdrv_io_unplug as void, suggested by Paolo
- avoid busy-wait for handling io_submit
V1:
- move queuing io stuff into linux-aio.c as suggested by Paolo

Thanks,
--
Ming Lei

author
#1 lore raw

PATCH v6 1/3 block: block: introduce APIs for submitting IO as a batch

This patch introduces three APIs so that following patches can support queuing I/O requests and submitting them as a batch for improving I/O performance.

---
 block.c                   |   31 +++++++++++++++++++++++++++++++
 include/block/block.h     |    4 ++++
 include/block/block_int.h |    5 +++++
 3 files changed, 40 insertions(+)
block.c +31 -0
index c8379ca..1aa452f 100644
@@ -1905,6 +1905,7 @@ void bdrv_drain_all(void)
             bool bs_busy;
 
             aio_context_acquire(aio_context);
+            bdrv_flush_io_queue(bs);
             bdrv_start_throttled_reqs(bs);
             bs_busy = bdrv_requests_pending(bs);
             bs_busy |= aio_poll(aio_context, bs_busy);
@@ -5786,3 +5787,33 @@ BlockDriverState *check_to_replace_node(const char *node_name, Error **errp)
 
     return to_replace_bs;
 }
+
+void bdrv_io_plug(BlockDriverState *bs)
+{
+    BlockDriver *drv = bs->drv;
+    if (drv && drv->bdrv_io_plug) {
+        drv->bdrv_io_plug(bs);
+    } else if (bs->file) {
+        bdrv_io_plug(bs->file);
+    }
+}
+
+void bdrv_io_unplug(BlockDriverState *bs)
+{
+    BlockDriver *drv = bs->drv;
+    if (drv && drv->bdrv_io_unplug) {
+        drv->bdrv_io_unplug(bs);
+    } else if (bs->file) {
+        bdrv_io_unplug(bs->file);
+    }
+}
+
+void bdrv_flush_io_queue(BlockDriverState *bs)
+{
+    BlockDriver *drv = bs->drv;
+    if (drv && drv->bdrv_flush_io_queue) {
+        drv->bdrv_flush_io_queue(bs);
+    } else if (bs->file) {
+        bdrv_flush_io_queue(bs->file);
+    }
+}
include/block/block.h +4 -0
index baecc26..32d3676 100644
@@ -584,4 +584,8 @@ AioContext *bdrv_get_aio_context(BlockDriverState *bs);
  */
 void bdrv_set_aio_context(BlockDriverState *bs, AioContext *new_context);
 
+void bdrv_io_plug(BlockDriverState *bs);
+void bdrv_io_unplug(BlockDriverState *bs);
+void bdrv_flush_io_queue(BlockDriverState *bs);
+
 #endif
include/block/block_int.h +5 -0
index 8f8e65e..f6c3bef 100644
@@ -261,6 +261,11 @@ struct BlockDriver {
     void (*bdrv_attach_aio_context)(BlockDriverState *bs,
                                     AioContext *new_context);
 
+    /* io queue for linux-aio */
+    void (*bdrv_io_plug)(BlockDriverState *bs);
+    void (*bdrv_io_unplug)(BlockDriverState *bs);
+    void (*bdrv_flush_io_queue)(BlockDriverState *bs);
+
     QLIST_ENTRY(BlockDriver) list;
 };
 
Signature · 1 line
1.7.9.5
author
#2 lore raw

PATCH v6 2/3 linux-aio: implement io plug, unplug and flush io queue

In reply to Ming Lei

This patch implements .bdrv_io_plug, .bdrv_io_unplug and .bdrv_flush_io_queue callbacks for linux-aio Block Drivers, so that submitting I/O as a batch can be supported on linux-aio.

---
 block/linux-aio.c |   96 +++++++++++++++++++++++++++++++++++++++++++++++++++--
 block/raw-aio.h   |    2 ++
 block/raw-posix.c |   45 +++++++++++++++++++++++++
 3 files changed, 141 insertions(+), 2 deletions(-)
block/linux-aio.c +94 -2
index f0a2c08..f8a5230 100644
@@ -25,6 +25,8 @@
  */
 #define MAX_EVENTS 128
 
+#define MAX_QUEUED_IO  128
+
 struct qemu_laiocb {
     BlockDriverAIOCB common;
     struct qemu_laio_state *ctx;
@@ -36,9 +38,19 @@ struct qemu_laiocb {
     QLIST_ENTRY(qemu_laiocb) node;
 };
 
+typedef struct {
+    struct iocb *iocbs[MAX_QUEUED_IO];
+    int plugged;
+    unsigned int size;
+    unsigned int idx;
+} LaioQueue;
+
 struct qemu_laio_state {
     io_context_t ctx;
     EventNotifier e;
+
+    /* io queue for submit at batch */
+    LaioQueue io_q;
 };
 
 static inline ssize_t io_event_ret(struct io_event *ev)
@@ -135,6 +147,79 @@ static const AIOCBInfo laio_aiocb_info = {
     .cancel             = laio_cancel,
 };
 
+static void ioq_init(LaioQueue *io_q)
+{
+    io_q->size = MAX_QUEUED_IO;
+    io_q->idx = 0;
+    io_q->plugged = 0;
+}
+
+static int ioq_submit(struct qemu_laio_state *s)
+{
+    int ret, i = 0;
+    int len = s->io_q.idx;
+
+    do {
+        ret = io_submit(s->ctx, len, s->io_q.iocbs);
+    } while (i++ < 3 && ret == -EAGAIN);
+
+    /* empty io queue */
+    s->io_q.idx = 0;
+
+    if (ret < 0) {
+        i = 0;
+    } else {
+        i = ret;
+    }
+
+    for (; i < len; i++) {
+        struct qemu_laiocb *laiocb =
+            container_of(s->io_q.iocbs[i], struct qemu_laiocb, iocb);
+
+        laiocb->ret = ret;
+        qemu_laio_process_completion(s, laiocb);
+    }
+    return ret;
+}
+
+static void ioq_enqueue(struct qemu_laio_state *s, struct iocb *iocb)
+{
+    unsigned int idx = s->io_q.idx;
+
+    s->io_q.iocbs[idx++] = iocb;
+    s->io_q.idx = idx;
+
+    /* submit immediately if queue is full */
+    if (idx == s->io_q.size) {
+        ioq_submit(s);
+    }
+}
+
+void laio_io_plug(BlockDriverState *bs, void *aio_ctx)
+{
+    struct qemu_laio_state *s = aio_ctx;
+
+    s->io_q.plugged++;
+}
+
+int laio_io_unplug(BlockDriverState *bs, void *aio_ctx, bool unplug)
+{
+    struct qemu_laio_state *s = aio_ctx;
+    int ret = 0;
+
+    assert(s->io_q.plugged > 0 || !unplug);
+
+    if (unplug && --s->io_q.plugged > 0) {
+        return 0;
+    }
+
+    if (s->io_q.idx > 0) {
+        ret = ioq_submit(s);
+    }
+
+    return ret;
+}
+
 BlockDriverAIOCB *laio_submit(BlockDriverState *bs, void *aio_ctx, int fd,
         int64_t sector_num, QEMUIOVector *qiov, int nb_sectors,
         BlockDriverCompletionFunc *cb, void *opaque, int type)
@@ -168,8 +253,13 @@ BlockDriverAIOCB *laio_submit(BlockDriverState *bs, void *aio_ctx, int fd,
     }
     io_set_eventfd(&laiocb->iocb, event_notifier_get_fd(&s->e));
 
-    if (io_submit(s->ctx, 1, &iocbs) < 0)
-        goto out_free_aiocb;
+    if (!s->io_q.plugged) {
+        if (io_submit(s->ctx, 1, &iocbs) < 0) {
+            goto out_free_aiocb;
+        }
+    } else {
+        ioq_enqueue(s, iocbs);
+    }
     return &laiocb->common;
 
 out_free_aiocb:
@@ -204,6 +294,8 @@ void *laio_init(void)
         goto out_close_efd;
     }
 
+    ioq_init(&s->io_q);
+
     return s;
 
 out_close_efd:
block/raw-aio.h +2 -0
index 8cf084e..e18c975 100644
@@ -40,6 +40,8 @@ BlockDriverAIOCB *laio_submit(BlockDriverState *bs, void *aio_ctx, int fd,
         BlockDriverCompletionFunc *cb, void *opaque, int type);
 void laio_detach_aio_context(void *s, AioContext *old_context);
 void laio_attach_aio_context(void *s, AioContext *new_context);
+void laio_io_plug(BlockDriverState *bs, void *aio_ctx);
+int laio_io_unplug(BlockDriverState *bs, void *aio_ctx, bool unplug);
 #endif
 
 #ifdef _WIN32
block/raw-posix.c +45 -0

81 lines not on this page · raw message

Signature · 1 line
1.7.9.5
author
#3 lore raw

PATCH v6 3/3 dataplane: submit I/O as a batch

In reply to Ming Lei

Before commit 580b6b2aa2(dataplane: use the QEMU block layer for I/O), dataplane for virtio-blk submits block I/O as a batch.

This commit 580b6b2aa2 replaces the custom linux AIO implementation(including submit I/O as a batch) with QEMU block layer, but this commit causes ~40% throughput regression on virtio-blk performance, and removing submitting I/O as a batch is one of the causes.

This patch applies the newly introduced bdrv_io_plug() and bdrv_io_unplug() interfaces to support submitting I/O at batch for Qemu block layer, and in my test, the change can improve throughput by ~30% with 'aio=native'.

Following my fio test script:

	[global]
	direct=1
	size=4G
	bsrange=4k-4k
	timeout=40
	numjobs=4
	ioengine=libaio
	iodepth=64
	filename=/dev/vdc
	group_reporting=1

	[f]
	rw=randread

Result on one of my small machine(host: x86_64, 2cores, 4thread, guest: 4cores):
- qemu master: 65K IOPS
- qemu master with these patches: 92K IOPS
- 2.0.0 release(dataplane using custom linux aio): 104K IOPS

---
 hw/block/dataplane/virtio-blk.c |    2 ++
 1 file changed, 2 insertions(+)
hw/block/dataplane/virtio-blk.c +2 -0
index 20f12c7..227bb15 100644
@@ -77,6 +77,7 @@ static void handle_notify(EventNotifier *e)
     VirtIOBlock *vblk = VIRTIO_BLK(s->vdev);
 
     event_notifier_test_and_clear(&s->host_notifier);
+    bdrv_io_plug(s->blk->conf.bs);
     for (;;) {
         MultiReqBuffer mrb = {
             .num_writes = 0,
@@ -115,6 +116,7 @@ static void handle_notify(EventNotifier *e)
             break;
         }
     }
+    bdrv_io_unplug(s->blk->conf.bs);
 }
 
 /* Context: QEMU global mutex held */
Signature · 1 line
1.7.9.5
#4 lore raw
In reply to Ming Lei
Quoted 39 lines · Ming Lei

Hi,

The commit 580b6b2aa2(dataplane: use the QEMU block layer for I/O) introduces ~40% throughput regression on virtio-blk dataplane, and one of causes is that submitting I/O as a batch is removed.

This patchset trys to introduce this mechanism on block, at least, linux-aio can benefit from that.

With these patches, it is observed that thoughout on virtio-blk dataplane can be improved a lot, see data in commit log of patch 3/3.

It should be possible to apply the batch mechanism to other devices (such as virtio-scsi) too.

TODO:
- support queuing I/O to multi files for scsi devies, which
need some changes to linux-aio

V6:
- fix requests leak if part of them arn't submitted successfully, pointed by Stefan
- linux-aio.c coding style fix

V5:
- rebase on v2.1.0-rc0 of qemu.git/master
- block/linux-aio.c code style fix
- don't flush io queue before flush, pointed by Paolo

V4:
- support other non-raw formats with under-optimized performance
- use reference counter for plug & unplug
- flush io queue before sending flush command

V3:
- only support submitting I/O as a batch for raw format, pointed by
Kevin

Quoted 5 lines

V2:
- define return value of bdrv_io_unplug as void, suggested by Paolo
- avoid busy-wait for handling io_submit
V1:
- move queuing io stuff into linux-aio.c as suggested by Paolo

Thanks, applied to my block tree:
https://github.com/stefanha/qemu/commits/block

In Patch 2 we should complete requests with -EIO if io_submit() returned 0 <= ret < len. I fixed this up when applying because the patch was completing with a bogus ret value.

Stefan

#5 lore raw

Re: [Qemu-devel] [PATCH v6 0/3] linux-aio: introduce submit I/O asa batch

In reply to Ming Lei
Quoted 39 lines
Quoted 39 lines

Hi,

The commit 580b6b2aa2(dataplane: use the QEMU block layer for I/O) introduces ~40% throughput regression on virtio-blk dataplane, and one of causes is that submitting I/O as a batch is removed.

This patchset trys to introduce this mechanism on block, at least, linux-aio can benefit from that.

With these patches, it is observed that thoughout on virtio-blk dataplane can be improved a lot, see data in commit log of patch 3/3.

It should be possible to apply the batch mechanism to other devices (such as virtio-scsi) too.

TODO:
- support queuing I/O to multi files for scsi devies, which
need some changes to linux-aio

V6:
- fix requests leak if part of them arn't submitted successfully, pointed by Stefan
- linux-aio.c coding style fix

V5:
- rebase on v2.1.0-rc0 of qemu.git/master
- block/linux-aio.c code style fix
- don't flush io queue before flush, pointed by Paolo

V4:
- support other non-raw formats with under-optimized performance
- use reference counter for plug & unplug
- flush io queue before sending flush command

V3:
- only support submitting I/O as a batch for raw format, pointed by
Kevin

Quoted 9 lines
Quoted 5 lines

V2:
- define return value of bdrv_io_unplug as void, suggested by Paolo
- avoid busy-wait for handling io_submit
V1:
- move queuing io stuff into linux-aio.c as suggested by Paolo

Thanks, applied to my block tree:
https://github.com/stefanha/qemu/commits/block

Can we use the queued io data as caches,
io write will directly return and tell the guest the io is completed after the io is enqueued, better user experience for burst io,
and io-read will firstly search the io queue, if matched data found, directly get the data from the queue, if not, then read the data from the disk or host page cache.
Any ideas?

Thanks,
Zhang Haoyu

Quoted 5 lines

In Patch 2 we should complete requests with -EIO if io_submit() returned 0 <= ret < len. I fixed this up when applying because the patch was completing with a bogus ret value.

Stefan

#6 lore raw

Re: [Qemu-devel] [PATCH v6 0/3] linux-aio: introduce submit I/O asa batch

Quoted 6 lines · Zhang Haoyu

Can we use the queued io data as caches,
io write will directly return and tell the guest the io is completed after the io is enqueued, better user experience for burst io,
and io-read will firstly search the io queue, if matched data found, directly get the data from the queue, if not, then read the data from the disk or host page cache.
Any ideas?

Guest kernel already has a page cache that exactly does this, also keeping a copy of guest request data in qemu may hurt the bandwidth in some cases.

Fam

#7 lore raw

Re: [Qemu-devel] [PATCH v6 0/3] linux-aio: introduce submit I/O asabatch

Quoted 10 lines
Quoted 6 lines

Can we use the queued io data as caches,
io write will directly return and tell the guest the io is completed after the io is enqueued, better user experience for burst io,
and io-read will firstly search the io queue, if matched data found, directly get the data from the queue, if not, then read the data from the disk or host page cache.
Any ideas?

Guest kernel already has a page cache that exactly does this, also keeping a copy of guest request data in qemu may hurt the bandwidth in some cases.

You are right.
Does the io merging in queue is worthy to be performed ?

Thanks,
Zhang Haoyu

Quoted 1 line

Fam

#8 lore raw
In reply to Zhang Haoyu
Quoted 45 lines
Quoted 45 lines
Quoted 44 lines

Hi,

The commit 580b6b2aa2(dataplane: use the QEMU block layer for I/O) introduces ~40% throughput regression on virtio-blk dataplane, and one of causes is that submitting I/O as a batch is removed.

This patchset trys to introduce this mechanism on block, at least, linux-aio can benefit from that.

With these patches, it is observed that thoughout on virtio-blk dataplane can be improved a lot, see data in commit log of patch 3/3.

It should be possible to apply the batch mechanism to other devices (such as virtio-scsi) too.

TODO:
- support queuing I/O to multi files for scsi devies, which
need some changes to linux-aio

V6:
- fix requests leak if part of them arn't submitted successfully, pointed by Stefan
- linux-aio.c coding style fix

V5:
- rebase on v2.1.0-rc0 of qemu.git/master
- block/linux-aio.c code style fix
- don't flush io queue before flush, pointed by Paolo

V4:
- support other non-raw formats with under-optimized performance
- use reference counter for plug & unplug
- flush io queue before sending flush command

V3:
- only support submitting I/O as a batch for raw format, pointed by
Kevin

V2:
- define return value of bdrv_io_unplug as void, suggested by Paolo
- avoid busy-wait for handling io_submit
V1:
- move queuing io stuff into linux-aio.c as suggested by Paolo

Quoted 8 lines
Quoted 3 lines

Thanks, applied to my block tree:
https://github.com/stefanha/qemu/commits/block

Can we use the queued io data as caches,
io write will directly return and tell the guest the io is completed after the io is enqueued, better user experience for burst io,
and io-read will firstly search the io queue, if matched data found, directly get the data from the queue, if not, then read the data from the disk or host page cache.

Because host page cache cannot assure the order of io-write, leading to VM image data corruption if suddenly poweroff happened, but we can assure the order of io-write in queue, and use cache=none/writethrough/directsync to submit the batched IO to disk directly, bypass host page cache.
IO merging also can be performed in the queue.

Quoted 10 lines

Any ideas?

Thanks,
Zhang Haoyu

Quoted 5 lines

In Patch 2 we should complete requests with -EIO if io_submit() returned 0 <= ret < len. I fixed this up when applying because the patch was completing with a bogus ret value.

Stefan