[PATCH v2] TTY: tty flip buffer optimisation.

From: Ilya Zykov
Date: Thu Oct 27 2011 - 05:48:53 EST


Currently, free flip buffer (tty->buf.free) reserve memory for further used,
only if driver send to ldisc less 257 bytes in one time.
If driver send more, flip buffer reserve(kmalloc()) and then
free(kfree()) every chunk more 256 bytes every time.

For avoiding useless looking up buffer more then 256 byte size.

diff -uprN -X ../../dontdiff a/drivers/tty/tty_buffer.c b/drivers/tty/tty_buffer.c
--- a/drivers/tty/tty_buffer.c 2011-05-19 08:06:34.000000000 +0400
+++ b/drivers/tty/tty_buffer.c 2011-10-27 13:37:33.000000000 +0400
@@ -91,7 +91,7 @@ static void tty_buffer_free(struct tty_s
tty->buf.memory_used -= b->size;
WARN_ON(tty->buf.memory_used < 0);

- if (b->size >= 512)
+ if (b->size ^ 0x100)
kfree(b);
else {
b->next = tty->buf.free;
@@ -156,34 +156,27 @@ void tty_buffer_flush(struct tty_struct
* @tty: tty owning the buffer
* @size: characters wanted
*
- * Locate an existing suitable tty buffer or if we are lacking one then
- * allocate a new one. We round our buffers off in 256 character chunks
- * to get better allocation behaviour.
+ * Locate an existing suitable tty buffer 256 byte size.
+ * If we are lacking one or size more 256 then allocate a new one.
*
* Locking: Caller must hold tty->buf.lock
*/

static struct tty_buffer *tty_buffer_find(struct tty_struct *tty, size_t size)
{
- struct tty_buffer **tbh = &tty->buf.free;
- while ((*tbh) != NULL) {
- struct tty_buffer *t = *tbh;
- if (t->size >= size) {
- *tbh = t->next;
- t->next = NULL;
- t->used = 0;
- t->commit = 0;
- t->read = 0;
- tty->buf.memory_used += t->size;
- return t;
- }
- tbh = &((*tbh)->next);
+ struct tty_buffer *ret;
+ if (size <= 256 && tty->buf.free != NULL) {
+ ret = tty->buf.free;
+ tty->buf.free = ret->next;
+ ret->next = NULL;
+ ret->used = 0;
+ ret->commit = 0;
+ ret->read = 0;
+ tty->buf.memory_used += 256;
+ } else {
+ ret = tty_buffer_alloc(tty, max_t(size_t, size, 256));
}
- /* Round the buffer size out */
- size = (size + 0xFF) & ~0xFF;
- return tty_buffer_alloc(tty, size);
- /* Should possibly check if this fails for the largest buffer we
- have queued and recycle that ? */
+ return ret;
}

/**
diff -uprN -X ../../dontdiff a/include/linux/tty.h b/include/linux/tty.h
--- a/include/linux/tty.h 2011-05-19 08:06:34.000000000 +0400
+++ b/include/linux/tty.h 2011-10-27 13:37:46.000000000 +0400
@@ -78,7 +78,7 @@ struct tty_buffer {
* logic this must match
*/

-#define TTY_BUFFER_PAGE (((PAGE_SIZE - sizeof(struct tty_buffer)) / 2) & ~0xFF)
+#define TTY_BUFFER_PAGE ((PAGE_SIZE - sizeof(struct tty_buffer)) / 2)


struct tty_bufhead {
Signed-off-by: Ilya Zykov <ilya@xxxxxxx>
--
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in
the body of a message to majordomo@xxxxxxxxxxxxxxx
More majordomo info at http://vger.kernel.org/majordomo-info.html
Please read the FAQ at http://www.tux.org/lkml/