diff options
author | Giampaolo Rodola <g.rodola@gmail.com> | 2018-10-16 20:28:34 +0200 |
---|---|---|
committer | Giampaolo Rodola <g.rodola@gmail.com> | 2018-10-16 20:28:34 +0200 |
commit | 2f9dcf3c7b37d4da155df62bc835ddbdfe10b24f (patch) | |
tree | e878799a5ef0a8657ce0fd772f71ea6f4a2e6b85 | |
parent | 8c414c48d9db0f32194d8ac31ac7654e4483e024 (diff) | |
download | psutil-2f9dcf3c7b37d4da155df62bc835ddbdfe10b24f.tar.gz |
fix #1277 / osx / virtual_memory: 'available' and 'used' memory were not calculated properly
-rw-r--r-- | HISTORY.rst | 2 | ||||
-rw-r--r-- | psutil/_psosx.py | 11 | ||||
-rw-r--r-- | psutil/_psutil_osx.c | 6 | ||||
-rwxr-xr-x | psutil/tests/test_osx.py | 6 |
4 files changed, 14 insertions, 11 deletions
diff --git a/HISTORY.rst b/HISTORY.rst index 735b9345..4b2e7ed2 100644 --- a/HISTORY.rst +++ b/HISTORY.rst @@ -16,6 +16,8 @@ XXXX-XX-XX **Bug fixes** - 715_: do not print exception on import time in case cpu_times() fails. +- 1277_: [OSX] available and used memory (psutil.virtual_memory()) metrics are + not accurate. - 1294_: [Windows] psutil.Process().connections() may sometimes fail with intermittent 0xC0000001. (patch by Sylvain Duchesne) - 1320_: [AIX] system CPU times (psutil.cpu_times()) were being reported with diff --git a/psutil/_psosx.py b/psutil/_psosx.py index fbfedf3e..94e22bc7 100644 --- a/psutil/_psosx.py +++ b/psutil/_psosx.py @@ -119,9 +119,16 @@ pmmap_ext = namedtuple( def virtual_memory(): """System virtual memory as a namedtuple.""" - total, active, inactive, wired, free = cext.virtual_mem() + total, active, inactive, wired, free, speculative = cext.virtual_mem() + # This is how Zabbix calculate avail and used mem: + # https://github.com/zabbix/zabbix/blob/trunk/src/libs/zbxsysinfo/ + # osx/memory.c + # Also see: https://github.com/giampaolo/psutil/issues/1277 avail = inactive + free - used = active + inactive + wired + used = active + wired + # This is NOT how Zabbix calculates free mem but it matches "free" + # cmdline utility. + free -= speculative percent = usage_percent((total - avail), total, round_=1) return svmem(total, avail, percent, used, free, active, inactive, wired) diff --git a/psutil/_psutil_osx.c b/psutil/_psutil_osx.c index 0db93fce..be08de55 100644 --- a/psutil/_psutil_osx.c +++ b/psutil/_psutil_osx.c @@ -711,13 +711,13 @@ psutil_virtual_mem(PyObject *self, PyObject *args) { return NULL; return Py_BuildValue( - "KKKKK", + "KKKKKK", total, (unsigned long long) vm.active_count * pagesize, // active (unsigned long long) vm.inactive_count * pagesize, // inactive (unsigned long long) vm.wire_count * pagesize, // wired - // free mem; this is how vm_stat cmd does it - (unsigned long long) (vm.free_count - vm.speculative_count) * pagesize + (unsigned long long) vm.free_count * pagesize, // free + (unsigned long long) vm.speculative_count * pagesize // speculative ); } diff --git a/psutil/tests/test_osx.py b/psutil/tests/test_osx.py index 557af9f9..7aabebe6 100755 --- a/psutil/tests/test_osx.py +++ b/psutil/tests/test_osx.py @@ -226,12 +226,6 @@ class TestSystemAPIs(unittest.TestCase): self.assertAlmostEqual(psutil_val, vmstat_val, delta=MEMORY_TOLERANCE) @retry_before_failing() - def test_vmem_available(self): - vmstat_val = vm_stat("inactive") + vm_stat("free") - psutil_val = psutil.virtual_memory().available - self.assertAlmostEqual(psutil_val, vmstat_val, delta=MEMORY_TOLERANCE) - - @retry_before_failing() def test_vmem_active(self): vmstat_val = vm_stat("active") psutil_val = psutil.virtual_memory().active |