import os.path
+import system
+
PROC_CPUINFO = "/proc/cpuinfo"
SYS_CLASS_CPUID = "/sys/class/cpuid/cpu%d"
"""
Initialize this class by reading all data from /proc/cpuinfo.
"""
- self.read_cpuinfo()
+ self.__cpuinfo = self.read_cpuinfo()
+
+ @property
+ def system(self):
+ return system.System()
- def read_cpuinfo(self):
+ @staticmethod
+ def read_cpuinfo():
"""
Read information from PROC_CPUINFO and store
- it into a dictionary self.__cpuinfo.
+ it into a dictionary cpuinfo.
"""
+ cpuinfo = {}
+
f = open(PROC_CPUINFO)
while True:
line = f.readline()
key = key.strip().replace(" ", "_")
val = val.strip()
- self.__cpuinfo[key] = val
+ cpuinfo[key] = val
f.close()
+ return cpuinfo
+
@property
def bogomips(self):
"""
Return the bogomips of this CPU.
"""
- return float(self.__cpuinfo["bogomips"])
+ try:
+ bogomips = self.__cpuinfo["bogomips"]
+ except KeyError:
+ bogomips = self.__cpuinfo["BogoMIPS"]
+
+ return float(bogomips)
@property
def model(self):
"""
Return the model id of this CPU.
"""
- return int(self.__cpuinfo["model"])
+ 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["model_name"]
+ return self.__cpuinfo["Processor"]
@property
def vendor(self):
"""
Return the vendor string of this CPU.
"""
- return self.__cpuinfo["vendor_id"]
+ 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 the stepping id of this CPU.
"""
- return int(self.__cpuinfo["stepping"])
+ try:
+ stepping = int(self.__cpuinfo["stepping"])
+ except KeyError:
+ stepping = None
+
+ return stepping
@property
def flags(self):
"""
Return all flags of this CPU.
"""
- return self.__cpuinfo["flags"].split()
+ try:
+ flags = self.__cpuinfo["flags"]
+ except KeyError:
+ flags = self.__cpuinfo["Features"]
+
+ return flags.split()
@property
def speed(self):
"""
Return the speed (in MHz) of this CPU.
"""
- return float(self.__cpuinfo["cpu_MHz"])
+ try:
+ speed = float(self.__cpuinfo["cpu_MHz"])
+ except KeyError:
+ speed = 0
+
+ return speed
@property
def family(self):
"""
Return the family id of this CPU.
"""
- return int(self.__cpuinfo["cpu_family"])
+ try:
+ family = int(self.__cpuinfo["cpu_family"])
+ except KeyError:
+ family = None
+
+ return family
@property
def count(self):
i = 0
while (os.path.exists(SYS_CLASS_CPUID % i)):
i += 1
- return i
+
+ return i or 1
if __name__ == "__main__":
self.cpu = cpu.CPU()
self.hypervisor = hypervisor.Hypervisor()
+ # Read /proc/cpuinfo for vendor information.
+ self.__cpuinfo = self.cpu.read_cpuinfo()
+
def profile(self):
p = {}
p["system"] = {
"""
Return the system release string.
"""
- return read_from_file("/etc/system-release")
+ return read_from_file("/etc/system-release") or "unknown"
@property
def bios_vendor(self):
"""
return read_from_file("/sys/class/dmi/id/bios_vendor")
+ def vendor_model_tuple(self):
+ try:
+ s = self.__cpuinfo["Hardware"]
+ except KeyError:
+ return (None, None)
+
+ if s.startswith("ARM-Versatile"):
+ return ("ARM", s)
+
+ v, m = s.split(maxsplit=1)
+
+ return v, m
+
@property
def vendor(self):
"""
if ret:
break
+ if ret is None:
+ v, m = self.vendor_model_tuple()
+ ret = v
+
return ret
@property
if ret:
break
+ if ret is None:
+ v, m = self.vendor_model_tuple()
+ ret = m
+
return ret
@property
}
}
+#if defined(__x86_64__) || defined(__i386__)
static bool
is_virtualized() {
unsigned int eax, ebx, ecx, edx;
: "memory"
);
}
+#else
+static bool
+is_virtualized() {
+ /*
+ Always return false, because other architectures
+ do not support the virtualization bit.
+ */
+ return false;
+}
+
+void
+hypervisor_port(unsigned int cmd, unsigned int *eax, unsigned int *ebx,
+ unsigned int *ecx, unsigned int *edx)
+{
+}
+#endif
int
hypervisor_port_check(void) {