2017-02-26 11:03:48 -05:00
|
|
|
/* $Id: get_mem.c,v 1.4 2017/02/26 16:03:48 bch Exp $
|
2017-02-18 16:06:47 -05:00
|
|
|
* Adapted: get memory usage on OpenBSD
|
|
|
|
* Author: Christian Barthel <bch@vcs.onfire.org>
|
|
|
|
*
|
2017-02-18 16:03:44 -05:00
|
|
|
* get memory usage, from get_load.c derived
|
|
|
|
*
|
|
|
|
* Author: Hans-Helmut Buehmann 20. Jan. 1996
|
|
|
|
*
|
|
|
|
* Modified for more recent kernels Helmut Geyer Oct. 1996
|
|
|
|
*/
|
|
|
|
|
2017-02-18 16:06:47 -05:00
|
|
|
#include <sys/param.h> /* DEV_BSIZE MAXCOMLEN PZERO */
|
|
|
|
#include <sys/sysctl.h>
|
|
|
|
#include <sys/swap.h>
|
|
|
|
#include <sys/mount.h>
|
|
|
|
#include <sys/proc.h>
|
2017-02-26 11:03:48 -05:00
|
|
|
|
|
|
|
#include <unistd.h>
|
|
|
|
|
2017-02-18 16:03:44 -05:00
|
|
|
#include <X11/Xos.h>
|
|
|
|
#include <X11/Intrinsic.h>
|
|
|
|
#include <stdio.h>
|
2017-02-18 16:06:47 -05:00
|
|
|
#include <stdlib.h>
|
2017-02-26 11:03:48 -05:00
|
|
|
#include <err.h>
|
2017-02-18 16:03:44 -05:00
|
|
|
#include "MemStripChart.h"
|
|
|
|
|
2017-02-18 16:06:47 -05:00
|
|
|
static int pageshift = -1;
|
|
|
|
#define pagetok(size) ((size) << pageshift)
|
|
|
|
#define LOG1024 10
|
|
|
|
|
|
|
|
static double allmem_kb = 0;
|
|
|
|
|
|
|
|
static void initPageShift(void)
|
|
|
|
{
|
|
|
|
int pagesize = getpagesize();
|
|
|
|
pageshift = 0;
|
|
|
|
while (pagesize > 1) {
|
|
|
|
pageshift++;
|
|
|
|
pagesize >>= 1;
|
|
|
|
}
|
|
|
|
pageshift -= LOG1024;
|
|
|
|
}
|
|
|
|
|
|
|
|
static double getSwapFrac()
|
|
|
|
{
|
|
|
|
struct swapent *swdev;
|
|
|
|
int used, total, nswap, rnswap, i;
|
|
|
|
|
|
|
|
nswap = swapctl(SWAP_NSWAP, 0, 0);
|
|
|
|
if (nswap == 0)
|
|
|
|
return 0;
|
2017-02-18 16:03:44 -05:00
|
|
|
|
2017-02-18 16:06:47 -05:00
|
|
|
swdev = calloc(nswap, sizeof(*swdev));
|
|
|
|
if (swdev == NULL)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
rnswap = swapctl(SWAP_STATS, swdev, nswap);
|
|
|
|
if (rnswap == -1) {
|
|
|
|
free(swdev);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* if rnswap != nswap, then what? */
|
|
|
|
|
|
|
|
/* Total things up */
|
|
|
|
total = used = 0;
|
|
|
|
for (i = 0; i < nswap; i++) {
|
|
|
|
if (swdev[i].se_flags & SWF_ENABLE) {
|
|
|
|
used += (swdev[i].se_inuse / (1024 / DEV_BSIZE));
|
|
|
|
total += (swdev[i].se_nblks / (1024 / DEV_BSIZE));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
free(swdev);
|
|
|
|
return (double)used/total;
|
|
|
|
}
|
|
|
|
|
|
|
|
void GetMemLoadPoint(Widget w, caddr_t closure, caddr_t call_data)
|
2017-02-18 16:03:44 -05:00
|
|
|
{
|
2017-02-18 16:06:47 -05:00
|
|
|
static int bcstats_mib[] = {CTL_VFS, VFS_GENERIC, VFS_BCACHESTAT};
|
|
|
|
static int uvmexp_mib[] = {CTL_VM, VM_UVMEXP};
|
|
|
|
MemStripChartCallbackData ret;
|
|
|
|
struct uvmexp uvmexp;
|
|
|
|
struct bcachestats bcstats;
|
|
|
|
size_t size;
|
|
|
|
|
|
|
|
if (pageshift < 0)
|
|
|
|
initPageShift();
|
2017-02-18 16:03:44 -05:00
|
|
|
|
2017-02-18 16:06:47 -05:00
|
|
|
size = sizeof(uvmexp);
|
|
|
|
if (sysctl(uvmexp_mib, 2, &uvmexp, &size, NULL, 0) < 0) {
|
|
|
|
warn("sysctl failed");
|
|
|
|
bzero(&uvmexp, sizeof(uvmexp));
|
|
|
|
}
|
|
|
|
size = sizeof(bcstats);
|
|
|
|
if (sysctl(bcstats_mib, 3, &bcstats, &size, NULL, 0) < 0) {
|
|
|
|
warn("sysctl failed");
|
|
|
|
bzero(&bcstats, sizeof(bcstats));
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
allmem_kb = pagetok(uvmexp.npages);
|
|
|
|
ret.cached = pagetok(bcstats.numbufpages) / allmem_kb;
|
|
|
|
ret.free = pagetok(uvmexp.free) / allmem_kb;
|
|
|
|
ret.buffer = 0;
|
|
|
|
ret.code = (1 - ret.cached - ret.free);
|
|
|
|
ret.swap = getSwapFrac();
|
2017-02-18 16:03:44 -05:00
|
|
|
|
2017-02-18 16:06:47 -05:00
|
|
|
#ifdef DEBUG
|
|
|
|
printf("%lf %lf %lf %lf %lf\n", allmem_kb, ret.code, ret.cached, ret.free, ret.swap);
|
|
|
|
#endif
|
|
|
|
|
|
|
|
memcpy(call_data, &ret, sizeof(MemStripChartCallbackData));
|
2017-02-18 16:03:44 -05:00
|
|
|
}
|
|
|
|
|