arm AioContext with its own timer stuff
v2 cover arm AioContext with its own timer stuff
summary of the model:
Three qemu-wide clock source allowed in system. And each AioContext has
three corresponding timer list to run timer against clocksource.
rfcv2:
drop patches about alarm-timer(if timeout of poll will not satisfy, will come back to it) fix qemu_clock_enable sync problem (Thanks for Jan and Stefan)
fix process=true when aio_poll run timers (Thanks for Alex)
Liu Ping Fan (5):
timer: protect timers_state with lock
timer: pick out timer list info from QemuClock
timer: make qemu_clock_enable sync between disable and timer's cb
timer: associate three timerlists with AioContext
timer: run timers on aio_poll
aio-posix.c | 2 + async.c | 9 +++ cpus.c | 26 ++++++-- include/block/aio.h | 13 ++++ include/qemu/timer.h | 24 ++++++- main-loop.c | 2 - qemu-timer.c | 184 ++++++++++++++++++++++++++++++++++++--------------- 7 files changed, 198 insertions(+), 62 deletions(-)
Signature · 1 line
1.8.1.4
RFC v2 1/5 timer: protect timers_state with lock
In 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 | 26 +++++++++++++++++++++----- 1 file changed, 21 insertions(+), 5 deletions(-)
cpus.c +21 -5
@@ -112,7 +112,9 @@ typedef struct TimersState {int64_t dummy; } TimersState; -TimersState timers_state; +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,14 @@ 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; }else {int64_t ticks; ticks =cpu_get_real_ticks ();
@@ -148,41 +153,51 @@ 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; } +qemu_mutex_unlock (&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 +368,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 v2 2/5 timer: pick out timer list info from QemuClock
In reply to Liu Ping FanIn qemu-wide, we will have three clocksource, they are present by rt_clock/vm_clock/host_clock. On the other hand, we want to run timers on the different backend threads by binding AioContext with its own three timer lists.
So clean up the QemuClock struct to use it as the clock source.
--- 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;
@@ -86,6 +92,13 @@ struct qemu_alarm_timer {static struct qemu_alarm_timer *alarm_timer; +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);
@@ -95,17 +108,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; }
@@ -244,16 +259,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; }
@@ -269,10 +293,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; }
@@ -280,11 +305,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); }
@@ -295,11 +321,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);
@@ -316,7 +343,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;
@@ -331,11 +358,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)
@@ -346,21 +373,21 @@ 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;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)) {
@@ -371,10 +398,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);
@@ -390,15 +417,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; }
@@ -411,22 +438,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 v2 3/5 timer: make qemu_clock_enable sync between disable and timer's cb
In reply to Liu Ping FanAfter disabling the QemuClock, we should make sure that no QemuTimers are still in flight. To implement that, the caller of disabling will wait until the last user's exit.
Note, the callers of qemu_clock_enable() should be sync by themselves, not protected by this patch.
--- qemu-timer.c | 34 ++++++++++++++++++++++++++++++++-- 1 file changed, 32 insertions(+), 2 deletions(-)
qemu-timer.c +32 -2
@@ -60,6 +60,14 @@ struct QEMUClock {int type;bool enabled; + +/* rule: Innermost lock +* to protect the disable against timer's cb in flight. +*/ +QemuMutex lock; +/* reference by timer list */ +int using; +QemuCond wait_using; };struct QEMUTimer {
@@ -274,6 +282,9 @@ static QEMUClock *qemu_new_clock(int type) clock->type = type; clock->enabled = true; clock->last =INT64_MIN ; + clock->using =0 ; +qemu_cond_init (&clock->wait_using); +qemu_mutex_init (&clock->lock);notifier_list_init (&clock->reset_notifiers); tlist =clock_to_timerlist (clock);timer_list_init (tlist);
@@ -287,6 +298,13 @@ void qemu_clock_enable(QEMUClock *clock, bool enabled) clock->enabled = enabled;if (enabled && !old) {qemu_rearm_alarm_timer (alarm_timer); + }else { +qemu_mutex_lock (&clock->lock); +/* Wait for cb in flight to terminate */ +while (atomic_read (clock->using)) { +qemu_cond_wait (&clock->wait_using, &clock->lock); + } +qemu_mutex_unlock (&clock->lock); } }
@@ -440,8 +458,11 @@ void qemu_run_timers(QEMUClock *clock)int64_t current_time;TimerList *tlist; -if (!clock->enabled) -return ; +atomic_inc (&clock->using); +if (unlikely (!clock->enabled)) { + goto exit; + } + current_time =qemu_get_clock_ns (clock); tlist =clock_to_timerlist (clock);
@@ -461,6 +482,15 @@ void qemu_run_timers(QEMUClock *clock)/* run the callback (the timer list can be modified) */ ts->cb (ts->opaque); } + +exit: +qemu_mutex_lock (&clock->lock); +if (atomic_fetch_dec (&clock->using) ==1 ) { +if (unlikely (!clock->enabled)) { +qemu_cond_signal (&clock->wait_using); + } + } +qemu_mutex_unlock (&clock->lock); }int64_t qemu_get_clock_ns (QEMUClock *clock)
Signature · 1 line
1.8.1.4
RFC v2 4/5 timer: associate three timerlists 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 based on different clocksource, we arm each AioContext with three timer lists in according to three clocksource (QemuClock).
A little later, we will 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().
--- async.c | 9 ++++++++ include/block/aio.h | 13 +++++++++++ include/qemu/timer.h | 20 ++++++++++++++++ qemu-timer.c | 65 +++++++++++++++++++++++++++++++--------------------- 4 files changed, 81 insertions(+), 26 deletions(-)
async.c +9 -0
@@ -201,11 +201,15 @@ 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); +for (i =0 ; i <QEMU_CLOCK_MAXCNT ; i++) { +timer_list_finalize (&ctx->timer_list[i]); + } }static GSourceFuncs aio_source_funcs = {
@@ -237,6 +241,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 ;
@@ -245,6 +251,9 @@ AioContext *aio_context_new(void)aio_set_event_notifier (ctx, &ctx->notifier, (EventNotifierHandler *) event_notifier_test_and_clear,NULL ); +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;
@@ -73,6 +85,7 @@ typedef struct AioContext {/* Thread pool for performing work and receiving completion callbacks */ struct ThreadPool *thread_pool; +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);
@@ -75,6 +83,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 +39 -26
177 lines not on this page · raw message
Signature · 1 line
1.8.1.4
RFC v2 5/5 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 ++ include/qemu/timer.h | 4 ++-- main-loop.c | 2 -- qemu-timer.c | 15 ++++++++++----- 4 files changed, 14 insertions(+), 9 deletions(-)
aio-posix.c +2 -0
@@ -191,6 +191,8 @@ bool aio_poll(AioContext *ctx, bool blocking) progress = true; } + progress |=qemu_run_all_timers (); +if (progress && !blocking) {return true; }
include/qemu/timer.h +2 -2
@@ -61,8 +61,8 @@ bool qemu_timer_pending(QEMUTimer *ts);bool qemu_timer_expired (QEMUTimer *timer_head,int64_t current_time);uint64_t qemu_timer_expire_time_ns (QEMUTimer *ts); -void qemu_run_timers (QEMUClock *clock); -void qemu_run_all_timers (void ); +bool qemu_run_timers (QEMUClock *clock); +bool qemu_run_all_timers (void );void configure_alarms (char const *opt);void init_clocks (void );int init_timer_alarm (void );
main-loop.c +0 -2
@@ -471,8 +471,6 @@ int main_loop_wait(int nonblocking)slirp_pollfds_poll (gpollfds, (ret <0 ));#endif -qemu_run_all_timers (); -return ret; }
qemu-timer.c +10 -5
@@ -463,12 +463,13 @@ bool qemu_timer_expired(QEMUTimer *timer_head, int64_t current_time)return qemu_timer_expired_ns (timer_head, current_time * timer_head->scale); } -void qemu_run_timers (QEMUClock *clock) +bool qemu_run_timers (QEMUClock *clock) {QEMUTimer *ts;int64_t current_time;TimerList *tlist;AioContext *ctx; +bool process = false;atomic_inc (&clock->using);if (unlikely (!clock->enabled)) {
@@ -494,6 +495,7 @@ void qemu_run_timers(QEMUClock *clock)/* run the callback (the timer list can be modified) */ ts->cb (ts->opaque); + process = true; } exit:
@@ -504,6 +506,7 @@ exit: } }qemu_mutex_unlock (&clock->lock); +return process; }int64_t qemu_get_clock_ns (QEMUClock *clock)
@@ -555,16 +558,17 @@ uint64_t qemu_timer_expire_time_ns(QEMUTimer *ts)return qemu_timer_pending (ts) ? ts->expire_time :-1 ; } -void qemu_run_all_timers (void ) +bool qemu_run_all_timers (void ) {bool timer_modified; +bool process = false; alarm_timer->pending = false;/* vm time timers */ -qemu_run_timers (vm_clock); -qemu_run_timers (rt_clock); -qemu_run_timers (host_clock); + process |=qemu_run_timers (vm_clock); + process |=qemu_run_timers (rt_clock); + process |=qemu_run_timers (host_clock);/* Check if qemu_mod_timer_ns() has been called */ qemu_mutex_lock (&alarm_timer->timer_modified_lock);
@@ -580,6 +584,7 @@ void qemu_run_all_timers(void)qemu_rearm_alarm_timer (alarm_timer); } +return process; }#ifdef _WIN32
Signature · 1 line
1.8.1.4
Re: [Qemu-devel] [RFC v2 1/5] timer: protect timers_state with lock
In reply to Liu Ping FanIl 29/07/2013 05:16, Liu Ping Fan ha scritto:
Quoted 7 lines
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"
This is quite expensive; on the other hand it is probably a good thing to do even without the other patches.
Can you use the seqlock primitive we initially worked on for the memory dispatch (with the BQL on the write side)?
Paolo
Quoted 100 lines
Signed-off-by Liu Ping Fan <[email protected]>--- cpus.c | 26 +++++++++++++++++++++----- 1 file changed, 21 insertions(+), 5 deletions(-)cpus.c +21
-5@@ -112,7 +112,9 @@ typedef struct TimersState {int64_t dummy; } TimersState; -TimersState timers_state; +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,14 @@ 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; }else {int64_t ticks; ticks =cpu_get_real_ticks ();@@ -148,41 +153,51 @@ 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; } +qemu_mutex_unlock (&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 +368,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 ;
Re: [Qemu-devel] [RFC v2 3/5] timer: make qemu_clock_enable sync between disable and timer's cb
In reply to Liu Ping FanIl 29/07/2013 05:16, Liu Ping Fan ha scritto:
Quoted 8 lines
After disabling the QemuClock, we should make sure that no QemuTimers are still in flight. To implement that, the caller of disabling will wait until the last user's exit.
Note, the callers of
qemu_clock_enable()should be sync by themselves, not protected by this patch.Signed-off-by Liu Ping Fan <[email protected]>
This is an interesting approach.
qemu-timer.c void qemu_run_timers(QEMUClock *clock) · line 464
Quoted 6 lines
qemu-timer.c +4
-2-if (!clock->enabled) -return ; +atomic_inc (&clock->using); +if (unlikely (!clock->enabled)) { + goto exit; + }
This can return directly, it doesn't need to increment and decrement clock->using.
Paolo
Quoted 20 lines
current_time = qemu_get_clock_ns(clock); tlist = clock_to_timerlist(clock);qemu-timer.c +9
-0@@ -461,6 +482,15 @@ void qemu_run_timers(QEMUClock *clock)/* run the callback (the timer list can be modified) */ ts->cb (ts->opaque); } + +exit: +qemu_mutex_lock (&clock->lock); +if (atomic_fetch_dec (&clock->using) ==1 ) { +if (unlikely (!clock->enabled)) { +qemu_cond_signal (&clock->wait_using); + } + } +qemu_mutex_unlock (&clock->lock); }int64_t qemu_get_clock_ns (QEMUClock *clock)
Re: [Qemu-devel] [RFC v2 4/5] timer: associate three timerlists with AioContext
In reply to Liu Ping FanIl 29/07/2013 05:16, Liu Ping Fan ha scritto:
Quoted 6 lines
Currently, 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 based on different clocksource, we arm each AioContext with three timer lists in according to three clocksource (QemuClock).
Quoted 7 lines
A little later, we will run timers in
aio_poll.Signed-off-by Liu Ping Fan <[email protected]>------ issue to fix ---
Note: before this patch, there should be another one to fix the race issue byqemu_mod_timer()and_run_timers().
Another issue is that deadline computation is not using the AioContext's timer lists.
Paolo
Quoted 321 lines
--- async.c | 9 ++++++++ include/block/aio.h | 13 +++++++++++ include/qemu/timer.h | 20 ++++++++++++++++ qemu-timer.c | 65 +++++++++++++++++++++++++++++++--------------------- 4 files changed, 81 insertions(+), 26 deletions(-)async.c +9
-0@@ -201,11 +201,15 @@ 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); +for (i =0 ; i <QEMU_CLOCK_MAXCNT ; i++) { +timer_list_finalize (&ctx->timer_list[i]); + } }static GSourceFuncs aio_source_funcs = {@@ -237,6 +241,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 ;@@ -245,6 +251,9 @@ AioContext *aio_context_new(void)aio_set_event_notifier (ctx, &ctx->notifier, (EventNotifierHandler *) event_notifier_test_and_clear,NULL ); +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;@@ -73,6 +85,7 @@ typedef struct AioContext {/* Thread pool for performing work and receiving completion callbacks */ struct ThreadPool *thread_pool; +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);@@ -75,6 +83,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 +39
-26@@ -45,14 +45,6 @@#define QEMU_CLOCK_REALTIME 0#define QEMU_CLOCK_VIRTUAL 1#define QEMU_CLOCK_HOST 2 -#define QEMU_CLOCK_MAXCNT 3 - -typedef struct TimerList { -QEMUTimer *active_timers; -QemuMutex active_timers_lock; -}TimerList ; - -static TimerList timer_list[QEMU_CLOCK_MAXCNT ];struct QEMUClock {NotifierList reset_notifiers;@@ -72,7 +64,9 @@ struct QEMUClock {struct QEMUTimer {int64_t expire_time;/* in nanoseconds */ +/* quick link to AioContext timer list */ TimerList *list; +AioContext *ctx;QEMUTimerCB *cb;void *opaque;QEMUTimer *next;@@ -100,11 +94,12 @@ struct qemu_alarm_timer {static struct qemu_alarm_timer *alarm_timer; -static TimerList *clock_to_timerlist (QEMUClock *clock) +static TimerList *clock_to_timerlist (QEMUClock *clock,AioContext *ctx) {int type = clock->type; -return &timer_list[type]; +assert (ctx); +return &ctx->timer_list[type]; }static bool qemu_timer_expired_ns (QEMUTimer *timer_head,int64_t current_time)@@ -112,7 +107,8 @@ static bool qemu_timer_expired_ns(QEMUTimer *timer_head, int64_t current_time)return timer_head && (timer_head->expire_time <= current_time); } -static int64_t qemu_next_clock_deadline (QEMUClock *clock,int64_t delta) +static int64_t qemu_next_clock_deadline (QEMUClock *clock,int64_t delta, +AioContext *ctx) {int64_t expire_time, next;bool has_timer = false;@@ -122,7 +118,7 @@ static int64_t qemu_next_clock_deadline(QEMUClock *clock, int64_t delta)return delta; } - tlist =clock_to_timerlist (clock); + tlist =clock_to_timerlist (clock, ctx);qemu_mutex_lock (&tlist->active_timers_lock);if (tlist->active_timers) { has_timer = true;@@ -140,12 +136,13 @@ static int64_t qemu_next_clock_deadline(QEMUClock *clock, int64_t delta)static int64_t qemu_next_alarm_deadline (void ) {int64_t delta =INT64_MAX ; +AioContext *ctx = *tls_get_thread_aio_context ();if (!use_icount) { - delta =qemu_next_clock_deadline (vm_clock, delta); + delta =qemu_next_clock_deadline (vm_clock, delta, ctx); } - delta =qemu_next_clock_deadline (host_clock, delta); -return qemu_next_clock_deadline (rt_clock, delta); + delta =qemu_next_clock_deadline (host_clock, delta, ctx); +return qemu_next_clock_deadline (rt_clock, delta, ctx); }static void qemu_rearm_alarm_timer (struct qemu_alarm_timer *t)@@ -267,16 +264,21 @@ QEMUClock *rt_clock;QEMUClock *vm_clock;QEMUClock *host_clock; -static void timer_list_init (TimerList *tlist) +void timer_list_init (TimerList *tlist) {qemu_mutex_init (&tlist->active_timers_lock); tlist->active_timers =NULL ; } +void timer_list_finalize (TimerList *tlist) +{ +qemu_mutex_destroy (&tlist->active_timers_lock); +assert (!tlist->active_timers); +} +static QEMUClock *qemu_new_clock (int type) {QEMUClock *clock; -TimerList *tlist; clock =g_malloc0 (sizeof (QEMUClock)); clock->type = type;@@ -286,8 +288,6 @@ static QEMUClock *qemu_new_clock(int type)qemu_cond_init (&clock->wait_using);qemu_mutex_init (&clock->lock);notifier_list_init (&clock->reset_notifiers); - tlist =clock_to_timerlist (clock); -timer_list_init (tlist);return clock; }@@ -308,10 +308,14 @@ void qemu_clock_enable(QEMUClock *clock, bool enabled) } } +/* 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) {bool has_timers; -TimerList *tlist =clock_to_timerlist (clock); +TimerList *tlist =clock_to_timerlist (clock,qemu_get_aio_context ());qemu_mutex_lock (&tlist->active_timers_lock); has_timers = !!tlist->active_timers;@@ -323,7 +327,7 @@ int64_t qemu_clock_expired(QEMUClock *clock) {bool has_timers;int64_t expire_time; -TimerList *tlist =clock_to_timerlist (clock); +TimerList *tlist =clock_to_timerlist (clock,qemu_get_aio_context ());qemu_mutex_lock (&tlist->active_timers_lock); has_timers = tlist->active_timers;@@ -339,7 +343,7 @@ 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); +TimerList *tlist =clock_to_timerlist (clock,qemu_get_aio_context ());qemu_mutex_lock (&tlist->active_timers_lock); has_timers = tlist->active_timers;@@ -355,19 +359,26 @@ int64_t qemu_clock_deadline(QEMUClock *clock)return delta; } -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) {QEMUTimer *ts; ts =g_malloc0 (sizeof (QEMUTimer)); - ts->list =clock_to_timerlist (clock); + ts->list =clock_to_timerlist (clock, ctx); ts->cb = cb; ts->opaque = opaque; ts->scale = scale; + ts->ctx = ctx;return ts; } +QEMUTimer *qemu_new_timer (QEMUClock *clock,int scale, +QEMUTimerCB *cb,void *opaque) +{ +return aioctx_new_timer (clock, scale, cb, opaque,qemu_get_aio_context ()); +} +void qemu_free_timer (QEMUTimer *ts) {g_free (ts);@@ -457,6 +468,7 @@ void qemu_run_timers(QEMUClock *clock)QEMUTimer *ts;int64_t current_time;TimerList *tlist; +AioContext *ctx;atomic_inc (&clock->using);if (unlikely (!clock->enabled)) {@@ -465,7 +477,8 @@ void qemu_run_timers(QEMUClock *clock) current_time =qemu_get_clock_ns (clock); - tlist =clock_to_timerlist (clock); + ctx = *tls_get_thread_aio_context (); + tlist =clock_to_timerlist (clock, ctx);for (;;) {qemu_mutex_lock (&tlist->active_timers_lock);
Re: [Qemu-devel] [RFC v2 1/5] timer: protect timers_state with lock
In reply to Paolo BonziniQuoted 7 lines · Paolo Bonzini
Il 29/07/2013 05:16, Liu Ping Fan ha scritto:
Quoted 6 lines
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.
Quoted 8 lines
Quoted 1 line
Lock rule: private lock innermost, ie BQL->"this lock"
This is quite expensive; on the other hand it is probably a good thing to do even without the other patches.
Can you use the seqlock primitive we initially worked on for the memory dispatch (with the BQL on the write side)?
Fine, of course, seqlock is a better one.
Thanks,
Pingfan
Quoted 103 lines
Paolo
Quoted 100 lines
Signed-off-by Liu Ping Fan <[email protected]>--- cpus.c | 26 +++++++++++++++++++++----- 1 file changed, 21 insertions(+), 5 deletions(-)cpus.c +21
-5@@ -112,7 +112,9 @@ typedef struct TimersState {int64_t dummy; } TimersState; -TimersState timers_state; +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,14 @@ 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; }else {int64_t ticks; ticks =cpu_get_real_ticks ();@@ -148,41 +153,51 @@ 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; } +qemu_mutex_unlock (&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 +368,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 ;
Re: [Qemu-devel] [RFC v2 3/5] timer: make qemu_clock_enable sync between disable and timer's cb
In reply to Paolo BonziniQuoted 19 lines · Paolo Bonzini
Il 29/07/2013 05:16, Liu Ping Fan ha scritto:
Quoted 8 lines
After disabling the QemuClock, we should make sure that no QemuTimers are still in flight. To implement that, the caller of disabling will wait until the last user's exit.
Note, the callers of
qemu_clock_enable()should be sync by themselves, not protected by this patch.Signed-off-by Liu Ping Fan <[email protected]>This is an interesting approach.
qemu-timer.c void qemu_run_timers(QEMUClock *clock) · line 464
Quoted 6 lines
qemu-timer.c +4
-2-if (!clock->enabled) -return ; +atomic_inc (&clock->using); +if (unlikely (!clock->enabled)) { + goto exit; + }
Quoted 3 lines
This can return directly, it doesn't need to increment and decrement
clock->using.
Here is a race condition like the following, so I swap the sequence
qemu_clock_enable(false) run timers
if(!clock->enabled) return;
------------> clock->enabled=false
if(atomic_read(using)==0)
atomic_inc(using)
Regards,
Pingfan
Quoted 23 lines
Paolo
Quoted 20 lines
current_time = qemu_get_clock_ns(clock); tlist = clock_to_timerlist(clock);qemu-timer.c +9
-0@@ -461,6 +482,15 @@ void qemu_run_timers(QEMUClock *clock)/* run the callback (the timer list can be modified) */ ts->cb (ts->opaque); } + +exit: +qemu_mutex_lock (&clock->lock); +if (atomic_fetch_dec (&clock->using) ==1 ) { +if (unlikely (!clock->enabled)) { +qemu_cond_signal (&clock->wait_using); + } + } +qemu_mutex_unlock (&clock->lock); }int64_t qemu_get_clock_ns (QEMUClock *clock)
Re: [Qemu-devel] [RFC v2 4/5] timer: associate three timerlists with AioContext
In reply to Paolo BonziniQuoted 9 lines · Paolo Bonzini
Il 29/07/2013 05:16, Liu Ping Fan ha scritto:
Quoted 8 lines
Currently, 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 based on different clocksource, we arm each AioContext with three timer lists in according to three clocksource (QemuClock).
A little later, we will run timers in
aio_poll.
Quoted 9 lines
Quoted 5 lines
Signed-off-by Liu Ping Fan <[email protected]>------ issue to fix ---
Note: before this patch, there should be another one to fix the race issue byqemu_mod_timer()and_run_timers().Another issue is that deadline computation is not using the AioContext's timer lists.
Sorry, can not catch the meaning. When AioContext has its own timer lists, it will have its own deadline(for ppoll timeout). So the computation should be based on AioContext's timer lists, right?
Thanks and regards,
Pingfan
Quoted 324 lines
Paolo
Quoted 321 lines
--- async.c | 9 ++++++++ include/block/aio.h | 13 +++++++++++ include/qemu/timer.h | 20 ++++++++++++++++ qemu-timer.c | 65 +++++++++++++++++++++++++++++++--------------------- 4 files changed, 81 insertions(+), 26 deletions(-)async.c +9
-0@@ -201,11 +201,15 @@ 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); +for (i =0 ; i <QEMU_CLOCK_MAXCNT ; i++) { +timer_list_finalize (&ctx->timer_list[i]); + } }static GSourceFuncs aio_source_funcs = {@@ -237,6 +241,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 ;@@ -245,6 +251,9 @@ AioContext *aio_context_new(void)aio_set_event_notifier (ctx, &ctx->notifier, (EventNotifierHandler *) event_notifier_test_and_clear,NULL ); +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;@@ -73,6 +85,7 @@ typedef struct AioContext {/* Thread pool for performing work and receiving completion callbacks */ struct ThreadPool *thread_pool; +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);@@ -75,6 +83,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 +39
-26@@ -45,14 +45,6 @@#define QEMU_CLOCK_REALTIME 0#define QEMU_CLOCK_VIRTUAL 1#define QEMU_CLOCK_HOST 2 -#define QEMU_CLOCK_MAXCNT 3 - -typedef struct TimerList { -QEMUTimer *active_timers; -QemuMutex active_timers_lock; -}TimerList ; - -static TimerList timer_list[QEMU_CLOCK_MAXCNT ];struct QEMUClock {NotifierList reset_notifiers;@@ -72,7 +64,9 @@ struct QEMUClock {struct QEMUTimer {int64_t expire_time;/* in nanoseconds */ +/* quick link to AioContext timer list */ TimerList *list; +AioContext *ctx;QEMUTimerCB *cb;void *opaque;QEMUTimer *next;@@ -100,11 +94,12 @@ struct qemu_alarm_timer {static struct qemu_alarm_timer *alarm_timer; -static TimerList *clock_to_timerlist (QEMUClock *clock) +static TimerList *clock_to_timerlist (QEMUClock *clock,AioContext *ctx) {int type = clock->type; -return &timer_list[type]; +assert (ctx); +return &ctx->timer_list[type]; }static bool qemu_timer_expired_ns (QEMUTimer *timer_head,int64_t current_time)@@ -112,7 +107,8 @@ static bool qemu_timer_expired_ns(QEMUTimer *timer_head, int64_t current_time)return timer_head && (timer_head->expire_time <= current_time); } -static int64_t qemu_next_clock_deadline (QEMUClock *clock,int64_t delta) +static int64_t qemu_next_clock_deadline (QEMUClock *clock,int64_t delta, +AioContext *ctx) {int64_t expire_time, next;bool has_timer = false;@@ -122,7 +118,7 @@ static int64_t qemu_next_clock_deadline(QEMUClock *clock, int64_t delta)return delta; } - tlist =clock_to_timerlist (clock); + tlist =clock_to_timerlist (clock, ctx);qemu_mutex_lock (&tlist->active_timers_lock);if (tlist->active_timers) { has_timer = true;@@ -140,12 +136,13 @@ static int64_t qemu_next_clock_deadline(QEMUClock *clock, int64_t delta)static int64_t qemu_next_alarm_deadline (void ) {int64_t delta =INT64_MAX ; +AioContext *ctx = *tls_get_thread_aio_context ();if (!use_icount) { - delta =qemu_next_clock_deadline (vm_clock, delta); + delta =qemu_next_clock_deadline (vm_clock, delta, ctx); } - delta =qemu_next_clock_deadline (host_clock, delta); -return qemu_next_clock_deadline (rt_clock, delta); + delta =qemu_next_clock_deadline (host_clock, delta, ctx); +return qemu_next_clock_deadline (rt_clock, delta, ctx); }static void qemu_rearm_alarm_timer (struct qemu_alarm_timer *t)@@ -267,16 +264,21 @@ QEMUClock *rt_clock;QEMUClock *vm_clock;QEMUClock *host_clock; -static void timer_list_init (TimerList *tlist) +void timer_list_init (TimerList *tlist) {qemu_mutex_init (&tlist->active_timers_lock); tlist->active_timers =NULL ; } +void timer_list_finalize (TimerList *tlist) +{ +qemu_mutex_destroy (&tlist->active_timers_lock); +assert (!tlist->active_timers); +} +static QEMUClock *qemu_new_clock (int type) {QEMUClock *clock; -TimerList *tlist; clock =g_malloc0 (sizeof (QEMUClock)); clock->type = type;@@ -286,8 +288,6 @@ static QEMUClock *qemu_new_clock(int type)qemu_cond_init (&clock->wait_using);qemu_mutex_init (&clock->lock);notifier_list_init (&clock->reset_notifiers); - tlist =clock_to_timerlist (clock); -timer_list_init (tlist);return clock; }@@ -308,10 +308,14 @@ void qemu_clock_enable(QEMUClock *clock, bool enabled) } } +/* 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) {bool has_timers; -TimerList *tlist =clock_to_timerlist (clock); +TimerList *tlist =clock_to_timerlist (clock,qemu_get_aio_context ());qemu_mutex_lock (&tlist->active_timers_lock); has_timers = !!tlist->active_timers;@@ -323,7 +327,7 @@ int64_t qemu_clock_expired(QEMUClock *clock) {bool has_timers;int64_t expire_time; -TimerList *tlist =clock_to_timerlist (clock); +TimerList *tlist =clock_to_timerlist (clock,qemu_get_aio_context ());qemu_mutex_lock (&tlist->active_timers_lock); has_timers = tlist->active_timers;@@ -339,7 +343,7 @@ 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); +TimerList *tlist =clock_to_timerlist (clock,qemu_get_aio_context ());qemu_mutex_lock (&tlist->active_timers_lock); has_timers = tlist->active_timers;@@ -355,19 +359,26 @@ int64_t qemu_clock_deadline(QEMUClock *clock)return delta; } -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) {QEMUTimer *ts; ts =g_malloc0 (sizeof (QEMUTimer)); - ts->list =clock_to_timerlist (clock); + ts->list =clock_to_timerlist (clock, ctx); ts->cb = cb; ts->opaque = opaque; ts->scale = scale; + ts->ctx = ctx;return ts; } +QEMUTimer *qemu_new_timer (QEMUClock *clock,int scale, +QEMUTimerCB *cb,void *opaque) +{ +return aioctx_new_timer (clock, scale, cb, opaque,qemu_get_aio_context ()); +} +void qemu_free_timer (QEMUTimer *ts) {g_free (ts);@@ -457,6 +468,7 @@ void qemu_run_timers(QEMUClock *clock)QEMUTimer *ts;int64_t current_time;TimerList *tlist; +AioContext *ctx;atomic_inc (&clock->using);if (unlikely (!clock->enabled)) {@@ -465,7 +477,8 @@ void qemu_run_timers(QEMUClock *clock) current_time =qemu_get_clock_ns (clock); - tlist =clock_to_timerlist (clock); + ctx = *tls_get_thread_aio_context (); + tlist =clock_to_timerlist (clock, ctx);for (;;) {qemu_mutex_lock (&tlist->active_timers_lock);
Quoted 16 lines · Liu Ping Fan
summary of the model:
Three qemu-wide clock source allowed in system. And each AioContext has
three corresponding timer list to run timer against clocksource.rfcv2:
drop patches about alarm-timer(if timeout of poll will not satisfy, will come back to it) fixqemu_clock_enablesync problem (Thanks for Jan and Stefan)
fix process=true whenaio_pollrun timers (Thanks for Alex)Liu Ping Fan (5):
timer: protecttimers_statewith lock
timer: pick out timer list info from QemuClock
timer: makeqemu_clock_enablesync between disable and timer's cb
timer: associate three timerlists with AioContext
timer: run timers onaio_poll
Quoted 8 lines
aio-posix.c | 2 + async.c | 9 +++ cpus.c | 26 ++++++-- include/block/aio.h | 13 ++++ include/qemu/timer.h | 24 ++++++- main-loop.c | 2 - qemu-timer.c | 184 ++++++++++++++++++++++++++++++++++++--------------- 7 files changed, 198 insertions(+), 62 deletions(-)
The potential for overlap with Alex Bligh's series is large. Can you base your patches on his v4?
It seems the difference is that you make clock sources to be available globally while Alex's series uses rt_clock (no synchronization necessary).
Stefan
Quoted 9 lines · --On 29 July 2013 11:16:03 +0800 Liu Ping Fan <[email protected]> wrote:
summary of the model:
Three qemu-wide clock source allowed in system. And each AioContext has
three corresponding timer list to run timer against clocksource.rfcv2:
drop patches about alarm-timer(if timeout of poll will not satisfy,
will come back to it) fixqemu_clock_enablesync problem (Thanks for
Jan and Stefan) fix process=true whenaio_pollrun timers (Thanks for
Alex)
This seems to have a considerable degree of overlap with my PATCHv4 where I split QEMUClock into QEMUClock and QEMUTimerList and then attached a QEMUTimerList to each AioContext.
Signature · 1 line
Alex Bligh
Quoted 6 lines · --On 29 July 2013 11:22:13 +0200 Stefan Hajnoczi <[email protected]> wrote:
The potential for overlap with Alex Bligh's series is large. Can you base your patches on his v4?
It seems the difference is that you make clock sources to be available globally while Alex's series uses
rt_clock(no synchronization necessary).
I should say PingFan has probably paid more attention to thread safety than me, as my work was intended to be applied before AioContexts were used by multiple threads.
Signature · 1 line
Alex Bligh
Re: [Qemu-devel] [RFC v2 3/5] timer: make qemu_clock_enable sync between disable and timer's cb
In reply to liu ping fanIl 29/07/2013 10:10, liu ping fan ha scritto:
Quoted 20 lines · Paolo Bonzini
Quoted 19 lines
Il 29/07/2013 05:16, Liu Ping Fan ha scritto:
Quoted 8 lines
After disabling the QemuClock, we should make sure that no QemuTimers are still in flight. To implement that, the caller of disabling will wait until the last user's exit.
Note, the callers of
qemu_clock_enable()should be sync by themselves, not protected by this patch.Signed-off-by Liu Ping Fan <[email protected]>This is an interesting approach.
Quoted 6 lines
qemu-timer.c +4
-2-if (!clock->enabled) -return ; +atomic_inc (&clock->using); +if (unlikely (!clock->enabled)) { + goto exit; + }
Quoted 4 lines
Quoted 3 lines
This can return directly, it doesn't need to increment and decrement
clock->using.Here is a race condition like the following
Ah, I see.
Still this seems a bit backwards. Most of the time you will have no one on the wait_using condvar, but you are almost always signaling the condvar (should be broadcast BTW)...
Paolo
Re: [Qemu-devel] [RFC v2 4/5] timer: associate three timerlists with AioContext
In reply to liu ping fanIl 29/07/2013 10:20, liu ping fan ha scritto:
Quoted 6 lines
Quoted 3 lines
Quoted 3 lines
Another issue is that deadline computation is not using the AioContext's timer lists.
Sorry, can not catch the meaning. When AioContext has its own timer lists, it will have its own deadline(for ppoll timeout). So the computation should be based on AioContext's timer lists, right?
Associating the main loop's timer lists to other AioContexts is really really wrong... It is _already_ wrong to run timers in the main AioContext's aio_poll (because timers may not be ready to run from an arbitrary aio_poll), it is even worse to run them in other threads without taking the BQL.
What exactly is the purpose of this series?
Paolo
Quoted 7 lines · Alex Bligh
Quoted 3 lines · --On 29 July 2013 11:22:13 +0200 Stefan Hajnoczi <[email protected]> wrote:
The potential for overlap with Alex Bligh's series is large. Can you base your patches on his v4?
Quoted 7 lines
Quoted 3 lines
It seems the difference is that you make clock sources to be available globally while Alex's series uses
rt_clock(no synchronization necessary).I should say PingFan has probably paid more attention to thread safety than me, as my work was intended to be applied before AioContexts were used by multiple threads.
Exactly. That's why I think these patches should be based on your series - they add the thread safety on top.
Stefan
Quoted 6 lines · --On 29 July 2013 15:36:03 +0200 Stefan Hajnoczi <[email protected]> wrote:
Quoted 3 lines
I should say PingFan has probably paid more attention to thread safety than me, as my work was intended to be applied before AioContexts were used by multiple threads.
Exactly. That's why I think these patches should be based on your series - they add the thread safety on top.
OK. I think I'm pretty much done with my series and have incorporated everyone's comments to date (save a couple I pushed back on of a minor nature and heard no response).
The only other thing I was thinking of was rather than each QemuTimerList holding an AioContext pointer so it knows it can use aio_notify, providing a callback for notify (this is on clock enable). I think that's probably polishing the ball though.
On the thread issue, for my own education, if an AioContext's timers only ever get called by that AioContext (and thus in the same thread), which is the case in PATCHv4, I don't think there is a thread safety beyond manipulation of the list of QemuTimerLists associated with each QEMUClock. That's only ever modified when the AioContext is created or deleted. This assumes that timers are always set from the same thread they are run (i.e. there is only one thread walking the QEMUTimerList).
Signature · 1 line
Alex Bligh
Re: [Qemu-devel] [RFC v2 4/5] timer: associate three timerlists with AioContext
In reply to Paolo BonziniQuoted 9 lines · Paolo Bonzini
Il 29/07/2013 10:20, liu ping fan ha scritto:
Quoted 6 lines
Quoted 3 lines
Quoted 3 lines
Another issue is that deadline computation is not using the AioContext's timer lists.
Sorry, can not catch the meaning. When AioContext has its own timer lists, it will have its own deadline(for ppoll timeout). So the computation should be based on AioContext's timer lists, right?
Associating the main loop's timer lists to other AioContexts is really
Main loop's timers will only be associated with the qemu_aio_context, not with other AioContext. (When creating timer, we have bound it with a specified AioContext, so it will only run on that AioContext)
Quoted 2 lines
really wrong... It is _already_ wrong to run timers in the main AioContext's
aio_poll(because timers may not be ready to run from an
Sorry, but except that timer cb will call aio_poll, any other reason ?
Quoted 3 lines
arbitrary
aio_poll), it is even worse to run them in other threads without taking the BQL.
Can be fixed by some method to sync the dedicated thread with vcpus?
Quoted 2 lines
What exactly is the purpose of this series?
Enable timers to run on the dedicated threads(using aio as the mini-loop), currently for the following devices,
virtio-blk-dataplane for throttling
hpet for the best-effort resolution
Thanks and regards,
Pingfan
Quoted 1 line
Paolo
Re: [Qemu-devel] [RFC v2 3/5] timer: make qemu_clock_enable sync between disable and timer's cb
In reply to Paolo BonziniQuoted 24 lines · Paolo Bonzini
Il 29/07/2013 10:10, liu ping fan ha scritto:
Quoted 23 lines · Paolo Bonzini
Quoted 22 lines
Il 29/07/2013 05:16, Liu Ping Fan ha scritto:
Quoted 8 lines
After disabling the QemuClock, we should make sure that no QemuTimers are still in flight. To implement that, the caller of disabling will wait until the last user's exit.
Note, the callers of
qemu_clock_enable()should be sync by themselves, not protected by this patch.Signed-off-by Liu Ping Fan <[email protected]>This is an interesting approach.
qemu-timer.c void qemu_run_timers(QEMUClock *clock) · line 464
Quoted 6 lines
qemu-timer.c +4
-2-if (!clock->enabled) -return ; +atomic_inc (&clock->using); +if (unlikely (!clock->enabled)) { + goto exit; + }This can return directly, it doesn't need to increment and decrement
clock->using.
Quoted 8 lines
Quoted 1 line
Here is a race condition like the following
Ah, I see.
Still this seems a bit backwards. Most of the time you will have no one on the
wait_usingcondvar, but you are almost always signaling the condvar (should be broadcast BTW)...
I have tried to filter out the normal case by
+ qemu_mutex_lock(&clock->lock);
+ if (atomic_fetch_dec(&clock->using) == 1) { ---------> 1st place
+ if (unlikely(!clock->enabled)) { -------> 2nd place
+ qemu_cond_signal(&clock->wait_using);
+ }
+ }
+ qemu_mutex_unlock(&clock->lock);
Could I do it better?
Thanks,
Pingfan
Quoted 26 lines · Stefan Hajnoczi
Quoted 16 lines · Liu Ping Fan
summary of the model:
Three qemu-wide clock source allowed in system. And each AioContext has
three corresponding timer list to run timer against clocksource.rfcv2:
drop patches about alarm-timer(if timeout of poll will not satisfy, will come back to it) fixqemu_clock_enablesync problem (Thanks for Jan and Stefan)
fix process=true whenaio_pollrun timers (Thanks for Alex)Liu Ping Fan (5):
timer: protecttimers_statewith lock
timer: pick out timer list info from QemuClock
timer: makeqemu_clock_enablesync between disable and timer's cb
timer: associate three timerlists with AioContext
timer: run timers onaio_pollQuoted 8 lines
aio-posix.c | 2 + async.c | 9 +++ cpus.c | 26 ++++++-- include/block/aio.h | 13 ++++ include/qemu/timer.h | 24 ++++++- main-loop.c | 2 - qemu-timer.c | 184 ++++++++++++++++++++++++++++++++++++--------------- 7 files changed, 198 insertions(+), 62 deletions(-)
Quoted 3 lines
The potential for overlap with Alex Bligh's series is large. Can you base your patches on his v4?
Re-check the patches and Alex' patches can meet my need for further step to make hpet run in dedicated thread. So I will rebase the sync part of my patches onto his.
Quoted 5 lines
It seems the difference is that you make clock sources to be available globally while Alex's series uses
rt_clock(no synchronization necessary).Stefan
Re: [Qemu-devel] [RFC v2 3/5] timer: make qemu_clock_enable sync between disable and timer's cb
In reply to liu ping fanIl 30/07/2013 04:42, liu ping fan ha scritto:
Quoted 42 lines · Paolo Bonzini
Quoted 24 lines
Il 29/07/2013 10:10, liu ping fan ha scritto:
Quoted 23 lines · Paolo Bonzini
Quoted 22 lines
Il 29/07/2013 05:16, Liu Ping Fan ha scritto:
Quoted 8 lines
After disabling the QemuClock, we should make sure that no QemuTimers are still in flight. To implement that, the caller of disabling will wait until the last user's exit.
Note, the callers of
qemu_clock_enable()should be sync by themselves, not protected by this patch.Signed-off-by Liu Ping Fan <[email protected]>This is an interesting approach.
qemu-timer.c void qemu_run_timers(QEMUClock *clock) · line 464
Quoted 6 lines
qemu-timer.c +4
-2-if (!clock->enabled) -return ; +atomic_inc (&clock->using); +if (unlikely (!clock->enabled)) { + goto exit; + }This can return directly, it doesn't need to increment and decrement
clock->using.Quoted 8 lines
Quoted 1 line
Here is a race condition like the following
Ah, I see.
Still this seems a bit backwards. Most of the time you will have no one on the
wait_usingcondvar, but you are almost always signaling the condvar (should be broadcast BTW)...I have tried to filter out the normal case by
qemu-timer.c +7
-0+qemu_mutex_lock (&clock->lock); + if (atomic_fetch_dec(&clock->using) == 1) { ---------> 1st place + if (unlikely(!clock->enabled)) { -------> 2nd place + qemu_cond_signal(&clock->wait_using); + } + } +qemu_mutex_unlock (&clock->lock);
Quoted 1 line
Could I do it better?
Hmm, do we even need clock->using at this point? For example:
qemu_clock_enable()
{
clock->enabled = enabled;
...
if (!enabled) {
/* If another thread is within qemu_run_timers,
* wait for it to finish.
*/
qemu_event_wait(&clock->callbacks_done_event);
}
}
qemu_run_timers()
{
qemu_event_reset(&clock->callbacks_done_event);
if (!clock->enabled) {
goto out;
}
...
out:
qemu_event_set(&eclock->callbacks_done_event);
}In the fast path this only does two atomic operations (an OR for reset, and XCHG for set).
Paolo
Re: [Qemu-devel] [RFC v2 3/5] timer: make qemu_clock_enable sync between disable and timer's cb
Paolo,
Quoted 25 lines · --On 30 July 2013 11:17:05 +0200 Paolo Bonzini <[email protected]> wrote:
Hmm, do we even need
clock->usingat this point? For example:qemu_clock_enable() { clock->enabled = enabled; ... if (!enabled) { /* If another thread is within qemu_run_timers, * wait for it to finish. */ qemu_event_wait(&clock->callbacks_done_event); } } qemu_run_timers() { qemu_event_reset(&clock->callbacks_done_event); if (!clock->enabled) { goto out; } ... out: qemu_event_set(&eclock->callbacks_done_event); }
Quoted 2 lines
In the fast path this only does two atomic operations (an OR for reset, and XCHG for set).
I think I'm missing something here. If Pingfan is rebasing on top of my patches, or is doing vaguely the same thing, then qemu_clock_enable will do two things:
1. It will will walk the list of QEMUTimerLists
2. For each QemuTimerList associated with the clock, it will call qemu_notify or aio_notify
The list of QEMUTimerLists is only accessed by qemu_clock_enable (to do task 2) and by the creation and deletion of QEMUTimerLists, which happen only in init and AioContext create/delete (which should be very rare). I'm presuming qemu_clock_enable(false) is also pretty rare. It seems to me there is no need to do anything more complex than a simple mutex protecting the list of QEMUTimerLists of each QEMUClock.
As far as walking the QEMUTimerList itself is concerned, this is something which is 99.999% done by the thread owning the AioContext. qemu_clock_enable should not even be walking this list. So I don't see why the protection here is needed.
Signature · 1 line
Alex Bligh
Re: [Qemu-devel] [RFC v2 3/5] timer: make qemu_clock_enable sync between disable and timer's cb
Il 30/07/2013 11:51, Alex Bligh ha scritto:
Quoted 4 lines
As far as walking the QEMUTimerList itself is concerned, this is something which is 99.999% done by the thread owning the AioContext.
qemu_clock_enableshould not even be walking this list. So I don't see why the protection here is needed.
The protection is needed not because of qemu_clock_enable, but rather because of code in qemu_clock_enable's caller. Such code likely expects not to run concurrently with timers.
qemu_clock_enable however can be called from other threads than the one owning the AioContext. Furthermore, it can happen while timer callbacks are being called, because callbacks are called without holding any lock.
If you put together these conditions, qemu_clock_enable has to wait for timer callbacks to finish running before returning.
Paolo
Re: [Qemu-devel] [RFC v2 3/5] timer: make qemu_clock_enable sync between disable and timer's cb
In reply to Paolo BonziniQuoted 70 lines · Paolo Bonzini
Il 30/07/2013 04:42, liu ping fan ha scritto:
Quoted 42 lines · Paolo Bonzini
Quoted 24 lines
Il 29/07/2013 10:10, liu ping fan ha scritto:
Quoted 23 lines · Paolo Bonzini
Quoted 22 lines
Il 29/07/2013 05:16, Liu Ping Fan ha scritto:
Quoted 8 lines
After disabling the QemuClock, we should make sure that no QemuTimers are still in flight. To implement that, the caller of disabling will wait until the last user's exit.
Note, the callers of
qemu_clock_enable()should be sync by themselves, not protected by this patch.Signed-off-by Liu Ping Fan <[email protected]>This is an interesting approach.
qemu-timer.c void qemu_run_timers(QEMUClock *clock) · line 464
Quoted 6 lines
qemu-timer.c +4
-2-if (!clock->enabled) -return ; +atomic_inc (&clock->using); +if (unlikely (!clock->enabled)) { + goto exit; + }This can return directly, it doesn't need to increment and decrement
clock->using.Quoted 8 lines
Quoted 1 line
Here is a race condition like the following
Ah, I see.
Still this seems a bit backwards. Most of the time you will have no one on the
wait_usingcondvar, but you are almost always signaling the condvar (should be broadcast BTW)...I have tried to filter out the normal case by
qemu-timer.c +7
-0+qemu_mutex_lock (&clock->lock); + if (atomic_fetch_dec(&clock->using) == 1) { ---------> 1st place + if (unlikely(!clock->enabled)) { -------> 2nd place + qemu_cond_signal(&clock->wait_using); + } + } +qemu_mutex_unlock (&clock->lock);Quoted 1 line
Could I do it better?
Hmm, do we even need
clock->usingat this point? For example:qemu_clock_enable() { clock->enabled = enabled; ... if (!enabled) { /* If another thread is within qemu_run_timers, * wait for it to finish. */ qemu_event_wait(&clock->callbacks_done_event); } } qemu_run_timers() { qemu_event_reset(&clock->callbacks_done_event); if (!clock->enabled) { goto out; } ... out: qemu_event_set(&eclock->callbacks_done_event); }
Quoted 3 lines
In the fast path this only does two atomic operations (an OR for reset, and XCHG for set).
There is race condition, suppose the following scenario with A/B thread
A: qemu_event_reset()
B: qemu_event_reset()
A: qemu_event_set() ----> B is still in flight when qemu_clock_enable() is notified
B: qemu_event_set()
I had tried to build something around futex(2) like qemu_event, but failed.
Thanks and regards,
Pingfan
Quoted 1 line
Paolo
Re: [Qemu-devel] [RFC v2 3/5] timer: make qemu_clock_enable sync between disable and timer's cb
Quoted 28 lines
Quoted 25 lines
Hmm, do we even need
clock->usingat this point? For example:qemu_clock_enable() { clock->enabled = enabled; ... if (!enabled) { /* If another thread is within qemu_run_timers, * wait for it to finish. */ qemu_event_wait(&clock->callbacks_done_event); } } qemu_run_timers() { qemu_event_reset(&clock->callbacks_done_event); if (!clock->enabled) { goto out; } ... out: qemu_event_set(&eclock->callbacks_done_event); }Quoted 3 lines
In the fast path this only does two atomic operations (an OR for reset, and XCHG for set).
Quoted 8 lines
There is race condition, suppose the following scenario with A/B thread
A:qemu_event_reset()
B:qemu_event_reset()
A:qemu_event_set()----> B is still in flight whenqemu_clock_enable()is notified
B:qemu_event_set()I had tried to build something around futex(2) like
qemu_event, but failed.
True, qemu_event basically works only when a single thread resets it. But there is no race condition here because qemu_run_timers cannot be executed concurrently by multiple threads (like aio_poll in your bottom half patches).
Paolo
Re: [Qemu-devel] [RFC v2 3/5] timer: make qemu_clock_enable sync between disable and timer's cb
Quoted 4 lines · --On 1 August 2013 04:57:52 -0400 Paolo Bonzini <[email protected]> wrote:
True,
qemu_eventbasically works only when a single thread resets it. But there is no race condition here becauseqemu_run_timerscannot be executed concurrently by multiple threads (likeaio_pollin your bottom half patches).
... or, if rebasing on top of my patches, qemu_run_timers *can* be executed concurrently by mulitple threads, but in respect of any given QEMUTimerList, it will only be executed by one thread.
Signature · 1 line
Alex Bligh
Re: [Qemu-devel] [RFC v2 3/5] timer: make qemu_clock_enable sync between disable and timer's cb
Quoted 8 lines
Quoted 4 lines
True,
qemu_eventbasically works only when a single thread resets it. But there is no race condition here becauseqemu_run_timerscannot be executed concurrently by multiple threads (likeaio_pollin your bottom half patches).... or, if rebasing on top of my patches,
qemu_run_timers*can* be executed concurrently by mulitple threads, but in respect of any given QEMUTimerList, it will only be executed by one thread.
... so the event would be placed in QEMUTimerList, not QEMUClock.
qemu_clock_enable then will have to visit all timer lists. That's not hard to do, but as locks proliferate we need to have a clear policy (e.g. BQL -> clock -> timerlist).
So actually there is another problem with this patch (both the condvar and the event approach are equally buggy). If a timer on clock X disables clock X, qemu_clock_enable will deadlock.
I suppose it's easier to ask each user of qemu_clock_enable to provide its own synchronization, and drop this patch. The simplest kind of synchronization is to delay some work to a bottom half in the clock's AioContext. If you do that, you know that the timers are not running.
Ping Fan, this teaches once more the same lesson: let's not invent complex concurrency mechanisms unless really necessary. So far they almost never survived (there are some exceptions, but we have always taken them from somewhere else: QemuEvent is an abstraction of liburcu code to make it portable, RCU and seqlock are from Linux, and I cannot think of anything else).
Paolo
Re: [Qemu-devel] [RFC v2 3/5] timer: make qemu_clock_enable sync between disable and timer's cb
Paolo,
Quoted 10 lines · --On 1 August 2013 08:19:34 -0400 Paolo Bonzini <[email protected]> wrote:
Quoted 8 lines
Quoted 4 lines
True,
qemu_eventbasically works only when a single thread resets it. But there is no race condition here becauseqemu_run_timerscannot be executed concurrently by multiple threads (likeaio_pollin your bottom half patches).... or, if rebasing on top of my patches,
qemu_run_timers*can* be executed concurrently by mulitple threads, but in respect of any given QEMUTimerList, it will only be executed by one thread.... so the event would be placed in QEMUTimerList, not QEMUClock.
Correct
Quoted 2 lines
qemu_clock_enablethen will have to visit all timer lists. That's not hard to do,
Correct, v4 of my patch does this.
Quoted 2 lines
but as locks proliferate we need to have a clear
policy (e.g. BQL -> clock -> timerlist).
But doesn't do the locking bit yet (Pingfan is going to do that I hope)
Quoted 3 lines
So actually there is another problem with this patch (both the condvar and the event approach are equally buggy). If a timer on clock X disables clock X,
qemu_clock_enablewill deadlock.
Yes. I believe there will be a similar problem if a timer created or deleted an AioContext (clearly less likely!)
Quoted 5 lines
I suppose it's easier to ask each user of
qemu_clock_enableto provide its own synchronization, and drop this patch. The simplest kind of synchronization is to delay some work to a bottom half in the clock's AioContext. If you do that, you know that the timers are not running.
I'm not sure that's true. If two AioContexts run in different threads, would their BH's and timers not also run in those two different threads?
Signature · 1 line
Alex Bligh
Re: [Qemu-devel] [RFC v2 3/5] timer: make qemu_clock_enable sync between disable and timer's cb
Quoted 13 lines · Alex Bligh
Quoted 3 lines
So actually there is another problem with this patch (both the condvar and the event approach are equally buggy). If a timer on clock X disables clock X,
qemu_clock_enablewill deadlock.Yes. I believe there will be a similar problem if a timer created or deleted an AioContext (clearly less likely!)
Quoted 5 lines
I suppose it's easier to ask each user of
qemu_clock_enableto provide its own synchronization, and drop this patch. The simplest kind of synchronization is to delay some work to a bottom half in the clock's AioContext. If you do that, you know that the timers are not running.
Quoted 3 lines
I'm not sure that's true. If two AioContexts run in different threads, would their BH's and timers not also run in those two different threads?
Suppose a thread wants to do qemu_clock_enable(foo, false), and the code after qemu_clock_enable assumes that no timers are running. Then you should move that code to a bottom half in foo's AioContext.
Paolo
Re: [Qemu-devel] [RFC v2 3/5] timer: make qemu_clock_enable sync between disable and timer's cb
Paolo,
Quoted 13 lines · --On 1 August 2013 15:51:11 +0200 Paolo Bonzini <[email protected]> wrote:
Quoted 13 lines
Quoted 3 lines
So actually there is another problem with this patch (both the condvar and the event approach are equally buggy). If a timer on clock X disables clock X,
qemu_clock_enablewill deadlock.Yes. I believe there will be a similar problem if a timer created or deleted an AioContext (clearly less likely!)
Quoted 5 lines
I suppose it's easier to ask each user of
qemu_clock_enableto provide its own synchronization, and drop this patch. The simplest kind of synchronization is to delay some work to a bottom half in the clock's AioContext. If you do that, you know that the timers are not running.
Quoted 7 lines
Quoted 3 lines
I'm not sure that's true. If two AioContexts run in different threads, would their BH's and timers not also run in those two different threads?
Suppose a thread wants to do
qemu_clock_enable(foo, false), and the code afterqemu_clock_enableassumes that no timers are running. Then you should move that code to a bottom half in foo's AioContext.
foo is a QEMUClock here.
A QEMUClock may not have just one AioContext. It could have several each operated by a different thread.
Signature · 1 line
Alex Bligh
Re: [Qemu-devel] [RFC v2 3/5] timer: make qemu_clock_enable sync between disable and timer's cb
Quoted 21 lines · Alex Bligh
Paolo,
Quoted 17 lines · --On 1 August 2013 15:51:11 +0200 Paolo Bonzini <[email protected]> wrote:
Quoted 16 lines
Quoted 3 lines
So actually there is another problem with this patch (both the condvar and the event approach are equally buggy). If a timer on clock X disables clock X,
qemu_clock_enablewill deadlock.Yes. I believe there will be a similar problem if a timer created or deleted an AioContext (clearly less likely!)
Quoted 5 lines
I suppose it's easier to ask each user of
qemu_clock_enableto provide its own synchronization, and drop this patch. The simplest kind of synchronization is to delay some work to a bottom half in the clock's AioContext. If you do that, you know that the timers are not running.I'm not sure that's true. If two AioContexts run in different threads, would their BH's and timers not also run in those two different threads?
Quoted 8 lines
Quoted 3 lines
Suppose a thread wants to do
qemu_clock_enable(foo, false), and the code afterqemu_clock_enableassumes that no timers are running. Then you should move that code to a bottom half in foo's AioContext.foo is a QEMUClock here.
A QEMUClock may not have just one AioContext. It could have several each operated by a different thread.
Oops, you're right.
Checking the code for callers of qemu_clock_enable() it seems like there shouldn't be any deadlocks. So it should work if qemu_clock_enable walks all of the timerlists and waits on each event.
But we should document the expectations since they are different from the current code.
Paolo
Re: [Qemu-devel] [RFC v2 3/5] timer: make qemu_clock_enable sync between disable and timer's cb
Quoted 28 lines · Paolo Bonzini
Quoted 27 lines · Alex Bligh
Paolo,
Quoted 13 lines · --On 1 August 2013 15:51:11 +0200 Paolo Bonzini <[email protected]> wrote:
Quoted 13 lines
Quoted 3 lines
So actually there is another problem with this patch (both the condvar and the event approach are equally buggy). If a timer on clock X disables clock X,
qemu_clock_enablewill deadlock.Yes. I believe there will be a similar problem if a timer created or deleted an AioContext (clearly less likely!)
Quoted 5 lines
I suppose it's easier to ask each user of
qemu_clock_enableto provide its own synchronization, and drop this patch. The simplest kind of synchronization is to delay some work to a bottom half in the clock's AioContext. If you do that, you know that the timers are not running.Quoted 7 lines
Quoted 3 lines
I'm not sure that's true. If two AioContexts run in different threads, would their BH's and timers not also run in those two different threads?
Suppose a thread wants to do
qemu_clock_enable(foo, false), and the code afterqemu_clock_enableassumes that no timers are running. Then you should move that code to a bottom half in foo's AioContext.foo is a QEMUClock here.
Quoted 7 lines
Quoted 2 lines
A QEMUClock may not have just one AioContext. It could have several each operated by a different thread.
Oops, you're right.
Checking the code for callers of
qemu_clock_enable()it seems like there shouldn't be any deadlocks. So it should work ifqemu_clock_enable
Do you mean the caller of qemu_clock_enable(foo,false) can NOT be timer cb? As for this deadlock issue, making qemu_clock_enabe(foo,false) ASYNC, and forcing the caller to sync explicitly ?
Quoted 7 lines
walks all of the timerlists and waits on each event.
But we should document the expectations since they are different from the current code.
Paolo
Re: [Qemu-devel] [RFC v2 3/5] timer: make qemu_clock_enable sync between disable and timer's cb
In reply to Alex BlighQuoted 25 lines · Alex Bligh
Paolo,
Quoted 11 lines · --On 1 August 2013 08:19:34 -0400 Paolo Bonzini <[email protected]> wrote:
Quoted 8 lines
Quoted 4 lines
True,
qemu_eventbasically works only when a single thread resets it. But there is no race condition here becauseqemu_run_timerscannot be executed concurrently by multiple threads (likeaio_pollin your bottom half patches).... or, if rebasing on top of my patches,
qemu_run_timers*can* be executed concurrently by mulitple threads, but in respect of any given QEMUTimerList, it will only be executed by one thread.... so the event would be placed in QEMUTimerList, not QEMUClock.
Correct
Quoted 2 lines
qemu_clock_enablethen will have to visit all timer lists. That's not hard to do,
Quoted 9 lines
Correct, v4 of my patch does this.
Quoted 2 lines
but as locks proliferate we need to have a clear
policy (e.g. BQL -> clock -> timerlist).But doesn't do the locking bit yet (Pingfan is going to do that I hope)
Seem that Stefanha had been involved in this, and sent out his patches :)
Quoted 23 lines
Quoted 4 lines
So actually there is another problem with this patch (both the condvar and the event approach are equally buggy). If a timer on clock X disables clock X,
qemu_clock_enablewill deadlock.Yes. I believe there will be a similar problem if a timer created or deleted an AioContext (clearly less likely!)
Quoted 5 lines
I suppose it's easier to ask each user of
qemu_clock_enableto provide its own synchronization, and drop this patch. The simplest kind of synchronization is to delay some work to a bottom half in the clock's AioContext. If you do that, you know that the timers are not running.I'm not sure that's true. If two AioContexts run in different threads, would their BH's and timers not also run in those two different threads?
--
Alex Bligh
Re: [Qemu-devel] [RFC v2 3/5] timer: make qemu_clock_enable sync between disable and timer's cb
In reply to liu ping fanQuoted 10 lines · liu ping fan
Quoted 6 lines · Paolo Bonzini
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
So actually there is another problem with this patch (both the condvar and the event approach are equally buggy). If a timer on clock X disables clock X,
qemu_clock_enablewill deadlock.Checking the code for callers of
qemu_clock_enable()it seems like there shouldn't be any deadlocks. So it should work ifqemu_clock_enableDo you mean the caller of
qemu_clock_enable(foo,false)can NOT be timer cb?
Yes.
Quoted 3 lines
As for this deadlock issue, making
qemu_clock_enable(foo,false)ASYNC, and forcing the caller to sync explicitly ?
Right now the callers of qemu_clock_enable(foo, false) are safe. So the problem can be "fixed" just by adequate documentation.
Quoted 4 lines
Quoted 4 lines
But we should document the expectations since they are different from the current code.
Paolo
Re: [Qemu-devel] [RFC v2 3/5] timer: make qemu_clock_enable sync between disable and timer's cb
In reply to liu ping fanQuoted 29 lines · liu ping fan
Quoted 28 lines · Alex Bligh
Paolo,
Quoted 11 lines · --On 1 August 2013 08:19:34 -0400 Paolo Bonzini <[email protected]> wrote:
Quoted 8 lines
Quoted 4 lines
True,
qemu_eventbasically works only when a single thread resets it. But there is no race condition here becauseqemu_run_timerscannot be executed concurrently by multiple threads (likeaio_pollin your bottom half patches).... or, if rebasing on top of my patches,
qemu_run_timers*can* be executed concurrently by mulitple threads, but in respect of any given QEMUTimerList, it will only be executed by one thread.... so the event would be placed in QEMUTimerList, not QEMUClock.
Correct
Quoted 2 lines
qemu_clock_enablethen will have to visit all timer lists. That's not hard to do,Correct, v4 of my patch does this.
Quoted 7 lines
Quoted 6 lines
Quoted 2 lines
but as locks proliferate we need to have a clear
policy (e.g. BQL -> clock -> timerlist).But doesn't do the locking bit yet (Pingfan is going to do that I hope)
Seem that Stefanha had been involved in this, and sent out his patches :)
Hi Ping Fan,
I dropped the series where I added locks to qemu-timer.c from a few weeks ago.
With your series rebased onto Alex's series, I think my patches are no longer needed?
Stefan
Re: [Qemu-devel] [RFC v2 3/5] timer: make qemu_clock_enable sync between disable and timer's cb
Quoted 36 lines · Stefan Hajnoczi
Quoted 35 lines · liu ping fan
Quoted 34 lines · Alex Bligh
Paolo,
Quoted 11 lines · --On 1 August 2013 08:19:34 -0400 Paolo Bonzini <[email protected]> wrote:
Quoted 8 lines
Quoted 4 lines
True,
qemu_eventbasically works only when a single thread resets it. But there is no race condition here becauseqemu_run_timerscannot be executed concurrently by multiple threads (likeaio_pollin your bottom half patches).... or, if rebasing on top of my patches,
qemu_run_timers*can* be executed concurrently by mulitple threads, but in respect of any given QEMUTimerList, it will only be executed by one thread.... so the event would be placed in QEMUTimerList, not QEMUClock.
Correct
Quoted 2 lines
qemu_clock_enablethen will have to visit all timer lists. That's not hard to do,Correct, v4 of my patch does this.
Quoted 2 lines
but as locks proliferate we need to have a clear
policy (e.g. BQL -> clock -> timerlist).But doesn't do the locking bit yet (Pingfan is going to do that I hope)
Quoted 9 lines
Quoted 1 line
Seem that Stefanha had been involved in this, and sent out his patches :)
Hi Ping Fan,
I dropped the series where I added locks toqemu-timer.cfrom a few weeks ago.With your series rebased onto Alex's series, I think my patches are no longer needed?
No. The active_timers list which is still touched by qemu_mod_timer_ns() needs protection between vcpu/iothread/.. I think you want to pick up your patches?
Regards,
Pingfan