diff mbox

[PATCHv2,1.8,8/9] qemu-img: add option to show progress in sectors

Message ID 1385456183-24840-9-git-send-email-pl@kamp.de
State New
Headers show

Commit Message

Peter Lieven Nov. 26, 2013, 8:56 a.m. UTC
Signed-off-by: Peter Lieven <pl@kamp.de>
---
 qemu-img-cmds.hx |    4 ++--
 qemu-img.c       |   31 ++++++++++++++++++++++++++++---
 qemu-img.texi    |    4 +++-
 3 files changed, 33 insertions(+), 6 deletions(-)

Comments

Paolo Bonzini Nov. 26, 2013, 10:04 a.m. UTC | #1
I think the right way to do this would be to add the functionality to
qemu-progress.c (i.e. pass a number of sectors and let it choose between
printing % or sectors).

I don't like the qemu-progress.c API anyway, so a rewrite would be
welcome. :)

Paolo

Il 26/11/2013 09:56, Peter Lieven ha scritto:
> Signed-off-by: Peter Lieven <pl@kamp.de>
> ---
>  qemu-img-cmds.hx |    4 ++--
>  qemu-img.c       |   31 ++++++++++++++++++++++++++++---
>  qemu-img.texi    |    4 +++-
>  3 files changed, 33 insertions(+), 6 deletions(-)
> 
> diff --git a/qemu-img-cmds.hx b/qemu-img-cmds.hx
> index da1d965..6c8183b 100644
> --- a/qemu-img-cmds.hx
> +++ b/qemu-img-cmds.hx
> @@ -34,9 +34,9 @@ STEXI
>  ETEXI
>  
>  DEF("convert", img_convert,
> -    "convert [-c] [-p] [-q] [-n] [-f fmt] [-t cache] [-O output_fmt] [-o options] [-s snapshot_name] [-S sparse_size] filename [filename2 [...]] output_filename")
> +    "convert [-c] [-p|-pp] [-q] [-n] [-f fmt] [-t cache] [-O output_fmt] [-o options] [-s snapshot_name] [-S sparse_size] filename [filename2 [...]] output_filename")
>  STEXI
> -@item convert [-c] [-p] [-q] [-n] [-f @var{fmt}] [-t @var{cache}] [-O @var{output_fmt}] [-o @var{options}] [-s @var{snapshot_name}] [-S @var{sparse_size}] @var{filename} [@var{filename2} [...]] @var{output_filename}
> +@item convert [-c] [-p|-pp] [-q] [-n] [-f @var{fmt}] [-t @var{cache}] [-O @var{output_fmt}] [-o @var{options}] [-s @var{snapshot_name}] [-S @var{sparse_size}] @var{filename} [@var{filename2} [...]] @var{output_filename}
>  ETEXI
>  
>  DEF("info", img_info,
> diff --git a/qemu-img.c b/qemu-img.c
> index 1421f0f..cc7540f 100644
> --- a/qemu-img.c
> +++ b/qemu-img.c
> @@ -99,6 +99,7 @@ static void help(void)
>             "       rebasing in this case (useful for renaming the backing file)\n"
>             "  '-h' with or without a command shows this help and lists the supported formats\n"
>             "  '-p' show progress of command (only certain commands)\n"
> +           "  '-pp' show progress of command in sectors (only convert command)\n"
>             "  '-q' use Quiet mode - do not print any output (except errors)\n"
>             "  '-S' indicates the consecutive number of bytes (defaults to 4k) that must\n"
>             "       contain only zeros for qemu-img to create a sparse image during\n"
> @@ -1122,6 +1123,22 @@ out3:
>      return ret;
>  }
>  
> +static void print_sector_progress(int progress, int64_t sector_num,
> +                                  int64_t total_sectors)
> +{
> +    static int64_t last_sector = -1;
> +    if (progress == 2) {
> +        if (sector_num == 0 ||
> +            sector_num > last_sector + 0.02 * total_sectors ||
> +            sector_num == total_sectors) {
> +            printf("%ld of %ld sectors converted.\r", sector_num,
> +                   total_sectors);
> +            fflush(stdout);
> +            last_sector = sector_num;
> +        }
> +    }
> +}
> +
>  static int img_convert(int argc, char **argv)
>  {
>      int c, n, n1, bs_n, bs_i, compress, cluster_sectors, skip_create;
> @@ -1130,7 +1147,7 @@ static int img_convert(int argc, char **argv)
>      const char *fmt, *out_fmt, *cache, *out_baseimg, *out_filename;
>      BlockDriver *drv, *proto_drv;
>      BlockDriverState **bs = NULL, *out_bs = NULL;
> -    int64_t total_sectors, nb_sectors, sector_num, bs_offset,
> +    int64_t total_sectors = 0, nb_sectors, sector_num, bs_offset,
>              sector_num_next_status = 0;
>      uint64_t bs_sectors;
>      uint8_t * buf = NULL;
> @@ -1201,7 +1218,7 @@ static int img_convert(int argc, char **argv)
>              break;
>          }
>          case 'p':
> -            progress = 1;
> +            progress++;
>              break;
>          case 't':
>              cache = optarg;
> @@ -1227,7 +1244,7 @@ static int img_convert(int argc, char **argv)
>      out_filename = argv[argc - 1];
>  
>      /* Initialize before goto out */
> -    qemu_progress_init(progress, 2.0);
> +    qemu_progress_init(progress == 1, 2.0);
>  
>      if (options && is_help_option(options)) {
>          ret = print_block_option_help(out_filename, out_fmt);
> @@ -1258,6 +1275,8 @@ static int img_convert(int argc, char **argv)
>          total_sectors += bs_sectors;
>      }
>  
> +    print_sector_progress(progress, 0, total_sectors);
> +
>      if (snapshot_name != NULL) {
>          if (bs_n > 1) {
>              error_report("No support for concatenating multiple snapshot");
> @@ -1472,6 +1491,7 @@ static int img_convert(int argc, char **argv)
>              }
>              sector_num += n;
>              qemu_progress_print(100.0 * sector_num / total_sectors, 0);
> +            print_sector_progress(progress, sector_num, total_sectors);
>          }
>          /* signal EOF to align */
>          bdrv_write_compressed(out_bs, 0, NULL, 0);
> @@ -1587,11 +1607,16 @@ static int img_convert(int argc, char **argv)
>                  buf1 += n1 * 512;
>              }
>              qemu_progress_print(100.0 * sector_num / total_sectors, 0);
> +            print_sector_progress(progress, sector_num, total_sectors);
>          }
>      }
>  out:
>      if (!ret) {
>          qemu_progress_print(100, 0);
> +        print_sector_progress(progress, total_sectors, total_sectors);
> +    }
> +    if (progress == 2) {
> +        printf("\n");
>      }
>      qemu_progress_end();
>      free_option_parameters(create_options);
> diff --git a/qemu-img.texi b/qemu-img.texi
> index da36975..cb4a3eb 100644
> --- a/qemu-img.texi
> +++ b/qemu-img.texi
> @@ -54,6 +54,8 @@ indicates that target image must be compressed (qcow format only)
>  with or without a command shows help and lists the supported formats
>  @item -p
>  display progress bar (convert and rebase commands only)
> +@item -pp
> +display progress in sectors (convert command only)
>  @item -q
>  Quiet mode - do not print any output (except errors). There's no progress bar
>  in case both @var{-q} and @var{-p} options are used.
> @@ -179,7 +181,7 @@ Error on reading data
>  
>  @end table
>  
> -@item convert [-c] [-p] [-n] [-f @var{fmt}] [-t @var{cache}] [-O @var{output_fmt}] [-o @var{options}] [-s @var{snapshot_name}] [-S @var{sparse_size}] @var{filename} [@var{filename2} [...]] @var{output_filename}
> +@item convert [-c] [-p|-pp] [-n] [-f @var{fmt}] [-t @var{cache}] [-O @var{output_fmt}] [-o @var{options}] [-s @var{snapshot_name}] [-S @var{sparse_size}] @var{filename} [@var{filename2} [...]] @var{output_filename}
>  
>  Convert the disk image @var{filename} or a snapshot @var{snapshot_name} to disk image @var{output_filename}
>  using format @var{output_fmt}. It can be optionally compressed (@code{-c}
>
Peter Lieven Nov. 26, 2013, 12:23 p.m. UTC | #2
On 26.11.2013 11:04, Paolo Bonzini wrote:
> I think the right way to do this would be to add the functionality to
> qemu-progress.c (i.e. pass a number of sectors and let it choose between
> printing % or sectors).
I was thinking about the same, but is this not beyond the scope of this patch? ;-)
I don't mind leaving this patch out if you or the maintainers are strongly against it.
I just need it internally to show the progress of a convert operation. I could
theoretically calculate this the information I need also from the percentage output.
Its a little bit more complicated if more than one hard drive is converted.

Peter
Paolo Bonzini Nov. 26, 2013, 12:40 p.m. UTC | #3
Il 26/11/2013 13:23, Peter Lieven ha scritto:
>> I think the right way to do this would be to add the functionality to
>> qemu-progress.c (i.e. pass a number of sectors and let it choose between
>> printing % or sectors).
> I was thinking about the same, but is this not beyond the scope of this
> patch? ;-)

I think the functionality is not important enough to warrant more code
in qemu-img.c (even 20 lines is already too much).  We should improve
the utility libraries instead.

> I don't mind leaving this patch out if you or the maintainers are
> strongly against it.

Yes, please leave it out.

Paolo
diff mbox

Patch

diff --git a/qemu-img-cmds.hx b/qemu-img-cmds.hx
index da1d965..6c8183b 100644
--- a/qemu-img-cmds.hx
+++ b/qemu-img-cmds.hx
@@ -34,9 +34,9 @@  STEXI
 ETEXI
 
 DEF("convert", img_convert,
-    "convert [-c] [-p] [-q] [-n] [-f fmt] [-t cache] [-O output_fmt] [-o options] [-s snapshot_name] [-S sparse_size] filename [filename2 [...]] output_filename")
+    "convert [-c] [-p|-pp] [-q] [-n] [-f fmt] [-t cache] [-O output_fmt] [-o options] [-s snapshot_name] [-S sparse_size] filename [filename2 [...]] output_filename")
 STEXI
-@item convert [-c] [-p] [-q] [-n] [-f @var{fmt}] [-t @var{cache}] [-O @var{output_fmt}] [-o @var{options}] [-s @var{snapshot_name}] [-S @var{sparse_size}] @var{filename} [@var{filename2} [...]] @var{output_filename}
+@item convert [-c] [-p|-pp] [-q] [-n] [-f @var{fmt}] [-t @var{cache}] [-O @var{output_fmt}] [-o @var{options}] [-s @var{snapshot_name}] [-S @var{sparse_size}] @var{filename} [@var{filename2} [...]] @var{output_filename}
 ETEXI
 
 DEF("info", img_info,
diff --git a/qemu-img.c b/qemu-img.c
index 1421f0f..cc7540f 100644
--- a/qemu-img.c
+++ b/qemu-img.c
@@ -99,6 +99,7 @@  static void help(void)
            "       rebasing in this case (useful for renaming the backing file)\n"
            "  '-h' with or without a command shows this help and lists the supported formats\n"
            "  '-p' show progress of command (only certain commands)\n"
+           "  '-pp' show progress of command in sectors (only convert command)\n"
            "  '-q' use Quiet mode - do not print any output (except errors)\n"
            "  '-S' indicates the consecutive number of bytes (defaults to 4k) that must\n"
            "       contain only zeros for qemu-img to create a sparse image during\n"
@@ -1122,6 +1123,22 @@  out3:
     return ret;
 }
 
+static void print_sector_progress(int progress, int64_t sector_num,
+                                  int64_t total_sectors)
+{
+    static int64_t last_sector = -1;
+    if (progress == 2) {
+        if (sector_num == 0 ||
+            sector_num > last_sector + 0.02 * total_sectors ||
+            sector_num == total_sectors) {
+            printf("%ld of %ld sectors converted.\r", sector_num,
+                   total_sectors);
+            fflush(stdout);
+            last_sector = sector_num;
+        }
+    }
+}
+
 static int img_convert(int argc, char **argv)
 {
     int c, n, n1, bs_n, bs_i, compress, cluster_sectors, skip_create;
@@ -1130,7 +1147,7 @@  static int img_convert(int argc, char **argv)
     const char *fmt, *out_fmt, *cache, *out_baseimg, *out_filename;
     BlockDriver *drv, *proto_drv;
     BlockDriverState **bs = NULL, *out_bs = NULL;
-    int64_t total_sectors, nb_sectors, sector_num, bs_offset,
+    int64_t total_sectors = 0, nb_sectors, sector_num, bs_offset,
             sector_num_next_status = 0;
     uint64_t bs_sectors;
     uint8_t * buf = NULL;
@@ -1201,7 +1218,7 @@  static int img_convert(int argc, char **argv)
             break;
         }
         case 'p':
-            progress = 1;
+            progress++;
             break;
         case 't':
             cache = optarg;
@@ -1227,7 +1244,7 @@  static int img_convert(int argc, char **argv)
     out_filename = argv[argc - 1];
 
     /* Initialize before goto out */
-    qemu_progress_init(progress, 2.0);
+    qemu_progress_init(progress == 1, 2.0);
 
     if (options && is_help_option(options)) {
         ret = print_block_option_help(out_filename, out_fmt);
@@ -1258,6 +1275,8 @@  static int img_convert(int argc, char **argv)
         total_sectors += bs_sectors;
     }
 
+    print_sector_progress(progress, 0, total_sectors);
+
     if (snapshot_name != NULL) {
         if (bs_n > 1) {
             error_report("No support for concatenating multiple snapshot");
@@ -1472,6 +1491,7 @@  static int img_convert(int argc, char **argv)
             }
             sector_num += n;
             qemu_progress_print(100.0 * sector_num / total_sectors, 0);
+            print_sector_progress(progress, sector_num, total_sectors);
         }
         /* signal EOF to align */
         bdrv_write_compressed(out_bs, 0, NULL, 0);
@@ -1587,11 +1607,16 @@  static int img_convert(int argc, char **argv)
                 buf1 += n1 * 512;
             }
             qemu_progress_print(100.0 * sector_num / total_sectors, 0);
+            print_sector_progress(progress, sector_num, total_sectors);
         }
     }
 out:
     if (!ret) {
         qemu_progress_print(100, 0);
+        print_sector_progress(progress, total_sectors, total_sectors);
+    }
+    if (progress == 2) {
+        printf("\n");
     }
     qemu_progress_end();
     free_option_parameters(create_options);
diff --git a/qemu-img.texi b/qemu-img.texi
index da36975..cb4a3eb 100644
--- a/qemu-img.texi
+++ b/qemu-img.texi
@@ -54,6 +54,8 @@  indicates that target image must be compressed (qcow format only)
 with or without a command shows help and lists the supported formats
 @item -p
 display progress bar (convert and rebase commands only)
+@item -pp
+display progress in sectors (convert command only)
 @item -q
 Quiet mode - do not print any output (except errors). There's no progress bar
 in case both @var{-q} and @var{-p} options are used.
@@ -179,7 +181,7 @@  Error on reading data
 
 @end table
 
-@item convert [-c] [-p] [-n] [-f @var{fmt}] [-t @var{cache}] [-O @var{output_fmt}] [-o @var{options}] [-s @var{snapshot_name}] [-S @var{sparse_size}] @var{filename} [@var{filename2} [...]] @var{output_filename}
+@item convert [-c] [-p|-pp] [-n] [-f @var{fmt}] [-t @var{cache}] [-O @var{output_fmt}] [-o @var{options}] [-s @var{snapshot_name}] [-S @var{sparse_size}] @var{filename} [@var{filename2} [...]] @var{output_filename}
 
 Convert the disk image @var{filename} or a snapshot @var{snapshot_name} to disk image @var{output_filename}
 using format @var{output_fmt}. It can be optionally compressed (@code{-c}