diff options
author | Giampaolo Rodola <g.rodola@gmail.com> | 2016-02-04 15:20:23 +0100 |
---|---|---|
committer | Giampaolo Rodola <g.rodola@gmail.com> | 2016-02-04 15:20:23 +0100 |
commit | 0c8e1047e0f10516f1c1c36b0dcddb75f373506e (patch) | |
tree | 9090e2a93768dd2e49a1cfccdde16e863f601298 | |
parent | 2d5e89d111937c61e098c7a5136108e2729682d5 (diff) | |
download | psutil-0c8e1047e0f10516f1c1c36b0dcddb75f373506e.tar.gz |
have memory_info() return the stats which were previosuly returned by memory_info_ex; have memory_info_ex() return the same + uss/pss
-rw-r--r-- | docs/index.rst | 110 | ||||
-rw-r--r-- | psutil/_common.py | 2 | ||||
-rw-r--r-- | psutil/_pslinux.py | 21 | ||||
-rw-r--r-- | test/test_psutil.py | 4 |
4 files changed, 64 insertions, 73 deletions
diff --git a/docs/index.rst b/docs/index.rst index 444c1849..88449152 100644 --- a/docs/index.rst +++ b/docs/index.rst @@ -977,83 +977,48 @@ Process class .. method:: memory_info() - Return a tuple representing RSS (Resident Set Size) and VMS (Virtual - Memory Size) in bytes. On UNIX *rss* and *vms* are the same values shown - by `ps` or by RES and VIRT column of `top`. - On Windows *rss* and *vms* refer to "Mem Usage" and "VM Size" - columns of taskmgr.exe. For more detailed memory stats use - :meth:`memory_info_ex`. - - .. method:: memory_info_ex() - Return a namedtuple with variable fields depending on the platform representing extended memory information about the process. + The "portable" fields available on all plaforms are `rss` and `vms`. All numbers are expressed in bytes. + For more detailed memory stats use :meth:`memory_info_ex`. +---------+---------+-------+---------+--------------------+ | Linux | OSX | BSD | Solaris | Windows | +=========+=========+=======+=========+====================+ - | rss | rss | rss | rss | num_page_faults | + | rss | rss | rss | rss | rss | +---------+---------+-------+---------+--------------------+ - | vms | vms | vms | vms | peak_wset | + | vms | vms | vms | vms | vms | +---------+---------+-------+---------+--------------------+ - | shared | pfaults | text | | wset | + | shared | pfaults | text | | num_page_faults | +---------+---------+-------+---------+--------------------+ - | text | pageins | data | | peak_paged_pool | + | text | pageins | data | | peak_wset | +---------+---------+-------+---------+--------------------+ - | lib | **uss** | stack | | paged_pool | + | lib | | stack | | wset | +---------+---------+-------+---------+--------------------+ - | data | | | | peak_nonpaged_pool | + | data | | | | peak_paged_pool | +---------+---------+-------+---------+--------------------+ - | dirty | | | | nonpaged_pool | + | dirty | | | | paged_pool | +---------+---------+-------+---------+--------------------+ - | **uss** | | | | pagefile | + | | | | | peak_nonpaged_pool | +---------+---------+-------+---------+--------------------+ - | **pss** | | | | peak_pagefile | + | | | | | nonpaged_pool | +---------+---------+-------+---------+--------------------+ - | | | | | private | + | | | | | pagefile | +---------+---------+-------+---------+--------------------+ - | | | | | **uss** | + | | | | | peak_pagefile | +---------+---------+-------+---------+--------------------+ - .. note:: - the most representative value for determining how much memory is - used by a process on Linux, OSX and Windows is probably *uss*, which - is the amount of memory that would be freed if the process was - terminated right now. - Also *pss* on Linux is useful (read later). - :meth:`memory_info_ex` method calculates these two values separately, by - passing through process address space (which is quite expensive BTW). - If this cannot be done due to lack of permissions `uss` and `pss` will - be set to `0` (instead of raising :class:`psutil.AccessDenied`). - - **Linux, OSX, Windows** - - - **uss**: aka "Unique Set Size" this is the set of - pages that are unique to a process. This is the amount of memory that - would be freed if the process was terminated right now. - It will be set to `0` if it cannot be determined due to permission - issues. - - **Linux** - - - **pss**: aka "Proportional Set Size", is the amount of memory shared - with other processes, accounted in a way that the amount is divided - evenly between the processes that share it. - I.e. if a process has 10 MBs all to itself, and 10 MBs shared with - another process, its PSS will be 15 MBs. - "pss" value can be set to `0` if it cannot be determined due to - permission issues. - - **UNIX** - - **rss**: aka "Resident Set Size", this is the non-swapped physical memory a process has used. - It matches "top"'s RES column + On UNIX it matches "top"'s RES column (see `doc <http://linux.die.net/man/1/top>`__). + On Windows it matches "Mem Usage" column of taskmgr.exe. - **vms**: aka "Virtual Memory Size", this is the total amount of virtual - memory used by the process. This matches "top"'s VIRT column + memory used by the process. + On UNIX it matches "top"'s VIRT column (see `doc <http://linux.die.net/man/1/top>`__). + On Windows it matches "Mem Usage" "VM Size" column of taskmgr.exe. - **shared**: (Linux) memory that could be potentially shared with other processes. This matches "top"'s SHR column @@ -1069,10 +1034,43 @@ Process class - **lib**: (Linux) the memory used by shared libraries. - **dirty**: (Linux) the number of dirty pages. - **Windows** + For Windows fields rely on + `PROCESS_MEMORY_COUNTERS_EX <http://msdn.microsoft.com/en-us/library/windows/desktop/ms684874(v=vs.85).aspx>`__ structure doc. + + Example on Linux: - For Windows fields rely on - `PROCESS_MEMORY_COUNTERS_EX <http://msdn.microsoft.com/en-us/library/windows/desktop/ms684874(v=vs.85).aspx>`__ structure doc. + >>> import psutil + >>> p = psutil.Process() + >>> p.memory_info_ex() + pextmem(rss=15491072, vms=84025344, shared=5206016, text=2555904, lib=0, data=9891840, dirty=0) + + .. versionchanged:: 3.5.0 mutiple fields are returned, not only `rss` and + `vms`. + + .. method:: memory_info_ex() + + This returns the same fields as :meth:`memory_info_` plus some extra + fields on Linux (`uss`, `pss`), OSX (`uss`) and Windows (`uss`). + + - **uss**: (Linux, Windows, OSX) aka "Unique Set Size", this is the set of + pages that are unique to a process. This is the amount of memory that + would be freed if the process was terminated right now. + It will be set to `0` if it cannot be determined due to permission + issues. + + - **pss**: (Linux) aka "Proportional Set Size", is the amount of memory + shared with other processes, accounted in a way that the amount is + divided evenly between the processes that share it. + I.e. if a process has 10 MBs all to itself, and 10 MBs shared with + another process, its PSS will be 15 MBs. + "pss" value can be set to `0` if it cannot be determined due to + permission issues. + + .. note:: + `uss` is probably the most representative value for determining how much + memory is being used by a process. + It represents the amount of memory that would be freed if the process + was terminated right now. Example on Linux: diff --git a/psutil/_common.py b/psutil/_common.py index ef209343..78d59309 100644 --- a/psutil/_common.py +++ b/psutil/_common.py @@ -287,8 +287,6 @@ snicstats = namedtuple('snicstats', ['isup', 'duplex', 'speed', 'mtu']) # --- namedtuples for psutil.Process methods -# psutil.Process.memory_info() -pmem = namedtuple('pmem', ['rss', 'vms']) # psutil.Process.cpu_times() pcputimes = namedtuple('pcputimes', ['user', 'system']) # psutil.Process.open_files() diff --git a/psutil/_pslinux.py b/psutil/_pslinux.py index 6865efb1..22a95e58 100644 --- a/psutil/_pslinux.py +++ b/psutil/_pslinux.py @@ -218,6 +218,7 @@ svmem = namedtuple( 'svmem', ['total', 'available', 'percent', 'used', 'free', 'active', 'inactive', 'buffers', 'cached']) +pmem = namedtuple('pmem', 'rss vms shared text lib data dirty') pextmem = namedtuple('pextmem', 'rss vms shared text lib data dirty uss pss') pmmap_grouped = namedtuple( @@ -953,15 +954,6 @@ class Process(object): @wrap_exceptions def memory_info(self): - with open_binary("%s/%s/statm" % (self._procfs_path, self.pid)) as f: - vms, rss = f.readline().split()[:2] - return _common.pmem(int(rss) * PAGESIZE, - int(vms) * PAGESIZE) - - @wrap_exceptions - def memory_info_ex(self, - _private_re=re.compile(b"Private.*:\s+(\d+)"), - _pss_re=re.compile(b"Pss.*:\s+(\d+)")): # ============================================================ # | FIELD | DESCRIPTION | AKA | TOP | # ============================================================ @@ -972,14 +964,17 @@ class Process(object): # | lib | library (unused in Linux 2.6) | lrs | | # | data | data + stack | drs | DATA | # | dirty | dirty pages (unused in Linux 2.6) | dt | | - # | ----------------------------------------------------------- - # | uss | unique set size ("real memory") | | | - # | pss | proportional set size | | | # ============================================================ with open_binary("%s/%s/statm" % (self._procfs_path, self.pid)) as f: vms, rss, shared, text, lib, data, dirty = \ [int(x) * PAGESIZE for x in f.readline().split()[:7]] + return pmem(rss, vms, shared, text, lib, data, dirty) + @wrap_exceptions + def memory_info_ex(self, + _private_re=re.compile(b"Private.*:\s+(\d+)"), + _pss_re=re.compile(b"Pss.*:\s+(\d+)")): + base_mem = self.memory_info() uss = pss = 0 if HAS_SMAPS: # Note: using two regexes is faster than reading the file @@ -1001,7 +996,7 @@ class Process(object): # configuration option is not enabled. pass - return pextmem(rss, vms, shared, text, lib, data, dirty, uss, pss) + return pextmem(*base_mem + (uss, pss)) if HAS_SMAPS: diff --git a/test/test_psutil.py b/test/test_psutil.py index f4eee6bb..d29ddf15 100644 --- a/test/test_psutil.py +++ b/test/test_psutil.py @@ -1675,7 +1675,7 @@ class TestProcess(unittest.TestCase): p = psutil.Process() # step 1 - get a base value to compare our results - rss1, vms1 = p.memory_info() + rss1, vms1 = p.memory_info()[:2] percent1 = p.memory_percent() self.assertGreater(rss1, 0) self.assertGreater(vms1, 0) @@ -1683,7 +1683,7 @@ class TestProcess(unittest.TestCase): # step 2 - allocate some memory memarr = [None] * 1500000 - rss2, vms2 = p.memory_info() + rss2, vms2 = p.memory_info()[:2] percent2 = p.memory_percent() # make sure that the memory usage bumped up self.assertGreater(rss2, rss1) |