qemu-devel.nongnu.org archive mirror
 help / color / mirror / Atom feed
From: Stefan Hajnoczi <stefanha@linux.vnet.ibm.com>
To: qemu-devel@nongnu.org
Cc: Kevin Wolf <kwolf@redhat.com>,
	Anthony Liguori <aliguori@us.ibm.com>,
	Stefan Hajnoczi <stefanha@linux.vnet.ibm.com>,
	Blue Swirl <blauwirbel@gmail.com>,
	Paolo Bonzini <pbonzini@redhat.com>
Subject: [Qemu-devel] [PATCH v3 4/4] coroutine: pool coroutines to speed up creation
Date: Fri, 13 May 2011 10:26:32 +0100	[thread overview]
Message-ID: <1305278792-20933-5-git-send-email-stefanha@linux.vnet.ibm.com> (raw)
In-Reply-To: <1305278792-20933-1-git-send-email-stefanha@linux.vnet.ibm.com>

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 <stefanha@linux.vnet.ibm.com>
---
 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

  parent reply	other threads:[~2011-05-13  9:26 UTC|newest]

Thread overview: 9+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2011-05-13  9:26 [Qemu-devel] [PATCH v3 0/4] Coroutines for better asynchronous programming Stefan Hajnoczi
2011-05-13  9:26 ` [Qemu-devel] [PATCH v3 1/4] coroutine: introduce coroutines Stefan Hajnoczi
2011-05-13 10:32   ` Paolo Bonzini
2011-05-13 11:19     ` Stefan Hajnoczi
2011-05-13  9:26 ` [Qemu-devel] [PATCH v3 2/4] coroutine: add check-coroutine automated tests Stefan Hajnoczi
2011-05-13  9:26 ` [Qemu-devel] [PATCH v3 3/4] coroutine: add check-coroutine --benchmark-lifecycle Stefan Hajnoczi
2011-05-13  9:26 ` Stefan Hajnoczi [this message]
2011-05-14  6:55 ` [Qemu-devel] [PATCH v3 0/4] Coroutines for better asynchronous programming Corentin Chary
2011-05-14  7:45   ` Stefan Hajnoczi

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=1305278792-20933-5-git-send-email-stefanha@linux.vnet.ibm.com \
    --to=stefanha@linux.vnet.ibm.com \
    --cc=aliguori@us.ibm.com \
    --cc=blauwirbel@gmail.com \
    --cc=kwolf@redhat.com \
    --cc=pbonzini@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).