Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add option to retrieve number of performance and efficiency cores #2474

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 9 additions & 2 deletions psutil/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import collections
import contextlib
import datetime
import enum
import functools
import os
import signal
Expand Down Expand Up @@ -222,6 +223,12 @@
_LOWEST_PID = None
_SENTINEL = object()

class CPUCoreType(enum.Flag):
PERFORMANCE = enum.auto()
EFFICIENCY = enum.auto()
ALL = PERFORMANCE | EFFICIENCY


# Sanity check in case the user messed up with psutil installation
# or did something weird with sys.path. In this case we might end
# up importing a python module using a C extension module which
Expand Down Expand Up @@ -1644,7 +1651,7 @@ def check_gone(proc, timeout):
# =====================================================================


def cpu_count(logical=True):
def cpu_count(logical=True, core_type: CPUCoreType=CPUCoreType.ALL):
"""Return the number of logical CPUs in the system (same as
os.cpu_count() in Python 3.4).

Expand All @@ -1661,7 +1668,7 @@ def cpu_count(logical=True):
if logical:
ret = _psplatform.cpu_count_logical()
else:
ret = _psplatform.cpu_count_cores()
ret = _psplatform.cpu_count_cores(core_type=core_type)
if ret is not None and ret < 1:
ret = None
return ret
Expand Down
4 changes: 2 additions & 2 deletions psutil/_psaix.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
import sys
from collections import namedtuple

from . import _common
from . import CPUCoreType, _common
from . import _psposix
from . import _psutil_aix as cext
from . import _psutil_posix as cext_posix
Expand Down Expand Up @@ -144,7 +144,7 @@ def cpu_count_logical():
return None


def cpu_count_cores():
def cpu_count_cores(core_type: CPUCoreType=CPUCoreType.ALL):
cmd = ["lsdev", "-Cc", "processor"]
p = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
stdout, stderr = p.communicate()
Expand Down
6 changes: 3 additions & 3 deletions psutil/_psbsd.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
from collections import namedtuple
from xml.etree import ElementTree # noqa ICN001

from . import _common
from . import CPUCoreType, _common
from . import _psposix
from . import _psutil_bsd as cext
from . import _psutil_posix as cext_posix
Expand Down Expand Up @@ -283,13 +283,13 @@ def cpu_count_logical():

if OPENBSD or NETBSD:

def cpu_count_cores():
def cpu_count_cores(core_type: CPUCoreType=CPUCoreType.ALL):
# OpenBSD and NetBSD do not implement this.
return 1 if cpu_count_logical() == 1 else None

else:

def cpu_count_cores():
def cpu_count_cores(core_type: CPUCoreType=CPUCoreType.ALL):
"""Return the number of CPU cores in the system."""
# From the C module we'll get an XML string similar to this:
# http://manpages.ubuntu.com/manpages/precise/man4/smp.4freebsd.html
Expand Down
4 changes: 2 additions & 2 deletions psutil/_pslinux.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
from collections import defaultdict
from collections import namedtuple

from . import _common
from . import CPUCoreType, _common
from . import _psposix
from . import _psutil_linux as cext
from . import _psutil_posix as cext_posix
Expand Down Expand Up @@ -673,7 +673,7 @@ def cpu_count_logical():
return num


def cpu_count_cores():
def cpu_count_cores(core_type: CPUCoreType=CPUCoreType.ALL):
"""Return the number of CPU cores in the system."""
# Method #1
ls = set()
Expand Down
4 changes: 2 additions & 2 deletions psutil/_psosx.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
import os
from collections import namedtuple

from . import _common
from . import CPUCoreType, _common
from . import _psposix
from . import _psutil_osx as cext
from . import _psutil_posix as cext_posix
Expand Down Expand Up @@ -159,7 +159,7 @@ def cpu_count_logical():
return cext.cpu_count_logical()


def cpu_count_cores():
def cpu_count_cores(core_type: CPUCoreType=CPUCoreType.ALL):
"""Return the number of CPU cores in the system."""
return cext.cpu_count_cores()

Expand Down
4 changes: 2 additions & 2 deletions psutil/_pssunos.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
from collections import namedtuple
from socket import AF_INET

from . import _common
from . import CPUCoreType, _common
from . import _psposix
from . import _psutil_posix as cext_posix
from . import _psutil_sunos as cext
Expand Down Expand Up @@ -202,7 +202,7 @@ def cpu_count_logical():
return None


def cpu_count_cores():
def cpu_count_cores(core_type: CPUCoreType=CPUCoreType.ALL):
"""Return the number of CPU cores in the system."""
return cext.cpu_count_cores()

Expand Down
4 changes: 2 additions & 2 deletions psutil/_pswindows.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
import time
from collections import namedtuple

from . import _common
from . import CPUCoreType, _common
from ._common import ENCODING
from ._common import ENCODING_ERRS
from ._common import AccessDenied
Expand Down Expand Up @@ -330,7 +330,7 @@ def cpu_count_logical():
return cext.cpu_count_logical()


def cpu_count_cores():
def cpu_count_cores(core_type: CPUCoreType=CPUCoreType.ALL):
"""Return the number of CPU cores in the system."""
return cext.cpu_count_cores()

Expand Down