Skip to content

Commit

Permalink
move some init stuff to machine.c
Browse files Browse the repository at this point in the history
  • Loading branch information
Florian Unglaub committed Mar 28, 2012
1 parent 61fa998 commit b57d7cf
Show file tree
Hide file tree
Showing 8 changed files with 179 additions and 143 deletions.
9 changes: 5 additions & 4 deletions Makefile
Original file line number Diff line number Diff line change
@@ -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

Expand All @@ -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
4 changes: 3 additions & 1 deletion gui.c
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -456,6 +457,7 @@ void info(struct myproc *p)
{
int y, x;
int i;
extern const char *state_abbrev[];

clear();
mvprintw(0, 0,
Expand Down Expand Up @@ -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();
Expand Down
137 changes: 137 additions & 0 deletions machine.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,137 @@
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <sys/types.h>
#include <sys/sysctl.h>

#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();
}
}
18 changes: 18 additions & 0 deletions machine.h
Original file line number Diff line number Diff line change
@@ -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_ */
66 changes: 14 additions & 52 deletions proc.c
Original file line number Diff line number Diff line change
@@ -1,3 +1,12 @@
#include <sys/param.h>
#include <sys/errno.h>
#include <sys/file.h>
#include <sys/proc.h>
#include <sys/resource.h>
#include <sys/sysctl.h>
#include <sys/time.h>
#include <sys/user.h>
#include <sys/vmmeter.h>
#include <sys/stat.h>
#include <stdio.h>
#include <stdlib.h>
Expand All @@ -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)
Expand All @@ -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;
Expand All @@ -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);
Expand All @@ -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];
Expand All @@ -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;

Expand All @@ -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);
Expand Down
18 changes: 2 additions & 16 deletions proc.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
#include <sys/file.h>
#include <sys/proc.h>
#include <sys/resource.h>
#include <sys/signal.h>
//#include <sys/signal.h>
#include <sys/sysctl.h>
#include <sys/time.h>
#include <sys/user.h>
Expand All @@ -27,21 +27,7 @@
#include <grp.h>

// 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
{
Expand Down
Loading

0 comments on commit b57d7cf

Please sign in to comment.