]> git.ipfire.org Git - oddments/fireinfo.git/blob - fireinfo/cpu.py
Add some code commenting.
[oddments/fireinfo.git] / 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.path
23
24 import _fireinfo
25
26 PROC_CPUINFO = "/proc/cpuinfo"
27 SYS_CLASS_CPUID = "/sys/class/cpuid/cpu%d"
28
29 class CPU(object):
30 """
31 A class that represents the first CPU in a system.
32
33 We get all information form the first CPU (or core) and assume that
34 all other ones are equal.
35 """
36
37 __cpuinfo = {}
38
39 def __init__(self):
40 """
41 Initialize this class by reading all data from /proc/cpuinfo.
42 """
43 self.read_cpuinfo()
44
45 def read_cpuinfo(self):
46 """
47 Read information from PROC_CPUINFO and store
48 it into a dictionary self.__cpuinfo.
49 """
50 f = open(PROC_CPUINFO)
51 while True:
52 line = f.readline()
53
54 if not line:
55 break
56
57 try:
58 key, val = line.split(":", 1)
59 except ValueError:
60 # We got a line without key, pass that.
61 pass
62
63 key = key.strip().replace(" ", "_")
64 val = val.strip()
65
66 self.__cpuinfo[key] = val
67
68 f.close()
69
70 @property
71 def bogomips(self):
72 """
73 Return the bogomips of this CPU.
74 """
75 return float(self.__cpuinfo["bogomips"])
76
77 @property
78 def model(self):
79 """
80 Return the model id of this CPU.
81 """
82 return int(self.__cpuinfo["model"])
83
84 @property
85 def model_string(self):
86 """
87 Return the model string of this CPU.
88 """
89 return self.__cpuinfo["model_name"]
90
91 @property
92 def vendor(self):
93 """
94 Return the vendor string of this CPU.
95 """
96 return self.__cpuinfo["vendor_id"]
97
98 @property
99 def stepping(self):
100 """
101 Return the stepping id of this CPU.
102 """
103 return int(self.__cpuinfo["stepping"])
104
105 @property
106 def flags(self):
107 """
108 Return all flags of this CPU.
109 """
110 return self.__cpuinfo["flags"].split()
111
112 @property
113 def speed(self):
114 """
115 Return the speed (in MHz) of this CPU.
116 """
117 return float(self.__cpuinfo["cpu_MHz"])
118
119 @property
120 def family(self):
121 """
122 Return the family id of this CPU.
123 """
124 return int(self.__cpuinfo["cpu_family"])
125
126 @property
127 def count(self):
128 """
129 Count number of CPUs (cores).
130 """
131 i = 0
132 while (os.path.exists(SYS_CLASS_CPUID % i)):
133 i += 1
134 return i
135
136
137 if __name__ == "__main__":
138 c = CPU()
139
140 print "Vendor:", c.vendor
141 print "Model:", c.model
142 print "Stepping:", c.stepping
143 print "Flags:", c.flags
144 print "Bogomips:", c.bogomips
145 print "Speed:", c.speed
146 print "Hypervisor:", c.hypervisor
147 print "Virtype:", c.virtype
148 print "Family:", c.family
149 print "Count:", c.count
150 print "Model string:", c.model_string