Skip to contentSkip to conversation

coroutine: dataplane support

PATCH Applied Stefan Hajnoczi 4 participants 7 messages May 17, 2013 – May 23, 2013

lore
Share & tools
lore
#0 lore raw

cover coroutine: dataplane support

There is ongoing work to enable multiple event loop threads. This will allow QEMU itself to take advantage of SMP and reduce Big QEMU Lock (BQL) contention. This series is one step in that effort.

These patches make coroutines safe in a multi-event loop/multi-threaded world. I have successfully tested them running qcow2 in a dataplane thread (further patches are required which I'll be sending soon).

Patch 1 protects the global coroutine freelist with a lock :).

Patch 2 drops the CoQueue dependency on AioContext. This allows CoMutex and CoRwlock to operate in a dataplane thread whereas previously we always scheduled coroutines in the QEMU iothread.

Stefan Hajnoczi (2):
coroutine: protect global pool with a mutex
coroutine: stop using AioContext in CoQueue

 include/block/coroutine_int.h |  4 ++++
 qemu-coroutine-lock.c         | 56 ++++++++++++++++---------------------------
 qemu-coroutine.c              | 23 ++++++++++++++++--
 trace-events                  |  2 +-
 4 files changed, 47 insertions(+), 38 deletions(-)
Signature · 1 line
1.8.1.4
#1 lore raw

PATCH 1/2 coroutine: protect global pool with a mutex

The coroutine freelist is a global pool of unused coroutines. It avoids the setup/teardown overhead associated with the coroutine lifecycle. Since the pool is global, we need to synchronize access so that coroutines can be used outside the BQL.

---
 qemu-coroutine.c | 20 ++++++++++++++++++--
 1 file changed, 18 insertions(+), 2 deletions(-)
qemu-coroutine.c +18 -2
index 25a14c6..60ac79e 100644
@@ -14,6 +14,7 @@
 
 #include "trace.h"
 #include "qemu-common.h"
+#include "qemu/thread.h"
 #include "block/coroutine.h"
 #include "block/coroutine_int.h"
 
@@ -23,6 +24,7 @@ enum {
 };
 
 /** Free list to speed up creation */
+static QemuMutex pool_lock;
 static QSLIST_HEAD(, Coroutine) pool = QSLIST_HEAD_INITIALIZER(pool);
 static unsigned int pool_size;
 
@@ -30,11 +32,15 @@ Coroutine *qemu_coroutine_create(CoroutineEntry *entry)
 {
     Coroutine *co;
 
+    qemu_mutex_lock(&pool_lock);
     co = QSLIST_FIRST(&pool);
     if (co) {
         QSLIST_REMOVE_HEAD(&pool, pool_next);
         pool_size--;
-    } else {
+    }
+    qemu_mutex_unlock(&pool_lock);
+
+    if (!co) {
         co = qemu_coroutine_new();
     }
 
@@ -44,17 +50,25 @@ Coroutine *qemu_coroutine_create(CoroutineEntry *entry)
 
 static void coroutine_delete(Coroutine *co)
 {
+    qemu_mutex_lock(&pool_lock);
     if (pool_size < POOL_MAX_SIZE) {
         QSLIST_INSERT_HEAD(&pool, co, pool_next);
         co->caller = NULL;
         pool_size++;
+        qemu_mutex_unlock(&pool_lock);
         return;
     }
+    qemu_mutex_unlock(&pool_lock);
 
     qemu_coroutine_delete(co);
 }
 
-static void __attribute__((destructor)) coroutine_cleanup(void)
+static void __attribute__((constructor)) coroutine_pool_init(void)
+{
+    qemu_mutex_init(&pool_lock);
+}
+
+static void __attribute__((destructor)) coroutine_pool_cleanup(void)
 {
     Coroutine *co;
     Coroutine *tmp;
@@ -63,6 +77,8 @@ static void __attribute__((destructor)) coroutine_cleanup(void)
         QSLIST_REMOVE_HEAD(&pool, pool_next);
         qemu_coroutine_delete(co);
     }
+
+    qemu_mutex_destroy(&pool_lock);
 }
 
 static void coroutine_swap(Coroutine *from, Coroutine *to)
Signature · 1 line
1.8.1.4
#2 lore raw

PATCH 2/2 coroutine: stop using AioContext in CoQueue

In reply to Stefan Hajnoczi

qemu_co_queue_next(&queue) arranges that the next queued coroutine is run at a later point in time. This deferred restart is useful because the caller may not want to transfer control yet.

This behavior was implemented using QEMUBH in the past, which meant that CoQueue (and hence CoMutex and CoRwlock) had a dependency on the AioContext event loop. This hidden dependency causes trouble when we move to a world with multiple event loops - now qemu_co_queue_next() needs to know which event loop to schedule the QEMUBH in.

After pondering how to stash AioContext I realized the best solution is to not use AioContext at all. This patch implements the deferred restart behavior purely in terms of coroutines and no longer uses QEMUBH.

Here is how it works:

Each Coroutine has a wakeup queue that starts out empty. When qemu_co_queue_next() is called, the next coroutine is added to our wakeup queue. The wakeup queue is processed when we yield or terminate.

---
 include/block/coroutine_int.h |  4 ++++
 qemu-coroutine-lock.c         | 56 ++++++++++++++++---------------------------
 qemu-coroutine.c              |  3 +++
 trace-events                  |  2 +-
 4 files changed, 29 insertions(+), 36 deletions(-)
include/block/coroutine_int.h +4 -0
index 17eb71e..f133d65 100644
@@ -38,6 +38,9 @@ struct Coroutine {
     void *entry_arg;
     Coroutine *caller;
     QSLIST_ENTRY(Coroutine) pool_next;
+
+    /* Coroutines that should be woken up when we yield or terminate */
+    QTAILQ_HEAD(, Coroutine) co_queue_wakeup;
     QTAILQ_ENTRY(Coroutine) co_queue_next;
 };
 
@@ -45,5 +48,6 @@ Coroutine *qemu_coroutine_new(void);
 void qemu_coroutine_delete(Coroutine *co);
 CoroutineAction qemu_coroutine_switch(Coroutine *from, Coroutine *to,
                                       CoroutineAction action);
+void coroutine_fn qemu_co_queue_run_restart(Coroutine *co);
 
 #endif
qemu-coroutine-lock.c +21 -35

83 lines not on this page · raw message

qemu-coroutine.c +3 -0
index 60ac79e..423430d 100644
@@ -45,6 +45,7 @@ Coroutine *qemu_coroutine_create(CoroutineEntry *entry)
     }
 
     co->entry = entry;
+    QTAILQ_INIT(&co->co_queue_wakeup);
     return co;
 }
 
@@ -87,6 +88,8 @@ static void coroutine_swap(Coroutine *from, Coroutine *to)
 
     ret = qemu_coroutine_switch(from, to, COROUTINE_YIELD);
 
+    qemu_co_queue_run_restart(to);
+
     switch (ret) {
     case COROUTINE_YIELD:
         return;
trace-events +1 -1
index c03b9cb..389105c 100644
@@ -825,7 +825,7 @@ qemu_coroutine_yield(void *from, void *to) "from %p to %p"
 qemu_coroutine_terminate(void *co) "self %p"
 
 # qemu-coroutine-lock.c
-qemu_co_queue_next_bh(void) ""
+qemu_co_queue_run_restart(void *co) "co %p"
 qemu_co_queue_next(void *nxt) "next %p"
 qemu_co_mutex_lock_entry(void *mutex, void *self) "mutex %p self %p"
 qemu_co_mutex_lock_return(void *mutex, void *self) "mutex %p self %p"
Signature · 1 line
1.8.1.4
Reviewed-by on the v1 series
#3 lore raw
In reply to Stefan Hajnoczi

Am 17.05.2013 um 15:51 hat Stefan Hajnoczi geschrieben:

Quoted 18 lines

There is ongoing work to enable multiple event loop threads. This will allow QEMU itself to take advantage of SMP and reduce Big QEMU Lock (BQL) contention. This series is one step in that effort.

These patches make coroutines safe in a multi-event loop/multi-threaded world. I have successfully tested them running qcow2 in a dataplane thread (further patches are required which I'll be sending soon).

Patch 1 protects the global coroutine freelist with a lock :).

Patch 2 drops the CoQueue dependency on AioContext. This allows CoMutex and CoRwlock to operate in a dataplane thread whereas previously we always scheduled coroutines in the QEMU iothread.

Stefan Hajnoczi (2):
coroutine: protect global pool with a mutex
coroutine: stop using AioContext in CoQueue

Quoted 5 lines
 include/block/coroutine_int.h |  4 ++++
 qemu-coroutine-lock.c         | 56 ++++++++++++++++---------------------------
 qemu-coroutine.c              | 23 ++++++++++++++++--
 trace-events                  |  2 +-
 4 files changed, 47 insertions(+), 38 deletions(-)
#4 lore raw
In reply to Stefan Hajnoczi
Quoted 7 lines · Stefan Hajnoczi

There is ongoing work to enable multiple event loop threads. This will allow QEMU itself to take advantage of SMP and reduce Big QEMU Lock (BQL) contention. This series is one step in that effort.

These patches make coroutines safe in a multi-event loop/multi-threaded world. I have successfully tested them running qcow2 in a dataplane thread (further patches are required which I'll be sending soon).

Did you test the sigaltstack backend as well as ucontext? I know in theory they should be the same but still :-)

thanks
-- PMM

#5 lore raw
Quoted 11 lines · Peter Maydell
Quoted 7 lines · Stefan Hajnoczi

There is ongoing work to enable multiple event loop threads. This will allow QEMU itself to take advantage of SMP and reduce Big QEMU Lock (BQL) contention. This series is one step in that effort.

These patches make coroutines safe in a multi-event loop/multi-threaded world. I have successfully tested them running qcow2 in a dataplane thread (further patches are required which I'll be sending soon).

Did you test the sigaltstack backend as well as ucontext? I know in theory they should be the same but still :-)

Yes, I have tested sigaltstack successfully with make check and qemu-iotests.

Stefan

#6 lore raw
In reply to Stefan Hajnoczi
Quoted 18 lines · Stefan Hajnoczi

There is ongoing work to enable multiple event loop threads. This will allow QEMU itself to take advantage of SMP and reduce Big QEMU Lock (BQL) contention. This series is one step in that effort.

These patches make coroutines safe in a multi-event loop/multi-threaded world. I have successfully tested them running qcow2 in a dataplane thread (further patches are required which I'll be sending soon).

Patch 1 protects the global coroutine freelist with a lock :).

Patch 2 drops the CoQueue dependency on AioContext. This allows CoMutex and CoRwlock to operate in a dataplane thread whereas previously we always scheduled coroutines in the QEMU iothread.

Stefan Hajnoczi (2):
coroutine: protect global pool with a mutex
coroutine: stop using AioContext in CoQueue

Quoted 10 lines
 include/block/coroutine_int.h |  4 ++++
 qemu-coroutine-lock.c         | 56 ++++++++++++++++---------------------------
 qemu-coroutine.c              | 23 ++++++++++++++++--
 trace-events                  |  2 +-
 4 files changed, 47 insertions(+), 38 deletions(-)
Signature · 1 line
1.8.1.4

Applied to my block tree:
https://github.com/stefanha/qemu/commits/block

Stefan