Merge "Change PTL informatin in INFO"
[bottlenecks.git] / testsuites / vstf / vstf_scripts / vstf / agent / equalizer / get_info.py
1 ##############################################################################
2 # Copyright (c) 2015 Huawei Technologies Co.,Ltd and others.
3 #
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 ##############################################################################
9
10 import commands
11
12 try:
13     import xml.etree.cElementTree as ET
14 except ImportError:
15     import xml.etree.ElementTree as ET
16
17
18 class GetPhyInfo(object):
19
20     def __init__(self):
21         pass
22
23     def _get_range(self, temp):
24         topo = {}
25         phy_core_flag = True
26         for sub in temp.split(','):
27             r_list = []
28             _start = sub.split('-')[0]
29             _end = sub.split('-')[1]
30             r_list.extend(range(int(_start), int(_end) + 1))
31             if phy_core_flag:
32                 topo['phy_cores'] = r_list
33             else:
34                 topo['virt_cores'] = r_list
35             phy_core_flag = False
36         return topo
37
38     def _get_numa_num(self):
39         flag, num = commands.getstatusoutput('lscpu | grep "NUMA node(s):"')
40         try:
41             num = num.split(':')[1]
42         except:
43             print('get numa %s value failed.' % (num))
44         return num
45
46     def get_numa_core(self):
47         numa = {}
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"' %
52                 (str(numa_id)))
53             try:
54                 temp = temp.split(':')[1].split()[0]
55             except:
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
59         return str(numa)
60
61     def get_nic_numa(self, nic):
62         result = {}
63         try:
64             flag, id = commands.getstatusoutput(
65                 'cat /sys/class/net/%s/device/numa_node' %
66                 (nic))
67         except:
68             print('get nic numa id failed.')
69         return id
70
71     def _get_main_pid(self, xml_file):
72         try:
73             tree = ET.ElementTree(file=xml_file)
74             root = tree.getroot()
75             _main_pid = root.attrib['pid']
76         except:
77             print('[ERROR]Parse xml file failed, could not get qemu main pid')
78         return _main_pid
79
80     def _get_qemu_threads(self, xml_file):
81         # import pdb
82         # pdb.set_trace()
83         _qemu_threads = []
84         try:
85             tree = ET.ElementTree(file=xml_file)
86             root = tree.getroot()
87             for element in tree.iterfind('vcpus/vcpu'):
88                 _qemu_threads.append(element.attrib['pid'])
89         except:
90             print('[ERROR]Parse xml file failed, could not get qemu threads.')
91
92         return _qemu_threads
93
94     def _get_mem_numa(self, xml_file):
95         try:
96             _mem_numa = None
97             tree = ET.ElementTree(file=xml_file)
98             root = tree.getroot()
99             for element in tree.iterfind('domain/numatune/memory'):
100                 _mem_numa = element.attrib['nodeset']
101         finally:
102             return _mem_numa
103
104     def _get_vhost_threads(self, xml_file):
105         _vhost = []
106         _main_pid = self._get_main_pid(xml_file)
107
108         # get vhost info
109         proc_name = 'vhost-' + _main_pid
110         flag, temp = commands.getstatusoutput(
111             'ps -ef | grep %s | grep -v grep' %
112             (proc_name))
113         for line in temp.split('\n'):
114             try:
115                 vhost = line.split()[1]
116                 _vhost.append(vhost)
117             except:
118                 print('get vhost %s proc id failed' % (line))
119
120         return _vhost
121
122     def get_vm_info(self, vm_name):
123         vm = {}
124         src_path = '/var/run/libvirt/qemu/'
125         xml_file = src_path + vm_name + '.xml'
126
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)
133         # get vhost thread
134         _vhosts = self._get_vhost_threads(xml_file)
135
136         vm['main_pid'] = _main_pid
137         vm['qemu_thread'] = _qemu_threads
138         vm['mem_numa'] = _mem_numa
139         vm['vhost_thread'] = _vhosts
140         return vm
141
142     def _get_proc_by_irq(self, irq):
143         try:
144             flag, info = commands.getstatusoutput(
145                 'ps -ef | grep irq/%s | grep -v grep ' % (irq))
146             proc_id = info.split('\n')[0].split()[1]
147         except:
148             print("[ERROR]grep process id failed.")
149         return proc_id
150
151     def get_nic_interrupt_proc(self, nic):
152         _phy_nic_thread = []
153         flag, info = commands.getstatusoutput(
154             'cat /proc/interrupts | grep %s' % (nic))
155         for line in info.split('\n'):
156             try:
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])
160             except:
161                 print("[ERROR]get irq num failed.")
162         return _phy_nic_thread
163
164     def get_libvirt_vms(self):
165         vm_list = []
166         flag, info = commands.getstatusoutput('virsh list')
167         list = info.split('\n')
168         if list[-1] == '':
169             list.pop()
170         del list[0]
171         del list[0]
172
173         for line in list:
174             try:
175                 vm_temp = line.split()[1]
176                 vm_list.append(vm_temp)
177             except:
178                 print("Get vm name failed from %s" % (line))
179         return vm_list