virtio-blk: Avoid zeroing every request structure
PATCH 1/1 virtio-blk: Avoid zeroing every request structure
The VirtIOBlockRequest structure is about 40 KB in size. This patch avoids zeroing every request by only initializing fields that are read. The other fields are either written to or may not be used at all.
Oprofile shows about 10% of CPU samples in memset called by virtio_blk_alloc_request(). The workload is
dd if=/dev/vda of=/dev/null iflag=direct bs=8k running concurrently 4 times. This patch makes memset disappear to the bottom of the profile.
---
This applies to qemu.git and qemu-kvm.git.
A related change would be a pool of requests to avoid malloc/free for every single request. That's a separate change and malloc/free do not show up at the top of the profile, so I am not introducing a pool yet.
hw/virtio-blk.c | 4 +++- 1 files changed, 3 insertions(+), 1 deletions(-)
hw/virtio-blk.c +3 -1
@@ -105,8 +105,10 @@ static void virtio_blk_flush_complete(void *opaque, int ret)static VirtIOBlockReq *virtio_blk_alloc_request (VirtIOBlock *s) { -VirtIOBlockReq *req =qemu_mallocz (sizeof (*req)); +VirtIOBlockReq *req =qemu_malloc (sizeof (*req)); req->dev = s; + req->qiov.size =0 ; + req->next =NULL ;return req; }
Signature · 1 line
1.7.1
Quoted 5 lines · Stefan Hajnoczi
hw/virtio-blk.c +0
-0
hw/virtio-blk.c static void virtio_blk_flush_complete(void *opaque, int ret) · line 112
Quoted 9 lines
hw/virtio-blk.c +3
-1@@ -105,8 +105,10 @@ static void virtio_blk_flush_complete(void *opaque, int ret)static VirtIOBlockReq *virtio_blk_alloc_request (VirtIOBlock *s) { -VirtIOBlockReq *req =qemu_mallocz (sizeof (*req)); +VirtIOBlockReq *req =qemu_malloc (sizeof (*req)); req->dev = s; + req->qiov.size =0 ; + req->next =NULL ;return req;
Looks good, but you shouldn't even need to initialize req->qiov.size, we do this later by calling qemu_iovec_init_external before using it.
Quoted 7 lines · Christoph Hellwig
Quoted 6 lines · Stefan Hajnoczi
hw/virtio-blk.c +0
-0@@ -105,8 +105,10 @@ static void virtio_blk_flush_complete(void *opaque, int ret)
Quoted 11 lines
hw/virtio-blk.c static void virtio_blk_flush_complete(void *opaque, int ret) · line 112
Quoted 8 lines
hw/virtio-blk.c +3
-1static VirtIOBlockReq *virtio_blk_alloc_request(VirtIOBlock *s) { - VirtIOBlockReq *req = qemu_mallocz(sizeof(*req)); + VirtIOBlockReq *req = qemu_malloc(sizeof(*req)); req->dev = s; + req->qiov.size = 0; + req->next = NULL; return req;Looks good, but you shouldn't even need to initialize
req->qiov.size, we do this later by callingqemu_iovec_init_externalbefore using it.
virtio_blk_req_complete() uses req->qiov.size and may be called by virtio_blk_handle_flush() or virtio_blk_handle_scsi() without being initialized. It's a little ugly that we use the qiov like that.
Stefan
Kevin Wolf
AppliedAm 14.05.2010 23:52, schrieb Stefan Hajnoczi:
Quoted 10 lines
The VirtIOBlockRequest structure is about 40 KB in size. This patch avoids zeroing every request by only initializing fields that are read. The other fields are either written to or may not be used at all.
Oprofile shows about 10% of CPU samples in memset called by
virtio_blk_alloc_request(). The workload is
dd if=/dev/vda of=/dev/null iflag=direct bs=8k running concurrently 4 times. This patch makes memset disappear to the bottom of the profile.Signed-off-by Stefan Hajnoczi <[email protected]>
Thanks, applied to the block branch.
Kevin
Quoted 10 lines · Stefan Hajnoczi
The VirtIOBlockRequest structure is about 40 KB in size. This patch avoids zeroing every request by only initializing fields that are read. The other fields are either written to or may not be used at all.
Oprofile shows about 10% of CPU samples in memset called by
virtio_blk_alloc_request(). The workload is
dd if=/dev/vda of=/dev/null iflag=direct bs=8k running concurrently 4 times. This patch makes memset disappear to the bottom of the profile.Signed-off-by Stefan Hajnoczi <[email protected]>
Great catch!
I ran some benchmarks using a ramdisk passed to the guest as a virtio device and with this patch I saw improvements ranging from 5-20%. I believe the fluctuations are due to not being able to numa bind it due to limited memory.
However a win all the way round!
Jes
Quoted 14 lines · Jes Sorensen
Quoted 11 lines · Stefan Hajnoczi
The VirtIOBlockRequest structure is about 40 KB in size. This patch avoids zeroing every request by only initializing fields that are read. The other fields are either written to or may not be used at all.
Oprofile shows about 10% of CPU samples in memset called by
virtio_blk_alloc_request(). The workload is
dd if=/dev/vda of=/dev/null iflag=direct bs=8k running concurrently 4 times. This patch makes memset disappear to the bottom of the profile.Signed-off-by Stefan Hajnoczi <[email protected]>
Quoted 9 lines
Great catch!
I ran some benchmarks using a ramdisk passed to the guest as a virtio device and with this patch I saw improvements ranging from 5-20%. I believe the fluctuations are due to not being able to numa bind it due to limited memory.
However a win all the way round!
It looks like a fairly small change with a huge win. Sounds like a perfect candidate for 0.12.5 to me.
Alex
Quoted 24 lines · Alexander Graf
Quoted 22 lines · Jes Sorensen
Quoted 4 lines · Stefan Hajnoczi
The VirtIOBlockRequest structure is about 40 KB in size. This patch avoids zeroing every request by only initializing fields that are read. The other fields are either written to or may not be used at all.
Quoted 8 lines
Oprofile shows about 10% of CPU samples in memset called by
virtio_blk_alloc_request(). The workload is
dd if=/dev/vda of=/dev/null iflag=direct bs=8k running concurrently 4 times. This patch makes memset disappear to the bottom of the profile.Signed-off-by Stefan Hajnoczi<[email protected]>Great catch!
I ran some benchmarks using a ramdisk passed to the guest as a virtio device and with this patch I saw improvements ranging from 5-20%. I believe the fluctuations are due to not being able to numa bind it due to limited memory.
Quoted 6 lines
Quoted 3 lines
However a win all the way round!
It looks like a fairly small change with a huge win. Sounds like a perfect candidate for 0.12.5 to me.
I'd prefer to stick to bug fixes for stable releases. Performance improvements are a good motivation for people to upgrade to 0.13 :-)
Regards,
Anthony Liguori
Quoted 4 lines
Alex
Quoted 30 lines · Anthony Liguori
Quoted 29 lines · Alexander Graf
Quoted 27 lines · Jes Sorensen
Quoted 5 lines · Stefan Hajnoczi
The VirtIOBlockRequest structure is about 40 KB in size. This patch avoids zeroing every request by only initializing fields that are read.
The other fields are either written to or may not be used at all.Quoted 9 lines
Oprofile shows about 10% of CPU samples in memset called by
virtio_blk_alloc_request(). The workload is
dd if=/dev/vda of=/dev/null iflag=direct bs=8k running concurrently 4 times. This patch makes memset disappear to the bottom of the profile.Signed-off-by Stefan Hajnoczi<[email protected]>Great catch!
I ran some benchmarks using a ramdisk passed to the guest as a virtio device and with this patch I saw improvements ranging from 5-20%. I believe the fluctuations are due to not being able to numa bind it due to limited memory.
However a win all the way round!
Quoted 6 lines
Quoted 3 lines
It looks like a fairly small change with a huge win. Sounds like a perfect candidate for 0.12.5 to me.
I'd prefer to stick to bug fixes for stable releases. Performance improvements are a good motivation for people to upgrade to 0.13 :-)
In general I agree, but this one looks like a really simple one.
Alex
Quoted 5 lines · Alexander Graf
Quoted 2 lines · Anthony Liguori
I'd prefer to stick to bug fixes for stable releases. Performance improvements are a good motivation for people to upgrade to 0.13 :-)
In general I agree, but this one looks like a really simple one.
Besides, there are too many reported guest regressions at the moment to upgrade if using any of them.
-- Jamie
Quoted 9 lines · Stefan Hajnoczi
The VirtIOBlockRequest structure is about 40 KB in size. This patch avoids zeroing every request by only initializing fields that are read. The other fields are either written to or may not be used at all.
Oprofile shows about 10% of CPU samples in memset called by
virtio_blk_alloc_request(). The workload is
dd if=/dev/vda of=/dev/null iflag=direct bs=8k running concurrently 4 times. This patch makes memset disappear to the bottom of the profile.
Did you try to profile using calloc in qemu_mallocz instead of malloc + memset ?
$ man calloc calloc() allocates memory for an array of nmemb elements of size bytes each and returns a pointer to the allocated memory. __The memory is set to zero.__
Signature · 2 lines
Corentin Chary http://xf.iksaif.net
Quoted 1 line · Corentin Chary
Did you try to profile using calloc in
qemu_malloczinstead of malloc + memset ?
No, I didn't try it. I don't see how it could perform on par with not clearing memory at all. Something is going to have to clear that memory when using memset() or calloc(). Either libc or the kernel (if using mmap).
Stefan
Quoted 8 lines · Stefan Hajnoczi
Quoted 1 line · Corentin Chary
Did you try to profile using calloc in
qemu_malloczinstead of malloc + memset ?No, I didn't try it. I don't see how it could perform on par with not clearing memory at all. Something is going to have to clear that memory when using
memset()orcalloc(). Either libc or the kernel (if using mmap).
I believe that if the allocation size is large enougth, getting a zeroed page can be almost free with clever memory management. Could you try to re-run your test with calloc and see what it does ? Speeding up all mallocz calls is probably a good idea :)
Signature · 2 lines
Corentin Chary http://xf.iksaif.net
Quoted 4 lines · Corentin Chary
I believe that if the allocation size is large enougth, getting a zeroed page can be almost free with clever memory management. Could you try to re-run your test with calloc and see what it does ? Speeding up all mallocz calls is probably a good idea :)
I think that scenario is libc using mmap to grab zeroed pages from the kernel. If the kernel manages zeroed pages so that zeroing is performed out-of-line (e.g. there is a pool of zeroed pages waiting), then it appears that calloc() is getting zeroed memory cheaply.
I'll rerun with profiling tomorrow to see if calloc() makes a difference for general qemu_mallocz() usage.
In the case of virtio-blk requests, we definitely shouldn't be clearing that memory even if calloc() were cheaper. Much of the request structure will not be touched in an average I/O request. Using zeroed pages is wasteful because they aren't needed here.
Stefan
Quoted 2 lines · Stefan Hajnoczi
I'll rerun with profiling tomorrow to see if
calloc()makes a difference for generalqemu_mallocz()usage.
The results are unchanged for direct calloc() instead of malloc+memset. The memset() symbol is still at the top of the profile because glibc is using it internally for calloc().
In theory calloc() allows libc to do optimizations so I think changing qemu_mallocz() to use calloc() is reasonable.
Stefan
cover Tidy qemu_malloc
I was reminded of this by the discussion on the list recently of using calloc for qemu_mallocz. I'd vaguely remembered that I already had a patch that did this.
The second patch is a cleanup enabled by Paul's patch:
2e9a5713f0567fffaa3518f495b8d16a2b74f84a
which removed PAGE_RESERVED.
r~
Richard Henderson (2):
Use calloc in qemu_mallocz.
linux-user: Use qemu-malloc.c.
Makefile.target | 3 ++- linux-user/mmap.c | 52 ---------------------------------------------------- qemu-malloc.c | 8 ++++---- 3 files changed, 6 insertions(+), 57 deletions(-)
Re: [Qemu-devel] [PATCH 0/2] Tidy qemu_malloc
Quoted 9 lines · Richard Henderson
I was reminded of this by the discussion on the list recently of using calloc for
qemu_mallocz. I'd vaguely remembered that I already had a patch that did this.The second patch is a cleanup enabled by Paul's patch:
2e9a5713f0567fffaa3518f495b8d16a2b74f84a
which removedPAGE_RESERVED.
Thanks, both applied.
Quoted 12 lines
Richard Henderson (2):
Use calloc inqemu_mallocz.
linux-user: Useqemu-malloc.c.Makefile.target | 3 ++- linux-user/mmap.c | 52 ---------------------------------------------------- qemu-malloc.c | 8 ++++---- 3 files changed, 6 insertions(+), 57 deletions(-)
Signature · 2 lines
Aurelien Jarno GPG: 1024D/F1BCDB73 [email protected] http://www.aurel32.net
PATCH 1/2 Use calloc in qemu_mallocz.
In reply to Stefan HajnocziAvoids the memset if the allocator has gotten new zeroed storage from the operating system.
--- qemu-malloc.c | 8 ++++---- 1 files changed, 4 insertions(+), 4 deletions(-)
qemu-malloc.c +4 -4
@@ -69,10 +69,10 @@ void *qemu_realloc(void *ptr, size_t size)void *qemu_mallocz (size_t size) { -void *ptr; - ptr =qemu_malloc (size); -memset (ptr,0 , size); -return ptr; +if (!size && !allow_zero_malloc ()) { +abort (); + } +return oom_check (calloc (1 , size ? size :1 )); }char *qemu_strdup (const char *str)
Signature · 1 line
1.7.0.1
PATCH 2/2 linux-user: Use qemu-malloc.c.
In reply to Stefan HajnocziSince we're no longer setting PAGE_RESERVED, there's no need to implement qemu_malloc via mmap.
--- Makefile.target | 3 ++- linux-user/mmap.c | 52 ---------------------------------------------------- 2 files changed, 2 insertions(+), 53 deletions(-)
Makefile.target +2 -1
@@ -85,7 +85,8 @@ $(call set-vpath, $(SRC_PATH)/linux-user:$(SRC_PATH)/linux-user/$(TARGET_ABI_DIR QEMU_CFLAGS+=-I$(SRC_PATH)/linux-user -I$(SRC_PATH)/linux-user/$(TARGET_ABI_DIR) obj-y = main.o syscall.o strace.o mmap.o signal.o thunk.o \ - elfload.o linuxload.o uaccess.o gdbstub.o cpu-uname.o + elfload.o linuxload.o uaccess.o gdbstub.o cpu-uname.o \ + qemu-malloc.o obj-$(TARGET_HAS_BFLT) += flatload.o
linux-user/mmap.c +0 -52
58 lines not on this page · raw message
Signature · 1 line
1.7.0.1