Coroutines for better asynchronous programming
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 first patch. The second 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.
Coroutines are also being used for virtfs (virtio-9p) so I have submitted this patch now because virtfs patches that depend on coroutines will follow shortly.
Other areas of QEMU that could take advantage of coroutines include the VNC server, migration, and qemu-tools.
PATCH 1/2 coroutine: introduce coroutines
From: Kevin Wolf <[email protected]>
Asynchronous 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 coroutines never execute concurrently and should only be used from threads which hold the global mutex. This restriction 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 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().
--- Makefile.objs | 7 +++ coroutine-ucontext.c | 73 +++++++++++++++++++++++++++ coroutine-win32.c | 57 +++++++++++++++++++++ qemu-coroutine-int.h | 57 +++++++++++++++++++++ qemu-coroutine.c | 132 ++++++++++++++++++++++++++++++++++++++++++++++++++ qemu-coroutine.h | 82 +++++++++++++++++++++++++++++++ trace-events | 5 ++ 7 files changed, 413 insertions(+), 0 deletions(-) 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
Makefile.objs +7 -0
@@ -11,6 +11,12 @@ oslib-obj-$(CONFIG_WIN32) += oslib-win32.o oslib-obj-$(CONFIG_POSIX) += oslib-posix.o ####################################################################### +# coroutines +coroutine-obj-y = qemu-coroutine.o +coroutine-obj-$(CONFIG_POSIX) += coroutine-ucontext.o +coroutine-obj-$(CONFIG_WIN32) += coroutine-win32.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
@@ -67,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-ucontext.c new file +73 -0
73 lines not on this page · raw message
coroutine-win32.c new file +57 -0
57 lines not on this page · raw message
qemu-coroutine-int.h new file +57 -0
57 lines not on this page · raw message
qemu-coroutine.c new file +132 -0
132 lines not on this page · raw message
qemu-coroutine.h new file +82 -0
82 lines not on this page · raw message
trace-events +5 -0
@@ -361,3 +361,8 @@ disable milkymist_uart_pulse_irq_tx(void) "Pulse IRQ TX" # hw/milkymist-vgafb.c disable milkymist_vgafb_memory_read(uint32_t addr, uint32_t value) "addr %08x value %08x" disable milkymist_vgafb_memory_write(uint32_t addr, uint32_t value) "addr %08x value %08x" + +# qemu-coroutine.c +qemu_coroutine_enter(void *from, void *to, void *opaque) "from %p to %p opaque %p" +qemu_coroutine_yield(void *from, void *to) "from %p to %p" +qemu_coroutine_terminate(void *co) "self %p"
Signature · 1 line
1.7.4.4
Re: [Qemu-devel] [PATCH 1/2] coroutine: introduce coroutines
Am 11.05.2011 12:15, schrieb Stefan Hajnoczi:
Quoted 41 lines
From: Kevin Wolf <[email protected]>
Asynchronous 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 invokeqemu_coroutine_enter()once more to switch back to the coroutine.Note that coroutines never execute concurrently and should only be used from threads which hold the global mutex. This restriction 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.
Quoted 7 lines
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().Signed-off-by Kevin Wolf <[email protected]>Signed-off-by Stefan Hajnoczi <[email protected]>
For the diff between my latest version and this patch:
Re: [Qemu-devel] [PATCH 1/2] coroutine: introduce coroutines
In reply to Stefan Hajnoczicoroutine-win32.c line 51
Quoted 9 lines · Stefan Hajnoczi
coroutine-win32.c new file +9
-0+#ifdef __i386__ + asmvolatile ( +"mov %%esp, %%ebx;" +"mov %0, %%esp;" +"pushl %1;" +"call _trampoline;" +"mov %%ebx, %%esp;" + : :"r" (co->stack + co->stack_size),"r" (co) :"ebx" + );
This is incomplete, it should set FS:[4] and FS:[8] to top and bottom of stack respectively, otherwise exception handling (including SIGSEGV) is broken. But I think for Windows it's anyway better to use fibers. Commit this and either I or Stefan Weil will fix it. :)
Paolo
Re: [Qemu-devel] [PATCH 1/2] coroutine: introduce coroutines
Am 11.05.2011 14:04, schrieb Paolo Bonzini:
Quoted 11 lines · Stefan Hajnoczi
coroutine-win32.c line 51
Quoted 9 lines
coroutine-win32.c new file +9
-0+#ifdef __i386__ + asmvolatile ( +"mov %%esp, %%ebx;" +"mov %0, %%esp;" +"pushl %1;" +"call _trampoline;" +"mov %%ebx, %%esp;" + : :"r" (co->stack + co->stack_size),"r" (co) :"ebx" + );
Quoted 6 lines
This is incomplete, it should set FS:[4] and FS:[8] to top and bottom of stack respectively, otherwise exception handling (including SIGSEGV) is broken. But I think for Windows it's anyway better to use fibers. Commit this and either I or Stefan Weil will fix it. :)
Acked-by Paolo Bonzini <[email protected]>
Yeah, I didn't feel like searching for the right APIs, but wanted to have something that builds for win32 at least. And this one seemed to work for some simple tests in Wine.
So my plan with it was to CC Stefan Weil and have him provide the real implementation that works on 64 bit, too. ;-)
Kevin
Re: [Qemu-devel] [PATCH 1/2] coroutine: introduce coroutines
In reply to Paolo BonziniQuoted 11 lines · Paolo Bonzini
Quoted 9 lines · Stefan Hajnoczi
+#ifdef
__i386__
+ asm volatile(
+ "mov %%esp, %%ebx;"
+ "mov %0, %%esp;"
+ "pushl %1;"
+ "call_trampoline;"
+ "mov %%ebx, %%esp;"
+ : : "r" (co->stack+co->stack_size), "r" (co) : "ebx"
+ );
Quoted 6 lines
This is incomplete, it should set FS:[4] and FS:[8] to top and bottom of stack respectively, otherwise exception handling (including SIGSEGV) is broken. But I think for Windows it's anyway better to use fibers. Commit this and either I or Stefan Weil will fix it. :)
Acked-by Paolo Bonzini <[email protected]>
How about a generic thread fallback? That's what we do in gtk-vnc and it solves the portability issue in a very robust way.
Regards,
Anthony Liguori
Quoted 3 lines
Paolo
Re: [Qemu-devel] [PATCH 1/2] coroutine: introduce coroutines
Quoted 2 lines · Anthony Liguori
How about a generic thread fallback? That's what we do in gtk-vnc and it solves the portability issue in a very robust way.
A very slow way, too (on Windows at least if you use qemu_cond...).
Paolo
Re: [Qemu-devel] [PATCH 1/2] coroutine: introduce coroutines
Quoted 5 lines · Paolo Bonzini
Quoted 2 lines · Anthony Liguori
How about a generic thread fallback? That's what we do in gtk-vnc and it solves the portability issue in a very robust way.
A very slow way, too (on Windows at least if you use
qemu_cond...).
That doesn't mean you can't do a fiber implementation for Windows... but having a highly portable fallback is a good thing.
Regards,
Anthony Liguori
Quoted 1 line
Paolo
Re: [Qemu-devel] [PATCH 1/2] coroutine: introduce coroutines
Quoted 5 lines · Anthony Liguori
Quoted 2 lines
A very slow way, too (on Windows at least if you use
qemu_cond...).That doesn't mean you can't do a fiber implementation for Windows... but having a highly portable fallback is a good thing.
I agree but where would you place it, since QEMU is only portable to POSIX and Windows?
osdep-$(CONFIG_POSIX) += coroutine-posix.c
osdep-$(CONFIG_WIN32) += coroutine-win32.c
osdep-??? += coroutine-fallback.c
:)
Paolo
Re: [Qemu-devel] [PATCH 1/2] coroutine: introduce coroutines
Quoted 7 lines · Paolo Bonzini
Quoted 5 lines · Anthony Liguori
Quoted 2 lines
A very slow way, too (on Windows at least if you use
qemu_cond...).That doesn't mean you can't do a fiber implementation for Windows... but having a highly portable fallback is a good thing.
Quoted 6 lines
I agree but where would you place it, since QEMU is only portable to POSIX and Windows?
osdep-$(CONFIG_POSIX) +=
coroutine-posix.c
osdep-$(CONFIG_WIN32) +=coroutine-win32.c
osdep-??? +=coroutine-fallback.c
NetBSD forbids the use of 'makecontext' in any application which also links to libpthread.so[1]. We used makecontext in GTK-VNC's coroutines and got random crashes in threaded apps running on NetBSD. So for NetBSD we tell people to use the thread based coroutines instead.
So at least one POSIX platform will need a different impl.
Regards,
Daniel
[1] http://www.eila.univ-paris-diderot.fr/cgi-bin/man.cgi?swapcontext+3
Signature · 4 lines
|: http://berrange.com -o- http://www.flickr.com/photos/dberrange/ :| |: http://libvirt.org -o- http://virt-manager.org :| |: http://autobuild.org -o- http://search.cpan.org/~danberr/ :| |: http://entangle-photo.org -o- http://live.gnome.org/gtk-vnc :|
Re: [Qemu-devel] [PATCH 1/2] coroutine: introduce coroutines
Quoted 15 lines · Daniel P. Berrange
Quoted 7 lines · Paolo Bonzini
Quoted 5 lines · Anthony Liguori
Quoted 2 lines
A very slow way, too (on Windows at least if you use
qemu_cond...).That doesn't mean you can't do a fiber implementation for Windows... but having a highly portable fallback is a good thing.
Quoted 6 lines
I agree but where would you place it, since QEMU is only portable to POSIX and Windows?
osdep-$(CONFIG_POSIX) +=
coroutine-posix.c
osdep-$(CONFIG_WIN32) +=coroutine-win32.c
osdep-??? +=coroutine-fallback.c
Quoted 5 lines
NetBSD forbids the use of 'makecontext' in any application which also links to libpthread.so[1]. We used makecontext in GTK-VNC's coroutines and got random crashes in threaded apps running on NetBSD. So for NetBSD we tell people to use the thread based coroutines instead.
You have to use swapcontext(), no wait, you have to use setjmp(), no wait, _setjmp(), no wait, threads.... Read on.
Quoted 1 line
From Glibc's FAQ, setjmp/longjmp are not portable choices:
- UNIX provides no other (portable) way of effecting a synchronous context switch (also known as co-routine switch). Some versions support this via setjmp()/longjmp() but this does not work universally.
So in principle you should use swapcontext() in portable code.
(By the way, Glibc goes on about how it won't support swapcontext() from async signal handlers, i.e. preemption, on some architectures (IA-64/S-390), and I know it has been very subtly broken from a signal handler on ARM. Fair enough, somehow disappointing, but doesn't matter for QEMU coroutines.)
But swapcontext() etc. have been withdrawn from POSIX 2008:
- Functions to be deleted
Legacy: Delete all legacy functions except utimes (which should not be legacy).
OB: Default position is to delete all OB functions.
XSI Functions to change state
....
_setjmp and _longjmp. Should become obsolete.
....
getcontext, setcontext, makecontext and swapcontext are already marked OB and should be withdrawn. And header file <ucontext.h>.
OB means obsolescent. They were marked obsolescent a few versions prior, with the rationale that you can use threads instead...
It's not surprising that NetBSD forbids makecontext() with libpthread.so. I suspect old versions of FreeBSD, OpenBSD, DragonFly BSD, (and Mac OS X?), have the same restriction, because they have a similar pthreads evolutionary history to LinuxThreads. LinuxThreads also breaks when using coroutines that switch stacks, because it uses the stack pointer to know the current thread.
(LinuxThreads is old now, but that particular quirk still affects me because some uCLinux platforms, on which I wish to use coroutines, still don't have working NPTL - but they aren't likely to be running QEMU :-)
Finally, if you are using setjmp/longjmp, consider (from FreeBSD man page):
The setjmp()/longjmp() pairs save and restore the signal mask while _setjmp()/_longjmp() pairs save and restore only the register set and the stack. (See sigprocmask(2).)
As setjmp/longjmp were chosen for performance, you may wish to use _setjmp/_longjmp instead (when available), as swizzling the signal mask on each switch may involve a system call and be rather slow.
-- Jamie
Re: [Qemu-devel] [PATCH 1/2] coroutine: introduce coroutines
Quoted 49 lines · Jamie Lokier
Quoted 15 lines · Daniel P. Berrange
Quoted 7 lines · Paolo Bonzini
Quoted 5 lines · Anthony Liguori
Quoted 2 lines
A very slow way, too (on Windows at least if you use
qemu_cond...).That doesn't mean you can't do a fiber implementation for Windows... but having a highly portable fallback is a good thing.
Quoted 6 lines
I agree but where would you place it, since QEMU is only portable to POSIX and Windows?
osdep-$(CONFIG_POSIX) +=
coroutine-posix.c
osdep-$(CONFIG_WIN32) +=coroutine-win32.c
osdep-??? +=coroutine-fallback.cQuoted 5 lines
NetBSD forbids the use of 'makecontext' in any application which also links to libpthread.so[1]. We used makecontext in GTK-VNC's coroutines and got random crashes in threaded apps running on NetBSD. So for NetBSD we tell people to use the thread based coroutines instead.
You have to use
swapcontext(), no wait, you have to usesetjmp(), no wait,_setjmp(), no wait, threads.... Read on.From Glibc's FAQ, setjmp/longjmp are not portable choices:
- UNIX provides no other (portable) way of effecting a synchronous context switch (also known as co-routine switch). Some versions support this via setjmp()/longjmp() but this does not work universally.
So in principle you should use
swapcontext()in portable code.(By the way, Glibc goes on about how it won't support
swapcontext()from async signal handlers, i.e. preemption, on some architectures (IA-64/S-390), and I know it has been very subtly broken from a signal handler on ARM. Fair enough, somehow disappointing, but doesn't matter for QEMU coroutines.)But
swapcontext()etc. have been withdrawn from POSIX 2008:- Functions to be deleted
Legacy: Delete all legacy functions except utimes (which should not be legacy).
OB: Default position is to delete all OB functions.XSI Functions to change state
Quoted 8 lines
....
_setjmpand_longjmp. Should become obsolete.
....
getcontext, setcontext, makecontext and swapcontext are already marked OB and should be withdrawn. And header file <ucontext.h>.OB means obsolescent. They were marked obsolescent a few versions prior, with the rationale that you can use threads instead...
Yep, aware of this but at the end of the day these functions are commonly available.
Quoted 10 lines
It's not surprising that NetBSD forbids
makecontext()with libpthread.so. I suspect old versions of FreeBSD, OpenBSD, DragonFly BSD, (and Mac OS X?), have the same restriction, because they have a similar pthreads evolutionary history to LinuxThreads. LinuxThreads also breaks when using coroutines that switch stacks, because it uses the stack pointer to know the current thread.(LinuxThreads is old now, but that particular quirk still affects me because some uCLinux platforms, on which I wish to use coroutines, still don't have working NPTL - but they aren't likely to be running QEMU :-)
That is nasty.
Quoted 9 lines
Finally, if you are using setjmp/longjmp, consider (from FreeBSD man page):
The setjmp()/longjmp() pairs save and restore the signal mask while _setjmp()/_longjmp() pairs save and restore only the register set and the stack. (See sigprocmask(2).)
As setjmp/longjmp were chosen for performance, you may wish to use _setjmp/_longjmp instead (when available), as swizzling the signal mask on each switch may involve a system call and be rather slow.
Thanks, I read about that but didn't try to implement special cases because I don't have relevant OSes here to test against.
My current plan is to try using sigaltstack(2) instead of makecontext()/swapcontext() as a hack since OpenBSD doesn't have makecontext()/swapcontext().
TBH I'm almost at the stage where I think we should just use threads and/or async callbacks, as appropriate. Hopefully I'll be able to cook up a reasonably portable implementation of coroutines though, because the prospect of having to go fully threaded or do async callbacks isn't attractive in many cases.
Stefan
Re: [Qemu-devel] [PATCH 1/2] coroutine: introduce coroutines
Quoted 3 lines · Stefan Hajnoczi
My current plan is to try using sigaltstack(2) instead of makecontext()/swapcontext() as a hack since OpenBSD doesn't have makecontext()/swapcontext().
sigaltstack() is just a system call to tell the system about an alternative signal stack - that you have allocated yourself using malloc(). According to 'info libc "Signal Stack"'. It won't help you get a new stack by itself.
Maybe take a look at what GNU Pth does. It has a similar matrix of tested platforms using different strategies on each, though it is slightly different because it obviously doesn't link with libpthread.so (it provides it!), and it has to context switch from the SIGALRM handler for pre-emption.
Quoted 5 lines
TBH I'm almost at the stage where I think we should just use threads and/or async callbacks, as appropriate. Hopefully I'll be able to cook up a reasonably portable implementation of coroutines though, because the prospect of having to go fully threaded or do async callbacks isn't attractive in many cases.
Another classic trick is just to call a function recursively which has a large local array(*), setjmp() every M calls, and longjmp() back to the start after M*N calls. That gets you N setjmp() contexts to switch between, all in the same larger stack so it's fine even with old pthread implementations, providing the total stack used isn't too big, and the individual stacks you've allocated aren't too small for the program.
If the large local array insists on being optimised away, it's probably better anyway to track the address of a local variable, and split the stack whenever the address has changed by enough. Try to make sure the compiler doesn't optimise away the tail recursion :-)
It works better on non-threaded programs as per-thread stacks are more likely to have limited size. *But* the initial thread often has a large growable stack, just like a single-threaded program. So it's a good idea to do the stack carving in the initial thread (doesn't necessarily have to be at the start of the program). You may be able to add guard pages afterwards with mprotect() if you're paranoid :-)
-- Jamie
Re: [Qemu-devel] [PATCH 1/2] coroutine: introduce coroutines
Quoted 9 lines · Jamie Lokier
Quoted 3 lines · Stefan Hajnoczi
My current plan is to try using sigaltstack(2) instead of makecontext()/swapcontext() as a hack since OpenBSD doesn't have makecontext()/swapcontext().
sigaltstack()is just a system call to tell the system about an alternative signal stack - that you have allocated yourself usingmalloc(). According to 'info libc "Signal Stack"'. It won't help you get a new stack by itself.
Issue sigaltstack() with the malloced new stack. Send yourself a signal and in a custom signal handler setjmp() to stash away the state (you're now on the new stack).
Quoted 13 lines
Maybe take a look at what GNU Pth does. It has a similar matrix of tested platforms using different strategies on each, though it is slightly different because it obviously doesn't link with libpthread.so (it provides it!), and it has to context switch from the SIGALRM handler for pre-emption.
Quoted 5 lines
TBH I'm almost at the stage where I think we should just use threads and/or async callbacks, as appropriate. Hopefully I'll be able to cook up a reasonably portable implementation of coroutines though, because the prospect of having to go fully threaded or do async callbacks isn't attractive in many cases.
Quoted 7 lines
Another classic trick is just to call a function recursively which has a large local
array(*),setjmp()every M calls, andlongjmp()back to the start after M*N calls. That gets you Nsetjmp()contexts to switch between, all in the same larger stack so it's fine even with old pthread implementations, providing the total stack used isn't too big, and the individual stacks you've allocated aren't too small for the program.
True, I think I've done something like this with alloca() before :(. It's extremely hacky though.
Stefan
Re: [Qemu-devel] [PATCH 1/2] coroutine: introduce coroutines
Quoted 6 lines · Stefan Hajnoczi
Quoted 5 lines · Jamie Lokier
Quoted 3 lines · Stefan Hajnoczi
My current plan is to try using sigaltstack(2) instead of makecontext()/swapcontext() as a hack since OpenBSD doesn't have makecontext()/swapcontext().
Quoted 8 lines
Quoted 4 lines
sigaltstack()is just a system call to tell the system about an alternative signal stack - that you have allocated yourself usingmalloc(). According to 'info libc "Signal Stack"'. It won't help you get a new stack by itself.Issue
sigaltstack()with the malloced new stack. Send yourself a signal and in a custom signal handlersetjmp()to stash away the state (you're now on the new stack).
Is any of this really easier than simply writing 20-30 lines of assembly to do what you Really Want And Nothing Else?
Honestly, this is qemu we're talking about, and we assume you've already ported TCG to the host cpu plus abi. How hard is it to just DTRT with a qemu-specific routine, anyway?
r~
Re: [Qemu-devel] [PATCH 1/2] coroutine: introduce coroutines
In reply to Stefan HajnocziQuoted 8 lines · Stefan Hajnoczi
On Tue, May 24, 2011 at 08:37:50PM +0100, Jamie Lokier wrote:
Thanks, I read about that but didn't try to implement special cases because I don't have relevant OSes here to test against.My current plan is to try using sigaltstack(2) instead of makecontext()/swapcontext() as a hack since OpenBSD doesn't have makecontext()/swapcontext().
Quoted 5 lines
TBH I'm almost at the stage where I think we should just use threads and/or async callbacks, as appropriate. Hopefully I'll be able to cook up a reasonably portable implementation of coroutines though, because the prospect of having to go fully threaded or do async callbacks isn't attractive in many cases.
Why not use threads as a coroutine callback? That's essentially what we would do to be "fully threaded".
Regards,
Anthony Liguori
Quoted 3 lines
Stefan
Re: [Qemu-devel] [PATCH 1/2] coroutine: introduce coroutines
Quoted 2 lines · Anthony Liguori
Why not use threads as a coroutine fallback? That's essentially what we would do to be "fully threaded".
Not exactly, there would be much less synchronization going on. Using threads to implement coroutines means you go through the slow path of the synchronization primitives (either mutexes/condvars or barriers) twice or more per coroutine switch. It is really slow, a 100 times difference perhaps.
Paolo
Re: [Qemu-devel] [PATCH 1/2] coroutine: introduce coroutines
In reply to Stefan HajnocziQuoted 92 lines · Stefan Hajnoczi
Quoted 49 lines · Jamie Lokier
Quoted 15 lines · Daniel P. Berrange
Quoted 7 lines · Paolo Bonzini
Quoted 5 lines · Anthony Liguori
Quoted 2 lines
A very slow way, too (on Windows at least if you use
qemu_cond...).That doesn't mean you can't do a fiber implementation for Windows... but having a highly portable fallback is a good thing.
Quoted 6 lines
I agree but where would you place it, since QEMU is only portable to POSIX and Windows?
osdep-$(CONFIG_POSIX) +=
coroutine-posix.c
osdep-$(CONFIG_WIN32) +=coroutine-win32.c
osdep-??? +=coroutine-fallback.cQuoted 5 lines
NetBSD forbids the use of 'makecontext' in any application which also links to libpthread.so[1]. We used makecontext in GTK-VNC's coroutines and got random crashes in threaded apps running on NetBSD. So for NetBSD we tell people to use the thread based coroutines instead.
You have to use
swapcontext(), no wait, you have to usesetjmp(), no wait,_setjmp(), no wait, threads.... Read on.From Glibc's FAQ, setjmp/longjmp are not portable choices:
- UNIX provides no other (portable) way of effecting a synchronous context switch (also known as co-routine switch). Some versions support this via setjmp()/longjmp() but this does not work universally.
So in principle you should use
swapcontext()in portable code.(By the way, Glibc goes on about how it won't support
swapcontext()from async signal handlers, i.e. preemption, on some architectures (IA-64/S-390), and I know it has been very subtly broken from a signal handler on ARM. Fair enough, somehow disappointing, but doesn't matter for QEMU coroutines.)But
swapcontext()etc. have been withdrawn from POSIX 2008:- Functions to be deleted
Legacy: Delete all legacy functions except utimes (which should not be legacy).
OB: Default position is to delete all OB functions.XSI Functions to change state
Quoted 8 lines
....
_setjmpand_longjmp. Should become obsolete.
....
getcontext, setcontext, makecontext and swapcontext are already marked OB and should be withdrawn. And header file<ucontext.h>.OB means obsolescent. They were marked obsolescent a few versions prior, with the rationale that you can use threads instead...
Yep, aware of this but at the end of the day these functions are commonly available.
Quoted 10 lines
It's not surprising that NetBSD forbids
makecontext()with libpthread.so. I suspect old versions of FreeBSD, OpenBSD, DragonFly BSD, (and Mac OS X?), have the same restriction, because they have a similar pthreads evolutionary history to LinuxThreads. LinuxThreads also breaks when using coroutines that switch stacks, because it uses the stack pointer to know the current thread.(LinuxThreads is old now, but that particular quirk still affects me because some uCLinux platforms, on which I wish to use coroutines, still don't have working NPTL - but they aren't likely to be running QEMU :-)
That is nasty.
Quoted 9 lines
Finally, if you are using setjmp/longjmp, consider (from FreeBSD man page):
The setjmp()/longjmp() pairs save and restore the signal mask while _setjmp()/_longjmp() pairs save and restore only the register set and the stack. (See sigprocmask(2).)
As setjmp/longjmp were chosen for performance, you may wish to use _setjmp/_longjmp instead (when available), as swizzling the signal mask on each switch may involve a system call and be rather slow.
Thanks, I read about that but didn't try to implement special cases because I don't have relevant OSes here to test against.
My current plan is to try using sigaltstack(2) instead of makecontext()/swapcontext() as a hack since OpenBSD doesn't have makecontext()/swapcontext().
Quoted 5 lines
TBH I'm almost at the stage where I think we should just use threads and/or async callbacks, as appropriate. Hopefully I'll be able to cook up a reasonably portable implementation of coroutines though, because the prospect of having to go fully threaded or do async callbacks isn't attractive in many cases.
I'm meant to say threads as a coroutine fallback.
Regards,
Anthony Liguori
Quoted 3 lines
Stefan
Re: [Qemu-devel] [PATCH 1/2] coroutine: introduce coroutines
In reply to Stefan HajnocziQuoted 6 lines · Stefan Hajnoczi
On Tue, May 24, 2011 at 08:37:50PM +0100, Jamie Lokier wrote:
TBH I'm almost at the stage where I think we should just use threads and/or async callbacks, as appropriate. Hopefully I'll be able to cook up a reasonably portable implementation of coroutines though, because the prospect of having to go fully threaded or do async callbacks isn't attractive in many cases.
May be Stupid question: why not using pcl lib ?
Bastien
Quoted 1 line
Stefan
Re: [Qemu-devel] [PATCH 1/2] coroutine: introduce coroutines
In reply to Stefan HajnocziQuoted 8 lines · Stefan Hajnoczi
From: Kevin Wolf<[email protected]>
Asynchronous 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.
Quoted 8 lines
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);
Why do away with yieldto?
Do we have performance data around setjmp vs. setcontext?
Quoted 198 lines
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 invokeqemu_coroutine_enter()once more to switch back to the coroutine.Note that coroutines never execute concurrently and should only be used from threads which hold the global mutex. This restriction 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 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().Signed-off-by Kevin Wolf<[email protected]>Signed-off-by Stefan Hajnoczi<[email protected]>--- Makefile.objs | 7 +++ coroutine-ucontext.c | 73 +++++++++++++++++++++++++++ coroutine-win32.c | 57 +++++++++++++++++++++ qemu-coroutine-int.h | 57 +++++++++++++++++++++ qemu-coroutine.c | 132 ++++++++++++++++++++++++++++++++++++++++++++++++++ qemu-coroutine.h | 82 +++++++++++++++++++++++++++++++ trace-events | 5 ++ 7 files changed, 413 insertions(+), 0 deletions(-)create mode 100644
coroutine-ucontext.c
create mode 100644coroutine-win32.c
create mode 100644qemu-coroutine-int.h
create mode 100644qemu-coroutine.c
create mode 100644qemu-coroutine.hMakefile.objs +7
-0@@ -11,6 +11,12 @@ oslib-obj-$(CONFIG_WIN32) += oslib-win32.o oslib-obj-$(CONFIG_POSIX) += oslib-posix.o ####################################################################### +# coroutines +coroutine-obj-y = qemu-coroutine.o +coroutine-obj-$(CONFIG_POSIX) += coroutine-ucontext.o +coroutine-obj-$(CONFIG_WIN32) += coroutine-win32.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@@ -67,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.ocoroutine-ucontext.c new file +73
-0@@ -0,0 +1,73 @@ +/* +* ucontext coroutine initialization code +* +* Copyright (C) 2006 Anthony Liguori<[email protected]> +* Copyright (C) 2011 Kevin Wolf<[email protected]> +* +* This library is free software; you can redistribute it and/or +* modify it under the terms of the GNU Lesser General Public +* License as published by the Free Software Foundation; either +* version 2.0 of the License, or (at your option) any later version. +* +* This library is distributed in the hope that it will be useful, +* but WITHOUT ANY WARRANTY; without even the implied warranty of +* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +* Lesser General Public License for more details. +* +* You should have received a copy of the GNU Lesser General Public +* License along with this library; if not, write to the Free Software +* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA +*/ + +/* XXX Is there a nicer way to disable glibc's stack check for longjmp? */ +#ifdef _FORTIFY_SOURCE +#undef _FORTIFY_SOURCE +#endif +#include <setjmp.h> +#include <stdint.h> +#include <ucontext.h> +#include "qemu-coroutine-int.h" + +static Coroutine *new_coroutine; + +static void continuation_trampoline (void ) +{ +Coroutine *co = new_coroutine; + +/* Initialize longjmp environment and switch back to +* qemu_coroutine_init_env() in the old ucontext. */ +if (!setjmp (co->env)) { +return ; + } + +while (true) { + co->entry (co->data); +if (!setjmp (co->env)) { +longjmp (co->caller->env,COROUTINE_TERMINATE ); + } + } +} + +int qemu_coroutine_init_env (Coroutine *co) +{ +ucontext_t old_uc, uc; + +/* Create a new ucontext for switching to the coroutine stack and setting +* up a longjmp environment. */ +if (getcontext (&uc) ==-1 ) { +return -errno; + } + + uc.uc_link =&old_uc; + uc.uc_stack.ss_sp = co->stack; + uc.uc_stack.ss_size = co->stack_size; + uc.uc_stack.ss_flags =0 ; + + new_coroutine = co; +makecontext (&uc, (void *)continuation_trampoline,0 ); + +/* Initialize the longjmp environment */ +swapcontext (&old_uc,&uc); + +return 0 ; +}coroutine-win32.c new file +41
-0@@ -0,0 +1,57 @@ +/* +* Win32 coroutine initialization code +* +* Copyright (c) 2011 Kevin Wolf<[email protected]> +* +* Permission is hereby granted, free of charge, to any person obtaining a copy +* of this software and associated documentation files (the "Software"), to deal +* in the Software without restriction, including without limitation the rights +* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +* copies of the Software, and to permit persons to whom the Software is +* furnished to do so, subject to the following conditions: +* +* The above copyright notice and this permission notice shall be included in +* all copies or substantial portions of the Software. +* +* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL +* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN +* THE SOFTWARE. +*/ + +#include "qemu-coroutine-int.h" + +static void __attribute__((used))trampoline (Coroutine *co) +{ +if (!setjmp (co->env)) { +return ; + } + +while (true) { + co->entry (co->data); +if (!setjmp (co->env)) { +longjmp (co->caller->env,COROUTINE_TERMINATE ); + } + } +} + +int qemu_coroutine_init_env (Coroutine *co)
coroutine-win32.c line 51
Quoted 10 lines
coroutine-win32.c new file +10
-0+{ +#ifdef __i386__ + asmvolatile ( +"mov %%esp, %%ebx;" +"mov %0, %%esp;" +"pushl %1;" +"call _trampoline;" +"mov %%ebx, %%esp;" + : :"r" (co->stack + co->stack_size),"r" (co) :"ebx" + );
So the only Linux host we support is x86??
We can't reasonably do this IMHO. If we're going to go this route, we should at least fall back to setcontext for the sake of portability.
Regards,
Anthony Liguori
Re: [Qemu-devel] [PATCH 1/2] coroutine: introduce coroutines
Quoted 5 lines · Anthony Liguori
So the only Linux host we support is x86??
We can't reasonably do this IMHO. If we're going to go this route, we should at least fall back to setcontext for the sake of portability.
That was:
coroutine-win32.c new file +0 -0
See my reply too.
Paolo
Re: [Qemu-devel] [PATCH 1/2] coroutine: introduce coroutines
coroutine-win32.c
Quoted 2 lines · Paolo Bonzini
On 05/11/2011 02:36 PM, Anthony Liguori wrote:
coroutine-win32.c +0
-0
Kevin: This reminds me that I did not run ./check-coroutine on win32. If you are able to run it in your environment that would be good to make sure the Windows code works.
Stefan
Re: [Qemu-devel] [PATCH 1/2] coroutine: introduce coroutines
In reply to Anthony LiguoriAm 11.05.2011 14:36, schrieb Anthony Liguori:
Quoted 13 lines · Stefan Hajnoczi
Quoted 12 lines
From: Kevin Wolf<[email protected]>
Asynchronous 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.
Quoted 8 lines
Quoted 4 lines
Creating and starting a coroutine is easy:
coroutine = qemu_coroutine_create(my_coroutine); qemu_coroutine_enter(coroutine, my_data);Why do away with yieldto?
Do we have performance data around setjmp vs. setcontext?
I did some quick ad-hoc tests when I introduced setjmp, but don't have them any more. IIRC, it was something like a factor of 3.
Quoted 60 lines
Quoted 47 lines
coroutine-win32.c new file +41
-0@@ -0,0 +1,57 @@ +/* +* Win32 coroutine initialization code +* +* Copyright (c) 2011 Kevin Wolf<[email protected]> +* +* Permission is hereby granted, free of charge, to any person obtaining a copy +* of this software and associated documentation files (the "Software"), to deal +* in the Software without restriction, including without limitation the rights +* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +* copies of the Software, and to permit persons to whom the Software is +* furnished to do so, subject to the following conditions: +* +* The above copyright notice and this permission notice shall be included in +* all copies or substantial portions of the Software. +* +* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL +* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN +* THE SOFTWARE. +*/ + +#include "qemu-coroutine-int.h" + +static void __attribute__((used))trampoline (Coroutine *co) +{ +if (!setjmp (co->env)) { +return ; + } + +while (true) { + co->entry (co->data); +if (!setjmp (co->env)) { +longjmp (co->caller->env,COROUTINE_TERMINATE ); + } + } +} + +int qemu_coroutine_init_env (Coroutine *co)coroutine-win32.c line 51
Quoted 10 lines
coroutine-win32.c new file +10
-0+{ +#ifdef __i386__ + asmvolatile ( +"mov %%esp, %%ebx;" +"mov %0, %%esp;" +"pushl %1;" +"call _trampoline;" +"mov %%ebx, %%esp;" + : :"r" (co->stack + co->stack_size),"r" (co) :"ebx" + );So the only Linux host we support is x86??
Quoted 2 lines
We can't reasonably do this IMHO. If we're going to go this route, we should at least fall back to setcontext for the sake of portability.
This is win32 code, Linux uses ucontext for initialization.
Kevin
Re: [Qemu-devel] [PATCH 1/2] coroutine: introduce coroutines
In reply to Stefan HajnocziAm 11.05.2011 12:15, schrieb Stefan Hajnoczi:
Quoted 41 lines
From: Kevin Wolf <[email protected]>
Asynchronous 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_fnmy_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 invokeqemu_coroutine_enter()once more to switch back to the coroutine.Note that coroutines never execute concurrently and should only be used from threads which hold the global mutex. This restriction 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.
Quoted 8 lines
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().Signed-off-by Kevin Wolf <[email protected]>Signed-off-by Stefan Hajnoczi <[email protected]>---
Hi Stefan,
you might want to add the following or a similar patch:
qemu-coroutine.c +4 -1
@@ -91,7 +91,10 @@ static void *coroutine_swap(Coroutine *from, Coroutine *to, void *opaque)case COROUTINE_TERMINATE : current = to->caller;qemu_coroutine_terminate (to); -return to->data; + opaque = to->data; +qemu_free (to->stack); +qemu_free (to); +return opaque;default :/* Switch to called coroutine */ current = to;
I tested your test code with Valgrind. Beside of the memory leaks which are fixed with the small modification shown above, Valgrind has a lot of complains. Maybe you can try it yourself, otherwise please wait until I have finished analyzing the Valgrind results. At a first glance, I'm afraid that debugging with gdb or Valgrind might become more difficult when coroutines are used. This is different with threads: they are fully supported by gdb.
The w32 build needs additional libraries (ws2_32, maybe more), then check-coroutine works.
Cheers,
Stefan W.
Re: [Qemu-devel] [PATCH 1/2] coroutine: introduce coroutines
Am 11.05.2011 21:12, schrieb Stefan Weil:
Quoted 72 lines
Am 11.05.2011 12:15, schrieb Stefan Hajnoczi:
Quoted 41 lines
From: Kevin Wolf <[email protected]>
Asynchronous 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_fnmy_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 invokeqemu_coroutine_enter()once more to switch back to the coroutine.Note that coroutines never execute concurrently and should only be used from threads which hold the global mutex. This restriction 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.
Quoted 8 lines
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().Signed-off-by Kevin Wolf <[email protected]>Signed-off-by Stefan Hajnoczi <[email protected]>---
Hi Stefan,
you might want to add the following or a similar patch:
qemu-coroutine.c +4
-1@@ -91,7 +91,10 @@ static void *coroutine_swap(Coroutine *from, Coroutine *to, void *opaque)case COROUTINE_TERMINATE : current = to->caller;qemu_coroutine_terminate (to); -return to->data; + opaque = to->data; +qemu_free (to->stack); +qemu_free (to); +return opaque;default :/* Switch to called coroutine */
Quoted 1 line
current = to;
No, this is wrong. qemu_coroutine_terminate() puts the coroutine back into a pool, so you can't free it.
Quoted 3 lines
I tested your test code with Valgrind. Beside of the memory leaks which are fixed
with the small modification shown above, Valgrind has a lot of complains.
Hm, for me Valgrind doesn't show any memory leaks, only some "uninitialised value of size 8" in the trampoline. I don't quite understand these.
Quoted 4 lines
Maybe you can try it yourself, otherwise please wait until I have finished analyzing the Valgrind results. At a first glance, I'm afraid that debugging with gdb or Valgrind might become more difficult when coroutines are used. This is different with threads: they are fully supported by gdb.
Yes, the debugging support is less than optimal. However, threads are not the right comparison. Coroutines are meant to replace AIOCBs and callbacks, and these are already hard to debug today. So I don't think it becomes more difficult.
Kevin
Re: [Qemu-devel] [PATCH 1/2] coroutine: introduce coroutines
In reply to Stefan HajnocziQuoted 35 lines · Stefan Hajnoczi
From: Kevin Wolf <[email protected]>
Asynchronous 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 invokeqemu_coroutine_enter()once more to switch back to the coroutine.
Quoted 5 lines
Note that coroutines never execute concurrently and should only be used from threads which hold the global mutex. This restriction 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.
Mmh, is there anything that conceptually prevent fixing this limitation later on? I would really like to remove such dependency long-term as well to have VCPUs operate truly independently on independent device models.
Jan
Signature · 2 lines
Siemens AG, Corporate Technology, CT T DE IT 1 Corporate Competence Center Embedded Linux
Re: [Qemu-devel] [PATCH 1/2] coroutine: introduce coroutines
Quoted 42 lines · Jan Kiszka
Quoted 35 lines · Stefan Hajnoczi
From: Kevin Wolf <[email protected]>
Asynchronous 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_fnmy_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 invokeqemu_coroutine_enter()once more to switch back to the coroutine.Quoted 5 lines
Note that coroutines never execute concurrently and should only be used from threads which hold the global mutex. This restriction 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.
Quoted 3 lines
Mmh, is there anything that conceptually prevent fixing this limitation later on? I would really like to remove such dependency long-term as well to have VCPUs operate truly independently on independent device models.
The use case that has motivated coroutines is the block layer. It is synchronous in many places and definitely not thread-safe. Coroutines is a step that solves the "synchronous" part of the problem but does not tackle the "not thread-safe" part.
It is possible to move from coroutines to threads but we need to remove single-thread assumptions from all the block layer code, which isn't a small task. Coroutines does not prevent us from making the block layer thread-safe!
Stefan
Re: [Qemu-devel] [PATCH 1/2] coroutine: introduce coroutines
Quoted 52 lines · Stefan Hajnoczi
Quoted 42 lines · Jan Kiszka
Quoted 35 lines · Stefan Hajnoczi
From: Kevin Wolf <[email protected]>
Asynchronous 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_fnmy_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 invokeqemu_coroutine_enter()once more to switch back to the coroutine.Quoted 5 lines
Note that coroutines never execute concurrently and should only be used from threads which hold the global mutex. This restriction 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.
Quoted 3 lines
Mmh, is there anything that conceptually prevent fixing this limitation later on? I would really like to remove such dependency long-term as well to have VCPUs operate truly independently on independent device models.
The use case that has motivated coroutines is the block layer. It is synchronous in many places and definitely not thread-safe. Coroutines is a step that solves the "synchronous" part of the problem but does not tackle the "not thread-safe" part.
Quoted 4 lines
It is possible to move from coroutines to threads but we need to remove single-thread assumptions from all the block layer code, which isn't a small task. Coroutines does not prevent us from making the block layer thread-safe!
Keeping in mind that you may have to do some of the work even with coroutines. If the code is not thread safe, it may contain assumptions that certain state does not change when it makes blocking I/O calls, which stops being true once you have coroutines and replace the I/O calls with async calls. But at least the checking can be confined to those places in the code.
It's quite similar to the Linux BKL - scheduling points have to be checked but nowhere else does. And, like the BKL, it could be "pushed down" in stages over a long time period, to convert the coroutine code over to concurrent threads over time, rather than in a single step.
By the end, even with full concurrency, there is still some potential for coroutines, and/or async calls, to be useful for performance balancing.
-- Jamie
Re: [Qemu-devel] [PATCH 1/2] coroutine: introduce coroutines
In reply to Jan KiszkaAm 12.05.2011 11:51, schrieb Jan Kiszka:
Quoted 42 lines · Stefan Hajnoczi
Quoted 35 lines
From: Kevin Wolf <[email protected]>
Asynchronous 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 invokeqemu_coroutine_enter()once more to switch back to the coroutine.Quoted 5 lines
Note that coroutines never execute concurrently and should only be used from threads which hold the global mutex. This restriction 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.
Quoted 3 lines
Mmh, is there anything that conceptually prevent fixing this limitation later on? I would really like to remove such dependency long-term as well to have VCPUs operate truly independently on independent device models.
I think it's the defining property of coroutines. If you remove it, you have full threads. Going from coroutines to threads may be an option in the long term. The advantage of coroutines is that they provide an incremental way forward from where we are today. This restriction is really what makes the transition easy.
Kevin
PATCH 2/2 coroutine: add check-coroutine automated tests
In reply to Stefan HajnocziTo run automated tests for coroutines:
make check-coroutine
./check-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.
--- Makefile | 3 +- check-coroutine.c | 188 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 190 insertions(+), 1 deletions(-) create mode 100644 check-coroutine.c
Makefile +2 -1
@@ -130,7 +130,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 check-coroutine.o: $(GENERATED_HEADERS) CHECK_PROG_DEPS = qemu-malloc.o $(oslib-obj-y) $(trace-obj-y)
@@ -140,6 +140,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 $(CHECK_PROG_DEPS) +check-coroutine: check-coroutine.o $(coroutine-obj-y) $(CHECK_PROG_DEPS) QEMULIBS=libhw32 libhw64 libuser libdis libdis-user
check-coroutine.c new file +188 -0
188 lines not on this page · raw message
Signature · 1 line
1.7.4.4