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):
23 def _get_range(self, temp):
26 for sub in temp.split(','):
28 _start = sub.split('-')[0]
29 _end = sub.split('-')[1]
30 r_list.extend(range(int(_start), int(_end) + 1))
32 topo['phy_cores'] = r_list
34 topo['virt_cores'] = r_list
38 def _get_numa_num(self):
39 flag, num = commands.getstatusoutput('lscpu | grep "NUMA node(s):"')
41 num = num.split(':')[1]
43 print('get numa %s value failed.' % (num))
46 def get_numa_core(self):
48 num = self._get_numa_num()
49 for numa_id in range(0, int(num)):
50 flag, temp = commands.getstatusoutput(
51 'lscpu | grep "NUMA node%s"' %
54 temp = temp.split(':')[1].split()[0]
56 print('get numa %s range %s failed.' % (str(numa_id), range))
57 topo = self._get_range(temp)
58 numa['node' + str(numa_id)] = topo
61 def get_nic_numa(self, nic):
64 flag, id = commands.getstatusoutput(
65 'cat /sys/class/net/%s/device/numa_node' %
68 print('get nic numa id failed.')
71 def _get_main_pid(self, xml_file):
73 tree = ET.ElementTree(file=xml_file)
75 _main_pid = root.attrib['pid']
77 print('[ERROR]Parse xml file failed, could not get qemu main pid')
80 def _get_qemu_threads(self, xml_file):
85 tree = ET.ElementTree(file=xml_file)
87 for element in tree.iterfind('vcpus/vcpu'):
88 _qemu_threads.append(element.attrib['pid'])
90 print('[ERROR]Parse xml file failed, could not get qemu threads.')
94 def _get_mem_numa(self, xml_file):
97 tree = ET.ElementTree(file=xml_file)
99 for element in tree.iterfind('domain/numatune/memory'):
100 _mem_numa = element.attrib['nodeset']
104 def _get_vhost_threads(self, xml_file):
106 _main_pid = self._get_main_pid(xml_file)
109 proc_name = 'vhost-' + _main_pid
110 flag, temp = commands.getstatusoutput(
111 'ps -ef | grep %s | grep -v grep' %
113 for line in temp.split('\n'):
115 vhost = line.split()[1]
118 print('get vhost %s proc id failed' % (line))
122 def get_vm_info(self, vm_name):
124 src_path = '/var/run/libvirt/qemu/'
125 xml_file = src_path + vm_name + '.xml'
127 # get vm main pid from file
128 _main_pid = self._get_main_pid(xml_file)
129 # get vm vcpu thread from the libvirt file
130 _qemu_threads = self._get_qemu_threads(xml_file)
131 # get vm bind mem numa id
132 _mem_numa = self._get_mem_numa(xml_file)
134 _vhosts = self._get_vhost_threads(xml_file)
136 vm['main_pid'] = _main_pid
137 vm['qemu_thread'] = _qemu_threads
138 vm['mem_numa'] = _mem_numa
139 vm['vhost_thread'] = _vhosts
142 def _get_proc_by_irq(self, irq):
144 flag, info = commands.getstatusoutput(
145 'ps -ef | grep irq/%s | grep -v grep ' % (irq))
146 proc_id = info.split('\n')[0].split()[1]
148 print("[ERROR]grep process id failed.")
151 def get_nic_interrupt_proc(self, nic):
153 flag, info = commands.getstatusoutput(
154 'cat /proc/interrupts | grep %s' % (nic))
155 for line in info.split('\n'):
157 irq_num = line.split(':')[0].split()[0]
158 proc_id = self._get_proc_by_irq(irq_num)
159 _phy_nic_thread.append([irq_num, proc_id])
161 print("[ERROR]get irq num failed.")
162 return _phy_nic_thread
164 def get_libvirt_vms(self):
166 flag, info = commands.getstatusoutput('virsh list')
167 list = info.split('\n')
175 vm_temp = line.split()[1]
176 vm_list.append(vm_temp)
178 print("Get vm name failed from %s" % (line))