Author: gherzan Date: Thu Nov 5 15:04:34 2009 New Revision: 1539 Log: pisasd: switch from select(2) to poll(2) Again, poll is faster than select but still POSIX-compliant. Modified: trunk/pisasd/sdmain.c Modified: trunk/pisasd/sdmain.c ============================================================================== --- trunk/pisasd/sdmain.c Thu Nov 5 14:54:31 2009 (r1538) +++ trunk/pisasd/sdmain.c Thu Nov 5 15:04:34 2009 (r1539) @@ -18,6 +18,7 @@ #include <sys/types.h> #include <sys/stat.h> +#include <sys/poll.h> #include <fcntl.h> #include <errno.h> @@ -40,6 +41,15 @@ #include "libpisa/log.h" #include "performance/pisaperf.h" +/* fd indexed in the pollfd array */ +#define POLL_CTL 0 +#define POLL_DATA 1 +#define POLL_TUN 2 +#define POLL_CONF 3 +#define POLL_SCHED 4 +#define POLL_FD_COUNT 5 + + #define OPTS "f:i:p:q:a:bkdvh" /** @@ -97,39 +107,39 @@ */ static inline void sd_do_main(void) { - int maxfd; + struct pollfd pfds[POLL_FD_COUNT]; + const int nfds = POLL_FD_COUNT; + int i; + + /* initialize the polling structures */ + pfds[POLL_CTL].fd = sd_ctx.fd_control; + pfds[POLL_DATA].fd = sd_ctx.fd_data; + pfds[POLL_TUN].fd = sd_ctx.fd_tunnel; + pfds[POLL_CONF].fd = sd_ctx.fd_pisaconf; + pfds[POLL_SCHED].fd = sd_ctx.scheduler.pipe_main[0]; - maxfd = 1 + pisa_maxof(5, sd_ctx.fd_control, sd_ctx.fd_data, sd_ctx.fd_tunnel, - sd_ctx.fd_tunnel, sd_ctx.scheduler.pipe_main[0]); + for (i = 0; i < nfds; i++) + pfds[i].events = POLLIN; sd_ctx.flag_running = TRUE; PISA_INFO("\nEntering main loop\n"); while (sd_ctx.flag_running) { - fd_set readfds; - - /* Add all sockets to the read set */ - FD_ZERO(&readfds); - FD_SET(sd_ctx.fd_control, &readfds); - FD_SET(sd_ctx.fd_data, &readfds); - FD_SET(sd_ctx.fd_tunnel, &readfds); - FD_SET(sd_ctx.fd_pisaconf, &readfds); - FD_SET(sd_ctx.scheduler.pipe_main[0], &readfds); - if (select(maxfd, &readfds, NULL, NULL, NULL) > 0) { - if (FD_ISSET(sd_ctx.fd_control, &readfds)) + if (poll(pfds, nfds, -1) > 0) { + if (pfds[POLL_CTL].revents & POLLIN) pisa_ctrlhandler_dispatch(&sd_ctx.ctrlhandlers, sd_ctx.fd_control); - if (FD_ISSET(sd_ctx.fd_data, &readfds)) + if (pfds[POLL_DATA].revents & POLLIN) pisa_sd_copy_from_sock_to_tun(); - if (FD_ISSET(sd_ctx.fd_tunnel, &readfds)) + if (pfds[POLL_TUN].revents & POLLIN) pisa_sd_copy_from_tun_to_sock(); - if (FD_ISSET(sd_ctx.fd_pisaconf, &readfds)) + if (pfds[POLL_CONF].revents & POLLIN) pisa_conf_handle_packet(sd_ctx.fd_pisaconf); - if (FD_ISSET(sd_ctx.scheduler.pipe_main[0], &readfds)) + if (pfds[POLL_SCHED].revents & POLLIN) pisa_handle_scheduler(); } }