test_spec: LTD: Add Caution for Scaleability Address Time-out.
[vswitchperf.git] / tools / systeminfo.py
1 # Copyright 2015 Intel Corporation.
2 #
3 # Licensed under the Apache License, Version 2.0 (the "License");
4 # you may not use this file except in compliance with the License.
5 # You may obtain a copy of the License at
6 #
7 #   http://www.apache.org/licenses/LICENSE-2.0
8 #
9 # Unless required by applicable law or agreed to in writing, software
10 # distributed under the License is distributed on an "AS IS" BASIS,
11 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 # See the License for the specific language governing permissions and
13 # limitations under the License.
14
15 """Tools for access to OS details
16 """
17
18 import os
19 import platform
20 import subprocess
21 import locale
22
23 from conf import settings
24
25 def get_os():
26     """Get distro name.
27
28     :returns: Return distro name as a string
29     """
30     return ' '.join(platform.dist())
31
32 def get_kernel():
33     """Get kernel version.
34
35     :returns: Return kernel version as a string
36     """
37     return platform.release()
38
39 def get_cpu():
40     """Get CPU information.
41
42     :returns: Return CPU information as a string
43     """
44     with open('/proc/cpuinfo') as file_:
45         for line in file_:
46             if not line.strip():
47                 continue
48             if not line.rstrip('\n').startswith('model name'):
49                 continue
50
51             return line.rstrip('\n').split(':')[1]
52
53 def get_nic():
54     """Get NIC(s) information.
55
56     :returns: Return NIC(s) information as a string
57     """
58     nics = []
59     output = subprocess.check_output('lspci', shell=True)
60     output = output.decode(locale.getdefaultlocale()[1])
61     for line in output.split('\n'):
62         for nic_pciid in settings.getValue('WHITELIST_NICS'):
63             if line.startswith(nic_pciid):
64                 nics.append(''.join(line.split(':')[2:]).strip())
65     return ', '.join(nics).strip()
66
67 def get_platform():
68     """Get platform information.
69
70     Currently this is the motherboard vendor, name and socket
71     count.
72
73     :returns: Return platform information as a string
74     """
75     output = []
76
77     with open('/sys/class/dmi/id/board_vendor', 'r') as file_:
78         output.append(file_.readline().rstrip())
79
80     with open('/sys/class/dmi/id/board_name', 'r') as file_:
81         output.append(file_.readline().rstrip())
82
83     num_nodes = len([name for name in os.listdir(
84         '/sys/devices/system/node/') if name.startswith('node')])
85     output.append(''.join(['[', str(num_nodes), ' sockets]']))
86
87     return ' '.join(output).strip()
88
89 def get_cpu_cores():
90     """Get number of CPU cores.
91
92     :returns: Return number of CPU cores
93     """
94     cores = 0
95     with open('/proc/cpuinfo') as file_:
96         for line in file_:
97             if line.rstrip('\n').startswith('processor'):
98                 cores += 1
99             continue
100
101     # this code must be executed by at leat one core...
102     if cores < 1:
103         cores = 1
104     return cores
105
106 def get_memory():
107     """Get memory information.
108
109     :returns: amount of system memory as string together with unit
110     """
111     with open('/proc/meminfo') as file_:
112         for line in file_:
113             if not line.strip():
114                 continue
115             if not line.rstrip('\n').startswith('MemTotal'):
116                 continue
117
118             return line.rstrip('\n').split(':')[1].strip()
119
120 def get_memory_bytes():
121     """Get memory information in bytes
122
123     :returns: amount of system memory
124     """
125     mem_list = get_memory().split(' ')
126     mem = float(mem_list[0].strip())
127     if mem_list.__len__() > 1:
128         unit = mem_list[1].strip().lower()
129         if unit == 'kb':
130             mem *= 1024
131         elif unit == 'mb':
132             mem *= 1024 ** 2
133         elif unit == 'gb':
134             mem *= 1024 ** 3
135         elif unit == 'tb':
136             mem *= 1024 ** 4
137
138     return int(mem)
139
140 def get_pids(proc_names_list):
141     """ Get pid(s) of process(es) with given name(s)
142
143     :returns: list with pid(s) of given processes or None if processes
144         with given names are not running
145     """
146
147     try:
148         pids = subprocess.check_output(['pidof'] + proc_names_list)
149     except:
150         # such process isn't running
151         return None
152
153     return list(map(str, map(int, pids.split())))
154
155 def get_pid(proc_name_str):
156     """ Get pid(s) of process with given name
157
158     :returns: list with pid(s) of given process or None if process
159         with given name is not running
160     """
161     return get_pids([proc_name_str])