Skip to content

Commit

Permalink
giampaolo#1222 / linux / memory_full_info: we were erroneously adding…
Browse files Browse the repository at this point in the history
… "Pss:" and "SwapPss"
  • Loading branch information
giampaolo committed Mar 13, 2018
1 parent d67e977 commit c08ec74
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 5 deletions.
4 changes: 2 additions & 2 deletions HISTORY.rst
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ XXXX-XX-XX
**Bug fixes**

- 1216_: fix compatibility with python 2.6 on Windows (patch by Dan Vinakovsky)
- 1222_: [Linux] Process.memory_full_info() was summing both "swap PSS" (swap
proportional set size) and plain "swap". Not anymore.
- 1222_: [Linux] Process.memory_full_info() was erroneously summing "Swap:" and
"SwapPss:". Same for "Pss:" and "SwapPss". Not anymore.
- 1240_: [Windows] cpu_times() float loses accuracy in a long running system.
(patch by stswandering)
- 1245_: [Linux] sensors_temperatures() may fail with IOError "no such file".
Expand Down
7 changes: 4 additions & 3 deletions psutil/_pslinux.py
Original file line number Diff line number Diff line change
Expand Up @@ -1606,9 +1606,10 @@ def memory_info(self):
@wrap_exceptions
def memory_full_info(
self,
_private_re=re.compile(br"Private.*:\s+(\d+)"),
_pss_re=re.compile(br"Pss.*:\s+(\d+)"),
_swap_re=re.compile(br"Swap:\s+(\d+)")):
# Gets Private_Clean, Private_Dirty, Private_Hugetlb.
_private_re=re.compile(br"\nPrivate.*:\s+(\d+)"),
_pss_re=re.compile(br"\nPss\:\s+(\d+)"),
_swap_re=re.compile(br"\nSwap\:\s+(\d+)")):
basic_mem = self.memory_info()
# Note: using 3 regexes is faster than reading the file
# line by line.
Expand Down
40 changes: 40 additions & 0 deletions psutil/tests/test_linux.py
Original file line number Diff line number Diff line change
Expand Up @@ -1482,6 +1482,46 @@ def test_memory_full_info(self):
self.assertAlmostEqual(
mem.swap, sum([x.swap for x in maps]), delta=4096)

def test_memory_full_info_mocked(self):
# See: https://github.com/giampaolo/psutil/issues/1222
def open_mock(name, *args, **kwargs):
if name == "/proc/%s/smaps" % os.getpid():
return io.BytesIO(textwrap.dedent("""\
fffff0 r-xp 00000000 00:00 0 [vsyscall]
Size: 1 kB
Rss: 2 kB
Pss: 3 kB
Shared_Clean: 4 kB
Shared_Dirty: 5 kB
Private_Clean: 6 kB
Private_Dirty: 7 kB
Referenced: 8 kB
Anonymous: 9 kB
LazyFree: 10 kB
AnonHugePages: 11 kB
ShmemPmdMapped: 12 kB
Shared_Hugetlb: 13 kB
Private_Hugetlb: 14 kB
Swap: 15 kB
SwapPss: 16 kB
KernelPageSize: 17 kB
MMUPageSize: 18 kB
Locked: 19 kB
VmFlags: rd ex
""").encode())
else:
return orig_open(name, *args, **kwargs)

orig_open = open
patch_point = 'builtins.open' if PY3 else '__builtin__.open'
with mock.patch(patch_point, create=True, side_effect=open_mock) as m:
p = psutil.Process()
mem = p.memory_full_info()
assert m.called
self.assertEqual(mem.uss, (6 + 7 + 14) * 1024)
self.assertEqual(mem.pss, 3 * 1024)
self.assertEqual(mem.swap, 15 * 1024)

# On PYPY file descriptors are not closed fast enough.
@unittest.skipIf(PYPY, "unreliable on PYPY")
def test_open_files_mode(self):
Expand Down

0 comments on commit c08ec74

Please sign in to comment.