[libvirt] [PATCH 8/8] latency: Update cmdBlkStats to use new API

Osier Yang jyang at redhat.com
Wed Aug 31 08:26:13 UTC 2011


The modified function fallbacks to use virDomainBlockStats if
virDomainBlockStatsFlags is not supported by the hypervisor driver.
If the new API is supported, it will be invoked instead of the
old API.
---
 tools/virsh.c |   78 ++++++++++++++++++++++++++++++++++++++++++--------------
 1 files changed, 58 insertions(+), 20 deletions(-)

diff --git a/tools/virsh.c b/tools/virsh.c
index 15b9bdd..cd01528 100644
--- a/tools/virsh.c
+++ b/tools/virsh.c
@@ -1070,6 +1070,9 @@ cmdDomblkstat (vshControl *ctl, const vshCmd *cmd)
     virDomainPtr dom;
     const char *name = NULL, *device = NULL;
     struct _virDomainBlockStats stats;
+    virDomainBlockStatsFlagsPtr params = NULL;
+    int rc, nparams = 0;
+    bool ret = false;
 
     if (!vshConnectionUsability (ctl, ctl->conn))
         return false;
@@ -1077,34 +1080,69 @@ cmdDomblkstat (vshControl *ctl, const vshCmd *cmd)
     if (!(dom = vshCommandOptDomain (ctl, cmd, &name)))
         return false;
 
-    if (vshCommandOptString (cmd, "device", &device) <= 0) {
-        virDomainFree(dom);
-        return false;
-    }
+    if (vshCommandOptString (cmd, "device", &device) <= 0)
+        goto cleanup;
 
-    if (virDomainBlockStats (dom, device, &stats, sizeof stats) == -1) {
-        vshError(ctl, _("Failed to get block stats %s %s"), name, device);
-        virDomainFree(dom);
-        return false;
-    }
+    rc = virDomainBlockStatsFlags(dom, device, NULL, &nparams, 0);
 
-    if (stats.rd_req >= 0)
-        vshPrint (ctl, "%s rd_req %lld\n", device, stats.rd_req);
+    /* It might fail when virDomainBlockStatsFlags is not
+     * supported on older libvirt, fallback to use virDomainBlockStats
+     * then.
+     */
+    if (rc < 0) {
+        if (last_error->code != VIR_ERR_NO_SUPPORT) {
+            virshReportError(ctl);
+            goto cleanup;
+        } else {
+            virFreeError(last_error);
+            last_error = NULL;
+
+            if (virDomainBlockStats (dom, device, &stats,
+                                     sizeof stats) == -1) {
+                vshError(ctl, _("Failed to get block stats %s %s"),
+                         name, device);
+                goto cleanup;
+            }
 
-    if (stats.rd_bytes >= 0)
-        vshPrint (ctl, "%s rd_bytes %lld\n", device, stats.rd_bytes);
+            if (stats.rd_req >= 0)
+                vshPrint (ctl, "%s rd_req %lld\n", device, stats.rd_req);
 
-    if (stats.wr_req >= 0)
-        vshPrint (ctl, "%s wr_req %lld\n", device, stats.wr_req);
+            if (stats.rd_bytes >= 0)
+                vshPrint (ctl, "%s rd_bytes %lld\n", device, stats.rd_bytes);
 
-    if (stats.wr_bytes >= 0)
-        vshPrint (ctl, "%s wr_bytes %lld\n", device, stats.wr_bytes);
+            if (stats.wr_req >= 0)
+                vshPrint (ctl, "%s wr_req %lld\n", device, stats.wr_req);
 
-    if (stats.errs >= 0)
-        vshPrint (ctl, "%s errs %lld\n", device, stats.errs);
+            if (stats.wr_bytes >= 0)
+                vshPrint (ctl, "%s wr_bytes %lld\n", device, stats.wr_bytes);
 
+            if (stats.errs >= 0)
+                vshPrint (ctl, "%s errs %lld\n", device, stats.errs);
+        }
+    } else {
+        params = vshMalloc(ctl, sizeof(*params) * nparams);
+        memset(params, 0, sizeof(*params) * nparams);
+
+        if (virDomainBlockStatsFlags (dom, device, params, &nparams, 0) < 0) {
+            vshError(ctl, _("Failed to get block stats %s %s"), name, device);
+            goto cleanup;
+        }
+
+        int i;
+        /* XXX: The output sequence will be different. */
+        for (i = 0; i < nparams; i++) {
+            if (params[i].value >= 0)
+                vshPrint (ctl, "%s %s %lld\n", device, params[i].field,
+                          params[i].value);
+        }
+    }
+
+    ret = true;
+
+cleanup:
+    VIR_FREE(params);
     virDomainFree(dom);
-    return true;
+    return ret;
 }
 
 /* "domifstat" command
-- 
1.7.6




More information about the libvir-list mailing list