Switch to poll in xenconsoled's io loop.
PATCH 1/1 Switch to poll in xenconsoled's io loop.
The original implementation utilies select(). In Linux select() typically supports up to 1024 file descriptors. This can be a problem when user tries to boot up many guests. Switching to poll() has minimum impact on existing code and has better scalibility.
Up to 8192 file descriptors are supported in the current implementation.
--- tools/console/daemon/io.c | 90 +++++++++++++++++++++++++-------------------- 1 file changed, 50 insertions(+), 40 deletions(-)
tools/console/daemon/io.c +50 -40
@@ -28,7 +28,7 @@#include <stdlib.h> #include <errno.h> #include <string.h> -#include <sys/select.h> +#include <poll.h> #include <fcntl.h> #include <unistd.h> #include <termios.h>
@@ -930,7 +930,6 @@ static void handle_log_reload(void)void handle_io (void ) { -fd_set readfds, writefds;int ret;if (log_hv) {
@@ -959,21 +958,33 @@ void handle_io(void)for (;;) {struct domain *d, *n; -int max_fd =-1 ; -struct timeval timeout; +int poll_timeout;/* timeout in milliseconds */ struct timespec ts;long long now, next_timeout =0 ; -FD_ZERO (&readfds); -FD_ZERO (&writefds); - -FD_SET (xs_fileno (xs), &readfds); - max_fd =MAX (xs_fileno (xs), max_fd); - -if (log_hv) { -FD_SET (xc_evtchn_fd (xce_handle), &readfds); - max_fd =MAX (xc_evtchn_fd (xce_handle), max_fd); - } +#define MAX_POLL_FDS 8192 +static struct pollfd fds[MAX_POLL_FDS ]; +static struct pollfd *fd_to_pollfd[MAX_POLL_FDS ]; +int nr_fds; +#define SET_FDS (_fd, _events) do { \ + if (_fd >= MAX_POLL_FDS) \ + break; \ + fds[nr_fds].fd = (_fd); \ + fds[nr_fds].events = (_events); \ + fd_to_pollfd[(_fd)] = &fds[nr_fds]; \ + nr_fds++; \ + } while (0) +#define FD_REVENTS (_fd) (((_fd) < MAX_POLL_FDS && fd_to_pollfd[(_fd)]) ? \ + fd_to_pollfd[(_fd)]->revents : 0) + + nr_fds =0 ; +memset (fds,0 ,sizeof (fds)); +memset (fd_to_pollfd,0 ,sizeof (fd_to_pollfd)); + +SET_FDS (xs_fileno (xs),POLLIN ); + +if (log_hv) +SET_FDS (xc_evtchn_fd (xce_handle),POLLIN );if (clock_gettime (CLOCK_MONOTONIC , &ts) <0 )return ;
@@ -982,11 +993,7 @@ void handle_io(void)/* Re-calculate any event counter allowances & unblock domains with new allowance */ for (d = dom_head; d; d = d->next) { -/* Add 5ms of fuzz since select() often returns -a couple of ms sooner than requested. Without -the fuzz we typically do an extra spin in select() -with a 1/2 ms timeout every other iteration */ -if ((now+5 ) > d->next_period) { +if (now > d->next_period) { d->next_period = now +RATE_LIMIT_PERIOD ;if (d->event_count >=RATE_LIMIT_ALLOWANCE ) { (void )xc_evtchn_unmask (d->xce_handle, d->local_port);
@@ -1006,74 +1013,73 @@ void handle_io(void) !d->buffer.max_capacity || d->buffer.size < d->buffer.max_capacity) {int evtchn_fd =xc_evtchn_fd (d->xce_handle); -FD_SET (evtchn_fd, &readfds); - max_fd =MAX (evtchn_fd, max_fd); +SET_FDS (evtchn_fd,POLLIN ); } }if (d->master_fd !=-1 ) { +short events =0 ;if (!d->is_dead &&ring_free_bytes (d)) -FD_SET (d->master_fd, &readfds); + events |=POLLIN ;if (!buffer_empty (&d->buffer)) -FD_SET (d->master_fd, &writefds); - max_fd =MAX (d->master_fd, max_fd); + events |=POLLOUT ; + +if (events) +SET_FDS (d->master_fd, events); } }/* If any domain has been rate limited, we need to work -out what timeout to supply to select */ +out what timeout to supply to poll */ if (next_timeout) {long long duration = (next_timeout - now);if (duration <=0 )/* sanity check */ duration =1 ; - timeout.tv_sec = duration /1000 ; - timeout.tv_usec = ((duration - (timeout.tv_sec *1000 )) - *1000 ); + poll_timeout = (int )duration; } - ret =select (max_fd +1 , &readfds, &writefds,0 , - next_timeout ? &timeout :NULL ); + ret =poll (fds, nr_fds, next_timeout ? poll_timeout :-1 );if (log_reload) {handle_log_reload (); log_reload =0 ; } -/* Abort if select failed, except for EINTR cases +/* Abort if poll failed, except for EINTR cases which indicate a possible log reload */ if (ret ==-1 ) {if (errno ==EINTR )continue ; -dolog (LOG_ERR ,"Failure in select: %d (%s)" , +dolog (LOG_ERR ,"Failure in poll: %d (%s)" , errno,strerror (errno));break ; } -if (log_hv &&FD_ISSET (xc_evtchn_fd (xce_handle), &readfds)) +if (log_hv &&FD_REVENTS (xc_evtchn_fd (xce_handle)) &POLLIN )handle_hv_logs ();if (ret <=0 )continue ; -if (FD_ISSET (xs_fileno (xs), &readfds)) +if (FD_REVENTS (xs_fileno (xs)) &POLLIN )handle_xs ();for (d = dom_head; d; d = n) { n = d->next;if (d->event_count <RATE_LIMIT_ALLOWANCE ) {if (d->xce_handle !=NULL && -FD_ISSET (xc_evtchn_fd (d->xce_handle), - &readfds)) +FD_REVENTS (xc_evtchn_fd (d->xce_handle)) & +POLLIN )handle_ring_read (d); } -if (d->master_fd !=-1 &&FD_ISSET (d->master_fd, - &readfds)) +if (d->master_fd !=-1 && +FD_REVENTS (d->master_fd) &POLLIN )handle_tty_read (d); -if (d->master_fd !=-1 &&FD_ISSET (d->master_fd, - &writefds)) +if (d->master_fd !=-1 && +FD_REVENTS (d->master_fd) &POLLOUT )handle_tty_write (d);if (d->last_seen != enum_pass)
@@ -1082,6 +1088,10 @@ void handle_io(void)if (d->is_dead)cleanup_domain (d); } + +#undef MAX_POLL_FDS +#undef SET_FDS +#undef FD_REVENTS } out:
Signature · 1 line
1.7.10.4
Quoted 67 lines · Wei Liu
The original implementation utilies
select(). In Linuxselect()typically supports up to 1024 file descriptors. This can be a problem when user tries to boot up many guests. Switching topoll()has minimum impact on existing code and has better scalibility.Up to 8192 file descriptors are supported in the current implementation.
Signed-off-by Wei Liu <[email protected]>--- tools/console/daemon/io.c | 90 +++++++++++++++++++++++++-------------------- 1 file changed, 50 insertions(+), 40 deletions(-)tools/console/daemon/io.c +16
-14@@ -28,7 +28,7 @@#include <stdlib.h> #include <errno.h> #include <string.h> -#include <sys/select.h> +#include <poll.h> #include <fcntl.h> #include <unistd.h> #include <termios.h> @@ -930,7 +930,6 @@ static void handle_log_reload(void)void handle_io (void ) { -fd_set readfds, writefds;int ret;if (log_hv) {@@ -959,21 +958,33 @@ void handle_io(void)for (;;) {struct domain *d, *n; -int max_fd =-1 ; -struct timeval timeout; +int poll_timeout;/* timeout in milliseconds */ struct timespec ts;long long now, next_timeout =0 ; -FD_ZERO (&readfds); -FD_ZERO (&writefds); - -FD_SET (xs_fileno (xs), &readfds); - max_fd =MAX (xs_fileno (xs), max_fd); - -if (log_hv) { -FD_SET (xc_evtchn_fd (xce_handle), &readfds); - max_fd =MAX (xc_evtchn_fd (xce_handle), max_fd); - } +#define MAX_POLL_FDS 8192 +static struct pollfd fds[MAX_POLL_FDS ]; +static struct pollfd *fd_to_pollfd[MAX_POLL_FDS ]; +int nr_fds; +#define SET_FDS (_fd, _events) do { \ + if (_fd >= MAX_POLL_FDS) \ + break; \ + fds[nr_fds].fd = (_fd); \ + fds[nr_fds].events = (_events); \ + fd_to_pollfd[(_fd)] = &fds[nr_fds]; \ + nr_fds++; \ + } while (0) +#define FD_REVENTS (_fd) (((_fd) < MAX_POLL_FDS && fd_to_pollfd[(_fd)]) ? \ + fd_to_pollfd[(_fd)]->revents : 0)
tools/console/daemon/io.c void handle_io(void) · line 988
Quoted 10 lines
tools/console/daemon/io.c +9
-0+ + nr_fds =0 ; +memset (fds,0 ,sizeof (fds)); +memset (fd_to_pollfd,0 ,sizeof (fd_to_pollfd)); + +SET_FDS (xs_fileno (xs),POLLIN ); + +if (log_hv) +SET_FDS (xc_evtchn_fd (xce_handle),POLLIN );
Would it not make sense to use dynamically allocated memory instead - that way, when we run out of 8192, there is nothing to change.
At the very least, can we have some sort of error output when it goes above the limit, so that we don't just silently "miss" a few consoles out from the poll.
--
Mats
Quoted 121 lines
tools/console/daemon/io.c +25
-26if (clock_gettime (CLOCK_MONOTONIC , &ts) <0 )return ;@@ -982,11 +993,7 @@ void handle_io(void)/* Re-calculate any event counter allowances & unblock domains with new allowance */ for (d = dom_head; d; d = d->next) { -/* Add 5ms of fuzz since select() often returns -a couple of ms sooner than requested. Without -the fuzz we typically do an extra spin in select() -with a 1/2 ms timeout every other iteration */ -if ((now+5 ) > d->next_period) { +if (now > d->next_period) { d->next_period = now +RATE_LIMIT_PERIOD ;if (d->event_count >=RATE_LIMIT_ALLOWANCE ) { (void )xc_evtchn_unmask (d->xce_handle, d->local_port);@@ -1006,74 +1013,73 @@ void handle_io(void) !d->buffer.max_capacity || d->buffer.size < d->buffer.max_capacity) {int evtchn_fd =xc_evtchn_fd (d->xce_handle); -FD_SET (evtchn_fd, &readfds); - max_fd =MAX (evtchn_fd, max_fd); +SET_FDS (evtchn_fd,POLLIN ); } }if (d->master_fd !=-1 ) { +short events =0 ;if (!d->is_dead &&ring_free_bytes (d)) -FD_SET (d->master_fd, &readfds); + events |=POLLIN ;if (!buffer_empty (&d->buffer)) -FD_SET (d->master_fd, &writefds); - max_fd =MAX (d->master_fd, max_fd); + events |=POLLOUT ; + +if (events) +SET_FDS (d->master_fd, events); } }/* If any domain has been rate limited, we need to work -out what timeout to supply to select */ +out what timeout to supply to poll */ if (next_timeout) {long long duration = (next_timeout - now);if (duration <=0 )/* sanity check */ duration =1 ; - timeout.tv_sec = duration /1000 ; - timeout.tv_usec = ((duration - (timeout.tv_sec *1000 )) - *1000 ); + poll_timeout = (int )duration; } - ret =select (max_fd +1 , &readfds, &writefds,0 , - next_timeout ? &timeout :NULL ); + ret =poll (fds, nr_fds, next_timeout ? poll_timeout :-1 );if (log_reload) {handle_log_reload (); log_reload =0 ; } -/* Abort if select failed, except for EINTR cases +/* Abort if poll failed, except for EINTR cases which indicate a possible log reload */ if (ret ==-1 ) {if (errno ==EINTR )continue ; -dolog (LOG_ERR ,"Failure in select: %d (%s)" , +dolog (LOG_ERR ,"Failure in poll: %d (%s)" , errno,strerror (errno));break ; } -if (log_hv &&FD_ISSET (xc_evtchn_fd (xce_handle), &readfds)) +if (log_hv &&FD_REVENTS (xc_evtchn_fd (xce_handle)) &POLLIN )handle_hv_logs ();if (ret <=0 )continue ; -if (FD_ISSET (xs_fileno (xs), &readfds)) +if (FD_REVENTS (xs_fileno (xs)) &POLLIN )handle_xs ();for (d = dom_head; d; d = n) { n = d->next;if (d->event_count <RATE_LIMIT_ALLOWANCE ) {if (d->xce_handle !=NULL && -FD_ISSET (xc_evtchn_fd (d->xce_handle), - &readfds)) +FD_REVENTS (xc_evtchn_fd (d->xce_handle)) & +POLLIN )handle_ring_read (d); } -if (d->master_fd !=-1 &&FD_ISSET (d->master_fd, - &readfds)) +if (d->master_fd !=-1 && +FD_REVENTS (d->master_fd) &POLLIN )handle_tty_read (d); -if (d->master_fd !=-1 &&FD_ISSET (d->master_fd, - &writefds)) +if (d->master_fd !=-1 && +FD_REVENTS (d->master_fd) &POLLOUT )handle_tty_write (d);if (d->last_seen != enum_pass)@@ -1082,6 +1088,10 @@ void handle_io(void)if (d->is_dead)cleanup_domain (d); } + +#undef MAX_POLL_FDS +#undef SET_FDS +#undef FD_REVENTS } out:
Quoted 72 lines · Mats Petersson
Quoted 71 lines · Wei Liu
The original implementation utilies
select(). In Linuxselect()typically supports up to 1024 file descriptors. This can be a problem when user tries to boot up many guests. Switching topoll()has minimum impact on existing code and has better scalibility.Up to 8192 file descriptors are supported in the current implementation.
Signed-off-by Wei Liu <[email protected]>--- tools/console/daemon/io.c | 90 +++++++++++++++++++++++++-------------------- 1 file changed, 50 insertions(+), 40 deletions(-)tools/console/daemon/io.c +20
-14@@ -28,7 +28,7 @@#include <stdlib.h> #include <errno.h> #include <string.h> -#include <sys/select.h> +#include <poll.h> #include <fcntl.h> #include <unistd.h> #include <termios.h> @@ -930,7 +930,6 @@ static void handle_log_reload(void)void handle_io (void ) { -fd_set readfds, writefds;int ret;if (log_hv) {@@ -959,21 +958,33 @@ void handle_io(void)for (;;) {struct domain *d, *n; -int max_fd =-1 ; -struct timeval timeout; +int poll_timeout;/* timeout in milliseconds */ struct timespec ts;long long now, next_timeout =0 ; -FD_ZERO (&readfds); -FD_ZERO (&writefds); - -FD_SET (xs_fileno (xs), &readfds); - max_fd =MAX (xs_fileno (xs), max_fd); - -if (log_hv) { -FD_SET (xc_evtchn_fd (xce_handle), &readfds); - max_fd =MAX (xc_evtchn_fd (xce_handle), max_fd); - } +#define MAX_POLL_FDS 8192 +static struct pollfd fds[MAX_POLL_FDS ]; +static struct pollfd *fd_to_pollfd[MAX_POLL_FDS ]; +int nr_fds; +#define SET_FDS (_fd, _events) do { \ + if (_fd >= MAX_POLL_FDS) \ + break; \ + fds[nr_fds].fd = (_fd); \ + fds[nr_fds].events = (_events); \ + fd_to_pollfd[(_fd)] = &fds[nr_fds]; \ + nr_fds++; \ + } while (0) +#define FD_REVENTS (_fd) (((_fd) < MAX_POLL_FDS && fd_to_pollfd[(_fd)]) ? \ + fd_to_pollfd[(_fd)]->revents : 0) + + nr_fds =0 ; +memset (fds,0 ,sizeof (fds)); +memset (fd_to_pollfd,0 ,sizeof (fd_to_pollfd));
Quoted 8 lines
tools/console/daemon/io.c void handle_io(void) · line 988
Quoted 6 lines
tools/console/daemon/io.c +5
-0+ +SET_FDS (xs_fileno (xs),POLLIN ); + +if (log_hv) +SET_FDS (xc_evtchn_fd (xce_handle),POLLIN );Would it not make sense to use dynamically allocated memory instead - that way, when we run out of 8192, there is nothing to change.
Writing a new version to use dynamically allocated memory.
Wei.
PATCH v2 1/1 Switch from select() to poll() in xenconsoled's IO loop.
In reply to Wei LiuIn Linux select() typically supports up to 1024 file descriptors. This can be a problem when user tries to boot up many guests. Switching to poll() has minimum impact on existing code and has better scalibility.
Tracking arrays are dynamically allocated / reallocated. If the tracking arrays fail to expand, we just ignore the incoming fd.
--- tools/console/daemon/io.c | 159 ++++++++++++++++++++++++++++++++++----------- 1 file changed, 121 insertions(+), 38 deletions(-)
tools/console/daemon/io.c +121 -38
@@ -28,7 +28,7 @@#include <stdlib.h> #include <errno.h> #include <string.h> -#include <sys/select.h> +#include <poll.h> #include <fcntl.h> #include <unistd.h> #include <termios.h>
@@ -928,9 +928,98 @@ static void handle_log_reload(void) } } + +/* Should have at least max_fd + 1 elements */ +#define DEFAULT_ARRAY_SIZE 1024 +#define GROWTH_LENGTH 512 +static struct pollfd *fds; +static struct pollfd **fd_to_pollfd; +static unsigned int current_array_size; +static unsigned int nr_fds; + +static int initialize_pollfd_arrays (void ) +{ + fds = (struct pollfd *) +malloc (sizeof (struct pollfd ) *DEFAULT_ARRAY_SIZE ); +if (!fds) + goto fail; + fd_to_pollfd = (struct pollfd **) +malloc (sizeof (struct pollfd *) *DEFAULT_ARRAY_SIZE ); +if (!fd_to_pollfd) + goto fail; +memset (fds,0 ,sizeof (struct pollfd ) *DEFAULT_ARRAY_SIZE ); +memset (fd_to_pollfd,0 ,sizeof (struct pollfd *) *DEFAULT_ARRAY_SIZE ); + current_array_size =DEFAULT_ARRAY_SIZE ; +return 0 ; +fail: +free (fds); +free (fd_to_pollfd); +return -ENOMEM ; +} + +static void destroy_pollfd_arrays (void ) +{ +free (fds); +free (fd_to_pollfd); + current_array_size =0 ; +} + +static void set_fds (int fd,short events) +{ +if (current_array_size < fd+1 ) { +struct pollfd *p1 =NULL ; +struct pollfd **p2 =NULL ; +unsigned int newsize = current_array_size; + +do { newsize +=GROWTH_LENGTH ; }while (newsize < fd+1 ); + + p1 =realloc (fds,sizeof (struct pollfd )*newsize); +if (!p1) + goto fail; + fds = p1; + + p2 =realloc (fd_to_pollfd,sizeof (struct pollfd *)*newsize); +if (!p2) + goto fail; + fd_to_pollfd = p2; + +memset (&fds[0 ] + current_array_size,0 , +sizeof (struct pollfd ) * (newsize-current_array_size)); +memset (&fd_to_pollfd[0 ] + current_array_size,0 , +sizeof (struct pollfd *) * (newsize-current_array_size)); + current_array_size = newsize; + } + + fds[nr_fds].fd = fd; + fds[nr_fds].events = events; + fd_to_pollfd[fd] = &fds[nr_fds]; + nr_fds++; + +return ; +fail: +dolog (LOG_ERR ,"realloc failed, ignoring fd %d\n" , fd); +return ; +} + +static short fd_revents (int fd) +{ +if (fd >= current_array_size) +return 0 ; +if (fd_to_pollfd[fd] ==NULL ) +return 0 ; +return fd_to_pollfd[fd]->revents; +} + +static void reset_fds (void ) +{ + nr_fds =0 ; +memset (fds,0 ,sizeof (struct pollfd ) * current_array_size); +memset (fd_to_pollfd,0 , +sizeof (struct pollfd *) * current_array_size); +} +void handle_io (void ) { -fd_set readfds, writefds;int ret;if (log_hv) {
@@ -957,23 +1046,21 @@ void handle_io(void) } } +if (initialize_pollfd_arrays ()) + goto out; +for (;;) {struct domain *d, *n; -int max_fd =-1 ; -struct timeval timeout; +int poll_timeout;/* timeout in milliseconds */ struct timespec ts;long long now, next_timeout =0 ; -FD_ZERO (&readfds); -FD_ZERO (&writefds); +reset_fds (); -FD_SET (xs_fileno (xs), &readfds); - max_fd =MAX (xs_fileno (xs), max_fd); +set_fds (xs_fileno (xs),POLLIN ); -if (log_hv) { -FD_SET (xc_evtchn_fd (xce_handle), &readfds); - max_fd =MAX (xc_evtchn_fd (xce_handle), max_fd); - } +if (log_hv) +set_fds (xc_evtchn_fd (xce_handle),POLLIN );if (clock_gettime (CLOCK_MONOTONIC , &ts) <0 )return ;
@@ -982,11 +1069,7 @@ void handle_io(void)/* Re-calculate any event counter allowances & unblock domains with new allowance */ for (d = dom_head; d; d = d->next) { -/* Add 5ms of fuzz since select() often returns -a couple of ms sooner than requested. Without -the fuzz we typically do an extra spin in select() -with a 1/2 ms timeout every other iteration */ -if ((now+5 ) > d->next_period) { +if (now > d->next_period) { d->next_period = now +RATE_LIMIT_PERIOD ;if (d->event_count >=RATE_LIMIT_ALLOWANCE ) { (void )xc_evtchn_unmask (d->xce_handle, d->local_port);
@@ -1006,74 +1089,73 @@ void handle_io(void) !d->buffer.max_capacity || d->buffer.size < d->buffer.max_capacity) {int evtchn_fd =xc_evtchn_fd (d->xce_handle); -FD_SET (evtchn_fd, &readfds); - max_fd =MAX (evtchn_fd, max_fd); +set_fds (evtchn_fd,POLLIN ); } }if (d->master_fd !=-1 ) { +short events =0 ;if (!d->is_dead &&ring_free_bytes (d)) -FD_SET (d->master_fd, &readfds); + events |=POLLIN ;if (!buffer_empty (&d->buffer)) -FD_SET (d->master_fd, &writefds); - max_fd =MAX (d->master_fd, max_fd); + events |=POLLOUT ; + +if (events) +set_fds (d->master_fd, events); } }/* If any domain has been rate limited, we need to work -out what timeout to supply to select */ +out what timeout to supply to poll */ if (next_timeout) {long long duration = (next_timeout - now);if (duration <=0 )/* sanity check */ duration =1 ; - timeout.tv_sec = duration /1000 ; - timeout.tv_usec = ((duration - (timeout.tv_sec *1000 )) - *1000 ); + poll_timeout = (int )duration; } - ret =select (max_fd +1 , &readfds, &writefds,0 , - next_timeout ? &timeout :NULL ); + ret =poll (fds, nr_fds, next_timeout ? poll_timeout :-1 );if (log_reload) {handle_log_reload (); log_reload =0 ; } -/* Abort if select failed, except for EINTR cases +/* Abort if poll failed, except for EINTR cases which indicate a possible log reload */ if (ret ==-1 ) {if (errno ==EINTR )continue ; -dolog (LOG_ERR ,"Failure in select: %d (%s)" , +dolog (LOG_ERR ,"Failure in poll: %d (%s)" , errno,strerror (errno));break ; } -if (log_hv &&FD_ISSET (xc_evtchn_fd (xce_handle), &readfds)) +if (log_hv &&fd_revents (xc_evtchn_fd (xce_handle)) &POLLIN )handle_hv_logs ();if (ret <=0 )continue ; -if (FD_ISSET (xs_fileno (xs), &readfds)) +if (fd_revents (xs_fileno (xs)) &POLLIN )handle_xs ();for (d = dom_head; d; d = n) { n = d->next;if (d->event_count <RATE_LIMIT_ALLOWANCE ) {if (d->xce_handle !=NULL && -FD_ISSET (xc_evtchn_fd (d->xce_handle), - &readfds)) +fd_revents (xc_evtchn_fd (d->xce_handle)) & +POLLIN )handle_ring_read (d); } -if (d->master_fd !=-1 &&FD_ISSET (d->master_fd, - &readfds)) +if (d->master_fd !=-1 && +fd_revents (d->master_fd) &POLLIN )handle_tty_read (d); -if (d->master_fd !=-1 &&FD_ISSET (d->master_fd, - &writefds)) +if (d->master_fd !=-1 && +fd_revents (d->master_fd) &POLLOUT )handle_tty_write (d);if (d->last_seen != enum_pass)
@@ -1084,6 +1166,7 @@ void handle_io(void) } } +destroy_pollfd_arrays (); out:if (log_hv_fd !=-1 ) {close (log_hv_fd);
Signature · 1 line
1.7.10.4
Re: [PATCH V2] Switch from select() to poll() in xenconsoled's IO loop.
Quoted 25 lines
tools/console/daemon/io.c +25
-0+static int initialize_pollfd_arrays (void ) +{ + fds = (struct pollfd *) +malloc (sizeof (struct pollfd ) *DEFAULT_ARRAY_SIZE ); +if (!fds) + goto fail; + fd_to_pollfd = (struct pollfd **) +malloc (sizeof (struct pollfd *) *DEFAULT_ARRAY_SIZE ); +if (!fd_to_pollfd) + goto fail; +memset (fds,0 ,sizeof (struct pollfd ) *DEFAULT_ARRAY_SIZE ); +memset (fd_to_pollfd,0 ,sizeof (struct pollfd *) *DEFAULT_ARRAY_SIZE ); + current_array_size =DEFAULT_ARRAY_SIZE ; +return 0 ; +fail: +free (fds); +free (fd_to_pollfd); +return -ENOMEM ; +} + +static void destroy_pollfd_arrays (void ) +{ +free (fds); +free (fd_to_pollfd); + current_array_size =0 ;
tools/console/daemon/io.c static void handle_log_reload(void) · line 974
Quoted 10 lines
tools/console/daemon/io.c +10
-0+} + +static void set_fds (int fd,short events) +{ +if (current_array_size < fd+1 ) { +struct pollfd *p1 =NULL ; +struct pollfd **p2 =NULL ; +unsigned int newsize = current_array_size; + +do { newsize +=GROWTH_LENGTH ; }while (newsize < fd+1 );
Steal #define ROUNDUP from tools/libxc/xc_linux_osdep.c.
tools/console/daemon/io.c static void handle_log_reload(void) · line 981
Quoted 7 lines
tools/console/daemon/io.c +7
-0+ + p1 =realloc (fds,sizeof (struct pollfd )*newsize); +if (!p1) + goto fail; + fds = p1; + + p2 =realloc (fd_to_pollfd,sizeof (struct pollfd *)*newsize);
realloc(NULL, ...) is the same as malloc() so I think you can initialise current_array_size to 0 and the various pointers to NULL and avoid the need for initialize_pollfd_arrays -- i.e. it will just grow from 0 on the first use here.
Quoted 5 lines
for (;;) { struct domain *d, *n;tools/console/daemon/io.c +1
-2-int max_fd =-1 ; -struct timeval timeout; +int poll_timeout;/* timeout in milliseconds */
tools/console/daemon/io.c void handle_io(void) · line 1060
Quoted 10 lines
tools/console/daemon/io.c +2
-4struct timespec ts;long long now, next_timeout =0 ; -FD_ZERO (&readfds); -FD_ZERO (&writefds); +reset_fds (); -FD_SET (xs_fileno (xs), &readfds); - max_fd =MAX (xs_fileno (xs), max_fd); +set_fds (xs_fileno (xs),POLLIN );
Do you know, or can you track, the maximum fd over time?
If you can then you could likely make use of automatic stack allocations (struct pollfd fds[max_fd]) and therefore avoid the pain of manual memory management.
Not sure what the semantics of those are inside a for loop where max_fd can change but worst case you could put the content of the loop into a function.
Ian.
Re: [PATCH V2] Switch from select() to poll() in xenconsoled's IO loop.
Quoted 37 lines · Ian Campbell
Quoted 25 lines
tools/console/daemon/io.c +25
-0+static int initialize_pollfd_arrays (void ) +{ + fds = (struct pollfd *) +malloc (sizeof (struct pollfd ) *DEFAULT_ARRAY_SIZE ); +if (!fds) + goto fail; + fd_to_pollfd = (struct pollfd **) +malloc (sizeof (struct pollfd *) *DEFAULT_ARRAY_SIZE ); +if (!fd_to_pollfd) + goto fail; +memset (fds,0 ,sizeof (struct pollfd ) *DEFAULT_ARRAY_SIZE ); +memset (fd_to_pollfd,0 ,sizeof (struct pollfd *) *DEFAULT_ARRAY_SIZE ); + current_array_size =DEFAULT_ARRAY_SIZE ; +return 0 ; +fail: +free (fds); +free (fd_to_pollfd); +return -ENOMEM ; +} + +static void destroy_pollfd_arrays (void ) +{ +free (fds); +free (fd_to_pollfd); + current_array_size =0 ;tools/console/daemon/io.c static void handle_log_reload(void) · line 974
Quoted 10 lines
tools/console/daemon/io.c +10
-0+} + +static void set_fds (int fd,short events) +{ +if (current_array_size < fd+1 ) { +struct pollfd *p1 =NULL ; +struct pollfd **p2 =NULL ; +unsigned int newsize = current_array_size; + +do { newsize +=GROWTH_LENGTH ; }while (newsize < fd+1 );Steal #define ROUNDUP from
tools/libxc/xc_linux_osdep.c.
Quoted 13 lines
tools/console/daemon/io.c static void handle_log_reload(void) · line 981
Quoted 7 lines
tools/console/daemon/io.c +7
-0+ + p1 =realloc (fds,sizeof (struct pollfd )*newsize); +if (!p1) + goto fail; + fds = p1; + + p2 =realloc (fd_to_pollfd,sizeof (struct pollfd *)*newsize);
realloc(NULL, ...)is the same asmalloc()so I think you can initialisecurrent_array_sizeto 0 and the various pointers to NULL and avoid the need forinitialize_pollfd_arrays-- i.e. it will just grow from 0 on the first use here.
Huh, good idea.
Quoted 12 lines
Quoted 12 lines
for (;;) { struct domain *d, *n;tools/console/daemon/io.c +2
-4-int max_fd =-1 ; -struct timeval timeout; +int poll_timeout;/* timeout in milliseconds */ struct timespec ts;long long now, next_timeout =0 ; -FD_ZERO (&readfds); -FD_ZERO (&writefds); +reset_fds ();
Quoted 8 lines
tools/console/daemon/io.c void handle_io(void) · line 1060
Quoted 3 lines
tools/console/daemon/io.c +1
-2-FD_SET (xs_fileno (xs), &readfds); - max_fd =MAX (xs_fileno (xs), max_fd); +set_fds (xs_fileno (xs),POLLIN );Do you know, or can you track, the maximum fd over time?
If you can then you could likely make use of automatic stack allocations (struct pollfd fds[max_fd]) and therefore avoid the pain of manual memory management.
Stack size is subject to system setting. Typically it is limited to 8MB in Linux as shown by ulimit -s. This is actually very small compared to heap space.
Of course we can make xenconsoled special among all the processes... But that's not ideal.
Wei.
Quoted 6 lines
Not sure what the semantics of those are inside a for loop where
max_fdcan change but worst case you could put the content of the loop into a function.Ian.
Re: [PATCH V2] Switch from select() to poll() in xenconsoled's IO loop.
Quoted 9 lines · Wei Liu
Quoted 8 lines · Ian Campbell
tools/console/daemon/io.c static void handle_log_reload(void) · line 947
Quoted 8 lines
tools/console/daemon/io.c +8
-0+static int initialize_pollfd_arrays (void ) +{ + fds = (struct pollfd *) +malloc (sizeof (struct pollfd ) *DEFAULT_ARRAY_SIZE ); +if (!fds) + goto fail; + fd_to_pollfd = (struct pollfd **) +malloc (sizeof (struct pollfd *) *DEFAULT_ARRAY_SIZE );
I believe it's considered "bad" to cast the result of malloc... Unless you are using a C++ compiler to compile C - in which case it's necessary. But then it should really be converted to "new" and "delete" where relevant. [Although that does cause problems with realloc!]
I do notice you are not casting realloc, so I expect it's safe to remove the casts here too.
Of course, if you remove these calls to malloc and just use realloc in the first place, you can ignore this comment! ;)
Quoted 57 lines
Quoted 28 lines
Quoted 17 lines
tools/console/daemon/io.c +17
-0+if (!fd_to_pollfd) + goto fail; +memset (fds,0 ,sizeof (struct pollfd ) *DEFAULT_ARRAY_SIZE ); +memset (fd_to_pollfd,0 ,sizeof (struct pollfd *) *DEFAULT_ARRAY_SIZE ); + current_array_size =DEFAULT_ARRAY_SIZE ; +return 0 ; +fail: +free (fds); +free (fd_to_pollfd); +return -ENOMEM ; +} + +static void destroy_pollfd_arrays (void ) +{ +free (fds); +free (fd_to_pollfd); + current_array_size =0 ;tools/console/daemon/io.c static void handle_log_reload(void) · line 974
Quoted 10 lines
tools/console/daemon/io.c +10
-0+} + +static void set_fds (int fd,short events) +{ +if (current_array_size < fd+1 ) { +struct pollfd *p1 =NULL ; +struct pollfd **p2 =NULL ; +unsigned int newsize = current_array_size; + +do { newsize +=GROWTH_LENGTH ; }while (newsize < fd+1 );Steal #define ROUNDUP from
tools/libxc/xc_linux_osdep.c.Quoted 12 lines
tools/console/daemon/io.c static void handle_log_reload(void) · line 981
Quoted 7 lines
tools/console/daemon/io.c +7
-0+ + p1 =realloc (fds,sizeof (struct pollfd )*newsize); +if (!p1) + goto fail; + fds = p1; + + p2 =realloc (fd_to_pollfd,sizeof (struct pollfd *)*newsize);
realloc(NULL, ...)is the same asmalloc()so I think you can initialisecurrent_array_sizeto 0 and the various pointers to NULL and avoid the need forinitialize_pollfd_arrays-- i.e. it will just grow from 0 on the first use here.Huh, good idea.
Quoted 15 lines
Quoted 15 lines
for (;;) { struct domain *d, *n;tools/console/daemon/io.c +3
-6-int max_fd =-1 ; -struct timeval timeout; +int poll_timeout;/* timeout in milliseconds */ struct timespec ts;long long now, next_timeout =0 ; -FD_ZERO (&readfds); -FD_ZERO (&writefds); +reset_fds (); -FD_SET (xs_fileno (xs), &readfds); - max_fd =MAX (xs_fileno (xs), max_fd); +set_fds (xs_fileno (xs),POLLIN );
Quoted 7 lines
Quoted 4 lines
Do you know, or can you track, the maximum fd over time?
If you can then you could likely make use of automatic stack allocations (struct pollfd fds[max_fd]) and therefore avoid the pain of manual memory management.Stack size is subject to system setting. Typically it is limited to 8MB in Linux as shown by
ulimit -s. This is actually very small compared to heap space.
Not to mention that if you run out of heapspace, you can "do something about it" (even if it's just print an error message and exit, it's better than "Stack overflow crash with no message at all").
I prefer allocations for that reason.
On the other hand, the number of fds we can fit in 8MB (or 4MB) is probably more than we'd ever get from running many guests with consoles.... And it wouldn't be hard to add a "set stack size to 16MB" or something like that as part of "start xenconsoled".
--
Mats
Quoted 21 lines
Of course we can make xenconsoled special among all the processes... But that's not ideal.
Wei.
Quoted 6 lines
Not sure what the semantics of those are inside a for loop where
max_fdcan change but worst case you could put the content of the loop into a function.Ian.
_______________________________________________
Xen-devel mailing list
[email protected]
http://lists.xen.org/xen-devel
PATCH v3 1/1 Switch from select() to poll() in xenconsoled's IO loop.
In reply to Wei LiuIn Linux select() typically supports up to 1024 file descriptors. This can be a problem when user tries to boot up many guests. Switching to poll() has minimum impact on existing code and has better scalibility.
Tracking arrays are dynamically allocated / reallocated. If the tracking arrays fail to expand, we just ignore the incoming fd.
Change from V2:
* remove unnecessary malloc in initialize_pollfd_arrays
* use ROUND_UP to get new size of arrays
--- tools/console/daemon/io.c | 147 +++++++++++++++++++++++++++++++++------------ 1 file changed, 109 insertions(+), 38 deletions(-)
tools/console/daemon/io.c +109 -38
@@ -28,7 +28,7 @@#include <stdlib.h> #include <errno.h> #include <string.h> -#include <sys/select.h> +#include <poll.h> #include <fcntl.h> #include <unistd.h> #include <termios.h>
@@ -928,9 +928,87 @@ static void handle_log_reload(void) } } + +/* Should have at least max_fd + 1 elements */ +static struct pollfd *fds; +static struct pollfd **fd_to_pollfd; +static unsigned int current_array_size; +static unsigned int nr_fds; + +static void initialize_pollfd_arrays (void ) +{ + fds =NULL ; + fd_to_pollfd =NULL ; + current_array_size =0 ; +} + +static void destroy_pollfd_arrays (void ) +{ +free (fds); +free (fd_to_pollfd); + current_array_size =0 ; +} + +#define ROUNDUP (_x,_w) (((unsigned long)(_x)+(1UL<<(_w))-1) & ~((1UL<<(_w))-1)) +static void set_fds (int fd,short events) +{ +if (current_array_size < fd+1 ) { +struct pollfd *p1 =NULL ; +struct pollfd **p2 =NULL ; +unsigned long newsize; + +/* Round up to 2^8 boundary, in practice this just +* make newsize larger than current_array_size. +*/ + newsize =ROUNDUP (fd+1 ,8 ); + + p1 =realloc (fds,sizeof (struct pollfd )*newsize); +if (!p1) + goto fail; + fds = p1; + + p2 =realloc (fd_to_pollfd,sizeof (struct pollfd *)*newsize); +if (!p2) + goto fail; + fd_to_pollfd = p2; + +memset (&fds[0 ] + current_array_size,0 , +sizeof (struct pollfd ) * (newsize-current_array_size)); +memset (&fd_to_pollfd[0 ] + current_array_size,0 , +sizeof (struct pollfd *) * (newsize-current_array_size)); + current_array_size = newsize; + } + + fds[nr_fds].fd = fd; + fds[nr_fds].events = events; + fd_to_pollfd[fd] = &fds[nr_fds]; + nr_fds++; + +return ; +fail: +dolog (LOG_ERR ,"realloc failed, ignoring fd %d\n" , fd); +return ; +} + +static short fd_revents (int fd) +{ +if (fd >= current_array_size) +return 0 ; +if (fd_to_pollfd[fd] ==NULL ) +return 0 ; +return fd_to_pollfd[fd]->revents; +} + +static void reset_fds (void ) +{ + nr_fds =0 ; +memset (fds,0 ,sizeof (struct pollfd ) * current_array_size); +memset (fd_to_pollfd,0 , +sizeof (struct pollfd *) * current_array_size); +} +void handle_io (void ) { -fd_set readfds, writefds;int ret;if (log_hv) {
@@ -957,23 +1035,20 @@ void handle_io(void) } } +initialize_pollfd_arrays (); +for (;;) {struct domain *d, *n; -int max_fd =-1 ; -struct timeval timeout; +int poll_timeout;/* timeout in milliseconds */ struct timespec ts;long long now, next_timeout =0 ; -FD_ZERO (&readfds); -FD_ZERO (&writefds); +reset_fds (); -FD_SET (xs_fileno (xs), &readfds); - max_fd =MAX (xs_fileno (xs), max_fd); +set_fds (xs_fileno (xs),POLLIN ); -if (log_hv) { -FD_SET (xc_evtchn_fd (xce_handle), &readfds); - max_fd =MAX (xc_evtchn_fd (xce_handle), max_fd); - } +if (log_hv) +set_fds (xc_evtchn_fd (xce_handle),POLLIN );if (clock_gettime (CLOCK_MONOTONIC , &ts) <0 )return ;
@@ -982,11 +1057,7 @@ void handle_io(void)/* Re-calculate any event counter allowances & unblock domains with new allowance */ for (d = dom_head; d; d = d->next) { -/* Add 5ms of fuzz since select() often returns -a couple of ms sooner than requested. Without -the fuzz we typically do an extra spin in select() -with a 1/2 ms timeout every other iteration */ -if ((now+5 ) > d->next_period) { +if (now > d->next_period) { d->next_period = now +RATE_LIMIT_PERIOD ;if (d->event_count >=RATE_LIMIT_ALLOWANCE ) { (void )xc_evtchn_unmask (d->xce_handle, d->local_port);
@@ -1006,74 +1077,73 @@ void handle_io(void) !d->buffer.max_capacity || d->buffer.size < d->buffer.max_capacity) {int evtchn_fd =xc_evtchn_fd (d->xce_handle); -FD_SET (evtchn_fd, &readfds); - max_fd =MAX (evtchn_fd, max_fd); +set_fds (evtchn_fd,POLLIN ); } }if (d->master_fd !=-1 ) { +short events =0 ;if (!d->is_dead &&ring_free_bytes (d)) -FD_SET (d->master_fd, &readfds); + events |=POLLIN ;if (!buffer_empty (&d->buffer)) -FD_SET (d->master_fd, &writefds); - max_fd =MAX (d->master_fd, max_fd); + events |=POLLOUT ; + +if (events) +set_fds (d->master_fd, events); } }/* If any domain has been rate limited, we need to work -out what timeout to supply to select */ +out what timeout to supply to poll */ if (next_timeout) {long long duration = (next_timeout - now);if (duration <=0 )/* sanity check */ duration =1 ; - timeout.tv_sec = duration /1000 ; - timeout.tv_usec = ((duration - (timeout.tv_sec *1000 )) - *1000 ); + poll_timeout = (int )duration; } - ret =select (max_fd +1 , &readfds, &writefds,0 , - next_timeout ? &timeout :NULL ); + ret =poll (fds, nr_fds, next_timeout ? poll_timeout :-1 );if (log_reload) {handle_log_reload (); log_reload =0 ; } -/* Abort if select failed, except for EINTR cases +/* Abort if poll failed, except for EINTR cases which indicate a possible log reload */ if (ret ==-1 ) {if (errno ==EINTR )continue ; -dolog (LOG_ERR ,"Failure in select: %d (%s)" , +dolog (LOG_ERR ,"Failure in poll: %d (%s)" , errno,strerror (errno));break ; } -if (log_hv &&FD_ISSET (xc_evtchn_fd (xce_handle), &readfds)) +if (log_hv &&fd_revents (xc_evtchn_fd (xce_handle)) &POLLIN )handle_hv_logs ();if (ret <=0 )continue ; -if (FD_ISSET (xs_fileno (xs), &readfds)) +if (fd_revents (xs_fileno (xs)) &POLLIN )handle_xs ();for (d = dom_head; d; d = n) { n = d->next;if (d->event_count <RATE_LIMIT_ALLOWANCE ) {if (d->xce_handle !=NULL && -FD_ISSET (xc_evtchn_fd (d->xce_handle), - &readfds)) +fd_revents (xc_evtchn_fd (d->xce_handle)) & +POLLIN )handle_ring_read (d); } -if (d->master_fd !=-1 &&FD_ISSET (d->master_fd, - &readfds)) +if (d->master_fd !=-1 && +fd_revents (d->master_fd) &POLLIN )handle_tty_read (d); -if (d->master_fd !=-1 &&FD_ISSET (d->master_fd, - &writefds)) +if (d->master_fd !=-1 && +fd_revents (d->master_fd) &POLLOUT )handle_tty_write (d);if (d->last_seen != enum_pass)
@@ -1084,6 +1154,7 @@ void handle_io(void) } } +destroy_pollfd_arrays (); out:if (log_hv_fd !=-1 ) {close (log_hv_fd);
Signature · 1 line
1.7.10.4
Re: [PATCH V3] Switch from select() to poll() in xenconsoled's IO loop.
Quoted 36 lines · Wei Liu
In Linux
select()typically supports up to 1024 file descriptors. This can be a problem when user tries to boot up many guests. Switching topoll()has minimum impact on existing code and has better scalibility.Tracking arrays are dynamically allocated / reallocated. If the tracking arrays fail to expand, we just ignore the incoming fd.
Change from V2:
* remove unnecessary malloc ininitialize_pollfd_arrays
* useROUND_UPto get new size of arraysSigned-off-by Wei Liu <[email protected]>--- tools/console/daemon/io.c | 147 +++++++++++++++++++++++++++++++++------------ 1 file changed, 109 insertions(+), 38 deletions(-)tools/console/daemon/io.c +4
-1@@ -28,7 +28,7 @@#include <stdlib.h> #include <errno.h> #include <string.h> -#include <sys/select.h> +#include <poll.h> #include <fcntl.h> #include <unistd.h> #include <termios.h> @@ -928,9 +928,87 @@ static void handle_log_reload(void) } } + +/* Should have at least max_fd + 1 elements */ +static struct pollfd *fds;
tools/console/daemon/io.c static void handle_log_reload(void) · line 943
Quoted 10 lines
tools/console/daemon/io.c +10
-0+static struct pollfd **fd_to_pollfd; +static unsigned int current_array_size; +static unsigned int nr_fds; + +static void initialize_pollfd_arrays (void ) +{ + fds =NULL ; + fd_to_pollfd =NULL ; + current_array_size =0 ; +}
These can all be done as part of the definition of the variables.
tools/console/daemon/io.c static void handle_log_reload(void) · line 950
Quoted 7 lines
tools/console/daemon/io.c +7
-0+ +static void destroy_pollfd_arrays (void ) +{ +free (fds); +free (fd_to_pollfd); + current_array_size =0 ; +}
Having done the above I'd be inclined to inline this at the single callsite.
Quoted 13 lines
tools/console/daemon/io.c +13
-0+ +#define ROUNDUP (_x,_w) (((unsigned long)(_x)+(1UL<<(_w))-1) & ~((1UL<<(_w))-1)) +static void set_fds (int fd,short events) +{ +if (current_array_size < fd+1 ) { +struct pollfd *p1 =NULL ; +struct pollfd **p2 =NULL ; +unsigned long newsize; + +/* Round up to 2^8 boundary, in practice this just +* make newsize larger than current_array_size. +*/ + newsize =ROUNDUP (fd+1 ,8 );
tools/console/daemon/io.c static void handle_log_reload(void) · line 973
Quoted 10 lines
tools/console/daemon/io.c +10
-0+ + p1 =realloc (fds,sizeof (struct pollfd )*newsize); +if (!p1) + goto fail; + fds = p1; + + p2 =realloc (fd_to_pollfd,sizeof (struct pollfd *)*newsize); +if (!p2) + goto fail; + fd_to_pollfd = p2;
Do these two arrays really need to be the same size?
I can see why fd_to_pollfd needs to be as big as the highest fd we are polling but the fds array only actually needs to scale with the total *number* of fds we are polling. Is it the case that the set of fds we are waiting on is pretty densely packed?
Or maybe even if the fds are sparse it doesn't matter and this is simpler at the cost of a little bit of extra memory usage?
Quoted 18 lines
tools/console/daemon/io.c +18
-0+ +memset (&fds[0 ] + current_array_size,0 , +sizeof (struct pollfd ) * (newsize-current_array_size)); +memset (&fd_to_pollfd[0 ] + current_array_size,0 , +sizeof (struct pollfd *) * (newsize-current_array_size)); + current_array_size = newsize; + } + + fds[nr_fds].fd = fd; + fds[nr_fds].events = events; + fd_to_pollfd[fd] = &fds[nr_fds]; + nr_fds++; + +return ; +fail: +dolog (LOG_ERR ,"realloc failed, ignoring fd %d\n" , fd); +return ; +}
[...]
tools/console/daemon/io.c void handle_io(void) · line 1129
Quoted 9 lines
tools/console/daemon/io.c +2
-2-if (log_hv &&FD_ISSET (xc_evtchn_fd (xce_handle), &readfds)) +if (log_hv &&fd_revents (xc_evtchn_fd (xce_handle)) &POLLIN )handle_hv_logs ();if (ret <=0 )continue ; -if (FD_ISSET (xs_fileno (xs), &readfds)) +if (fd_revents (xs_fileno (xs)) &POLLIN )
It seems like both this and the logging fd could be handled with a struct pollfd * variable specifically for each. Which combined with...
tools/console/daemon/io.c void handle_io(void) · line 1136
Quoted 9 lines
handle_xs(); for (d = dom_head; d; d = n) { n = d->next; if (d->event_count < RATE_LIMIT_ALLOWANCE) { if (d->xce_handle != NULL &&tools/console/daemon/io.c +1
-2-FD_ISSET (xc_evtchn_fd (d->xce_handle), - &readfds)) + fd_revents(xc_evtchn_fd(d->xce_handle)) &
... adding some struct pollfd pointers to struct domain (for this and master_fd) would remove the need for the fd_to_pollfd lookup array altogether.
Quoted 7 lines
tools/console/daemon/io.c +2
-2+ POLLIN)handle_ring_read (d ); } -if (d->master_fd !=-1 &&FD_ISSET (d->master_fd, - &readfds)) + if (d->master_fd != -1 &&
tools/console/daemon/io.c void handle_io(void) · line 1149
Quoted 10 lines
tools/console/daemon/io.c +3
-2+ fd_revents(d->master_fd) & POLLIN)handle_tty_read (d); -if (d->master_fd !=-1 &&FD_ISSET (d->master_fd, - &writefds)) +if (d->master_fd !=-1 && +fd_revents (d->master_fd) &POLLOUT )handle_tty_write (d);if (d->last_seen != enum_pass)
[...]
Ian.
Re: [PATCH V3] Switch from select() to poll() in xenconsoled's IO loop.
Quoted 50 lines · Ian Campbell
Quoted 36 lines · Wei Liu
In Linux
select()typically supports up to 1024 file descriptors. This can be a problem when user tries to boot up many guests. Switching topoll()has minimum impact on existing code and has better scalibility.Tracking arrays are dynamically allocated / reallocated. If the tracking arrays fail to expand, we just ignore the incoming fd.
Change from V2:
* remove unnecessary malloc ininitialize_pollfd_arrays
* useROUND_UPto get new size of arraysSigned-off-by Wei Liu <[email protected]>--- tools/console/daemon/io.c | 147 +++++++++++++++++++++++++++++++++------------ 1 file changed, 109 insertions(+), 38 deletions(-)tools/console/daemon/io.c +4
-1@@ -28,7 +28,7 @@#include <stdlib.h> #include <errno.h> #include <string.h> -#include <sys/select.h> +#include <poll.h> #include <fcntl.h> #include <unistd.h> #include <termios.h> @@ -928,9 +928,87 @@ static void handle_log_reload(void) } } + +/* Should have at least max_fd + 1 elements */ +static struct pollfd *fds;tools/console/daemon/io.c static void handle_log_reload(void) · line 943
Quoted 10 lines
tools/console/daemon/io.c +10
-0+static struct pollfd **fd_to_pollfd; +static unsigned int current_array_size; +static unsigned int nr_fds; + +static void initialize_pollfd_arrays (void ) +{ + fds =NULL ; + fd_to_pollfd =NULL ; + current_array_size =0 ; +}These can all be done as part of the definition of the variables.
Quoted 11 lines
tools/console/daemon/io.c static void handle_log_reload(void) · line 950
Quoted 7 lines
tools/console/daemon/io.c +7
-0+ +static void destroy_pollfd_arrays (void ) +{ +free (fds); +free (fd_to_pollfd); + current_array_size =0 ; +}Having done the above I'd be inclined to inline this at the single callsite.
I left the initialize() there just to pair with destroy(). But as you suggested, I can eliminate both.
Quoted 17 lines
Quoted 17 lines
tools/console/daemon/io.c +17
-0+ +#define ROUNDUP (_x,_w) (((unsigned long)(_x)+(1UL<<(_w))-1) & ~((1UL<<(_w))-1)) +static void set_fds (int fd,short events) +{ +if (current_array_size < fd+1 ) { +struct pollfd *p1 =NULL ; +struct pollfd **p2 =NULL ; +unsigned long newsize; + +/* Round up to 2^8 boundary, in practice this just +* make newsize larger than current_array_size. +*/ + newsize =ROUNDUP (fd+1 ,8 ); + + p1 =realloc (fds,sizeof (struct pollfd )*newsize); +if (!p1) + goto fail;
Quoted 9 lines
tools/console/daemon/io.c static void handle_log_reload(void) · line 973
Quoted 6 lines
tools/console/daemon/io.c +6
-0+ fds = p1; + + p2 =realloc (fd_to_pollfd,sizeof (struct pollfd *)*newsize); +if (!p2) + goto fail; + fd_to_pollfd = p2;Do these two arrays really need to be the same size?
Not necessary. I will see what I can do.
Quoted 5 lines
I can see why
fd_to_pollfdneeds to be as big as the highest fd we are polling but the fds array only actually needs to scale with the total *number* of fds we are polling. Is it the case that the set of fds we are waiting on is pretty densely packed?
It depends. If we have many guests up then destroy them all, then the arrays can be sparse. There is no way to shrink the arrays. But see below.
Quoted 4 lines
Or maybe even if the fds are sparse it doesn't matter and this is simpler at the cost of a little bit of extra memory usage?
Even we have thousands of guests, the tracking arrays only occupy a few megabytes.
Quoted 42 lines
Quoted 18 lines
tools/console/daemon/io.c +18
-0+ +memset (&fds[0 ] + current_array_size,0 , +sizeof (struct pollfd ) * (newsize-current_array_size)); +memset (&fd_to_pollfd[0 ] + current_array_size,0 , +sizeof (struct pollfd *) * (newsize-current_array_size)); + current_array_size = newsize; + } + + fds[nr_fds].fd = fd; + fds[nr_fds].events = events; + fd_to_pollfd[fd] = &fds[nr_fds]; + nr_fds++; + +return ; +fail: +dolog (LOG_ERR ,"realloc failed, ignoring fd %d\n" , fd); +return ; +}[...]
tools/console/daemon/io.c void handle_io(void) · line 1129
Quoted 9 lines
tools/console/daemon/io.c +2
-2-if (log_hv &&FD_ISSET (xc_evtchn_fd (xce_handle), &readfds)) +if (log_hv &&fd_revents (xc_evtchn_fd (xce_handle)) &POLLIN )handle_hv_logs ();if (ret <=0 )continue ; -if (FD_ISSET (xs_fileno (xs), &readfds)) +if (fd_revents (xs_fileno (xs)) &POLLIN )It seems like both this and the logging fd could be handled with a struct pollfd * variable specifically for each. Which combined with...
tools/console/daemon/io.c void handle_io(void) · line 1136
Quoted 9 lines
handle_xs(); for (d = dom_head; d; d = n) { n = d->next; if (d->event_count < RATE_LIMIT_ALLOWANCE) { if (d->xce_handle != NULL &&tools/console/daemon/io.c +1
-2-FD_ISSET (xc_evtchn_fd (d->xce_handle), - &readfds)) + fd_revents(xc_evtchn_fd(d->xce_handle)) &
Quoted 4 lines
... adding some struct pollfd pointers to struct domain (for this and
master_fd) would remove the need for thefd_to_pollfdlookup array altogether.
Yes, this is possible. But my original thought was to make as little impact to original code as possible, so I didn't touch struct domain and choose to add extra tracking facilities to make it just work.
If we hit poll() bottleneck we can move to libevent or libev, which requires big surgery on existing code.
Of course I can do that as well if that suits you.
Wei.
Re: [PATCH V3] Switch from select() to poll() in xenconsoled's IO loop.
Quoted 11 lines
Quoted 11 lines
Quoted 10 lines
handle_xs(); for (d = dom_head; d; d = n) { n = d->next; if (d->event_count < RATE_LIMIT_ALLOWANCE) { if (d->xce_handle != NULL &&tools/console/daemon/io.c +1
-2-FD_ISSET (xc_evtchn_fd (d->xce_handle), - &readfds)) + fd_revents(xc_evtchn_fd(d->xce_handle)) &
Quoted 8 lines
Quoted 4 lines
... adding some struct pollfd pointers to struct domain (for this and
master_fd) would remove the need for thefd_to_pollfdlookup array altogether.Yes, this is possible. But my original thought was to make as little impact to original code as possible, so I didn't touch struct domain and choose to add extra tracking facilities to make it just work.
I think adding a few pointers to struct domain would be less code changed over all since you wouldn't need to create/manage one of the arrays at all.
Quoted 8 lines
If we hit
poll()bottleneck we can move to libevent or libev, which requires big surgery on existing code.Of course I can do that as well if that suits you.
Wei.
PATCH v4 1/1 Switch from select() to poll() in xenconsoled's IO loop
In reply to Wei LiuIn Linux select() typically supports up to 1024 file descriptors. This can be a problem when user tries to boot up many guests. Switching to poll() has minimum impact on existing code and has better scalibility.
pollfd array is dynamically allocated / reallocated. If the array fails to expand, we just ignore the incoming fd.
Change from V2:
* remove unnecessary malloc in initialize_pollfd_arrays
* use ROUND_UP to get new size of arrays
Change from V3:
* remove initialize and destroy function for array
* embedded tracking structure in struct domain, eliminate fd_to_pollfd
--- tools/console/daemon/io.c | 119 ++++++++++++++++++++++++++++-------------- tools/console/daemon/utils.c | 2 + tools/console/daemon/utils.h | 2 + 3 files changed, 85 insertions(+), 38 deletions(-)
tools/console/daemon/io.c +81 -38
@@ -28,7 +28,7 @@#include <stdlib.h> #include <errno.h> #include <string.h> -#include <sys/select.h> +#include <poll.h> #include <fcntl.h> #include <unistd.h> #include <termios.h>
@@ -69,6 +69,7 @@ static int log_hv_fd = -1;static evtchn_port_or_error_t log_hv_evtchn =-1 ;static xc_interface *xch;/* why does xenconsoled have two xc handles ? */ static xc_evtchn *xce_handle =NULL ; +static struct pollfd *xce_pollfd =NULL ;struct buffer {char *data;
@@ -81,7 +82,9 @@ struct buffer {struct domain {int domid;int master_fd; +struct pollfd *master_pollfd;int slave_fd; +struct pollfd *slave_pollfd;int log_fd;bool is_dead;unsigned last_seen;
@@ -92,6 +95,7 @@ struct domain {evtchn_port_or_error_t local_port;evtchn_port_or_error_t remote_port;xc_evtchn *xce_handle; +struct pollfd *xce_pollfd;struct xencons_interface *interface;int event_count;long long next_period;
@@ -928,9 +932,52 @@ static void handle_log_reload(void) } } +static struct pollfd *fds; +static unsigned int current_array_size; +static unsigned int nr_fds; + +#define ROUNDUP (_x,_w) (((unsigned long)(_x)+(1UL<<(_w))-1) & ~((1UL<<(_w))-1)) +static struct pollfd *set_fds (int fd,short events) +{ +struct pollfd *ret; +if (current_array_size < nr_fds +1 ) { +struct pollfd *new_fds =NULL ; +unsigned long newsize; + +/* Round up to 2^8 boundary, in practice this just +* make newsize larger than current_array_size. +*/ + newsize =ROUNDUP (nr_fds +1 ,8 ); + + new_fds =realloc (fds,sizeof (struct pollfd )*newsize); +if (!new_fds) + goto fail; + fds = new_fds; + +memset (&fds[0 ] + current_array_size,0 , +sizeof (struct pollfd ) * (newsize-current_array_size)); + current_array_size = newsize; + } + + fds[nr_fds].fd = fd; + fds[nr_fds].events = events; + ret = &fds[nr_fds]; + nr_fds++; + +return ret; +fail: +dolog (LOG_ERR ,"realloc failed, ignoring fd %d\n" , fd); +return NULL ; +} + +static void reset_fds (void ) +{ + nr_fds =0 ; +memset (fds,0 ,sizeof (struct pollfd ) * current_array_size); +} +void handle_io (void ) { -fd_set readfds, writefds;int ret;if (log_hv) {
@@ -959,21 +1006,16 @@ void handle_io(void)for (;;) {struct domain *d, *n; -int max_fd =-1 ; -struct timeval timeout; +int poll_timeout;/* timeout in milliseconds */ struct timespec ts;long long now, next_timeout =0 ; -FD_ZERO (&readfds); -FD_ZERO (&writefds); +reset_fds (); -FD_SET (xs_fileno (xs), &readfds); - max_fd =MAX (xs_fileno (xs), max_fd); + xs_pollfd =set_fds (xs_fileno (xs),POLLIN ); -if (log_hv) { -FD_SET (xc_evtchn_fd (xce_handle), &readfds); - max_fd =MAX (xc_evtchn_fd (xce_handle), max_fd); - } +if (log_hv) + xce_pollfd =set_fds (xc_evtchn_fd (xce_handle),POLLIN );if (clock_gettime (CLOCK_MONOTONIC , &ts) <0 )return ;
@@ -982,11 +1024,7 @@ void handle_io(void)/* Re-calculate any event counter allowances & unblock domains with new allowance */ for (d = dom_head; d; d = d->next) { -/* Add 5ms of fuzz since select() often returns -a couple of ms sooner than requested. Without -the fuzz we typically do an extra spin in select() -with a 1/2 ms timeout every other iteration */ -if ((now+5 ) > d->next_period) { +if (now > d->next_period) { d->next_period = now +RATE_LIMIT_PERIOD ;if (d->event_count >=RATE_LIMIT_ALLOWANCE ) { (void )xc_evtchn_unmask (d->xce_handle, d->local_port);
@@ -1006,74 +1044,76 @@ void handle_io(void) !d->buffer.max_capacity || d->buffer.size < d->buffer.max_capacity) {int evtchn_fd =xc_evtchn_fd (d->xce_handle); -FD_SET (evtchn_fd, &readfds); - max_fd =MAX (evtchn_fd, max_fd); + d->xce_pollfd =set_fds (evtchn_fd,POLLIN ); } }if (d->master_fd !=-1 ) { +short events =0 ;if (!d->is_dead &&ring_free_bytes (d)) -FD_SET (d->master_fd, &readfds); + events |=POLLIN ;if (!buffer_empty (&d->buffer)) -FD_SET (d->master_fd, &writefds); - max_fd =MAX (d->master_fd, max_fd); + events |=POLLOUT ; + +if (events) + d->master_pollfd = +set_fds (d->master_fd, events); } }/* If any domain has been rate limited, we need to work -out what timeout to supply to select */ +out what timeout to supply to poll */ if (next_timeout) {long long duration = (next_timeout - now);if (duration <=0 )/* sanity check */ duration =1 ; - timeout.tv_sec = duration /1000 ; - timeout.tv_usec = ((duration - (timeout.tv_sec *1000 )) - *1000 ); + poll_timeout = (int )duration; } - ret =select (max_fd +1 , &readfds, &writefds,0 , - next_timeout ? &timeout :NULL ); + ret =poll (fds, nr_fds, next_timeout ? poll_timeout :-1 );if (log_reload) {handle_log_reload (); log_reload =0 ; } -/* Abort if select failed, except for EINTR cases +/* Abort if poll failed, except for EINTR cases which indicate a possible log reload */ if (ret ==-1 ) {if (errno ==EINTR )continue ; -dolog (LOG_ERR ,"Failure in select: %d (%s)" , +dolog (LOG_ERR ,"Failure in poll: %d (%s)" , errno,strerror (errno));break ; } -if (log_hv &&FD_ISSET (xc_evtchn_fd (xce_handle), &readfds)) +if (log_hv && xce_pollfd && xce_pollfd->revents &POLLIN )handle_hv_logs ();if (ret <=0 )continue ; -if (FD_ISSET (xs_fileno (xs), &readfds)) +if (xs_pollfd && xs_pollfd->revents &POLLIN )handle_xs ();for (d = dom_head; d; d = n) { n = d->next;if (d->event_count <RATE_LIMIT_ALLOWANCE ) {if (d->xce_handle !=NULL && -FD_ISSET (xc_evtchn_fd (d->xce_handle), - &readfds)) + d->xce_pollfd !=NULL && + d->xce_pollfd->revents &POLLIN )handle_ring_read (d); } -if (d->master_fd !=-1 &&FD_ISSET (d->master_fd, - &readfds)) +if (d->master_fd !=-1 && + d->master_pollfd && + d->master_pollfd->revents &POLLIN )handle_tty_read (d); -if (d->master_fd !=-1 &&FD_ISSET (d->master_fd, - &writefds)) +if (d->master_fd !=-1 && + d->master_pollfd && + d->master_pollfd->revents &POLLOUT )handle_tty_write (d);if (d->last_seen != enum_pass)
@@ -1084,6 +1124,9 @@ void handle_io(void) } } +free (fds); + current_array_size =0 ; + out:if (log_hv_fd !=-1 ) {close (log_hv_fd);
tools/console/daemon/utils.c +2 -0
@@ -33,11 +33,13 @@#include <sys/un.h> #include <string.h> #include <signal.h> +#include <poll.h> #include "xenctrl.h" #include "utils.h" struct xs_handle *xs; +struct pollfd *xs_pollfd;xc_interface *xc;static void child_exit (int sig)
tools/console/daemon/utils.h +2 -0
@@ -24,6 +24,7 @@#include <stdbool.h> #include <syslog.h> #include <stdio.h> +#include <poll.h> #include <xenctrl.h> #include <xenstore.h>
@@ -32,6 +33,7 @@ void daemonize(const char *pidfile);bool xen_setup (void );extern struct xs_handle *xs; +extern struct pollfd *xs_pollfd;extern xc_interface *xc;#if 1
Signature · 1 line
1.7.10.4
Re: [PATCH V4] Switch from select() to poll() in xenconsoled's IO loop
On Mon, 2013-01-07 at 14:28 +0000, Wei Liu wrote
Quoted 8 lines
tools/console/daemon/io.c +1
-0@@ -69,6 +69,7 @@ static int log_hv_fd = -1;static evtchn_port_or_error_t log_hv_evtchn =-1 ;static xc_interface *xch;/* why does xenconsoled have two xc handles ? */ static xc_evtchn *xce_handle =NULL ; +static struct pollfd *xce_pollfd =NULL ;struct buffer {char *data;
[...]
tools/console/daemon/utils.h void daemonize(const char *pidfile); · line 37
Quoted 6 lines
tools/console/daemon/utils.h +1
-0@@ -32,6 +33,7 @@ void daemonize(const char *pidfile);bool xen_setup (void );extern struct xs_handle *xs; +extern struct pollfd *xs_pollfd;extern xc_interface *xc;
xs_pollfd and the xce_pollfd can both be local to the handle_io function, I think.
Quoted 6 lines
tools/console/daemon/io.c +3
-2-if (d->master_fd !=-1 &&FD_ISSET (d->master_fd, - &readfds)) +if (d->master_fd !=-1 && + d->master_pollfd && + d->master_pollfd->revents &POLLIN )
tools/console/daemon/io.c void handle_io(void) · line 1119
Quoted 10 lines
tools/console/daemon/io.c +3
-2handle_tty_read (d); -if (d->master_fd !=-1 &&FD_ISSET (d->master_fd, - &writefds)) +if (d->master_fd !=-1 && + d->master_pollfd && + d->master_pollfd->revents &POLLOUT )handle_tty_write (d);if (d->last_seen != enum_pass)
This is probably one for Ian J but I wonder if you need to handle POLLERR or POLLHUP here. ISTR some of oddness WRT these when Ian implemented the libxl event subsystem.
Ian.
Re: [PATCH V4] Switch from select() to poll() in xenconsoled's IO loop
Quoted 35 lines · Ian Campbell
On Mon, 2013-01-07 at 14:28 +0000, Wei Liu wrote
Quoted 8 lines
tools/console/daemon/io.c +1
-0@@ -69,6 +69,7 @@ static int log_hv_fd = -1;static evtchn_port_or_error_t log_hv_evtchn =-1 ;static xc_interface *xch;/* why does xenconsoled have two xc handles ? */ static xc_evtchn *xce_handle =NULL ; +static struct pollfd *xce_pollfd =NULL ;struct buffer {char *data;[...]
tools/console/daemon/utils.h void daemonize(const char *pidfile); · line 37
Quoted 6 lines
tools/console/daemon/utils.h +1
-0@@ -32,6 +33,7 @@ void daemonize(const char *pidfile);bool xen_setup (void );extern struct xs_handle *xs; +extern struct pollfd *xs_pollfd;extern xc_interface *xc;
xs_pollfdand thexce_pollfdcan both be local to thehandle_iofunction, I think.Quoted 15 lines
tools/console/daemon/io.c +6
-4-if (d->master_fd !=-1 &&FD_ISSET (d->master_fd, - &readfds)) +if (d->master_fd !=-1 && + d->master_pollfd && + d->master_pollfd->revents &POLLIN )handle_tty_read (d); -if (d->master_fd !=-1 &&FD_ISSET (d->master_fd, - &writefds)) +if (d->master_fd !=-1 && + d->master_pollfd && + d->master_pollfd->revents &POLLOUT )handle_tty_write (d);
Quoted 6 lines
Quoted 1 line
if (d->last_seen != enum_pass)This is probably one for Ian J but I wonder if you need to handle POLLERR or POLLHUP here. ISTR some of oddness WRT these when Ian implemented the libxl event subsystem.
Ian J, can you elaborate on this?
Wei.
Re: [PATCH V4] Switch from select() to poll() in xenconsoled's IO loop
In reply to Ian CampbellIan Campbell writes ("Re: [Xen-devel] [PATCH V4] Switch from select() to poll() in xenconsoled's IO loop"):
Quoted 3 lines
On Mon, 2013-01-07 at 14:28 +0000, Wei Liu wrote
Quoted 2 lines
tools/console/daemon/io.c +0
-2-if (d->master_fd !=-1 &&FD_ISSET (d->master_fd, - &readfds))
...
Quoted 3 lines
This is probably one for Ian J but I wonder if you need to handle POLLERR or POLLHUP here. ISTR some of oddness WRT these when Ian implemented the libxl event subsystem.
I haven't read the patch in question but:
You certainly need to check for POLLERR and POLLHUP. They appear in revents even if not requested. Normally they can be treated as fatal for the descriptor.
You should also think about POLLPRI. Often it's correct to request it and then treat it as fatal for the descriptor if it occurs.
Ian.
(I haven't read the patch I'm afraid...)
Re: [PATCH V4] Switch from select() to poll() in xenconsoled's IO loop
In reply to Wei LiuQuoted 143 lines · Wei Liu
In Linux
select()typically supports up to 1024 file descriptors. This can be a problem when user tries to boot up many guests. Switching topoll()has minimum impact on existing code and has better scalibility.pollfd array is dynamically allocated / reallocated. If the array fails to expand, we just ignore the incoming fd.
Change from V2:
* remove unnecessary malloc ininitialize_pollfd_arrays
* useROUND_UPto get new size of arraysChange from V3:
* remove initialize and destroy function for array
* embedded tracking structure in struct domain, eliminatefd_to_pollfdSigned-off-by Wei Liu <[email protected]>--- tools/console/daemon/io.c | 119 ++++++++++++++++++++++++++++-------------- tools/console/daemon/utils.c | 2 + tools/console/daemon/utils.h | 2 + 3 files changed, 85 insertions(+), 38 deletions(-)tools/console/daemon/io.c +54
-12@@ -28,7 +28,7 @@#include <stdlib.h> #include <errno.h> #include <string.h> -#include <sys/select.h> +#include <poll.h> #include <fcntl.h> #include <unistd.h> #include <termios.h> @@ -69,6 +69,7 @@ static int log_hv_fd = -1;static evtchn_port_or_error_t log_hv_evtchn =-1 ;static xc_interface *xch;/* why does xenconsoled have two xc handles ? */ static xc_evtchn *xce_handle =NULL ; +static struct pollfd *xce_pollfd =NULL ;struct buffer {char *data;@@ -81,7 +82,9 @@ struct buffer {struct domain {int domid;int master_fd; +struct pollfd *master_pollfd;int slave_fd; +struct pollfd *slave_pollfd;int log_fd;bool is_dead;unsigned last_seen;@@ -92,6 +95,7 @@ struct domain {evtchn_port_or_error_t local_port;evtchn_port_or_error_t remote_port;xc_evtchn *xce_handle; +struct pollfd *xce_pollfd;struct xencons_interface *interface;int event_count;long long next_period;@@ -928,9 +932,52 @@ static void handle_log_reload(void) } } +static struct pollfd *fds; +static unsigned int current_array_size; +static unsigned int nr_fds; + +#define ROUNDUP (_x,_w) (((unsigned long)(_x)+(1UL<<(_w))-1) & ~((1UL<<(_w))-1)) +static struct pollfd *set_fds (int fd,short events) +{ +struct pollfd *ret; +if (current_array_size < nr_fds +1 ) { +struct pollfd *new_fds =NULL ; +unsigned long newsize; + +/* Round up to 2^8 boundary, in practice this just +* make newsize larger than current_array_size. +*/ + newsize =ROUNDUP (nr_fds +1 ,8 ); + + new_fds =realloc (fds,sizeof (struct pollfd )*newsize); +if (!new_fds) + goto fail; + fds = new_fds; + +memset (&fds[0 ] + current_array_size,0 , +sizeof (struct pollfd ) * (newsize-current_array_size)); + current_array_size = newsize; + } + + fds[nr_fds].fd = fd; + fds[nr_fds].events = events; + ret = &fds[nr_fds]; + nr_fds++; + +return ret; +fail: +dolog (LOG_ERR ,"realloc failed, ignoring fd %d\n" , fd); +return NULL ; +} + +static void reset_fds (void ) +{ + nr_fds =0 ; +memset (fds,0 ,sizeof (struct pollfd ) * current_array_size); +} +void handle_io (void ) { -fd_set readfds, writefds;int ret;if (log_hv) {@@ -959,21 +1006,16 @@ void handle_io(void)for (;;) {struct domain *d, *n; -int max_fd =-1 ; -struct timeval timeout; +int poll_timeout;/* timeout in milliseconds */ struct timespec ts;long long now, next_timeout =0 ; -FD_ZERO (&readfds); -FD_ZERO (&writefds); +reset_fds (); -FD_SET (xs_fileno (xs), &readfds); - max_fd =MAX (xs_fileno (xs), max_fd); + xs_pollfd =set_fds (xs_fileno (xs),POLLIN ); -if (log_hv) { -FD_SET (xc_evtchn_fd (xce_handle), &readfds); - max_fd =MAX (xc_evtchn_fd (xce_handle), max_fd); - } +if (log_hv) + xce_pollfd =set_fds (xc_evtchn_fd (xce_handle),POLLIN );if (clock_gettime (CLOCK_MONOTONIC , &ts) <0 )return ;
tools/console/daemon/io.c void handle_io(void) · line 1027
Quoted 9 lines
tools/console/daemon/io.c +1
-5@@ -982,11 +1024,7 @@ void handle_io(void)/* Re-calculate any event counter allowances & unblock domains with new allowance */ for (d = dom_head; d; d = d->next) { -/* Add 5ms of fuzz since select() often returns -a couple of ms sooner than requested. Without -the fuzz we typically do an extra spin in select() -with a 1/2 ms timeout every other iteration */ -if ((now+5 ) > d->next_period) { +if (now > d->next_period) {
Is poll more accurate than select? I would have thought that they were based on the same timing, and thus equally "fuzzy"?
Quoted 116 lines
tools/console/daemon/io.c +26
-21d->next_period = now +RATE_LIMIT_PERIOD ;if (d->event_count >=RATE_LIMIT_ALLOWANCE ) { (void )xc_evtchn_unmask (d->xce_handle, d->local_port);@@ -1006,74 +1044,76 @@ void handle_io(void) !d->buffer.max_capacity || d->buffer.size < d->buffer.max_capacity) {int evtchn_fd =xc_evtchn_fd (d->xce_handle); -FD_SET (evtchn_fd, &readfds); - max_fd =MAX (evtchn_fd, max_fd); + d->xce_pollfd =set_fds (evtchn_fd,POLLIN ); } }if (d->master_fd !=-1 ) { +short events =0 ;if (!d->is_dead &&ring_free_bytes (d)) -FD_SET (d->master_fd, &readfds); + events |=POLLIN ;if (!buffer_empty (&d->buffer)) -FD_SET (d->master_fd, &writefds); - max_fd =MAX (d->master_fd, max_fd); + events |=POLLOUT ; + +if (events) + d->master_pollfd = +set_fds (d->master_fd, events); } }/* If any domain has been rate limited, we need to work -out what timeout to supply to select */ +out what timeout to supply to poll */ if (next_timeout) {long long duration = (next_timeout - now);if (duration <=0 )/* sanity check */ duration =1 ; - timeout.tv_sec = duration /1000 ; - timeout.tv_usec = ((duration - (timeout.tv_sec *1000 )) - *1000 ); + poll_timeout = (int )duration; } - ret =select (max_fd +1 , &readfds, &writefds,0 , - next_timeout ? &timeout :NULL ); + ret =poll (fds, nr_fds, next_timeout ? poll_timeout :-1 );if (log_reload) {handle_log_reload (); log_reload =0 ; } -/* Abort if select failed, except for EINTR cases +/* Abort if poll failed, except for EINTR cases which indicate a possible log reload */ if (ret ==-1 ) {if (errno ==EINTR )continue ; -dolog (LOG_ERR ,"Failure in select: %d (%s)" , +dolog (LOG_ERR ,"Failure in poll: %d (%s)" , errno,strerror (errno));break ; } -if (log_hv &&FD_ISSET (xc_evtchn_fd (xce_handle), &readfds)) +if (log_hv && xce_pollfd && xce_pollfd->revents &POLLIN )handle_hv_logs ();if (ret <=0 )continue ; -if (FD_ISSET (xs_fileno (xs), &readfds)) +if (xs_pollfd && xs_pollfd->revents &POLLIN )handle_xs ();for (d = dom_head; d; d = n) { n = d->next;if (d->event_count <RATE_LIMIT_ALLOWANCE ) {if (d->xce_handle !=NULL && -FD_ISSET (xc_evtchn_fd (d->xce_handle), - &readfds)) + d->xce_pollfd !=NULL && + d->xce_pollfd->revents &POLLIN )handle_ring_read (d); } -if (d->master_fd !=-1 &&FD_ISSET (d->master_fd, - &readfds)) +if (d->master_fd !=-1 && + d->master_pollfd && + d->master_pollfd->revents &POLLIN )handle_tty_read (d); -if (d->master_fd !=-1 &&FD_ISSET (d->master_fd, - &writefds)) +if (d->master_fd !=-1 && + d->master_pollfd && + d->master_pollfd->revents &POLLOUT )handle_tty_write (d);if (d->last_seen != enum_pass)@@ -1084,6 +1124,9 @@ void handle_io(void) } } +free (fds); + current_array_size =0 ; + out:if (log_hv_fd !=-1 ) {close (log_hv_fd);tools/console/daemon/utils.c +0
-0
tools/console/daemon/utils.c line 42
Quoted 10 lines
tools/console/daemon/utils.c +2
-0@@ -33,11 +33,13 @@#include <sys/un.h> #include <string.h> #include <signal.h> +#include <poll.h> #include "xenctrl.h" #include "utils.h" struct xs_handle *xs; +struct pollfd *xs_pollfd;
I don't see this used outside of io.c - am I missing something?
Adding more dependencies between different source files seems unnecessary...
--
Mats
Quoted 34 lines
tools/console/daemon/utils.c +0
-0xc_interface *xc;static void child_exit (int sig)tools/console/daemon/utils.h +2
-1@@ -24,6 +24,7 @@#include <stdbool.h> #include <syslog.h> #include <stdio.h> +#include <poll.h> #include <xenctrl.h> #include <xenstore.h> @@ -32,6 +33,7 @@ void daemonize(const char *pidfile);bool xen_setup (void );extern struct xs_handle *xs; +extern struct pollfd *xs_pollfd;extern xc_interface *xc;#if 1 --1.7.10.4
_______________________________________________
Xen-devel mailing list
[email protected]
http://lists.xen.org/xen-devel
Re: [PATCH V4] Switch from select() to poll() in xenconsoled's IO loop
Quoted 5 lines · Mats Petersson
Quoted 5 lines
return;tools/console/daemon/io.c +0
-0@@ -982,11 +1024,7 @@ void handle_io(void)/* Re-calculate any event counter allowances & unblock domains with new allowance */ for (d = dom_head; d; d = d->next) {
Quoted 8 lines
tools/console/daemon/io.c void handle_io(void) · line 1027
Quoted 6 lines
tools/console/daemon/io.c +1
-5-/* Add 5ms of fuzz since select() often returns -a couple of ms sooner than requested. Without -the fuzz we typically do an extra spin in select() -with a 1/2 ms timeout every other iteration */ -if ((now+5 ) > d->next_period) { +if (now > d->next_period) {Is poll more accurate than select? I would have thought that they were based on the same timing, and thus equally "fuzzy"?
Is there any actual proof that the fuzz is needed? Specs of both select() and poll() don't seem to mention this behaviour at all.
Quoted 9 lines
tools/console/daemon/utils.c line 42
Quoted 4 lines
tools/console/daemon/utils.c +1
-0#include "utils.h" struct xs_handle *xs; +struct pollfd *xs_pollfd;I don't see this used outside of
io.c- am I missing something?Adding more dependencies between different source files seems unnecessary...
Fixed.
Wei.
Re: [PATCH V4] Switch from select() to poll() in xenconsoled's IO loop
Quoted 15 lines · Wei Liu
Quoted 5 lines · Mats Petersson
Quoted 5 lines
return;tools/console/daemon/io.c +0
-0@@ -982,11 +1024,7 @@ void handle_io(void)/* Re-calculate any event counter allowances & unblock domains with new allowance */ for (d = dom_head; d; d = d->next) {Quoted 8 lines
tools/console/daemon/io.c void handle_io(void) · line 1027
Quoted 6 lines
tools/console/daemon/io.c +1
-5-/* Add 5ms of fuzz since select() often returns -a couple of ms sooner than requested. Without -the fuzz we typically do an extra spin in select() -with a 1/2 ms timeout every other iteration */ -if ((now+5 ) > d->next_period) { +if (now > d->next_period) {Is poll more accurate than select? I would have thought that they were based on the same timing, and thus equally "fuzzy"?
Quoted 2 lines
Is there any actual proof that the fuzz is needed? Specs of both
select()andpoll()don't seem to mention this behaviour at all.
That's a good question. I don't know. The tricky part with this sort of thing is that it may well depend on configurations, hardware differences, etc, so you may find that it works just fine on your test-box, but some big customer with Another-brand Co's servers don't work, because there is some subtle difference in hardware. Or it stops working if you have more than X number of CPU's. If you are convinced it's fine as it is, then by all means. I'm just thinking that it probably wasn't put there "by accident".
--
Mats
Quoted 16 lines
Quoted 10 lines
tools/console/daemon/utils.c line 42
Quoted 5 lines
tools/console/daemon/utils.c +1
-0#include "utils.h" struct xs_handle *xs; +struct pollfd *xs_pollfd;I don't see this used outside of
io.c- am I missing something?Adding more dependencies between different source files seems unnecessary...
Fixed.
Wei.
Re: [PATCH V4] Switch from select() to poll() in xenconsoled's IO loop
Quoted 18 lines · Mats Petersson
Quoted 15 lines · Wei Liu
Quoted 5 lines · Mats Petersson
Quoted 5 lines
return;tools/console/daemon/io.c +0
-0@@ -982,11 +1024,7 @@ void handle_io(void)/* Re-calculate any event counter allowances & unblock domains with new allowance */ for (d = dom_head; d; d = d->next) {Quoted 8 lines
tools/console/daemon/io.c void handle_io(void) · line 1027
Quoted 6 lines
tools/console/daemon/io.c +1
-5-/* Add 5ms of fuzz since select() often returns -a couple of ms sooner than requested. Without -the fuzz we typically do an extra spin in select() -with a 1/2 ms timeout every other iteration */ -if ((now+5 ) > d->next_period) { +if (now > d->next_period) {Is poll more accurate than select? I would have thought that they were based on the same timing, and thus equally "fuzzy"?
Quoted 2 lines
Is there any actual proof that the fuzz is needed? Specs of both
select()andpoll()don't seem to mention this behaviour at all.
Quoted 8 lines
That's a good question. I don't know. The tricky part with this sort of thing is that it may well depend on configurations, hardware differences, etc, so you may find that it works just fine on your test-box, but some big customer with Another-brand Co's servers don't work, because there is some subtle difference in hardware. Or it stops working if you have more than X number of CPU's. If you are convinced it's fine as it is, then by all means. I'm just thinking that it probably wasn't put there "by accident".
There's certainly an argument for removing it in a separate changeset though in case it does cause issues.
Ian
Re: [PATCH V4] Switch from select() to poll() in xenconsoled's IO loop
In reply to Wei LiuQuoted 16 lines · Wei Liu
Quoted 5 lines · Mats Petersson
Quoted 5 lines
return;tools/console/daemon/io.c +0
-0@@ -982,11 +1024,7 @@ void handle_io(void)/* Re-calculate any event counter allowances & unblock domains with new allowance */ for (d = dom_head; d; d = d->next) {Quoted 8 lines
tools/console/daemon/io.c void handle_io(void) · line 1027
Quoted 6 lines
tools/console/daemon/io.c +1
-5-/* Add 5ms of fuzz since select() often returns -a couple of ms sooner than requested. Without -the fuzz we typically do an extra spin in select() -with a 1/2 ms timeout every other iteration */ -if ((now+5 ) > d->next_period) { +if (now > d->next_period) {Is poll more accurate than select? I would have thought that they were based on the same timing, and thus equally "fuzzy"?
Quoted 2 lines
Is there any actual proof that the fuzz is needed? Specs of both
select()andpoll()don't seem to mention this behaviour at all.
I agree that it seems pretty dubious but it might be interesting to see what strace shows, specifically if it shows this extra spin every other iteration.
The fuzz was introduced in 16257:955ee4fa1345 "Rate-limit activity caused by each domU." but the commit log doesn't make any reference to the reason for it.
It may just have been a kernel bug at around the time that patch was authored, but google doesn't seem to show any evidence of such a bug ever being widespread (i.e. I don't find any references to it).
Ian.
Re: [PATCH V4] Switch from select() to poll() in xenconsoled's IO loop
Quoted 24 lines · Ian Campbell
Quoted 16 lines · Wei Liu
Quoted 5 lines · Mats Petersson
Quoted 5 lines
return;tools/console/daemon/io.c +0
-0@@ -982,11 +1024,7 @@ void handle_io(void)/* Re-calculate any event counter allowances & unblock domains with new allowance */ for (d = dom_head; d; d = d->next) {Quoted 8 lines
tools/console/daemon/io.c void handle_io(void) · line 1027
Quoted 6 lines
tools/console/daemon/io.c +1
-5-/* Add 5ms of fuzz since select() often returns -a couple of ms sooner than requested. Without -the fuzz we typically do an extra spin in select() -with a 1/2 ms timeout every other iteration */ -if ((now+5 ) > d->next_period) { +if (now > d->next_period) {Is poll more accurate than select? I would have thought that they were based on the same timing, and thus equally "fuzzy"?
Quoted 2 lines
Is there any actual proof that the fuzz is needed? Specs of both
select()andpoll()don't seem to mention this behaviour at all.I agree that it seems pretty dubious but it might be interesting to see what strace shows, specifically if it shows this extra spin every other iteration.
Quoted 8 lines
The fuzz was introduced in 16257:955ee4fa1345 "Rate-limit activity caused by each domU." but the commit log doesn't make any reference to the reason for it.
It may just have been a kernel bug at around the time that patch was authored, but google doesn't seem to show any evidence of such a bug ever being widespread (i.e. I don't find any references to it).
Then I will leave the fuzz here, and do another patch to remove it.
I will also add reference to CS 16257 in the comment.
Wei.