Skip to contentSkip to conversation

use blk_pwrite_zeroes for each zero cluster

PATCH Superseded [email protected] 2 participants 7 messages Apr 6, 2017 – Apr 7, 2017 Block layer

v1 of 6 Superseded Latest is v6 →

lore
Share & tools
lore
#0 lore raw

cover use blk_pwrite_zeroes for each zero cluster

From: Lidong Chen <[email protected]>

BLOCK_SIZE is (1 << 20), qcow2 cluster size is 65536 by default, this maybe cause the qcow2 file size is bigger after migration. This patch check each cluster, use blk_pwrite_zeroes for each zero cluster.

Lidong Chen (2):
block: make bdrv_get_cluster_size public migration/block: use blk_pwrite_zeroes for each zero cluster

 block/io.c            |  2 +-
 include/block/block.h |  1 +
 migration/block.c     | 34 ++++++++++++++++++++++++++++++++--
 3 files changed, 34 insertions(+), 3 deletions(-)
Signature · 1 line
1.8.3.1
#1 lore raw

PATCH 1/2 block: make bdrv_get_cluster_size public

From: Lidong Chen <[email protected]>

The bdrv_get_cluster_size function is need by block migration to optimize zero cluster.

---
 block/io.c            | 2 +-
 include/block/block.h | 1 +
 2 files changed, 2 insertions(+), 1 deletion(-)
block/io.c +1 -1
index 2709a70..1c31c2b 100644
@@ -459,7 +459,7 @@ void bdrv_round_to_clusters(BlockDriverState *bs,
     }
 }
 
-static int bdrv_get_cluster_size(BlockDriverState *bs)
+int bdrv_get_cluster_size(BlockDriverState *bs)
 {
     BlockDriverInfo bdi;
     int ret;
include/block/block.h +1 -0
index 5149260..a0d78fc 100644
@@ -473,6 +473,7 @@ void bdrv_round_to_clusters(BlockDriverState *bs,
                             int64_t offset, unsigned int bytes,
                             int64_t *cluster_offset,
                             unsigned int *cluster_bytes);
+int bdrv_get_cluster_size(BlockDriverState *bs);
 
 const char *bdrv_get_encrypted_filename(BlockDriverState *bs);
 void bdrv_get_backing_filename(BlockDriverState *bs,
Signature · 1 line
1.8.3.1
#2 lore raw

PATCH 2/2 migration/block: use blk_pwrite_zeroes for each zero cluster

In reply to [email protected]

From: Lidong Chen <[email protected]>

BLOCK_SIZE is (1 << 20), qcow2 cluster size is 65536 by default, this maybe cause the qcow2 file size is bigger after migration. This patch check each cluster, use blk_pwrite_zeroes for each zero cluster.

---
 migration/block.c | 34 ++++++++++++++++++++++++++++++++--
 1 file changed, 32 insertions(+), 2 deletions(-)
migration/block.c +32 -2
index 7734ff7..1fce9b9 100644
@@ -885,6 +885,10 @@ static int block_load(QEMUFile *f, void *opaque, int version_id)
     int64_t total_sectors = 0;
     int nr_sectors;
     int ret;
+    int i;
+    int cluster_size;
+    int64_t addr_offset;
+    uint8_t *buf_offset;
 
     do {
         addr = qemu_get_be64(f);
@@ -934,8 +938,34 @@ static int block_load(QEMUFile *f, void *opaque, int version_id)
             } else {
                 buf = g_malloc(BLOCK_SIZE);
                 qemu_get_buffer(f, buf, BLOCK_SIZE);
-                ret = blk_pwrite(blk, addr * BDRV_SECTOR_SIZE, buf,
-                                 nr_sectors * BDRV_SECTOR_SIZE, 0);
+
+                cluster_size = bdrv_get_cluster_size(blk_bs(blk));
+
+                if (cluster_size > 0) {
+                    for (i = 0; i < BLOCK_SIZE / cluster_size; i++) {
+                        addr_offset = addr * BDRV_SECTOR_SIZE
+                                        + i * cluster_size;
+                        buf_offset = buf + i * cluster_size;
+
+                        if (buffer_is_zero(buf_offset, cluster_size)) {
+                            ret = blk_pwrite_zeroes(blk, addr_offset,
+                                                    cluster_size,
+                                                    BDRV_REQ_MAY_UNMAP);
+                        } else {
+                            ret = blk_pwrite(blk, addr_offset,
+                                             buf_offset, cluster_size, 0);
+                        }
+
+                        if (ret < 0) {
+                            g_free(buf);
+                            return ret;
+                        }
+                    }
+                } else {
+                    ret = blk_pwrite(blk, addr * BDRV_SECTOR_SIZE, buf,
+                                     nr_sectors * BDRV_SECTOR_SIZE, 0);
+                }
+
                 g_free(buf);
             }
 
Signature · 1 line
1.8.3.1
#3 lore raw

Re: [Qemu-devel] [PATCH 2/2] migration/block: use blk_pwrite_zeroes for each zero cluster

Quoted 28 lines · On Thu, 04/06 21:15, [email protected] wrote:

From: Lidong Chen <[email protected]>

BLOCK_SIZE is (1 << 20), qcow2 cluster size is 65536 by default, this maybe cause the qcow2 file size is bigger after migration. This patch check each cluster, use blk_pwrite_zeroes for each zero cluster.

---
 migration/block.c | 34 ++++++++++++++++++++++++++++++++--
 1 file changed, 32 insertions(+), 2 deletions(-)
migration/block.c +4 -0
index 7734ff7..1fce9b9 100644
@@ -885,6 +885,10 @@ static int block_load(QEMUFile *f, void *opaque, int version_id)
     int64_t total_sectors = 0;
     int nr_sectors;
     int ret;
+    int i;
+    int cluster_size;
+    int64_t addr_offset;
+    uint8_t *buf_offset;
 
     do {
         addr = qemu_get_be64(f);

migration/block.c static int block_load(QEMUFile *f, void *opaque, int version_id) · line 945

Quoted 10 lines
migration/block.c +5 -2
@@ -934,8 +938,34 @@ static int block_load(QEMUFile *f, void *opaque, int version_id)
             } else {
                 buf = g_malloc(BLOCK_SIZE);
                 qemu_get_buffer(f, buf, BLOCK_SIZE);
-                ret = blk_pwrite(blk, addr * BDRV_SECTOR_SIZE, buf,
-                                 nr_sectors * BDRV_SECTOR_SIZE, 0);
+
+                cluster_size = bdrv_get_cluster_size(blk_bs(blk));
+
+                if (cluster_size > 0) {
+                    for (i = 0; i < BLOCK_SIZE / cluster_size; i++) {

Should we check that cluster_size < BLOCK_SIZE and (BLOCK_SIZE % cluster_size == 0)?

Fam

Quoted 29 lines
migration/block.c +23 -0
+                        addr_offset = addr * BDRV_SECTOR_SIZE
+                                        + i * cluster_size;
+                        buf_offset = buf + i * cluster_size;
+
+                        if (buffer_is_zero(buf_offset, cluster_size)) {
+                            ret = blk_pwrite_zeroes(blk, addr_offset,
+                                                    cluster_size,
+                                                    BDRV_REQ_MAY_UNMAP);
+                        } else {
+                            ret = blk_pwrite(blk, addr_offset,
+                                             buf_offset, cluster_size, 0);
+                        }
+
+                        if (ret < 0) {
+                            g_free(buf);
+                            return ret;
+                        }
+                    }
+                } else {
+                    ret = blk_pwrite(blk, addr * BDRV_SECTOR_SIZE, buf,
+                                     nr_sectors * BDRV_SECTOR_SIZE, 0);
+                }
+
                 g_free(buf);
             }
 
Signature · 1 line
1.8.3.1
#4 lore raw

Re: [Qemu-devel] [PATCH 2/2] migration/block: use blk_pwrite_zeroes for each zero cluster

Quoted 26 lines · Fam Zheng
Quoted 25 lines · On Thu, 04/06 21:15, [email protected] wrote:

From: Lidong Chen <[email protected]>

BLOCK_SIZE is (1 << 20), qcow2 cluster size is 65536 by default, this maybe cause the qcow2 file size is bigger after migration. This patch check each cluster, use blk_pwrite_zeroes for each zero cluster.

---
 migration/block.c | 34 ++++++++++++++++++++++++++++++++--
 1 file changed, 32 insertions(+), 2 deletions(-)
migration/block.c +4 -0
index 7734ff7..1fce9b9 100644
@@ -885,6 +885,10 @@ static int block_load(QEMUFile *f, void *opaque, int version_id)
     int64_t total_sectors = 0;
     int nr_sectors;
     int ret;
+    int i;
+    int cluster_size;
+    int64_t addr_offset;
+    uint8_t *buf_offset;
 
Quoted 16 lines

migration/block.c static int block_load(QEMUFile *f, void *opaque, int version_id) · line 945

Quoted 13 lines
migration/block.c +5 -2
     do {
         addr = qemu_get_be64(f);
@@ -934,8 +938,34 @@ static int block_load(QEMUFile *f, void *opaque, int version_id)
             } else {
                 buf = g_malloc(BLOCK_SIZE);
                 qemu_get_buffer(f, buf, BLOCK_SIZE);
-                ret = blk_pwrite(blk, addr * BDRV_SECTOR_SIZE, buf,
-                                 nr_sectors * BDRV_SECTOR_SIZE, 0);
+
+                cluster_size = bdrv_get_cluster_size(blk_bs(blk));
+
+                if (cluster_size > 0) {
+                    for (i = 0; i < BLOCK_SIZE / cluster_size; i++) {

Should we check that cluster_size < BLOCK_SIZE and (BLOCK_SIZE % cluster_size == 0)?

I think this is necessary.
Thanks.

Quoted 32 lines

Fam

Quoted 29 lines
migration/block.c +23 -1
+                        addr_offset = addr * BDRV_SECTOR_SIZE
+                                        + i * cluster_size;
+                        buf_offset = buf + i * cluster_size;
+
+                        if (buffer_is_zero(buf_offset, cluster_size)) {
+                            ret = blk_pwrite_zeroes(blk, addr_offset,
+                                                    cluster_size,
+                                                    BDRV_REQ_MAY_UNMAP);
+                        } else {
+                            ret = blk_pwrite(blk, addr_offset,
+                                             buf_offset, cluster_size, 0);
+                        }
+
+                        if (ret < 0) {
+                            g_free(buf);
+                            return ret;
+                        }
+                    }
+                } else {
+                    ret = blk_pwrite(blk, addr * BDRV_SECTOR_SIZE, buf,
+                                     nr_sectors * BDRV_SECTOR_SIZE, 0);
+                }
+
                 g_free(buf);
             }
 
--

1.8.3.1

#5 lore raw
In reply to [email protected]

the test result for this patch:

the migration command :
virsh migrate --live 165cf436-312f-47e7-90f2-f8aa63f34893 --copy-storage-all qemu+ssh://10.59.163.38/system

the qemu-img info on source host:
qemu-img info /instanceimage/165cf436-312f-47e7-90f2-f8aa63f34893/165cf436-312f-47e7-90f2-f8aa63f34893_vda.qcow2
image: /instanceimage/165cf436-312f-47e7-90f2-f8aa63f34893/165cf436-312f-47e7-90f2-f8aa63f34893_vda.qcow2 file format: qcow2
virtual size: 1.0T (1095216660480 bytes)
disk size: 1.5G (1638989824 bytes)
cluster_size: 65536
backing file: /baseimage/img2016042213665396/img2016042213665396.qcow2

the qemu-img info on dest host(before apply patch):
qemu-img info /instanceimage/165cf436-312f-47e7-90f2-f8aa63f34893/165cf436-312f-47e7-90f2-f8aa63f34893_vda.qcow2
image: /instanceimage/165cf436-312f-47e7-90f2-f8aa63f34893/165cf436-312f-47e7-90f2-f8aa63f34893_vda.qcow2 file format: qcow2
virtual size: 40G (42949672960 bytes)
disk size: 4.1G (4423286784 bytes)
cluster_size: 65536
backing file: /baseimage/img2016042213665396/img2016042213665396.qcow2

the qemu-img info on dest host(after apply patch):
qemu-img info /instanceimage/165cf436-312f-47e7-90f2-f8aa63f34893/165cf436-312f-47e7-90f2-f8aa63f34893_vda.qcow2
image: /instanceimage/165cf436-312f-47e7-90f2-f8aa63f34893/165cf436-312f-47e7-90f2-f8aa63f34893_vda.qcow2 file format: qcow2
virtual size: 40G (42949672960 bytes)
disk size: 2.3G (2496200704 bytes)
cluster_size: 65536
backing file: /baseimage/img2016042213665396/img2016042213665396.qcow2

the disk size reduce from 4.1G to 2.3G.

Quoted 19 lines · On Thu, Apr 6, 2017 at 9:15 PM, <[email protected]> wrote:

From: Lidong Chen <[email protected]>

BLOCK_SIZE is (1 << 20), qcow2 cluster size is 65536 by default, this maybe cause the qcow2 file size is bigger after migration. This patch check each cluster, use blk_pwrite_zeroes for each zero cluster.

Lidong Chen (2):
block: make bdrv_get_cluster_size public migration/block: use blk_pwrite_zeroes for each zero cluster

 block/io.c            |  2 +-
 include/block/block.h |  1 +
 migration/block.c     | 34 ++++++++++++++++++++++++++++++++--
 3 files changed, 34 insertions(+), 3 deletions(-)

--
1.8.3.1

#6 lore raw
Quoted 33 lines · On Fri, Apr 7, 2017 at 1:22 PM, 858585 jemmy <[email protected]> wrote:

the test result for this patch:

the migration command :
virsh migrate --live 165cf436-312f-47e7-90f2-f8aa63f34893 --copy-storage-all qemu+ssh://10.59.163.38/system

the qemu-img info on source host:
qemu-img info /instanceimage/165cf436-312f-47e7-90f2-f8aa63f34893/165cf436-312f-47e7-90f2-f8aa63f34893_vda.qcow2
image: /instanceimage/165cf436-312f-47e7-90f2-f8aa63f34893/165cf436-312f-47e7-90f2-f8aa63f34893_vda.qcow2 file format: qcow2
virtual size: 1.0T (1095216660480 bytes)
disk size: 1.5G (1638989824 bytes)
cluster_size: 65536
backing file: /baseimage/img2016042213665396/img2016042213665396.qcow2

the qemu-img info on dest host(before apply patch):
qemu-img info /instanceimage/165cf436-312f-47e7-90f2-f8aa63f34893/165cf436-312f-47e7-90f2-f8aa63f34893_vda.qcow2
image: /instanceimage/165cf436-312f-47e7-90f2-f8aa63f34893/165cf436-312f-47e7-90f2-f8aa63f34893_vda.qcow2 file format: qcow2
virtual size: 40G (42949672960 bytes)
disk size: 4.1G (4423286784 bytes)
cluster_size: 65536
backing file: /baseimage/img2016042213665396/img2016042213665396.qcow2

the qemu-img info on dest host(after apply patch):
qemu-img info /instanceimage/165cf436-312f-47e7-90f2-f8aa63f34893/165cf436-312f-47e7-90f2-f8aa63f34893_vda.qcow2
image: /instanceimage/165cf436-312f-47e7-90f2-f8aa63f34893/165cf436-312f-47e7-90f2-f8aa63f34893_vda.qcow2 file format: qcow2
virtual size: 40G (42949672960 bytes)
disk size: 2.3G (2496200704 bytes)
cluster_size: 65536
backing file: /baseimage/img2016042213665396/img2016042213665396.qcow2

Quoted 2 lines

the disk size reduce from 4.1G to 2.3G.

I find a bug for my patch.
unfortunately, when use raw format , bdrv_get_cluster_size return 1. the raw format cluster size is 1.
and will reduce migration speed.

bdrv_get_cluster_size return bs->bl.request_alignment when cluster_size is zero. and bs->bl.request_alignment is 1 when use raw format.

static int bdrv_get_cluster_size(BlockDriverState *bs)

{
    BlockDriverInfo bdi;
    int ret;

    ret = bdrv_get_info(bs, &bdi);
    if (ret < 0 || bdi.cluster_size == 0) {
        return bs->bl.request_alignment;
    } else {
        return bdi.cluster_size;
    }
}

so i will change to use bdrv_get_info function to get cluster_size. and bdrv_get_cluster_size is not need to be public.

Quoted 21 lines · On Thu, Apr 6, 2017 at 9:15 PM, <[email protected]> wrote:
Quoted 19 lines

From: Lidong Chen <[email protected]>

BLOCK_SIZE is (1 << 20), qcow2 cluster size is 65536 by default, this maybe cause the qcow2 file size is bigger after migration. This patch check each cluster, use blk_pwrite_zeroes for each zero cluster.

Lidong Chen (2):
block: make bdrv_get_cluster_size public migration/block: use blk_pwrite_zeroes for each zero cluster

 block/io.c            |  2 +-
 include/block/block.h |  1 +
 migration/block.c     | 34 ++++++++++++++++++++++++++++++++--
 3 files changed, 34 insertions(+), 3 deletions(-)

--
1.8.3.1