[libvirt] [PATCHv2 5/6] virNodeGetCPUTime: Implement virsh support

Daniel P. Berrange berrange at redhat.com
Tue Apr 19 11:01:34 UTC 2011


On Fri, Apr 08, 2011 at 08:36:52PM +0900, Minoru Usui wrote:
> virNodeGetCPUTime: Implement virsh support
> 
> Add nodecputime subcommand to virsh.
> This subcommand prints below output.
> 
> [Linux]
>   # build/tools/virsh nodecputime
>   usage:            2.8%
>     user  :         0.8%
>     system:         1.9%
>     idle  :        97.2%
>     iowait:         0.0%
> 
> [can get cpu utilization directly(ESX?)]
>   # build/tools/virsh nodecputime
>   usage:            2.8%
> 
> Signed-off-by: Minoru Usui <usui at mxm.nes.nec.co.jp>
> ---
>  tools/virsh.c   |   96 +++++++++++++++++++++++++++++++++++++++++++++++++++++++
>  tools/virsh.pod |    4 ++
>  2 files changed, 100 insertions(+), 0 deletions(-)
> 
> diff --git a/tools/virsh.c b/tools/virsh.c
> index 19e3449..93288ba 100644
> --- a/tools/virsh.c
> +++ b/tools/virsh.c
> @@ -3388,6 +3388,101 @@ cmdNodeinfo(vshControl *ctl, const vshCmd *cmd ATTRIBUTE_UNUSED)
>  }
>  
>  /*
> + * "nodecputime" command
> + */
> +static const vshCmdInfo info_nodecputime[] = {
> +    {"help", N_("Prints cpu utilizatoin of the node.")},
> +    {"desc", N_("Returns cpu utilizatoin of the node.(%)")},
> +    {NULL, NULL}
> +};
> +
> +static int
> +cmdNodeCpuTime(vshControl *ctl, const vshCmd *cmd ATTRIBUTE_UNUSED)
> +{
> +    int i, j;
> +    int flag_utilization = FALSE;
> +    virNodeCpuTime stats[VIR_NODE_CPU_TIME_NR];
> +    int nr_stats;
> +    struct cpu_time {
> +        unsigned long long user;
> +        unsigned long long sys;
> +        unsigned long long idle;
> +        unsigned long long iowait;
> +        unsigned long long util;
> +    } cpu_time[2];
> +    double user_time, sys_time, idle_time, iowait_time, total_time;
> +    double usage;
> +
> +    if (!vshConnectionUsability(ctl, ctl->conn))
> +        return FALSE;
> +
> +    memset(cpu_time, 0, sizeof(struct cpu_time) * 2);
> +
> +    for (i = 0; i < 2; i++) {
> +        memset(stats, 0, sizeof(virNodeCpuTime) * VIR_NODE_CPU_TIME_NR);
> +        nr_stats = virNodeGetCpuTime(ctl->conn, &stats[0],
> +                                     VIR_NODE_CPU_TIME_NR, 0);
> +        if (nr_stats < 0) {
> +            vshError(ctl, "%s", _("failed to get cpu time of the node."));
> +            return FALSE;
> +        }
> +
> +        for (j = 0; j < nr_stats; j++) {
> +            switch (stats[j].tag) {
> +            case VIR_NODE_CPU_TIME_KERNEL:
> +                cpu_time[i].sys = stats[j].val;
> +                break;
> +            case VIR_NODE_CPU_TIME_USER:
> +                cpu_time[i].user = stats[j].val;
> +                break;
> +            case VIR_NODE_CPU_TIME_IDLE:
> +                cpu_time[i].idle = stats[j].val;
> +                break;
> +            case VIR_NODE_CPU_TIME_IOWAIT:
> +                cpu_time[i].iowait = stats[j].val;
> +                break;
> +            case VIR_NODE_CPU_TIME_UTILIZATION:
> +                flag_utilization = TRUE;
> +                cpu_time[i].util = stats[j].val;
> +                break;
> +            case VIR_NODE_CPU_TIME_NR:
> +            default:
> +                break;
> +            }
> +        }
> +
> +        sleep(1);
> +    }

I'm not sure about this bit. In other places in virsh, we just report
the absolute CPU time value, and don't try to calculate the deltas.
There is a virt-top program that might like to use this data to provide
a continuous display of utilization.

Alternatively, we could add an arg for this to request that it calculate
delta over 'n' seconds.

Daniel
-- 
|: http://berrange.com      -o-    http://www.flickr.com/photos/dberrange/ :|
|: http://libvirt.org              -o-             http://virt-manager.org :|
|: http://autobuild.org       -o-         http://search.cpan.org/~danberr/ :|
|: http://entangle-photo.org       -o-       http://live.gnome.org/gtk-vnc :|




More information about the libvir-list mailing list