5 import xml.etree.cElementTree as ET
7 import xml.etree.ElementTree as ET
10 class GetPhyInfo(object):
14 def _get_range(self, temp):
17 for sub in temp.split(','):
19 _start = sub.split('-')[0]
20 _end = sub.split('-')[1]
21 r_list.extend(range(int(_start), int(_end) + 1))
23 topo['phy_cores'] = r_list
25 topo['virt_cores'] = r_list
29 def _get_numa_num(self):
30 flag, num = commands.getstatusoutput('lscpu | grep "NUMA node(s):"')
32 num = num.split(':')[1]
34 print('get numa %s value failed.' % (num))
37 def get_numa_core(self):
39 num = self._get_numa_num()
40 for numa_id in range(0, int(num)):
41 flag, temp = commands.getstatusoutput('lscpu | grep "NUMA node%s"' % (str(numa_id)))
43 temp = temp.split(':')[1].split()[0]
45 print('get numa %s range %s failed.' % (str(numa_id), range))
46 topo = self._get_range(temp)
47 numa['node' + str(numa_id)] = topo
50 def get_nic_numa(self, nic):
53 flag, id = commands.getstatusoutput('cat /sys/class/net/%s/device/numa_node' % (nic))
55 print('get nic numa id failed.')
58 def _get_main_pid(self, xml_file):
60 tree = ET.ElementTree(file=xml_file)
62 _main_pid = root.attrib['pid']
64 print('[ERROR]Parse xml file failed, could not get qemu main pid')
67 def _get_qemu_threads(self, xml_file):
72 tree = ET.ElementTree(file=xml_file)
74 for element in tree.iterfind('vcpus/vcpu'):
75 _qemu_threads.append(element.attrib['pid'])
77 print('[ERROR]Parse xml file failed, could not get qemu threads.')
81 def _get_mem_numa(self, xml_file):
84 tree = ET.ElementTree(file=xml_file)
86 for element in tree.iterfind('domain/numatune/memory'):
87 _mem_numa = element.attrib['nodeset']
91 def _get_vhost_threads(self, xml_file):
93 _main_pid = self._get_main_pid(xml_file)
96 proc_name = 'vhost-' + _main_pid
97 flag, temp = commands.getstatusoutput('ps -ef | grep %s | grep -v grep' % (proc_name))
98 for line in temp.split('\n'):
100 vhost = line.split()[1]
103 print('get vhost %s proc id failed' % (line))
107 def get_vm_info(self, vm_name):
109 src_path = '/var/run/libvirt/qemu/'
110 xml_file = src_path + vm_name + '.xml'
112 # get vm main pid from file
113 _main_pid = self._get_main_pid(xml_file)
114 # get vm vcpu thread from the libvirt file
115 _qemu_threads = self._get_qemu_threads(xml_file)
116 # get vm bind mem numa id
117 _mem_numa = self._get_mem_numa(xml_file)
119 _vhosts = self._get_vhost_threads(xml_file)
121 vm['main_pid'] = _main_pid
122 vm['qemu_thread'] = _qemu_threads
123 vm['mem_numa'] = _mem_numa
124 vm['vhost_thread'] = _vhosts
127 def _get_proc_by_irq(self, irq):
129 flag, info = commands.getstatusoutput('ps -ef | grep irq/%s | grep -v grep ' % (irq))
130 proc_id = info.split('\n')[0].split()[1]
132 print("[ERROR]grep process id failed.")
135 def get_nic_interrupt_proc(self, nic):
137 flag, info = commands.getstatusoutput('cat /proc/interrupts | grep %s' % (nic))
138 for line in info.split('\n'):
140 irq_num = line.split(':')[0].split()[0]
141 proc_id = self._get_proc_by_irq(irq_num)
142 _phy_nic_thread.append([irq_num, proc_id])
144 print("[ERROR]get irq num failed.")
145 return _phy_nic_thread
147 def get_libvirt_vms(self):
149 flag, info = commands.getstatusoutput('virsh list')
150 list = info.split('\n')
158 vm_temp = line.split()[1]
159 vm_list.append(vm_temp)
161 print("Get vm name failed from %s" % (line))