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,2 +1,11 @@
# xmem(1) X11 Utility
![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
```

103
get_mem.c
View File

@ -244,3 +244,106 @@ void GetMemLoadPoint(Widget w, caddr_t closure, caddr_t call_data)
}
#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