Cleanups from Denis Vlasenko.

This commit is contained in:
Rob Landley 2006-02-13 22:04:27 +00:00
parent 90632d021c
commit b2804551a0
2 changed files with 175 additions and 166 deletions

View File

@ -13,9 +13,24 @@
#include <stdlib.h> #include <stdlib.h>
#include <unistd.h> #include <unistd.h>
#include <asm/page.h> #include <asm/page.h>
#include <fcntl.h>
#include "libbb.h" #include "libbb.h"
static int read_to_buf(char *filename, void *buf, int bufsize)
{
int fd;
fd = open(filename, O_RDONLY);
if(fd < 0)
return -1;
bufsize = read(fd, buf, bufsize);
close(fd);
return bufsize;
}
extern procps_status_t * procps_scan(int save_user_arg0) extern procps_status_t * procps_scan(int save_user_arg0)
{ {
static DIR *dir; static DIR *dir;
@ -24,8 +39,8 @@ extern procps_status_t * procps_scan(int save_user_arg0)
char *name; char *name;
int n; int n;
char status[32]; char status[32];
char *status_tail;
char buf[1024]; char buf[1024];
FILE *fp;
procps_status_t curstatus; procps_status_t curstatus;
int pid; int pid;
long tasknice; long tasknice;
@ -50,18 +65,14 @@ extern procps_status_t * procps_scan(int save_user_arg0)
pid = atoi(name); pid = atoi(name);
curstatus.pid = pid; curstatus.pid = pid;
sprintf(status, "/proc/%d", pid); status_tail = status + sprintf(status, "/proc/%d", pid);
if(stat(status, &sb)) if(stat(status, &sb))
continue; continue;
bb_getpwuid(curstatus.user, sb.st_uid, sizeof(curstatus.user)); bb_getpwuid(curstatus.user, sb.st_uid, sizeof(curstatus.user));
sprintf(status, "/proc/%d/stat", pid); strcpy(status_tail, "/stat");
n = read_to_buf(status, buf, sizeof(buf));
if((fp = fopen(status, "r")) == NULL) if(n < 0)
continue;
name = fgets(buf, sizeof(buf), fp);
fclose(fp);
if(name == NULL)
continue; continue;
name = strrchr(buf, ')'); /* split into "PID (cmd" and "<rest>" */ name = strrchr(buf, ')'); /* split into "PID (cmd" and "<rest>" */
if(name == 0 || name[1] != ' ') if(name == 0 || name[1] != ' ')
@ -113,10 +124,9 @@ extern procps_status_t * procps_scan(int save_user_arg0)
#endif #endif
if(save_user_arg0) { if(save_user_arg0) {
sprintf(status, "/proc/%d/cmdline", pid); strcpy(status_tail, "/cmdline");
if((fp = fopen(status, "r")) == NULL) n = read_to_buf(status, buf, sizeof(buf));
continue; if(n > 0) {
if((n=fread(buf, 1, sizeof(buf)-1, fp)) > 0) {
if(buf[n-1]=='\n') if(buf[n-1]=='\n')
buf[--n] = 0; buf[--n] = 0;
name = buf; name = buf;
@ -131,7 +141,6 @@ extern procps_status_t * procps_scan(int save_user_arg0)
curstatus.cmd = strdup(buf); curstatus.cmd = strdup(buf);
/* if NULL it work true also */ /* if NULL it work true also */
} }
fclose(fp);
} }
return memcpy(&ret_status, &curstatus, sizeof(procps_status_t)); return memcpy(&ret_status, &curstatus, sizeof(procps_status_t));
} }

View File

@ -53,13 +53,13 @@ static int ntop;
#ifdef CONFIG_FEATURE_USE_TERMIOS #ifdef CONFIG_FEATURE_USE_TERMIOS
static int pid_sort (procps_status_t *P, procps_status_t *Q) static int pid_sort (procps_status_t *P, procps_status_t *Q)
{ {
return (Q->pid - P->pid); return (Q->pid - P->pid);
} }
#endif #endif
static int mem_sort (procps_status_t *P, procps_status_t *Q) static int mem_sort (procps_status_t *P, procps_status_t *Q)
{ {
return (int)(Q->rss - P->rss); return (int)(Q->rss - P->rss);
} }
#ifdef CONFIG_FEATURE_TOP_CPU_USAGE_PERCENTAGE #ifdef CONFIG_FEATURE_TOP_CPU_USAGE_PERCENTAGE
@ -69,32 +69,32 @@ static cmp_t sort_function[sort_depth];
static int pcpu_sort (procps_status_t *P, procps_status_t *Q) static int pcpu_sort (procps_status_t *P, procps_status_t *Q)
{ {
return (Q->pcpu - P->pcpu); return (Q->pcpu - P->pcpu);
} }
static int time_sort (procps_status_t *P, procps_status_t *Q) static int time_sort (procps_status_t *P, procps_status_t *Q)
{ {
return (int)((Q->stime + Q->utime) - (P->stime + P->utime)); return (int)((Q->stime + Q->utime) - (P->stime + P->utime));
} }
static int mult_lvl_cmp(void* a, void* b) { static int mult_lvl_cmp(void* a, void* b) {
int i, cmp_val; int i, cmp_val;
for(i = 0; i < sort_depth; i++) { for(i = 0; i < sort_depth; i++) {
cmp_val = (*sort_function[i])(a, b); cmp_val = (*sort_function[i])(a, b);
if (cmp_val != 0) if (cmp_val != 0)
return cmp_val; return cmp_val;
} }
return 0; return 0;
} }
/* This structure stores some critical information from one frame to /* This structure stores some critical information from one frame to
the next. mostly used for sorting. Added cumulative and resident fields. */ the next. mostly used for sorting. Added cumulative and resident fields. */
struct save_hist { struct save_hist {
int ticks; int ticks;
int pid; int pid;
int utime; int utime;
int stime; int stime;
}; };
/* /*
@ -130,152 +130,152 @@ static unsigned long Hertz;
* *
*/ */
#define FILE_TO_BUF(filename, fd) do{ \ static int file_to_buf(char *buf, int bufsize, char *filename, int *d)
if (fd == -1 && (fd = open(filename, O_RDONLY)) == -1) { \ {
bb_perror_msg_and_die("/proc not be mounted?"); \ int fd = *d;
} \ int sz;
lseek(fd, 0L, SEEK_SET); \
if ((local_n = read(fd, buf, sizeof buf - 1)) < 0) { \
bb_perror_msg_and_die("%s", filename); \
} \
buf[local_n] = '\0'; \
}while(0)
#define FILE_TO_BUF2(filename, fd) do{ \ if (fd == -1) {
lseek(fd, 0L, SEEK_SET); \ fd = open(filename, O_RDONLY);
if ((local_n = read(fd, buf, sizeof buf - 1)) < 0) { \ if(fd == -1)
bb_perror_msg_and_die("%s", filename); \ bb_perror_msg_and_die("is /proc mounted?");
} \ } else {
buf[local_n] = '\0'; \ lseek(fd, 0L, SEEK_SET);
}while(0) }
sz = read(fd, buf, bufsize - 1);
if (sz < 0) {
bb_perror_msg_and_die("%s", filename);
}
buf[sz] = '\0';
*d = fd;
return sz;
}
static void init_Hertz_value(void) { static void init_Hertz_value(void)
unsigned long user_j, nice_j, sys_j, other_j; /* jiffies (clock ticks) */ {
double up_1, up_2, seconds; unsigned long user_j, nice_j, sys_j, other_j; /* jiffies (clock ticks) */
unsigned long jiffies, h; double up_1, up_2, seconds;
char buf[80]; unsigned long jiffies, h;
int uptime_fd = -1; char buf[80];
int stat_fd = -1; int uptime_fd = -1;
int stat_fd = -1;
long smp_num_cpus = sysconf(_SC_NPROCESSORS_CONF); long smp_num_cpus = sysconf(_SC_NPROCESSORS_CONF);
if(smp_num_cpus<1) smp_num_cpus=1; if(smp_num_cpus<1) smp_num_cpus=1;
do {
int local_n;
FILE_TO_BUF("uptime", uptime_fd); do {
up_1 = strtod(buf, 0); file_to_buf(buf, sizeof(buf), "uptime", &uptime_fd);
FILE_TO_BUF("stat", stat_fd); up_1 = strtod(buf, 0);
sscanf(buf, "cpu %lu %lu %lu %lu", &user_j, &nice_j, &sys_j, &other_j); file_to_buf(buf, sizeof(buf), "stat", &stat_fd);
FILE_TO_BUF2("uptime", uptime_fd); sscanf(buf, "cpu %lu %lu %lu %lu", &user_j, &nice_j, &sys_j, &other_j);
up_2 = strtod(buf, 0); file_to_buf(buf, sizeof(buf), "uptime", &uptime_fd);
} while((long)( (up_2-up_1)*1000.0/up_1 )); /* want under 0.1% error */ up_2 = strtod(buf, 0);
} while((long)( (up_2-up_1)*1000.0/up_1 )); /* want under 0.1% error */
close(uptime_fd);
close(stat_fd);
close(uptime_fd); jiffies = user_j + nice_j + sys_j + other_j;
close(stat_fd); seconds = (up_1 + up_2) / 2;
h = (unsigned long)( (double)jiffies/seconds/smp_num_cpus );
jiffies = user_j + nice_j + sys_j + other_j; /* actual values used by 2.4 kernels: 32 64 100 128 1000 1024 1200 */
seconds = (up_1 + up_2) / 2; switch(h) {
h = (unsigned long)( (double)jiffies/seconds/smp_num_cpus ); case 30 ... 34 : Hertz = 32; break; /* ia64 emulator */
/* actual values used by 2.4 kernels: 32 64 100 128 1000 1024 1200 */ case 48 ... 52 : Hertz = 50; break;
switch(h){ case 58 ... 62 : Hertz = 60; break;
case 30 ... 34 : Hertz = 32; break; /* ia64 emulator */ case 63 ... 65 : Hertz = 64; break; /* StrongARM /Shark */
case 48 ... 52 : Hertz = 50; break; case 95 ... 105 : Hertz = 100; break; /* normal Linux */
case 58 ... 62 : Hertz = 60; break; case 124 ... 132 : Hertz = 128; break; /* MIPS, ARM */
case 63 ... 65 : Hertz = 64; break; /* StrongARM /Shark */ case 195 ... 204 : Hertz = 200; break; /* normal << 1 */
case 95 ... 105 : Hertz = 100; break; /* normal Linux */ case 253 ... 260 : Hertz = 256; break;
case 124 ... 132 : Hertz = 128; break; /* MIPS, ARM */ case 295 ... 304 : Hertz = 300; break; /* 3 cpus */
case 195 ... 204 : Hertz = 200; break; /* normal << 1 */ case 393 ... 408 : Hertz = 400; break; /* normal << 2 */
case 253 ... 260 : Hertz = 256; break; case 495 ... 504 : Hertz = 500; break; /* 5 cpus */
case 295 ... 304 : Hertz = 300; break; /* 3 cpus */ case 595 ... 604 : Hertz = 600; break; /* 6 cpus */
case 393 ... 408 : Hertz = 400; break; /* normal << 2 */ case 695 ... 704 : Hertz = 700; break; /* 7 cpus */
case 495 ... 504 : Hertz = 500; break; /* 5 cpus */ case 790 ... 808 : Hertz = 800; break; /* normal << 3 */
case 595 ... 604 : Hertz = 600; break; /* 6 cpus */ case 895 ... 904 : Hertz = 900; break; /* 9 cpus */
case 695 ... 704 : Hertz = 700; break; /* 7 cpus */ case 990 ... 1010 : Hertz = 1000; break; /* ARM */
case 790 ... 808 : Hertz = 800; break; /* normal << 3 */ case 1015 ... 1035 : Hertz = 1024; break; /* Alpha, ia64 */
case 895 ... 904 : Hertz = 900; break; /* 9 cpus */ case 1095 ... 1104 : Hertz = 1100; break; /* 11 cpus */
case 990 ... 1010 : Hertz = 1000; break; /* ARM */ case 1180 ... 1220 : Hertz = 1200; break; /* Alpha */
case 1015 ... 1035 : Hertz = 1024; break; /* Alpha, ia64 */ default:
case 1095 ... 1104 : Hertz = 1100; break; /* 11 cpus */ /* If 32-bit or big-endian (not Alpha or ia64), assume HZ is 100. */
case 1180 ... 1220 : Hertz = 1200; break; /* Alpha */ Hertz = (sizeof(long)==sizeof(int) || htons(999)==999) ? 100UL : 1024UL;
default: }
/* If 32-bit or big-endian (not Alpha or ia64), assume HZ is 100. */
Hertz = (sizeof(long)==sizeof(int) || htons(999)==999) ? 100UL : 1024UL;
}
} }
static void do_stats(void) static void do_stats(void)
{ {
struct timeval t; struct timeval t;
static struct timeval oldtime; static struct timeval oldtime;
struct timezone timez; struct timezone timez;
float elapsed_time; float elapsed_time;
procps_status_t *cur; procps_status_t *cur;
int total_time, i, n; int total_time, i, n;
static int prev_count; static int prev_count;
int systime, usrtime, pid; int systime, usrtime, pid;
struct save_hist *New_save_hist; struct save_hist *New_save_hist;
/*
* Finds the current time (in microseconds) and calculates the time
* elapsed since the last update.
*/
gettimeofday(&t, &timez);
elapsed_time = (t.tv_sec - oldtime.tv_sec)
+ (float) (t.tv_usec - oldtime.tv_usec) / 1000000.0;
oldtime.tv_sec = t.tv_sec;
oldtime.tv_usec = t.tv_usec;
New_save_hist = alloca(sizeof(struct save_hist)*ntop);
/*
* Make a pass through the data to get stats.
*/
for(n = 0; n < ntop; n++) {
cur = top + n;
/* /*
* Calculate time in cur process. Time is sum of user time * Finds the current time (in microseconds) and calculates the time
* (usrtime) plus system time (systime). * elapsed since the last update.
*/ */
systime = cur->stime; gettimeofday(&t, &timez);
usrtime = cur->utime; elapsed_time = (t.tv_sec - oldtime.tv_sec)
pid = cur->pid; + (float) (t.tv_usec - oldtime.tv_usec) / 1000000.0;
total_time = systime + usrtime; oldtime.tv_sec = t.tv_sec;
New_save_hist[n].ticks = total_time; oldtime.tv_usec = t.tv_usec;
New_save_hist[n].pid = pid;
New_save_hist[n].stime = systime;
New_save_hist[n].utime = usrtime;
/* find matching entry from previous pass */ New_save_hist = alloca(sizeof(struct save_hist)*ntop);
for (i = 0; i < prev_count; i++) { /*
if (save_history[i].pid == pid) { * Make a pass through the data to get stats.
total_time -= save_history[i].ticks; */
systime -= save_history[i].stime; for(n = 0; n < ntop; n++) {
usrtime -= save_history[i].utime; cur = top + n;
break;
} /*
* Calculate time in cur process. Time is sum of user time
* (usrtime) plus system time (systime).
*/
systime = cur->stime;
usrtime = cur->utime;
pid = cur->pid;
total_time = systime + usrtime;
New_save_hist[n].ticks = total_time;
New_save_hist[n].pid = pid;
New_save_hist[n].stime = systime;
New_save_hist[n].utime = usrtime;
/* find matching entry from previous pass */
for (i = 0; i < prev_count; i++) {
if (save_history[i].pid == pid) {
total_time -= save_history[i].ticks;
systime -= save_history[i].stime;
usrtime -= save_history[i].utime;
break;
}
}
/*
* Calculate percent cpu time for cur task.
*/
i = (total_time * 10 * 100/Hertz) / elapsed_time;
if (i > 999)
i = 999;
cur->pcpu = i;
} }
/* /*
* Calculate percent cpu time for cur task. * Save cur frame's information.
*/ */
i = (total_time * 10 * 100/Hertz) / elapsed_time; free(save_history);
if (i > 999) save_history = memcpy(xmalloc(sizeof(struct save_hist)*n), New_save_hist,
i = 999;
cur->pcpu = i;
}
/*
* Save cur frame's information.
*/
free(save_history);
save_history = memcpy(xmalloc(sizeof(struct save_hist)*n), New_save_hist,
sizeof(struct save_hist)*n); sizeof(struct save_hist)*n);
prev_count = n; prev_count = n;
qsort(top, n, sizeof(procps_status_t), (void*)mult_lvl_cmp); qsort(top, n, sizeof(procps_status_t), (void*)mult_lvl_cmp);
} }
#else #else
static cmp_t sort_function; static cmp_t sort_function;
@ -335,7 +335,7 @@ static unsigned long display_generic(void)
/* read load average */ /* read load average */
fp = bb_xfopen("loadavg", "r"); fp = bb_xfopen("loadavg", "r");
if (fscanf(fp, "%f %f %f", &avg1, &avg2, &avg3) != 3) { if (fscanf(fp, "%f %f %f", &avg1, &avg2, &avg3) != 3) {
bb_error_msg_and_die("failed to read '%s'", "loadavg"); bb_error_msg_and_die("failed to read 'loadavg'");
} }
fclose(fp); fclose(fp);
@ -352,11 +352,11 @@ static unsigned long display_generic(void)
/* output memory info and load average */ /* output memory info and load average */
/* clear screen & go to top */ /* clear screen & go to top */
printf("\e[H\e[J" "Mem: " printf("\e[H\e[J" "Mem: "
"%ldK used, %ldK free, %ldK shrd, %ldK buff, %ldK cached\n", "%ldK used, %ldK free, %ldK shrd, %ldK buff, %ldK cached\n",
used, mfree, shared, buffers, cached); used, mfree, shared, buffers, cached);
printf("Load average: %.2f, %.2f, %.2f " printf("Load average: %.2f, %.2f, %.2f "
"(State: S=sleeping R=running, W=waiting)\n", "(State: S=sleeping R=running, W=waiting)\n",
avg1, avg2, avg3); avg1, avg2, avg3);
return total; return total;
} }
@ -428,7 +428,7 @@ static void reset_term(void)
#endif /* CONFIG_FEATURE_CLEAN_UP */ #endif /* CONFIG_FEATURE_CLEAN_UP */
} }
static void sig_catcher (int sig ATTRIBUTE_UNUSED) static void sig_catcher(int sig ATTRIBUTE_UNUSED)
{ {
reset_term(); reset_term();
} }
@ -486,11 +486,11 @@ int top_main(int argc, char **argv)
get_terminal_width_height(0, &col, &lines); get_terminal_width_height(0, &col, &lines);
if (lines > 4) { if (lines > 4) {
lines -= 5; lines -= 5;
#ifdef CONFIG_FEATURE_TOP_CPU_USAGE_PERCENTAGE #ifdef CONFIG_FEATURE_TOP_CPU_USAGE_PERCENTAGE
col = col - 80 + 35 - 6; col = col - 80 + 35 - 6;
#else #else
col = col - 80 + 35; col = col - 80 + 35;
#endif #endif
} }
#endif /* CONFIG_FEATURE_USE_TERMIOS */ #endif /* CONFIG_FEATURE_USE_TERMIOS */
@ -514,8 +514,8 @@ int top_main(int argc, char **argv)
memcpy(top + n, p, sizeof(procps_status_t)); memcpy(top + n, p, sizeof(procps_status_t));
} }
if (ntop == 0) { if (ntop == 0) {
bb_perror_msg_and_die("scandir('/proc')"); bb_error_msg_and_die("Can't find process info in /proc");
} }
#ifdef CONFIG_FEATURE_TOP_CPU_USAGE_PERCENTAGE #ifdef CONFIG_FEATURE_TOP_CPU_USAGE_PERCENTAGE
if(!Hertz) { if(!Hertz) {
init_Hertz_value(); init_Hertz_value();
@ -523,7 +523,7 @@ int top_main(int argc, char **argv)
sleep(1); sleep(1);
clearmems(); clearmems();
continue; continue;
} }
do_stats(); do_stats();
#else #else
qsort(top, ntop, sizeof(procps_status_t), (void*)sort_function); qsort(top, ntop, sizeof(procps_status_t), (void*)sort_function);