JIRA: BOTTLENECKS-29
[bottlenecks.git] / vstf / 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     def __init__(self):
20         pass
21
22     def _get_range(self, temp):
23         topo = {}
24         phy_core_flag = True
25         for sub in temp.split(','):
26             r_list = []
27             _start = sub.split('-')[0]
28             _end = sub.split('-')[1]
29             r_list.extend(range(int(_start), int(_end) + 1))
30             if phy_core_flag:
31                 topo['phy_cores'] = r_list
32             else:
33                 topo['virt_cores'] = r_list
34             phy_core_flag = False
35         return topo
36
37     def _get_numa_num(self):
38         flag, num = commands.getstatusoutput('lscpu | grep "NUMA node(s):"')
39         try:
40             num = num.split(':')[1]
41         except:
42             print('get numa %s value failed.' % (num))
43         return num
44
45     def get_numa_core(self):
46         numa = {}
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)))
50             try:
51                 temp = temp.split(':')[1].split()[0]
52             except:
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
56         return str(numa)
57
58     def get_nic_numa(self, nic):
59         result = {}
60         try:
61             flag, id = commands.getstatusoutput('cat /sys/class/net/%s/device/numa_node' % (nic))
62         except:
63             print('get nic numa id failed.')
64         return id
65
66     def _get_main_pid(self, xml_file):
67         try:
68             tree = ET.ElementTree(file=xml_file)
69             root = tree.getroot()
70             _main_pid = root.attrib['pid']
71         except:
72             print('[ERROR]Parse xml file failed, could not get qemu main pid')
73         return _main_pid
74
75     def _get_qemu_threads(self, xml_file):
76         # import pdb
77         # pdb.set_trace()
78         _qemu_threads = []
79         try:
80             tree = ET.ElementTree(file=xml_file)
81             root = tree.getroot()
82             for element in tree.iterfind('vcpus/vcpu'):
83                 _qemu_threads.append(element.attrib['pid'])
84         except:
85             print('[ERROR]Parse xml file failed, could not get qemu threads.')
86
87         return _qemu_threads
88
89     def _get_mem_numa(self, xml_file):
90         try:
91             _mem_numa = None
92             tree = ET.ElementTree(file=xml_file)
93             root = tree.getroot()
94             for element in tree.iterfind('domain/numatune/memory'):
95                 _mem_numa = element.attrib['nodeset']
96         finally:
97             return _mem_numa
98
99     def _get_vhost_threads(self, xml_file):
100         _vhost = []
101         _main_pid = self._get_main_pid(xml_file)
102
103         # get vhost info
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'):
107             try:
108                 vhost = line.split()[1]
109                 _vhost.append(vhost)
110             except:
111                 print('get vhost %s proc id failed' % (line))
112
113         return _vhost
114
115     def get_vm_info(self, vm_name):
116         vm = {}
117         src_path = '/var/run/libvirt/qemu/'
118         xml_file = src_path + vm_name + '.xml'
119
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)
126         # get vhost thread
127         _vhosts = self._get_vhost_threads(xml_file)
128
129         vm['main_pid'] = _main_pid
130         vm['qemu_thread'] = _qemu_threads
131         vm['mem_numa'] = _mem_numa
132         vm['vhost_thread'] = _vhosts
133         return vm
134
135     def _get_proc_by_irq(self, irq):
136         try:
137             flag, info = commands.getstatusoutput('ps -ef | grep irq/%s | grep -v grep ' % (irq))
138             proc_id = info.split('\n')[0].split()[1]
139         except:
140             print("[ERROR]grep process id failed.")
141         return proc_id
142
143     def get_nic_interrupt_proc(self, nic):
144         _phy_nic_thread = []
145         flag, info = commands.getstatusoutput('cat /proc/interrupts | grep %s' % (nic))
146         for line in info.split('\n'):
147             try:
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])
151             except:
152                 print("[ERROR]get irq num failed.")
153         return _phy_nic_thread
154
155     def get_libvirt_vms(self):
156         vm_list = []
157         flag, info = commands.getstatusoutput('virsh list')
158         list = info.split('\n')
159         if list[-1] == '':
160             list.pop()
161         del list[0]
162         del list[0]
163
164         for line in list:
165             try:
166                 vm_temp = line.split()[1]
167                 vm_list.append(vm_temp)
168             except:
169                 print("Get vm name failed from %s" % (line))
170         return vm_list