load memory usage numbers (OpenBSD)
This commit is contained in:
parent
81e6bddfa1
commit
6d54da1b0f
110
get_mem.c
110
get_mem.c
@ -1,5 +1,7 @@
|
|||||||
/*
|
/*
|
||||||
* get_mem.c
|
* Adapted: get memory usage on OpenBSD
|
||||||
|
* Author: Christian Barthel <bch@vcs.onfire.org>
|
||||||
|
*
|
||||||
* get memory usage, from get_load.c derived
|
* get memory usage, from get_load.c derived
|
||||||
*
|
*
|
||||||
* Author: Hans-Helmut Buehmann 20. Jan. 1996
|
* Author: Hans-Helmut Buehmann 20. Jan. 1996
|
||||||
@ -7,36 +9,110 @@
|
|||||||
* Modified for more recent kernels Helmut Geyer Oct. 1996
|
* Modified for more recent kernels Helmut Geyer Oct. 1996
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
#include <sys/param.h> /* DEV_BSIZE MAXCOMLEN PZERO */
|
||||||
|
#include <sys/sysctl.h>
|
||||||
|
#include <sys/swap.h>
|
||||||
|
#include <sys/mount.h>
|
||||||
|
#include <sys/proc.h>
|
||||||
#include <X11/Xos.h>
|
#include <X11/Xos.h>
|
||||||
#include <X11/Intrinsic.h>
|
#include <X11/Intrinsic.h>
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
|
#include <stdlib.h>
|
||||||
#include <unistd.h>
|
#include <unistd.h>
|
||||||
#include "MemStripChart.h"
|
#include "MemStripChart.h"
|
||||||
|
|
||||||
|
static int pageshift = -1;
|
||||||
|
#define pagetok(size) ((size) << pageshift)
|
||||||
|
#define LOG1024 10
|
||||||
|
|
||||||
void GetMemLoadPoint(w, closure, call_data) /* Linux version */
|
static double allmem_kb = 0;
|
||||||
Widget w; /* unused */
|
|
||||||
caddr_t closure; /* unused */
|
static void initPageShift(void)
|
||||||
caddr_t call_data; /* pointer to (MemStripChartCallbackData) return value */
|
|
||||||
{
|
{
|
||||||
MemStripChartCallbackData ret;
|
int pagesize = getpagesize();
|
||||||
/* meminfo();*/
|
pageshift = 0;
|
||||||
|
while (pagesize > 1) {
|
||||||
|
pageshift++;
|
||||||
|
pagesize >>= 1;
|
||||||
|
}
|
||||||
|
pageshift -= LOG1024;
|
||||||
|
}
|
||||||
|
|
||||||
/*et.code = (double)(kb_main_used - kb_main_buffers - kb_main_cached) / kb_main_total;
|
static double getSwapFrac()
|
||||||
|
{
|
||||||
|
struct swapent *swdev;
|
||||||
|
int used, total, nswap, rnswap, i;
|
||||||
|
|
||||||
|
nswap = swapctl(SWAP_NSWAP, 0, 0);
|
||||||
|
if (nswap == 0)
|
||||||
|
return 0;
|
||||||
|
|
||||||
|
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)
|
||||||
|
{
|
||||||
|
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();
|
||||||
|
|
||||||
|
/*ret.code = (double)(kb_main_used - kb_main_buffers - kb_main_cached) / kb_main_total;
|
||||||
ret.cached = (double)kb_main_cached / (double)kb_main_total;
|
ret.cached = (double)kb_main_cached / (double)kb_main_total;
|
||||||
ret.buffer = (double)kb_main_buffers / (double)kb_main_total;
|
ret.buffer = (double)kb_main_buffers / (double)kb_main_total;
|
||||||
ret.free = (double)kb_main_free / (double)kb_main_total;
|
ret.free = (double)kb_main_free / (double)kb_main_total;
|
||||||
ret.swap = (double)kb_swap_used / (double)kb_main_total;
|
ret.swap = (double)kb_swap_used / (double)kb_main_total;
|
||||||
|
|
||||||
*/
|
*/
|
||||||
|
|
||||||
printf("test?\n");
|
size = sizeof(uvmexp);
|
||||||
ret.code = 0.5;
|
if (sysctl(uvmexp_mib, 2, &uvmexp, &size, NULL, 0) < 0) {
|
||||||
ret.cached = 0.5;
|
warn("sysctl failed");
|
||||||
ret.buffer = 0.5;
|
bzero(&uvmexp, sizeof(uvmexp));
|
||||||
ret.free = 0.5;
|
}
|
||||||
ret.swap = 0.5;
|
size = sizeof(bcstats);
|
||||||
|
if (sysctl(bcstats_mib, 3, &bcstats, &size, NULL, 0) < 0) {
|
||||||
|
warn("sysctl failed");
|
||||||
|
bzero(&bcstats, sizeof(bcstats));
|
||||||
|
}
|
||||||
|
|
||||||
memcpy(call_data, &ret, sizeof(MemStripChartCallbackData));
|
|
||||||
|
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();
|
||||||
|
|
||||||
|
#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));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user