[libvirt] [PATCH] virsh-domain-monitor: add human readable output for 'domblkinfo'.

Michal Privoznik mprivozn at redhat.com
Tue May 2 07:50:20 UTC 2017


On 05/02/2017 12:35 AM, Julio Faracco wrote:
> The virsh command 'domblkinfo' returns the capacity, allocation and phisycal
> size of the devices attached in a domain. Usually, this sizes are very big
> and hard to understand and calculate. This commits introduce a human readable
> support to check the size of each field easilly.
> 
> For example, the command today:
> 
> virsh # domblkinfo my_domain hda
> Capacity:       21474836480
> Allocation:     14875545600
> Physical:       21474836480
> 
> After this patch:
> 
> virsh # domblkinfo my_domain hda --human
> Capacity:       20.0G
> Allocation:     13.9G
> Physical:       20.0G
> 
> This commit supports Bytes, KiB and MiB too.
> 
> Resolves: https://bugzilla.redhat.com/show_bug.cgi?id=1330940
> 
> Signed-off-by: Julio Faracco <jcfaracco at gmail.com>
> ---
>  tools/virsh-domain-monitor.c | 41 ++++++++++++++++++++++++++++++++++++++---
>  tools/virsh.pod              |  5 +++--
>  2 files changed, 41 insertions(+), 5 deletions(-)
> 
> diff --git a/tools/virsh-domain-monitor.c b/tools/virsh-domain-monitor.c
> index 3db4795..755d740 100644
> --- a/tools/virsh-domain-monitor.c
> +++ b/tools/virsh-domain-monitor.c
> @@ -396,6 +396,10 @@ static const vshCmdOptDef opts_domblkinfo[] = {
>       .flags = VSH_OFLAG_REQ,
>       .help = N_("block device")
>      },
> +    {.name = "human",
> +     .type = VSH_OT_BOOL,
> +     .help = N_("Human readable output")
> +    },
>      {.name = NULL}
>  };
>  
> @@ -405,6 +409,7 @@ cmdDomblkinfo(vshControl *ctl, const vshCmd *cmd)
>      virDomainBlockInfo info;
>      virDomainPtr dom;
>      bool ret = false;
> +    bool human = false;
>      const char *device = NULL;
>  
>      if (!(dom = virshCommandOptDomain(ctl, cmd, NULL)))
> @@ -416,9 +421,39 @@ cmdDomblkinfo(vshControl *ctl, const vshCmd *cmd)
>      if (virDomainGetBlockInfo(dom, device, &info, 0) < 0)
>          goto cleanup;
>  
> -    vshPrint(ctl, "%-15s %llu\n", _("Capacity:"), info.capacity);
> -    vshPrint(ctl, "%-15s %llu\n", _("Allocation:"), info.allocation);
> -    vshPrint(ctl, "%-15s %llu\n", _("Physical:"), info.physical);
> +    human = vshCommandOptBool(cmd, "human");
> +
> +    if (!human) {
> +        vshPrint(ctl, "%-15s %llu\n", _("Capacity:"), info.capacity);
> +        vshPrint(ctl, "%-15s %llu\n", _("Allocation:"), info.allocation);
> +        vshPrint(ctl, "%-15s %llu\n", _("Physical:"), info.physical);
> +    } else {
> +        double sizeCapacity = 1;
> +        const char *sizeStr = "B";
> +
> +        /* Check if capacity can be in K. */
> +        if (info.capacity >= 1024) {
> +            sizeCapacity = 1024.0;
> +            sizeStr = "K";
> +        }
> +        /* Check if capacity can be in M. */
> +        if (info.capacity >= 1048576) {
> +            sizeCapacity = 1048576.0;
> +            sizeStr = "M";
> +        }
> +        /* Check if capacity can be in G. */
> +        if (info.capacity >= 1073741824) {
> +            sizeCapacity = 1073741824.0;
> +            sizeStr = "G";
> +        }

There's no need for this complexity. We have vshPrettyCapacity() just
for that. You can look at cmdDomjobinfo() for example of its usage.

> +
> +        vshPrint(ctl, "%-15s %.1f%s\n", _("Capacity:"),

Here we prefer slightly different format: "%-.3lf":
- double is long float
- .3 for more digits after decimal point
- `-` for left alignment

> +                                info.capacity/sizeCapacity, sizeStr);
> +        vshPrint(ctl, "%-15s %.1f%s\n", _("Allocation:"),
> +                                info.allocation/sizeCapacity, sizeStr);
> +        vshPrint(ctl, "%-15s %.1f%s\n", _("Physical:"),
> +                                info.physical/sizeCapacity, sizeStr);
> +    }
>  
>      ret = true;
>  
> diff --git a/tools/virsh.pod b/tools/virsh.pod
> index e16f62f..9f67de2 100644
> --- a/tools/virsh.pod
> +++ b/tools/virsh.pod
> @@ -841,12 +841,13 @@ B<domstate> command says that a domain was paused due to I/O error.
>  The B<domblkerror> command lists all block devices in error state and
>  the error seen on each of them.
>  
> -=item B<domblkinfo> I<domain> I<block-device>
> +=item B<domblkinfo> I<domain> I<block-device> [I<--human>]
>  
>  Get block device size info for a domain.  A I<block-device> corresponds
>  to a unique target name (<target dev='name'/>) or source file (<source
>  file='name'/>) for one of the disk devices attached to I<domain> (see
> -also B<domblklist> for listing these names).
> +also B<domblklist> for listing these names). If I<--human> is set, the
> +output will have a human readable design.

I personally prefer "human readable output".

So, I'm fixing all the nits I've pointed out, ACKing and pushing.

Michal




More information about the libvir-list mailing list