*: replace select-for-one descriptor with poll, it's smaller.

$ ./.cmk bloatcheck
function                                             old     new   delta
readit                                               406     364     -42
syslogd_main                                        1249    1206     -43
traceroute_main                                     4115    4060     -55
mysleep                                              112      45     -67
arpping                                              579     441    -138
tftp                                                1575    1182    -393
------------------------------------------------------------------------------
(add/remove: 0/0 grow/shrink: 0/6 up/down: 0/-738)           Total: -738 bytes
   text    data     bss     dec     hex filename
 770580    1051   10764  782395   bf03b busybox_old
 769820    1051   10764  781635   bed43 busybox_unstripped
This commit is contained in:
Denis Vlasenko 2007-09-07 13:43:28 +00:00
parent 40f0bcf9d3
commit 87f3b26b3a
6 changed files with 61 additions and 82 deletions

View File

@ -237,7 +237,8 @@ static char *yank_delete(char *, char *, int, int); // yank text[] into register
static void show_help(void); // display some help info static void show_help(void); // display some help info
static void rawmode(void); // set "raw" mode on tty static void rawmode(void); // set "raw" mode on tty
static void cookmode(void); // return to "cooked" mode on tty static void cookmode(void); // return to "cooked" mode on tty
static int mysleep(int); // sleep for 'h' 1/100 seconds // sleep for 'h' 1/100 seconds, return 1/0 if stdin is (ready for read)/(not ready)
static int mysleep(int);
static char readit(void); // read (maybe cursor) key from stdin static char readit(void); // read (maybe cursor) key from stdin
static char get_one_char(void); // read 1 char from stdin static char get_one_char(void); // read 1 char from stdin
static int file_size(const char *); // what is the byte size of "fn" static int file_size(const char *); // what is the byte size of "fn"
@ -2134,17 +2135,11 @@ static void catch_sig(int sig)
static int mysleep(int hund) // sleep for 'h' 1/100 seconds static int mysleep(int hund) // sleep for 'h' 1/100 seconds
{ {
fd_set rfds; struct pollfd pfd[1];
struct timeval tv;
// Don't hang- Wait 5/100 seconds- 1 Sec= 1000000 pfd[0].fd = 0;
fflush(stdout); pfd[0].events = POLLIN;
FD_ZERO(&rfds); return poll(pfd, 1, hund*10) > 0;
FD_SET(0, &rfds);
tv.tv_sec = 0;
tv.tv_usec = hund * 10000;
select(1, &rfds, NULL, NULL, &tv);
return FD_ISSET(0, &rfds);
} }
#define readbuffer bb_common_bufsiz1 #define readbuffer bb_common_bufsiz1
@ -2217,25 +2212,20 @@ static char readit(void) // read (maybe cursor) key from stdin
if (n <= 0) if (n <= 0)
return 0; // error return 0; // error
if (readbuffer[0] == 27) { if (readbuffer[0] == 27) {
fd_set rfds;
struct timeval tv;
// This is an ESC char. Is this Esc sequence? // This is an ESC char. Is this Esc sequence?
// Could be bare Esc key. See if there are any // Could be bare Esc key. See if there are any
// more chars to read after the ESC. This would // more chars to read after the ESC. This would
// be a Function or Cursor Key sequence. // be a Function or Cursor Key sequence.
FD_ZERO(&rfds); struct pollfd pfd[1];
FD_SET(0, &rfds); pfd[0].fd = 0;
tv.tv_sec = 0; pfd[0].events = POLLIN;
tv.tv_usec = 50000; // Wait 5/100 seconds- 1 Sec=1000000 // Wait 50 ms
// keep reading while there are input chars and room in buffer // keep reading while there are input chars and room in buffer
while (select(1, &rfds, NULL, NULL, &tv) > 0 && n <= (MAX_LINELEN - 5)) { while (poll(pfd, 1, 50) > 0 && n <= (MAX_LINELEN - 5)) {
// read the rest of the ESC string // read the rest of the ESC string
int r = read(0, (void *) (readbuffer + n), MAX_LINELEN - n); int r = read(0, readbuffer + n, MAX_LINELEN - n);
if (r > 0) { if (r > 0)
n += r; n += r;
}
} }
} }
readed_for_parse = n; readed_for_parse = n;

View File

@ -23,10 +23,10 @@
#if ENABLE_FEATURE_TFTP_GET || ENABLE_FEATURE_TFTP_PUT #if ENABLE_FEATURE_TFTP_GET || ENABLE_FEATURE_TFTP_PUT
#define TFTP_BLOCKSIZE_DEFAULT 512 /* according to RFC 1350, don't change */ #define TFTP_BLOCKSIZE_DEFAULT 512 /* according to RFC 1350, don't change */
#define TFTP_TIMEOUT 50000 /* 50ms, in microseconds */ #define TFTP_TIMEOUT_MS 50
#define TFTP_MAXTIMEOUT 999000 /* about 1 second, in microseconds */ #define TFTP_MAXTIMEOUT_MS 2000
#define TFTP_NUM_RETRIES 12 /* number of backed-off retries */ #define TFTP_NUM_RETRIES 12 /* number of backed-off retries */
/* opcodes we support */ /* opcodes we support */
#define TFTP_RRQ 1 #define TFTP_RRQ 1
@ -114,9 +114,8 @@ static int tftp( USE_GETPUT(const int cmd,)
const char *remotefile, const int localfd, const char *remotefile, const int localfd,
unsigned port, int tftp_bufsize) unsigned port, int tftp_bufsize)
{ {
struct timeval tv; struct pollfd pfd[1];
fd_set rfds; #define socketfd (pfd[0].fd)
int socketfd;
int len; int len;
int send_len; int send_len;
USE_FEATURE_TFTP_BLOCKSIZE(smallint want_option_ack = 0;) USE_FEATURE_TFTP_BLOCKSIZE(smallint want_option_ack = 0;)
@ -124,7 +123,7 @@ static int tftp( USE_GETPUT(const int cmd,)
uint16_t opcode; uint16_t opcode;
uint16_t block_nr = 1; uint16_t block_nr = 1;
uint16_t recv_blk; uint16_t recv_blk;
int retries, waittime; int retries, waittime_ms;
char *cp; char *cp;
unsigned org_port; unsigned org_port;
@ -208,7 +207,7 @@ static int tftp( USE_GETPUT(const int cmd,)
* for potential resend */ * for potential resend */
retries = TFTP_NUM_RETRIES; /* re-initialize */ retries = TFTP_NUM_RETRIES; /* re-initialize */
waittime = TFTP_TIMEOUT; waittime_ms = TFTP_TIMEOUT_MS;
send_again: send_again:
#if ENABLE_DEBUG_TFTP #if ENABLE_DEBUG_TFTP
@ -224,11 +223,9 @@ static int tftp( USE_GETPUT(const int cmd,)
recv_again: recv_again:
/* Receive packet */ /* Receive packet */
tv.tv_sec = 0; /*pfd[0].fd = socketfd;*/
tv.tv_usec = waittime; pfd[0].events = POLLIN;
FD_ZERO(&rfds); switch (poll(pfd, 1, waittime_ms)) {
FD_SET(socketfd, &rfds);
switch (select(socketfd + 1, &rfds, NULL, NULL, &tv)) {
unsigned from_port; unsigned from_port;
case 1: case 1:
from->len = peer_lsa->len; from->len = peer_lsa->len;
@ -258,14 +255,14 @@ static int tftp( USE_GETPUT(const int cmd,)
} }
/* exponential backoff with limit */ /* exponential backoff with limit */
waittime += waittime/2; waittime_ms += waittime_ms/2;
if (waittime > TFTP_MAXTIMEOUT) { if (waittime_ms > TFTP_MAXTIMEOUT_MS) {
waittime = TFTP_MAXTIMEOUT; waittime_ms = TFTP_MAXTIMEOUT_MS;
} }
goto send_again; /* resend last sent pkt */ goto send_again; /* resend last sent pkt */
default: default:
bb_perror_msg("select"); bb_perror_msg("poll");
goto ret; goto ret;
} }
process_pkt: process_pkt:

View File

@ -346,10 +346,10 @@ static int optlen; /* length of ip options */
struct globals { struct globals {
/* last inbound (icmp) packet */
unsigned char packet[512];
struct sockaddr_storage whereto; /* Who to try to reach */ struct sockaddr_storage whereto; /* Who to try to reach */
struct sockaddr_storage wherefrom; /* Who we are */ struct sockaddr_storage wherefrom; /* Who we are */
/* last inbound (icmp) packet */
unsigned char packet[512];
#if ENABLE_FEATURE_TRACEROUTE_SOURCE_ROUTE #if ENABLE_FEATURE_TRACEROUTE_SOURCE_ROUTE
/* Maximum number of gateways (include room for one noop) */ /* Maximum number of gateways (include room for one noop) */
#define NGATEWAYS ((int)((MAX_IPOPTLEN - IPOPT_MINOFF - 1) / sizeof(uint32_t))) #define NGATEWAYS ((int)((MAX_IPOPTLEN - IPOPT_MINOFF - 1) / sizeof(uint32_t)))
@ -359,7 +359,7 @@ struct globals {
}; };
#define G (*ptr_to_globals) #define G (*ptr_to_globals)
#define INIT_G() PTR_TO_GLOBALS = xzalloc(sizeof(G))
#define packet (G.packet ) #define packet (G.packet )
#define whereto (G.whereto ) #define whereto (G.whereto )
#define wherefrom (G.wherefrom) #define wherefrom (G.wherefrom)
@ -537,21 +537,15 @@ findsaddr(const struct sockaddr_in *to, struct sockaddr_in *from)
static int static int
wait_for_reply(int sock, struct sockaddr_in *fromp) wait_for_reply(int sock, struct sockaddr_in *fromp)
{ {
fd_set fds; struct pollfd pfd[1];
struct timeval tvwait;
int cc = 0; int cc = 0;
socklen_t fromlen = sizeof(*fromp); socklen_t fromlen = sizeof(*fromp);
FD_ZERO(&fds); pfd[0].fd = sock;
FD_SET(sock, &fds); pfd[0].events = POLLIN;
if (poll(pfd, 1, waittime * 1000) > 0)
tvwait.tv_sec = waittime; cc = recvfrom(sock, packet, sizeof(packet), 0,
tvwait.tv_usec = 0;
if (select(sock + 1, &fds, NULL, NULL, &tvwait) > 0)
cc = recvfrom(sock, (char *)packet, sizeof(packet), 0,
(struct sockaddr *)fromp, &fromlen); (struct sockaddr *)fromp, &fromlen);
return cc; return cc;
} }
@ -930,7 +924,7 @@ int traceroute_main(int argc, char **argv)
llist_t *source_route_list = NULL; llist_t *source_route_list = NULL;
#endif #endif
PTR_TO_GLOBALS = xzalloc(sizeof(G)); INIT_G();
from = (struct sockaddr_in *)&wherefrom; from = (struct sockaddr_in *)&wherefrom;
to = (struct sockaddr_in *)&whereto; to = (struct sockaddr_in *)&whereto;

View File

@ -37,14 +37,12 @@ struct arpMsg {
int arpping(uint32_t test_ip, uint32_t from_ip, uint8_t *from_mac, const char *interface) int arpping(uint32_t test_ip, uint32_t from_ip, uint8_t *from_mac, const char *interface)
{ {
int timeout = 2; int timeout_ms = 2000;
int s; /* socket */ struct pollfd pfd[1];
#define s (pfd[0].fd) /* socket */
int rv = 1; /* "no reply received" yet */ int rv = 1; /* "no reply received" yet */
struct sockaddr addr; /* for interface name */ struct sockaddr addr; /* for interface name */
struct arpMsg arp; struct arpMsg arp;
fd_set fdset;
struct timeval tm;
unsigned prevTime;
s = socket(PF_PACKET, SOCK_PACKET, htons(ETH_P_ARP)); s = socket(PF_PACKET, SOCK_PACKET, htons(ETH_P_ARP));
if (s == -1) { if (s == -1) {
@ -80,18 +78,17 @@ int arpping(uint32_t test_ip, uint32_t from_ip, uint8_t *from_mac, const char *i
/* wait for arp reply, and check it */ /* wait for arp reply, and check it */
do { do {
int r; int r;
prevTime = monotonic_sec(); unsigned prevTime = monotonic_us();
FD_ZERO(&fdset);
FD_SET(s, &fdset); pfd[0].events = POLLIN;
tm.tv_sec = timeout; r = poll(pfd, 1, timeout_ms);
tm.tv_usec = 0;
r = select(s + 1, &fdset, NULL, NULL, &tm);
if (r < 0) { if (r < 0) {
bb_perror_msg("error on ARPING request"); if (errno != EINTR) {
if (errno != EINTR) bb_perror_msg("poll");
break; break;
}
} else if (r) { } else if (r) {
if (recv(s, &arp, sizeof(arp), 0) < 0) if (read(s, &arp, sizeof(arp)) < 0)
break; break;
if (arp.operation == htons(ARPOP_REPLY) if (arp.operation == htons(ARPOP_REPLY)
&& memcmp(arp.tHaddr, from_mac, 6) == 0 && memcmp(arp.tHaddr, from_mac, 6) == 0
@ -101,8 +98,8 @@ int arpping(uint32_t test_ip, uint32_t from_ip, uint8_t *from_mac, const char *i
break; break;
} }
} }
timeout -= monotonic_sec() - prevTime; timeout_ms -= (monotonic_us() - prevTime) / 1000;
} while (timeout > 0); } while (timeout_ms > 0);
ret: ret:
close(s); close(s);

View File

@ -11589,6 +11589,7 @@ readcmd(int argc, char **argv)
#endif #endif
#if ENABLE_ASH_READ_TIMEOUT #if ENABLE_ASH_READ_TIMEOUT
if (ts.tv_sec || ts.tv_usec) { if (ts.tv_sec || ts.tv_usec) {
// TODO: replace with poll, it is smaller
FD_ZERO(&set); FD_ZERO(&set);
FD_SET(0, &set); FD_SET(0, &set);

View File

@ -471,8 +471,8 @@ static void do_syslogd(void) ATTRIBUTE_NORETURN;
static void do_syslogd(void) static void do_syslogd(void)
{ {
struct sockaddr_un sunx; struct sockaddr_un sunx;
int sock_fd; struct pollfd pfd[1];
fd_set fds; #define sock_fd (pfd[0].fd)
char *dev_log_name; char *dev_log_name;
/* Set up signal handlers */ /* Set up signal handlers */
@ -526,20 +526,20 @@ static void do_syslogd(void)
(char*)"syslogd started: BusyBox v" BB_VER, 0); (char*)"syslogd started: BusyBox v" BB_VER, 0);
for (;;) { for (;;) {
FD_ZERO(&fds); /*pfd[0].fd = sock_fd;*/
FD_SET(sock_fd, &fds); pfd[0].events = POLLIN;
pfd[0].revents = 0;
if (select(sock_fd + 1, &fds, NULL, NULL, NULL) < 0) { if (poll(pfd, 1, -1) < 0) { /* -1: no timeout */
if (errno == EINTR) { if (errno == EINTR) {
/* alarm may have happened */ /* alarm may have happened */
continue; continue;
} }
bb_perror_msg_and_die("select"); bb_perror_msg_and_die("poll");
} }
if (FD_ISSET(sock_fd, &fds)) { if (pfd[0].revents) {
int i; int i;
i = recv(sock_fd, G.recvbuf, MAX_READ - 1, 0); i = read(sock_fd, G.recvbuf, MAX_READ - 1);
if (i <= 0) if (i <= 0)
bb_perror_msg_and_die("UNIX socket error"); bb_perror_msg_and_die("UNIX socket error");
/* TODO: maybe suppress duplicates? */ /* TODO: maybe suppress duplicates? */
@ -559,7 +559,7 @@ static void do_syslogd(void)
#endif #endif
G.recvbuf[i] = '\0'; G.recvbuf[i] = '\0';
split_escape_and_log(G.recvbuf, i); split_escape_and_log(G.recvbuf, i);
} /* FD_ISSET() */ }
} /* for */ } /* for */
} }