top: use poll instead of select for waiting on one descriptor

smart_ulltoa5: make it more cryptic. -50 bytes.

function                                             old     new   delta
passwd_main                                         1095    1103      +8
getNum                                               557     565      +8
buffer_fill_and_print                                 73      76      +3
udhcpc_main                                         2393    2395      +2
mkfs_minix_main                                     3071    3070      -1
dname_enc                                            377     373      -4
expmeta                                              480     472      -8
smart_ulltoa5                                        334     283     -51
top_main                                             911     815     -96
------------------------------------------------------------------------------
(add/remove: 0/0 grow/shrink: 4/5 up/down: 21/-160)          Total: -139 bytes
   text    data     bss     dec     hex filename
 770872    1063   10788  782723   bf183 busybox_old
 770732    1063   10788  782583   bf0f7 busybox_unstripped
This commit is contained in:
Denis Vlasenko 2007-08-28 19:35:34 +00:00
parent 7d8de4d55d
commit b308d81e92
3 changed files with 48 additions and 38 deletions

View File

@ -27,6 +27,7 @@
#include <stddef.h> #include <stddef.h>
#include <string.h> #include <string.h>
/* #include <strings.h> - said to be obsolete */ /* #include <strings.h> - said to be obsolete */
#include <sys/poll.h>
#include <sys/ioctl.h> #include <sys/ioctl.h>
#include <sys/mman.h> #include <sys/mman.h>
#include <sys/socket.h> #include <sys/socket.h>

View File

@ -284,35 +284,44 @@ void smart_ulltoa5(unsigned long long ul, char buf[5])
{ {
const char *fmt; const char *fmt;
char c; char c;
unsigned v,idx = 0; unsigned v, u, idx = 0;
ul *= 10;
if (ul > 9999*10) { // do not scale if 9999 or less if (ul > 9999) { // do not scale if 9999 or less
while (ul >= 10000) { ul *= 10;
do {
ul /= 1024; ul /= 1024;
idx++; idx++;
} } while (ul >= 10000);
} }
v = ul; // ullong divisions are expensive, avoid them v = ul; // ullong divisions are expensive, avoid them
fmt = " 123456789"; fmt = " 123456789";
if (!idx) { // 9999 or less: use 1234 format u = v / 10;
c = buf[0] = " 123456789"[v/10000]; v = v % 10;
if (!idx) {
// 9999 or less: use "1234" format
// u is value/10, v is last digit
c = buf[0] = " 123456789"[u/100];
if (c != ' ') fmt = "0123456789"; if (c != ' ') fmt = "0123456789";
c = buf[1] = fmt[v/1000%10]; c = buf[1] = fmt[u/10%10];
if (c != ' ') fmt = "0123456789"; if (c != ' ') fmt = "0123456789";
buf[2] = fmt[v/100%10]; buf[2] = fmt[u%10];
buf[3] = "0123456789"[v/10%10]; buf[3] = "0123456789"[v];
} else { } else {
if (v >= 10*10) { // scaled value is >=10: use 123M format // u is value, v is 1/10ths (allows for 9.2M format)
c = buf[0] = " 123456789"[v/1000]; if (u >= 10) {
// value is >= 10: use "123M', " 12M" formats
c = buf[0] = " 123456789"[u/100];
if (c != ' ') fmt = "0123456789"; if (c != ' ') fmt = "0123456789";
buf[1] = fmt[v/100%10]; v = u % 10;
buf[2] = "0123456789"[v/10%10]; u = u / 10;
} else { // scaled value is <10: use 1.2M format buf[1] = fmt[u%10];
buf[0] = "0123456789"[v/10]; } else {
// value is < 10: use "9.2M" format
buf[0] = "0123456789"[u];
buf[1] = '.'; buf[1] = '.';
buf[2] = "0123456789"[v%10];
} }
buf[2] = "0123456789"[v];
// see http://en.wikipedia.org/wiki/Tera // see http://en.wikipedia.org/wiki/Tera
buf[3] = " kMGTPEZY"[idx]; buf[3] = " kMGTPEZY"[idx];
} }
@ -470,7 +479,8 @@ char *xasprintf(const char *format, ...)
va_end(p); va_end(p);
#endif #endif
if (r < 0) bb_error_msg_and_die(bb_msg_memory_exhausted); if (r < 0)
bb_error_msg_and_die(bb_msg_memory_exhausted);
return string_ptr; return string_ptr;
} }

View File

@ -448,7 +448,7 @@ static void display_status(int count, int scr_width)
#endif #endif
if (s->vsz >= 100*1024) if (s->vsz >= 100*1024)
sprintf(vsz_str_buf, "%6ldM", s->vsz/1024); sprintf(vsz_str_buf, "%6ldm", s->vsz/1024);
else else
sprintf(vsz_str_buf, "%7ld", s->vsz); sprintf(vsz_str_buf, "%7ld", s->vsz);
// PID PPID USER STAT VSZ %MEM [%CPU] COMMAND // PID PPID USER STAT VSZ %MEM [%CPU] COMMAND
@ -519,23 +519,29 @@ int top_main(int argc, char **argv);
int top_main(int argc, char **argv) int top_main(int argc, char **argv)
{ {
int count, lines, col; int count, lines, col;
unsigned interval = 5; /* default update rate is 5 seconds */ unsigned interval;
unsigned iterations = UINT_MAX; /* 2^32 iterations by default :) */ int iterations = -1; /* infinite */
char *sinterval, *siterations; char *sinterval, *siterations;
#if ENABLE_FEATURE_USE_TERMIOS #if ENABLE_FEATURE_USE_TERMIOS
struct termios new_settings; struct termios new_settings;
struct timeval tv; struct pollfd pfd[1];
fd_set readfds;
unsigned char c; unsigned char c;
pfd[0].fd = 0;
pfd[0].events = POLLIN;
#endif /* FEATURE_USE_TERMIOS */ #endif /* FEATURE_USE_TERMIOS */
interval = 5; interval = 5; /* default update rate is 5 seconds */
/* do normal option parsing */ /* do normal option parsing */
opt_complementary = "-"; opt_complementary = "-";
getopt32(argv, "d:n:b", &sinterval, &siterations); getopt32(argv, "d:n:b", &sinterval, &siterations);
if (option_mask32 & 0x1) interval = xatou(sinterval); // -d if (option_mask32 & 0x1) {
if (option_mask32 & 0x2) iterations = xatou(siterations); // -n /* Need to limit it to not overflow poll timeout */
interval = xatou16(sinterval); // -d
}
if (option_mask32 & 0x2)
iterations = xatoi_u(siterations); // -n
//if (option_mask32 & 0x4) // -b //if (option_mask32 & 0x4) // -b
/* change to /proc */ /* change to /proc */
@ -584,9 +590,8 @@ int top_main(int argc, char **argv)
| PSSCAN_UTIME | PSSCAN_UTIME
| PSSCAN_STATE | PSSCAN_STATE
| PSSCAN_COMM | PSSCAN_COMM
| PSSCAN_SID
| PSSCAN_UIDGID | PSSCAN_UIDGID
))) { )) != NULL) {
int n = ntop; int n = ntop;
top = xrealloc(top, (++ntop) * sizeof(*top)); top = xrealloc(top, (++ntop) * sizeof(*top));
top[n].pid = p->pid; top[n].pid = p->pid;
@ -622,15 +627,9 @@ int top_main(int argc, char **argv)
/* show status for each of the processes */ /* show status for each of the processes */
display_status(count, col); display_status(count, col);
#if ENABLE_FEATURE_USE_TERMIOS #if ENABLE_FEATURE_USE_TERMIOS
tv.tv_sec = interval; if (poll(pfd, 1, interval * 1000) != 0) {
tv.tv_usec = 0; if (read(0, &c, 1) != 1) /* signal */
FD_ZERO(&readfds); break;
FD_SET(0, &readfds);
select(1, &readfds, NULL, NULL, &tv);
if (FD_ISSET(0, &readfds)) {
if (read(0, &c, 1) <= 0) { /* signal */
return EXIT_FAILURE;
}
if (c == 'q' || c == initial_settings.c_cc[VINTR]) if (c == 'q' || c == initial_settings.c_cc[VINTR])
break; break;
if (c == 'M') { if (c == 'M') {
@ -662,7 +661,7 @@ int top_main(int argc, char **argv)
#endif #endif
} }
} }
if (!--iterations) if (iterations >= 0 && !--iterations)
break; break;
#else #else
sleep(interval); sleep(interval);