Skip to contentSkip to conversation

migration/block: use blk_pwrite_zeroes for each zero cluster

PATCH Superseded [email protected] 3 participants 6 messages Apr 9, 2017 – Apr 11, 2017

v3 of 6 Superseded Latest is v6 →

lore
Share & tools
lore
#0 lore raw

PATCH v3 1/1 migration/block: 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.

---
 migration/block.c | 38 ++++++++++++++++++++++++++++++++++++--
 1 file changed, 36 insertions(+), 2 deletions(-)
migration/block.c +36 -2
index 7734ff7..fe613db 100644
@@ -885,6 +885,8 @@ static int block_load(QEMUFile *f, void *opaque, int version_id)
     int64_t total_sectors = 0;
     int nr_sectors;
     int ret;
+    BlockDriverInfo bdi;
+    int cluster_size;
 
     do {
         addr = qemu_get_be64(f);
@@ -934,8 +936,40 @@ 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);
+
+                ret = bdrv_get_info(blk_bs(blk), &bdi);
+                cluster_size = bdi.cluster_size;
+
+                if (ret == 0 && cluster_size > 0 &&
+                    cluster_size <= BLOCK_SIZE &&
+                    BLOCK_SIZE % cluster_size == 0) {
+                    int i;
+                    int64_t cur_addr;
+                    uint8_t *cur_buf;
+
+                    for (i = 0; i < BLOCK_SIZE / cluster_size; i++) {
+                        cur_addr = addr * BDRV_SECTOR_SIZE
+                                        + i * cluster_size;
+                        cur_buf = buf + i * cluster_size;
+
+                        if (buffer_is_zero(cur_buf, cluster_size)) {
+                            ret = blk_pwrite_zeroes(blk, cur_addr,
+                                                    cluster_size,
+                                                    BDRV_REQ_MAY_UNMAP);
+                        } else {
+                             ret = blk_pwrite(blk, cur_addr, cur_buf,
+                                              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
Reviewed-by on v3 1/1
#1 lore raw
Quoted 50 lines · On Sun, 04/09 20:37, [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 | 38 ++++++++++++++++++++++++++++++++++++--
 1 file changed, 36 insertions(+), 2 deletions(-)
migration/block.c +21 -2
index 7734ff7..fe613db 100644
@@ -885,6 +885,8 @@ static int block_load(QEMUFile *f, void *opaque, int version_id)
     int64_t total_sectors = 0;
     int nr_sectors;
     int ret;
+    BlockDriverInfo bdi;
+    int cluster_size;
 
     do {
         addr = qemu_get_be64(f);
@@ -934,8 +936,40 @@ 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);
+
+                ret = bdrv_get_info(blk_bs(blk), &bdi);
+                cluster_size = bdi.cluster_size;
+
+                if (ret == 0 && cluster_size > 0 &&
+                    cluster_size <= BLOCK_SIZE &&
+                    BLOCK_SIZE % cluster_size == 0) {
+                    int i;
+                    int64_t cur_addr;
+                    uint8_t *cur_buf;
+
+                    for (i = 0; i < BLOCK_SIZE / cluster_size; i++) {
+                        cur_addr = addr * BDRV_SECTOR_SIZE
+                                        + i * cluster_size;
+                        cur_buf = buf + i * cluster_size;
+
+                        if (buffer_is_zero(cur_buf, cluster_size)) {
+                            ret = blk_pwrite_zeroes(blk, cur_addr,
+                                                    cluster_size,

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

Quoted 10 lines
migration/block.c +10 -0
+                                                    BDRV_REQ_MAY_UNMAP);
+                        } else {
+                             ret = blk_pwrite(blk, cur_addr, cur_buf,
+                                              cluster_size, 0);
+                        }
+
+                        if (ret < 0) {
+                            g_free(buf);
+                            return ret;
+                        }

This if block is not necessary because...

Quoted 6 lines
migration/block.c +5 -0
+                    }
+                } else {
+                    ret = blk_pwrite(blk, addr * BDRV_SECTOR_SIZE, buf,
+                                     nr_sectors * BDRV_SECTOR_SIZE, 0);
+                }
                 g_free(buf);

...

            if (ret < 0) {
                return ret;
            }
Quoted 6 lines
             }
Signature · 1 line
1.8.3.1

If you remove that:

#2 lore raw
Quoted 55 lines · Fam Zheng
Quoted 54 lines · On Sun, 04/09 20:37, [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 | 38 ++++++++++++++++++++++++++++++++++++--
 1 file changed, 36 insertions(+), 2 deletions(-)
migration/block.c +25 -2
index 7734ff7..fe613db 100644
@@ -885,6 +885,8 @@ static int block_load(QEMUFile *f, void *opaque, int version_id)
     int64_t total_sectors = 0;
     int nr_sectors;
     int ret;
+    BlockDriverInfo bdi;
+    int cluster_size;
 
     do {
         addr = qemu_get_be64(f);
@@ -934,8 +936,40 @@ 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);
+
+                ret = bdrv_get_info(blk_bs(blk), &bdi);
+                cluster_size = bdi.cluster_size;
+
+                if (ret == 0 && cluster_size > 0 &&
+                    cluster_size <= BLOCK_SIZE &&
+                    BLOCK_SIZE % cluster_size == 0) {
+                    int i;
+                    int64_t cur_addr;
+                    uint8_t *cur_buf;
+
+                    for (i = 0; i < BLOCK_SIZE / cluster_size; i++) {
+                        cur_addr = addr * BDRV_SECTOR_SIZE
+                                        + i * cluster_size;
+                        cur_buf = buf + i * cluster_size;
+
+                        if (buffer_is_zero(cur_buf, cluster_size)) {
+                            ret = blk_pwrite_zeroes(blk, cur_addr,
+                                                    cluster_size,
+                                                    BDRV_REQ_MAY_UNMAP);
+                        } else {
+                             ret = blk_pwrite(blk, cur_addr, cur_buf,
+                                              cluster_size, 0);
Quoted 8 lines

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

Quoted 6 lines
migration/block.c +6 -0
+                        }
+
+                        if (ret < 0) {
+                            g_free(buf);
+                            return ret;
+                        }

This if block is not necessary because...

Hi Fam:
It's necessary to check each cluster is written successfully. if we remove this if block, it maybe ignore some error, and only check the last cluster.
Thanks.

Quoted 23 lines
Quoted 7 lines
migration/block.c +5 -0
+                    }
+                } else {
+                    ret = blk_pwrite(blk, addr * BDRV_SECTOR_SIZE, buf,
+                                     nr_sectors * BDRV_SECTOR_SIZE, 0);
+                }
                 g_free(buf);

...

            if (ret < 0) {
                return ret;
            }
Quoted 6 lines
             }

--
1.8.3.1

If you remove that:

Reviewed-by
#3 lore raw
Quoted 5 lines · On Mon, 04/10 09:10, 858585 jemmy wrote:
Quoted 5 lines
Quoted 4 lines
migration/block.c +4 -0
+                        if (ret < 0) {
+                            g_free(buf);
+                            return ret;
+                        }
Quoted 7 lines
Quoted 1 line

This if block is not necessary because...

Hi Fam:
It's necessary to check each cluster is written successfully. if we remove this if block, it maybe ignore some error, and only check the last cluster.
Thanks.

Yes, I missed the fact it is in the loop body. My bad. Thanks for pointint out:

#4 lore raw
In reply to [email protected]
Quoted 28 lines · On Sun, Apr 09, 2017 at 08:37:40PM +0800, [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 | 38 ++++++++++++++++++++++++++++++++++++--
 1 file changed, 36 insertions(+), 2 deletions(-)
migration/block.c +2 -0
index 7734ff7..fe613db 100644
@@ -885,6 +885,8 @@ static int block_load(QEMUFile *f, void *opaque, int version_id)
     int64_t total_sectors = 0;
     int nr_sectors;
     int ret;
+    BlockDriverInfo bdi;
+    int cluster_size;
 
     do {
         addr = qemu_get_be64(f);
@@ -934,8 +936,40 @@ static int block_load(QEMUFile *f, void *opaque, int version_id)
             } else {
                 buf = g_malloc(BLOCK_SIZE);

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

Quoted 10 lines
migration/block.c +7 -2
                 qemu_get_buffer(f, buf, BLOCK_SIZE);
-                ret = blk_pwrite(blk, addr * BDRV_SECTOR_SIZE, buf,
-                                 nr_sectors * BDRV_SECTOR_SIZE, 0);
+
+                ret = bdrv_get_info(blk_bs(blk), &bdi);
+                cluster_size = bdi.cluster_size;
+
+                if (ret == 0 && cluster_size > 0 &&
+                    cluster_size <= BLOCK_SIZE &&
+                    BLOCK_SIZE % cluster_size == 0) {

How about:

  if (blk != blk_prev) {
      blk_prev = blk;
      total_sectors = blk_nb_sectors(blk);
      if (total_sectors <= 0) {
          error_report("Error getting length of block device %s",
                       device_name);
          return -EINVAL;
      }
      blk_invalidate_cache(blk, &local_err);
      if (local_err) {
          error_report_err(local_err);
          return -EINVAL;
      }

+ ret = bdrv_get_info(blk_bs(blk), &bdi);
+ if (ret == 0 && cluster_size > 0 && cluster_size <= BLOCK_SIZE &&
+ BLOCK_SIZE % cluster_size == 0) {
+ zero_cluster_size = bdi.cluster_size;
+ } else {
+ zero_cluster_size = 0;
+ }
}

That way we only fetch the cluster size once per device.

When processing a block we do without repeatedly fetching the cluster size:

  if (zero_cluster_size) {
     ...detect zeroes...
  }
Quoted 6 lines
migration/block.c +6 -0
+                    int i;
+                    int64_t cur_addr;
+                    uint8_t *cur_buf;
+
+                    for (i = 0; i < BLOCK_SIZE / cluster_size; i++) {
+                        cur_addr = addr * BDRV_SECTOR_SIZE

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

Quoted 10 lines
migration/block.c +10 -0
+                                        + i * cluster_size;
+                        cur_buf = buf + i * cluster_size;
+
+                        if (buffer_is_zero(cur_buf, cluster_size)) {
+                            ret = blk_pwrite_zeroes(blk, cur_addr,
+                                                    cluster_size,
+                                                    BDRV_REQ_MAY_UNMAP);
+                        } else {
+                             ret = blk_pwrite(blk, cur_addr, cur_buf,
+                                              cluster_size, 0);

Indentation is off here.

Quoted 17 lines
migration/block.c +11 -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
#5 lore raw
Quoted 66 lines · Stefan Hajnoczi
Quoted 28 lines · On Sun, Apr 09, 2017 at 08:37:40PM +0800, [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 | 38 ++++++++++++++++++++++++++++++++++++--
 1 file changed, 36 insertions(+), 2 deletions(-)
migration/block.c +2 -0
index 7734ff7..fe613db 100644
@@ -885,6 +885,8 @@ static int block_load(QEMUFile *f, void *opaque, int version_id)
     int64_t total_sectors = 0;
     int nr_sectors;
     int ret;
+    BlockDriverInfo bdi;
+    int cluster_size;
 
     do {
         addr = qemu_get_be64(f);
@@ -934,8 +936,40 @@ static int block_load(QEMUFile *f, void *opaque, int version_id)
             } else {
                 buf = g_malloc(BLOCK_SIZE);

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

Quoted 10 lines
migration/block.c +7 -2
                 qemu_get_buffer(f, buf, BLOCK_SIZE);
-                ret = blk_pwrite(blk, addr * BDRV_SECTOR_SIZE, buf,
-                                 nr_sectors * BDRV_SECTOR_SIZE, 0);
+
+                ret = bdrv_get_info(blk_bs(blk), &bdi);
+                cluster_size = bdi.cluster_size;
+
+                if (ret == 0 && cluster_size > 0 &&
+                    cluster_size <= BLOCK_SIZE &&
+                    BLOCK_SIZE % cluster_size == 0) {

How about:

  if (blk != blk_prev) {
      blk_prev = blk;
      total_sectors = blk_nb_sectors(blk);
      if (total_sectors <= 0) {
          error_report("Error getting length of block device %s",
                       device_name);
          return -EINVAL;
      }
      blk_invalidate_cache(blk, &local_err);
      if (local_err) {
          error_report_err(local_err);
          return -EINVAL;
      }
migration/block.c +7 -0
+     ret = bdrv_get_info(blk_bs(blk), &bdi);
+     if (ret == 0 && cluster_size > 0 && cluster_size <= BLOCK_SIZE &&
+         BLOCK_SIZE % cluster_size == 0) {
+         zero_cluster_size = bdi.cluster_size;
+     } else {
+         zero_cluster_size = 0;
+     }
  }
Quoted 8 lines

That way we only fetch the cluster size once per device.

When processing a block we do without repeatedly fetching the cluster size:

  if (zero_cluster_size) {
     ...detect zeroes...
  }

good idea, i will test again for this patch.

Quoted 37 lines
Quoted 7 lines
migration/block.c +6 -0
+                    int i;
+                    int64_t cur_addr;
+                    uint8_t *cur_buf;
+
+                    for (i = 0; i < BLOCK_SIZE / cluster_size; i++) {
+                        cur_addr = addr * BDRV_SECTOR_SIZE

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

Quoted 10 lines
migration/block.c +10 -0
+                                        + i * cluster_size;
+                        cur_buf = buf + i * cluster_size;
+
+                        if (buffer_is_zero(cur_buf, cluster_size)) {
+                            ret = blk_pwrite_zeroes(blk, cur_addr,
+                                                    cluster_size,
+                                                    BDRV_REQ_MAY_UNMAP);
+                        } else {
+                             ret = blk_pwrite(blk, cur_addr, cur_buf,
+                                              cluster_size, 0);

Indentation is off here.

Quoted 17 lines
migration/block.c +11 -1
+                        }
+
+                        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