Skip to contentSkip to conversation

CPU consuming optimization

PATCH In review Dmitry Konishchev 4 participants 13 messages May 17, 2011 – May 18, 2011

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 CPU consuming optimization

Hi! I was wondering why qemu-img consumes so much CPU when it converts one partially allocated qcow2 image to another qcow2 image and I've written a patch which improves the situation a little.

I have an image:

Quoted 6 lines
$ qemu-img info ubuntu.10.04.qcow2
image: ubuntu.10.04.qcow2
file format: qcow2
virtual size: 20G (21474836480 bytes)
disk size: 2.2G
cluster_size: 65536

I create a new copy on write image:

Quoted 1 line
$ qemu-img create -f qcow2 -o backing_file=ubuntu.10.04.qcow2 volume.qcow2 100G

... and use it for a while.

Then I want to create a non-copy on write image from it to send it to someone:

Quoted 1 line

qemu-img convert -O qcow2 volume.qcow2 snapshot.qcow2

The last operation consumes a lot of CPU, so I run qemu-img under profiler and realized, that most of CPU time is consumed by is_not_zero() function. I had made a couple of optimizations on it and got the following output for time qemu-img convert -O qcow2 volume.qcow2 snapshot.qcow2:

x86_64 machine:

Original qemu-img:
real 0m56.159s
user 0m34.670s
sys 0m12.079s

Patched qemu-img:
real 0m34.805s
user 0m18.445s
sys 0m12.552s

x86 machine:

Original qemu-img:
real 1m13.991s
user 0m24.734s
sys 0m6.604s

Patched qemu-img:
real 1m6.898s
user 0m16.021s
sys 0m6.700s

Please, see on the consumed user CPU time. I think that the optimization worth it, so it will be awesome if you accept this patch (see the attachment).

Thanks for your attention.

From 61d228c0ea0d518de48a08577cd6d282e2f97759 Mon Sep 17 00:00:00 2001
From: Dmitry Konishchev <[email protected]>
Date: Tue, 17 May 2011 16:29:48 +0400
Subject: [PATCH] is_not_zero() optimization in qemu-img

---
 qemu-img.c |   24 +++++++++++++++++++++---
 1 files changed, 21 insertions(+), 3 deletions(-)
qemu-img.c +21 -3
index e825123..41b4e32 100644
@@ -498,12 +498,30 @@ static int img_commit(int argc, char **argv)
 
 static int is_not_zero(const uint8_t *sector, int len)
 {
+    /*
+     * Use long as the biggest available internal data type that fits into the
+     * CPU register and unroll the loop to smooth out the effect of memory
+     * latency.
+     */
+
     int i;
-    len >>= 2;
-    for(i = 0;i < len; i++) {
-        if (((uint32_t *)sector)[i] != 0)
+    len /= sizeof(long);
+
+    long d0;
+    long d1;
+    long d2;
+    long d3;
+
+    for(i = 0; i < len; i += 4) {
+        d0 = ((const long*) sector)[i + 0];
+        d1 = ((const long*) sector)[i + 1];
+        d2 = ((const long*) sector)[i + 2];
+        d3 = ((const long*) sector)[i + 3];
+
+        if (d0 || d1 || d2 || d3)
             return 1;
     }
+
     return 0;
 }
 
Signature · 1 line
1.7.4.1
#1 lore raw
Quoted 3 lines · Dmitry Konishchev

Hi! I was wondering why qemu-img consumes so much CPU when it converts one partially allocated qcow2 image to another qcow2 image and I've written a patch which improves the situation a little.

Please see http://wiki.qemu.org/Contribute/SubmitAPatch, which asks that patches are sent inline (not as attachments) for easy review and that you follow the coding style (see the CODING_STYLE file). Patches also need a Signed-off-by: line.

The unrolled loop makes the function rely on len being a multiple of sizeof(long) * 4, otherwise it accesses beyond the end of sector[]. So for this use case it's okay but the function is generic anymore.

GNU cp(1) tries to detect holes in files and image formats could tell us about unallocated regions using bdrv_is_allocated(). So I think there are ways to avoid comparing so much data in the first place, if you are interested in looking into that.

Stefan

#2 lore raw
Quoted 4 lines · Stefan Hajnoczi

Please see http://wiki.qemu.org/Contribute/SubmitAPatch, which asks that patches are sent inline (not as attachments) for easy review and that you follow the coding style (see the CODING_STYLE file).  Patches also need a Signed-off-by: line.

OK, thanks. I'll fix this in case you are willing to accept the patch.

Quoted 3 lines · Stefan Hajnoczi

The unrolled loop makes the function rely on len being a multiple of sizeof(long) * 4, otherwise it accesses beyond the end of sector[]. So for this use case it's okay but the function is generic anymore.

Yeah, but this function is static and within the whole file it is used only for comparing clusters, so I think that we can sacrifice it's universality for the sake of the performance.

Quoted 4 lines · Stefan Hajnoczi

GNU cp(1) tries to detect holes in files and image formats could tell us about unallocated regions using bdrv_is_allocated().  So I think there are ways to avoid comparing so much data in the first place, if you are interested in looking into that.

OK, thanks, I'll look on this function (but sorry, only after 2 weeks, since I'm on my vacation). But actually I think, that it will be better to use the both ways, since they can give a boost in different usecases.

So, if you are agreed with the said above, you can accept this patch and then I'll write an enchancement for it with bdrv_is_allocated() because it is going to include this patch.

--
Dmitry Konishchev
mailto:[email protected]

#3 lore raw
Quoted 3 lines · Dmitry Konishchev

So, if you are agreed with the said above, you can accept this patch and then I'll write an enchancement for it with bdrv_is_allocated() because it is going to include this patch.

Yes, optimizing is_not_zero() is good. The only additional thing I suggest is adding a comment before the function to document the length constraint.

Kevin Wolf is CCed, he's the QEMU block layer maintainer and may have additional thoughts before accepting this patch.

Stefan

#4 lore raw

Am 18.05.2011 09:57, schrieb Stefan Hajnoczi:

Quoted 11 lines · Dmitry Konishchev
Quoted 3 lines

So, if you are agreed with the said above, you can accept this patch and then I'll write an enchancement for it with bdrv_is_allocated() because it is going to include this patch.

Yes, optimizing is_not_zero() is good. The only additional thing I suggest is adding a comment before the function to document the length constraint.

Kevin Wolf is CCed, he's the QEMU block layer maintainer and may have additional thoughts before accepting this patch.

For this one not really. Except for the coding style it looks good to me.

A future bdrv_is_allocated() patch must make sure that the conversion falls back to a simple is_not_zero() when a backing file is used.

Kevin

#5 lore raw
In reply to Stefan Hajnoczi
Quoted 3 lines · Stefan Hajnoczi

Yes, optimizing is_not_zero() is good. The only additional thing I suggest is adding a comment before the function to document the length constraint.

OK, fixed.

Quoted 2 lines · Kevin Wolf

A future bdrv_is_allocated() patch must make sure that the conversion falls back to a simple is_not_zero() when a backing file is used.

Thanks, I'll take this into account.

---
 qemu-img.c |   30 +++++++++++++++++++++++++++---
 1 files changed, 27 insertions(+), 3 deletions(-)
qemu-img.c +27 -3
index e825123..7665c2f 100644
@@ -496,14 +496,38 @@ static int img_commit(int argc, char **argv)
     return 0;
 }
 
+/*
+ * Checks whether the sector is not a zero sector.
+ *
+ * Attention! The len must be a multiple of 4 * sizeof(long) due to
+ * restriction of optimizations in this function.
+ */
 static int is_not_zero(const uint8_t *sector, int len)
 {
+    /*
+     * Use long as the biggest available internal data type that fits into the
+     * CPU register and unroll the loop to smooth out the effect of memory
+     * latency.
+     */
+
     int i;
-    len >>= 2;
-    for(i = 0;i < len; i++) {
-        if (((uint32_t *)sector)[i] != 0)
+    len /= sizeof(long);
+
+    long d0;
+    long d1;
+    long d2;
+    long d3;
+
+    for(i = 0; i < len; i += 4) {
+        d0 = ((const long*) sector)[i + 0];
+        d1 = ((const long*) sector)[i + 1];
+        d2 = ((const long*) sector)[i + 2];
+        d3 = ((const long*) sector)[i + 3];
+
+        if (d0 || d1 || d2 || d3)
             return 1;
     }
+
     return 0;
 }
 
Signature · 1 line
1.7.4.1
#6 lore raw

Am 18.05.2011 11:18, schrieb Dmitry Konishchev:

Quoted 43 lines · Stefan Hajnoczi
Quoted 3 lines

Yes, optimizing is_not_zero() is good. The only additional thing I suggest is adding a comment before the function to document the length constraint.

OK, fixed.

Quoted 2 lines · Kevin Wolf

A future bdrv_is_allocated() patch must make sure that the conversion falls back to a simple is_not_zero() when a backing file is used.

Thanks, I'll take this into account.

---
  qemu-img.c |   30 +++++++++++++++++++++++++++---
  1 files changed, 27 insertions(+), 3 deletions(-)
qemu-img.c +8 -0
index e825123..7665c2f 100644
@@ -496,14 +496,38 @@ static int img_commit(int argc, char **argv)
      return 0;
  }
 
+/*
+ * Checks whether the sector is not a zero sector.
+ *
+ * Attention! The len must be a multiple of 4 * sizeof(long) due to
+ * restriction of optimizations in this function.
+ */
  static int is_not_zero(const uint8_t *sector, int len)
  {
+    /*
+     * Use long as the biggest available internal data type that fits 

into the

qemu-img.c +4 -0
+     * CPU register and unroll the loop to smooth out the effect of memory
+     * latency.
+     */
+

qemu-img.c static int img_commit(int argc, char **argv) · line 519

Quoted 10 lines
qemu-img.c +6 -3
      int i;
-    len >>= 2;
-    for(i = 0;i < len; i++) {
-        if (((uint32_t *)sector)[i] != 0)
+    len /= sizeof(long);
+
+    long d0;
+    long d1;
+    long d2;
+    long d3;

Please move the declarations to the start of the function.

I also would use a single line like "long d0, d1, d2, d3;", but that's up to you.

qemu-img.c static int img_commit(int argc, char **argv) · line 525

Quoted 6 lines
qemu-img.c +6 -0
+
+    for(i = 0; i < len; i += 4) {
+        d0 = ((const long*) sector)[i + 0];
+        d1 = ((const long*) sector)[i + 1];
+        d2 = ((const long*) sector)[i + 2];
+        d3 = ((const long*) sector)[i + 3];

I would suggest to declare a const long* variable so that you don't have to cast each time you use, but that's probably a matter of taste.

qemu-img.c static int img_commit(int argc, char **argv) · line 528

Quoted 3 lines
qemu-img.c +2 -0
+
+        if (d0 || d1 || d2 || d3)
              return 1;

Coding style requires braces here.

Quoted 4 lines
      }
+
      return 0;

}

Please make sure that your patch isn't line-wrapped when you send it for inclusion. git send-email will do the right thing.

Kevin

#7 lore raw
Quoted 12 lines · Kevin Wolf

Please move the declarations to the start of the function.

I also would use a single line like "long d0, d1, d2, d3;", but that's up to you.

Review

Quoted 6 lines
(quoted diff) +6 -0
+
+    for(i = 0; i<  len; i += 4) {
+        d0 = ((const long*) sector)[i + 0];
+        d1 = ((const long*) sector)[i + 1];
+        d2 = ((const long*) sector)[i + 2];
+        d3 = ((const long*) sector)[i + 3];
Quoted 13 lines

I would suggest to declare a const long* variable so that you don't have to cast each time you use, but that's probably a matter of taste.

Review

Quoted 3 lines
(quoted diff) +2 -0
+
+        if (d0 || d1 || d2 || d3)
               return 1;

Coding style requires braces here.

Quoted 4 lines
       }
+
       return 0;

}

OK, fixed.

---
 qemu-img.c |   29 ++++++++++++++++++++++++++---
 1 files changed, 26 insertions(+), 3 deletions(-)
qemu-img.c +26 -3
index e825123..c849c6f 100644
@@ -496,14 +496,37 @@ static int img_commit(int argc, char **argv)
     return 0;
 }
 
+/*
+ * Checks whether the sector is not a zero sector.
+ *
+ * Attention! The len must be a multiple of 4 * sizeof(long) due to
+ * restriction of optimizations in this function.
+ */
 static int is_not_zero(const uint8_t *sector, int len)
 {
+    /*
+     * Use long as the biggest available internal data type that fits into the
+     * CPU register and unroll the loop to smooth out the effect of memory
+     * latency.
+     */
+
     int i;
-    len >>= 2;
-    for(i = 0;i < len; i++) {
-        if (((uint32_t *)sector)[i] != 0)
+    long d0, d1, d2, d3;
+    const long * const data = (const long *) sector;
+
+    len /= sizeof(long);
+
+    for(i = 0; i < len; i += 4) {
+        d0 = data[i + 0];
+        d1 = data[i + 1];
+        d2 = data[i + 2];
+        d3 = data[i + 3];
+
+        if (d0 || d1 || d2 || d3) {
             return 1;
+        }
     }
+
     return 0;
 }
 
Signature · 1 line
1.7.4.1
#8 lore raw

PATCH 1/1 is_not_zero() optimization in qemu-img

In reply to Kevin Wolf

Please sorry - I've sent the previous email via Thunderbird which promised that won't wrap the lines but it did. :( Sending this via git send-email.

---
 qemu-img.c |   29 ++++++++++++++++++++++++++---
 1 files changed, 26 insertions(+), 3 deletions(-)
qemu-img.c +26 -3
index e825123..c849c6f 100644
@@ -496,14 +496,37 @@ static int img_commit(int argc, char **argv)
     return 0;
 }
 
+/*
+ * Checks whether the sector is not a zero sector.
+ *
+ * Attention! The len must be a multiple of 4 * sizeof(long) due to
+ * restriction of optimizations in this function.
+ */
 static int is_not_zero(const uint8_t *sector, int len)
 {
+    /*
+     * Use long as the biggest available internal data type that fits into the
+     * CPU register and unroll the loop to smooth out the effect of memory
+     * latency.
+     */
+
     int i;
-    len >>= 2;
-    for(i = 0;i < len; i++) {
-        if (((uint32_t *)sector)[i] != 0)
+    long d0, d1, d2, d3;
+    const long * const data = (const long *) sector;
+
+    len /= sizeof(long);
+
+    for(i = 0; i < len; i += 4) {
+        d0 = data[i + 0];
+        d1 = data[i + 1];
+        d2 = data[i + 2];
+        d3 = data[i + 3];
+
+        if (d0 || d1 || d2 || d3) {
             return 1;
+        }
     }
+
     return 0;
 }
 
Signature · 1 line
1.7.4.1
Applied
#9 lore raw

Re: [Qemu-devel] [PATCH] is_not_zero() optimization in qemu-img

Am 18.05.2011 13:03, schrieb Dmitry Konishchev:

Quoted 4 lines

Please sorry - I've sent the previous email via Thunderbird which promised that won't wrap the lines but it did. :( Sending this via git send-email.

Thanks, applied to the block branch.

If you add a commit message that describes what the patch changes (and why) next time, the patch would be perfect. ;-) For now I took some part from your initial mail as the commit message.

Kevin

#10 lore raw
In reply to Dmitry Konishchev
Quoted 2 lines · Dmitry Konishchev

+ * Attention! The len must be a multiple of 4 * sizeof(long) due to
+ * restriction of optimizations in this function.

You could assert() this:
assert(argc % (4 * sizeof(long)) == 0);

-- PMM

#11 lore raw
Quoted 7 lines · Peter Maydell
Quoted 2 lines · Dmitry Konishchev

+ * Attention! The len must be a multiple of 4 * sizeof(long) due to
+ * restriction of optimizations in this function.

You could assert() this:
 assert(argc % (4 * sizeof(long)) == 0);

s/len/argc/, obviously!

-- PMM

#12 lore raw
In reply to Peter Maydell
Quoted 2 lines · Peter Maydell

You could assert() this:
 assert(argc % (4 * sizeof(long)) == 0);

Yeah, but actually I'm not really like the idea to include asserts in the little bottleneck functions if the configuration script doesn't include -DNDEBUG in the compiler cflags by default. Because in this case it's yet additional instructions on which CPU pipeline is going to stumble upon + it also decreases the chances for this function to be inlined by the compiler. But inlining can give us an additional boost + compiler will be able to understand that, in the place where the function is inlined, it is always called with len == 512 and optimize the code for this case by automatically unroll the loop and so on.

But in the bottom line I don't really mind to include the assert - just believe that it's not really worth it.

Signature · 2 lines
Dmitry Konishchev
mailto:[email protected]