arm AioContext with its own timer stuff
cover arm AioContext with its own timer stuff
Currently, the timers run on iothread within BQL, so virtio-block dataplane can not use throttle, as Stefan Hajnoczi pointed out in his patches to port dataplane onto block layer.(Thanks, Stefan) To enable this feature, I plan to enable timers to run on AioContext's thread. And maybe in future, hpet can run with its dedicated thread too.
Also, I see Alex Bligh is on the same effort by another method,(it is a good idea)
"[RFC] aio/async: Add timed bottom-halves".
So I think it is better to post my series for discussion, although I have not thought very clearly point, e.g. sigaction
This series ports two parts of timer stuff onto AioContext: alarm timer and timer list. Currently I worked based on Stefanha's git tree
https://github.com/stefanha/qemu.git dataplane-block-layer.
---
Open issue:
The thread safe problem on timer list. To resolve that, I plan to adopt the bh model.
(Not sure about this, maybe Stefan's solution in another thread is better) Although leave most of the race issue unresolved, patch 4 has tried to fix one of them as Jan Kiszka points out that vm_clock can be read outside BQL, thanks Jan :)
Liu Ping Fan (8):
timer: associate alarm_timer with AioContext
timer: pick out timer list info from QemuClock
timer: make timers_state static
timer: protect timers_state with lock
timer: associate timer with AioContext
timer: run timers on aio_poll
block: associate BlockDriverState with AioContext
block: enable throttle with aiocontext
aio-posix.c | 2 + async.c | 12 ++ block.c | 13 ++- cpus.c | 29 ++++- hw/block/dataplane/virtio-blk.c | 1 + include/block/aio.h | 14 +++ include/block/block.h | 1 + include/block/block_int.h | 1 + include/qemu/timer.h | 24 +++- main-loop.c | 6 - qemu-timer.c | 237 +++++++++++++++++++++++++++------------- 11 files changed, 253 insertions(+), 87 deletions(-)
Signature · 1 line
1.8.1.4
RFC 1/8 timer: associate alarm_timer with AioContext
We will arm each AioContext with its own timer stuff. As the first step, we should make each AioContext with its own alarm_timer, so they can raise the deadline independent.
Each thread with AioContext will have dedicated signal handler to trigger it own alarm_timer. And all of the alarm timers are linked on a QSLIST for clock reset.
----- To fix ------------sigaction(SIGALRM, &act, NULL) is process-wide, not thread? I think what I need is an timerfd for each thread, right? Will fix in next version.
--- async.c | 3 ++ include/block/aio.h | 1 + include/qemu/timer.h | 4 +- main-loop.c | 4 -- qemu-timer.c | 106 +++++++++++++++++++++++++++++++++++++-------------- 5 files changed, 85 insertions(+), 33 deletions(-)
async.c +3 -0
@@ -26,6 +26,7 @@#include "block/aio.h" #include "block/thread-pool.h" #include "qemu/main-loop.h" +#include "qemu/timer.h" DEFINE_TLS(AioContext*, thread_aio_context);
@@ -206,6 +207,7 @@ aio_ctx_finalize(GSource *source)aio_set_event_notifier (ctx, &ctx->notifier,NULL ,NULL );event_notifier_cleanup (&ctx->notifier);g_array_free (ctx->pollfds, TRUE); +alarm_timer_destroy (ctx->alarm_timer); }static GSourceFuncs aio_source_funcs = {
@@ -245,6 +247,7 @@ AioContext *aio_context_new(void)aio_set_event_notifier (ctx, &ctx->notifier, (EventNotifierHandler *) event_notifier_test_and_clear,NULL ); + ctx->alarm_timer =alarm_timer_create (ctx);return ctx; }
include/block/aio.h +1 -0
@@ -73,6 +73,7 @@ typedef struct AioContext {/* Thread pool for performing work and receiving completion callbacks */ struct ThreadPool *thread_pool; +struct qemu_alarm_timer *alarm_timer; } AioContext;/* Returns 1 if there are still outstanding AIO requests; 0 otherwise */
include/qemu/timer.h +3 -1
@@ -57,7 +57,9 @@ void qemu_run_timers(QEMUClock *clock);void qemu_run_all_timers (void );void configure_alarms (char const *opt);void init_clocks (void ); -int init_timer_alarm (void ); +int init_timer_alarm (struct qemu_alarm_timer *t); +struct qemu_alarm_timer *alarm_timer_create (AioContext *ctx); +void alarm_timer_destroy (struct qemu_alarm_timer *alarm);int64_t cpu_get_ticks (void );void cpu_enable_ticks (void );
main-loop.c +0 -4
@@ -131,10 +131,6 @@ int qemu_init_main_loop(void)GSource *src;init_clocks (); -if (init_timer_alarm () <0 ) { -fprintf (stderr,"could not initialize alarm timer\n" ); -exit (1 ); - } ret =qemu_signal_init ();if (ret) {
qemu-timer.c +78 -28
195 lines not on this page · raw message
Signature · 1 line
1.8.1.4
RFC 2/8 timer: pick out timer list info from QemuClock
In reply to Liu Ping FanTo allow timer to run on the different backend threads, we will arm the thread's AioContext with its own timer list. So separate these kind of info from QemuClock.
--- qemu-timer.c | 106 ++++++++++++++++++++++++++++++++++++++--------------------- 1 file changed, 68 insertions(+), 38 deletions(-)
qemu-timer.c +68 -38
@@ -30,6 +30,7 @@#include "qemu/timer.h" #include "qemu/thread.h" +#include "qemu/tls.h" #ifdef CONFIG_POSIX #include <pthread.h> #endif
@@ -44,11 +45,16 @@#define QEMU_CLOCK_REALTIME 0#define QEMU_CLOCK_VIRTUAL 1#define QEMU_CLOCK_HOST 2 +#define QEMU_CLOCK_MAXCNT 3 -struct QEMUClock { +typedef struct TimerList {QEMUTimer *active_timers;QemuMutex active_timers_lock; +}TimerList ; + +static TimerList timer_list[QEMU_CLOCK_MAXCNT ]; +struct QEMUClock {NotifierList reset_notifiers;int64_t last;
@@ -58,7 +64,7 @@ struct QEMUClock {struct QEMUTimer {int64_t expire_time;/* in nanoseconds */ -QEMUClock *clock; +TimerList *list;QEMUTimerCB *cb;void *opaque;QEMUTimer *next;
@@ -122,6 +128,13 @@ void alarm_timer_destroy(struct qemu_alarm_timer *t)g_free (t); } +static TimerList *clock_to_timerlist (QEMUClock *clock) +{ +int type = clock->type; + +return &timer_list[type]; +} +static bool qemu_timer_expired_ns (QEMUTimer *timer_head,int64_t current_time) {return timer_head && (timer_head->expire_time <= current_time);
@@ -131,17 +144,19 @@ static int64_t qemu_next_clock_deadline(QEMUClock *clock, int64_t delta) {int64_t expire_time, next;bool has_timer = false; +TimerList *tlist;if (!clock->enabled) {return delta; } -qemu_mutex_lock (&clock->active_timers_lock); -if (clock->active_timers) { + tlist =clock_to_timerlist (clock); +qemu_mutex_lock (&tlist->active_timers_lock); +if (tlist->active_timers) { has_timer = true; - expire_time = clock->active_timers->expire_time; + expire_time = tlist->active_timers->expire_time; } -qemu_mutex_unlock (&clock->active_timers_lock); +qemu_mutex_unlock (&tlist->active_timers_lock);if (!has_timer) {return delta; }
@@ -294,16 +309,25 @@ QEMUClock *rt_clock;QEMUClock *vm_clock;QEMUClock *host_clock; +static void timer_list_init (TimerList *tlist) +{ +qemu_mutex_init (&tlist->active_timers_lock); + tlist->active_timers =NULL ; +} +static QEMUClock *qemu_new_clock (int type) {QEMUClock *clock; +TimerList *tlist; clock =g_malloc0 (sizeof (QEMUClock)); clock->type = type; clock->enabled = true; clock->last =INT64_MIN ;notifier_list_init (&clock->reset_notifiers); -qemu_mutex_init (&clock->active_timers_lock); + tlist =clock_to_timerlist (clock); +timer_list_init (tlist); +return clock; }
@@ -319,10 +343,11 @@ void qemu_clock_enable(QEMUClock *clock, bool enabled)int64_t qemu_clock_has_timers (QEMUClock *clock) {bool has_timers; +TimerList *tlist =clock_to_timerlist (clock); -qemu_mutex_lock (&clock->active_timers_lock); - has_timers = !!clock->active_timers; -qemu_mutex_unlock (&clock->active_timers_lock); +qemu_mutex_lock (&tlist->active_timers_lock); + has_timers = !!tlist->active_timers; +qemu_mutex_unlock (&tlist->active_timers_lock);return has_timers; }
@@ -330,11 +355,12 @@ int64_t qemu_clock_expired(QEMUClock *clock) {bool has_timers;int64_t expire_time; +TimerList *tlist =clock_to_timerlist (clock); -qemu_mutex_lock (&clock->active_timers_lock); - has_timers = clock->active_timers; - expire_time = clock->active_timers->expire_time; -qemu_mutex_unlock (&clock->active_timers_lock); +qemu_mutex_lock (&tlist->active_timers_lock); + has_timers = tlist->active_timers; + expire_time = tlist->active_timers->expire_time; +qemu_mutex_unlock (&tlist->active_timers_lock);return has_timers && expire_time <qemu_get_clock_ns (clock); }
@@ -345,11 +371,12 @@ int64_t qemu_clock_deadline(QEMUClock *clock)int64_t delta =INT32_MAX ;bool has_timers;int64_t expire_time; +TimerList *tlist =clock_to_timerlist (clock); -qemu_mutex_lock (&clock->active_timers_lock); - has_timers = clock->active_timers; - expire_time = clock->active_timers->expire_time; -qemu_mutex_unlock (&clock->active_timers_lock); +qemu_mutex_lock (&tlist->active_timers_lock); + has_timers = tlist->active_timers; + expire_time = tlist->active_timers->expire_time; +qemu_mutex_unlock (&tlist->active_timers_lock);if (has_timers) { delta = expire_time -qemu_get_clock_ns (clock);
@@ -366,7 +393,7 @@ QEMUTimer *qemu_new_timer(QEMUClock *clock, int scale,QEMUTimer *ts; ts =g_malloc0 (sizeof (QEMUTimer)); - ts->clock = clock; + ts->list =clock_to_timerlist (clock); ts->cb = cb; ts->opaque = opaque; ts->scale = scale;
@@ -381,11 +408,11 @@ void qemu_free_timer(QEMUTimer *ts)/* stop a timer, but do not dealloc it */ void qemu_del_timer (QEMUTimer *ts) { -QEMUClock *clock = ts->clock; +TimerList *tlist = ts->list;QEMUTimer **pt, *t; -qemu_mutex_lock (&clock->active_timers_lock); - pt = &ts->clock->active_timers; +qemu_mutex_lock (&tlist->active_timers_lock); + pt = &tlist->active_timers;for (;;) { t = *pt;if (!t)
@@ -396,14 +423,14 @@ void qemu_del_timer(QEMUTimer *ts) } pt = &t->next; } -qemu_mutex_unlock (&clock->active_timers_lock); +qemu_mutex_unlock (&tlist->active_timers_lock); }/* modify the current timer so that it will be fired when current_time >= expire_time. The corresponding callback will be called. */ void qemu_mod_timer_ns (QEMUTimer *ts,int64_t expire_time) { -QEMUClock *clock = ts->clock; +TimerList *tlist = ts->list;QEMUTimer **pt, *t;AioContext *ctx = *tls_get_thread_aio_context ();struct qemu_alarm_timer *alarm_timer = ctx->alarm_timer;
@@ -411,8 +438,8 @@ void qemu_mod_timer_ns(QEMUTimer *ts, int64_t expire_time)qemu_del_timer (ts);/* add the timer in the sorted list */ -qemu_mutex_lock (&clock->active_timers_lock); - pt = &clock->active_timers; +qemu_mutex_lock (&tlist->active_timers_lock); + pt = &tlist->active_timers;for (;;) { t = *pt;if (!qemu_timer_expired_ns (t, expire_time)) {
@@ -423,10 +450,10 @@ void qemu_mod_timer_ns(QEMUTimer *ts, int64_t expire_time) ts->expire_time = expire_time; ts->next = *pt; *pt = ts; -qemu_mutex_unlock (&clock->active_timers_lock); +qemu_mutex_unlock (&tlist->active_timers_lock);/* Rearm if necessary */ -if (pt == &ts->clock->active_timers) { +if (pt == &tlist->active_timers) {qemu_mutex_lock (&alarm_timer->timer_modified_lock); alarm_timer->timer_modified = true;qemu_mutex_unlock (&alarm_timer->timer_modified_lock);
@@ -442,15 +469,15 @@ void qemu_mod_timer(QEMUTimer *ts, int64_t expire_time)bool qemu_timer_pending (QEMUTimer *ts) {QEMUTimer *t; -QEMUClock *clock = ts->clock; +TimerList *tlist = ts->list; -qemu_mutex_lock (&clock->active_timers_lock); -for (t = ts->clock->active_timers; t !=NULL ; t = t->next) { +qemu_mutex_lock (&tlist->active_timers_lock); +for (t = tlist->active_timers; t !=NULL ; t = t->next) {if (t == ts) {break ; } } -qemu_mutex_unlock (&clock->active_timers_lock); +qemu_mutex_unlock (&tlist->active_timers_lock);return t; }
@@ -463,22 +490,25 @@ void qemu_run_timers(QEMUClock *clock) {QEMUTimer *ts;int64_t current_time; - +TimerList *tlist; +if (!clock->enabled)return ; current_time =qemu_get_clock_ns (clock); + tlist =clock_to_timerlist (clock); +for (;;) { -qemu_mutex_lock (&clock->active_timers_lock); - ts = clock->active_timers; +qemu_mutex_lock (&tlist->active_timers_lock); + ts = tlist->active_timers;if (!qemu_timer_expired_ns (ts, current_time)) { -qemu_mutex_unlock (&clock->active_timers_lock); +qemu_mutex_unlock (&tlist->active_timers_lock);break ; }/* remove timer from the list before calling the callback */ - clock->active_timers = ts->next; + tlist->active_timers = ts->next; ts->next =NULL ; -qemu_mutex_unlock (&clock->active_timers_lock); +qemu_mutex_unlock (&tlist->active_timers_lock);/* run the callback (the timer list can be modified) */ ts->cb (ts->opaque);
Signature · 1 line
1.8.1.4
RFC 3/8 timer: make timers_state static
In reply to Liu Ping Fan--- cpus.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
cpus.c +1 -1
@@ -112,7 +112,7 @@ typedef struct TimersState {int64_t dummy; } TimersState; -TimersState timers_state; +static TimersState timers_state;/* Return the virtual CPU time, based on the instruction counter. */ int64_t cpu_get_icount (void )
Signature · 1 line
1.8.1.4
RFC 4/8 timer: protect timers_state with lock
In reply to Liu Ping FanIn kvm mode, vm_clock may be read on AioContexts outside BQL(next patch). This will make timers_state --the foundation of vm_clock exposed to race condition. Using private lock to protect it. Note in tcg mode, vm_clock still read inside BQL, so icount is left without change.
Lock rule: private lock innermost, ie BQL->"this lock"
--- cpus.c | 27 +++++++++++++++++++++++---- 1 file changed, 23 insertions(+), 4 deletions(-)
cpus.c +23 -4
@@ -113,6 +113,8 @@ typedef struct TimersState { } TimersState;static TimersState timers_state; +/* lock rule: innermost */ +static QemuMutex timers_state_lock;/* Return the virtual CPU time, based on the instruction counter. */ int64_t cpu_get_icount (void )
@@ -134,11 +136,15 @@ int64_t cpu_get_icount(void)/* return the host CPU cycle counter and handle stop/restart */ int64_t cpu_get_ticks (void ) { +int64_t ret; +if (use_icount) {return cpu_get_icount (); } +qemu_mutex_lock (&timers_state_lock);if (!timers_state.cpu_ticks_enabled) { -return timers_state.cpu_ticks_offset; + ret = timers_state.cpu_ticks_offset; + goto out; }else {int64_t ticks; ticks =cpu_get_real_ticks ();
@@ -148,41 +154,53 @@ int64_t cpu_get_ticks(void) timers_state.cpu_ticks_offset += timers_state.cpu_ticks_prev - ticks; } timers_state.cpu_ticks_prev = ticks; -return ticks + timers_state.cpu_ticks_offset; + ret = ticks + timers_state.cpu_ticks_offset; + goto out; } +out: +qemu_mutex_lock (&timers_state_lock); +return ret; }/* return the host CPU monotonic timer and handle stop/restart */ int64_t cpu_get_clock (void ) {int64_t ti; + +qemu_mutex_lock (&timers_state_lock);if (!timers_state.cpu_ticks_enabled) { -return timers_state.cpu_clock_offset; + ti = timers_state.cpu_clock_offset; }else { ti =get_clock (); -return ti + timers_state.cpu_clock_offset; + ti += timers_state.cpu_clock_offset; } +qemu_mutex_unlock (&timers_state_lock); +return ti; }/* enable cpu_get_ticks() */ void cpu_enable_ticks (void ) { +qemu_mutex_lock (&timers_state_lock);if (!timers_state.cpu_ticks_enabled) { timers_state.cpu_ticks_offset -=cpu_get_real_ticks (); timers_state.cpu_clock_offset -=get_clock (); timers_state.cpu_ticks_enabled =1 ; } +qemu_mutex_unlock (&timers_state_lock); }/* disable cpu_get_ticks() : the clock is stopped. You must not call cpu_get_ticks() after that. */ void cpu_disable_ticks (void ) { +qemu_mutex_lock (&timers_state_lock);if (timers_state.cpu_ticks_enabled) { timers_state.cpu_ticks_offset =cpu_get_ticks (); timers_state.cpu_clock_offset =cpu_get_clock (); timers_state.cpu_ticks_enabled =0 ; } +qemu_mutex_unlock (&timers_state_lock); }/* Correlation between real and virtual time is always going to be
@@ -353,6 +371,7 @@ static const VMStateDescription vmstate_timers = {void configure_icount (const char *option) { +qemu_mutex_init (&timers_state_lock);vmstate_register (NULL ,0 , &vmstate_timers, &timers_state);if (!option) {return ;
Signature · 1 line
1.8.1.4
RFC 5/8 timer: associate timer with AioContext
In reply to Liu Ping FanCurrently, timers run on iothread inside QBL, this limits the usage of timers in some case, e.g. virtio-blk-dataplane. In order to run timers on private thread, we arm AioContext with three lists in according to three QemuClock (and later, run timers in aio_poll).
------ issue to fix ---
Note: before this patch, there should be another one to fix the race issue by qemu_mod_timer() and _run_timers(). I plan to adopt the BH method for timers to fix it.
--- async.c | 9 +++++++ include/block/aio.h | 13 ++++++++++ include/qemu/timer.h | 20 ++++++++++++++++ qemu-timer.c | 67 +++++++++++++++++++++++++++++----------------------- 4 files changed, 80 insertions(+), 29 deletions(-)
async.c +9 -0
@@ -202,12 +202,16 @@ static void aio_ctx_finalize(GSource *source) {AioContext *ctx = (AioContext *) source; +int i;thread_pool_free (ctx->thread_pool);aio_set_event_notifier (ctx, &ctx->notifier,NULL ,NULL );event_notifier_cleanup (&ctx->notifier);g_array_free (ctx->pollfds, TRUE);alarm_timer_destroy (ctx->alarm_timer); +for (i =0 ; i <QEMU_CLOCK_MAXCNT ; i++) { +timer_list_finalize (&ctx->timer_list[i]); + } }static GSourceFuncs aio_source_funcs = {
@@ -239,6 +243,8 @@ void aio_notify(AioContext *ctx)AioContext *aio_context_new (void ) {AioContext *ctx; +int i; + ctx = (AioContext *)g_source_new (&aio_source_funcs,sizeof (AioContext)); ctx->pollfds =g_array_new (FALSE, FALSE,sizeof (GPollFD)); ctx->thread_pool =NULL ;
@@ -248,6 +254,9 @@ AioContext *aio_context_new(void) (EventNotifierHandler *) event_notifier_test_and_clear, NULL); ctx->alarm_timer =alarm_timer_create (ctx); +for (i =0 ; i <QEMU_CLOCK_MAXCNT ; i++) { +timer_list_init (&ctx->timer_list[i]); + }return ctx; }
include/block/aio.h +13 -0
@@ -43,6 +43,18 @@ typedef struct AioHandler AioHandler;typedef void QEMUBHFunc (void *opaque);typedef void IOHandler (void *opaque); +/* Related timer with AioContext */ +typedef struct QEMUTimer QEMUTimer ; +#define QEMU_CLOCK_MAXCNT 3 + +typedef struct TimerList { +QEMUTimer *active_timers; +QemuMutex active_timers_lock; +}TimerList ; + +void timer_list_init (TimerList *tlist); +void timer_list_finalize (TimerList *tlist); + typedef struct AioContext { GSource source;
@@ -74,6 +86,7 @@ typedef struct AioContext {/* Thread pool for performing work and receiving completion callbacks */ struct ThreadPool *thread_pool;struct qemu_alarm_timer *alarm_timer; +TimerList timer_list[QEMU_CLOCK_MAXCNT ]; } AioContext;/* Returns 1 if there are still outstanding AIO requests; 0 otherwise */
include/qemu/timer.h +20 -0
@@ -33,9 +33,14 @@ extern QEMUClock *vm_clock;extern QEMUClock *host_clock;int64_t qemu_get_clock_ns (QEMUClock *clock); +/* qemu_clock_has_timers, qemu_clock_expired, qemu_clock_deadline +* run In tcg icount mode. There is only one AioContext i.e. qemu_aio_context. +* So we only count the timers on qemu_aio_context. +*/ int64_t qemu_clock_has_timers (QEMUClock *clock);int64_t qemu_clock_expired (QEMUClock *clock);int64_t qemu_clock_deadline (QEMUClock *clock); +void qemu_clock_enable (QEMUClock *clock,bool enabled);void qemu_clock_warp (QEMUClock *clock);
@@ -45,6 +50,9 @@ void qemu_unregister_clock_reset_notifier(QEMUClock *clock,QEMUTimer *qemu_new_timer (QEMUClock *clock,int scale,QEMUTimerCB *cb,void *opaque); +QEMUTimer *aioctx_new_timer (QEMUClock *clock,int scale, +QEMUTimerCB *cb,void *opaque,AioContext *ctx); +void qemu_free_timer (QEMUTimer *ts);void qemu_del_timer (QEMUTimer *ts);void qemu_mod_timer_ns (QEMUTimer *ts,int64_t expire_time);
@@ -77,6 +85,18 @@ static inline QEMUTimer *qemu_new_timer_ms(QEMUClock *clock, QEMUTimerCB *cb,return qemu_new_timer (clock,SCALE_MS , cb, opaque); } +static inline QEMUTimer *aioctx_new_timer_ns (QEMUClock *clock,QEMUTimerCB *cb, +void *opaque,AioContext *ctx) +{ +return aioctx_new_timer (clock,SCALE_NS , cb, opaque, ctx); +} + +static inline QEMUTimer *aioctx_new_timer_ms (QEMUClock *clock,QEMUTimerCB *cb, +void *opaque,AioContext *ctx) +{ +return aioctx_new_timer (clock,SCALE_MS , cb, opaque, ctx); +} +static inline int64_t qemu_get_clock_ms (QEMUClock *clock) {return qemu_get_clock_ns (clock) /SCALE_MS ;
qemu-timer.c +38 -29
181 lines not on this page · raw message
Signature · 1 line
1.8.1.4
RFC 6/8 timer: run timers on aio_poll
In reply to Liu Ping FanStop call timers in main loop and let each mini event-loop run its own timers.
--- aio-posix.c | 2 ++ main-loop.c | 2 -- 2 files changed, 2 insertions(+), 2 deletions(-)
aio-posix.c +2 -0
@@ -191,6 +191,8 @@ bool aio_poll(AioContext *ctx, bool blocking) progress = true; } +qemu_run_all_timers (); +if (progress && !blocking) {return true; }
main-loop.c +0 -2
@@ -467,8 +467,6 @@ int main_loop_wait(int nonblocking)slirp_pollfds_poll (gpollfds, (ret <0 ));#endif -qemu_run_all_timers (); -return ret; }
Signature · 1 line
1.8.1.4
RFC 7/8 block: associate BlockDriverState with AioContext
In reply to Liu Ping FanThe default aio for BDS is qemu_aio_context, while for data plane, it will has its own ctx. Relating BDS with AioContext can help block layer to determine its running environment. Some stuff like timers need such info, so that they can run in the correct environment.
--- block.c | 6 ++++++ hw/block/dataplane/virtio-blk.c | 1 + include/block/block.h | 1 + include/block/block_int.h | 1 + 4 files changed, 9 insertions(+)
block.c +6 -0
@@ -315,6 +315,12 @@ BlockDriverState *bdrv_new(const char *device_name)return bs; } +void bdrv_set_aio_context (BlockDriverState *bs,AioContext *ctx) +{ +assert (ctx); + bs->ctx = ctx; +} +void bdrv_add_close_notifier (BlockDriverState *bs,Notifier *notify) {notifier_list_add (&bs->close_notifiers, notify);
hw/block/dataplane/virtio-blk.c +1 -0
@@ -378,6 +378,7 @@ void virtio_blk_data_plane_start(VirtIOBlockDataPlane *s) } s->ctx =aio_context_new (); +bdrv_set_aio_context (s->bs, s->ctx);/* Set up guest notifier (irq) */ if (k->set_guest_notifiers (qbus->parent,1 , true) !=0 ) {
include/block/block.h +1 -0
@@ -128,6 +128,7 @@ int bdrv_create(BlockDriver *drv, const char* filename, QEMUOptionParameter *options);int bdrv_create_file (const char * filename,QEMUOptionParameter *options);BlockDriverState *bdrv_new (const char *device_name); +void bdrv_set_aio_context (BlockDriverState *bs,AioContext *ctx);void bdrv_make_anon (BlockDriverState *bs);void bdrv_swap (BlockDriverState *bs_new,BlockDriverState *bs_old);void bdrv_append (BlockDriverState *bs_new,BlockDriverState *bs_top);
include/block/block_int.h +1 -0
@@ -299,6 +299,7 @@ struct BlockDriverState { QLIST_HEAD(, BdrvTrackedRequest) tracked_requests; +AioContext *ctx;/* long-running background operation */ BlockJob *job;
Signature · 1 line
1.8.1.4
RFC 8/8 block: enable throttle with aiocontext
In reply to Liu Ping Fan--- block.c | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-)
block.c +6 -1
@@ -149,7 +149,12 @@ static void bdrv_block_timer(void *opaque)void bdrv_io_limits_enable (BlockDriverState *bs) {qemu_co_queue_init (&bs->throttled_reqs); - bs->block_timer =qemu_new_timer_ns (vm_clock, bdrv_block_timer, bs); +if (!bs->ctx) { + bs->block_timer =qemu_new_timer_ns (vm_clock, bdrv_block_timer, bs); + }else { + bs->block_timer =aioctx_new_timer_ns (vm_clock, bdrv_block_timer, bs, + bs->ctx); + } bs->io_limits_enabled = true; }
Signature · 1 line
1.8.1.4
Liu,
Quoted 8 lines · --On 21 July 2013 16:42:57 +0800 Liu Ping Fan <[email protected]> wrote:
Currently, the timers run on iothread within BQL, so virtio-block dataplane can not use throttle, as Stefan Hajnoczi pointed out in his patches to port dataplane onto block layer.(Thanks, Stefan) To enable this feature, I plan to enable timers to run on AioContext's thread. And maybe in future, hpet can run with its dedicated thread too.
Also, I see Alex Bligh is on the same effort by another method,(it is a
good idea) "[RFC] aio/async: Add timed bottom-halves".
Stefan & Paolo did not like that method much, so I did a third method (posted yesterday) suggested by Stefan which adds a clock to AioContext (to which timers can be attached), deletes ALL the alarm_timer stuff (which was very cathartic), uses timeouts on the g_poll, and adds ppoll where this is available. Series at:
http://lists.nongnu.org/archive/html/qemu-devel/2013-07/msg03334.html
I suspect this also overlaps with your code.
So now we have 3 methods to do similar things!
One advantage of my approach is that it removes more code than it adds (by quite a margin). However, alarm timers could have been left in. What's the advantage in giving an AioContext its own alarm timer as opposed to just its own clock?
Signature · 1 line
Alex Bligh
Re: [Qemu-devel] [RFC 6/8] timer: run timers on aio_poll
In reply to Liu Ping Fanaio-posix.c bool aio_poll(AioContext *ctx, bool blocking) · line 198
Quoted 13 lines · --On 21 July 2013 16:43:03 +0800 Liu Ping Fan <[email protected]> wrote:
aio-posix.c +2
-0@@ -191,6 +191,8 @@ bool aio_poll(AioContext *ctx, bool blocking) progress = true; } +qemu_run_all_timers (); +if (progress && !blocking) {return true; }
I am told (by Stefan H) this approach is unsafe as existing timers may not expect to be run within aio_poll.
Also, I suspect you need to change the value of progress if timers run so bdrv draining terminates properly.
Signature · 1 line
Alex Bligh
Quoted 22 lines · Alex Bligh
Liu,
Quoted 8 lines · --On 21 July 2013 16:42:57 +0800 Liu Ping Fan <[email protected]> wrote:
Currently, the timers run on iothread within BQL, so virtio-block dataplane can not use throttle, as Stefan Hajnoczi pointed out in his patches to port dataplane onto block layer.(Thanks, Stefan) To enable this feature, I plan to enable timers to run on AioContext's thread. And maybe in future, hpet can run with its dedicated thread too.
Also, I see Alex Bligh is on the same effort by another method,(it is a
good idea) "[RFC] aio/async: Add timed bottom-halves".Stefan & Paolo did not like that method much, so I did a third method (posted yesterday) suggested by Stefan which adds a clock to AioContext (to which timers can be attached), deletes ALL the
alarm_timerstuff (which was very cathartic), uses timeouts on theg_poll, and adds ppoll where this is available. Series at:
http://lists.nongnu.org/archive/html/qemu-devel/2013-07/msg03334.html
Quoted 9 lines
I suspect this also overlaps with your code.
So now we have 3 methods to do similar things!
One advantage of my approach is that it removes more code than it adds (by quite a margin). However, alarm timers could have been left in. What's the advantage in giving an AioContext its own alarm timer as opposed to just its own clock?
I read your second series, and try to summary the main different between us. Please correct me, if I misunderstood something.
--1st. You try to create a separate QemuClock for AioContext.
I think QemuClock is the clock event source and we have three
classic with fine definition. They should be qemu-wide for time measurement. On the other handler, timer is a concept for timeout, so it can be AioContext-related. So I have patch2&5.
--2nd. You want to substitute alarm_timer with timeout of poll.
I try to trigger each specified thread when its deadline comes.
But unfortunately, the signal can not be delivered to the specified thread directly, and I need timerfd for each AioContext. (If we can have the equivalent on other platform)
Anything else?
Thanks and regards,
Pingfan
Quoted 2 lines
--
Alex Bligh
Quoted 32 lines · liu ping fan
Quoted 22 lines · Alex Bligh
Liu,
Quoted 8 lines · --On 21 July 2013 16:42:57 +0800 Liu Ping Fan <[email protected]> wrote:
Currently, the timers run on iothread within BQL, so virtio-block dataplane can not use throttle, as Stefan Hajnoczi pointed out in his patches to port dataplane onto block layer.(Thanks, Stefan) To enable this feature, I plan to enable timers to run on AioContext's thread. And maybe in future, hpet can run with its dedicated thread too.
Also, I see Alex Bligh is on the same effort by another method,(it is a
good idea) "[RFC] aio/async: Add timed bottom-halves".Stefan & Paolo did not like that method much, so I did a third method (posted yesterday) suggested by Stefan which adds a clock to AioContext (to which timers can be attached), deletes ALL the
alarm_timerstuff (which was very cathartic), uses timeouts on theg_poll, and adds ppoll where this is available. Series at:
http://lists.nongnu.org/archive/html/qemu-devel/2013-07/msg03334.htmlQuoted 9 lines
I suspect this also overlaps with your code.
So now we have 3 methods to do similar things!
One advantage of my approach is that it removes more code than it adds (by quite a margin). However, alarm timers could have been left in. What's the advantage in giving an AioContext its own alarm timer as opposed to just its own clock?
Quoted 6 lines
I read your second series, and try to summary the main different between us. Please correct me, if I misunderstood something.
--1st. You try to create a separate QemuClock for AioContext.
I think QemuClock is the clock event source and we have three
classic with fine definition. They should be qemu-wide for time measurement. On the other handler, timer is a concept for timeout,
Timers, as used in QEMU, are not only for "unimportant" and unlikely-to-fire timeouts. They are also for potential high-rate, high resolution events. Your last series neglects this. We may need two versions of timers - or one interface that caters both use cases properly.
Jan
Quoted 14 lines
so it can be AioContext-related. So I have patch2&5. --2nd. You want to substitute
alarm_timerwith timeout of poll.
I try to trigger each specified thread when its deadline comes.
But unfortunately, the signal can not be delivered to the specified thread directly, and I need timerfd for each AioContext. (If we can have the equivalent on other platform)Anything else?
Thanks and regards,
PingfanQuoted 2 lines
--
Alex Bligh
Signature · 2 lines
Siemens AG, Corporate Technology, CT RTC ITP SES-DE Corporate Competence Center Embedded Linux
Re: [Qemu-devel] [RFC 3/8] timer: make timers_state static
In reply to Liu Ping FanQuoted 11 lines · Liu Ping Fan
Signed-off-by Liu Ping Fan <[email protected]>--- cpus.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)cpus.c +0
-0
cpus.c typedef struct TimersState { · line 118
Quoted 8 lines
cpus.c +1
-1@@ -112,7 +112,7 @@ typedef struct TimersState {int64_t dummy; } TimersState; -TimersState timers_state; +static TimersState timers_state;/* Return the virtual CPU time, based on the instruction counter. */ int64_t cpu_get_icount (void )
Could go in directly via trivial IMHO.
Signature · 2 lines
Siemens AG, Corporate Technology, CT RTC ITP SES-DE Corporate Competence Center Embedded Linux
Re: [Qemu-devel] [RFC 4/8] timer: protect timers_state with lock
In reply to Liu Ping FanQuoted 27 lines · Liu Ping Fan
In kvm mode,
vm_clockmay be read on AioContexts outside BQL(next patch). This will maketimers_state--the foundation ofvm_clockexposed to race condition. Using private lock to protect it. Note in tcg mode,vm_clockstill read inside BQL, so icount is left without change.Lock rule: private lock innermost, ie BQL->"this lock"
Signed-off-by Liu Ping Fan <[email protected]>--- cpus.c | 27 +++++++++++++++++++++++---- 1 file changed, 23 insertions(+), 4 deletions(-)cpus.c +2
-0@@ -113,6 +113,8 @@ typedef struct TimersState { } TimersState;static TimersState timers_state; +/* lock rule: innermost */ +static QemuMutex timers_state_lock;/* Return the virtual CPU time, based on the instruction counter. */ int64_t cpu_get_icount (void )
cpus.c int64_t cpu_get_icount(void) · line 141
Quoted 6 lines
cpus.c +2
-0@@ -134,11 +136,15 @@ int64_t cpu_get_icount(void)/* return the host CPU cycle counter and handle stop/restart */ int64_t cpu_get_ticks (void ) { +int64_t ret; +if (use_icount) {
[ Some day we should introduce something like assert_bql_held() and add it here, among other places. ]
cpus.c int64_t cpu_get_icount(void) · line 147
Quoted 7 lines
cpus.c +3
-1return cpu_get_icount (); } +qemu_mutex_lock (&timers_state_lock);if (!timers_state.cpu_ticks_enabled) { -return timers_state.cpu_ticks_offset; + ret = timers_state.cpu_ticks_offset; + goto out;
No need for goto here and below.
Quoted 61 lines
cpus.c +15
-3}else {int64_t ticks; ticks =cpu_get_real_ticks ();@@ -148,41 +154,53 @@ int64_t cpu_get_ticks(void) timers_state.cpu_ticks_offset += timers_state.cpu_ticks_prev - ticks; } timers_state.cpu_ticks_prev = ticks; -return ticks + timers_state.cpu_ticks_offset; + ret = ticks + timers_state.cpu_ticks_offset; + goto out; } +out: +qemu_mutex_lock (&timers_state_lock); +return ret; }/* return the host CPU monotonic timer and handle stop/restart */ int64_t cpu_get_clock (void ) {int64_t ti; + +qemu_mutex_lock (&timers_state_lock);if (!timers_state.cpu_ticks_enabled) { -return timers_state.cpu_clock_offset; + ti = timers_state.cpu_clock_offset; }else { ti =get_clock (); -return ti + timers_state.cpu_clock_offset; + ti += timers_state.cpu_clock_offset; } +qemu_mutex_unlock (&timers_state_lock); +return ti; }/* enable cpu_get_ticks() */ void cpu_enable_ticks (void ) { +qemu_mutex_lock (&timers_state_lock);if (!timers_state.cpu_ticks_enabled) { timers_state.cpu_ticks_offset -=cpu_get_real_ticks (); timers_state.cpu_clock_offset -=get_clock (); timers_state.cpu_ticks_enabled =1 ; } +qemu_mutex_unlock (&timers_state_lock); }/* disable cpu_get_ticks() : the clock is stopped. You must not call cpu_get_ticks() after that. */ void cpu_disable_ticks (void ) { +qemu_mutex_lock (&timers_state_lock);if (timers_state.cpu_ticks_enabled) { timers_state.cpu_ticks_offset =cpu_get_ticks (); timers_state.cpu_clock_offset =cpu_get_clock (); timers_state.cpu_ticks_enabled =0 ; } +qemu_mutex_unlock (&timers_state_lock); }/* Correlation between real and virtual time is always going to be
cpus.c static const VMStateDescription vmstate_timers = { · line 372
Quoted 2 lines
cpus.c +0
-0@@ -353,6 +371,7 @@ static const VMStateDescription vmstate_timers = {void configure_icount (const char *option)
[ Misnamed function, it's not only about icount stuff - but not an issue of this patch. ]
cpus.c static const VMStateDescription vmstate_timers = { · line 377
Quoted 6 lines
cpus.c +1
-0{ +qemu_mutex_init (&timers_state_lock);vmstate_register (NULL ,0 , &vmstate_timers, &timers_state);if (!option) {return ;
Looks good except for the goto.
Jan
Signature · 2 lines
Siemens AG, Corporate Technology, CT RTC ITP SES-DE Corporate Competence Center Embedded Linux
Re: [Qemu-devel] [RFC 1/8] timer: associate alarm_timer with AioContext
In reply to Liu Ping FanQuoted 291 lines · Liu Ping Fan
We will arm each AioContext with its own timer stuff. As the first step, we should make each AioContext with its own
alarm_timer, so they can raise the deadline independent.
Each thread with AioContext will have dedicated signal handler to trigger it ownalarm_timer. And all of the alarm timers are linked on a QSLIST for clock reset.Signed-off-by Liu Ping Fan <[email protected]>----- To fix ------------
sigaction(SIGALRM, &act, NULL)is process-wide, not thread? I think what I need is an timerfd for each thread, right? Will fix in next version.--- async.c | 3 ++ include/block/aio.h | 1 + include/qemu/timer.h | 4 +- main-loop.c | 4 -- qemu-timer.c | 106 +++++++++++++++++++++++++++++++++++++-------------- 5 files changed, 85 insertions(+), 33 deletions(-)async.c +3
-0@@ -26,6 +26,7 @@#include "block/aio.h" #include "block/thread-pool.h" #include "qemu/main-loop.h" +#include "qemu/timer.h" DEFINE_TLS(AioContext*, thread_aio_context);@@ -206,6 +207,7 @@ aio_ctx_finalize(GSource *source)aio_set_event_notifier (ctx, &ctx->notifier,NULL ,NULL );event_notifier_cleanup (&ctx->notifier);g_array_free (ctx->pollfds, TRUE); +alarm_timer_destroy (ctx->alarm_timer); }static GSourceFuncs aio_source_funcs = {@@ -245,6 +247,7 @@ AioContext *aio_context_new(void)aio_set_event_notifier (ctx, &ctx->notifier, (EventNotifierHandler *) event_notifier_test_and_clear,NULL ); + ctx->alarm_timer =alarm_timer_create (ctx);return ctx; }include/block/aio.h +1
-0@@ -73,6 +73,7 @@ typedef struct AioContext {/* Thread pool for performing work and receiving completion callbacks */ struct ThreadPool *thread_pool; +struct qemu_alarm_timer *alarm_timer; } AioContext;/* Returns 1 if there are still outstanding AIO requests; 0 otherwise */ include/qemu/timer.h +3
-1@@ -57,7 +57,9 @@ void qemu_run_timers(QEMUClock *clock);void qemu_run_all_timers (void );void configure_alarms (char const *opt);void init_clocks (void ); -int init_timer_alarm (void ); +int init_timer_alarm (struct qemu_alarm_timer *t); +struct qemu_alarm_timer *alarm_timer_create (AioContext *ctx); +void alarm_timer_destroy (struct qemu_alarm_timer *alarm);int64_t cpu_get_ticks (void );void cpu_enable_ticks (void );main-loop.c +0
-4@@ -131,10 +131,6 @@ int qemu_init_main_loop(void)GSource *src;init_clocks (); -if (init_timer_alarm () <0 ) { -fprintf (stderr,"could not initialize alarm timer\n" ); -exit (1 ); - } ret =qemu_signal_init ();if (ret) {qemu-timer.c +75
-27@@ -65,6 +65,8 @@ struct QEMUTimer {int scale; }; +typedef struct qemu_alarm_timer qemu_alarm_timer ; +struct qemu_alarm_timer {char const *name;int (*start)(struct qemu_alarm_timer *t);@@ -82,9 +84,43 @@ struct qemu_alarm_timer {/* Was the nearest deadline timer modified (possibly by another thread)? */ QemuMutex timer_modified_lock;bool timer_modified; +AioContext *ctx; +/* protected by alarm_timer_list_lock */ +QSLIST_ENTRY (qemu_alarm_timer) next_alarm_timer; }; -static struct qemu_alarm_timer *alarm_timer; +static QSLIST_HEAD(, qemu_alarm_timer) \ + alarm_timer_list =QSLIST_HEAD_INITIALIZER (alarm_timer_list); +/* innermost lock */ +static QemuMutex alarm_timer_list_lock; + +struct qemu_alarm_timer *alarm_timer_create (AioContext *ctx) +{ +struct qemu_alarm_timer *t; + + t =g_malloc0 (sizeof (qemu_alarm_timer)); +init_timer_alarm (t); + t->ctx = ctx; +qemu_mutex_lock (&alarm_timer_list_lock); +QSLIST_INSERT_HEAD (&alarm_timer_list, t, next_alarm_timer); +qemu_mutex_unlock (&alarm_timer_list_lock); +return t; +} + +void alarm_timer_destroy (struct qemu_alarm_timer *t) +{ +struct qemu_alarm_timer *var, *tvar; + + t->stop (t); +qemu_mutex_lock (&alarm_timer_list_lock); + QSLIST_FOREACH_SAFE(var, &alarm_timer_list, next_alarm_timer, tvar) { +if (tvar == t) { +QSLIST_REMOVE_AFTER (var, next_alarm_timer); + } + } +qemu_mutex_unlock (&alarm_timer_list_lock); +g_free (t); +}static bool qemu_timer_expired_ns (QEMUTimer *timer_head,int64_t current_time) {@@ -114,7 +150,10 @@ static int64_t qemu_next_clock_deadline(QEMUClock *clock, int64_t delta)return MIN (next, delta); } -static int64_t qemu_next_alarm_deadline (void ) +/* Soon this will be fixed: till now, timer list is not associated with +* AioContext, so @ctx has no effect on deadline currently. +*/ +static int64_t qemu_next_alarm_deadline (AioContext *ctx) {int64_t delta =INT64_MAX ;@@ -127,12 +166,23 @@ static int64_t qemu_next_alarm_deadline(void)static void qemu_rearm_alarm_timer (struct qemu_alarm_timer *t) { -int64_t nearest_delta_ns =qemu_next_alarm_deadline (); +int64_t nearest_delta_ns =qemu_next_alarm_deadline (t->ctx);if (nearest_delta_ns <INT64_MAX ) { t->rearm (t, nearest_delta_ns); } } +static void qemu_rearm_alarm_timers (void ) +{ +struct qemu_alarm_timer *t; + +qemu_mutex_lock (&alarm_timer_list_lock); + QSLIST_FOREACH(t, &alarm_timer_list, next_alarm_timer) { +qemu_rearm_alarm_timer (t); + } +qemu_mutex_unlock (&alarm_timer_list_lock); +} +/* TODO: MIN_TIMER_REARM_NS should be optimized */ #define MIN_TIMER_REARM_NS 250000@@ -262,7 +312,7 @@ void qemu_clock_enable(QEMUClock *clock, bool enabled)bool old = clock->enabled; clock->enabled = enabled;if (enabled && !old) { -qemu_rearm_alarm_timer (alarm_timer); +qemu_rearm_alarm_timers (); } }@@ -355,6 +405,8 @@ void qemu_mod_timer_ns(QEMUTimer *ts, int64_t expire_time) {QEMUClock *clock = ts->clock;QEMUTimer **pt, *t; +AioContext *ctx = *tls_get_thread_aio_context (); +struct qemu_alarm_timer *alarm_timer = ctx->alarm_timer;qemu_del_timer (ts);@@ -485,6 +537,8 @@ uint64_t qemu_timer_expire_time_ns(QEMUTimer *ts)void qemu_run_all_timers (void ) {bool timer_modified; +AioContext *ctx = *tls_get_thread_aio_context (); +struct qemu_alarm_timer *alarm_timer = ctx->alarm_timer; alarm_timer->pending = false;@@ -515,13 +569,15 @@ static void CALLBACK host_alarm_handler(PVOID lpParam, BOOLEAN unused) static void host_alarm_handler(int host_signum)#endif { -struct qemu_alarm_timer *t = alarm_timer; +AioContext *ctx = *tls_get_thread_aio_context (); +struct qemu_alarm_timer *t = ctx->alarm_timer; +if (!t)return ; t->expired = true; t->pending = true; -qemu_notify_event (); +aio_notify (ctx); }#if defined(__linux__)@@ -774,37 +830,30 @@ static void win32_rearm_timer(struct qemu_alarm_timer *t,#endif /* _WIN32 */ -static void quit_timers (void ) -{ -struct qemu_alarm_timer *t = alarm_timer; - alarm_timer =NULL ; - t->stop (t); -} -#ifdef CONFIG_POSIX static void reinit_timers (void ) { -struct qemu_alarm_timer *t = alarm_timer; - t->stop (t); -if (t->start (t)) { -fprintf (stderr,"Internal timer error: aborting\n" ); -exit (1 ); +struct qemu_alarm_timer *t; + +qemu_mutex_lock (&alarm_timer_list_lock); + QSLIST_FOREACH(t, &alarm_timer_list, next_alarm_timer) { + t->stop (t); +if (t->start (t)) { +fprintf (stderr,"Internal timer error: aborting\n" ); +exit (1 ); + } +qemu_rearm_alarm_timer (t); } -qemu_rearm_alarm_timer (t); +qemu_mutex_unlock (&alarm_timer_list_lock); }#endif /* CONFIG_POSIX */ -int init_timer_alarm (void ) +int init_timer_alarm (struct qemu_alarm_timer *t) { -struct qemu_alarm_timer *t =NULL ;int i, err =-1 ; -if (alarm_timer) { -return 0 ; - } -for (i =0 ; alarm_timers[i].name; i++) { - t = &alarm_timers[i]; + *t = alarm_timers[i]; err = t->start (t);if (!err)@@ -818,14 +867,15 @@ int init_timer_alarm(void)qemu_mutex_init (&t->timer_modified_lock); -atexit (quit_timers);#ifdef CONFIG_POSIX pthread_atfork (NULL ,NULL , reinit_timers);#endif
qemu-timer.c int init_timer_alarm(void) · line 881
Quoted 10 lines
qemu-timer.c +3
-1- alarm_timer = t;return 0 ; fail: +fprintf (stderr,"could not initialize alarm timer\n" ); +exit (1 ); +return err; }
This goes into a similar direction like what I have here locally. However, as said in a different thread, you must not remove the main alarm timers from the main loop, rather just add the option for additional alarm timers so that AIO etc. could define their private ones.
Jan
Signature · 2 lines
Siemens AG, Corporate Technology, CT RTC ITP SES-DE Corporate Competence Center Embedded Linux
Liu,
Quoted 7 lines · --On 22 July 2013 12:38:02 +0800 liu ping fan <[email protected]> wrote:
I read your second series, and try to summary the main different between us. Please correct me, if I misunderstood something.
--1st. You try to create a separate QemuClock for AioContext.
I think QemuClock is the clock event source and we have three
classic with fine definition. They should be qemu-wide for time measurement. On the other handler, timer is a concept for timeout, so it can be AioContext-related. So I have patch2&5.
Yes and no. QEMUClock is not a clock source. QEMUClock /has/ a clock source, and more than one QEMUClock can share the same clock source.
QEMUClock does have its own list of timers, and so if you only want to run a subset of timers in aio_poll (which is I believe the requirement so not to change the behaviour of existing timers), you need a separate QEMUClock.
The approach I took (StefanH's idea) was to put a QEMUClock into each AioContext. Arguably you might want to put a set of 3 QEMUClock's into each AioContext, one for each clock source.
QEMUClock itself is very lightweight as far as I can tell.
Quoted 1 line
--2nd. You want to substitute
alarm_timerwith timeout of poll.
Correct.
Quoted 4 lines
I try to trigger each specified thread when its deadline comes.
But unfortunately, the signal can not be delivered to the specified thread directly, and I need timerfd for each AioContext. (If we can have the equivalent on other platform)
Firstly, I can't see the advantage of keeping the alarm_timer stuff around at all if we can delete it. Save, of course, that on systems that don't have ppoll or equivalent you lose sub-millisecond timing by deleting them.
Secondly, I don't understand this point. Let's assume we keep alarm_timers. The timers run from signals at the moment (under POSIX) and could easily run in their own thread. They synchronise with the thread waiting on poll by using the event notifiers. Why do you need to have multiple threads dealing with timers? At most one new thread (and quite possibly zero) should be sufficient, as all they need to do is write() to the correct notifier FD, which will end the relevant poll. Of course if we delete alarm_timers this is all irrelevant.
Signature · 1 line
Alex Bligh
Quoted 14 lines · Alex Bligh
Liu,
Quoted 7 lines · --On 22 July 2013 12:38:02 +0800 liu ping fan <[email protected]> wrote:
I read your second series, and try to summary the main different between us. Please correct me, if I misunderstood something.
--1st. You try to create a separate QemuClock for AioContext.
I think QemuClock is the clock event source and we have three
classic with fine definition. They should be qemu-wide for time measurement. On the other handler, timer is a concept for timeout, so it can be AioContext-related. So I have patch2&5.
Quoted 3 lines
Yes and no. QEMUClock is not a clock source. QEMUClock /has/ a clock source, and more than one QEMUClock can share the same clock source.
Supposed the case sync the time across iothread/vcpu/dataplane, the vm_clock is a unique interface for all of them.
Quoted 11 lines
QEMUClock does have its own list of timers, and so if you only want to run a subset of timers in
aio_poll(which is I believe the requirement so not to change the behaviour of existing timers), you need a separate QEMUClock.The approach I took (StefanH's idea) was to put a QEMUClock into each AioContext. Arguably you might want to put a set of 3 QEMUClock's into each AioContext, one for each clock source.
QEMUClock itself is very lightweight as far as I can tell.
I think the weight is not the point here, rather, the concept.
Quoted 13 lines
Quoted 2 lines
--2nd. You want to substitute
alarm_timerwith timeout of poll.Correct.
Quoted 4 lines
I try to trigger each specified thread when its deadline comes.
But unfortunately, the signal can not be delivered to the specified thread directly, and I need timerfd for each AioContext. (If we can have the equivalent on other platform)
Quoted 4 lines
Firstly, I can't see the advantage of keeping the
alarm_timerstuff around at all if we can delete it. Save, of course, that on systems that don't have ppoll or equivalent you lose sub-millisecond timing by deleting them.
I do not think it very clearly. But the final aim is to associate an eventfd with deadline, and attach the eventfd to g_poll. For linux, it can be easily achieved by timerfd, for other posix, what I can thought till now is to distinguish the source of timer in signal handler, and revoke a eventfd (stands for deadline) on AioContext. I am not sure whether this is better than timeout of poll.
Quoted 7 lines
Secondly, I don't understand this point. Let's assume we keep
alarm_timers. The timers run from signals at the moment (under POSIX) and could easily run in their own thread. They synchronise with the thread waiting on poll by using the event notifiers. Why do you need to have multiple threads dealing with timers? At most one new thread (and quite possibly zero) should be sufficient, as all they need to do iswrite()to the correct notifier FD, which will end the relevant poll. Of course if we delete
Different AioContext may have different deadline, so I separate the alarm_timers
Regards,
Pingfan
Quoted 4 lines
alarm_timersthis is all irrelevant.--
Alex Bligh
Michael Tokarev
AppliedRe: [Qemu-devel] [Qemu-trivial] [RFC 3/8] timer: make timers_state static
In reply to Jan KiszkaQuoted 5 lines · Jan Kiszka
cpus.c typedef struct TimersState { · line 115
Quoted 2 lines · Liu Ping Fan
cpus.c +1
-1-TimersState timers_state; +static TimersState timers_state;Could go in directly via trivial IMHO.
It should not conflict with anything else.
Thanks, applied to the trivial patches queue.
/mjt
Quoted 40 lines · Jan Kiszka
Quoted 32 lines · liu ping fan
Quoted 22 lines · Alex Bligh
Liu,
Quoted 8 lines · --On 21 July 2013 16:42:57 +0800 Liu Ping Fan <[email protected]> wrote:
Currently, the timers run on iothread within BQL, so virtio-block dataplane can not use throttle, as Stefan Hajnoczi pointed out in his patches to port dataplane onto block layer.(Thanks, Stefan) To enable this feature, I plan to enable timers to run on AioContext's thread. And maybe in future, hpet can run with its dedicated thread too.
Also, I see Alex Bligh is on the same effort by another method,(it is a
good idea) "[RFC] aio/async: Add timed bottom-halves".Stefan & Paolo did not like that method much, so I did a third method (posted yesterday) suggested by Stefan which adds a clock to AioContext (to which timers can be attached), deletes ALL the
alarm_timerstuff (which was very cathartic), uses timeouts on theg_poll, and adds ppoll where this is available. Series at:
http://lists.nongnu.org/archive/html/qemu-devel/2013-07/msg03334.htmlQuoted 9 lines
I suspect this also overlaps with your code.
So now we have 3 methods to do similar things!
One advantage of my approach is that it removes more code than it adds (by quite a margin). However, alarm timers could have been left in. What's the advantage in giving an AioContext its own alarm timer as opposed to just its own clock?
Quoted 6 lines
I read your second series, and try to summary the main different between us. Please correct me, if I misunderstood something.
--1st. You try to create a separate QemuClock for AioContext.
I think QemuClock is the clock event source and we have three
classic with fine definition. They should be qemu-wide for time measurement. On the other handler, timer is a concept for timeout,
Quoted 2 lines
Timers, as used in QEMU, are not only for "unimportant" and unlikely-to-fire timeouts. They are also for potential high-rate, high
Sorry, but can not catch the point. QemuTimer is used to trigger something, so the process can be delayed for some reason. For high resolution events, do we go directly to QEMUClock, which fall back on host's hpet? And the use case is just for get the time stamp, NOT for trigger?
Thx®ards,
Pingfan
Quoted 23 lines
resolution events. Your last series neglects this. We may need two versions of timers - or one interface that caters both use cases properly.
Jan
Quoted 7 lines
so it can be AioContext-related. So I have patch2&5. --2nd. You want to substitute
alarm_timerwith timeout of poll.
I try to trigger each specified thread when its deadline comes.
But unfortunately, the signal can not be delivered to the specified thread directly, and I need timerfd for each AioContext. (If we can have the equivalent on other platform)Quoted 7 lines
Anything else?
Thanks and regards,
PingfanQuoted 2 lines
--
Alex Bligh--
Siemens AG, Corporate Technology, CT RTC ITP SES-DE Corporate Competence Center Embedded Linux
Quoted 26 lines · liu ping fan
Quoted 14 lines · Alex Bligh
Liu,
Quoted 7 lines · --On 22 July 2013 12:38:02 +0800 liu ping fan <[email protected]> wrote:
I read your second series, and try to summary the main different between us. Please correct me, if I misunderstood something.
--1st. You try to create a separate QemuClock for AioContext.
I think QemuClock is the clock event source and we have three
classic with fine definition. They should be qemu-wide for time measurement. On the other handler, timer is a concept for timeout, so it can be AioContext-related. So I have patch2&5.Quoted 3 lines
Yes and no. QEMUClock is not a clock source. QEMUClock /has/ a clock source, and more than one QEMUClock can share the same clock source.
Supposed the case sync the time across iothread/vcpu/dataplane, the
vm_clockis a unique interface for all of them.Quoted 5 lines
QEMUClock does have its own list of timers, and so if you only want to run a subset of timers in
aio_poll(which is I believe the requirement so not to change the behaviour of existing timers), you need a separate QEMUClock.
Quoted 24 lines
Quoted 6 lines
The approach I took (StefanH's idea) was to put a QEMUClock into each AioContext. Arguably you might want to put a set of 3 QEMUClock's into each AioContext, one for each clock source.
QEMUClock itself is very lightweight as far as I can tell.
I think the weight is not the point here, rather, the concept.
Quoted 17 lines
Quoted 2 lines
--2nd. You want to substitute
alarm_timerwith timeout of poll.Correct.
Quoted 4 lines
I try to trigger each specified thread when its deadline comes.
But unfortunately, the signal can not be delivered to the specified thread directly, and I need timerfd for each AioContext. (If we can have the equivalent on other platform)Firstly, I can't see the advantage of keeping the
alarm_timerstuff around at all if we can delete it. Save, of course, that on systems that don't have ppoll or equivalent you lose sub-millisecond timing by deleting them.
The scenior I can figure out is if adopting timeout of poll, then when changing the deadline, we need to invoke poll, and set the new timeout, right?
Regards,
Pingfan
Quoted 23 lines
I do not think it very clearly. But the final aim is to associate an eventfd with deadline, and attach the eventfd to
g_poll. For linux, it can be easily achieved by timerfd, for other posix, what I can thought till now is to distinguish the source of timer in signal handler, and revoke a eventfd (stands for deadline) on AioContext. I am not sure whether this is better than timeout of poll.Quoted 7 lines
Secondly, I don't understand this point. Let's assume we keep
alarm_timers. The timers run from signals at the moment (under POSIX) and could easily run in their own thread. They synchronise with the thread waiting on poll by using the event notifiers. Why do you need to have multiple threads dealing with timers? At most one new thread (and quite possibly zero) should be sufficient, as all they need to do iswrite()to the correct notifier FD, which will end the relevant poll. Of course if we deleteDifferent AioContext may have different deadline, so I separate the
alarm_timersRegards,
PingfanQuoted 4 lines
alarm_timersthis is all irrelevant.--
Alex Bligh
Re: [Qemu-devel] [RFC 6/8] timer: run timers on aio_poll
In reply to Alex BlighQuoted 12 lines · Alex Bligh
Quoted 8 lines · --On 21 July 2013 16:43:03 +0800 Liu Ping Fan <[email protected]> wrote:
aio-posix.c +0
-0@@ -191,6 +191,8 @@ bool aio_poll(AioContext *ctx, bool blocking) progress = true; }
Quoted 10 lines
aio-posix.c bool aio_poll(AioContext *ctx, bool blocking) · line 198
Quoted 5 lines
aio-posix.c +2
-0+qemu_run_all_timers (); +if (progress && !blocking) {return true; }I am told (by Stefan H) this approach is unsafe as existing timers may not expect to be run within
aio_poll.
Have not figure out the reason, could you elaborate ?
Thanks
Quoted 3 lines
Also, I suspect you need to change the value of progress if timers run so bdrv draining terminates properly.
Yes, you are right
Quoted 2 lines
--
Alex Bligh
Il 23/07/2013 04:53, liu ping fan ha scritto:
Quoted 3 lines
The scenior I can figure out is if adopting timeout of poll, then when changing the deadline, we need to invoke poll, and set the new timeout, right?
Yes, you need to call aio_notify so that poll is reinvoked.
Paolo
Quoted 8 lines · --On 23 July 2013 10:53:26 +0800 liu ping fan <[email protected]> wrote:
Quoted 5 lines
Quoted 5 lines
Firstly, I can't see the advantage of keeping the
alarm_timerstuff around at all if we can delete it. Save, of course, that on systems that don't have ppoll or equivalent you lose sub-millisecond timing by deleting them.The scenior I can figure out is if adopting timeout of poll, then when changing the deadline, we need to invoke poll, and set the new timeout, right?
Are you asking how I deal with modification of the timeout when the poll is already running? If so, I use the event notifier to end the current poll. It will then (presumably) repoll with a recalculated timeout.
Signature · 1 line
Alex Bligh
Re: [Qemu-devel] [RFC 6/8] timer: run timers on aio_poll
In reply to liu ping fanLiu,
Quoted 4 lines · --On 23 July 2013 10:56:42 +0800 liu ping fan <[email protected]> wrote:
Quoted 3 lines
I am told (by Stefan H) this approach is unsafe as existing timers may not expect to be run within
aio_poll.Have not figure out the reason, could you elaborate ?
I asked Stefan H whether we could guarantee that it was safe to call any timer routine within aio_poll. He said no, because they might (reasonably) assume they were only being called from the main loop. Therefore we'd have to audit any existing user. I suppose an example of something unsafe might be calling aio_poll from inside a timer routine.
Stefan thus recommended using a separate QEMUClock, and only running that clock's timers.
Signature · 1 line
Alex Bligh
Quoted 7 lines · Paolo Bonzini
Il 23/07/2013 04:53, liu ping fan ha scritto:
Quoted 3 lines
The scenior I can figure out is if adopting timeout of poll, then when changing the deadline, we need to invoke poll, and set the new timeout, right?
Yes, you need to call
aio_notifyso that poll is reinvoked.
I try to list the difference between alarm_timer and timeout of poll. It includes thread-affinity, resolution and easy-use.
Most of all, thread-affinity
The main issue with alarm timer is the affinity of timer_t with threads. For linux, SIGEV_THREAD_ID has been supported for a very long time and we already associate the signal with the specified thread. So the only issue is left for other unix, we can emulate the affinity by using SIGEV_THREAD and repost the event to the specified thread. As to timeout of poll, it has the affinity of threads.
Resolution:alarm_timer provides higher resolution, but do we care about it?
easy-use:
The reset of the deadline as mentioned.
Finally, I admit timeout of poll will save large chunk of platform-related code.
Thanks and regards,
Pingfan
Quoted 1 line
Paolo
Il 24/07/2013 03:28, liu ping fan ha scritto:
Quoted 19 lines · Paolo Bonzini
Quoted 7 lines
Quoted 7 lines
Il 23/07/2013 04:53, liu ping fan ha scritto:
Quoted 3 lines
Quoted 3 lines
The scenior I can figure out is if adopting timeout of poll, then when changing the deadline, we need to invoke poll, and set the new timeout, right?
Yes, you need to call
aio_notifyso that poll is reinvoked.I try to list the difference between
alarm_timerand timeout of poll. It includes thread-affinity, resolution and easy-use.Most of all, thread-affinity
The main issue with alarm timer is the affinity oftimer_twith threads. For linux,SIGEV_THREAD_IDhas been supported for a very long time and we already associate the signal with the specified thread. So the only issue is left for other unix, we can emulate the affinity by usingSIGEV_THREADand repost the event to the specified thread. As to timeout of poll, it has the affinity of threads.
Quoted 2 lines
Resolution:
alarm_timerprovides higher resolution, but do we care about it?
With ppoll, is this true or just hearsay?
(Without ppoll, indeed setitimer has 1 us resolution while poll has 1 ms; too bad that select has other problems, because select has also 1 us resolution).
Paolo
Quoted 4 lines
easy-use:
The reset of the deadline as mentioned.Finally, I admit timeout of poll will save large chunk of platform-related code.
Quoted 5 lines · --On 24 July 2013 08:42:26 +0200 Paolo Bonzini <[email protected]> wrote:
With ppoll, is this true or just hearsay?
(Without ppoll, indeed setitimer has 1 us resolution while poll has 1 ms; too bad that select has other problems, because select has also 1 us resolution).
Most 'reasonable' POSIX compliant operating systems have ppoll and I would have thought there is /better/ resolution there than relying not only on signal, but also a pipe or eventfd plus the underlying poll().
If it was my comments you are referring to, my concern was mainly about Windows (which I know very little about), as there does not appear to be a nanosecond or even microsecond alternative to WaitForMultipleObjects. However, articles like this:
suggest that WaitFor{Single,Multiple}Objects can have pretty appalling latency anyway (100ms!), and there's no evidence that's limited by making one of the FDs (or objects) ready. In these circumstances, I'd question whether we gain anything by worrying about timer resolution.
Signature · 1 line
Alex Bligh
Il 24/07/2013 09:31, Alex Bligh ha scritto:
Quoted 11 lines
Quoted 5 lines · --On 24 July 2013 08:42:26 +0200 Paolo Bonzini <[email protected]> wrote:
With ppoll, is this true or just hearsay?
(Without ppoll, indeed setitimer has 1 us resolution while poll has 1 ms; too bad that select has other problems, because select has also 1 us resolution).
Most 'reasonable' POSIX compliant operating systems have ppoll
Really? I could find no manpages for any of Solaris and *BSD.
Quoted 3 lines
and I would
have thought there is /better/ resolution there than relying not only on signal, but also a pipe or eventfd plus the underlyingpoll().
I agree.
Quoted 1 line
If it was my comments you are referring to
The message I replied to was Ping Fan's, but perhaps you brought it up first. I don't know.
Quoted 10 lines
, my concern was mainly about
Windows (which I know very little about), as there does not appear to be a nanosecond or even microsecond alternative to WaitForMultipleObjects. However, articles like this:suggest that WaitFor{Single,Multiple}Objects can have pretty appalling latency anyway (100ms!), and there's no evidence that's limited by making one of the FDs (or objects) ready.
... especially when making one of the FDs ready would likely have the same latency in some internal Windows thread that implements timers.
Quoted 3 lines
In these
circumstances, I'd question whether we gain anything by worrying about timer resolution.
Part of it should be fixed by os_setup_early_signal_handling.
This is corroborated by the fact that without os_setup_early_signal_handling Wine always works, and Windows breaks.
Paolo
Quoted 20 lines · Paolo Bonzini
Il 24/07/2013 03:28, liu ping fan ha scritto:
Quoted 19 lines · Paolo Bonzini
Quoted 7 lines
Quoted 7 lines
Il 23/07/2013 04:53, liu ping fan ha scritto:
Quoted 3 lines
Quoted 3 lines
The scenior I can figure out is if adopting timeout of poll, then when changing the deadline, we need to invoke poll, and set the new timeout, right?
Yes, you need to call
aio_notifyso that poll is reinvoked.I try to list the difference between
alarm_timerand timeout of poll. It includes thread-affinity, resolution and easy-use.Most of all, thread-affinity
The main issue with alarm timer is the affinity oftimer_twith threads. For linux,SIGEV_THREAD_IDhas been supported for a very long time and we already associate the signal with the specified thread. So the only issue is left for other unix, we can emulate the affinity by usingSIGEV_THREADand repost the event to the specified thread. As to timeout of poll, it has the affinity of threads.
Quoted 9 lines
Quoted 2 lines
Resolution:
alarm_timerprovides higher resolution, but do we care about it?With ppoll, is this true or just hearsay?
(Without ppoll, indeed setitimer has 1 us resolution while poll has 1 ms; too bad that select has other problems, because select has also 1 us resolution).
Paid some time to dig the kernel code, and find out that the resolution lost by timeout of poll/select..etc is cause by the timeout is a slack region.
See code indo_poll()
if (!poll_schedule_timeout(wait, TASK_INTERRUPTIBLE, to, slack))
Notice the slack param, it causes the lose of resolution. The process default slack time inherits from init_task and it is
.timer_slack_ns = 50000, /* 50 usec default slack */
But we can fix it by PR_SET_TIMERSLACK to decrease it for select/poll/...
So ppoll with timerslack adjustment will meet our requirement. But what about other non-linux system?
Regards,
Pingfan
Quoted 7 lines
Paolo
Quoted 4 lines
easy-use:
The reset of the deadline as mentioned.Finally, I admit timeout of poll will save large chunk of platform-related code.
Il 24/07/2013 09:43, liu ping fan ha scritto:
Quoted 11 lines
Paid some time to dig the kernel code, and find out that the resolution lost by timeout of poll/select..etc is cause by the timeout is a slack region.
See code indo_poll()
if (!poll_schedule_timeout(wait,TASK_INTERRUPTIBLE, to, slack))Notice the slack param, it causes the lose of resolution. The process default slack time inherits from
init_taskand it is.timer_slack_ns = 50000, /* 50 usec default slack */But we can fix it by
PR_SET_TIMERSLACKto decrease it for select/poll/...
Right, good catch. I just learnt about PR_SET_TIMERSLACK. :)
Alex, can you add it to your series? (Note that you must set a timer slack of 1, because 0 is interpreted as "default").
Quoted 2 lines
So ppoll with timerslack adjustment will meet our requirement. But what about other non-linux system?
They might have their own mechanism similar to PR_SET_TIMERSLACK.
Paolo
Paolo,
Quoted 3 lines · --On 24 July 2013 09:43:28 +0200 Paolo Bonzini <[email protected]> wrote:
Quoted 1 line
Most 'reasonable' POSIX compliant operating systems have ppoll
Really? I could find no manpages for any of Solaris and *BSD.
OK I shall (re)research that then! I suppose select() / pselect() is an alternative when there are few FDs.
Quoted 15 lines
Quoted 11 lines
, my concern was mainly about
Windows (which I know very little about), as there does not appear to be a nanosecond or even microsecond alternative to WaitForMultipleObjects. However, articles like this:http://social.msdn.microsoft.com/Forums/vstudio/en-US/e8a7cb1e-9edd-4ee3
-982e-f66b7bf6ae44/improve-accuracy-waitforsingleobjectsuggest that WaitFor{Single,Multiple}Objects can have pretty appalling latency anyway (100ms!), and there's no evidence that's limited by making one of the FDs (or objects) ready.
... especially when making one of the FDs ready would likely have the same latency in some internal Windows thread that implements timers.
Quoted 8 lines
Quoted 3 lines
In these
circumstances, I'd question whether we gain anything by worrying about timer resolution.Part of it should be fixed by
os_setup_early_signal_handling.This is corroborated by the fact that without
os_setup_early_signal_handlingWine always works, and Windows breaks.
This:
http://www.windowstimestamp.com/description
suggests that whilst WaitForMultipleEvents has a millisecond timeout, one can (see section 3.2) use these to wait for an object which is itself a timer and expires with - in this case - 100ns resolution which is probably enough.
Again I know nothing about Windows so this may be completely wrong.
Signature · 1 line
Alex Bligh
Paolo,
Quoted 2 lines · --On 24 July 2013 09:54:57 +0200 Paolo Bonzini <[email protected]> wrote:
Alex, can you add it to your series? (Note that you must set a timer slack of 1, because 0 is interpreted as "default").
Sure, will do. I'm guessing I'll have to look for that inside configure as well.
Signature · 1 line
Alex Bligh
Il 24/07/2013 10:01, Alex Bligh ha scritto:
Quoted 7 lines
Quoted 6 lines
Quoted 1 line
Part of it should be fixed by
os_setup_early_signal_handling.This is corroborated by the fact that without
os_setup_early_signal_handlingWine always works, and Windows breaks.
Quoted 8 lines
This:
http://www.windowstimestamp.com/description
suggests that whilst WaitForMultipleEvents has a millisecond timeout, one can (see section 3.2) use these to wait for an object which is itself a timer and expires with - in this case - 100ns resolution which is probably enough.Again I know nothing about Windows so this may be completely wrong.
This is roughly what the alarm timer code does on Windows. I also don't know much about the internals, I wouldn't worry too much.
Paolo
[...]
Quoted 13 lines
Quoted 5 lines
suggest that WaitFor{Single,Multiple}Objects can have pretty appalling latency anyway (100ms!), and there's no evidence that's limited by making one of the FDs (or objects) ready.
... especially when making one of the FDs ready would likely have the same latency in some internal Windows thread that implements timers.
Quoted 3 lines
In these
circumstances, I'd question whether we gain anything by worrying about timer resolution.
Does hpet emulation care about it?
Quoted 7 lines
Part of it should be fixed by
os_setup_early_signal_handling.This is corroborated by the fact that without
os_setup_early_signal_handlingWine always works, and Windows breaks.Paolo
Quoted 6 lines · --On 24 July 2013 09:01:22 +0100 Alex Bligh <[email protected]> wrote:
Quoted 3 lines
Quoted 1 line
Most 'reasonable' POSIX compliant operating systems have ppoll
Really? I could find no manpages for any of Solaris and *BSD.
OK I shall (re)research that then! I suppose
select()/pselect()is an alternative when there are few FDs.
Looks like I was wrong. However, pselect support is pretty wide.
Signature · 1 line
Alex Bligh
Il 24/07/2013 10:37, Alex Bligh ha scritto:
Quoted 4 lines
--On 24 July 2013 09:01:22 +0100 Alex Bligh <[email protected]> wrote:
Quoted 8 lines
Quoted 6 lines
Quoted 3 lines
Quoted 1 line
Most 'reasonable' POSIX compliant operating systems have ppoll
Really? I could find no manpages for any of Solaris and *BSD.
OK I shall (re)research that then! I suppose
select()/pselect()is an alternative when there are few FDs.Looks like I was wrong. However, pselect support is pretty wide.
Yes, on the other hand we only recently switched from select() to poll().
I guess using ms resolution wouldn't be too bad for non-Linux. After all before dynticks support was added to the alarm timer, it used to use /dev/rtc or /dev/hpet -- which is very precise but only has ms resolution too.
We might not care about the slack either, in practice. TCG timing sucks anyway, and for KVM/Xen most relevant device models are in the kernel.
Paolo
[Qemu-devel] [PATCHv2a] [RFC 8/7 (really)] Add prctl(PR_SET_TIMERSLACK, 1, ...) to reduce timer slack
In reply to Paolo BonziniWhere supported, called prctl(PR_SET_TIMERSLACK, 1, ...) to set one nanosecond timer slack to increase precision of timer calls.
---
[ Additional patch on the the end of the PATCHv2 series - I'll resend if I have further comments with this reordered so it's next to the ppoll introduction ]
configure | 18 ++++++++++++++++++ qemu-timer.c | 7 +++++++ 2 files changed, 25 insertions(+)
configure +18 -0
@@ -2817,6 +2817,21 @@ if compile_prog "" "" ; then ppoll=yes fi +# check for prctl(PR_SET_TIMERSLACK , ... ) support +prctl_pr_set_timerslack=no +cat > $TMPC << EOF +#include <sys/prctl.h> + +int main(void) +{ + prctl(PR_SET_TIMERSLACK, 1, 0, 0, 0); + return 0; +} +EOF +if compile_prog "" "" ; then + prctl_pr_set_timerslack=yes +fi + # check for epoll support epoll=no cat > $TMPC << EOF
@@ -3811,6 +3826,9 @@ fi if test "$ppoll" = "yes" ; then echo "CONFIG_PPOLL=y" >> $config_host_mak fi +if test "$prctl_pr_set_timerslack" = "yes" ; then + echo "CONFIG_PRCTL_PR_SET_TIMERSLACK=y" >> $config_host_mak +fi if test "$epoll" = "yes" ; then echo "CONFIG_EPOLL=y" >> $config_host_mak fi
qemu-timer.c +7 -0
@@ -37,6 +37,10 @@#include <poll.h> #endif +#ifdef CONFIG_PRCTL_PR_SET_TIMERSLACK +#include <sys/prctl.h> +#endif +/***********************************************************/ /* timers */
@@ -367,6 +371,9 @@ void init_clocks(void) vm_clock =qemu_new_clock (QEMU_CLOCK_VIRTUAL ); host_clock =qemu_new_clock (QEMU_CLOCK_HOST ); } +#ifdef CONFIG_PRCTL_PR_SET_TIMERSLACK +prctl (PR_SET_TIMERSLACK ,1 ,0 ,0 ,0 ); +#endif }uint64_t qemu_timer_expire_time_ns (QEMUTimer *ts)
Signature · 1 line
1.7.9.5
Quoted 41 lines · liu ping fan
Quoted 40 lines · Jan Kiszka
Quoted 38 lines · liu ping fan
Quoted 22 lines · Alex Bligh
Liu,
Quoted 8 lines · --On 21 July 2013 16:42:57 +0800 Liu Ping Fan <[email protected]> wrote:
Currently, the timers run on iothread within BQL, so virtio-block dataplane can not use throttle, as Stefan Hajnoczi pointed out in his patches to port dataplane onto block layer.(Thanks, Stefan) To enable this feature, I plan to enable timers to run on AioContext's thread. And maybe in future, hpet can run with its dedicated thread too.
Also, I see Alex Bligh is on the same effort by another method,(it is a
good idea) "[RFC] aio/async: Add timed bottom-halves".Stefan & Paolo did not like that method much, so I did a third method (posted yesterday) suggested by Stefan which adds a clock to AioContext (to which timers can be attached), deletes ALL the
alarm_timerstuff (which was very cathartic), uses timeouts on theg_poll, and adds ppoll where this is available. Series at:
http://lists.nongnu.org/archive/html/qemu-devel/2013-07/msg03334.htmlQuoted 9 lines
I suspect this also overlaps with your code.
So now we have 3 methods to do similar things!
One advantage of my approach is that it removes more code than it adds (by quite a margin). However, alarm timers could have been left in. What's the advantage in giving an AioContext its own alarm timer as opposed to just its own clock?
I read your second series, and try to summary the main different between us. Please correct me, if I misunderstood something.
--1st. You try to create a separate QemuClock for AioContext.
I think QemuClock is the clock event source and we have three
classic with fine definition. They should be qemu-wide for time measurement. On the other handler, timer is a concept for timeout,
Quoted 8 lines
Quoted 2 lines
Timers, as used in QEMU, are not only for "unimportant" and unlikely-to-fire timeouts. They are also for potential high-rate, high
Sorry, but can not catch the point. QemuTimer is used to trigger something, so the process can be delayed for some reason. For high resolution events, do we go directly to QEMUClock, which fall back on host's hpet? And the use case is just for get the time stamp, NOT for trigger?
It's also unclear to me what you mean, Jan. Which approach exactly are you unhappy with?
We always invoke timer callbacks from the main loop, there is no high-frequency high-priority timer code path, so I'm not sure what the issue is.
Stefan
Quoted 50 lines · liu ping fan
Quoted 14 lines · Alex Bligh
Liu,
Quoted 7 lines · --On 22 July 2013 12:38:02 +0800 liu ping fan <[email protected]> wrote:
I read your second series, and try to summary the main different between us. Please correct me, if I misunderstood something.
--1st. You try to create a separate QemuClock for AioContext.
I think QemuClock is the clock event source and we have three
classic with fine definition. They should be qemu-wide for time measurement. On the other handler, timer is a concept for timeout, so it can be AioContext-related. So I have patch2&5.Quoted 3 lines
Yes and no. QEMUClock is not a clock source. QEMUClock /has/ a clock source, and more than one QEMUClock can share the same clock source.
Supposed the case sync the time across iothread/vcpu/dataplane, the
vm_clockis a unique interface for all of them.Quoted 11 lines
QEMUClock does have its own list of timers, and so if you only want to run a subset of timers in
aio_poll(which is I believe the requirement so not to change the behaviour of existing timers), you need a separate QEMUClock.The approach I took (StefanH's idea) was to put a QEMUClock into each AioContext. Arguably you might want to put a set of 3 QEMUClock's into each AioContext, one for each clock source.
QEMUClock itself is very lightweight as far as I can tell.
I think the weight is not the point here, rather, the concept.
Quoted 13 lines
Quoted 2 lines
--2nd. You want to substitute
alarm_timerwith timeout of poll.Correct.
Quoted 4 lines
I try to trigger each specified thread when its deadline comes.
But unfortunately, the signal can not be delivered to the specified thread directly, and I need timerfd for each AioContext. (If we can have the equivalent on other platform)Quoted 4 lines
Firstly, I can't see the advantage of keeping the
alarm_timerstuff around at all if we can delete it. Save, of course, that on systems that don't have ppoll or equivalent you lose sub-millisecond timing by deleting them.
Quoted 6 lines
I do not think it very clearly. But the final aim is to associate an eventfd with deadline, and attach the eventfd to
g_poll. For linux, it can be easily achieved by timerfd, for other posix, what I can thought till now is to distinguish the source of timer in signal handler, and revoke a eventfd (stands for deadline) on AioContext. I am not sure whether this is better than timeout of poll.
Using eventfds for timeouts is more heavy-weight. It means a file
descriptor per timeout and more system calls. We already need to do
the poll(2) system call, we might as well make use of the timeout argument.
Stefan
Quoted 45 lines · Stefan Hajnoczi
Quoted 44 lines · liu ping fan
Quoted 42 lines · Jan Kiszka
Quoted 32 lines · liu ping fan
Quoted 22 lines · Alex Bligh
Liu,
Quoted 8 lines · --On 21 July 2013 16:42:57 +0800 Liu Ping Fan <[email protected]> wrote:
Currently, the timers run on iothread within BQL, so virtio-block dataplane can not use throttle, as Stefan Hajnoczi pointed out in his patches to port dataplane onto block layer.(Thanks, Stefan) To enable this feature, I plan to enable timers to run on AioContext's thread. And maybe in future, hpet can run with its dedicated thread too.
Also, I see Alex Bligh is on the same effort by another method,(it is a
good idea) "[RFC] aio/async: Add timed bottom-halves".Stefan & Paolo did not like that method much, so I did a third method (posted yesterday) suggested by Stefan which adds a clock to AioContext (to which timers can be attached), deletes ALL the
alarm_timerstuff (which was very cathartic), uses timeouts on theg_poll, and adds ppoll where this is available. Series at:
http://lists.nongnu.org/archive/html/qemu-devel/2013-07/msg03334.htmlQuoted 9 lines
I suspect this also overlaps with your code.
So now we have 3 methods to do similar things!
One advantage of my approach is that it removes more code than it adds (by quite a margin). However, alarm timers could have been left in. What's the advantage in giving an AioContext its own alarm timer as opposed to just its own clock?
Quoted 6 lines
I read your second series, and try to summary the main different between us. Please correct me, if I misunderstood something.
--1st. You try to create a separate QemuClock for AioContext.
I think QemuClock is the clock event source and we have three
classic with fine definition. They should be qemu-wide for time measurement. On the other handler, timer is a concept for timeout,Timers, as used in QEMU, are not only for "unimportant" and unlikely-to-fire timeouts. They are also for potential high-rate, high
Quoted 8 lines
Quoted 5 lines
Sorry, but can not catch the point. QemuTimer is used to trigger something, so the process can be delayed for some reason. For high resolution events, do we go directly to QEMUClock, which fall back on host's hpet? And the use case is just for get the time stamp, NOT for trigger?
It's also unclear to me what you mean, Jan. Which approach exactly are you unhappy with?
"Timer is a concept for timeouts" is either an unfortunate term or indicates a misunderstanding what timers are used for, also in QEMU. You may currently see them only in the context of timeouts, but they are also used for periodic or continuously reprogrammed one-shot guest timers.
Make sure that we can easily extend the timer architecture so that selected timers can run against their own carrier threads while the rest sticks with the mainloop or whatever best-effort context, and I'm fine.
Quoted 3 lines
We always invoke timer callbacks from the main loop, there is no high-frequency high-priority timer code path,
...yet...
Quoted 5 lines
so I'm not sure what the
issue is.Stefan
Jan
Signature · 2 lines
Siemens AG, Corporate Technology, CT RTC ITP SES-DE Corporate Competence Center Embedded Linux
Quoted 14 lines · Liu Ping Fan
Currently, the timers run on iothread within BQL, so virtio-block dataplane can not use throttle, as Stefan Hajnoczi pointed out in his patches to port dataplane onto block layer.(Thanks, Stefan) To enable this feature, I plan to enable timers to run on AioContext's thread. And maybe in future, hpet can run with its dedicated thread too.
Also, I see Alex Bligh is on the same effort by another method,(it is a good idea)
"[RFC] aio/async: Add timed bottom-halves".
So I think it is better to post my series for discussion, although I have not thought very clearly point, e.g. sigactionThis series ports two parts of timer stuff onto AioContext: alarm timer and timer list. Currently I worked based on Stefanha's git tree
https://github.com/stefanha/qemu.git dataplane-block-layer.
Quoted 6 lines
---
Open issue:
The thread safe problem on timer list. To resolve that, I plan to adopt the bh model.
(Not sure about this, maybe Stefan's solution in another thread is better) Although leave most of the race issue unresolved, patch 4 has tried to fix one of them as Jan Kiszka points out thatvm_clockcan be read outside BQL, thanks Jan :)
Thanks for sharing this series.
I think Paolo's idea of getting rid of alarm timers is a good one. We end up with less code and fewer system calls. The only issue is timer resolution and at least on Linux we can use ppoll(2).
Alex Bligh's series gives each AioContext its own rt_clock. This avoids the need for synchronization in the simple case. If we require timer access between threads then we really need to synchronize.
You pointed out in another email that vm_clock stops when the guest is paused. I think we can find a solution for I/O throttling and QED, which use vm_clock in the block layer. Note that block jobs already use rt_clock.
I'm in favor of Alex Bligh's series rather than continuing to use alarm timers.
Stefan
Quoted 8 lines · --On 25 July 2013 14:05:30 +0200 Stefan Hajnoczi <[email protected]> wrote:
Alex Bligh's series gives each AioContext its own
rt_clock. This avoids the need for synchronization in the simple case. If we require timer access between threads then we really need to synchronize.You pointed out in another email that
vm_clockstops when the guest is paused. I think we can find a solution for I/O throttling and QED, which usevm_clockin the block layer. Note that block jobs already usert_clock.
I would happily at a QEMUClock of each type to AioContext. They are after all pretty lightweight.
Signature · 1 line
Alex Bligh
Quoted 9 lines · Alex Bligh
--On 25 July 2013 14:05:30 +0200 Stefan Hajnoczi <[email protected]>
Quoted 4 lines · wrote:
Alex Bligh's series gives each AioContext its own
rt_clock. This avoids the need for synchronization in the simple case. If we require timer access between threads then we really need to synchronize.
Quoted 7 lines
Quoted 4 lines
You pointed out in another email that
vm_clockstops when the guest is paused. I think we can find a solution for I/O throttling and QED, which usevm_clockin the block layer. Note that block jobs already usert_clock.I would happily at a QEMUClock of each type to AioContext. They are after all pretty lightweight.
What's the point of adding tones of QEMUClock instances? Considering proper abstraction, how are they different for each AioContext? Will they run against different clock sources, start/stop at different times? If the answer is "they have different timer list", then fix this incorrect abstraction.
Jan
Signature · 2 lines
Siemens AG, Corporate Technology, CT RTC ITP SES-DE Corporate Competence Center Embedded Linux
Il 25/07/2013 14:32, Jan Kiszka ha scritto:
Quoted 15 lines · Alex Bligh
Quoted 14 lines
--On 25 July 2013 14:05:30 +0200 Stefan Hajnoczi <[email protected]>
Quoted 8 lines · wrote:
Alex Bligh's series gives each AioContext its own
rt_clock. This avoids the need for synchronization in the simple case. If we require timer access between threads then we really need to synchronize.You pointed out in another email that
vm_clockstops when the guest is paused. I think we can find a solution for I/O throttling and QED, which usevm_clockin the block layer. Note that block jobs already usert_clock.
Quoted 8 lines
Quoted 2 lines
I would happily at a QEMUClock of each type to AioContext. They are after all pretty lightweight.
What's the point of adding tones of QEMUClock instances? Considering proper abstraction, how are they different for each AioContext? Will they run against different clock sources, start/stop at different times? If the answer is "they have different timer list", then fix this incorrect abstraction.
s/QEMUClock/QEMUTimerList/ ? :)
Paolo
Quoted 19 lines · Paolo Bonzini
Il 25/07/2013 14:32, Jan Kiszka ha scritto:
Quoted 18 lines · Alex Bligh
Quoted 16 lines
--On 25 July 2013 14:05:30 +0200 Stefan Hajnoczi <[email protected]>
Quoted 8 lines · wrote:
Alex Bligh's series gives each AioContext its own
rt_clock. This avoids the need for synchronization in the simple case. If we require timer access between threads then we really need to synchronize.You pointed out in another email that
vm_clockstops when the guest is paused. I think we can find a solution for I/O throttling and QED, which usevm_clockin the block layer. Note that block jobs already usert_clock.I would happily at a QEMUClock of each type to AioContext. They are after all pretty lightweight.
Quoted 7 lines
Quoted 5 lines
What's the point of adding tones of QEMUClock instances? Considering proper abstraction, how are they different for each AioContext? Will they run against different clock sources, start/stop at different times? If the answer is "they have different timer list", then fix this incorrect abstraction.
s/QEMUClock/QEMUTimerList/ ? :)
What do you mean? If the content of struct QEMUClock remained the same, that would just paper over a design mistake.
Jan
Signature · 2 lines
Siemens AG, Corporate Technology, CT RTC ITP SES-DE Corporate Competence Center Embedded Linux
Quoted 26 lines · Jan Kiszka
Quoted 25 lines · Paolo Bonzini
Il 25/07/2013 14:32, Jan Kiszka ha scritto:
Quoted 23 lines · Alex Bligh
Quoted 9 lines
--On 25 July 2013 14:05:30 +0200 Stefan Hajnoczi <[email protected]>
Quoted 4 lines · wrote:
Alex Bligh's series gives each AioContext its own
rt_clock. This avoids the need for synchronization in the simple case. If we require timer access between threads then we really need to synchronize.Quoted 7 lines
Quoted 4 lines
You pointed out in another email that
vm_clockstops when the guest is paused. I think we can find a solution for I/O throttling and QED, which usevm_clockin the block layer. Note that block jobs already usert_clock.I would happily at a QEMUClock of each type to AioContext. They are after all pretty lightweight.
What's the point of adding tones of QEMUClock instances? Considering proper abstraction, how are they different for each AioContext? Will they run against different clock sources, start/stop at different times? If the answer is "they have different timer list", then fix this incorrect abstraction.
Quoted 4 lines
Quoted 1 line
s/QEMUClock/QEMUTimerList/ ? :)
What do you mean? If the content of struct QEMUClock remained the same, that would just paper over a design mistake.
QEMUClock really isn't much more than a reference to a clock source, an enabled/disabled flag, and a timer list.
The point of having one QEMUClock per AioContext is to allow each thread's event loop to have its own timers, without synchronization.
What is the design mistake? I only see poor naming.
Stefan
Quoted 32 lines · Stefan Hajnoczi
Quoted 26 lines · Jan Kiszka
Quoted 25 lines · Paolo Bonzini
Il 25/07/2013 14:32, Jan Kiszka ha scritto:
Quoted 23 lines · Alex Bligh
Quoted 9 lines
--On 25 July 2013 14:05:30 +0200 Stefan Hajnoczi <[email protected]>
Quoted 4 lines · wrote:
Alex Bligh's series gives each AioContext its own
rt_clock. This avoids the need for synchronization in the simple case. If we require timer access between threads then we really need to synchronize.Quoted 7 lines
Quoted 4 lines
You pointed out in another email that
vm_clockstops when the guest is paused. I think we can find a solution for I/O throttling and QED, which usevm_clockin the block layer. Note that block jobs already usert_clock.I would happily at a QEMUClock of each type to AioContext. They are after all pretty lightweight.
What's the point of adding tones of QEMUClock instances? Considering proper abstraction, how are they different for each AioContext? Will they run against different clock sources, start/stop at different times? If the answer is "they have different timer list", then fix this incorrect abstraction.
Quoted 4 lines
Quoted 1 line
s/QEMUClock/QEMUTimerList/ ? :)
What do you mean? If the content of struct QEMUClock remained the same, that would just paper over a design mistake.
Quoted 7 lines
QEMUClock really isn't much more than a reference to a clock source, an enabled/disabled flag, and a timer list.
The point of having one QEMUClock per AioContext is to allow each thread's event loop to have its own timers, without synchronization.
What is the design mistake? I only see poor naming.
The concept of clocks (with start/stop property) and active timers shall not be mixed, they are independent. Just factor out a separate list of running timers and pass that to the infrastructure that deals with them. I attached them to a QEMUAlarmTimer instance, you will likely need a different abstraction.
Jan
Signature · 2 lines
Siemens AG, Corporate Technology, CT RTC ITP SES-DE Corporate Competence Center Embedded Linux
Il 25/07/2013 14:38, Jan Kiszka ha scritto:
Quoted 26 lines · Paolo Bonzini
Quoted 25 lines
Il 25/07/2013 14:32, Jan Kiszka ha scritto:
Quoted 23 lines · Alex Bligh
Quoted 9 lines
--On 25 July 2013 14:05:30 +0200 Stefan Hajnoczi <[email protected]>
Quoted 4 lines · wrote:
Alex Bligh's series gives each AioContext its own
rt_clock. This avoids the need for synchronization in the simple case. If we require timer access between threads then we really need to synchronize.Quoted 7 lines
Quoted 4 lines
You pointed out in another email that
vm_clockstops when the guest is paused. I think we can find a solution for I/O throttling and QED, which usevm_clockin the block layer. Note that block jobs already usert_clock.I would happily at a QEMUClock of each type to AioContext. They are after all pretty lightweight.
What's the point of adding tones of QEMUClock instances? Considering proper abstraction, how are they different for each AioContext? Will they run against different clock sources, start/stop at different times? If the answer is "they have different timer list", then fix this incorrect abstraction.
Quoted 4 lines
Quoted 1 line
s/QEMUClock/QEMUTimerList/ ? :)
What do you mean? If the content of struct QEMUClock remained the same, that would just paper over a design mistake.
I mean that really the "clock" part of QEMUClock is really just
#define QEMU_CLOCK_REALTIME 0
#define QEMU_CLOCK_VIRTUAL 1
#define QEMU_CLOCK_HOST 2
and
switch(clock_type) {
case QEMU_CLOCK_REALTIME:
return get_clock();
default:
case QEMU_CLOCK_VIRTUAL:
if (use_icount) {
return cpu_get_icount();
} else {
return cpu_get_clock();
}
case QEMU_CLOCK_HOST:
now = get_clock_realtime();
last = clock->last;
clock->last = now;
if (now < last) {
notifier_list_notify(&clock->reset_notifiers, &now);
}
return now;
}Everything else in QEMUClock is just a list of timers. You also cannot put timers from different clocks in the same list.
Different AioContexts needs different lists of timers even for the same source (for obvious reasons of thread-safety). Different sources need different lists of timers because they do not advance together. The most obvious example is vm_clock which can stop for an arbitrary amount of time while other rt_clock timers can be executed. It is a fundamental invariant of QEMUClock that if the first timer in the list hasn't expired, the following ones also haven't.
Paolo
Quoted 15 lines · Paolo Bonzini
Il 25/07/2013 14:48, Jan Kiszka ha scritto:
Quoted 2 lines
The concept of clocks (with start/stop property) and active timers shall not be mixed, they are independent.
Are you referring to this in particular:
void
pause_all_vcpus(void){ CPUState *cpu = first_cpu; qemu_clock_enable(vm_clock, false);...
}void
resume_all_vcpus(void)
Quoted 8 lines
{ CPUState *cpu = first_cpu; qemu_clock_enable(vm_clock, true);...
}where _all_ the
vm_clocktimer lists need to be stopped at the same time?
Still wrong abstraction: the vm_clock is stopped. And that implies that no timer using this clock will fire anymore.
Jan
Signature · 2 lines
Siemens AG, Corporate Technology, CT RTC ITP SES-DE Corporate Competence Center Embedded Linux
Il 25/07/2013 14:48, Jan Kiszka ha scritto:
Quoted 2 lines
The concept of clocks (with start/stop property) and active timers shall not be mixed, they are independent.
Are you referring to this in particular:
void pause_all_vcpus(void)
{
CPUState *cpu = first_cpu;
qemu_clock_enable(vm_clock, false);...
}
void resume_all_vcpus(void)
{
CPUState *cpu = first_cpu;
qemu_clock_enable(vm_clock, true);...
}
where _all_ the vm_clock timer lists need to be stopped at the same time?
Paolo
Quoted 19 lines · Jan Kiszka
Quoted 18 lines · Paolo Bonzini
Il 25/07/2013 14:48, Jan Kiszka ha scritto:
Quoted 2 lines
The concept of clocks (with start/stop property) and active timers shall not be mixed, they are independent.
Are you referring to this in particular:
void
pause_all_vcpus(void){ CPUState *cpu = first_cpu; qemu_clock_enable(vm_clock, false);...
}void
resume_all_vcpus(void){ CPUState *cpu = first_cpu;
Quoted 8 lines
Quoted 5 lines
qemu_clock_enable(vm_clock, true);...
}where _all_ the
vm_clocktimer lists need to be stopped at the same time?Still wrong abstraction: the
vm_clockis stopped. And that implies that no timer using this clock will fire anymore.
Perhaps we should split QEMUClock into ClockSource and TimerList. Instead of using the int type field in TimerList we keep a ClockSource pointer.
A ClockSource can be enabled/disabled.
Then there's the question of ClockSource thread-safety. Host clock is weird because it notifies listeners when the clock source jumps. vm clock is hard because of icount.
It's also weird to think about multiple threads dispatching timer callbacks while a ClockSource is disabled. Basically, do we need a guarantee that timer callbacks have completed and will not be invoked in any thread after the disable function returns?
But assuming thread-safety is addressed, this would fit the design you're suggesting.
Stefan
Quoted 29 lines · Stefan Hajnoczi
Quoted 19 lines · Jan Kiszka
Quoted 18 lines · Paolo Bonzini
Il 25/07/2013 14:48, Jan Kiszka ha scritto:
Quoted 2 lines
The concept of clocks (with start/stop property) and active timers shall not be mixed, they are independent.
Are you referring to this in particular:
void
pause_all_vcpus(void){ CPUState *cpu = first_cpu; qemu_clock_enable(vm_clock, false);...
}void
resume_all_vcpus(void){ CPUState *cpu = first_cpu;Quoted 8 lines
Quoted 5 lines
qemu_clock_enable(vm_clock, true);...
}where _all_ the
vm_clocktimer lists need to be stopped at the same time?Still wrong abstraction: the
vm_clockis stopped. And that implies that no timer using this clock will fire anymore.
Quoted 8 lines
Perhaps we should split QEMUClock into ClockSource and TimerList. Instead of using the int type field in TimerList we keep a ClockSource pointer.
A ClockSource can be enabled/disabled.
Then there's the question of ClockSource thread-safety. Host clock is weird because it notifies listeners when the clock source jumps.
Detection can be handled with a per-clock lock, dispatching can continue to run under BQL for now.
Quoted 1 line
vm clock is hard because of icount.
True.
Quoted 5 lines
It's also weird to think about multiple threads dispatching timer callbacks while a ClockSource is disabled. Basically, do we need a guarantee that timer callbacks have completed and will not be invoked in any thread after the disable function returns?
We need to make sure that no timer callback dispatcher works with a clock value that is past the one at which we stopped. The simple answer to this is a lock...
Quoted 3 lines
But assuming thread-safety is addressed, this would fit the design you're suggesting.
Yes, this looks good.
Jan
Signature · 2 lines
Siemens AG, Corporate Technology, CT RTC ITP SES-DE Corporate Competence Center Embedded Linux
Quoted 8 lines · --On 25 July 2013 14:32:59 +0200 Jan Kiszka <[email protected]> wrote:
Quoted 2 lines
I would happily at a QEMUClock of each type to AioContext. They are after all pretty lightweight.
What's the point of adding tones of QEMUClock instances? Considering proper abstraction, how are they different for each AioContext? Will they run against different clock sources, start/stop at different times? If the answer is "they have different timer list", then fix this incorrect abstraction.
Even if I fix the abstraction, there is a question of whether it is necessary to have more than one timer list per AioContext, because the timer list is fundamentally per clock-source. I am currently just using QEMU_CLOCK_REALTIME as that's what the block drivers normally want. Will block drivers ever want timers from a different clock source?
Signature · 1 line
Alex Bligh
Quoted 13 lines · Alex Bligh
Quoted 8 lines · --On 25 July 2013 14:32:59 +0200 Jan Kiszka <[email protected]> wrote:
Quoted 2 lines
I would happily at a QEMUClock of each type to AioContext. They are after all pretty lightweight.
What's the point of adding tones of QEMUClock instances? Considering proper abstraction, how are they different for each AioContext? Will they run against different clock sources, start/stop at different times? If the answer is "they have different timer list", then fix this incorrect abstraction.
Quoted 5 lines
Even if I fix the abstraction, there is a question of whether it is necessary to have more than one timer list per AioContext, because the timer list is fundamentally per clock-source. I am currently just using
QEMU_CLOCK_REALTIMEas that's what the block drivers normally want. Will block drivers ever want timers from a different clock source?
block.c and block/qed.c use vm_clock because block drivers should not do guest I/O while the vm is stopped. This is especially true during live migration where it's important to hand off the image file from the source host to the destination host with good cache consistency. The source host is not allowed to modify the image file anymore once the destination host has resumed the guest.
Block jobs use rt_clock because they aren't considered guest I/O.
Stefan
Quoted 8 lines · --On 26 July 2013 10:43:45 +0200 Stefan Hajnoczi <[email protected]> wrote:
block.candblock/qed.cusevm_clockbecause block drivers should not do guest I/O while the vm is stopped. This is especially true during live migration where it's important to hand off the image file from the source host to the destination host with good cache consistency. The source host is not allowed to modify the image file anymore once the destination host has resumed the guest.Block jobs use
rt_clockbecause they aren't considered guest I/O.
That's going to be 'interesting' if qemu-img uses a block driver that uses a timer, as I don't think the vm timer is running when qemu-img is running.
Signature · 1 line
Alex Bligh
Il 26/07/2013 11:08, Alex Bligh ha scritto:
Quoted 12 lines
--On 26 July 2013 10:43:45 +0200 Stefan Hajnoczi <[email protected]>
Quoted 7 lines · wrote:
block.candblock/qed.cusevm_clockbecause block drivers should not do guest I/O while the vm is stopped. This is especially true during live migration where it's important to hand off the image file from the source host to the destination host with good cache consistency. The source host is not allowed to modify the image file anymore once the destination host has resumed the guest.
Quoted 5 lines
Quoted 1 line
Block jobs use
rt_clockbecause they aren't considered guest I/O.That's going to be 'interesting' if qemu-img uses a block driver that uses a timer, as I don't think the vm timer is running when qemu-img is running.
Yes, it is, see stubs/cpu-get-clock.c:
#include "qemu-common.h"
#include "qemu/timer.h"
int64_t cpu_get_clock(void)
{
return get_clock_realtime();
}vm_clock becomes a synonym of rt_clock basically.
Paolo
Quoted 14 lines · Alex Bligh
Quoted 9 lines · --On 25 July 2013 14:32:59 +0200 Jan Kiszka <[email protected]> wrote:
Quoted 3 lines
I would happily at a QEMUClock of each type to AioContext. They are after
all pretty lightweight.What's the point of adding tones of QEMUClock instances? Considering proper abstraction, how are they different for each AioContext? Will they run against different clock sources, start/stop at different times? If the answer is "they have different timer list", then fix this incorrect abstraction.
Quoted 3 lines
Even if I fix the abstraction, there is a question of whether it is necessary to have more than one timer list per AioContext, because the timer list is fundamentally per clock-source.
So far. Once we support different handler thread for timers, there will be more lists than clock sources.
Jan
Signature · 2 lines
Siemens AG, Corporate Technology, CT RTC ITP SES-DE Corporate Competence Center Embedded Linux
Jan,
Quoted 10 lines · --On 26 July 2013 12:05:06 +0200 Jan Kiszka <[email protected]> wrote:
Quoted 10 lines
Quoted 9 lines
Quoted 3 lines
I would happily at a QEMUClock of each type to AioContext. They are after
all pretty lightweight.What's the point of adding tones of QEMUClock instances? Considering proper abstraction, how are they different for each AioContext? Will they run against different clock sources, start/stop at different times? If the answer is "they have different timer list", then fix this incorrect abstraction.
Quoted 6 lines
Quoted 3 lines
Even if I fix the abstraction, there is a question of whether it is necessary to have more than one timer list per AioContext, because the timer list is fundamentally per clock-source.
So far. Once we support different handler thread for timers, there will be more lists than clock sources.
Right.
So my PATCHv4 series breaks up QEMUClock into QEMUClock and QEMUTimerList:
http://lists.nongnu.org/archive/html/qemu-devel/2013-07/msg04887.html
Unfortunately I've needed to keep qemu_new_timer AND qemu_new_timer_timerlist about to avoid horrendous git stats due to the consequent API changes (I did it, but you don't want to see it), and these have several variants. Apart from that it wasn't too bad.
Signature · 1 line
Alex Bligh
Am 26.07.2013 um 10:43 hat Stefan Hajnoczi geschrieben:
Quoted 20 lines · Alex Bligh
Quoted 13 lines
Quoted 8 lines · --On 25 July 2013 14:32:59 +0200 Jan Kiszka <[email protected]> wrote:
Quoted 2 lines
I would happily at a QEMUClock of each type to AioContext. They are after all pretty lightweight.
What's the point of adding tones of QEMUClock instances? Considering proper abstraction, how are they different for each AioContext? Will they run against different clock sources, start/stop at different times? If the answer is "they have different timer list", then fix this incorrect abstraction.
Quoted 5 lines
Even if I fix the abstraction, there is a question of whether it is necessary to have more than one timer list per AioContext, because the timer list is fundamentally per clock-source. I am currently just using
QEMU_CLOCK_REALTIMEas that's what the block drivers normally want. Will block drivers ever want timers from a different clock source?
Quoted 8 lines
block.candblock/qed.cusevm_clockbecause block drivers should not do guest I/O while the vm is stopped. This is especially true during live migration where it's important to hand off the image file from the source host to the destination host with good cache consistency. The source host is not allowed to modify the image file anymore once the destination host has resumed the guest.Block jobs use
rt_clockbecause they aren't considered guest I/O.
But considering your first paragraph, why is it safe to let block jobs running while we're migrating? Do we really do that? It sounds unsafe to me.
Kevin
Quoted 3 lines · --On 29 July 2013 10:58:40 +0200 Kevin Wolf <[email protected]> wrote:
But considering your first paragraph, why is it safe to let block jobs running while we're migrating? Do we really do that? It sounds unsafe to me.
If I remember right, the sending end now does the bdrv_close before the receiving end does the bdrv_open, and that's right at the end of migration. It seems to me it should be safe to do block jobs right up to the bdrv_close.
Signature · 1 line
Alex Bligh
Il 29/07/2013 10:58, Kevin Wolf ha scritto:
Quoted 28 lines
Am 26.07.2013 um 10:43 hat Stefan Hajnoczi geschrieben:
Quoted 27 lines · Alex Bligh
Quoted 13 lines
Quoted 8 lines · --On 25 July 2013 14:32:59 +0200 Jan Kiszka <[email protected]> wrote:
Quoted 2 lines
I would happily at a QEMUClock of each type to AioContext. They are after all pretty lightweight.
What's the point of adding tones of QEMUClock instances? Considering proper abstraction, how are they different for each AioContext? Will they run against different clock sources, start/stop at different times? If the answer is "they have different timer list", then fix this incorrect abstraction.
Quoted 5 lines
Even if I fix the abstraction, there is a question of whether it is necessary to have more than one timer list per AioContext, because the timer list is fundamentally per clock-source. I am currently just using
QEMU_CLOCK_REALTIMEas that's what the block drivers normally want. Will block drivers ever want timers from a different clock source?
block.candblock/qed.cusevm_clockbecause block drivers should not do guest I/O while the vm is stopped. This is especially true during live migration where it's important to hand off the image file from the source host to the destination host with good cache consistency. The source host is not allowed to modify the image file anymore once the destination host has resumed the guest.
Quoted 5 lines
Quoted 1 line
Block jobs use
rt_clockbecause they aren't considered guest I/O.But considering your first paragraph, why is it safe to let block jobs running while we're migrating? Do we really do that? It sounds unsafe to me.
I think we should cancel them (synchronously) before the final bdrv_drain_all().
Paolo
Quoted 28 lines · Kevin Wolf
Am 26.07.2013 um 10:43 hat Stefan Hajnoczi geschrieben:
Quoted 27 lines · Alex Bligh
Quoted 13 lines
Quoted 8 lines · --On 25 July 2013 14:32:59 +0200 Jan Kiszka <[email protected]> wrote:
Quoted 2 lines
I would happily at a QEMUClock of each type to AioContext. They are after all pretty lightweight.
What's the point of adding tones of QEMUClock instances? Considering proper abstraction, how are they different for each AioContext? Will they run against different clock sources, start/stop at different times? If the answer is "they have different timer list", then fix this incorrect abstraction.
Quoted 5 lines
Even if I fix the abstraction, there is a question of whether it is necessary to have more than one timer list per AioContext, because the timer list is fundamentally per clock-source. I am currently just using
QEMU_CLOCK_REALTIMEas that's what the block drivers normally want. Will block drivers ever want timers from a different clock source?
block.candblock/qed.cusevm_clockbecause block drivers should not do guest I/O while the vm is stopped. This is especially true during live migration where it's important to hand off the image file from the source host to the destination host with good cache consistency. The source host is not allowed to modify the image file anymore once the destination host has resumed the guest.
Quoted 5 lines
Quoted 1 line
Block jobs use
rt_clockbecause they aren't considered guest I/O.But considering your first paragraph, why is it safe to let block jobs running while we're migrating? Do we really do that? It sounds unsafe to me.
It is not safe:
1. Block jobs are not migration-aware and it therefore does not make sense to run them across live migration.
2. Running block jobs may modify the image file after the destination host has resumed the guest.
We simply forgot to add the check. I'll try to send a patch for this today.
Stefan