]> git.ipfire.org Git - oddments/fireinfo.git/blobdiff - fireinfo/cpu.py
Support ARM in fireinfo.
[oddments/fireinfo.git] / fireinfo / cpu.py
index 267f63450718305a1ebec9ae1d5a68931fabde29..ed6317783d01de0046aa5e0a39df4a860e9e3b82 100644 (file)
 #!/usr/bin/python
+###############################################################################
+#                                                                             #
+# Fireinfo                                                                    #
+# Copyright (C) 2010, 2011 IPFire Team (www.ipfire.org)                       #
+#                                                                             #
+# This program is free software: you can redistribute it and/or modify        #
+# it under the terms of the GNU General Public License as published by        #
+# the Free Software Foundation, either version 3 of the License, or           #
+# (at your option) any later version.                                         #
+#                                                                             #
+# This program is distributed in the hope that it will be useful,             #
+# but WITHOUT ANY WARRANTY; without even the implied warranty of              #
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the               #
+# GNU General Public License for more details.                                #
+#                                                                             #
+# You should have received a copy of the GNU General Public License           #
+# along with this program.  If not, see <http://www.gnu.org/licenses/>.       #
+#                                                                             #
+###############################################################################
 
-import _fireinfo
+import os.path
 
+import system
+
+PROC_CPUINFO = "/proc/cpuinfo"
+SYS_CLASS_CPUID = "/sys/class/cpuid/cpu%d"
 
 class CPU(object):
-       __info = _fireinfo.cpuinfo()
+       """
+               A class that represents the first CPU in a system.
+
+               We get all information form the first CPU (or core) and assume that
+               all other ones are equal.
+       """
+
+       __cpuinfo = {}
+
+       def __init__(self):
+               """
+                       Initialize this class by reading all data from /proc/cpuinfo.
+               """
+               self.__cpuinfo = self.read_cpuinfo()
+
+       @property
+       def system(self):
+               return system.System()
+
+       @staticmethod
+       def read_cpuinfo():
+               """
+                       Read information from PROC_CPUINFO and store
+                       it into a dictionary cpuinfo.
+               """
+               cpuinfo = {}
+
+               f = open(PROC_CPUINFO)
+               while True:
+                       line = f.readline()
+
+                       if not line:
+                               break
+
+                       try:
+                               key, val = line.split(":", 1)
+                       except ValueError:
+                               # We got a line without key, pass that.
+                               pass
+
+                       key = key.strip().replace(" ", "_")
+                       val = val.strip()
+
+                       cpuinfo[key] = val
+
+               f.close()
+
+               return cpuinfo
 
        @property
        def bogomips(self):
-               return float(self.__info["bogomips"])
+               """
+                       Return the bogomips of this CPU.
+               """
+               try:
+                       bogomips = self.__cpuinfo["bogomips"]
+               except KeyError:
+                       bogomips = self.__cpuinfo["BogoMIPS"]
+
+               return float(bogomips)
 
        @property
        def model(self):
-               return int(self.__info["model"])
+               """
+                       Return the model id of this CPU.
+               """
+               try:
+                       model = int(self.__cpuinfo["model"])
+               except KeyError:
+                       model = None
+
+               return model
+
+       @property
+       def model_string(self):
+               """
+                       Return the model string of this CPU.
+               """
+               return self.__cpuinfo["Processor"]
 
        @property
        def vendor(self):
-               return self.__info["vendor"]
+               """
+                       Return the vendor string of this CPU.
+               """
+               try:
+                       vendor = self.__cpuinfo["vendor_id"]
+               except KeyError:
+                       if self.system.arch.startswith("arm"):
+                               vendor = "ARM"
+                       else:
+                               vendor = ""
+
+               return vendor
 
        @property
        def stepping(self):
-               return int(self.__info["stepping"])
+               """
+                       Return the stepping id of this CPU.
+               """
+               try:
+                       stepping = int(self.__cpuinfo["stepping"])
+               except KeyError:
+                       stepping = None
+
+               return stepping
 
        @property
        def flags(self):
-               return self.__info["flags"].split()
+               """
+                       Return all flags of this CPU.
+               """
+               try:
+                       flags = self.__cpuinfo["flags"]
+               except KeyError:
+                       flags = self.__cpuinfo["Features"]
 
-       @property
-       def speed(self):
-               mhz = float(self.__info["mhz"])
-               return round(mhz)
+               return flags.split()
 
        @property
-       def modes(self):
-               return self.__info.get("modes", [])
+       def speed(self):
+               """
+                       Return the speed (in MHz) of this CPU.
+               """
+               try:
+                       speed = float(self.__cpuinfo["cpu_MHz"])
+               except KeyError:
+                       speed = 0
 
-       @property
-       def hypervisor(self):
-               return self.__info["hypervisor"]
+               return speed
 
-       @property
-       def virtype(self):
-               return self.__info["virtype"]
-       
        @property
        def family(self):
-               return int(self.__info["family"])
+               """
+                       Return the family id of this CPU.
+               """
+               try:
+                       family = int(self.__cpuinfo["cpu_family"])
+               except KeyError:
+                       family = None
+
+               return family
        
        @property
        def count(self):
-               return int(self.__info["ncpus"])
-       
+               """
+                       Count number of CPUs (cores).
+               """
+               i = 0
+               while (os.path.exists(SYS_CLASS_CPUID % i)):
+                       i += 1
+
+               return i or 1
 
 
 if __name__ == "__main__":
-       c = CPU(0)
+       c = CPU()
 
        print "Vendor:", c.vendor
        print "Model:", c.model
@@ -62,8 +191,8 @@ if __name__ == "__main__":
        print "Flags:", c.flags
        print "Bogomips:", c.bogomips
        print "Speed:", c.speed
-       print "Modes:", c.modes
        print "Hypervisor:", c.hypervisor
        print "Virtype:", c.virtype
        print "Family:", c.family
        print "Count:", c.count
+       print "Model string:", c.model_string