2
# This file is part of Checkbox.
4
# Copyright 2011 Canonical Ltd.
6
# Checkbox is free software: you can redistribute it and/or modify
7
# it under the terms of the GNU General Public License as published by
8
# the Free Software Foundation, either version 3 of the License, or
9
# (at your option) any later version.
11
# Checkbox is distributed in the hope that it will be useful,
12
# but WITHOUT ANY WARRANTY; without even the implied warranty of
13
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14
# GNU General Public License for more details.
16
# You should have received a copy of the GNU General Public License
17
# along with Checkbox. If not, see <http://www.gnu.org/licenses/>.
23
from checkbox.lib.conversion import string_to_type
27
"""Parser for the /proc/cpuinfo file."""
29
def __init__(self, stream, machine=None):
31
self.machine = machine or uname()[4].lower()
33
def getAttributes(self):
36
cpuinfo = self.stream.read()
37
for block in re.split(r"\n{2,}", cpuinfo):
46
for line in block.split("\n"):
49
key, value = line.split(":", 1)
50
key, value = key.strip(), value.strip()
52
# Handle bogomips on sparc
53
if key.endswith("Bogo"):
56
attributes[key.lower()] = value
59
attributes["count"] = count
63
def run(self, result):
64
attributes = self.getAttributes()
69
machine = self.machine
84
platform_to_conversion = {
85
("i386", "i486", "i586", "i686", "x86_64",): {
87
"model": "model name",
88
"model_number": "cpu family",
89
"model_version": "model",
90
"model_revision": "stepping",
91
"cache": "cache size",
94
("alpha", "alphaev6",): {
95
"count": "cpus detected",
98
"model_number": "cpu variation",
99
"model_version": ("system type", "system variation",),
100
"model_revision": "cpu revision",
101
"other": "platform string",
102
"speed": "cycle frequency [Hz]"},
105
"model": "processor",
106
"model_number": "cpu variant",
107
"model_version": "cpu architecture",
108
"model_revision": "cpu revision",
109
"other": "features"},
113
"model_version": "archrev",
114
"model_revision": "revision",
120
"model_version": "revision",
122
("sparc64", "sparc",): {
123
"count": "ncpus probed",
126
"model_version": "type",
127
"speed": "bogomips"}}
129
processor["count"] = attributes.get("count", 1)
130
bogompips_string = attributes.get("bogomips", "0.0")
131
processor["bogomips"] = int(round(float(bogompips_string)))
132
for platform, conversion in platform_to_conversion.items():
133
if machine in platform:
134
for pkey, ckey in conversion.items():
135
if isinstance(ckey, (list, tuple)):
136
processor[pkey] = "/".join([attributes[k]
138
elif ckey in attributes:
139
processor[pkey] = attributes[ckey]
142
if machine[0] == "i" and machine[-2:] == "86":
143
processor["platform"] = "i386"
144
elif machine[:5] == "alpha":
145
processor["platform"] = "alpha"
148
if processor["cache"]:
149
processor["cache"] = string_to_type(processor["cache"])
153
if machine[:5] == "alpha":
154
speed = processor["speed"].split()[0]
155
processor["speed"] = int(round(float(speed))) / 1000000
156
elif machine[:5] == "sparc":
157
speed = processor["speed"]
158
processor["speed"] = int(round(float(speed))) / 2
160
if machine[:3] == "ppc":
161
# String is appended with "mhz"
162
speed = processor["speed"][:-3]
164
speed = processor["speed"]
165
processor["speed"] = int(round(float(speed)) - 1)
167
processor["speed"] = -1
171
processor["count"] = int(processor["count"])
173
processor["count"] = 1
175
# There is at least one processor
176
if processor["count"] == 0:
177
processor["count"] = 1
179
result.setProcessor(processor)