16
16
# You should have received a copy of the GNU General Public License
17
17
# along with Checkbox. If not, see <http://www.gnu.org/licenses/>.
19
22
from checkbox.lib.cache import cache
20
23
from checkbox.lib.conversion import string_to_type
22
25
from checkbox.properties import Path
23
from checkbox.registries.data import DataRegistry
24
26
from checkbox.registries.filename import FilenameRegistry
25
from checkbox.registries.link import LinkRegistry
28
class ProcessorRegistry(DataRegistry):
29
"""Registry for processor information.
31
Each item contained in this registry consists of the information
32
for a single processor in the /proc/cpuinfo file.
37
for line in [l.strip() for l in self.split("\n")]:
38
(key, value) = line.split(":", 1)
40
# Sanitize key so that it can be expressed as
43
key = key.replace(" ", "_")
46
# Rename processor entry to name
47
if key == "processor":
50
# Express value as a list if it is flags
29
def cpuinfo_to_processor(uname, cpuinfo):
45
platform_to_conversion = {
46
("i386", "i486", "i586", "i686", "x86_64",): {
48
"model": "model_name",
49
"model_number": "cpu_family",
50
"model_version": "model",
51
"model_revision": "stepping",
52
"cache": "cache_size",
55
("alpha", "alphaev6",): {
56
"count": "cpus_detected",
59
"model_number": "cpu_variation",
60
"model_version": ("system_type", "system_variation",),
61
"model_revision": "cpu_revision",
62
"other": "platform_string",
63
"speed": "cycle_frequency_[Hz]"},
67
"model_version": "archrev",
68
"model_revision": "revision",
74
"model_version": "revision",
77
("sparc64", "sparc",): {
78
"count": "ncpus_probed",
81
"model_version": "type",
84
processor["count"] = cpuinfo.get("count", 1)
85
processor["bogomips"] = int(round(float(cpuinfo.get("bogomips", "0.0"))))
86
for platform, conversion in platform_to_conversion.items():
88
for pkey, ckey in conversion.items():
89
if isinstance(ckey, (list, tuple)):
90
processor[pkey] = "/".join([cpuinfo[k] for k in ckey])
92
processor[pkey] = cpuinfo[ckey]
94
# Adjust platform and vendor
95
if uname[0] == "i" and uname[-2:] == "86":
96
processor["platform"] = "i386"
97
elif uname[:5] == "alpha":
98
processor["platform"] = "alpha"
99
elif uname[:5] == "sparc":
100
processor["vendor"] = "sun"
103
if processor["cache"]:
104
processor["cache"] = string_to_type(processor["cache"])
108
if uname[:5] == "alpha":
109
speed = processor["speed"].split()[0]
110
processor["speed"] = int(round(float(speed))) / 1000000
111
elif uname[:5] == "sparc":
112
speed = processor["speed"]
113
processor["speed"] = int(round(float(speed))) / 2
115
if uname[:3] == "ppc":
116
# String is appended with "mhz"
117
speed = processor["speed"][:-3]
55
value = string_to_type(value)
57
items.append((key, value))
59
items.append(("processor", LinkRegistry(self)))
119
speed = processor["speed"]
120
processor["speed"] = int(round(float(speed)) - 1)
122
processor["speed"] = -1
126
processor["count"] = int(processor["count"])
128
processor["count"] = 1
130
# There is at least one processor
131
if processor["count"] == 0:
132
processor["count"] = 1
64
137
class CpuinfoRegistry(FilenameRegistry):
71
144
# Filename where cpuinfo is stored.
72
145
filename = Path(default="/proc/cpuinfo")
147
# Filename where maximum frequency is stored.
148
frequency_filename = Path(default="/sys/devices/system/cpu/cpu0/cpufreq/cpuinfo_max_freq")
150
def _get_attributes(self):
153
for block in self.split("\n\n"):
161
for line in block.split("\n"):
165
key, value = line.split(":")
167
# Sanitize key and value
169
key = key.replace(" ", "_")
170
value = value.strip()
172
# Handle bogomips on sparc
173
if key.endswith("Bogo"):
176
attributes[key.lower()] = value
178
attributes["count"] = count
77
for data in [d.strip() for d in self.split("\n\n") if d]:
79
value = ProcessorRegistry(data)
80
items.append((key, value))
183
uname = os.uname()[4].lower()
184
attributes = self._get_attributes()
186
processor = cpuinfo_to_processor(uname, attributes)
188
# Check for frequency scaling
189
if posixpath.exists(self.frequency_filename):
190
speed = open(self.frequency_filename).read().strip()
191
processor["speed"] = int(speed) / 1000
193
return processor.items()
85
196
factory = CpuinfoRegistry