]> git.ipfire.org Git - oddments/fireinfo.git/blob - src/fireinfo/cpu.py
e433a04f3dd90c59c74c4c3ac3af03186dc9b746
[oddments/fireinfo.git] / src / fireinfo / cpu.py
1 #!/usr/bin/python
2 ###############################################################################
3 # #
4 # Fireinfo #
5 # Copyright (C) 2010, 2011 IPFire Team (www.ipfire.org) #
6 # #
7 # This program is free software: you can redistribute it and/or modify #
8 # it under the terms of the GNU General Public License as published by #
9 # the Free Software Foundation, either version 3 of the License, or #
10 # (at your option) any later version. #
11 # #
12 # This program is distributed in the hope that it will be useful, #
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of #
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the #
15 # GNU General Public License for more details. #
16 # #
17 # You should have received a copy of the GNU General Public License #
18 # along with this program. If not, see <http://www.gnu.org/licenses/>. #
19 # #
20 ###############################################################################
21
22 import os
23
24 from . import system
25
26 PROC_CPUINFO = "/proc/cpuinfo"
27
28 class CPU(object):
29 """
30 A class that represents the first CPU in a system.
31
32 We get all information form the first CPU (or core) and assume that
33 all other ones are equal.
34 """
35
36 __cpuinfo = {}
37
38 def __init__(self):
39 """
40 Initialize this class by reading all data from /proc/cpuinfo.
41 """
42 self.__cpuinfo = self.read_cpuinfo()
43
44 @property
45 def system(self):
46 return system.System()
47
48 @staticmethod
49 def read_cpuinfo():
50 """
51 Read information from PROC_CPUINFO and store
52 it into a dictionary cpuinfo.
53 """
54 cpuinfo = {}
55
56 f = open(PROC_CPUINFO)
57 while True:
58 line = f.readline()
59
60 if not line:
61 break
62
63 try:
64 key, val = line.split(":", 1)
65 except ValueError:
66 # We got a line without key, pass that.
67 pass
68
69 key = key.strip().replace(" ", "_")
70 val = val.strip()
71
72 cpuinfo[key] = val
73
74 f.close()
75
76 return cpuinfo
77
78 @property
79 def bogomips(self):
80 """
81 Return the bogomips of this CPU.
82 """
83 bogomips = None
84
85 for key in ("bogomips", "BogoMIPS"):
86 try:
87 bogomips = self.__cpuinfo[key]
88 except KeyError:
89 continue
90
91 if bogomips:
92 return float(bogomips)
93
94 @property
95 def model(self):
96 """
97 Return the model id of this CPU.
98 """
99 try:
100 model = int(self.__cpuinfo["model"])
101 except KeyError:
102 model = None
103
104 return model
105
106 @property
107 def model_string(self):
108 """
109 Return the model string of this CPU.
110 """
111 for key in ("model_name", "Processor"):
112 try:
113 return self.__cpuinfo[key]
114 except KeyError:
115 pass
116
117 @property
118 def vendor(self):
119 """
120 Return the vendor string of this CPU.
121 """
122 try:
123 vendor = self.__cpuinfo["vendor_id"]
124 except KeyError:
125 if self.system.arch.startswith("arm"):
126 vendor = "ARM"
127 else:
128 vendor = ""
129
130 return vendor
131
132 @property
133 def stepping(self):
134 """
135 Return the stepping id of this CPU.
136 """
137 try:
138 stepping = int(self.__cpuinfo["stepping"])
139 except KeyError:
140 stepping = None
141
142 return stepping
143
144 @property
145 def flags(self):
146 """
147 Return all flags of this CPU.
148 """
149 try:
150 flags = self.__cpuinfo["flags"]
151 except KeyError:
152 flags = self.__cpuinfo["Features"]
153
154 return flags.split()
155
156 @property
157 def speed(self):
158 """
159 Return the speed (in MHz) of this CPU.
160 """
161 try:
162 speed = float(self.__cpuinfo["cpu_MHz"])
163 except KeyError:
164 speed = 0
165
166 return speed
167
168 @property
169 def family(self):
170 """
171 Return the family id of this CPU.
172 """
173 try:
174 family = int(self.__cpuinfo["cpu_family"])
175 except KeyError:
176 family = None
177
178 return family
179
180 @property
181 def count(self):
182 """
183 Count number of CPUs (cores).
184 """
185 return os.sysconf("SC_NPROCESSORS_ONLN")
186
187
188 if __name__ == "__main__":
189 c = CPU()
190
191 print("Vendor:", c.vendor)
192 print("Model:", c.model)
193 print("Stepping:", c.stepping)
194 print("Flags:", c.flags)
195 print("Bogomips:", c.bogomips)
196 print("Speed:", c.speed)
197 print("Family:", c.family)
198 print("Count:", c.count)
199 print("Model string:", c.model_string)