6ae9ed
From 7f30c5668d4b1e41daad07357f3dc1ed7cff8a71 Mon Sep 17 00:00:00 2001
6ae9ed
Message-Id: <7f30c5668d4b1e41daad07357f3dc1ed7cff8a71@dist-git>
6ae9ed
From: Peter Krempa <pkrempa@redhat.com>
6ae9ed
Date: Wed, 24 Aug 2016 16:10:50 -0400
6ae9ed
Subject: [PATCH] qemu: domain: Extract formating and parsing of vCPU thread
6ae9ed
 ids
6ae9ed
6ae9ed
https://bugzilla.redhat.com/show_bug.cgi?id=1097930
6ae9ed
https://bugzilla.redhat.com/show_bug.cgi?id=1224341
6ae9ed
6ae9ed
Further patches will be adding index and modifying the source variables
6ae9ed
so this will make it more clear.
6ae9ed
6ae9ed
(cherry picked from commit b91335afe48a406223caf15a0598b95ec29a26b5)
6ae9ed
---
6ae9ed
 src/qemu/qemu_domain.c | 82 +++++++++++++++++++++++++++++++++-----------------
6ae9ed
 1 file changed, 54 insertions(+), 28 deletions(-)
6ae9ed
6ae9ed
diff --git a/src/qemu/qemu_domain.c b/src/qemu/qemu_domain.c
6ae9ed
index c213a9f..01f0d6a 100644
6ae9ed
--- a/src/qemu/qemu_domain.c
6ae9ed
+++ b/src/qemu/qemu_domain.c
6ae9ed
@@ -1354,6 +1354,27 @@ qemuDomainObjPrivateFree(void *data)
6ae9ed
 }
6ae9ed
 
6ae9ed
 
6ae9ed
+static void
6ae9ed
+qemuDomainObjPrivateXMLFormatVcpus(virBufferPtr buf,
6ae9ed
+                                   int *vcpupids,
6ae9ed
+                                   int nvcpupids)
6ae9ed
+{
6ae9ed
+    size_t i;
6ae9ed
+
6ae9ed
+    if (!nvcpupids)
6ae9ed
+        return;
6ae9ed
+
6ae9ed
+    virBufferAddLit(buf, "<vcpus>\n");
6ae9ed
+    virBufferAdjustIndent(buf, 2);
6ae9ed
+
6ae9ed
+    for (i = 0; i < nvcpupids; i++)
6ae9ed
+        virBufferAsprintf(buf, "<vcpu pid='%d'/>\n", vcpupids[i]);
6ae9ed
+
6ae9ed
+    virBufferAdjustIndent(buf, -2);
6ae9ed
+    virBufferAddLit(buf, "</vcpus>\n");
6ae9ed
+}
6ae9ed
+
6ae9ed
+
6ae9ed
 static int
6ae9ed
 qemuDomainObjPrivateXMLFormat(virBufferPtr buf,
6ae9ed
                               virDomainObjPtr vm)
6ae9ed
@@ -1381,16 +1402,7 @@ qemuDomainObjPrivateXMLFormat(virBufferPtr buf,
6ae9ed
                           virDomainChrTypeToString(priv->monConfig->type));
6ae9ed
     }
6ae9ed
 
6ae9ed
-
6ae9ed
-    if (priv->nvcpupids) {
6ae9ed
-        size_t i;
6ae9ed
-        virBufferAddLit(buf, "<vcpus>\n");
6ae9ed
-        virBufferAdjustIndent(buf, 2);
6ae9ed
-        for (i = 0; i < priv->nvcpupids; i++)
6ae9ed
-            virBufferAsprintf(buf, "<vcpu pid='%d'/>\n", priv->vcpupids[i]);
6ae9ed
-        virBufferAdjustIndent(buf, -2);
6ae9ed
-        virBufferAddLit(buf, "</vcpus>\n");
6ae9ed
-    }
6ae9ed
+    qemuDomainObjPrivateXMLFormatVcpus(buf, priv->vcpupids, priv->nvcpupids);
6ae9ed
 
6ae9ed
     if (priv->qemuCaps) {
6ae9ed
         size_t i;
6ae9ed
@@ -1479,6 +1491,29 @@ qemuDomainObjPrivateXMLFormat(virBufferPtr buf,
6ae9ed
     return 0;
6ae9ed
 }
6ae9ed
 
6ae9ed
+
6ae9ed
+static int
6ae9ed
+qemuDomainObjPrivateXMLParseVcpu(xmlNodePtr node,
6ae9ed
+                                 unsigned int idx,
6ae9ed
+                                 qemuDomainObjPrivatePtr priv)
6ae9ed
+{
6ae9ed
+    char *pidstr;
6ae9ed
+    int ret = -1;
6ae9ed
+
6ae9ed
+    if (!(pidstr = virXMLPropString(node, "pid")))
6ae9ed
+        goto cleanup;
6ae9ed
+
6ae9ed
+    if (virStrToLong_i(pidstr, NULL, 10, &(priv->vcpupids[idx])) < 0)
6ae9ed
+        goto cleanup;
6ae9ed
+
6ae9ed
+    ret = 0;
6ae9ed
+
6ae9ed
+ cleanup:
6ae9ed
+    VIR_FREE(pidstr);
6ae9ed
+    return ret;
6ae9ed
+}
6ae9ed
+
6ae9ed
+
6ae9ed
 static int
6ae9ed
 qemuDomainObjPrivateXMLParse(xmlXPathContextPtr ctxt,
6ae9ed
                              virDomainObjPtr vm,
6ae9ed
@@ -1529,27 +1564,18 @@ qemuDomainObjPrivateXMLParse(xmlXPathContextPtr ctxt,
6ae9ed
         goto error;
6ae9ed
     }
6ae9ed
 
6ae9ed
-    n = virXPathNodeSet("./vcpus/vcpu", ctxt, &nodes);
6ae9ed
-    if (n < 0)
6ae9ed
+    if ((n = virXPathNodeSet("./vcpus/vcpu", ctxt, &nodes)) < 0)
6ae9ed
         goto error;
6ae9ed
-    if (n) {
6ae9ed
-        priv->nvcpupids = n;
6ae9ed
-        if (VIR_REALLOC_N(priv->vcpupids, priv->nvcpupids) < 0)
6ae9ed
+
6ae9ed
+    priv->nvcpupids = n;
6ae9ed
+    if (VIR_REALLOC_N(priv->vcpupids, priv->nvcpupids) < 0)
6ae9ed
+        goto error;
6ae9ed
+
6ae9ed
+    for (i = 0; i < n; i++) {
6ae9ed
+        if (qemuDomainObjPrivateXMLParseVcpu(nodes[i], i, priv) < 0)
6ae9ed
             goto error;
6ae9ed
-
6ae9ed
-        for (i = 0; i < n; i++) {
6ae9ed
-            char *pidstr = virXMLPropString(nodes[i], "pid");
6ae9ed
-            if (!pidstr)
6ae9ed
-                goto error;
6ae9ed
-
6ae9ed
-            if (virStrToLong_i(pidstr, NULL, 10, &(priv->vcpupids[i])) < 0) {
6ae9ed
-                VIR_FREE(pidstr);
6ae9ed
-                goto error;
6ae9ed
-            }
6ae9ed
-            VIR_FREE(pidstr);
6ae9ed
-        }
6ae9ed
-        VIR_FREE(nodes);
6ae9ed
     }
6ae9ed
+    VIR_FREE(nodes);
6ae9ed
 
6ae9ed
     if ((n = virXPathNodeSet("./qemuCaps/flag", ctxt, &nodes)) < 0) {
6ae9ed
         virReportError(VIR_ERR_INTERNAL_ERROR,
6ae9ed
-- 
6ae9ed
2.10.0
6ae9ed