summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorGiampaolo Rodola <g.rodola@gmail.com>2016-02-04 13:11:29 +0100
committerGiampaolo Rodola <g.rodola@gmail.com>2016-02-04 13:11:29 +0100
commitae8ba9f37773fe5cb1462aba821a79425ed00a40 (patch)
treece9ec90035b6393acf738a59a320371d0f497fb6
parent79163c6c17ac89fe9c84cd79b5f85e080c970727 (diff)
downloadpsutil-ae8ba9f37773fe5cb1462aba821a79425ed00a40.tar.gz
fix #755: Process.memory_percent() memtype parameter.
-rw-r--r--HISTORY.rst1
-rw-r--r--docs/index.rst12
-rw-r--r--psutil/__init__.py27
-rw-r--r--test/_sunos.py2
-rw-r--r--test/test_psutil.py10
5 files changed, 42 insertions, 10 deletions
diff --git a/HISTORY.rst b/HISTORY.rst
index 69026773..b09a2105 100644
--- a/HISTORY.rst
+++ b/HISTORY.rst
@@ -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**
diff --git a/docs/index.rst b/docs/index.rst
index 3a55c3b7..4c372465 100644
--- a/docs/index.rst
+++ b/docs/index.rst
@@ -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)
diff --git a/psutil/__init__.py b/psutil/__init__.py
index 06e04b4c..5a186c6e 100644
--- a/psutil/__init__.py
+++ b/psutil/__init__.py
@@ -971,15 +971,32 @@ class Process(object):
"""
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
diff --git a/test/_sunos.py b/test/_sunos.py
index 83fcade3..4df6e18f 100644
--- a/test/_sunos.py
+++ b/test/_sunos.py
@@ -10,7 +10,7 @@ import sys
import os
import psutil
-from psutil import SUNOS
+from psutil._common import SUNOS
from test_psutil import sh
from test_psutil import unittest
diff --git a/test/test_psutil.py b/test/test_psutil.py
index 3c38e262..be2049cf 100644
--- a/test/test_psutil.py
+++ b/test/test_psutil.py
@@ -1727,7 +1727,15 @@ class TestProcess(unittest.TestCase):
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)