diff options
author | Giampaolo Rodola <g.rodola@gmail.com> | 2020-10-17 04:57:36 +0200 |
---|---|---|
committer | Giampaolo Rodola <g.rodola@gmail.com> | 2020-10-17 04:57:36 +0200 |
commit | bfae1fc4a371c9e08f2c3f5053a80542e43d18f7 (patch) | |
tree | 886c46ed0363029c5f4ad7ce89c0ef6eefe31077 | |
parent | f1e2137e9f7198a087ccf706201c648313bdae5a (diff) | |
download | psutil-bfae1fc4a371c9e08f2c3f5053a80542e43d18f7.tar.gz |
linux / cpu_count phys: take depreated */thread_siblings_list into
account
/sys/devices/system/cpu/cpu[0-9]*/topology/thread_siblings_list is
deprecated
/sys/devices/system/cpu/cpu[0-9]*/topology/core_cpus_list is the new
name
also add test which makes sure method 1 and 2 return the same result
-rw-r--r-- | CREDITS | 4 | ||||
-rw-r--r-- | HISTORY.rst | 2 | ||||
-rw-r--r-- | psutil/_pslinux.py | 24 | ||||
-rwxr-xr-x | psutil/tests/test_linux.py | 8 |
4 files changed, 29 insertions, 9 deletions
@@ -716,3 +716,7 @@ I: 1830 N: aristocratos W: https://github.com/aristocratos I: 1837, 1838 + +N: Vincent A. Arcila +W: https://github.com/jandrovins +I: 1620, 1727 diff --git a/HISTORY.rst b/HISTORY.rst index 3be3915f..1ef5573a 100644 --- a/HISTORY.rst +++ b/HISTORY.rst @@ -15,6 +15,8 @@ XXXX-XX-XX **Bug fixes** +- 1620_: [Linux] physical cpu_count() result is incorrect on systems with more + than one CPU socket. (patch by Vincent A. Arcila) - 1738_: [macOS] Process.exe() may raise FileNotFoundError if process is still alive but the exe file which launched it got deleted. - 1791_: [macOS] fix missing include for getpagesize(). diff --git a/psutil/_pslinux.py b/psutil/_pslinux.py index 153ddc29..d5ce358c 100644 --- a/psutil/_pslinux.py +++ b/psutil/_pslinux.py @@ -618,12 +618,18 @@ def cpu_count_logical(): def cpu_count_physical(): """Return the number of physical cores in the system.""" # Method #1 - thread_siblings_lists = set() - for path in glob.glob( - "/sys/devices/system/cpu/cpu[0-9]*/topology/thread_siblings_list"): + ls = set() + # These 2 files are the same but */core_cpus_list is newer while + # */thread_siblings_list is deprecated and may disappear in the future. + # https://www.kernel.org/doc/Documentation/admin-guide/cputopology.rst + # https://github.com/giampaolo/psutil/pull/1727#issuecomment-707624964 + # https://lkml.org/lkml/2019/2/26/41 + p1 = "/sys/devices/system/cpu/cpu[0-9]*/topology/core_cpus_list" + p2 = "/sys/devices/system/cpu/cpu[0-9]*/topology/thread_siblings_list" + for path in glob.glob(p1) or glob.glob(p2): with open_binary(path) as f: - thread_siblings_lists.add(f.read()) - result = len(thread_siblings_lists) + ls.add(f.read().strip()) + result = len(ls) if result != 0: return result @@ -635,15 +641,15 @@ def cpu_count_physical(): line = line.strip().lower() if not line: # new section - if (b'physical id' in current_info and - b'cpu cores' in current_info): + try: mapping[current_info[b'physical id']] = \ current_info[b'cpu cores'] + except KeyError: + pass current_info = {} else: # ongoing section - if (line.startswith(b'physical id') or - line.startswith(b'cpu cores')): + if line.startswith((b'physical id', b'cpu cores')): key, value = line.split(b'\t:', 1) current_info[key] = int(value) diff --git a/psutil/tests/test_linux.py b/psutil/tests/test_linux.py index 0d247aa5..b303ed6d 100755 --- a/psutil/tests/test_linux.py +++ b/psutil/tests/test_linux.py @@ -734,6 +734,14 @@ class TestSystemCPUCountPhysical(PsutilTestCase): core_ids.add(fields[1]) self.assertEqual(psutil.cpu_count(logical=False), len(core_ids)) + def test_method_2(self): + meth_1 = psutil._pslinux.cpu_count_physical() + with mock.patch('glob.glob', return_value=[]) as m: + meth_2 = psutil._pslinux.cpu_count_physical() + assert m.called + if meth_1 is not None: + self.assertEqual(meth_1, meth_2) + def test_emulate_none(self): with mock.patch('glob.glob', return_value=[]) as m1: with mock.patch('psutil._common.open', create=True) as m2: |