From patchwork Fri May 13 09:26:32 2011 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Stefan Hajnoczi X-Patchwork-Id: 95448 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Received: from lists.gnu.org (lists.gnu.org [140.186.70.17]) (using TLSv1 with cipher AES256-SHA (256/256 bits)) (Client did not present a certificate) by ozlabs.org (Postfix) with ESMTPS id 0825CB6F00 for ; Fri, 13 May 2011 19:27:03 +1000 (EST) Received: from localhost ([::1]:37894 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1QKoeC-0003xi-AZ for incoming@patchwork.ozlabs.org; Fri, 13 May 2011 05:27:00 -0400 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 Received: from stefanha-thinkpad.manchester-maybrook.uk.ibm.com (dyn-9-174-219-27.manchester-maybrook.uk.ibm.com [9.174.219.27]) by d06av02.portsmouth.uk.ibm.com (8.14.4/8.13.1/NCO v10.0 AVin) with ESMTP id p4D9QcnZ013654; Fri, 13 May 2011 03:26:40 -0600 From: Stefan Hajnoczi To: Date: Fri, 13 May 2011 10:26:32 +0100 Message-Id: <1305278792-20933-5-git-send-email-stefanha@linux.vnet.ibm.com> X-Mailer: git-send-email 1.7.4.4 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> X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.6, seldom 2.4 (older, 4) X-Received-From: 194.196.100.164 Cc: Kevin Wolf , Anthony Liguori , Stefan Hajnoczi , Blue Swirl , Paolo Bonzini Subject: [Qemu-devel] [PATCH v3 4/4] coroutine: pool coroutines to speed up creation X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.14 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org Sender: qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org 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;