Add Linux support, code cleanups
authorteodor <teodor>
Thu, 29 Nov 2007 17:55:03 +0000 (17:55 +0000)
committerteodor <teodor>
Thu, 29 Nov 2007 17:55:03 +0000 (17:55 +0000)
Makefile
rtop.c
sendtop.c
td_smsg.c
top.h
topd.c

index e531efd..89029ab 100644 (file)
--- 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 (file)
--- a/rtop.c
+++ b/rtop.c
@@ -34,6 +34,7 @@
 #include <errno.h>
 #include <sys/sysctl.h>
 #include <sys/types.h>
+#include <time.h>
 #include <sys/time.h>
 #include <sys/socket.h>
 #include <netinet/in.h>
@@ -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; i<mu->number; 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) )
                        );
        }
index 297eabc..4a48662 100644 (file)
--- a/sendtop.c
+++ b/sendtop.c
@@ -33,6 +33,7 @@
 #include <unistd.h>
 #include <signal.h>
 #include <errno.h>
+#include <ctype.h>
 #include <sys/sysctl.h>
 
 #include "tlog.h"
 
 #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
index 3bfded6..1a8031f 100644 (file)
--- a/td_smsg.c
+++ b/td_smsg.c
@@ -27,6 +27,7 @@
  * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  */
 #include <string.h>
+#include <time.h>
 
 #include "tlog.h"
 #include "connection.h"
diff --git a/top.h b/top.h
index ca89a59..e12f537 100644 (file)
--- 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 (file)
--- a/topd.c
+++ b/topd.c
@@ -94,7 +94,7 @@ main( int argc, char *argv[] ) {
                }
        }
 
-       if (!host);
+       if (!host)
                usage();
 
        signal(SIGCHLD, SIG_IGN);