forked from kimchi-project/gingerbase
-
Notifications
You must be signed in to change notification settings - Fork 0
/
lscpu.py
186 lines (174 loc) · 6.88 KB
/
lscpu.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
#
# Project Ginger Base
#
# Copyright IBM, Corp. 2015
#
# This library is free software; you can redistribute it and/or
# modify it under the terms of the GNU Lesser General Public
# License as published by the Free Software Foundation; either
# version 2.1 of the License, or (at your option) any later version.
#
# This library is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
# Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public
# License along with this library; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
import logging
import platform
from wok.utils import run_command
from wok.exception import NotFoundError
ARCH = platform.machine()
class LsCpu(object):
"""
Get CPU information about a CPU hyper threading/architecture on x86
"""
def log_error(e):
"""
param e: error details to be logged
"""
log = logging.getLogger('Util')
log.warning('Exception in fetching the CPU architecture details: %s',
e)
def __init__(self):
self.lsCpuInfo = {}
try:
# lscpu - display information about the CPU architecture
out, error, rc = run_command(['lscpu'])
# Output of lscpu on x86 is expected to be:
# Architecture: x86_64
# CPU op-mode(s): 32-bit, 64-bit
# Byte Order: Little Endian
# CPU(s): 4
# On-line CPU(s) list: 0-3
# Thread(s) per core: 2
# Core(s) per socket: 2
# Socket(s): 1
# NUMA node(s): 1
# Vendor ID: GenuineIntel
# CPU family: 6
# Model: 42
# Model name: Intel(R) Core(TM) i5-2540M CPU @ 2.60GHz
# Stepping: 7
# CPU MHz: 976.421
# CPU max MHz: 3300.0000
# CPU min MHz: 800.0000
# BogoMIPS: 5182.99
# Virtualization: VT-x
# L1d cache: 32K
# L1i cache: 32K
# L2 cache: 256K
# L3 cache: 3072K
# NUMA node0 CPU(s): 0-3
#
# Output of lscpu in s390x is expected to be
# Architecture: s390x
# CPU op-mode(s): 32-bit, 64-bit
# Byte Order: Big Endian
# CPU(s): 4
# On-line CPU(s) list: 0,1
# Off-line CPU(s) list: 2,3
# Thread(s) per core: 1
# Core(s) per socket: 6
# Socket(s) per book: 6
# Book(s): 4
# Vendor ID: IBM/S390
# BogoMIPS: 18115.00
# Hypervisor: PR/SM
# Hypervisor vendor: IBM
# Virtualization type: full
# Dispatching mode: horizontal
# L1d cache: 96K
# L1i cache: 64K
# L2d cache: 1024K
# L2i cache: 1024K
if not rc and (not out.isspace()):
lscpuout = out.split('\n')
if lscpuout and len(lscpuout) > 0:
for line in lscpuout:
if ":" in line and (len(line.split(':')) == 2):
self.lsCpuInfo[line.split(':')[0].strip()] = \
line.split(':')[1].strip()
else:
continue
except Exception, e:
self.log_error(e)
raise NotFoundError("GGBCPUINF0004E")
def get_sockets(self):
"""
param self: object of the class self
return: Socket(s) (information about the CPU architecture)
"""
try:
sockets = "Socket(s)"
if ARCH.startswith('s390x'):
sockets = "Socket(s) per book"
if len(self.lsCpuInfo) > 0 and sockets in self.lsCpuInfo.keys():
return int(self.lsCpuInfo[sockets])
else:
raise NotFoundError("GGBCPUINF0005E")
except IndexError, e:
self.log_error(e)
raise NotFoundError("GGBCPUINF0005E")
def get_cores_per_socket(self):
"""
param self: object of the class self
return: Core(s) per socket (information about the CPU architecture)
"""
try:
cores_per_socket = "Core(s) per socket"
if len(self.lsCpuInfo) > 0 and cores_per_socket \
in self.lsCpuInfo.keys():
return int(self.lsCpuInfo[cores_per_socket])
else:
raise NotFoundError("GGBCPUINF0006E")
except IndexError, e:
self.log_error(e)
raise NotFoundError("GGBCPUINF0006E")
def get_threads_per_core(self):
"""
param self: object of the class self
return: Thread(s) per core (information about the CPU architecture)
"""
try:
threads_per_core = "Thread(s) per core"
if len(self.lsCpuInfo) > 0 and threads_per_core \
in self.lsCpuInfo.keys():
return int(self.lsCpuInfo[threads_per_core])
else:
raise NotFoundError("GGBCPUINF0007E")
except IndexError, e:
self.log_error(e)
raise NotFoundError("GGBCPUINF0007E")
def get_total_cpus(self):
"""
method to get total cpus retrieved from CPU(s) field of lscpu
:return: total cpus
"""
total_cpus = 'CPU(s)'
if len(self.lsCpuInfo) > 0 and total_cpus in self.lsCpuInfo.keys():
return int(self.lsCpuInfo[total_cpus])
else:
self.log_error("Failed to fetch total cpus count in lscpu output")
raise NotFoundError("GGBCPUINF0008E")
def get_hypervisor(self):
"""
method to get hypervisor name if present in lscpu o/p
:return: Hypervisor Name
"""
hypervisor = 'Hypervisor'
if len(self.lsCpuInfo) > 0 and hypervisor in self.lsCpuInfo.keys():
return self.lsCpuInfo[hypervisor]
return None
def get_hypervisor_vendor(self):
"""
method to get hypervisor vendor if present in lscpu o/p
:return: Hypervisor Vendor
"""
hypervisor_vendor = 'Hypervisor vendor'
if len(self.lsCpuInfo) > 0 and hypervisor_vendor in \
self.lsCpuInfo.keys():
return self.lsCpuInfo[hypervisor_vendor]
return None