summaryrefslogtreecommitdiff
path: root/examples/topology.py
blob: c39ee9c216a6144a56848cadfdc129245f743531 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
#!/usr/bin/env python3
# Parse topology information from the capabilities XML and use
# them to calculate host topology
#
# Authors:
#   Amador Pahim <apahim@redhat.com>
#   Peter Krempa <pkrempa@redhat.com>

import libvirt
from xml.dom import minidom

try:
    conn = libvirt.openReadOnly(None)
except libvirt.libvirtError:
    print('Failed to connect to the hypervisor')
    exit(1)

try:
    capsXML = conn.getCapabilities()
except libvirt.libvirtError:
    print('Failed to request capabilities')
    exit(1)

caps = minidom.parseString(capsXML)
host = caps.getElementsByTagName('host')[0]
cells = host.getElementsByTagName('cells')[0]
total_cpus = cells.getElementsByTagName('cpu').length

socketIds = []
siblingsIds = []

socketIds = [
    proc.getAttribute('socket_id')
    for proc in cells.getElementsByTagName('cpu')
    if proc.getAttribute('socket_id') not in socketIds
]

siblingsIds = [
    proc.getAttribute('siblings')
    for proc in cells.getElementsByTagName('cpu')
    if proc.getAttribute('siblings') not in siblingsIds
]

print("Host topology")
print("NUMA nodes:", cells.getAttribute('num'))
print("   Sockets:", len(set(socketIds)))
print("     Cores:", len(set(siblingsIds)))
print("   Threads:", total_cpus)