1 ##############################################################################
2 # Copyright (c) 2015 Huawei Technologies Co.,Ltd and others.
4 # All rights reserved. This program and the accompanying materials
5 # are made available under the terms of the Apache License, Version 2.0
6 # which accompanies this distribution, and is available at
7 # http://www.apache.org/licenses/LICENSE-2.0
8 ##############################################################################
13 import xml.etree.cElementTree as ET
15 import xml.etree.ElementTree as ET
18 class GetPhyInfo(object):
22 def _get_range(self, temp):
25 for sub in temp.split(','):
27 _start = sub.split('-')[0]
28 _end = sub.split('-')[1]
29 r_list.extend(range(int(_start), int(_end) + 1))
31 topo['phy_cores'] = r_list
33 topo['virt_cores'] = r_list
37 def _get_numa_num(self):
38 flag, num = commands.getstatusoutput('lscpu | grep "NUMA node(s):"')
40 num = num.split(':')[1]
42 print('get numa %s value failed.' % (num))
45 def get_numa_core(self):
47 num = self._get_numa_num()
48 for numa_id in range(0, int(num)):
49 flag, temp = commands.getstatusoutput('lscpu | grep "NUMA node%s"' % (str(numa_id)))
51 temp = temp.split(':')[1].split()[0]
53 print('get numa %s range %s failed.' % (str(numa_id), range))
54 topo = self._get_range(temp)
55 numa['node' + str(numa_id)] = topo
58 def get_nic_numa(self, nic):
61 flag, id = commands.getstatusoutput('cat /sys/class/net/%s/device/numa_node' % (nic))
63 print('get nic numa id failed.')
66 def _get_main_pid(self, xml_file):
68 tree = ET.ElementTree(file=xml_file)
70 _main_pid = root.attrib['pid']
72 print('[ERROR]Parse xml file failed, could not get qemu main pid')
75 def _get_qemu_threads(self, xml_file):
80 tree = ET.ElementTree(file=xml_file)
82 for element in tree.iterfind('vcpus/vcpu'):
83 _qemu_threads.append(element.attrib['pid'])
85 print('[ERROR]Parse xml file failed, could not get qemu threads.')
89 def _get_mem_numa(self, xml_file):
92 tree = ET.ElementTree(file=xml_file)
94 for element in tree.iterfind('domain/numatune/memory'):
95 _mem_numa = element.attrib['nodeset']
99 def _get_vhost_threads(self, xml_file):
101 _main_pid = self._get_main_pid(xml_file)
104 proc_name = 'vhost-' + _main_pid
105 flag, temp = commands.getstatusoutput('ps -ef | grep %s | grep -v grep' % (proc_name))
106 for line in temp.split('\n'):
108 vhost = line.split()[1]
111 print('get vhost %s proc id failed' % (line))
115 def get_vm_info(self, vm_name):
117 src_path = '/var/run/libvirt/qemu/'
118 xml_file = src_path + vm_name + '.xml'
120 # get vm main pid from file
121 _main_pid = self._get_main_pid(xml_file)
122 # get vm vcpu thread from the libvirt file
123 _qemu_threads = self._get_qemu_threads(xml_file)
124 # get vm bind mem numa id
125 _mem_numa = self._get_mem_numa(xml_file)
127 _vhosts = self._get_vhost_threads(xml_file)
129 vm['main_pid'] = _main_pid
130 vm['qemu_thread'] = _qemu_threads
131 vm['mem_numa'] = _mem_numa
132 vm['vhost_thread'] = _vhosts
135 def _get_proc_by_irq(self, irq):
137 flag, info = commands.getstatusoutput('ps -ef | grep irq/%s | grep -v grep ' % (irq))
138 proc_id = info.split('\n')[0].split()[1]
140 print("[ERROR]grep process id failed.")
143 def get_nic_interrupt_proc(self, nic):
145 flag, info = commands.getstatusoutput('cat /proc/interrupts | grep %s' % (nic))
146 for line in info.split('\n'):
148 irq_num = line.split(':')[0].split()[0]
149 proc_id = self._get_proc_by_irq(irq_num)
150 _phy_nic_thread.append([irq_num, proc_id])
152 print("[ERROR]get irq num failed.")
153 return _phy_nic_thread
155 def get_libvirt_vms(self):
157 flag, info = commands.getstatusoutput('virsh list')
158 list = info.split('\n')
166 vm_temp = line.split()[1]
167 vm_list.append(vm_temp)
169 print("Get vm name failed from %s" % (line))