C++20 coroutine backend
Subsystems (2)
cover C++20 coroutine backend
It turns out that going from a prototype C++ implementation of the QEMU API, to something that could build tests/unit/test-coroutine, was just a few hours work; and once it compiled, only one line had to be changed for every test to pass.
Most of the differences between C and C++ already show up here:
- keywords such as "new" (or "class", which I didn't encounter yet)
- _Generic must be replaced by templates and/or overloading (QemuCoLockable is implemented completely different from QemuLockable, in fact I spent most of the time on that)
- PRI* functions must be separated with a space from string constants that precede it
- void* casts must be explicit (g_new takes care of that most of the time, but not for opaque pointers passed to coroutine).
There are 300 lines of hard-core C++ in the backend and in coroutine.h. I tried to comment it as much as possible (this time I didn't include a big commit message on stackless coroutines in general) but it still requires some knowledge of the basic C++ coroutine concepts of resumable types, promise types and awaiter types.
https://www.youtube.com/watch?v=ZTqHjjm86Bw is an excellent introduction and it's where I learnt most of what was needed.
However, there are no ramifications to actual coroutine code, except for the template syntax "CoroutineFn<return_type>" for the function and the mandatory co_await/co_return keywords... both of which are an improvement, really: the fact that a single function cannot run either inside or outside coroutines is checked by the compiler now, because qemu_coroutine_create accepts a function that returns CoroutineFn<void>. Therefore I had to disable some more code in util/ and qapi/ that used qemu_in_coroutine() or coroutine_fn.
Here is the performance comparison of the three backends:
ucontext stackless C stackless C++ /perf/lifecycle 0.068 s 0.025 s 0.065 s /perf/nesting 55 s 4.7 s 1.7 s /perf/yield 6.0 s 1.3 s 1.3 s /perf/cost 8 Mops/s (125ns) 35 ns 10000 Mops/s (99 ns)
One important difference is that C++ coroutines allocate frames on the heap, and that explains why performance is better in /perf/nesting, which has to do many large memory allocations for the stack in the other two backends (and also a makecontext/swapcontext in the ucontext case). C++ coroutines hardly benefit from the coroutine pool; OTOH that also means the coroutine pool could be removed if we went this way.
I haven't checked why /perf/lifecycle (and therefore /perf/cost; they are roughly the same test) is so much slower than the handwritten C code. It's still comparable with the ucontext backend though.
Overall this was ~twice the amount of work of the C experiment, but that's because the two are very different ways to achieve the same goal:
- the design work was substantially smaller in the C experiment, where all the backend does is allocate stack frames and do a loop that invokes a function pointer. Here the backend has to map between the C++ concepts and the QEMU API. In the C case, most of the work was really in the manual conversion which I had to do one function at a time.
- the remaining work is also completely different: a source-to-source translator (and only build system work in QEMU) for the C experiment; making ~100 files compile in C++ for this one (and relatively little work as far as coroutines are concerned).
This was compiled with GCC 11 only. Coroutine support was added in GCC 10, released in 2020, which IIRC is much newer than the most recent release we support.
Paolo
Paolo Bonzini (17):
coroutine: add missing coroutine_fn annotations for CoRwlock functions
coroutine: qemu_coroutine_get_aio_context is not a coroutine_fn
coroutine: small code cleanup in qemu_co_rwlock_wrlock
coroutine: introduce QemuCoLockable
port atomic.h to C++
use g_new0 instead of g_malloc0
start porting compiler.h to C++
tracetool: add extern "C" around generated headers
start adding extern "C" markers
add space between liter and string macro
bump to C++20
remove "new" keyword from trace-events
disable some code
util: introduce C++ stackless coroutine backend
port QemuCoLockable to C++ coroutines
port test-coroutine to C++ coroutines
configure | 48 +-
include/block/aio.h | 5 +
include/fpu/softfloat-types.h | 4 +
include/qemu/atomic.h | 5 +
include/qemu/bitops.h | 3 +
include/qemu/bswap.h | 10 +-
include/qemu/co-lockable.h | 93 ++++
include/qemu/compiler.h | 4 +
include/qemu/coroutine.h | 466 +++++++++++++-----
include/qemu/coroutine_int.h | 8 +
include/qemu/host-utils.h | 4 +
include/qemu/lockable.h | 13 +-
include/qemu/notify.h | 4 +
include/qemu/osdep.h | 1 +
include/qemu/qsp.h | 4 +
include/qemu/thread.h | 4 +
include/qemu/timer.h | 6 +-
include/qemu/typedefs.h | 1 +
meson.build | 2 +-
qapi/qmp-dispatch.c | 2 +
scripts/tracetool/format/h.py | 8 +-
tests/unit/meson.build | 8 +-
.../{test-coroutine.c => test-coroutine.cc} | 138 +++---
util/async.c | 2 +
util/coroutine-stackless.cc | 145 ++++++
util/meson.build | 14 +-
...oroutine-lock.c => qemu-coroutine-lock.cc} | 78 +--
...outine-sleep.c => qemu-coroutine-sleep.cc} | 10 +-
util/{qemu-coroutine.c => qemu-coroutine.cc} | 18 +-
util/thread-pool.c | 2 +
util/trace-events | 40 +-
31 files changed, 805 insertions(+), 345 deletions(-)
create mode 100644 include/qemu/co-lockable.h
rename tests/unit/{test-coroutine.c => test-coroutine.cc} (81%)
create mode 100644 util/coroutine-stackless.cc
rename util/{qemu-coroutine-lock.c => qemu-coroutine-lock.cc} (86%)
rename util/{qemu-coroutine-sleep.c => qemu-coroutine-sleep.cc} (89%)
rename util/{qemu-coroutine.c => qemu-coroutine.cc} (93%)Signature · 1 line
2.35.1
PATCH 1/16 coroutine: add missing coroutine_fn annotations for CoRwlock functions
CoRwlock can only be taken or released from a coroutine, and it can yield. Mark it as coroutine_fn.
--- include/qemu/coroutine.h | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-)
include/qemu/coroutine.h +5 -5
@@ -261,7 +261,7 @@ void qemu_co_rwlock_init(CoRwlock *lock);* of a parallel writer, control is transferred to the caller of the current * coroutine. */ -void qemu_co_rwlock_rdlock (CoRwlock *lock); +void coroutine_fn qemu_co_rwlock_rdlock(CoRwlock *lock);/** * Write Locks the CoRwlock from a reader. This is a bit more efficient than
@@ -270,7 +270,7 @@ void qemu_co_rwlock_rdlock(CoRwlock *lock);* to the caller of the current coroutine; another writer might run while * @qemu_co_rwlock_upgrade blocks. */ -void qemu_co_rwlock_upgrade (CoRwlock *lock); +void coroutine_fn qemu_co_rwlock_upgrade(CoRwlock *lock);/** * Downgrades a write-side critic section to a reader. Downgrading with
@@ -278,20 +278,20 @@ void qemu_co_rwlock_upgrade(CoRwlock *lock);* followed by @qemu_co_rwlock_rdlock. This makes it more efficient, but * may also sometimes be necessary for correctness. */ -void qemu_co_rwlock_downgrade (CoRwlock *lock); +void coroutine_fn qemu_co_rwlock_downgrade(CoRwlock *lock);/** * Write Locks the mutex. If the lock cannot be taken immediately because * of a parallel reader, control is transferred to the caller of the current * coroutine. */ -void qemu_co_rwlock_wrlock (CoRwlock *lock); +void coroutine_fn qemu_co_rwlock_wrlock(CoRwlock *lock);/** * Unlocks the read/write lock and schedules the next coroutine that was * waiting for this lock to be run. */ -void qemu_co_rwlock_unlock (CoRwlock *lock); +void coroutine_fn qemu_co_rwlock_unlock(CoRwlock *lock); typedef struct QemuCoSleep { Coroutine *to_wake;
Signature · 1 line
2.35.1
PATCH 2/16 coroutine: qemu_coroutine_get_aio_context is not a coroutine_fn
In reply to Paolo BonziniSince it operates on a given coroutine, qemu_coroutine_get_aio_context can be called from outside coroutine context.
This is for example how qio_channel_restart_read uses it.
--- include/qemu/coroutine.h | 2 +- util/qemu-coroutine.c | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-)
include/qemu/coroutine.h +1 -1
@@ -92,7 +92,7 @@ void coroutine_fn qemu_coroutine_yield(void);/** * Get the AioContext of the given coroutine */ -AioContext *coroutine_fn qemu_coroutine_get_aio_context(Coroutine *co); +AioContext *qemu_coroutine_get_aio_context (Coroutine *co);/** * Get the currently executing coroutine
util/qemu-coroutine.c +1 -1
@@ -200,7 +200,7 @@ bool qemu_coroutine_entered(Coroutine *co)return co->caller; } -AioContext *coroutine_fn qemu_coroutine_get_aio_context(Coroutine *co) +AioContext *qemu_coroutine_get_aio_context (Coroutine *co) {return co->ctx; }
Signature · 1 line
2.35.1
PATCH 3/16 coroutine: small code cleanup in qemu_co_rwlock_wrlock
In reply to Paolo Bonziniqemu_co_rwlock_wrlock stores the current coroutine in a loc variable, use it instead of calling qemu_coroutine_self() again.
--- util/qemu-coroutine-lock.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
util/qemu-coroutine-lock.c +1 -1
@@ -436,7 +436,7 @@ void qemu_co_rwlock_wrlock(CoRwlock *lock) lock->owners =-1 ;qemu_co_mutex_unlock (&lock->mutex); }else { -CoRwTicket my_ticket = { false,qemu_coroutine_self () }; +CoRwTicket my_ticket = { false, self };QSIMPLEQ_INSERT_TAIL (&lock->tickets, &my_ticket, next);qemu_co_mutex_unlock (&lock->mutex);
Signature · 1 line
2.35.1
Re: [PATCH experiment 03/16] coroutine: small code cleanup in qemu_co_rwlock_wrlock
Quoted 7 lines · Paolo Bonzini
qemu_co_rwlock_wrlockstores the current coroutine in a loc variable, use it instead of callingqemu_coroutine_self()again.Signed-off-by Paolo Bonzini <[email protected]>--- util/qemu-coroutine-lock.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
PATCH 4/16 coroutine: introduce QemuCoLockable
In reply to Paolo BonziniIn preparation for splitting "from coroutine" ("awaitable" in other languages) and "not from coroutine" functions, remove the CoMutex case from QemuLockable---thus making qemu_lockable_lock and qemu_lockable_unlock "not awaitable".
To satisfy the qemu_co_queue_wait use case, introduce QemuCoLockable which can be used for both QemuMutex (which will trivially never yield) and CoMutex. qemu_co_lockable_lock and qemu_co_lockable_unlock are coroutine_fns.
--- include/qemu/co-lockable.h | 98 +++++++++++++++++++++++++++++++++++++ include/qemu/coroutine.h | 5 +- include/qemu/lockable.h | 13 ++--- include/qemu/typedefs.h | 1 + tests/unit/test-coroutine.c | 10 ++-- util/qemu-coroutine-lock.c | 6 +-- 6 files changed, 114 insertions(+), 19 deletions(-) create mode 100644 include/qemu/co-lockable.h
include/qemu/co-lockable.h new file +98 -0
@@ -0,0 +1,98 @@ +/* +* Polymorphic locking functions (aka poor man templates) +* +* Copyright Red Hat, Inc. 2017, 2018 +* +* Author: Paolo Bonzini <[email protected]> +* +* This work is licensed under the terms of the GNU LGPL, version 2 or later. +* See the COPYING.LIB file in the top-level directory. +* +*/ + +#ifndef QEMU_CO_LOCKABLE_H +#define QEMU_CO_LOCKABLE_H + +#include "qemu/coroutine.h" +#include "qemu/thread.h" + +typedef void coroutine_fn QemuCoLockUnlockFunc(void *); + +struct QemuCoLockable { +void *object; +QemuCoLockUnlockFunc *lock; +QemuCoLockUnlockFunc *unlock; +}; + +static inline __attribute__((__always_inline__))QemuCoLockable * +qemu_make_co_lockable (void *x,QemuCoLockable *lockable) +{ +/* +* We cannot test this in a macro, otherwise we get compiler +* warnings like "the address of 'm' will always evaluate as 'true'". +*/ +return x ? lockable :NULL ; +} + +static inline __attribute__((__always_inline__))QemuCoLockable * +qemu_null_co_lockable (void *x) +{ +if (x !=NULL ) { +qemu_build_not_reached (); + } +return NULL ; +} + +/* +* In C, compound literals have the lifetime of an automatic variable. +* In C++ it would be different, but then C++ wouldn't need QemuCoLockable +* either... +*/ +#define QMCL_OBJ_ (x, name) (&(QemuCoLockable) { \ + .object = (x), \ + .lock = (QemuCoLockUnlockFunc *) qemu_ ## name ## _lock, \ + .unlock = (QemuCoLockUnlockFunc *) qemu_ ## name ## _unlock \ + }) + +/** +* QEMU_MAKE_CO_LOCKABLE - Make a polymorphic QemuCoLockable +* +* @x: a lock object (currently one of QemuMutex, CoMutex). +* +* Returns a QemuCoLockable object that can be passed around +* to a function that can operate with locks of any kind, or +* NULL if @x is %NULL. +* +* Note the speci case for void *, so that we may pass "NULL". +*/ +#define QEMU_MAKE_CO_LOCKABLE (x) \ + _Generic((x), QemuCoLockable *: (x), \ + void *: qemu_null_co_lockable(x), \ + QemuMutex *: qemu_make_co_lockable(x, QMCL_OBJ_(x, mutex)), \ + CoMutex *: qemu_make_co_lockable(x, QMCL_OBJ_(x, co_mutex))) \ + +/** +* QEMU_MAKE_CO_LOCKABLE_NONNULL - Make a polymorphic QemuCoLockable +* +* @x: a lock object (currently one of QemuMutex, QemuRecMutex, +* CoMutex, QemuSpin). +* +* Returns a QemuCoLockable object that can be passed around +* to a function that can operate with locks of any kind. +*/ +#define QEMU_MAKE_CO_LOCKABLE_NONNULL (x) \ + _Generic((x), QemuCoLockable *: (x), \ + QemuMutex *: QMCL_OBJ_(x, mutex), \ + CoMutex *: QMCL_OBJ_(x, co_mutex)) + +static inline void coroutine_fn qemu_co_lockable_lock(QemuCoLockable *x) +{ + x->lock (x->object); +} + +static inline void coroutine_fn qemu_co_lockable_unlock(QemuCoLockable *x) +{ + x->unlock (x->object); +} + +#endif
include/qemu/coroutine.h +3 -2
@@ -204,8 +204,8 @@ void qemu_co_queue_init(CoQueue *queue);* locked again afterwards. */ #define qemu_co_queue_wait (queue, lock) \ - qemu_co_queue_wait_impl(queue, QEMU_MAKE_LOCKABLE(lock)) -void coroutine_fn qemu_co_queue_wait_impl(CoQueue *queue, QemuLockable *lock); + qemu_co_queue_wait_impl(queue, QEMU_MAKE_CO_LOCKABLE(lock)) +void coroutine_fn qemu_co_queue_wait_impl(CoQueue *queue, QemuCoLockable *lock);/** * Removes the next coroutine from the CoQueue, and wake it up.
@@ -342,5 +342,6 @@ void qemu_coroutine_increase_pool_batch_size(unsigned int additional_pool_size);void qemu_coroutine_decrease_pool_batch_size (unsigned int additional_pool_size);#include "qemu/lockable.h" +#include "qemu/co-lockable.h" #endif /* QEMU_COROUTINE_H */
include/qemu/lockable.h +4 -9
@@ -13,7 +13,6 @@#ifndef QEMU_LOCKABLE_H #define QEMU_LOCKABLE_H -#include "qemu/coroutine.h" #include "qemu/thread.h" typedef void QemuLockUnlockFunc (void *);
@@ -57,8 +56,7 @@ qemu_null_lockable(void *x)/** * QEMU_MAKE_LOCKABLE - Make a polymorphic QemuLockable * -* @x: a lock object (currently one of QemuMutex, QemuRecMutex, -* CoMutex, QemuSpin). +* @x: a lock object (currently one of QemuMutex, QemuRecMutex, QemuSpin). * * Returns a QemuLockable object that can be passed around * to a function that can operate with locks of any kind, or
@@ -71,14 +69,12 @@ qemu_null_lockable(void *x) void *: qemu_null_lockable(x), \ QemuMutex *: qemu_make_lockable(x, QML_OBJ_(x, mutex)), \ QemuRecMutex *: qemu_make_lockable(x, QML_OBJ_(x, rec_mutex)), \ - CoMutex *: qemu_make_lockable(x, QML_OBJ_(x, co_mutex)), \ QemuSpin *: qemu_make_lockable(x, QML_OBJ_(x, spin)))/** * QEMU_MAKE_LOCKABLE_NONNULL - Make a polymorphic QemuLockable * -* @x: a lock object (currently one of QemuMutex, QemuRecMutex, -* CoMutex, QemuSpin). +* @x: a lock object (currently one of QemuMutex, QemuRecMutex, QemuSpin). * * Returns a QemuLockable object that can be passed around * to a function that can operate with locks of any kind.
@@ -87,7 +83,6 @@ qemu_null_lockable(void *x) _Generic((x),QemuLockable *: (x), \QemuMutex *:QML_OBJ_ (x, mutex), \QemuRecMutex *:QML_OBJ_ (x, rec_mutex), \ -CoMutex *:QML_OBJ_ (x, co_mutex), \ QemuSpin *: QML_OBJ_(x, spin))static inline void qemu_lockable_lock (QemuLockable *x)
@@ -124,7 +119,7 @@ G_DEFINE_AUTOPTR_CLEANUP_FUNC(QemuLockable, qemu_lockable_auto_unlock)/** * WITH_QEMU_LOCK_GUARD - Lock a lock object for scope * -* @x: a lock object (currently one of QemuMutex, CoMutex, QemuSpin). +* @x: a lock object (currently one of QemuMutex, QemuRecMutex, QemuSpin). * * This macro defines a lock scope such that entering the scope takes the lock * and leaving the scope releases the lock. Return statements are allowed
@@ -149,7 +144,7 @@ G_DEFINE_AUTOPTR_CLEANUP_FUNC(QemuLockable, qemu_lockable_auto_unlock)/** * QEMU_LOCK_GUARD - Lock an object until the end of the scope * -* @x: a lock object (currently one of QemuMutex, CoMutex, QemuSpin). +* @x: a lock object (currently one of QemuMutex, QemuRecMutex, QemuSpin). * * This macro takes a lock until the end of the scope. Return statements * release the lock.
include/qemu/typedefs.h +1 -0
@@ -103,6 +103,7 @@ typedef struct QBool QBool;typedef struct QDict QDict ;typedef struct QEMUBH QEMUBH ;typedef struct QemuConsole QemuConsole ; +typedef struct QemuCoLockable QemuCoLockable ;typedef struct QEMUFile QEMUFile ;typedef struct QemuLockable QemuLockable ;typedef struct QemuMutex QemuMutex ;
tests/unit/test-coroutine.c +5 -5
@@ -213,13 +213,13 @@ static void coroutine_fn mutex_fn(void *opaque) static void coroutine_fn lockable_fn(void *opaque) { -QemuLockable *x = opaque; -qemu_lockable_lock (x); +QemuCoLockable *x = opaque; +qemu_co_lockable_lock (x);assert (!locked); locked = true;qemu_coroutine_yield (); locked = false; -qemu_lockable_unlock (x); +qemu_co_lockable_unlock (x); done++; }
@@ -259,9 +259,9 @@ static void test_co_mutex_lockable(void)CoMutex *null_pointer =NULL ;qemu_co_mutex_init (&m); -do_test_co_mutex (lockable_fn,QEMU_MAKE_LOCKABLE (&m)); +do_test_co_mutex (lockable_fn,QEMU_MAKE_CO_LOCKABLE (&m)); -g_assert (QEMU_MAKE_LOCKABLE (null_pointer) ==NULL ); +g_assert (QEMU_MAKE_CO_LOCKABLE (null_pointer) ==NULL ); }static CoRwlock rwlock;
util/qemu-coroutine-lock.c +3 -3
@@ -39,13 +39,13 @@ void qemu_co_queue_init(CoQueue *queue)QSIMPLEQ_INIT (&queue->entries); } -void coroutine_fn qemu_co_queue_wait_impl(CoQueue *queue, QemuLockable *lock) +void coroutine_fn qemu_co_queue_wait_impl(CoQueue *queue, QemuCoLockable *lock) {Coroutine *self =qemu_coroutine_self ();QSIMPLEQ_INSERT_TAIL (&queue->entries, self, co_queue_next);if (lock) { -qemu_lockable_unlock (lock); +qemu_co_lockable_unlock (lock); }/* There is no race condition here. Other threads will call
@@ -63,7 +63,7 @@ void coroutine_fn qemu_co_queue_wait_impl(CoQueue *queue, QemuLockable *lock)* other cases of QemuLockable. */ if (lock) { -qemu_lockable_lock (lock); +qemu_co_lockable_lock (lock); } }
Signature · 1 line
2.35.1
PATCH 5/16 port atomic.h to C++
In reply to Paolo BonziniThe functionality of typeof_strip_qual is provided by the standard library.
--- include/qemu/atomic.h | 5 +++++ include/qemu/osdep.h | 1 + 2 files changed, 6 insertions(+)
include/qemu/atomic.h +5 -0
@@ -26,6 +26,10 @@* implicit promotion. int and larger types, as well as pointers, can be * converted to a non-qualified type just by applying a binary operator. */ +#ifdef __cplusplus +#define typeof_strip_qual (expr) \ + std::remove_cv<typeof(expr)>::type +#else #define typeof_strip_qual (expr) \ typeof( \ __builtin_choose_expr( \
@@ -59,6 +63,7 @@ __builtin_types_compatible_p(typeof(expr), const volatile unsigned short), \ (unsigned short)1, \ (expr)+0)))))) +#endif #ifndef __ATOMIC_RELAXED#error "Expecting C11 atomic ops"
include/qemu/osdep.h +1 -0
@@ -140,6 +140,7 @@ QEMU_EXTERN_C int daemon(int, int);#endif #ifdef __cplusplus +#include <type_traits> extern "C" {#endif
Signature · 1 line
2.35.1
PATCH 6/16 use g_new0 instead of g_malloc0
In reply to Paolo BonziniCasting to/from void* must be explicit in C++. g_new0 takes care of that.
--- include/qemu/timer.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
include/qemu/timer.h +1 -1
@@ -520,7 +520,7 @@ static inline QEMUTimer *timer_new_full(QEMUTimerListGroup *timer_list_group, int scale, int attributes, QEMUTimerCB *cb, void *opaque) { - QEMUTimer *ts = g_malloc0(sizeof(QEMUTimer)); + QEMUTimer *ts = g_new0(QEMUTimer, 1);timer_init_full (ts, timer_list_group, type, scale, attributes, cb, opaque);return ts; }
Signature · 1 line
2.35.1
Re: [PATCH experiment 06/16] use g_new0 instead of g_malloc0
Quoted 12 lines · Paolo Bonzini
Casting to/from void* must be explicit in C++.
g_new0takes care of that.Signed-off-by Paolo Bonzini <[email protected]>--- include/qemu/timer.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)include/qemu/timer.h +0
-0
include/qemu/timer.h static inline QEMUTimer *timer_new_full(QEMUTimerListGroup *timer_list_group, · line 526
Quoted 8 lines
include/qemu/timer.h +1
-1@@ -520,7 +520,7 @@ static inline QEMUTimer *timer_new_full(QEMUTimerListGroup *timer_list_group, int scale, int attributes, QEMUTimerCB *cb, void *opaque) { - QEMUTimer *ts = g_malloc0(sizeof(QEMUTimer)); + QEMUTimer *ts = g_new0(QEMUTimer, 1);timer_init_full (ts, timer_list_group, type, scale, attributes, cb, opaque);return ts; }
Looks like a rerun of commit b45c03f585's Coccinelle script is due. I'll take care of it.
PATCH 7/16 start porting compiler.h to C++
In reply to Paolo Bonzini--- include/qemu/compiler.h | 4 ++++ 1 file changed, 4 insertions(+)
include/qemu/compiler.h +4 -0
@@ -72,6 +72,10 @@ int:(x) ? -1 : 1; \ } +#ifdef __cplusplus +#define _Static_assert static_assert +#endif +#define QEMU_BUILD_BUG_MSG (x, msg) _Static_assert(!(x), msg)#define QEMU_BUILD_BUG_ON (x) QEMU_BUILD_BUG_MSG(x, "not expecting: " #x)
Signature · 1 line
2.35.1
PATCH 8/16 tracetool: add extern "C" around generated headers
In reply to Paolo Bonzini--- scripts/tracetool/format/h.py | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-)
scripts/tracetool/format/h.py +7 -1
@@ -27,6 +27,9 @@ def generate(events, backend, group): '#define TRACE_%s_GENERATED_TRACERS_H' % group.upper(), '', '#include "%s"' % header, + '#ifdef __cplusplus', + 'extern "C" {', + '#endif' '') for e in events:
@@ -100,4 +103,7 @@ def generate(events, backend, group): backend.generate_end(events, group) - out('#endif /* TRACE_%s_GENERATED_TRACERS_H */' % group.upper()) + out('#ifdef __cplusplus', + '}', + '#endif', + '#endif /* TRACE_%s_GENERATED_TRACERS_H */' % group.upper())
Signature · 1 line
2.35.1
Re: [PATCH experiment 08/16] tracetool: add extern "C" around generated headers
Quoted 10 lines · Paolo Bonzini
Signed-off-by Paolo Bonzini <[email protected]>--- scripts/tracetool/format/h.py | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-)scripts/tracetool/format/h.py +0
-0
scripts/tracetool/format/h.py def generate(events, backend, group): · line 32
Quoted 6 lines
scripts/tracetool/format/h.py +3
-0@@ -27,6 +27,9 @@ def generate(events, backend, group): '#define TRACE_%s_GENERATED_TRACERS_H' % group.upper(), '', '#include "%s"' % header, + '#ifdef __cplusplus', + 'extern "C" {', + '#endif'
Why not use G_BEGIN_DECLS?
Anyhow,
Re: [PATCH experiment 08/16] tracetool: add extern "C" around generated headers
Quoted 6 lines · Philippe Mathieu-Daudé
Quoted 5 lines · Paolo Bonzini
Signed-off-by Paolo Bonzini <[email protected]>---
scripts/tracetool/format/h.py| 8 +++++++-1 file changed, 7 insertions(+), 1 deletion(-)
Quoted 13 lines
scripts/tracetool/format/h.py def generate(events, backend, group): · line 32
Quoted 11 lines
scripts/tracetool/format/h.py +3
-0@@ -27,6 +27,9 @@ def generate(events, backend, group): '#define TRACE_%s_GENERATED_TRACERS_H' % group.upper(), '', '#include "%s"' % header, + '#ifdef __cplusplus', + 'extern "C" {', + '#endif'Why not use
G_BEGIN_DECLS?
I wasn't sure if tracetool dependend on glib. It's more a philosophical question than an actual difference, of course.
Paolo
PATCH 9/16 start adding extern "C" markers
In reply to Paolo Bonzini--- include/block/aio.h | 5 +++++ include/fpu/softfloat-types.h | 4 ++++ include/qemu/bitops.h | 3 +++ include/qemu/bswap.h | 10 +++------- include/qemu/coroutine.h | 4 ++++ include/qemu/host-utils.h | 4 ++++ include/qemu/notify.h | 4 ++++ include/qemu/qsp.h | 4 ++++ include/qemu/thread.h | 4 ++++ include/qemu/timer.h | 4 ++++ 10 files changed, 39 insertions(+), 7 deletions(-)
include/block/aio.h +5 -0
@@ -17,12 +17,15 @@#ifdef CONFIG_LINUX_IO_URING #include <liburing.h> #endif +#include "qemu/coroutine.h" #include "qemu/queue.h" #include "qemu/event_notifier.h" #include "qemu/thread.h" #include "qemu/timer.h" +G_BEGIN_DECLS + typedef struct BlockAIOCB BlockAIOCB;typedef void BlockCompletionFunc (void *opaque,int ret);
@@ -769,4 +772,6 @@ void aio_context_set_poll_params(AioContext *ctx, int64_t max_ns,void aio_context_set_aio_params (AioContext *ctx,int64_t max_batch,Error **errp); +G_END_DECLS +#endif
include/fpu/softfloat-types.h +4 -0
@@ -80,6 +80,8 @@ this code that are retained.#ifndef SOFTFLOAT_TYPES_H #define SOFTFLOAT_TYPES_H +G_BEGIN_DECLS +/* * Software IEC/IEEE floating-point types. */
@@ -197,4 +199,6 @@ typedef struct float_status {bool no_signaling_nans; } float_status; +G_END_DECLS +#endif /* SOFTFLOAT_TYPES_H */
include/qemu/bitops.h +3 -0
@@ -12,6 +12,7 @@#ifndef BITOPS_H #define BITOPS_H +G_BEGIN_DECLS#include "host-utils.h" #include "atomic.h"
@@ -618,4 +619,6 @@ static inline uint64_t half_unshuffle64(uint64_t x)return x; } +G_END_DECLS +#endif
include/qemu/bswap.h +3 -7
@@ -15,12 +15,10 @@#define BSWAP_FROM_FALLBACKS #endif /* ! CONFIG_MACHINE_BSWAP_H */ -#ifdef __cplusplus -extern "C" { -#endif -#include "fpu/softfloat-types.h" +G_BEGIN_DECLS +#ifdef BSWAP_FROM_BYTESWAP static inline uint16_t bswap16 (uint16_t x) {
@@ -508,8 +506,6 @@ DO_STN_LDN_P(be)#undef le_bswaps#undef be_bswaps -#ifdef __cplusplus -} -#endif +G_END_DECLS#endif /* BSWAP_H */
include/qemu/coroutine.h +4 -0
@@ -18,6 +18,8 @@#include "qemu/queue.h" #include "qemu/timer.h" +G_BEGIN_DECLS +/** * Coroutines are a mechanism for stack switching and can be used for * cooperative userspace threading. These functions provide a simple but
@@ -341,6 +343,8 @@ void qemu_coroutine_increase_pool_batch_size(unsigned int additional_pool_size);*/ void qemu_coroutine_decrease_pool_batch_size (unsigned int additional_pool_size); +G_END_DECLS +#include "qemu/lockable.h" #include "qemu/co-lockable.h"
include/qemu/host-utils.h +4 -0
@@ -30,6 +30,8 @@#ifndef HOST_UTILS_H #define HOST_UTILS_H +G_BEGIN_DECLS +#include "qemu/compiler.h" #include "qemu/bswap.h"
@@ -849,4 +851,6 @@ static inline uint64_t udiv_qrnnd(uint64_t *r, uint64_t n1,#endif } +G_END_DECLS +#endif
include/qemu/notify.h +4 -0
@@ -14,6 +14,8 @@#ifndef QEMU_NOTIFY_H #define QEMU_NOTIFY_H +G_BEGIN_DECLS + #include "qemu/queue.h" typedef struct Notifier Notifier;
@@ -71,4 +73,6 @@ void notifier_with_return_remove(NotifierWithReturn *notifier);int notifier_with_return_list_notify (NotifierWithReturnList *list,void *data); +G_END_DECLS +#endif
include/qemu/qsp.h +4 -0
@@ -16,6 +16,8 @@ enum QSPSortBy { QSP_SORT_BY_AVG_WAIT_TIME, }; +G_BEGIN_DECLS + void qsp_report(size_t max, enum QSPSortBy sort_by,bool callsite_coalesce);
@@ -24,4 +26,6 @@ void qsp_enable(void);void qsp_disable (void );void qsp_reset (void ); +G_END_DECLS +#endif /* QEMU_QSP_H */
include/qemu/thread.h +4 -0
@@ -22,6 +22,8 @@ typedef struct QemuThread QemuThread;#define QEMU_THREAD_JOINABLE 0#define QEMU_THREAD_DETACHED 1 +G_BEGIN_DECLS + void qemu_mutex_init(QemuMutex *mutex);void qemu_mutex_destroy (QemuMutex *mutex);int qemu_mutex_trylock_impl (QemuMutex *mutex,const char *file,const int line);
@@ -397,4 +399,6 @@ void qemu_lockcnt_inc_and_unlock(QemuLockCnt *lockcnt);*/ unsigned qemu_lockcnt_count (QemuLockCnt *lockcnt); +G_END_DECLS +#endif
include/qemu/timer.h +4 -0
@@ -5,6 +5,8 @@#include "qemu/notify.h" #include "qemu/host-utils.h" +G_BEGIN_DECLS +#define NANOSECONDS_PER_SECOND 1000000000LL/* timers */
@@ -998,4 +1000,6 @@ static inline int64_t profile_getclock(void)extern int64_t dev_time;#endif +G_END_DECLS +#endif
Signature · 1 line
2.35.1
PATCH 10/16 add space between liter and string macro
In reply to Paolo Bonzini--- util/trace-events | 28 ++++++++++++++-------------- 1 file changed, 14 insertions(+), 14 deletions(-)
util/trace-events +14 -14
@@ -1,10 +1,10 @@ # See docs/devel/tracing.rst for syntax documentation. # aio-posix.c -run_poll_handlers_begin(void *ctx, int64_t max_ns, int64_t timeout) "ctx %p max_ns %"PRId64 " timeout %"PRId64 -run_poll_handlers_end(void *ctx, bool progress, int64_t timeout) "ctx %p progress %d new timeout %"PRId64 -poll_shrink(void *ctx, int64_t old, int64_t new) "ctx %p old %"PRId64" new %"PRId64 -poll_grow(void *ctx, int64_t old, int64_t new) "ctx %p old %"PRId64" new %"PRId64 +run_poll_handlers_begin(void *ctx, int64_t max_ns, int64_t timeout) "ctx %p max_ns %" PRId64 " timeout %" PRId64 +run_poll_handlers_end(void *ctx, bool progress, int64_t timeout) "ctx %p progress %d new timeout %" PRId64 +poll_shrink(void *ctx, int64_t old, int64_t new) "ctx %p old %" PRId64" new %" PRId64 +poll_grow(void *ctx, int64_t old, int64_t new) "ctx %p old %" PRId64" new %" PRId64 poll_add(void *ctx, void *node, int fd, unsigned revents) "ctx %p node %p fd %d revents 0x%x" poll_remove(void *ctx, void *node, int fd) "ctx %p node %p fd %d"
@@ -52,9 +52,9 @@ qemu_vfree(void *ptr) "ptr %p" qemu_anon_ram_free(void *ptr, size_t size) "ptr %p size %zu" # hbitmap.c -hbitmap_iter_skip_words(const void *hb, void *hbi, uint64_t pos, unsigned long cur) "hb %p hbi %p pos %"PRId64" cur 0x%lx" -hbitmap_reset(void *hb, uint64_t start, uint64_t count, uint64_t sbit, uint64_t ebit) "hb %p items %"PRIu64",%"PRIu64" bits %"PRIu64"..%"PRIu64 -hbitmap_set(void *hb, uint64_t start, uint64_t count, uint64_t sbit, uint64_t ebit) "hb %p items %"PRIu64",%"PRIu64" bits %"PRIu64"..%"PRIu64 +hbitmap_iter_skip_words(const void *hb, void *hbi, uint64_t pos, unsigned long cur) "hb %p hbi %p pos %" PRId64" cur 0x%lx" +hbitmap_reset(void *hb, uint64_t start, uint64_t count, uint64_t sbit, uint64_t ebit) "hb %p items %" PRIu64",%" PRIu64" bits %" PRIu64"..%" PRIu64 +hbitmap_set(void *hb, uint64_t start, uint64_t count, uint64_t sbit, uint64_t ebit) "hb %p items %" PRIu64",%" PRIu64" bits %" PRIu64"..%" PRIu64 # lockcnt.c lockcnt_fast_path_attempt(const void *lockcnt, int expected, int new) "lockcnt %p fast path %d->%d"
@@ -82,15 +82,15 @@ qemu_vfio_ram_block_added(void *s, void *p, size_t size) "s %p host %p size 0x%z qemu_vfio_ram_block_removed(void *s, void *p, size_t size) "s %p host %p size 0x%zx" qemu_vfio_dump_mapping(void *host, uint64_t iova, size_t size) "vfio mapping %p to iova 0x%08" PRIx64 " size 0x%zx" qemu_vfio_find_mapping(void *s, void *p) "s %p host %p" -qemu_vfio_new_mapping(void *s, void *host, size_t size, int index, uint64_t iova) "s %p host %p size 0x%zx index %d iova 0x%"PRIx64 -qemu_vfio_do_mapping(void *s, void *host, uint64_t iova, size_t size) "s %p host %p <-> iova 0x%"PRIx64 " size 0x%zx" +qemu_vfio_new_mapping(void *s, void *host, size_t size, int index, uint64_t iova) "s %p host %p size 0x%zx index %d iova 0x%" PRIx64 +qemu_vfio_do_mapping(void *s, void *host, uint64_t iova, size_t size) "s %p host %p <-> iova 0x%" PRIx64 " size 0x%zx" qemu_vfio_dma_map(void *s, void *host, size_t size, bool temporary, uint64_t *iova) "s %p host %p size 0x%zx temporary %d &iova %p" -qemu_vfio_dma_mapped(void *s, void *host, uint64_t iova, size_t size) "s %p host %p <-> iova 0x%"PRIx64" size 0x%zx" +qemu_vfio_dma_mapped(void *s, void *host, uint64_t iova, size_t size) "s %p host %p <-> iova 0x%" PRIx64" size 0x%zx" qemu_vfio_dma_unmap(void *s, void *host) "s %p host %p" -qemu_vfio_pci_read_config(void *buf, int ofs, int size, uint64_t region_ofs, uint64_t region_size) "read cfg ptr %p ofs 0x%x size 0x%x (region addr 0x%"PRIx64" size 0x%"PRIx64")" -qemu_vfio_pci_write_config(void *buf, int ofs, int size, uint64_t region_ofs, uint64_t region_size) "write cfg ptr %p ofs 0x%x size 0x%x (region addr 0x%"PRIx64" size 0x%"PRIx64")" -qemu_vfio_region_info(const char *desc, uint64_t region_ofs, uint64_t region_size, uint32_t cap_offset) "region '%s' addr 0x%"PRIx64" size 0x%"PRIx64" cap_ofs 0x%"PRIx32 -qemu_vfio_pci_map_bar(int index, uint64_t region_ofs, uint64_t region_size, int ofs, void *host) "map region bar#%d addr 0x%"PRIx64" size 0x%"PRIx64" ofs 0x%x host %p" +qemu_vfio_pci_read_config(void *buf, int ofs, int size, uint64_t region_ofs, uint64_t region_size) "read cfg ptr %p ofs 0x%x size 0x%x (region addr 0x%" PRIx64" size 0x%" PRIx64")" +qemu_vfio_pci_write_config(void *buf, int ofs, int size, uint64_t region_ofs, uint64_t region_size) "write cfg ptr %p ofs 0x%x size 0x%x (region addr 0x%" PRIx64" size 0x%" PRIx64")" +qemu_vfio_region_info(const char *desc, uint64_t region_ofs, uint64_t region_size, uint32_t cap_offset) "region '%s' addr 0x%" PRIx64" size 0x%" PRIx64" cap_ofs 0x%" PRIx32 +qemu_vfio_pci_map_bar(int index, uint64_t region_ofs, uint64_t region_size, int ofs, void *host) "map region bar#%d addr 0x%" PRIx64" size 0x%" PRIx64" ofs 0x%x host %p" #userfaultfd.c uffd_query_features_nosys(int err) "errno: %i"
Signature · 1 line
2.35.1
PATCH 11/16 bump to C++20
In reply to Paolo Bonzini--- configure | 4 ++-- meson.build | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-)
configure +2 -2
@@ -157,8 +157,8 @@ update_cxxflags() { # Set QEMU_CXXFLAGS from QEMU_CFLAGS by filtering out those # options which some versions of GCC's C++ compiler complain about # because they only make sense for C programs. - QEMU_CXXFLAGS="-D__STDC_LIMIT_MACROS -D__STDC_CONSTANT_MACROS -D__STDC_FORMAT_MACROS" - CONFIGURE_CXXFLAGS=$(echo "$CONFIGURE_CFLAGS" | sed s/-std=gnu11/-std=gnu++11/) + QEMU_CXXFLAGS="-D__STDC_LIMIT_MACROS -D__STDC_CONSTANT_MACROS -D__STDC_FORMAT_MACROS -fcoroutines" + CONFIGURE_CXXFLAGS=$(echo "$CONFIGURE_CFLAGS" | sed s/-std=gnu11/-std=gnu++20/) for arg in $QEMU_CFLAGS; do case $arg in -Wstrict-prototypes|-Wmissing-prototypes|-Wnested-externs|\
meson.build +1 -1
@@ -1,5 +1,5 @@ project('qemu', ['c'], meson_version: '>=0.59.3', - default_options: ['warning_level=1', 'c_std=gnu11', 'cpp_std=gnu++11', 'b_colorout=auto', + default_options: ['warning_level=1', 'c_std=gnu11', 'cpp_std=gnu++20', 'b_colorout=auto', 'b_staticpic=false', 'stdsplit=false'], version: files('VERSION'))
Signature · 1 line
2.35.1
PATCH 12/16 remove "new" keyword from trace-events
In reply to Paolo BonziniThis is invalid in C++.
--- util/trace-events | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-)
util/trace-events +8 -8
@@ -3,8 +3,8 @@ # aio-posix.c run_poll_handlers_begin(void *ctx, int64_t max_ns, int64_t timeout) "ctx %p max_ns %" PRId64 " timeout %" PRId64 run_poll_handlers_end(void *ctx, bool progress, int64_t timeout) "ctx %p progress %d new timeout %" PRId64 -poll_shrink(void *ctx, int64_t old, int64_t new) "ctx %p old %" PRId64" new %" PRId64 -poll_grow(void *ctx, int64_t old, int64_t new) "ctx %p old %" PRId64" new %" PRId64 +poll_shrink(void *ctx, int64_t prev, int64_t curr) "ctx %p old %" PRId64" new %" PRId64 +poll_grow(void *ctx, int64_t prev, int64_t curr) "ctx %p old %" PRId64" new %" PRId64 poll_add(void *ctx, void *node, int fd, unsigned revents) "ctx %p node %p fd %d revents 0x%x" poll_remove(void *ctx, void *node, int fd) "ctx %p node %p fd %d"
@@ -57,13 +57,13 @@ hbitmap_reset(void *hb, uint64_t start, uint64_t count, uint64_t sbit, uint64_t hbitmap_set(void *hb, uint64_t start, uint64_t count, uint64_t sbit, uint64_t ebit) "hb %p items %" PRIu64",%" PRIu64" bits %" PRIu64"..%" PRIu64 # lockcnt.c -lockcnt_fast_path_attempt(const void *lockcnt, int expected, int new) "lockcnt %p fast path %d->%d" -lockcnt_fast_path_success(const void *lockcnt, int expected, int new) "lockcnt %p fast path %d->%d succeeded" -lockcnt_unlock_attempt(const void *lockcnt, int expected, int new) "lockcnt %p unlock %d->%d" -lockcnt_unlock_success(const void *lockcnt, int expected, int new) "lockcnt %p unlock %d->%d succeeded" -lockcnt_futex_wait_prepare(const void *lockcnt, int expected, int new) "lockcnt %p preparing slow path %d->%d" +lockcnt_fast_path_attempt(const void *lockcnt, int expected, int newval) "lockcnt %p fast path %d->%d" +lockcnt_fast_path_success(const void *lockcnt, int expected, int newval) "lockcnt %p fast path %d->%d succeeded" +lockcnt_unlock_attempt(const void *lockcnt, int expected, int newval) "lockcnt %p unlock %d->%d" +lockcnt_unlock_success(const void *lockcnt, int expected, int newval) "lockcnt %p unlock %d->%d succeeded" +lockcnt_futex_wait_prepare(const void *lockcnt, int expected, int newval) "lockcnt %p preparing slow path %d->%d" lockcnt_futex_wait(const void *lockcnt, int val) "lockcnt %p waiting on %d" -lockcnt_futex_wait_resume(const void *lockcnt, int new) "lockcnt %p after wait: %d" +lockcnt_futex_wait_resume(const void *lockcnt, int newval) "lockcnt %p after wait: %d" lockcnt_futex_wake(const void *lockcnt) "lockcnt %p waking up one waiter" # qemu-sockets.c
Signature · 1 line
2.35.1
Re: [PATCH experiment 12/16] remove "new" keyword from trace-events
Quoted 6 lines · Paolo Bonzini
This is invalid in C++.
Signed-off-by Paolo Bonzini <[email protected]>--- util/trace-events | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-)
PATCH 13/16 disable some code
In reply to Paolo BonziniDisable a lot of code that I can't be bothered to convert right now.
--- include/qemu/coroutine.h | 2 ++ qapi/qmp-dispatch.c | 2 ++ tests/unit/meson.build | 2 +- tests/unit/test-coroutine.c | 6 ++++++ util/async.c | 2 ++ util/meson.build | 10 +++++----- util/qemu-coroutine-lock.c | 2 ++ util/qemu-coroutine-sleep.c | 2 ++ util/thread-pool.c | 2 ++ 9 files changed, 24 insertions(+), 6 deletions(-)
include/qemu/coroutine.h +2 -0
@@ -200,6 +200,7 @@ typedef struct CoQueue {*/ void qemu_co_queue_init (CoQueue *queue); +#if 0 /** * Adds the current coroutine to the CoQueue and transfers control to the * caller of the coroutine. The mutex is unlocked during the wait and
@@ -208,6 +209,7 @@ void qemu_co_queue_init(CoQueue *queue);#define qemu_co_queue_wait (queue, lock) \ qemu_co_queue_wait_impl(queue, QEMU_MAKE_CO_LOCKABLE(lock)) void coroutine_fn qemu_co_queue_wait_impl(CoQueue *queue, QemuCoLockable *lock); +#endif /** * Removes the next coroutine from the CoQueue, and wake it up.
qapi/qmp-dispatch.c +2 -0
@@ -235,7 +235,9 @@ QDict *qmp_dispatch(const QmpCommandList *cmds, QObject *request, };aio_bh_schedule_oneshot (qemu_get_aio_context (), do_qmp_dispatch_bh, &data); +#if 0 qemu_coroutine_yield (); +#endif }qobject_unref (args);if (err) {
tests/unit/meson.build +1 -1
@@ -61,7 +61,7 @@ endif if have_block tests += { - 'test-coroutine': [testblock], + 'test-coroutine': [], 'test-aio': [testblock], 'test-aio-multithread': [testblock], 'test-throttle': [testblock],
tests/unit/test-coroutine.c +6 -0
@@ -16,6 +16,7 @@#include "qemu/coroutine_int.h" #include "qemu/lockable.h" +#if 0 /* * Check that qemu_in_coroutine() works */
@@ -638,11 +639,13 @@ static void perf_cost(void) duration, ops, (unsigned long)(1000000000.0 * duration / maxcycles)); } +#endif int main (int argc,char **argv) {g_test_init (&argc, &argv,NULL ); +#if 0 /* This test assumes there is a freelist and marks freed coroutine memory * with a sentinel value. If there is no freelist this would legitimately * crash, so skip it.
@@ -650,7 +653,9 @@ int main(int argc, char **argv)if (CONFIG_COROUTINE_POOL ) {g_test_add_func ("/basic/no-dangling-access" , test_no_dangling_access); } +#endif +#if 0 g_test_add_func ("/basic/lifecycle" , test_lifecycle);g_test_add_func ("/basic/yield" , test_yield);g_test_add_func ("/basic/nesting" , test_nesting);
@@ -669,5 +674,6 @@ int main(int argc, char **argv)g_test_add_func ("/perf/function-call" , perf_baseline); g_test_add_func("/perf/cost", perf_cost); } +#endif return g_test_run (); }
util/async.c +2 -0
@@ -595,6 +595,7 @@ void aio_co_schedule(AioContext *ctx, Coroutine *co)aio_context_unref (ctx); } +#if 0 typedef struct AioCoRescheduleSelf { Coroutine *co; AioContext *new_ctx;
@@ -624,6 +625,7 @@ void coroutine_fn aio_co_reschedule_self(AioContext *new_ctx) qemu_coroutine_yield(); } } +#endif void aio_co_wake (struct Coroutine *co) {
util/meson.build +5 -5
@@ -76,13 +76,13 @@ if have_block util_ss.add(files('lockcnt.c')) util_ss.add(files('main-loop.c')) util_ss.add(files('nvdimm-utils.c')) - util_ss.add(files('qemu-coroutine.c', 'qemu-coroutine-lock.c', 'qemu-coroutine-io.c')) - util_ss.add(when: 'CONFIG_LINUX', if_true: [ - files('vhost-user-server.c'), vhost_user - ]) + util_ss.add(files('qemu-coroutine.c', 'qemu-coroutine-lock.c')) # 'qemu-coroutine-io.c' +# util_ss.add(when: 'CONFIG_LINUX', if_true: [ +# files('vhost-user-server.c'), vhost_user +# ]) util_ss.add(files('block-helpers.c')) util_ss.add(files('qemu-coroutine-sleep.c')) - util_ss.add(files('qemu-co-shared-resource.c')) +# util_ss.add(files('qemu-co-shared-resource.c')) util_ss.add(files('thread-pool.c', 'qemu-timer.c')) util_ss.add(files('readline.c')) util_ss.add(files('throttle.c'))
util/qemu-coroutine-lock.c +2 -0
@@ -34,6 +34,7 @@#include "block/aio.h" #include "trace.h" +#if 0 void qemu_co_queue_init (CoQueue *queue) {QSIMPLEQ_INIT (&queue->entries);
@@ -465,3 +466,4 @@ void qemu_co_rwlock_upgrade(CoRwlock *lock) assert(lock->owners == -1); } } +#endif
util/qemu-coroutine-sleep.c +2 -0
@@ -17,6 +17,7 @@#include "qemu/timer.h" #include "block/aio.h" +#if 0 static const char *qemu_co_sleep_ns__scheduled ="qemu_co_sleep_ns" ;void qemu_co_sleep_wake (QemuCoSleep *w)
@@ -78,3 +79,4 @@ void coroutine_fn qemu_co_sleep_ns_wakeable(QemuCoSleep *w,qemu_co_sleep (w); timer_del(&ts); } +#endif
util/thread-pool.c +2 -0
@@ -266,6 +266,7 @@ BlockAIOCB *thread_pool_submit_aio(ThreadPool *pool,return &req->common; } +#if 0 typedef struct ThreadPoolCo { Coroutine *co; int ret;
@@ -288,6 +289,7 @@ int coroutine_fn thread_pool_submit_co(ThreadPool *pool, ThreadPoolFunc *func,qemu_coroutine_yield (); return tpc.ret; } +#endif void thread_pool_submit (ThreadPool *pool,ThreadPoolFunc *func,void *arg) {
Signature · 1 line
2.35.1
PATCH 14/16 util: introduce C++ stackless coroutine backend
In reply to Paolo Bonzini---
configure | 44 +-
include/qemu/coroutine.h | 445 +++++++++++++-----
include/qemu/coroutine_int.h | 8 +
util/coroutine-stackless.cc | 145 ++++++
util/meson.build | 6 +-
...oroutine-lock.c => qemu-coroutine-lock.cc} | 70 +--
...outine-sleep.c => qemu-coroutine-sleep.cc} | 12 +-
util/{qemu-coroutine.c => qemu-coroutine.cc} | 16 -
8 files changed, 530 insertions(+), 216 deletions(-)
create mode 100644 util/coroutine-stackless.cc
rename util/{qemu-coroutine-lock.c => qemu-coroutine-lock.cc} (87%)
rename util/{qemu-coroutine-sleep.c => qemu-coroutine-sleep.cc} (89%)
rename util/{qemu-coroutine.c => qemu-coroutine.cc} (94%)configure +2 -42
@@ -1220,8 +1220,6 @@ Advanced options (experts only): --with-trace-file=NAME Full PATH,NAME of file to store traces Default:trace-<pid> --cpu=CPU Build for host CPU [$cpu] - --with-coroutine=BACKEND coroutine backend. Supported options: - ucontext, sigaltstack, windows --enable-gcov enable test coverage analysis with gcov --tls-priority default TLS protocol/cipher priority string --enable-plugins
@@ -1242,7 +1240,7 @@ cat << EOF debug-info debugging information lto Enable Link-Time Optimization. safe-stack SafeStack Stack Smash Protection. Depends on - clang/llvm >= 3.7 and requires coroutine backend ucontext. + clang/llvm >= 3.7 rdma Enable RDMA-based migration pvrdma Enable PVRDMA support vhost-net vhost-net kernel acceleration support
@@ -2338,39 +2336,7 @@ EOF fi fi -if test "$coroutine" = ""; then - if test "$mingw32" = "yes"; then - coroutine=win32 - elif test "$ucontext_works" = "yes"; then - coroutine=ucontext - else - coroutine=sigaltstack - fi -else - case $coroutine in - windows) - if test "$mingw32" != "yes"; then - error_exit "'windows' coroutine backend only valid for Windows" - fi - # Unfortunately the user visible backend name doesn't match the - # coroutine-*.c filename for this case, so we have to adjust it here. - coroutine=win32 - ;; - ucontext) - if test "$ucontext_works" != "yes"; then - feature_not_found "ucontext" - fi - ;; - sigaltstack) - if test "$mingw32" = "yes"; then - error_exit "only the 'windows' coroutine backend is valid for Windows" - fi - ;; - *) - error_exit "unknown coroutine backend $coroutine" - ;; - esac -fi +coroutine=stackless ################################################## # SafeStack
@@ -2395,9 +2361,6 @@ EOF else error_exit "SafeStack not supported by your compiler" fi - if test "$coroutine" != "ucontext"; then - error_exit "SafeStack is only supported by the coroutine backend ucontext" - fi else cat > $TMPC << EOF int main(int argc, char *argv[])
@@ -2427,9 +2390,6 @@ else # "$safe_stack" = "" safe_stack="no" else safe_stack="yes" - if test "$coroutine" != "ucontext"; then - error_exit "SafeStack is only supported by the coroutine backend ucontext" - fi fi fi fi
include/qemu/coroutine.h +332 -113
491 lines not on this page · raw message
include/qemu/coroutine_int.h +8 -0
@@ -28,6 +28,8 @@#include "qemu/queue.h" #include "qemu/coroutine.h" +G_BEGIN_DECLS +#ifdef CONFIG_SAFESTACK /* Pointer to the unsafe stack, defined by the compiler */ extern __threadvoid *__safestack_unsafe_stack_ptr;
@@ -41,6 +43,10 @@ typedef enum { COROUTINE_ENTER = 3, } CoroutineAction; +#ifndef __cplusplus +typedef struct IncompleteType *CoroutineEntry ; +#endif +struct Coroutine {CoroutineEntry *entry;void *entry_arg;
@@ -74,4 +80,6 @@ void qemu_coroutine_delete(Coroutine *co);CoroutineAction qemu_coroutine_switch (Coroutine *from,Coroutine *to,CoroutineAction action); +G_END_DECLS +#endif
util/coroutine-stackless.cc new file +145 -0
145 lines not on this page · raw message
util/meson.build +3 -3
@@ -68,7 +68,7 @@ if have_block util_ss.add(files('base64.c')) util_ss.add(files('buffer.c')) util_ss.add(files('bufferiszero.c')) - util_ss.add(files('coroutine-@[email protected]'.format(config_host['CONFIG_COROUTINE_BACKEND']))) + util_ss.add(files('coroutine-stackless.cc')) util_ss.add(files('hbitmap.c')) util_ss.add(files('hexdump.c')) util_ss.add(files('iova-tree.c'))
@@ -76,12 +76,12 @@ if have_block util_ss.add(files('lockcnt.c')) util_ss.add(files('main-loop.c')) util_ss.add(files('nvdimm-utils.c')) - util_ss.add(files('qemu-coroutine.c', 'qemu-coroutine-lock.c')) # 'qemu-coroutine-io.c' + util_ss.add(files('qemu-coroutine.cc', 'qemu-coroutine-lock.cc')) # 'qemu-coroutine-io.c' # util_ss.add(when: 'CONFIG_LINUX', if_true: [ # files('vhost-user-server.c'), vhost_user # ]) util_ss.add(files('block-helpers.c')) - util_ss.add(files('qemu-coroutine-sleep.c')) + util_ss.add(files('qemu-coroutine-sleep.cc')) # util_ss.add(files('qemu-co-shared-resource.c')) util_ss.add(files('thread-pool.c', 'qemu-timer.c')) util_ss.add(files('readline.c'))
util/qemu-coroutine-lock.cc renamed from util/qemu-coroutine-lock.c +35 -35
200 lines not on this page · raw message
util/qemu-coroutine-sleep.cc renamed from util/qemu-coroutine-sleep.c +5 -7
@@ -17,7 +17,6 @@ #include "qemu/timer.h" #include "block/aio.h" -#if 0 static const char *qemu_co_sleep_ns__scheduled = "qemu_co_sleep_ns"; void qemu_co_sleep_wake(QemuCoSleep *w)
@@ -38,11 +37,11 @@ void qemu_co_sleep_wake(QemuCoSleep *w) static void co_sleep_cb(void *opaque) { - QemuCoSleep *w = opaque; + QemuCoSleep *w = (QemuCoSleep *)opaque; qemu_co_sleep_wake(w); } -void coroutine_fn qemu_co_sleep(QemuCoSleep *w) +CoroutineFn<void> qemu_co_sleep(QemuCoSleep *w) { Coroutine *co = qemu_coroutine_self();
@@ -56,13 +55,13 @@ void coroutine_fn qemu_co_sleep(QemuCoSleep *w) } w->to_wake = co; - qemu_coroutine_yield(); + co_await qemu_coroutine_yield(); /* w->to_wake is cleared before resuming this coroutine. */ assert(w->to_wake == NULL); } -void coroutine_fn qemu_co_sleep_ns_wakeable(QemuCoSleep *w, +CoroutineFn<void> qemu_co_sleep_ns_wakeable(QemuCoSleep *w, QEMUClockType type, int64_t ns) { AioContext *ctx = qemu_get_current_aio_context();
@@ -76,7 +75,6 @@ void coroutine_fn qemu_co_sleep_ns_wakeable(QemuCoSleep *w, * must happen after qemu_co_sleep yields and there is no race * between timer_mod and qemu_co_sleep. */ - qemu_co_sleep(w); + co_await qemu_co_sleep(w); timer_del(&ts); } -#endif
util/qemu-coroutine.cc renamed from util/qemu-coroutine.c +0 -16
@@ -179,22 +179,6 @@ void qemu_coroutine_enter_if_inactive(Coroutine *co) } } -void coroutine_fn qemu_coroutine_yield(void) -{ - Coroutine *self = qemu_coroutine_self(); - Coroutine *to = self->caller; - - trace_qemu_coroutine_yield(self, to); - - if (!to) { - fprintf(stderr, "Co-routine is yielding to no one\n"); - abort(); - } - - self->caller = NULL; - qemu_coroutine_switch(self, to, COROUTINE_YIELD); -} - bool qemu_coroutine_entered(Coroutine *co) { return co->caller;
Signature · 1 line
2.35.1
Re: [PATCH experiment 14/16] util: introduce C++ stackless coroutine backend
Quoted 11 lines · Paolo Bonzini
include/qemu/coroutine.h +11
-0+// ------------------------ + +// CoroutineFn does not even need anything more than what +// BaseCoroutine provides, so it's just a type alias. The magic +// is all in ValuePromise<T>. +// +// Suspended CoroutineFns are chained between themselves. Whenever a +// coroutine is suspended, all those that have done a co_await are +// also suspended, and whenever a coroutine finishes, it has to +// check if its parent can now be resumed. +//
include/qemu/coroutine.h void qemu_coroutine_decrease_pool_batch_size(unsigned int additional_pool_size); · line 339
Quoted 10 lines
include/qemu/coroutine.h +10
-0+// The two auxiliary classes Awaiter and ResumeAndFinish take +// care of the two sides of this. Awaiter's await_suspend() stores +// the parent coroutine into ValuePromise; ResumeAndFinish's runs +// after a coroutine returns, and resumes the parent coroutine. + +template<typename T> struct ValuePromise; +template<typename T> +using CoroutineFn = BaseCoroutine<ValuePromise<T>>; + +typedef CoroutineFn<void> CoroutineFunc(void *);
CoroutineFunc looks like a coroutine entry point function. If that's correct then I suggest naming it CoroutineEntryFunc to avoid confusion between CoroutineFn vs CoroutineFunc (their names are too similar).
Also, where is CoroutineFunc used?
Quoted 1 line
+// The actu promises, respectively for non-void and void types.
s/actu/actual/?
Re: [PATCH experiment 14/16] util: introduce C++ stackless coroutine backend
Il lun 14 mar 2022, 15:37 Stefan Hajnoczi <[email protected]> ha scritto:
Quoted 18 lines · Paolo Bonzini
Quoted 17 lines
include/qemu/coroutine.h +17
-0+// ------------------------ + +// CoroutineFn does not even need anything more than what +// BaseCoroutine provides, so it's just a type alias. The magic +// is all in ValuePromise<T>. +// +// Suspended CoroutineFns are chained between themselves. Whenever a +// coroutine is suspended, all those that have done a co_await are +// also suspended, and whenever a coroutine finishes, it has to +// check if its parent can now be resumed. +// +// The two auxiliary classes Awaiter and ResumeAndFinish take +// care of the two sides of this. Awaiter's await_suspend() stores +// the parent coroutine into ValuePromise; ResumeAndFinish's runs +// after a coroutine returns, and resumes the parent coroutine. + +template<typename T> struct ValuePromise;
Quoted 9 lines
include/qemu/coroutine.h void qemu_coroutine_decrease_pool_batch_size(unsigned int additional_pool_size); · line 339
Quoted 4 lines
include/qemu/coroutine.h +4
-0+template<typename T> +using CoroutineFn = BaseCoroutine<ValuePromise<T>>; + +typedef CoroutineFn<void> CoroutineFunc(void *);CoroutineFunc looks like a coroutine entry point function. If that's correct then I suggest naming it CoroutineEntryFunc to avoid confusion between CoroutineFn vs CoroutineFunc (their names are too similar).
Nevermind, it's a useless dup of CoroutineEntry.
Quoted 6 lines
Also, where is CoroutineFunc used?
Quoted 1 line
+// The actu promises, respectively for non-void and void types.
s/actu/actual/?
Yes.
Paolo
Quoted 1 line
PATCH 15/16 port QemuCoLockable to C++ coroutines
In reply to Paolo BonziniConvert "T coroutine_fn" annotations to the new type CoroutineFn<T>, and add co_await as needed.
_Generic is replaced by an overloaded constructor. C++ also does not like & on a temporary, so that is replaced by a function qemu_make_co_lockable_nonnull that hides it from the compiler.
--- include/qemu/co-lockable.h | 111 ++++++++++++++++++------------------- 1 file changed, 53 insertions(+), 58 deletions(-)
include/qemu/co-lockable.h +53 -58
@@ -16,83 +16,78 @@#include "qemu/coroutine.h" #include "qemu/thread.h" -typedef void coroutine_fn QemuCoLockUnlockFunc(void *); +typedef CoroutineFn<void> QemuCoLockUnlockFunc(void *); + +extern CoroutineFn<void> qemu_mutex_co_lock(QemuMutex *m); +extern CoroutineFn<void> qemu_mutex_co_unlock(QemuMutex *m);struct QemuCoLockable {void *object;QemuCoLockUnlockFunc *lock;QemuCoLockUnlockFunc *unlock; + + QemuCoLockable() : + object{NULL}, + lock{(QemuCoLockUnlockFunc *) NULL}, + unlock{(QemuCoLockUnlockFunc *) NULL} {} + QemuCoLockable(QemuMutex *x) : + object{x}, + lock{(QemuCoLockUnlockFunc *) qemu_mutex_co_lock}, + unlock{(QemuCoLockUnlockFunc *) qemu_mutex_co_unlock} {} + QemuCoLockable(CoMutex *x) : + object{x}, + lock{(QemuCoLockUnlockFunc *) qemu_co_mutex_lock}, + unlock{(QemuCoLockUnlockFunc *) qemu_co_mutex_unlock} {} }; -static inline __attribute__((__always_inline__))QemuCoLockable * -qemu_make_co_lockable (void *x,QemuCoLockable *lockable) +template<typename T> +static inline QemuCoLockable qcml_obj_nonnull_(T *x) { -/* -* We cannot test this in a macro, otherwise we get compiler -* warnings like "the address of 'm' will always evaluate as 'true'". -*/ -return x ? lockable :NULL ; + return QemuCoLockable{x}; } -static inline __attribute__((__always_inline__))QemuCoLockable * -qemu_null_co_lockable (void *x) +static inline QemuCoLockable const *qemu_make_co_lockable_nonnull(QemuCoLockable const &x) { +return &x; +} + +template<typename T> +static inline QemuCoLockable qcml_obj_(T *x) +{ + return QemuCoLockable{x}; +} +extern void build_not_reached (); + +template<> inline +QemuCoLockable qcml_obj_ (void *x) +{ +#ifdef __OPTIMIZE__if (x !=NULL ) { -qemu_build_not_reached (); +build_not_reached (); } -return NULL ; +#endif + return QemuCoLockable{}; } -/* -* In C, compound literals have the lifetime of an automatic variable. -* In C++ it would be different, but then C++ wouldn't need QemuCoLockable -* either... -*/ -#define QMCL_OBJ_ (x, name) (&(QemuCoLockable) { \ - .object = (x), \ - .lock = (QemuCoLockUnlockFunc *) qemu_ ## name ## _lock, \ - .unlock = (QemuCoLockUnlockFunc *) qemu_ ## name ## _unlock \ - }) - -/** -* QEMU_MAKE_CO_LOCKABLE - Make a polymorphic QemuCoLockable -* -* @x: a lock object (currently one of QemuMutex, CoMutex). -* -* Returns a QemuCoLockable object that can be passed around -* to a function that can operate with locks of any kind, or -* NULL if @x is %NULL. -* -* Note the speci case for void *, so that we may pass "NULL". -*/ -#define QEMU_MAKE_CO_LOCKABLE (x) \ - _Generic((x), QemuCoLockable *: (x), \ - void *: qemu_null_co_lockable(x), \ - QemuMutex *: qemu_make_co_lockable(x, QMCL_OBJ_(x, mutex)), \ - CoMutex *: qemu_make_co_lockable(x, QMCL_OBJ_(x, co_mutex))) \ - -/** -* QEMU_MAKE_CO_LOCKABLE_NONNULL - Make a polymorphic QemuCoLockable -* -* @x: a lock object (currently one of QemuMutex, QemuRecMutex, -* CoMutex, QemuSpin). -* -* Returns a QemuCoLockable object that can be passed around -* to a function that can operate with locks of any kind. -*/ -#define QEMU_MAKE_CO_LOCKABLE_NONNULL (x) \ - _Generic((x), QemuCoLockable *: (x), \ - QemuMutex *: QMCL_OBJ_(x, mutex), \ - CoMutex *: QMCL_OBJ_(x, co_mutex)) - -static inline void coroutine_fn qemu_co_lockable_lock(QemuCoLockable *x) +static inline QemuCoLockable const *qemu_make_co_lockable(QemuCoLockable const &x) { - x->lock (x->object); +if (x.object) +return &x; +else +return NULL ; } -static inline void coroutine_fn qemu_co_lockable_unlock(QemuCoLockable *x) +#define QEMU_MAKE_CO_LOCKABLE_NONNULL (x) qemu_make_co_lockable_nonnull(qcml_obj_nonnull_(x)) +#define QEMU_MAKE_CO_LOCKABLE (x) qemu_make_co_lockable(qcml_obj_(x)) + +static inline CoroutineFn<void> qemu_co_lockable_lock(const QemuCoLockable *x) { - x->unlock (x->object); + co_await x->lock(x->object); +} + +static inline CoroutineFn<void> qemu_co_lockable_unlock(const QemuCoLockable *x) +{ + co_await x->unlock(x->object); }#endif
Signature · 1 line
2.35.1
PATCH 16/16 port test-coroutine to C++ coroutines
In reply to Paolo Bonzini---
tests/unit/meson.build | 6 +-
.../{test-coroutine.c => test-coroutine.cc} | 140 +++++++++---------
2 files changed, 76 insertions(+), 70 deletions(-)
rename tests/unit/{test-coroutine.c => test-coroutine.cc} (81%)tests/unit/meson.build +5 -1
@@ -168,7 +168,11 @@ slow_tests = { } foreach test_name, extra: tests - src = [test_name + '.c'] + if test_name == 'test-coroutine' + src = [test_name + '.cc'] + else + src = [test_name + '.c'] + endif deps = [qemuutil] if extra.length() > 0 # use a sourceset to quickly separate sources and deps
tests/unit/test-coroutine.cc renamed from tests/unit/test-coroutine.c +71 -69
348 lines not on this page · raw message
Signature · 1 line
2.35.1
Quoted 6 lines · Paolo Bonzini
However, there are no ramifications to actual coroutine code, except for the template syntax "CoroutineFn<return_type>" for the function and the mandatory co_await/co_return keywords... both of which are an improvement, really: the fact that a single function cannot run either inside or outside coroutines is checked by the compiler now, because
qemu_coroutine_createaccepts a function that returns CoroutineFn<void>.
Yeah, these are nice.
Quoted 6 lines
One important difference is that C++ coroutines allocate frames on the heap, and that explains why performance is better in /perf/nesting, which has to do many large memory allocations for the stack in the other two backends (and also a makecontext/swapcontext in the ucontext case). C++ coroutines hardly benefit from the coroutine pool; OTOH that also means the coroutine pool could be removed if we went this way.
Removing the pool would be nice.
Quoted 9 lines
Overall this was ~twice the amount of work of the C experiment, but that's because the two are very different ways to achieve the same goal:
- the design work was substantially smaller in the C experiment, where all the backend does is allocate stack frames and do a loop that invokes a function pointer. Here the backend has to map between the C++ concepts and the QEMU API. In the C case, most of the work was really in the manual conversion which I had to do one function at a time.
Quoted 8 lines
- the remaining work is also completely different: a source-to-source translator (and only build system work in QEMU) for the C experiment; making ~100 files compile in C++ for this one (and relatively little work as far as coroutines are concerned).
This was compiled with GCC 11 only. Coroutine support was added in GCC 10, released in 2020, which IIRC is much newer than the most recent release we support.
Using C++ coroutines is likely to be lower risk than maintaining our own C coroutine source-to-source translator. On the other hand, it exposes QEMU developers to C++ whether they like it or not so it may not be popular.
If we can reach a consensus about C++ language usage in QEMU then I'm in favor of using C++ coroutines. It's probably not realistic to think we can limit C++ language usage to just coroutines forever. Someone finds another C++ feature they absolutely need and over time the codebase becomes C++ - with both its advantages and disadvantages. I'm not sure what the best solution is but this sounds like a recipe for an identity crisis with the potential to cause friction for years. From that perspective I feel it's better to allow C++ and get over it although what I'd really like is just C++ coroutines and nothing else :P.
To add some more detail, although you can write C in C++, it's not idiomatic modern C++. The language lends itself to a different style of programming that some will embrace while others will not. It will be a bigger impedance mismatch than anything currently in the codebase (e.g. glib vs non-glib code).
On the other hand, a number of projects have already gone through a transition like this (gcc, gdb, ...). Maybe we can learn from their mistakes?
Stefan
Quoted 9 lines · Stefan Hajnoczi
If we can reach a consensus about C++ language usage in QEMU then I'm in favor of using C++ coroutines. It's probably not realistic to think we can limit C++ language usage to just coroutines forever. Someone finds another C++ feature they absolutely need and over time the codebase becomes C++ - with both its advantages and disadvantages.
[...] although you can write C in C++, it's not idiomatic modern C++. The language lends itself to a different style of programming that some will embrace while others will not.
Yes, this is an important aspect to discuss. I think coroutines provide a good blueprint for how QEMU might use C++.
I totally agree that, if we go this way, the genie is out of the bottle and other uses of C++ will pop up with 100% probability. But the important thing to note is that our dialect of C is already not standard C, and that some of our or GLib's "innovations" are actually based on experience with C++. We can keep on writing "QEMU's C" if we think of C++ as a supercharged way of writing these quality-of-life improvements that we already write. In some sense coroutines are an extreme example of this idea.
In fact, a C API would have to remain unless all source files are changed to C++, so QEMU would remain mostly a C project with C idioms, but that doesn't prevent _abstracting_ the use of C++ features (written in modern, idiomatic C++) behind an API that C programmers have no problems learning. Again, coroutines are an example of this of keeping the familiar create/enter/yield API and hiding the "magic" of C++ coroutines (and when they don't, that had better be an improvement).
In the end, C++ is a tool that you can use if it leads to better code. For example, I don't see much use of C++ for devices for example, and the storage devices in particular do not even need coroutines because they use the callback-based interface. But perhaps someone will try to use templates to replace repeated inclusion (which is common in hw/display) and others will follow suit. Or perhaps not.
One example that was brought up on IRC is type-safe operations on things such as hwaddr (i.e. hwaddr+int is allowed but hwaddr-hwaddr gives back an int64_t and might even check for overflow). These would be opt in (you get them just by changing a file from .c to .cc), but the actual C++ code would still look very much like C code that uses hwaddr with no type checking. All the operator overloading gunk would be in include/.
A different topic is what would happen if all of QEMU could be compiled as C++, and could inform our selection of C++ idioms even long before we get there. For example, I'm fine with GLib and our type-safe intrusive lists, so I don't have much interest in STL containers and I would prefer _not_ to use STL containers even in .cc files[1]. However, perhaps QEMU's home-grown lock guard might be replaced by something that uses C++ destructors instead of g_autoptr, so perhaps we should consider using std::lock_guard<>, or something like that, instead of QEMU_LOCK_GUARD. It may be interesting to pass down lock_guards as arguments to enforce "this lock is taken" invariants.
But really, coroutines would be enough work so my dish would be full for some time and I wouldn't really have time to look at any of this. :)
Thanks,
Paolo
[1] One big advantage of STL containers for example is that std::vector<> automatically constructs objects instead of just filling memory with zero (or leaving it uninitialized). But that doesn't really matter if we don't use classes with constructors.
Quoted 1 line · Paolo Bonzini
But perhaps someone will try to use templates to replace repeated inclusion (which is common in hw/display) and others will follow suit.
The code in fpu/ desperately calls out for overloading and templates. At present it is a tangle of _Generic and multiple inclusion.
r~
Quoted 55 lines · Paolo Bonzini
Quoted 9 lines · Stefan Hajnoczi
If we can reach a consensus about C++ language usage in QEMU then I'm in favor of using C++ coroutines. It's probably not realistic to think we can limit C++ language usage to just coroutines forever. Someone finds another C++ feature they absolutely need and over time the codebase becomes C++ - with both its advantages and disadvantages.
[...] although you can write C in C++, it's not idiomatic modern C++. The language lends itself to a different style of programming that some will embrace while others will not.
Yes, this is an important aspect to discuss. I think coroutines provide a good blueprint for how QEMU might use C++.
I totally agree that, if we go this way, the genie is out of the bottle and other uses of C++ will pop up with 100% probability. But the important thing to note is that our dialect of C is already not standard C, and that some of our or GLib's "innovations" are actually based on experience with C++. We can keep on writing "QEMU's C" if we think of C++ as a supercharged way of writing these quality-of-life improvements that we already write. In some sense coroutines are an extreme example of this idea.
In fact, a C API would have to remain unless all source files are changed to C++, so QEMU would remain mostly a C project with C idioms, but that doesn't prevent _abstracting_ the use of C++ features (written in modern, idiomatic C++) behind an API that C programmers have no problems learning. Again, coroutines are an example of this of keeping the familiar create/enter/yield API and hiding the "magic" of C++ coroutines (and when they don't, that had better be an improvement).
In the end, C++ is a tool that you can use if it leads to better code. For example, I don't see much use of C++ for devices for example, and the storage devices in particular do not even need coroutines because they use the callback-based interface. But perhaps someone will try to use templates to replace repeated inclusion (which is common in hw/display) and others will follow suit. Or perhaps not.
One example that was brought up on IRC is type-safe operations on things such as hwaddr (i.e. hwaddr+int is allowed but hwaddr-hwaddr gives back an
int64_tand might even check for overflow). These would be opt in (you get them just by changing a file from .c to .cc), but the actual C++ code would still look very much like C code that uses hwaddr with no type checking. All the operator overloading gunk would be ininclude/.A different topic is what would happen if all of QEMU could be compiled as C++, and could inform our selection of C++ idioms even long before we get there. For example, I'm fine with GLib and our type-safe intrusive lists, so I don't have much interest in STL containers and I would prefer _not_ to use STL containers even in .cc files[1]. However, perhaps QEMU's home-grown lock guard might be replaced by something that uses C++ destructors instead of
g_autoptr, so perhaps we should consider using std::lock_guard<>, or something like that, instead ofQEMU_LOCK_GUARD. It may be interesting to pass downlock_guardsas arguments to enforce "this lock is taken" invariants.
Quoted 2 lines
But really, coroutines would be enough work so my dish would be full for some time and I wouldn't really have time to look at any of this. :)
I think it will be necessary to compile QEMU with a C++ compiler quite soon. It is possible to provide C APIs like in the case of coroutines, but sometimes C++ features need to be exposed to the caller (like the lock guards you mentioned). Also, once C++ is available people will start submitting C++ patches simply because they are more comfortable with C++ (especially one-time/infrequent contributors).
We need to agree on a policy so that people know how and when they can use C++.
The policy needs to be simple so it doesn't create hurdles for new contributors.
Does anyone have experience with C projects that introduced C++ and what worked/didn't work?
Stefan
Quoted 3 lines · Stefan Hajnoczi
Also, once C++ is available people will
start submitting C++ patches simply because they are more comfortable with C++ (especially one-time/infrequent contributors).
This to my mind is the major argument against using C++ for coroutines...
-- PMM
Quoted 7 lines · Peter Maydell
Quoted 3 lines · Stefan Hajnoczi
Also, once C++ is available people will
start submitting C++ patches simply because they are more comfortable with C++ (especially one-time/infrequent contributors).This to my mind is the major argument against using C++ for coroutines...
I agree on the need for a policy, but _what_ C++ are they going to be contributing that we should be scared of? We're talking about:
* major features contributed by one-time/infrequent participants (which is already a once-in-a-year thing or so, at least for me)
* ... in an area where there are no examples of using C++ in the tree (or presumably the maintainer would be comfortable reviewing it)
* ... but yet C++ offer killer features (right now there's only C++ coroutines and fpu/)
* ... and where the one-time contributor has put enough investment in using these killer C++ features, that telling them to remove the features would amount to a rewrite.
That does not seem to be a common situation, and not even a problematic one if it were to happen.
Paolo
Quoted 12 lines · Paolo Bonzini
Quoted 7 lines · Peter Maydell
Quoted 3 lines · Stefan Hajnoczi
Also, once C++ is available people will
start submitting C++ patches simply because they are more comfortable with C++ (especially one-time/infrequent contributors).This to my mind is the major argument against using C++ for coroutines...
I agree on the need for a policy, but _what_ C++ are they going to be contributing that we should be scared of? We're talking about:
Quoted 8 lines
* major features contributed by one-time/infrequent participants (which is already a once-in-a-year thing or so, at least for me)
* ... in an area where there are no examples of using C++ in the tree (or presumably the maintainer would be comfortable reviewing it)
* ... but yet C++ offer killer features (right now there's only C++ coroutines and fpu/)
You are assuming people only choose C++ only when it offers features not available in C. I think they might simply be more comfortable in C++.
In other words, if an existing file is compiled using a C++ compiler or they are adding a new file, they don't need a reason to use C++, they can just use it.
You can define rules and a way to enforce a subset of C++, but I think over time the code will be C++. A policy that is complicated discourages contributors.
For these reasons I think that if code runs through a C++ compiler we should just allow C++. Either way, it will take time but that way no one will feel betrayed when C++ creeps in.
That said, I hope we find an option that doesn't involve C++.
Stefan
Quoted 33 lines · Stefan Hajnoczi
Quoted 12 lines · Paolo Bonzini
Quoted 7 lines · Peter Maydell
Quoted 3 lines · Stefan Hajnoczi
Also, once C++ is available people will
start submitting C++ patches simply because they are more comfortable with C++ (especially one-time/infrequent contributors).This to my mind is the major argument against using C++ for coroutines...
I agree on the need for a policy, but _what_ C++ are they going to be contributing that we should be scared of? We're talking about:
Quoted 8 lines
* major features contributed by one-time/infrequent participants (which is already a once-in-a-year thing or so, at least for me)
* ... in an area where there are no examples of using C++ in the tree (or presumably the maintainer would be comfortable reviewing it)
* ... but yet C++ offer killer features (right now there's only C++ coroutines and fpu/)
You are assuming people only choose C++ only when it offers features not available in C. I think they might simply be more comfortable in C++.
In other words, if an existing file is compiled using a C++ compiler or they are adding a new file, they don't need a reason to use C++, they can just use it.
You can define rules and a way to enforce a subset of C++, but I think over time the code will be C++. A policy that is complicated discourages contributors.
Quoted 5 lines
For these reasons I think that if code runs through a C++ compiler we should just allow C++. Either way, it will take time but that way no one will feel betrayed when C++ creeps in.
That said, I hope we find an option that doesn't involve C++.
The real show stopper with our current coroutine impl IIUC, is the undefined behaviour when we yield and restore across different threads.
Is there any relastic hope that we can change QEMU's usage, such that each coroutine is confined to a single thread, to avoid the undefined behaviour ?
With regards,
Daniel
Signature · 3 lines
|: https://berrange.com -o- https://www.flickr.com/photos/dberrange :| |: https://libvirt.org -o- https://fstop138.berrange.com :| |: https://entangle-photo.org -o- https://www.instagram.com/dberrange :|
Quoted 38 lines · Daniel P. Berrangé
Quoted 37 lines · Stefan Hajnoczi
Quoted 12 lines · Paolo Bonzini
Quoted 7 lines · Peter Maydell
Quoted 3 lines · Stefan Hajnoczi
Also, once C++ is available people will
start submitting C++ patches simply because they are more comfortable with C++ (especially one-time/infrequent contributors).This to my mind is the major argument against using C++ for coroutines...
I agree on the need for a policy, but _what_ C++ are they going to be contributing that we should be scared of? We're talking about:
Quoted 8 lines
* major features contributed by one-time/infrequent participants (which is already a once-in-a-year thing or so, at least for me)
* ... in an area where there are no examples of using C++ in the tree (or presumably the maintainer would be comfortable reviewing it)
* ... but yet C++ offer killer features (right now there's only C++ coroutines and fpu/)
You are assuming people only choose C++ only when it offers features not available in C. I think they might simply be more comfortable in C++.
In other words, if an existing file is compiled using a C++ compiler or they are adding a new file, they don't need a reason to use C++, they can just use it.
You can define rules and a way to enforce a subset of C++, but I think over time the code will be C++. A policy that is complicated discourages contributors.
For these reasons I think that if code runs through a C++ compiler we should just allow C++. Either way, it will take time but that way no one will feel betrayed when C++ creeps in.
Quoted 8 lines
Quoted 1 line
That said, I hope we find an option that doesn't involve C++.
The real show stopper with our current coroutine impl IIUC, is the undefined behaviour when we yield and restore across different threads.
Is there any relastic hope that we can change QEMU's usage, such that each coroutine is confined to a single thread, to avoid the undefined behaviour ?
I don't think so. At the point where a coroutine stops executing in the vCPU thread the call stack is too deep. The benefit of coroutines would be lost because it would be necessary to use callback functions (BHs).
Fixes that paper over the undefined behavior have been merged so the bugs should be kept at bay for a while. In theory we can continue with stack-based coroutines but we're likely to hit issues again in the future.
Paolo also prototyped C stackless coroutines. That would require a source-to-source translation that converts a coroutine_fn into a Duff's device state machine. That solution doesn't require C++ but it would be necessary to develop the source-to-source translator and maintain it.
Finally it may be possible to get coroutine in C from clang/gcc. They have the machinery to do it for C++ so maybe they could also offer it in C compiler mode. It would be great to have coroutines available in the toolchain - more reliable and supported than if we roll our own.
Stefan
Quoted 7 lines · Stefan Hajnoczi
You can define rules and a way to enforce a subset of C++, but I think over time the code will be C++. A policy that is complicated discourages contributors.
For these reasons I think that if code runs through a C++ compiler we should just allow C++. Either way, it will take time but that way no one will feel betrayed when C++ creeps in.
Fair enough. We even already have some conventions that will make any C++ that creeps in less weird (for example, mandatory typedef of structs).
I don't think it would be a big deal overall. I actually agree that we should "just allow C++", what matters more to have style rules that make QEMU's flavors of C and C++ consistent.
Most files are leaves (i.e. they just expose an interface via type_init/block_init/etc.), where almost always people will copy from something that already exists and which will be C. What matters are the core APIs, and maintainers have way more authority there to say no to code they're not comfortable reviewing. This way, code within a subsystem will keep some consistency.
Paolo
Quoted 5 lines · Paolo Bonzini
Quoted 4 lines · Stefan Hajnoczi
You can define rules and a way to enforce a subset of C++, but I think over time the code will be C++. A policy that is complicated discourages contributors.
Quoted 7 lines
Quoted 3 lines
For these reasons I think that if code runs through a C++ compiler we should just allow C++. Either way, it will take time but that way no one will feel betrayed when C++ creeps in.
Fair enough. We even already have some conventions that will make any C++ that creeps in less weird (for example, mandatory typedef of structs).
I don't think it would be a big deal overall. I actually agree that we should "just allow C++", what matters more to have style rules that make QEMU's flavors of C and C++ consistent.
I just want to throw in that I personally am absolutely not confident in reviewing C++ code. As for Rust, you keep mentioning that we don’t have anyone who would “shepherd us through the learning experience”, but I find the very same argument applies to C++.
C++ may start out looking like C, but if used ideally, then it is a very different language, too. I understand the difference is that we can incrementally convert our C code to C++, but I’m not comfortable overseeing that process, which I would have to do as a maintainer. Assuming our overall stance does change to “just allowing C++”, I’d feel unjust if I were to reject C++isms just based on the fact that I don’t know C++, so I’d be forced to learn C++. I’m not strictly opposed to that (though from experience I’m more than hesitant), but forcing maintainers to learn C++ is something that has a cost associated with it.
My biggest gripe about C++ is that as far as I understand there are many antipatterns, many of which I think stem from the exact fact that it is kind of compatible with C, and so you can easily accidentally write really bad C++ code; but without years of experience, I’m absolutely not confident that I could recognize them. Now, I might say I’d just disallow complicated stuff, and keep everything C-like, but from what I’ve heard, C-like C++ seems to be exactly one case that is considered bad C++. I’m really under the impression that I’d need years of experience to discern good from bad C++, and I don’t have that experience.
Hanna
Quoted 14 lines · Hanna Reitz
Quoted 13 lines · Paolo Bonzini
Quoted 7 lines · Stefan Hajnoczi
You can define rules and a way to enforce a subset of C++, but I think over time the code will be C++. A policy that is complicated discourages contributors.
For these reasons I think that if code runs through a C++ compiler we should just allow C++. Either way, it will take time but that way no one will feel betrayed when C++ creeps in.
Fair enough. We even already have some conventions that will make any C++ that creeps in less weird (for example, mandatory typedef of structs).
Quoted 6 lines
Quoted 3 lines
I don't think it would be a big deal overall. I actually agree that we should "just allow C++", what matters more to have style rules that make QEMU's flavors of C and C++ consistent.
I just want to throw in that I personally am absolutely not confident in reviewing C++ code.
Me neither.
Quoted 7 lines
As for Rust, you keep mentioning that we don’t
have anyone who would “shepherd us through the learning experience”, but I find the very same argument applies to C++.C++ may start out looking like C, but if used ideally, then it is a very different language, too. I understand the difference is that we can incrementally convert our C code to C++,
Bad C++ in need of rework, presumably.
Quoted 2 lines
but I’m not comfortable
overseeing that process, which I would have to do as a maintainer.
That makes two of us.
The plain language version of "I'm not comfortable serving" is of course "I do not intend to serve".
Quoted 6 lines
Assuming our overall stance does change to “just allowing C++”, I’d feel unjust if I were to reject C++isms just based on the fact that I don’t know C++, so I’d be forced to learn C++. I’m not strictly opposed to that (though from experience I’m more than hesitant), but forcing maintainers to learn C++ is something that has a cost associated with it.
I'm a lot more interested in learning Rust than in (re-)learning C++.
Quoted 10 lines
My biggest gripe about C++ is that as far as I understand there are many antipatterns, many of which I think stem from the exact fact that it is kind of compatible with C, and so you can easily accidentally write really bad C++ code; but without years of experience, I’m absolutely not confident that I could recognize them. Now, I might say I’d just disallow complicated stuff, and keep everything C-like, but from what I’ve heard, C-like C++ seems to be exactly one case that is considered bad C++. I’m really under the impression that I’d need years of experience to discern good from bad C++, and I don’t have that experience.
Right.
Am 15.03.2022 um 15:05 hat Stefan Hajnoczi geschrieben:
Quoted 56 lines · Paolo Bonzini
Quoted 55 lines · Stefan Hajnoczi
Quoted 9 lines
If we can reach a consensus about C++ language usage in QEMU then I'm in favor of using C++ coroutines. It's probably not realistic to think we can limit C++ language usage to just coroutines forever. Someone finds another C++ feature they absolutely need and over time the codebase becomes C++ - with both its advantages and disadvantages.
[...] although you can write C in C++, it's not idiomatic modern C++. The language lends itself to a different style of programming that some will embrace while others will not.
Yes, this is an important aspect to discuss. I think coroutines provide a good blueprint for how QEMU might use C++.
I totally agree that, if we go this way, the genie is out of the bottle and other uses of C++ will pop up with 100% probability. But the important thing to note is that our dialect of C is already not standard C, and that some of our or GLib's "innovations" are actually based on experience with C++. We can keep on writing "QEMU's C" if we think of C++ as a supercharged way of writing these quality-of-life improvements that we already write. In some sense coroutines are an extreme example of this idea.
In fact, a C API would have to remain unless all source files are changed to C++, so QEMU would remain mostly a C project with C idioms, but that doesn't prevent _abstracting_ the use of C++ features (written in modern, idiomatic C++) behind an API that C programmers have no problems learning. Again, coroutines are an example of this of keeping the familiar create/enter/yield API and hiding the "magic" of C++ coroutines (and when they don't, that had better be an improvement).
In the end, C++ is a tool that you can use if it leads to better code. For example, I don't see much use of C++ for devices for example, and the storage devices in particular do not even need coroutines because they use the callback-based interface. But perhaps someone will try to use templates to replace repeated inclusion (which is common in hw/display) and others will follow suit. Or perhaps not.
One example that was brought up on IRC is type-safe operations on things such as hwaddr (i.e. hwaddr+int is allowed but hwaddr-hwaddr gives back an
int64_tand might even check for overflow). These would be opt in (you get them just by changing a file from .c to .cc), but the actual C++ code would still look very much like C code that uses hwaddr with no type checking. All the operator overloading gunk would be ininclude/.A different topic is what would happen if all of QEMU could be compiled as C++, and could inform our selection of C++ idioms even long before we get there. For example, I'm fine with GLib and our type-safe intrusive lists, so I don't have much interest in STL containers and I would prefer _not_ to use STL containers even in .cc files[1]. However, perhaps QEMU's home-grown lock guard might be replaced by something that uses C++ destructors instead of
g_autoptr, so perhaps we should consider using std::lock_guard<>, or something like that, instead ofQEMU_LOCK_GUARD. It may be interesting to pass downlock_guardsas arguments to enforce "this lock is taken" invariants.
Quoted 7 lines
Quoted 2 lines
But really, coroutines would be enough work so my dish would be full for some time and I wouldn't really have time to look at any of this. :)
I think it will be necessary to compile QEMU with a C++ compiler quite soon. It is possible to provide C APIs like in the case of coroutines, but sometimes C++ features need to be exposed to the caller (like the lock guards you mentioned).
I'm not sure what the C++ lock guards offer that our current lock guards don't? Passing down lock guards makes sense to me, but why can't you do that with QemuLockable? (Hm, or can the C++ version somehow check at compile time that it's the _right_ lock that is held rather than just any lock? It didn't look like it at the first sight.)
But I do see the benefit of a compiler checked CoroutineFn<> return type compared to the coroutine_fn markers we have today. On the other hand...
Quoted 3 lines
Also, once C++ is available people will start submitting C++ patches simply because they are more comfortable with C++ (especially one-time/infrequent contributors).
...using C++ in coroutine code means that all of the block layer would suddenly become C++ and would be most affected by this effect. I'm not sure if that's something I would like to see, at least until there is a clear tree-wide policy (that preferably limits it to a subset that I understand).
Kevin
Quoted 65 lines · Kevin Wolf
Am 15.03.2022 um 15:05 hat Stefan Hajnoczi geschrieben:
Quoted 56 lines · Paolo Bonzini
Quoted 55 lines · Stefan Hajnoczi
Quoted 9 lines
If we can reach a consensus about C++ language usage in QEMU then I'm in favor of using C++ coroutines. It's probably not realistic to think we can limit C++ language usage to just coroutines forever. Someone finds another C++ feature they absolutely need and over time the codebase becomes C++ - with both its advantages and disadvantages.
[...] although you can write C in C++, it's not idiomatic modern C++. The language lends itself to a different style of programming that some will embrace while others will not.
Yes, this is an important aspect to discuss. I think coroutines provide a good blueprint for how QEMU might use C++.
I totally agree that, if we go this way, the genie is out of the bottle and other uses of C++ will pop up with 100% probability. But the important thing to note is that our dialect of C is already not standard C, and that some of our or GLib's "innovations" are actually based on experience with C++. We can keep on writing "QEMU's C" if we think of C++ as a supercharged way of writing these quality-of-life improvements that we already write. In some sense coroutines are an extreme example of this idea.
In fact, a C API would have to remain unless all source files are changed to C++, so QEMU would remain mostly a C project with C idioms, but that doesn't prevent _abstracting_ the use of C++ features (written in modern, idiomatic C++) behind an API that C programmers have no problems learning. Again, coroutines are an example of this of keeping the familiar create/enter/yield API and hiding the "magic" of C++ coroutines (and when they don't, that had better be an improvement).
In the end, C++ is a tool that you can use if it leads to better code. For example, I don't see much use of C++ for devices for example, and the storage devices in particular do not even need coroutines because they use the callback-based interface. But perhaps someone will try to use templates to replace repeated inclusion (which is common in hw/display) and others will follow suit. Or perhaps not.
One example that was brought up on IRC is type-safe operations on things such as hwaddr (i.e. hwaddr+int is allowed but hwaddr-hwaddr gives back an
int64_tand might even check for overflow). These would be opt in (you get them just by changing a file from .c to .cc), but the actual C++ code would still look very much like C code that uses hwaddr with no type checking. All the operator overloading gunk would be ininclude/.A different topic is what would happen if all of QEMU could be compiled as C++, and could inform our selection of C++ idioms even long before we get there. For example, I'm fine with GLib and our type-safe intrusive lists, so I don't have much interest in STL containers and I would prefer _not_ to use STL containers even in .cc files[1]. However, perhaps QEMU's home-grown lock guard might be replaced by something that uses C++ destructors instead of
g_autoptr, so perhaps we should consider using std::lock_guard<>, or something like that, instead ofQEMU_LOCK_GUARD. It may be interesting to pass downlock_guardsas arguments to enforce "this lock is taken" invariants.Quoted 7 lines
Quoted 2 lines
But really, coroutines would be enough work so my dish would be full for some time and I wouldn't really have time to look at any of this. :)
I think it will be necessary to compile QEMU with a C++ compiler quite soon. It is possible to provide C APIs like in the case of coroutines, but sometimes C++ features need to be exposed to the caller (like the lock guards you mentioned).
Quoted 8 lines
I'm not sure what the C++ lock guards offer that our current lock guards don't? Passing down lock guards makes sense to me, but why can't you do that with QemuLockable? (Hm, or can the C++ version somehow check at compile time that it's the _right_ lock that is held rather than just any lock? It didn't look like it at the first sight.)
But I do see the benefit of a compiler checked CoroutineFn<> return type compared to the
coroutine_fnmarkers we have today. On the other hand...
Sorry, I made a mistake: the C++ coroutines implementation does not hide everything behind a C API. Coroutine functions need to be defined in C++ source units.
Stefan
Quoted 81 lines · Kevin Wolf
Am 15.03.2022 um 15:05 hat Stefan Hajnoczi geschrieben:
Quoted 56 lines · Paolo Bonzini
Quoted 55 lines · Stefan Hajnoczi
Quoted 9 lines
If we can reach a consensus about C++ language usage in QEMU then I'm in favor of using C++ coroutines. It's probably not realistic to think we can limit C++ language usage to just coroutines forever. Someone finds another C++ feature they absolutely need and over time the codebase becomes C++ - with both its advantages and disadvantages.
[...] although you can write C in C++, it's not idiomatic modern C++. The language lends itself to a different style of programming that some will embrace while others will not.
Yes, this is an important aspect to discuss. I think coroutines provide a good blueprint for how QEMU might use C++.
I totally agree that, if we go this way, the genie is out of the bottle and other uses of C++ will pop up with 100% probability. But the important thing to note is that our dialect of C is already not standard C, and that some of our or GLib's "innovations" are actually based on experience with C++. We can keep on writing "QEMU's C" if we think of C++ as a supercharged way of writing these quality-of-life improvements that we already write. In some sense coroutines are an extreme example of this idea.
In fact, a C API would have to remain unless all source files are changed to C++, so QEMU would remain mostly a C project with C idioms, but that doesn't prevent _abstracting_ the use of C++ features (written in modern, idiomatic C++) behind an API that C programmers have no problems learning. Again, coroutines are an example of this of keeping the familiar create/enter/yield API and hiding the "magic" of C++ coroutines (and when they don't, that had better be an improvement).
In the end, C++ is a tool that you can use if it leads to better code. For example, I don't see much use of C++ for devices for example, and the storage devices in particular do not even need coroutines because they use the callback-based interface. But perhaps someone will try to use templates to replace repeated inclusion (which is common in hw/display) and others will follow suit. Or perhaps not.
One example that was brought up on IRC is type-safe operations on things such as hwaddr (i.e. hwaddr+int is allowed but hwaddr-hwaddr gives back an
int64_tand might even check for overflow). These would be opt in (you get them just by changing a file from .c to .cc), but the actual C++ code would still look very much like C code that uses hwaddr with no type checking. All the operator overloading gunk would be ininclude/.A different topic is what would happen if all of QEMU could be compiled as C++, and could inform our selection of C++ idioms even long before we get there. For example, I'm fine with GLib and our type-safe intrusive lists, so I don't have much interest in STL containers and I would prefer _not_ to use STL containers even in .cc files[1]. However, perhaps QEMU's home-grown lock guard might be replaced by something that uses C++ destructors instead of
g_autoptr, so perhaps we should consider using std::lock_guard<>, or something like that, instead ofQEMU_LOCK_GUARD. It may be interesting to pass downlock_guardsas arguments to enforce "this lock is taken" invariants.Quoted 7 lines
Quoted 2 lines
But really, coroutines would be enough work so my dish would be full for some time and I wouldn't really have time to look at any of this. :)
I think it will be necessary to compile QEMU with a C++ compiler quite soon. It is possible to provide C APIs like in the case of coroutines, but sometimes C++ features need to be exposed to the caller (like the lock guards you mentioned).
I'm not sure what the C++ lock guards offer that our current lock guards don't? Passing down lock guards makes sense to me, but why can't you do that with QemuLockable? (Hm, or can the C++ version somehow check at compile time that it's the _right_ lock that is held rather than just any lock? It didn't look like it at the first sight.)
But I do see the benefit of a compiler checked CoroutineFn<> return type compared to the
coroutine_fnmarkers we have today. On the other hand...Quoted 3 lines
Also, once C++ is available people will start submitting C++ patches simply because they are more comfortable with C++ (especially one-time/infrequent contributors).
Quoted 5 lines
...using C++ in coroutine code means that all of the block layer would suddenly become C++ and would be most affected by this effect. I'm not sure if that's something I would like to see, at least until there is a clear tree-wide policy (that preferably limits it to a subset that I understand).
Expecting maintainers to enforce a subset during code review feels like it would be a tedious burden, that will inevitably let stuff through because humans are fallible, especially when presented with uninspiring, tedious, repetitive tasks.
Restricting ourselves to a subset is only viable if we have an automated tool that can reliably enforce that subset. I'm not sure that any such tool exists, and not convinced our time is best served by trying to write & maintainer one either.
IOW, I fear one we allow C++ in any level, it won't be practical to constrain it as much we desire. I fear us turning QEMU into even more of a monster like other big C++ apps I see which take all hours to compile while using all available RAM in Fedora RPM build hosts.
My other question is whether adoption of C++ would complicate any desire to make more use of Rust in QEMU ? I know Rust came out of work by the Mozilla Firefox crew, and Firefox was C++, but I don't have any idea how they integrated use of Rust with Firefox, so whether there are any gotcha's for us or not ?
Regards,
Daniel
Signature · 3 lines
|: https://berrange.com -o- https://www.flickr.com/photos/dberrange :| |: https://libvirt.org -o- https://fstop138.berrange.com :| |: https://entangle-photo.org -o- https://www.instagram.com/dberrange :|
Quoted 9 lines · Daniel P. Berrangé
Expecting maintainers to enforce a subset during code review feels like it would be a tedious burden, that will inevitably let stuff through because humans are fallible, especially when presented with uninspiring, tedious, repetitive tasks.
Restricting ourselves to a subset is only viable if we have an automated tool that can reliably enforce that subset. I'm not sure that any such tool exists, and not convinced our time is best served by trying to write & maintainer one either.
We don't need to have a policy on which features are used. We need to have goals for what to use C++ for. I won't go into further details here, because I had already posted "When and how to use C++"[1] about an hour before your reply.
Quoted 5 lines
IOW, I fear one we allow C++ in any level, it won't be practical to constrain it as much we desire. I fear us turning QEMU into even more of a monster like other big C++ apps I see which take all hours to compile while using all available RAM in Fedora RPM build hosts.
Sorry but this is FUD. There's plenty of C++ apps and libraries that do not "take hours to compile while using all available RAM". You're probably thinking of the Chromium/Firefox/Libreoffice triplet but those are an order of magnitude larger than QEMU. And in fact, QEMU is *already* a monster that takes longer to compile than most other packages, no matter the language they're written in.
Most of KDE and everything that uses Qt is written in C++, and so is Inkscape in GTK+ land. LLVM and Clang are written in C++. Hotspot and V8 are written in C++. Kodi, MAME and DolphinEmu are written in C++. GCC and GDB have migrated to C++ and their compile times have not exploded.
Quoted 5 lines
My other question is whether adoption of C++ would complicate any desire to make more use of Rust in QEMU ? I know Rust came out of work by the Mozilla Firefox crew, and Firefox was C++, but I don't have any idea how they integrated use of Rust with Firefox, so whether there are any gotcha's for us or not ?
Any Rust integration would go through C APIs. Using Rust in the block layer would certainly be much harder, though perhaps not impossible, if the block layer uses C++ coroutines. Rust supports something similar, but two-direction interoperability would be hard.
For everything else, not much. Even if using C++, the fact that QEMU's APIs are primarily C would not change. Changing "timer_mod_ns(timer, ns)" to "timer.modify_ns(ns)" is not on the table.
But really, first of all the question should be who is doing work on integrating Rust with QEMU. I typically hear about this topic exactly once a year at KVM Forum, and then nothing. We have seen Marc-André's QAPI integration experiment, but it's not clear to me what the path would be from there to wider use in QEMU.
In particular, after ~3 years of talking about it, it is not even clear:
- what subsystems would benefit the most from the adoption of Rust, and whether that would be feasible without a rewrite which will simply never happen
- what the plans would be for coexistence of Rust and C code within a subsystem
- whether maintainers would be on board with adopting a completely different language, and who in the community has enough Rust experience to shepherd us through the learning experience
The first two questions have answers in the other message if s/Rust/C++/, and as to the last I think we're already further in the discussion.
Thanks,
Paolo
Quoted 40 lines · Paolo Bonzini
Quoted 9 lines · Daniel P. Berrangé
Expecting maintainers to enforce a subset during code review feels like it would be a tedious burden, that will inevitably let stuff through because humans are fallible, especially when presented with uninspiring, tedious, repetitive tasks.
Restricting ourselves to a subset is only viable if we have an automated tool that can reliably enforce that subset. I'm not sure that any such tool exists, and not convinced our time is best served by trying to write & maintainer one either.
We don't need to have a policy on which features are used. We need to have goals for what to use C++ for. I won't go into further details here, because I had already posted "When and how to use C++"[1] about an hour before your reply.
Quoted 5 lines
IOW, I fear one we allow C++ in any level, it won't be practical to constrain it as much we desire. I fear us turning QEMU into even more of a monster like other big C++ apps I see which take all hours to compile while using all available RAM in Fedora RPM build hosts.
Sorry but this is FUD. There's plenty of C++ apps and libraries that do not "take hours to compile while using all available RAM". You're probably thinking of the Chromium/Firefox/Libreoffice triplet but those are an order of magnitude larger than QEMU. And in fact, QEMU is *already* a monster that takes longer to compile than most other packages, no matter the language they're written in.
Most of KDE and everything that uses Qt is written in C++, and so is Inkscape in GTK+ land. LLVM and Clang are written in C++. Hotspot and V8 are written in C++. Kodi, MAME and DolphinEmu are written in C++. GCC and GDB have migrated to C++ and their compile times have not exploded.
Quoted 5 lines
My other question is whether adoption of C++ would complicate any desire to make more use of Rust in QEMU ? I know Rust came out of work by the Mozilla Firefox crew, and Firefox was C++, but I don't have any idea how they integrated use of Rust with Firefox, so whether there are any gotcha's for us or not ?
Quoted 4 lines
Any Rust integration would go through C APIs. Using Rust in the block layer would certainly be much harder, though perhaps not impossible, if the block layer uses C++ coroutines. Rust supports something similar, but two-direction interoperability would be hard.
I haven't looked at this in depth but there is a solution for Rust-C++
interop: https://cxx.rs/
Stefan
Am 16.03.2022 um 13:40 hat Stefan Hajnoczi geschrieben:
Quoted 41 lines · Paolo Bonzini
Quoted 40 lines · Daniel P. Berrangé
Quoted 9 lines
Expecting maintainers to enforce a subset during code review feels like it would be a tedious burden, that will inevitably let stuff through because humans are fallible, especially when presented with uninspiring, tedious, repetitive tasks.
Restricting ourselves to a subset is only viable if we have an automated tool that can reliably enforce that subset. I'm not sure that any such tool exists, and not convinced our time is best served by trying to write & maintainer one either.
We don't need to have a policy on which features are used. We need to have goals for what to use C++ for. I won't go into further details here, because I had already posted "When and how to use C++"[1] about an hour before your reply.
Quoted 5 lines
IOW, I fear one we allow C++ in any level, it won't be practical to constrain it as much we desire. I fear us turning QEMU into even more of a monster like other big C++ apps I see which take all hours to compile while using all available RAM in Fedora RPM build hosts.
Sorry but this is FUD. There's plenty of C++ apps and libraries that do not "take hours to compile while using all available RAM". You're probably thinking of the Chromium/Firefox/Libreoffice triplet but those are an order of magnitude larger than QEMU. And in fact, QEMU is *already* a monster that takes longer to compile than most other packages, no matter the language they're written in.
Most of KDE and everything that uses Qt is written in C++, and so is Inkscape in GTK+ land. LLVM and Clang are written in C++. Hotspot and V8 are written in C++. Kodi, MAME and DolphinEmu are written in C++. GCC and GDB have migrated to C++ and their compile times have not exploded.
Quoted 5 lines
My other question is whether adoption of C++ would complicate any desire to make more use of Rust in QEMU ? I know Rust came out of work by the Mozilla Firefox crew, and Firefox was C++, but I don't have any idea how they integrated use of Rust with Firefox, so whether there are any gotcha's for us or not ?
Quoted 7 lines
Quoted 4 lines
Any Rust integration would go through C APIs. Using Rust in the block layer would certainly be much harder, though perhaps not impossible, if the block layer uses C++ coroutines. Rust supports something similar, but two-direction interoperability would be hard.
I haven't looked at this in depth but there is a solution for Rust-C++
interop: https://cxx.rs/
"Direct FFI of async functions is absolutely in scope for CXX (on C++20 and up) but is not implemented yet in the current release."
With the current QEMU coroutines, calling Rust async fns from C is relatively easy, and calling C coroutine_fns from a Rust async fn is trivial when the Rust async fn is already called from a C coroutine (because qemu_coroutine_yield() just works, we still have a coroutine stack from the original C caller).
I suppose calling Rust async fns from C++ could actually have the same implementation as with C when using Paolo's wrappers, but the other direction might be a bit harder - to be honest, I can't tell because I've never checked how C++ coroutines work internally. Could as well be that a Rust wrapper for them isn't that hard after all.
Kevin
Quoted 29 lines · * Paolo Bonzini ([email protected]) wrote:
Quoted 9 lines · Daniel P. Berrangé
Expecting maintainers to enforce a subset during code review feels like it would be a tedious burden, that will inevitably let stuff through because humans are fallible, especially when presented with uninspiring, tedious, repetitive tasks.
Restricting ourselves to a subset is only viable if we have an automated tool that can reliably enforce that subset. I'm not sure that any such tool exists, and not convinced our time is best served by trying to write & maintainer one either.
We don't need to have a policy on which features are used. We need to have goals for what to use C++ for. I won't go into further details here, because I had already posted "When and how to use C++"[1] about an hour before your reply.
Quoted 5 lines
IOW, I fear one we allow C++ in any level, it won't be practical to constrain it as much we desire. I fear us turning QEMU into even more of a monster like other big C++ apps I see which take all hours to compile while using all available RAM in Fedora RPM build hosts.
Sorry but this is FUD. There's plenty of C++ apps and libraries that do not "take hours to compile while using all available RAM". You're probably thinking of the Chromium/Firefox/Libreoffice triplet but those are an order of magnitude larger than QEMU. And in fact, QEMU is *already* a monster that takes longer to compile than most other packages, no matter the language they're written in.
Quoted 4 lines
Most of KDE and everything that uses Qt is written in C++, and so is Inkscape in GTK+ land. LLVM and Clang are written in C++. Hotspot and V8 are written in C++. Kodi, MAME and DolphinEmu are written in C++. GCC and GDB have migrated to C++ and their compile times have not exploded.
While I think it does take longer to compile, the bigger problem for the CI setup is the amount of RAM-per-compile-process; it's not so much the fact that those applications are huge that's the problem, it's that a make -j ($threads) can run out of RAM.
Dave
Quoted 41 lines
Quoted 5 lines
My other question is whether adoption of C++ would complicate any desire to make more use of Rust in QEMU ? I know Rust came out of work by the Mozilla Firefox crew, and Firefox was C++, but I don't have any idea how they integrated use of Rust with Firefox, so whether there are any gotcha's for us or not ?
Any Rust integration would go through C APIs. Using Rust in the block layer would certainly be much harder, though perhaps not impossible, if the block layer uses C++ coroutines. Rust supports something similar, but two-direction interoperability would be hard.
For everything else, not much. Even if using C++, the fact that QEMU's APIs are primarily C would not change. Changing "
timer_mod_ns(timer, ns)" to "timer.modify_ns(ns)" is not on the table.But really, first of all the question should be who is doing work on integrating Rust with QEMU. I typically hear about this topic exactly once a year at KVM Forum, and then nothing. We have seen Marc-André's QAPI integration experiment, but it's not clear to me what the path would be from there to wider use in QEMU.
In particular, after ~3 years of talking about it, it is not even clear:
- what subsystems would benefit the most from the adoption of Rust, and whether that would be feasible without a rewrite which will simply never happen
- what the plans would be for coexistence of Rust and C code within a subsystem
- whether maintainers would be on board with adopting a completely different language, and who in the community has enough Rust experience to shepherd us through the learning experience
The first two questions have answers in the other message if s/Rust/C++/, and as to the last I think we're already further in the discussion.
Thanks,
Paolo
Signature · 1 line
Dr. David Alan Gilbert / [email protected] / Manchester, UK
Quoted 11 lines · Paolo Bonzini
Quoted 9 lines · Daniel P. Berrangé
Expecting maintainers to enforce a subset during code review feels like it would be a tedious burden, that will inevitably let stuff through because humans are fallible, especially when presented with uninspiring, tedious, repetitive tasks.
Restricting ourselves to a subset is only viable if we have an automated tool that can reliably enforce that subset. I'm not sure that any such tool exists, and not convinced our time is best served by trying to write & maintainer one either.
Quoted 4 lines
We don't need to have a policy on which features are used. We need to have goals for what to use C++ for. I won't go into further details here, because I had already posted "When and how to use C++"[1] about an hour before your reply.
I see that mail, but I don't think it addresses my point.
We already use GLib and it provides a bunch of data structures, objects and APIs, alot of which will overlap with C++ standard library, not to mention QEMU's own stuff that predates GLib.
The guidelines say what we want to use C++ for which fine, but it doesn't help maintainers/reviewers evaluating whether the proposed C++ usage is desired.
Do we prefer GLib APIs and data structures for consistency & interoperability with remaining C-only code, or do we prefer the libstdc++, or do we allow both but set rules on when to use each, or do we allow a free-for-all with author or reviewer deciding based on their personal preference.
If we don't have a policy for C++ feature usage, then we get the free-for-all by default. May be that is OK, maybe not. Either way we need to declare our intent, so reviewers know what to complain about and what to not.
Personally I'm not a fan of having a codebase with many different APIs/data structures for doing the same job, so would like a clear policy for what to use / prefer, ideally one we can enforce with tools rather than rely on humans to spot.
Quoted 6 lines
Quoted 5 lines
IOW, I fear one we allow C++ in any level, it won't be practical to constrain it as much we desire. I fear us turning QEMU into even more of a monster like other big C++ apps I see which take all hours to compile while using all available RAM in Fedora RPM build hosts.
Quoted 6 lines
Sorry but this is FUD. There's plenty of C++ apps and libraries that do not "take hours to compile while using all available RAM". You're probably thinking of the Chromium/Firefox/Libreoffice triplet but those are an order of magnitude larger than QEMU. And in fact, QEMU is *already* a monster that takes longer to compile than most other packages, no matter the language they're written in.
I was particularly considering Ceph when I wrote the above, which is bigger, but still a similar order of magnutude in size compared to QEMU, but in Fedora Ceph takes 12 hours to compile on the slower arch, vs QEMU's 3 hrs, or 3 hrs on x86_64 vs QEMU's 1 hr. Maybe I'm being mistaken in blaming C++ in general, and it is something else Or maybe certain features trigger this slowness and we can consider if we should stay away from them
With regards,
Daniel
Signature · 3 lines
|: https://berrange.com -o- https://www.flickr.com/photos/dberrange :| |: https://libvirt.org -o- https://fstop138.berrange.com :| |: https://entangle-photo.org -o- https://www.instagram.com/dberrange :|
Quoted 1 line · Daniel P. Berrangé
On Tue, Mar 15, 2022 at 03:50:26PM +0100, Kevin Wolf wrote:
[...]
Quoted 6 lines
Quoted 5 lines
...using C++ in coroutine code means that all of the block layer would suddenly become C++ and would be most affected by this effect. I'm not sure if that's something I would like to see, at least until there is a clear tree-wide policy (that preferably limits it to a subset that I understand).
Quoted 7 lines
Expecting maintainers to enforce a subset during code review feels like it would be a tedious burden, that will inevitably let stuff through because humans are fallible, especially when presented with uninspiring, tedious, repetitive tasks.
Restricting ourselves to a subset is only viable if we have an automated tool that can reliably enforce that subset.
Concur. We're talking about a complex subset of an even more complex whole, where few to none of us will fully understand either.
Quoted 9 lines
I'm not
sure that any such tool exists, and not convinced our time is best served by trying to write & maintainer one either.IOW, I fear one we allow C++ in any level, it won't be practical to constrain it as much we desire. I fear us turning QEMU into even more of a monster like other big C++ apps I see which take all hours to compile while using all available RAM in Fedora RPM build hosts.
While I'm certainly concerned about the often poor ergonomics of compiling C++ code, I'm even more concerned about the poor ergonomics of *grokking* C++ code.
Quoted 5 lines
My other question is whether adoption of C++ would complicate any desire to make more use of Rust in QEMU ? I know Rust came out of work by the Mozilla Firefox crew, and Firefox was C++, but I don't have any idea how they integrated use of Rust with Firefox, so whether there are any gotcha's for us or not ?
Good question.
When and how to use C++ (was Re: [PATCH experiment 00/16] C++20 coroutine backend)
In reply to Kevin WolfQuoted 3 lines · Kevin Wolf
I'm not sure what the C++ lock guards offer that our current lock guards don't? Passing down lock guards makes sense to me, but why can't you do that with QemuLockable?
Passing a QemuLockable alone doesn't ensure that the lock has been taken. I guess we could build a QemuLockGuard on top that ensures that.
Quoted 3 lines
(Hm, or can the C++ version somehow check at
compile time that it's the _right_ lock that is held rather than just any lock? It didn't look like it at the first sight.)
It's not related to lock guards but clang++ has thread-safety analysis that checks for the right lock. It is in theory there for C as well but it's mostly a joke. Ironically, the only good use that I've seen of it is Marc-André's never committed work to check coroutine_fn markers at compile-time[1].
Quoted 7 lines
But I do see the benefit of a compiler checked CoroutineFn<> return type compared to the
coroutine_fnmarkers we have today. On the other hand...Quoted 3 lines
Also, once C++ is available people will start submitting C++ patches simply because they are more comfortable with C++ (especially one-time/infrequent contributors).
Quoted 5 lines
...using C++ in coroutine code means that all of the block layer would suddenly become C++ and would be most affected by this effect. I'm not sure if that's something I would like to see, at least until there is a clear tree-wide policy (that preferably limits it to a subset that I understand).
You are the maintainer of the block layer, so you would have the biggest impact and obviously have input in such a policy. I obviously don't have a policy ready[2], but I do have some goals:
1) whenever C++ is used to build an API for use in C++ source files:
Those source files and their debugging experience should look
like C, and not any more "foreign" than the "QEMU C" dialect
that we already have.
Rationale: we might have funky code in headers, but "QEMU_WITH_LOCK_GUARD(x) { }" is understandable; the same would hold for "CoroutineFn<void>", "co_await fn();" or if hwaddr internally used operator overloading.
2) whenever C and C++ provide two different implementations of an API, for use in C and C++ source files respectively:
If the same API is implemented in different ways for C and C++, the semantics should be equivalent, and covered by both C and C++ testcases.
Rationale: this should be a rare occasion, but there are features such as _Generic that are C only, so it should be catered for; QemuLockable came out already in this proof of concept. The implementation is completely different for C and C++, but it works the same (as proved to some extent by testcases).
Looking again at the hwaddr example, it would be okay to forbid "hwaddr - hwaddr" subtraction in C++ code. But it would not be okay to have it return a signed integer in C++, while it returns an unsigned integer in C. Could there be a C++-only method "hwaddr.diff(hwaddr)" that does return a signed integer? That would have to be discussed, right now my opinion is "probably not" but see the next point too.
3) whenever C++ features (templates, classes, etc.) are used in .cc files:
The features should have a clear benefit over equivalent C-only code (e.g. using the preprocessor instead of templates) and if applicable they should publish a C API[3] that can be consumed outside a particular subsystem.
If in doubt, err on the side of moderation, i.e. less C++ and fewer C++ features.
Example: I can imagine that in some cases the semantic replacement abilities provided by templates will for example improve compile-time checking and reduce code duplication compared to gcc. However, QEMU already has a wide range of data structures and auxiliary APIs for C, and those should be used for C++ code to avoid splitting the code base.
In turn, this should limit the usage of other C++ features. For example, constructors (as opposed to just being able to zero-initialize memory) can be more of a pain than a benefit if you can't use "new", "delete" or "std::vector<>". Likewise, it's unlikely that we'll have reasons to use anonymous namespaces instead of "static", because that's mostly useful for classes defined in .cc files and their member functions[5]. And VMState will prevent the usage of "private:", thus lowering the benefit from methods.
Would this kind of formulation be enough? Maybe, maybe not, but either way I don't really believe in prohibiting features by policy. The QEMU community and especially its maintainers won't change overnight if C++ is allowed in the codebase; but they should be allowed to evolve their opinions as their experience grows. If some day the block layer maintainers want to extend coroutines to variable arguments rather than a single void *, they shouldn't be forbidden from doing so just because (I guess) it entails using variadic templates. They should show that there is a benefit, though.
In any case, Stefan's suggestion to look into what other projects did, especially GCC/GDB is a good one:
- https://gcc.gnu.org/wiki/CppConventions probably is a good start[4], but we would probably cut on the "explicitly permitted" parts. https://gcc.gnu.org/codingrationale.html also focuses a lot on things that QEMU would likely not use.
- the discussion at https://gcc.gnu.org/legacy-ml/gcc/2010-05/msg00705.html has some quotes that capture interesting the point. It's possible that I have subconsciously introduced it in my two messages on the topic:
The goal is not to reimplement GCC from the ground up using modern OO/C++ techniques. The goal is simply to permit ourselves to use C++ features where appropriate in the codebase.
---
The right subset is dependent on context. We're not in a safety-critical environment, we have a large existing C codebase, and we have a developer team made up of experienced C programmers, not all of whom are used to programming in C++. So, we need to take those factors into account.
---
Switching to C++ should never be excuse to bring more more brittle codes or more obscurities. Rather, it should be opportunity to write simpler and better code.
Also of interest is the replies to less enthusiastic developers starting at https://gcc.gnu.org/legacy-ml/gcc/2010-05/msg00744.html at https://gcc.gnu.org/legacy-ml/gcc/2010-06/msg00174.html.
- Richard might have some experience here, and his insight is valuable.
Paolo
[1] https://lists.gnu.org/archive/html/qemu-devel/2017-07/msg00793.html
[2] in fact, even if we decided to go with C++ coroutines, it would take quite some time to split the coroutine/non-coroutine cases, which is a prerequisite for that, so we'd have a lot of time to develop a policy. Once the policy is there, there will be even more time before a C++-using patch that does something useful gets submitted and approved.
[3] or at least C-like, for example qemu_coroutine_create technically is a C++-only API because it takes a CoroutineFn<void>(*)(void *) argument. But it *looks* like C.
[4] lol, there's even a remark fom me in there, but I stopped working on GCC about at the time that the document was being written.
[5] https://stackoverflow.com/questions/154469/unnamed-anonymous-namespaces-vs-static-functions
Quoted 3 lines · Paolo Bonzini
This was compiled with GCC 11 only. Coroutine support was added in GCC 10, released in 2020, which IIRC is much newer than the most recent release we support.
Currrently we target 7.4:
commit 2a85a08c998e418a46a308095893f223642f6fc9
Author: Daniel P. Berrangé <[email protected]>
Date: Fri May 14 13:04:15 2021 +0100
configure: bump min required CLang to 6.0 / XCode 10.0
Several distros have been dropped since the last time we bumped the
minimum required CLang version.
Per repology, currently shipping versions are:
RHEL-8: 10.0.1
Debian Buster: 7.0.1
openSUSE Leap 15.2: 9.0.1
Ubuntu LTS 18.04: 6.0.0
Ubuntu LTS 20.04: 10.0.0
FreeBSD 12: 8.0.1
Fedora 33: 11.0.0
Fedora 34: 11.1.0Ubuntu 18.04 drops off our list after 7.0 comes out
Buster is already off our list as that hit the 2 year mark in AUg 2021.
OpenSUSE Leap 15.2 was EOL'd by SUSE themselves in Jan 2022, We use it as a proxy for SLES, but I think we can required SLES 15 sp3.
FreeBSD 12 is something we still support until April 2023, but arguably we only care about CLang there.
NetBSD 9 wasn't listed, but it was reported to required GCC 7.4 (commit 3830df5f83b9b52d9496763ce1a50afb9231c998) and that is still the latest release of NetBSD.
So NetBSD is our biggest constraint on requiring GCC 10
Regards,
Daniel
Signature · 3 lines
|: https://berrange.com -o- https://www.flickr.com/photos/dberrange :| |: https://libvirt.org -o- https://fstop138.berrange.com :| |: https://entangle-photo.org -o- https://www.instagram.com/dberrange :|
Quoted 10 lines · Daniel P. Berrangé
RHEL-8: 10.0.1 openSUSE Leap 15.3: 9.0.1 Ubuntu LTS 18.04: 6.0.0 FreeBSD 12: 8.0.1Ubuntu 18.04 drops off our list after 7.0 comes out
OpenSUSE Leap 15.2 was EOL'd by SUSE themselves in Jan 2022, We use it as a proxy for SLES, but I think we can required SLES 15 sp3.
(FTR, OpenSUSE Leap 15.3 has GCC 10.3.0).
Quoted 8 lines
FreeBSD 12 is something we still support until April 2023, but arguably we only care about CLang there.
NetBSD 9 wasn't listed, but it was reported to required GCC 7.4 (commit
3830df5f83b9b52d9496763ce1a50afb9231c998) and that is still the latest release of NetBSD.So NetBSD is our biggest constraint on requiring GCC 10
Do we care about the BSDs since they have newer compilers (including gcc10) available in pkgsrc? If you go by the base system, then RHEL8 has 8.5.0 and newer version are only available with packages such as gcc-toolset-10 and gcc-toolset-11.
Paolo
Quoted 21 lines · Paolo Bonzini
Quoted 10 lines · Daniel P. Berrangé
RHEL-8: 10.0.1 openSUSE Leap 15.3: 9.0.1 Ubuntu LTS 18.04: 6.0.0 FreeBSD 12: 8.0.1Ubuntu 18.04 drops off our list after 7.0 comes out
OpenSUSE Leap 15.2 was EOL'd by SUSE themselves in Jan 2022, We use it as a proxy for SLES, but I think we can required SLES 15 sp3.
(FTR, OpenSUSE Leap 15.3 has GCC 10.3.0).
Quoted 7 lines
FreeBSD 12 is something we still support until April 2023, but arguably we only care about CLang there.
NetBSD 9 wasn't listed, but it was reported to required GCC 7.4 (commit
3830df5f83b9b52d9496763ce1a50afb9231c998) and that is still the latest release of NetBSD.
Quoted 6 lines
Quoted 1 line
So NetBSD is our biggest constraint on requiring GCC 10
Do we care about the BSDs since they have newer compilers (including gcc10) available in pkgsrc? If you go by the base system, then RHEL8 has 8.5.0 and newer version are only available with packages such as gcc-toolset-10 and gcc-toolset-11.
I mention NetBSD because we had an explicit request to support 7.4 GCC from there, as it was the current system compiler.
That's a mistake in my original commit logs then wrt RHEL8, as I only ever intended to consider the standard base repos, not random addon repos.
Regards,
Daniel
Signature · 3 lines
|: https://berrange.com -o- https://www.flickr.com/photos/dberrange :| |: https://libvirt.org -o- https://fstop138.berrange.com :| |: https://entangle-photo.org -o- https://www.instagram.com/dberrange :|
Quoted 9 lines · Daniel P. Berrangé
Quoted 6 lines
Quoted 1 line
So NetBSD is our biggest constraint on requiring GCC 10
Do we care about the BSDs since they have newer compilers (including gcc10) available in pkgsrc? If you go by the base system, then RHEL8 has 8.5.0 and newer version are only available with packages such as gcc-toolset-10 and gcc-toolset-11.
I mention NetBSD because we had an explicit request to support 7.4 GCC from there, as it was the current system compiler.
Thanks, do you have a reference? I would like to understand the reason for that (adding Werner too).
For RHEL8, considering that the switch would be several months away anyway, I don't think it would be an issue to require AppStream toolset packages. It's unlikely that RHEL8 would rebase QEMU in 2023 and beyond.
Paolo
Quoted 8 lines · Paolo Bonzini
Quoted 7 lines · Daniel P. Berrangé
Quoted 6 lines
Quoted 1 line
So NetBSD is our biggest constraint on requiring GCC 10
Do we care about the BSDs since they have newer compilers (including gcc10) available in pkgsrc? If you go by the base system, then RHEL8 has 8.5.0 and newer version are only available with packages such as gcc-toolset-10 and gcc-toolset-11.
Quoted 5 lines
Quoted 2 lines
I mention NetBSD because we had an explicit request to support 7.4 GCC from there, as it was the current system compiler.
Thanks, do you have a reference? I would like to understand the reason for that (adding Werner too).
It was commit 3830df5f83b9b52d9496763ce1a50afb9231c998
Quoted 3 lines
For RHEL8, considering that the switch would be several months away anyway, I don't think it would be an issue to require AppStream toolset packages. It's unlikely that RHEL8 would rebase QEMU in 2023 and beyond.
It isn't so much RHEL8 rebases I'm thinking of wrt the policy, but rather our corporate contributors who can be constrained by annoying corporate policies to only use RHEL-8 for their work.
With regards,
Daniel
Signature · 3 lines
|: https://berrange.com -o- https://www.flickr.com/photos/dberrange :| |: https://libvirt.org -o- https://fstop138.berrange.com :| |: https://entangle-photo.org -o- https://www.instagram.com/dberrange :|
Quoted 8 lines · Paolo Bonzini
However, there are no ramifications to actual coroutine code, except for the template syntax "CoroutineFn<return_type>" for the function and the mandatory co_await/co_return keywords... both of which are an improvement, really: the fact that a single function cannot run either inside or outside coroutines is checked by the compiler now, because
qemu_coroutine_createaccepts a function that returns CoroutineFn<void>. Therefore I had to disable some more code in util/ and qapi/ that usedqemu_in_coroutine()orcoroutine_fn.
Bear with me as I suggest something potentially/probably silly given my limited knowledge of C++ coroutines.
Given a function I know about:
void coroutine_fn qio_channel_yield(QIOChannel *ioc,
GIOCondition condition);
IIUC, you previously indicated that the header file declaration, the implementation and any callers of this would need to be in C++ source files.
The caller is what I'm most curious about, because I feel that is where the big ripple effects come into play that cause large parts of QEMU to become C++ code.
In general it is possible to call C++ functions from C.
I presume there is something special about the CoroutineFn<void> prototype preventing that from working as needed, thus requiring the caller to be compiled as C++ ? IIUC compiling as C++ though is not neccessarily the same as using C++ linkage.
So I'm assuming the caller as C++ requirement is not recursive, otherwise it would immediately mean all of QEMU needs to be C++.
This leads me to wonder if we can workaround this problem with a wrapper function. eg in a io/channel.hpp file can be declare something like:
CoroutineFn<void> qio_channel_yield_impl(QIOChannel *ioc,
GIOCondition condition);
extern "C" {
static inline void qio_chanel_yield(QIOChannel *ioc,
GIOCondition condition) {
qio_channel_yield_impl(ioc, condition)
}
}With this qio_channel_yield_impl and qio_channel_yield are both compiled as C++, but qio_channel_yield is exposed with C linkage semantics. Thus enabling callers of qio_channel_yield can carry on being compiled as C, since the invokation of the CoroutineFn is in the inline C++ function ?
This would mean an extra function call, but perhaps this gets optimized away, espeically with LTO, such that it doesn't impact performance negatively ?
The impl of qio_channel_yield_impl() could also call from C++ back to C functions as much as possible.
IOW, can we get it such that the C++ bit is just a thin shim "C -> C++ wrapper -> C++ CoroutineFn -> C", enabling all the C++ bits to be well encapsulated and thus prevent arbitrary usage of C++ features leaking all across the codebase ?
With Regards,
Daniel
Signature · 3 lines
|: https://berrange.com -o- https://www.flickr.com/photos/dberrange :| |: https://libvirt.org -o- https://fstop138.berrange.com :| |: https://entangle-photo.org -o- https://www.instagram.com/dberrange :|
Quoted 12 lines · Daniel P. Berrangé
Bear with me as I suggest something potentially/probably silly given my limited knowledge of C++ coroutines.
Given a function I know about:
void
coroutine_fnqio_channel_yield(QIOChannel *ioc,GIOCondition condition);IIUC, you previously indicated that the header file declaration, the implementation and any callers of this would need to be in C++ source files.
Quoted 7 lines
The caller is what I'm most curious about, because I feel that is where the big ripple effects come into play that cause large parts of QEMU to become C++ code. [...]
I presume there is something special about the CoroutineFn<void> prototype preventing that from working as needed, thus requiring the caller to be compiled as C++ ? IIUC compiling as C++ though is not neccessarily the same as using C++ linkage.
Yes, the CoroutineFn<void> function must either be passed to qemu_coroutine_create() or called as "co_await f()". If you call it as "f()" it does nothing except leak the memory needed by its stack frame, so that only leaves passing the function to qemu_coroutine_create().
I suppose you could do some games with typedefs, like
#ifdef __cplusplus
typedef CoroutineFn<void> VoidCoroutine
#else
typedef struct VoidCoroutine VoidCoroutine;
#endif
to be able to declare a function that returns CoroutineFn<void> but I'm not sure of the advantage.
Quoted 2 lines
So I'm assuming the caller as C++ requirement is not recursive, otherwise it would immediately mean all of QEMU needs to be C++.
Right, qemu_coroutine_create() must be called from C++ but the caller of qemu_coroutine_create() can be extern "C". In the particular case of the block layer, callers of qemu_coroutine_create() include callback-based functions such as bdrv_aio_readv(), and synchronous functions such as bdrv_flush(). Both of these can be called from C.
Quoted 4 lines
IOW, can we get it such that the C++ bit is just a thin shim "C -> C++ wrapper -> C++ CoroutineFn -> C", enabling all the C++ bits to be well encapsulated and thus prevent arbitrary usage of C++ features leaking all across the codebase ?
No, unfortunately not. But in particular, even though the block layer would be C++, device models that use it would not.
Paolo