]> git.ipfire.org Git - oddments/fireinfo.git/blame - fireinfo/cpu.py
Add sendprofile script (version 0.2).
[oddments/fireinfo.git] / fireinfo / cpu.py
CommitLineData
5b1919ae
SP
1#!/usr/bin/python
2
f6fe0dfc
MT
3import os
4
5b1919ae
SP
5import _fireinfo
6
f6fe0dfc
MT
7PROC_CPUINFO = "/proc/cpuinfo"
8SYS_CLASS_CPUID = "/sys/class/cpuid/cpu%d"
5b1919ae
SP
9
10class CPU(object):
8ef0b2a9
MT
11 __cpuinfo = {}
12
13 def __init__(self):
14 self.read_cpuinfo()
15
16 def read_cpuinfo(self):
17 """
f6fe0dfc 18 Read information from PROC_CPUINFO and store
8ef0b2a9
MT
19 it into a dictionary self.__cpuinfo.
20 """
f6fe0dfc 21 f = open(PROC_CPUINFO)
8ef0b2a9
MT
22 while True:
23 line = f.readline()
24
25 if not line:
26 break
27
28 try:
29 key, val = line.split(":", 1)
30 except ValueError:
31 # We got a line without key, pass that.
32 pass
33
34 key = key.strip().replace(" ", "_")
35 val = val.strip()
36
37 self.__cpuinfo[key] = val
38
39 f.close()
5b1919ae
SP
40
41 @property
42 def bogomips(self):
c2a02693 43 return float(self.__cpuinfo["bogomips"])
5b1919ae
SP
44
45 @property
46 def model(self):
c2a02693
MT
47 return int(self.__cpuinfo["model"])
48
49 @property
50 def model_string(self):
51 return self.__cpuinfo["model_name"]
5b1919ae
SP
52
53 @property
54 def vendor(self):
c2a02693 55 return self.__cpuinfo["vendor_id"]
5b1919ae
SP
56
57 @property
58 def stepping(self):
c2a02693 59 return int(self.__cpuinfo["stepping"])
5b1919ae
SP
60
61 @property
62 def flags(self):
c2a02693 63 return self.__cpuinfo["flags"].split()
5b1919ae
SP
64
65 @property
66 def speed(self):
63d47dcd 67 return float(self.__cpuinfo["cpu_MHz"])
5b1919ae 68
5b1919ae
SP
69 @property
70 def family(self):
c2a02693 71 return int(self.__cpuinfo["cpu_family"])
5b1919ae
SP
72
73 @property
74 def count(self):
f6fe0dfc
MT
75 """
76 Count number of CPUs (cores).
77 """
78 i = 0
79 while (os.path.exists(SYS_CLASS_CPUID % i)):
80 i += 1
81 return i
5b1919ae
SP
82
83
84if __name__ == "__main__":
8ef0b2a9 85 c = CPU()
5b1919ae
SP
86
87 print "Vendor:", c.vendor
88 print "Model:", c.model
89 print "Stepping:", c.stepping
90 print "Flags:", c.flags
91 print "Bogomips:", c.bogomips
92 print "Speed:", c.speed
5b1919ae
SP
93 print "Hypervisor:", c.hypervisor
94 print "Virtype:", c.virtype
95 print "Family:", c.family
96 print "Count:", c.count
c2a02693 97 print "Model string:", c.model_string