]> git.ipfire.org Git - thirdparty/libvirt.git/commitdiff
virsh: host: refactor cmdFreecell()
authorKristina Hanicova <khanicov@redhat.com>
Thu, 23 Sep 2021 23:25:10 +0000 (01:25 +0200)
committerMichal Privoznik <mprivozn@redhat.com>
Fri, 24 Sep 2021 07:52:42 +0000 (09:52 +0200)
Signed-off-by: Kristina Hanicova <khanicov@redhat.com>
Reviewed-by: Michal Privoznik <mprivozn@redhat.com>
tools/virsh-host.c

index e6ed4a26ce334f3baa9494dfd76ab2c5f82a776d..be9cbc2096fddc8e940e3c97b8e0b133e1cf8025 100644 (file)
@@ -162,7 +162,6 @@ cmdFreecell(vshControl *ctl, const vshCmd *cmd)
     bool cellno = vshCommandOptBool(cmd, "cellno");
     size_t i;
     g_autofree char *cap_xml = NULL;
-    g_autoptr(xmlDoc) xml = NULL;
     g_autoptr(xmlXPathContext) ctxt = NULL;
     virshControl *priv = ctl->privData;
 
@@ -171,68 +170,68 @@ cmdFreecell(vshControl *ctl, const vshCmd *cmd)
     if (cellno && vshCommandOptInt(ctl, cmd, "cellno", &cell) < 0)
         return false;
 
-    if (all) {
-        if (!(cap_xml = virConnectGetCapabilities(priv->conn))) {
-            vshError(ctl, "%s", _("unable to get node capabilities"));
-            return false;
-        }
+    if (!all) {
+        if (cellno) {
+            if (virNodeGetCellsFreeMemory(priv->conn, &memory, cell, 1) != 1)
+                return false;
 
-        xml = virXMLParseStringCtxt(cap_xml, _("(capabilities)"), &ctxt);
-        if (!xml) {
-            vshError(ctl, "%s", _("unable to get node capabilities"));
-            return false;
+            vshPrint(ctl, "%d: %llu KiB\n", cell, (memory/1024));
+            return true;
         }
 
-        nodes_cnt = virXPathNodeSet("/capabilities/host/topology/cells/cell",
-                                    ctxt, &nodes);
-
-        if (nodes_cnt == -1) {
-            vshError(ctl, "%s", _("could not get information about "
-                                  "NUMA topology"));
+        if ((memory = virNodeGetFreeMemory(priv->conn)) == 0)
             return false;
-        }
 
-        nodes_free = g_new0(unsigned long long, nodes_cnt);
-        nodes_id = g_new0(unsigned long, nodes_cnt);
+        vshPrint(ctl, "%s: %llu KiB\n", _("Total"), (memory/1024));
+        return true;
+    }
 
-        for (i = 0; i < nodes_cnt; i++) {
-            unsigned long id;
-            g_autofree char *val = virXMLPropString(nodes[i], "id");
-            if (virStrToLong_ulp(val, NULL, 10, &id)) {
-                vshError(ctl, "%s", _("conversion from string failed"));
-                return false;
-            }
-            nodes_id[i] = id;
-            if (virNodeGetCellsFreeMemory(priv->conn, &(nodes_free[i]),
-                                          id, 1) != 1) {
-                vshError(ctl, _("failed to get free memory for NUMA node "
-                                "number: %lu"), id);
-                return false;
-            }
-        }
+    if (!(cap_xml = virConnectGetCapabilities(priv->conn))) {
+        vshError(ctl, "%s", _("unable to get node capabilities"));
+        return false;
+    }
 
-        for (cell = 0; cell < nodes_cnt; cell++) {
-            vshPrint(ctl, "%5lu: %10llu KiB\n", nodes_id[cell],
-                    (nodes_free[cell]/1024));
-            memory += nodes_free[cell];
-        }
+    if (!virXMLParseStringCtxt(cap_xml, _("(capabilities)"), &ctxt)) {
+        vshError(ctl, "%s", _("unable to get node capabilities"));
+        return false;
+    }
 
-        vshPrintExtra(ctl, "--------------------\n");
-        vshPrintExtra(ctl, "%5s: %10llu KiB\n", _("Total"), memory/1024);
-    } else {
-        if (cellno) {
-            if (virNodeGetCellsFreeMemory(priv->conn, &memory, cell, 1) != 1)
-                return false;
+    nodes_cnt = virXPathNodeSet("/capabilities/host/topology/cells/cell",
+                                ctxt, &nodes);
 
-            vshPrint(ctl, "%d: %llu KiB\n", cell, (memory/1024));
-        } else {
-            if ((memory = virNodeGetFreeMemory(priv->conn)) == 0)
-                return false;
+    if (nodes_cnt == -1) {
+        vshError(ctl, "%s", _("could not get information about NUMA topology"));
+        return false;
+    }
+
+    nodes_free = g_new0(unsigned long long, nodes_cnt);
+    nodes_id = g_new0(unsigned long, nodes_cnt);
 
-            vshPrint(ctl, "%s: %llu KiB\n", _("Total"), (memory/1024));
+    for (i = 0; i < nodes_cnt; i++) {
+        unsigned long id;
+        g_autofree char *val = virXMLPropString(nodes[i], "id");
+        if (virStrToLong_ulp(val, NULL, 10, &id)) {
+            vshError(ctl, "%s", _("conversion from string failed"));
+            return false;
+        }
+        nodes_id[i] = id;
+        if (virNodeGetCellsFreeMemory(priv->conn, &(nodes_free[i]),
+                                      id, 1) != 1) {
+            vshError(ctl, _("failed to get free memory for NUMA node "
+                            "number: %lu"), id);
+            return false;
         }
     }
 
+    for (cell = 0; cell < nodes_cnt; cell++) {
+        vshPrint(ctl, "%5lu: %10llu KiB\n", nodes_id[cell],
+                (nodes_free[cell]/1024));
+        memory += nodes_free[cell];
+    }
+
+    vshPrintExtra(ctl, "--------------------\n");
+    vshPrintExtra(ctl, "%5s: %10llu KiB\n", _("Total"), memory/1024);
+
     return true;
 }