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

Minoru Usui usui at mxm.nes.nec.co.jp
Thu Apr 14 10:11:28 UTC 2011


virNodeGetCPUTime: Implement virsh support

Add nodecputime subcommand to virsh.
This subcommand prints below output.

[Linux]
  # virsh nodecputime
  usage :           0.5%
      user  :       0.0%
      system:       0.5%
  idle  :          99.5%
  iowait:           0.0%

[can get cpu utilization directly(ESX?)]
  # virsh nodecputime
  usage :           0.5%
  idle  :          99.5%

Signed-off-by: Minoru Usui <usui at mxm.nes.nec.co.jp>
---
 tools/virsh.c   |   84 +++++++++++++++++++++++++++++++++++++++++++++++++++++++
 tools/virsh.pod |    4 ++
 2 files changed, 88 insertions(+), 0 deletions(-)

diff --git a/tools/virsh.c b/tools/virsh.c
index 2e35021..0d22a13 100644
--- a/tools/virsh.c
+++ b/tools/virsh.c
@@ -3427,6 +3427,89 @@ 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, sleep_done = FALSE;
+    unsigned long long stats[4];
+    int nr_stats;
+    unsigned int flags;
+    struct cpu_time {
+        unsigned long long user;
+        unsigned long long sys;
+        unsigned long long idle;
+        unsigned long long iowait;
+    } cpu_time[2];
+    double user_time, sys_time, idle_time, iowait_time, total_time;
+
+    if (!vshConnectionUsability(ctl, ctl->conn))
+        return FALSE;
+
+    /* Get cpu utilization directly */
+    memset(stats, 0, sizeof(unsigned long long) * 4);
+    flags = VIR_NODE_CPU_TIME_UTILIZATION;
+    nr_stats = virNodeGetCpuTime(ctl->conn, stats, 1, flags);
+    if (nr_stats > 0) {
+        vshPrint(ctl, "%-15s %5.1lf%%\n", _("usage:"), (double)stats[0]);
+        vshPrint(ctl, "%-15s %5.1lf%%\n", _("idle :"), 100 - (double)stats[0]);
+        return TRUE;
+    }
+
+    /* Calculate cpu utilization by getting cpu time two times */
+    memset(cpu_time, 0, sizeof(struct cpu_time) * 2);
+    for (i = 0; i < 2; i++) {
+        memset(stats, 0, sizeof(unsigned long long) * 4);
+        flags = (VIR_NODE_CPU_TIME_KERNEL |
+                 VIR_NODE_CPU_TIME_USER |
+                 VIR_NODE_CPU_TIME_IDLE |
+                 VIR_NODE_CPU_TIME_IOWAIT);
+        nr_stats = virNodeGetCpuTime(ctl->conn, stats, 4, flags);
+        if (nr_stats != 4) {
+            vshError(ctl, "%s", _("failed to get cpu time of the node."));
+            return FALSE;
+        }
+
+        cpu_time[i].sys    = stats[0];
+        cpu_time[i].user   = stats[1];
+        cpu_time[i].idle   = stats[2];
+        cpu_time[i].iowait = stats[3];
+
+        if (sleep_done == FALSE) {
+            sleep(1);
+            sleep_done = TRUE;
+        }
+    }
+
+    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;
+
+    vshPrint(ctl, "%-15s %5.1lf%%\n",
+             _("usage :"), (user_time + sys_time) / total_time * 100);
+    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[] = {
@@ -10891,6 +10974,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 2a708f6..8e3cd3e 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