| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
import libvirt |
| 8 |
from xml.dom import minidom |
| 9 |
import libxml2 |
| 10 |
from typing import Any |
| 11 |
|
| 12 |
|
| 13 |
def xpath_eval(ctxt, path: str) -> str: |
| 14 |
res = ctxt.xpathEval(path) |
| 15 |
if res is None or len(res) == 0: |
| 16 |
value = '' |
| 17 |
else: |
| 18 |
value = res[0].content |
| 19 |
return value |
| 20 |
|
| 21 |
|
| 22 |
try: |
| 23 |
conn = libvirt.openReadOnly(None) |
| 24 |
except libvirt.libvirtError: |
| 25 |
print("Failed to connect to the hypervisor") |
| 26 |
exit(1) |
| 27 |
|
| 28 |
try: |
| 29 |
capsXML = conn.getCapabilities() |
| 30 |
except libvirt.libvirtError: |
| 31 |
print("Failed to request capabilities") |
| 32 |
exit(1) |
| 33 |
|
| 34 |
caps = minidom.parseString(capsXML) |
| 35 |
cells = caps.getElementsByTagName("cells")[0] |
| 36 |
|
| 37 |
nodesIDs = [ |
| 38 |
int(proc.getAttribute("id")) |
| 39 |
for proc in cells.getElementsByTagName("cell") |
| 40 |
] |
| 41 |
|
| 42 |
nodesMem = [ |
| 43 |
conn.getMemoryStats(int(proc)) |
| 44 |
for proc in nodesIDs |
| 45 |
] |
| 46 |
|
| 47 |
doms = conn.listAllDomains(libvirt.VIR_CONNECT_LIST_DOMAINS_ACTIVE) |
| 48 |
|
| 49 |
domsStrict = [ |
| 50 |
proc |
| 51 |
for proc in doms |
| 52 |
if proc.numaParameters()["numa_mode"] == libvirt.VIR_DOMAIN_NUMATUNE_MEM_STRICT |
| 53 |
] |
| 54 |
|
| 55 |
domsStrictCfg = {} |
| 56 |
for dom in domsStrict: |
| 57 |
xmlStr = dom.XMLDesc() |
| 58 |
doc = libxml2.parseDoc(xmlStr) |
| 59 |
ctxt = doc.xpathNewContext() |
| 60 |
|
| 61 |
domsStrictCfg[dom] = {} |
| 62 |
|
| 63 |
pin = ctxt.xpathEval("string(/domain/numatune/memory/@nodeset)") |
| 64 |
memsize = ctxt.xpathEval("string(/domain/memory)") |
| 65 |
domsStrictCfg[dom]["memory"] = {"size": int(memsize), "pin": pin} |
| 66 |
|
| 67 |
for memnode in ctxt.xpathEval("/domain/numatune/memnode"): |
| 68 |
ctxt.setContextNode(memnode) |
| 69 |
cellid = xpath_eval(ctxt, "@cellid") |
| 70 |
nodeset = xpath_eval(ctxt, "@nodeset") |
| 71 |
|
| 72 |
nodesize = xpath_eval(ctxt, "/domain/cpu/numa/cell[@id='%s']/@memory" % cellid) |
| 73 |
domsStrictCfg[dom][cellid] = {"size": int(nodesize), "pin": nodeset} |
| 74 |
|
| 75 |
|
| 76 |
print("NUMA stats") |
| 77 |
print("NUMA nodes:\t" + "\t".join(str(node) for node in nodesIDs)) |
| 78 |
print("MemTotal:\t" + "\t".join(str(i.get("total") // 1024) for i in nodesMem)) |
| 79 |
print("MemFree:\t" + "\t".join(str(i.get("free") // 1024) for i in nodesMem)) |
| 80 |
|
| 81 |
for dom, v in domsStrictCfg.items(): |
| 82 |
print("Domain '%s':\t" % dom.name()) |
| 83 |
|
| 84 |
toPrint = "\tOverall memory: %d MiB" % (v["memory"]["size"] // 1024) |
| 85 |
if v["memory"]["pin"] is not None and v["memory"]["pin"] != "": |
| 86 |
toPrint = toPrint + " nodes %s" % v["memory"]["pin"] |
| 87 |
print(toPrint) |
| 88 |
|
| 89 |
for k, node in sorted(v.items()): |
| 90 |
if k == "memory": |
| 91 |
continue |
| 92 |
toPrint = "\tNode %s:\t%d MiB" % (k, node["size"] // 1024) |
| 93 |
if node["pin"] is not None and node["pin"] != "": |
| 94 |
toPrint = toPrint + " nodes %s" % node["pin"] |
| 95 |
print(toPrint) |