Skip to content

Commit

Permalink
#557 / NetBSD: fix total virt mem
Browse files Browse the repository at this point in the history
  • Loading branch information
giampaolo committed Jan 11, 2016
1 parent bcb0c84 commit f81770f
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 7 deletions.
19 changes: 13 additions & 6 deletions psutil/arch/bsd/netbsd.c
Original file line number Diff line number Diff line change
Expand Up @@ -424,20 +424,27 @@ psutil_get_cmdline(pid_t pid) {

PyObject *
psutil_virtual_mem(PyObject *self, PyObject *args) {
unsigned int total;
size_t size = sizeof(total);
int64_t total_physmem;
size_t size;
struct uvmexp_sysctl uv;
int mib[] = {CTL_VM, VM_UVMEXP2};
int physmem_mib[] = {CTL_HW, HW_PHYSMEM64};
int uvmexp_mib[] = {CTL_VM, VM_UVMEXP2};
long pagesize = getpagesize();
size = sizeof(uv);

if (sysctl(mib, 2, &uv, &size, NULL, 0) < 0) {
size = sizeof(total_physmem);
if (sysctl(physmem_mib, 2, &total_physmem, &size, NULL, 0) < 0) {
PyErr_SetFromErrno(PyExc_OSError);
return NULL;
}

size = sizeof(uv);
if (sysctl(uvmexp_mib, 2, &uv, &size, NULL, 0) < 0) {
PyErr_SetFromErrno(PyExc_OSError);
return NULL;
}

return Py_BuildValue("KKKKKKKK",
(unsigned long long) uv.npages * pagesize, // total
(unsigned long long) total_physmem, // total
(unsigned long long) uv.free * pagesize, // free
(unsigned long long) uv.active * pagesize, // active
(unsigned long long) uv.inactive * pagesize, // inactive
Expand Down
15 changes: 14 additions & 1 deletion test/_bsd.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
from test_psutil import FREEBSD
from test_psutil import get_test_subprocess
from test_psutil import MEMORY_TOLERANCE
from test_psutil import NETBSD
from test_psutil import OPENBSD
from test_psutil import reap_children
from test_psutil import retry_before_failing
Expand All @@ -44,7 +45,7 @@ def sysctl(cmdline):
result = sh("sysctl " + cmdline)
if FREEBSD:
result = result[result.find(": ") + 2:]
elif OPENBSD:
elif OPENBSD or NETBSD:
result = result[result.find("=") + 1:]
try:
return int(result)
Expand Down Expand Up @@ -292,13 +293,25 @@ def test_boot_time(self):
self.assertEqual(sys_bt, psutil_bt)


# =====================================================================
# --- NetBSD
# =====================================================================


@unittest.skipUnless(NETBSD, "not an NetBSD system")
class NetBSDSpecificTestCase(unittest.TestCase):
pass


def main():
test_suite = unittest.TestSuite()
test_suite.addTest(unittest.makeSuite(BSDSpecificTestCase))
if FREEBSD:
test_suite.addTest(unittest.makeSuite(FreeBSDSpecificTestCase))
elif OPENBSD:
test_suite.addTest(unittest.makeSuite(OpenBSDSpecificTestCase))
elif NETBSD:
test_suite.addTest(unittest.makeSuite(NetBSDSpecificTestCase))
result = unittest.TextTestRunner(verbosity=2).run(test_suite)
return result.wasSuccessful()

Expand Down
3 changes: 3 additions & 0 deletions test/test_psutil.py
Original file line number Diff line number Diff line change
Expand Up @@ -3248,6 +3248,9 @@ def main():
elif OPENBSD:
from _bsd import OpenBSDSpecificTestCase
tests.append(OpenBSDSpecificTestCase)
elif NETBSD:
from _bsd import NetBSDSpecificTestCase
tests.append(NetBSDSpecificTestCase)

if stc is not None:
tests.append(stc)
Expand Down

0 comments on commit f81770f

Please sign in to comment.