aio/async: Add timed bottom-halves
RFC PATCH 1/1 aio/async: Add timed bottom-halves
Add timed bottom halves. A timed bottom half is a bottom half that will not execute until a given time has passed (qemu_bh_schedule_at) or a given interval has passed (qemu_bh_schedule_in). Any qemu clock can be used, and times are specified in nanoseconds.
Timed bottom halves can be used where timers cannot. For instance, in block drivers where there is no mainloop that calls timers (qemu-nbd, qemu-img), or where (per [email protected]) the aio code loops internally and thus timers never get called.
--- async.c | 53 +++++++++++++++++++++++++++++++++++++++++++++------ include/block/aio.h | 33 ++++++++++++++++++++++++++++++++ tests/test-aio.c | 47 +++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 127 insertions(+), 6 deletions(-)
async.c +47 -6
@@ -35,6 +35,8 @@ struct QEMUBH {QEMUBHFunc *cb;void *opaque;QEMUBH *next; +QEMUClock *clock; +int64_t time;bool scheduled;bool idle;bool deleted;
@@ -52,6 +54,11 @@ QEMUBH *aio_bh_new(AioContext *ctx, QEMUBHFunc *cb, void *opaque)return bh; } +static inline int64_t qemu_bh_ready_in (QEMUBH *bh) +{ +return (bh->clock) ? (bh->time -qemu_get_clock_ns (bh->clock)) :0 ; +} +int aio_bh_poll (AioContext *ctx) {QEMUBH *bh, **bhp, *next;
@@ -62,8 +69,10 @@ int aio_bh_poll(AioContext *ctx) ret =0 ;for (bh = ctx->first_bh; bh; bh = next) { next = bh->next; -if (!bh->deleted && bh->scheduled) { +if (!bh->deleted && bh->scheduled &&qemu_bh_ready_in (bh) <=0 ) { bh->scheduled =0 ; + bh->clock =NULL ; + bh->time =0 ;if (!bh->idle) ret =1 ; bh->idle =0 ;
@@ -96,6 +105,8 @@ void qemu_bh_schedule_idle(QEMUBH *bh)return ; bh->scheduled =1 ; bh->idle =1 ; + bh->clock =NULL ; + bh->time =0 ; }void qemu_bh_schedule (QEMUBH *bh)
@@ -104,18 +115,39 @@ void qemu_bh_schedule(QEMUBH *bh)return ; bh->scheduled =1 ; bh->idle =0 ; + bh->clock =NULL ; + bh->time =0 ;aio_notify (bh->ctx); } +void qemu_bh_schedule_at (QEMUBH *bh,QEMUClock *clock,int64_t time) +{ +/* Allow rescheduling if already scheduled */ + bh->scheduled =1 ; + bh->idle =0 ; + bh->clock = clock; + bh->time = time; +aio_notify (bh->ctx);/*FIXME: is this right?*/ +} + +void qemu_bh_schedule_in (QEMUBH *bh,QEMUClock *clock,int64_t time) +{ +qemu_bh_schedule_at (bh, clock,qemu_get_clock_ns (clock) + time); +} +void qemu_bh_cancel (QEMUBH *bh) { bh->scheduled =0 ; + bh->clock =NULL ; + bh->time =0 ; }void qemu_bh_delete (QEMUBH *bh) { bh->scheduled =0 ; bh->deleted =1 ; + bh->clock =NULL ; + bh->time =0 ; } static gboolean
@@ -126,13 +158,22 @@ aio_ctx_prepare(GSource *source, gint *timeout)for (bh = ctx->first_bh; bh; bh = bh->next) {if (!bh->deleted && bh->scheduled) { -if (bh->idle) { +int64_t wait =qemu_bh_ready_in (bh) /SCALE_MS ; +if (!wait && bh->idle) {/* idle bottom halves will be polled at least * every 10ms */ - *timeout =10 ; + wait =10 ; + } +if (wait) { +/* Use the minimum wait across all bottom +* halves */ +if (*timeout ==-1 || *timeout > wait) { + *timeout = wait; + } }else { -/* non-idle bottom halves will be executed -* immediately */ +/* non-idle bottom halves or timed bottom +* halves which are ready to run will be +* executed immediately */ *timeout =0 ;return true; }
@@ -149,7 +190,7 @@ aio_ctx_check(GSource *source)QEMUBH *bh;for (bh = ctx->first_bh; bh; bh = bh->next) { -if (!bh->deleted && bh->scheduled) { +if (!bh->deleted && bh->scheduled &&qemu_bh_ready_in (bh) <=0 ) {return true; } }
include/block/aio.h +33 -0
@@ -20,6 +20,7 @@typedef struct BlockDriverAIOCB BlockDriverAIOCB ;typedef void BlockDriverCompletionFunc (void *opaque,int ret); +typedef struct QEMUClock QEMUClock ; typedef struct AIOCBInfo { void (*cancel)(BlockDriverAIOCB *acb);
@@ -145,6 +146,38 @@ int aio_bh_poll(AioContext *ctx);void qemu_bh_schedule (QEMUBH *bh);/** +* qemu_bh_schedule_at: Schedule a bottom half at a future time +* +* Scheduling a bottom half interrupts the main loop and causes the +* execution of the callback that was passed to qemu_bh_new. +* +* Bottom halves that are scheduled from a bottom half handler are instantly +* invoked. This can create an infinite loop if a bottom half handler +* schedules itself. +* +* @bh: The bottom half to be scheduled. +* @clock: The clock to be used +* @time: The time in nanoseconds at which the bh is scheduled +*/ +void qemu_bh_schedule_at (QEMUBH *bh,QEMUClock *clock,int64_t time); + +/** +* qemu_bh_schedule_in: Schedule a bottom half after an interval +* +* Scheduling a bottom half interrupts the main loop and causes the +* execution of the callback that was passed to qemu_bh_new. +* +* Bottom halves that are scheduled from a bottom half handler are instantly +* invoked. This can create an infinite loop if a bottom half handler +* schedules itself. +* +* @bh: The bottom half to be scheduled. +* @clock: The clock to be used +* @time: The interval in nanoseconds after which the bh is scheduled +*/ +void qemu_bh_schedule_in (QEMUBH *bh,QEMUClock *clock,int64_t time); + +/** * qemu_bh_cancel: Cancel execution of a bottom half. * * Canceling execution of a bottom half undoes the effect of calls to
tests/test-aio.c +47 -0
83 lines not on this page · raw message
Signature · 1 line
1.7.9.5
Quoted 4 lines · --On 6 July 2013 17:24:57 +0100 Alex Bligh <[email protected]> wrote:
Add timed bottom halves. A timed bottom half is a bottom half that will not execute until a given time has passed (
qemu_bh_schedule_at) or a given interval has passed (qemu_bh_schedule_in). Any qemu clock can be used, and times are specified in nanoseconds.
For background, this patch resulted from a discussion with Kevin & Stefan on IRC as to the best way to use timers (or rather avoid using timers) in block drivers.
There is one thing I'm not entirely sure about:
async.c void qemu_bh_schedule(QEMUBH *bh) · line 131
Quoted 9 lines
async.c +9
-0+void qemu_bh_schedule_at (QEMUBH *bh,QEMUClock *clock,int64_t time) +{ +/* Allow rescheduling if already scheduled */ + bh->scheduled =1 ; + bh->idle =0 ; + bh->clock = clock; + bh->time = time; +aio_notify (bh->ctx);/*FIXME: is this right?*/ +}
qemu_bh_schedule calls aio_notify, but qemu_bh_schedule_idle does not. I am not quite sure why the latter doesn't - possibly through not fully understanding the aio system. Should qemu_bh_schedule_at be calling this, when I don't know whether the bh is scheduled for 1 nanosecond ahead or 1 second?
Signature · 1 line
Alex Bligh
RFC PATCH v2 1/1 aio/async: Add timed bottom-halves
Add timed bottom halves. A timed bottom half is a bottom half that will not execute until a given time has passed (qemu_bh_schedule_at) or a given interval has passed (qemu_bh_schedule_in). Any qemu clock can be used, and times are specified in nanoseconds.
Timed bottom halves can be used where timers cannot. For instance, in block drivers where there is no mainloop that calls timers (qemu-nbd, qemu-img), or where (per [email protected]) the aio code loops internally and thus timers never get called.
Changes since v1:
* aio_ctx_prepare should cope with wait<0
* aio_ctx_prepare should round up wait time
--- async.c | 53 +++++++++++++++++++++++++++++++++++++++++++++------ include/block/aio.h | 33 ++++++++++++++++++++++++++++++++ tests/test-aio.c | 47 +++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 127 insertions(+), 6 deletions(-)
async.c +47 -6
@@ -35,6 +35,8 @@ struct QEMUBH {QEMUBHFunc *cb;void *opaque;QEMUBH *next; +QEMUClock *clock; +int64_t time;bool scheduled;bool idle;bool deleted;
@@ -52,6 +54,11 @@ QEMUBH *aio_bh_new(AioContext *ctx, QEMUBHFunc *cb, void *opaque)return bh; } +static inline int64_t qemu_bh_ready_in (QEMUBH *bh) +{ +return (bh->clock) ? (bh->time -qemu_get_clock_ns (bh->clock)) :0 ; +} +int aio_bh_poll (AioContext *ctx) {QEMUBH *bh, **bhp, *next;
@@ -62,8 +69,10 @@ int aio_bh_poll(AioContext *ctx) ret =0 ;for (bh = ctx->first_bh; bh; bh = next) { next = bh->next; -if (!bh->deleted && bh->scheduled) { +if (!bh->deleted && bh->scheduled &&qemu_bh_ready_in (bh) <=0 ) { bh->scheduled =0 ; + bh->clock =NULL ; + bh->time =0 ;if (!bh->idle) ret =1 ; bh->idle =0 ;
@@ -96,6 +105,8 @@ void qemu_bh_schedule_idle(QEMUBH *bh)return ; bh->scheduled =1 ; bh->idle =1 ; + bh->clock =NULL ; + bh->time =0 ; }void qemu_bh_schedule (QEMUBH *bh)
@@ -104,18 +115,39 @@ void qemu_bh_schedule(QEMUBH *bh)return ; bh->scheduled =1 ; bh->idle =0 ; + bh->clock =NULL ; + bh->time =0 ;aio_notify (bh->ctx); } +void qemu_bh_schedule_at (QEMUBH *bh,QEMUClock *clock,int64_t time) +{ +/* Allow rescheduling if already scheduled */ + bh->scheduled =1 ; + bh->idle =0 ; + bh->clock = clock; + bh->time = time; +aio_notify (bh->ctx);/*FIXME: is this right?*/ +} + +void qemu_bh_schedule_in (QEMUBH *bh,QEMUClock *clock,int64_t time) +{ +qemu_bh_schedule_at (bh, clock,qemu_get_clock_ns (clock) + time); +} +void qemu_bh_cancel (QEMUBH *bh) { bh->scheduled =0 ; + bh->clock =NULL ; + bh->time =0 ; }void qemu_bh_delete (QEMUBH *bh) { bh->scheduled =0 ; bh->deleted =1 ; + bh->clock =NULL ; + bh->time =0 ; } static gboolean
@@ -126,13 +158,22 @@ aio_ctx_prepare(GSource *source, gint *timeout)for (bh = ctx->first_bh; bh; bh = bh->next) {if (!bh->deleted && bh->scheduled) { -if (bh->idle) { +int64_t wait =qemu_bh_ready_in (bh) /SCALE_MS ; +if (!wait && bh->idle) {/* idle bottom halves will be polled at least * every 10ms */ - *timeout =10 ; + wait =10 ; + } +if (wait) { +/* Use the minimum wait across all bottom +* halves */ +if (*timeout ==-1 || *timeout > wait) { + *timeout = wait; + } }else { -/* non-idle bottom halves will be executed -* immediately */ +/* non-idle bottom halves or timed bottom +* halves which are ready to run will be +* executed immediately */ *timeout =0 ;return true; }
@@ -149,7 +190,7 @@ aio_ctx_check(GSource *source)QEMUBH *bh;for (bh = ctx->first_bh; bh; bh = bh->next) { -if (!bh->deleted && bh->scheduled) { +if (!bh->deleted && bh->scheduled &&qemu_bh_ready_in (bh) <=0 ) {return true; } }
include/block/aio.h +33 -0
@@ -20,6 +20,7 @@typedef struct BlockDriverAIOCB BlockDriverAIOCB ;typedef void BlockDriverCompletionFunc (void *opaque,int ret); +typedef struct QEMUClock QEMUClock ; typedef struct AIOCBInfo { void (*cancel)(BlockDriverAIOCB *acb);
@@ -145,6 +146,38 @@ int aio_bh_poll(AioContext *ctx);void qemu_bh_schedule (QEMUBH *bh);/** +* qemu_bh_schedule_at: Schedule a bottom half at a future time +* +* Scheduling a bottom half interrupts the main loop and causes the +* execution of the callback that was passed to qemu_bh_new. +* +* Bottom halves that are scheduled from a bottom half handler are instantly +* invoked. This can create an infinite loop if a bottom half handler +* schedules itself. +* +* @bh: The bottom half to be scheduled. +* @clock: The clock to be used +* @time: The time in nanoseconds at which the bh is scheduled +*/ +void qemu_bh_schedule_at (QEMUBH *bh,QEMUClock *clock,int64_t time); + +/** +* qemu_bh_schedule_in: Schedule a bottom half after an interval +* +* Scheduling a bottom half interrupts the main loop and causes the +* execution of the callback that was passed to qemu_bh_new. +* +* Bottom halves that are scheduled from a bottom half handler are instantly +* invoked. This can create an infinite loop if a bottom half handler +* schedules itself. +* +* @bh: The bottom half to be scheduled. +* @clock: The clock to be used +* @time: The interval in nanoseconds after which the bh is scheduled +*/ +void qemu_bh_schedule_in (QEMUBH *bh,QEMUClock *clock,int64_t time); + +/** * qemu_bh_cancel: Cancel execution of a bottom half. * * Canceling execution of a bottom half undoes the effect of calls to
tests/test-aio.c +47 -0
83 lines not on this page · raw message
Signature · 1 line
1.7.9.5
Il 06/07/2013 18:24, Alex Bligh ha scritto:
Quoted 3 lines
Add timed bottom halves. A timed bottom half is a bottom half that will not execute until a given time has passed (
qemu_bh_schedule_at) or a given interval has passed (qemu_bh_schedule_in).
... and may be delayed arbitrarily past that given interval if you are running in qemu-img or in other synchronous I/O APIs. I'm especially worried that this will not have any effect if bdrv_aio_cancel is calling qemu_aio_wait. bdrv_aio_cancel is presumably one place where you want timeout/reconnect functionality to trigger.
I would really prefer to have a TimeEventNotifier or something like that, which is API-compatibile with EventNotifier (so you can use the regular aio-*.c APIs) but triggers when a given time has passed. Basically an "heavyweight" QEMUTimer; that would be a timerfd on Linux, and a queue timer on Windows. No idea on other POSIX systems, unfortunately.
Even better would be to remove the whole timer stuff (POSIX timers, setitimer, and the Win32 equivalents), and just make the timers use a shorter timeout for the main loop. If you do this, I suspect adding timer support to AioContext would be much simpler.
BTW, note that qemu-nbd (and qemu-io too) does call timers.
Paolo
Quoted 9 lines
Any qemu
clock can be used, and times are specified in nanoseconds.Timed bottom halves can be used where timers cannot. For instance, in block drivers where there is no mainloop that calls timers (qemu-nbd, qemu-img), or where (per [email protected]) the aio code loops internally and thus timers never get called.
Signed-off-by Alex Bligh <[email protected]>
Paolo,
--On 15 July 2013 16:25:01 +0200 Paolo Bonzini <[email protected]> wrote:
Thanks for the review.
Quoted 7 lines
Il 06/07/2013 18:24, Alex Bligh ha scritto:
Quoted 3 lines
Add timed bottom halves. A timed bottom half is a bottom half that will not execute until a given time has passed (
qemu_bh_schedule_at) or a given interval has passed (qemu_bh_schedule_in).... and may be delayed arbitrarily past that given interval if you are running in qemu-img or in other synchronous I/O APIs.
That's true. However, the problem with timers is worse, in that we poll for timers even less frequently as far as I can tell.
Quoted 4 lines
I'm especially
worried that this will not have any effect ifbdrv_aio_cancelis callingqemu_aio_wait.bdrv_aio_cancelis presumably one place where you want timeout/reconnect functionality to trigger.
Well, I'm a newbie here, so may well be wrong but I thought qemu_aio_wait /did/ call bottom halves (but didn't call QemuTimers). Provided time does actually advance (which inspection suggests it does), then these bh's should be called just like any other bh's. I may have missed the point here entirely.
Quoted 6 lines
I would really prefer to have a TimeEventNotifier or something like that, which is API-compatibile with EventNotifier (so you can use the regular aio-*.c APIs) but triggers when a given time has passed. Basically an "heavyweight" QEMUTimer; that would be a timerfd on Linux, and a queue timer on Windows. No idea on other POSIX systems, unfortunately.
I was trying to use the bh API because that's what the existing block drivers use, and what I really wanted was a bh that wouldn't execute for a while. Do EventNotifiers run whilst AIO is polling for completion?
Quoted 4 lines
Even better would be to remove the whole timer stuff (POSIX timers, setitimer, and the Win32 equivalents), and just make the timers use a shorter timeout for the main loop. If you do this, I suspect adding timer support to AioContext would be much simpler.
In discussion with Stefan H on IRC, I originally suggested moving the QemuTimer poll to the AIO loop (or adding another), which is a half arsed way to do what you are suggesting. He suggested this would be hairy because the existing users might not be safe to be called there. This was an attempt at a minimal change to fix that use.
Quoted 1 line
BTW, note that qemu-nbd (and qemu-io too) does call timers.
I'd thought I tested qemu-io. qemu-convert definitely does not.
Alex
Quoted 13 lines
Paolo
Quoted 9 lines
Any qemu
clock can be used, and times are specified in nanoseconds.Timed bottom halves can be used where timers cannot. For instance, in block drivers where there is no mainloop that calls timers (qemu-nbd, qemu-img), or where (per [email protected]) the aio code loops internally and thus timers never get called.
Signed-off-by Alex Bligh <[email protected]>
Signature · 1 line
Alex Bligh
Il 15/07/2013 22:15, Alex Bligh ha scritto:
Quoted 11 lines
Paolo,
--On 15 July 2013 16:25:01 +0200 Paolo Bonzini <[email protected]> wrote:
Thanks for the review.
Quoted 5 lines
Il 06/07/2013 18:24, Alex Bligh ha scritto:
Quoted 3 lines
Add timed bottom halves. A timed bottom half is a bottom half that will not execute until a given time has passed (
qemu_bh_schedule_at) or a given interval has passed (qemu_bh_schedule_in).
Quoted 5 lines
Quoted 2 lines
... and may be delayed arbitrarily past that given interval if you are running in qemu-img or in other synchronous I/O APIs.
That's true. However, the problem with timers is worse, in that we poll for timers even less frequently as far as I can tell.
Right, we poll for bottom halves during qemu_aio_wait(). We don't poll for timers.
Quoted 10 lines
Quoted 4 lines
I'm especially
worried that this will not have any effect ifbdrv_aio_cancelis callingqemu_aio_wait.bdrv_aio_cancelis presumably one place where you want timeout/reconnect functionality to trigger.Well, I'm a newbie here, so may well be wrong but I thought
qemu_aio_wait/did/ call bottom halves (but didn't call QemuTimers). Provided time does actually advance (which inspection suggests it does), then these bh's should be called just like any other bh's. I may have missed the point here entirely.
So far you are right.
But this only happens if qemu_aio_wait() actually returns, so that on the next call we poll for timers. If QEMU is stuck in qemu_aio_wait()'s infinite-timeout poll(), it will never advance and process the timed bottom halves.
This goes to the question of having aio_notify() or not. If you have it, you will immediately process timed BHs that are "born expired". For other bottom halves, there will be no difference if you add it or not.
Quoted 10 lines
Quoted 6 lines
I would really prefer to have a TimeEventNotifier or something like that, which is API-compatibile with EventNotifier (so you can use the regular aio-*.c APIs) but triggers when a given time has passed. Basically an "heavyweight" QEMUTimer; that would be a timerfd on Linux, and a queue timer on Windows. No idea on other POSIX systems, unfortunately.
I was trying to use the bh API because that's what the existing block drivers use, and what I really wanted was a bh that wouldn't execute for a while. Do EventNotifiers run whilst AIO is polling for completion?
Yes, and they can actually interrupt qemu_aio_wait(). See aio_set_event_notifier.
Quoted 11 lines
Quoted 4 lines
Even better would be to remove the whole timer stuff (POSIX timers, setitimer, and the Win32 equivalents), and just make the timers use a shorter timeout for the main loop. If you do this, I suspect adding timer support to AioContext would be much simpler.
In discussion with Stefan H on IRC, I originally suggested moving the QemuTimer poll to the AIO loop (or adding another), which is a half arsed way to do what you are suggesting. He suggested this would be hairy because the existing users might not be safe to be called there. This was an attempt at a minimal change to fix that use.
Quoted 3 lines
Quoted 1 line
BTW, note that qemu-nbd (and qemu-io too) does call timers.
I'd thought I tested qemu-io. qemu-convert definitely does not.
qemu-io is the tool that is used by the unit tests. Conversion is in qemu-img.
Paolo
Quoted 18 lines
Alex
Quoted 13 lines
Paolo
Quoted 9 lines
Any qemu
clock can be used, and times are specified in nanoseconds.Timed bottom halves can be used where timers cannot. For instance, in block drivers where there is no mainloop that calls timers (qemu-nbd, qemu-img), or where (per [email protected]) the aio code loops internally and thus timers never get called.
Signed-off-by Alex Bligh <[email protected]>
Paolo,
Quoted 6 lines · --On 15 July 2013 22:53:17 +0200 Paolo Bonzini <[email protected]> wrote:
So far you are right.
But this only happens if
qemu_aio_wait()actually returns, so that on the next call we poll for timers. If QEMU is stuck in qemu_aio_wait()'s infinite-timeoutpoll(), it will never advance and process the timed bottom halves.
I may have misunderstood the code here. I thought what it was doing was setting the timeout to poll() to 0 if there was a bh queued, 10ms if there was a bh->idle timeout queued, or infinite otherwise.
What I changed it to do was set the timeout to:
* 0 if there was an untimed bh ready (no change)
* 10ms if there was an untimed idle bh ready (no change)
* the number of ms to expiry if there is a timed bh ready (rounded up)
* infinite otherwise (no change)
* and if there is more the one, the minimum of those
So the infinite timeout poll should not be entered if there is a timed bh there. This of course assumes a single thread as else there is a TOCTOU problem if a timed bh gets inserted between calculating the expiry time and the poll.
Quoted 3 lines
This goes to the question of having
aio_notify()or not. If you have it, you will immediately process timed BHs that are "born expired". For other bottom halves, there will be no difference if you add it or not.
You've lost me there. I'm taking it by 'born expired' you mean when poll() is entered, they're already expired. Timed BH's that are 'born expired' should (with the patch I sent) be treated exactly the same as untimed BH's, i.e. the timeout to poll should be being set to zero.
Signature · 1 line
Alex Bligh
Il 16/07/2013 01:04, Alex Bligh ha scritto:
Quoted 15 lines
Paolo,
Quoted 6 lines · --On 15 July 2013 22:53:17 +0200 Paolo Bonzini <[email protected]> wrote:
So far you are right.
But this only happens if
qemu_aio_wait()actually returns, so that on the next call we poll for timers. If QEMU is stuck in qemu_aio_wait()'s infinite-timeoutpoll(), it will never advance and process the timed bottom halves.I may have misunderstood the code here. I thought what it was doing was setting the timeout to
poll()to 0 if there was a bh queued, 10ms if there was abh->idletimeout queued, or infinite otherwise.
Quoted 6 lines
What I changed it to do was set the timeout to:
* 0 if there was an untimed bh ready (no change)
* 10ms if there was an untimed idle bh ready (no change)
* the number of ms to expiry if there is a timed bh ready (rounded up)
* infinite otherwise (no change)
* and if there is more the one, the minimum of those
You did. But aio_wait() ignores the timeout. It is only used by the main loop.
Quoted 9 lines
So the infinite timeout poll should not be entered if there is a timed bh there. This of course assumes a single thread as else there is a TOCTOU problem if a timed bh gets inserted between calculating the expiry time and the poll.
Quoted 3 lines
This goes to the question of having
aio_notify()or not. If you have it, you will immediately process timed BHs that are "born expired". For other bottom halves, there will be no difference if you add it or not.
Quoted 5 lines
You've lost me there. I'm taking it by 'born expired' you mean when
poll()is entered, they're already expired. Timed BH's that are 'born expired' should (with the patch I sent) be treated exactly the same as untimed BH's, i.e. the timeout to poll should be being set to zero.
Yes, that's exactly what I meant. Looks like you understood well! :)
Paolo
Paolo,
Quoted 2 lines · --On 16 July 2013 08:16:42 +0200 Paolo Bonzini <[email protected]> wrote:
You did. But
aio_wait()ignores the timeout. It is only used by the main loop.
OK well that seems worth fixing in any case, as even without timed bh's that means no bh can be executed for an indeterminate time. I'll have a look at that.
Signature · 1 line
Alex Bligh
Il 16/07/2013 09:30, Alex Bligh ha scritto:
Quoted 7 lines
Quoted 3 lines
You did. But
aio_wait()ignores the timeout. It is only used by the main loop.OK well that seems worth fixing in any case, as even without timed bh's that means no bh can be executed for an indeterminate time. I'll have a look at that.
No, BHs work because they do aio_notify(). Idle BHs can be skipped for an indeterminate time, but that's fine because idle BHs are a hack that we should not need at all.
Paolo
Paolo,
Quoted 10 lines · --On 16 July 2013 09:34:20 +0200 Paolo Bonzini <[email protected]> wrote:
Quoted 6 lines
Quoted 2 lines
You did. But
aio_wait()ignores the timeout. It is only used by the main loop.OK well that seems worth fixing in any case, as even without timed bh's that means no bh can be executed for an indeterminate time. I'll have a look at that.
No, BHs work because they do
aio_notify(). Idle BHs can be skipped for an indeterminate time, but that's fine because idle BHs are a hack that we should not need at all.
OK, so a bit more code reading later, I think I now understand.
1. non-idle bh's call aio_notify at schedule time, which will cause any poll to exit immediately because at least one FD will be ready.
2. idle bh's do not do aio_notify() because we don't care whether they get stuck in aio_poll and they are a hack [actually I think we could do better here]
3. aio_poll calls aio_bh_poll. If this returns true, this indicates at least one non-idle bh exists, which causes aio_poll not to block.
Question 1: it then calls aio_dispatch - if this itself generates non-idle bh's, this would seem not to clear the blocking flag. Does this rely on aio_notify?
Question 2: if we're already telling aio_poll not to block by the presence of non-idle bh's as detected in aio_bh_poll, why do we need to use aio_notify too? IE why do we have both the blocking= logic AND the aio_notify logic?
4. aio_poll then calls g_poll (POSIX) or WaitForMultipleObjects (Windows). However, the timeout is either 0 or infinite. Both functions take a milliseconds (yuck) timeout, but that is not used.
So, the first thing I don't understand is why aio_poll needs the return value of aio_bh_poll at all. Firstly, after sampling it, it then causes aio_dispatch, and that can presumably set its own bottom half callbacks; if this happens 'int blocking' won't be cleared, and it will still enter g_poll with an infinite timeout. Secondly, there seems to be an entirely separate mechanism (aio_notify) in any case. If a non-idle bh has been scheduled, this will cause g_poll to exit immediately as a read will be ready. I believe this is cleared by the bh being used.
The second thing I don't understand is why we aren't using the timeout on g_poll / WaitForMultipleObjects. It would seem to be reasonably easy to make aio_poll call aio_ctx_prepare or something that does the same calculation. This would fix idle bh's to be more reliable (we know it's safe to call them within aio_poll anyway, it's just a question of whether we turn an infinite wait into a 10ms wait).
Perhaps these two are related.
I /think/ fixing the second (and removing the aio_notify from qemu_bh_schedule_at) is sufficient provided it checks for scheduled bh's immediately prior to the poll. This assumes other threads cannot schedule bh's. This would seem to be less intrusive that a TimedEventNotifier approach which (as far as I can see) requires another thread.
Signature · 1 line
Alex Bligh
Il 16/07/2013 17:29, Alex Bligh ha scritto:
Quoted 20 lines
Paolo,
Quoted 10 lines · --On 16 July 2013 09:34:20 +0200 Paolo Bonzini <[email protected]> wrote:
Quoted 6 lines
Quoted 2 lines
You did. But
aio_wait()ignores the timeout. It is only used by the main loop.OK well that seems worth fixing in any case, as even without timed bh's that means no bh can be executed for an indeterminate time. I'll have a look at that.
No, BHs work because they do
aio_notify(). Idle BHs can be skipped for an indeterminate time, but that's fine because idle BHs are a hack that we should not need at all.OK, so a bit more code reading later, I think I now understand.
1. non-idle bh's call
aio_notifyat schedule time, which will cause any poll to exit immediately because at least one FD will be ready.
Quoted 7 lines
2. idle bh's do not do
aio_notify()because we don't care whether they get stuck inaio_polland they are a hack [actually I think we could do better here]3.
aio_pollcallsaio_bh_poll. If this returns true, this indicates at least one non-idle bh exists, which causesaio_pollnot to block.
No, this indicates that at least one scheduled non-idle bh exist*ed*, which causes aio_poll not to block (because some progress has been done).
There could be non-idle BHs scheduled during aio_poll, but not visited by aio_bh_poll. These rely on aio_notify (it could be an idle BH who scheduled this non-idle BH, so aio_bh_poll might return 0).
Quoted 3 lines
Question 1: it then calls
aio_dispatch- if this itself generates non-idle bh's, this would seem not to clear the blocking flag. Does this rely onaio_notify?
Yes.
Quoted 4 lines
Question 2: if we're already telling
aio_pollnot to block by the presence of non-idle bh's as detected inaio_bh_poll, why do we need to useaio_notifytoo? IE why do we have both the blocking= logic AND theaio_notifylogic?
See above (newly-scheduled BHs are always handled with aio_notify, the blocking=false logic is for previously-scheduled BHs).
Quoted 4 lines
4.
aio_pollthen callsg_poll(POSIX) or WaitForMultipleObjects (Windows). However, the timeout is either 0 or infinite. Both functions take a milliseconds (yuck) timeout, but that is not used.
I agree with the yuck. :) But Linux has the nanoseconds-resolution ppoll, too.
Quoted 9 lines
So, the first thing I don't understand is why
aio_pollneeds the return value ofaio_bh_pollat all. Firstly, after sampling it, it then causesaio_dispatch, and that can presumably set its own bottom half callbacks; if this happens 'int blocking' won't be cleared, and it will still enterg_pollwith an infinite timeout. Secondly, there seems to be an entirely separate mechanism (aio_notify) in any case. If a non-idle bh has been scheduled, this will causeg_pollto exit immediately as a read will be ready. I believe this is cleared by the bh being used.
I hope the above
Quoted 2 lines
The second thing I don't understand is why we aren't using the timeout on
g_poll/ WaitForMultipleObjects.
Because so far it wasn't needed (insert rant about idle BHs being a hack). This is a good occasion to use it. But I wouldn't introduce a new one-off concept (almost as much of a hack as idle BHs), I would rather reuse as much code as possible from QEMUTimer/QEMUClock. I must admit I don't have a clear idea of how the API would look like.
Quoted 6 lines
It would
seem to be reasonably easy to makeaio_pollcallaio_ctx_prepareor something that does the same calculation. This would fix idle bh's to be more reliable (we know it's safe to call them withinaio_pollanyway, it's just a question of whether we turn an infinite wait into a 10ms wait).
Idle BHs could be changed to timers as well, and then they would disappear.
Paolo
Quoted 9 lines
Perhaps these two are related.
I /think/ fixing the second (and removing the
aio_notifyfromqemu_bh_schedule_at) is sufficient provided it checks for scheduled bh's immediately prior to the poll. This assumes other threads cannot schedule bh's. This would seem to be less intrusive that a TimedEventNotifier approach which (as far as I can see) requires another thread.
Paolo,
Quoted 6 lines
Quoted 3 lines
3.
aio_pollcallsaio_bh_poll. If this returns true, this indicates at least one non-idle bh exists, which causesaio_pollnot to block.No, this indicates that at least one scheduled non-idle bh exist*ed*, which causes
aio_pollnot to block (because some progress has been done).
Ah yes, in the sense the callback will have been called and it now doesn't exist.
Quoted 7 lines
Quoted 4 lines
4.
aio_pollthen callsg_poll(POSIX) or WaitForMultipleObjects (Windows). However, the timeout is either 0 or infinite. Both functions take a milliseconds (yuck) timeout, but that is not used.I agree with the yuck. :) But Linux has the nanoseconds-resolution ppoll, too.
Sadly I don't think we have a g_ppoll.
Quoted 10 lines
Quoted 9 lines
So, the first thing I don't understand is why
aio_pollneeds the return value ofaio_bh_pollat all. Firstly, after sampling it, it then causesaio_dispatch, and that can presumably set its own bottom half callbacks; if this happens 'int blocking' won't be cleared, and it will still enterg_pollwith an infinite timeout. Secondly, there seems to be an entirely separate mechanism (aio_notify) in any case. If a non-idle bh has been scheduled, this will causeg_pollto exit immediately as a read will be ready. I believe this is cleared by the bh being used.
Sorry - forgot to delete the questions from above when I moved them down below. Yes that make sense.
Quoted 5 lines
Quoted 2 lines
The second thing I don't understand is why we aren't using the timeout on
g_poll/ WaitForMultipleObjects.Because so far it wasn't needed (insert rant about idle BHs being a hack). This is a good occasion to use it.
OK
Quoted 4 lines
But I wouldn't introduce a
new one-off concept (almost as much of a hack as idle BHs), I would rather reuse as much code as possible from QEMUTimer/QEMUClock. I must admit I don't have a clear idea of how the API would look like.
So the reason I was trying to avoid using QEMUTimer stuff was that bh's get called from aio_poll and it was not evident that all timers would be safe to call from aio_poll.
My original approach was simply to call qemu_run_all_timers from aio_poll at the top. I now know I'd have to (slightly) modify the poll routine too use a specific timeout if there are any timers ready to run and the timeout would otherwise be infinity. However Stefan H pointed out on IRC that we'd then have to audit every timer usage to check it was safe to call from aio_poll, or have a 'isSafeToCallFromAioPoll' type flag (yuck). Hence we thought the timed bh would be less intrusive as actually what I and other block driver folks might want is something more like a bh. Equally happy to recode it as a QEMUTimer.
What do you think? In the end I thought the schedule_bh_at stuff was simpler.
Quoted 9 lines
Quoted 6 lines
It would
seem to be reasonably easy to makeaio_pollcallaio_ctx_prepareor something that does the same calculation. This would fix idle bh's to be more reliable (we know it's safe to call them withinaio_pollanyway, it's just a question of whether we turn an infinite wait into a 10ms wait).Idle BHs could be changed to timers as well, and then they would disappear.
Yes I was in essence hoping an idle BH would be treated as an epsilon millisecond timed bh, or (if above) an epsilon millisecond timer. By that I mean poll() would be called at least once before it ran, but with a 0 ms timeout.
Signature · 1 line
Alex Bligh
Il 16/07/2013 18:14, Alex Bligh ha scritto:
Quoted 11 lines
Paolo,
Quoted 6 lines
Quoted 3 lines
3.
aio_pollcallsaio_bh_poll. If this returns true, this indicates at least one non-idle bh exists, which causesaio_pollnot to block.No, this indicates that at least one scheduled non-idle bh exist*ed*, which causes
aio_pollnot to block (because some progress has been done).Ah yes, in the sense the callback will have been called and it now doesn't exist.
Or at least isn't scheduled.
Quoted 9 lines
Quoted 7 lines
Quoted 4 lines
4.
aio_pollthen callsg_poll(POSIX) or WaitForMultipleObjects (Windows). However, the timeout is either 0 or infinite. Both functions take a milliseconds (yuck) timeout, but that is not used.I agree with the yuck. :) But Linux has the nanoseconds-resolution ppoll, too.
Sadly I don't think we have a
g_ppoll.
Doesn't matter, poll==g_poll on POSIX systems. You can just use poll (or g_poll) on non-Linux, and ppoll on Linux.
Quoted 8 lines
Quoted 4 lines
But I wouldn't introduce a
new one-off concept (almost as much of a hack as idle BHs), I would rather reuse as much code as possible from QEMUTimer/QEMUClock. I must admit I don't have a clear idea of how the API would look like.So the reason I was trying to avoid using QEMUTimer stuff was that bh's get called from
aio_polland it was not evident that all timers would be safe to call fromaio_poll.
It wouldn't.
Quoted 2 lines
What do you think? In the end I thought the
schedule_bh_atstuff was simpler.
It is simpler, but I'm not sure it is the right API. Of course, if Kevin and Stefan says it is, I have no problem with that.
Paolo
RFC PATCH v3 1/1 aio/async: Add timed bottom-halves
Add timed bottom halves. A timed bottom half is a bottom half that will not execute until a given time has passed (qemu_bh_schedule_at) or a given interval has passed (qemu_bh_schedule_in). Any qemu clock can be used, and times are specified in nanoseconds.
Timed bottom halves can be used where timers cannot. For instance, in block drivers where there is no mainloop that calls timers (qemu-nbd, qemu-img), or where (per [email protected]) the aio code loops internally and thus timers never get called.
Changes to aio-win32.c have not even been compile tested.
Changes since v1:
* aio_ctx_prepare should cope with wait<0
* aio_ctx_prepare should round up wait time
Changes since v2:
* aio_poll timeout depends on presence of timed bottom halves
* timed bh's do not do aio_notify immediately
--- aio-posix.c | 20 +++++++++++++- aio-win32.c | 21 ++++++++++++++- async.c | 74 ++++++++++++++++++++++++++++++++++++++++++++++----- include/block/aio.h | 43 ++++++++++++++++++++++++++++++ tests/test-aio.c | 47 ++++++++++++++++++++++++++++++++ 5 files changed, 196 insertions(+), 9 deletions(-)
aio-posix.c +19 -1
@@ -173,6 +173,7 @@ bool aio_poll(AioContext *ctx, bool blocking) {AioHandler *node;int ret; +int timeout =-1 ;bool busy, progress; progress = false;
@@ -183,6 +184,10 @@ bool aio_poll(AioContext *ctx, bool blocking)* does not need a complete flush (as is the case for qemu_aio_wait loops). */ if (aio_bh_poll (ctx)) { +/* At least one non-idle but scheduled bh existed when aio_bh_poll +* was called. We're thus making progress, so don't block. +* Note it may no longer be scheduled. +*/ blocking = false; progress = true; }
@@ -191,6 +196,11 @@ bool aio_poll(AioContext *ctx, bool blocking) progress = true; } +/* The above may schedule bh's in which case if they need the poll below to +* exit immediately, they will have called aio_notify. If they are timed, +* we'll process the timeout below. +*/ +if (progress && !blocking) {return true; }
@@ -231,10 +241,18 @@ bool aio_poll(AioContext *ctx, bool blocking)return progress; } +/* calculate the timeout. This will be infinite if no bh's +* are scheduled, else the lowest accross all scheduled bh's for +* * 0ms for a non-timed, non-idle bh +* * 0ms for a timed, non-idle bh which is ready +* * 10ms for a idle-bh +*/ +aio_bh_get_timeout (ctx, &timeout); +/* wait until next event */ ret =g_poll ((GPollFD *)ctx->pollfds->data, ctx->pollfds->len, - blocking ?-1 :0 ); + blocking ? timeout :0 );/* if we have any readable fds, dispatch event */ if (ret >0 ) {
aio-win32.c +20 -1
@@ -107,6 +107,10 @@ bool aio_poll(AioContext *ctx, bool blocking)* does not need a complete flush (as is the case for qemu_aio_wait loops). */ if (aio_bh_poll (ctx)) { +/* At least one non-idle but scheduled bh existed when aio_bh_poll +* was called. We're thus making progress, so don't block. +* Note it may no longer be scheduled. +*/ blocking = false; progress = true; }
@@ -140,6 +144,11 @@ bool aio_poll(AioContext *ctx, bool blocking) } } +/* The above may schedule bh's in which case if they need the poll below to +* exit immediately, they will have called aio_notify. If they are timed, +* we'll process the timeout below. +*/ +if (progress && !blocking) {return true; }
@@ -175,7 +184,17 @@ bool aio_poll(AioContext *ctx, bool blocking)/* wait until next event */ while (count >0 ) {int timeout = blocking ?INFINITE :0 ; -int ret =WaitForMultipleObjects (count, events, FALSE, timeout); +int ret; + +/* calculate the timeout. This will be infinite if no bh's +* are scheduled, else the lowest accross all scheduled bh's for +* * 0ms for a non-timed, non-idle bh +* * 0ms for a timed, non-idle bh which is ready +* * 10ms for a idle-bh +*/ +aio_bh_get_timeout (ctx, &timeout); + + ret =WaitForMultipleObjects (count, events, FALSE, timeout);/* if we have any signaled events, dispatch event */ if ((DWORD ) (ret -WAIT_OBJECT_0 ) >= count) {
async.c +67 -7
135 lines not on this page · raw message
include/block/aio.h +43 -0
61 lines not on this page · raw message
tests/test-aio.c +47 -0
83 lines not on this page · raw message
Signature · 1 line
1.7.9.5
Paolo,
Quoted 5 lines · --On 16 July 2013 18:55:15 +0200 Paolo Bonzini <[email protected]> wrote:
Quoted 2 lines
What do you think? In the end I thought the
schedule_bh_atstuff was simpler.It is simpler, but I'm not sure it is the right API. Of course, if Kevin and Stefan says it is, I have no problem with that.
For the sake of having something to comment on, I just sent v3 of this patch to the list. This is basically a 'minimal change' version that fixes the issue with aio_poll (I think). It passes make check.
Stefan? Kevin?
Signature · 1 line
Alex Bligh
Quoted 11 lines · Alex Bligh
Quoted 5 lines · --On 16 July 2013 18:55:15 +0200 Paolo Bonzini <[email protected]> wrote:
Quoted 2 lines
What do you think? In the end I thought the
schedule_bh_atstuff was simpler.It is simpler, but I'm not sure it is the right API. Of course, if Kevin and Stefan says it is, I have no problem with that.
For the sake of having something to comment on, I just sent v3 of this patch to the list. This is basically a 'minimal change' version that fixes the issue with
aio_poll(I think). It passes make check.
I would prefer to stick with QEMUTimer instead of introducing an AioContext-specific concept that does something very similar.
This can be done by introducing a per-AioContext QEMUClock. Legacy QEMUTimers will not run during aio_poll() because they are associated with vm_clock, host_clock, or rt_clock. Only QEMUTimers associated with this AioContext's aio_ctx_clock will run.
In other words, the main loop will run vm_clock, host_clock, and rt_clock timers. The AioContext will run its aio_ctx_clock timers.
A few notes about QEMUTimer and QEMUClock:
* QEMUClock can be enabled/disabled. Disabled clocks suppress timer expiration until re-enabled.
* QEMUClock can use an arbitrary time source, which is used to present a virtual time based on the instruction counter when icount mode is active.
* QEMUTimer is associated with a QEMUClock. This allows timers that only expire when the vm_clock is enabled, for example.
* Modifying a QEMUTimer uses qemu_notify_event() since it may be called from a vcpu thread while the iothread is blocked.
The steps to achieving this:
1. Drop alarm timers from qemu-timer.c and calculate g_poll() timeout instead for the main loop.
2. Introduce a per-AioContext aio_ctx_clock that can be used with qemu_new_timer() to create a QEMUTimer that expires during aio_poll().
3. Calculate g_poll() timeout for aio_ctx_clock in aio_poll().
Stefan
Stefan,
Quoted 10 lines · --On 17 July 2013 11:02:30 +0800 Stefan Hajnoczi <[email protected]> wrote:
The steps to achieving this:
1. Drop alarm timers from
qemu-timer.cand calculateg_poll()timeout instead for the main loop.2. Introduce a per-AioContext
aio_ctx_clockthat can be used withqemu_new_timer()to create a QEMUTimer that expires duringaio_poll().3. Calculate
g_poll()timeout foraio_ctx_clockinaio_poll().
A couple of questions:
1. How would this work where the user has no main loop, e.g. qemu-img? A block driver may well still need timers.
2. Are we guaranteed that no aio_poll user can repeatedly call aio_poll without exiting to the main loop?
3. Is it safe to anything you can do in a bh in a timer? IE are users every going to need to schedule a bh from a timer? If so, this seems a bit long winded for users that want bh functionality.
The timed bh solution (which I'm by no means set on) can use any QEMUClock in the bh, so you get to choose the clock per BH, not per AioContext, which may or may not have advantages.
Signature · 1 line
Alex Bligh
Il 17/07/2013 10:07, Alex Bligh ha scritto:
Quoted 14 lines
Stefan,
--On 17 July 2013 11:02:30 +0800 Stefan Hajnoczi <[email protected]>
Quoted 9 lines · wrote:
The steps to achieving this:
1. Drop alarm timers from
qemu-timer.cand calculateg_poll()timeout instead for the main loop.2. Introduce a per-AioContext
aio_ctx_clockthat can be used withqemu_new_timer()to create a QEMUTimer that expires duringaio_poll().
Quoted 6 lines
Quoted 1 line
3. Calculate
g_poll()timeout foraio_ctx_clockinaio_poll().A couple of questions:
1. How would this work where the user has no main loop, e.g. qemu-img? A block driver may well still need timers.
The block driver should only use aio_ctx_clock, and those _would_ be handled in aio_poll().
Quoted 3 lines
3. Is it safe to anything you can do in a bh in a timer? IE are users every going to need to schedule a bh from a timer? If so, this seems a bit long winded for users that want bh functionality.
It is safe.
Paolo
Paolo,
Quoted 11 lines · --On 17 July 2013 10:11:07 +0200 Paolo Bonzini <[email protected]> wrote:
Quoted 11 lines
Quoted 10 lines
The steps to achieving this:
1. Drop alarm timers from
qemu-timer.cand calculateg_poll()timeout instead for the main loop.2. Introduce a per-AioContext
aio_ctx_clockthat can be used withqemu_new_timer()to create a QEMUTimer that expires duringaio_poll().3. Calculate
g_poll()timeout foraio_ctx_clockinaio_poll().
Quoted 7 lines
Quoted 4 lines
A couple of questions:
1. How would this work where the user has no main loop, e.g. qemu-img? A block driver may well still need timers.
The block driver should only use
aio_ctx_clock, and those _would_ be handled inaio_poll().
OK, so modify aio_poll not to run qemu_run_all_timers but to run qemu_run_timer on that particular clock.
We'd still need to modify some of the executables to call init_timers() or whatever it is, but that's easy enough.
I might have a go at that.
Signature · 1 line
Alex Bligh
Stefan,
Quoted 10 lines · --On 17 July 2013 11:02:30 +0800 Stefan Hajnoczi <[email protected]> wrote:
The steps to achieving this:
1. Drop alarm timers from
qemu-timer.cand calculateg_poll()timeout instead for the main loop.2. Introduce a per-AioContext
aio_ctx_clockthat can be used withqemu_new_timer()to create a QEMUTimer that expires duringaio_poll().3. Calculate
g_poll()timeout foraio_ctx_clockinaio_poll().
I've pretty much written this.
Two issues:
1. I very much enjoyed deleting all the alarm timers code. However, it was doing something vaguely useful, which was calling qemu_notify_event when the timer expired. Under the new regime above, if the AioContext clock's timers expires within aio_poll, life is good as the timeout to g_poll will expire. However, this won't apply if any of the other three static clock's timers expire. Also, it won't work within the mainloop poll. Also, we lose the ability to respond to timers in a sub millisecond way.
Options:
a) restore alarm timers (at least for the time being). Make all alarm timers do qemu_notify_event. However, only run the AioContext clock's timers within aio_poll. This is the least intrusive change.
b) calculate the timeout in aio_poll with respect to the minimum deadline across all clocks, not just AioContext's clock. Use the same logic in mainloop.
I'd go for (b), except for the millisecond accuracy thing. So my temptation (sadly) is (a).
2. If we do delete alarm timers, I'll need to delete the -clock option.
WDYT?
Signature · 1 line
Alex Bligh
Quoted 34 lines · Alex Bligh
Stefan,
Quoted 10 lines · --On 17 July 2013 11:02:30 +0800 Stefan Hajnoczi <[email protected]> wrote:
The steps to achieving this:
1. Drop alarm timers from
qemu-timer.cand calculateg_poll()timeout instead for the main loop.2. Introduce a per-AioContext
aio_ctx_clockthat can be used withqemu_new_timer()to create a QEMUTimer that expires duringaio_poll().3. Calculate
g_poll()timeout foraio_ctx_clockinaio_poll().I've pretty much written this.
Two issues:
1. I very much enjoyed deleting all the alarm timers code. However, it was doing something vaguely useful, which was calling
qemu_notify_eventwhen the timer expired. Under the new regime above, if the AioContext clock's timers expires withinaio_poll, life is good as the timeout tog_pollwill expire. However, this won't apply if any of the other three static clock's timers expire. Also, it won't work within the mainloop poll. Also, we lose the ability to respond to timers in a sub millisecond way.Options:
a) restore alarm timers (at least for the time being). Make all alarm timers do
qemu_notify_event. However, only run the AioContext clock's timers withinaio_poll. This is the least intrusive change.
Quoted 6 lines
b) calculate the timeout in
aio_pollwith respect to the minimum deadline across all clocks, not just AioContext's clock. Use the same logic in mainloop.I'd go for (b), except for the millisecond accuracy thing. So my temptation (sadly) is (a).
I think this is a non-issue because host_alarm_handler() can only be called from the main loop:
main-loop.c:qemu_signal_init() sets up signalfd to monitor SIGALRM. Therefore we do not asynchronously invoke the SIGALRM signals handler. It is only invoked from main-loop.c:sigfd_handler() when the main loop runs.
That's how I read the code. I haven't checked a running process to be sure.
Quoted 1 line
2. If we do delete alarm timers, I'll need to delete the -clock option.
I noticed this too because I think we should stub it out for compatibility. Accept existing options but ignore them, update documentation to state that they are kept for compatibility.
Stefan
Il 19/07/2013 03:58, Stefan Hajnoczi ha scritto:
Quoted 16 lines
Quoted 6 lines
Options:
a) restore alarm timers (at least for the time being). Make all alarm timers do
qemu_notify_event. However, only run the AioContext clock's timers withinaio_poll. This is the least intrusive change.Quoted 6 lines
b) calculate the timeout in
aio_pollwith respect to the minimum deadline across all clocks, not just AioContext's clock. Use the same logic in mainloop.I'd go for (b), except for the millisecond accuracy thing. So my temptation (sadly) is (a).
I think this is a non-issue because
host_alarm_handler()can only be called from the main loop:
Quoted 7 lines
main-loop.c:qemu_signal_init() sets up signalfd to monitor SIGALRM. Therefore we do not asynchronously invoke the SIGALRM signals handler. It is only invoked from main-loop.c:sigfd_handler() when the main loop runs.
That's how I read the code. I haven't checked a running process to be sure.
I think you're right. It was not strictly needed even with alarm timers, because host_alarm_handler() is called always before qemu_run_all_timers. But it made the code more robust.
With your change to delete alarm timers and move the deadline to poll, the next poll call will have a timeout of 0 and all will be well.
As to millisecond accuracy, as discussed earlier we can use ppoll on Linux. This of course should be introduced before alarm timers are deleted, to avoid breaking bisection.
Paolo
Quoted 8 lines
Quoted 1 line
2. If we do delete alarm timers, I'll need to delete the -clock option.
I noticed this too because I think we should stub it out for compatibility. Accept existing options but ignore them, update documentation to state that they are kept for compatibility.
Stefan
Stefan,
Quoted 16 lines · --On 19 July 2013 09:58:50 +0800 Stefan Hajnoczi <[email protected]> wrote:
Quoted 6 lines
Options:
a) restore alarm timers (at least for the time being). Make all alarm timers do
qemu_notify_event. However, only run the AioContext clock's timers withinaio_poll. This is the least intrusive change.Quoted 6 lines
b) calculate the timeout in
aio_pollwith respect to the minimum deadline across all clocks, not just AioContext's clock. Use the same logic in mainloop.I'd go for (b), except for the millisecond accuracy thing. So my temptation (sadly) is (a).
I think this is a non-issue because
host_alarm_handler()can only be called from the main loop:
Quoted 7 lines
main-loop.c:qemu_signal_init() sets up signalfd to monitor SIGALRM. Therefore we do not asynchronously invoke the SIGALRM signals handler. It is only invoked from main-loop.c:sigfd_handler() when the main loop runs.
That's how I read the code. I haven't checked a running process to be sure.
Not sure that's the case on win32, but OK let's go with that.
However, I still don't quite see how the poll in the mainloop is meant to exit when a timer expires. There's now no qemu_notify_event, and no SIGALRM, and the timeout will still be infinite (unless I calculate the timeout as the minimum across all clocks, in which case I might as well do (b) above).
Signature · 1 line
Alex Bligh
Il 19/07/2013 08:38, Alex Bligh ha scritto:
Quoted 6 lines
However, I still don't quite see how the poll in the mainloop is meant to exit when a timer expires. There's now no
qemu_notify_event, and no SIGALRM, and the timeout will still be infinite (unless I calculate the timeout as the minimum across all clocks, in which case I might as well do (b) above).
Yes, that was the idea.
Paolo
RFC PATCH 1/1 aio/timers: Drop alarm timers; introduce QEMUClock to AioContext; run timers in aio_poll
[ This is a patch for RFC purposes only. It is compile tested on Linux x86_64 only and passes make check (or rather did before make check started dying in the boot order test - different bug). I'd like to know whether I'm going in the right direction ]
We no longer need alarm timers to trigger QEMUTimer as we'll be polling them in aio_poll.
Remove static declaration from qemu_new_clock and introduce qemu_free_clock.
Maintain a list of QEMUClocks.
Introduce qemu_clock_deadline_ns and qemu_clock_deadine_all_ns which calculate how long aio_poll etc. should wait, plus (for the time being) a conversion to milliseconds.
Make qemu_run_timers return a bool to indicate progress.
Add QEMUClock to AioContext.
Run timers attached to clock in aio_poll
--- aio-posix.c | 16 +- aio-win32.c | 20 +- async.c | 2 + include/block/aio.h | 5 + include/qemu/timer.h | 15 +- main-loop.c | 9 +- qemu-timer.c | 599 ++++++++------------------------------------------ vl.c | 5 +- 8 files changed, 150 insertions(+), 521 deletions(-)
aio-posix.c +15 -1
@@ -173,6 +173,7 @@ bool aio_poll(AioContext *ctx, bool blocking) {AioHandler *node;int ret; +int timeout;bool busy, progress; progress = false;
@@ -195,6 +196,9 @@ bool aio_poll(AioContext *ctx, bool blocking)return true; } +/* Run our timers */ + progress |=qemu_run_timers (ctx->clock); + ctx->walking_handlers++;g_array_set_size (ctx->pollfds,0 );
@@ -232,9 +236,10 @@ bool aio_poll(AioContext *ctx, bool blocking) }/* wait until next event */ + timeout =qemu_timeout_ns_to_ms (qemu_clock_deadline_all_ns ()); ret =g_poll ((GPollFD *)ctx->pollfds->data, ctx->pollfds->len, - blocking ?-1 :0 ); + blocking ? timeout :0 );/* if we have any readable fds, dispatch event */ if (ret >0 ) {
@@ -250,6 +255,15 @@ bool aio_poll(AioContext *ctx, bool blocking) } } +if (blocking) { +/* Run the timers a second time. We do this because otherwise aio_wait +* will not note progress - and will stop a drain early - if we have +* a timer that was not ready to run entering g_poll but is ready +* after g_poll. This will only do anything if a timer has expired. +*/ + progress |=qemu_run_timers (ctx->clock); + } +assert (progress || busy);return true; }
aio-win32.c +18 -2
@@ -98,6 +98,7 @@ bool aio_poll(AioContext *ctx, bool blocking)HANDLE events[MAXIMUM_WAIT_OBJECTS +1 ];bool busy, progress;int count; +int timeout; progress = false;
@@ -111,6 +112,9 @@ bool aio_poll(AioContext *ctx, bool blocking) progress = true; } +/* Run timers */ + progress |=qemu_run_timers (ctx->clock); +/* * Then dispatch any pending callbacks from the GSource. *
@@ -174,8 +178,11 @@ bool aio_poll(AioContext *ctx, bool blocking)/* wait until next event */ while (count >0 ) { -int timeout = blocking ?INFINITE :0 ; -int ret =WaitForMultipleObjects (count, events, FALSE, timeout); +int ret; + + timeout = blocking ? +qemu_timeout_ns_to_ms (qemu_clock_deadline_all_ns ()) :0 ; + ret =WaitForMultipleObjects (count, events, FALSE, timeout);/* if we have any signaled events, dispatch event */ if ((DWORD ) (ret -WAIT_OBJECT_0 ) >= count) {
@@ -214,6 +221,15 @@ bool aio_poll(AioContext *ctx, bool blocking) events[ret -WAIT_OBJECT_0 ] = events[--count]; } +if (blocking) { +/* Run the timers a second time. We do this because otherwise aio_wait +* will not note progress - and will stop a drain early - if we have +* a timer that was not ready to run entering g_poll but is ready +* after g_poll. This will only do anything if a timer has expired. +*/ + progress |=qemu_run_timers (ctx->clock); + } +assert (progress || busy);return true; }
async.c +2 -0
@@ -177,6 +177,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); +qemu_free_clock (ctx->clock); }static GSourceFuncs aio_source_funcs = {
@@ -215,6 +216,7 @@ AioContext *aio_context_new(void)aio_set_event_notifier (ctx, &ctx->notifier, (EventNotifierHandler *) event_notifier_test_and_clear,NULL ); + ctx->clock =qemu_new_clock (QEMU_CLOCK_REALTIME );return ctx; }
include/block/aio.h +5 -0
@@ -41,6 +41,8 @@ typedef struct AioHandler AioHandler;typedef void QEMUBHFunc (void *opaque);typedef void IOHandler (void *opaque); +typedef struct QEMUClock QEMUClock ; + typedef struct AioContext { GSource source;
@@ -69,6 +71,9 @@ typedef struct AioContext {/* Thread pool for performing work and receiving completion callbacks */ struct ThreadPool *thread_pool; + +/* Clock for calling timers */ +QEMUClock *clock; } AioContext;/* Returns 1 if there are still outstanding AIO requests; 0 otherwise */
include/qemu/timer.h +11 -4
@@ -11,6 +11,10 @@#define SCALE_US 1000#define SCALE_NS 1 +#define QEMU_CLOCK_REALTIME 0 +#define QEMU_CLOCK_VIRTUAL 1 +#define QEMU_CLOCK_HOST 2 +typedef struct QEMUClock QEMUClock ;typedef void QEMUTimerCB (void *opaque);
@@ -32,10 +36,15 @@ extern QEMUClock *vm_clock;the virtual clock. */ extern QEMUClock *host_clock; +QEMUClock *qemu_new_clock (int type); +void qemu_free_clock (QEMUClock *clock);int64_t qemu_get_clock_ns (QEMUClock *clock);int64_t qemu_clock_has_timers (QEMUClock *clock);int64_t qemu_clock_expired (QEMUClock *clock);int64_t qemu_clock_deadline (QEMUClock *clock); +int64_t qemu_clock_deadline_ns (QEMUClock *clock); +int64_t qemu_clock_deadline_all_ns (void ); +int qemu_timeout_ns_to_ms (int64_t ns);void qemu_clock_enable (QEMUClock *clock,bool enabled);void qemu_clock_warp (QEMUClock *clock);
@@ -53,11 +62,9 @@ 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 ); -void configure_alarms (char const *opt); +bool qemu_run_timers (QEMUClock *clock); +bool qemu_run_all_timers (void );void init_clocks (void ); -int init_timer_alarm (void );int64_t cpu_get_ticks (void );void cpu_enable_ticks (void );
main-loop.c +5 -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) {
@@ -449,6 +445,11 @@ int main_loop_wait(int nonblocking) {int ret;uint32_t timeout =UINT32_MAX ; +int32_t timer_timeout =qemu_timeout_ns_to_ms (qemu_clock_deadline_all_ns ()); + +if (timer_timeout >=0 ) { + timeout = timer_timeout; + }if (nonblocking) { timeout =0 ;
qemu-timer.c +90 -509
687 lines not on this page · raw message
vl.c +4 -1
@@ -3714,7 +3714,10 @@ int main(int argc, char **argv, char **envp) old_param =1 ;break ;case QEMU_OPTION_clock: -configure_alarms (optarg); +/* Once upon a time we did: +* configure_alarms(optarg); +* here. This is stubbed out for compatibility. +*/ break ;case QEMU_OPTION_startdate:configure_rtc_date_offset (optarg,1 );
Signature · 1 line
1.7.9.5
Re: [Qemu-devel] [PATCH] [RFC] aio/timers: Drop alarm timers; introduce QEMUClock to AioContext; run timers in aio_poll
Quoted 4 lines · Alex Bligh
[ This is a patch for RFC purposes only. It is compile tested on Linux
x86_64only and passes make check (or rather did before make check started dying in the boot order test - different bug). I'd like to know whether I'm going in the right direction ]
Looks promising.
Quoted 7 lines
We no longer need alarm timers to trigger QEMUTimer as we'll be polling them in
aio_poll.Remove static declaration from
qemu_new_clockand introduceqemu_free_clock.Maintain a list of QEMUClocks.
Quoted 8 lines
Introduce
qemu_clock_deadline_nsandqemu_clock_deadine_all_nswhich calculate how longaio_polletc. should wait, plus (for the time being) a conversion to milliseconds.Make
qemu_run_timersreturn a bool to indicate progress.Add QEMUClock to AioContext.
Run timers attached to clock in
aio_poll
Too many logical changes for a single patch :). Please split this into a series.
qemu-timer.c struct QEMUClock { · line 48
Quoted 6 lines
qemu-timer.c +2
-0@@ -52,6 +44,8 @@ struct QEMUClock {int type;bool enabled; + +QLIST_ENTRY (QEMUClock) list;
Please avoid global state. AioContext should be usable from multiple threads, this list would require synchronization.
main-loop.c should call functions to run timers and calculate the nearest deadline on vm_clock/host_clock/rt_clock. It knows about them, they are defined in <qemu/timer.h>.
That way no list is needed.
Re: [Qemu-devel] [PATCH] [RFC] aio/timers: Drop alarm timers; introduce QEMUClock to AioContext; run timers in aio_poll
In reply to Alex BlighQuoted 22 lines · Alex Bligh
[ This is a patch for RFC purposes only. It is compile tested on Linux
x86_64only and passes make check (or rather did before make check started dying in the boot order test - different bug). I'd like to know whether I'm going in the right direction ]We no longer need alarm timers to trigger QEMUTimer as we'll be polling them in
aio_poll.Remove static declaration from
qemu_new_clockand introduceqemu_free_clock.Maintain a list of QEMUClocks.
Introduce
qemu_clock_deadline_nsandqemu_clock_deadine_all_nswhich calculate how longaio_polletc. should wait, plus (for the time being) a conversion to milliseconds.Make
qemu_run_timersreturn a bool to indicate progress.Add QEMUClock to AioContext.
Run timers attached to clock in
aio_pollSigned-off-by Alex Bligh <[email protected]>
Quoted 10 lines
--- aio-posix.c | 16 +- aio-win32.c | 20 +- async.c | 2 + include/block/aio.h | 5 + include/qemu/timer.h | 15 +- main-loop.c | 9 +- qemu-timer.c | 599 ++++++++------------------------------------------ vl.c | 5 +- 8 files changed, 150 insertions(+), 521 deletions(-)
I think you've already split this into a series. Will review that, just catching up on your emails :).
Stefan
Am 16.07.2013 um 18:55 hat Paolo Bonzini geschrieben:
Quoted 9 lines
Quoted 8 lines
Quoted 4 lines
But I wouldn't introduce a
new one-off concept (almost as much of a hack as idle BHs), I would rather reuse as much code as possible from QEMUTimer/QEMUClock. I must admit I don't have a clear idea of how the API would look like.So the reason I was trying to avoid using QEMUTimer stuff was that bh's get called from
aio_polland it was not evident that all timers would be safe to call fromaio_poll.
Quoted 7 lines
It wouldn't.
Quoted 2 lines
What do you think? In the end I thought the
schedule_bh_atstuff was simpler.It is simpler, but I'm not sure it is the right API. Of course, if Kevin and Stefan says it is, I have no problem with that.
Well, the one thing I'm pretty sure we need is an additional interface, so that existing timers continue to run only from the main loop, and new timers / timed BHs / whatever have the option to run in nested event loops as well.
I don't really care what the thing is called as long as it does what is needed. Timed BHs seemed to do that, so I agreed.
Kevin