From b57d7cf43e7a7febf8dd9fca4806b946b3149de7 Mon Sep 17 00:00:00 2001 From: Florian Unglaub Date: Wed, 28 Mar 2012 16:22:11 +0200 Subject: [PATCH] move some init stuff to machine.c --- Makefile | 9 ++-- gui.c | 4 +- machine.c | 137 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ machine.h | 18 +++++++ proc.c | 66 ++++++-------------------- proc.h | 18 +------ util.c | 67 -------------------------- util.h | 3 -- 8 files changed, 179 insertions(+), 143 deletions(-) create mode 100644 machine.c create mode 100644 machine.h diff --git a/Makefile b/Makefile index 985c300..6b55060 100644 --- a/Makefile +++ b/Makefile @@ -1,7 +1,7 @@ CFLAGS = -g -Wall -Wextra -pedantic -std=c99 LDFLAGS = -g -lncurses -lkvm PREFIX = /usr -OBJ = main.o proc.o gui.o util.o +OBJ = main.o proc.o gui.o util.o machine.o .PHONY: clean install uninstall @@ -19,7 +19,8 @@ install: utop uninstall: rm -f ${PREFIX}/bin/utop ${PREFIX}/share/man/man1/ -gui.o: gui.c proc.h gui.h util.h config.h -main.o: main.c proc.h gui.h -proc.o: proc.c proc.h util.h +gui.o: gui.c proc.h gui.h util.h config.h machine.c machine.h +main.o: main.c proc.h gui.h config.h +proc.o: proc.c proc.h util.h machine.c machine.h util.o: util.c util.h +machine.o: machine.c diff --git a/gui.c b/gui.c index a1194df..a5e7674 100644 --- a/gui.c +++ b/gui.c @@ -13,6 +13,7 @@ #include "proc.h" #include "gui.h" #include "config.h" +#include "machine.h" #include "util.h" #define STATUS(y, x, ...) do{ mvprintw(y, x, __VA_ARGS__); clrtoeol(); }while(0) @@ -456,6 +457,7 @@ void info(struct myproc *p) { int y, x; int i; + extern const char *state_abbrev[]; clear(); mvprintw(0, 0, @@ -613,7 +615,7 @@ void gui_run(struct myproc **procs) int fin = 0; memset(&pst, 0, sizeof pst); - + init_machine(&pst); proc_update(procs, &pst); last_full_refresh = mstime(); diff --git a/machine.c b/machine.c new file mode 100644 index 0000000..b8e8c0f --- /dev/null +++ b/machine.c @@ -0,0 +1,137 @@ +#include +#include +#include +#include +#include +#include + +#include "util.h" +#include "machine.h" +#include "proc.h" + +// Process states - short form +// SIDL 1 /* Process being created by fork. */ +// SRUN 2 /* Currently runnable. */ +// SSLEEP 3 /* Sleeping on an address. */ +// SSTOP 4 /* Process debugging or suspension. */ +// SZOMB 5 /* Awaiting collection by parent. */ +// SWAIT 6 /* Waiting for interrupt. */ +// SLOCK 7 /* Blocked on a lock. */ + +// Take from top(8) +const char *state_abbrev[] = { + "", "START", "RUN\0\0\0", "SLEEP", "STOP", "ZOMB", "WAIT", "LOCK" +}; + +/* these are for detailing the memory statistics */ +char *memorynames[] = { + "Active, ", "Inact, ", "Wired, ", "Cache, ", "Buf, ", + "Free", NULL +}; + +void init_machine(struct procstat *pst) +{ + extern int pageshift; // defined in machine.h + int mib[2], pagesize; + struct timeval boottime; + size_t bt_size; + + /* get the page size and calculate pageshift from it */ + pagesize = getpagesize(); + pageshift = 0; + while (pagesize > 1) { + pageshift++; + pagesize >>= 1; + } + + /* we only need the amount of log(2)1024 for our conversion */ + pageshift -= LOG1024; + + // TODO: do this only once, not continously + // Get the boottime from the kernel to calculate uptime + mib[0] = CTL_KERN; + mib[1] = KERN_BOOTTIME; + bt_size = sizeof(boottime); + if (sysctl(mib, 2, &boottime, &bt_size, NULL, 0) != -1 && + boottime.tv_sec != 0) { + pst->boottime = boottime; + } else { + pst->boottime.tv_sec = -1; + } +} + +const char *uptime_from_boottime(time_t boottime) +{ + static char buf[64]; // Should be sufficient + time_t now; + struct tm *ltime; + unsigned long int diff_secs; // The difference between now and the epoch + unsigned long int rest; + unsigned int days, hours, minutes, seconds; + + time(&now); + ltime = localtime(&now); + + diff_secs = now-boottime; + + days = diff_secs/86400; + rest = diff_secs % 86400; + hours = rest / 3600; + rest = rest % 3600; + minutes = rest/60; + rest = rest % 60; + seconds = (unsigned int)rest; + + snprintf(buf, sizeof buf, "up %d+%02d:%02d:%02d %02d:%02d:%02d", days, hours, minutes, seconds, ltime->tm_hour, ltime->tm_min, ltime->tm_sec); + return buf; +} + +const char* format_memory(int memory[6]) +{ + int i, slen; + char prefix; + char buf[6][128]; + static char *memory_string; + + for(i=0; i<6; i++){ + int val = memory[i]; // default is KiloBytes + + if(val/1024/1024 > 1){ // display GigaBytes + prefix = 'G'; + val = val/(1024*1024); + } + else if(val/1024 > 1){ // display MegaBytes + prefix = 'M'; + val /= 1024; + } else { // KiloBytes + prefix = 'K'; + } + snprintf(buf[i], sizeof buf[i], "%d%c %s", val, prefix, memorynames[i]); + slen += strlen(buf[i]); + } + + memory_string = umalloc(slen+1); + for(i=0; i<6; i++){ + /* memory_string += sprintf(memory_string, "%s ", buf[i]); */ + memory_string = strncat(memory_string, buf[i], strlen(buf[i])); + } + + return memory_string; +} + +// Taken from top(8) +void getsysctl(const char *name, void *ptr, size_t len) +{ + size_t nlen = len; + + if (sysctlbyname(name, ptr, &nlen, NULL, 0) == -1) { + fprintf(stderr, "top: sysctl(%s...) failed: %s\n", name, + strerror(errno)); + abort(); + } + if (nlen != len) { + fprintf(stderr, "top: sysctl(%s...) expected %lu, got %lu\n", + name, (unsigned long)len, (unsigned long)nlen); + abort(); + } +} diff --git a/machine.h b/machine.h new file mode 100644 index 0000000..a71be33 --- /dev/null +++ b/machine.h @@ -0,0 +1,18 @@ +#ifndef _MACHINE_H_ +#define _MACHINE_H_ + +#include "proc.h" + +#define GETSYSCTL(name, var) getsysctl(name, &(var), sizeof(var)) + +int pageshift; + +/* define pagetok in terms of pageshift */ +#define LOG1024 10 // log_2(1024) +#define pagetok(size) ((size) << pageshift) + +void init_machine(struct procstat *pst); +const char *uptime_from_boottime(time_t boottime); +const char* format_memory(int memory[6]); +void getsysctl(const char *name, void *ptr, size_t len); +#endif /* _MACHINE_H_ */ diff --git a/proc.c b/proc.c index fc0c1ba..2199792 100644 --- a/proc.c +++ b/proc.c @@ -1,3 +1,12 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include #include #include #include @@ -6,8 +15,8 @@ #include "proc.h" #include "util.h" #include "main.h" +#include "machine.h" -#define GETSYSCTL(name, var) getsysctl(name, &(var), sizeof(var)) #define ITER_PROCS(i, p, ps) \ for(i = 0; i < HASH_TABLE_SIZE; i++) \ for(p = ps[i]; p; p = p->hash_next) @@ -24,34 +33,11 @@ */ -// Take from top(8) -char *state_abbrev[] = { - "", "START", "RUN\0\0\0", "SLEEP", "STOP", "ZOMB", "WAIT", "LOCK" -}; - static void proc_update_single(struct myproc *proc, struct myproc **procs, struct procstat *ps); static void proc_handle_rename(struct myproc *p); static kvm_t *kd = NULL; // kvm handle - -// Taken from top(8) -static void getsysctl(const char *name, void *ptr, size_t len) -{ - size_t nlen = len; - - if (sysctlbyname(name, ptr, &nlen, NULL, 0) == -1) { - fprintf(stderr, "top: sysctl(%s...) failed: %s\n", name, - strerror(errno)); - abort(); - } - if (nlen != len) { - fprintf(stderr, "top: sysctl(%s...) expected %lu, got %lu\n", - name, (unsigned long)len, (unsigned long)nlen); - abort(); - } -} - void proc_free(struct myproc *p) { char **iter; @@ -71,21 +57,8 @@ void proc_free(struct myproc *p) static void getprocstat(struct procstat *pst) { struct loadavg sysload; - int i, mib[2], pagesize; - size_t bt_size; - struct timeval boottime; - static int pageshift; - - /* get the page size and calculate pageshift from it */ - pagesize = getpagesize(); - pageshift = 0; - while (pagesize > 1) { - pageshift++; - pagesize >>= 1; - } - - /* we only need the amount of log(2)1024 for our conversion */ - pageshift -= LOG1024; + int i; + extern int pageshift; // defined in machine.h // Load average GETSYSCTL("vm.loadavg", sysload); @@ -94,18 +67,6 @@ static void getprocstat(struct procstat *pst) pst->fscale = sysload.fscale; - // TODO: do this only once, not continously - // Get the boottime from the kernel to calculate uptime - mib[0] = CTL_KERN; - mib[1] = KERN_BOOTTIME; - bt_size = sizeof(boottime); - if (sysctl(mib, 2, &boottime, &bt_size, NULL, 0) != -1 && - boottime.tv_sec != 0) { - pst->boottime = boottime; - } else { - pst->boottime.tv_sec = -1; - } - // Memory stuff static long bufspace = 0; static int memory_stats[6]; @@ -128,6 +89,7 @@ static void getprocstat(struct procstat *pst) const char *proc_state_str(struct kinfo_proc *pp) { static char status[10]; + extern const char *state_abbrev[]; char state = pp->ki_stat; @@ -153,7 +115,7 @@ const char *proc_state_str(struct kinfo_proc *pp) { /* FALLTHROUGH */ default: if (state >= 0 && - state < (signed) (sizeof(state_abbrev) / sizeof(*state_abbrev))) + state < (signed) (sizeof(char**) / sizeof(char*))) sprintf(status, "%.6s", state_abbrev[(int)state]); else sprintf(status, "?%5d", state); diff --git a/proc.h b/proc.h index 9d92029..3f8e77f 100644 --- a/proc.h +++ b/proc.h @@ -6,7 +6,7 @@ #include #include #include -#include +//#include #include #include #include @@ -27,21 +27,7 @@ #include // defined in proc.c -extern char *state_abbrev[]; -extern int pageshift; - -/* define pagetok in terms of pageshift */ -#define LOG1024 10 // log_2(1024) -#define pagetok(size) ((size) << pageshift) - -// Process states - short form -// SIDL 1 /* Process being created by fork. */ -// SRUN 2 /* Currently runnable. */ -// SSLEEP 3 /* Sleeping on an address. */ -// SSTOP 4 /* Process debugging or suspension. */ -// SZOMB 5 /* Awaiting collection by parent. */ -// SWAIT 6 /* Waiting for interrupt. */ -// SLOCK 7 /* Blocked on a lock. */ +//extern char *state_abbrev[]; struct myproc { diff --git a/util.c b/util.c index cb36392..35c8340 100644 --- a/util.c +++ b/util.c @@ -5,14 +5,6 @@ #include #include - -/* these are for detailing the memory statistics */ -char *memorynames[] = { - "Active, ", "Inact, ", "Wired, ", "Cache, ", "Buf, ", - "Free", NULL -}; - - void *umalloc(size_t l) { void *p = malloc(l); @@ -100,62 +92,3 @@ int longest_passwd_line(const char *fname) return max; } - -const char *uptime_from_boottime(time_t boottime) -{ - static char buf[64]; // Should be sufficient - time_t now; - struct tm *ltime; - unsigned long int diff_secs; // The difference between now and the epoch - unsigned long int rest; - unsigned int days, hours, minutes, seconds; - - time(&now); - ltime = localtime(&now); - - diff_secs = now-boottime; - - days = diff_secs/86400; - rest = diff_secs % 86400; - hours = rest / 3600; - rest = rest % 3600; - minutes = rest/60; - rest = rest % 60; - seconds = (unsigned int)rest; - - snprintf(buf, sizeof buf, "up %d+%02d:%02d:%02d %02d:%02d:%02d", days, hours, minutes, seconds, ltime->tm_hour, ltime->tm_min, ltime->tm_sec); - return buf; -} - -const char* format_memory(int memory[6]) -{ - int i, slen; - char prefix; - char buf[6][128]; - static char *memory_string; - - for(i=0; i<6; i++){ - int val = memory[i]; // default is KiloBytes - - if(val/1024/1024 > 1){ // display GigaBytes - prefix = 'G'; - val = val/(1024*1024); - } - else if(val/1024 > 1){ // display MegaBytes - prefix = 'M'; - val /= 1024; - } else { // KiloBytes - prefix = 'K'; - } - snprintf(buf[i], sizeof buf[i], "%d%c %s", val, prefix, memorynames[i]); - slen += strlen(buf[i]); - } - - memory_string = umalloc(slen+1); - for(i=0; i<6; i++){ - /* memory_string += sprintf(memory_string, "%s ", buf[i]); */ - memory_string = strncat(memory_string, buf[i], strlen(buf[i])); - } - - return memory_string; -} diff --git a/util.h b/util.h index 5ed06d3..3162a55 100644 --- a/util.h +++ b/util.h @@ -11,7 +11,4 @@ char *ustrdup(const char *s); int str_to_sig(const char *); int longest_passwd_line(const char *fname); - -const char *uptime_from_boottime(time_t boottime); -const char* format_memory(int memory[6]); #endif