873f0caf61bedcfe4977a608546aa471e3856d5d
[bottlenecks.git] / vstf / vstf / agent / equalizer / get_info.py
1 #!/usr/bin/python
2 import commands
3
4 try:
5     import xml.etree.cElementTree as ET
6 except ImportError:
7     import xml.etree.ElementTree as ET
8
9
10 class GetPhyInfo(object):
11     def __init__(self):
12         pass
13
14     def _get_range(self, temp):
15         topo = {}
16         phy_core_flag = True
17         for sub in temp.split(','):
18             r_list = []
19             _start = sub.split('-')[0]
20             _end = sub.split('-')[1]
21             r_list.extend(range(int(_start), int(_end) + 1))
22             if phy_core_flag:
23                 topo['phy_cores'] = r_list
24             else:
25                 topo['virt_cores'] = r_list
26             phy_core_flag = False
27         return topo
28
29     def _get_numa_num(self):
30         flag, num = commands.getstatusoutput('lscpu | grep "NUMA node(s):"')
31         try:
32             num = num.split(':')[1]
33         except:
34             print('get numa %s value failed.' % (num))
35         return num
36
37     def get_numa_core(self):
38         numa = {}
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)))
42             try:
43                 temp = temp.split(':')[1].split()[0]
44             except:
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
48         return str(numa)
49
50     def get_nic_numa(self, nic):
51         result = {}
52         try:
53             flag, id = commands.getstatusoutput('cat /sys/class/net/%s/device/numa_node' % (nic))
54         except:
55             print('get nic numa id failed.')
56         return id
57
58     def _get_main_pid(self, xml_file):
59         try:
60             tree = ET.ElementTree(file=xml_file)
61             root = tree.getroot()
62             _main_pid = root.attrib['pid']
63         except:
64             print('[ERROR]Parse xml file failed, could not get qemu main pid')
65         return _main_pid
66
67     def _get_qemu_threads(self, xml_file):
68         # import pdb
69         # pdb.set_trace()
70         _qemu_threads = []
71         try:
72             tree = ET.ElementTree(file=xml_file)
73             root = tree.getroot()
74             for element in tree.iterfind('vcpus/vcpu'):
75                 _qemu_threads.append(element.attrib['pid'])
76         except:
77             print('[ERROR]Parse xml file failed, could not get qemu threads.')
78
79         return _qemu_threads
80
81     def _get_mem_numa(self, xml_file):
82         try:
83             _mem_numa = None
84             tree = ET.ElementTree(file=xml_file)
85             root = tree.getroot()
86             for element in tree.iterfind('domain/numatune/memory'):
87                 _mem_numa = element.attrib['nodeset']
88         finally:
89             return _mem_numa
90
91     def _get_vhost_threads(self, xml_file):
92         _vhost = []
93         _main_pid = self._get_main_pid(xml_file)
94
95         # get vhost info
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'):
99             try:
100                 vhost = line.split()[1]
101                 _vhost.append(vhost)
102             except:
103                 print('get vhost %s proc id failed' % (line))
104
105         return _vhost
106
107     def get_vm_info(self, vm_name):
108         vm = {}
109         src_path = '/var/run/libvirt/qemu/'
110         xml_file = src_path + vm_name + '.xml'
111
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)
118         # get vhost thread
119         _vhosts = self._get_vhost_threads(xml_file)
120
121         vm['main_pid'] = _main_pid
122         vm['qemu_thread'] = _qemu_threads
123         vm['mem_numa'] = _mem_numa
124         vm['vhost_thread'] = _vhosts
125         return vm
126
127     def _get_proc_by_irq(self, irq):
128         try:
129             flag, info = commands.getstatusoutput('ps -ef | grep irq/%s | grep -v grep ' % (irq))
130             proc_id = info.split('\n')[0].split()[1]
131         except:
132             print("[ERROR]grep process id failed.")
133         return proc_id
134
135     def get_nic_interrupt_proc(self, nic):
136         _phy_nic_thread = []
137         flag, info = commands.getstatusoutput('cat /proc/interrupts | grep %s' % (nic))
138         for line in info.split('\n'):
139             try:
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])
143             except:
144                 print("[ERROR]get irq num failed.")
145         return _phy_nic_thread
146
147     def get_libvirt_vms(self):
148         vm_list = []
149         flag, info = commands.getstatusoutput('virsh list')
150         list = info.split('\n')
151         if list[-1] == '':
152             list.pop()
153         del list[0]
154         del list[0]
155
156         for line in list:
157             try:
158                 vm_temp = line.split()[1]
159                 vm_list.append(vm_temp)
160             except:
161                 print("Get vm name failed from %s" % (line))
162         return vm_list