Skip to content

Commit

Permalink
#714: [OpenBSD] return shared virtual mem
Browse files Browse the repository at this point in the history
  • Loading branch information
giampaolo committed Nov 25, 2015
1 parent ba88214 commit 25ab77d
Showing 1 changed file with 11 additions and 2 deletions.
13 changes: 11 additions & 2 deletions psutil/arch/bsd/openbsd.c
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
#include <sys/proc.h>
#include <sys/mount.h> // for VFS_*
#include <sys/swap.h> // for swap_mem
#include <sys/vmmeter.h> // for vmtotal struct
#include <signal.h>
#include <kvm.h>
// connection stuff
Expand Down Expand Up @@ -326,9 +327,11 @@ psutil_virtual_mem(PyObject *self, PyObject *args) {
int uvmexp_mib[] = {CTL_VM, VM_UVMEXP};
int bcstats_mib[] = {CTL_VFS, VFS_GENERIC, VFS_BCACHESTAT};
int physmem_mib[] = {CTL_HW, HW_PHYSMEM64};
int vmmeter_mib[] = {CTL_VM, VM_METER};
size_t size;
struct uvmexp uvmexp;
struct bcachestats bcstats;
struct vmtotal vmdata;
long pagesize = getpagesize();

size = sizeof(total_physmem);
Expand All @@ -343,13 +346,18 @@ psutil_virtual_mem(PyObject *self, PyObject *args) {
return NULL;
}

// This is how "top" calculates cached memory.
size = sizeof(bcstats);
if (sysctl(bcstats_mib, 3, &bcstats, &size, NULL, 0) < 0) {
PyErr_SetFromErrno(PyExc_OSError);
return NULL;
}

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

return Py_BuildValue("KKKKKKKK",
// Note: many programs calculate total memory as
// "uvmexp.npages * pagesize" but this is incorrect and does not
Expand All @@ -359,9 +367,10 @@ psutil_virtual_mem(PyObject *self, PyObject *args) {
(unsigned long long) uvmexp.active * pagesize,
(unsigned long long) uvmexp.inactive * pagesize,
(unsigned long long) uvmexp.wired * pagesize,
// this is how "top" determines it
(unsigned long long) bcstats.numbufpages * pagesize, // cached
(unsigned long long) 0, // buffers
(unsigned long long) 0 // shared
(unsigned long long) vmdata.t_vmshr + vmdata.t_rmshr // shared
);
}

Expand Down

0 comments on commit 25ab77d

Please sign in to comment.