Skip to contentSkip to conversation

qemu-img compare --stat

PATCH Superseded Vladimir Sementsov-Ogievskiy 2 participants 14 messages Oct 21, 2021 – Oct 28, 2021

v2 of 3 Superseded Latest is v3 →

lore
Share & tools
lore
#0 lore raw

v2 cover qemu-img compare --stat

Hi all!

v2: allow using --shallow without --stat

Recently we faced the following task:

Customer comes and say: incremental backup images are too fat. Does you incremental backup works correct?

What to answer? We should check something. At least check that incremental images doesn't store same data twice. And we don't have a tool for it. I just wrote a simple python script to compare raw files cluster-by-cluster. Then we've mounted the qcow2 images with help of qemu-nbd, the resulting /dev/nbd* were compared and we proved that incremental backups don't store same data.

But that leads to idea that some kind of that script would be good to have at hand.

So, here is a new option for qemu-img compare, that is a lot more powerful and effective than original script, and allows to compare and calculate statistics, i.e. how many clusters differs, how many clusters changed from unallocated to data, and so on.

For examples of output look at the test in patch 04.

Vladimir Sementsov-Ogievskiy (4):
qemu-img: implement compare --stat
qemu-img: make --block-size optional for compare --stat
qemu-img: add --shallow option for qemu-img compare
iotests: add qemu-img-compare-stat test

 docs/tools/qemu-img.rst                       |  30 +-
 qemu-img.c                                    | 269 +++++++++++++++++-
 qemu-img-cmds.hx                              |   4 +-
 .../qemu-iotests/tests/qemu-img-compare-stat  |  88 ++++++
 .../tests/qemu-img-compare-stat.out           | 106 +++++++
 5 files changed, 479 insertions(+), 18 deletions(-)
 create mode 100755 tests/qemu-iotests/tests/qemu-img-compare-stat
 create mode 100644 tests/qemu-iotests/tests/qemu-img-compare-stat.out
Signature · 1 line
2.31.1
#1 lore raw

PATCH v2 1/4 qemu-img: implement compare --stat

With new option qemu-img compare will not stop at first mismatch, but instead calculate statistics: how many clusters with different data, how many clusters with equal data, how many clusters were unallocated but become data and so on.

We compare images chunk by chunk. Chunk size depends on what block_status returns for both images. It may return less than cluster (remember about qcow2 subclusters), it may return more than cluster (if several consecutive clusters share same status). Finally images may have different cluster sizes. This all leads to ambiguity in how to finally compare the data.

What we can say for sure is that, when we compare two qcow2 images with same cluster size, we should compare clusters with data separately. Otherwise, if we for example compare 10 consecutive clusters of data where only one byte differs we'll report 10 different clusters. Expected result in this case is 1 different cluster and 9 equal ones.

So, to serve this case and just to have some defined rule let's do the following:

1. Select some block-size for compare procedure. In this commit it must be specified by user, next commit will add some automatic logic and make --block-size optional.

2. Go chunk-by-chunk using block_status as we do now with only one differency:
If block_status() returns DATA region that intersects block-size aligned boundary, crop this region at this boundary.

This way it's still possible to compare less than cluster and report subcluster-level accuracy, but we newer compare more than one cluster of data.

---
 docs/tools/qemu-img.rst |  18 +++-
 qemu-img.c              | 206 +++++++++++++++++++++++++++++++++++++---
 qemu-img-cmds.hx        |   4 +-
 3 files changed, 212 insertions(+), 16 deletions(-)
docs/tools/qemu-img.rst +15 -3
index d58980aef8..21164253d4 100644
@@ -159,6 +159,18 @@ Parameters to compare subcommand:
 
   Strict mode - fail on different image size or sector allocation
 
+.. option:: --stat
+
+  Instead of exit on first mismatch compare the whole images and print
+  statistics on amount of different pairs of clusters, based on their
+  block-status and are they equal or not.
+
+.. option:: --block-size BLOCK_SIZE
+
+  Block size for comparing with ``--stat``. This doesn't guarantee exact
+  size of comparing chunks, but that guarantee that data chunks being
+  compared will never cross aligned block-size boundary.
+
 Parameters to convert subcommand:
 
 .. program:: qemu-img-convert
@@ -378,7 +390,7 @@ Command description:
 
   The rate limit for the commit process is specified by ``-r``.
 
-.. option:: compare [--object OBJECTDEF] [--image-opts] [-f FMT] [-F FMT] [-T SRC_CACHE] [-p] [-q] [-s] [-U] FILENAME1 FILENAME2
+.. option:: compare [--object OBJECTDEF] [--image-opts] [-f FMT] [-F FMT] [-T SRC_CACHE] [-p] [-q] [-s] [-U] [--stat --block-size BLOCK_SIZE] FILENAME1 FILENAME2
 
   Check if two images have the same content. You can compare images with
   different format or settings.
@@ -405,9 +417,9 @@ Command description:
   The following table sumarizes all exit codes of the compare subcommand:
 
   0
-    Images are identical (or requested help was printed)
+    Images are identical (or requested help was printed, or ``--stat`` was used)
   1
-    Images differ
+    Images differ (1 is never returned when ``--stat`` option specified)
   2
     Error on opening an image
   3
qemu-img.c +195 -11

330 lines not on this page · raw message

qemu-img-cmds.hx +2 -2
index 4c4d94ab22..0b2d63ca5f 100644
@@ -40,9 +40,9 @@ SRST
 ERST
 
 DEF("compare", img_compare,
-    "compare [--object objectdef] [--image-opts] [-f fmt] [-F fmt] [-T src_cache] [-p] [-q] [-s] [-U] filename1 filename2")
+    "compare [--object objectdef] [--image-opts] [-f fmt] [-F fmt] [-T src_cache] [-p] [-q] [-s] [-U] [--stat --block-size BLOCK_SIZE] filename1 filename2")
 SRST
-.. option:: compare [--object OBJECTDEF] [--image-opts] [-f FMT] [-F FMT] [-T SRC_CACHE] [-p] [-q] [-s] [-U] FILENAME1 FILENAME2
+.. option:: compare [--object OBJECTDEF] [--image-opts] [-f FMT] [-F FMT] [-T SRC_CACHE] [-p] [-q] [-s] [-U] [--stat --block-size BLOCK_SIZE] FILENAME1 FILENAME2
 ERST
 
 DEF("convert", img_convert,
Signature · 1 line
2.31.1
#2 lore raw

Re: [PATCH v2 1/4] qemu-img: implement compare --stat

Quoted 34 lines · Vladimir Sementsov-Ogievskiy

With new option qemu-img compare will not stop at first mismatch, but instead calculate statistics: how many clusters with different data, how many clusters with equal data, how many clusters were unallocated but become data and so on.

We compare images chunk by chunk. Chunk size depends on what block_status returns for both images. It may return less than cluster (remember about qcow2 subclusters), it may return more than cluster (if several consecutive clusters share same status). Finally images may have different cluster sizes. This all leads to ambiguity in how to finally compare the data.

What we can say for sure is that, when we compare two qcow2 images with same cluster size, we should compare clusters with data separately. Otherwise, if we for example compare 10 consecutive clusters of data where only one byte differs we'll report 10 different clusters. Expected result in this case is 1 different cluster and 9 equal ones.

So, to serve this case and just to have some defined rule let's do the following:

1. Select some block-size for compare procedure. In this commit it must be specified by user, next commit will add some automatic logic and make --block-size optional.

2. Go chunk-by-chunk using block_status as we do now with only one differency:
If block_status() returns DATA region that intersects block-size aligned boundary, crop this region at this boundary.

This way it's still possible to compare less than cluster and report subcluster-level accuracy, but we newer compare more than one cluster of data.

Quoted 6 lines
---
  docs/tools/qemu-img.rst |  18 +++-
  qemu-img.c              | 206 +++++++++++++++++++++++++++++++++++++---
  qemu-img-cmds.hx        |   4 +-
  3 files changed, 212 insertions(+), 16 deletions(-)

Looks good to me overall!  Just some technical comments below.

docs/tools/qemu-img.rst Parameters to compare subcommand: · line 166

Quoted 13 lines
docs/tools/qemu-img.rst +5 -0
index d58980aef8..21164253d4 100644
@@ -159,6 +159,18 @@ Parameters to compare subcommand:
 
    Strict mode - fail on different image size or sector allocation
 
+.. option:: --stat
+
+  Instead of exit on first mismatch compare the whole images and print
+  statistics on amount of different pairs of clusters, based on their
+  block-status and are they equal or not.

I’d phrase this as:

Instead of exiting on the first mismatch, compare the whole images and print statistics on how much they differ in terms of block status (i.e. are blocks allocated or not, do they contain data, are they marked as containing only zeroes) and block content (a block of data that contains only zero still has the same content as a marked-zero block).

docs/tools/qemu-img.rst Parameters to compare subcommand: · line 172

Quoted 6 lines
docs/tools/qemu-img.rst +6 -0
+
+.. option:: --block-size BLOCK_SIZE
+
+  Block size for comparing with ``--stat``. This doesn't guarantee exact
+  size of comparing chunks, but that guarantee that data chunks being
+  compared will never cross aligned block-size boundary.

I’d do just some minor tweaks to the second sentence:

This doesn't guarantee an exact size for comparing chunks, but it does guarantee that those chunks will never cross a block-size-aligned boundary.

Quoted 4 lines
docs/tools/qemu-img.rst +1 -0
+
  Parameters to convert subcommand:
 
  .. program:: qemu-img-convert

[...]

qemu-img.c enum { · line 86

Quoted 9 lines
qemu-img.c +1 -0
index f036a1d428..79a0589167 100644
@@ -83,6 +83,8 @@ enum {
      OPTION_BITMAPS = 275,
      OPTION_FORCE = 276,
      OPTION_SKIP_BROKEN = 277,
+    OPTION_STAT = 277,

That doesn’t look ideal, I believe OPTION_STAT should have a different value than OPTION_SKIP_BROKEN.  (I guess a rebase is to blame?)

Quoted 5 lines
qemu-img.c +1 -0
+    OPTION_BLOCK_SIZE = 278,
  };
 
  typedef enum OutputFormat {

qemu-img.c static int check_empty_sectors(BlockBackend *blk, int64_t offset, · line 1315

Quoted 10 lines
qemu-img.c +7 -0
@@ -1304,6 +1306,107 @@ static int check_empty_sectors(BlockBackend *blk, int64_t offset,
      return 0;
  }
 
+#define IMG_CMP_STATUS_MASK (BDRV_BLOCK_DATA | BDRV_BLOCK_ZERO | \
+                             BDRV_BLOCK_ALLOCATED)
+#define IMG_CMP_STATUS_MAX (IMG_CMP_STATUS_MASK | BDRV_BLOCK_EOF)
+
+typedef struct ImgCmpStat {
+    /* stat: [ret: 0 is equal, 1 is not][status1][status2] -> n_bytes */
+    uint64_t stat[2][IMG_CMP_STATUS_MAX + 1][IMG_CMP_STATUS_MAX + 1];

IMG_CMP_STATUS_MAX isn’t packed tightly because it only has four bits set (0x33).  That in itself isn’t a problem, but it means that IMG_CMP_STATUS_MAX + 1 is 52, and so this array’s size is 52 * 52 * 2 * sizeof(uint64_t) = 43264.  Again, that isn’t a problem in itself (although it is a bit sad that this could fit into 16 * 16 * 2 * 8 = 4 kB), but in img_compare() [1], you put this structure on the stack, and I believe it’s too big for that.

Quoted 1 line
qemu-img.c +1 -0
+} ImgCmpStat;

[...]

qemu-img.c static int check_empty_sectors(BlockBackend *blk, int64_t offset, · line 1352

Quoted 4 lines
qemu-img.c +4 -0
+static void cmp_stat_print_agenda(void)
+{
+    printf("Compare stats:\n"
+           "Agenda\n"

I’m more used to the term “Key” for this, but my experience is mostly limited to the git-backport-diff output, so it’s not that strong.

Quoted 5 lines
qemu-img.c +5 -0
+           "D: DATA\n"
+           "Z: ZERO\n"
+           "A: ALLOCATED\n"
+           "E: after end of file\n\n");
+}

[...]

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

Quoted 5 lines
qemu-img.c +1 -0
@@ -1331,6 +1436,9 @@ static int img_compare(int argc, char **argv)
      uint64_t progress_base;
      bool image_opts = false;
      bool force_share = false;
+    ImgCmpStat stat = {0};

[1] (Here is where ImgCmpStat goes on the stack, which it shouldn’t, considering it’s over 40 kB in size.)

Quoted 5 lines
qemu-img.c +2 -0
+    bool do_stat;
+    int64_t block_size = 0;
 
      cache = BDRV_DEFAULT_CACHE;
      for (;;) {

[...]

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

Quoted 11 lines
qemu-img.c +7 -0
@@ -1395,6 +1505,15 @@ static int img_compare(int argc, char **argv)
          case OPTION_IMAGE_OPTS:
              image_opts = true;
              break;
+        case OPTION_STAT:
+            do_stat = true;
+            break;
+        case OPTION_BLOCK_SIZE:
+            block_size = cvtnum_full("block size", optarg, 1, INT64_MAX);
+            if (block_size < 0) {
+                exit(EXIT_SUCCESS);

Shouldn’t this be 2 instead of EXIT_SUCCESS?

(Above for --object we use EXIT_SUCCESS, but only for the case where a help text was printed.  When parsing fails, we exit with 2, which is documented as the error code (“>1”).)

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

Quoted 12 lines
qemu-img.c +5 -0
+            }
+            break;
          }
      }
 
@@ -1410,6 +1529,20 @@ static int img_compare(int argc, char **argv)
      filename1 = argv[optind++];
      filename2 = argv[optind++];
 
+    if (!do_stat && block_size) {
+        error_report("--block-size can be used only together with --stat");
+        ret = 1;

2 is the error code, 1 means “success, but images differ”.

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

Quoted 1 line
qemu-img.c +1 -0
+        goto out;

The out label frees buf1 and buf2, and unrefs blk1 and blk2.  My gcc complains that blk1 and blk2 have not been initialized by this point, though.  I believe we should go to out3 here.

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

Quoted 7 lines
qemu-img.c +7 -0
+    }
+
+    if (do_stat && !block_size) {
+        /* TODO: make block-size optional */
+        error_report("You must specify --block-size together with --stat");
+        ret = 1;
+        goto out;

Same here.

Quoted 6 lines
qemu-img.c +3 -0
+    }
+
+
      /* Initialize before goto out */
      qemu_progress_init(progress, 2.0);
 

[...]

Quoted 4 lines
qemu-img.c +0 -0
@@ -1486,11 +1623,15 @@ static int img_compare(int argc, char **argv)
              goto out;
          }
          allocated2 = status2 & BDRV_BLOCK_ALLOCATED;

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

Quoted 10 lines
qemu-img.c +5 -1
+        if (do_stat && (status2 & BDRV_BLOCK_DATA)) {
+            /* Don't compare cross-block data */
+            pnum2 = MIN(block_end, offset + pnum2) - offset;
+        }
 
          assert(pnum1 && pnum2);
          chunk = MIN(pnum1, pnum2);
 
-        if (strict) {
+        if (strict && !do_stat) {

Question is, do we want to allow strict mode together with --stat at all?  I think I’d prefer making it an outright error.

Hanna

Quoted 3 lines
qemu-img.c +0 -0
              if (status1 != status2) {
                  ret = 1;
                  qprintf(quiet, "Strict mode: Offset %" PRId64
#3 lore raw

Re: [PATCH v2 1/4] qemu-img: implement compare --stat

Quoted 58 lines · Hanna Reitz
Quoted 34 lines · Vladimir Sementsov-Ogievskiy

With new option qemu-img compare will not stop at first mismatch, but instead calculate statistics: how many clusters with different data, how many clusters with equal data, how many clusters were unallocated but become data and so on.

We compare images chunk by chunk. Chunk size depends on what block_status returns for both images. It may return less than cluster (remember about qcow2 subclusters), it may return more than cluster (if several consecutive clusters share same status). Finally images may have different cluster sizes. This all leads to ambiguity in how to finally compare the data.

What we can say for sure is that, when we compare two qcow2 images with same cluster size, we should compare clusters with data separately. Otherwise, if we for example compare 10 consecutive clusters of data where only one byte differs we'll report 10 different clusters. Expected result in this case is 1 different cluster and 9 equal ones.

So, to serve this case and just to have some defined rule let's do the following:

1. Select some block-size for compare procedure. In this commit it must be specified by user, next commit will add some automatic logic and make --block-size optional.

2. Go chunk-by-chunk using block_status as we do now with only one differency:
    If block_status() returns DATA region that intersects block-size aligned boundary, crop this region at this boundary.

This way it's still possible to compare less than cluster and report subcluster-level accuracy, but we newer compare more than one cluster of data.

Quoted 6 lines

---
  docs/tools/qemu-img.rst |  18 +++-
  qemu-img.c              | 206 +++++++++++++++++++++++++++++++++++++--- qemu-img-cmds.hx        |   4 +-

  3 files changed, 212 insertions(+), 16 deletions(-)

Looks good to me overall!  Just some technical comments below.

Quoted 11 lines
docs/tools/qemu-img.rst +0 -0
index d58980aef8..21164253d4 100644
@@ -159,6 +159,18 @@ Parameters to compare subcommand:

    Strict mode - fail on different image size or sector allocation +.. option:: --stat
+
+  Instead of exit on first mismatch compare the whole images and print +  statistics on amount of different pairs of clusters, based on their +  block-status and are they equal or not.

I’d phrase this as:

Quoted 1 line

Instead of exiting on the first mismatch, compare the whole images and print statistics on how much they differ in terms of block status (i.e. are blocks allocated or not, do they contain data, are they marked as containing only zeroes) and block content (a block of data that contains only zero still has the same content as a marked-zero block).

For me the rest starting from "and block content" sounds unclear, seems doesn't add any information to previous (i.e. are blocks allocated ...)

Quoted 11 lines

docs/tools/qemu-img.rst Parameters to compare subcommand: · line 172

Quoted 7 lines
docs/tools/qemu-img.rst +6 -0
+
+.. option:: --block-size BLOCK_SIZE
+
+  Block size for comparing with ``--stat``. This doesn't guarantee exact
+  size of comparing chunks, but that guarantee that data chunks being
+  compared will never cross aligned block-size boundary.

I’d do just some minor tweaks to the second sentence:

This doesn't guarantee an exact size for comparing chunks, but it does guarantee that those chunks will never cross a block-size-aligned boundary.

OK, sounds good

Quoted 7 lines
Quoted 4 lines

+
  Parameters to convert subcommand:
  .. program:: qemu-img-convert

[...]

Quoted 11 lines

qemu-img.c enum { · line 86

Quoted 9 lines
qemu-img.c +1 -0
index f036a1d428..79a0589167 100644
@@ -83,6 +83,8 @@ enum {
       OPTION_BITMAPS = 275,
       OPTION_FORCE = 276,
       OPTION_SKIP_BROKEN = 277,
+    OPTION_STAT = 277,

That doesn’t look ideal, I believe OPTION_STAT should have a different value than OPTION_SKIP_BROKEN.  (I guess a rebase is to blame?)

Oops

Quoted 16 lines
Quoted 5 lines
qemu-img.c +1 -0
+    OPTION_BLOCK_SIZE = 278,
   };
   typedef enum OutputFormat {

qemu-img.c static int check_empty_sectors(BlockBackend *blk, int64_t offset, · line 1314

Quoted 9 lines
qemu-img.c +7 -0
@@ -1304,6 +1306,107 @@ static int check_empty_sectors(BlockBackend *blk, int64_t offset,
       return 0;
   }
+#define IMG_CMP_STATUS_MASK (BDRV_BLOCK_DATA | BDRV_BLOCK_ZERO | \
+                             BDRV_BLOCK_ALLOCATED)
+#define IMG_CMP_STATUS_MAX (IMG_CMP_STATUS_MASK | BDRV_BLOCK_EOF)
+
+typedef struct ImgCmpStat {
+    /* stat: [ret: 0 is equal, 1 is not][status1][status2] -> n_bytes */
+    uint64_t stat[2][IMG_CMP_STATUS_MAX + 1][IMG_CMP_STATUS_MAX + 1];

IMG_CMP_STATUS_MAX isn’t packed tightly because it only has four bits set (0x33).  That in itself isn’t a problem, but it means that IMG_CMP_STATUS_MAX + 1 is 52, and so this array’s size is 52 * 52 * 2 * sizeof(uint64_t) = 43264.  Again, that isn’t a problem in itself (although it is a bit sad that this could fit into 16 * 16 * 2 * 8 = 4 kB), but in img_compare() [1], you put this structure on the stack, and I believe it’s too big for that.

Hmm. May be, it's better just use GHashTables and don't bother with these sparse arrays

Quoted 11 lines
Quoted 2 lines
qemu-img.c +1 -0
+} ImgCmpStat;

[...]

qemu-img.c static int check_empty_sectors(BlockBackend *blk, int64_t offset, · line 1352

Quoted 4 lines
qemu-img.c +4 -0
+static void cmp_stat_print_agenda(void)
+{
+    printf("Compare stats:\n"
+           "Agenda\n"

I’m more used to the term “Key” for this, but my experience is mostly limited to the git-backport-diff output, so it’s not that strong.

I don't care, "key" is OK for me.

Quoted 24 lines
Quoted 6 lines

+           "D: DATA\n"
+           "Z: ZERO\n"
+           "A: ALLOCATED\n"
+           "E: after end of file\n\n");
+}

[...]

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

Quoted 5 lines
qemu-img.c +1 -0
@@ -1331,6 +1436,9 @@ static int img_compare(int argc, char **argv)
       uint64_t progress_base;
       bool image_opts = false;
       bool force_share = false;
+    ImgCmpStat stat = {0};

[1] (Here is where ImgCmpStat goes on the stack, which it shouldn’t, considering it’s over 40 kB in size.)

Quoted 4 lines
qemu-img.c +2 -0
+    bool do_stat;
+    int64_t block_size = 0;
       cache = BDRV_DEFAULT_CACHE;
       for (;;) {

[...]

Quoted 13 lines
Quoted 11 lines
qemu-img.c +0 -0
@@ -1395,6 +1505,15 @@ static int img_compare(int argc, char **argv)

          case OPTION_IMAGE_OPTS:
              image_opts = true;
              break;
+        case OPTION_STAT:
+            do_stat = true;
+            break;
+        case OPTION_BLOCK_SIZE:
+            block_size = cvtnum_full("block size", optarg, 1, INT64_MAX); +            if (block_size < 0) {
+                exit(EXIT_SUCCESS);

Shouldn’t this be 2 instead of EXIT_SUCCESS?

Oops, right, it should be an error

Quoted 15 lines

(Above for --object we use EXIT_SUCCESS, but only for the case where a help text was printed.  When parsing fails, we exit with 2, which is documented as the error code (“>1”).)

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

Quoted 10 lines

+            }
+            break;
          }
      }

qemu-img.c +3 -0
@@ -1410,6 +1529,20 @@ static int img_compare(int argc, char **argv)
       filename1 = argv[optind++];
       filename2 = argv[optind++];
+    if (!do_stat && block_size) {
+        error_report("--block-size can be used only together with --stat");
+        ret = 1;

2 is the error code, 1 means “success, but images differ”.

Will fix

Quoted 23 lines

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

Quoted 2 lines
qemu-img.c +1 -0
+        goto out;

The out label frees buf1 and buf2, and unrefs blk1 and blk2. My gcc complains that blk1 and blk2 have not been initialized by this point, though.  I believe we should go to out3 here.

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

Quoted 7 lines
qemu-img.c +7 -0
+    }
+
+    if (do_stat && !block_size) {
+        /* TODO: make block-size optional */
+        error_report("You must specify --block-size together with --stat");
+        ret = 1;
+        goto out;

Same here.

Quoted 5 lines
qemu-img.c +3 -0
+    }
+
+
       /* Initialize before goto out */
       qemu_progress_init(progress, 2.0);

[...]

Quoted 15 lines

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

Quoted 12 lines
qemu-img.c +5 -1
@@ -1486,11 +1623,15 @@ static int img_compare(int argc, char **argv)
               goto out;
           }
           allocated2 = status2 & BDRV_BLOCK_ALLOCATED;
+        if (do_stat && (status2 & BDRV_BLOCK_DATA)) {
+            /* Don't compare cross-block data */
+            pnum2 = MIN(block_end, offset + pnum2) - offset;
+        }
           assert(pnum1 && pnum2);
           chunk = MIN(pnum1, pnum2);
-        if (strict) {
+        if (strict && !do_stat) {

Question is, do we want to allow strict mode together with --stat at all?  I think I’d prefer making it an outright error.

Hmm. I think, I tend to agree, will do.

Quoted 5 lines
Quoted 4 lines
qemu-img.c +0 -0
               if (status1 != status2) {
                   ret = 1;

                  qprintf(quiet, "Strict mode: Offset %" PRId64

Thanks!

Signature · 2 lines
Best regards,
Vladimir
#4 lore raw

Re: [PATCH v2 1/4] qemu-img: implement compare --stat

Quoted 60 lines · Vladimir Sementsov-Ogievskiy
Quoted 59 lines · Hanna Reitz
Quoted 34 lines · Vladimir Sementsov-Ogievskiy

With new option qemu-img compare will not stop at first mismatch, but instead calculate statistics: how many clusters with different data, how many clusters with equal data, how many clusters were unallocated but become data and so on.

We compare images chunk by chunk. Chunk size depends on what block_status returns for both images. It may return less than cluster (remember about qcow2 subclusters), it may return more than cluster (if several consecutive clusters share same status). Finally images may have different cluster sizes. This all leads to ambiguity in how to finally compare the data.

What we can say for sure is that, when we compare two qcow2 images with same cluster size, we should compare clusters with data separately. Otherwise, if we for example compare 10 consecutive clusters of data where only one byte differs we'll report 10 different clusters. Expected result in this case is 1 different cluster and 9 equal ones.

So, to serve this case and just to have some defined rule let's do the following:

1. Select some block-size for compare procedure. In this commit it must be specified by user, next commit will add some automatic logic and make --block-size optional.

2. Go chunk-by-chunk using block_status as we do now with only one differency:
    If block_status() returns DATA region that intersects block-size aligned boundary, crop this region at this boundary.

This way it's still possible to compare less than cluster and report subcluster-level accuracy, but we newer compare more than one cluster of data.

Quoted 7 lines

---
  docs/tools/qemu-img.rst |  18 +++-
  qemu-img.c              | 206
+++++++++++++++++++++++++++++++++++++---
  qemu-img-cmds.hx        |   4 +-

  3 files changed, 212 insertions(+), 16 deletions(-)

Looks good to me overall!  Just some technical comments below.

Quoted 11 lines
docs/tools/qemu-img.rst +0 -0
index d58980aef8..21164253d4 100644
@@ -159,6 +159,18 @@ Parameters to compare subcommand:

    Strict mode - fail on different image size or sector allocation +.. option:: --stat
+
+  Instead of exit on first mismatch compare the whole images and print +  statistics on amount of different pairs of clusters, based on their +  block-status and are they equal or not.

I’d phrase this as:

Quoted 3 lines
Quoted 1 line

Instead of exiting on the first mismatch, compare the whole images and print statistics on how much they differ in terms of block status (i.e. are blocks allocated or not, do they contain data, are they marked as containing only zeroes) and block content (a block of data that contains only zero still has the same content as a marked-zero block).

For me the rest starting from "and block content" sounds unclear, seems doesn't add any information to previous (i.e. are blocks allocated ...)

By “block content” I meant what you said by “equal or not”, i.e. what is returned when reading from the block.

Now that I think about it again, I believe we should go with your original “equal or not”, though, because that reflects what qemu-img --stat prints, like so perhaps:

Instead of exiting on the first mismatch, compare the whole images and print statistics on the amount of different pairs of blocks, based on their block status and whether they are equal or not.

I’d still like to add something like what I had in parentheses, though, because as a user, I’d find the “block status” and “equal or not” terms to be a bit handwave-y.  I don’t think “block status” is a common term in our documentation, so I wanted to add some examples; and I wanted to show by example that “equal” blocks don’t need to have the same block status.

[...]

Quoted 15 lines
Quoted 6 lines
Quoted 6 lines
docs/tools/qemu-img.rst +2 -0
@@ -1304,6 +1306,107 @@ static int check_empty_sectors(BlockBackend 
 *blk, int64_t offset,
       return 0;
   }
+#define IMG_CMP_STATUS_MASK (BDRV_BLOCK_DATA | BDRV_BLOCK_ZERO | \
+                             BDRV_BLOCK_ALLOCATED)
Quoted 8 lines
Quoted 6 lines
docs/tools/qemu-img.rst +4 -0
+#define IMG_CMP_STATUS_MAX (IMG_CMP_STATUS_MASK | BDRV_BLOCK_EOF)
+
+typedef struct ImgCmpStat {
+    /* stat: [ret: 0 is equal, 1 is not][status1][status2] -> 

n_bytes */
+    uint64_t stat[2][IMG_CMP_STATUS_MAX + 1][IMG_CMP_STATUS_MAX + 1];

IMG_CMP_STATUS_MAX isn’t packed tightly because it only has four bits set (0x33).  That in itself isn’t a problem, but it means that IMG_CMP_STATUS_MAX + 1 is 52, and so this array’s size is 52 * 52 * 2 * sizeof(uint64_t) = 43264.  Again, that isn’t a problem in itself (although it is a bit sad that this could fit into 16 * 16 * 2 * 8 = 4 kB), but in img_compare() [1], you put this structure on the stack, and I believe it’s too big for that.

Quoted 1 line

Hmm. May be, it's better just use GHashTables and don't bother with these sparse arrays

Or we could use our own bits here (ALLOCATED = (1 << 2), EOF = (1 << 3)) and have a small function that translates BDRV_BLOCK_* values into them.

In any case, I don’t mind the sparseness too much, it’s just that it shouldn’t go on the stack.

Hanna

#5 lore raw

Re: [PATCH v2 1/4] qemu-img: implement compare --stat

Quoted 62 lines · Hanna Reitz
Quoted 61 lines · Vladimir Sementsov-Ogievskiy
Quoted 59 lines · Hanna Reitz
Quoted 34 lines · Vladimir Sementsov-Ogievskiy

With new option qemu-img compare will not stop at first mismatch, but instead calculate statistics: how many clusters with different data, how many clusters with equal data, how many clusters were unallocated but become data and so on.

We compare images chunk by chunk. Chunk size depends on what block_status returns for both images. It may return less than cluster (remember about qcow2 subclusters), it may return more than cluster (if several consecutive clusters share same status). Finally images may have different cluster sizes. This all leads to ambiguity in how to finally compare the data.

What we can say for sure is that, when we compare two qcow2 images with same cluster size, we should compare clusters with data separately. Otherwise, if we for example compare 10 consecutive clusters of data where only one byte differs we'll report 10 different clusters. Expected result in this case is 1 different cluster and 9 equal ones.

So, to serve this case and just to have some defined rule let's do the following:

1. Select some block-size for compare procedure. In this commit it must be specified by user, next commit will add some automatic logic and make --block-size optional.

2. Go chunk-by-chunk using block_status as we do now with only one differency:
    If block_status() returns DATA region that intersects block-size aligned boundary, crop this region at this boundary.

This way it's still possible to compare less than cluster and report subcluster-level accuracy, but we newer compare more than one cluster of data.

Quoted 6 lines

---
  docs/tools/qemu-img.rst |  18 +++-
  qemu-img.c              | 206 +++++++++++++++++++++++++++++++++++++--- qemu-img-cmds.hx        |   4 +-

  3 files changed, 212 insertions(+), 16 deletions(-)

Looks good to me overall!  Just some technical comments below.

Quoted 11 lines
docs/tools/qemu-img.rst +0 -0
index d58980aef8..21164253d4 100644
@@ -159,6 +159,18 @@ Parameters to compare subcommand:

    Strict mode - fail on different image size or sector allocation +.. option:: --stat
+
+  Instead of exit on first mismatch compare the whole images and print +  statistics on amount of different pairs of clusters, based on their +  block-status and are they equal or not.

I’d phrase this as:

Instead of exiting on the first mismatch, compare the whole images and print statistics on how much they differ in terms of block status (i.e. are blocks allocated or not, do they contain data, are they marked as containing only zeroes) and block content (a block of data that contains only zero still has the same content as a marked-zero block).

Quoted 7 lines
Quoted 1 line

For me the rest starting from "and block content" sounds unclear, seems doesn't add any information to previous (i.e. are blocks allocated ...)

By “block content” I meant what you said by “equal or not”, i.e. what is returned when reading from the block.

Now that I think about it again, I believe we should go with your original “equal or not”, though, because that reflects what qemu-img --stat prints, like so perhaps:

Instead of exiting on the first mismatch, compare the whole images and print statistics on the amount of different pairs of blocks, based on their block status and whether they are equal or not.

OK

Quoted 2 lines

I’d still like to add something like what I had in parentheses, though, because as a user, I’d find the “block status” and “equal or not” terms to be a bit handwave-y.  I don’t think “block status” is a common term in our documentation, so I wanted to add some examples; and I wanted to show by example that “equal” blocks don’t need to have the same block status.

Actually, I think, that the resulting tool is anyway developer-oriented, to use it you should know what this block status really means.. May be just s/block status/block type/, will it be more human friendly?

Quoted 24 lines

[...]

Quoted 13 lines
Quoted 4 lines
Quoted 4 lines
docs/tools/qemu-img.rst +1 -0
@@ -1304,6 +1306,107 @@ static int check_empty_sectors(BlockBackend *blk, int64_t offset,
       return 0;
   }
+#define IMG_CMP_STATUS_MASK (BDRV_BLOCK_DATA | BDRV_BLOCK_ZERO | \
Quoted 8 lines

docs/tools/qemu-img.rst static int check_empty_sectors(BlockBackend *blk, int64_t offset, · line 1314

Quoted 6 lines
docs/tools/qemu-img.rst +6 -0
+                             BDRV_BLOCK_ALLOCATED)
+#define IMG_CMP_STATUS_MAX (IMG_CMP_STATUS_MASK | BDRV_BLOCK_EOF)
+
+typedef struct ImgCmpStat {
+    /* stat: [ret: 0 is equal, 1 is not][status1][status2] -> n_bytes */
+    uint64_t stat[2][IMG_CMP_STATUS_MAX + 1][IMG_CMP_STATUS_MAX + 1];

IMG_CMP_STATUS_MAX isn’t packed tightly because it only has four bits set (0x33).  That in itself isn’t a problem, but it means that IMG_CMP_STATUS_MAX + 1 is 52, and so this array’s size is 52 * 52 * 2 * sizeof(uint64_t) = 43264.  Again, that isn’t a problem in itself (although it is a bit sad that this could fit into 16 * 16 * 2 * 8 = 4 kB), but in img_compare() [1], you put this structure on the stack, and I believe it’s too big for that.

Quoted 1 line

Hmm. May be, it's better just use GHashTables and don't bother with these sparse arrays

Or we could use our own bits here (ALLOCATED = (1 << 2), EOF = (1 << 3)) and have a small function that translates BDRV_BLOCK_* values into them.

In any case, I don’t mind the sparseness too much, it’s just that it shouldn’t go on the stack.

Hanna

Signature · 2 lines
Best regards,
Vladimir
#6 lore raw

Re: [PATCH v2 1/4] qemu-img: implement compare --stat

Quoted 73 lines · Vladimir Sementsov-Ogievskiy
Quoted 72 lines · Hanna Reitz
Quoted 62 lines · Vladimir Sementsov-Ogievskiy
Quoted 61 lines · Hanna Reitz
Quoted 34 lines · Vladimir Sementsov-Ogievskiy

With new option qemu-img compare will not stop at first mismatch, but instead calculate statistics: how many clusters with different data, how many clusters with equal data, how many clusters were unallocated but become data and so on.

We compare images chunk by chunk. Chunk size depends on what block_status returns for both images. It may return less than cluster (remember about qcow2 subclusters), it may return more than cluster (if several consecutive clusters share same status). Finally images may have different cluster sizes. This all leads to ambiguity in how to finally compare the data.

What we can say for sure is that, when we compare two qcow2 images with same cluster size, we should compare clusters with data separately. Otherwise, if we for example compare 10 consecutive clusters of data where only one byte differs we'll report 10 different clusters. Expected result in this case is 1 different cluster and 9 equal ones.

So, to serve this case and just to have some defined rule let's do the following:

1. Select some block-size for compare procedure. In this commit it must be specified by user, next commit will add some automatic logic and make --block-size optional.

2. Go chunk-by-chunk using block_status as we do now with only one differency:
    If block_status() returns DATA region that intersects block-size aligned boundary, crop this region at this boundary.

This way it's still possible to compare less than cluster and report subcluster-level accuracy, but we newer compare more than one cluster of data.

Quoted 7 lines

---
  docs/tools/qemu-img.rst |  18 +++-
  qemu-img.c              | 206
+++++++++++++++++++++++++++++++++++++---
  qemu-img-cmds.hx        |   4 +-

  3 files changed, 212 insertions(+), 16 deletions(-)

Looks good to me overall!  Just some technical comments below.

Quoted 5 lines
docs/tools/qemu-img.rst +0 -0
index d58980aef8..21164253d4 100644
@@ -159,6 +159,18 @@ Parameters to compare subcommand:
Quoted 8 lines

    Strict mode - fail on different image size or sector allocation +.. option:: --stat
+
+  Instead of exit on first mismatch compare the whole images and print
+  statistics on amount of different pairs of clusters, based on their
+  block-status and are they equal or not.

I’d phrase this as:

Quoted 3 lines
Quoted 1 line

Instead of exiting on the first mismatch, compare the whole images and print statistics on how much they differ in terms of block status (i.e. are blocks allocated or not, do they contain data, are they marked as containing only zeroes) and block content (a block of data that contains only zero still has the same content as a marked-zero block).

For me the rest starting from "and block content" sounds unclear, seems doesn't add any information to previous (i.e. are blocks allocated ...)

By “block content” I meant what you said by “equal or not”, i.e. what is returned when reading from the block.

Now that I think about it again, I believe we should go with your original “equal or not”, though, because that reflects what qemu-img --stat prints, like so perhaps:

Quoted 8 lines
Quoted 1 line

Instead of exiting on the first mismatch, compare the whole images and print statistics on the amount of different pairs of blocks, based on their block status and whether they are equal or not.

OK

Quoted 2 lines

I’d still like to add something like what I had in parentheses, though, because as a user, I’d find the “block status” and “equal or not” terms to be a bit handwave-y.  I don’t think “block status” is a common term in our documentation, so I wanted to add some examples; and I wanted to show by example that “equal” blocks don’t need to have the same block status.

Actually, I think, that the resulting tool is anyway developer-oriented, to use it you should know what this block status really means.. May be just s/block status/block type/, will it be more human friendly?

Oh, OK.  I think I’d prefer “block status” still, because that’s what we use in the code.

Hanna

#7 lore raw

PATCH v2 2/4 qemu-img: make --block-size optional for compare --stat

In reply to Vladimir Sementsov-Ogievskiy

Let's detect block-size automatically if not specified by user:

If both files define cluster-size, use minimum to be more precise. If both files don't specify cluster-size, use default of 64K If only one file specify cluster-size, just use it.

---
 docs/tools/qemu-img.rst |  7 +++-
 qemu-img.c              | 71 ++++++++++++++++++++++++++++++++++++-----
 qemu-img-cmds.hx        |  4 +--
 3 files changed, 71 insertions(+), 11 deletions(-)
docs/tools/qemu-img.rst +6 -1
index 21164253d4..4b382ca2b0 100644
@@ -170,6 +170,11 @@ Parameters to compare subcommand:
   Block size for comparing with ``--stat``. This doesn't guarantee exact
   size of comparing chunks, but that guarantee that data chunks being
   compared will never cross aligned block-size boundary.
+  When unspecified the following logic is used:
+
+    - If both files define cluster-size, use minimum to be more precise.
+    - If both files don't specify cluster-size, use default of 64K
+    - If only one file specify cluster-size, just use it.
 
 Parameters to convert subcommand:
 
@@ -390,7 +395,7 @@ Command description:
 
   The rate limit for the commit process is specified by ``-r``.
 
-.. option:: compare [--object OBJECTDEF] [--image-opts] [-f FMT] [-F FMT] [-T SRC_CACHE] [-p] [-q] [-s] [-U] [--stat --block-size BLOCK_SIZE] FILENAME1 FILENAME2
+.. option:: compare [--object OBJECTDEF] [--image-opts] [-f FMT] [-F FMT] [-T SRC_CACHE] [-p] [-q] [-s] [-U] [--stat [--block-size BLOCK_SIZE]] FILENAME1 FILENAME2
 
   Check if two images have the same content. You can compare images with
   different format or settings.
qemu-img.c +63 -8

89 lines not on this page · raw message

qemu-img-cmds.hx +2 -2
index 0b2d63ca5f..96a193eea8 100644
@@ -40,9 +40,9 @@ SRST
 ERST
 
 DEF("compare", img_compare,
-    "compare [--object objectdef] [--image-opts] [-f fmt] [-F fmt] [-T src_cache] [-p] [-q] [-s] [-U] [--stat --block-size BLOCK_SIZE] filename1 filename2")
+    "compare [--object objectdef] [--image-opts] [-f fmt] [-F fmt] [-T src_cache] [-p] [-q] [-s] [-U] [--stat [--block-size BLOCK_SIZE]] filename1 filename2")
 SRST
-.. option:: compare [--object OBJECTDEF] [--image-opts] [-f FMT] [-F FMT] [-T SRC_CACHE] [-p] [-q] [-s] [-U] [--stat --block-size BLOCK_SIZE] FILENAME1 FILENAME2
+.. option:: compare [--object OBJECTDEF] [--image-opts] [-f FMT] [-F FMT] [-T SRC_CACHE] [-p] [-q] [-s] [-U] [--stat [--block-size BLOCK_SIZE]] FILENAME1 FILENAME2
 ERST
 
 DEF("convert", img_convert,
Signature · 1 line
2.31.1
#8 lore raw

Re: [PATCH v2 2/4] qemu-img: make --block-size optional for compare --stat

Quoted 6 lines · Vladimir Sementsov-Ogievskiy

Let's detect block-size automatically if not specified by user:

If both files define cluster-size, use minimum to be more precise. If both files don't specify cluster-size, use default of 64K If only one file specify cluster-size, just use it.

Quoted 6 lines
---
  docs/tools/qemu-img.rst |  7 +++-
  qemu-img.c              | 71 ++++++++++++++++++++++++++++++++++++-----
  qemu-img-cmds.hx        |  4 +--
  3 files changed, 71 insertions(+), 11 deletions(-)

Looks good, just grammar nits and a request for an assertion below.

docs/tools/qemu-img.rst Parameters to compare subcommand: · line 177

Quoted 13 lines
docs/tools/qemu-img.rst +5 -0
index 21164253d4..4b382ca2b0 100644
@@ -170,6 +170,11 @@ Parameters to compare subcommand:
    Block size for comparing with ``--stat``. This doesn't guarantee exact
    size of comparing chunks, but that guarantee that data chunks being
    compared will never cross aligned block-size boundary.
+  When unspecified the following logic is used:
+
+    - If both files define cluster-size, use minimum to be more precise.
+    - If both files don't specify cluster-size, use default of 64K
+    - If only one file specify cluster-size, just use it.

s/specify/specifies/; and perhaps s/it/that/

[...]

Quoted 26 lines
qemu-img.c +18 -0
index 79a0589167..61e7f470bb 100644
@@ -1407,6 +1407,61 @@ static void cmp_stat_print(ImgCmpStat *stat, int64_t total_bytes)
      }
  }
 
+/* Get default value for qemu-img compare --block-size option. */
+static int img_compare_block_size(BlockDriverState *bs1,
+                                  BlockDriverState *bs2,
+                                  bool quiet)
+{
+    const int default_block_size = 64 * 1024; /* 64K */
+
+    int ret;
+    BlockDriverInfo bdi;
+    int cluster_size1, cluster_size2, block_size;
+    const char *note = "Note: to alter it, set --block-size option.";
+    const char *fname1 = bs1->filename;
+    const char *fname2 = bs2->filename;
+
+    ret = bdrv_get_info(bs1, &bdi);
+    if (ret < 0 && ret != -ENOTSUP) {
+        error_report("Failed to get info of %s: %s", fname1, strerror(-ret));
+        return ret;

qemu-img.c static void cmp_stat_print(ImgCmpStat *stat, int64_t total_bytes) · line 1437

Quoted 10 lines
qemu-img.c +10 -0
+    }
+    cluster_size1 = ret < 0 ? 0 : bdi.cluster_size;
+
+    ret = bdrv_get_info(bs2, &bdi);
+    if (ret < 0 && ret != -ENOTSUP) {
+        error_report("Failed to get info of %s: %s", fname2, strerror(-ret));
+        return ret;
+    }
+    cluster_size2 = ret < 0 ? 0 : bdi.cluster_size;
+

I’d feel better with an assert(cluster_size1 >= 0 && cluster_size2 >= 0); here.

qemu-img.c static void cmp_stat_print(ImgCmpStat *stat, int64_t total_bytes) · line 1443

Quoted 6 lines
qemu-img.c +6 -0
+    if (cluster_size1 > 0 && cluster_size2 > 0) {
+        if (cluster_size1 == cluster_size2) {
+            block_size = cluster_size1;
+        } else {
+            block_size = MIN(cluster_size1, cluster_size2);
+            qprintf(quiet, "%s and %s has different cluster sizes: %d and %d "

s/has/have/

qemu-img.c static void cmp_stat_print(ImgCmpStat *stat, int64_t total_bytes) · line 1444

Quoted 1 line
qemu-img.c +1 -0
+                    "correspondingly. Use minimum as block-size for "

s/correspondingly/respectively/; s/Use/Using/ (“Use” sounds like an imperative)

qemu-img.c static void cmp_stat_print(ImgCmpStat *stat, int64_t total_bytes) · line 1451

Quoted 7 lines
qemu-img.c +7 -0
+                    "accuracy: %d. %s\n",
+                    fname1, fname2, cluster_size1,
+                    cluster_size2, block_size, note);
+        }
+    } else if (cluster_size1 == 0 && cluster_size2 == 0) {
+        block_size = default_block_size;
+        qprintf(quiet, "Neither of %s and %s has explicit cluster size. Use "

s/has/have an/; s/Use/Using/

qemu-img.c static void cmp_stat_print(ImgCmpStat *stat, int64_t total_bytes) · line 1455

Quoted 4 lines
qemu-img.c +4 -0
+                "default of %d bytes. %s\n", fname1, fname2, block_size, note);
+    } else {
+        block_size = MAX(cluster_size1, cluster_size2);
+        qprintf(quiet, "%s has explicit cluster size of %d and %s "

s/has/has an/

qemu-img.c static void cmp_stat_print(ImgCmpStat *stat, int64_t total_bytes) · line 1456

Quoted 1 line
qemu-img.c +1 -0
+                "doesn't have one. Use %d as block-size. %s\n",

s/Use/Using/

Hanna

Quoted 11 lines
qemu-img.c +8 -0
+                cluster_size1 ? fname1 : fname2, block_size,
+                cluster_size1 ? fname2 : fname1,
+                block_size, note);
+    }
+
+    return block_size;
+}
+
  /*
   * Compares two images. Exit codes:
   *
#9 lore raw

Re: [PATCH v2 2/4] qemu-img: make --block-size optional for compare --stat

Quoted 63 lines · Hanna Reitz
Quoted 6 lines · Vladimir Sementsov-Ogievskiy

Let's detect block-size automatically if not specified by user:

  If both files define cluster-size, use minimum to be more precise. If both files don't specify cluster-size, use default of 64K If only one file specify cluster-size, just use it.

Quoted 6 lines

---
  docs/tools/qemu-img.rst |  7 +++-
  qemu-img.c              | 71 ++++++++++++++++++++++++++++++++++++----- qemu-img-cmds.hx        |  4 +--

  3 files changed, 71 insertions(+), 11 deletions(-)

Looks good, just grammar nits and a request for an assertion below.

Quoted 5 lines
docs/tools/qemu-img.rst +0 -0
index 21164253d4..4b382ca2b0 100644
@@ -170,6 +170,11 @@ Parameters to compare subcommand:
Quoted 8 lines

    Block size for comparing with ``--stat``. This doesn't guarantee exact size of comparing chunks, but that guarantee that data chunks being compared will never cross aligned block-size boundary.
+  When unspecified the following logic is used:
+
+    - If both files define cluster-size, use minimum to be more precise. +    - If both files don't specify cluster-size, use default of 64K +    - If only one file specify cluster-size, just use it.

s/specify/specifies/; and perhaps s/it/that/

[...]

Quoted 29 lines
qemu-img.c +22 -0
index 79a0589167..61e7f470bb 100644
@@ -1407,6 +1407,61 @@ static void cmp_stat_print(ImgCmpStat *stat, int64_t total_bytes)
       }
   }
+/* Get default value for qemu-img compare --block-size option. */
+static int img_compare_block_size(BlockDriverState *bs1,
+                                  BlockDriverState *bs2,
+                                  bool quiet)
+{
+    const int default_block_size = 64 * 1024; /* 64K */
+
+    int ret;
+    BlockDriverInfo bdi;
+    int cluster_size1, cluster_size2, block_size;
+    const char *note = "Note: to alter it, set --block-size option.";
+    const char *fname1 = bs1->filename;
+    const char *fname2 = bs2->filename;
+
+    ret = bdrv_get_info(bs1, &bdi);
+    if (ret < 0 && ret != -ENOTSUP) {
+        error_report("Failed to get info of %s: %s", fname1, strerror(-ret));
+        return ret;
+    }
+    cluster_size1 = ret < 0 ? 0 : bdi.cluster_size;
+
+    ret = bdrv_get_info(bs2, &bdi);
Quoted 8 lines

qemu-img.c static void cmp_stat_print(ImgCmpStat *stat, int64_t total_bytes) · line 1437

Quoted 6 lines
qemu-img.c +6 -0
+    if (ret < 0 && ret != -ENOTSUP) {
+        error_report("Failed to get info of %s: %s", fname2, strerror(-ret));
+        return ret;
+    }
+    cluster_size2 = ret < 0 ? 0 : bdi.cluster_size;
+

I’d feel better with an assert(cluster_size1 >= 0 && cluster_size2 >= 0); here.

Hmm.. But it seems obvious: bdi.cluster_size should not be <0 on success of bdrv_get_info.

Quoted 49 lines

qemu-img.c static void cmp_stat_print(ImgCmpStat *stat, int64_t total_bytes) · line 1443

Quoted 7 lines
qemu-img.c +6 -0
+    if (cluster_size1 > 0 && cluster_size2 > 0) {
+        if (cluster_size1 == cluster_size2) {
+            block_size = cluster_size1;
+        } else {
+            block_size = MIN(cluster_size1, cluster_size2);
+            qprintf(quiet, "%s and %s has different cluster sizes: %d and %d "

s/has/have/

qemu-img.c static void cmp_stat_print(ImgCmpStat *stat, int64_t total_bytes) · line 1444

Quoted 1 line
qemu-img.c +1 -0
+                    "correspondingly. Use minimum as block-size for "

s/correspondingly/respectively/; s/Use/Using/ (“Use” sounds like an imperative)

qemu-img.c static void cmp_stat_print(ImgCmpStat *stat, int64_t total_bytes) · line 1451

Quoted 7 lines
qemu-img.c +7 -0
+                    "accuracy: %d. %s\n",
+                    fname1, fname2, cluster_size1,
+                    cluster_size2, block_size, note);
+        }
+    } else if (cluster_size1 == 0 && cluster_size2 == 0) {
+        block_size = default_block_size;
+        qprintf(quiet, "Neither of %s and %s has explicit cluster size. Use "

s/has/have an/; s/Use/Using/

qemu-img.c static void cmp_stat_print(ImgCmpStat *stat, int64_t total_bytes) · line 1455

Quoted 4 lines
qemu-img.c +4 -0
+                "default of %d bytes. %s\n", fname1, fname2, block_size, note);
+    } else {
+        block_size = MAX(cluster_size1, cluster_size2);
+        qprintf(quiet, "%s has explicit cluster size of %d and %s "

s/has/has an/

qemu-img.c static void cmp_stat_print(ImgCmpStat *stat, int64_t total_bytes) · line 1456

Quoted 1 line
qemu-img.c +1 -0
+                "doesn't have one. Use %d as block-size. %s\n",

s/Use/Using/

Hanna

Quoted 11 lines
qemu-img.c +8 -0
+                cluster_size1 ? fname1 : fname2, block_size,
+                cluster_size1 ? fname2 : fname1,
+                block_size, note);
+    }
+
+    return block_size;
+}
+
   /*
    * Compares two images. Exit codes:
    *
Signature · 2 lines
Best regards,
Vladimir
#10 lore raw

PATCH v2 3/4 qemu-img: add --shallow option for qemu-img compare

In reply to Vladimir Sementsov-Ogievskiy

Allow compare only top images of backing chains. This is useful to compare images with same backing file or to compare incremental images from the chain of incremental backups with "--stat" option.

---
 docs/tools/qemu-img.rst | 9 ++++++++-
 qemu-img.c              | 8 ++++++--
 qemu-img-cmds.hx        | 4 ++--
 3 files changed, 16 insertions(+), 5 deletions(-)
docs/tools/qemu-img.rst +8 -1
index 4b382ca2b0..4ae9543472 100644
@@ -176,6 +176,13 @@ Parameters to compare subcommand:
     - If both files don't specify cluster-size, use default of 64K
     - If only one file specify cluster-size, just use it.
 
+.. option:: --shallow
+
+  This option prevents opening and comparing any backing files.
+  This is useful to compare images with same backing file or to compare
+  incremental images from the chain of incremental backups with
+  ``--stat`` option.
+
 Parameters to convert subcommand:
 
 .. program:: qemu-img-convert
@@ -395,7 +402,7 @@ Command description:
 
   The rate limit for the commit process is specified by ``-r``.
 
-.. option:: compare [--object OBJECTDEF] [--image-opts] [-f FMT] [-F FMT] [-T SRC_CACHE] [-p] [-q] [-s] [-U] [--stat [--block-size BLOCK_SIZE]] FILENAME1 FILENAME2
+.. option:: compare [--object OBJECTDEF] [--image-opts] [-f FMT] [-F FMT] [-T SRC_CACHE] [-p] [-q] [-s] [-U] [--stat [--block-size BLOCK_SIZE]] [--shallow] FILENAME1 FILENAME2
 
   Check if two images have the same content. You can compare images with
   different format or settings.
qemu-img.c +6 -2
index 61e7f470bb..c9b5067353 100644
@@ -85,6 +85,7 @@ enum {
     OPTION_SKIP_BROKEN = 277,
     OPTION_STAT = 277,
     OPTION_BLOCK_SIZE = 278,
+    OPTION_SHALLOW = 279,
 };
 
 typedef enum OutputFormat {
@@ -1482,7 +1483,7 @@ static int img_compare(int argc, char **argv)
     int64_t block_end;
     int ret = 0; /* return value - 0 Ident, 1 Different, >1 Error */
     bool progress = false, quiet = false, strict = false;
-    int flags;
+    int flags = 0;
     bool writethrough;
     int64_t total_size;
     int64_t offset = 0;
@@ -1504,6 +1505,7 @@ static int img_compare(int argc, char **argv)
             {"force-share", no_argument, 0, 'U'},
             {"stat", no_argument, 0, OPTION_STAT},
             {"block-size", required_argument, 0, OPTION_BLOCK_SIZE},
+            {"shallow", no_argument, 0, OPTION_SHALLOW},
             {0, 0, 0, 0}
         };
         c = getopt_long(argc, argv, ":hf:F:T:pqsU",
@@ -1569,6 +1571,9 @@ static int img_compare(int argc, char **argv)
                 exit(EXIT_SUCCESS);
             }
             break;
+        case OPTION_SHALLOW:
+            flags |= BDRV_O_NO_BACKING;
+            break;
         }
     }
 
@@ -1593,7 +1598,6 @@ static int img_compare(int argc, char **argv)
     /* Initialize before goto out */
     qemu_progress_init(progress, 2.0);
 
-    flags = 0;
     ret = bdrv_parse_cache_mode(cache, &flags, &writethrough);
     if (ret < 0) {
         error_report("Invalid source cache option: %s", cache);
qemu-img-cmds.hx +2 -2
index 96a193eea8..6b164767fd 100644
@@ -40,9 +40,9 @@ SRST
 ERST
 
 DEF("compare", img_compare,
-    "compare [--object objectdef] [--image-opts] [-f fmt] [-F fmt] [-T src_cache] [-p] [-q] [-s] [-U] [--stat [--block-size BLOCK_SIZE]] filename1 filename2")
+    "compare [--object objectdef] [--image-opts] [-f fmt] [-F fmt] [-T src_cache] [-p] [-q] [-s] [-U] [--stat [--block-size BLOCK_SIZE]] [--shallow] filename1 filename2")
 SRST
-.. option:: compare [--object OBJECTDEF] [--image-opts] [-f FMT] [-F FMT] [-T SRC_CACHE] [-p] [-q] [-s] [-U] [--stat [--block-size BLOCK_SIZE]] FILENAME1 FILENAME2
+.. option:: compare [--object OBJECTDEF] [--image-opts] [-f FMT] [-F FMT] [-T SRC_CACHE] [-p] [-q] [-s] [-U] [--stat [--block-size BLOCK_SIZE]] [--shallow] FILENAME1 FILENAME2
 ERST
 
 DEF("convert", img_convert,
Signature · 1 line
2.31.1
Reviewed-by on v2 3/4
#11 lore raw

Re: [PATCH v2 3/4] qemu-img: add --shallow option for qemu-img compare

Quoted 10 lines · Vladimir Sementsov-Ogievskiy

Allow compare only top images of backing chains. This is useful to compare images with same backing file or to compare incremental images from the chain of incremental backups with "--stat" option.

---
  docs/tools/qemu-img.rst | 9 ++++++++-
  qemu-img.c              | 8 ++++++--
  qemu-img-cmds.hx        | 4 ++--
  3 files changed, 16 insertions(+), 5 deletions(-)
#12 lore raw

PATCH v2 4/4 iotests: add qemu-img-compare-stat test

In reply to Vladimir Sementsov-Ogievskiy

Test new feature qemu-img compare --stat.

---
 .../qemu-iotests/tests/qemu-img-compare-stat  |  88 +++++++++++++++
 .../tests/qemu-img-compare-stat.out           | 106 ++++++++++++++++++
 2 files changed, 194 insertions(+)
 create mode 100755 tests/qemu-iotests/tests/qemu-img-compare-stat
 create mode 100644 tests/qemu-iotests/tests/qemu-img-compare-stat.out
tests/qemu-iotests/tests/qemu-img-compare-stat new file +88 -0
new file mode 100755
index 0000000000..e2c0bcc7ef
@@ -0,0 +1,88 @@
+#!/usr/bin/env python3
+#
+# Test qemu-img compare --stat
+#
+# Copyright (c) 2021 Virtuozzo International GmbH.
+#
+# This program is free software; you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation; either version 2 of the License, or
+# (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with this program.  If not, see <http://www.gnu.org/licenses/>.
+#
+
+import iotests
+from iotests import qemu_img_create, qemu_io, qemu_img_log, log
+
+iotests.script_initialize(supported_fmts=['qcow2'])
+
+a, b, c = iotests.file_path('a', 'b', 'c')
+
+log('= compare two images =\n')
+
+qemu_img_create('-f', iotests.imgfmt, a, '1M')
+qemu_img_create('-f', iotests.imgfmt, b, '1M')
+
+# equal data and zero
+qemu_io('-c', 'write -z 0 64K', a)
+qemu_io('-c', 'write -P 0 0 64K', b)
+
+# different data
+qemu_io('-c', 'write -P 1 64K 64K', a)
+qemu_io('-c', 'write -P 0 64K 64K', b)
+
+# equal data
+qemu_io('-c', 'write -P 2 128K 64K', a)
+qemu_io('-c', 'write -P 2 128K 64K', b)
+
+# equal unallocated and allocated zero
+qemu_io('-c', 'write -z 192K 64K', b)
+
+# unequal data and unallocated zero
+qemu_io('-c', 'write -P 3 256K 64K', a)
+
+qemu_img_log('compare', '--stat', a, b)
+
+log('\n= compare two increments =\n')
+
+qemu_img_create('-f', iotests.imgfmt, a, '1M')
+qemu_img_create('-f', iotests.imgfmt, '-b', a, '-F', iotests.imgfmt, b, '1M')
+qemu_img_create('-f', iotests.imgfmt, '-b', b, '-F', iotests.imgfmt, c, '1M')
+
+qemu_io('-c', 'write -P 1 0 1M', a)
+qemu_io('-c', 'write -P 2 0 64K', b)
+qemu_io('-c', 'write -P 3 64K 64K', c)
+qemu_img_log('compare', '--stat', b, c)
+
+log('\n= compare two increments with --shallow=\n')
+qemu_img_log('compare', '--stat', '--shallow', b, c)
+
+log('\n= compare images of different size =\n')
+qemu_img_create('-f', iotests.imgfmt, a, '1M')
+qemu_img_create('-f', iotests.imgfmt, b, '2M')
+qemu_io('-c', 'write -P 1 0 1M', a)
+qemu_io('-c', 'write -P 2 0 1M', b)
+qemu_io('-c', 'write -P 1 1M 64K', b)
+qemu_io('-c', f'write -z {1024 + 64 * 2}K 64K', b)
+qemu_io('-c', f'write -P 0 {1024 + 64 * 3}K 64K', b)
+qemu_img_log('compare', '--stat', a, b)
+
+log('\n= compare images with only 512 bytes different =\n')
+qemu_img_create('-f', iotests.imgfmt, a, '1M')
+qemu_img_create('-f', iotests.imgfmt, b, '1M')
+qemu_io('-c', 'write -P 1 0 1M', a)
+qemu_io('-c', 'write -P 2 0 512', b)
+qemu_io('-c', f'write -P 1 512 {1024 * 1024 - 512}', b)
+qemu_img_log('compare', '--stat', a, b)
+
+log('\n= compare images with only 512 bytes different, block-size=4K =\n')
+qemu_img_log('compare', '--stat', '--block-size', '4K', a, b)
+
+log('\n= end =')
tests/qemu-iotests/tests/qemu-img-compare-stat.out new file +106 -0

106 lines not on this page · raw message

Signature · 1 line
2.31.1
Reviewed-by on v2 4/4
#13 lore raw

Re: [PATCH v2 4/4] iotests: add qemu-img-compare-stat test

Quoted 9 lines · Vladimir Sementsov-Ogievskiy

Test new feature qemu-img compare --stat.

---
  .../qemu-iotests/tests/qemu-img-compare-stat  |  88 +++++++++++++++
  .../tests/qemu-img-compare-stat.out           | 106 ++++++++++++++++++
  2 files changed, 194 insertions(+)

create mode 100755 tests/qemu-iotests/tests/qemu-img-compare-stat create mode 100644 tests/qemu-iotests/tests/qemu-img-compare-stat.out