qemu-devel.nongnu.org archive mirror
 help / color / mirror / Atom feed
* [Qemu-devel] [PATCH] block/io: optimize bdrv_co_pwritev for small requests
@ 2016-05-24 13:39 Peter Lieven
  2016-05-24 13:59 ` Paolo Bonzini
  0 siblings, 1 reply; 5+ messages in thread
From: Peter Lieven @ 2016-05-24 13:39 UTC (permalink / raw)
  To: qemu-block; +Cc: qemu-devel, famz, kwolf, stefanha, mreitz, Peter Lieven

in a read-modify-write cycle a small request might cause
head and tail to fall into the same alignment. Currently
QEMU reads the same block twice in this case which is
not necessary.

Signed-off-by: Peter Lieven <pl@kamp.de>
---
 block/io.c | 12 ++++++++++++
 1 file changed, 12 insertions(+)

diff --git a/block/io.c b/block/io.c
index 60a6bd8..fa40121 100644
--- a/block/io.c
+++ b/block/io.c
@@ -1430,6 +1430,18 @@ int coroutine_fn bdrv_co_pwritev(BlockDriverState *bs,
 
         bytes += offset & (align - 1);
         offset = offset & ~(align - 1);
+
+        /* if head and tail fall into the same alignment
+         * we can omit the second read as it would read
+         * the same block again */
+        if ((offset + bytes) & (align - 1) &&
+            offset / align == (offset + bytes) / align) {
+            size_t tail_offs;
+            tail_offs = (offset + bytes) & (align - 1);
+            qemu_iovec_add(&local_qiov, head_buf + tail_offs,
+                           align - tail_offs);
+            bytes += align - tail_offs;
+        }
     }
 
     if ((offset + bytes) & (align - 1)) {
-- 
1.9.1

^ permalink raw reply related	[flat|nested] 5+ messages in thread

* Re: [Qemu-devel] [PATCH] block/io: optimize bdrv_co_pwritev for small requests
  2016-05-24 13:39 [Qemu-devel] [PATCH] block/io: optimize bdrv_co_pwritev for small requests Peter Lieven
@ 2016-05-24 13:59 ` Paolo Bonzini
  2016-05-24 14:07   ` Peter Lieven
  0 siblings, 1 reply; 5+ messages in thread
From: Paolo Bonzini @ 2016-05-24 13:59 UTC (permalink / raw)
  To: Peter Lieven, qemu-block; +Cc: kwolf, famz, qemu-devel, mreitz, stefanha



On 24/05/2016 15:39, Peter Lieven wrote:
>          bytes += offset & (align - 1);
>          offset = offset & ~(align - 1);

Because the low bits have been masked away from offset and added to bytes,

> +
> +        /* if head and tail fall into the same alignment
> +         * we can omit the second read as it would read
> +         * the same block again */
> +        if ((offset + bytes) & (align - 1) &&

... the first part is just "bytes & (align - 1)"...

> +            offset / align == (offset + bytes) / align) {

... and the second part is just "bytes < align" (you can distribute
division over addition because offset / align has no reminder, and
simplify to "0 == bytes / align").

Putting it together, it becomes "bytes > 0 && bytes < align", or even
"bytes < align".

Thanks,

Paolo

> +            size_t tail_offs;
> +            tail_offs = (offset + bytes) & (align - 1);
> +            qemu_iovec_add(&local_qiov, head_buf + tail_offs,
> +                           align - tail_offs);
> +            bytes += align - tail_offs;
> +        }

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: [Qemu-devel] [PATCH] block/io: optimize bdrv_co_pwritev for small requests
  2016-05-24 13:59 ` Paolo Bonzini
@ 2016-05-24 14:07   ` Peter Lieven
  2016-05-24 14:20     ` Paolo Bonzini
  2016-05-24 14:22     ` Kevin Wolf
  0 siblings, 2 replies; 5+ messages in thread
From: Peter Lieven @ 2016-05-24 14:07 UTC (permalink / raw)
  To: Paolo Bonzini, qemu-block; +Cc: kwolf, famz, qemu-devel, mreitz, stefanha

Am 24.05.2016 um 15:59 schrieb Paolo Bonzini:
>
> On 24/05/2016 15:39, Peter Lieven wrote:
>>           bytes += offset & (align - 1);
>>           offset = offset & ~(align - 1);
> Because the low bits have been masked away from offset and added to bytes,
>
>> +
>> +        /* if head and tail fall into the same alignment
>> +         * we can omit the second read as it would read
>> +         * the same block again */
>> +        if ((offset + bytes) & (align - 1) &&
> ... the first part is just "bytes & (align - 1)"...
>
>> +            offset / align == (offset + bytes) / align) {
> ... and the second part is just "bytes < align" (you can distribute
> division over addition because offset / align has no reminder, and
> simplify to "0 == bytes / align").
>
> Putting it together, it becomes "bytes > 0 && bytes < align", or even
> "bytes < align".

Oh, thanks, and the if block also too complicated. If I am right it should
collapse to:

         if (bytes < align) {
             qemu_iovec_add(&local_qiov, head_buf + bytes,
                            align - bytes);
             bytes = align;
         }

Right?

Thanks,
Peter

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: [Qemu-devel] [PATCH] block/io: optimize bdrv_co_pwritev for small requests
  2016-05-24 14:07   ` Peter Lieven
@ 2016-05-24 14:20     ` Paolo Bonzini
  2016-05-24 14:22     ` Kevin Wolf
  1 sibling, 0 replies; 5+ messages in thread
From: Paolo Bonzini @ 2016-05-24 14:20 UTC (permalink / raw)
  To: Peter Lieven, qemu-block; +Cc: kwolf, famz, qemu-devel, stefanha, mreitz



On 24/05/2016 16:07, Peter Lieven wrote:
> 
> Oh, thanks, and the if block also too complicated. If I am right it should
> collapse to:
> 
>         if (bytes < align) {
>             qemu_iovec_add(&local_qiov, head_buf + bytes,
>                            align - bytes);
>             bytes = align;
>         }
> 
> Right?

Yes, that should work.  But add a comment because it's much more
mysterious than your v1 patch. :)

Even just

	/* We have read the tail already if the request is smaller
	 * than one aligned block.
	 */

Thanks,

Paolo

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: [Qemu-devel] [PATCH] block/io: optimize bdrv_co_pwritev for small requests
  2016-05-24 14:07   ` Peter Lieven
  2016-05-24 14:20     ` Paolo Bonzini
@ 2016-05-24 14:22     ` Kevin Wolf
  1 sibling, 0 replies; 5+ messages in thread
From: Kevin Wolf @ 2016-05-24 14:22 UTC (permalink / raw)
  To: Peter Lieven
  Cc: Paolo Bonzini, qemu-block, famz, qemu-devel, mreitz, stefanha

Am 2.05.2016 um 16:07 hat Peter Lieven geschrieben:
> Am 24.05.2016 um 15:59 schrieb Paolo Bonzini:
> >
> >On 24/05/2016 15:39, Peter Lieven wrote:
> >>          bytes += offset & (align - 1);
> >>          offset = offset & ~(align - 1);
> >Because the low bits have been masked away from offset and added to bytes,
> >
> >>+
> >>+        /* if head and tail fall into the same alignment
> >>+         * we can omit the second read as it would read
> >>+         * the same block again */
> >>+        if ((offset + bytes) & (align - 1) &&
> >... the first part is just "bytes & (align - 1)"...
> >
> >>+            offset / align == (offset + bytes) / align) {
> >... and the second part is just "bytes < align" (you can distribute
> >division over addition because offset / align has no reminder, and
> >simplify to "0 == bytes / align").
> >
> >Putting it together, it becomes "bytes > 0 && bytes < align", or even
> >"bytes < align".
> 
> Oh, thanks, and the if block also too complicated. If I am right it should
> collapse to:
> 
>         if (bytes < align) {
>             qemu_iovec_add(&local_qiov, head_buf + bytes,
>                            align - bytes);
>             bytes = align;
>         }
> 
> Right?

Looks good to me.

Another mostly unrelated thing I just noticed while looking at this
code: Should we assert(is_power_of_2(align)) somewhere?

Kevin

^ permalink raw reply	[flat|nested] 5+ messages in thread

end of thread, other threads:[~2016-05-24 14:22 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2016-05-24 13:39 [Qemu-devel] [PATCH] block/io: optimize bdrv_co_pwritev for small requests Peter Lieven
2016-05-24 13:59 ` Paolo Bonzini
2016-05-24 14:07   ` Peter Lieven
2016-05-24 14:20     ` Paolo Bonzini
2016-05-24 14:22     ` Kevin Wolf

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).