Author: gherzan Date: Thu Nov 5 14:02:40 2009 New Revision: 1534 Log: pisacd: switch from select(2) to poll(2) Although not as fast as epoll, poll _is_ faster than select and it's still POSIX-compliant. Modified: trunk/pisacd/cdmain.c Modified: trunk/pisacd/cdmain.c ============================================================================== --- trunk/pisacd/cdmain.c Thu Nov 5 12:56:47 2009 (r1533) +++ trunk/pisacd/cdmain.c Thu Nov 5 14:02:40 2009 (r1534) @@ -13,6 +13,7 @@ #include <signal.h> #include <getopt.h> #include <sys/utsname.h> +#include <sys/poll.h> #include <unistd.h> #include "libpisa/tunnel.h" @@ -32,6 +33,14 @@ #include "cdtimeout.h" #include "performance/pisaperf.h" +/* fd indexing the the pollfd array */ +#define POLL_TUN 0 +#define POLL_CTL 1 +#define POLL_DATA 2 +#define POLL_CONF 3 +#define POLL_SCHED 4 +#define POLL_FD_COUNT 5 + extern pisa_conf_handle_func_set conf_handle_func_set; #define OPTS "f:i:p:q:bkdvh" @@ -87,33 +96,36 @@ */ static inline void cd_do_main(void) { - int maxfd; - fd_set readfds; + const int nfds = POLL_FD_COUNT; + int i; + struct pollfd pfds[nfds]; cd_ctx.flag_running = TRUE; pisa_servers_add_all(); - PISA_INFO("\nEntering main loop\n"); - - maxfd = pisa_maxof(5, cd_ctx.fd_tunnel, cd_ctx.fd_pisaconf, - cd_ctx.fd_control, cd_ctx.fd_data, cd_ctx.scheduler.pipe_main[0]) + 1; + /* initialize the poll descriptors + * sockets.tunnel needs to be always included in the reading fd list + */ + pfds[POLL_TUN].fd = cd_ctx.fd_tunnel; + pfds[POLL_CONF].fd = cd_ctx.fd_pisaconf; + pfds[POLL_CTL].fd = cd_ctx.fd_control; + pfds[POLL_DATA].fd = cd_ctx.fd_data; + pfds[POLL_SCHED].fd = cd_ctx.scheduler.pipe_main[0]; + + for (i = 0; i < nfds; i++) + pfds[i].events = POLLIN; + PISA_INFO("\nEntering main loop\n"); + while (cd_ctx.flag_running || cd_ctx.flag_pending) { - /* sockets.tunnel needs to be always included in the reading fd list */ - FD_ZERO(&readfds); - FD_SET(cd_ctx.fd_tunnel, &readfds); - FD_SET(cd_ctx.fd_control, &readfds); - FD_SET(cd_ctx.fd_data, &readfds); - FD_SET(cd_ctx.fd_pisaconf, &readfds); - FD_SET(cd_ctx.scheduler.pipe_main[0], &readfds); - if (select(maxfd, &readfds, NULL, NULL, NULL) > 0) { + if (poll(pfds, nfds, -1) > 0) { if (cd_ctx.flag_running) { - if (FD_ISSET(cd_ctx.fd_tunnel, &readfds)) + if (pfds[POLL_TUN].revents & POLLIN) pisa_cd_copy_from_tun_to_sock(); - if (FD_ISSET(cd_ctx.fd_data, &readfds)) + if (pfds[POLL_DATA].revents & POLLIN) pisa_cd_copy_from_sock_to_tun(); } else { /* TODO: Incoming packets after @@ -123,13 +135,13 @@ * above. */ } - if (FD_ISSET(cd_ctx.fd_control, &readfds)) + if (pfds[POLL_CTL].revents & POLLIN) pisa_ctrlhandler_dispatch(&cd_ctx.ctrlhandlers, cd_ctx.fd_control); - if (FD_ISSET(cd_ctx.fd_pisaconf, &readfds)) + if (pfds[POLL_CONF].revents & POLLIN) pisa_conf_handle_packet(cd_ctx.fd_pisaconf); - if (FD_ISSET(cd_ctx.scheduler.pipe_main[0], &readfds)) + if (pfds[POLL_SCHED].revents & POLLIN) pisa_handle_scheduler(); } }