[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]

[libvirt] [PATCHv2 5/5] virsh: Implement command to excercise the bulk stats APIs



Add "domstats" command that excercises both of the new APIs depending if
you specify a domain list or not. The output is printed as a key=value
list of the returned parameters.

Man page section will be added in a separate patch.
---
 tools/virsh-domain-monitor.c | 140 +++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 140 insertions(+)

diff --git a/tools/virsh-domain-monitor.c b/tools/virsh-domain-monitor.c
index 8bd58ad..8eb3a21 100644
--- a/tools/virsh-domain-monitor.c
+++ b/tools/virsh-domain-monitor.c
@@ -1954,6 +1954,140 @@ cmdList(vshControl *ctl, const vshCmd *cmd)
 }
 #undef FILTER

+/*
+ * "domstats" command
+ */
+static const vshCmdInfo info_domstats[] = {
+    {.name = "help",
+     .data = N_("get statistics about one or multiple domains")
+    },
+    {.name = "desc",
+     .data = N_("Gets statistics about one or more (or all) domains")
+    },
+    {.name = NULL}
+};
+
+static const vshCmdOptDef opts_domstats[] = {
+    {.name = "stats",
+     .type = VSH_OT_INT,
+     .flags = VSH_OFLAG_REQ_OPT,
+     .help = N_("bit map of stats to retrieve"),
+    },
+    {.name = "allstats",
+     .type = VSH_OT_BOOL,
+     .help = N_("report all stats supported by the hypervisor"),
+    },
+    {.name = "state",
+     .type = VSH_OT_BOOL,
+     .help = N_("report domain state"),
+    },
+     {.name = "domains",
+     .type = VSH_OT_ARGV,
+     .flags = VSH_OFLAG_NONE,
+     .help = N_("list of domains to get stats for"),
+    },
+   {.name = NULL}
+};
+
+static bool
+vshDomainStatsPrint(vshControl *ctl ATTRIBUTE_UNUSED,
+                    virDomainStatsRecordPtr record)
+{
+    char *param;
+    size_t i;
+
+    vshPrint(ctl, "Domain: '%s'\n", virDomainGetName(record->dom));
+
+    for (i = 0; i < record->nparams; i++) {
+        if (!(param = vshGetTypedParamValue(ctl, record->params + i)))
+            return false;
+
+        vshPrint(ctl, "  %s=%s\n", record->params[i].field, param);
+
+        VIR_FREE(param);
+    }
+
+    vshPrint(ctl, "\n");
+    return true;
+}
+
+static bool
+cmdDomstats(vshControl *ctl, const vshCmd *cmd)
+{
+    unsigned int stats = 0;
+    virDomainPtr *domlist = NULL;
+    virDomainPtr *nextdom;
+    virDomainPtr dom;
+    size_t ndoms = 0;
+    virDomainStatsRecordPtr *records = NULL;
+    virDomainStatsRecordPtr *next;
+    int flags = 0;
+    const vshCmdOpt *opt = NULL;
+    bool ret = false;
+
+    if (vshCommandOptUInt(cmd, "stats", &stats) < 0) {
+        vshError(ctl, "%s", _("Unable to parse stats bitmap"));
+        return false;
+    }
+
+    if (vshCommandOptBool(cmd, "allstats"))
+        stats |= VIR_DOMAIN_STATS_ALL;
+
+    if (vshCommandOptBool(cmd, "state"))
+        stats |= VIR_DOMAIN_STATS_STATE;
+
+    if (stats == 0)
+        stats = VIR_DOMAIN_STATS_ALL;
+
+    if (vshCommandOptBool(cmd, "domains")) {
+        if (VIR_ALLOC_N(domlist, 1) < 0)
+            goto cleanup;
+        ndoms = 1;
+
+        while ((opt = vshCommandOptArgv(cmd, opt))) {
+            if (!(dom = vshLookupDomainBy(ctl, opt->data,
+                                          VSH_BYID | VSH_BYUUID | VSH_BYNAME)))
+                goto cleanup;
+
+            if (VIR_INSERT_ELEMENT(domlist, 0, ndoms, dom) < 0)
+                goto cleanup;
+        }
+
+        if (virDomainListGetStats(domlist,
+                                  stats,
+                                  &records,
+                                  flags) < 0)
+            goto cleanup;
+    } else {
+       if ((virConnectGetAllDomainStats(ctl->conn,
+                                        stats,
+                                        &records,
+                                        flags)) < 0)
+           goto cleanup;
+    }
+
+    if (records) {
+        for (next = records; *next; next++) {
+            if (!vshDomainStatsPrint(ctl, *next))
+                goto cleanup;
+        }
+    }
+
+    ret = true;
+ cleanup:
+    if (records)
+        virDomainStatsRecordListFree(records);
+
+    if (domlist) {
+        for (nextdom = domlist; *nextdom; nextdom++)
+            virDomainFree(*nextdom);
+
+        VIR_FREE(domlist);
+    }
+
+    return ret;
+}
+
 const vshCmdDef domMonitoringCmds[] = {
     {.name = "domblkerror",
      .handler = cmdDomBlkError,
@@ -2021,6 +2155,12 @@ const vshCmdDef domMonitoringCmds[] = {
      .info = info_domstate,
      .flags = 0
     },
+    {.name = "domstats",
+     .handler = cmdDomstats,
+     .opts = opts_domstats,
+     .info = info_domstats,
+     .flags = 0
+    },
     {.name = "domtime",
      .handler = cmdDomTime,
      .opts = opts_domtime,
-- 
2.0.2


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]