qemu-devel.nongnu.org archive mirror
 help / color / mirror / Atom feed
From: "Denis V. Lunev" <den@openvz.org>
Cc: Kevin Wolf <kwolf@redhat.com>, "Denis V. Lunev" <den@openvz.org>,
	qemu-devel@nongnu.org, Paolo Bonzini <pbonzini@redhat.com>
Subject: [Qemu-devel] [PATCH 2/2] block: align bounce buffers to page
Date: Fri,  6 Feb 2015 20:37:52 +0300	[thread overview]
Message-ID: <1423244272-24887-3-git-send-email-den@openvz.org> (raw)
In-Reply-To: <1423244272-24887-1-git-send-email-den@openvz.org>

The following sequence
    int fd = open(argv[1], O_RDWR | O_CREAT | O_DIRECT, 0644);
    for (i = 0; i < 100000; i++)
            write(fd, buf, 4096);
performs 5% better if buf is aligned to 4096 bytes.

The difference is quite reliable.

On the other hand we do not want at the moment to enforce bounce
buffering if guest request is aligned to 512 bytes.

The patch introduces new concept: minimal memory alignment for bounce
buffers. Original so called "optimal" value is actually minimal required
value for aligment. Optimal should be set to page size by default.
There is no driver which should change this default at the moment.

Signed-off-by: Denis V. Lunev <den@openvz.org>
CC: Paolo Bonzini <pbonzini@redhat.com>
CC: Kevin Wolf <kwolf@redhat.com>
---
 block.c                   | 20 ++++++++++++++++++--
 block/raw-posix.c         |  2 +-
 include/block/block.h     |  2 ++
 include/block/block_int.h |  3 +++
 4 files changed, 24 insertions(+), 3 deletions(-)

diff --git a/block.c b/block.c
index e98d651..569a2d8 100644
--- a/block.c
+++ b/block.c
@@ -232,6 +232,16 @@ size_t bdrv_opt_mem_align(BlockDriverState *bs)
     return bs->bl.opt_mem_alignment;
 }
 
+size_t bdrv_min_mem_align(BlockDriverState *bs)
+{
+    if (!bs || !bs->drv) {
+        /* page size should be on the safe side */
+        return getpagesize();
+    }
+
+    return bs->bl.min_mem_alignment;
+}
+
 /* check if the path starts with "<protocol>:" */
 int path_has_protocol(const char *path)
 {
@@ -541,9 +551,11 @@ void bdrv_refresh_limits(BlockDriverState *bs, Error **errp)
         }
         bs->bl.opt_transfer_length = bs->file->bl.opt_transfer_length;
         bs->bl.max_transfer_length = bs->file->bl.max_transfer_length;
+        bs->bl.min_mem_alignment = bs->file->bl.min_mem_alignment;
         bs->bl.opt_mem_alignment = bs->file->bl.opt_mem_alignment;
     } else {
-        bs->bl.opt_mem_alignment = BDRV_SECTOR_SIZE;
+        bs->bl.min_mem_alignment = BDRV_SECTOR_SIZE;
+        bs->bl.opt_mem_alignment = getpagesize();
     }
 
     if (bs->backing_hd) {
@@ -558,6 +570,9 @@ void bdrv_refresh_limits(BlockDriverState *bs, Error **errp)
         bs->bl.max_transfer_length =
             MIN_NON_ZERO(bs->bl.max_transfer_length,
                          bs->backing_hd->bl.max_transfer_length);
+        bs->bl.min_mem_alignment =
+            MAX(bs->bl.min_mem_alignment,
+                bs->backing_hd->bl.min_mem_alignment);
         bs->bl.opt_mem_alignment =
             MAX(bs->bl.opt_mem_alignment,
                 bs->backing_hd->bl.opt_mem_alignment);
@@ -1044,6 +1059,7 @@ static int bdrv_open_common(BlockDriverState *bs, BlockDriverState *file,
     }
 
     assert(bdrv_opt_mem_align(bs) != 0);
+    assert(bdrv_min_mem_align(bs) != 0);
     assert((bs->request_alignment != 0) || bs->sg);
     return 0;
 
@@ -5331,7 +5347,7 @@ void *qemu_try_blockalign0(BlockDriverState *bs, size_t size)
 bool bdrv_qiov_is_aligned(BlockDriverState *bs, QEMUIOVector *qiov)
 {
     int i;
-    size_t alignment = bdrv_opt_mem_align(bs);
+    size_t alignment = bdrv_min_mem_align(bs);
 
     for (i = 0; i < qiov->niov; i++) {
         if ((uintptr_t) qiov->iov[i].iov_base % alignment) {
diff --git a/block/raw-posix.c b/block/raw-posix.c
index 9848f83..1205e9d 100644
--- a/block/raw-posix.c
+++ b/block/raw-posix.c
@@ -650,7 +650,7 @@ static void raw_refresh_limits(BlockDriverState *bs, Error **errp)
     BDRVRawState *s = bs->opaque;
 
     raw_probe_alignment(bs, s->fd, errp);
-    bs->bl.opt_mem_alignment = s->buf_align;
+    bs->bl.min_mem_alignment = s->buf_align;
 }
 
 static ssize_t handle_aiocb_ioctl(RawPosixAIOData *aiocb)
diff --git a/include/block/block.h b/include/block/block.h
index 3082d2b..b9b24b5 100644
--- a/include/block/block.h
+++ b/include/block/block.h
@@ -424,6 +424,8 @@ void bdrv_img_create(const char *filename, const char *fmt,
 
 /* Returns the alignment in bytes that is required so that no bounce buffer
  * is required throughout the stack */
+size_t bdrv_min_mem_align(BlockDriverState *bs);
+/* Returns optimal alignment in bytes for bounce buffer */
 size_t bdrv_opt_mem_align(BlockDriverState *bs);
 void bdrv_set_guest_block_size(BlockDriverState *bs, int align);
 void *qemu_blockalign(BlockDriverState *bs, size_t size);
diff --git a/include/block/block_int.h b/include/block/block_int.h
index e264be9..98b183c 100644
--- a/include/block/block_int.h
+++ b/include/block/block_int.h
@@ -296,6 +296,9 @@ typedef struct BlockLimits {
     int max_transfer_length;
 
     /* memory alignment so that no bounce buffer is needed */
+    size_t min_mem_alignment;
+
+    /* memory alignment for bounce buffer */
     size_t opt_mem_alignment;
 } BlockLimits;
 
-- 
1.9.1

  parent reply	other threads:[~2015-02-06 17:37 UTC|newest]

Thread overview: 18+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2015-02-06 17:37 [Qemu-devel] [PATCH v4 0/1] block: enforce minimal 4096 alignment in qemu_blockalign Denis V. Lunev
2015-02-06 17:37 ` [Qemu-devel] [PATCH 1/2] block, raw-posix: replace 512/4096 constants with proper macros/values Denis V. Lunev
2015-02-16 10:32   ` Kevin Wolf
2015-02-16 10:34     ` Denis V. Lunev
2015-02-06 17:37 ` Denis V. Lunev [this message]
2015-02-16 10:59   ` [Qemu-devel] [PATCH 2/2] block: align bounce buffers to page Kevin Wolf
2015-02-16 11:14     ` Denis V. Lunev
2015-02-13  7:52 ` [Qemu-devel] [PATCH v4 0/1] block: enforce minimal 4096 alignment in qemu_blockalign Denis V. Lunev
  -- strict thread matches above, loose matches on Subject: below --
2015-05-04 13:42 [Qemu-devel] [PATCH v5 0/2] " Denis V. Lunev
2015-05-04 13:42 ` [Qemu-devel] [PATCH 2/2] block: align bounce buffers to page Denis V. Lunev
2015-05-12  5:47 [Qemu-devel] [PATCH v6 0/2] block: enforce minimal 4096 alignment in qemu_blockalign Denis V. Lunev
2015-05-12  5:47 ` [Qemu-devel] [PATCH 2/2] block: align bounce buffers to page Denis V. Lunev
2015-05-12 10:27   ` Kevin Wolf
2015-05-12 10:50     ` Paolo Bonzini
2015-05-12 13:40 [Qemu-devel] [PATCH v7 0/2] block: enforce minimal 4096 alignment in qemu_blockalign Denis V. Lunev
2015-05-12 13:41 ` [Qemu-devel] [PATCH 2/2] block: align bounce buffers to page Denis V. Lunev
2015-05-12 14:08   ` Kevin Wolf
2015-05-12 14:20     ` Denis V. Lunev
2015-05-12 14:26       ` Kevin Wolf
2015-05-12 14:26         ` Denis V. Lunev
2015-05-12 14:30 [Qemu-devel] [PATCH v8 0/2] block: enforce minimal 4096 alignment in qemu_blockalign Denis V. Lunev
2015-05-12 14:30 ` [Qemu-devel] [PATCH 2/2] block: align bounce buffers to page Denis V. Lunev

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=1423244272-24887-3-git-send-email-den@openvz.org \
    --to=den@openvz.org \
    --cc=kwolf@redhat.com \
    --cc=pbonzini@redhat.com \
    --cc=qemu-devel@nongnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).