[libvirt] [PATCH 5/6] virsh: Enable virDomainMemStats as a new command

Adam Litke agl at us.ibm.com
Tue Dec 8 19:57:19 UTC 2009


Define a new command 'dommemstat' to report domain memory statistics.  The
output format is inspired by 'domblkstat' and 'domifstat' and consists of
tag/value pairs, one per line.  The command can complete successfully and print
no output if virDomainMemStats is supported by the driver, but not the guest
operating system.

Sample output:

swap_in 0
swap_out 0
major_fault 54
minor_fault 58259
mem_free 499384320
mem_tot 514531328

All stats referring to a quantity of memory (eg. all above except major and
minor faults) represent the quantity in bytes.

Signed-off-by: Adam Litke <agl at us.ibm.com>
To: libvirt list <libvir-list at redhat.com>
---
 tools/virsh.c |   51 +++++++++++++++++++++++++++++++++++++++++++++++++++
 1 files changed, 51 insertions(+), 0 deletions(-)

diff --git a/tools/virsh.c b/tools/virsh.c
index 46c5454..a5133d5 100644
--- a/tools/virsh.c
+++ b/tools/virsh.c
@@ -874,6 +874,56 @@ cmdDomIfstat (vshControl *ctl, const vshCmd *cmd)
 }
 
 /*
+ * "dommemstat" command
+ */
+static const vshCmdInfo info_dommemstat[] = {
+    {"help", gettext_noop("get memory stats for a domain")},
+    {"desc", gettext_noop("Get memory stats for a running domain.")},
+    {NULL,NULL}
+};
+
+static const vshCmdOptDef opts_dommemstat[] = {
+    {"domain", VSH_OT_DATA, VSH_OFLAG_REQ, gettext_noop("domain name, id or uuid")},
+    {NULL, 0, 0, NULL}
+};
+
+static int
+cmdDomMemStat(vshControl *ctl, const vshCmd *cmd)
+{
+    virDomainPtr dom;
+    char *name;
+    struct _virDomainMemStats stats;
+
+    if (!vshConnectionUsability(ctl, ctl->conn, TRUE))
+        return FALSE;
+
+    if (!(dom = vshCommandOptDomain(ctl, cmd, &name)))
+        return FALSE;
+
+    if (virDomainMemStats (dom, &stats, sizeof stats) == -1) {
+        vshError(ctl, _("Failed to get memory stats for domain %s"), name);
+        virDomainFree(dom);
+        return FALSE;
+    }
+
+    if (stats.swap_in != (uint64_t)-1)
+        vshPrint (ctl, "swap_in %llu\n", stats.swap_in);
+    if (stats.swap_out != (uint64_t)-1)
+        vshPrint (ctl, "swap_out %llu\n", stats.swap_out);
+    if (stats.major_fault != (uint64_t)-1)
+        vshPrint (ctl, "major_fault %llu\n", stats.major_fault);
+    if (stats.minor_fault != (uint64_t)-1)
+        vshPrint (ctl, "minor_fault %llu\n", stats.minor_fault);
+    if (stats.mem_free != (uint64_t)-1)
+        vshPrint (ctl, "mem_free %llu\n", stats.mem_free);
+    if (stats.mem_tot != (uint64_t)-1)
+        vshPrint (ctl, "mem_tot %llu\n", stats.mem_tot);
+
+    virDomainFree(dom);
+    return TRUE;
+}
+
+/*
  * "suspend" command
  */
 static const vshCmdInfo info_suspend[] = {
@@ -7183,6 +7233,7 @@ static const vshCmdDef commands[] = {
     {"domstate", cmdDomstate, opts_domstate, info_domstate},
     {"domblkstat", cmdDomblkstat, opts_domblkstat, info_domblkstat},
     {"domifstat", cmdDomIfstat, opts_domifstat, info_domifstat},
+    {"dommemstat", cmdDomMemStat, opts_dommemstat, info_dommemstat},
     {"domxml-from-native", cmdDomXMLFromNative, opts_domxmlfromnative, info_domxmlfromnative},
     {"domxml-to-native", cmdDomXMLToNative, opts_domxmltonative, info_domxmltonative},
     {"dumpxml", cmdDumpXML, opts_dumpxml, info_dumpxml},
-- 
1.6.5




More information about the libvir-list mailing list