Coroutines for better asynchronous programming
v8 cover Coroutines for better asynchronous programming
QEMU is event-driven and suffers when blocking operations are performed because VM execution may be stopped until the operation completes. Therefore many operations that could block are performed asynchronously and a callback is invoked when the operation has completed. This allows QEMU to continue executing while the operation is pending.
The downside to callbacks is that they split up code into many smaller functions, each of which is a single step in a state machine that quickly becomes complex and hard to understand. Callback functions also result in lots of noise as variables are packed and unpacked into temporary structs that pass state to the callback function.
This patch series introduces coroutines as a solution for writing asynchronous code while still having a nice sequential control flow. The semantics are explained in the second patch. The fourth patch adds automated tests.
A nice feature of coroutines is that it is relatively easy to take synchronous code and lift it into a coroutine to make it asynchronous. Work has been done to move qcow2 request processing into coroutines and thereby make it asynchronous (today qcow2 will perform synchronous metadata accesses). This qcow2 work is still ongoing and not quite ready for mainline yet.
v8:
* Bisectability: introduce gthread implementation before ucontext/fibers
v7:
* Reduce ucontext and win32 fiber stack size to 1 MB
* Add qemu-timer-common.o to test-coroutine dependencies for OpenBSD build
v6:
* Use GThread on Mac OS X, fix from Andreas Färber <[email protected]>
* abort(3) if coroutine-ucontext.c fails to create a coroutine
v5:
* GThread-based implementation for platforms without makecontext(3) (Aneesh Kumar K.V <[email protected]>)
* Switch to gtester test framework
v4:
* Windows Fibers support (Paolo Bonzini <[email protected]>)
* Return-after-setjmp() fix (Aneesh Kumar K.V <[email protected]>)
* Re-entrancy for multi-threaded coroutines support
* qemu-coroutine.h cleanup and documentation
v3:
* Updated LGPL v2 license header to use web link
* Removed atexit(3) pool freeing
* Removed thread-local current/leader
* Documented thread-safety limitation
* Disabled trace events
v2:
* Added ./check-coroutine --lifecycle-benchmark for performance measurement
* Split pooling into a separate patch with performance justification
* Set maximum pool size to prevent holding onto too many free coroutines
* Added atexit(3) handler to free pool
* Coding style cleanups
Kevin Wolf (1):
coroutine: add ucontext and win32 implementations
Stefan Hajnoczi (4):
coroutine: add gthread dependency
coroutine: introduce coroutines API
coroutine: add test-coroutine automated tests
coroutine: add test-coroutine --benchmark-lifecycle
.gitignore | 1 + Makefile | 3 +- Makefile.objs | 11 +++ configure | 24 +++++- coroutine-gthread.c | 131 ++++++++++++++++++++++++++++ coroutine-ucontext.c | 230 ++++++++++++++++++++++++++++++++++++++++++++++++++ coroutine-win32.c | 91 ++++++++++++++++++++ qemu-coroutine-int.h | 48 +++++++++++ qemu-coroutine.c | 75 ++++++++++++++++ qemu-coroutine.h | 95 +++++++++++++++++++++ test-coroutine.c | 192 +++++++++++++++++++++++++++++++++++++++++ trace-events | 5 + 12 files changed, 902 insertions(+), 4 deletions(-) create mode 100644 coroutine-gthread.c create mode 100644 coroutine-ucontext.c create mode 100644 coroutine-win32.c create mode 100644 qemu-coroutine-int.h create mode 100644 qemu-coroutine.c create mode 100644 qemu-coroutine.h create mode 100644 test-coroutine.c
Signature · 1 line
1.7.5.4
PATCH v8 1/5 coroutine: add gthread dependency
Commit 1fc7bd4a86a2bfeafcec29445871eb97469a2699 removed the gthread and gio dependency since qemu-ga did not require it. Coroutines require gthread, so add it back in.
--- configure | 6 +++--- 1 files changed, 3 insertions(+), 3 deletions(-)
configure +3 -3
@@ -1811,9 +1811,9 @@ fi ########################################## # glib support probe -if $pkg_config --modversion glib-2.0 > /dev/null 2>&1 ; then - glib_cflags=`$pkg_config --cflags glib-2.0 2>/dev/null` - glib_libs=`$pkg_config --libs glib-2.0 2>/dev/null` +if $pkg_config --modversion gthread-2.0 > /dev/null 2>&1 ; then + glib_cflags=`$pkg_config --cflags gthread-2.0 2>/dev/null` + glib_libs=`$pkg_config --libs gthread-2.0 2>/dev/null` libs_softmmu="$glib_libs $libs_softmmu" libs_tools="$glib_libs $libs_tools" else
Signature · 1 line
1.7.5.4
PATCH v8 2/5 coroutine: introduce coroutines API
In reply to Stefan HajnocziAsynchronous code is becoming very complex. At the same time synchronous code is growing because it is convenient to write. Sometimes duplicate code paths are even added, one synchronous and the other asynchronous. This patch introduces coroutines which allow code that looks synchronous but is asynchronous under the covers.
A coroutine has its own stack and is therefore able to preserve state across blocking operations, which traditionally require callback functions and manual marshalling of parameters.
Creating and starting a coroutine is easy:
coroutine = qemu_coroutine_create(my_coroutine);qemu_coroutine_enter(coroutine, my_data);
The coroutine then executes until it returns or yields:
void coroutine_fn my_coroutine(void *opaque) {
MyData *my_data = opaque;
/* do some work */
qemu_coroutine_yield();
/* do some more work */
}Yielding switches control back to the caller of qemu_coroutine_enter(). This is typically used to switch back to the main thread's event loop after issuing an asynchronous I/O request. The request callback will then invoke qemu_coroutine_enter() once more to switch back to the coroutine.
Note that if coroutines are used only from threads which hold the global mutex they will never execute concurrently. This makes programming with coroutines easier than with threads. Race conditions cannot occur since only one coroutine may be active at any time. Other coroutines can only run across yield.
This patch includes a gthread-based coroutines implementation which should work on all platforms.
[Original gthread patch by Aneesh, made consistent with coroutine-ucontext.c and switched to GStaticPrivate by Stefan. Tested on Linux and OpenBSD.]
--- Makefile.objs | 5 ++ coroutine-gthread.c | 131 ++++++++++++++++++++++++++++++++++++++++++++++++++ qemu-coroutine-int.h | 48 ++++++++++++++++++ qemu-coroutine.c | 75 ++++++++++++++++++++++++++++ qemu-coroutine.h | 95 ++++++++++++++++++++++++++++++++++++ trace-events | 5 ++ 6 files changed, 359 insertions(+), 0 deletions(-) create mode 100644 coroutine-gthread.c create mode 100644 qemu-coroutine-int.h create mode 100644 qemu-coroutine.c create mode 100644 qemu-coroutine.h
Makefile.objs +5 -0
@@ -11,6 +11,10 @@ oslib-obj-$(CONFIG_WIN32) += oslib-win32.o qemu-thread-win32.o oslib-obj-$(CONFIG_POSIX) += oslib-posix.o qemu-thread-posix.o ####################################################################### +# coroutines +coroutine-obj-y = qemu-coroutine.o coroutine-gthread.o + +####################################################################### # block-obj-y is code used by both qemu system emulation and qemu-img block-obj-y = cutils.o cache-utils.o qemu-malloc.o qemu-option.o module.o async.o
@@ -69,6 +73,7 @@ common-obj-y += readline.o console.o cursor.o qemu-error.o common-obj-y += $(oslib-obj-y) common-obj-$(CONFIG_WIN32) += os-win32.o common-obj-$(CONFIG_POSIX) += os-posix.o +common-obj-y += $(coroutine-obj-y) common-obj-y += tcg-runtime.o host-utils.o common-obj-y += irq.o ioport.o input.o
coroutine-gthread.c new file +131 -0
131 lines not on this page · raw message
qemu-coroutine-int.h new file +48 -0
48 lines not on this page · raw message
qemu-coroutine.c new file +75 -0
75 lines not on this page · raw message
qemu-coroutine.h new file +95 -0
95 lines not on this page · raw message
trace-events +5 -0
@@ -425,3 +425,8 @@ disable qemu_put_ram_ptr(void* addr) "%p" # hw/xen_platform.c disable xen_platform_log(char *s) "xen platform: %s" + +# qemu-coroutine.c +disable qemu_coroutine_enter(void *from, void *to, void *opaque) "from %p to %p opaque %p" +disable qemu_coroutine_yield(void *from, void *to) "from %p to %p" +disable qemu_coroutine_terminate(void *co) "self %p"
Signature · 1 line
1.7.5.4
PATCH v8 3/5 coroutine: add ucontext and win32 implementations
In reply to Stefan HajnocziFrom: Kevin Wolf <[email protected]>
This coroutines implementation is based on the gtk-vnc implementation written by Anthony Liguori <[email protected]> but it has been significantly rewritten by Kevin Wolf <[email protected]> to use setjmp()/longjmp() instead of the more expensive swapcontext() and by Paolo Bonzini <[email protected]> for Windows Fibers support.
Darwin has makecontext(3) but getcontext(3) is stubbed out to return ENOTSUP. Andreas Färber <[email protected]> debugged this and contributed the ./configure test which solves the issue for Darwin/ppc64 (and ppc) v10.5.
--- Makefile.objs | 8 ++- configure | 18 ++++ coroutine-ucontext.c | 230 ++++++++++++++++++++++++++++++++++++++++++++++++++ coroutine-win32.c | 91 ++++++++++++++++++++ 4 files changed, 346 insertions(+), 1 deletions(-) create mode 100644 coroutine-ucontext.c create mode 100644 coroutine-win32.c
Makefile.objs +7 -1
@@ -12,7 +12,13 @@ oslib-obj-$(CONFIG_POSIX) += oslib-posix.o qemu-thread-posix.o ####################################################################### # coroutines -coroutine-obj-y = qemu-coroutine.o coroutine-gthread.o +coroutine-obj-y = qemu-coroutine.o +ifeq ($(CONFIG_UCONTEXT_COROUTINE),y) +coroutine-obj-$(CONFIG_POSIX) += coroutine-ucontext.o +else +coroutine-obj-$(CONFIG_POSIX) += coroutine-gthread.o +endif +coroutine-obj-$(CONFIG_WIN32) += coroutine-win32.o ####################################################################### # block-obj-y is code used by both qemu system emulation and qemu-img
configure +18 -0
@@ -2499,6 +2499,20 @@ if test "$trace_backend" = "dtrace"; then fi ########################################## +# check if we have makecontext + +ucontext_coroutine=no +if test "$darwin" != "yes"; then + cat > $TMPC << EOF +#include <ucontext.h> +int main(void) { makecontext(0, 0, 0); } +EOF + if compile_prog "" "" ; then + ucontext_coroutine=yes + fi +fi + +########################################## # End of CC checks # After here, no more $cc or $ld runs
@@ -2970,6 +2984,10 @@ if test "$rbd" = "yes" ; then echo "CONFIG_RBD=y" >> $config_host_mak fi +if test "$ucontext_coroutine" = "yes" ; then + echo "CONFIG_UCONTEXT_COROUTINE=y" >> $config_host_mak +fi + # USB host support case "$usb" in linux)
coroutine-ucontext.c new file +230 -0
230 lines not on this page · raw message
coroutine-win32.c new file +91 -0
91 lines not on this page · raw message
Signature · 1 line
1.7.5.4
PATCH v8 4/5 coroutine: add test-coroutine automated tests
In reply to Stefan HajnocziTo run automated tests for coroutines:
make test-coroutine
./test-coroutine
On success the program terminates with exit status 0. On failure an error message is written to stderr and the program exits with exit status 1.
--- .gitignore | 1 + Makefile | 3 +- test-coroutine.c | 162 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 165 insertions(+), 1 deletions(-) create mode 100644 test-coroutine.c
.gitignore +1 -0
@@ -36,6 +36,7 @@ qemu-io qemu-ga qemu-monitor.texi QMP/qmp-commands.txt +test-coroutine .gdbinit *.a *.aux
Makefile +2 -1
@@ -151,7 +151,7 @@ qemu-io$(EXESUF): qemu-io.o cmd.o qemu-tool.o qemu-error.o $(oslib-obj-y) $(trac qemu-img-cmds.h: $(SRC_PATH)/qemu-img-cmds.hx $(call quiet-command,sh $(SRC_PATH)/scripts/hxtool -h < $< > $@," GEN $@") -check-qint.o check-qstring.o check-qdict.o check-qlist.o check-qfloat.o check-qjson.o: $(GENERATED_HEADERS) +check-qint.o check-qstring.o check-qdict.o check-qlist.o check-qfloat.o check-qjson.o test-coroutine.o: $(GENERATED_HEADERS) CHECK_PROG_DEPS = qemu-malloc.o $(oslib-obj-y) $(trace-obj-y) qemu-tool.o
@@ -161,6 +161,7 @@ check-qdict: check-qdict.o qdict.o qfloat.o qint.o qstring.o qbool.o qlist.o $(C check-qlist: check-qlist.o qlist.o qint.o $(CHECK_PROG_DEPS) check-qfloat: check-qfloat.o qfloat.o $(CHECK_PROG_DEPS) check-qjson: check-qjson.o qfloat.o qint.o qdict.o qstring.o qlist.o qbool.o qjson.o json-streamer.o json-lexer.o json-parser.o error.o qerror.o qemu-error.o $(CHECK_PROG_DEPS) +test-coroutine: test-coroutine.o qemu-timer-common.o $(coroutine-obj-y) $(CHECK_PROG_DEPS) $(qapi-obj-y): $(GENERATED_HEADERS) qapi-dir := qapi-generated
test-coroutine.c new file +162 -0
162 lines not on this page · raw message
Signature · 1 line
1.7.5.4
Re: [Qemu-devel] [PATCH v8 4/5] coroutine: add test-coroutine automated tests
Am 26.07.2011 11:21, schrieb Stefan Hajnoczi:
Quoted 10 lines
To run automated tests for coroutines:
make test-coroutine
./test-coroutineOn success the program terminates with exit status 0. On failure an error message is written to stderr and the program exits with exit status 1.
Signed-off-by Stefan Hajnoczi <[email protected]>
Adding this build fix in the block branch:
Makefile +0 -0
@@ -161,7 +161,7 @@ check-qdict: check-qdict.o qdict.o qfloat.o qint.o
qstring.o qbool.o qlist.o $(C
check-qlist: check-qlist.o qlist.o qint.o $(CHECK_PROG_DEPS)
check-qfloat: check-qfloat.o qfloat.o $(CHECK_PROG_DEPS)
check-qjson: check-qjson.o qfloat.o qint.o qdict.o qstring.o qlist.o
qbool.o qjson.o json-streamer.o json-lexer.o json-parser.o error.o qerror.o qemu-error.o $(CHECK_PROG_DEPS)
-test-coroutine: test-coroutine.o qemu-timer-common.o $(coroutine-obj-y) $(CHECK_PROG_DEPS)
+test-coroutine: test-coroutine.o qemu-timer-common.o async.o $(coroutine-obj-y) $(CHECK_PROG_DEPS)
$(qapi-obj-y): $(GENERATED_HEADERS)
qapi-dir := qapi-generated
PATCH v8 5/5 coroutine: add test-coroutine --benchmark-lifecycle
In reply to Stefan HajnocziAdd a microbenchmark for coroutine create, enter, and return (aka lifecycle). This is a useful benchmark because users are expected to create many coroutines, one per I/O request for example, and we therefore need to provide good performance in that scenario.
To run:
make test-coroutine ./test-coroutine --benchmark-lifecycle 20000000
This will do 20,000,000 coroutine create, enter, return iterations and print the resulting time.
--- test-coroutine.c | 30 ++++++++++++++++++++++++++++++ 1 files changed, 30 insertions(+), 0 deletions(-)
test-coroutine.c +30 -0
@@ -150,6 +150,33 @@ static void test_lifecycle(void)g_assert (done);/* expect done to be true (second time) */ } +/* +* Lifecycle benchmark +*/ + +static void coroutine_fn empty_coroutine(void *opaque) +{ +/* Do nothing */ +} + +static void perf_lifecycle (void ) +{ +Coroutine *coroutine; +unsigned int i, max; +double duration; + + max =1000000 ; + +g_test_timer_start (); +for (i =0 ; i < max; i++) { + coroutine =qemu_coroutine_create (empty_coroutine); +qemu_coroutine_enter (coroutine,NULL ); + } + duration =g_test_timer_elapsed (); + +g_test_message ("Lifecycle %u iterations: %f s\n" , max, duration); +} +int main (int argc,char **argv) {g_test_init (&argc, &argv,NULL );
@@ -158,5 +185,8 @@ int main(int argc, char **argv)g_test_add_func ("/basic/nesting" , test_nesting);g_test_add_func ("/basic/self" , test_self);g_test_add_func ("/basic/in_coroutine" , test_in_coroutine); +if (g_test_perf ()) { +g_test_add_func ("/perf/lifecycle" , perf_lifecycle); + }return g_test_run (); }
Signature · 1 line
1.7.5.4
Quoted 16 lines · Stefan Hajnoczi
QEMU is event-driven and suffers when blocking operations are performed because VM execution may be stopped until the operation completes. Therefore many operations that could block are performed asynchronously and a callback is invoked when the operation has completed. This allows QEMU to continue executing while the operation is pending.
The downside to callbacks is that they split up code into many smaller functions, each of which is a single step in a state machine that quickly becomes complex and hard to understand. Callback functions also result in lots of noise as variables are packed and unpacked into temporary structs that pass state to the callback function.
This patch series introduces coroutines as a solution for writing asynchronous code while still having a nice sequential control flow. The semantics are explained in the second patch. The fourth patch adds automated tests.
Quoted 8 lines
A nice feature of coroutines is that it is relatively easy to take synchronous code and lift it into a coroutine to make it asynchronous. Work has been done to move qcow2 request processing into coroutines and thereby make it asynchronous (today qcow2 will perform synchronous metadata accesses). This qcow2 work is still ongoing and not quite ready for mainline yet.
v8:
* Bisectability: introduce gthread implementation before ucontext/fibers
Forgot to mention:
* Depend on gthread, not just glib, for multithreaded programming
Stefan
Quoted 16 lines · Stefan Hajnoczi
QEMU is event-driven and suffers when blocking operations are performed because VM execution may be stopped until the operation completes. Therefore many operations that could block are performed asynchronously and a callback is invoked when the operation has completed. This allows QEMU to continue executing while the operation is pending.
The downside to callbacks is that they split up code into many smaller functions, each of which is a single step in a state machine that quickly becomes complex and hard to understand. Callback functions also result in lots of noise as variables are packed and unpacked into temporary structs that pass state to the callback function.
This patch series introduces coroutines as a solution for writing asynchronous code while still having a nice sequential control flow. The semantics are explained in the second patch. The fourth patch adds automated tests.
Quoted 8 lines
A nice feature of coroutines is that it is relatively easy to take synchronous code and lift it into a coroutine to make it asynchronous. Work has been done to move qcow2 request processing into coroutines and thereby make it asynchronous (today qcow2 will perform synchronous metadata accesses). This qcow2 work is still ongoing and not quite ready for mainline yet.
v8:
* Bisectability: introduce gthread implementation before ucontext/fibers
Can we also get CoMutex and CoQueue patches also merged. I use them in the VirtFS series.
http://repo.or.cz/w/qemu/kevin.git/shortlog/refs/heads/coroutine-devel
http://repo.or.cz/w/qemu/v9fs.git/commit/318ef0b9b01cd296f8c30d8288139b9bed859892
-aneesh
Am 27.07.2011 11:45, schrieb Aneesh Kumar K.V:
Quoted 23 lines · Stefan Hajnoczi
Quoted 22 lines
QEMU is event-driven and suffers when blocking operations are performed because VM execution may be stopped until the operation completes. Therefore many operations that could block are performed asynchronously and a callback is invoked when the operation has completed. This allows QEMU to continue executing while the operation is pending.
The downside to callbacks is that they split up code into many smaller functions, each of which is a single step in a state machine that quickly becomes complex and hard to understand. Callback functions also result in lots of noise as variables are packed and unpacked into temporary structs that pass state to the callback function.
This patch series introduces coroutines as a solution for writing asynchronous code while still having a nice sequential control flow. The semantics are explained in the second patch. The fourth patch adds automated tests.
A nice feature of coroutines is that it is relatively easy to take synchronous code and lift it into a coroutine to make it asynchronous. Work has been done to move qcow2 request processing into coroutines and thereby make it asynchronous (today qcow2 will perform synchronous metadata accesses). This qcow2 work is still ongoing and not quite ready for mainline yet.
Quoted 8 lines
Quoted 2 lines
v8:
* Bisectability: introduce gthread implementation before ucontext/fibersCan we also get CoMutex and CoQueue patches also merged. I use them in the VirtFS series.
http://repo.or.cz/w/qemu/kevin.git/shortlog/refs/heads/coroutine-devel
http://repo.or.cz/w/qemu/v9fs.git/commit/318ef0b9b01cd296f8c30d8288139b9bed859892
I introduce these in my block coroutine patches. I posted a RFC last week and the first "real" patch series yesterday, so I hope they get review and can be merged into master soon.
Kevin
Quoted 30 lines · Kevin Wolf
Am 27.07.2011 11:45, schrieb Aneesh Kumar K.V:
Quoted 29 lines · Stefan Hajnoczi
Quoted 16 lines
QEMU is event-driven and suffers when blocking operations are performed because VM execution may be stopped until the operation completes. Therefore many operations that could block are performed asynchronously and a callback is invoked when the operation has completed. This allows QEMU to continue executing while the operation is pending.
The downside to callbacks is that they split up code into many smaller functions, each of which is a single step in a state machine that quickly becomes complex and hard to understand. Callback functions also result in lots of noise as variables are packed and unpacked into temporary structs that pass state to the callback function.
This patch series introduces coroutines as a solution for writing asynchronous code while still having a nice sequential control flow. The semantics are explained in the second patch. The fourth patch adds automated tests.
Quoted 8 lines
A nice feature of coroutines is that it is relatively easy to take synchronous code and lift it into a coroutine to make it asynchronous. Work has been done to move qcow2 request processing into coroutines and thereby make it asynchronous (today qcow2 will perform synchronous metadata accesses). This qcow2 work is still ongoing and not quite ready for mainline yet.
v8:
* Bisectability: introduce gthread implementation before ucontext/fibersCan we also get CoMutex and CoQueue patches also merged. I use them in the VirtFS series.
Quoted 7 lines
Quoted 2 lines
http://repo.or.cz/w/qemu/kevin.git/shortlog/refs/heads/coroutine-devel
http://repo.or.cz/w/qemu/v9fs.git/commit/318ef0b9b01cd296f8c30d8288139b9bed859892I introduce these in my block coroutine patches. I posted a RFC last week and the first "real" patch series yesterday, so I hope they get review and can be merged into master soon.
Can you review the patch that add CoRWlock ?
http://article.gmane.org/gmane.comp.emulators.qemu/105402
Message-id:[email protected]
commit 8c787d8b81aca1f4f7be45adb67b9e1a6dde7f1f
Author: Aneesh Kumar K.V <[email protected]> Date: Tue May 24 22:14:04 2011 +0530
coroutine: Add CoRwlock support
qemu-coroutine-lock.c +44 -0
@@ -115,3 +115,47 @@ void qemu_co_mutex_unlock(CoMutex *mutex)trace_qemu_co_mutex_unlock_return (mutex, self); } + +void qemu_co_rwlock_init (CoRwlock *lock) +{ +memset (lock,0 ,sizeof (*lock)); +qemu_co_queue_init (&lock->queue); +} + +void qemu_co_rwlock_rdlock (CoRwlock *lock) +{ +while (lock->writer) { +qemu_co_queue_wait (&lock->queue); + } + lock->reader++; +} + +void qemu_co_rwlock_unlock (CoRwlock *lock) +{ +assert (qemu_in_coroutine ()); +if (lock->writer) { + lock->writer--;; +assert (lock->writer ==0 ); +while (!qemu_co_queue_empty (&lock->queue)) { +/* +* Wakeup every body. This will include some +* writers too. +*/ +qemu_co_queue_next (&lock->queue); + } + }else { + lock->reader--; +assert (lock->reader >=0 ); +/* Wakeup only one waiting writer */ +qemu_co_queue_next (&lock->queue); + } +} + +void qemu_co_rwlock_wrlock (CoRwlock *lock) +{ +while (lock->writer || lock->reader) { +qemu_co_queue_wait (&lock->queue); + } + lock->writer++; +assert (lock->writer ==1 ); +}
qemu-coroutine.h +12 -0
@@ -17,6 +17,7 @@#include <stdbool.h> #include "qemu-queue.h" +#include "qemu-thread.h" /** * Coroutines are a mechanism for stack switching and can be used for
@@ -114,4 +115,15 @@ void qemu_co_mutex_init(CoMutex *mutex);void qemu_co_mutex_lock (CoMutex *mutex);void qemu_co_mutex_unlock (CoMutex *mutex); +typedef struct CoRwlock { +bool writer; +int reader; +CoQueue queue; +}CoRwlock ; + +void qemu_co_rwlock_init (CoRwlock *lock); +void qemu_co_rwlock_rdlock (CoRwlock *lock); +void qemu_co_rwlock_wrlock (CoRwlock *lock); +void qemu_co_rwlock_unlock (CoRwlock *lock); +#endif /* QEMU_COROUTINE_H */
Am 27.07.2011 13:39, schrieb Aneesh Kumar K.V:
Quoted 5 lines
Can you review the patch that add CoRWlock ?
http://article.gmane.org/gmane.comp.emulators.qemu/105402
Message-id:[email protected]
Quoted 7 lines
commit
8c787d8b81aca1f4f7be45adb67b9e1a6dde7f1fAuthor: Aneesh Kumar K.V <[email protected]> Date: Tue May 24 22:14:04 2011 +0530coroutine: Add CoRwlock support
Signed-off-by Aneesh Kumar K.V <[email protected]>
Nice! I think I'll need this, too.
Quoted 18 lines
qemu-coroutine-lock.c +10
-0@@ -115,3 +115,47 @@ void qemu_co_mutex_unlock(CoMutex *mutex)trace_qemu_co_mutex_unlock_return (mutex, self); } + +void qemu_co_rwlock_init (CoRwlock *lock) +{ +memset (lock,0 ,sizeof (*lock)); +qemu_co_queue_init (&lock->queue); +} + +void qemu_co_rwlock_rdlock (CoRwlock *lock) +{ +while (lock->writer) {
qemu-coroutine-lock.c void qemu_co_mutex_unlock(CoMutex *mutex) · line 137
Quoted 10 lines
qemu-coroutine-lock.c +10
-0+qemu_co_queue_wait (&lock->queue); + } + lock->reader++; +} + +void qemu_co_rwlock_unlock (CoRwlock *lock) +{ +assert (qemu_in_coroutine ()); +if (lock->writer) { + lock->writer--;;
Please don't do arithmetics on bools, just say lock->write = false;
Also there's a double semicolon.
qemu-coroutine-lock.c void qemu_co_mutex_unlock(CoMutex *mutex) · line 150
Quoted 13 lines
qemu-coroutine-lock.c +13
-0+assert (lock->writer ==0 ); +while (!qemu_co_queue_empty (&lock->queue)) { +/* +* Wakeup every body. This will include some +* writers too. +*/ +qemu_co_queue_next (&lock->queue); + } + }else { + lock->reader--; +assert (lock->reader >=0 ); +/* Wakeup only one waiting writer */ +qemu_co_queue_next (&lock->queue);
This is only useful if lock->reader == 0.
qemu-coroutine-lock.c void qemu_co_mutex_unlock(CoMutex *mutex) · line 161
Quoted 11 lines
qemu-coroutine-lock.c +11
-0+ } +} + +void qemu_co_rwlock_wrlock (CoRwlock *lock) +{ +while (lock->writer || lock->reader) { +qemu_co_queue_wait (&lock->queue); + } + lock->writer++; +assert (lock->writer ==1 ); +}
I wonder if we should have a mechanism that stops new readers from taking the lock while a writer is waiting in order to avoid starvation.
Anyway, the locking itself looks correct.
Kevin
Quoted 23 lines · Aneesh Kumar K.V
Quoted 22 lines · Stefan Hajnoczi
QEMU is event-driven and suffers when blocking operations are performed because VM execution may be stopped until the operation completes. Therefore many operations that could block are performed asynchronously and a callback is invoked when the operation has completed. This allows QEMU to continue executing while the operation is pending.
The downside to callbacks is that they split up code into many smaller functions, each of which is a single step in a state machine that quickly becomes complex and hard to understand. Callback functions also result in lots of noise as variables are packed and unpacked into temporary structs that pass state to the callback function.
This patch series introduces coroutines as a solution for writing asynchronous code while still having a nice sequential control flow. The semantics are explained in the second patch. The fourth patch adds automated tests.
A nice feature of coroutines is that it is relatively easy to take synchronous code and lift it into a coroutine to make it asynchronous. Work has been done to move qcow2 request processing into coroutines and thereby make it asynchronous (today qcow2 will perform synchronous metadata accesses). This qcow2 work is still ongoing and not quite ready for mainline yet.
Quoted 9 lines
Quoted 2 lines
v8:
* Bisectability: introduce gthread implementation before ucontext/fibersCan we also get CoMutex and CoQueue patches also merged. I use them in the VirtFS series.
http://repo.or.cz/w/qemu/kevin.git/shortlog/refs/heads/coroutine-devel
http://repo.or.cz/w/qemu/v9fs.git/commit/318ef0b9b01cd296f8c30d8288139b9bed859892
I have pushed VirtFS related changes to
git://repo.or.cz/qemu/v9fs.git for-upstream-1
Once the co-routine patches are merged I can send a proper pull request. There are more changes pending for VirtFS. We will push them in batches.
The following changes since commit c729e893aba79680bf410a2b9c671fb9e2c2c1a4:
coroutine: add test-coroutine --benchmark-lifecycle (2011-07-27 16:30:26 +0530)
are available in the git repository at:
git://repo.or.cz/qemu/v9fs.git for-upstream-1
Aneesh Kumar K.V (32):
hw/9pfs: Add yield support for readdir related coroutines hw/9pfs: Update v9fs_readdir to use coroutines hw/9pfs: Add yield support to statfs coroutine hw/9pfs: Update v9fs_statfs to use coroutines hw/9pfs: Add yield support to lstat coroutine hw/9pfs: Update v9fs_getattr to use coroutines hw/9pfs: Add yield support to setattr related coroutines hw/9pfs: Update v9fs_setattr to use coroutines hw/9pfs: Add yield support to xattr related coroutine hw/9pfs: Update v9fs_xattrwalk to coroutines hw/9pfs: Update v9fs_xattrcreate to use coroutines hw/9pfs: Add yield support to mknod coroutine hw/9pfs: Update v9fs_mknod to use coroutines hw/9pfs: Add yeild support to rename coroutine hw/9pfs: Update vfs_rename to use coroutines hw/9pfs: Add yeild support for fstat coroutine hw/9pfs: Update v9fs_lock to use coroutines hw/9pfs: Update v9fs_getlock to use coroutines hw/9pfs: Add yield support for open and opendir coroutine hw/9pfs: Update v9fs_open to use coroutines hw/9pfs: Update v9fs_stat to use coroutines hw/9pfs: Update v9fs_walk to use coroutines hw/9pfs: Add yeild support for clunk related coroutine hw/9pfs: Update v9fs_clunk to use coroutines hw/9pfs: Add yield support for fsync coroutine hw/9pfs: Update v9fs_fsync to use coroutines hw/9pfs: Add yield support for pwritev coroutine hw/9pfs: Update v9fs_write to use coroutines hw/9pfs: Update v9fs_wstat to use coroutines hw/9pfs: Update v9fs_attach to use coroutines hw/9pfs: Add yield support for preadv coroutine hw/9pfs: Update v9fs_read to use coroutines
Harsh Prateek Bora (1):
use readdir_r instead of readdir for reentrancy
Venkateswararao Jujjuri (JV) (20):
[virtio-9p] Add infrastructure to support glib threads and coroutines. [virtio-9p] Change all pdu handlers to coroutines. [virtio-9p] Remove post functions for v9fs_readlink. [virtio-9p] clean up v9fs_readlink.
[virtio-9p] coroutines for readlink
[virtio-9p] Remove post functions for v9fs_mkdir. [virtio-9p] clean up v9fs_mkdir.
[virtio-9p] coroutine and threading for mkdir
[virtio-9p] Remove post functions for v9fs_remove [virtio-9p] clean up v9fs_remove.
[virtio-9p] coroutine and threading for remove/unlink [virtio-9p] Remove post functions for v9fs_lcreate [virtio-9p] clean up v9fs_lcreate
[virtio-9p] coroutine and threading for open2
[virtio-9p] Remove post functions for v9fs_create [virtio-9p] clean up v9fs_create Rearrange the code [virtio-9p] Remove post functions for v9fs_symlink [virtio-9p] clean up v9fs_symlink
[virtio-9p] coroutine and threading for v9fs_do_symlink [virtio-9p] coroutine and threading for v9fs_do_link
Makefile.objs | 3 + fsdev/file-op-9p.h | 2 +- hw/9pfs/codir.c | 117 ++ hw/9pfs/cofile.c | 163 +++ hw/9pfs/cofs.c | 191 +++ hw/9pfs/coxattr.c | 84 ++ hw/9pfs/virtio-9p-coth.c | 102 ++ hw/9pfs/virtio-9p-coth.h | 96 ++ hw/9pfs/virtio-9p-device.c | 7 +- hw/9pfs/virtio-9p-local.c | 7 +- hw/9pfs/virtio-9p.c | 3052 ++++++++++++++++---------------------------- hw/9pfs/virtio-9p.h | 155 +--- 12 files changed, 1840 insertions(+), 2139 deletions(-) create mode 100644 hw/9pfs/codir.c create mode 100644 hw/9pfs/cofile.c create mode 100644 hw/9pfs/cofs.c create mode 100644 hw/9pfs/coxattr.c create mode 100644 hw/9pfs/virtio-9p-coth.c create mode 100644 hw/9pfs/virtio-9p-coth.h
Quoted 16 lines · Stefan Hajnoczi
QEMU is event-driven and suffers when blocking operations are performed because VM execution may be stopped until the operation completes. Therefore many operations that could block are performed asynchronously and a callback is invoked when the operation has completed. This allows QEMU to continue executing while the operation is pending.
The downside to callbacks is that they split up code into many smaller functions, each of which is a single step in a state machine that quickly becomes complex and hard to understand. Callback functions also result in lots of noise as variables are packed and unpacked into temporary structs that pass state to the callback function.
This patch series introduces coroutines as a solution for writing asynchronous code while still having a nice sequential control flow. The semantics are explained in the second patch. The fourth patch adds automated tests.
Quoted 8 lines
A nice feature of coroutines is that it is relatively easy to take synchronous code and lift it into a coroutine to make it asynchronous. Work has been done to move qcow2 request processing into coroutines and thereby make it asynchronous (today qcow2 will perform synchronous metadata accesses). This qcow2 work is still ongoing and not quite ready for mainline yet.
v8:
* Bisectability: introduce gthread implementation before ucontext/fibers
Aneesh's code on top of this will go in separately.
We're good to go.
Stefan