diff options
author | Giampaolo Rodola <g.rodola@gmail.com> | 2019-03-14 20:07:11 +0100 |
---|---|---|
committer | Giampaolo Rodola <g.rodola@gmail.com> | 2019-03-14 20:07:11 +0100 |
commit | b583b8b10d744412f0ecd0f8265f6cf73b147d77 (patch) | |
tree | 452de5175867a26f7c090ab507a75bc5644fe0b2 | |
parent | fc423782b90168344f29597bb0200cf11fe035dc (diff) | |
download | psutil-b583b8b10d744412f0ecd0f8265f6cf73b147d77.tar.gz |
issue #1404 / linux / phys CPUs count
determine CPUs from /sys/devices/system/cpu/cpu[0-9]/topology/core_id in
case /proc/cpuinfo does not provide this info
-rw-r--r-- | HISTORY.rst | 3 | ||||
-rw-r--r-- | psutil/_pslinux.py | 14 | ||||
-rwxr-xr-x | psutil/tests/test_linux.py | 10 |
3 files changed, 21 insertions, 6 deletions
diff --git a/HISTORY.rst b/HISTORY.rst index 9e2b5477..217effbd 100644 --- a/HISTORY.rst +++ b/HISTORY.rst @@ -5,6 +5,9 @@ **Enhancements** +- 1404_: [Linux] cpu_count(logical=False) uses a second method (read from + `/sys/devices/system/cpu/cpu[0-9]/topology/core_id`) in order to determine + the number of physical CPUs in case /proc/cpuinfo does not provide this info. - 1458_: provide coloured test output. Also show failures on KeyboardInterrupt. - 1464_: various docfixes (always point to python3 doc, fix links, etc.). diff --git a/psutil/_pslinux.py b/psutil/_pslinux.py index 41be6665..3502f1b1 100644 --- a/psutil/_pslinux.py +++ b/psutil/_pslinux.py @@ -627,6 +627,16 @@ def cpu_count_logical(): def cpu_count_physical(): """Return the number of physical cores in the system.""" + # Method #1 + core_ids = set() + for path in glob.glob("/sys/devices/system/cpu/cpu[0-9]/topology/core_id"): + with open_binary(path) as f: + core_ids.add(int(f.read())) + result = len(core_ids) + if result != 0: + return result + + # Method #2 mapping = {} current_info = {} with open_binary('%s/cpuinfo' % get_procfs_path()) as f: @@ -646,8 +656,8 @@ def cpu_count_physical(): key, value = line.split(b'\t:', 1) current_info[key] = int(value) - # mimic os.cpu_count() - return sum(mapping.values()) or None + result = sum(mapping.values()) + return result or None # mimic os.cpu_count() def cpu_stats(): diff --git a/psutil/tests/test_linux.py b/psutil/tests/test_linux.py index 394ce654..66c50aa6 100755 --- a/psutil/tests/test_linux.py +++ b/psutil/tests/test_linux.py @@ -685,10 +685,12 @@ class TestSystemCPUCountPhysical(unittest.TestCase): core_ids.add(fields[1]) self.assertEqual(psutil.cpu_count(logical=False), len(core_ids)) - def test_emulate_empty_cpuinfo(self): - with mock.patch('psutil._common.open', create=True) as m: - self.assertIsNone(psutil._pslinux.cpu_count_physical()) - assert m.called + def test_emulate_none(self): + with mock.patch('glob.glob', return_value=[]) as m1: + with mock.patch('psutil._common.open', create=True) as m2: + self.assertIsNone(psutil._pslinux.cpu_count_physical()) + assert m1.called + assert m2.called @unittest.skipIf(not LINUX, "LINUX only") |