block: enforce minimal 4096 alignment in qemu_blockalign
v8 cover block: enforce minimal 4096 alignment in qemu_blockalign
I have used the following program to test
#define _GNU_SOURCE
#include <stdio.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/types.h>
#include <malloc.h>
#include <string.h>
int main(int argc, char *argv[])
{
int fd = open(argv[1], O_RDWR | O_CREAT | O_DIRECT, 0644);
void *buf;
int i = 0, align = atoi(argv[2]);
do {
buf = memalign(align, 4096);
if (align >= 4096)
break;
if ((unsigned long)buf & 4095)
break;
i++;
} while (1);
printf("%d %p\n", i, buf);
memset(buf, 0x11, 4096);
for (i = 0; i < 100000; i++) {
lseek(fd, SEEK_CUR, 4096);
write(fd, buf, 4096);
}
close(fd);
return 0;
}for in in seq 1 30 ; do a.out aa ; done
The file was placed into 8 GB partition on HDD below to avoid speed change due to different offset on disk. Results are reliable:
- 189 vs 180 seconds on Linux 3.16
The following setups have been tested:
1) ext4 with block size equals to 1024 over 512/512 physical/logical sector size SSD disk
2) ext4 with block size equals to 4096 over 512/512 physical/logical sector size SSD disk
3) ext4 with block size equals to 4096 over 512/4096 physical/logical sector size rotational disk (WDC WD20EZRX)
4) xfs with block size equals to 4096 over 512/512 physical/logical sector size SSD disk
The difference is quite reliable and the same 5%.
qemu-io -n -c 'write -P 0xaa 0 1G' 1.img
for image in qcow2 format is 1% faster.
qemu-img is also affected. The difference in between
qemu-img create -f qcow2 1.img 64G
qemu-io -n -c 'write -P 0xaa 0 1G' 1.img
time for i in seq 1 30 ; do qemu-img convert 1.img -t none -O raw 2.img ; rm -rf 2.img ; done
is around 126 vs 119 seconds.
The justification of the performance improve is quite interesting.
Quoted 1 line
From the kernel point of view each request to the disk was split
by two. This could be seen by blktrace like this:
9,0 11 1 0.000000000 11151 Q WS 312737792 + 1023 [qemu-img] 9,0 11 2 0.000007938 11151 Q WS 312738815 + 8 [qemu-img] 9,0 11 3 0.000030735 11151 Q WS 312738823 + 1016 [qemu-img] 9,0 11 4 0.000032482 11151 Q WS 312739839 + 8 [qemu-img] 9,0 11 5 0.000041379 11151 Q WS 312739847 + 1016 [qemu-img] 9,0 11 6 0.000042818 11151 Q WS 312740863 + 8 [qemu-img] 9,0 11 7 0.000051236 11151 Q WS 312740871 + 1017 [qemu-img] 9,0 5 1 0.169071519 11151 Q WS 312741888 + 1023 [qemu-img] After the patch the pattern becomes normal: 9,0 6 1 0.000000000 12422 Q WS 314834944 + 1024 [qemu-img] 9,0 6 2 0.000038527 12422 Q WS 314835968 + 1024 [qemu-img] 9,0 6 3 0.000072849 12422 Q WS 314836992 + 1024 [qemu-img] 9,0 6 4 0.000106276 12422 Q WS 314838016 + 1024 [qemu-img]
and the amount of requests sent to disk (could be calculated counting number of lines in the output of blktrace) is reduced about 2 times.
Both qemu-img and qemu-io are affected while qemu-kvm is not. The guest does his job well and real requests comes properly aligned (to page).
Changes from v7:
- make assignment from v6 unconditional (Kevin)
Changes from v6:
- explicitely assign opt_mem_alignemnt in raw-posix.c with MAX(s->buf_align, getpagesize()) (Kevin)
Changes from v5:
- found justification from kernel point of view
- fixed checkpatch warnings in the patch 2
Changes from v4:
- patches reordered
- dropped conversion from 512 to BDRV_SECTOR_SIZE
- getpagesize() is replaced with MAX(4096, getpagesize()) as suggested by Kevin
Changes from v3:
- portable way to calculate system page size used
- 512/4096 values are replaced with proper macros/values
Changes from v2:
- opt_mem_alignment is split to opt_mem_alignment for bounce buffering and min_mem_alignment to check buffers coming from guest.
Changes from v1:
- enforces 4096 alignment in qemu_(try_)blockalign, avoid touching of bdrv_qiov_is_aligned path not to enforce additional bounce buffering as suggested by Paolo
- reduces 10% to 5% in patch description to better fit 180 vs 189 difference
PATCH v8 1/2 block: minimal bounce buffer alignment
The patch introduces new concept: minimal memory alignment for bounce buffers. Original so called "optimal" value is actually minimal required value for aligment. It should be used for validation that the IOVec is properly aligned and bounce buffer is not required.
Though, from the performance point of view, it would be better if bounce buffer or IOVec allocated by QEMU will be aligned stricter.
The patch does not change any alignment value yet.
--- block.c | 11 +++++++++++ block/io.c | 7 ++++++- block/raw-posix.c | 1 + include/block/block.h | 2 ++ include/block/block_int.h | 3 +++ 5 files changed, 23 insertions(+), 1 deletion(-)
block.c +11 -0
@@ -113,6 +113,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) { +/* 4k should be on the safe side */ +return 4096 ; + } + +return bs->bl.min_mem_alignment; +} +/* check if the path starts with "<protocol>:" */ int path_has_protocol (const char *path) {
@@ -890,6 +900,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 ;
block/io.c +6 -1
@@ -201,8 +201,10 @@ 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.min_mem_alignment =512 ; bs->bl.opt_mem_alignment =512 ; }
@@ -221,6 +223,9 @@ void bdrv_refresh_limits(BlockDriverState *bs, Error **errp) bs->bl.opt_mem_alignment =MAX (bs->bl.opt_mem_alignment, bs->backing_hd->bl.opt_mem_alignment); + bs->bl.min_mem_alignment = +MAX (bs->bl.min_mem_alignment, + bs->backing_hd->bl.min_mem_alignment); }/* Then let the driver override it */
@@ -2489,7 +2494,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) {
block/raw-posix.c +1 -0
@@ -725,6 +725,7 @@ static void raw_refresh_limits(BlockDriverState *bs, Error **errp)BDRVRawState *s = bs->opaque;raw_probe_alignment (bs, s->fd, errp); + bs->bl.min_mem_alignment = s->buf_align; bs->bl.opt_mem_alignment = s->buf_align; }
include/block/block.h +2 -0
@@ -440,6 +440,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);
include/block/block_int.h +3 -0
@@ -313,6 +313,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;
Signature · 1 line
1.9.1
PATCH v8 2/2 block: align bounce buffers to page
In reply to Denis V. LunevThe 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 changes default bounce buffer optimal alignment to MAX(page size, 4k). 4k is chosen as maximal known sector size on real HDD.
The justification of the performance improve is quite interesting.
Quoted 1 line
From the kernel point of view each request to the disk was split
by two. This could be seen by blktrace like this:
9,0 11 1 0.000000000 11151 Q WS 312737792 + 1023 [qemu-img] 9,0 11 2 0.000007938 11151 Q WS 312738815 + 8 [qemu-img] 9,0 11 3 0.000030735 11151 Q WS 312738823 + 1016 [qemu-img] 9,0 11 4 0.000032482 11151 Q WS 312739839 + 8 [qemu-img] 9,0 11 5 0.000041379 11151 Q WS 312739847 + 1016 [qemu-img] 9,0 11 6 0.000042818 11151 Q WS 312740863 + 8 [qemu-img] 9,0 11 7 0.000051236 11151 Q WS 312740871 + 1017 [qemu-img] 9,0 5 1 0.169071519 11151 Q WS 312741888 + 1023 [qemu-img] After the patch the pattern becomes normal: 9,0 6 1 0.000000000 12422 Q WS 314834944 + 1024 [qemu-img] 9,0 6 2 0.000038527 12422 Q WS 314835968 + 1024 [qemu-img] 9,0 6 3 0.000072849 12422 Q WS 314836992 + 1024 [qemu-img] 9,0 6 4 0.000106276 12422 Q WS 314838016 + 1024 [qemu-img]
and the amount of requests sent to disk (could be calculated counting number of lines in the output of blktrace) is reduced about 2 times.
Both qemu-img and qemu-io are affected while qemu-kvm is not. The guest does his job well and real requests comes properly aligned (to page).
--- block.c | 8 ++++---- block/io.c | 2 +- block/raw-posix.c | 13 +++++++------ 3 files changed, 12 insertions(+), 11 deletions(-)
block.c +4 -4
@@ -106,8 +106,8 @@ int is_windows_drive(const char *filename)size_t bdrv_opt_mem_align (BlockDriverState *bs) {if (!bs || !bs->drv) { -/* 4k should be on the safe side */ -return 4096 ; +/* page size or 4k (hdd sector size) should be on the safe side */ +return MAX (4096 ,getpagesize ()); }return bs->bl.opt_mem_alignment;
@@ -116,8 +116,8 @@ size_t bdrv_opt_mem_align(BlockDriverState *bs)size_t bdrv_min_mem_align (BlockDriverState *bs) {if (!bs || !bs->drv) { -/* 4k should be on the safe side */ -return 4096 ; +/* page size or 4k (hdd sector size) should be on the safe side */ +return MAX (4096 ,getpagesize ()); }return bs->bl.min_mem_alignment;
block/io.c +1 -1
@@ -205,7 +205,7 @@ void bdrv_refresh_limits(BlockDriverState *bs, Error **errp) bs->bl.opt_mem_alignment = bs->file->bl.opt_mem_alignment; }else { bs->bl.min_mem_alignment =512 ; - bs->bl.opt_mem_alignment =512 ; + bs->bl.opt_mem_alignment =getpagesize (); }if (bs->backing_hd) {
block/raw-posix.c +7 -6
@@ -301,6 +301,7 @@ static void raw_probe_alignment(BlockDriverState *bs, int fd, Error **errp) {BDRVRawState *s = bs->opaque;char *buf; +size_t max_align =MAX (MAX_BLOCKSIZE ,getpagesize ());/* For /dev/sg devices the alignment is not really used. With buffered I/O, we don't have any restrictions. */
@@ -330,9 +331,9 @@ static void raw_probe_alignment(BlockDriverState *bs, int fd, Error **errp)/* If we could not get the sizes so far, we can only guess them */ if (!s->buf_align) {size_t align; - buf =qemu_memalign (MAX_BLOCKSIZE ,2 *MAX_BLOCKSIZE ); -for (align =512 ; align <=MAX_BLOCKSIZE ; align <<=1 ) { -if (raw_is_io_aligned (fd, buf + align,MAX_BLOCKSIZE )) { + buf =qemu_memalign (max_align,2 * max_align); +for (align =512 ; align <= max_align; align <<=1 ) { +if (raw_is_io_aligned (fd, buf + align, max_align)) { s->buf_align = align;break ; }
@@ -342,8 +343,8 @@ static void raw_probe_alignment(BlockDriverState *bs, int fd, Error **errp)if (!bs->request_alignment) {size_t align; - buf =qemu_memalign (s->buf_align,MAX_BLOCKSIZE ); -for (align =512 ; align <=MAX_BLOCKSIZE ; align <<=1 ) { + buf =qemu_memalign (s->buf_align, max_align); +for (align =512 ; align <= max_align; align <<=1 ) {if (raw_is_io_aligned (fd, buf, align)) { bs->request_alignment = align;break ;
@@ -726,7 +727,7 @@ static void raw_refresh_limits(BlockDriverState *bs, Error **errp)raw_probe_alignment (bs, s->fd, errp); bs->bl.min_mem_alignment = s->buf_align; - bs->bl.opt_mem_alignment = s->buf_align; + bs->bl.opt_mem_alignment =MAX (s->buf_align,getpagesize ()); }static int check_for_dasd (int fd)
Signature · 1 line
1.9.1
Am 12.05.2015 um 16:30 hat Denis V. Lunev geschrieben:
Quoted 9 lines
I have used the following program to test
[...]
and the amount of requests sent to disk (could be calculated counting number of lines in the output of blktrace) is reduced about 2 times.[...]Signed-off-by Denis V. Lunev <[email protected]>CC Paolo Bonzini <[email protected]>CC Kevin Wolf <[email protected]>CC Stefan Hajnoczi <[email protected]>
Stefan Hajnoczi
AppliedQuoted 116 lines · Denis V. Lunev
I have used the following program to test
#define_GNU_SOURCE#include <
stdio.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/types.h>
#include <malloc.h>
#include <string.h>int
main(int argc, char *argv[]){ int fd = open(argv[1], O_RDWR | O_CREAT | O_DIRECT, 0644); void *buf; int i = 0, align = atoi(argv[2]); do { buf = memalign(align, 4096); if (align >= 4096) break; if ((unsigned long)buf & 4095) break; i++; } while (1); printf("%d %p\n", i, buf); memset(buf, 0x11, 4096); for (i = 0; i < 100000; i++) { lseek(fd, SEEK_CUR, 4096); write(fd, buf, 4096); } close(fd); return 0; }for in in
seq 1 30; do a.out aa ; doneThe file was placed into 8 GB partition on HDD below to avoid speed change due to different offset on disk. Results are reliable:
- 189 vs 180 seconds on Linux 3.16The following setups have been tested:
1) ext4 with block size equals to 1024 over 512/512 physical/logical sector size SSD disk
2) ext4 with block size equals to 4096 over 512/512 physical/logical sector size SSD disk
3) ext4 with block size equals to 4096 over 512/4096 physical/logical sector size rotational disk (WDC WD20EZRX)
4) xfs with block size equals to 4096 over 512/512 physical/logical sector size SSD diskThe difference is quite reliable and the same 5%.
qemu-io -n -c 'write -P0xaa0 1G' 1.img
for image in qcow2 format is 1% faster.qemu-img is also affected. The difference in between
qemu-img create -f qcow2 1.img 64G
qemu-io -n -c 'write -P0xaa0 1G' 1.img
time for i inseq 1 30; do qemu-img convert 1.img -t none -O raw 2.img ; rm -rf 2.img ; done
is around 126 vs 119 seconds.The justification of the performance improve is quite interesting. From the kernel point of view each request to the disk was split by two. This could be seen by blktrace like this:
9,0 11 1 0.000000000 11151 Q WS 312737792 + 1023 [qemu-img] 9,0 11 2 0.000007938 11151 Q WS 312738815 + 8 [qemu-img] 9,0 11 3 0.000030735 11151 Q WS 312738823 + 1016 [qemu-img] 9,0 11 4 0.000032482 11151 Q WS 312739839 + 8 [qemu-img] 9,0 11 5 0.000041379 11151 Q WS 312739847 + 1016 [qemu-img] 9,0 11 6 0.000042818 11151 Q WS 312740863 + 8 [qemu-img] 9,0 11 7 0.000051236 11151 Q WS 312740871 + 1017 [qemu-img] 9,0 5 1 0.169071519 11151 Q WS 312741888 + 1023 [qemu-img] After the patch the pattern becomes normal: 9,0 6 1 0.000000000 12422 Q WS 314834944 + 1024 [qemu-img] 9,0 6 2 0.000038527 12422 Q WS 314835968 + 1024 [qemu-img] 9,0 6 3 0.000072849 12422 Q WS 314836992 + 1024 [qemu-img] 9,0 6 4 0.000106276 12422 Q WS 314838016 + 1024 [qemu-img]and the amount of requests sent to disk (could be calculated counting number of lines in the output of blktrace) is reduced about 2 times.
Both qemu-img and qemu-io are affected while qemu-kvm is not. The guest does his job well and real requests comes properly aligned (to page).
Changes from v7:
- make assignment from v6 unconditional (Kevin)Changes from v6:
- explicitely assignopt_mem_alignemntinraw-posix.cwithMAX(s->buf_align, getpagesize())(Kevin)Changes from v5:
- found justification from kernel point of view
- fixed checkpatch warnings in the patch 2Changes from v4:
- patches reordered
- dropped conversion from 512 toBDRV_SECTOR_SIZE
-getpagesize()is replaced withMAX(4096, getpagesize())as suggested by KevinChanges from v3:
- portable way to calculate system page size used
- 512/4096 values are replaced with proper macros/valuesChanges from v2:
-opt_mem_alignmentis split toopt_mem_alignmentfor bounce buffering andmin_mem_alignmentto check buffers coming from guest.Changes from v1:
- enforces 4096 alignment in qemu_(try_)blockalign, avoid touching ofbdrv_qiov_is_alignedpath not to enforce additional bounce buffering as suggested by Paolo
- reduces 10% to 5% in patch description to better fit 180 vs 189 difference
Quoted 6 lines
Signed-off-by Denis V. Lunev <[email protected]>CC Paolo Bonzini <[email protected]>CC Kevin Wolf <[email protected]>CC Stefan Hajnoczi <[email protected]>
Thanks, applied to my block tree:
https://github.com/stefanha/qemu/commits/block
Stefan