Cleanup get_mem for Open-/FreeBSD

Remove unnecessary files, use ifdef to decide
for which platform to compile for.
This commit is contained in:
Christian Barthel 2020-11-17 18:14:24 +01:00
parent 1268b76c3c
commit 65bf48110a
3 changed files with 154 additions and 148 deletions

View File

@ -1,27 +1,17 @@
# OS=$(uname) make
### OS = OpenBSD
### OS = FreeBSD
PROG= xmem PROG= xmem
VERSION= 1.27 VERSION= 1.27
FreeBSD_lflag= -lkvm
LDFLAGS+= -L/usr/X11R6/lib -L/usr/local/lib/ -lX11 -lXt -lXaw -lXmu LDFLAGS+= -L/usr/X11R6/lib -L/usr/local/lib/ -lX11 -lXt -lXaw -lXmu
LDFLAGS+= $($(OS)_lflag) LDFLAGS+= -lkvm
CFLAGS+= -Wall -I/usr/X11R6/include/ -I/usr/local/include/ CFLAGS+= -Wall -I/usr/X11R6/include/ -I/usr/local/include/
OBJECTS= xmem.o MemStripChart.o OBJECTS= xmem.o MemStripChart.o get_mem.o
FreeBSD_stub = get_mem_fbsd.o
OpenBSD_stub = get_mem.o
_stub = get_mem.o
OBJECTS += $($(OS)_stub)
all: xmem all: xmem
xmem.full: xmem.full:
echo "full not supported yet" @echo "full not supported yet"
xmem.debug: xmem.debug:
echo "debug not supported yet" @echo "debug not supported yet"
xmem: $(OBJECTS) xmem: $(OBJECTS)
$(CC) -o xmem $(OBJECTS) $(LDFLAGS) $(CC) -o xmem $(OBJECTS) $(LDFLAGS)
@ -52,5 +42,4 @@ install:
install -c -o root -g bin -m 644 xmem.bit ${PREFIX}/share/${PROG}/xmem.bit 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 install -c -o root -g bin -m 644 copyright ${PREFIX}/share/${PROG}/copyright
.include <bsd.prog.mk> .include <bsd.prog.mk>

136
get_mem.c
View File

@ -1,7 +1,8 @@
/* $Id: get_mem.c,v 1.5 2018/12/26 18:46:04 bch Exp $ /* $Id: get_mem.c,v 1.5 2018/12/26 18:46:04 bch Exp $
* *
* Adapted: get memory usage on OpenBSD * xmem(1) utility adapted by Christian Barthel <bch@online.de>:
* Author: Christian Barthel <bch@online.de> * - Added OpenBSD support
* - Added FreeBSD support
* *
* get memory usage, from get_load.c derived * get memory usage, from get_load.c derived
* *
@ -10,6 +11,7 @@
* Modified for more recent kernels Helmut Geyer Oct. 1996 * Modified for more recent kernels Helmut Geyer Oct. 1996
*/ */
#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>
@ -108,9 +110,137 @@ void GetMemLoadPoint(Widget w, caddr_t closure, caddr_t call_data)
ret.swap = getSwapFrac(); ret.swap = getSwapFrac();
#ifdef DEBUG #ifdef DEBUG
printf("%lf %lf %lf %lf %lf\n", allmem_kb, ret.code, ret.cached, ret.free, ret.swap); printf("%lf %lf %lf %lf %lf\n",
allmem_kb,
ret.code,
ret.cached,
ret.free,
ret.swap);
#endif #endif
memcpy(call_data, &ret, sizeof(MemStripChartCallbackData)); memcpy(call_data, &ret, sizeof(MemStripChartCallbackData));
} }
#endif
/* ------------------------------------------------------------------- */
#if __FreeBSD__
/* $Id: get_mem_fbsd.c,v 1.1 2018/12/26 18:46:16 bch Exp $
*
* Author: Christian Barthel <bch@online.de>
*/
#include <sys/types.h> /* DEV_BSIZE MAXCOMLEN PZERO */
#include <sys/sysctl.h>
#include <unistd.h>
#include <kvm.h>
#include <X11/Xos.h>
#include <X11/Intrinsic.h>
#include <stdio.h>
#include <stdlib.h>
#include <err.h>
#include "MemStripChart.h"
static int pageshift = -1;
static int pagesize = 0;
static kvm_t *kd = 0;
#define pagetok(size) ((size) << pageshift)
#define LOG1024 10
static unsigned int allmem_pages = 0;
static void initPageShift(void)
{
pagesize = getpagesize();
size_t len = sizeof(allmem_pages);
int mib[len];
pageshift = 0;
while (pagesize > 1) {
pageshift++;
pagesize >>= 1;
}
pageshift -= LOG1024;
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)
perror("sysctl");
#ifdef DEBUG
printf("page_count: %d\n", allmem_pages);
#endif
}
static double getSwapFrac()
{
/* XXX Borrowed from top(1) code: */
int n;
struct kvm_swap swapary[1];
static int pagesize = 0;
static unsigned long swap_maxpages = 0;
size_t sz = sizeof(swap_maxpages);
int retavail = 0;
int retfree = 0;
n = kvm_getswapinfo(kd, swapary, 1, 0);
if (n < 0 || swapary[0].ksw_total == 0)
return (0);
if (swap_maxpages == 0)
sysctlbyname("vm.swap_maxpages",
&swap_maxpages, &sz, NULL, 0);
if ( swapary[0].ksw_total > swap_maxpages )
swapary[0].ksw_total = swap_maxpages;
retavail = swapary[0].ksw_total;
retfree = swapary[0].ksw_total - swapary[0].ksw_used;
#ifdef DEBUG
printf("swap: %d %d %d %d\n", retavail,
retavail, swapary[0].ksw_total,
swapary[0].ksw_used);
#endif
return (double)(retavail-retfree)/(double)retavail;
}
void GetMemLoadPoint(Widget w, caddr_t closure, caddr_t call_data)
{
MemStripChartCallbackData ret;
int a, b;
size_t sz = sizeof(int);
double cache = 0;
if (pageshift < 0)
initPageShift();
sysctlbyname("vm.stats.vm.v_wire_count", &a, &sz, NULL, 0);
ret.code = (double)a / (double)allmem_pages;
sysctlbyname("vm.stats.vm.v_active_count", &a, &sz, NULL, 0);
ret.buffer = (double)a / (double)allmem_pages;
/* 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_cache_count", &b, &sz, NULL, 0);
ret.cached = (double)(a + b) / allmem_pages;
sysctlbyname("vm.stats.vm.v_free_count", &a, &sz, NULL, 0);
ret.free = (double)a / allmem_pages;
ret.swap = getSwapFrac();
#ifdef DEBUG
printf("%u %lf %lf %lf %lf\n", allmem_pages,
ret.code, ret.cached, ret.free, ret.swap);
#endif
memcpy(call_data, &ret, sizeof(MemStripChartCallbackData));
}
#endif

View File

@ -1,113 +0,0 @@
/* $Id: get_mem_fbsd.c,v 1.1 2018/12/26 18:46:16 bch Exp $
*
* Author: Christian Barthel <bch@online.de>
*/
#include <sys/types.h> /* DEV_BSIZE MAXCOMLEN PZERO */
#include <sys/sysctl.h>
#include <unistd.h>
#include <kvm.h>
#include <X11/Xos.h>
#include <X11/Intrinsic.h>
#include <stdio.h>
#include <stdlib.h>
#include <err.h>
#include "MemStripChart.h"
static int pageshift = -1;
static int pagesize = 0;
static kvm_t *kd = 0;
#define pagetok(size) ((size) << pageshift)
#define LOG1024 10
static unsigned int allmem_pages = 0;
static void initPageShift(void)
{
pagesize = getpagesize();
size_t len = sizeof(allmem_pages);
int mib[len];
pageshift = 0;
while (pagesize > 1) {
pageshift++;
pagesize >>= 1;
}
pageshift -= LOG1024;
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)
perror("sysctl");
#ifdef DEBUG
printf("page_count: %d\n", allmem_pages);
#endif
}
static double getSwapFrac()
{
/* XXX Borrowed from top(1) code: */
int n;
struct kvm_swap swapary[1];
static int pagesize = 0;
static unsigned long swap_maxpages = 0;
size_t sz = sizeof(swap_maxpages);
int retavail = 0;
int retfree = 0;
n = kvm_getswapinfo(kd, swapary, 1, 0);
if (n < 0 || swapary[0].ksw_total == 0)
return (0);
if (swap_maxpages == 0)
sysctlbyname("vm.swap_maxpages", &swap_maxpages, &sz, NULL, 0);
if ( swapary[0].ksw_total > swap_maxpages )
swapary[0].ksw_total = swap_maxpages;
retavail = swapary[0].ksw_total;
retfree = swapary[0].ksw_total - swapary[0].ksw_used;
#ifdef DEBUG
printf("swap: %d %d %d %d\n", retavail, retavail, swapary[0].ksw_total,
swapary[0].ksw_used);
#endif
return (double)(retavail-retfree)/(double)retavail;
}
void GetMemLoadPoint(Widget w, caddr_t closure, caddr_t call_data)
{
MemStripChartCallbackData ret;
int a, b;
size_t sz = sizeof(int);
double cache = 0;
if (pageshift < 0)
initPageShift();
sysctlbyname("vm.stats.vm.v_wire_count", &a, &sz, NULL, 0);
ret.code = (double)a / (double)allmem_pages;
sysctlbyname("vm.stats.vm.v_active_count", &a, &sz, NULL, 0);
ret.buffer = (double)a / (double)allmem_pages;
/* 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_cache_count", &b, &sz, NULL, 0);
ret.cached = (double)(a + b) / allmem_pages;
sysctlbyname("vm.stats.vm.v_free_count", &a, &sz, NULL, 0);
ret.free = (double)a / allmem_pages;
ret.swap = getSwapFrac();
#ifdef DEBUG
printf("%u %lf %lf %lf %lf\n", allmem_pages,
ret.code, ret.cached, ret.free, ret.swap);
#endif
memcpy(call_data, &ret, sizeof(MemStripChartCallbackData));
}