Skip to content

Commit

Permalink
fix #755: Process.memory_percent() memtype parameter.
Browse files Browse the repository at this point in the history
  • Loading branch information
giampaolo committed Feb 4, 2016
1 parent 79163c6 commit ae8ba9f
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 10 deletions.
1 change: 1 addition & 0 deletions HISTORY.rst
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ Bug tracker at https://github.com/giampaolo/psutil/issues
- #732: Process.environ(). (patch by Frank Benkstein)
- #753: [Linux, OSX, Windows] Process USS and PSS (Linux) "real" memory stats.
(patch by Eric Rahm)
- #755: Process.memory_percent() "memtype" parameter.

**Bug fixes**

Expand Down
12 changes: 9 additions & 3 deletions docs/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -1085,10 +1085,16 @@ Process class
.. versionchanged:: 3.5.0 added `uss` field on Linux, OSX and Windows.
.. versionchanged:: 3.5.0 added `pss` field on Linux.

.. method:: memory_percent()
.. method:: memory_percent(memtype="rss")

Compare physical system memory to process resident memory (RSS) and
calculate process memory utilization as a percentage.
Compare process memory to total physical system memory and calculate
process memory utilization as a percentage.
*memtype* argument is a string that dictates what type of process memory
you want to compare against (defaults to *"rss"*).
The list of available strings can be obtained like this:
``psutil.Process().memory_info_ex()._fields``.

.. versionchanged:: 3.5.0 added `memtype` parameter.

.. method:: memory_maps(grouped=True)

Expand Down
27 changes: 22 additions & 5 deletions psutil/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -971,15 +971,32 @@ def memory_info_ex(self):
"""
return self._proc.memory_info_ex()

def memory_percent(self):
"""Compare physical system memory to process resident memory
(RSS) and calculate process memory utilization as a percentage.
def memory_percent(self, memtype="rss"):
"""Compare process memory to total physical system memory and
calculate process memory utilization as a percentage.
'memtype' argument is a string that dictates what type of
process memory you want to compare against (defaults to "rss").
The list of available strings can be obtained like this:
>>> psutil.Process().memory_info_ex()._fields
('rss', 'vms', 'shared', 'text', 'lib', 'data', 'dirty', 'uss', 'pss')
"""
rss = self._proc.memory_info()[0]
if memtype in ("rss", "vsz"):
value = getattr(self.memory_info(), memtype)
else:
memex = self.memory_info_ex()
if memtype not in memex._fields:
raise ValueError("invalid memtype %r; valid types are %r" % (
memtype, memex._fields))
value = getattr(memex, memtype)
if value == 0 and memtype in ('uss', 'pss'):
raise AccessDenied(self.pid, self._name,
msg="can't retrieve %s memory" % memtype)

# use cached value if available
total_phymem = _TOTAL_PHYMEM or virtual_memory().total
try:
return (rss / float(total_phymem)) * 100
return (value / float(total_phymem)) * 100
except ZeroDivisionError:
return 0.0

Expand Down
2 changes: 1 addition & 1 deletion test/_sunos.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
import os

import psutil
from psutil import SUNOS
from psutil._common import SUNOS
from test_psutil import sh
from test_psutil import unittest

Expand Down
10 changes: 9 additions & 1 deletion test/test_psutil.py
Original file line number Diff line number Diff line change
Expand Up @@ -1727,7 +1727,15 @@ def test_memory_maps(self):

def test_memory_percent(self):
p = psutil.Process()
self.assertGreater(p.memory_percent(), 0.0)
ret = p.memory_percent()
assert 0 <= ret <= 100, ret
ret = p.memory_percent(memtype='vms')
assert 0 <= ret <= 100, ret
memtype = psutil._psplatform.pextmem._fields[-1]
ret = p.memory_percent(memtype=memtype)
assert 0 <= ret <= 100, ret
with self.assertRaises(ValueError):
p.memory_percent(memtype="?!?")

def test_is_running(self):
sproc = get_test_subprocess(wait=True)
Expand Down

0 comments on commit ae8ba9f

Please sign in to comment.