diff options
author | Giampaolo Rodola <g.rodola@gmail.com> | 2015-11-25 20:32:54 +0100 |
---|---|---|
committer | Giampaolo Rodola <g.rodola@gmail.com> | 2015-11-25 20:32:54 +0100 |
commit | 991bd6807bb5bc0edfc01e666dce1e18267a8a81 (patch) | |
tree | 7e16f452564ab175c9945a61f0b05f6ae96ca22b | |
parent | 8485b4ef130c50a63e79dafe6f77f5e72d4a289a (diff) | |
download | psutil-991bd6807bb5bc0edfc01e666dce1e18267a8a81.tar.gz |
#714: [OpenBSD] return the right physical mem value
-rw-r--r-- | psutil/arch/bsd/openbsd.c | 13 | ||||
-rw-r--r-- | test/_openbsd.py | 6 |
2 files changed, 15 insertions, 4 deletions
diff --git a/psutil/arch/bsd/openbsd.c b/psutil/arch/bsd/openbsd.c index fc1673c3..4797b739 100644 --- a/psutil/arch/bsd/openbsd.c +++ b/psutil/arch/bsd/openbsd.c @@ -322,13 +322,21 @@ error: PyObject * psutil_virtual_mem(PyObject *self, PyObject *args) { + int64_t total_physmem; int uvmexp_mib[] = {CTL_VM, VM_UVMEXP}; int bcstats_mib[] = {CTL_VFS, VFS_GENERIC, VFS_BCACHESTAT}; + int physmem_mib[] = {CTL_HW, HW_PHYSMEM64}; size_t size; struct uvmexp uvmexp; struct bcachestats bcstats; long pagesize = getpagesize(); + size = sizeof(total_physmem); + if (sysctl(physmem_mib, 2, &total_physmem, &size, NULL, 0) < 0) { + PyErr_SetFromErrno(PyExc_OSError); + return NULL; + } + size = sizeof(uvmexp); if (sysctl(uvmexp_mib, 2, &uvmexp, &size, NULL, 0) < 0) { PyErr_SetFromErrno(PyExc_OSError); @@ -343,7 +351,10 @@ psutil_virtual_mem(PyObject *self, PyObject *args) { } return Py_BuildValue("KKKKKKKK", - (unsigned long long) uvmexp.npages * pagesize, + // Note: many programs calculate total memory as + // "uvmexp.npages * pagesize" but this is incorrect and does not + // match "sysctl | grep hw.physmem". + (unsigned long long) total_physmem, (unsigned long long) uvmexp.free * pagesize, (unsigned long long) uvmexp.active * pagesize, (unsigned long long) uvmexp.inactive * pagesize, diff --git a/test/_openbsd.py b/test/_openbsd.py index 7b079cf6..bff5cce6 100644 --- a/test/_openbsd.py +++ b/test/_openbsd.py @@ -100,9 +100,9 @@ class OpenBSDSpecificTestCase(unittest.TestCase): self.assertEqual(smem.used, int(used) * 512) self.assertEqual(smem.free, int(free) * 512) - # def test_virtual_memory(self): - # s = sysctl('hw.physmem') - # self.assertEqual(s, psutil.virtual_memory().total) + def test_virtual_memory(self): + num = sysctl('hw.physmem') + self.assertEqual(num, psutil.virtual_memory().total) def main(): |