Skip to content

Commit

Permalink
fix #614: [Linux] return the num of physical cores instead of physica…
Browse files Browse the repository at this point in the history
…l CPUs
  • Loading branch information
giampaolo committed Jun 3, 2015
1 parent 636fb7e commit 97fd107
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 6 deletions.
5 changes: 5 additions & 0 deletions CREDITS
Original file line number Diff line number Diff line change
Expand Up @@ -298,3 +298,8 @@ N: desbma
W: https://github.com/desbma
C: France
I: 628

N: John Burnett
W: http://www.johnburnett.com/
C: Irvine, CA, US
I: 614
2 changes: 2 additions & 0 deletions HISTORY.rst
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ Bug tracker at https://github.com/giampaolo/psutil/issues
- #593: [FreeBSD] Process().memory_maps() segfaults.
- #606: Process.parent() may swallow NoSuchProcess exceptions.
- #611: [SunOS] net_io_counters has send and received swapped
- #614: [Linux]: cpu_count(logical=False) return the number of physical CPUs
instead of physical cores.
- #618: [SunOS] swap tests fail on Solaris when run as normal user
- #628: [Linux] Process.name() truncates process name in case it contains
spaces or parentheses.
Expand Down
2 changes: 1 addition & 1 deletion psutil/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -1342,7 +1342,7 @@ def cpu_count(logical=True):
os.cpu_count() in Python 3.4).
If logical is False return the number of physical cores only
(hyper thread CPUs are excluded).
(e.g. hyper thread CPUs are excluded).
Return None if undetermined.
Expand Down
24 changes: 19 additions & 5 deletions psutil/_pslinux.py
Original file line number Diff line number Diff line change
Expand Up @@ -276,14 +276,28 @@ def cpu_count_logical():


def cpu_count_physical():
"""Return the number of physical CPUs in the system."""
"""Return the number of physical cores in the system."""
mapping = {}
current_info = {}
with open('/proc/cpuinfo', 'rb') as f:
found = set()
for line in f:
if line.lower().startswith(b'physical id'):
found.add(line.strip())
line = line.strip().lower()
if not line:
# new section
if (b'physical id' in current_info and
b'cpu cores' in current_info):
mapping[current_info[b'physical id']] = \
current_info[b'cpu cores']
current_info = {}
else:
# ongoing section
if (line.startswith(b'physical id') or
line.startswith(b'cpu cores')):
key, value = line.split(b'\t:', 1)
current_info[key] = int(value)

# mimic os.cpu_count()
return len(found) if found else None
return sum(mapping.values()) or None


# --- other system functions
Expand Down
11 changes: 11 additions & 0 deletions test/_linux.py
Original file line number Diff line number Diff line change
Expand Up @@ -204,6 +204,17 @@ def test_net_if_names(self):
self.assertEqual(len(nics), found, msg="%s\n---\n%s" % (
pprint.pformat(nics), out))

@unittest.skipUnless(which("nproc"), "nproc utility not available")
def test_cpu_count_logical_w_nproc(self):
num = int(sh("nproc --all"))
self.assertEqual(psutil.cpu_count(logical=True), num)

@unittest.skipUnless(which("lscpu"), "lscpu utility not available")
def test_cpu_count_logical_w_lscpu(self):
out = sh("lscpu -p")
num = len([x for x in out.split('\n') if not x.startswith('#')])
self.assertEqual(psutil.cpu_count(logical=True), num)

# --- tests for specific kernel versions

@unittest.skipUnless(
Expand Down

0 comments on commit 97fd107

Please sign in to comment.