From d6f268f6059fde4283276b0501ad145d85106313 Mon Sep 17 00:00:00 2001 From: David Brochart Date: Fri, 26 Apr 2019 05:06:23 +0200 Subject: [PATCH] Fix cpu freq (#1496) --- psutil/_pslinux.py | 22 +++++++++++++++++----- psutil/tests/test_linux.py | 29 +++++++++++++++++++++++------ psutil/tests/test_system.py | 3 ++- 3 files changed, 42 insertions(+), 12 deletions(-) diff --git a/psutil/_pslinux.py b/psutil/_pslinux.py index c8733e6fe..027846027 100644 --- a/psutil/_pslinux.py +++ b/psutil/_pslinux.py @@ -708,13 +708,25 @@ def cpu_freq(): min and max frequencies are not available and are set to None. """ ret = [] - with open_binary('%s/cpuinfo' % get_procfs_path()) as f: - for line in f: - if line.lower().startswith(b'cpu mhz'): - key, value = line.split(b'\t:', 1) - ret.append(_common.scpufreq(float(value), 0.0, 0.0)) + path = '%s/cpuinfo' % get_procfs_path() + if os.path.exists(path): + try: + with open_binary(path) as f: + for line in f: + if line.lower().startswith(b'cpu mhz'): + key, value = line.split(b'\t:', 1) + ret.append(_common.scpufreq(float(value), 0., 0.)) + except IOError as err: + raise NotImplementedError( + "%r for file %r" % (err, path)) return ret +else: + def cpu_freq(): + """Dummy implementation when none of the above files are present. + """ + return [] + # ===================================================================== # --- network diff --git a/psutil/tests/test_linux.py b/psutil/tests/test_linux.py index 4cdc9cbf7..a4b79e4fc 100755 --- a/psutil/tests/test_linux.py +++ b/psutil/tests/test_linux.py @@ -760,6 +760,8 @@ def open_mock(name, *args, **kwargs): elif (name.endswith('/scaling_max_freq') and name.startswith("/sys/devices/system/cpu/cpufreq/policy")): return io.BytesIO(b"700000") + elif name == '/proc/cpuinfo': + return io.BytesIO(b"cpu MHz : 500") else: return orig_open(name, *args, **kwargs) @@ -770,8 +772,12 @@ def open_mock(name, *args, **kwargs): 'os.path.exists', return_value=True): freq = psutil.cpu_freq() self.assertEqual(freq.current, 500.0) - self.assertEqual(freq.min, 600.0) - self.assertEqual(freq.max, 700.0) + # when /proc/cpuinfo is used min and max frequencies are not + # available and are set to 0. + if freq.min != 0.0: + self.assertEqual(freq.min, 600.0) + if freq.max != 0.0: + self.assertEqual(freq.max, 700.0) @unittest.skipIf(not HAS_CPU_FREQ, "not supported") def test_emulate_multi_cpu(self): @@ -795,6 +801,9 @@ def open_mock(name, *args, **kwargs): elif (n.endswith('/scaling_max_freq') and n.startswith("/sys/devices/system/cpu/cpufreq/policy1")): return io.BytesIO(b"600000") + elif name == '/proc/cpuinfo': + return io.BytesIO(b"cpu MHz : 100\n" + b"cpu MHz : 400") else: return orig_open(name, *args, **kwargs) @@ -806,11 +815,15 @@ def open_mock(name, *args, **kwargs): return_value=2): freq = psutil.cpu_freq(percpu=True) self.assertEqual(freq[0].current, 100.0) - self.assertEqual(freq[0].min, 200.0) - self.assertEqual(freq[0].max, 300.0) + if freq[0].min != 0.0: + self.assertEqual(freq[0].min, 200.0) + if freq[0].max != 0.0: + self.assertEqual(freq[0].max, 300.0) self.assertEqual(freq[1].current, 400.0) - self.assertEqual(freq[1].min, 500.0) - self.assertEqual(freq[1].max, 600.0) + if freq[1].min != 0.0: + self.assertEqual(freq[1].min, 500.0) + if freq[1].max != 0.0: + self.assertEqual(freq[1].max, 600.0) @unittest.skipIf(TRAVIS, "fails on Travis") @unittest.skipIf(not HAS_CPU_FREQ, "not supported") @@ -821,6 +834,8 @@ def open_mock(name, *args, **kwargs): raise IOError(errno.ENOENT, "") elif name.endswith('/cpuinfo_cur_freq'): return io.BytesIO(b"200000") + elif name == '/proc/cpuinfo': + return io.BytesIO(b"cpu MHz : 200") else: return orig_open(name, *args, **kwargs) @@ -841,6 +856,8 @@ def open_mock(name, *args, **kwargs): raise IOError(errno.ENOENT, "") elif name.endswith('/cpuinfo_cur_freq'): raise IOError(errno.ENOENT, "") + elif name == '/proc/cpuinfo': + raise IOError(errno.ENOENT, "") else: return orig_open(name, *args, **kwargs) diff --git a/psutil/tests/test_system.py b/psutil/tests/test_system.py index c4c503a21..eb9016f08 100755 --- a/psutil/tests/test_system.py +++ b/psutil/tests/test_system.py @@ -761,7 +761,8 @@ def test_cpu_freq(self): def check_ls(ls): for nt in ls: self.assertEqual(nt._fields, ('current', 'min', 'max')) - self.assertLessEqual(nt.current, nt.max) + if nt.max != 0.0: + self.assertLessEqual(nt.current, nt.max) for name in nt._fields: value = getattr(nt, name) self.assertIsInstance(value, (int, long, float))