From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from eggs.gnu.org ([140.186.70.92]:57161) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1QKody-0003xY-Sl for qemu-devel@nongnu.org; Fri, 13 May 2011 05:26:47 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1QKodx-0003MW-Nr for qemu-devel@nongnu.org; Fri, 13 May 2011 05:26:46 -0400 Received: from mtagate4.uk.ibm.com ([194.196.100.164]:39552) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1QKodx-0003MB-FB for qemu-devel@nongnu.org; Fri, 13 May 2011 05:26:45 -0400 Received: from d06nrmr1307.portsmouth.uk.ibm.com (d06nrmr1307.portsmouth.uk.ibm.com [9.149.38.129]) by mtagate4.uk.ibm.com (8.13.1/8.13.1) with ESMTP id p4D9QiEM009109 for ; Fri, 13 May 2011 09:26:44 GMT Received: from d06av02.portsmouth.uk.ibm.com (d06av02.portsmouth.uk.ibm.com [9.149.37.228]) by d06nrmr1307.portsmouth.uk.ibm.com (8.13.8/8.13.8/NCO v10.0) with ESMTP id p4D9QfHi2142214 for ; Fri, 13 May 2011 10:26:43 +0100 Received: from d06av02.portsmouth.uk.ibm.com (loopback [127.0.0.1]) by d06av02.portsmouth.uk.ibm.com (8.14.4/8.13.1/NCO v10.0 AVout) with ESMTP id p4D9QetO013724 for ; Fri, 13 May 2011 03:26:40 -0600 From: Stefan Hajnoczi Date: Fri, 13 May 2011 10:26:32 +0100 Message-Id: <1305278792-20933-5-git-send-email-stefanha@linux.vnet.ibm.com> In-Reply-To: <1305278792-20933-1-git-send-email-stefanha@linux.vnet.ibm.com> References: <1305278792-20933-1-git-send-email-stefanha@linux.vnet.ibm.com> Subject: [Qemu-devel] [PATCH v3 4/4] coroutine: pool coroutines to speed up creation List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , To: qemu-devel@nongnu.org Cc: Kevin Wolf , Anthony Liguori , Stefan Hajnoczi , Blue Swirl , Paolo Bonzini 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 --- 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