[libvirt] [libvirt-python][PATCH v2] examples: Introduce nodestats example

Michal Privoznik mprivozn at redhat.com
Thu Jul 16 12:38:09 UTC 2015


So, this is an exercise to show libvirt capabilities. Firstly, for
each host NUMA nodes some statistics are printed out, i.e. total
memory and free memory. Then, for each running domain, that has memory
strictly bound to certain host nodes, a small statistics of how much
memory it takes is printed out too. For instance:

        # ./examples/nodestats.py
        NUMA stats
        NUMA nodes:     0       1       2       3
        MemTotal:       3950    3967    3937    3943
        MemFree:        66      56      42      41
        Domain 'fedora':
                Overall memory: 1536 MiB nodes
        Domain 'fedora22':
                Overall memory: 2048 MiB nodes
        Domain 'fedora21':
                Overall memory: 1024 MiB nodes 0-1
                Node 0: 1024 MiB nodes 0-1
        Domain 'gentoo':
                Overall memory: 4096 MiB nodes 0-3
                Node 0: 1024 MiB nodes 0
                Node 1: 1024 MiB nodes 1
                Node 2: 1024 MiB nodes 2
                Node 3: 1024 MiB nodes 3

We can see 4 host NUMA nodes, all of them having roughly 4GB of RAM.
Yeah, all of them has nearly all the memory consumed. Then, there are
four domains running. For instance, domain 'fedora' has 1.5GB memory
which is not pinned onto any specific host NUMA node. Domain 'gentoo' on
the other hand has 4GB memory and has 4 NUMA nodes which are pinned 1:1
to host nodes.

Signed-off-by: Michal Privoznik <mprivozn at redhat.com>
---

diff to v1:
- Martin review suggestions worked in
- Changed the format of printing per-domain stats

 examples/nodestats.py | 80 +++++++++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 80 insertions(+)
 create mode 100755 examples/nodestats.py

diff --git a/examples/nodestats.py b/examples/nodestats.py
new file mode 100755
index 0000000..4a58268
--- /dev/null
+++ b/examples/nodestats.py
@@ -0,0 +1,80 @@
+#!/usr/bin/env python
+# Print some host NUMA node statistics
+#
+# Authors:
+#   Michal Privoznik <mprivozn at redhat.com>
+
+import libvirt
+import sys
+from xml.dom import minidom
+import libxml2
+
+def xpath_eval(ctxt, path):
+    res = ctxt.xpathEval(path)
+    if res is None or len(res) == 0:
+        value = None
+    else:
+        value = res[0].content
+    return value
+
+try:
+    conn = libvirt.openReadOnly(None)
+except libvirt.libvirtError:
+    print("Failed to connect to the hypervisor")
+    sys.exit(1)
+
+try:
+    capsXML = conn.getCapabilities()
+except libvirt.libvirtError:
+    print("Failed to request capabilities")
+    sys.exit(1)
+
+caps = minidom.parseString(capsXML)
+cells = caps.getElementsByTagName("cells")[0]
+
+nodesIDs = [ int(proc.getAttribute("id"))
+             for proc in cells.getElementsByTagName("cell") ]
+
+nodesMem = [ conn.getMemoryStats(int(proc))
+             for proc in nodesIDs]
+
+doms = conn.listAllDomains(libvirt.VIR_CONNECT_LIST_DOMAINS_ACTIVE)
+
+domsStrict = [ proc
+               for proc in doms
+               if proc.numaParameters()["numa_mode"] == libvirt.VIR_DOMAIN_NUMATUNE_MEM_STRICT ]
+
+domsStrictCfg = {}
+for dom in domsStrict:
+    xmlStr = dom.XMLDesc()
+    doc = libxml2.parseDoc(xmlStr)
+    ctxt = doc.xpathNewContext()
+
+    domsStrictCfg[dom] = {}
+
+    pin = ctxt.xpathEval("string(/domain/numatune/memory/@nodeset)")
+    memsize = ctxt.xpathEval("string(/domain/memory)")
+    domsStrictCfg[dom]["memory"] = {"size": int(memsize), "pin": pin}
+
+    for memnode in ctxt.xpathEval("/domain/numatune/memnode"):
+        ctxt.setContextNode(memnode)
+        cellid = xpath_eval(ctxt, "@cellid")
+        nodeset = xpath_eval(ctxt, "@nodeset")
+
+        nodesize = xpath_eval(ctxt, "/domain/cpu/numa/cell[@id='%s']/@memory" % cellid)
+        domsStrictCfg[dom][cellid] = {"size": int(nodesize), "pin": nodeset}
+
+
+print("NUMA stats")
+print("NUMA nodes:\t" + "\t".join(str(node) for node in nodesIDs))
+print("MemTotal:\t" + "\t".join(str(i.get("total") // 1024) for i in nodesMem))
+print("MemFree:\t" + "\t".join(str(i.get("free") // 1024) for i in nodesMem))
+
+for dom, v in domsStrictCfg.items():
+    print("Domain '%s':\t" % dom.name())
+    print("\tOverall memory: %d MiB nodes %s" % (v["memory"]["size"] // 1024, v["memory"]["pin"]))
+
+    for k, node in sorted(v.items()):
+        if k is "memory":
+            continue
+        print("\tNode %s:\t%d MiB nodes %s" % (k, node["size"] // 1024, node["pin"]))
-- 
2.3.6




More information about the libvir-list mailing list