[Top][All Lists]
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[Qemu-devel] [PATCH v3 4/4] coroutine: pool coroutines to speed up creat
From: |
Stefan Hajnoczi |
Subject: |
[Qemu-devel] [PATCH v3 4/4] coroutine: pool coroutines to speed up creation |
Date: |
Fri, 13 May 2011 10:26:32 +0100 |
This patch speeds up coroutine creation by reusing freed coroutines.
When a coroutine terminates it is placed in the pool instead of having
its resources freed. The next time a coroutine is created it can be
taken straight from the pool and requires no initialization.
Performance results on an Intel Core2 Duo T9400 (2.53GHz) for
./check-coroutine --benchmark-lifecycle 20000000:
No pooling: 19.5 sec
With pooling: 1.1 sec
Signed-off-by: Stefan Hajnoczi <address@hidden>
---
qemu-coroutine-int.h | 2 ++
qemu-coroutine.c | 33 ++++++++++++++++++++++++++++++---
2 files changed, 32 insertions(+), 3 deletions(-)
diff --git a/qemu-coroutine-int.h b/qemu-coroutine-int.h
index 71c6ee9..b264881 100644
--- a/qemu-coroutine-int.h
+++ b/qemu-coroutine-int.h
@@ -45,6 +45,8 @@ struct Coroutine {
void *data;
CoroutineEntry *entry;
+ QLIST_ENTRY(Coroutine) pool_next;
+
jmp_buf env;
};
diff --git a/qemu-coroutine.c b/qemu-coroutine.c
index a80468b..0f4e58f 100644
--- a/qemu-coroutine.c
+++ b/qemu-coroutine.c
@@ -24,16 +24,35 @@
#include "qemu-coroutine.h"
#include "qemu-coroutine-int.h"
+enum {
+ /* Maximum free pool size prevents holding too many freed coroutines */
+ POOL_MAX_SIZE = 64,
+};
+
+static QLIST_HEAD(, Coroutine) pool = QLIST_HEAD_INITIALIZER(&pool);
+static unsigned int pool_size;
static Coroutine leader;
static Coroutine *current;
-static void coroutine_terminate(Coroutine *co)
+static void coroutine_delete(Coroutine *co)
{
- trace_qemu_coroutine_terminate(co);
qemu_free(co->stack);
qemu_free(co);
}
+static void coroutine_terminate(Coroutine *co)
+{
+ trace_qemu_coroutine_terminate(co);
+
+ if (pool_size < POOL_MAX_SIZE) {
+ QLIST_INSERT_HEAD(&pool, co, pool_next);
+ co->caller = NULL;
+ pool_size++;
+ } else {
+ coroutine_delete(co);
+ }
+}
+
static Coroutine *coroutine_new(void)
{
const size_t stack_size = 4 << 20;
@@ -49,7 +68,15 @@ Coroutine *qemu_coroutine_create(CoroutineEntry *entry)
{
Coroutine *co;
- co = coroutine_new();
+ co = QLIST_FIRST(&pool);
+
+ if (co) {
+ QLIST_REMOVE(co, pool_next);
+ pool_size--;
+ } else {
+ co = coroutine_new();
+ }
+
co->entry = entry;
return co;
--
1.7.4.4