From 72d828bf3924ad09b92e247c8806447b373c1c64 Mon Sep 17 00:00:00 2001 From: teodor Date: Thu, 29 Nov 2007 17:55:03 +0000 Subject: [PATCH] Add Linux support, code cleanups --- Makefile | 11 +++++- rtop.c | 5 +-- sendtop.c | 105 +++++++++++++++++++++++++++++++++++++++++++----------- td_smsg.c | 1 + top.h | 6 ++-- topd.c | 2 +- 6 files changed, 103 insertions(+), 27 deletions(-) diff --git a/Makefile b/Makefile index e531efd..89029ab 100644 --- a/Makefile +++ b/Makefile @@ -3,10 +3,19 @@ AR=ar rcv RANLIB=ranlib LD=ld -x -shared +ifndef OS +OS=$(shell uname) +endif + + INCLUDE=-I. -I../tedtools -CFLAGS=-Wall -g -O2 -pedantic -ansi -DASSERT_CORE -D_GNU_SOURCE -DHAVE_POLL_H -DHAVE_SYS_POLL_H -DHAVE_HSTRERROR +CFLAGS=-Wall -g -O2 -pedantic -ansi -D$(OS) LIB=-L../tedtools -ltedtools +ifeq ($(OS), Linux) +CFLAGS+= -D_GNU_SOURCE -D_LARGE_FILES -D_FILE_OFFSET_BITS=64 +endif + PROGS=sendtop topd rtop .SUFFIXES: .o.c diff --git a/rtop.c b/rtop.c index 410e5c6..85a0eb3 100644 --- a/rtop.c +++ b/rtop.c @@ -34,6 +34,7 @@ #include #include #include +#include #include #include #include @@ -102,13 +103,13 @@ main( int argc, char *argv[] ) { pmsg = (TCMsg*) cs.buf; mu = (MassiveUnit*)pmsg->data; - printf("\tIP\tLoad\tFree\tTotal\tData\n"); + printf("\tIP\tLoad\tFree\tTotal\tDate\n"); for(i=0; inumber; i++) printf("%s\t%.2f\t%.1fM\t%.1fM\t%s", inet_ntoa(mu->units[i].ip), mu->units[i].top.load, ((float) mu->units[i].top.freemem)/( 1024*1024.0 ), - ((float) mu->units[i].top.usermem)/( 1024*1024.0 ), + ((float) mu->units[i].top.physmem)/( 1024*1024.0 ), ctime( &(mu->units[i].stamp) ) ); } diff --git a/sendtop.c b/sendtop.c index 297eabc..4a48662 100644 --- a/sendtop.c +++ b/sendtop.c @@ -33,6 +33,7 @@ #include #include #include +#include #include #include "tlog.h" @@ -41,18 +42,18 @@ #include "top.h" -#define NITEM 0 +#ifdef FreeBSD static int getsysctl(char *name, void *ptr, size_t len) { size_t nlen = len; if (sysctlbyname(name, ptr, &nlen, NULL, 0) == -1) { - tlog(TL_ALARM, "sysctl(%s...) failed: %s\n", name, + tlog(TL_ALARM, "sysctl(\"%s\", ...) failed: %s\n", name, strerror(errno)); return 1; } if (nlen != len) { - tlog(TL_ALARM, "sysctl(%s...) expected %lu, got %lu\n", name, + tlog(TL_ALARM, "sysctl(\"%s\", ...) expected %lu, got %lu\n", name, (unsigned long)len, (unsigned long)nlen); return 1; } @@ -63,25 +64,15 @@ getsysctl(char *name, void *ptr, size_t len) { #define GETSYSCTL(name, var) getsysctl(name, &(var), sizeof(var)) static void -fillMsgTop(TCMsgTop *m) { - double lll[3]; +getmeminfo(TCMsgTop *m) { int memory_tmp; + long memory_tmp1; - memset(m,0,sizeof(TCMsgTop)); - if ( getloadavg(lll,3) >= NITEM+1 ) - m->load=lll[NITEM]; - else - m->load=-1.0; - - m->freemem = 0; if ( GETSYSCTL("vm.stats.vm.v_inactive_count", memory_tmp) == 0 ) m->freemem += memory_tmp; else m->freemem = -1; - if ( m->freemem>=0 && GETSYSCTL("vm.stats.vm.v_cache_count", memory_tmp) == 0 ) - m->freemem += memory_tmp; - else - m->freemem = -1; + if ( m->freemem>=0 && GETSYSCTL("vm.stats.vm.v_free_count", memory_tmp) == 0 ) m->freemem += memory_tmp; else @@ -90,11 +81,85 @@ fillMsgTop(TCMsgTop *m) { if ( m->freemem>=0 ) m->freemem *= getpagesize(); - m->usermem =-1; - if ( GETSYSCTL("hw.usermem", memory_tmp) == 0 ) - m->usermem = memory_tmp; + if ( GETSYSCTL("hw.physmem", memory_tmp1) == 0 ) + m->physmem = memory_tmp1; +} + + +#elif defined Linux /* end FreeBSD */ + +#define MEMBUFSIZE 256 + +static char* +cmpPattern( char *str, char *pattern ) { + while( *str != '\0' && *str == *pattern ) { + str++; + pattern++; + } + + if ( *pattern == '\0' ) { + if ( *str == '\0' ) + return NULL; + + /* + * str matches by pattern, so skip leading spaces + * before actual value + */ + while( *str != '\0' ) { + if ( isspace( *str ) ) + str++; + else + return str; + } + } + + return NULL; +} + +static void +getmeminfo(TCMsgTop *m) { + FILE *fh; + static char buf[MEMBUFSIZE]; + char *ptr; + + fh=fopen("/proc/meminfo", "r"); + if (fh == NULL) { + tlog(TL_ALARM, "fopen(\"/proc/meminfo\", ...) failed: %s", strerror(errno)); + return; + } + + while( fgets(buf, MEMBUFSIZE, fh) && (m->physmem == 0 || m->freemem == 0) ) { + if ( (ptr = cmpPattern(buf, "MemTotal:")) != NULL ) { + m->physmem = atoll(ptr) * 1024; /* mem in Kb */ + } else if ( (ptr = cmpPattern(buf, "MemFree:")) != NULL ) { + m->freemem = atoll(ptr) * 1024; /* mem in Kb */ + } + } + + fclose(fh); +} + +#else /* end Linux*/ +#error No memory stat for current OS +#endif + +#define NITEM 0 + +static void +fillMsgTop(TCMsgTop *m) { + double lll[3]; + + memset(m,0,sizeof(TCMsgTop)); + if ( getloadavg(lll,3) >= NITEM+1 ) + m->load=lll[NITEM]; + else + m->load=-1.0; + + m->freemem = 0; + m->physmem = 0; + getmeminfo(m); - tlog( TL_DEBUG, "Sended topdata: %.2f load, %d free, %d total", m->load, m->freemem, m->usermem); + tlog( TL_DEBUG, "Sended topdata: %.2f load, %lld free, %lld total", m->load, m->freemem, m->physmem); } static int diff --git a/td_smsg.c b/td_smsg.c index 3bfded6..1a8031f 100644 --- a/td_smsg.c +++ b/td_smsg.c @@ -27,6 +27,7 @@ * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ #include +#include #include "tlog.h" #include "connection.h" diff --git a/top.h b/top.h index ca89a59..e12f537 100644 --- a/top.h +++ b/top.h @@ -38,9 +38,9 @@ #define TOPGETTYPE 0x06ED10AD typedef struct { - double load; - int freemem; - int usermem; + double load; + int64_t freemem; + int64_t physmem; } TCMsgTop; typedef struct { diff --git a/topd.c b/topd.c index 1f1378c..f532a7d 100644 --- a/topd.c +++ b/topd.c @@ -94,7 +94,7 @@ main( int argc, char *argv[] ) { } } - if (!host); + if (!host) usage(); signal(SIGCHLD, SIG_IGN); -- 2.37.3