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

Minoru Usui usui at mxm.nes.nec.co.jp
Fri Apr 8 11:36:52 UTC 2011


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);
+    }
+
+    if (flag_utilization == TRUE) {
+        usage  = cpu_time[0].util;
+    } else {
+        user_time   = cpu_time[1].user   - cpu_time[0].user;
+        sys_time    = cpu_time[1].sys    - cpu_time[0].sys;
+        idle_time   = cpu_time[1].idle   - cpu_time[0].idle;
+        iowait_time = cpu_time[1].iowait - cpu_time[0].iowait;
+        total_time  = user_time + sys_time + idle_time + iowait_time;
+
+        usage  = (user_time + sys_time) / total_time * 100;
+    }
+
+    vshPrint(ctl, "%-15s %5.1lf%%\n", _("usage:"), usage);
+
+    if (flag_utilization != TRUE) {
+        vshPrint(ctl, "%-15s %5.1lf%%\n",
+                 _("  user  :"), user_time   / total_time * 100);
+        vshPrint(ctl, "%-15s %5.1lf%%\n",
+                 _("  system:"), sys_time    / total_time * 100);
+        vshPrint(ctl, "%-15s %5.1lf%%\n",
+                 _("  idle  :"), idle_time   / total_time * 100);
+        vshPrint(ctl, "%-15s %5.1lf%%\n",
+                 _("  iowait:"), iowait_time / total_time * 100);
+    }
+
+    return TRUE;
+}
+
+/*
  * "capabilities" command
  */
 static const vshCmdInfo info_capabilities[] = {
@@ -10852,6 +10947,7 @@ static const vshCmdDef hostAndHypervisorCmds[] = {
     {"freecell", cmdFreecell, opts_freecell, info_freecell},
     {"hostname", cmdHostname, NULL, info_hostname},
     {"nodeinfo", cmdNodeinfo, NULL, info_nodeinfo},
+    {"nodecputime", cmdNodeCpuTime, NULL, info_nodecputime},
     {"qemu-monitor-command", cmdQemuMonitorCommand, opts_qemu_monitor_command, info_qemu_monitor_command},
     {"sysinfo", cmdSysinfo, NULL, info_sysinfo},
     {"uri", cmdURI, NULL, info_uri},
diff --git a/tools/virsh.pod b/tools/virsh.pod
index f4bd294..d3e7eab 100644
--- a/tools/virsh.pod
+++ b/tools/virsh.pod
@@ -237,6 +237,10 @@ Print the XML representation of the hypervisor sysinfo, if available.
 Returns basic information about the node, like number and type of CPU,
 and size of the physical memory.
 
+=item B<nodecputime>
+
+Returns cpu utilization of the node.
+
 =item B<capabilities>
 
 Print an XML document describing the capabilities of the hypervisor
-- 
1.7.1



-- 
Minoru Usui <usui at mxm.nes.nec.co.jp>




More information about the libvir-list mailing list