Skip to contentSkip to conversation

virtio-blk: Avoid zeroing every request structure

PATCH Applied Stefan Hajnoczi 11 participants 18 messages May 14, 2010 – May 29, 2010 Kbuild

lore
Share & tools
lore
#0 lore raw

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
index b05d15e..d270225 100644
@@ -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
#1 lore raw
Quoted 5 lines · Stefan Hajnoczi
hw/virtio-blk.c +0 -0
index b05d15e..d270225 100644

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.

#2 lore raw
Quoted 7 lines · Christoph Hellwig
Quoted 6 lines · Stefan Hajnoczi
hw/virtio-blk.c +0 -0
index b05d15e..d270225 100644
@@ -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 -1
  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.

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

Applied
#3 lore raw
In reply to Stefan Hajnoczi

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

Thanks, applied to the block branch.

Kevin

Acked-by on v1 1/1
#4 lore raw
In reply to Stefan Hajnoczi
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.

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

#5 lore raw
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.

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

#6 lore raw
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.

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

#7 lore raw
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.

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

#8 lore raw
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

#9 lore raw
In reply to Stefan Hajnoczi
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
#10 lore raw
Quoted 1 line · Corentin Chary

Did you try to profile using calloc in qemu_mallocz instead 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

#11 lore raw
Quoted 8 lines · Stefan Hajnoczi
Quoted 1 line · Corentin Chary

Did you try to profile using calloc in qemu_mallocz instead 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).

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
#12 lore raw
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

#13 lore raw
Quoted 2 lines · Stefan Hajnoczi

I'll rerun with profiling tomorrow to see if calloc() makes a difference for general qemu_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

#14 lore raw

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(-)
#15 lore raw

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 removed PAGE_RESERVED.

Thanks, both applied.

Quoted 12 lines

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(-)
Signature · 2 lines
Aurelien Jarno                          GPG: 1024D/F1BCDB73
[email protected]                 http://www.aurel32.net
#16 lore raw

PATCH 1/2 Use calloc in qemu_mallocz.

In reply to Stefan Hajnoczi

Avoids 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
index 6cdc5de..1b33e04 100644
@@ -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
#17 lore raw

PATCH 2/2 linux-user: Use qemu-malloc.c.

In reply to Stefan Hajnoczi

Since 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
index a22484e..93ee085 100644
@@ -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