From: Peter Lieven <pl@kamp.de>
To: qemu-devel@nongnu.org
Cc: Peter Lieven <pl@kamp.de>, kraxel@redhat.com
Subject: [Qemu-devel] [PATCH 1/2] io/buffer: allow a buffer to shrink gracefully
Date: Tue, 3 Nov 2015 10:01:15 +0100 [thread overview]
Message-ID: <1446541276-32467-2-git-send-email-pl@kamp.de> (raw)
In-Reply-To: <1446541276-32467-1-git-send-email-pl@kamp.de>
the idea behind this patch is to allow the buffer to shrink, but
make this a seldom operation. The buffers average size is measured
exponentionally smoothed with am alpha of 1/128.
Signed-off-by: Peter Lieven <pl@kamp.de>
---
include/io/buffer.h | 1 +
io/buffer.c | 42 +++++++++++++++++++++++++++++++++---------
2 files changed, 34 insertions(+), 9 deletions(-)
diff --git a/include/io/buffer.h b/include/io/buffer.h
index f6668cb..f63869e 100644
--- a/include/io/buffer.h
+++ b/include/io/buffer.h
@@ -37,6 +37,7 @@ struct QIOBuffer {
char *name;
size_t capacity;
size_t offset;
+ uint64_t avg_size;
uint8_t *buffer;
};
diff --git a/io/buffer.c b/io/buffer.c
index 0fd3cea..d2a6043 100644
--- a/io/buffer.c
+++ b/io/buffer.c
@@ -23,6 +23,10 @@
#define QIO_BUFFER_MIN_INIT_SIZE 4096
#define QIO_BUFFER_MIN_SHRINK_SIZE 65536
+/* define the factor alpha for the expentional smoothing
+ * that is used in the average size calculation. a shift
+ * of 7 results in an alpha of 1/2^7. */
+#define QIO_BUFFER_AVG_SIZE_SHIFT 7
static size_t buf_req_size(QIOBuffer *buffer, size_t len)
{
@@ -37,6 +41,11 @@ static void buf_adj_size(QIOBuffer *buffer, size_t len)
buffer->buffer = g_realloc(buffer->buffer, buffer->capacity);
trace_qio_buffer_resize(buffer->name ?: "unnamed",
old, buffer->capacity);
+
+ /* make it even harder for the buffer to shrink, reset average size
+ * to currenty capacity if it is larger than the average. */
+ buffer->avg_size = MAX(buffer->avg_size,
+ buffer->capacity << QIO_BUFFER_AVG_SIZE_SHIFT);
}
void qio_buffer_init(QIOBuffer *buffer, const char *name, ...)
@@ -48,21 +57,34 @@ void qio_buffer_init(QIOBuffer *buffer, const char *name, ...)
va_end(ap);
}
-void qio_buffer_shrink(QIOBuffer *buffer)
+static uint64_t get_buf_avg_size(QIOBuffer *buffer)
{
- /*
- * Only shrink in case the used size is *much* smaller than the
- * capacity, to avoid bumping up & down the buffers all the time.
+ return buffer->avg_size >> QIO_BUFFER_AVG_SIZE_SHIFT;
+}
+
+void qio_buffer_shrink(QIOBuffer *buffer)
+ {
+ size_t new;
+
+ /* Calculate the average size of the buffer as
+ * avg_size = avg_size * ( 1 - a ) + required_size * a
+ * where a is 1 / 2 ^ QIO_BUFFER_AVG_SIZE_SHIFT. */
+ buffer->avg_size *= (1 << QIO_BUFFER_AVG_SIZE_SHIFT) - 1;
+ buffer->avg_size >>= QIO_BUFFER_AVG_SIZE_SHIFT;
+ buffer->avg_size += buf_req_size(buffer, 0);
+
+ /* And then only shrink if the average size of the buffer is much
+ * too big, to avoid bumping up & down the buffers all the time.
* realloc() isn't exactly cheap ...
*/
- if (buffer->offset < (buffer->capacity >> 3) &&
- buffer->capacity > QIO_BUFFER_MIN_SHRINK_SIZE) {
- return;
+ new = buf_req_size(buffer, get_buf_avg_size(buffer));
+ if (new < buffer->capacity >> 3 &&
+ new >= QIO_BUFFER_MIN_SHRINK_SIZE) {
+ buf_adj_size(buffer, get_buf_avg_size(buffer));
}
-
- buf_adj_size(buffer, 0);
}
+
void qio_buffer_reserve(QIOBuffer *buffer, size_t len)
{
if ((buffer->capacity - buffer->offset) < len) {
@@ -83,6 +105,7 @@ uint8_t *qio_buffer_end(QIOBuffer *buffer)
void qio_buffer_reset(QIOBuffer *buffer)
{
buffer->offset = 0;
+ qio_buffer_shrink(buffer);
}
void qio_buffer_free(QIOBuffer *buffer)
@@ -107,6 +130,7 @@ void qio_buffer_advance(QIOBuffer *buffer, size_t len)
memmove(buffer->buffer, buffer->buffer + len,
(buffer->offset - len));
buffer->offset -= len;
+ qio_buffer_shrink(buffer);
}
void qio_buffer_move_empty(QIOBuffer *to, QIOBuffer *from)
--
1.9.1
next prev parent reply other threads:[~2015-11-03 9:01 UTC|newest]
Thread overview: 8+ messages / expand[flat|nested] mbox.gz Atom feed top
2015-11-03 9:01 [Qemu-devel] [PATCH 0/2] vnc buffer enhancements for 2.5 Peter Lieven
2015-11-03 9:01 ` Peter Lieven [this message]
2015-11-03 10:40 ` [Qemu-devel] [PATCH 1/2] io/buffer: allow a buffer to shrink gracefully Gerd Hoffmann
2015-11-05 22:09 ` Peter Lieven
2015-11-03 9:01 ` [Qemu-devel] [PATCH 2/2] io/buffer: avoid memmove at each qio_buffer_advance Peter Lieven
2015-11-03 10:52 ` Gerd Hoffmann
2015-11-03 12:20 ` Peter Lieven
2015-11-03 11:17 ` Markus Armbruster
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=1446541276-32467-2-git-send-email-pl@kamp.de \
--to=pl@kamp.de \
--cc=kraxel@redhat.com \
--cc=qemu-devel@nongnu.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).