Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Measure USS on Linux #744

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions docs/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -1006,7 +1006,7 @@ Process class
+--------+---------+-------+---------+--------------------+
| dirty | | | | nonpaged_pool |
+--------+---------+-------+---------+--------------------+
| | | | | pagefile |
| uss | | | | pagefile |
+--------+---------+-------+---------+--------------------+
| | | | | peak_pagefile |
+--------+---------+-------+---------+--------------------+
Expand All @@ -1020,7 +1020,7 @@ Process class
>>> import psutil
>>> p = psutil.Process()
>>> p.memory_info_ex()
pextmem(rss=15491072, vms=84025344, shared=5206016, text=2555904, lib=0, data=9891840, dirty=0)
pextmem(rss=15491072, vms=84025344, shared=5206016, text=2555904, lib=0, data=9891840, dirty=0, uss=7380992)

.. method:: memory_percent()

Expand Down
17 changes: 15 additions & 2 deletions psutil/_pslinux.py
Original file line number Diff line number Diff line change
Expand Up @@ -217,7 +217,7 @@ def set_scputimes_ntuple(procfs_path):
'svmem', ['total', 'available', 'percent', 'used', 'free',
'active', 'inactive', 'buffers', 'cached'])

pextmem = namedtuple('pextmem', 'rss vms shared text lib data dirty')
pextmem = namedtuple('pextmem', 'rss vms shared text lib data dirty uss')

pmmap_grouped = namedtuple(
'pmmap_grouped', ['path', 'rss', 'size', 'pss', 'shared_clean',
Expand Down Expand Up @@ -987,10 +987,20 @@ def memory_info_ex(self):
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 pextmem(rss, vms, shared, text, lib, data, dirty)
uss = self.calc_uss()
return pextmem(rss, vms, shared, text, lib, data, dirty, uss)

if os.path.exists('/proc/%s/smaps' % os.getpid()):

def calc_uss(self):
"""Calculates the USS of the process by inspecting the mapped
memory regions.
"""
with open_text("%s/%s/smaps" % (self._procfs_path, self.pid),
buffering=BIGGER_FILE_BUFFERING) as f:
p = re.compile(r"Private.*:\s+(\d+)")
return sum(map(int, p.findall(f.read()))) * 1024

@wrap_exceptions
def memory_maps(self):
"""Return process's mapped memory regions as a list of named tuples.
Expand Down Expand Up @@ -1051,6 +1061,9 @@ def get_blocks():
return ls

else:
def calc_uss(self):
return 0

def memory_maps(self):
msg = "couldn't find /proc/%s/smaps; kernel < 2.6.14 or " \
"CONFIG_MMU kernel configuration option is not enabled" \
Expand Down