Compare commits

...

4 Commits

Author SHA1 Message Date
Christian Barthel
cfb359736f Whitespace cleanup 2022-02-05 07:40:03 +01:00
Christian Barthel
34c8e85e49 Add Linux support, fix whitespace
Read /proc/meminfo, pull out some numbers and use them.
2022-01-08 08:31:23 +01:00
Christian Barthel
a6e2ded980 Whitespace 2022-01-08 08:28:08 +01:00
Christian Barthel
2458539dc7 Add Linux makefile 2022-01-07 22:55:21 +01:00
5 changed files with 322 additions and 166 deletions

44
Makefile.linux Normal file
View File

@ -0,0 +1,44 @@
PROG= xmem
VERSION= 1.27
LDFLAGS+= -L/usr/lib/x86_64-linux-gnu/ -lX11 -lXt -lXaw -lXmu
CFLAGS+= -Wall -I/usr/include/X11/ -I/usr/X11R6/include/ -I/usr/local/include/
OBJECTS= xmem.o MemStripChart.o get_mem.o
all: xmem
xmem.full:
@echo "full not supported yet"
xmem.debug:
@echo "debug not supported yet"
xmem: $(OBJECTS)
$(CC) -o xmem $(OBJECTS) $(LDFLAGS)
%.o: %c
$(CC) $(CFLAGS) -c $<
clean:
-rm -rf $(OBJECTS) $(PROG)
dist:
mkdir -p ${PROG}-${VERSION}
cp *.h *.c Makefile xmem.1 xmem.bit copyright XMem.ad ${PROG}-${VERSION}
tar cfvz ${PROG}-${VERSION}.tar.gz ${PROG}-${VERSION}
rm -rf ${PROG}-${VERSION}
dist-clean:
-rm ${PROG}-${VERSION}.tar.gz
install:
install -d -m 755 ${PREFIX}/bin/
install -c -S -s -o root -g bin -m 755 xmem ${PREFIX}/bin/xmem
install -d -m 755 ${PREFIX}/man/man1/
install -c -o root -g bin -m 644 xmem.1 ${PREFIX}/man/man1/xmem.1
install -d -m 755 ${PREFIX}/lib/X11/app-defaults/
install -c -o root -g bin -m 644 XMem.ad ${PREFIX}/lib/X11/app-defaults/XMem
install -d -m 755 ${PREFIX}/share/${PROG}/
install -c -o root -g bin -m 644 xmem.bit ${PREFIX}/share/${PROG}/xmem.bit
install -c -o root -g bin -m 644 copyright ${PREFIX}/share/${PROG}/copyright

View File

@ -1,7 +1,7 @@
/*********************************************************************** /***********************************************************************
* *
* MemStripChart Widget * MemStripChart Widget
* from StripChart Widget derived * from StripChart Widget derived
* *
* Author: Hans-Helmut B"uhmann 20. Jan. 1996 * Author: Hans-Helmut B"uhmann 20. Jan. 1996
* *
@ -165,10 +165,10 @@ CreateGC(MemStripChartWidget w, unsigned int which)
static void static void
DestroyGC(MemStripChartWidget w, unsigned int which) DestroyGC(MemStripChartWidget w, unsigned int which)
{ {
if (which & FOREGROUND) if (which & FOREGROUND)
XtReleaseGC((Widget) w, w->mem_strip_chart.fgGC); XtReleaseGC((Widget) w, w->mem_strip_chart.fgGC);
if (which & HIGHLIGHT) if (which & HIGHLIGHT)
XtReleaseGC((Widget) w, w->mem_strip_chart.hiGC); XtReleaseGC((Widget) w, w->mem_strip_chart.hiGC);
if (which & CODE) { if (which & CODE) {
@ -229,7 +229,7 @@ DrawMemStrip(MemStripChartWidget w, unsigned int x)
if (w->mem_strip_chart.valuedata[x].buffer != 0.0) { if (w->mem_strip_chart.valuedata[x].buffer != 0.0) {
top = (int) (w->core.height top = (int) (w->core.height
- (int)(w->core.height * (1.0 - w->mem_strip_chart.valuedata[x].free)) - (int)(w->core.height * (1.0 - w->mem_strip_chart.valuedata[x].free))
/ w->mem_strip_chart.scale); / w->mem_strip_chart.scale);
XFillRectangle(XtDisplay(w), XtWindow(w), w->mem_strip_chart.bufferGC, XFillRectangle(XtDisplay(w), XtWindow(w), w->mem_strip_chart.bufferGC,
x, top, (unsigned int) 1, bottom - top); x, top, (unsigned int) 1, bottom - top);
@ -246,7 +246,7 @@ DrawMemStrip(MemStripChartWidget w, unsigned int x)
} }
if (w->mem_strip_chart.valuedata[x].swap != 0.0) { if (w->mem_strip_chart.valuedata[x].swap != 0.0) {
top = (int) (w->core.height top = (int) (w->core.height
- (int)(w->core.height * (1.0 + w->mem_strip_chart.valuedata[x].swap)) - (int)(w->core.height * (1.0 + w->mem_strip_chart.valuedata[x].swap))
/ w->mem_strip_chart.scale); / w->mem_strip_chart.scale);
XFillRectangle(XtDisplay(w), XtWindow(w), w->mem_strip_chart.swapGC, XFillRectangle(XtDisplay(w), XtWindow(w), w->mem_strip_chart.swapGC,
x, top, (unsigned int) 1, bottom - top); x, top, (unsigned int) 1, bottom - top);
@ -261,7 +261,7 @@ static void Initialize (Widget greq, Widget gnew, ArgList args, Cardinal *num_ar
if (w->mem_strip_chart.update > 0) if (w->mem_strip_chart.update > 0)
w->mem_strip_chart.interval_id = XtAppAddTimeOut( w->mem_strip_chart.interval_id = XtAppAddTimeOut(
XtWidgetToApplicationContext(gnew), XtWidgetToApplicationContext(gnew),
w->mem_strip_chart.update * MS_PER_SEC, w->mem_strip_chart.update * MS_PER_SEC,
draw_it, (XtPointer) gnew); draw_it, (XtPointer) gnew);
CreateGC(w, (unsigned int) ALL_GCS); CreateGC(w, (unsigned int) ALL_GCS);
@ -271,7 +271,7 @@ static void Initialize (Widget greq, Widget gnew, ArgList args, Cardinal *num_ar
w->mem_strip_chart.points = NULL; w->mem_strip_chart.points = NULL;
SetPoints(w); SetPoints(w);
} }
static void Destroy (Widget gw) static void Destroy (Widget gw)
{ {
MemStripChartWidget w = (MemStripChartWidget)gw; MemStripChartWidget w = (MemStripChartWidget)gw;
@ -284,7 +284,7 @@ static void Destroy (Widget gw)
} }
/* /*
* NOTE: This function really needs to recieve graphics exposure * NOTE: This function really needs to recieve graphics exposure
* events, but since this is not easily supported until R4 I am * events, but since this is not easily supported until R4 I am
* going to hold off until then. * going to hold off until then.
*/ */
@ -301,13 +301,13 @@ static void Redisplay(Widget w, XEvent *event, Region region)
} }
/* ARGSUSED */ /* ARGSUSED */
static void static void
draw_it(XtPointer client_data, XtIntervalId *id) draw_it(XtPointer client_data, XtIntervalId *id)
{ {
MemStripChartWidget w = (MemStripChartWidget)client_data; MemStripChartWidget w = (MemStripChartWidget)client_data;
MemStripChartCallbackData value; MemStripChartCallbackData value;
double usedmem; double usedmem;
if (w->mem_strip_chart.update > 0) if (w->mem_strip_chart.update > 0)
w->mem_strip_chart.interval_id = w->mem_strip_chart.interval_id =
XtAppAddTimeOut(XtWidgetToApplicationContext( (Widget) w), XtAppAddTimeOut(XtWidgetToApplicationContext( (Widget) w),
@ -323,15 +323,15 @@ draw_it(XtPointer client_data, XtIntervalId *id)
XtCallCallbacks( (Widget)w, XtNgetValue, (XtPointer)&value ); XtCallCallbacks( (Widget)w, XtNgetValue, (XtPointer)&value );
/* /*
* Keep w->mem_strip_chart.max_value up to date, and if this data * Keep w->mem_strip_chart.max_value up to date, and if this data
* point is off the graph, change the scale to make it fit. * point is off the graph, change the scale to make it fit.
*/ */
usedmem = 1.0 + value.swap; usedmem = 1.0 + value.swap;
if (usedmem > w->mem_strip_chart.max_value) { if (usedmem > w->mem_strip_chart.max_value) {
w->mem_strip_chart.max_value = usedmem; w->mem_strip_chart.max_value = usedmem;
if (XtIsRealized((Widget)w) && if (XtIsRealized((Widget)w) &&
w->mem_strip_chart.max_value > w->mem_strip_chart.scale) { w->mem_strip_chart.max_value > w->mem_strip_chart.scale) {
XClearWindow( XtDisplay (w), XtWindow (w)); XClearWindow( XtDisplay (w), XtWindow (w));
w->mem_strip_chart.interval = repaint_window(w, 0, (int) w->core.width); w->mem_strip_chart.interval = repaint_window(w, 0, (int) w->core.width);
@ -343,7 +343,7 @@ draw_it(XtPointer client_data, XtIntervalId *id)
/* XXX DrawMemStrip(w, w->mem_strip_chart.interval, value); */ /* XXX DrawMemStrip(w, w->mem_strip_chart.interval, value); */
DrawMemStrip(w, w->mem_strip_chart.interval); DrawMemStrip(w, w->mem_strip_chart.interval);
/* /*
* Fill in the graph lines we just painted over. * Fill in the graph lines we just painted over.
*/ */
@ -370,7 +370,7 @@ draw_it(XtPointer client_data, XtIntervalId *id)
* largest data point. * largest data point.
*/ */
static int static int
repaint_window(MemStripChartWidget w, int left, int width) repaint_window(MemStripChartWidget w, int left, int width)
{ {
int i, j; int i, j;
@ -393,7 +393,7 @@ repaint_window(MemStripChartWidget w, int left, int width)
SetPoints(w); SetPoints(w);
if (XtIsRealized ((Widget) w)) if (XtIsRealized ((Widget) w))
XClearWindow (XtDisplay (w), XtWindow (w)); XClearWindow (XtDisplay (w), XtWindow (w));
} }
@ -445,20 +445,20 @@ MoveChart(MemStripChartWidget w, Boolean blit)
if (j < 0) j = 0; if (j < 0) j = 0;
} }
(void) memmove((char *)(w->mem_strip_chart.valuedata), (void) memmove((char *)(w->mem_strip_chart.valuedata),
(char *)(w->mem_strip_chart.valuedata + next - j), (char *)(w->mem_strip_chart.valuedata + next - j),
j * sizeof(MemStripChartCallbackData)); j * sizeof(MemStripChartCallbackData));
next = w->mem_strip_chart.interval = j; next = w->mem_strip_chart.interval = j;
/* /*
* Since we just lost some data, recompute the * Since we just lost some data, recompute the
* w->mem_strip_chart.max_value. * w->mem_strip_chart.max_value.
*/ */
old_max = w->mem_strip_chart.max_value; old_max = w->mem_strip_chart.max_value;
w->mem_strip_chart.max_value = 0.0; w->mem_strip_chart.max_value = 0.0;
for (i = 0; i < next; i++) { for (i = 0; i < next; i++) {
if (w->mem_strip_chart.valuedata[i].swap + 1.0 > w->mem_strip_chart.max_value) if (w->mem_strip_chart.valuedata[i].swap + 1.0 > w->mem_strip_chart.max_value)
w->mem_strip_chart.max_value = w->mem_strip_chart.valuedata[i].swap + 1.0; w->mem_strip_chart.max_value = w->mem_strip_chart.valuedata[i].swap + 1.0;
} }
@ -475,8 +475,8 @@ MoveChart(MemStripChartWidget w, Boolean blit)
(unsigned int) j, (unsigned int) w->core.height, (unsigned int) j, (unsigned int) w->core.height,
0, 0); 0, 0);
XClearArea(XtDisplay((Widget)w), XtWindow((Widget)w), XClearArea(XtDisplay((Widget)w), XtWindow((Widget)w),
(int) j, 0, (int) j, 0,
(unsigned int) w->core.width - j, (unsigned int)w->core.height, (unsigned int) w->core.width - j, (unsigned int)w->core.height,
FALSE); FALSE);
@ -510,12 +510,12 @@ static Boolean SetValues (Widget current, Widget request, Widget new, ArgList ar
if ( w->mem_strip_chart.min_scale > (int) ((w->mem_strip_chart.max_value) + 1) ) if ( w->mem_strip_chart.min_scale > (int) ((w->mem_strip_chart.max_value) + 1) )
ret_val = TRUE; ret_val = TRUE;
if ( w->mem_strip_chart.fgpixel != old->mem_strip_chart.fgpixel ) { if ( w->mem_strip_chart.fgpixel != old->mem_strip_chart.fgpixel ) {
new_gc |= FOREGROUND; new_gc |= FOREGROUND;
ret_val = True; ret_val = True;
} }
if ( w->mem_strip_chart.hipixel != old->mem_strip_chart.hipixel ) { if ( w->mem_strip_chart.hipixel != old->mem_strip_chart.hipixel ) {
new_gc |= HIGHLIGHT; new_gc |= HIGHLIGHT;
ret_val = True; ret_val = True;

View File

@ -1,2 +1,11 @@
# xmem(1) X11 Utility # xmem(1) X11 Utility
![fvwm and xmem](https://git.sdf.org/bch/xmem/raw/branch/master/www/xmem_fvwmbar.png) ![fvwm and xmem](https://git.sdf.org/bch/xmem/raw/branch/master/www/xmem_fvwmbar.png)
# Compile
```shell
# Linux with DEBUG:
CFLAGS=-DDEBUG make -f Makefile.linux
# BSD:
make
```

341
get_mem.c
View File

@ -13,7 +13,7 @@
#if __OpenBSD__ #if __OpenBSD__
#include <sys/param.h> /* DEV_BSIZE MAXCOMLEN PZERO */ #include <sys/param.h> /* DEV_BSIZE MAXCOMLEN PZERO */
#include <sys/sysctl.h> #include <sys/sysctl.h>
#include <sys/swap.h> #include <sys/swap.h>
#include <sys/mount.h> #include <sys/mount.h>
@ -36,89 +36,89 @@ static double allmem_kb = 0;
static void initPageShift(void) static void initPageShift(void)
{ {
int pagesize = getpagesize(); int pagesize = getpagesize();
pageshift = 0; pageshift = 0;
while (pagesize > 1) { while (pagesize > 1) {
pageshift++; pageshift++;
pagesize >>= 1; pagesize >>= 1;
} }
pageshift -= LOG1024; pageshift -= LOG1024;
} }
static double getSwapFrac() static double getSwapFrac()
{ {
struct swapent *swdev; struct swapent *swdev;
int used, total, nswap, rnswap, i; int used, total, nswap, rnswap, i;
nswap = swapctl(SWAP_NSWAP, 0, 0); nswap = swapctl(SWAP_NSWAP, 0, 0);
if (nswap == 0) if (nswap == 0)
return 0; return 0;
swdev = calloc(nswap, sizeof(*swdev)); swdev = calloc(nswap, sizeof(*swdev));
if (swdev == NULL) if (swdev == NULL)
return 0; return 0;
rnswap = swapctl(SWAP_STATS, swdev, nswap); rnswap = swapctl(SWAP_STATS, swdev, nswap);
if (rnswap == -1) { if (rnswap == -1) {
free(swdev); free(swdev);
return 0; return 0;
} }
/* if rnswap != nswap, then what? */ /* if rnswap != nswap, then what? */
/* Total things up */ /* Total things up */
total = used = 0; total = used = 0;
for (i = 0; i < nswap; i++) { for (i = 0; i < nswap; i++) {
if (swdev[i].se_flags & SWF_ENABLE) { if (swdev[i].se_flags & SWF_ENABLE) {
used += (swdev[i].se_inuse / (1024 / DEV_BSIZE)); used += (swdev[i].se_inuse / (1024 / DEV_BSIZE));
total += (swdev[i].se_nblks / (1024 / DEV_BSIZE)); total += (swdev[i].se_nblks / (1024 / DEV_BSIZE));
} }
} }
free(swdev); free(swdev);
return (double)used/total; return (double)used/total;
} }
void GetMemLoadPoint(Widget w, caddr_t closure, caddr_t call_data) void GetMemLoadPoint(Widget w, caddr_t closure, caddr_t call_data)
{ {
static int bcstats_mib[] = {CTL_VFS, VFS_GENERIC, VFS_BCACHESTAT}; static int bcstats_mib[] = {CTL_VFS, VFS_GENERIC, VFS_BCACHESTAT};
static int uvmexp_mib[] = {CTL_VM, VM_UVMEXP}; static int uvmexp_mib[] = {CTL_VM, VM_UVMEXP};
MemStripChartCallbackData ret; MemStripChartCallbackData ret;
struct uvmexp uvmexp; struct uvmexp uvmexp;
struct bcachestats bcstats; struct bcachestats bcstats;
size_t size; size_t size;
if (pageshift < 0) if (pageshift < 0)
initPageShift(); initPageShift();
size = sizeof(uvmexp); size = sizeof(uvmexp);
if (sysctl(uvmexp_mib, 2, &uvmexp, &size, NULL, 0) < 0) { if (sysctl(uvmexp_mib, 2, &uvmexp, &size, NULL, 0) < 0) {
warn("sysctl failed"); warn("sysctl failed");
bzero(&uvmexp, sizeof(uvmexp)); bzero(&uvmexp, sizeof(uvmexp));
} }
size = sizeof(bcstats); size = sizeof(bcstats);
if (sysctl(bcstats_mib, 3, &bcstats, &size, NULL, 0) < 0) { if (sysctl(bcstats_mib, 3, &bcstats, &size, NULL, 0) < 0) {
warn("sysctl failed"); warn("sysctl failed");
bzero(&bcstats, sizeof(bcstats)); bzero(&bcstats, sizeof(bcstats));
} }
allmem_kb = pagetok(uvmexp.npages); allmem_kb = pagetok(uvmexp.npages);
ret.cached = pagetok(bcstats.numbufpages) / allmem_kb; ret.cached = pagetok(bcstats.numbufpages) / allmem_kb;
ret.free = pagetok(uvmexp.free) / allmem_kb; ret.free = pagetok(uvmexp.free) / allmem_kb;
ret.buffer = 0; ret.buffer = 0;
ret.code = (1 - ret.cached - ret.free); ret.code = (1 - ret.cached - ret.free);
ret.swap = getSwapFrac(); ret.swap = getSwapFrac();
#ifdef DEBUG #ifdef DEBUG
printf("%lf %lf %lf %lf %lf\n", printf("%lf %lf %lf %lf %lf\n",
allmem_kb, allmem_kb,
ret.code, ret.code,
ret.cached, ret.cached,
ret.free, ret.free,
ret.swap); ret.swap);
#endif #endif
memcpy(call_data, &ret, sizeof(MemStripChartCallbackData)); memcpy(call_data, &ret, sizeof(MemStripChartCallbackData));
} }
#endif #endif
@ -133,7 +133,7 @@ void GetMemLoadPoint(Widget w, caddr_t closure, caddr_t call_data)
*/ */
#include <sys/types.h> /* DEV_BSIZE MAXCOMLEN PZERO */ #include <sys/types.h> /* DEV_BSIZE MAXCOMLEN PZERO */
#include <sys/sysctl.h> #include <sys/sysctl.h>
#include <unistd.h> #include <unistd.h>
@ -156,91 +156,194 @@ static unsigned int allmem_pages = 0;
static void initPageShift(void) static void initPageShift(void)
{ {
pagesize = getpagesize(); pagesize = getpagesize();
size_t len = sizeof(allmem_pages); size_t len = sizeof(allmem_pages);
int mib[len]; int mib[len];
pageshift = 0; pageshift = 0;
while (pagesize > 1) { while (pagesize > 1) {
pageshift++; pageshift++;
pagesize >>= 1; pagesize >>= 1;
} }
pageshift -= LOG1024; pageshift -= LOG1024;
kd = kvm_open(NULL, "/dev/null" ,NULL, O_RDONLY, "kvm_open"); kd = kvm_open(NULL, "/dev/null" ,NULL, O_RDONLY, "kvm_open");
if (sysctlbyname("vm.stats.vm.v_page_count", &allmem_pages, &len, NULL, 0) == -1) if (sysctlbyname("vm.stats.vm.v_page_count", &allmem_pages, &len, NULL, 0) == -1)
perror("sysctl"); perror("sysctl");
#ifdef DEBUG #ifdef DEBUG
printf("page_count: %d\n", allmem_pages); printf("page_count: %d\n", allmem_pages);
#endif #endif
} }
static double getSwapFrac() static double getSwapFrac()
{ {
/* XXX Borrowed from top(1) code: */ /* XXX Borrowed from top(1) code: */
int n; int n;
struct kvm_swap swapary[1]; struct kvm_swap swapary[1];
static int pagesize = 0; static int pagesize = 0;
static unsigned long swap_maxpages = 0; static unsigned long swap_maxpages = 0;
size_t sz = sizeof(swap_maxpages); size_t sz = sizeof(swap_maxpages);
int retavail = 0; int retavail = 0;
int retfree = 0; int retfree = 0;
n = kvm_getswapinfo(kd, swapary, 1, 0); n = kvm_getswapinfo(kd, swapary, 1, 0);
if (n < 0 || swapary[0].ksw_total == 0) if (n < 0 || swapary[0].ksw_total == 0)
return (0); return (0);
if (swap_maxpages == 0) if (swap_maxpages == 0)
sysctlbyname("vm.swap_maxpages", sysctlbyname("vm.swap_maxpages",
&swap_maxpages, &sz, NULL, 0); &swap_maxpages, &sz, NULL, 0);
if ( swapary[0].ksw_total > swap_maxpages ) if ( swapary[0].ksw_total > swap_maxpages )
swapary[0].ksw_total = swap_maxpages; swapary[0].ksw_total = swap_maxpages;
retavail = swapary[0].ksw_total; retavail = swapary[0].ksw_total;
retfree = swapary[0].ksw_total - swapary[0].ksw_used; retfree = swapary[0].ksw_total - swapary[0].ksw_used;
#ifdef DEBUG #ifdef DEBUG
printf("swap: %d %d %d %d\n", retavail, printf("swap: %d %d %d %d\n", retavail,
retavail, swapary[0].ksw_total, retavail, swapary[0].ksw_total,
swapary[0].ksw_used); swapary[0].ksw_used);
#endif #endif
return (double)(retavail-retfree)/(double)retavail; return (double)(retavail-retfree)/(double)retavail;
} }
void GetMemLoadPoint(Widget w, caddr_t closure, caddr_t call_data) void GetMemLoadPoint(Widget w, caddr_t closure, caddr_t call_data)
{ {
MemStripChartCallbackData ret; MemStripChartCallbackData ret;
int a, b; int a, b;
size_t sz = sizeof(int); size_t sz = sizeof(int);
double cache = 0; double cache = 0;
if (pageshift < 0) if (pageshift < 0)
initPageShift(); initPageShift();
sysctlbyname("vm.stats.vm.v_wire_count", &a, &sz, NULL, 0); sysctlbyname("vm.stats.vm.v_wire_count", &a, &sz, NULL, 0);
ret.code = (double)a / (double)allmem_pages; ret.code = (double)a / (double)allmem_pages;
sysctlbyname("vm.stats.vm.v_active_count", &a, &sz, NULL, 0); sysctlbyname("vm.stats.vm.v_active_count", &a, &sz, NULL, 0);
ret.buffer = (double)a / (double)allmem_pages; ret.buffer = (double)a / (double)allmem_pages;
/* v_cache_count and v_inactive_count are treated similarly */ /* v_cache_count and v_inactive_count are treated similarly */
sysctlbyname("vm.stats.vm.v_inactive_count", &a, &sz, NULL, 0); sysctlbyname("vm.stats.vm.v_inactive_count", &a, &sz, NULL, 0);
sysctlbyname("vm.stats.vm.v_cache_count", &b, &sz, NULL, 0); sysctlbyname("vm.stats.vm.v_cache_count", &b, &sz, NULL, 0);
ret.cached = (double)(a + b) / allmem_pages; ret.cached = (double)(a + b) / allmem_pages;
sysctlbyname("vm.stats.vm.v_free_count", &a, &sz, NULL, 0); sysctlbyname("vm.stats.vm.v_free_count", &a, &sz, NULL, 0);
ret.free = (double)a / allmem_pages; ret.free = (double)a / allmem_pages;
ret.swap = getSwapFrac(); ret.swap = getSwapFrac();
#ifdef DEBUG #ifdef DEBUG
printf("%u %lf %lf %lf %lf\n", allmem_pages, printf("%u %lf %lf %lf %lf\n", allmem_pages,
ret.code, ret.cached, ret.free, ret.swap); ret.code, ret.cached, ret.free, ret.swap);
#endif #endif
memcpy(call_data, &ret, sizeof(MemStripChartCallbackData)); memcpy(call_data, &ret, sizeof(MemStripChartCallbackData));
}
#endif
/* ------------------------------------------------------------------ */
#if __gnu_linux__
#include <X11/Xos.h>
#include <X11/Intrinsic.h>
#include <stdio.h>
#include <stdlib.h>
#include <err.h>
#include "MemStripChart.h"
static unsigned int total_mem = 0;
int GetRamInKB(int type)
{
FILE *meminfo = fopen("/proc/meminfo", "r");
if(meminfo == NULL)
err(1, "fopen on /proc/meminfo failed: ");
char line[256];
int memory = -1;
int found = 0;
while(found == 0 && fgets(line, sizeof(line), meminfo))
{
if (type == 1) {
if((sscanf(line, "MemTotal: %d kB", &memory)) == 1)
found = 1;
}
if (type == 2) {
if(sscanf(line, "MemFree: %d kB", &memory) == 1)
found = 1;
}
if (type == 3) {
if(sscanf(line, "Buffers: %d kB", &memory) == 1)
found = 1;
}
if (type == 4) {
if(sscanf(line, "Cached: %d kB", &memory) == 1)
found = 1;
}
if (type == 5) {
if(sscanf(line, "SwapTotal: %d kB", &memory) == 1)
found = 1;
}
if (type == 6) {
if(sscanf(line, "SwapFree: %d kB", &memory) == 1)
found = 1;
}
}
if (!found)
warnx("failed to sscanf type %d", type);
fclose(meminfo);
return memory;
}
static void init_total_mem(void)
{
if (total_mem <= 0)
total_mem = GetRamInKB(1);
#ifdef DEBUG
printf("MemTotal: %d\n", total_mem);
#endif
}
void GetMemLoadPoint(Widget w, caddr_t closure, caddr_t call_data)
{
MemStripChartCallbackData ret;
init_total_mem();
/* free(1):
* total
* used = total - free - buffers - cache
* /proc/meminfo
* total = MemTotal
* code = total - free - buffers - cache (used)
* buffer = Buffers
* cached = Cached
* free = MemFree
*/
int mem_free = GetRamInKB(2);
int buffers = GetRamInKB(3);
int cached = GetRamInKB(4);
int swap_total = GetRamInKB(5);
int swap_free = GetRamInKB(6);
ret.code = (total_mem - mem_free - buffers - cached)/(float)total_mem;
ret.buffer = (buffers)/(float)total_mem;
ret.cached = (cached)/(float)total_mem;
ret.free = (mem_free)/(float)total_mem;
ret.swap = (swap_total - swap_free)/(float)swap_total;
#ifdef DEBUG
printf("%u %lf %lf %lf %lf\n", total_mem,
ret.code, ret.cached, ret.free, ret.swap);
#endif
memcpy(call_data, &ret, sizeof(MemStripChartCallbackData));
} }
#endif #endif

38
xmem.c
View File

@ -136,20 +136,20 @@ int main(int argc, char **argv)
Arg args[1]; Arg args[1];
Pixmap icon_pixmap = None; Pixmap icon_pixmap = None;
char *label, host[256]; char *label, host[256];
/* For security reasons, we reset our uid/gid after doing the necessary /* For security reasons, we reset our uid/gid after doing the necessary
system initialization and before calling any X routines. */ system initialization and before calling any X routines. */
if (setgid(getgid()) == -1) if (setgid(getgid()) == -1)
errx(1, "%s: setgid failed: %s\n", argv[0], strerror(errno)); errx(1, "%s: setgid failed: %s\n", argv[0], strerror(errno));
if (setuid(getuid()) == -1) if (setuid(getuid()) == -1)
errx(1, "%s: setuid failed: %s\n", argv[0], strerror(errno)); errx(1, "%s: setuid failed: %s\n", argv[0], strerror(errno));
toplevel = XtAppInitialize(&app_con, "XMem", options_mem, XtNumber(options_mem), toplevel = XtAppInitialize(&app_con, "XMem", options_mem, XtNumber(options_mem),
&argc, argv, NULL, NULL, (Cardinal) 0); &argc, argv, NULL, NULL, (Cardinal) 0);
if (argc != 1) usage(argv[0]); if (argc != 1) usage(argv[0]);
XtGetApplicationResources( toplevel, (XtPointer) &resources, XtGetApplicationResources( toplevel, (XtPointer) &resources,
my_resources_mem, XtNumber(my_resources_mem), my_resources_mem, XtNumber(my_resources_mem),
NULL, (Cardinal) 0); NULL, (Cardinal) 0);
@ -160,7 +160,7 @@ int main(int argc, char **argv)
XtAppAddActions (app_con, xload_actions, XtNumber(xload_actions)); XtAppAddActions (app_con, xload_actions, XtNumber(xload_actions));
XtOverrideTranslations(toplevel, XtOverrideTranslations(toplevel,
XtParseTranslationTable ("<Message>WM_PROTOCOLS: quit()")); XtParseTranslationTable ("<Message>WM_PROTOCOLS: quit()"));
XtSetArg (args[0], XtNiconPixmap, &icon_pixmap); XtSetArg (args[0], XtNiconPixmap, &icon_pixmap);
XtGetValues(toplevel, args, ONE); XtGetValues(toplevel, args, ONE);
if (icon_pixmap == None) { if (icon_pixmap == None) {
@ -171,33 +171,33 @@ int main(int argc, char **argv)
xload_width, xload_height)); xload_width, xload_height));
XtSetValues (toplevel, args, ONE); XtSetValues (toplevel, args, ONE);
} }
if (resources.show_label) { if (resources.show_label) {
pane = XtCreateManagedWidget ("paned", panedWidgetClass, pane = XtCreateManagedWidget ("paned", panedWidgetClass,
toplevel, NULL, ZERO); toplevel, NULL, ZERO);
label_wid = XtCreateManagedWidget ("label", labelWidgetClass, label_wid = XtCreateManagedWidget ("label", labelWidgetClass,
pane, NULL, ZERO); pane, NULL, ZERO);
XtSetArg (args[0], XtNlabel, &label); XtSetArg (args[0], XtNlabel, &label);
XtGetValues(label_wid, args, ONE); XtGetValues(label_wid, args, ONE);
if ( strcmp("label", label) == 0 ) { if ( strcmp("label", label) == 0 ) {
(void) XmuGetHostname (host, 255); (void) XmuGetHostname (host, 255);
XtSetArg (args[0], XtNlabel, host); XtSetArg (args[0], XtNlabel, host);
XtSetValues (label_wid, args, ONE); XtSetValues (label_wid, args, ONE);
} }
load_parent = pane; load_parent = pane;
} }
else else
load_parent = toplevel; load_parent = toplevel;
load = XtCreateManagedWidget ("mem", memStripChartWidgetClass, load = XtCreateManagedWidget ("mem", memStripChartWidgetClass,
load_parent, NULL, ZERO); load_parent, NULL, ZERO);
XtAddCallback(load, XtNgetValue, (void*)GetMemLoadPoint, NULL); XtAddCallback(load, XtNgetValue, (void*)GetMemLoadPoint, NULL);
XtRealizeWidget (toplevel); XtRealizeWidget (toplevel);
wm_delete_window = XInternAtom (XtDisplay(toplevel), "WM_DELETE_WINDOW", wm_delete_window = XInternAtom (XtDisplay(toplevel), "WM_DELETE_WINDOW",
False); False);
@ -205,12 +205,12 @@ int main(int argc, char **argv)
&wm_delete_window, 1); &wm_delete_window, 1);
#if __OpenBSD__ #if __OpenBSD__
if (pledge("ps vminfo stdio", NULL) == -1) if (pledge("ps vminfo stdio", NULL) == -1)
errx(1, "pledge failed: %s", strerror(errno)); errx(1, "pledge failed: %s", strerror(errno));
#endif #endif
XtAppMainLoop(app_con); XtAppMainLoop(app_con);
return 0; return 0;
} }