]> git.ipfire.org Git - oddments/fireinfo.git/blob - src/fireinfo/cpu.py
Exclude some more invalid patterns.
[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 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 try:
84 bogomips = self.__cpuinfo["bogomips"]
85 except KeyError:
86 bogomips = self.__cpuinfo["BogoMIPS"]
87
88 return float(bogomips)
89
90 @property
91 def model(self):
92 """
93 Return the model id of this CPU.
94 """
95 try:
96 model = int(self.__cpuinfo["model"])
97 except KeyError:
98 model = None
99
100 return model
101
102 @property
103 def model_string(self):
104 """
105 Return the model string of this CPU.
106 """
107 try:
108 return self.__cpuinfo["model_name"]
109 except KeyError:
110 return self.__cpuinfo["Processor"]
111
112 @property
113 def vendor(self):
114 """
115 Return the vendor string of this CPU.
116 """
117 try:
118 vendor = self.__cpuinfo["vendor_id"]
119 except KeyError:
120 if self.system.arch.startswith("arm"):
121 vendor = "ARM"
122 else:
123 vendor = ""
124
125 return vendor
126
127 @property
128 def stepping(self):
129 """
130 Return the stepping id of this CPU.
131 """
132 try:
133 stepping = int(self.__cpuinfo["stepping"])
134 except KeyError:
135 stepping = None
136
137 return stepping
138
139 @property
140 def flags(self):
141 """
142 Return all flags of this CPU.
143 """
144 try:
145 flags = self.__cpuinfo["flags"]
146 except KeyError:
147 flags = self.__cpuinfo["Features"]
148
149 return flags.split()
150
151 @property
152 def speed(self):
153 """
154 Return the speed (in MHz) of this CPU.
155 """
156 try:
157 speed = float(self.__cpuinfo["cpu_MHz"])
158 except KeyError:
159 speed = 0
160
161 return speed
162
163 @property
164 def family(self):
165 """
166 Return the family id of this CPU.
167 """
168 try:
169 family = int(self.__cpuinfo["cpu_family"])
170 except KeyError:
171 family = None
172
173 return family
174
175 @property
176 def count(self):
177 """
178 Count number of CPUs (cores).
179 """
180 return os.sysconf("SC_NPROCESSORS_ONLN")
181
182
183 if __name__ == "__main__":
184 c = CPU()
185
186 print "Vendor:", c.vendor
187 print "Model:", c.model
188 print "Stepping:", c.stepping
189 print "Flags:", c.flags
190 print "Bogomips:", c.bogomips
191 print "Speed:", c.speed
192 print "Family:", c.family
193 print "Count:", c.count
194 print "Model string:", c.model_string