New source of cpu information
* Added new source of cpu information
Starting with kernel 4.13 /proc/cpuinfo only show the basic clock of the
processor not the current frequency. Change adds information from
/sys/devices/system/cpu , considering online and offline cpu's.
* PEP8 fixes
* Fix cpus_online_range
@@ -25,9 +25,28 @@ class CpuFreq(IntervalModule):
def createvaluesdict(self):
"""
- function processes the /proc/cpuinfo file
+ function processes the /proc/cpuinfo file, use file=/sys to use kernel >=4.13 location
:return: dictionary used as the full-text output for the module
"""
+ cpus_offline = 0
+ if self.file == '/sys':
+ with open('/sys/devices/system/cpu/online') as f:
+ line = f.readline()
+ cpus_online = [int(cpu) for cpu in line.split(',') if cpu.find('-') < 0]
+ cpus_online_range = [cpu_range for cpu_range in line.split(',') if cpu_range.find('-') > 0]
+
+ for cpu_range in cpus_online_range:
+ cpus_online += [cpu for cpu in range(int(cpu_range.split('-')[0]), int(cpu_range.split('-')[1]) + 1)]
+
+ mhz_values = [0.0 for cpu in range(max(cpus_online) + 1)]
+ ghz_values = [0.0 for cpu in range(max(cpus_online) + 1)]
+ for cpu in cpus_online:
+ with open('/sys/devices/system/cpu/cpu{}/cpufreq/scaling_cur_freq'.format(cpu)) as f:
+ line = f.readline()
+ mhz_values[cpu] = float(line.rstrip()) / 1000.0
+ ghz_values[cpu] = float(line.rstrip()) / 1000000.0
+ cpus_offline = mhz_values.count(0.0)
+ else:
with open(self.file) as f:
mhz_values = [float(line.split(':')[1]) for line in f if line.startswith('cpu MHz')]
ghz_values = [value / 1000.0 for value in mhz_values]
@@ -36,8 +55,8 @@ class CpuFreq(IntervalModule):
ghz = {"core{}g".format(key): "{0:1.2f}".format(value) for key, value in enumerate(ghz_values)}
cdict = mhz.copy()
cdict.update(ghz)
- cdict['avg'] = "{0:4.3f}".format(sum(mhz_values) / len(mhz_values))
- cdict['avgg'] = "{0:1.2f}".format(sum(ghz_values) / len(ghz_values), 2)
+ cdict['avg'] = "{0:4.3f}".format(sum(mhz_values) / (len(mhz_values) - cpus_offline))
+ cdict['avgg'] = "{0:1.2f}".format(sum(ghz_values) / (len(ghz_values) - cpus_offline), 2)
return cdict
def run(self):
