block/io: optimize bdrv_co_pwritev for small requests
PATCH 1/1 block/io: optimize bdrv_co_pwritev for small requests
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.
--- block/io.c | 12 ++++++++++++ 1 file changed, 12 insertions(+)
block/io.c +12 -0
@@ -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 )) {
Signature · 1 line
1.9.1
Quoted 2 lines · Peter Lieven
bytes += offset & (align - 1);
offset = offset & ~(align - 1);Because the low bits have been masked away from offset and added to bytes,
block/io.c int coroutine_fn bdrv_co_pwritev(BlockDriverState *bs, · line 1437
Quoted 5 lines
block/io.c +5
-0+ +/* 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)"...
Quoted 1 line
+ 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
Quoted 6 lines
block/io.c +6
-0+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; + }
Am 24.05.2016 um 15:59 schrieb Paolo Bonzini:
Quoted 13 lines · Peter Lieven
Quoted 2 lines
bytes += offset & (align - 1); offset = offset & ~(align - 1);Because the low bits have been masked away from offset and added to bytes,
block/io.c int coroutine_fn bdrv_co_pwritev(BlockDriverState *bs, · line 1437
Quoted 5 lines
block/io.c +5
-0+ +/* 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)"...
Quoted 7 lines
Quoted 1 line
+ 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
Quoted 11 lines · Peter Lieven
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
Am 2.05.2016 um 16:07 hat Peter Lieven geschrieben:
Quoted 25 lines
Am 24.05.2016 um 15:59 schrieb Paolo Bonzini:
Quoted 13 lines · Peter Lieven
Quoted 2 lines
bytes += offset & (align - 1); offset = offset & ~(align - 1);Because the low bits have been masked away from offset and added to bytes,
block/io.c int coroutine_fn bdrv_co_pwritev(BlockDriverState *bs, · line 1437
Quoted 5 lines
block/io.c +5
-0+ +/* 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)"...
Quoted 7 lines
Quoted 1 line
+ 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:
Quoted 7 lines
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