ia64/xen-unstable

changeset 8256:2b8efe11096b

Add a "consumed" parameter to the buffer declaration, and use this to advance
through the buffer when the data are read by console clients. This avoids a
costly memmove with every read from the console.

Signed-off-by: Ewan Mellor <ewan@xensource.com>
author emellor@leeni.uk.xensource.com
date Tue Dec 06 17:10:59 2005 +0000 (2005-12-06)
parents 06b80b837c92
children 0faff739723e
files tools/console/daemon/io.c
line diff
     1.1 --- a/tools/console/daemon/io.c	Tue Dec 06 16:40:50 2005 +0000
     1.2 +++ b/tools/console/daemon/io.c	Tue Dec 06 17:10:59 2005 +0000
     1.3 @@ -48,6 +48,7 @@
     1.4  struct buffer
     1.5  {
     1.6  	char *data;
     1.7 +	size_t consumed;
     1.8  	size_t size;
     1.9  	size_t capacity;
    1.10  	size_t max_capacity;
    1.11 @@ -109,12 +110,17 @@ static void buffer_append(struct domain 
    1.12  
    1.13  	if (buffer->max_capacity &&
    1.14  	    buffer->size > buffer->max_capacity) {
    1.15 -		memmove(buffer->data + (buffer->size -
    1.16 -					buffer->max_capacity),
    1.17 -			buffer->data, buffer->max_capacity);
    1.18 -		buffer->data = realloc(buffer->data,
    1.19 -				       buffer->max_capacity);
    1.20 +		/* Discard the middle of the data. */
    1.21 +
    1.22 +		size_t over = buffer->size - buffer->max_capacity;
    1.23 +		char *maxpos = buffer->data + buffer->max_capacity;
    1.24 +
    1.25 +		memmove(maxpos - over, maxpos, over);
    1.26 +		buffer->data = realloc(buffer->data, buffer->max_capacity);
    1.27  		buffer->size = buffer->capacity = buffer->max_capacity;
    1.28 +
    1.29 +		if (buffer->consumed > buffer->max_capacity - over)
    1.30 +			buffer->consumed = buffer->max_capacity - over;
    1.31  	}
    1.32  }
    1.33  
    1.34 @@ -123,11 +129,13 @@ static bool buffer_empty(struct buffer *
    1.35  	return buffer->size == 0;
    1.36  }
    1.37  
    1.38 -static void buffer_advance(struct buffer *buffer, size_t size)
    1.39 +static void buffer_advance(struct buffer *buffer, size_t len)
    1.40  {
    1.41 -	size = MIN(size, buffer->size);
    1.42 -	memmove(buffer->data, buffer + size, buffer->size - size);
    1.43 -	buffer->size -= size;
    1.44 +	buffer->consumed += len;
    1.45 +	if (buffer->consumed == buffer->size) {
    1.46 +		buffer->consumed = 0;
    1.47 +		buffer->size = 0;
    1.48 +	}
    1.49  }
    1.50  
    1.51  static bool domain_is_valid(int domid)
    1.52 @@ -327,6 +335,7 @@ static struct domain *create_domain(int 
    1.53  	dom->tty_fd = -1;
    1.54  	dom->is_dead = false;
    1.55  	dom->buffer.data = 0;
    1.56 +	dom->buffer.consumed = 0;
    1.57  	dom->buffer.size = 0;
    1.58  	dom->buffer.capacity = 0;
    1.59  	dom->buffer.max_capacity = 0;
    1.60 @@ -474,8 +483,12 @@ static void handle_tty_write(struct doma
    1.61  {
    1.62  	ssize_t len;
    1.63  
    1.64 -	len = write(dom->tty_fd, dom->buffer.data, dom->buffer.size);
    1.65 -	if (len < 1) {
    1.66 +	len = write(dom->tty_fd, dom->buffer.data + dom->buffer.consumed,
    1.67 +		    dom->buffer.size - dom->buffer.consumed);
    1.68 + 	if (len < 1) {
    1.69 +		dolog(LOG_DEBUG, "Write failed on domain %d: %d, %d\n",
    1.70 +		      dom->domid, len, errno);
    1.71 +
    1.72  		close(dom->tty_fd);
    1.73  		dom->tty_fd = -1;
    1.74