Skip to content

[linux] cpu_count_physical is not correct #1620

Closed
@sctlee

Description

Platform

  • linux
  • psutil 5.6.2

Bug description
currently, psutil use '/sys/devices/system/cpu/cpu[0-9]*/topology/core_id' to calculate physical cores, but core id is only uniq in one socket.

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

Test results

[root@dce100 ~]# lscpu
Architecture:          x86_64
CPU op-mode(s):        32-bit, 64-bit
Byte Order:            Little Endian
CPU(s):                4
On-line CPU(s) list:   0-3
Thread(s) per core:    1
Core(s) per socket:    2
座:                 2
[root@dce100 ~]# cat /proc/cpuinfo | grep -E "^(physical|core|process)"
processor	: 0
physical id	: 0
core id		: 0
processor	: 1
physical id	: 0
core id		: 1
processor	: 2
physical id	: 1
core id		: 0
processor	: 3
physical id	: 1
core id		: 1
>>> import psutil
>>> psutil.cpu_count(logical=False)
2

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions