Skip to contentSkip to conversation

vmdk: Optimize cluster allocation

PATCH Superseded Fam Zheng 3 participants 7 messages May 6, 2014 – May 7, 2014 Block layer

v2 of 6 Superseded Latest is v6 →

lore
Share & tools
lore

Fam Zheng

author
#0 lore raw

PATCH v2 1/1 vmdk: Optimize cluster allocation

On mounted NFS filesystem, ftruncate is much much slower than doing a zero write. Changing this significantly speeds up cluster allocation.

Comparing by converting a cirros image (296M) to VMDK on an NFS mount point, over 1Gbe LAN:

    $ time qemu-img convert cirros-0.3.1.img /mnt/a.raw -O vmdk

Before:

    real    0m26.464s
    user    0m0.133s
    sys     0m0.527s

After:

    real    0m2.120s
    user    0m0.080s
    sys     0m0.197s

---
V2: Fix cluster_offset check. (Kevin)

---
 block/vmdk.c | 19 ++++++++++++++-----
 1 file changed, 14 insertions(+), 5 deletions(-)
block/vmdk.c +14 -5
index 06a1f9f..98d2d56 100644
@@ -1037,6 +1037,7 @@ static int get_cluster_offset(BlockDriverState *bs,
     int min_index, i, j;
     uint32_t min_count, *l2_table;
     bool zeroed = false;
+    int64_t ret;
 
     if (m_data) {
         m_data->valid = 0;
@@ -1110,12 +1111,20 @@ static int get_cluster_offset(BlockDriverState *bs,
         }
 
         /* Avoid the L2 tables update for the images that have snapshots. */
-        *cluster_offset = bdrv_getlength(extent->file);
+        ret = bdrv_getlength(extent->file);
+        if (ret < 0 ||
+            ret & ((extent->cluster_sectors << BDRV_SECTOR_BITS) - 1)) {
+            return VMDK_ERROR;
+        }
+        *cluster_offset = ret;
         if (!extent->compressed) {
-            bdrv_truncate(
-                extent->file,
-                *cluster_offset + (extent->cluster_sectors << 9)
-            );
+            ret = bdrv_write_zeroes(extent->file,
+                                    *cluster_offset >> BDRV_SECTOR_BITS,
+                                    extent->cluster_sectors,
+                                    0);
+            if (ret) {
+                return VMDK_ERROR;
+            }
         }
 
         *cluster_offset >>= 9;
Signature · 1 line
1.9.2
author
#1 lore raw
Quoted 50 lines · Fam Zheng

On mounted NFS filesystem, ftruncate is much much slower than doing a zero write. Changing this significantly speeds up cluster allocation.

Comparing by converting a cirros image (296M) to VMDK on an NFS mount point, over 1Gbe LAN:

    $ time qemu-img convert cirros-0.3.1.img /mnt/a.raw -O vmdk

Before:

    real    0m26.464s
    user    0m0.133s
    sys     0m0.527s

After:

    real    0m2.120s
    user    0m0.080s
    sys     0m0.197s

---
V2: Fix cluster_offset check. (Kevin)

---
 block/vmdk.c | 19 ++++++++++++++-----
 1 file changed, 14 insertions(+), 5 deletions(-)
block/vmdk.c +6 -1
index 06a1f9f..98d2d56 100644
@@ -1037,6 +1037,7 @@ static int get_cluster_offset(BlockDriverState *bs,
     int min_index, i, j;
     uint32_t min_count, *l2_table;
     bool zeroed = false;
+    int64_t ret;
 
     if (m_data) {
         m_data->valid = 0;
@@ -1110,12 +1111,20 @@ static int get_cluster_offset(BlockDriverState *bs,
         }
 
         /* Avoid the L2 tables update for the images that have snapshots. */
-        *cluster_offset = bdrv_getlength(extent->file);
+        ret = bdrv_getlength(extent->file);
+        if (ret < 0 ||
+            ret & ((extent->cluster_sectors << BDRV_SECTOR_BITS) - 1)) {
+            return VMDK_ERROR;
+        }

block/vmdk.c static int get_cluster_offset(BlockDriverState *bs, · line 1124

Quoted 10 lines
block/vmdk.c +5 -4
+        *cluster_offset = ret;
         if (!extent->compressed) {
-            bdrv_truncate(
-                extent->file,
-                *cluster_offset + (extent->cluster_sectors << 9)
-            );
+            ret = bdrv_write_zeroes(extent->file,
+                                    *cluster_offset >> BDRV_SECTOR_BITS,
+                                    extent->cluster_sectors,
+                                    0);

Hi Stefan,

By considering a bdrv_write_zeroes as a pre-write, it in general doubles the write for the whole image, so it's not a good solution.

A better way would be removing the bdrv_truncate and require the caller to do full cluster write (with a bounce buffer if necessary).

So let's drop this patch.

Thanks,
Fam

#2 lore raw
Quoted 24 lines · Fam Zheng
Quoted 10 lines · Fam Zheng
block/vmdk.c +5 -1
@@ -1110,12 +1111,20 @@ static int get_cluster_offset(BlockDriverState *bs,
         }
 
         /* Avoid the L2 tables update for the images that have snapshots. */
-        *cluster_offset = bdrv_getlength(extent->file);
+        ret = bdrv_getlength(extent->file);
+        if (ret < 0 ||
+            ret & ((extent->cluster_sectors << BDRV_SECTOR_BITS) - 1)) {
+            return VMDK_ERROR;
+        }

block/vmdk.c static int get_cluster_offset(BlockDriverState *bs, · line 1124

Quoted 10 lines
block/vmdk.c +5 -4
+        *cluster_offset = ret;
         if (!extent->compressed) {
-            bdrv_truncate(
-                extent->file,
-                *cluster_offset + (extent->cluster_sectors << 9)
-            );
+            ret = bdrv_write_zeroes(extent->file,
+                                    *cluster_offset >> BDRV_SECTOR_BITS,
+                                    extent->cluster_sectors,
+                                    0);

Hi Stefan,

Quoted 7 lines

By considering a bdrv_write_zeroes as a pre-write, it in general doubles the write for the whole image, so it's not a good solution.

A better way would be removing the bdrv_truncate and require the caller to do full cluster write (with a bounce buffer if necessary).

So let's drop this patch.

Okay, thanks for investigating this.

Stefan

#3 lore raw
In reply to Fam Zheng

Am 07.05.2014 um 03:45 hat Fam Zheng geschrieben:

Quoted 64 lines · Fam Zheng
Quoted 50 lines

On mounted NFS filesystem, ftruncate is much much slower than doing a zero write. Changing this significantly speeds up cluster allocation.

Comparing by converting a cirros image (296M) to VMDK on an NFS mount point, over 1Gbe LAN:

    $ time qemu-img convert cirros-0.3.1.img /mnt/a.raw -O vmdk

Before:

    real    0m26.464s
    user    0m0.133s
    sys     0m0.527s

After:

    real    0m2.120s
    user    0m0.080s
    sys     0m0.197s

---
V2: Fix cluster_offset check. (Kevin)

---
 block/vmdk.c | 19 ++++++++++++++-----
 1 file changed, 14 insertions(+), 5 deletions(-)
block/vmdk.c +6 -1
index 06a1f9f..98d2d56 100644
@@ -1037,6 +1037,7 @@ static int get_cluster_offset(BlockDriverState *bs,
     int min_index, i, j;
     uint32_t min_count, *l2_table;
     bool zeroed = false;
+    int64_t ret;
 
     if (m_data) {
         m_data->valid = 0;
@@ -1110,12 +1111,20 @@ static int get_cluster_offset(BlockDriverState *bs,
         }
 
         /* Avoid the L2 tables update for the images that have snapshots. */
-        *cluster_offset = bdrv_getlength(extent->file);
+        ret = bdrv_getlength(extent->file);
+        if (ret < 0 ||
+            ret & ((extent->cluster_sectors << BDRV_SECTOR_BITS) - 1)) {
+            return VMDK_ERROR;
+        }

block/vmdk.c static int get_cluster_offset(BlockDriverState *bs, · line 1124

Quoted 10 lines
block/vmdk.c +5 -4
+        *cluster_offset = ret;
         if (!extent->compressed) {
-            bdrv_truncate(
-                extent->file,
-                *cluster_offset + (extent->cluster_sectors << 9)
-            );
+            ret = bdrv_write_zeroes(extent->file,
+                                    *cluster_offset >> BDRV_SECTOR_BITS,
+                                    extent->cluster_sectors,
+                                    0);

Hi Stefan,

Quoted 5 lines

By considering a bdrv_write_zeroes as a pre-write, it in general doubles the write for the whole image, so it's not a good solution.

A better way would be removing the bdrv_truncate and require the caller to do full cluster write (with a bounce buffer if necessary).

Doesn't get_whole_cluster() already ensure that you already write a full cluster to the image file?

However, it might be better to not use bdrv_getlength() each time you need a new cluster, but instead use a field in VmdkExtent to keep the next free cluster offset (which is rounded up in vmdk_open). This will ensure that we don't overlap the next cluster allocation in case get_whole_cluster() fails halfway through.

(In fact, the L2 table should only be updated after get_whole_cluster() has succeeded, but we can do both to be on the safe side...)

Kevin

author
#4 lore raw
Quoted 65 lines · Kevin Wolf

Am 07.05.2014 um 03:45 hat Fam Zheng geschrieben:

Quoted 64 lines · Fam Zheng
Quoted 50 lines

On mounted NFS filesystem, ftruncate is much much slower than doing a zero write. Changing this significantly speeds up cluster allocation.

Comparing by converting a cirros image (296M) to VMDK on an NFS mount point, over 1Gbe LAN:

    $ time qemu-img convert cirros-0.3.1.img /mnt/a.raw -O vmdk

Before:

    real    0m26.464s
    user    0m0.133s
    sys     0m0.527s

After:

    real    0m2.120s
    user    0m0.080s
    sys     0m0.197s

---
V2: Fix cluster_offset check. (Kevin)

---
 block/vmdk.c | 19 ++++++++++++++-----
 1 file changed, 14 insertions(+), 5 deletions(-)
block/vmdk.c +6 -1
index 06a1f9f..98d2d56 100644
@@ -1037,6 +1037,7 @@ static int get_cluster_offset(BlockDriverState *bs,
     int min_index, i, j;
     uint32_t min_count, *l2_table;
     bool zeroed = false;
+    int64_t ret;
 
     if (m_data) {
         m_data->valid = 0;
@@ -1110,12 +1111,20 @@ static int get_cluster_offset(BlockDriverState *bs,
         }
 
         /* Avoid the L2 tables update for the images that have snapshots. */
-        *cluster_offset = bdrv_getlength(extent->file);
+        ret = bdrv_getlength(extent->file);
+        if (ret < 0 ||
+            ret & ((extent->cluster_sectors << BDRV_SECTOR_BITS) - 1)) {
+            return VMDK_ERROR;
+        }

block/vmdk.c static int get_cluster_offset(BlockDriverState *bs, · line 1124

Quoted 10 lines
block/vmdk.c +5 -4
+        *cluster_offset = ret;
         if (!extent->compressed) {
-            bdrv_truncate(
-                extent->file,
-                *cluster_offset + (extent->cluster_sectors << 9)
-            );
+            ret = bdrv_write_zeroes(extent->file,
+                                    *cluster_offset >> BDRV_SECTOR_BITS,
+                                    extent->cluster_sectors,
+                                    0);

Hi Stefan,

Quoted 8 lines
Quoted 5 lines

By considering a bdrv_write_zeroes as a pre-write, it in general doubles the write for the whole image, so it's not a good solution.

A better way would be removing the bdrv_truncate and require the caller to do full cluster write (with a bounce buffer if necessary).

Doesn't get_whole_cluster() already ensure that you already write a full cluster to the image file?

That one is actually called get_backing_cluster(), if you look at the code it has. :)

Quoted 4 lines

However, it might be better to not use bdrv_getlength() each time you need a new cluster, but instead use a field in VmdkExtent to keep the next free cluster offset (which is rounded up in vmdk_open).

Yes, indeed. We should do that.

Thanks,
Fam

#5 lore raw

Am 07.05.2014 um 10:57 hat Fam Zheng geschrieben:

Quoted 69 lines · Kevin Wolf
Quoted 68 lines

Am 07.05.2014 um 03:45 hat Fam Zheng geschrieben:

Quoted 67 lines · Fam Zheng
Quoted 50 lines

On mounted NFS filesystem, ftruncate is much much slower than doing a zero write. Changing this significantly speeds up cluster allocation.

Comparing by converting a cirros image (296M) to VMDK on an NFS mount point, over 1Gbe LAN:

    $ time qemu-img convert cirros-0.3.1.img /mnt/a.raw -O vmdk

Before:

    real    0m26.464s
    user    0m0.133s
    sys     0m0.527s

After:

    real    0m2.120s
    user    0m0.080s
    sys     0m0.197s

---
V2: Fix cluster_offset check. (Kevin)

---
 block/vmdk.c | 19 ++++++++++++++-----
 1 file changed, 14 insertions(+), 5 deletions(-)
block/vmdk.c +6 -1
index 06a1f9f..98d2d56 100644
@@ -1037,6 +1037,7 @@ static int get_cluster_offset(BlockDriverState *bs,
     int min_index, i, j;
     uint32_t min_count, *l2_table;
     bool zeroed = false;
+    int64_t ret;
 
     if (m_data) {
         m_data->valid = 0;
@@ -1110,12 +1111,20 @@ static int get_cluster_offset(BlockDriverState *bs,
         }
 
         /* Avoid the L2 tables update for the images that have snapshots. */
-        *cluster_offset = bdrv_getlength(extent->file);
+        ret = bdrv_getlength(extent->file);
+        if (ret < 0 ||
+            ret & ((extent->cluster_sectors << BDRV_SECTOR_BITS) - 1)) {
+            return VMDK_ERROR;
+        }

block/vmdk.c static int get_cluster_offset(BlockDriverState *bs, · line 1124

Quoted 10 lines
block/vmdk.c +5 -4
+        *cluster_offset = ret;
         if (!extent->compressed) {
-            bdrv_truncate(
-                extent->file,
-                *cluster_offset + (extent->cluster_sectors << 9)
-            );
+            ret = bdrv_write_zeroes(extent->file,
+                                    *cluster_offset >> BDRV_SECTOR_BITS,
+                                    extent->cluster_sectors,
+                                    0);

Hi Stefan,

By considering a bdrv_write_zeroes as a pre-write, it in general doubles the write for the whole image, so it's not a good solution.

Quoted 8 lines
Quoted 5 lines
Quoted 2 lines

A better way would be removing the bdrv_truncate and require the caller to do full cluster write (with a bounce buffer if necessary).

Doesn't get_whole_cluster() already ensure that you already write a full cluster to the image file?

That one is actually called get_backing_cluster(), if you look at the code it has. :)

Right, it doesn't do anything without a backing file. This is different from qcow2, whose mechanism I assumed without reading the code in detail. :-)

I think it would make sense to rewrite get_whole_cluster() to write the cluster for both image with a backing file and standalone images; just that without a backing file it would use memset() to fill the buffer instead of bdrv_read().

Not sure how easy it would be, but it might be an opportunity to also change it to write only those parts of the cluster that aren't written to anyway by the cluster.

Kevin

author
#6 lore raw
Quoted 83 lines · Kevin Wolf

Am 07.05.2014 um 10:57 hat Fam Zheng geschrieben:

Quoted 69 lines · Kevin Wolf
Quoted 68 lines

Am 07.05.2014 um 03:45 hat Fam Zheng geschrieben:

Quoted 67 lines · Fam Zheng
Quoted 50 lines

On mounted NFS filesystem, ftruncate is much much slower than doing a zero write. Changing this significantly speeds up cluster allocation.

Comparing by converting a cirros image (296M) to VMDK on an NFS mount point, over 1Gbe LAN:

    $ time qemu-img convert cirros-0.3.1.img /mnt/a.raw -O vmdk

Before:

    real    0m26.464s
    user    0m0.133s
    sys     0m0.527s

After:

    real    0m2.120s
    user    0m0.080s
    sys     0m0.197s

---
V2: Fix cluster_offset check. (Kevin)

---
 block/vmdk.c | 19 ++++++++++++++-----
 1 file changed, 14 insertions(+), 5 deletions(-)
block/vmdk.c +6 -1
index 06a1f9f..98d2d56 100644
@@ -1037,6 +1037,7 @@ static int get_cluster_offset(BlockDriverState *bs,
     int min_index, i, j;
     uint32_t min_count, *l2_table;
     bool zeroed = false;
+    int64_t ret;
 
     if (m_data) {
         m_data->valid = 0;
@@ -1110,12 +1111,20 @@ static int get_cluster_offset(BlockDriverState *bs,
         }
 
         /* Avoid the L2 tables update for the images that have snapshots. */
-        *cluster_offset = bdrv_getlength(extent->file);
+        ret = bdrv_getlength(extent->file);
+        if (ret < 0 ||
+            ret & ((extent->cluster_sectors << BDRV_SECTOR_BITS) - 1)) {
+            return VMDK_ERROR;
+        }

block/vmdk.c static int get_cluster_offset(BlockDriverState *bs, · line 1124

Quoted 10 lines
block/vmdk.c +5 -4
+        *cluster_offset = ret;
         if (!extent->compressed) {
-            bdrv_truncate(
-                extent->file,
-                *cluster_offset + (extent->cluster_sectors << 9)
-            );
+            ret = bdrv_write_zeroes(extent->file,
+                                    *cluster_offset >> BDRV_SECTOR_BITS,
+                                    extent->cluster_sectors,
+                                    0);

Hi Stefan,

By considering a bdrv_write_zeroes as a pre-write, it in general doubles the write for the whole image, so it's not a good solution.

Quoted 8 lines
Quoted 5 lines
Quoted 2 lines

A better way would be removing the bdrv_truncate and require the caller to do full cluster write (with a bounce buffer if necessary).

Doesn't get_whole_cluster() already ensure that you already write a full cluster to the image file?

That one is actually called get_backing_cluster(), if you look at the code it has. :)

Right, it doesn't do anything without a backing file. This is different from qcow2, whose mechanism I assumed without reading the code in detail. :-)

Quoted 9 lines

I think it would make sense to rewrite get_whole_cluster() to write the cluster for both image with a backing file and standalone images; just that without a backing file it would use memset() to fill the buffer instead of bdrv_read().

Not sure how easy it would be, but it might be an opportunity to also change it to write only those parts of the cluster that aren't written to anyway by the cluster.

I think that shouldn't be hard . I'll make the change and send another patch later.

Thanks,
Fam