Add CPU count for GNU/Linux
@@ -15,15 +15,32 @@ class LinuxProcessors(CPU):
'logical': None # cores * threads
}
+ physical = [] # list of CPU ids from kernel
+ # open Linux kernel data file for CPU
+ with open('/proc/cpuinfo', 'rb') as fle:
+ lines = fle.readlines()
+ # go through the lines and obtain CPU core ids
+ for line in lines:
+ line = line.decode('utf-8')
+ if 'core id' not in line:
+ continue
+ cpuid = line.split(':')[1].strip()
+ physical.append(cpuid)
+ # total cores (socket * core per socket)
+ # is the length of unique CPU ids from kernel
+ physical = len(set(physical))
+ cpus['physical'] = physical
+
logical = Popen(
['nproc', '--all'],
stdout=PIPE
)
output = logical.communicate()[0].decode('utf-8').strip()
- environ['LANG'] = old_lang
if output:
cpus['logical'] = int(output)
+
+ environ['LANG'] = old_lang
return cpus
