Skip to contentSkip to conversation

WIP: add GCoroutine support

PATCH In review Marc-André Lureau 4 participants 6 messages Jan 9, 2015 – Jan 22, 2015 Kbuild

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 WIP: add GCoroutine support

Learn to use the GCoroutine library instead of qemu own coroutine implementation.

GCoroutine is hosted on github:
https://github.com/elmarco/gcoroutine

This allows to share the same coroutine implementation between various projects (gtk-vnc and spice-gtk). It is related to the effort to push coroutine support in GLib. See also:
https://bugzilla.gnome.org/show_bug.cgi?id=719362

Notes:
- there are no GCoroutine releases, the API isn't frozen
- this patch hasn't been thoroughly tested
- GCoroutine doesn't implement pools yet
- GCoroutine is missing sigaltstack based coroutines
- spice-gtk and gtk-vnc patches are being worked on

---
 Makefile.objs             |  11 +++-
 configure                 |  26 +++++++-
 coroutine-gcoroutine.c    | 154 ++++++++++++++++++++++++++++++++++++++++++++++
 include/block/coroutine.h |  15 +++++
 4 files changed, 201 insertions(+), 5 deletions(-)
 create mode 100644 coroutine-gcoroutine.c
Makefile.objs +9 -2
index abeb902..250c58a 100644
@@ -14,10 +14,17 @@ block-obj-$(CONFIG_WIN32) += aio-win32.o
 block-obj-y += block/
 block-obj-y += qemu-io-cmds.o
 
-block-obj-y += qemu-coroutine.o qemu-coroutine-lock.o qemu-coroutine-io.o
-block-obj-y += qemu-coroutine-sleep.o
 block-obj-y += coroutine-$(CONFIG_COROUTINE_BACKEND).o
 
+ifeq ($(CONFIG_COROUTINE_BACKEND),gcoroutine)
+coroutine-gcoroutine.o-cflags := $(GCOROUTINE_CFLAGS)
+coroutine-gcoroutine.o-libs := $(GCOROUTINE_LIBS)
+else
+block-obj-y += qemu-coroutine.o qemu-coroutine-lock.o
+endif
+block-obj-y += qemu-coroutine-io.o
+block-obj-y += qemu-coroutine-sleep.o
+
 block-obj-m = block/
 
 
configure +23 -3
index cae588c..e36ee74 100755
@@ -1381,7 +1381,7 @@ Advanced options (experts only):
   --disable-seccomp        disable seccomp support
   --enable-seccomp         enable seccomp support
   --with-coroutine=BACKEND coroutine backend. Supported options:
-                           gthread, ucontext, sigaltstack, windows
+                           gcoroutine, gthread, ucontext, sigaltstack, windows
   --disable-coroutine-pool disable coroutine freelist (worse performance)
   --enable-coroutine-pool  enable coroutine freelist (better performance)
   --enable-glusterfs       enable GlusterFS backend
@@ -3870,8 +3870,16 @@ EOF
   fi
 fi
 
+if $pkg_config gcoroutine-1.0 --exists; then
+  gcoroutine_cflags=`$pkg_config --cflags gcoroutine-1.0`
+  gcoroutine_libs=`$pkg_config --libs gcoroutine-1.0`
+  gcoroutine=yes
+fi
+
 if test "$coroutine" = ""; then
-  if test "$mingw32" = "yes"; then
+  if test "$gcoroutine" = "yes"; then
+    coroutine=gcoroutine
+  elif test "$mingw32" = "yes"; then
     coroutine=win32
   elif test "$ucontext_works" = "yes"; then
     coroutine=ucontext
@@ -3898,6 +3906,11 @@ else
       error_exit "only the 'windows' coroutine backend is valid for Windows"
     fi
     ;;
+  gcoroutine)
+    if test "$gcoroutine" != "yes"; then
+      feature_not_found "gcoroutine"
+    fi
+    ;;
   *)
     error_exit "unknown coroutine backend $coroutine"
     ;;
@@ -4213,7 +4226,7 @@ EOF
 fi
 
 # prepend pixman and ftd flags after all config tests are done
-QEMU_CFLAGS="$pixman_cflags $fdt_cflags $QEMU_CFLAGS"
+QEMU_CFLAGS="$pixman_cflags $fdt_cflags $gcoroutine_cflags $QEMU_CFLAGS"
 libs_softmmu="$pixman_libs $libs_softmmu"
 
 echo "Install prefix    $prefix"
@@ -4733,6 +4746,13 @@ else
   echo "CONFIG_COROUTINE_POOL=0" >> $config_host_mak
 fi
 
+if test "$coroutine" = "gcoroutine" ; then
+  echo "CONFIG_GCOROUTINE=1" >> $config_host_mak
+  echo "GCOROUTINE_LIBS=$gcoroutine_libs" >> $config_host_mak
+  echo "GCOROUTINE_CFLAGS=$gcoroutine_cflags" >> $config_host_mak
+fi
+
+
 if test "$open_by_handle_at" = "yes" ; then
   echo "CONFIG_OPEN_BY_HANDLE=y" >> $config_host_mak
 fi
coroutine-gcoroutine.c new file +154 -0

154 lines not on this page · raw message

include/block/coroutine.h +15 -0
index 793df0e..cb1fbfc 100644
@@ -19,6 +19,9 @@
 #include "qemu/typedefs.h"
 #include "qemu/queue.h"
 #include "qemu/timer.h"
+#ifdef CONFIG_GCOROUTINE
+#include <gcoroutine.h>
+#endif
 
 /**
  * Coroutines are a mechanism for stack switching and can be used for
@@ -103,9 +106,13 @@ bool qemu_in_coroutine(void);
  * them later. They provide the fundamental primitives on which coroutine locks
  * are built.
  */
+#ifdef CONFIG_GCOROUTINE
+typedef GCoQueue CoQueue;
+#else
 typedef struct CoQueue {
     QTAILQ_HEAD(, Coroutine) entries;
 } CoQueue;
+#endif
 
 /**
  * Initialise a CoQueue. This must be called before any other operation is used
@@ -145,10 +152,14 @@ bool qemu_co_queue_empty(CoQueue *queue);
 /**
  * Provides a mutex that can be used to synchronise coroutines
  */
+#ifdef CONFIG_GCOROUTINE
+typedef GCoMutex CoMutex;
+#else
 typedef struct CoMutex {
     bool locked;
     CoQueue queue;
 } CoMutex;
+#endif
 
 /**
  * Initialises a CoMutex. This must be called before any other operation is used
@@ -168,11 +179,15 @@ void coroutine_fn qemu_co_mutex_lock(CoMutex *mutex);
  */
 void coroutine_fn qemu_co_mutex_unlock(CoMutex *mutex);
 
+#ifdef CONFIG_GCOROUTINE
+typedef GCoRWLock CoRwlock;
+#else
 typedef struct CoRwlock {
     bool writer;
     int reader;
     CoQueue queue;
 } CoRwlock;
+#endif
 
 /**
  * Initialises a CoRwlock. This must be called before any other operation
Signature · 1 line
2.1.0
#1 lore raw
Quoted 11 lines · Marc-André Lureau

Learn to use the GCoroutine library instead of qemu own coroutine implementation.

GCoroutine is hosted on github:
https://github.com/elmarco/gcoroutine

This allows to share the same coroutine implementation between various projects (gtk-vnc and spice-gtk). It is related to the effort to push coroutine support in GLib. See also:
https://bugzilla.gnome.org/show_bug.cgi?id=719362

Quoted 6 lines

Notes:
- there are no GCoroutine releases, the API isn't frozen
- this patch hasn't been thoroughly tested
- GCoroutine doesn't implement pools yet
- GCoroutine is missing sigaltstack based coroutines
- spice-gtk and gtk-vnc patches are being worked on

It's bad enough having four coroutine backends without adding yet another one :-( I'm not sure we should take this patch unless/until GCoroutine is fully implemented and supported in a common enough version of glib that we could delete at least one of our other backends in favour of this...

-- PMM

#2 lore raw
Quoted 20 lines · Peter Maydell
Quoted 11 lines · Marc-André Lureau

Learn to use the GCoroutine library instead of qemu own coroutine implementation.

GCoroutine is hosted on github:
https://github.com/elmarco/gcoroutine

This allows to share the same coroutine implementation between various projects (gtk-vnc and spice-gtk). It is related to the effort to push coroutine support in GLib. See also:
https://bugzilla.gnome.org/show_bug.cgi?id=719362

Quoted 6 lines

Notes:
- there are no GCoroutine releases, the API isn't frozen
- this patch hasn't been thoroughly tested
- GCoroutine doesn't implement pools yet
- GCoroutine is missing sigaltstack based coroutines
- spice-gtk and gtk-vnc patches are being worked on

Quoted 5 lines

It's bad enough having four coroutine backends without adding yet another one :-( I'm not sure we should take this patch unless/until GCoroutine is fully implemented and supported in a common enough version of glib that we could delete at least one of our other backends in favour of this...

It's an RFC. I see this more of a way to collaborate on GCoroutine than something that needs to be merged into qemu.git today.

Stefan

#3 lore raw
In reply to Marc-André Lureau
Quoted 11 lines · Marc-André Lureau

Learn to use the GCoroutine library instead of qemu own coroutine implementation.

GCoroutine is hosted on github:
https://github.com/elmarco/gcoroutine

This allows to share the same coroutine implementation between various projects (gtk-vnc and spice-gtk). It is related to the effort to push coroutine support in GLib. See also:
https://bugzilla.gnome.org/show_bug.cgi?id=719362

Quoted 6 lines

Notes:
- there are no GCoroutine releases, the API isn't frozen
- this patch hasn't been thoroughly tested
- GCoroutine doesn't implement pools yet
- GCoroutine is missing sigaltstack based coroutines
- spice-gtk and gtk-vnc patches are being worked on

The GCoroutine API has a very direct mapping to QEMU's coroutine interface:
https://github.com/elmarco/gcoroutine/blob/master/src/gcoroutine.h

I like that you added gpointer arguments and return values to enter/yield. When writing the QEMU coroutine interface I dropped them because I felt we wouldn't need them, but they are appropriate in a general-purpose coroutine library.

Kevin Lieven and Peter Lieven are currently measuring and improving coroutine performance in the QEMU block layer. I guess this will result in code changes to QEMU's coroutines implementation over the next weeks.

Stefan

#4 lore raw
In reply to Marc-André Lureau

Am 09.01.2015 um 18:19 hat Marc-André Lureau geschrieben:

Quoted 10 lines

Learn to use the GCoroutine library instead of qemu own coroutine implementation.

GCoroutine is hosted on github:
https://github.com/elmarco/gcoroutine

This allows to share the same coroutine implementation between various projects (gtk-vnc and spice-gtk). It is related to the effort to push coroutine support in GLib. See also:
https://bugzilla.gnome.org/show_bug.cgi?id=719362

Hm, I guess there might be uses for holding references after the coroutine has exited if there are external references to the coroutine, but is it useful not to drop one reference after the coroutine exits? I guess in most cases, and in qemu always, the caller isn't aware that the coroutine ended, so requiring an explicit call isn't as nice as it could be.

Also, you modified the copyright lines. Would you mind restoring the author names as in the original code, as their license requires? I see that you also converted MIT licensed code into LGPL, without keeping the original license text as required. For my code, I'm fine with the conversion, but you should either have asked or left the original text in place.

Kevin

#5 lore raw
Quoted 6 lines · Kevin Wolf

Hm, I guess there might be uses for holding references after the coroutine has exited if there are external references to the coroutine, but is it useful not to drop one reference after the coroutine exits? I guess in most cases, and in qemu always, the caller isn't aware that the coroutine ended, so requiring an explicit call isn't as nice as it could be.

I thought it would be better to make memory management explicit by the caller. However, I can also see how inconvenient it is for fire and forget cases. Unfortunately, the coroutine can't ref/unref() itself at beginning and end since the "unfinished" warning would show up, also it feels wrong to unref itself inside the coroutine. So I changed it to take an implicit ref on resume (similar to g_task_run_in_thread for ex) and unref automatically when the coroutine finishes. So unlike qemu-coroutine, you still have a strong reference returned, and must unref it at some point. This should be similar to GThread/GTask I think.

Fire and forget fix:
https://github.com/elmarco/gcoroutine/commit/7c88e1f90bb812481883da512a5d7d3b1e7d6028

Copyright fix:
https://github.com/elmarco/gcoroutine/commit/4a513d92e1377a5cd3526eeb8ed974f0a3221636

Signature · 1 line
Marc-André Lureau