summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorGiampaolo Rodola <g.rodola@gmail.com>2016-09-18 16:34:48 +0200
committerGiampaolo Rodola <g.rodola@gmail.com>2016-09-18 16:34:48 +0200
commit75aecfefea66f66d3524d74ea15ea7210a278fec (patch)
tree575c3fe98d00ce4800da329504e007cb615373b3
parente67cf876f4e987d7f3ceebe3bd6caecf20086fa3 (diff)
downloadpsutil-75aecfefea66f66d3524d74ea15ea7210a278fec.tar.gz
#887: MemAvailable column may not be there; in that case calculate available mem the same way htop does
-rw-r--r--psutil/_pslinux.py21
-rw-r--r--psutil/tests/test_linux.py2
2 files changed, 21 insertions, 2 deletions
diff --git a/psutil/_pslinux.py b/psutil/_pslinux.py
index 82f79074..04ed4eb2 100644
--- a/psutil/_pslinux.py
+++ b/psutil/_pslinux.py
@@ -289,6 +289,16 @@ except Exception:
def virtual_memory():
+ """Report memory stats trying to match "free" and "vmstat -s" cmdline
+ utility values as much as possible.
+
+ This implementation uses procps-ng-3.3.12 as a reference (2016-09-18):
+ https://gitlab.com/procps-ng/procps/blob/
+ 24fd2605c51fccc375ab0287cec33aa767f06718/proc/sysinfo.c
+
+ ...or "free" / procps-ng-3.3.10 version which is available in Ubuntu
+ 16.04 and which should report the same numbers.
+ """
total, free, buffers, shared, _, _, unit_multiplier = cext.linux_sysinfo()
total *= unit_multiplier
free *= unit_multiplier
@@ -329,7 +339,16 @@ def virtual_memory():
# free and htop available memory differs as per:
# http://askubuntu.com/a/369589
# http://unix.stackexchange.com/a/65852/168884
- avail = mems['MemAvailable:']
+ try:
+ avail = mems['MemAvailable:']
+ except KeyError:
+ # Column is not there; it's likely this is an older kernel.
+ # In this case "free" won't show an "available" column.
+ # Also, procps does some hacky things:
+ # https://gitlab.com/procps-ng/procps/blob/
+ # /24fd2605c51fccc375ab0287cec33aa767f06718/proc/sysinfo.c#L774
+ # We won't. Like this we'll match "htop".
+ avail = free + buffers + cached
# XXX: this value matches "free", but not all the time, see:
# https://github.com/giampaolo/psutil/issues/685#issuecomment-202914057
diff --git a/psutil/tests/test_linux.py b/psutil/tests/test_linux.py
index 87765107..520ae8e3 100644
--- a/psutil/tests/test_linux.py
+++ b/psutil/tests/test_linux.py
@@ -191,7 +191,6 @@ class TestSystemVirtualMemory(unittest.TestCase):
vmstat_value, psutil_value, delta=MEMORY_TOLERANCE)
@retry_before_failing()
- @unittest.skipIf(TRAVIS, "fails on travis")
def test_shared(self):
free = free_physmem()
free_value = free.shared
@@ -202,6 +201,7 @@ class TestSystemVirtualMemory(unittest.TestCase):
free_value, psutil_value, delta=MEMORY_TOLERANCE,
msg='%s %s \n%s' % (free_value, psutil_value, free.output))
+ @retry_before_failing()
def test_available(self):
# "free" output format has changed at some point:
# https://github.com/giampaolo/psutil/issues/538#issuecomment-147192098