aio / timers: add ppoll support with qemu_g_poll_ns
Subsystems (4)
RFC PATCH v2 3/7 aio / timers: add ppoll support with qemu_g_poll_ns
In reply to Alex BlighAdd qemu_g_poll_ns which works like g_poll but takes a nanosecond timeout.
--- configure | 19 +++++++++++++++++++ qemu-timer.c | 24 ++++++++++++++++++++++++ 2 files changed, 43 insertions(+)
configure +19 -0
@@ -2801,6 +2801,22 @@ if compile_prog "" "" ; then dup3=yes fi +# check for ppoll support +ppoll=no +cat > $TMPC << EOF +#include <poll.h> + +int main(void) +{ + struct pollfd pfd = { .fd = 0, .events = 0, .revents = 0 }; + ppoll(&pfd, 1, 0, 0); + return 0; +} +EOF +if compile_prog "" "" ; then + ppoll=yes +fi + # check for epoll support epoll=no cat > $TMPC << EOF
@@ -3792,6 +3808,9 @@ fi if test "$dup3" = "yes" ; then echo "CONFIG_DUP3=y" >> $config_host_mak fi +if test "$ppoll" = "yes" ; then + echo "CONFIG_PPOLL=y" >> $config_host_mak +fi if test "$epoll" = "yes" ; then echo "CONFIG_EPOLL=y" >> $config_host_mak fi
qemu-timer.c +24 -0
@@ -33,6 +33,10 @@#include <pthread.h> #endif +#ifdef CONFIG_PPOLL +#include <poll.h> +#endif +/***********************************************************/ /* timers */
@@ -181,6 +185,26 @@ int qemu_timeout_ns_to_ms(int64_t ns) } +/* qemu implementation of g_poll which uses a nanosecond timeout but is +* otherwise identical to g_poll +*/ +gint qemu_g_poll_ns (GPollFD *fds,guint nfds,int64_t timeout) +{ +#ifdef CONFIG_PPOLL +if (timeout <0 ) { +return ppoll ((struct pollfd *)fds, nfds,NULL ,NULL ); + }else { +struct timespec ts; + ts.tv_sec = timeout /1000000000LL ; + ts.tv_nsec = timeout %1000000000LL ; +return ppoll ((struct pollfd *)fds, nfds, &ts,NULL ); + } +#else +return g_poll (fds, nfds,qemu_timeout_ns_to_ms (timeout)); +#endif +} + +QEMUTimer *qemu_new_timer (QEMUClock *clock,int scale,QEMUTimerCB *cb,void *opaque) {
Signature · 1 line
1.7.9.5
RFC PATCH v2 4/7 aio / timers: Make qemu_run_timers and qemu_run_all_timers return progress
In reply to Alex BlighMake qemu_run_timers and qemu_run_all_timers return progress so that aio_poll etc. can determine whether a timer has been run.
--- include/qemu/timer.h | 4 ++-- qemu-timer.c | 17 +++++++++++------ 2 files changed, 13 insertions(+), 8 deletions(-)
include/qemu/timer.h +2 -2
@@ -63,8 +63,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 init_clocks (void );int64_t cpu_get_ticks (void );
qemu-timer.c +11 -6
@@ -299,13 +299,14 @@ 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; +bool progress = false;if (!clock->enabled) -return ; +return progress; current_time =qemu_get_clock_ns (clock);for (;;) {
@@ -319,7 +320,9 @@ void qemu_run_timers(QEMUClock *clock)/* run the callback (the timer list can be modified) */ ts->cb (ts->opaque); + progress = true; } +return progress; }int64_t qemu_get_clock_ns (QEMUClock *clock)
@@ -371,10 +374,12 @@ 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 ) {/* vm time timers */ -qemu_run_timers (vm_clock); -qemu_run_timers (rt_clock); -qemu_run_timers (host_clock); +bool progress = false; + progress |=qemu_run_timers (vm_clock); + progress |=qemu_run_timers (rt_clock); + progress |=qemu_run_timers (host_clock); +return progress; }
Signature · 1 line
1.7.9.5
v2 cover aio / timers: Add AioContext timers and use ppoll
In reply to a message not in the archiveThis patch series adds support for timers attached to an AioContext clock which get called within aio_poll.
In doing so it removes alarm timers and moves to use ppoll where possible.
This patch set 'sort of' passes make check (see below for caveat) including a new test harness for the aio timers, but has not been tested much beyond that. In particular, the win32 changes have not even been compile tested.
Caveat: make check fails one test only with:
ERROR:tests/test-aio.c:346:test_wait_event_notifier_noflush: assertion failed: (aio_poll(ctx, false))
As gar as I can tell, this check is incorrect, in that it checking aio_poll makes progress when in fact it should not make progress. I fixed an issue where aio_poll was (as far as I can tell) wrongly returning true on a timeout, and that generated this error.
Alex Bligh (7):
aio / timers: Remove alarm timers
aio / timers: qemu-timer.c utility functions and add list of clocks aio / timers: add ppoll support with qemu_g_poll_ns
aio / timers: Make qemu_run_timers and qemu_run_all_timers return
progress
aio / timers: Add a clock to AioContext
aio / timers: Switch to ppoll, run AioContext timers in
aio_poll/aio_dispatch
aio / timers: Add test harness for AioContext timers
aio-posix.c | 20 +- aio-win32.c | 20 +- async.c | 18 +- configure | 19 ++ include/block/aio.h | 5 + include/qemu/timer.h | 25 +- main-loop.c | 47 ++-- qemu-timer.c | 619 +++++++++----------------------------------------- tests/test-aio.c | 124 +++++++++- vl.c | 5 +- 10 files changed, 363 insertions(+), 539 deletions(-)
Signature · 1 line
1.7.9.5
RFC PATCH v2 5/7 aio / timers: Add a clock to AioContext
Add a clock to each AioContext and delete it when freed.
--- async.c | 2 ++ include/block/aio.h | 5 +++++ 2 files changed, 7 insertions(+)
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 */
Signature · 1 line
1.7.9.5
RFC PATCH v2 2/7 aio / timers: qemu-timer.c utility functions and add list of clocks
In reply to Alex BlighAdd utility functions to qemu-timer.c for nanosecond timing.
Ensure we keep track of all QEMUClocks on a list.
Add qemu_clock_deadline_ns and qemu_clock_deadline_all_ns to calculate deadlines to nanosecond accuracy.
Add utility function qemu_soonest_timeout to calculate soonest deadline.
Add qemu_timeout_ns_to_ms to convert a timeout in nanoseconds back to milliseconds for when ppoll is not used.
--- include/qemu/timer.h | 19 ++++++++++++ qemu-timer.c | 83 ++++++++++++++++++++++++++++++++++++++++++++++---- 2 files changed, 96 insertions(+), 6 deletions(-)
include/qemu/timer.h +19 -0
@@ -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,16 @@ 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); +gint qemu_g_poll_ns (GPollFD *fds,guint nfds,int64_t timeout);void qemu_clock_enable (QEMUClock *clock,bool enabled);void qemu_clock_warp (QEMUClock *clock);
@@ -61,6 +71,15 @@ int64_t cpu_get_ticks(void);void cpu_enable_ticks (void );void cpu_disable_ticks (void ); +static inline int64_t qemu_soonest_timeout (int64_t timeout1,int64_t timeout2) +{ +/* we can abuse the fact that -1 (which means infinite) is a maximal +* value when cast to unsigned. As this is disgusting, it's kept in +* one inline function. +*/ +return ((uint64_t ) timeout1 < (uint64_t ) timeout2) ? timeout1 : timeout2; +} +static inline QEMUTimer *qemu_new_timer_ns (QEMUClock *clock,QEMUTimerCB *cb,void *opaque) {
qemu-timer.c +77 -6
128 lines not on this page · raw message
Signature · 1 line
1.7.9.5
RFC PATCH v2 7/7 aio / timers: Add test harness for AioContext timers
In reply to Alex BlighAdd a test harness for AioContext timers. The g_source equivalent is unsatisfactory as it suffers from false wakeups.
--- tests/test-aio.c | 124 +++++++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 123 insertions(+), 1 deletion(-)
tests/test-aio.c +123 -1
@@ -1,5 +1,5 @@/* -* AioContext tests +* Aiocontext tests * * Copyright Red Hat, Inc. 2012 *
@@ -12,6 +12,7 @@#include <glib.h> #include "block/aio.h" +#include "qemu/timer.h" AioContext *ctx;
@@ -31,6 +32,15 @@ typedef struct {int max; } BHTestData; +typedef struct { +QEMUTimer *timer; +QEMUClock *clock; +int n; +int max; +int64_t ns; +AioContext *ctx; +}TimerTestData ; +static void bh_test_cb (void *opaque) {BHTestData *data = opaque;
@@ -39,6 +49,24 @@ static void bh_test_cb(void *opaque) } } +static void timer_test_cb (void *opaque) +{ +TimerTestData *data = opaque; +if (++data->n < data->max) { +qemu_mod_timer (data->timer, +qemu_get_clock_ns (data->clock) + data->ns); + } +} + +static void dummy_io_handler_read (void *opaque) +{ +} + +static int dummy_io_handler_flush (void *opaque) +{ +return 1 ; +} +static void bh_delete_cb (void *opaque) {BHTestData *data = opaque;
@@ -340,6 +368,51 @@ static void test_wait_event_notifier_noflush(void)event_notifier_cleanup (&data.e); } +static void test_timer_schedule (void ) +{ +TimerTestData data = { .n =0 , .ctx = ctx, .ns =SCALE_MS *750 , + .max =2 , .clock = ctx->clock }; +int pipefd[2 ]; + +/* aio_poll will not block to wait for timers to complete unless it has +* an fd to wait on. Fixing this breaks other tests. So create a dummy one. +*/ +g_assert (!pipe2 (pipefd,O_NONBLOCK )); +aio_set_fd_handler (ctx, pipefd[0 ], + dummy_io_handler_read,NULL , dummy_io_handler_flush, +NULL ); +aio_poll (ctx, false); + + data.timer =qemu_new_timer_ns (data.clock, timer_test_cb, &data); +qemu_mod_timer (data.timer,qemu_get_clock_ns (data.clock) + data.ns); + + g_assert_cmpint(data.n, ==, 0); + +g_assert (!aio_poll (ctx, false)); + g_assert_cmpint(data.n, ==, 0); + +sleep (1 ); + g_assert_cmpint(data.n, ==, 0); + +g_assert (aio_poll (ctx, false)); + g_assert_cmpint(data.n, ==, 1); + +g_assert (!aio_poll (ctx, false)); + g_assert_cmpint(data.n, ==, 1); + +g_assert (aio_poll (ctx, true)); + g_assert_cmpint(data.n, ==, 2); + +g_assert (!aio_poll (ctx, false)); + g_assert_cmpint(data.n, ==, 2); + +aio_set_fd_handler (ctx, pipefd[0 ],NULL ,NULL ,NULL ,NULL ); +close (pipefd[0 ]); +close (pipefd[1 ]); + +qemu_del_timer (data.timer); +} +/* Now the same tests, using the context as a GSource. They are * very similar to the ones above, with g_main_context_iteration * replacing aio_poll. However:
@@ -622,12 +695,59 @@ static void test_source_wait_event_notifier_noflush(void)event_notifier_cleanup (&data.e); } +static void test_source_timer_schedule (void ) +{ +TimerTestData data = { .n =0 , .ctx = ctx, .ns =SCALE_MS *750 , + .max =2 , .clock = ctx->clock }; +int pipefd[2 ]; +int64_t expiry; + +/* aio_poll will not block to wait for timers to complete unless it has +* an fd to wait on. Fixing this breaks other tests. So create a dummy one. +*/ +g_assert (!pipe2 (pipefd,O_NONBLOCK )); +aio_set_fd_handler (ctx, pipefd[0 ], + dummy_io_handler_read,NULL , dummy_io_handler_flush, +NULL ); +while (g_main_context_iteration (NULL , false)); + + data.timer =qemu_new_timer_ns (data.clock, timer_test_cb, &data); + expiry =qemu_get_clock_ns (data.clock) + data.ns; +qemu_mod_timer (data.timer, expiry); + + g_assert_cmpint(data.n, ==, 0); + +sleep (1 ); + g_assert_cmpint(data.n, ==, 0); + +g_assert (g_main_context_iteration (NULL , false)); + g_assert_cmpint(data.n, ==, 1); + +/* The comment above was not kidding when it said this wakes up itself */ +do { +g_assert (g_main_context_iteration (NULL , true)); + }while (qemu_get_clock_ns (data.clock) <= expiry); +sleep (1 ); +g_main_context_iteration (NULL , false); + + g_assert_cmpint(data.n, ==, 2); + +aio_set_fd_handler (ctx, pipefd[0 ],NULL ,NULL ,NULL ,NULL ); +close (pipefd[0 ]); +close (pipefd[1 ]); + +qemu_del_timer (data.timer); +} + +/* End of tests. */ int main (int argc,char **argv) {GSource *src; +init_clocks (); + ctx =aio_context_new (); src =aio_get_g_source (ctx);g_source_attach (src,NULL );
@@ -648,6 +768,7 @@ int main(int argc, char **argv)g_test_add_func ("/aio/event/wait" , test_wait_event_notifier);g_test_add_func ("/aio/event/wait/no-flush-cb" , test_wait_event_notifier_noflush);g_test_add_func ("/aio/event/flush" , test_flush_event_notifier); +g_test_add_func ("/aio/timer/schedule" , test_timer_schedule);g_test_add_func ("/aio-gsource/notify" , test_source_notify);g_test_add_func ("/aio-gsource/flush" , test_source_flush);
@@ -662,5 +783,6 @@ int main(int argc, char **argv)g_test_add_func ("/aio-gsource/event/wait" , test_source_wait_event_notifier);g_test_add_func ("/aio-gsource/event/wait/no-flush-cb" , test_source_wait_event_notifier_noflush);g_test_add_func ("/aio-gsource/event/flush" , test_source_flush_event_notifier); +g_test_add_func ("/aio-gsource/timer/schedule" , test_source_timer_schedule);return g_test_run (); }
Signature · 1 line
1.7.9.5
RFC PATCH v2 6/7 aio / timers: Switch to ppoll, run AioContext timers in aio_poll/aio_dispatch
In reply to Alex BlighSwitch to ppoll (or rather qemu_g_poll_ns which will use ppoll if available).
Set timeouts for aio, g_source, and mainloop from earliest timer deadline.
Run timers for AioContext (only) in aio_poll/aio_dispatch.
--- aio-posix.c | 20 +++++++++++++------- aio-win32.c | 20 ++++++++++++++++++-- async.c | 16 ++++++++++++++-- main-loop.c | 43 ++++++++++++++++++++++++++++++++----------- 4 files changed, 77 insertions(+), 22 deletions(-)
aio-posix.c +13 -7
@@ -166,6 +166,10 @@ static bool aio_dispatch(AioContext *ctx)g_free (tmp); } } + +/* Run our timers */ + progress |=qemu_run_timers (ctx->clock); +return progress; }
@@ -232,9 +236,9 @@ bool aio_poll(AioContext *ctx, bool blocking) }/* wait until next event */ - ret =g_poll ((GPollFD *)ctx->pollfds->data, - ctx->pollfds->len, - blocking ?-1 :0 ); + ret =qemu_g_poll_ns ((GPollFD *)ctx->pollfds->data, + ctx->pollfds->len, + blocking ?qemu_clock_deadline_all_ns () :0 );/* if we have any readable fds, dispatch event */ if (ret >0 ) {
@@ -245,11 +249,13 @@ bool aio_poll(AioContext *ctx, bool blocking) node->pfd.revents = pfd->revents; } } -if (aio_dispatch (ctx)) { - progress = true; - } + } + +/* Run dispatch even if there were no readable fds to run timers */ +if (aio_dispatch (ctx)) { + progress = true; }assert (progress || busy); -return true; +return progress; }
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 +14 -2
@@ -123,13 +123,16 @@ aio_ctx_prepare(GSource *source, gint *timeout) {AioContext *ctx = (AioContext *) source;QEMUBH *bh; +int deadline;for (bh = ctx->first_bh; bh; bh = bh->next) {if (!bh->deleted && bh->scheduled) {if (bh->idle) {/* idle bottom halves will be polled at least * every 10ms */ - *timeout =10 ; +if ((*timeout <0 ) || (*timeout >10 )) { + *timeout =10 ; + } }else {/* non-idle bottom halves will be executed * immediately */
@@ -139,6 +142,15 @@ aio_ctx_prepare(GSource *source, gint *timeout) } } + deadline =qemu_timeout_ns_to_ms (qemu_clock_deadline_ns (ctx->clock)); +if (deadline ==0 ) { + *timeout =0 ; +return true; + }else if ((deadline >0 ) && + ((*timeout <0 ) || (deadline < *timeout))) { + *timeout = deadline; + } +return false; }
@@ -153,7 +165,7 @@ aio_ctx_check(GSource *source)return true; } } -return aio_pending (ctx); +return aio_pending (ctx) || (qemu_clock_deadline_ns (ctx->clock) >=0 ); } static gboolean
main-loop.c +32 -11
110 lines not on this page · raw message
Signature · 1 line
1.7.9.5
RFC PATCH v2 1/7 aio / timers: Remove alarm timers
In reply to Alex BlighRemove alarm timers from qemu-timers.c in anticipation of using timeouts for g_poll / p_poll instead.
--- include/qemu/timer.h | 2 - main-loop.c | 4 - qemu-timer.c | 501 +------------------------------------------------- vl.c | 5 +- 4 files changed, 7 insertions(+), 505 deletions(-)
include/qemu/timer.h +0 -2
@@ -55,9 +55,7 @@ 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);void init_clocks (void ); -int init_timer_alarm (void );int64_t cpu_get_ticks (void );void cpu_enable_ticks (void );
main-loop.c +0 -4
@@ -131,10 +131,6 @@ int qemu_init_main_loop(void)GSource *src;init_clocks (); -if (init_timer_alarm () <0 ) { -fprintf (stderr,"could not initialize alarm timer\n" ); -exit (1 ); - } ret =qemu_signal_init ();if (ret) {
qemu-timer.c +3 -498
539 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] [PATCHv2] [RFC 2/7] aio / timers: qemu-timer.c utility functions and add list of clocks
In reply to Alex Blighinclude/qemu/timer.h extern QEMUClock *vm_clock; · line 48
Quoted 4 lines · Alex Bligh
include/qemu/timer.h +4
-0+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); +gint qemu_g_poll_ns (GPollFD *fds,guint nfds,int64_t timeout);
Why continue with the g_ prefix here? Surely qemu_poll_ns is sufficient.
r~
Re: [Qemu-devel] [PATCHv2] [RFC 2/7] aio / timers: qemu-timer.c utility functions and add list of clocks
Richard,
Quoted 7 lines · --On 23 July 2013 13:09:18 -0800 Richard Henderson <[email protected]> wrote:
include/qemu/timer.h extern QEMUClock *vm_clock; · line 48
Quoted 4 lines · Alex Bligh
include/qemu/timer.h +4
-0+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); +gint qemu_g_poll_ns (GPollFD *fds,guint nfds,int64_t timeout);Why continue with the g_ prefix here? Surely
qemu_poll_nsis sufficient.
Only because it was a g_ type function (takes GPollFD*, guint, and returns gint). Quite happy to make it a qemu_poll_ns. Perhaps I should make it take uint and return int at the same time.
TBH I am confused as to why we use g_poll at all. I thought originally we were using it for win32, but that doesn't use g_poll. Given poll is POSIX, what platforms do we target which are glib, !win32, and !posix? Using straight posix would be easier.
Signature · 1 line
Alex Bligh
Re: [Qemu-devel] [PATCHv2] [RFC 1/7] aio / timers: Remove alarm timers
In reply to Alex BlighQuoted 10 lines · Alex Bligh
Remove alarm timers from
qemu-timers.cin anticipation of using timeouts forg_poll/p_pollinstead.Signed-off-by Alex Bligh <[email protected]>--- include/qemu/timer.h | 2 - main-loop.c | 4 - qemu-timer.c | 501 +------------------------------------------------- vl.c | 5 +- 4 files changed, 7 insertions(+), 505 deletions(-)
This should be one of the last patches so qemu.git remains bisectable. Only remove the alarm timer once the event loops are already using the timeout argument.
qemu-timer.c static QEMUClock *qemu_new_clock(int type) · line 252
Quoted 9 lines
qemu-timer.c +0
-4@@ -245,11 +82,7 @@ static QEMUClock *qemu_new_clock(int type)void qemu_clock_enable (QEMUClock *clock,bool enabled) { -bool old = clock->enabled; clock->enabled = enabled; -if (enabled && !old) { -qemu_rearm_alarm_timer (alarm_timer); - }
If this function is supposed to work when called from another thread (e.g. vcpu thread), then you need to call qemu_notify_event(). For AioContext clocks that should be aio_notify() with the relevant AioContext, but we don't need that yet.
Quoted 7 lines
qemu-timer.c +0
-0}int64_t qemu_clock_has_timers (QEMUClock *clock)@@ -340,10 +173,9 @@ void qemu_mod_timer_ns(QEMUTimer *ts, int64_t expire_time)/* Rearm if necessary */ if (pt == &ts->clock->active_timers) {
qemu-timer.c void qemu_mod_timer_ns(QEMUTimer *ts, int64_t expire_time) · line 181
Quoted 10 lines
qemu-timer.c +3
-4-if (!alarm_timer->pending) { -qemu_rearm_alarm_timer (alarm_timer); - } -/* Interrupt execution to force deadline recalculation. */ +/* Interrupt execution to force deadline recalculation. +* FIXME: Do we need to do this now? +*/ qemu_clock_warp (ts->clock);if (use_icount) {qemu_notify_event ();
Same here.
Quoted 5 lines
vl.c +0
-0
vl.c int main(int argc, char **argv, char **envp) · line 3721
Quoted 9 lines
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 ;
This could be made clearer to say outright that the options don't exist anymore:
/* Clock options no longer exist. Keep this option for backward
* compatibility.
*/
Re: [Qemu-devel] [PATCHv2] [RFC 1/7] aio / timers: Remove alarm timers
Il 25/07/2013 11:10, Stefan Hajnoczi ha scritto:
Quoted 18 lines · Alex Bligh
Quoted 10 lines
Remove alarm timers from
qemu-timers.cin anticipation of using timeouts forg_poll/p_pollinstead.Signed-off-by Alex Bligh <[email protected]>--- include/qemu/timer.h | 2 - main-loop.c | 4 - qemu-timer.c | 501 +------------------------------------------------- vl.c | 5 +- 4 files changed, 7 insertions(+), 505 deletions(-)This should be one of the last patches so qemu.git remains bisectable. Only remove the alarm timer once the event loops are already using the timeout argument.
Quoted 2 lines
(quoted diff) +0
-0@@ -245,11 +82,7 @@ static QEMUClock *qemu_new_clock(int type)
Quoted 12 lines
qemu-timer.c static QEMUClock *qemu_new_clock(int type) · line 252
Quoted 7 lines
qemu-timer.c +0
-4void qemu_clock_enable (QEMUClock *clock,bool enabled) { -bool old = clock->enabled; clock->enabled = enabled; -if (enabled && !old) { -qemu_rearm_alarm_timer (alarm_timer); - }If this function is supposed to work when called from another thread (e.g. vcpu thread), then you need to call
qemu_notify_event(). For AioContext clocks that should beaio_notify()with the relevant AioContext, but we don't need that yet.
In general QEMUClock should have an AioContext pointer so that it can call aio_notify.
Paolo
Quoted 42 lines
Quoted 7 lines
qemu-timer.c +0
-0}int64_t qemu_clock_has_timers (QEMUClock *clock)@@ -340,10 +173,9 @@ void qemu_mod_timer_ns(QEMUTimer *ts, int64_t expire_time)/* Rearm if necessary */ if (pt == &ts->clock->active_timers) {qemu-timer.c void qemu_mod_timer_ns(QEMUTimer *ts, int64_t expire_time) · line 181
Quoted 10 lines
qemu-timer.c +3
-4-if (!alarm_timer->pending) { -qemu_rearm_alarm_timer (alarm_timer); - } -/* Interrupt execution to force deadline recalculation. */ +/* Interrupt execution to force deadline recalculation. +* FIXME: Do we need to do this now? +*/ qemu_clock_warp (ts->clock);if (use_icount) {qemu_notify_event ();Same here.
Quoted 5 lines
vl.c +0
-0vl.c int main(int argc, char **argv, char **envp) · line 3721
Quoted 9 lines
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 ;This could be made clearer to say outright that the options don't exist anymore:
/* Clock options no longer exist. Keep this option for backward
* compatibility.
*/
Re: [Qemu-devel] [PATCHv2] [RFC 2/7] aio / timers: qemu-timer.c utility functions and add list of clocks
In reply to Alex BlighQuoted 11 lines · Alex Bligh
Add utility functions to
qemu-timer.cfor nanosecond timing.Ensure we keep track of all QEMUClocks on a list.
Add
qemu_clock_deadline_nsandqemu_clock_deadline_all_nsto calculate deadlines to nanosecond accuracy.Add utility function
qemu_soonest_timeoutto calculate soonest deadline.Add
qemu_timeout_ns_to_msto convert a timeout in nanoseconds back to milliseconds for when ppoll is not used.
Please split this into smaller patches. There are several logical changes happening here.
include/qemu/timer.h int64_t cpu_get_ticks(void); · line 80
Quoted 11 lines
include/qemu/timer.h +7
-0@@ -61,6 +71,15 @@ int64_t cpu_get_ticks(void);void cpu_enable_ticks (void );void cpu_disable_ticks (void ); +static inline int64_t qemu_soonest_timeout (int64_t timeout1,int64_t timeout2) +{ +/* we can abuse the fact that -1 (which means infinite) is a maximal +* value when cast to unsigned. As this is disgusting, it's kept in +* one inline function. +*/ +return ((uint64_t ) timeout1 < (uint64_t ) timeout2) ? timeout1 : timeout2;
The straightforward version isn't much longer than the commented casting trick:
if (timeout1 == -1) {
return timeout2;
} else if (timeout2 == -1) {
return timeout1;
} else {
return timeout1 < timeout2 ? timeout1 : timeout2;
}qemu-timer.c struct QEMUClock { · line 48
Quoted 6 lines
qemu-timer.c +2
-0@@ -48,6 +44,8 @@ struct QEMUClock {int type;bool enabled; + +QLIST_ENTRY (QEMUClock) list;
I don't think global state is necessary. The run_timers()/clock_deadline() users have QEMUClock references, they can just call per-QEMUClock functions instead of requiring qemu-timers.c to keep a list.
This way AioContext clocks are safe to use from threads.
qemu-timer.c static QEMUClock *qemu_new_clock(int type) · line 88
Quoted 4 lines
qemu-timer.c +4
-0+void qemu_free_clock (QEMUClock *clock) +{ +QLIST_REMOVE (clock, list); +g_free (clock);
assert that there are no timers left?
Re: [Qemu-devel] [PATCHv2] [RFC 2/7] aio / timers: qemu-timer.c utility functions and add list of clocks
In reply to Alex BlighQuoted 1 line · Alex Bligh
+gint
qemu_g_poll_ns(GPollFD *fds, guint nfds, int64_t timeout);
You didn't define the function in this patch.
Re: [Qemu-devel] [PATCHv2] [RFC 6/7] aio / timers: Switch to ppoll, run AioContext timers in aio_poll/aio_dispatch
In reply to Alex BlighQuoted 7 lines · Alex Bligh
aio-posix.c +0
-3@@ -245,11 +249,13 @@ bool aio_poll(AioContext *ctx, bool blocking) node->pfd.revents = pfd->revents; } } -if (aio_dispatch (ctx)) { - progress = true; - }
aio-posix.c bool aio_poll(AioContext *ctx, bool blocking) · line 260
Quoted 10 lines
aio-posix.c +6
-1+ } + +/* Run dispatch even if there were no readable fds to run timers */ +if (aio_dispatch (ctx)) { + progress = true; }assert (progress || busy); -return true; +return progress;
Now aio_poll() can return false when it used to return true?
Quoted 5 lines
aio-win32.c +1
-0@@ -214,6 +221,15 @@ bool aio_poll(AioContext *ctx, bool blocking) events[ret -WAIT_OBJECT_0 ] = events[--count]; } +if (blocking) {
aio-win32.c bool aio_poll(AioContext *ctx, bool blocking) · line 234
Quoted 10 lines
aio-win32.c +8
-0+/* 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;
You didn't update this to return just progress.
Quoted 10 lines
(quoted diff) +0
-0}async.c +1
-0@@ -123,13 +123,16 @@ aio_ctx_prepare(GSource *source, gint *timeout) {AioContext *ctx = (AioContext *) source;QEMUBH *bh; +int deadline;
async.c aio_ctx_prepare(GSource *source, gint *timeout) · line 135
Quoted 10 lines
async.c +3
-1for (bh = ctx->first_bh; bh; bh = bh->next) {if (!bh->deleted && bh->scheduled) {if (bh->idle) {/* idle bottom halves will be polled at least * every 10ms */ - *timeout =10 ; +if ((*timeout <0 ) || (*timeout >10 )) { + *timeout =10 ; + }
Use the function you introduced earlier to return the nearest timeout?
Quoted 4 lines
async.c +0
-0}else {/* non-idle bottom halves will be executed * immediately */
async.c aio_ctx_prepare(GSource *source, gint *timeout) · line 151
Quoted 10 lines
async.c +7
-0@@ -139,6 +142,15 @@ aio_ctx_prepare(GSource *source, gint *timeout) } } + deadline =qemu_timeout_ns_to_ms (qemu_clock_deadline_ns (ctx->clock)); +if (deadline ==0 ) { + *timeout =0 ; +return true; + }else if ((deadline >0 ) && + ((*timeout <0 ) || (deadline < *timeout))) { + *timeout = deadline;
Same here.
async.c static void glib_pollfds_fill(uint32_t *cur_timeout) · line 177
Quoted 10 lines
async.c +4
-2@@ -170,9 +171,13 @@ static void glib_pollfds_fill(uint32_t *cur_timeout) glib_n_poll_fds); }while (n != glib_n_poll_fds); -if (timeout >=0 && timeout < *cur_timeout) { - *cur_timeout = timeout; +if (timeout <0 ) { + timeout_ns =-1 ; + }else { + timeout_ns = (int64_t )timeout * (int64_t )SCALE_MS ;
Indentation.
Re: [Qemu-devel] [PATCHv2] [RFC 1/7] aio / timers: Remove alarm timers
In reply to Paolo BonziniQuoted 7 lines · --On 25 July 2013 11:11:22 +0200 Paolo Bonzini <[email protected]> wrote:
Quoted 4 lines
If this function is supposed to work when called from another thread (e.g. vcpu thread), then you need to call
qemu_notify_event(). For AioContext clocks that should beaio_notify()with the relevant AioContext, but we don't need that yet.In general QEMUClock should have an AioContext pointer so that it can call
aio_notify.
OK, if we do that, disregard my comment about always using qemu_notify_event.
Signature · 1 line
Alex Bligh
Re: [Qemu-devel] [PATCHv2] [RFC 1/7] aio / timers: Remove alarm timers
In reply to Alex BlighIl 25/07/2013 11:37, Alex Bligh ha scritto:
Quoted 23 lines
Stefan,
Quoted 3 lines
This should be one of the last patches so qemu.git remains bisectable. Only remove the alarm timer once the event loops are already using the timeout argument.
OK
Quoted 14 lines
qemu-timer.c static QEMUClock *qemu_new_clock(int type) · line 252
Quoted 9 lines
qemu-timer.c +0
-4@@ -245,11 +82,7 @@ static QEMUClock *qemu_new_clock(int type)void qemu_clock_enable (QEMUClock *clock,bool enabled) { -bool old = clock->enabled; clock->enabled = enabled; -if (enabled && !old) { -qemu_rearm_alarm_timer (alarm_timer); - }If this function is supposed to work when called from another thread (e.g. vcpu thread), then you need to call
qemu_notify_event(). For AioContext clocks that should beaio_notify()with the relevant AioContext, but we don't need that yet.
Quoted 3 lines
Each AioContext knows which clock it has but each clock doesn't know if it's part of an AioContext. I suggest this is infrequent enough that always using
qemu_notify_event()would be OK. That should interrupt any poll.
No, qemu_notify_event() only interrupts the main clock's poll.
Paolo
Re: [Qemu-devel] [PATCHv2] [RFC 2/7] aio / timers: qemu-timer.c utility functions and add list of clocks
In reply to Stefan HajnocziStefan,
Quoted 8 lines · --On 25 July 2013 11:19:29 +0200 Stefan Hajnoczi <[email protected]> wrote:
Quoted 7 lines · Alex Bligh
Add utility functions to
qemu-timer.cfor nanosecond timing.Ensure we keep track of all QEMUClocks on a list.
Add
qemu_clock_deadline_nsandqemu_clock_deadline_all_nsto calculate deadlines to nanosecond accuracy.
Quoted 7 lines
Quoted 4 lines
Add utility function
qemu_soonest_timeoutto calculate soonest deadline.Add
qemu_timeout_ns_to_msto convert a timeout in nanoseconds back to milliseconds for when ppoll is not used.Please split this into smaller patches. There are several logical changes happening here.
OK
Quoted 16 lines
include/qemu/timer.h int64_t cpu_get_ticks(void); · line 81
Quoted 12 lines
include/qemu/timer.h +6
-0@@ -61,6 +71,15 @@ int64_t cpu_get_ticks(void);void cpu_enable_ticks (void );void cpu_disable_ticks (void ); +static inline int64_t qemu_soonest_timeout (int64_t timeout1,int64_t timeout2) +{ +/* we can abuse the fact that -1 (which means infinite) is a maximal +* value when cast to unsigned. As this is disgusting, it's kept in +* one inline function. +*/ +return ((uint64_t ) timeout1 < (uint64_t ) timeout2) ? timeout1 : timeout2;The straightforward version isn't much longer than the commented casting trick:
Quoted 7 lines
if (timeout1 == -1) {
return timeout2;
} else if (timeout2 == -1) {
return timeout1;
} else {
return timeout1 < timeout2 ? timeout1 : timeout2;
}Well, it should be (timeout1 < 0) for consistency. It may be a micro optimisation but I'm pretty sure the casting trick will produce better code. With the comment, it's arguably more readable too.
Quoted 12 lines
qemu-timer.c struct QEMUClock { · line 48
Quoted 6 lines
qemu-timer.c +2
-0@@ -48,6 +44,8 @@ struct QEMUClock {int type;bool enabled; + +QLIST_ENTRY (QEMUClock) list;I don't think global state is necessary. The run_timers()/clock_deadline() users have QEMUClock references, they can just call per-QEMUClock functions instead of requiring
qemu-timers.cto keep a list.
Quoted 1 line
This way AioContext clocks are safe to use from threads.
I don't think that's true. The thing that uses the list is qemu_run_all_timers which doesn't have a list of all *clocks* (as opposed to timers).
I had assumed (possibly wrongly) that AioContexts and clocks were only created / deleted from the main thread. This is certainly true currently as there is only one AioContext (as far as I can see - perhaps I'm wrong here) and the other clocks are created on init.
If we need to support locking here I think we probably must.
Quoted 6 lines
qemu-timer.c static QEMUClock *qemu_new_clock(int type) · line 88
Quoted 4 lines
qemu-timer.c +4
-0+void qemu_free_clock (QEMUClock *clock) +{ +QLIST_REMOVE (clock, list); +g_free (clock);assert that there are no timers left?
Yes I wasn't quite sure of the right semantics here as no clocks are currently ever destroyed. I'm not quite sure how we know all timers are destroyed when an AioContext is destroyed. Should I go and manually free them or assert the right way?
Signature · 1 line
Alex Bligh
Re: [Qemu-devel] [PATCHv2] [RFC 2/7] aio / timers: qemu-timer.c utility functions and add list of clocks
In reply to Stefan HajnocziQuoted 4 lines · --On 25 July 2013 11:21:33 +0200 Stefan Hajnoczi <[email protected]> wrote:
Quoted 1 line · Alex Bligh
+gint
qemu_g_poll_ns(GPollFD *fds, guint nfds, int64_t timeout);You didn't define the function in this patch.
Will fix
Signature · 1 line
Alex Bligh
Re: [Qemu-devel] [PATCHv2] [RFC 1/7] aio / timers: Remove alarm timers
In reply to Stefan HajnocziStefan,
Quoted 3 lines
This should be one of the last patches so qemu.git remains bisectable. Only remove the alarm timer once the event loops are already using the timeout argument.
OK
Quoted 14 lines
qemu-timer.c static QEMUClock *qemu_new_clock(int type) · line 252
Quoted 9 lines
qemu-timer.c +0
-4@@ -245,11 +82,7 @@ static QEMUClock *qemu_new_clock(int type)void qemu_clock_enable (QEMUClock *clock,bool enabled) { -bool old = clock->enabled; clock->enabled = enabled; -if (enabled && !old) { -qemu_rearm_alarm_timer (alarm_timer); - }If this function is supposed to work when called from another thread (e.g. vcpu thread), then you need to call
qemu_notify_event(). For AioContext clocks that should beaio_notify()with the relevant AioContext, but we don't need that yet.
Each AioContext knows which clock it has but each clock doesn't know if it's part of an AioContext. I suggest this is infrequent enough that always using qemu_notify_event() would be OK. That should interrupt any poll.
Quoted 4 lines
Quoted 4 lines
qemu-timer.c +0
-0int64_t qemu_clock_has_timers (QEMUClock *clock)@@ -340,10 +173,9 @@ void qemu_mod_timer_ns(QEMUTimer *ts, int64_t expire_time)
Quoted 14 lines
Quoted 11 lines
qemu-timer.c +3
-4/* Rearm if necessary */ if (pt == &ts->clock->active_timers) { -if (!alarm_timer->pending) { -qemu_rearm_alarm_timer (alarm_timer); - } -/* Interrupt execution to force deadline recalculation. */ +/* Interrupt execution to force deadline recalculation. +* FIXME: Do we need to do this now? +*/ qemu_clock_warp (ts->clock);if (use_icount) {Quoted 1 line
qemu_notify_event();Same here.
I think I just need to delete the FIXME comment.
Quoted 18 lines
Quoted 5 lines
vl.c +0
-0vl.c int main(int argc, char **argv, char **envp) · line 3721
Quoted 9 lines
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 ;This could be made clearer to say outright that the options don't exist anymore:
Quoted 3 lines
/* Clock options no longer exist. Keep this option for backward
* compatibility.
*/
OK
Signature · 1 line
Alex Bligh
Re: [Qemu-devel] [PATCHv2] [RFC 0/7] aio / timers: Add AioContext timers and use ppoll
In reply to Alex BlighQuoted 30 lines · Alex Bligh
This patch series adds support for timers attached to an AioContext clock which get called within
aio_poll.In doing so it removes alarm timers and moves to use ppoll where possible.
This patch set 'sort of' passes make check (see below for caveat) including a new test harness for the aio timers, but has not been tested much beyond that. In particular, the win32 changes have not even been compile tested.
Caveat: make check fails one test only with:
ERROR:tests/test-aio.c:346:test_wait_event_notifier_noflush: assertion failed: (
aio_poll(ctx, false))As gar as I can tell, this check is incorrect, in that it checking
aio_pollmakes progress when in fact it should not make progress. I fixed an issue whereaio_pollwas (as far as I can tell) wrongly returning true on a timeout, and that generated this error.Alex Bligh (7):
aio / timers: Remove alarm timers
aio / timers:qemu-timer.cutility functions and add list of clocks aio / timers: add ppoll support withqemu_g_poll_ns
aio / timers: Makeqemu_run_timersandqemu_run_all_timersreturn
progress
aio / timers: Add a clock to AioContext
aio / timers: Switch to ppoll, run AioContext timers in
aio_poll/aio_dispatch
aio / timers: Add test harness for AioContext timers
Quoted 11 lines
aio-posix.c | 20 +- aio-win32.c | 20 +- async.c | 18 +- configure | 19 ++ include/block/aio.h | 5 + include/qemu/timer.h | 25 +- main-loop.c | 47 ++-- qemu-timer.c | 619 +++++++++----------------------------------------- tests/test-aio.c | 124 +++++++++- vl.c | 5 +- 10 files changed, 363 insertions(+), 539 deletions(-)
This looks promising for QEMU 1.7.
Please split into logical patches - some of the patches you posted are too big and do several independent things.
Also please order patches so qemu.git remains bisectable - remove alarm timers after switch event loops to use timeouts.
Stefan
Re: [Qemu-devel] [PATCHv2] [RFC 6/7] aio / timers: Switch to ppoll, run AioContext timers in aio_poll/aio_dispatch
In reply to Stefan HajnocziStefan,
Quoted 5 lines · --On 25 July 2013 11:33:43 +0200 Stefan Hajnoczi <[email protected]> wrote:
aio-posix.c bool aio_poll(AioContext *ctx, bool blocking) · line 260
Quoted 3 lines
assert(progress || busy);aio-posix.c +1
-1-return true; +return progress;Now
aio_poll()can return false when it used to return true?
I don't think that's a bug.
Firstly, this is the same thing you fixed and we discussed on another thread.
Secondly, aio_poll always could return false. With the post patch line numbering, here:
233 /* No AIO operations? Get us out of here */
234 if (!busy) {
235 return progress;
236 }
The only circumstance where it now return false when previously it would have exited at the bottom of aio_poll and returned true is if g_poll returns such that aio_dispatch does nothing. That requires there to be no aio_dispatch to the normal FD handlers (which would generally mean a timeout) AND no timers running. This might happen if there was zero timeout.
Signature · 1 line
Alex Bligh
Re: [Qemu-devel] [PATCHv2] [RFC 6/7] aio / timers: Switch to ppoll, run AioContext timers in aio_poll/aio_dispatch
In reply to Stefan HajnocziStefan,
I missed a couple of comments.
Quoted 4 lines · --On 25 July 2013 11:33:43 +0200 Stefan Hajnoczi <[email protected]> wrote:
Quoted 4 lines
aio-win32.c +0
-0@@ -214,6 +221,15 @@ bool aio_poll(AioContext *ctx, bool blocking) events[ret -WAIT_OBJECT_0 ] = events[--count]; }
Quoted 13 lines
aio-win32.c bool aio_poll(AioContext *ctx, bool blocking) · line 234
Quoted 11 lines
(quoted diff) +2
-0+ 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
enteringg_pollbut is ready + * afterg_poll. This will only do
anything if a timer has expired. + */aio-win32.c +3
-0+ progress |=qemu_run_timers (ctx->clock); + } +assert (progress || busy);return true;You didn't update this to return just progress.
Will fix
Quoted 11 lines
Quoted 11 lines
}
async.c +1
-0@@ -123,13 +123,16 @@ aio_ctx_prepare(GSource *source, gint *timeout) {AioContext *ctx = (AioContext *) source;QEMUBH *bh; +int deadline;
Quoted 11 lines
async.c aio_ctx_prepare(GSource *source, gint *timeout) · line 135
Quoted 9 lines
async.c +3
-1for (bh = ctx->first_bh; bh; bh = bh->next) {if (!bh->deleted && bh->scheduled) {if (bh->idle) {/* idle bottom halves will be polled at least * every 10ms */ - *timeout =10 ; +if ((*timeout <0 ) || (*timeout >10 )) { + *timeout =10 ; + }Use the function you introduced earlier to return the nearest timeout?
The function I introduced takes an int64_t not an int. I think that's OK though.
Quoted 8 lines
Quoted 8 lines
async.c +0
-0}else {/* non-idle bottom halves will be executed * immediately */ @@ -139,6 +142,15 @@ aio_ctx_prepare(GSource *source, gint *timeout) } }
Quoted 10 lines
async.c aio_ctx_prepare(GSource *source, gint *timeout) · line 151
Quoted 8 lines
async.c +1
-0+ deadline =qemu_timeout_ns_to_ms(qemu_clock_deadline_ns(ctx->clock)); + if
(deadline == 0) {async.c +5
-0+ *timeout =0 ; +return true; + }else if ((deadline >0 ) && + ((*timeout <0 ) || (deadline < *timeout))) { + *timeout = deadline;Same here.
Ditto
Quoted 4 lines
Quoted 4 lines
async.c +0
-0@@ -170,9 +171,13 @@ static void glib_pollfds_fill(uint32_t *cur_timeout) glib_n_poll_fds); }while (n != glib_n_poll_fds);
Quoted 8 lines
async.c static void glib_pollfds_fill(uint32_t *cur_timeout) · line 177
Quoted 6 lines
async.c +4
-2-if (timeout >=0 && timeout < *cur_timeout) { - *cur_timeout = timeout; +if (timeout <0 ) { + timeout_ns =-1 ; + }else { + timeout_ns = (int64_t )timeout * (int64_t )SCALE_MS ;Indentation.
Will fix
Signature · 1 line
Alex Bligh
v3 cover aio / timers: Add AioContext timers and use ppoll
In reply to Stefan HajnocziThis patch series adds support for timers attached to an AioContext clock which get called within aio_poll.
In doing so it removes alarm timers and moves to use ppoll where possible.
This patch set 'sort of' passes make check (see below for caveat) including a new test harness for the aio timers, but has not been tested much beyond that. In particular, the win32 changes have not even been compile tested.
Caveat: make check fails one test only with:
ERROR:tests/test-aio.c:346:test_wait_event_notifier_noflush: assertion failed: (aio_poll(ctx, false))
As gar as I can tell, this check is incorrect, in that it checking aio_poll makes progress when in fact it should not make progress. I fixed an issue where aio_poll was (as far as I can tell) wrongly returning true on a timeout, and that generated this error.
Changes since v2:
* Reordered to remove alarm timers last
* Added prctl(PR_SET_TIMERSLACK, 1, ...)
* Renamed qemu_g_poll_ns to qemu_poll_ns
* Moved declaration of above & drop glib types
* Do not use a global list of qemu clocks
* Add AioContext * to QEMUClock
* Split up conversion to use ppoll and timers
* Indentation fix
* Fix aio_win32.c aio_poll to return progress
* aio_notify / qemu_notify when timers are modified
* change comment in deprecation of clock options
Things NOT changed since v2: I have *NOT* disaggregated QEMUClock and QEMUTimerList. This is very intrusive (horrendous git stats) and largely orthogonal. I will however submit a separate patch set to do this.
Alex Bligh (12):
aio / timers: add qemu-timer.c utility functions
aio / timers: add ppoll support with qemu_poll_ns
aio / timers: Add prctl(PR_SET_TIMERSLACK, 1, ...) to reduce timer
slack
aio / timers: Make qemu_run_timers and qemu_run_all_timers return
progress
aio / timers: Add a clock to AioContext
aio / timers: Add an AioContext pointer to QEMUClock aio / timers: aio_ctx_prepare sets timeout from AioContext timers aio / timers: Convert aio_poll to use AioContext timers' deadline aio / timers: convert mainloop to use timeout
aio / timers: on timer modification, qemu_notify or aio_notify aio / timers: Remove alarm timers
aio / timers: Add test harness for AioContext timers
aio-posix.c | 20 +- aio-win32.c | 22 +- async.c | 16 +- configure | 37 +++ include/block/aio.h | 5 + include/qemu/timer.h | 27 ++- main-loop.c | 52 +++-- qemu-timer.c | 629 ++++++++++---------------------------------------- tests/test-aio.c | 122 ++++++++++ vl.c | 5 +- 10 files changed, 396 insertions(+), 539 deletions(-)
Signature · 1 line
1.7.9.5
RFC PATCH v3 2/12 aio / timers: add ppoll support with qemu_poll_ns
Add qemu_poll_ns which works like g_poll but takes a nanosecond timeout.
--- configure | 19 +++++++++++++++++++ include/qemu/timer.h | 1 + qemu-timer.c | 24 ++++++++++++++++++++++++ 3 files changed, 44 insertions(+)
configure +19 -0
@@ -2801,6 +2801,22 @@ if compile_prog "" "" ; then dup3=yes fi +# check for ppoll support +ppoll=no +cat > $TMPC << EOF +#include <poll.h> + +int main(void) +{ + struct pollfd pfd = { .fd = 0, .events = 0, .revents = 0 }; + ppoll(&pfd, 1, 0, 0); + return 0; +} +EOF +if compile_prog "" "" ; then + ppoll=yes +fi + # check for epoll support epoll=no cat > $TMPC << EOF
@@ -3792,6 +3808,9 @@ fi if test "$dup3" = "yes" ; then echo "CONFIG_DUP3=y" >> $config_host_mak fi +if test "$ppoll" = "yes" ; then + echo "CONFIG_PPOLL=y" >> $config_host_mak +fi if test "$epoll" = "yes" ; then echo "CONFIG_EPOLL=y" >> $config_host_mak fi
include/qemu/timer.h +1 -0
@@ -44,6 +44,7 @@ int64_t qemu_clock_expired(QEMUClock *clock);int64_t qemu_clock_deadline (QEMUClock *clock);int64_t qemu_clock_deadline_ns (QEMUClock *clock);int qemu_timeout_ns_to_ms (int64_t ns); +int qemu_poll_ns (GPollFD *fds,uint nfds,int64_t timeout);void qemu_clock_enable (QEMUClock *clock,bool enabled);void qemu_clock_warp (QEMUClock *clock);
qemu-timer.c +24 -0
@@ -37,6 +37,10 @@#include <mmsystem.h> #endif +#ifdef CONFIG_PPOLL +#include <poll.h> +#endif +/***********************************************************/ /* timers */
@@ -328,6 +332,26 @@ int qemu_timeout_ns_to_ms(int64_t ns) } +/* qemu implementation of g_poll which uses a nanosecond timeout but is +* otherwise identical to g_poll +*/ +int qemu_poll_ns (GPollFD *fds,uint nfds,int64_t timeout) +{ +#ifdef CONFIG_PPOLL +if (timeout <0 ) { +return ppoll ((struct pollfd *)fds, nfds,NULL ,NULL ); + }else { +struct timespec ts; + ts.tv_sec = timeout /1000000000LL ; + ts.tv_nsec = timeout %1000000000LL ; +return ppoll ((struct pollfd *)fds, nfds, &ts,NULL ); + } +#else +return g_poll (fds, nfds,qemu_timeout_ns_to_ms (timeout)); +#endif +} + +QEMUTimer *qemu_new_timer (QEMUClock *clock,int scale,QEMUTimerCB *cb,void *opaque) {
Signature · 1 line
1.7.9.5
RFC PATCH v3 3/12 aio / timers: Add prctl(PR_SET_TIMERSLACK, 1, ...) to reduce timer slack
In reply to Alex BlighWhere supported, called prctl(PR_SET_TIMERSLACK, 1, ...) to set one nanosecond timer slack to increase precision of timer calls.
--- configure | 18 ++++++++++++++++++ qemu-timer.c | 7 +++++++ 2 files changed, 25 insertions(+)
configure +18 -0
@@ -2817,6 +2817,21 @@ if compile_prog "" "" ; then ppoll=yes fi +# check for prctl(PR_SET_TIMERSLACK , ... ) support +prctl_pr_set_timerslack=no +cat > $TMPC << EOF +#include <sys/prctl.h> + +int main(void) +{ + prctl(PR_SET_TIMERSLACK, 1, 0, 0, 0); + return 0; +} +EOF +if compile_prog "" "" ; then + prctl_pr_set_timerslack=yes +fi + # check for epoll support epoll=no cat > $TMPC << EOF
@@ -3811,6 +3826,9 @@ fi if test "$ppoll" = "yes" ; then echo "CONFIG_PPOLL=y" >> $config_host_mak fi +if test "$prctl_pr_set_timerslack" = "yes" ; then + echo "CONFIG_PRCTL_PR_SET_TIMERSLACK=y" >> $config_host_mak +fi if test "$epoll" = "yes" ; then echo "CONFIG_EPOLL=y" >> $config_host_mak fi
qemu-timer.c +7 -0
@@ -41,6 +41,10 @@#include <poll.h> #endif +#ifdef CONFIG_PRCTL_PR_SET_TIMERSLACK +#include <sys/prctl.h> +#endif +/***********************************************************/ /* timers */
@@ -512,6 +516,9 @@ void init_clocks(void) vm_clock =qemu_new_clock (QEMU_CLOCK_VIRTUAL ); host_clock =qemu_new_clock (QEMU_CLOCK_HOST ); } +#ifdef CONFIG_PRCTL_PR_SET_TIMERSLACK +prctl (PR_SET_TIMERSLACK ,1 ,0 ,0 ,0 ); +#endif }uint64_t qemu_timer_expire_time_ns (QEMUTimer *ts)
Signature · 1 line
1.7.9.5
RFC PATCH v3 1/12 aio / timers: add qemu-timer.c utility functions
In reply to Alex BlighAdd qemu_free_clock and expose qemu_new_clock and clock types.
Add utility functions to qemu-timer.c for nanosecond timing.
Add qemu_clock_deadline_ns to calculate deadlines to nanosecond accuracy.
Add utility function qemu_soonest_timeout to calculate soonest deadline.
Add qemu_timeout_ns_to_ms to convert a timeout in nanoseconds back to milliseconds for when ppoll is not used.
--- include/qemu/timer.h | 17 ++++++++++++++ qemu-timer.c | 63 +++++++++++++++++++++++++++++++++++++++++++++----- 2 files changed, 74 insertions(+), 6 deletions(-)
include/qemu/timer.h +17 -0
@@ -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,14 @@ 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); +int qemu_timeout_ns_to_ms (int64_t ns);void qemu_clock_enable (QEMUClock *clock,bool enabled);void qemu_clock_warp (QEMUClock *clock);
@@ -63,6 +71,15 @@ int64_t cpu_get_ticks(void);void cpu_enable_ticks (void );void cpu_disable_ticks (void ); +static inline int64_t qemu_soonest_timeout (int64_t timeout1,int64_t timeout2) +{ +/* we can abuse the fact that -1 (which means infinite) is a maximal +* value when cast to unsigned. As this is disgusting, it's kept in +* one inline function. +*/ +return ((uint64_t ) timeout1 < (uint64_t ) timeout2) ? timeout1 : timeout2; +} +static inline QEMUTimer *qemu_new_timer_ns (QEMUClock *clock,QEMUTimerCB *cb,void *opaque) {
qemu-timer.c +57 -6
93 lines not on this page · raw message
Signature · 1 line
1.7.9.5
RFC PATCH v3 4/12 aio / timers: Make qemu_run_timers and qemu_run_all_timers return progress
In reply to Alex BlighMake qemu_run_timers and qemu_run_all_timers return progress so that aio_poll etc. can determine whether a timer has been run.
--- include/qemu/timer.h | 4 ++-- qemu-timer.c | 18 ++++++++++++------ 2 files changed, 14 insertions(+), 8 deletions(-)
include/qemu/timer.h +2 -2
@@ -62,8 +62,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 );
qemu-timer.c +12 -6
@@ -451,13 +451,14 @@ 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; +bool progress = false;if (!clock->enabled) -return ; +return progress; current_time =qemu_get_clock_ns (clock);for (;;) {
@@ -471,7 +472,9 @@ void qemu_run_timers(QEMUClock *clock)/* run the callback (the timer list can be modified) */ ts->cb (ts->opaque); + progress = true; } +return progress; }int64_t qemu_get_clock_ns (QEMUClock *clock)
@@ -526,20 +529,23 @@ 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 progress = false; alarm_timer->pending = false;/* vm time timers */ -qemu_run_timers (vm_clock); -qemu_run_timers (rt_clock); -qemu_run_timers (host_clock); + progress |=qemu_run_timers (vm_clock); + progress |=qemu_run_timers (rt_clock); + progress |=qemu_run_timers (host_clock);/* rearm timer, if not periodic */ if (alarm_timer->expired) { alarm_timer->expired = false;qemu_rearm_alarm_timer (alarm_timer); } + +return progress; }#ifdef _WIN32
Signature · 1 line
1.7.9.5
RFC PATCH v3 5/12 aio / timers: Add a clock to AioContext
In reply to Alex BlighAdd a clock to each AioContext and delete it when freed.
--- async.c | 2 ++ include/block/aio.h | 5 +++++ 2 files changed, 7 insertions(+)
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 */
Signature · 1 line
1.7.9.5
RFC PATCH v3 6/12 aio / timers: Add an AioContext pointer to QEMUClock
In reply to Alex BlighAdd an AioContext pointer to QEMUClock so it knows what to notify on a timer change.
--- async.c | 1 + include/qemu/timer.h | 3 +++ qemu-timer.c | 12 ++++++++++++ 3 files changed, 16 insertions(+)
async.c +1 -0
@@ -217,6 +217,7 @@ AioContext *aio_context_new(void) (EventNotifierHandler *) event_notifier_test_and_clear, NULL); ctx->clock =qemu_new_clock (QEMU_CLOCK_REALTIME ); +qemu_clock_set_ctx (ctx->clock, ctx);return ctx; }
include/qemu/timer.h +3 -0
@@ -16,6 +16,7 @@#define QEMU_CLOCK_HOST 2typedef struct QEMUClock QEMUClock ; +typedef struct AioContext AioContext ;typedef void QEMUTimerCB (void *opaque);/* The real time clock should be used only for stuff which does not
@@ -38,6 +39,8 @@ extern QEMUClock *host_clock;QEMUClock *qemu_new_clock (int type);void qemu_free_clock (QEMUClock *clock); +AioContext *qemu_clock_get_ctx (QEMUClock *clock); +void qemu_clock_set_ctx (QEMUClock *clock,AioContext * ctx);int64_t qemu_get_clock_ns (QEMUClock *clock);int64_t qemu_clock_has_timers (QEMUClock *clock);int64_t qemu_clock_expired (QEMUClock *clock);
qemu-timer.c +12 -0
@@ -29,6 +29,7 @@#include "hw/hw.h" #include "qemu/timer.h" +#include "block/aio.h" #ifdef CONFIG_POSIX #include <pthread.h> #endif
@@ -50,6 +51,7 @@struct QEMUClock {QEMUTimer *active_timers; +AioContext *ctx;NotifierList reset_notifiers;int64_t last;
@@ -252,6 +254,16 @@ void qemu_free_clock(QEMUClock *clock)g_free (clock); } +AioContext *qemu_clock_get_ctx (QEMUClock *clock) +{ +return clock->ctx; +} + +void qemu_clock_set_ctx (QEMUClock *clock,AioContext * ctx) +{ + clock->ctx = ctx; +} +void qemu_clock_enable (QEMUClock *clock,bool enabled) {bool old = clock->enabled;
Signature · 1 line
1.7.9.5
RFC PATCH v3 7/12 aio / timers: aio_ctx_prepare sets timeout from AioContext timers
In reply to Alex BlighCalculate the timeout in aio_ctx_prepare taking into account the timers attached to the AioContext.
Alter aio_ctx_check similarly.
--- async.c | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-)
async.c +11 -2
@@ -123,13 +123,14 @@ aio_ctx_prepare(GSource *source, gint *timeout) {AioContext *ctx = (AioContext *) source;QEMUBH *bh; +int deadline;for (bh = ctx->first_bh; bh; bh = bh->next) {if (!bh->deleted && bh->scheduled) {if (bh->idle) {/* idle bottom halves will be polled at least * every 10ms */ - *timeout =10 ; + *timeout =qemu_soonest_timeout (*timeout,10 ); }else {/* non-idle bottom halves will be executed * immediately */
@@ -139,6 +140,14 @@ aio_ctx_prepare(GSource *source, gint *timeout) } } + deadline =qemu_timeout_ns_to_ms (qemu_clock_deadline_ns (ctx->clock)); +if (deadline ==0 ) { + *timeout =0 ; +return true; + }else { + *timeout =qemu_soonest_timeout (*timeout, deadline); + } +return false; }
@@ -153,7 +162,7 @@ aio_ctx_check(GSource *source)return true; } } -return aio_pending (ctx); +return aio_pending (ctx) || (qemu_clock_deadline_ns (ctx->clock) >=0 ); } static gboolean
Signature · 1 line
1.7.9.5
RFC PATCH v3 8/12 aio / timers: Convert aio_poll to use AioContext timers' deadline
In reply to Alex BlighConvert aio_poll to use deadline based on AioContext's timers.
--- aio-posix.c | 20 +++++++++++++------- aio-win32.c | 22 +++++++++++++++++++--- 2 files changed, 32 insertions(+), 10 deletions(-)
aio-posix.c +13 -7
@@ -166,6 +166,10 @@ static bool aio_dispatch(AioContext *ctx)g_free (tmp); } } + +/* Run our timers */ + progress |=qemu_run_timers (ctx->clock); +return progress; }
@@ -232,9 +236,9 @@ bool aio_poll(AioContext *ctx, bool blocking) }/* wait until next event */ - ret =g_poll ((GPollFD *)ctx->pollfds->data, - ctx->pollfds->len, - blocking ?-1 :0 ); + ret =qemu_poll_ns ((GPollFD *)ctx->pollfds->data, + ctx->pollfds->len, + blocking ?qemu_clock_deadline_ns (ctx->clock) :0 );/* if we have any readable fds, dispatch event */ if (ret >0 ) {
@@ -245,11 +249,13 @@ bool aio_poll(AioContext *ctx, bool blocking) node->pfd.revents = pfd->revents; } } -if (aio_dispatch (ctx)) { - progress = true; - } + } + +/* Run dispatch even if there were no readable fds to run timers */ +if (aio_dispatch (ctx)) { + progress = true; }assert (progress || busy); -return true; +return progress; }
aio-win32.c +19 -3
@@ -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_ns (ctx->clock)) :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; +return progress; }
Signature · 1 line
1.7.9.5
RFC PATCH v3 9/12 aio / timers: convert mainloop to use timeout
In reply to Alex BlighConvert mainloop to use timeout from 3 static timers.
--- main-loop.c | 48 +++++++++++++++++++++++++++++++++++++----------- 1 file changed, 37 insertions(+), 11 deletions(-)
main-loop.c +37 -11
@@ -155,10 +155,11 @@ static int max_priority;static int glib_pollfds_idx;static int glib_n_poll_fds; -static void glib_pollfds_fill (uint32_t *cur_timeout) +static void glib_pollfds_fill (int64_t *cur_timeout) {GMainContext *context =g_main_context_default ();int timeout =0 ; +int64_t timeout_ns;int n;g_main_context_prepare (context, &max_priority);
@@ -174,9 +175,13 @@ static void glib_pollfds_fill(uint32_t *cur_timeout) glib_n_poll_fds); }while (n != glib_n_poll_fds); -if (timeout >=0 && timeout < *cur_timeout) { - *cur_timeout = timeout; +if (timeout <0 ) { + timeout_ns =-1 ; + }else { + timeout_ns = (int64_t )timeout * (int64_t )SCALE_MS ; } + + *cur_timeout =qemu_soonest_timeout (timeout_ns, *cur_timeout); }static void glib_pollfds_poll (void )
@@ -191,7 +196,7 @@ static void glib_pollfds_poll(void)#define MAX_MAIN_LOOP_SPIN (1000) -static int os_host_main_loop_wait (uint32_t timeout) +static int os_host_main_loop_wait (int64_t timeout) {int ret;static int spin_counter;
@@ -214,7 +219,7 @@ static int os_host_main_loop_wait(uint32_t timeout) notified = true; } - timeout =1 ; + timeout =SCALE_MS ; }if (timeout >0 ) {
@@ -224,7 +229,7 @@ static int os_host_main_loop_wait(uint32_t timeout) spin_counter++; } - ret =g_poll ((GPollFD *)gpollfds->data, gpollfds->len, timeout); + ret =qemu_poll_ns ((GPollFD *)gpollfds->data, gpollfds->len, timeout);if (timeout >0 ) {qemu_mutex_lock_iothread ();
@@ -373,7 +378,7 @@ static void pollfds_poll(GArray *pollfds, int nfds, fd_set *rfds, } } -static int os_host_main_loop_wait (uint32_t timeout) +static int os_host_main_loop_wait (int64_t timeout) {GMainContext *context =g_main_context_default ();GPollFD poll_fds[1024 *2 ];/* this is probably overkill */
@@ -382,6 +387,7 @@ static int os_host_main_loop_wait(uint32_t timeout)PollingEntry *pe;WaitObjects *w = &wait_objects;gint poll_timeout; +int64_t poll_timeout_ns;static struct timeval tv0;fd_set rfds, wfds, xfds;int nfds;
@@ -419,12 +425,17 @@ static int os_host_main_loop_wait(uint32_t timeout) poll_fds[n_poll_fds + i].events =G_IO_IN ; } -if (poll_timeout <0 || timeout < poll_timeout) { - poll_timeout = timeout; +if (poll_timeout <0 ) { + poll_timeout_ns =-1 ; + }else { + poll_timeout_ns = (int64_t )poll_timeout * (int64_t )SCALE_MS ; } + poll_timeout_ns =qemu_soonest_timeout (poll_timeout_ns, timeout); +qemu_mutex_unlock_iothread (); - g_poll_ret =g_poll (poll_fds, n_poll_fds + w->num, poll_timeout); + g_poll_ret =qemu_poll_ns (poll_fds, n_poll_fds + w->num, poll_timeout_ns); +qemu_mutex_lock_iothread ();if (g_poll_ret >0 ) {for (i =0 ; i < w->num; i++) {
@@ -449,6 +460,7 @@ int main_loop_wait(int nonblocking) {int ret;uint32_t timeout =UINT32_MAX ; +int64_t timeout_ns;if (nonblocking) { timeout =0 ;
@@ -462,7 +474,21 @@ int main_loop_wait(int nonblocking)slirp_pollfds_fill (gpollfds);#endif qemu_iohandler_fill (gpollfds); - ret =os_host_main_loop_wait (timeout); + +if (timeout ==UINT32_MAX ) { + timeout_ns =-1 ; + }else { + timeout_ns = (uint64_t )timeout * (int64_t )(SCALE_MS ); + } + + timeout_ns =qemu_soonest_timeout (timeout_ns, +qemu_clock_deadline_ns (rt_clock)); + timeout_ns =qemu_soonest_timeout (timeout_ns, +qemu_clock_deadline_ns (vm_clock)); + timeout_ns =qemu_soonest_timeout (timeout_ns, +qemu_clock_deadline_ns (host_clock)); + + ret =os_host_main_loop_wait (timeout_ns);qemu_iohandler_poll (gpollfds, ret);#ifdef CONFIG_SLIRP slirp_pollfds_poll (gpollfds, (ret <0 ));
Signature · 1 line
1.7.9.5
RFC PATCH v3 10/12 aio / timers: on timer modification, qemu_notify or aio_notify
In reply to Alex BlighOn qemu_clock_enable or qemu_mod_timer_ns, ensure qemu_notify or aio_notify is called to end the appropriate poll().
--- qemu-timer.c | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-)
qemu-timer.c +11 -3
@@ -264,11 +264,21 @@ void qemu_clock_set_ctx(QEMUClock *clock, AioContext * ctx) clock->ctx = ctx; } +static void qemu_clock_notify (QEMUClock *clock) +{ +if (clock->ctx) { +aio_notify (clock->ctx); + }else { +qemu_notify_event (); + } +} +void qemu_clock_enable (QEMUClock *clock,bool enabled) {bool old = clock->enabled; clock->enabled = enabled;if (enabled && !old) { +qemu_clock_notify (clock);qemu_rearm_alarm_timer (alarm_timer); } }
@@ -436,9 +446,7 @@ void qemu_mod_timer_ns(QEMUTimer *ts, int64_t expire_time) }/* Interrupt execution to force deadline recalculation. */ qemu_clock_warp (ts->clock); -if (use_icount) { -qemu_notify_event (); - } +qemu_clock_notify (ts->clock); } }
Signature · 1 line
1.7.9.5
RFC PATCH v3 12/12 aio / timers: Add test harness for AioContext timers
In reply to Alex BlighAdd a test harness for AioContext timers. The g_source equivalent is unsatisfactory as it suffers from false wakeups.
--- tests/test-aio.c | 122 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 122 insertions(+)
tests/test-aio.c +122 -0
@@ -12,6 +12,7 @@#include <glib.h> #include "block/aio.h" +#include "qemu/timer.h" AioContext *ctx;
@@ -31,6 +32,15 @@ typedef struct {int max; } BHTestData; +typedef struct { +QEMUTimer *timer; +QEMUClock *clock; +int n; +int max; +int64_t ns; +AioContext *ctx; +}TimerTestData ; +static void bh_test_cb (void *opaque) {BHTestData *data = opaque;
@@ -39,6 +49,24 @@ static void bh_test_cb(void *opaque) } } +static void timer_test_cb (void *opaque) +{ +TimerTestData *data = opaque; +if (++data->n < data->max) { +qemu_mod_timer (data->timer, +qemu_get_clock_ns (data->clock) + data->ns); + } +} + +static void dummy_io_handler_read (void *opaque) +{ +} + +static int dummy_io_handler_flush (void *opaque) +{ +return 1 ; +} +static void bh_delete_cb (void *opaque) {BHTestData *data = opaque;
@@ -340,6 +368,51 @@ static void test_wait_event_notifier_noflush(void)event_notifier_cleanup (&data.e); } +static void test_timer_schedule (void ) +{ +TimerTestData data = { .n =0 , .ctx = ctx, .ns =SCALE_MS *750 , + .max =2 , .clock = ctx->clock }; +int pipefd[2 ]; + +/* aio_poll will not block to wait for timers to complete unless it has +* an fd to wait on. Fixing this breaks other tests. So create a dummy one. +*/ +g_assert (!pipe2 (pipefd,O_NONBLOCK )); +aio_set_fd_handler (ctx, pipefd[0 ], + dummy_io_handler_read,NULL , dummy_io_handler_flush, +NULL ); +aio_poll (ctx, false); + + data.timer =qemu_new_timer_ns (data.clock, timer_test_cb, &data); +qemu_mod_timer (data.timer,qemu_get_clock_ns (data.clock) + data.ns); + + g_assert_cmpint(data.n, ==, 0); + +g_assert (!aio_poll (ctx, false)); + g_assert_cmpint(data.n, ==, 0); + +sleep (1 ); + g_assert_cmpint(data.n, ==, 0); + +g_assert (aio_poll (ctx, false)); + g_assert_cmpint(data.n, ==, 1); + +g_assert (!aio_poll (ctx, false)); + g_assert_cmpint(data.n, ==, 1); + +g_assert (aio_poll (ctx, true)); + g_assert_cmpint(data.n, ==, 2); + +g_assert (!aio_poll (ctx, false)); + g_assert_cmpint(data.n, ==, 2); + +aio_set_fd_handler (ctx, pipefd[0 ],NULL ,NULL ,NULL ,NULL ); +close (pipefd[0 ]); +close (pipefd[1 ]); + +qemu_del_timer (data.timer); +} +/* Now the same tests, using the context as a GSource. They are * very similar to the ones above, with g_main_context_iteration * replacing aio_poll. However:
@@ -622,12 +695,59 @@ static void test_source_wait_event_notifier_noflush(void)event_notifier_cleanup (&data.e); } +static void test_source_timer_schedule (void ) +{ +TimerTestData data = { .n =0 , .ctx = ctx, .ns =SCALE_MS *750 , + .max =2 , .clock = ctx->clock }; +int pipefd[2 ]; +int64_t expiry; + +/* aio_poll will not block to wait for timers to complete unless it has +* an fd to wait on. Fixing this breaks other tests. So create a dummy one. +*/ +g_assert (!pipe2 (pipefd,O_NONBLOCK )); +aio_set_fd_handler (ctx, pipefd[0 ], + dummy_io_handler_read,NULL , dummy_io_handler_flush, +NULL ); +while (g_main_context_iteration (NULL , false)); + + data.timer =qemu_new_timer_ns (data.clock, timer_test_cb, &data); + expiry =qemu_get_clock_ns (data.clock) + data.ns; +qemu_mod_timer (data.timer, expiry); + + g_assert_cmpint(data.n, ==, 0); + +sleep (1 ); + g_assert_cmpint(data.n, ==, 0); + +g_assert (g_main_context_iteration (NULL , false)); + g_assert_cmpint(data.n, ==, 1); + +/* The comment above was not kidding when it said this wakes up itself */ +do { +g_assert (g_main_context_iteration (NULL , true)); + }while (qemu_get_clock_ns (data.clock) <= expiry); +sleep (1 ); +g_main_context_iteration (NULL , false); + + g_assert_cmpint(data.n, ==, 2); + +aio_set_fd_handler (ctx, pipefd[0 ],NULL ,NULL ,NULL ,NULL ); +close (pipefd[0 ]); +close (pipefd[1 ]); + +qemu_del_timer (data.timer); +} + +/* End of tests. */ int main (int argc,char **argv) {GSource *src; +init_clocks (); + ctx =aio_context_new (); src =aio_get_g_source (ctx);g_source_attach (src,NULL );
@@ -648,6 +768,7 @@ int main(int argc, char **argv)g_test_add_func ("/aio/event/wait" , test_wait_event_notifier);g_test_add_func ("/aio/event/wait/no-flush-cb" , test_wait_event_notifier_noflush);g_test_add_func ("/aio/event/flush" , test_flush_event_notifier); +g_test_add_func ("/aio/timer/schedule" , test_timer_schedule);g_test_add_func ("/aio-gsource/notify" , test_source_notify);g_test_add_func ("/aio-gsource/flush" , test_source_flush);
@@ -662,5 +783,6 @@ int main(int argc, char **argv)g_test_add_func ("/aio-gsource/event/wait" , test_source_wait_event_notifier);g_test_add_func ("/aio-gsource/event/wait/no-flush-cb" , test_source_wait_event_notifier_noflush);g_test_add_func ("/aio-gsource/event/flush" , test_source_flush_event_notifier); +g_test_add_func ("/aio-gsource/timer/schedule" , test_source_timer_schedule);return g_test_run (); }
Signature · 1 line
1.7.9.5
RFC PATCH v3 11/12 aio / timers: Remove alarm timers
In reply to Alex BlighRemove alarm timers from qemu-timers.c now we use g_poll / ppoll instead.
--- include/qemu/timer.h | 2 - main-loop.c | 4 - qemu-timer.c | 501 +------------------------------------------------- vl.c | 5 +- 4 files changed, 8 insertions(+), 504 deletions(-)
include/qemu/timer.h +0 -2
@@ -67,9 +67,7 @@ uint64_t qemu_timer_expire_time_ns(QEMUTimer *ts);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 );int64_t cpu_get_ticks (void );void cpu_enable_ticks (void );
main-loop.c +0 -4
@@ -131,10 +131,6 @@ int qemu_init_main_loop(void)GSource *src;init_clocks (); -if (init_timer_alarm () <0 ) { -fprintf (stderr,"could not initialize alarm timer\n" ); -exit (1 ); - } ret =qemu_signal_init ();if (ret) {
qemu-timer.c +4 -497
539 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] [RFC] [PATCHv3 00/12] aio / timers: Add AioContext timers and use ppoll
In reply to Alex BlighQuoted 1 line · --On 25 July 2013 23:16:36 +0100 Alex Bligh <[email protected]> wrote:
* change comment in deprecation of clock options
This got missed due to my own inadequacy. Delta is below. It will be in the next series.
Signature · 7 lines
Alex Bligh commit 699c55b421822ad2d14b5520b04db8fa9f77c4e0 Author: Alex Bligh <[email protected]> Date: Thu Jul 25 23:19:52 2013 +0100 aio / timers: fix comment in vl.c
vl.c +2 -3
@@ -3714,9 +3714,8 @@ int main(int argc, char **argv, char **envp) old_param =1 ;break ;case QEMU_OPTION_clock: -/* Once upon a time we did: -* configure_alarms(optarg); -* here. This is stubbed out for compatibility. +/* Clock options no longer exist. Keep this option for +* backward compatibility. */ break ;case QEMU_OPTION_startdate:
Re: [Qemu-devel] [PATCHv2] [RFC 2/7] aio / timers: qemu-timer.c utility functions and add list of clocks
In reply to Alex BlighQuoted 24 lines · Alex Bligh
Quoted 16 lines
include/qemu/timer.h int64_t cpu_get_ticks(void); · line 81
Quoted 12 lines
include/qemu/timer.h +6
-0@@ -61,6 +71,15 @@ int64_t cpu_get_ticks(void);void cpu_enable_ticks (void );void cpu_disable_ticks (void ); +static inline int64_t qemu_soonest_timeout (int64_t timeout1,int64_t timeout2) +{ +/* we can abuse the fact that -1 (which means infinite) is a maximal +* value when cast to unsigned. As this is disgusting, it's kept in +* one inline function. +*/ +return ((uint64_t ) timeout1 < (uint64_t ) timeout2) ? timeout1 : timeout2;The straightforward version isn't much longer than the commented casting trick:
Quoted 7 lines
if (timeout1 == -1) { return timeout2; } else if (timeout2 == -1) { return timeout1; } else { return timeout1 < timeout2 ? timeout1 : timeout2; }
Quoted 3 lines
Well, it should be (timeout1 < 0) for consistency. It may be a micro optimisation but I'm pretty sure the casting trick will produce better code. With the comment, it's arguably more readable too.
Seems like a compiler could be smart enough to use unsigned instructions. Seems like a ">> 9" vs "/ 512" micro-optimization to me.
Quoted 11 lines
Quoted 6 lines
qemu-timer.c static QEMUClock *qemu_new_clock(int type) · line 88
Quoted 4 lines
qemu-timer.c +4
-0+void qemu_free_clock (QEMUClock *clock) +{ +QLIST_REMOVE (clock, list); +g_free (clock);assert that there are no timers left?
Yes I wasn't quite sure of the right semantics here as no clocks are currently ever destroyed. I'm not quite sure how we know all timers are destroyed when an AioContext is destroyed. Should I go and manually free them or assert the right way?
It is not possible to free them since their owner still holds a pointer. That is why I'd use an assert. The code needs to be written so that timers are destroyed before the clock is destroyed.
Stefan
Re: [Qemu-devel] [PATCHv2] [RFC 6/7] aio / timers: Switch to ppoll, run AioContext timers in aio_poll/aio_dispatch
In reply to Alex BlighQuoted 8 lines · Alex Bligh
Stefan,
Quoted 4 lines · --On 25 July 2013 11:33:43 +0200 Stefan Hajnoczi <[email protected]> wrote:
Quoted 3 lines
assert(progress || busy);aio-posix.c +1
-1-return true; +return progress;
Quoted 6 lines
Quoted 1 line
Now
aio_poll()can return false when it used to return true?I don't think that's a bug.
Firstly, this is the same thing you fixed and we discussed on another thread.
I'm fine with the change itself but it needs to be explained in the commit message or a comment.
In the patch where I changed the semantics of aio_poll() the change is explained in detail in the commit message:
http://patchwork.ozlabs.org/patch/261786/
Stefan
v4 cover aio / timers: Add AioContext timers and use ppoll
In reply to Alex BlighThis patch series adds support for timers attached to an AioContext clock which get called within aio_poll.
In doing so it removes alarm timers and moves to use ppoll where possible.
This patch set 'sort of' passes make check (see below for caveat) including a new test harness for the aio timers, but has not been tested much beyond that. In particular, the win32 changes have not even been compile tested.
Caveat: I have had to alter tests/test-aio.c so the following error no longer occurs.
ERROR:tests/test-aio.c:346:test_wait_event_notifier_noflush: assertion failed: (aio_poll(ctx, false))
As gar as I can tell, this check was incorrect, in that it checking aio_poll makes progress when in fact it should not make progress. I fixed an issue where aio_poll was (as far as I can tell) wrongly returning true on a timeout, and that generated this error.
Changes since v3
* Split up QEMUClock and QEMUClock list
* Improve commenting
* Fix comment in vl.c
* Change test/test-aio.c to reflect correct behaviour in aio_poll.
Changes since v2:
* Reordered to remove alarm timers last
* Added prctl(PR_SET_TIMERSLACK, 1, ...)
* Renamed qemu_g_poll_ns to qemu_poll_ns
* Moved declaration of above & drop glib types
* Do not use a global list of qemu clocks
* Add AioContext * to QEMUClock
* Split up conversion to use ppoll and timers
* Indentation fix
* Fix aio_win32.c aio_poll to return progress
* aio_notify / qemu_notify when timers are modified
* change comment in deprecation of clock options
Alex Bligh (13):
aio / timers: add qemu-timer.c utility functions
aio / timers: add ppoll support with qemu_poll_ns
aio / timers: Add prctl(PR_SET_TIMERSLACK, 1, ...) to reduce timer
slack
aio / timers: Make qemu_run_timers and qemu_run_all_timers return
progress
aio / timers: Split QEMUClock into QEMUClock and QEMUTimerList aio / timers: Add a QEMUTimerList to AioContext
aio / timers: Add an AioContext pointer to QEMUTimerList aio / timers: aio_ctx_prepare sets timeout from AioContext timers aio / timers: Convert aio_poll to use AioContext timers' deadline aio / timers: Convert mainloop to use timeout
aio / timers: on timer modification, qemu_notify or aio_notify aio / timers: Remove alarm timers
aio / timers: Add test harness for AioContext timers
aio-posix.c | 20 +- aio-win32.c | 22 +- async.c | 21 +- configure | 37 +++ include/block/aio.h | 5 + include/qemu/timer.h | 53 +++- main-loop.c | 52 +++- qemu-timer.c | 735 ++++++++++++++-------------------------------- tests/test-aio.c | 143 ++++++++- tests/test-thread-pool.c | 3 + vl.c | 4 +- 11 files changed, 544 insertions(+), 551 deletions(-)
Signature · 1 line
1.7.9.5
RFC PATCH v4 3/13 aio / timers: Add prctl(PR_SET_TIMERSLACK, 1, ...) to reduce timer slack
Where supported, called prctl(PR_SET_TIMERSLACK, 1, ...) to set one nanosecond timer slack to increase precision of timer calls.
--- configure | 18 ++++++++++++++++++ qemu-timer.c | 7 +++++++ 2 files changed, 25 insertions(+)
configure +18 -0
@@ -2817,6 +2817,21 @@ if compile_prog "" "" ; then ppoll=yes fi +# check for prctl(PR_SET_TIMERSLACK , ... ) support +prctl_pr_set_timerslack=no +cat > $TMPC << EOF +#include <sys/prctl.h> + +int main(void) +{ + prctl(PR_SET_TIMERSLACK, 1, 0, 0, 0); + return 0; +} +EOF +if compile_prog "" "" ; then + prctl_pr_set_timerslack=yes +fi + # check for epoll support epoll=no cat > $TMPC << EOF
@@ -3811,6 +3826,9 @@ fi if test "$ppoll" = "yes" ; then echo "CONFIG_PPOLL=y" >> $config_host_mak fi +if test "$prctl_pr_set_timerslack" = "yes" ; then + echo "CONFIG_PRCTL_PR_SET_TIMERSLACK=y" >> $config_host_mak +fi if test "$epoll" = "yes" ; then echo "CONFIG_EPOLL=y" >> $config_host_mak fi
qemu-timer.c +7 -0
@@ -41,6 +41,10 @@#include <poll.h> #endif +#ifdef CONFIG_PRCTL_PR_SET_TIMERSLACK +#include <sys/prctl.h> +#endif +/***********************************************************/ /* timers */
@@ -512,6 +516,9 @@ void init_clocks(void) vm_clock =qemu_new_clock (QEMU_CLOCK_VIRTUAL ); host_clock =qemu_new_clock (QEMU_CLOCK_HOST ); } +#ifdef CONFIG_PRCTL_PR_SET_TIMERSLACK +prctl (PR_SET_TIMERSLACK ,1 ,0 ,0 ,0 ); +#endif }uint64_t qemu_timer_expire_time_ns (QEMUTimer *ts)
Signature · 1 line
1.7.9.5
RFC PATCH v4 1/13 aio / timers: add qemu-timer.c utility functions
In reply to Alex BlighAdd qemu_free_clock and expose qemu_new_clock and clock types.
Add utility functions to qemu-timer.c for nanosecond timing.
Add qemu_clock_deadline_ns to calculate deadlines to nanosecond accuracy.
Add utility function qemu_soonest_timeout to calculate soonest deadline.
Add qemu_timeout_ns_to_ms to convert a timeout in nanoseconds back to milliseconds for when ppoll is not used.
--- include/qemu/timer.h | 17 ++++++++++++++ qemu-timer.c | 63 +++++++++++++++++++++++++++++++++++++++++++++----- 2 files changed, 74 insertions(+), 6 deletions(-)
include/qemu/timer.h +17 -0
@@ -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,14 @@ 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); +int qemu_timeout_ns_to_ms (int64_t ns);void qemu_clock_enable (QEMUClock *clock,bool enabled);void qemu_clock_warp (QEMUClock *clock);
@@ -63,6 +71,15 @@ int64_t cpu_get_ticks(void);void cpu_enable_ticks (void );void cpu_disable_ticks (void ); +static inline int64_t qemu_soonest_timeout (int64_t timeout1,int64_t timeout2) +{ +/* we can abuse the fact that -1 (which means infinite) is a maximal +* value when cast to unsigned. As this is disgusting, it's kept in +* one inline function. +*/ +return ((uint64_t ) timeout1 < (uint64_t ) timeout2) ? timeout1 : timeout2; +} +static inline QEMUTimer *qemu_new_timer_ns (QEMUClock *clock,QEMUTimerCB *cb,void *opaque) {
qemu-timer.c +57 -6
93 lines not on this page · raw message
Signature · 1 line
1.7.9.5
RFC PATCH v4 4/13 aio / timers: Make qemu_run_timers and qemu_run_all_timers return progress
In reply to Alex BlighMake qemu_run_timers and qemu_run_all_timers return progress so that aio_poll etc. can determine whether a timer has been run.
--- include/qemu/timer.h | 4 ++-- qemu-timer.c | 18 ++++++++++++------ 2 files changed, 14 insertions(+), 8 deletions(-)
include/qemu/timer.h +2 -2
@@ -62,8 +62,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 );
qemu-timer.c +12 -6
@@ -451,13 +451,14 @@ 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; +bool progress = false;if (!clock->enabled) -return ; +return progress; current_time =qemu_get_clock_ns (clock);for (;;) {
@@ -471,7 +472,9 @@ void qemu_run_timers(QEMUClock *clock)/* run the callback (the timer list can be modified) */ ts->cb (ts->opaque); + progress = true; } +return progress; }int64_t qemu_get_clock_ns (QEMUClock *clock)
@@ -526,20 +529,23 @@ 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 progress = false; alarm_timer->pending = false;/* vm time timers */ -qemu_run_timers (vm_clock); -qemu_run_timers (rt_clock); -qemu_run_timers (host_clock); + progress |=qemu_run_timers (vm_clock); + progress |=qemu_run_timers (rt_clock); + progress |=qemu_run_timers (host_clock);/* rearm timer, if not periodic */ if (alarm_timer->expired) { alarm_timer->expired = false;qemu_rearm_alarm_timer (alarm_timer); } + +return progress; }#ifdef _WIN32
Signature · 1 line
1.7.9.5
RFC PATCH v4 2/13 aio / timers: add ppoll support with qemu_poll_ns
In reply to Alex BlighAdd qemu_poll_ns which works like g_poll but takes a nanosecond timeout.
--- configure | 19 +++++++++++++++++++ include/qemu/timer.h | 1 + qemu-timer.c | 24 ++++++++++++++++++++++++ 3 files changed, 44 insertions(+)
configure +19 -0
@@ -2801,6 +2801,22 @@ if compile_prog "" "" ; then dup3=yes fi +# check for ppoll support +ppoll=no +cat > $TMPC << EOF +#include <poll.h> + +int main(void) +{ + struct pollfd pfd = { .fd = 0, .events = 0, .revents = 0 }; + ppoll(&pfd, 1, 0, 0); + return 0; +} +EOF +if compile_prog "" "" ; then + ppoll=yes +fi + # check for epoll support epoll=no cat > $TMPC << EOF
@@ -3792,6 +3808,9 @@ fi if test "$dup3" = "yes" ; then echo "CONFIG_DUP3=y" >> $config_host_mak fi +if test "$ppoll" = "yes" ; then + echo "CONFIG_PPOLL=y" >> $config_host_mak +fi if test "$epoll" = "yes" ; then echo "CONFIG_EPOLL=y" >> $config_host_mak fi
include/qemu/timer.h +1 -0
@@ -44,6 +44,7 @@ int64_t qemu_clock_expired(QEMUClock *clock);int64_t qemu_clock_deadline (QEMUClock *clock);int64_t qemu_clock_deadline_ns (QEMUClock *clock);int qemu_timeout_ns_to_ms (int64_t ns); +int qemu_poll_ns (GPollFD *fds,uint nfds,int64_t timeout);void qemu_clock_enable (QEMUClock *clock,bool enabled);void qemu_clock_warp (QEMUClock *clock);
qemu-timer.c +24 -0
@@ -37,6 +37,10 @@#include <mmsystem.h> #endif +#ifdef CONFIG_PPOLL +#include <poll.h> +#endif +/***********************************************************/ /* timers */
@@ -328,6 +332,26 @@ int qemu_timeout_ns_to_ms(int64_t ns) } +/* qemu implementation of g_poll which uses a nanosecond timeout but is +* otherwise identical to g_poll +*/ +int qemu_poll_ns (GPollFD *fds,uint nfds,int64_t timeout) +{ +#ifdef CONFIG_PPOLL +if (timeout <0 ) { +return ppoll ((struct pollfd *)fds, nfds,NULL ,NULL ); + }else { +struct timespec ts; + ts.tv_sec = timeout /1000000000LL ; + ts.tv_nsec = timeout %1000000000LL ; +return ppoll ((struct pollfd *)fds, nfds, &ts,NULL ); + } +#else +return g_poll (fds, nfds,qemu_timeout_ns_to_ms (timeout)); +#endif +} + +QEMUTimer *qemu_new_timer (QEMUClock *clock,int scale,QEMUTimerCB *cb,void *opaque) {
Signature · 1 line
1.7.9.5
RFC PATCH v4 6/13 aio / timers: Add a QEMUTimerList to AioContext
In reply to Alex BlighAdd a QEMUTimerList to each AioContext and delete it when the AioContext is freed.
--- async.c | 7 +++++++ include/block/aio.h | 5 +++++ tests/test-aio.c | 3 +++ tests/test-thread-pool.c | 3 +++ 4 files changed, 18 insertions(+)
async.c +7 -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_timerlist (ctx->tl); }static GSourceFuncs aio_source_funcs = {
@@ -215,6 +216,12 @@ AioContext *aio_context_new(void)aio_set_event_notifier (ctx, &ctx->notifier, (EventNotifierHandler *) event_notifier_test_and_clear,NULL ); +/* Assert if we don't have rt_clock yet. If you see this assertion +* it means you are using AioContext without having first called +* init_clocks() in main(). +*/ +assert (rt_clock); + ctx->tl =qemu_new_timerlist (rt_clock);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 QEMUTimerList QEMUTimerList ; + 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; + +/* TimerList for calling timers */ +QEMUTimerList *tl; } AioContext;/* Returns 1 if there are still outstanding AIO requests; 0 otherwise */
tests/test-aio.c +3 -0
@@ -12,6 +12,7 @@#include <glib.h> #include "block/aio.h" +#include "qemu/timer.h" AioContext *ctx;
@@ -628,6 +629,8 @@ int main(int argc, char **argv) {GSource *src; +init_clocks (); + ctx =aio_context_new (); src =aio_get_g_source (ctx);g_source_attach (src,NULL );
tests/test-thread-pool.c +3 -0
@@ -3,6 +3,7 @@#include "block/aio.h" #include "block/thread-pool.h" #include "block/block.h" +#include "qemu/timer.h" static AioContext *ctx;static ThreadPool *pool;
@@ -205,6 +206,8 @@ int main(int argc, char **argv) {int ret; +init_clocks (); + ctx =aio_context_new (); pool =aio_get_thread_pool (ctx);
Signature · 1 line
1.7.9.5
RFC PATCH v4 8/13 aio / timers: aio_ctx_prepare sets timeout from AioContext timers
In reply to Alex BlighCalculate the timeout in aio_ctx_prepare taking into account the timers attached to the AioContext.
Alter aio_ctx_check similarly.
--- async.c | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-)
async.c +11 -2
@@ -123,13 +123,14 @@ aio_ctx_prepare(GSource *source, gint *timeout) {AioContext *ctx = (AioContext *) source;QEMUBH *bh; +int deadline;for (bh = ctx->first_bh; bh; bh = bh->next) {if (!bh->deleted && bh->scheduled) {if (bh->idle) {/* idle bottom halves will be polled at least * every 10ms */ - *timeout =10 ; + *timeout =qemu_soonest_timeout (*timeout,10 ); }else {/* non-idle bottom halves will be executed * immediately */
@@ -139,6 +140,14 @@ aio_ctx_prepare(GSource *source, gint *timeout) } } + deadline =qemu_timeout_ns_to_ms (qemu_timerlist_deadline_ns (ctx->tl)); +if (deadline ==0 ) { + *timeout =0 ; +return true; + }else { + *timeout =qemu_soonest_timeout (*timeout, deadline); + } +return false; }
@@ -153,7 +162,7 @@ aio_ctx_check(GSource *source)return true; } } -return aio_pending (ctx); +return aio_pending (ctx) || (qemu_timerlist_deadline_ns (ctx->tl) >=0 ); } static gboolean
Signature · 1 line
1.7.9.5
RFC PATCH v4 7/13 aio / timers: Add an AioContext pointer to QEMUTimerList
In reply to Alex BlighAdd an AioContext pointer to QEMUTimerList so it knows what to notify on a timer change.
--- async.c | 1 + include/qemu/timer.h | 3 +++ qemu-timer.c | 12 ++++++++++++ 3 files changed, 16 insertions(+)
async.c +1 -0
@@ -222,6 +222,7 @@ AioContext *aio_context_new(void)*/ assert (rt_clock); ctx->tl =qemu_new_timerlist (rt_clock); +qemu_timerlist_set_ctx (ctx->tl, ctx);return ctx; }
include/qemu/timer.h +3 -0
@@ -17,6 +17,7 @@typedef struct QEMUClock QEMUClock ;typedef struct QEMUTimerList QEMUTimerList ; +typedef struct AioContext AioContext ;typedef void QEMUTimerCB (void *opaque);/* The real time clock should be used only for stuff which does not
@@ -52,6 +53,8 @@ int64_t qemu_timerlist_deadline(QEMUTimerList *tl);int64_t qemu_timerlist_deadline_ns (QEMUTimerList *tl);QEMUClock *qemu_timerlist_get_clock (QEMUTimerList *tl);QEMUTimerList *qemu_clock_get_default_timerlist (QEMUClock *clock); +AioContext *qemu_timerlist_get_ctx (QEMUTimerList *tl); +void qemu_timerlist_set_ctx (QEMUTimerList *tl,AioContext * ctx);int qemu_timeout_ns_to_ms (int64_t ns);int qemu_poll_ns (GPollFD *fds,uint nfds,int64_t timeout);void qemu_clock_enable (QEMUClock *clock,bool enabled);
qemu-timer.c +12 -0
@@ -29,6 +29,7 @@#include "hw/hw.h" #include "qemu/timer.h" +#include "block/aio.h" #ifdef CONFIG_POSIX #include <pthread.h> #endif
@@ -70,6 +71,7 @@ struct QEMUTimerList {QEMUClock *clock;QEMUTimer *active_timers;QLIST_ENTRY (QEMUTimerList) list; +AioContext *ctx; };struct QEMUTimer {
@@ -379,6 +381,16 @@ QEMUTimerList *qemu_clock_get_default_timerlist(QEMUClock *clock)return clock->default_timerlist; } +AioContext *qemu_timerlist_get_ctx (QEMUTimerList *tl) +{ +return tl->ctx; +} + +void qemu_timerlist_set_ctx (QEMUTimerList *tl,AioContext * ctx) +{ + tl->ctx = ctx; +} +/* Transition function to convert a nanosecond timeout to ms * This is used where a system does not support ppoll */
Signature · 1 line
1.7.9.5
RFC PATCH v4 9/13 aio / timers: Convert aio_poll to use AioContext timers' deadline
In reply to Alex BlighConvert aio_poll to use deadline based on AioContext's timers.
aio_poll has been changed to return accurately whether progress has occurred. Prior to this commit, aio_poll always returned true if g_poll was entered, whether or not any progress was made. This required a change to tests/test-aio.c where an assert was backwards.
--- aio-posix.c | 20 +++++++++++++------- aio-win32.c | 22 +++++++++++++++++++--- tests/test-aio.c | 4 ++-- 3 files changed, 34 insertions(+), 12 deletions(-)
aio-posix.c +13 -7
@@ -166,6 +166,10 @@ static bool aio_dispatch(AioContext *ctx)g_free (tmp); } } + +/* Run our timers */ + progress |=qemu_run_timerlist (ctx->tl); +return progress; }
@@ -232,9 +236,9 @@ bool aio_poll(AioContext *ctx, bool blocking) }/* wait until next event */ - ret =g_poll ((GPollFD *)ctx->pollfds->data, - ctx->pollfds->len, - blocking ?-1 :0 ); + ret =qemu_poll_ns ((GPollFD *)ctx->pollfds->data, + ctx->pollfds->len, + blocking ?qemu_timerlist_deadline_ns (ctx->tl) :0 );/* if we have any readable fds, dispatch event */ if (ret >0 ) {
@@ -245,11 +249,13 @@ bool aio_poll(AioContext *ctx, bool blocking) node->pfd.revents = pfd->revents; } } -if (aio_dispatch (ctx)) { - progress = true; - } + } + +/* Run dispatch even if there were no readable fds to run timers */ +if (aio_dispatch (ctx)) { + progress = true; }assert (progress || busy); -return true; +return progress; }
aio-win32.c +19 -3
@@ -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_timerlist (ctx->tl); +/* * 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_timerlist_ns (ctx->tl)) :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_timerlist (ctx->tl); + } +assert (progress || busy); -return true; +return progress; }
tests/test-aio.c +2 -2
@@ -316,13 +316,13 @@ static void test_wait_event_notifier_noflush(void)event_notifier_set (&data.e);g_assert (aio_poll (ctx, false)); g_assert_cmpint(data.n, ==, 1); -g_assert (aio_poll (ctx, false)); +g_assert (!aio_poll (ctx, false)); g_assert_cmpint(data.n, ==, 1);event_notifier_set (&data.e);g_assert (aio_poll (ctx, false)); g_assert_cmpint(data.n, ==, 2); -g_assert (aio_poll (ctx, false)); +g_assert (!aio_poll (ctx, false)); g_assert_cmpint(data.n, ==, 2);event_notifier_set (&dummy.e);
Signature · 1 line
1.7.9.5
RFC PATCH v4 11/13 aio / timers: on timer modification, qemu_notify or aio_notify
In reply to Alex BlighOn qemu_mod_timer_ns, ensure qemu_notify or aio_notify is called to end the appropriate poll().
On qemu_clock_enable, ensure qemu_notify or aio_notify is called for all QEMUTimerLists attached to the QEMUClock.
--- qemu-timer.c | 22 +++++++++++++++++++--- 1 file changed, 19 insertions(+), 3 deletions(-)
qemu-timer.c +19 -3
@@ -295,11 +295,29 @@ void qemu_free_timerlist(QEMUTimerList *tl)g_free (tl); } +static void qemu_timerlist_notify (QEMUTimerList *tl) +{ +if (tl->ctx) { +aio_notify (tl->ctx); + }else { +qemu_notify_event (); + } +} + +static void qemu_clock_notify (QEMUClock *clock) +{ +QEMUTimerList *tl; + QLIST_FOREACH(tl, &clock->timerlists, list) { +qemu_timerlist_notify (tl); + } +} +void qemu_clock_enable (QEMUClock *clock,bool enabled) {bool old = clock->enabled; clock->enabled = enabled;if (enabled && !old) { +qemu_clock_notify (clock);qemu_rearm_alarm_timer (alarm_timer); } }
@@ -514,9 +532,7 @@ void qemu_mod_timer_ns(QEMUTimer *ts, int64_t expire_time) }/* Interrupt execution to force deadline recalculation. */ qemu_clock_warp (ts->tl->clock); -if (use_icount) { -qemu_notify_event (); - } +qemu_timerlist_notify (ts->tl); } }
Signature · 1 line
1.7.9.5
RFC PATCH v4 10/13 aio / timers: Convert mainloop to use timeout
In reply to Alex BlighConvert mainloop to use timeout from 3 static timers.
--- main-loop.c | 48 +++++++++++++++++++++++++++++++++++++----------- 1 file changed, 37 insertions(+), 11 deletions(-)
main-loop.c +37 -11
@@ -155,10 +155,11 @@ static int max_priority;static int glib_pollfds_idx;static int glib_n_poll_fds; -static void glib_pollfds_fill (uint32_t *cur_timeout) +static void glib_pollfds_fill (int64_t *cur_timeout) {GMainContext *context =g_main_context_default ();int timeout =0 ; +int64_t timeout_ns;int n;g_main_context_prepare (context, &max_priority);
@@ -174,9 +175,13 @@ static void glib_pollfds_fill(uint32_t *cur_timeout) glib_n_poll_fds); }while (n != glib_n_poll_fds); -if (timeout >=0 && timeout < *cur_timeout) { - *cur_timeout = timeout; +if (timeout <0 ) { + timeout_ns =-1 ; + }else { + timeout_ns = (int64_t )timeout * (int64_t )SCALE_MS ; } + + *cur_timeout =qemu_soonest_timeout (timeout_ns, *cur_timeout); }static void glib_pollfds_poll (void )
@@ -191,7 +196,7 @@ static void glib_pollfds_poll(void)#define MAX_MAIN_LOOP_SPIN (1000) -static int os_host_main_loop_wait (uint32_t timeout) +static int os_host_main_loop_wait (int64_t timeout) {int ret;static int spin_counter;
@@ -214,7 +219,7 @@ static int os_host_main_loop_wait(uint32_t timeout) notified = true; } - timeout =1 ; + timeout =SCALE_MS ; }if (timeout >0 ) {
@@ -224,7 +229,7 @@ static int os_host_main_loop_wait(uint32_t timeout) spin_counter++; } - ret =g_poll ((GPollFD *)gpollfds->data, gpollfds->len, timeout); + ret =qemu_poll_ns ((GPollFD *)gpollfds->data, gpollfds->len, timeout);if (timeout >0 ) {qemu_mutex_lock_iothread ();
@@ -373,7 +378,7 @@ static void pollfds_poll(GArray *pollfds, int nfds, fd_set *rfds, } } -static int os_host_main_loop_wait (uint32_t timeout) +static int os_host_main_loop_wait (int64_t timeout) {GMainContext *context =g_main_context_default ();GPollFD poll_fds[1024 *2 ];/* this is probably overkill */
@@ -382,6 +387,7 @@ static int os_host_main_loop_wait(uint32_t timeout)PollingEntry *pe;WaitObjects *w = &wait_objects;gint poll_timeout; +int64_t poll_timeout_ns;static struct timeval tv0;fd_set rfds, wfds, xfds;int nfds;
@@ -419,12 +425,17 @@ static int os_host_main_loop_wait(uint32_t timeout) poll_fds[n_poll_fds + i].events =G_IO_IN ; } -if (poll_timeout <0 || timeout < poll_timeout) { - poll_timeout = timeout; +if (poll_timeout <0 ) { + poll_timeout_ns =-1 ; + }else { + poll_timeout_ns = (int64_t )poll_timeout * (int64_t )SCALE_MS ; } + poll_timeout_ns =qemu_soonest_timeout (poll_timeout_ns, timeout); +qemu_mutex_unlock_iothread (); - g_poll_ret =g_poll (poll_fds, n_poll_fds + w->num, poll_timeout); + g_poll_ret =qemu_poll_ns (poll_fds, n_poll_fds + w->num, poll_timeout_ns); +qemu_mutex_lock_iothread ();if (g_poll_ret >0 ) {for (i =0 ; i < w->num; i++) {
@@ -449,6 +460,7 @@ int main_loop_wait(int nonblocking) {int ret;uint32_t timeout =UINT32_MAX ; +int64_t timeout_ns;if (nonblocking) { timeout =0 ;
@@ -462,7 +474,21 @@ int main_loop_wait(int nonblocking)slirp_pollfds_fill (gpollfds);#endif qemu_iohandler_fill (gpollfds); - ret =os_host_main_loop_wait (timeout); + +if (timeout ==UINT32_MAX ) { + timeout_ns =-1 ; + }else { + timeout_ns = (uint64_t )timeout * (int64_t )(SCALE_MS ); + } + + timeout_ns =qemu_soonest_timeout (timeout_ns, +qemu_clock_deadline_ns (rt_clock)); + timeout_ns =qemu_soonest_timeout (timeout_ns, +qemu_clock_deadline_ns (vm_clock)); + timeout_ns =qemu_soonest_timeout (timeout_ns, +qemu_clock_deadline_ns (host_clock)); + + ret =os_host_main_loop_wait (timeout_ns);qemu_iohandler_poll (gpollfds, ret);#ifdef CONFIG_SLIRP slirp_pollfds_poll (gpollfds, (ret <0 ));
Signature · 1 line
1.7.9.5
RFC PATCH v4 5/13 aio / timers: Split QEMUClock into QEMUClock and QEMUTimerList
In reply to Alex BlighSplit QEMUClock into QEMUClock and QEMUTimerList so that we can have more than one QEMUTimerList associated with the same clock.
Introduce a default_timerlist concept and make existing qemu_clock_* calls that actually should operate on a QEMUTimerList call the relevant QEMUTimerList implementations, using the clock's default timerlist. This vastly reduces the invasiveness of this change and means the API stays constant for existing users.
Introduce a list of QEMUTimerLists associated with each clock so that reenabling the clock can cause all the notifiers to be called. Note the code to do the notifications is added in a later patch.
--- include/qemu/timer.h | 26 +++++++++ qemu-timer.c | 150 +++++++++++++++++++++++++++++++++++++++----------- 2 files changed, 143 insertions(+), 33 deletions(-)
include/qemu/timer.h +26 -0
@@ -16,6 +16,7 @@#define QEMU_CLOCK_HOST 2typedef struct QEMUClock QEMUClock ; +typedef struct QEMUTimerList QEMUTimerList ;typedef void QEMUTimerCB (void *opaque);/* The real time clock should be used only for stuff which does not
@@ -38,11 +39,19 @@ extern QEMUClock *host_clock;QEMUClock *qemu_new_clock (int type);void qemu_free_clock (QEMUClock *clock); +QEMUTimerList *qemu_new_timerlist (QEMUClock *clock); +void qemu_free_timerlist (QEMUTimerList *tl);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_timerlist_has_timers (QEMUTimerList *tl); +int64_t qemu_timerlist_expired (QEMUTimerList *tl); +int64_t qemu_timerlist_deadline (QEMUTimerList *tl); +int64_t qemu_timerlist_deadline_ns (QEMUTimerList *tl); +QEMUClock *qemu_timerlist_get_clock (QEMUTimerList *tl); +QEMUTimerList *qemu_clock_get_default_timerlist (QEMUClock *clock);int qemu_timeout_ns_to_ms (int64_t ns);int qemu_poll_ns (GPollFD *fds,uint nfds,int64_t timeout);void qemu_clock_enable (QEMUClock *clock,bool enabled);
@@ -54,6 +63,8 @@ void qemu_unregister_clock_reset_notifier(QEMUClock *clock,QEMUTimer *qemu_new_timer (QEMUClock *clock,int scale,QEMUTimerCB *cb,void *opaque); +QEMUTimer *qemu_new_timer_timerlist (QEMUTimerList *tl,int scale, +QEMUTimerCB *cb,void *opaque);void qemu_free_timer (QEMUTimer *ts);void qemu_del_timer (QEMUTimer *ts);void qemu_mod_timer_ns (QEMUTimer *ts,int64_t expire_time);
@@ -63,6 +74,7 @@ bool qemu_timer_expired(QEMUTimer *timer_head, int64_t current_time);uint64_t qemu_timer_expire_time_ns (QEMUTimer *ts);bool qemu_run_timers (QEMUClock *clock); +bool qemu_run_timerlist (QEMUTimerList *tl);bool qemu_run_all_timers (void );void configure_alarms (char const *opt);void init_clocks (void );
@@ -87,12 +99,26 @@ static inline QEMUTimer *qemu_new_timer_ns(QEMUClock *clock, QEMUTimerCB *cb,return qemu_new_timer (clock,SCALE_NS , cb, opaque); } +static inline QEMUTimer *qemu_new_timer_timerlist_ns (QEMUTimerList *tl, +QEMUTimerCB *cb, +void *opaque) +{ +return qemu_new_timer_timerlist (tl,SCALE_NS , cb, opaque); +} +static inline QEMUTimer *qemu_new_timer_ms (QEMUClock *clock,QEMUTimerCB *cb,void *opaque) {return qemu_new_timer (clock,SCALE_MS , cb, opaque); } +static inline QEMUTimer *qemu_new_timer_timerlist_ms (QEMUTimerList *tl, +QEMUTimerCB *cb, +void *opaque) +{ +return qemu_new_timer_timerlist (tl,SCALE_MS , cb, opaque); +} +static inline int64_t qemu_get_clock_ms (QEMUClock *clock) {return qemu_get_clock_ns (clock) /SCALE_MS ;
qemu-timer.c +117 -33
299 lines not on this page · raw message
Signature · 1 line
1.7.9.5
RFC PATCH v4 13/13 aio / timers: Add test harness for AioContext timers
In reply to Alex BlighAdd a test harness for AioContext timers. The g_source equivalent is unsatisfactory as it suffers from false wakeups.
--- tests/test-aio.c | 136 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 136 insertions(+)
tests/test-aio.c +136 -0
@@ -32,6 +32,15 @@ typedef struct {int max; } BHTestData; +typedef struct { +QEMUTimer *timer; +QEMUTimerList *tl; +int n; +int max; +int64_t ns; +AioContext *ctx; +}TimerTestData ; +static void bh_test_cb (void *opaque) {BHTestData *data = opaque;
@@ -40,6 +49,25 @@ static void bh_test_cb(void *opaque) } } +static void timer_test_cb (void *opaque) +{ +TimerTestData *data = opaque; +if (++data->n < data->max) { +qemu_mod_timer (data->timer, +qemu_get_clock_ns (qemu_timerlist_get_clock (data->tl)) + + data->ns); + } +} + +static void dummy_io_handler_read (void *opaque) +{ +} + +static int dummy_io_handler_flush (void *opaque) +{ +return 1 ; +} +static void bh_delete_cb (void *opaque) {BHTestData *data = opaque;
@@ -341,6 +369,64 @@ static void test_wait_event_notifier_noflush(void)event_notifier_cleanup (&data.e); } +static void test_timer_schedule (void ) +{ +TimerTestData data = { .n =0 , .ctx = ctx, .ns =SCALE_MS *750LL , + .max =2 , .tl = ctx->tl }; +int pipefd[2 ]; + +/* aio_poll will not block to wait for timers to complete unless it has +* an fd to wait on. Fixing this breaks other tests. So create a dummy one. +*/ +g_assert (!pipe2 (pipefd,O_NONBLOCK )); +aio_set_fd_handler (ctx, pipefd[0 ], + dummy_io_handler_read,NULL , dummy_io_handler_flush, +NULL ); +aio_poll (ctx, false); + + data.timer =qemu_new_timer_timerlist (data.tl,SCALE_NS , + timer_test_cb, &data); +qemu_mod_timer (data.timer, +qemu_get_clock_ns (qemu_timerlist_get_clock (data.tl)) + + data.ns); + + g_assert_cmpint(data.n, ==, 0); + +/* qemu_mod_timer may well cause an event notifer to have gone off, +* so clear that +*/ +do {}while (aio_poll (ctx, false)); + +g_assert (!aio_poll (ctx, false)); + g_assert_cmpint(data.n, ==, 0); + +sleep (1 ); + g_assert_cmpint(data.n, ==, 0); + +g_assert (aio_poll (ctx, false)); + g_assert_cmpint(data.n, ==, 1); + +/* qemu_mod_timer called by our callback */ +do {}while (aio_poll (ctx, false)); + +g_assert (!aio_poll (ctx, false)); + g_assert_cmpint(data.n, ==, 1); + +g_assert (aio_poll (ctx, true)); + g_assert_cmpint(data.n, ==, 2); + +/* As max is now 2, an event notifier should not have gone off */ + +g_assert (!aio_poll (ctx, false)); + g_assert_cmpint(data.n, ==, 2); + +aio_set_fd_handler (ctx, pipefd[0 ],NULL ,NULL ,NULL ,NULL ); +close (pipefd[0 ]); +close (pipefd[1 ]); + +qemu_del_timer (data.timer); +} +/* Now the same tests, using the context as a GSource. They are * very similar to the ones above, with g_main_context_iteration * replacing aio_poll. However:
@@ -623,6 +709,54 @@ static void test_source_wait_event_notifier_noflush(void)event_notifier_cleanup (&data.e); } +static void test_source_timer_schedule (void ) +{ +TimerTestData data = { .n =0 , .ctx = ctx, .ns =SCALE_MS *750LL , + .max =2 , .tl = ctx->tl }; +int pipefd[2 ]; +int64_t expiry; + +/* aio_poll will not block to wait for timers to complete unless it has +* an fd to wait on. Fixing this breaks other tests. So create a dummy one. +*/ +g_assert (!pipe2 (pipefd,O_NONBLOCK )); +aio_set_fd_handler (ctx, pipefd[0 ], + dummy_io_handler_read,NULL , dummy_io_handler_flush, +NULL ); +do {}while (g_main_context_iteration (NULL , false)); + + data.timer =qemu_new_timer_timerlist (data.tl,SCALE_NS , + timer_test_cb, &data); + expiry =qemu_get_clock_ns (qemu_timerlist_get_clock (data.tl)) + + data.ns; +qemu_mod_timer (data.timer, expiry); + + g_assert_cmpint(data.n, ==, 0); + +sleep (1 ); + g_assert_cmpint(data.n, ==, 0); + +g_assert (g_main_context_iteration (NULL , false)); + g_assert_cmpint(data.n, ==, 1); + +/* The comment above was not kidding when it said this wakes up itself */ +do { +g_assert (g_main_context_iteration (NULL , true)); + }while (qemu_get_clock_ns ( +qemu_timerlist_get_clock (data.tl)) <= expiry); +sleep (1 ); +g_main_context_iteration (NULL , false); + + g_assert_cmpint(data.n, ==, 2); + +aio_set_fd_handler (ctx, pipefd[0 ],NULL ,NULL ,NULL ,NULL ); +close (pipefd[0 ]); +close (pipefd[1 ]); + +qemu_del_timer (data.timer); +} + +/* End of tests. */ int main (int argc,char **argv)
@@ -651,6 +785,7 @@ int main(int argc, char **argv)g_test_add_func ("/aio/event/wait" , test_wait_event_notifier);g_test_add_func ("/aio/event/wait/no-flush-cb" , test_wait_event_notifier_noflush);g_test_add_func ("/aio/event/flush" , test_flush_event_notifier); +g_test_add_func ("/aio/timer/schedule" , test_timer_schedule);g_test_add_func ("/aio-gsource/notify" , test_source_notify);g_test_add_func ("/aio-gsource/flush" , test_source_flush);
@@ -665,5 +800,6 @@ int main(int argc, char **argv)g_test_add_func ("/aio-gsource/event/wait" , test_source_wait_event_notifier);g_test_add_func ("/aio-gsource/event/wait/no-flush-cb" , test_source_wait_event_notifier_noflush);g_test_add_func ("/aio-gsource/event/flush" , test_source_flush_event_notifier); +g_test_add_func ("/aio-gsource/timer/schedule" , test_source_timer_schedule);return g_test_run (); }
Signature · 1 line
1.7.9.5
RFC PATCH v4 12/13 aio / timers: Remove alarm timers
In reply to Alex BlighRemove alarm timers from qemu-timers.c now we use g_poll / ppoll instead.
--- include/qemu/timer.h | 2 - main-loop.c | 4 - qemu-timer.c | 501 +------------------------------------------------- vl.c | 4 +- 4 files changed, 4 insertions(+), 507 deletions(-)
include/qemu/timer.h +0 -2
@@ -79,9 +79,7 @@ uint64_t qemu_timer_expire_time_ns(QEMUTimer *ts);bool qemu_run_timers (QEMUClock *clock);bool qemu_run_timerlist (QEMUTimerList *tl);bool qemu_run_all_timers (void ); -void configure_alarms (char const *opt);void init_clocks (void ); -int init_timer_alarm (void );int64_t cpu_get_ticks (void );void cpu_enable_ticks (void );
main-loop.c +0 -4
@@ -131,10 +131,6 @@ int qemu_init_main_loop(void)GSource *src;init_clocks (); -if (init_timer_alarm () <0 ) { -fprintf (stderr,"could not initialize alarm timer\n" ); -exit (1 ); - } ret =qemu_signal_init ();if (ret) {
qemu-timer.c +1 -500
539 lines not on this page · raw message
vl.c +3 -1
@@ -3714,7 +3714,9 @@ int main(int argc, char **argv, char **envp) old_param =1 ;break ;case QEMU_OPTION_clock: -configure_alarms (optarg); +/* Clock options no longer exist. Keep this option for +* backward compatibility. +*/ break ;case QEMU_OPTION_startdate:configure_rtc_date_offset (optarg,1 );
Signature · 1 line
1.7.9.5
Re: [Qemu-devel] [RFC] [PATCHv4 01/13] aio / timers: add qemu-timer.c utility functions
In reply to Alex BlighQuoted 51 lines · Alex Bligh
Add
qemu_free_clockand exposeqemu_new_clockand clock types.Add utility functions to
qemu-timer.cfor nanosecond timing.Add
qemu_clock_deadline_nsto calculate deadlines to nanosecond accuracy.Add utility function
qemu_soonest_timeoutto calculate soonest deadline.Add
qemu_timeout_ns_to_msto convert a timeout in nanoseconds back to milliseconds for when ppoll is not used.Signed-off-by Alex Bligh <[email protected]>--- include/qemu/timer.h | 17 ++++++++++++++ qemu-timer.c | 63 +++++++++++++++++++++++++++++++++++++++++++++----- 2 files changed, 74 insertions(+), 6 deletions(-)include/qemu/timer.h +8
-0@@ -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,14 @@ 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); +int qemu_timeout_ns_to_ms (int64_t ns);void qemu_clock_enable (QEMUClock *clock,bool enabled);void qemu_clock_warp (QEMUClock *clock);@@ -63,6 +71,15 @@ int64_t cpu_get_ticks(void);void cpu_enable_ticks (void );void cpu_disable_ticks (void );
include/qemu/timer.h int64_t cpu_get_ticks(void); · line 82
Quoted 10 lines
include/qemu/timer.h +9
-0+static inline int64_t qemu_soonest_timeout (int64_t timeout1,int64_t timeout2) +{ +/* we can abuse the fact that -1 (which means infinite) is a maximal +* value when cast to unsigned. As this is disgusting, it's kept in +* one inline function. +*/ +return ((uint64_t ) timeout1 < (uint64_t ) timeout2) ? timeout1 : timeout2; +} +
It becomes much less disgusting if timeouts are made unsigned. I agree we can do it later.
Quoted 108 lines
include/qemu/timer.h +0
-0static inline QEMUTimer *qemu_new_timer_ns (QEMUClock *clock,QEMUTimerCB *cb,void *opaque) {qemu-timer.c +57
-6@@ -40,10 +40,6 @@/***********************************************************/ /* timers */ -#define QEMU_CLOCK_REALTIME 0 -#define QEMU_CLOCK_VIRTUAL 1 -#define QEMU_CLOCK_HOST 2 -struct QEMUClock {QEMUTimer *active_timers;@@ -231,7 +227,7 @@ QEMUClock *rt_clock;QEMUClock *vm_clock;QEMUClock *host_clock; -static QEMUClock *qemu_new_clock (int type) +QEMUClock *qemu_new_clock (int type) {QEMUClock *clock;@@ -243,6 +239,11 @@ static QEMUClock *qemu_new_clock(int type)return clock; } +void qemu_free_clock (QEMUClock *clock) +{ +g_free (clock); +} +void qemu_clock_enable (QEMUClock *clock,bool enabled) {bool old = clock->enabled;@@ -268,7 +269,7 @@ int64_t qemu_clock_deadline(QEMUClock *clock)/* To avoid problems with overflow limit this to 2^32. */ int64_t delta =INT32_MAX ; -if (clock->active_timers) { +if (clock->enabled && clock->active_timers) { delta = clock->active_timers->expire_time -qemu_get_clock_ns (clock); }if (delta <0 ) {@@ -277,6 +278,56 @@ int64_t qemu_clock_deadline(QEMUClock *clock)return delta; } +/* +* As above, but return -1 for no deadline, and do not cap to 2^32 +* as we know the result is always positive. +*/ + +int64_t qemu_clock_deadline_ns (QEMUClock *clock) +{ +int64_t delta; + +if (!clock->enabled || !clock->active_timers) { +return -1 ; + } + + delta = clock->active_timers->expire_time -qemu_get_clock_ns (clock); + +if (delta <=0 ) { +return 0 ; + } + +return delta; +} + +/* Transition function to convert a nanosecond timeout to ms +* This is used where a system does not support ppoll +*/ +int qemu_timeout_ns_to_ms (int64_t ns) +{ +int64_t ms; +if (ns <0 ) { +return -1 ; + } + +if (!ns) { +return 0 ; + } + +/* Always round up, because it's better to wait too long than to wait too +* little and effectively busy-wait +*/ + ms = (ns +SCALE_MS -1 ) /SCALE_MS ; + +/* To avoid overflow problems, limit this to 2^31, i.e. approx 25 days */ +if (ms > (int64_t )INT32_MAX ) { + ms =INT32_MAX ; + } + +return (int ) ms; +} + +QEMUTimer *qemu_new_timer (QEMUClock *clock,int scale,QEMUTimerCB *cb,void *opaque) {Signature · 1 line
1.7.9.5
Re: [Qemu-devel] [RFC] [PATCHv4 10/13] aio / timers: Convert mainloop to use timeout
In reply to Alex BlighQuoted 119 lines · Alex Bligh
Convert mainloop to use timeout from 3 static timers.
Signed-off-by Alex Bligh <[email protected]>--- main-loop.c | 48 +++++++++++++++++++++++++++++++++++++----------- 1 file changed, 37 insertions(+), 11 deletions(-)main-loop.c +23
-11@@ -155,10 +155,11 @@ static int max_priority;static int glib_pollfds_idx;static int glib_n_poll_fds; -static void glib_pollfds_fill (uint32_t *cur_timeout) +static void glib_pollfds_fill (int64_t *cur_timeout) {GMainContext *context =g_main_context_default ();int timeout =0 ; +int64_t timeout_ns;int n;g_main_context_prepare (context, &max_priority);@@ -174,9 +175,13 @@ static void glib_pollfds_fill(uint32_t *cur_timeout) glib_n_poll_fds); }while (n != glib_n_poll_fds); -if (timeout >=0 && timeout < *cur_timeout) { - *cur_timeout = timeout; +if (timeout <0 ) { + timeout_ns =-1 ; + }else { + timeout_ns = (int64_t )timeout * (int64_t )SCALE_MS ; } + + *cur_timeout =qemu_soonest_timeout (timeout_ns, *cur_timeout); }static void glib_pollfds_poll (void )@@ -191,7 +196,7 @@ static void glib_pollfds_poll(void)#define MAX_MAIN_LOOP_SPIN (1000) -static int os_host_main_loop_wait (uint32_t timeout) +static int os_host_main_loop_wait (int64_t timeout) {int ret;static int spin_counter;@@ -214,7 +219,7 @@ static int os_host_main_loop_wait(uint32_t timeout) notified = true; } - timeout =1 ; + timeout =SCALE_MS ; }if (timeout >0 ) {@@ -224,7 +229,7 @@ static int os_host_main_loop_wait(uint32_t timeout) spin_counter++; } - ret =g_poll ((GPollFD *)gpollfds->data, gpollfds->len, timeout); + ret =qemu_poll_ns ((GPollFD *)gpollfds->data, gpollfds->len, timeout);if (timeout >0 ) {qemu_mutex_lock_iothread ();@@ -373,7 +378,7 @@ static void pollfds_poll(GArray *pollfds, int nfds, fd_set *rfds, } } -static int os_host_main_loop_wait (uint32_t timeout) +static int os_host_main_loop_wait (int64_t timeout) {GMainContext *context =g_main_context_default ();GPollFD poll_fds[1024 *2 ];/* this is probably overkill */ @@ -382,6 +387,7 @@ static int os_host_main_loop_wait(uint32_t timeout)PollingEntry *pe;WaitObjects *w = &wait_objects;gint poll_timeout; +int64_t poll_timeout_ns;static struct timeval tv0;fd_set rfds, wfds, xfds;int nfds;@@ -419,12 +425,17 @@ static int os_host_main_loop_wait(uint32_t timeout) poll_fds[n_poll_fds + i].events =G_IO_IN ; } -if (poll_timeout <0 || timeout < poll_timeout) { - poll_timeout = timeout; +if (poll_timeout <0 ) { + poll_timeout_ns =-1 ; + }else { + poll_timeout_ns = (int64_t )poll_timeout * (int64_t )SCALE_MS ; } + poll_timeout_ns =qemu_soonest_timeout (poll_timeout_ns, timeout); +qemu_mutex_unlock_iothread (); - g_poll_ret =g_poll (poll_fds, n_poll_fds + w->num, poll_timeout); + g_poll_ret =qemu_poll_ns (poll_fds, n_poll_fds + w->num, poll_timeout_ns); +qemu_mutex_lock_iothread ();if (g_poll_ret >0 ) {for (i =0 ; i < w->num; i++) {@@ -449,6 +460,7 @@ int main_loop_wait(int nonblocking) {int ret;uint32_t timeout =UINT32_MAX ; +int64_t timeout_ns;if (nonblocking) { timeout =0 ;@@ -462,7 +474,21 @@ int main_loop_wait(int nonblocking)slirp_pollfds_fill (gpollfds);#endif qemu_iohandler_fill (gpollfds); - ret =os_host_main_loop_wait (timeout); +
main-loop.c int main_loop_wait(int nonblocking) · line 487
Quoted 10 lines
main-loop.c +10
-0+if (timeout ==UINT32_MAX ) { + timeout_ns =-1 ; + }else { + timeout_ns = (uint64_t )timeout * (int64_t )(SCALE_MS ); + } + + timeout_ns =qemu_soonest_timeout (timeout_ns, +qemu_clock_deadline_ns (rt_clock)); + timeout_ns =qemu_soonest_timeout (timeout_ns, +qemu_clock_deadline_ns (vm_clock));
This must not be included if use_icount.
Allowing only one rt_clock clock for each AioContext is a simplification, but I'm worried that it will be a problem later. For example, the block layer wants to use vm_clock. Perhaps QEMUTimerList should really have three lists, one for each clock type?
Once you do this, you get some complications due to more data structures, but other code is simplified noticeably. For example, you lose the concept of a default timerlist (it's just the timerlist of the default AioContext). And because all timerlists have an AioContext, you do not need to special case aio_notify() vs. qemu_notify_event().
There are a couple of places to be careful about, of course. For example,
if (use_icount && qemu_clock_deadline(vm_clock) <= 0) {
qemu_notify_event();
}in cpus.c must be changed to iterate over all timerlists.
Paolo
Quoted 10 lines
main-loop.c +4
-0+ timeout_ns =qemu_soonest_timeout (timeout_ns, +qemu_clock_deadline_ns (host_clock)); + + ret =os_host_main_loop_wait (timeout_ns);qemu_iohandler_poll (gpollfds, ret);#ifdef CONFIG_SLIRP slirp_pollfds_poll (gpollfds, (ret <0 ));Signature · 1 line
1.7.9.5
Re: [Qemu-devel] [RFC] [PATCHv4 10/13] aio / timers: Convert mainloop to use timeout
Paolo,
Quoted 18 lines
Quoted 18 lines
main-loop.c +6
-1@@ -449,6 +460,7 @@ int main_loop_wait(int nonblocking) {int ret;uint32_t timeout =UINT32_MAX ; +int64_t timeout_ns;if (nonblocking) { timeout =0 ;@@ -462,7 +474,21 @@ int main_loop_wait(int nonblocking)slirp_pollfds_fill (gpollfds);# endif qemu_iohandler_fill (gpollfds); - ret =os_host_main_loop_wait (timeout); + +if (timeout ==UINT32_MAX ) { + timeout_ns =-1 ; + }else { + timeout_ns = (uint64_t )timeout * (int64_t )(SCALE_MS );
Quoted 8 lines
main-loop.c int main_loop_wait(int nonblocking) · line 487
Quoted 6 lines
main-loop.c +6
-0+ } + + timeout_ns =qemu_soonest_timeout (timeout_ns, +qemu_clock_deadline_ns (rt_clock)); + timeout_ns =qemu_soonest_timeout (timeout_ns, +qemu_clock_deadline_ns (vm_clock));This must not be included if
use_icount.
Really? qemu_run_all_timers was running all 3 timers irrespective of use_icount, and doing a qemu_notify if anything expired, so I'm merely mimicking the existing behaviour here.
I'm not quite sure what use_icount does. Does vm_clock get disabled if it is set? (in which case it won't matter).
Quoted 4 lines
Allowing only one
rt_clockclock for each AioContext is a simplification, but I'm worried that it will be a problem later. For example, the block layer wants to usevm_clock. Perhaps QEMUTimerList should really have three lists, one for each clock type?
Well currently each QEMUClock has a default QEMUTimerList, so that wouldn't work well - see below.
The way it's done at the moment is that the QEMUTimerList user can create as many QEMUTimerLists as he wants. So AioContext asks for one of one type. It could equally ask for three - one of each type.
I think that's probably adequate.
Quoted 4 lines
Once you do this, you get some complications due to more data structures, but other code is simplified noticeably. For example, you lose the concept of a default timerlist (it's just the timerlist of the default AioContext).
Yep - per the above that's really intrusive (I think I touched well over a hundred files). The problem is that lots of things refer to vm_clock to set a timer (when it's a clock so should use a timer list) and also to vm_clock to read the timer (which is a clock function). Changing vm_clock to vm_timerlist and vm_clock was truly horrible. Hence the default timer list concept, which I admit is not great but saved a horribly intrusive patch not all of which I could test. I did this patch, and scrapped it.
Quoted 1 line
And because all timerlists have an AioContext,
Well old callers, particularly those not using an AioContext, would not have an AioContext would they?
Quoted 2 lines
you do not
need to special caseaio_notify()vs.qemu_notify_event().
Well, if I do a v5, I was going to make the constructor for creating a timerlist specify a callback function to say what should happen if the clock is enabled etc., and if none was specified call qemu_notify_event(). The AioContext user would specify a callback that called aio_notify(). This would be a bit nicer.
Quoted 7 lines
There are a couple of places to be careful about, of course. For example,
if (use_icount && qemu_clock_deadline(vm_clock) <= 0) { qemu_notify_event(); }in
cpus.cmust be changed to iterate over all timerlists.
I was trying hard to avoid anything having to iterate over all timerlists, and leave the timerlist to be per-thread where possible. This may well fail for the clock warp stuff. I probably need to exactly the same as on qemu_clock_enable() here if use_icount is true. WDYT?
Signature · 1 line
Alex Bligh
Re: [Qemu-devel] [RFC] [PATCHv4 10/13] aio / timers: Convert mainloop to use timeout
Quoted 29 lines · Alex Bligh
Paolo,
Quoted 18 lines
Quoted 18 lines
main-loop.c +6
-1@@ -449,6 +460,7 @@ int main_loop_wait(int nonblocking) {int ret;uint32_t timeout =UINT32_MAX ; +int64_t timeout_ns;if (nonblocking) { timeout =0 ;@@ -462,7 +474,21 @@ int main_loop_wait(int nonblocking)slirp_pollfds_fill (gpollfds);# endif qemu_iohandler_fill (gpollfds); - ret =os_host_main_loop_wait (timeout); + +if (timeout ==UINT32_MAX ) { + timeout_ns =-1 ; + }else { + timeout_ns = (uint64_t )timeout * (int64_t )(SCALE_MS );Quoted 8 lines
main-loop.c int main_loop_wait(int nonblocking) · line 487
Quoted 6 lines
main-loop.c +6
-0+ } + + timeout_ns =qemu_soonest_timeout (timeout_ns, +qemu_clock_deadline_ns (rt_clock)); + timeout_ns =qemu_soonest_timeout (timeout_ns, +qemu_clock_deadline_ns (vm_clock));This must not be included if
use_icount.
Quoted 3 lines
Really?
qemu_run_all_timerswas running all 3 timers irrespective ofuse_icount, and doing aqemu_notifyif anything expired, so I'm merely mimicking the existing behaviour here.
Maybe I'm misreading the code. If it is a replacement of qemu_next_alarm_deadline, then it is indeed omitting vm_clock.
Quoted 2 lines
I'm not quite sure what
use_icountdoes. Doesvm_clockget disabled if it is set? (in which case it won't matter).
It doesn't count nanoseconds anymore. The clock is updated by the VCPU thread. When the VCPU thread notices that the clock is past the earliest timers, it does a qemu_notify_event(). That exits the g_poll and qemu_run_all_timers then can process the callbacks.
Quoted 11 lines
The way it's done at the moment is that the QEMUTimerList user can create as many QEMUTimerLists as he wants. So AioContext asks for one of one type. It could equally ask for three - one of each type.
I think that's probably adequate.
Quoted 4 lines
Once you do this, you get some complications due to more data structures, but other code is simplified noticeably. For example, you lose the concept of a default timerlist (it's just the timerlist of the default AioContext).
Quoted 4 lines
Yep - per the above that's really intrusive (I think I touched well over a hundred files). The problem is that lots of things refer to
vm_clockto set a timer (when it's a clock so should use a timer list) and also tovm_clockto read the timer (which is a clock function).
Yes, that's fine. You can still keep the shorter invocation, but instead of using clock->timerlist it would use qemu_aio_context->clocks[clock->type].
Related to this, a better name for the "full" functions taking a timerlist could be simply timer_new_ns etc. And I would remove the allocation step for these functions. It is shameful how little we use qemu_free_timer, and removing allocation would "fix" the problem completely for users of the QEMUTimerList-based functions.
It's already a convention to use qemu_* only for functions that use some global state, for example qemu_notify_event() vs. aio_notify().
Quoted 4 lines
Quoted 1 line
And because all timerlists have an AioContext,
Well old callers, particularly those not using an AioContext, would not have an AioContext would they?
It would be qemu_aio_context.
Quoted 5 lines
I was trying hard to avoid anything having to iterate over all timerlists, and leave the timerlist to be per-thread where possible. This may well fail for the clock warp stuff. I probably need to exactly the same as on
qemu_clock_enable()here ifuse_icountis true. WDYT?
Yes. This:
qemu_mod_timer(icount_warp_timer, vm_clock_warp_start + deadline);
would have to use the earliest deadline of all vm_clock timerlists.
And this:
if (qemu_clock_expired(vm_clock)) {
qemu_notify_event();
}would also have to walk all timerlists for vm_clock, and notify those that have expired. But you would not need one warp timer per timerlist.
Paolo
Re: [Qemu-devel] [RFC] [PATCHv4 10/13] aio / timers: Convert mainloop to use timeout
Paolo,
(apologies for taking a little time to reply to this one)
Quoted 29 lines · --On 1 August 2013 16:14:11 +0200 Paolo Bonzini <[email protected]> wrote:
Quoted 29 lines
Quoted 28 lines
Quoted 22 lines
main-loop.c +10
-1@@ -449,6 +460,7 @@ int main_loop_wait(int nonblocking) {int ret;uint32_t timeout =UINT32_MAX ; +int64_t timeout_ns;if (nonblocking) { timeout =0 ;@@ -462,7 +474,21 @@ int main_loop_wait(int nonblocking)slirp_pollfds_fill (gpollfds);# endif qemu_iohandler_fill (gpollfds); - ret =os_host_main_loop_wait (timeout); + +if (timeout ==UINT32_MAX ) { + timeout_ns =-1 ; + }else { + timeout_ns = (uint64_t )timeout * (int64_t )(SCALE_MS ); + } + + timeout_ns = qemu_soonest_timeout(timeout_ns, +Quoted 4 lines
qemu_clock_deadline_ns(rt_clock)); +timeout_ns=
qemu_soonest_timeout(timeout_ns,
+qemu_clock_deadline_ns(vm_clock));This must not be included if
use_icount.
Quoted 6 lines
Quoted 3 lines
Really?
qemu_run_all_timerswas running all 3 timers irrespective ofuse_icount, and doing aqemu_notifyif anything expired, so I'm merely mimicking the existing behaviour here.Maybe I'm misreading the code. If it is a replacement of
qemu_next_alarm_deadline, then it is indeed omittingvm_clock.
Well, qemu_next_alarm_deadline calculated the time at which the next alarm signal would be sent. What I'm calculating is the wait timeout for poll(), either in the mainloop or in AioContext.
In the mainloop, what this appears to do is to ignore the vm_clock (whether or not it is enabled) in calculating the timeout if use_icount is set. However qemu_run_timers is running the timers attached to the vm_clock whether or not use_icount is set.
If use_icount is set, should be assuming vm_timers have an infinite timeout for the purpose of calculating timeouts? What the the timer has already expired (i.e. qemu_run_timers would run it immediately)?
In AioContext, there were previously no timers, so I'm not sure whether or not this should be doing the same thing.
Quoted 4 lines
Quoted 2 lines
I'm not quite sure what
use_icountdoes. Doesvm_clockget disabled if it is set? (in which case it won't matter).It doesn't count nanoseconds anymore.
"it" being the vm_timer clock source I presume.
Quoted 4 lines
The clock is updated by the
VCPU thread. When the VCPU thread notices that the clock is past the earliest timers, it does aqemu_notify_event(). That exits theg_pollandqemu_run_all_timersthen can process the callbacks.
So the first problem is that this will not cause the other threads to have aio_notify called, which they should do.
The second question is whether this approach is consistent with using a timeout for poll at all. I think the answer to that is "yes" PROVIDED THAT the VCPU thread is never the one doing the poll, or we'll go in with an infinite timeout.
I am presuming use_icount never changes at runtime? If not, I think the way to address this is to add a 'lineartime' member to QEMUClock, and set that false on the vm_timer QEMUClock if use_icount is true. I can then ignore it when looping through clocksources bases on that rather than special casing vm_clock.
Quoted 8 lines
Quoted 4 lines
Yep - per the above that's really intrusive (I think I touched well over a hundred files). The problem is that lots of things refer to
vm_clockto set a timer (when it's a clock so should use a timer list) and also tovm_clockto read the timer (which is a clock function).Yes, that's fine. You can still keep the shorter invocation, but instead of using
clock->timerlistit would use qemu_aio_context->clocks[clock->type].
Note that the aio context contains a QEMUTimerList and not a clock. So it would have 3 QEMUTimerLists, and not 3 QEMUClocks.
I might see if there is a neat way to encapsulate these (QEMUTimerListGroup or similar?) but yes I get the idea.
Do you want this in a v5 or are we polishing the ball a bit much here?
Quoted 8 lines
Related to this, a better name for the "full" functions taking a timerlist could be simply
timer_new_nsetc. And I would remove the allocation step for these functions. It is shameful how little we useqemu_free_timer, and removing allocation would "fix" the problem completely for users of the QEMUTimerList-based functions.It's already a convention to use qemu_* only for functions that use some global state, for example
qemu_notify_event()vs.aio_notify().
Agree
Quoted 6 lines
Quoted 4 lines
Quoted 1 line
And because all timerlists have an AioContext,
Well old callers, particularly those not using an AioContext, would not have an AioContext would they?
It would be
qemu_aio_context.
If this were the case, then the timeout for the mainloop would be the same as the timeout for the qemu_aio_context. Is that an improvement?
What about a (putative) thread that wants a timer but has no AioContext?
Quoted 14 lines
Quoted 5 lines
I was trying hard to avoid anything having to iterate over all timerlists, and leave the timerlist to be per-thread where possible. This may well fail for the clock warp stuff. I probably need to exactly the same as on
qemu_clock_enable()here ifuse_icountis true. WDYT?Yes. This:
qemu_mod_timer(icount_warp_timer, vm_clock_warp_start + deadline);would have to use the earliest deadline of all
vm_clocktimerlists.And this:
Quoted 7 lines
if (qemu_clock_expired(vm_clock)) { qemu_notify_event(); }would also have to walk all timerlists for
vm_clock, and notify those that have expired. But you would not need one warp timer per timerlist.
OK looks like I need to review the patch looking for use_icount.
Signature · 1 line
Alex Bligh
v5 cover aio / timers: Add AioContext timers and use ppoll
This patch series adds support for timers attached to an AioContext clock which get called within aio_poll.
In doing so it removes alarm timers and moves to use ppoll where possible.
This patch set 'sort of' passes make check (see below for caveat) including a new test harness for the aio timers, but has not been tested much beyond that. In particular, the win32 changes have not even been compile tested. Equally, alterations to use_icount are untested.
Caveat: I have had to alter tests/test-aio.c so the following error no longer occurs.
ERROR:tests/test-aio.c:346:test_wait_event_notifier_noflush: assertion failed: (aio_poll(ctx, false))
As gar as I can tell, this check was incorrect, in that it checking aio_poll makes progress when in fact it should not make progress. I fixed an issue where aio_poll was (as far as I can tell) wrongly returning true on a timeout, and that generated this error.
Note also the comment on patch 15 in relation to a possible bug in cpus.c.
Changes since v4:
* Rename qemu_timerlist_ functions to timer_list (per Paolo Bonzini)
* Rename qemu_timer_.*timerlist.* to timer_ (per Paolo Bonzini)
* Use enum for QEMUClockType
* Put clocks into an array; remove global variables
* Introduce QEMUTimerListGroup - a timeliest of each type
* Add a QEMUTimerListGroup to AioContext
* Use a callback on timer modification, rather than binding in AioContext into the timeliest
* Make cpus.c iterate over all timerlists when it does a notify
* Make cpus.c icount timeout use soonest timeout
across all timerlists
Not changed since v4:
* Paolo Bonzini suggested getting rid of the default timerlist from the clock. The replacement would be the default AioContext's timer lists. I am concerned about the lifetime of the default AioContext compared to existing timers that might use the default clocks. This could be changed at a later date after code audit.
Changes since v3:
* Split up QEMUClock and QEMUClock list
* Improve commenting
* Fix comment in vl.c
* Change test/test-aio.c to reflect correct behaviour in aio_poll.
Changes since v2:
* Reordered to remove alarm timers last
* Added prctl(PR_SET_TIMERSLACK, 1, ...)
* Renamed qemu_g_poll_ns to qemu_poll_ns
* Moved declaration of above & drop glib types
* Do not use a global list of qemu clocks
* Add AioContext * to QEMUClock
* Split up conversion to use ppoll and timers
* Indentation fix
* Fix aio_win32.c aio_poll to return progress
* aio_notify / qemu_notify when timers are modified
* change comment in deprecation of clock options
Alex Bligh (16):
aio / timers: add qemu-timer.c utility functions
aio / timers: add ppoll support with qemu_poll_ns
aio / timers: Add prctl(PR_SET_TIMERSLACK, 1, ...) to reduce timer
slack
aio / timers: Make qemu_run_timers and qemu_run_all_timers return
progress
aio / timers: Split QEMUClock into QEMUClock and QEMUTimerList aio / timers: Untangle include files
aio / timers: Add QEMUTimerListGroup and helper functions aio / timers: Add QEMUTimerListGroup to AioContext
aio / timers: Add a notify callback to QEMUTimerList aio / timers: aio_ctx_prepare sets timeout from AioContext timers aio / timers: Convert aio_poll to use AioContext timers' deadline aio / timers: Convert mainloop to use timeout
aio / timers: On timer modification, qemu_notify or aio_notify aio / timers: Use all timerlists in icount warp calculations aio / timers: Remove alarm timers
aio / timers: Add test harness for AioContext timers
aio-posix.c | 20 +- aio-win32.c | 22 +- async.c | 20 +- configure | 37 +++ cpus.c | 44 ++- dma-helpers.c | 1 + hw/dma/xilinx_axidma.c | 1 + hw/timer/arm_timer.c | 1 + hw/timer/grlib_gptimer.c | 2 + hw/timer/imx_epit.c | 1 + hw/timer/imx_gpt.c | 1 + hw/timer/lm32_timer.c | 1 + hw/timer/puv3_ost.c | 1 + hw/timer/slavio_timer.c | 1 + hw/timer/xilinx_timer.c | 1 + hw/usb/hcd-uhci.c | 1 + include/block/aio.h | 4 + include/block/block_int.h | 1 + include/block/coroutine.h | 2 + include/qemu/timer.h | 122 ++++++- main-loop.c | 49 ++- migration-exec.c | 1 + migration-fd.c | 1 + migration-tcp.c | 1 + migration-unix.c | 1 + migration.c | 1 + nbd.c | 1 + net/net.c | 1 + net/socket.c | 1 + qemu-coroutine-io.c | 1 + qemu-io-cmds.c | 1 + qemu-nbd.c | 1 + qemu-timer.c | 803 ++++++++++++++++----------------------------- slirp/misc.c | 1 + tests/test-aio.c | 141 +++++++- tests/test-thread-pool.c | 3 + thread-pool.c | 1 + ui/vnc-auth-vencrypt.c | 2 +- ui/vnc-ws.c | 1 + vl.c | 4 +- 40 files changed, 736 insertions(+), 564 deletions(-)
Signature · 1 line
1.7.9.5
RFC PATCH v5 1/16 aio / timers: add qemu-timer.c utility functions
Add qemu_free_clock and expose qemu_new_clock and clock types.
Add utility functions to qemu-timer.c for nanosecond timing.
Add qemu_clock_deadline_ns to calculate deadlines to nanosecond accuracy.
Add utility function qemu_soonest_timeout to calculate soonest deadline.
Add qemu_timeout_ns_to_ms to convert a timeout in nanoseconds back to milliseconds for when ppoll is not used.
--- include/qemu/timer.h | 17 ++++++++++++++ qemu-timer.c | 63 +++++++++++++++++++++++++++++++++++++++++++++----- 2 files changed, 74 insertions(+), 6 deletions(-)
include/qemu/timer.h +17 -0
@@ -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,14 @@ 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); +int qemu_timeout_ns_to_ms (int64_t ns);void qemu_clock_enable (QEMUClock *clock,bool enabled);void qemu_clock_warp (QEMUClock *clock);
@@ -63,6 +71,15 @@ int64_t cpu_get_ticks(void);void cpu_enable_ticks (void );void cpu_disable_ticks (void ); +static inline int64_t qemu_soonest_timeout (int64_t timeout1,int64_t timeout2) +{ +/* we can abuse the fact that -1 (which means infinite) is a maximal +* value when cast to unsigned. As this is disgusting, it's kept in +* one inline function. +*/ +return ((uint64_t ) timeout1 < (uint64_t ) timeout2) ? timeout1 : timeout2; +} +static inline QEMUTimer *qemu_new_timer_ns (QEMUClock *clock,QEMUTimerCB *cb,void *opaque) {
qemu-timer.c +57 -6
93 lines not on this page · raw message
Signature · 1 line
1.7.9.5
RFC PATCH v5 4/16 aio / timers: Make qemu_run_timers and qemu_run_all_timers return progress
In reply to Alex BlighMake qemu_run_timers and qemu_run_all_timers return progress so that aio_poll etc. can determine whether a timer has been run.
--- include/qemu/timer.h | 4 ++-- qemu-timer.c | 18 ++++++++++++------ 2 files changed, 14 insertions(+), 8 deletions(-)
include/qemu/timer.h +2 -2
@@ -62,8 +62,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 );
qemu-timer.c +12 -6
@@ -451,13 +451,14 @@ 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; +bool progress = false;if (!clock->enabled) -return ; +return progress; current_time =qemu_get_clock_ns (clock);for (;;) {
@@ -471,7 +472,9 @@ void qemu_run_timers(QEMUClock *clock)/* run the callback (the timer list can be modified) */ ts->cb (ts->opaque); + progress = true; } +return progress; }int64_t qemu_get_clock_ns (QEMUClock *clock)
@@ -526,20 +529,23 @@ 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 progress = false; alarm_timer->pending = false;/* vm time timers */ -qemu_run_timers (vm_clock); -qemu_run_timers (rt_clock); -qemu_run_timers (host_clock); + progress |=qemu_run_timers (vm_clock); + progress |=qemu_run_timers (rt_clock); + progress |=qemu_run_timers (host_clock);/* rearm timer, if not periodic */ if (alarm_timer->expired) { alarm_timer->expired = false;qemu_rearm_alarm_timer (alarm_timer); } + +return progress; }#ifdef _WIN32
Signature · 1 line
1.7.9.5
RFC PATCH v5 7/16 aio / timers: Add QEMUTimerListGroup and helper functions
In reply to Alex BlighAdd QEMUTimerListGroup and helper functions, to represent a QEMUTimerList associated with each clock. Add a default QEMUTimerListGroup representing the default timer lists which are not associated with any other object (e.g. an AioContext as added by future patches).
--- include/qemu/timer.h | 7 +++++++ qemu-timer.c | 41 +++++++++++++++++++++++++++++++++++++++++ 2 files changed, 48 insertions(+)
include/qemu/timer.h +7 -0
@@ -19,8 +19,10 @@ typedef enum {typedef struct QEMUClock QEMUClock ;typedef struct QEMUTimerList QEMUTimerList ; +typedef QEMUTimerList *QEMUTimerListGroup [QEMU_CLOCK_MAX ];typedef void QEMUTimerCB (void *opaque); +extern QEMUTimerListGroup qemu_default_tlg;extern QEMUClock *QEMUClocks[QEMU_CLOCK_MAX ];static inline QEMUClock *qemu_get_clock (QEMUClockType type)
@@ -69,6 +71,11 @@ int64_t timerlist_deadline_ns(QEMUTimerList *tl);QEMUClock *timerlist_get_clock (QEMUTimerList *tl);bool timerlist_run_timers (QEMUTimerList *tl); +void timerlistgroup_init (QEMUTimerListGroup tlg); +void timerlistgroup_deinit (QEMUTimerListGroup tlg); +bool timerlistgroup_run_timers (QEMUTimerListGroup tlg); +int64_t timerlistgroup_deadline_ns (QEMUTimerListGroup tlg); +int qemu_timeout_ns_to_ms (int64_t ns);int qemu_poll_ns (GPollFD *fds,uint nfds,int64_t timeout);void qemu_clock_enable (QEMUClock *clock,bool enabled);
qemu-timer.c +41 -0
59 lines not on this page · raw message
Signature · 1 line
1.7.9.5
RFC PATCH v5 8/16 aio / timers: Add QEMUTimerListGroup to AioContext
In reply to Alex BlighAdd a QEMUTimerListGroup each AioContext (meaning a QEMUTimerList associated with each clock is added) and delete it when the AioContext is freed.
--- async.c | 2 ++ include/block/aio.h | 4 ++++ tests/test-aio.c | 3 +++ tests/test-thread-pool.c | 3 +++ 4 files changed, 12 insertions(+)
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); +timerlistgroup_deinit (ctx->tlg); }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 ); +timerlistgroup_init (ctx->tlg);return ctx; }
include/block/aio.h +4 -0
@@ -17,6 +17,7 @@#include "qemu-common.h" #include "qemu/queue.h" #include "qemu/event_notifier.h" +#include "qemu/timer.h" typedef struct BlockDriverAIOCB BlockDriverAIOCB ;typedef void BlockDriverCompletionFunc (void *opaque,int ret);
@@ -69,6 +70,9 @@ typedef struct AioContext {/* Thread pool for performing work and receiving completion callbacks */ struct ThreadPool *thread_pool; + +/* TimerLists for calling timers - one per clock type */ +QEMUTimerListGroup tlg; } AioContext;/* Returns 1 if there are still outstanding AIO requests; 0 otherwise */
tests/test-aio.c +3 -0
@@ -12,6 +12,7 @@#include <glib.h> #include "block/aio.h" +#include "qemu/timer.h" AioContext *ctx;
@@ -628,6 +629,8 @@ int main(int argc, char **argv) {GSource *src; +init_clocks (); + ctx =aio_context_new (); src =aio_get_g_source (ctx);g_source_attach (src,NULL );
tests/test-thread-pool.c +3 -0
@@ -3,6 +3,7 @@#include "block/aio.h" #include "block/thread-pool.h" #include "block/block.h" +#include "qemu/timer.h" static AioContext *ctx;static ThreadPool *pool;
@@ -205,6 +206,8 @@ int main(int argc, char **argv) {int ret; +init_clocks (); + ctx =aio_context_new (); pool =aio_get_thread_pool (ctx);
Signature · 1 line
1.7.9.5
RFC PATCH v5 10/16 aio / timers: aio_ctx_prepare sets timeout from AioContext timers
In reply to Alex BlighCalculate the timeout in aio_ctx_prepare taking into account the timers attached to the AioContext.
Alter aio_ctx_check similarly.
--- async.c | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-)
async.c +11 -2
@@ -123,13 +123,14 @@ aio_ctx_prepare(GSource *source, gint *timeout) {AioContext *ctx = (AioContext *) source;QEMUBH *bh; +int deadline;for (bh = ctx->first_bh; bh; bh = bh->next) {if (!bh->deleted && bh->scheduled) {if (bh->idle) {/* idle bottom halves will be polled at least * every 10ms */ - *timeout =10 ; + *timeout =qemu_soonest_timeout (*timeout,10 ); }else {/* non-idle bottom halves will be executed * immediately */
@@ -139,6 +140,14 @@ aio_ctx_prepare(GSource *source, gint *timeout) } } + deadline =qemu_timeout_ns_to_ms (timerlistgroup_deadline_ns (ctx->tlg)); +if (deadline ==0 ) { + *timeout =0 ; +return true; + }else { + *timeout =qemu_soonest_timeout (*timeout, deadline); + } +return false; }
@@ -153,7 +162,7 @@ aio_ctx_check(GSource *source)return true; } } -return aio_pending (ctx); +return aio_pending (ctx) || (timerlistgroup_deadline_ns (ctx->tlg) >=0 ); } static gboolean
Signature · 1 line
1.7.9.5
RFC PATCH v5 12/16 aio / timers: Convert mainloop to use timeout
In reply to Alex BlighConvert mainloop to use timeout from default timerlist group (i.e. the current 3 static timers)
--- main-loop.c | 45 ++++++++++++++++++++++++++++++++++----------- 1 file changed, 34 insertions(+), 11 deletions(-)
main-loop.c +34 -11
@@ -155,10 +155,11 @@ static int max_priority;static int glib_pollfds_idx;static int glib_n_poll_fds; -static void glib_pollfds_fill (uint32_t *cur_timeout) +static void glib_pollfds_fill (int64_t *cur_timeout) {GMainContext *context =g_main_context_default ();int timeout =0 ; +int64_t timeout_ns;int n;g_main_context_prepare (context, &max_priority);
@@ -174,9 +175,13 @@ static void glib_pollfds_fill(uint32_t *cur_timeout) glib_n_poll_fds); }while (n != glib_n_poll_fds); -if (timeout >=0 && timeout < *cur_timeout) { - *cur_timeout = timeout; +if (timeout <0 ) { + timeout_ns =-1 ; + }else { + timeout_ns = (int64_t )timeout * (int64_t )SCALE_MS ; } + + *cur_timeout =qemu_soonest_timeout (timeout_ns, *cur_timeout); }static void glib_pollfds_poll (void )
@@ -191,7 +196,7 @@ static void glib_pollfds_poll(void)#define MAX_MAIN_LOOP_SPIN (1000) -static int os_host_main_loop_wait (uint32_t timeout) +static int os_host_main_loop_wait (int64_t timeout) {int ret;static int spin_counter;
@@ -214,7 +219,7 @@ static int os_host_main_loop_wait(uint32_t timeout) notified = true; } - timeout =1 ; + timeout =SCALE_MS ; }if (timeout >0 ) {
@@ -224,7 +229,7 @@ static int os_host_main_loop_wait(uint32_t timeout) spin_counter++; } - ret =g_poll ((GPollFD *)gpollfds->data, gpollfds->len, timeout); + ret =qemu_poll_ns ((GPollFD *)gpollfds->data, gpollfds->len, timeout);if (timeout >0 ) {qemu_mutex_lock_iothread ();
@@ -373,7 +378,7 @@ static void pollfds_poll(GArray *pollfds, int nfds, fd_set *rfds, } } -static int os_host_main_loop_wait (uint32_t timeout) +static int os_host_main_loop_wait (int64_t timeout) {GMainContext *context =g_main_context_default ();GPollFD poll_fds[1024 *2 ];/* this is probably overkill */
@@ -382,6 +387,7 @@ static int os_host_main_loop_wait(uint32_t timeout)PollingEntry *pe;WaitObjects *w = &wait_objects;gint poll_timeout; +int64_t poll_timeout_ns;static struct timeval tv0;fd_set rfds, wfds, xfds;int nfds;
@@ -419,12 +425,17 @@ static int os_host_main_loop_wait(uint32_t timeout) poll_fds[n_poll_fds + i].events =G_IO_IN ; } -if (poll_timeout <0 || timeout < poll_timeout) { - poll_timeout = timeout; +if (poll_timeout <0 ) { + poll_timeout_ns =-1 ; + }else { + poll_timeout_ns = (int64_t )poll_timeout * (int64_t )SCALE_MS ; } + poll_timeout_ns =qemu_soonest_timeout (poll_timeout_ns, timeout); +qemu_mutex_unlock_iothread (); - g_poll_ret =g_poll (poll_fds, n_poll_fds + w->num, poll_timeout); + g_poll_ret =qemu_poll_ns (poll_fds, n_poll_fds + w->num, poll_timeout_ns); +qemu_mutex_lock_iothread ();if (g_poll_ret >0 ) {for (i =0 ; i < w->num; i++) {
@@ -449,6 +460,7 @@ int main_loop_wait(int nonblocking) {int ret;uint32_t timeout =UINT32_MAX ; +int64_t timeout_ns;if (nonblocking) { timeout =0 ;
@@ -462,7 +474,18 @@ int main_loop_wait(int nonblocking)slirp_pollfds_fill (gpollfds);#endif qemu_iohandler_fill (gpollfds); - ret =os_host_main_loop_wait (timeout); + +if (timeout ==UINT32_MAX ) { + timeout_ns =-1 ; + }else { + timeout_ns = (uint64_t )timeout * (int64_t )(SCALE_MS ); + } + + timeout_ns =qemu_soonest_timeout (timeout_ns, +timerlistgroup_deadline_ns ( + qemu_default_tlg)); + + ret =os_host_main_loop_wait (timeout_ns);qemu_iohandler_poll (gpollfds, ret);#ifdef CONFIG_SLIRP slirp_pollfds_poll (gpollfds, (ret <0 ));
Signature · 1 line
1.7.9.5
RFC PATCH v5 13/16 aio / timers: On timer modification, qemu_notify or aio_notify
In reply to Alex BlighOn qemu_mod_timer_ns, ensure qemu_notify or aio_notify is called to end the appropriate poll(), irrespective of use_icount value.
On qemu_clock_enable, ensure qemu_notify or aio_notify is called for all QEMUTimerLists attached to the QEMUClock.
--- include/qemu/timer.h | 1 + qemu-timer.c | 13 ++++++++++--- 2 files changed, 11 insertions(+), 3 deletions(-)
include/qemu/timer.h +1 -0
@@ -62,6 +62,7 @@ int64_t qemu_clock_deadline(QEMUClock *clock);int64_t qemu_clock_deadline_ns (QEMUClock *clock);bool qemu_clock_use_for_deadline (QEMUClock *clock);QEMUTimerList *qemu_clock_get_default_timerlist (QEMUClock *clock); +void qemu_clock_notify (QEMUClock *clock);QEMUTimerList *timerlist_new (QEMUClockType type);void timerlist_free (QEMUTimerList *tl);
qemu-timer.c +10 -3
@@ -304,11 +304,20 @@ bool qemu_clock_use_for_deadline(QEMUClock *clock)return !(use_icount && (clock->type =QEMU_CLOCK_VIRTUAL )); } +void qemu_clock_notify (QEMUClock *clock) +{ +QEMUTimerList *tl; + QLIST_FOREACH(tl, &clock->timerlists, list) { +timerlist_notify (tl); + } +} +void qemu_clock_enable (QEMUClock *clock,bool enabled) {bool old = clock->enabled; clock->enabled = enabled;if (enabled && !old) { +qemu_clock_notify (clock);qemu_rearm_alarm_timer (alarm_timer); } }
@@ -529,9 +538,7 @@ void qemu_mod_timer_ns(QEMUTimer *ts, int64_t expire_time) }/* Interrupt execution to force deadline recalculation. */ qemu_clock_warp (ts->tl->clock); -if (use_icount) { -timerlist_notify (ts->tl); - } +timerlist_notify (ts->tl); } }
Signature · 1 line
1.7.9.5
RFC PATCH v5 2/16 aio / timers: add ppoll support with qemu_poll_ns
In reply to Alex BlighAdd qemu_poll_ns which works like g_poll but takes a nanosecond timeout.
--- configure | 19 +++++++++++++++++++ include/qemu/timer.h | 1 + qemu-timer.c | 24 ++++++++++++++++++++++++ 3 files changed, 44 insertions(+)
configure +19 -0
@@ -2801,6 +2801,22 @@ if compile_prog "" "" ; then dup3=yes fi +# check for ppoll support +ppoll=no +cat > $TMPC << EOF +#include <poll.h> + +int main(void) +{ + struct pollfd pfd = { .fd = 0, .events = 0, .revents = 0 }; + ppoll(&pfd, 1, 0, 0); + return 0; +} +EOF +if compile_prog "" "" ; then + ppoll=yes +fi + # check for epoll support epoll=no cat > $TMPC << EOF
@@ -3792,6 +3808,9 @@ fi if test "$dup3" = "yes" ; then echo "CONFIG_DUP3=y" >> $config_host_mak fi +if test "$ppoll" = "yes" ; then + echo "CONFIG_PPOLL=y" >> $config_host_mak +fi if test "$epoll" = "yes" ; then echo "CONFIG_EPOLL=y" >> $config_host_mak fi
include/qemu/timer.h +1 -0
@@ -44,6 +44,7 @@ int64_t qemu_clock_expired(QEMUClock *clock);int64_t qemu_clock_deadline (QEMUClock *clock);int64_t qemu_clock_deadline_ns (QEMUClock *clock);int qemu_timeout_ns_to_ms (int64_t ns); +int qemu_poll_ns (GPollFD *fds,uint nfds,int64_t timeout);void qemu_clock_enable (QEMUClock *clock,bool enabled);void qemu_clock_warp (QEMUClock *clock);
qemu-timer.c +24 -0
@@ -37,6 +37,10 @@#include <mmsystem.h> #endif +#ifdef CONFIG_PPOLL +#include <poll.h> +#endif +/***********************************************************/ /* timers */
@@ -328,6 +332,26 @@ int qemu_timeout_ns_to_ms(int64_t ns) } +/* qemu implementation of g_poll which uses a nanosecond timeout but is +* otherwise identical to g_poll +*/ +int qemu_poll_ns (GPollFD *fds,uint nfds,int64_t timeout) +{ +#ifdef CONFIG_PPOLL +if (timeout <0 ) { +return ppoll ((struct pollfd *)fds, nfds,NULL ,NULL ); + }else { +struct timespec ts; + ts.tv_sec = timeout /1000000000LL ; + ts.tv_nsec = timeout %1000000000LL ; +return ppoll ((struct pollfd *)fds, nfds, &ts,NULL ); + } +#else +return g_poll (fds, nfds,qemu_timeout_ns_to_ms (timeout)); +#endif +} + +QEMUTimer *qemu_new_timer (QEMUClock *clock,int scale,QEMUTimerCB *cb,void *opaque) {
Signature · 1 line
1.7.9.5
RFC PATCH v5 3/16 aio / timers: Add prctl(PR_SET_TIMERSLACK, 1, ...) to reduce timer slack
In reply to Alex BlighWhere supported, called prctl(PR_SET_TIMERSLACK, 1, ...) to set one nanosecond timer slack to increase precision of timer calls.
--- configure | 18 ++++++++++++++++++ qemu-timer.c | 7 +++++++ 2 files changed, 25 insertions(+)
configure +18 -0
@@ -2817,6 +2817,21 @@ if compile_prog "" "" ; then ppoll=yes fi +# check for prctl(PR_SET_TIMERSLACK , ... ) support +prctl_pr_set_timerslack=no +cat > $TMPC << EOF +#include <sys/prctl.h> + +int main(void) +{ + prctl(PR_SET_TIMERSLACK, 1, 0, 0, 0); + return 0; +} +EOF +if compile_prog "" "" ; then + prctl_pr_set_timerslack=yes +fi + # check for epoll support epoll=no cat > $TMPC << EOF
@@ -3811,6 +3826,9 @@ fi if test "$ppoll" = "yes" ; then echo "CONFIG_PPOLL=y" >> $config_host_mak fi +if test "$prctl_pr_set_timerslack" = "yes" ; then + echo "CONFIG_PRCTL_PR_SET_TIMERSLACK=y" >> $config_host_mak +fi if test "$epoll" = "yes" ; then echo "CONFIG_EPOLL=y" >> $config_host_mak fi
qemu-timer.c +7 -0
@@ -41,6 +41,10 @@#include <poll.h> #endif +#ifdef CONFIG_PRCTL_PR_SET_TIMERSLACK +#include <sys/prctl.h> +#endif +/***********************************************************/ /* timers */
@@ -512,6 +516,9 @@ void init_clocks(void) vm_clock =qemu_new_clock (QEMU_CLOCK_VIRTUAL ); host_clock =qemu_new_clock (QEMU_CLOCK_HOST ); } +#ifdef CONFIG_PRCTL_PR_SET_TIMERSLACK +prctl (PR_SET_TIMERSLACK ,1 ,0 ,0 ,0 ); +#endif }uint64_t qemu_timer_expire_time_ns (QEMUTimer *ts)
Signature · 1 line
1.7.9.5
RFC PATCH v5 11/16 aio / timers: Convert aio_poll to use AioContext timers' deadline
In reply to Alex BlighConvert aio_poll to use deadline based on AioContext's timers.
aio_poll has been changed to return accurately whether progress has occurred. Prior to this commit, aio_poll always returned true if g_poll was entered, whether or not any progress was made. This required a change to tests/test-aio.c where an assert was backwards.
--- aio-posix.c | 20 +++++++++++++------- aio-win32.c | 22 +++++++++++++++++++--- tests/test-aio.c | 4 ++-- 3 files changed, 34 insertions(+), 12 deletions(-)
aio-posix.c +13 -7
@@ -166,6 +166,10 @@ static bool aio_dispatch(AioContext *ctx)g_free (tmp); } } + +/* Run our timers */ + progress |=timerlistgroup_run_timers (ctx->tlg); +return progress; }
@@ -232,9 +236,9 @@ bool aio_poll(AioContext *ctx, bool blocking) }/* wait until next event */ - ret =g_poll ((GPollFD *)ctx->pollfds->data, - ctx->pollfds->len, - blocking ?-1 :0 ); + ret =qemu_poll_ns ((GPollFD *)ctx->pollfds->data, + ctx->pollfds->len, + blocking ?timerlistgroup_deadline_ns (ctx->tlg) :0 );/* if we have any readable fds, dispatch event */ if (ret >0 ) {
@@ -245,11 +249,13 @@ bool aio_poll(AioContext *ctx, bool blocking) node->pfd.revents = pfd->revents; } } -if (aio_dispatch (ctx)) { - progress = true; - } + } + +/* Run dispatch even if there were no readable fds to run timers */ +if (aio_dispatch (ctx)) { + progress = true; }assert (progress || busy); -return true; +return progress; }
aio-win32.c +19 -3
@@ -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 |=timerlistgroup_run_timers (ctx->tlg); +/* * 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 (timerlistgroup_deadline_ns (ctx->tlg)) :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 |=timerlistgroup_run_timers (ctx->tl); + } +assert (progress || busy); -return true; +return progress; }
tests/test-aio.c +2 -2
@@ -316,13 +316,13 @@ static void test_wait_event_notifier_noflush(void)event_notifier_set (&data.e);g_assert (aio_poll (ctx, false)); g_assert_cmpint(data.n, ==, 1); -g_assert (aio_poll (ctx, false)); +g_assert (!aio_poll (ctx, false)); g_assert_cmpint(data.n, ==, 1);event_notifier_set (&data.e);g_assert (aio_poll (ctx, false)); g_assert_cmpint(data.n, ==, 2); -g_assert (aio_poll (ctx, false)); +g_assert (!aio_poll (ctx, false)); g_assert_cmpint(data.n, ==, 2);event_notifier_set (&dummy.e);
Signature · 1 line
1.7.9.5
RFC PATCH v5 14/16 aio / timers: Use all timerlists in icount warp calculations
In reply to Alex BlighNotify all timerlists derived from vm_clock in icount warp calculations.
When calculating timer delay based on vm_clock deadline, use all timerlists.
For compatibility, maintain an apparent bug where when using icount, if no vm_clock timer was set, qemu_clock_deadline would return INT32_MAX and always set an icount clock expiry about 2 seconds ahead.
NB: thread safety - when different timerlists sit on different threads, this will need some locking.
--- cpus.c | 44 ++++++++++++++++++++++++++++++++++++-------- include/qemu/timer.h | 1 + qemu-timer.c | 16 ++++++++++++++++ 3 files changed, 53 insertions(+), 8 deletions(-)
cpus.c +36 -8
@@ -262,7 +262,7 @@ static void icount_warp_rt(void *opaque) qemu_icount_bias +=MIN (warp_delta, delta); }if (qemu_clock_expired (vm_clock)) { -qemu_notify_event (); +qemu_clock_notify (vm_clock); } } vm_clock_warp_start =-1 ;
@@ -279,7 +279,7 @@ void qtest_clock_warp(int64_t dest)qemu_run_timers (vm_clock); clock =qemu_get_clock_ns (vm_clock); } -qemu_notify_event (); +qemu_clock_notify (vm_clock); }void qemu_clock_warp (QEMUClock *clock)
@@ -314,7 +314,18 @@ void qemu_clock_warp(QEMUClock *clock) } vm_clock_warp_start =qemu_get_clock_ns (rt_clock); - deadline =qemu_clock_deadline (vm_clock); +/* We want to use the earliest deadline from ALL vm_clocks */ + deadline =qemu_clock_deadline_ns_all (vm_clock); + +/* Maintain prior (possibly buggy) behaviour where if no deadline +* was set (as there is no vm_clock timer) or it is more than +* INT32_MAX nanoseconds ahead, we still use INT32_MAX +* nanoseconds. +*/ +if ((deadline <0 ) || (deadline >INT32_MAX )) { + deadline =INT32_MAX ; + } +if (deadline >0 ) {/* * Ensure the vm_clock proceeds even when the virtual CPU goes to
@@ -333,8 +344,8 @@ void qemu_clock_warp(QEMUClock *clock)* packets continuously instead of every 100ms. */ qemu_mod_timer (icount_warp_timer, vm_clock_warp_start + deadline); - }else { -qemu_notify_event (); + }else if (deadline ==0 ) { +qemu_clock_notify (vm_clock); } }
@@ -865,8 +876,13 @@ static void *qemu_tcg_cpu_thread_fn(void *arg)while (1 ) {tcg_exec_all (); -if (use_icount &&qemu_clock_deadline (vm_clock) <=0 ) { -qemu_notify_event (); + +if (use_icount) { +int64_t deadline =qemu_clock_deadline_ns_all (vm_clock); + +if (deadline ==0 ) { +qemu_clock_notify (vm_clock); + } }qemu_tcg_wait_io_event (); }
@@ -1142,11 +1158,23 @@ static int tcg_cpu_exec(CPUArchState *env)#endif if (use_icount) {int64_t count; +int64_t deadline;int decr; qemu_icount -= (env->icount_decr.u16.low + env->icount_extra); env->icount_decr.u16.low =0 ; env->icount_extra =0 ; - count =qemu_icount_round (qemu_clock_deadline (vm_clock)); + deadline =qemu_clock_deadline_ns_all (vm_clock); + +/* Maintain prior (possibly buggy) behaviour where if no deadline +* was set (as there is no vm_clock timer) or it is more than +* INT32_MAX nanoseconds ahead, we still use INT32_MAX +* nanoseconds. +*/ +if ((deadline <0 ) || (deadline >INT32_MAX )) { + deadline =INT32_MAX ; + } + + count =qemu_icount_round (deadline); qemu_icount += count; decr = (count >0xffff ) ?0xffff : count; count -= decr;
include/qemu/timer.h +1 -0
@@ -60,6 +60,7 @@ 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_ns_all (QEMUClock *clock);bool qemu_clock_use_for_deadline (QEMUClock *clock);QEMUTimerList *qemu_clock_get_default_timerlist (QEMUClock *clock);void qemu_clock_notify (QEMUClock *clock);
qemu-timer.c +16 -0
@@ -389,6 +389,22 @@ int64_t qemu_clock_deadline_ns(QEMUClock *clock)return timerlist_deadline_ns (clock->default_timerlist); } +/* Calculate the soonest deadline across all timerlists attached +* to the clock. This is used for the icount timeout so we +* ignore whether or not the clock should be used in deadline +* calculations. +*/ +int64_t qemu_clock_deadline_ns_all (QEMUClock *clock) +{ +int64_t deadline =-1 ; +QEMUTimerList *tl; + QLIST_FOREACH(tl, &clock->timerlists, list) { + deadline =qemu_soonest_timeout (deadline, +timerlist_deadline_ns (tl)); + } +return deadline; +} +QEMUClock *timerlist_get_clock (QEMUTimerList *tl) {return tl->clock;
Signature · 1 line
1.7.9.5
RFC PATCH v5 5/16 aio / timers: Split QEMUClock into QEMUClock and QEMUTimerList
In reply to Alex BlighSplit QEMUClock into QEMUClock and QEMUTimerList so that we can have more than one QEMUTimerList associated with the same clock.
Introduce a default_timerlist concept and make existing qemu_clock_* calls that actually should operate on a QEMUTimerList call the relevant QEMUTimerList implementations, using the clock's default timerlist. This vastly reduces the invasiveness of this change and means the API stays constant for existing users.
Introduce a list of QEMUTimerLists associated with each clock so that reenabling the clock can cause all the notifiers to be called. Note the code to do the notifications is added in a later patch.
Switch QEMUClockType to an enum. Remove global variables vm_clock, host_clock and rt_clock and add compatibility defines. Do not fix qemu_next_alarm_deadline as it's going to be deleted.
--- include/qemu/timer.h | 91 +++++++++++++++++++++++-- qemu-timer.c | 185 ++++++++++++++++++++++++++++++++++++++------------ 2 files changed, 224 insertions(+), 52 deletions(-)
include/qemu/timer.h +84 -7
@@ -11,38 +11,65 @@#define SCALE_US 1000#define SCALE_NS 1 -#define QEMU_CLOCK_REALTIME 0 -#define QEMU_CLOCK_VIRTUAL 1 -#define QEMU_CLOCK_HOST 2 +typedef enum { +QEMU_CLOCK_REALTIME =0 , +QEMU_CLOCK_VIRTUAL =1 , +QEMU_CLOCK_HOST =2 , +QEMU_CLOCK_MAX +}QEMUClockType ;typedef struct QEMUClock QEMUClock ; +typedef struct QEMUTimerList QEMUTimerList ;typedef void QEMUTimerCB (void *opaque); +extern QEMUClock *QEMUClocks[QEMU_CLOCK_MAX ]; + +static inline QEMUClock *qemu_get_clock (QEMUClockType type) +{ +return QEMUClocks[type]; +} + +/* These three clocks are maintained here with separate variable +names for compatibility only. +*/ +/* The real time clock should be used only for stuff which does not change the virtual machine state, as it is run even if the virtual machine is stopped. The real time clock has a frequency of 1000 Hz. */ -extern QEMUClock *rt_clock; +#define rt_clock (qemu_get_clock(QEMU_CLOCK_REALTIME))/* The virtual clock is only run during the emulation. It is stopped when the virtual machine is stopped. Virtual timers use a high precision clock, usually cpu cycles (use ticks_per_sec). */ -extern QEMUClock *vm_clock; +#define vm_clock (qemu_get_clock(QEMU_CLOCK_VIRTUAL))/* The host clock should be use for device models that emulate accurate real time sources. It will continue to run when the virtual machine is suspended, and it will reflect system time changes the host may undergo (e.g. due to NTP). The host clock has the same precision as the virtual clock. */ -extern QEMUClock *host_clock; +#define host_clock (qemu_get_clock(QEMU_CLOCK_HOST)) -QEMUClock *qemu_new_clock (int type); +QEMUClock *qemu_new_clock (QEMUClockType 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); +bool qemu_clock_use_for_deadline (QEMUClock *clock); +QEMUTimerList *qemu_clock_get_default_timerlist (QEMUClock *clock); + +QEMUTimerList *timerlist_new (QEMUClockType type); +void timerlist_free (QEMUTimerList *tl); +int64_t timerlist_has_timers (QEMUTimerList *tl); +int64_t timerlist_expired (QEMUTimerList *tl); +int64_t timerlist_deadline (QEMUTimerList *tl); +int64_t timerlist_deadline_ns (QEMUTimerList *tl); +QEMUClock *timerlist_get_clock (QEMUTimerList *tl); +bool timerlist_run_timers (QEMUTimerList *tl); +int qemu_timeout_ns_to_ms (int64_t ns);int qemu_poll_ns (GPollFD *fds,uint nfds,int64_t timeout);void qemu_clock_enable (QEMUClock *clock,bool enabled);
@@ -54,6 +81,8 @@ void qemu_unregister_clock_reset_notifier(QEMUClock *clock,QEMUTimer *qemu_new_timer (QEMUClock *clock,int scale,QEMUTimerCB *cb,void *opaque); +QEMUTimer *timer_new (QEMUTimerList *tl,int scale, +QEMUTimerCB *cb,void *opaque);void qemu_free_timer (QEMUTimer *ts);void qemu_del_timer (QEMUTimer *ts);void qemu_mod_timer_ns (QEMUTimer *ts,int64_t expire_time);
@@ -62,6 +91,42 @@ 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); +/* New format calling conventions for timers */ +static inline void timer_free (QEMUTimer *ts) +{ +qemu_free_timer (ts); +} + +static inline void timer_del (QEMUTimer *ts) +{ +qemu_del_timer (ts); +} + +static inline void timer_mod_ns (QEMUTimer *ts,int64_t expire_time) +{ +qemu_mod_timer_ns (ts, expire_time); +} + +static inline void timer_mod (QEMUTimer *ts,int64_t expire_timer) +{ +qemu_mod_timer (ts, expire_timer); +} + +static inline bool timer_pending (QEMUTimer *ts) +{ +return qemu_timer_pending (ts); +} + +static inline bool timer_expired (QEMUTimer *timer_head,int64_t current_time) +{ +return qemu_timer_expired (timer_head, current_time); +} + +static inline uint64_t timer_expire_time_ns (QEMUTimer *ts) +{ +return qemu_timer_expire_time_ns (ts); +} +bool qemu_run_timers (QEMUClock *clock);bool qemu_run_all_timers (void );void configure_alarms (char const *opt);
@@ -87,12 +152,24 @@ static inline QEMUTimer *qemu_new_timer_ns(QEMUClock *clock, QEMUTimerCB *cb,return qemu_new_timer (clock,SCALE_NS , cb, opaque); } +static inline QEMUTimer *timer_new_ns (QEMUTimerList *tl,QEMUTimerCB *cb, +void *opaque) +{ +return timer_new (tl,SCALE_NS , cb, opaque); +} +static inline QEMUTimer *qemu_new_timer_ms (QEMUClock *clock,QEMUTimerCB *cb,void *opaque) {return qemu_new_timer (clock,SCALE_MS , cb, opaque); } +static inline QEMUTimer *timer_new_ms (QEMUTimerList *tl,QEMUTimerCB *cb, +void *opaque) +{ +return timer_new (tl,SCALE_MS , cb, opaque); +} +static inline int64_t qemu_get_clock_ms (QEMUClock *clock) {return qemu_get_clock_ns (clock) /SCALE_MS ;
qemu-timer.c +140 -45
355 lines not on this page · raw message
Signature · 1 line
1.7.9.5
RFC PATCH v5 6/16 aio / timers: Untangle include files
In reply to Alex Blighinclude/qemu/timer.h has no need to include main-loop.h and doing so causes an issue for the next patch. Unfortunately various files assume including timers.h will pull in main-loop.h. Untangle this mess.
--- dma-helpers.c | 1 + hw/dma/xilinx_axidma.c | 1 + hw/timer/arm_timer.c | 1 + hw/timer/grlib_gptimer.c | 2 ++ hw/timer/imx_epit.c | 1 + hw/timer/imx_gpt.c | 1 + hw/timer/lm32_timer.c | 1 + hw/timer/puv3_ost.c | 1 + hw/timer/slavio_timer.c | 1 + hw/timer/xilinx_timer.c | 1 + hw/usb/hcd-uhci.c | 1 + include/block/block_int.h | 1 + include/block/coroutine.h | 2 ++ include/qemu/timer.h | 1 - migration-exec.c | 1 + migration-fd.c | 1 + migration-tcp.c | 1 + migration-unix.c | 1 + migration.c | 1 + nbd.c | 1 + net/net.c | 1 + net/socket.c | 1 + qemu-coroutine-io.c | 1 + qemu-io-cmds.c | 1 + qemu-nbd.c | 1 + slirp/misc.c | 1 + thread-pool.c | 1 + ui/vnc-auth-vencrypt.c | 2 +- ui/vnc-ws.c | 1 + 29 files changed, 30 insertions(+), 2 deletions(-)
dma-helpers.c +1 -0
@@ -11,6 +11,7 @@#include "trace.h" #include "qemu/range.h" #include "qemu/thread.h" +#include "qemu/main-loop.h" /* #define DEBUG_IOMMU */
hw/dma/xilinx_axidma.c +1 -0
@@ -27,6 +27,7 @@#include "hw/ptimer.h" #include "qemu/log.h" #include "qapi/qmp/qerror.h" +#include "qemu/main-loop.h" #include "hw/stream.h"
hw/timer/arm_timer.c +1 -0
@@ -12,6 +12,7 @@#include "qemu-common.h" #include "hw/qdev.h" #include "hw/ptimer.h" +#include "qemu/main-loop.h" /* Common timer implementation. */
hw/timer/grlib_gptimer.c +2 -0
@@ -25,6 +25,8 @@#include "hw/sysbus.h" #include "qemu/timer.h" #include "hw/ptimer.h" +#include "qemu/timer.h" +#include "qemu/main-loop.h" #include "trace.h"
hw/timer/imx_epit.c +1 -0
@@ -18,6 +18,7 @@#include "hw/ptimer.h" #include "hw/sysbus.h" #include "hw/arm/imx.h" +#include "qemu/main-loop.h" #define TYPE_IMX_EPIT "imx.epit"
hw/timer/imx_gpt.c +1 -0
@@ -18,6 +18,7 @@#include "hw/ptimer.h" #include "hw/sysbus.h" #include "hw/arm/imx.h" +#include "qemu/main-loop.h" #define TYPE_IMX_GPT "imx.gpt"
hw/timer/lm32_timer.c +1 -0
@@ -27,6 +27,7 @@#include "qemu/timer.h" #include "hw/ptimer.h" #include "qemu/error-report.h" +#include "qemu/main-loop.h" #define DEFAULT_FREQUENCY (50*1000000)
hw/timer/puv3_ost.c +1 -0
@@ -10,6 +10,7 @@*/ #include "hw/sysbus.h" #include "hw/ptimer.h" +#include "qemu/main-loop.h" #undef DEBUG_PUV3#include "hw/unicore32/puv3.h"
hw/timer/slavio_timer.c +1 -0
@@ -27,6 +27,7 @@#include "hw/ptimer.h" #include "hw/sysbus.h" #include "trace.h" +#include "qemu/main-loop.h" /* * Registers of hardware timer in sun4m.
hw/timer/xilinx_timer.c +1 -0
@@ -25,6 +25,7 @@#include "hw/sysbus.h" #include "hw/ptimer.h" #include "qemu/log.h" +#include "qemu/main-loop.h" #define D (x)
hw/usb/hcd-uhci.c +1 -0
@@ -32,6 +32,7 @@#include "qemu/iov.h" #include "sysemu/dma.h" #include "trace.h" +#include "qemu/main-loop.h" //#define DEBUG //#define DEBUG_DUMP_DATA
include/block/block_int.h +1 -0
@@ -34,6 +34,7 @@#include "monitor/monitor.h" #include "qemu/hbitmap.h" #include "block/snapshot.h" +#include "qemu/main-loop.h" #define BLOCK_FLAG_ENCRYPT 1#define BLOCK_FLAG_COMPAT6 4
include/block/coroutine.h +2 -0
@@ -19,6 +19,8 @@#include "qemu/queue.h" #include "qemu/timer.h" +typedef struct AioContext AioContext ; +/** * Coroutines are a mechanism for stack switching and can be used for * cooperative userspace threading. These functions provide a simple but
include/qemu/timer.h +0 -1
@@ -2,7 +2,6 @@#define QEMU_TIMER_H #include "qemu-common.h" -#include "qemu/main-loop.h" #include "qemu/notify.h" /* timers */
migration-exec.c +1 -0
@@ -17,6 +17,7 @@#include "qemu-common.h" #include "qemu/sockets.h" +#include "qemu/main-loop.h" #include "migration/migration.h" #include "migration/qemu-file.h" #include "block/block.h"
migration-fd.c +1 -0
@@ -14,6 +14,7 @@*/ #include "qemu-common.h" +#include "qemu/main-loop.h" #include "qemu/sockets.h" #include "migration/migration.h" #include "monitor/monitor.h"
migration-tcp.c +1 -0
@@ -18,6 +18,7 @@#include "migration/migration.h" #include "migration/qemu-file.h" #include "block/block.h" +#include "qemu/main-loop.h" //#define DEBUG_MIGRATION_TCP
migration-unix.c +1 -0
@@ -15,6 +15,7 @@#include "qemu-common.h" #include "qemu/sockets.h" +#include "qemu/main-loop.h" #include "migration/migration.h" #include "migration/qemu-file.h" #include "block/block.h"
migration.c +1 -0
@@ -14,6 +14,7 @@*/ #include "qemu-common.h" +#include "qemu/main-loop.h" #include "migration/migration.h" #include "monitor/monitor.h" #include "migration/qemu-file.h"
nbd.c +1 -0
@@ -38,6 +38,7 @@#include "qemu/sockets.h" #include "qemu/queue.h" +#include "qemu/main-loop.h" //#define DEBUG_NBD
net/net.c +1 -0
@@ -36,6 +36,7 @@#include "qmp-commands.h" #include "hw/qdev.h" #include "qemu/iov.h" +#include "qemu/main-loop.h" #include "qapi-visit.h" #include "qapi/opts-visitor.h" #include "qapi/dealloc-visitor.h"
net/socket.c +1 -0
@@ -31,6 +31,7 @@#include "qemu/option.h" #include "qemu/sockets.h" #include "qemu/iov.h" +#include "qemu/main-loop.h" typedef struct NetSocketState { NetClientState nc;
qemu-coroutine-io.c +1 -0
@@ -26,6 +26,7 @@#include "qemu/sockets.h" #include "block/coroutine.h" #include "qemu/iov.h" +#include "qemu/main-loop.h" ssize_t coroutine_fn qemu_co_sendv_recvv(int sockfd, struct iovec *iov, unsigned iov_cnt,
qemu-io-cmds.c +1 -0
@@ -10,6 +10,7 @@#include "qemu-io.h" #include "block/block_int.h" +#include "qemu/main-loop.h" #define CMD_NOFILE_OK 0x01
qemu-nbd.c +1 -0
@@ -19,6 +19,7 @@#include "qemu-common.h" #include "block/block.h" #include "block/nbd.h" +#include "qemu/main-loop.h" #include <stdarg.h> #include <stdio.h>
slirp/misc.c +1 -0
@@ -9,6 +9,7 @@#include <libslirp.h> #include "monitor/monitor.h" +#include "qemu/main-loop.h" #ifdef DEBUG int slirp_debug =DBG_CALL |DBG_MISC |DBG_ERROR ;
thread-pool.c +1 -0
@@ -23,6 +23,7 @@#include "block/block_int.h" #include "qemu/event_notifier.h" #include "block/thread-pool.h" +#include "qemu/main-loop.h" static void do_spawn_thread (ThreadPool *pool);
ui/vnc-auth-vencrypt.c +1 -1
@@ -25,7 +25,7 @@*/ #include "vnc.h" - +#include "qemu/main-loop.h" static void start_auth_vencrypt_subauth (VncState *vs) {
ui/vnc-ws.c +1 -0
@@ -19,6 +19,7 @@*/ #include "vnc.h" +#include "qemu/main-loop.h" #ifdef CONFIG_VNC_TLS #include "qemu/sockets.h"
Signature · 1 line
1.7.9.5
RFC PATCH v5 9/16 aio / timers: Add a notify callback to QEMUTimerList
In reply to Alex BlighAdd a notify pointer to QEMUTimerList so it knows what to notify on a timer change.
--- async.c | 7 ++++++- include/qemu/timer.h | 7 ++++++- qemu-timer.c | 24 ++++++++++++++++++++++-- 3 files changed, 34 insertions(+), 4 deletions(-)
async.c +6 -1
@@ -206,6 +206,11 @@ void aio_notify(AioContext *ctx)event_notifier_set (&ctx->notifier); } +static void aio_timerlist_notify (void *opaque) +{ +aio_notify ((AioContext *)opaque); +} +AioContext *aio_context_new (void ) {AioContext *ctx;
@@ -216,7 +221,7 @@ AioContext *aio_context_new(void)aio_set_event_notifier (ctx, &ctx->notifier, (EventNotifierHandler *) event_notifier_test_and_clear,NULL ); -timerlistgroup_init (ctx->tlg); +timerlistgroup_init (ctx->tlg, aio_timerlist_notify, ctx);return ctx; }
include/qemu/timer.h +6 -1
@@ -21,6 +21,7 @@ typedef struct QEMUClock QEMUClock;typedef struct QEMUTimerList QEMUTimerList ;typedef QEMUTimerList *QEMUTimerListGroup [QEMU_CLOCK_MAX ];typedef void QEMUTimerCB (void *opaque); +typedef void QEMUTimerListNotifyCB (void *opaque);extern QEMUTimerListGroup qemu_default_tlg;extern QEMUClock *QEMUClocks[QEMU_CLOCK_MAX ];
@@ -70,8 +71,12 @@ int64_t timerlist_deadline(QEMUTimerList *tl);int64_t timerlist_deadline_ns (QEMUTimerList *tl);QEMUClock *timerlist_get_clock (QEMUTimerList *tl);bool timerlist_run_timers (QEMUTimerList *tl); +void timerlist_set_notify_cb (QEMUTimerList *tl, +QEMUTimerListNotifyCB *cb,void *opaque); +void timerlist_notify (QEMUTimerList *tl); -void timerlistgroup_init (QEMUTimerListGroup tlg); +void timerlistgroup_init (QEMUTimerListGroup tlg, +QEMUTimerListNotifyCB *cb,void *opaque);void timerlistgroup_deinit (QEMUTimerListGroup tlg);bool timerlistgroup_run_timers (QEMUTimerListGroup tlg);int64_t timerlistgroup_deadline_ns (QEMUTimerListGroup tlg);
qemu-timer.c +22 -2
@@ -73,6 +73,8 @@ struct QEMUTimerList {QEMUClock *clock;QEMUTimer *active_timers;QLIST_ENTRY (QEMUTimerList) list; +QEMUTimerListNotifyCB *notify_cb; +void *notify_opaque; };struct QEMUTimer {
@@ -388,6 +390,22 @@ QEMUTimerList *qemu_clock_get_default_timerlist(QEMUClock *clock)return clock->default_timerlist; } +void timerlist_set_notify_cb (QEMUTimerList *tl, +QEMUTimerListNotifyCB *cb,void *opaque) +{ + tl->notify_cb = cb; + tl->notify_opaque = opaque; +} + +void timerlist_notify (QEMUTimerList *tl) +{ +if (tl->notify_cb) { + tl->notify_cb (tl->notify_opaque); + }else { +qemu_notify_event (); + } +} +/* Transition function to convert a nanosecond timeout to ms * This is used where a system does not support ppoll */
@@ -512,7 +530,7 @@ void qemu_mod_timer_ns(QEMUTimer *ts, int64_t expire_time)/* Interrupt execution to force deadline recalculation. */ qemu_clock_warp (ts->tl->clock);if (use_icount) { -qemu_notify_event (); +timerlist_notify (ts->tl); } } }
@@ -570,11 +588,13 @@ bool qemu_run_timers(QEMUClock *clock)return timerlist_run_timers (clock->default_timerlist); } -void timerlistgroup_init (QEMUTimerListGroup tlg) +void timerlistgroup_init (QEMUTimerListGroup tlg, +QEMUTimerListNotifyCB *cb,void *opaque) {QEMUClockType clock;for (clock =0 ; clock <QEMU_CLOCK_MAX ; clock++) { tlg[clock] =timerlist_new (clock); +timerlist_set_notify_cb (tlg[clock], cb, opaque); } }
Signature · 1 line
1.7.9.5
RFC PATCH v5 16/16 aio / timers: Add test harness for AioContext timers
In reply to Alex BlighAdd a test harness for AioContext timers. The g_source equivalent is unsatisfactory as it suffers from false wakeups.
--- tests/test-aio.c | 134 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 134 insertions(+)
tests/test-aio.c +134 -0
@@ -32,6 +32,15 @@ typedef struct {int max; } BHTestData; +typedef struct { +QEMUTimer *timer; +QEMUTimerList *tl; +int n; +int max; +int64_t ns; +AioContext *ctx; +}TimerTestData ; +static void bh_test_cb (void *opaque) {BHTestData *data = opaque;
@@ -40,6 +49,25 @@ static void bh_test_cb(void *opaque) } } +static void timer_test_cb (void *opaque) +{ +TimerTestData *data = opaque; +if (++data->n < data->max) { +qemu_mod_timer (data->timer, +qemu_get_clock_ns (timerlist_get_clock (data->tl)) + + data->ns); + } +} + +static void dummy_io_handler_read (void *opaque) +{ +} + +static int dummy_io_handler_flush (void *opaque) +{ +return 1 ; +} +static void bh_delete_cb (void *opaque) {BHTestData *data = opaque;
@@ -341,6 +369,63 @@ static void test_wait_event_notifier_noflush(void)event_notifier_cleanup (&data.e); } +static void test_timer_schedule (void ) +{ +TimerTestData data = { .n =0 , .ctx = ctx, .ns =SCALE_MS *750LL , + .max =2 , .tl = ctx->tlg[QEMU_CLOCK_VIRTUAL ] }; +int pipefd[2 ]; + +/* aio_poll will not block to wait for timers to complete unless it has +* an fd to wait on. Fixing this breaks other tests. So create a dummy one. +*/ +g_assert (!pipe2 (pipefd,O_NONBLOCK )); +aio_set_fd_handler (ctx, pipefd[0 ], + dummy_io_handler_read,NULL , dummy_io_handler_flush, +NULL ); +aio_poll (ctx, false); + + data.timer =timer_new (data.tl,SCALE_NS , timer_test_cb, &data); +qemu_mod_timer (data.timer, +qemu_get_clock_ns (timerlist_get_clock (data.tl)) + + data.ns); + + g_assert_cmpint(data.n, ==, 0); + +/* qemu_mod_timer may well cause an event notifer to have gone off, +* so clear that +*/ +do {}while (aio_poll (ctx, false)); + +g_assert (!aio_poll (ctx, false)); + g_assert_cmpint(data.n, ==, 0); + +sleep (1 ); + g_assert_cmpint(data.n, ==, 0); + +g_assert (aio_poll (ctx, false)); + g_assert_cmpint(data.n, ==, 1); + +/* qemu_mod_timer called by our callback */ +do {}while (aio_poll (ctx, false)); + +g_assert (!aio_poll (ctx, false)); + g_assert_cmpint(data.n, ==, 1); + +g_assert (aio_poll (ctx, true)); + g_assert_cmpint(data.n, ==, 2); + +/* As max is now 2, an event notifier should not have gone off */ + +g_assert (!aio_poll (ctx, false)); + g_assert_cmpint(data.n, ==, 2); + +aio_set_fd_handler (ctx, pipefd[0 ],NULL ,NULL ,NULL ,NULL ); +close (pipefd[0 ]); +close (pipefd[1 ]); + +qemu_del_timer (data.timer); +} +/* Now the same tests, using the context as a GSource. They are * very similar to the ones above, with g_main_context_iteration * replacing aio_poll. However:
@@ -623,6 +708,53 @@ static void test_source_wait_event_notifier_noflush(void)event_notifier_cleanup (&data.e); } +static void test_source_timer_schedule (void ) +{ +TimerTestData data = { .n =0 , .ctx = ctx, .ns =SCALE_MS *750LL , + .max =2 , .tl = ctx->tlg[QEMU_CLOCK_VIRTUAL ] }; +int pipefd[2 ]; +int64_t expiry; + +/* aio_poll will not block to wait for timers to complete unless it has +* an fd to wait on. Fixing this breaks other tests. So create a dummy one. +*/ +g_assert (!pipe2 (pipefd,O_NONBLOCK )); +aio_set_fd_handler (ctx, pipefd[0 ], + dummy_io_handler_read,NULL , dummy_io_handler_flush, +NULL ); +do {}while (g_main_context_iteration (NULL , false)); + + data.timer =timer_new (data.tl,SCALE_NS , timer_test_cb, &data); + expiry =qemu_get_clock_ns (timerlist_get_clock (data.tl)) + + data.ns; +qemu_mod_timer (data.timer, expiry); + + g_assert_cmpint(data.n, ==, 0); + +sleep (1 ); + g_assert_cmpint(data.n, ==, 0); + +g_assert (g_main_context_iteration (NULL , false)); + g_assert_cmpint(data.n, ==, 1); + +/* The comment above was not kidding when it said this wakes up itself */ +do { +g_assert (g_main_context_iteration (NULL , true)); + }while (qemu_get_clock_ns ( +timerlist_get_clock (data.tl)) <= expiry); +sleep (1 ); +g_main_context_iteration (NULL , false); + + g_assert_cmpint(data.n, ==, 2); + +aio_set_fd_handler (ctx, pipefd[0 ],NULL ,NULL ,NULL ,NULL ); +close (pipefd[0 ]); +close (pipefd[1 ]); + +qemu_del_timer (data.timer); +} + +/* End of tests. */ int main (int argc,char **argv)
@@ -651,6 +783,7 @@ int main(int argc, char **argv)g_test_add_func ("/aio/event/wait" , test_wait_event_notifier);g_test_add_func ("/aio/event/wait/no-flush-cb" , test_wait_event_notifier_noflush);g_test_add_func ("/aio/event/flush" , test_flush_event_notifier); +g_test_add_func ("/aio/timer/schedule" , test_timer_schedule);g_test_add_func ("/aio-gsource/notify" , test_source_notify);g_test_add_func ("/aio-gsource/flush" , test_source_flush);
@@ -665,5 +798,6 @@ int main(int argc, char **argv)g_test_add_func ("/aio-gsource/event/wait" , test_source_wait_event_notifier);g_test_add_func ("/aio-gsource/event/wait/no-flush-cb" , test_source_wait_event_notifier_noflush);g_test_add_func ("/aio-gsource/event/flush" , test_source_flush_event_notifier); +g_test_add_func ("/aio-gsource/timer/schedule" , test_source_timer_schedule);return g_test_run (); }
Signature · 1 line
1.7.9.5
RFC PATCH v5 15/16 aio / timers: Remove alarm timers
In reply to Alex BlighRemove alarm timers from qemu-timers.c now we use g_poll / ppoll instead.
--- include/qemu/timer.h | 2 - main-loop.c | 4 - qemu-timer.c | 500 +------------------------------------------------- vl.c | 4 +- 4 files changed, 4 insertions(+), 506 deletions(-)
include/qemu/timer.h +0 -2
@@ -142,9 +142,7 @@ static inline uint64_t timer_expire_time_ns(QEMUTimer *ts)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 );int64_t cpu_get_ticks (void );void cpu_enable_ticks (void );
main-loop.c +0 -4
@@ -131,10 +131,6 @@ int qemu_init_main_loop(void)GSource *src;init_clocks (); -if (init_timer_alarm () <0 ) { -fprintf (stderr,"could not initialize alarm timer\n" ); -exit (1 ); - } ret =qemu_signal_init ();if (ret) {
qemu-timer.c +1 -499
539 lines not on this page · raw message
vl.c +3 -1
@@ -3714,7 +3714,9 @@ int main(int argc, char **argv, char **envp) old_param =1 ;break ;case QEMU_OPTION_clock: -configure_alarms (optarg); +/* Clock options no longer exist. Keep this option for +* backward compatibility. +*/ break ;case QEMU_OPTION_startdate:configure_rtc_date_offset (optarg,1 );
Signature · 1 line
1.7.9.5
RFC PATCH v6 3/16 aio / timers: Add prctl(PR_SET_TIMERSLACK, 1, ...) to reduce timer slack
In reply to Alex BlighWhere supported, called prctl(PR_SET_TIMERSLACK, 1, ...) to set one nanosecond timer slack to increase precision of timer calls.
--- configure | 18 ++++++++++++++++++ qemu-timer.c | 7 +++++++ 2 files changed, 25 insertions(+)
configure +18 -0
@@ -2834,6 +2834,21 @@ if compile_prog "" "" ; then ppoll=yes fi +# check for prctl(PR_SET_TIMERSLACK , ... ) support +prctl_pr_set_timerslack=no +cat > $TMPC << EOF +#include <sys/prctl.h> + +int main(void) +{ + prctl(PR_SET_TIMERSLACK, 1, 0, 0, 0); + return 0; +} +EOF +if compile_prog "" "" ; then + prctl_pr_set_timerslack=yes +fi + # check for epoll support epoll=no cat > $TMPC << EOF
@@ -3828,6 +3843,9 @@ fi if test "$ppoll" = "yes" ; then echo "CONFIG_PPOLL=y" >> $config_host_mak fi +if test "$prctl_pr_set_timerslack" = "yes" ; then + echo "CONFIG_PRCTL_PR_SET_TIMERSLACK=y" >> $config_host_mak +fi if test "$epoll" = "yes" ; then echo "CONFIG_EPOLL=y" >> $config_host_mak fi
qemu-timer.c +7 -0
@@ -41,6 +41,10 @@#include <poll.h> #endif +#ifdef CONFIG_PRCTL_PR_SET_TIMERSLACK +#include <sys/prctl.h> +#endif +/***********************************************************/ /* timers */
@@ -512,6 +516,9 @@ void init_clocks(void) vm_clock =qemu_new_clock (QEMU_CLOCK_VIRTUAL ); host_clock =qemu_new_clock (QEMU_CLOCK_HOST ); } +#ifdef CONFIG_PRCTL_PR_SET_TIMERSLACK +prctl (PR_SET_TIMERSLACK ,1 ,0 ,0 ,0 ); +#endif }uint64_t qemu_timer_expire_time_ns (QEMUTimer *ts)
Signature · 1 line
1.7.9.5
RFC PATCH v6 4/16 aio / timers: Make qemu_run_timers and qemu_run_all_timers return progress
In reply to Alex BlighMake qemu_run_timers and qemu_run_all_timers return progress so that aio_poll etc. can determine whether a timer has been run.
--- include/qemu/timer.h | 4 ++-- qemu-timer.c | 18 ++++++++++++------ 2 files changed, 14 insertions(+), 8 deletions(-)
include/qemu/timer.h +2 -2
@@ -62,8 +62,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 );
qemu-timer.c +12 -6
@@ -451,13 +451,14 @@ 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; +bool progress = false;if (!clock->enabled) -return ; +return progress; current_time =qemu_get_clock_ns (clock);for (;;) {
@@ -471,7 +472,9 @@ void qemu_run_timers(QEMUClock *clock)/* run the callback (the timer list can be modified) */ ts->cb (ts->opaque); + progress = true; } +return progress; }int64_t qemu_get_clock_ns (QEMUClock *clock)
@@ -526,20 +529,23 @@ 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 progress = false; alarm_timer->pending = false;/* vm time timers */ -qemu_run_timers (vm_clock); -qemu_run_timers (rt_clock); -qemu_run_timers (host_clock); + progress |=qemu_run_timers (vm_clock); + progress |=qemu_run_timers (rt_clock); + progress |=qemu_run_timers (host_clock);/* rearm timer, if not periodic */ if (alarm_timer->expired) { alarm_timer->expired = false;qemu_rearm_alarm_timer (alarm_timer); } + +return progress; }#ifdef _WIN32
Signature · 1 line
1.7.9.5
v6 cover aio / timers: Add AioContext timers and use ppoll
In reply to Alex BlighThis patch series adds support for timers attached to an AioContext clock which get called within aio_poll.
In doing so it removes alarm timers and moves to use ppoll where possible.
This patch set 'sort of' passes make check (see below for caveat) including a new test harness for the aio timers, but has not been tested much beyond that. In particular, the win32 changes have not even been compile tested. Equally, alterations to use_icount are untested.
Caveat: I have had to alter tests/test-aio.c so the following error no longer occurs.
ERROR:tests/test-aio.c:346:test_wait_event_notifier_noflush: assertion failed: (aio_poll(ctx, false))
As gar as I can tell, this check was incorrect, in that it checking aio_poll makes progress when in fact it should not make progress. I fixed an issue where aio_poll was (as far as I can tell) wrongly returning true on a timeout, and that generated this error.
Note also the comment on patch 15 in relation to a possible bug in cpus.c.
Changes since v5:
* Rebase onto master (b9ac5d9)
* Fix spacing in typedef QEMUTimerList
* Rename 'QEMUClocks' extern to 'qemu_clocks'
Changes since v4:
* Rename qemu_timerlist_ functions to timer_list (per Paolo Bonzini)
* Rename qemu_timer_.*timerlist.* to timer_ (per Paolo Bonzini)
* Use enum for QEMUClockType
* Put clocks into an array; remove global variables
* Introduce QEMUTimerListGroup - a timeliest of each type
* Add a QEMUTimerListGroup to AioContext
* Use a callback on timer modification, rather than binding in AioContext into the timeliest
* Make cpus.c iterate over all timerlists when it does a notify
* Make cpus.c icount timeout use soonest timeout
across all timerlists
Changes since v3:
* Split up QEMUClock and QEMUClock list
* Improve commenting
* Fix comment in vl.c
* Change test/test-aio.c to reflect correct behaviour in aio_poll.
Changes since v2:
* Reordered to remove alarm timers last
* Added prctl(PR_SET_TIMERSLACK, 1, ...)
* Renamed qemu_g_poll_ns to qemu_poll_ns
* Moved declaration of above & drop glib types
* Do not use a global list of qemu clocks
* Add AioContext * to QEMUClock
* Split up conversion to use ppoll and timers
* Indentation fix
* Fix aio_win32.c aio_poll to return progress
* aio_notify / qemu_notify when timers are modified
* change comment in deprecation of clock options
Alex Bligh (16):
aio / timers: add qemu-timer.c utility functions
aio / timers: add ppoll support with qemu_poll_ns
aio / timers: Add prctl(PR_SET_TIMERSLACK, 1, ...) to reduce timer
slack
aio / timers: Make qemu_run_timers and qemu_run_all_timers return
progress
aio / timers: Split QEMUClock into QEMUClock and QEMUTimerList aio / timers: Untangle include files
aio / timers: Add QEMUTimerListGroup and helper functions aio / timers: Add QEMUTimerListGroup to AioContext
aio / timers: Add a notify callback to QEMUTimerList aio / timers: aio_ctx_prepare sets timeout from AioContext timers aio / timers: Convert aio_poll to use AioContext timers' deadline aio / timers: Convert mainloop to use timeout
aio / timers: On timer modification, qemu_notify or aio_notify aio / timers: Use all timerlists in icount warp calculations aio / timers: Remove alarm timers
aio / timers: Add test harness for AioContext timers
aio-posix.c | 20 +- aio-win32.c | 22 +- async.c | 20 +- configure | 37 +++ cpus.c | 44 ++- dma-helpers.c | 1 + hw/dma/xilinx_axidma.c | 1 + hw/timer/arm_timer.c | 1 + hw/timer/grlib_gptimer.c | 2 + hw/timer/imx_epit.c | 1 + hw/timer/imx_gpt.c | 1 + hw/timer/lm32_timer.c | 1 + hw/timer/puv3_ost.c | 1 + hw/timer/slavio_timer.c | 1 + hw/timer/xilinx_timer.c | 1 + hw/usb/hcd-uhci.c | 1 + include/block/aio.h | 4 + include/block/block_int.h | 1 + include/block/coroutine.h | 2 + include/qemu/timer.h | 122 ++++++- main-loop.c | 49 ++- migration-exec.c | 1 + migration-fd.c | 1 + migration-tcp.c | 1 + migration-unix.c | 1 + migration.c | 1 + nbd.c | 1 + net/net.c | 1 + net/socket.c | 1 + qemu-coroutine-io.c | 1 + qemu-io-cmds.c | 1 + qemu-nbd.c | 1 + qemu-timer.c | 803 ++++++++++++++++----------------------------- slirp/misc.c | 1 + tests/test-aio.c | 141 +++++++- tests/test-thread-pool.c | 3 + thread-pool.c | 1 + ui/vnc-auth-vencrypt.c | 2 +- ui/vnc-ws.c | 1 + vl.c | 4 +- 40 files changed, 736 insertions(+), 564 deletions(-)
Signature · 68 lines
1.7.9.5
*** BLRUB STOP ***
Alex Bligh (16):
aio / timers: add qemu-timer.c utility functions
aio / timers: add ppoll support with qemu_poll_ns
aio / timers: Add prctl(PR_SET_TIMERSLACK, 1, ...) to reduce timer
slack
aio / timers: Make qemu_run_timers and qemu_run_all_timers return
progress
aio / timers: Split QEMUClock into QEMUClock and QEMUTimerList
aio / timers: Untangle include files
aio / timers: Add QEMUTimerListGroup and helper functions
aio / timers: Add QEMUTimerListGroup to AioContext
aio / timers: Add a notify callback to QEMUTimerList
aio / timers: aio_ctx_prepare sets timeout from AioContext timers
aio / timers: Convert aio_poll to use AioContext timers' deadline
aio / timers: Convert mainloop to use timeout
aio / timers: On timer modification, qemu_notify or aio_notify
aio / timers: Use all timerlists in icount warp calculations
aio / timers: Remove alarm timers
aio / timers: Add test harness for AioContext timers
aio-posix.c | 20 +-
aio-win32.c | 22 +-
async.c | 20 +-
configure | 37 +++
cpus.c | 44 ++-
dma-helpers.c | 1 +
hw/dma/xilinx_axidma.c | 1 +
hw/timer/arm_timer.c | 1 +
hw/timer/grlib_gptimer.c | 2 +
hw/timer/imx_epit.c | 1 +
hw/timer/imx_gpt.c | 1 +
hw/timer/lm32_timer.c | 1 +
hw/timer/puv3_ost.c | 1 +
hw/timer/slavio_timer.c | 1 +
hw/timer/xilinx_timer.c | 1 +
hw/usb/hcd-uhci.c | 1 +
include/block/aio.h | 4 +
include/block/block_int.h | 1 +
include/block/coroutine.h | 2 +
include/qemu/timer.h | 122 ++++++-
main-loop.c | 49 ++-
migration-exec.c | 1 +
migration-fd.c | 1 +
migration-tcp.c | 1 +
migration-unix.c | 1 +
migration.c | 1 +
nbd.c | 1 +
net/net.c | 1 +
net/socket.c | 1 +
qemu-coroutine-io.c | 1 +
qemu-io-cmds.c | 1 +
qemu-nbd.c | 1 +
qemu-timer.c | 803 ++++++++++++++++-----------------------------
slirp/misc.c | 1 +
tests/test-aio.c | 141 +++++++-
tests/test-thread-pool.c | 3 +
thread-pool.c | 1 +
ui/vnc-auth-vencrypt.c | 2 +-
ui/vnc-ws.c | 1 +
vl.c | 4 +-
40 files changed, 736 insertions(+), 564 deletions(-)
--
1.7.9.5RFC PATCH v6 7/16 aio / timers: Add QEMUTimerListGroup and helper functions
Add QEMUTimerListGroup and helper functions, to represent a QEMUTimerList associated with each clock. Add a default QEMUTimerListGroup representing the default timer lists which are not associated with any other object (e.g. an AioContext as added by future patches).
--- include/qemu/timer.h | 7 +++++++ qemu-timer.c | 41 +++++++++++++++++++++++++++++++++++++++++ 2 files changed, 48 insertions(+)
include/qemu/timer.h +7 -0
@@ -19,8 +19,10 @@ typedef enum {typedef struct QEMUClock QEMUClock ;typedef struct QEMUTimerList QEMUTimerList ; +typedef QEMUTimerList *QEMUTimerListGroup [QEMU_CLOCK_MAX ];typedef void QEMUTimerCB (void *opaque); +extern QEMUTimerListGroup qemu_default_tlg;extern QEMUClock *qemu_clocks[QEMU_CLOCK_MAX ];static inline QEMUClock *qemu_get_clock (QEMUClockType type)
@@ -69,6 +71,11 @@ int64_t timerlist_deadline_ns(QEMUTimerList *tl);QEMUClock *timerlist_get_clock (QEMUTimerList *tl);bool timerlist_run_timers (QEMUTimerList *tl); +void timerlistgroup_init (QEMUTimerListGroup tlg); +void timerlistgroup_deinit (QEMUTimerListGroup tlg); +bool timerlistgroup_run_timers (QEMUTimerListGroup tlg); +int64_t timerlistgroup_deadline_ns (QEMUTimerListGroup tlg); +int qemu_timeout_ns_to_ms (int64_t ns);int qemu_poll_ns (GPollFD *fds,uint nfds,int64_t timeout);void qemu_clock_enable (QEMUClock *clock,bool enabled);
qemu-timer.c +41 -0
59 lines not on this page · raw message
Signature · 1 line
1.7.9.5
RFC PATCH v6 5/16 aio / timers: Split QEMUClock into QEMUClock and QEMUTimerList
In reply to Alex BlighSplit QEMUClock into QEMUClock and QEMUTimerList so that we can have more than one QEMUTimerList associated with the same clock.
Introduce a default_timerlist concept and make existing qemu_clock_* calls that actually should operate on a QEMUTimerList call the relevant QEMUTimerList implementations, using the clock's default timerlist. This vastly reduces the invasiveness of this change and means the API stays constant for existing users.
Introduce a list of QEMUTimerLists associated with each clock so that reenabling the clock can cause all the notifiers to be called. Note the code to do the notifications is added in a later patch.
Switch QEMUClockType to an enum. Remove global variables vm_clock, host_clock and rt_clock and add compatibility defines. Do not fix qemu_next_alarm_deadline as it's going to be deleted.
--- include/qemu/timer.h | 91 +++++++++++++++++++++++-- qemu-timer.c | 185 ++++++++++++++++++++++++++++++++++++++------------ 2 files changed, 224 insertions(+), 52 deletions(-)
include/qemu/timer.h +84 -7
@@ -11,38 +11,65 @@#define SCALE_US 1000#define SCALE_NS 1 -#define QEMU_CLOCK_REALTIME 0 -#define QEMU_CLOCK_VIRTUAL 1 -#define QEMU_CLOCK_HOST 2 +typedef enum { +QEMU_CLOCK_REALTIME =0 , +QEMU_CLOCK_VIRTUAL =1 , +QEMU_CLOCK_HOST =2 , +QEMU_CLOCK_MAX +}QEMUClockType ;typedef struct QEMUClock QEMUClock ; +typedef struct QEMUTimerList QEMUTimerList ;typedef void QEMUTimerCB (void *opaque); +extern QEMUClock *qemu_clocks[QEMU_CLOCK_MAX ]; + +static inline QEMUClock *qemu_get_clock (QEMUClockType type) +{ +return qemu_clocks[type]; +} + +/* These three clocks are maintained here with separate variable +names for compatibility only. +*/ +/* The real time clock should be used only for stuff which does not change the virtual machine state, as it is run even if the virtual machine is stopped. The real time clock has a frequency of 1000 Hz. */ -extern QEMUClock *rt_clock; +#define rt_clock (qemu_get_clock(QEMU_CLOCK_REALTIME))/* The virtual clock is only run during the emulation. It is stopped when the virtual machine is stopped. Virtual timers use a high precision clock, usually cpu cycles (use ticks_per_sec). */ -extern QEMUClock *vm_clock; +#define vm_clock (qemu_get_clock(QEMU_CLOCK_VIRTUAL))/* The host clock should be use for device models that emulate accurate real time sources. It will continue to run when the virtual machine is suspended, and it will reflect system time changes the host may undergo (e.g. due to NTP). The host clock has the same precision as the virtual clock. */ -extern QEMUClock *host_clock; +#define host_clock (qemu_get_clock(QEMU_CLOCK_HOST)) -QEMUClock *qemu_new_clock (int type); +QEMUClock *qemu_new_clock (QEMUClockType 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); +bool qemu_clock_use_for_deadline (QEMUClock *clock); +QEMUTimerList *qemu_clock_get_default_timerlist (QEMUClock *clock); + +QEMUTimerList *timerlist_new (QEMUClockType type); +void timerlist_free (QEMUTimerList *tl); +int64_t timerlist_has_timers (QEMUTimerList *tl); +int64_t timerlist_expired (QEMUTimerList *tl); +int64_t timerlist_deadline (QEMUTimerList *tl); +int64_t timerlist_deadline_ns (QEMUTimerList *tl); +QEMUClock *timerlist_get_clock (QEMUTimerList *tl); +bool timerlist_run_timers (QEMUTimerList *tl); +int qemu_timeout_ns_to_ms (int64_t ns);int qemu_poll_ns (GPollFD *fds,uint nfds,int64_t timeout);void qemu_clock_enable (QEMUClock *clock,bool enabled);
@@ -54,6 +81,8 @@ void qemu_unregister_clock_reset_notifier(QEMUClock *clock,QEMUTimer *qemu_new_timer (QEMUClock *clock,int scale,QEMUTimerCB *cb,void *opaque); +QEMUTimer *timer_new (QEMUTimerList *tl,int scale, +QEMUTimerCB *cb,void *opaque);void qemu_free_timer (QEMUTimer *ts);void qemu_del_timer (QEMUTimer *ts);void qemu_mod_timer_ns (QEMUTimer *ts,int64_t expire_time);
@@ -62,6 +91,42 @@ 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); +/* New format calling conventions for timers */ +static inline void timer_free (QEMUTimer *ts) +{ +qemu_free_timer (ts); +} + +static inline void timer_del (QEMUTimer *ts) +{ +qemu_del_timer (ts); +} + +static inline void timer_mod_ns (QEMUTimer *ts,int64_t expire_time) +{ +qemu_mod_timer_ns (ts, expire_time); +} + +static inline void timer_mod (QEMUTimer *ts,int64_t expire_timer) +{ +qemu_mod_timer (ts, expire_timer); +} + +static inline bool timer_pending (QEMUTimer *ts) +{ +return qemu_timer_pending (ts); +} + +static inline bool timer_expired (QEMUTimer *timer_head,int64_t current_time) +{ +return qemu_timer_expired (timer_head, current_time); +} + +static inline uint64_t timer_expire_time_ns (QEMUTimer *ts) +{ +return qemu_timer_expire_time_ns (ts); +} +bool qemu_run_timers (QEMUClock *clock);bool qemu_run_all_timers (void );void configure_alarms (char const *opt);
@@ -87,12 +152,24 @@ static inline QEMUTimer *qemu_new_timer_ns(QEMUClock *clock, QEMUTimerCB *cb,return qemu_new_timer (clock,SCALE_NS , cb, opaque); } +static inline QEMUTimer *timer_new_ns (QEMUTimerList *tl,QEMUTimerCB *cb, +void *opaque) +{ +return timer_new (tl,SCALE_NS , cb, opaque); +} +static inline QEMUTimer *qemu_new_timer_ms (QEMUClock *clock,QEMUTimerCB *cb,void *opaque) {return qemu_new_timer (clock,SCALE_MS , cb, opaque); } +static inline QEMUTimer *timer_new_ms (QEMUTimerList *tl,QEMUTimerCB *cb, +void *opaque) +{ +return timer_new (tl,SCALE_MS , cb, opaque); +} +static inline int64_t qemu_get_clock_ms (QEMUClock *clock) {return qemu_get_clock_ns (clock) /SCALE_MS ;
qemu-timer.c +140 -45
355 lines not on this page · raw message
Signature · 1 line
1.7.9.5
RFC PATCH v6 8/16 aio / timers: Add QEMUTimerListGroup to AioContext
In reply to Alex BlighAdd a QEMUTimerListGroup each AioContext (meaning a QEMUTimerList associated with each clock is added) and delete it when the AioContext is freed.
--- async.c | 2 ++ include/block/aio.h | 4 ++++ tests/test-aio.c | 3 +++ tests/test-thread-pool.c | 3 +++ 4 files changed, 12 insertions(+)
async.c +2 -0
@@ -205,6 +205,7 @@ aio_ctx_finalize(GSource *source)event_notifier_cleanup (&ctx->notifier);qemu_mutex_destroy (&ctx->bh_lock);g_array_free (ctx->pollfds, TRUE); +timerlistgroup_deinit (ctx->tlg); }static GSourceFuncs aio_source_funcs = {
@@ -244,6 +245,7 @@ AioContext *aio_context_new(void)aio_set_event_notifier (ctx, &ctx->notifier, (EventNotifierHandler *) event_notifier_test_and_clear,NULL ); +timerlistgroup_init (ctx->tlg);return ctx; }
include/block/aio.h +4 -0
@@ -18,6 +18,7 @@#include "qemu/queue.h" #include "qemu/event_notifier.h" #include "qemu/thread.h" +#include "qemu/timer.h" typedef struct BlockDriverAIOCB BlockDriverAIOCB ;typedef void BlockDriverCompletionFunc (void *opaque,int ret);
@@ -72,6 +73,9 @@ typedef struct AioContext {/* Thread pool for performing work and receiving completion callbacks */ struct ThreadPool *thread_pool; + +/* TimerLists for calling timers - one per clock type */ +QEMUTimerListGroup tlg; } AioContext;/* Returns 1 if there are still outstanding AIO requests; 0 otherwise */
tests/test-aio.c +3 -0
@@ -12,6 +12,7 @@#include <glib.h> #include "block/aio.h" +#include "qemu/timer.h" AioContext *ctx;
@@ -628,6 +629,8 @@ int main(int argc, char **argv) {GSource *src; +init_clocks (); + ctx =aio_context_new (); src =aio_get_g_source (ctx);g_source_attach (src,NULL );
tests/test-thread-pool.c +3 -0
@@ -3,6 +3,7 @@#include "block/aio.h" #include "block/thread-pool.h" #include "block/block.h" +#include "qemu/timer.h" static AioContext *ctx;static ThreadPool *pool;
@@ -205,6 +206,8 @@ int main(int argc, char **argv) {int ret; +init_clocks (); + ctx =aio_context_new (); pool =aio_get_thread_pool (ctx);
Signature · 1 line
1.7.9.5
RFC PATCH v6 6/16 aio / timers: Untangle include files
In reply to Alex Blighinclude/qemu/timer.h has no need to include main-loop.h and doing so causes an issue for the next patch. Unfortunately various files assume including timers.h will pull in main-loop.h. Untangle this mess.
--- dma-helpers.c | 1 + hw/dma/xilinx_axidma.c | 1 + hw/timer/arm_timer.c | 1 + hw/timer/grlib_gptimer.c | 2 ++ hw/timer/imx_epit.c | 1 + hw/timer/imx_gpt.c | 1 + hw/timer/lm32_timer.c | 1 + hw/timer/puv3_ost.c | 1 + hw/timer/slavio_timer.c | 1 + hw/timer/xilinx_timer.c | 1 + hw/usb/hcd-uhci.c | 1 + include/block/block_int.h | 1 + include/block/coroutine.h | 2 ++ include/qemu/timer.h | 1 - migration-exec.c | 1 + migration-fd.c | 1 + migration-tcp.c | 1 + migration-unix.c | 1 + migration.c | 1 + nbd.c | 1 + net/net.c | 1 + net/socket.c | 1 + qemu-coroutine-io.c | 1 + qemu-io-cmds.c | 1 + qemu-nbd.c | 1 + slirp/misc.c | 1 + thread-pool.c | 1 + ui/vnc-auth-vencrypt.c | 2 +- ui/vnc-ws.c | 1 + 29 files changed, 30 insertions(+), 2 deletions(-)
dma-helpers.c +1 -0
@@ -11,6 +11,7 @@#include "trace.h" #include "qemu/range.h" #include "qemu/thread.h" +#include "qemu/main-loop.h" /* #define DEBUG_IOMMU */
hw/dma/xilinx_axidma.c +1 -0
@@ -27,6 +27,7 @@#include "hw/ptimer.h" #include "qemu/log.h" #include "qapi/qmp/qerror.h" +#include "qemu/main-loop.h" #include "hw/stream.h"
hw/timer/arm_timer.c +1 -0
@@ -12,6 +12,7 @@#include "qemu-common.h" #include "hw/qdev.h" #include "hw/ptimer.h" +#include "qemu/main-loop.h" /* Common timer implementation. */
hw/timer/grlib_gptimer.c +2 -0
@@ -25,6 +25,8 @@#include "hw/sysbus.h" #include "qemu/timer.h" #include "hw/ptimer.h" +#include "qemu/timer.h" +#include "qemu/main-loop.h" #include "trace.h"
hw/timer/imx_epit.c +1 -0
@@ -18,6 +18,7 @@#include "hw/ptimer.h" #include "hw/sysbus.h" #include "hw/arm/imx.h" +#include "qemu/main-loop.h" #define TYPE_IMX_EPIT "imx.epit"
hw/timer/imx_gpt.c +1 -0
@@ -18,6 +18,7 @@#include "hw/ptimer.h" #include "hw/sysbus.h" #include "hw/arm/imx.h" +#include "qemu/main-loop.h" #define TYPE_IMX_GPT "imx.gpt"
hw/timer/lm32_timer.c +1 -0
@@ -27,6 +27,7 @@#include "qemu/timer.h" #include "hw/ptimer.h" #include "qemu/error-report.h" +#include "qemu/main-loop.h" #define DEFAULT_FREQUENCY (50*1000000)
hw/timer/puv3_ost.c +1 -0
@@ -10,6 +10,7 @@*/ #include "hw/sysbus.h" #include "hw/ptimer.h" +#include "qemu/main-loop.h" #undef DEBUG_PUV3#include "hw/unicore32/puv3.h"
hw/timer/slavio_timer.c +1 -0
@@ -27,6 +27,7 @@#include "hw/ptimer.h" #include "hw/sysbus.h" #include "trace.h" +#include "qemu/main-loop.h" /* * Registers of hardware timer in sun4m.
hw/timer/xilinx_timer.c +1 -0
@@ -25,6 +25,7 @@#include "hw/sysbus.h" #include "hw/ptimer.h" #include "qemu/log.h" +#include "qemu/main-loop.h" #define D (x)
hw/usb/hcd-uhci.c +1 -0
@@ -32,6 +32,7 @@#include "qemu/iov.h" #include "sysemu/dma.h" #include "trace.h" +#include "qemu/main-loop.h" //#define DEBUG //#define DEBUG_DUMP_DATA
include/block/block_int.h +1 -0
@@ -34,6 +34,7 @@#include "monitor/monitor.h" #include "qemu/hbitmap.h" #include "block/snapshot.h" +#include "qemu/main-loop.h" #define BLOCK_FLAG_ENCRYPT 1#define BLOCK_FLAG_COMPAT6 4
include/block/coroutine.h +2 -0
@@ -19,6 +19,8 @@#include "qemu/queue.h" #include "qemu/timer.h" +typedef struct AioContext AioContext ; +/** * Coroutines are a mechanism for stack switching and can be used for * cooperative userspace threading. These functions provide a simple but
include/qemu/timer.h +0 -1
@@ -2,7 +2,6 @@#define QEMU_TIMER_H #include "qemu-common.h" -#include "qemu/main-loop.h" #include "qemu/notify.h" /* timers */
migration-exec.c +1 -0
@@ -17,6 +17,7 @@#include "qemu-common.h" #include "qemu/sockets.h" +#include "qemu/main-loop.h" #include "migration/migration.h" #include "migration/qemu-file.h" #include "block/block.h"
migration-fd.c +1 -0
@@ -14,6 +14,7 @@*/ #include "qemu-common.h" +#include "qemu/main-loop.h" #include "qemu/sockets.h" #include "migration/migration.h" #include "monitor/monitor.h"
migration-tcp.c +1 -0
@@ -18,6 +18,7 @@#include "migration/migration.h" #include "migration/qemu-file.h" #include "block/block.h" +#include "qemu/main-loop.h" //#define DEBUG_MIGRATION_TCP
migration-unix.c +1 -0
@@ -15,6 +15,7 @@#include "qemu-common.h" #include "qemu/sockets.h" +#include "qemu/main-loop.h" #include "migration/migration.h" #include "migration/qemu-file.h" #include "block/block.h"
migration.c +1 -0
@@ -14,6 +14,7 @@*/ #include "qemu-common.h" +#include "qemu/main-loop.h" #include "migration/migration.h" #include "monitor/monitor.h" #include "migration/qemu-file.h"
nbd.c +1 -0
@@ -38,6 +38,7 @@#include "qemu/sockets.h" #include "qemu/queue.h" +#include "qemu/main-loop.h" //#define DEBUG_NBD
net/net.c +1 -0
@@ -36,6 +36,7 @@#include "qmp-commands.h" #include "hw/qdev.h" #include "qemu/iov.h" +#include "qemu/main-loop.h" #include "qapi-visit.h" #include "qapi/opts-visitor.h" #include "qapi/dealloc-visitor.h"
net/socket.c +1 -0
@@ -31,6 +31,7 @@#include "qemu/option.h" #include "qemu/sockets.h" #include "qemu/iov.h" +#include "qemu/main-loop.h" typedef struct NetSocketState { NetClientState nc;
qemu-coroutine-io.c +1 -0
@@ -26,6 +26,7 @@#include "qemu/sockets.h" #include "block/coroutine.h" #include "qemu/iov.h" +#include "qemu/main-loop.h" ssize_t coroutine_fn qemu_co_sendv_recvv(int sockfd, struct iovec *iov, unsigned iov_cnt,
qemu-io-cmds.c +1 -0
@@ -10,6 +10,7 @@#include "qemu-io.h" #include "block/block_int.h" +#include "qemu/main-loop.h" #define CMD_NOFILE_OK 0x01
qemu-nbd.c +1 -0
@@ -19,6 +19,7 @@#include "qemu-common.h" #include "block/block.h" #include "block/nbd.h" +#include "qemu/main-loop.h" #include <stdarg.h> #include <stdio.h>
slirp/misc.c +1 -0
@@ -9,6 +9,7 @@#include <libslirp.h> #include "monitor/monitor.h" +#include "qemu/main-loop.h" #ifdef DEBUG int slirp_debug =DBG_CALL |DBG_MISC |DBG_ERROR ;
thread-pool.c +1 -0
@@ -23,6 +23,7 @@#include "block/block_int.h" #include "qemu/event_notifier.h" #include "block/thread-pool.h" +#include "qemu/main-loop.h" static void do_spawn_thread (ThreadPool *pool);
ui/vnc-auth-vencrypt.c +1 -1
@@ -25,7 +25,7 @@*/ #include "vnc.h" - +#include "qemu/main-loop.h" static void start_auth_vencrypt_subauth (VncState *vs) {
ui/vnc-ws.c +1 -0
@@ -19,6 +19,7 @@*/ #include "vnc.h" +#include "qemu/main-loop.h" #ifdef CONFIG_VNC_TLS #include "qemu/sockets.h"
Signature · 1 line
1.7.9.5
RFC PATCH v6 10/16 aio / timers: aio_ctx_prepare sets timeout from AioContext timers
In reply to Alex BlighCalculate the timeout in aio_ctx_prepare taking into account the timers attached to the AioContext.
Alter aio_ctx_check similarly.
--- async.c | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-)
async.c +11 -2
@@ -150,13 +150,14 @@ aio_ctx_prepare(GSource *source, gint *timeout) {AioContext *ctx = (AioContext *) source;QEMUBH *bh; +int deadline;for (bh = ctx->first_bh; bh; bh = bh->next) {if (!bh->deleted && bh->scheduled) {if (bh->idle) {/* idle bottom halves will be polled at least * every 10ms */ - *timeout =10 ; + *timeout =qemu_soonest_timeout (*timeout,10 ); }else {/* non-idle bottom halves will be executed * immediately */
@@ -166,6 +167,14 @@ aio_ctx_prepare(GSource *source, gint *timeout) } } + deadline =qemu_timeout_ns_to_ms (timerlistgroup_deadline_ns (ctx->tlg)); +if (deadline ==0 ) { + *timeout =0 ; +return true; + }else { + *timeout =qemu_soonest_timeout (*timeout, deadline); + } +return false; }
@@ -180,7 +189,7 @@ aio_ctx_check(GSource *source)return true; } } -return aio_pending (ctx); +return aio_pending (ctx) || (timerlistgroup_deadline_ns (ctx->tlg) >=0 ); } static gboolean
Signature · 1 line
1.7.9.5
RFC PATCH v6 1/16 aio / timers: add qemu-timer.c utility functions
In reply to Alex BlighAdd qemu_free_clock and expose qemu_new_clock and clock types.
Add utility functions to qemu-timer.c for nanosecond timing.
Add qemu_clock_deadline_ns to calculate deadlines to nanosecond accuracy.
Add utility function qemu_soonest_timeout to calculate soonest deadline.
Add qemu_timeout_ns_to_ms to convert a timeout in nanoseconds back to milliseconds for when ppoll is not used.
--- include/qemu/timer.h | 17 ++++++++++++++ qemu-timer.c | 63 +++++++++++++++++++++++++++++++++++++++++++++----- 2 files changed, 74 insertions(+), 6 deletions(-)
include/qemu/timer.h +17 -0
@@ -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,14 @@ 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); +int qemu_timeout_ns_to_ms (int64_t ns);void qemu_clock_enable (QEMUClock *clock,bool enabled);void qemu_clock_warp (QEMUClock *clock);
@@ -63,6 +71,15 @@ int64_t cpu_get_ticks(void);void cpu_enable_ticks (void );void cpu_disable_ticks (void ); +static inline int64_t qemu_soonest_timeout (int64_t timeout1,int64_t timeout2) +{ +/* we can abuse the fact that -1 (which means infinite) is a maximal +* value when cast to unsigned. As this is disgusting, it's kept in +* one inline function. +*/ +return ((uint64_t ) timeout1 < (uint64_t ) timeout2) ? timeout1 : timeout2; +} +static inline QEMUTimer *qemu_new_timer_ns (QEMUClock *clock,QEMUTimerCB *cb,void *opaque) {
qemu-timer.c +57 -6
93 lines not on this page · raw message
Signature · 1 line
1.7.9.5
RFC PATCH v6 9/16 aio / timers: Add a notify callback to QEMUTimerList
In reply to Alex BlighAdd a notify pointer to QEMUTimerList so it knows what to notify on a timer change.
--- async.c | 7 ++++++- include/qemu/timer.h | 7 ++++++- qemu-timer.c | 24 ++++++++++++++++++++++-- 3 files changed, 34 insertions(+), 4 deletions(-)
async.c +6 -1
@@ -234,6 +234,11 @@ void aio_notify(AioContext *ctx)event_notifier_set (&ctx->notifier); } +static void aio_timerlist_notify (void *opaque) +{ +aio_notify ((AioContext *)opaque); +} +AioContext *aio_context_new (void ) {AioContext *ctx;
@@ -245,7 +250,7 @@ AioContext *aio_context_new(void)aio_set_event_notifier (ctx, &ctx->notifier, (EventNotifierHandler *) event_notifier_test_and_clear,NULL ); -timerlistgroup_init (ctx->tlg); +timerlistgroup_init (ctx->tlg, aio_timerlist_notify, ctx);return ctx; }
include/qemu/timer.h +6 -1
@@ -21,6 +21,7 @@ typedef struct QEMUClock QEMUClock;typedef struct QEMUTimerList QEMUTimerList ;typedef QEMUTimerList *QEMUTimerListGroup [QEMU_CLOCK_MAX ];typedef void QEMUTimerCB (void *opaque); +typedef void QEMUTimerListNotifyCB (void *opaque);extern QEMUTimerListGroup qemu_default_tlg;extern QEMUClock *qemu_clocks[QEMU_CLOCK_MAX ];
@@ -70,8 +71,12 @@ int64_t timerlist_deadline(QEMUTimerList *tl);int64_t timerlist_deadline_ns (QEMUTimerList *tl);QEMUClock *timerlist_get_clock (QEMUTimerList *tl);bool timerlist_run_timers (QEMUTimerList *tl); +void timerlist_set_notify_cb (QEMUTimerList *tl, +QEMUTimerListNotifyCB *cb,void *opaque); +void timerlist_notify (QEMUTimerList *tl); -void timerlistgroup_init (QEMUTimerListGroup tlg); +void timerlistgroup_init (QEMUTimerListGroup tlg, +QEMUTimerListNotifyCB *cb,void *opaque);void timerlistgroup_deinit (QEMUTimerListGroup tlg);bool timerlistgroup_run_timers (QEMUTimerListGroup tlg);int64_t timerlistgroup_deadline_ns (QEMUTimerListGroup tlg);
qemu-timer.c +22 -2
@@ -73,6 +73,8 @@ struct QEMUTimerList {QEMUClock *clock;QEMUTimer *active_timers;QLIST_ENTRY (QEMUTimerList) list; +QEMUTimerListNotifyCB *notify_cb; +void *notify_opaque; };struct QEMUTimer {
@@ -388,6 +390,22 @@ QEMUTimerList *qemu_clock_get_default_timerlist(QEMUClock *clock)return clock->default_timerlist; } +void timerlist_set_notify_cb (QEMUTimerList *tl, +QEMUTimerListNotifyCB *cb,void *opaque) +{ + tl->notify_cb = cb; + tl->notify_opaque = opaque; +} + +void timerlist_notify (QEMUTimerList *tl) +{ +if (tl->notify_cb) { + tl->notify_cb (tl->notify_opaque); + }else { +qemu_notify_event (); + } +} +/* Transition function to convert a nanosecond timeout to ms * This is used where a system does not support ppoll */
@@ -512,7 +530,7 @@ void qemu_mod_timer_ns(QEMUTimer *ts, int64_t expire_time)/* Interrupt execution to force deadline recalculation. */ qemu_clock_warp (ts->tl->clock);if (use_icount) { -qemu_notify_event (); +timerlist_notify (ts->tl); } } }
@@ -570,11 +588,13 @@ bool qemu_run_timers(QEMUClock *clock)return timerlist_run_timers (clock->default_timerlist); } -void timerlistgroup_init (QEMUTimerListGroup tlg) +void timerlistgroup_init (QEMUTimerListGroup tlg, +QEMUTimerListNotifyCB *cb,void *opaque) {QEMUClockType clock;for (clock =0 ; clock <QEMU_CLOCK_MAX ; clock++) { tlg[clock] =timerlist_new (clock); +timerlist_set_notify_cb (tlg[clock], cb, opaque); } }
Signature · 1 line
1.7.9.5
RFC PATCH v6 11/16 aio / timers: Convert aio_poll to use AioContext timers' deadline
In reply to Alex BlighConvert aio_poll to use deadline based on AioContext's timers.
aio_poll has been changed to return accurately whether progress has occurred. Prior to this commit, aio_poll always returned true if g_poll was entered, whether or not any progress was made. This required a change to tests/test-aio.c where an assert was backwards.
--- aio-posix.c | 20 +++++++++++++------- aio-win32.c | 22 +++++++++++++++++++--- tests/test-aio.c | 4 ++-- 3 files changed, 34 insertions(+), 12 deletions(-)
aio-posix.c +13 -7
@@ -166,6 +166,10 @@ static bool aio_dispatch(AioContext *ctx)g_free (tmp); } } + +/* Run our timers */ + progress |=timerlistgroup_run_timers (ctx->tlg); +return progress; }
@@ -232,9 +236,9 @@ bool aio_poll(AioContext *ctx, bool blocking) }/* wait until next event */ - ret =g_poll ((GPollFD *)ctx->pollfds->data, - ctx->pollfds->len, - blocking ?-1 :0 ); + ret =qemu_poll_ns ((GPollFD *)ctx->pollfds->data, + ctx->pollfds->len, + blocking ?timerlistgroup_deadline_ns (ctx->tlg) :0 );/* if we have any readable fds, dispatch event */ if (ret >0 ) {
@@ -245,11 +249,13 @@ bool aio_poll(AioContext *ctx, bool blocking) node->pfd.revents = pfd->revents; } } -if (aio_dispatch (ctx)) { - progress = true; - } + } + +/* Run dispatch even if there were no readable fds to run timers */ +if (aio_dispatch (ctx)) { + progress = true; }assert (progress || busy); -return true; +return progress; }
aio-win32.c +19 -3
@@ -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 |=timerlistgroup_run_timers (ctx->tlg); +/* * 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 (timerlistgroup_deadline_ns (ctx->tlg)) :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 |=timerlistgroup_run_timers (ctx->tl); + } +assert (progress || busy); -return true; +return progress; }
tests/test-aio.c +2 -2
@@ -316,13 +316,13 @@ static void test_wait_event_notifier_noflush(void)event_notifier_set (&data.e);g_assert (aio_poll (ctx, false)); g_assert_cmpint(data.n, ==, 1); -g_assert (aio_poll (ctx, false)); +g_assert (!aio_poll (ctx, false)); g_assert_cmpint(data.n, ==, 1);event_notifier_set (&data.e);g_assert (aio_poll (ctx, false)); g_assert_cmpint(data.n, ==, 2); -g_assert (aio_poll (ctx, false)); +g_assert (!aio_poll (ctx, false)); g_assert_cmpint(data.n, ==, 2);event_notifier_set (&dummy.e);
Signature · 1 line
1.7.9.5
RFC PATCH v6 12/16 aio / timers: Convert mainloop to use timeout
In reply to Alex BlighConvert mainloop to use timeout from default timerlist group (i.e. the current 3 static timers)
--- main-loop.c | 45 ++++++++++++++++++++++++++++++++++----------- 1 file changed, 34 insertions(+), 11 deletions(-)
main-loop.c +34 -11
@@ -155,10 +155,11 @@ static int max_priority;static int glib_pollfds_idx;static int glib_n_poll_fds; -static void glib_pollfds_fill (uint32_t *cur_timeout) +static void glib_pollfds_fill (int64_t *cur_timeout) {GMainContext *context =g_main_context_default ();int timeout =0 ; +int64_t timeout_ns;int n;g_main_context_prepare (context, &max_priority);
@@ -174,9 +175,13 @@ static void glib_pollfds_fill(uint32_t *cur_timeout) glib_n_poll_fds); }while (n != glib_n_poll_fds); -if (timeout >=0 && timeout < *cur_timeout) { - *cur_timeout = timeout; +if (timeout <0 ) { + timeout_ns =-1 ; + }else { + timeout_ns = (int64_t )timeout * (int64_t )SCALE_MS ; } + + *cur_timeout =qemu_soonest_timeout (timeout_ns, *cur_timeout); }static void glib_pollfds_poll (void )
@@ -191,7 +196,7 @@ static void glib_pollfds_poll(void)#define MAX_MAIN_LOOP_SPIN (1000) -static int os_host_main_loop_wait (uint32_t timeout) +static int os_host_main_loop_wait (int64_t timeout) {int ret;static int spin_counter;
@@ -214,7 +219,7 @@ static int os_host_main_loop_wait(uint32_t timeout) notified = true; } - timeout =1 ; + timeout =SCALE_MS ; }if (timeout >0 ) {
@@ -224,7 +229,7 @@ static int os_host_main_loop_wait(uint32_t timeout) spin_counter++; } - ret =g_poll ((GPollFD *)gpollfds->data, gpollfds->len, timeout); + ret =qemu_poll_ns ((GPollFD *)gpollfds->data, gpollfds->len, timeout);if (timeout >0 ) {qemu_mutex_lock_iothread ();
@@ -373,7 +378,7 @@ static void pollfds_poll(GArray *pollfds, int nfds, fd_set *rfds, } } -static int os_host_main_loop_wait (uint32_t timeout) +static int os_host_main_loop_wait (int64_t timeout) {GMainContext *context =g_main_context_default ();GPollFD poll_fds[1024 *2 ];/* this is probably overkill */
@@ -382,6 +387,7 @@ static int os_host_main_loop_wait(uint32_t timeout)PollingEntry *pe;WaitObjects *w = &wait_objects;gint poll_timeout; +int64_t poll_timeout_ns;static struct timeval tv0;fd_set rfds, wfds, xfds;int nfds;
@@ -419,12 +425,17 @@ static int os_host_main_loop_wait(uint32_t timeout) poll_fds[n_poll_fds + i].events =G_IO_IN ; } -if (poll_timeout <0 || timeout < poll_timeout) { - poll_timeout = timeout; +if (poll_timeout <0 ) { + poll_timeout_ns =-1 ; + }else { + poll_timeout_ns = (int64_t )poll_timeout * (int64_t )SCALE_MS ; } + poll_timeout_ns =qemu_soonest_timeout (poll_timeout_ns, timeout); +qemu_mutex_unlock_iothread (); - g_poll_ret =g_poll (poll_fds, n_poll_fds + w->num, poll_timeout); + g_poll_ret =qemu_poll_ns (poll_fds, n_poll_fds + w->num, poll_timeout_ns); +qemu_mutex_lock_iothread ();if (g_poll_ret >0 ) {for (i =0 ; i < w->num; i++) {
@@ -449,6 +460,7 @@ int main_loop_wait(int nonblocking) {int ret;uint32_t timeout =UINT32_MAX ; +int64_t timeout_ns;if (nonblocking) { timeout =0 ;
@@ -462,7 +474,18 @@ int main_loop_wait(int nonblocking)slirp_pollfds_fill (gpollfds);#endif qemu_iohandler_fill (gpollfds); - ret =os_host_main_loop_wait (timeout); + +if (timeout ==UINT32_MAX ) { + timeout_ns =-1 ; + }else { + timeout_ns = (uint64_t )timeout * (int64_t )(SCALE_MS ); + } + + timeout_ns =qemu_soonest_timeout (timeout_ns, +timerlistgroup_deadline_ns ( + qemu_default_tlg)); + + ret =os_host_main_loop_wait (timeout_ns);qemu_iohandler_poll (gpollfds, ret);#ifdef CONFIG_SLIRP slirp_pollfds_poll (gpollfds, (ret <0 ));
Signature · 1 line
1.7.9.5
RFC PATCH v6 13/16 aio / timers: On timer modification, qemu_notify or aio_notify
In reply to Alex BlighOn qemu_mod_timer_ns, ensure qemu_notify or aio_notify is called to end the appropriate poll(), irrespective of use_icount value.
On qemu_clock_enable, ensure qemu_notify or aio_notify is called for all QEMUTimerLists attached to the QEMUClock.
--- include/qemu/timer.h | 1 + qemu-timer.c | 13 ++++++++++--- 2 files changed, 11 insertions(+), 3 deletions(-)
include/qemu/timer.h +1 -0
@@ -62,6 +62,7 @@ int64_t qemu_clock_deadline(QEMUClock *clock);int64_t qemu_clock_deadline_ns (QEMUClock *clock);bool qemu_clock_use_for_deadline (QEMUClock *clock);QEMUTimerList *qemu_clock_get_default_timerlist (QEMUClock *clock); +void qemu_clock_notify (QEMUClock *clock);QEMUTimerList *timerlist_new (QEMUClockType type);void timerlist_free (QEMUTimerList *tl);
qemu-timer.c +10 -3
@@ -304,11 +304,20 @@ bool qemu_clock_use_for_deadline(QEMUClock *clock)return !(use_icount && (clock->type =QEMU_CLOCK_VIRTUAL )); } +void qemu_clock_notify (QEMUClock *clock) +{ +QEMUTimerList *tl; + QLIST_FOREACH(tl, &clock->timerlists, list) { +timerlist_notify (tl); + } +} +void qemu_clock_enable (QEMUClock *clock,bool enabled) {bool old = clock->enabled; clock->enabled = enabled;if (enabled && !old) { +qemu_clock_notify (clock);qemu_rearm_alarm_timer (alarm_timer); } }
@@ -529,9 +538,7 @@ void qemu_mod_timer_ns(QEMUTimer *ts, int64_t expire_time) }/* Interrupt execution to force deadline recalculation. */ qemu_clock_warp (ts->tl->clock); -if (use_icount) { -timerlist_notify (ts->tl); - } +timerlist_notify (ts->tl); } }
Signature · 1 line
1.7.9.5
RFC PATCH v6 14/16 aio / timers: Use all timerlists in icount warp calculations
In reply to Alex BlighNotify all timerlists derived from vm_clock in icount warp calculations.
When calculating timer delay based on vm_clock deadline, use all timerlists.
For compatibility, maintain an apparent bug where when using icount, if no vm_clock timer was set, qemu_clock_deadline would return INT32_MAX and always set an icount clock expiry about 2 seconds ahead.
NB: thread safety - when different timerlists sit on different threads, this will need some locking.
--- cpus.c | 44 ++++++++++++++++++++++++++++++++++++-------- include/qemu/timer.h | 1 + qemu-timer.c | 16 ++++++++++++++++ 3 files changed, 53 insertions(+), 8 deletions(-)
cpus.c +36 -8
@@ -262,7 +262,7 @@ static void icount_warp_rt(void *opaque) qemu_icount_bias +=MIN (warp_delta, delta); }if (qemu_clock_expired (vm_clock)) { -qemu_notify_event (); +qemu_clock_notify (vm_clock); } } vm_clock_warp_start =-1 ;
@@ -279,7 +279,7 @@ void qtest_clock_warp(int64_t dest)qemu_run_timers (vm_clock); clock =qemu_get_clock_ns (vm_clock); } -qemu_notify_event (); +qemu_clock_notify (vm_clock); }void qemu_clock_warp (QEMUClock *clock)
@@ -314,7 +314,18 @@ void qemu_clock_warp(QEMUClock *clock) } vm_clock_warp_start =qemu_get_clock_ns (rt_clock); - deadline =qemu_clock_deadline (vm_clock); +/* We want to use the earliest deadline from ALL vm_clocks */ + deadline =qemu_clock_deadline_ns_all (vm_clock); + +/* Maintain prior (possibly buggy) behaviour where if no deadline +* was set (as there is no vm_clock timer) or it is more than +* INT32_MAX nanoseconds ahead, we still use INT32_MAX +* nanoseconds. +*/ +if ((deadline <0 ) || (deadline >INT32_MAX )) { + deadline =INT32_MAX ; + } +if (deadline >0 ) {/* * Ensure the vm_clock proceeds even when the virtual CPU goes to
@@ -333,8 +344,8 @@ void qemu_clock_warp(QEMUClock *clock)* packets continuously instead of every 100ms. */ qemu_mod_timer (icount_warp_timer, vm_clock_warp_start + deadline); - }else { -qemu_notify_event (); + }else if (deadline ==0 ) { +qemu_clock_notify (vm_clock); } }
@@ -866,8 +877,13 @@ static void *qemu_tcg_cpu_thread_fn(void *arg)while (1 ) {tcg_exec_all (); -if (use_icount &&qemu_clock_deadline (vm_clock) <=0 ) { -qemu_notify_event (); + +if (use_icount) { +int64_t deadline =qemu_clock_deadline_ns_all (vm_clock); + +if (deadline ==0 ) { +qemu_clock_notify (vm_clock); + } }qemu_tcg_wait_io_event (); }
@@ -1145,11 +1161,23 @@ static int tcg_cpu_exec(CPUArchState *env)#endif if (use_icount) {int64_t count; +int64_t deadline;int decr; qemu_icount -= (env->icount_decr.u16.low + env->icount_extra); env->icount_decr.u16.low =0 ; env->icount_extra =0 ; - count =qemu_icount_round (qemu_clock_deadline (vm_clock)); + deadline =qemu_clock_deadline_ns_all (vm_clock); + +/* Maintain prior (possibly buggy) behaviour where if no deadline +* was set (as there is no vm_clock timer) or it is more than +* INT32_MAX nanoseconds ahead, we still use INT32_MAX +* nanoseconds. +*/ +if ((deadline <0 ) || (deadline >INT32_MAX )) { + deadline =INT32_MAX ; + } + + count =qemu_icount_round (deadline); qemu_icount += count; decr = (count >0xffff ) ?0xffff : count; count -= decr;
include/qemu/timer.h +1 -0
@@ -60,6 +60,7 @@ 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_ns_all (QEMUClock *clock);bool qemu_clock_use_for_deadline (QEMUClock *clock);QEMUTimerList *qemu_clock_get_default_timerlist (QEMUClock *clock);void qemu_clock_notify (QEMUClock *clock);
qemu-timer.c +16 -0
@@ -389,6 +389,22 @@ int64_t qemu_clock_deadline_ns(QEMUClock *clock)return timerlist_deadline_ns (clock->default_timerlist); } +/* Calculate the soonest deadline across all timerlists attached +* to the clock. This is used for the icount timeout so we +* ignore whether or not the clock should be used in deadline +* calculations. +*/ +int64_t qemu_clock_deadline_ns_all (QEMUClock *clock) +{ +int64_t deadline =-1 ; +QEMUTimerList *tl; + QLIST_FOREACH(tl, &clock->timerlists, list) { + deadline =qemu_soonest_timeout (deadline, +timerlist_deadline_ns (tl)); + } +return deadline; +} +QEMUClock *timerlist_get_clock (QEMUTimerList *tl) {return tl->clock;
Signature · 1 line
1.7.9.5
RFC PATCH v6 2/16 aio / timers: add ppoll support with qemu_poll_ns
In reply to Alex BlighAdd qemu_poll_ns which works like g_poll but takes a nanosecond timeout.
--- configure | 19 +++++++++++++++++++ include/qemu/timer.h | 1 + qemu-timer.c | 24 ++++++++++++++++++++++++ 3 files changed, 44 insertions(+)
configure +19 -0
@@ -2818,6 +2818,22 @@ if compile_prog "" "" ; then dup3=yes fi +# check for ppoll support +ppoll=no +cat > $TMPC << EOF +#include <poll.h> + +int main(void) +{ + struct pollfd pfd = { .fd = 0, .events = 0, .revents = 0 }; + ppoll(&pfd, 1, 0, 0); + return 0; +} +EOF +if compile_prog "" "" ; then + ppoll=yes +fi + # check for epoll support epoll=no cat > $TMPC << EOF
@@ -3809,6 +3825,9 @@ fi if test "$dup3" = "yes" ; then echo "CONFIG_DUP3=y" >> $config_host_mak fi +if test "$ppoll" = "yes" ; then + echo "CONFIG_PPOLL=y" >> $config_host_mak +fi if test "$epoll" = "yes" ; then echo "CONFIG_EPOLL=y" >> $config_host_mak fi
include/qemu/timer.h +1 -0
@@ -44,6 +44,7 @@ int64_t qemu_clock_expired(QEMUClock *clock);int64_t qemu_clock_deadline (QEMUClock *clock);int64_t qemu_clock_deadline_ns (QEMUClock *clock);int qemu_timeout_ns_to_ms (int64_t ns); +int qemu_poll_ns (GPollFD *fds,uint nfds,int64_t timeout);void qemu_clock_enable (QEMUClock *clock,bool enabled);void qemu_clock_warp (QEMUClock *clock);
qemu-timer.c +24 -0
@@ -37,6 +37,10 @@#include <mmsystem.h> #endif +#ifdef CONFIG_PPOLL +#include <poll.h> +#endif +/***********************************************************/ /* timers */
@@ -328,6 +332,26 @@ int qemu_timeout_ns_to_ms(int64_t ns) } +/* qemu implementation of g_poll which uses a nanosecond timeout but is +* otherwise identical to g_poll +*/ +int qemu_poll_ns (GPollFD *fds,uint nfds,int64_t timeout) +{ +#ifdef CONFIG_PPOLL +if (timeout <0 ) { +return ppoll ((struct pollfd *)fds, nfds,NULL ,NULL ); + }else { +struct timespec ts; + ts.tv_sec = timeout /1000000000LL ; + ts.tv_nsec = timeout %1000000000LL ; +return ppoll ((struct pollfd *)fds, nfds, &ts,NULL ); + } +#else +return g_poll (fds, nfds,qemu_timeout_ns_to_ms (timeout)); +#endif +} + +QEMUTimer *qemu_new_timer (QEMUClock *clock,int scale,QEMUTimerCB *cb,void *opaque) {
Signature · 1 line
1.7.9.5
RFC PATCH v6 16/16 aio / timers: Add test harness for AioContext timers
In reply to Alex BlighAdd a test harness for AioContext timers. The g_source equivalent is unsatisfactory as it suffers from false wakeups.
--- tests/test-aio.c | 134 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 134 insertions(+)
tests/test-aio.c +134 -0
@@ -32,6 +32,15 @@ typedef struct {int max; } BHTestData; +typedef struct { +QEMUTimer *timer; +QEMUTimerList *tl; +int n; +int max; +int64_t ns; +AioContext *ctx; +}TimerTestData ; +static void bh_test_cb (void *opaque) {BHTestData *data = opaque;
@@ -40,6 +49,25 @@ static void bh_test_cb(void *opaque) } } +static void timer_test_cb (void *opaque) +{ +TimerTestData *data = opaque; +if (++data->n < data->max) { +qemu_mod_timer (data->timer, +qemu_get_clock_ns (timerlist_get_clock (data->tl)) + + data->ns); + } +} + +static void dummy_io_handler_read (void *opaque) +{ +} + +static int dummy_io_handler_flush (void *opaque) +{ +return 1 ; +} +static void bh_delete_cb (void *opaque) {BHTestData *data = opaque;
@@ -341,6 +369,63 @@ static void test_wait_event_notifier_noflush(void)event_notifier_cleanup (&data.e); } +static void test_timer_schedule (void ) +{ +TimerTestData data = { .n =0 , .ctx = ctx, .ns =SCALE_MS *750LL , + .max =2 , .tl = ctx->tlg[QEMU_CLOCK_VIRTUAL ] }; +int pipefd[2 ]; + +/* aio_poll will not block to wait for timers to complete unless it has +* an fd to wait on. Fixing this breaks other tests. So create a dummy one. +*/ +g_assert (!pipe2 (pipefd,O_NONBLOCK )); +aio_set_fd_handler (ctx, pipefd[0 ], + dummy_io_handler_read,NULL , dummy_io_handler_flush, +NULL ); +aio_poll (ctx, false); + + data.timer =timer_new (data.tl,SCALE_NS , timer_test_cb, &data); +qemu_mod_timer (data.timer, +qemu_get_clock_ns (timerlist_get_clock (data.tl)) + + data.ns); + + g_assert_cmpint(data.n, ==, 0); + +/* qemu_mod_timer may well cause an event notifer to have gone off, +* so clear that +*/ +do {}while (aio_poll (ctx, false)); + +g_assert (!aio_poll (ctx, false)); + g_assert_cmpint(data.n, ==, 0); + +sleep (1 ); + g_assert_cmpint(data.n, ==, 0); + +g_assert (aio_poll (ctx, false)); + g_assert_cmpint(data.n, ==, 1); + +/* qemu_mod_timer called by our callback */ +do {}while (aio_poll (ctx, false)); + +g_assert (!aio_poll (ctx, false)); + g_assert_cmpint(data.n, ==, 1); + +g_assert (aio_poll (ctx, true)); + g_assert_cmpint(data.n, ==, 2); + +/* As max is now 2, an event notifier should not have gone off */ + +g_assert (!aio_poll (ctx, false)); + g_assert_cmpint(data.n, ==, 2); + +aio_set_fd_handler (ctx, pipefd[0 ],NULL ,NULL ,NULL ,NULL ); +close (pipefd[0 ]); +close (pipefd[1 ]); + +qemu_del_timer (data.timer); +} +/* Now the same tests, using the context as a GSource. They are * very similar to the ones above, with g_main_context_iteration * replacing aio_poll. However:
@@ -623,6 +708,53 @@ static void test_source_wait_event_notifier_noflush(void)event_notifier_cleanup (&data.e); } +static void test_source_timer_schedule (void ) +{ +TimerTestData data = { .n =0 , .ctx = ctx, .ns =SCALE_MS *750LL , + .max =2 , .tl = ctx->tlg[QEMU_CLOCK_VIRTUAL ] }; +int pipefd[2 ]; +int64_t expiry; + +/* aio_poll will not block to wait for timers to complete unless it has +* an fd to wait on. Fixing this breaks other tests. So create a dummy one. +*/ +g_assert (!pipe2 (pipefd,O_NONBLOCK )); +aio_set_fd_handler (ctx, pipefd[0 ], + dummy_io_handler_read,NULL , dummy_io_handler_flush, +NULL ); +do {}while (g_main_context_iteration (NULL , false)); + + data.timer =timer_new (data.tl,SCALE_NS , timer_test_cb, &data); + expiry =qemu_get_clock_ns (timerlist_get_clock (data.tl)) + + data.ns; +qemu_mod_timer (data.timer, expiry); + + g_assert_cmpint(data.n, ==, 0); + +sleep (1 ); + g_assert_cmpint(data.n, ==, 0); + +g_assert (g_main_context_iteration (NULL , false)); + g_assert_cmpint(data.n, ==, 1); + +/* The comment above was not kidding when it said this wakes up itself */ +do { +g_assert (g_main_context_iteration (NULL , true)); + }while (qemu_get_clock_ns ( +timerlist_get_clock (data.tl)) <= expiry); +sleep (1 ); +g_main_context_iteration (NULL , false); + + g_assert_cmpint(data.n, ==, 2); + +aio_set_fd_handler (ctx, pipefd[0 ],NULL ,NULL ,NULL ,NULL ); +close (pipefd[0 ]); +close (pipefd[1 ]); + +qemu_del_timer (data.timer); +} + +/* End of tests. */ int main (int argc,char **argv)
@@ -651,6 +783,7 @@ int main(int argc, char **argv)g_test_add_func ("/aio/event/wait" , test_wait_event_notifier);g_test_add_func ("/aio/event/wait/no-flush-cb" , test_wait_event_notifier_noflush);g_test_add_func ("/aio/event/flush" , test_flush_event_notifier); +g_test_add_func ("/aio/timer/schedule" , test_timer_schedule);g_test_add_func ("/aio-gsource/notify" , test_source_notify);g_test_add_func ("/aio-gsource/flush" , test_source_flush);
@@ -665,5 +798,6 @@ int main(int argc, char **argv)g_test_add_func ("/aio-gsource/event/wait" , test_source_wait_event_notifier);g_test_add_func ("/aio-gsource/event/wait/no-flush-cb" , test_source_wait_event_notifier_noflush);g_test_add_func ("/aio-gsource/event/flush" , test_source_flush_event_notifier); +g_test_add_func ("/aio-gsource/timer/schedule" , test_source_timer_schedule);return g_test_run (); }
Signature · 1 line
1.7.9.5
RFC PATCH v6 15/16 aio / timers: Remove alarm timers
In reply to Alex BlighRemove alarm timers from qemu-timers.c now we use g_poll / ppoll instead.
--- include/qemu/timer.h | 2 - main-loop.c | 4 - qemu-timer.c | 500 +------------------------------------------------- vl.c | 4 +- 4 files changed, 4 insertions(+), 506 deletions(-)
include/qemu/timer.h +0 -2
@@ -142,9 +142,7 @@ static inline uint64_t timer_expire_time_ns(QEMUTimer *ts)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 );int64_t cpu_get_ticks (void );void cpu_enable_ticks (void );
main-loop.c +0 -4
@@ -131,10 +131,6 @@ int qemu_init_main_loop(void)GSource *src;init_clocks (); -if (init_timer_alarm () <0 ) { -fprintf (stderr,"could not initialize alarm timer\n" ); -exit (1 ); - } ret =qemu_signal_init ();if (ret) {
qemu-timer.c +1 -499
539 lines not on this page · raw message
vl.c +3 -1
@@ -3714,7 +3714,9 @@ int main(int argc, char **argv, char **envp) old_param =1 ;break ;case QEMU_OPTION_clock: -configure_alarms (optarg); +/* Clock options no longer exist. Keep this option for +* backward compatibility. +*/ break ;case QEMU_OPTION_startdate:configure_rtc_date_offset (optarg,1 );
Signature · 1 line
1.7.9.5
v6 cover aio / timers: Add AioContext timers and use ppoll
In reply to Alex Bligh[ I managed to screw up the cover letter by including 2 diffstats;
here's the fixed one]
This patch series adds support for timers attached to an AioContext clock which get called within aio_poll.
In doing so it removes alarm timers and moves to use ppoll where possible.
This patch set 'sort of' passes make check (see below for caveat) including a new test harness for the aio timers, but has not been tested much beyond that. In particular, the win32 changes have not even been compile tested. Equally, alterations to use_icount are untested.
Caveat: I have had to alter tests/test-aio.c so the following error no longer occurs.
ERROR:tests/test-aio.c:346:test_wait_event_notifier_noflush: assertion failed: (aio_poll(ctx, false))
As gar as I can tell, this check was incorrect, in that it checking aio_poll makes progress when in fact it should not make progress. I fixed an issue where aio_poll was (as far as I can tell) wrongly returning true on a timeout, and that generated this error.
Note also the comment on patch 15 in relation to a possible bug in cpus.c.
Changes since v5:
* Rebase onto master (b9ac5d9)
* Fix spacing in typedef QEMUTimerList
* Rename 'QEMUClocks' extern to 'qemu_clocks'
Changes since v4:
* Rename qemu_timerlist_ functions to timer_list (per Paolo Bonzini)
* Rename qemu_timer_.*timerlist.* to timer_ (per Paolo Bonzini)
* Use enum for QEMUClockType
* Put clocks into an array; remove global variables
* Introduce QEMUTimerListGroup - a timeliest of each type
* Add a QEMUTimerListGroup to AioContext
* Use a callback on timer modification, rather than binding in AioContext into the timeliest
* Make cpus.c iterate over all timerlists when it does a notify
* Make cpus.c icount timeout use soonest timeout
across all timerlists
Changes since v3:
* Split up QEMUClock and QEMUClock list
* Improve commenting
* Fix comment in vl.c
* Change test/test-aio.c to reflect correct behaviour in aio_poll.
Changes since v2:
* Reordered to remove alarm timers last
* Added prctl(PR_SET_TIMERSLACK, 1, ...)
* Renamed qemu_g_poll_ns to qemu_poll_ns
* Moved declaration of above & drop glib types
* Do not use a global list of qemu clocks
* Add AioContext * to QEMUClock
* Split up conversion to use ppoll and timers
* Indentation fix
* Fix aio_win32.c aio_poll to return progress
* aio_notify / qemu_notify when timers are modified
* change comment in deprecation of clock options
Alex Bligh (16):
aio / timers: add qemu-timer.c utility functions
aio / timers: add ppoll support with qemu_poll_ns
aio / timers: Add prctl(PR_SET_TIMERSLACK, 1, ...) to reduce timer
slack
aio / timers: Make qemu_run_timers and qemu_run_all_timers return
progress
aio / timers: Split QEMUClock into QEMUClock and QEMUTimerList aio / timers: Untangle include files
aio / timers: Add QEMUTimerListGroup and helper functions aio / timers: Add QEMUTimerListGroup to AioContext
aio / timers: Add a notify callback to QEMUTimerList aio / timers: aio_ctx_prepare sets timeout from AioContext timers aio / timers: Convert aio_poll to use AioContext timers' deadline aio / timers: Convert mainloop to use timeout
aio / timers: On timer modification, qemu_notify or aio_notify aio / timers: Use all timerlists in icount warp calculations aio / timers: Remove alarm timers
aio / timers: Add test harness for AioContext timers
aio-posix.c | 20 +- aio-win32.c | 22 +- async.c | 20 +- configure | 37 +++ cpus.c | 44 ++- dma-helpers.c | 1 + hw/dma/xilinx_axidma.c | 1 + hw/timer/arm_timer.c | 1 + hw/timer/grlib_gptimer.c | 2 + hw/timer/imx_epit.c | 1 + hw/timer/imx_gpt.c | 1 + hw/timer/lm32_timer.c | 1 + hw/timer/puv3_ost.c | 1 + hw/timer/slavio_timer.c | 1 + hw/timer/xilinx_timer.c | 1 + hw/usb/hcd-uhci.c | 1 + include/block/aio.h | 4 + include/block/block_int.h | 1 + include/block/coroutine.h | 2 + include/qemu/timer.h | 122 ++++++- main-loop.c | 49 ++- migration-exec.c | 1 + migration-fd.c | 1 + migration-tcp.c | 1 + migration-unix.c | 1 + migration.c | 1 + nbd.c | 1 + net/net.c | 1 + net/socket.c | 1 + qemu-coroutine-io.c | 1 + qemu-io-cmds.c | 1 + qemu-nbd.c | 1 + qemu-timer.c | 803 ++++++++++++++++----------------------------- slirp/misc.c | 1 + tests/test-aio.c | 141 +++++++- tests/test-thread-pool.c | 3 + thread-pool.c | 1 + ui/vnc-auth-vencrypt.c | 2 +- ui/vnc-ws.c | 1 + vl.c | 4 +- 40 files changed, 736 insertions(+), 564 deletions(-)
Signature · 1 line
1.7.9.5
Re: [Qemu-devel] [RFC] [PATCHv5 06/16] aio / timers: Untangle include files
In reply to Alex BlighQuoted 29 lines · Alex Bligh
include/qemu/timer.hhas no need to includemain-loop.hand doing so causes an issue for the next patch. Unfortunately various files assume includingtimers.hwill pull inmain-loop.h. Untangle this mess.Signed-off-by Alex Bligh <[email protected]>--- dma-helpers.c | 1 + hw/dma/xilinx_axidma.c | 1 + hw/timer/arm_timer.c | 1 + hw/timer/grlib_gptimer.c | 2 ++ hw/timer/imx_epit.c | 1 + hw/timer/imx_gpt.c | 1 + hw/timer/lm32_timer.c | 1 + hw/timer/puv3_ost.c | 1 + hw/timer/slavio_timer.c | 1 + hw/timer/xilinx_timer.c | 1 + hw/usb/hcd-uhci.c | 1 + include/block/block_int.h | 1 + include/block/coroutine.h | 2 ++ include/qemu/timer.h | 1 - migration-exec.c | 1 + migration-fd.c | 1 + migration-tcp.c | 1 + migration-unix.c | 1 + migration.c | 1 + nbd.c | 1 + net/net.c | 1 + net/socket.c | 1 +
Quoted 8 lines
qemu-coroutine-io.c | 1 + qemu-io-cmds.c | 1 + qemu-nbd.c | 1 + slirp/misc.c | 1 + thread-pool.c | 1 + ui/vnc-auth-vencrypt.c | 2 +- ui/vnc-ws.c | 1 + 29 files changed, 30 insertions(+), 2 deletions(-)
Fails to build ui/vnc-auth-sasl.c
Please make sure that your ./configure output shows most optional dependencies are available (you need to install development packages for these libraries). Otherwise you will not see build breakage.
Stefan
Re: [Qemu-devel] [RFC] [PATCHv5 06/16] aio / timers: Untangle include files
Stefan,
Quoted 5 lines · --On 6 August 2013 12:10:17 +0200 Stefan Hajnoczi <[email protected]> wrote:
Fails to build
ui/vnc-auth-sasl.cPlease make sure that your ./configure output shows most optional dependencies are available (you need to install development packages for these libraries). Otherwise you will not see build breakage.
Thanks. It will no doubt need a:
#include "qemu/main-loop.h"
I'll need to find something to build all this on a little more powerful than the puny VM on my laptop.
Is this untangling include files approach a good one? Most of the functions that these things needed appeared to have little to do with main-loop per se. (though they are in main-loop.c).
Signature · 1 line
Alex Bligh
Re: [Qemu-devel] [RFC] [PATCHv5 06/16] aio / timers: Untangle include files
Quoted 3 lines · Alex Bligh
Is this untangling include files approach a good one? Most of the functions that these things needed appeared to have little to do with main-loop per se. (though they are in
main-loop.c).
I'm happy with it since qemu_set_fd_handler2() is indeed a main-loop function.
Stefan
Re: [Qemu-devel] [RFC] [PATCHv6 00/16] aio / timers: Add AioContext timers and use ppoll
In reply to Alex BlighQuoted 1 line · Alex Bligh
Changes since v5:
Please post revisions as separate email threads instead of replying to the last revision. It's easy for patches to get overlooked by maintainers if they are not top-level threads.
No need to resend v6 but please send v7 as a separate thread.
Stefan
Re: [Qemu-devel] [RFC] [PATCHv6 01/16] aio / timers: add qemu-timer.c utility functions
In reply to Alex BlighQuoted 9 lines · Alex Bligh
Add
qemu_free_clockand exposeqemu_new_clockand clock types.Add utility functions to
qemu-timer.cfor nanosecond timing.Add
qemu_clock_deadline_nsto calculate deadlines to nanosecond accuracy.Add utility function
qemu_soonest_timeoutto calculate soonest deadline.
Quoted 8 lines
Add
qemu_timeout_ns_to_msto convert a timeout in nanoseconds back to milliseconds for when ppoll is not used.Signed-off-by Alex Bligh <[email protected]>--- include/qemu/timer.h | 17 ++++++++++++++ qemu-timer.c | 63 +++++++++++++++++++++++++++++++++++++++++++++----- 2 files changed, 74 insertions(+), 6 deletions(-)
There is still too much happening in this patch. Making qemu_new_clock()/qemu_free_clock() public and moving the clock source constants can be done in a single patch.
The next patch can change the semantics of qemu_clock_deadline() to return INT32_MAX when the clock source is disabled. I'm not sure why you do this and whether you checked that existing users continue to work correctly? This is worth a separate patch.
Introducing qemu_timeout_ns_to_ms() and qemu_soonest_timeout() could be done separately or together, I don't care as much there. Please include an explanation of why qemu_timeout_ns_to_ms() will be needed in the future (there are no callers in this patch).
Re: [Qemu-devel] [RFC] [PATCHv6 05/16] aio / timers: Split QEMUClock into QEMUClock and QEMUTimerList
In reply to Alex BlighQuoted 18 lines · Alex Bligh
Split QEMUClock into QEMUClock and QEMUTimerList so that we can have more than one QEMUTimerList associated with the same clock.
Introduce a
default_timerlistconcept and make existing qemu_clock_* calls that actually should operate on a QEMUTimerList call the relevant QEMUTimerList implementations, using the clock's default timerlist. This vastly reduces the invasiveness of this change and means the API stays constant for existing users.Introduce a list of QEMUTimerLists associated with each clock so that reenabling the clock can cause all the notifiers to be called. Note the code to do the notifications is added in a later patch.
Switch QEMUClockType to an enum. Remove global variables
vm_clock,host_clockandrt_clockand add compatibility defines. Do not fixqemu_next_alarm_deadlineas it's going to be deleted.
Quoted 5 lines
Signed-off-by Alex Bligh <[email protected]>--- include/qemu/timer.h | 91 +++++++++++++++++++++++-- qemu-timer.c | 185 ++++++++++++++++++++++++++++++++++++++------------ 2 files changed, 224 insertions(+), 52 deletions(-)
Although in the short time it's easier to keep the old API where QEMUClock also acts as a timer list, I don't think it's a good idea to do that.
It makes timers harder to understand for everyone because we now have timerlists but an extra layer to sort of make QEMUClock like a timerlist.
The API we should end up with has two concepts: clock sources (rt, vm, host) and timer lists. This patch is adding unnecessary indirections and making the clock source look like a timer list. I think it's worth converting existing code once and for all instead of carrying this baggage forever.
Quoted 21 lines
include/qemu/timer.h +12
-1+extern QEMUClock *qemu_clocks[QEMU_CLOCK_MAX ]; + +static inline QEMUClock *qemu_get_clock (QEMUClockType type) +{ +return qemu_clocks[type]; +} + +/* These three clocks are maintained here with separate variable +names for compatibility only. +*/ +/* The real time clock should be used only for stuff which does not change the virtual machine state, as it is run even if the virtual machine is stopped. The real time clock has a frequency of 1000 Hz. */ -extern QEMUClock *rt_clock; +#define rt_clock (qemu_get_clock(QEMU_CLOCK_REALTIME))/* The virtual clock is only run during the emulation. It is stopped when the virtual machine is stopped. Virtual timers use a high precision clock, usually cpu cycles (use ticks_per_sec). */
include/qemu/timer.h line 52
Quoted 10 lines
include/qemu/timer.h +2
-2-extern QEMUClock *vm_clock; +#define vm_clock (qemu_get_clock(QEMU_CLOCK_VIRTUAL))/* The host clock should be use for device models that emulate accurate real time sources. It will continue to run when the virtual machine is suspended, and it will reflect system time changes the host may undergo (e.g. due to NTP). The host clock has the same precision as the virtual clock. */ -extern QEMUClock *host_clock; +#define host_clock (qemu_get_clock(QEMU_CLOCK_HOST))
What is the point of this change? It's not clear how using qemu_clocks[] is an improvement over rt_clock, vm_clock, and host_clock.
Or in other words: why is timerlist_new necessary?
qemu-timer.c line 79
Quoted 4 lines
struct QEMUTimer {
int64_texpire_time; /* in nanoseconds */qemu-timer.c +1
-1-QEMUClock *clock; +QEMUTimerList *tl;
'timer_list' is easier to read than just 'tl'.
qemu-timer.c next: · line 270
Quoted 3 lines
qemu-timer.c +2
-1-QEMUClock *qemu_new_clock (int type) +void timerlist_free (QEMUTimerList *tl) +{
Assert that active_timers is empty.
qemu-timer.c QEMUClock *qemu_new_clock(int type) · line 302
Quoted 4 lines
qemu-timer.c +4
-0+bool qemu_clock_use_for_deadline (QEMUClock *clock) +{ +return !(use_icount && (clock->type =QEMU_CLOCK_VIRTUAL )); +}
Please use doc comments (see include/object/qom.h for example doc comment syntax). No idea why this function is needed or what it will be used for.
Also, should it be '==' instead of '='?
Re: [Qemu-devel] [RFC] [PATCHv6 01/16] aio / timers: add qemu-timer.c utility functions
In reply to Stefan HajnocziStefan,
Quoted 3 lines · --On 6 August 2013 14:02:18 +0200 Stefan Hajnoczi <[email protected]> wrote:
There is still too much happening in this patch. Making qemu_new_clock()/qemu_free_clock() public and moving the clock source constants can be done in a single patch.
OK I'll split it up.
Quoted 4 lines
The next patch can change the semantics of
qemu_clock_deadline()to returnINT32_MAXwhen the clock source is disabled. I'm not sure why you do this and whether you checked that existing users continue to work correctly?
Rationale: I suspect it doesn't really matter (see below) but the rationale was that if the clock is disabled, timers will expire in an infinite time. I was trying to make treatment of expire times consistent throughout qemu-timer, and this was the one place a disabled clock was being treated as if the timers were going to expire.
Audit: The only users (at least by the time my patch set is finished) are:
* cpus.c within qtest_warp(), which appears not to consider the case of vm_clock being disabled. I do not think this function has been written considering the possibility that there are no active vm_clock timers or where the deadline is > INT32_MAX ns away.
* qtest_process_command(), evaluating clock_step, which calls the above.
My preference would be to move these to qemu_clock_deadline_ns (without the INT32_MAX check) and delete the old qemu_clock_deadline routine entirely, but I don't really understand the full set of circumstances in which the qtest routines are meant to work.
Of course cpus.c now uses qemu_clock_deadline_ns_all for a couple of the previous two uses, and in patch 14 of my series I've commented that I think the previous use was buggy but have maintained bug-for-bug compatibility.
I'd particularly like comments on patch 14, which I've mostly blind coded based on what Paolo asked for. I'm afraid I found the icount stuff a bit opaque. I'll hold off from v7 until someone's had a look at these.
Quoted 3 lines
Please include
an explanation of whyqemu_timeout_ns_to_ms()will be needed in the future (there are no callers in this patch).
You mean in the commit text as well as the following?
+/* Transition function to convert a nanosecond timeout to ms
+ * This is used where a system does not support ppoll
+ */
Signature · 1 line
Alex Bligh
Re: [Qemu-devel] [RFC] [PATCHv6 08/16] aio / timers: Add QEMUTimerListGroup to AioContext
In reply to Alex Blightests/test-aio.c int main(int argc, char **argv) · line 633
Quoted 6 lines · Alex Bligh
tests/test-aio.c +2
-0@@ -628,6 +629,8 @@ int main(int argc, char **argv) {GSource *src; +init_clocks (); +
Why add this call now? Are there any other programs where this should be added too?
Re: [Qemu-devel] [RFC] [PATCHv6 09/16] aio / timers: Add a notify callback to QEMUTimerList
In reply to Alex BlighQuoted 15 lines · Alex Bligh
Add a notify pointer to QEMUTimerList so it knows what to notify on a timer change.
Signed-off-by Alex Bligh <[email protected]>--- async.c | 7 ++++++- include/qemu/timer.h | 7 ++++++- qemu-timer.c | 24 ++++++++++++++++++++++-- 3 files changed, 34 insertions(+), 4 deletions(-)async.c +0
-0
async.c void aio_notify(AioContext *ctx) · line 239
Quoted 6 lines
async.c +3
-0@@ -234,6 +234,11 @@ void aio_notify(AioContext *ctx)event_notifier_set (&ctx->notifier); } +static void aio_timerlist_notify (void *opaque) +{ +aio_notify ((AioContext *)opaque);
void * is automatically converted to any pointer type. No need for an explicit cast, a C compiler doesn't emit a warning here.
Re: [Qemu-devel] [RFC] [PATCHv6 10/16] aio / timers: aio_ctx_prepare sets timeout from AioContext timers
In reply to Alex Blighasync.c aio_ctx_check(GSource *source) · line 192
Quoted 6 lines · Alex Bligh
async.c +1
-1@@ -180,7 +189,7 @@ aio_ctx_check(GSource *source)return true; } } -return aio_pending (ctx); +return aio_pending (ctx) || (timerlistgroup_deadline_ns (ctx->tlg) >=0 );
Now we always dispatch if there is a timer? Should this comparison be timerlistgroup_deadline_ns(ctx->tlg) == 0 instead to only dispatch expired timers?
Stefan
Re: [Qemu-devel] [RFC] [PATCHv6 05/16] aio / timers: Split QEMUClock into QEMUClock and QEMUTimerList
In reply to Stefan HajnocziStefan,
Quoted 7 lines
Although in the short time it's easier to keep the old API where QEMUClock also acts as a timer list, I don't think it's a good idea to do that.
It makes timers harder to understand for everyone because we now have timerlists but an extra layer to sort of make QEMUClock like a timerlist.
I think I disagree here.
At the very least we should put the conversion to use the new API into a separate patch (possibly a separate patch set). It's fantastically intrusive.
However it touches so much (it touches just about every driver) that it's going to be really hard for people to maintain a driver that works with two versions of qemu. This is really useful (having just backported the modern ceph driver to qemu 1.0 and 1.3). Moreover it's going to break any developer with an existing non-upstreamed branch
The change is pretty mechanical, so what I'd suggest is that we keep the old API around too (as deprecated) for a little while. At some stage we can fix existing code and after that point not accept patches that use the existing API.
Even if the period is just a month (i.e. the old API goes before 1.7), why break things unnecessarily?
Quoted 2 lines
What is the point of this change? It's not clear how using qemu_clocks[] is an improvement over
rt_clock,vm_clock, andhost_clock.
Because you can iterate through all the clocks with a for() loop as is done in six places in qemu-timer.c by the end of the patch series (look for QEMU_CLOCK_MAX). This also allows us to use a timerlistgroup (a set of timerlists, one for each clock) so each AioContext can have a clock of each type, as Paolo requested in response to v4.
Quoted 1 line
Or in other words: why is
timerlist_newnecessary?
I think that question is entirely orthogonal. This generates a new timerlist. In v4 each AioContext had its own timerlist. Now it has its own timerlistgroup, with one timerlist of each clock type. The constructor timerlist_new is there to initialise the timerlist which broadly is a malloc(), setting ->clock, and inserting it onto the list of timerlists associated with that clock. How would we avoid this if we still want multiple timerlists per clock?
Quoted 6 lines
qemu-timer.c line 79
Quoted 4 lines
struct QEMUTimer {
int64_texpire_time; /* in nanoseconds */qemu-timer.c +1
-1-QEMUClock *clock; +QEMUTimerList *tl;'
timer_list' is easier to read than just 'tl'.
It caused a pile of line wrap issues which made the patch harder to read, so I shortened it. I can put it back if you like.
Quoted 5 lines
qemu-timer.c next: · line 270
Quoted 3 lines
qemu-timer.c +2
-1-QEMUClock *qemu_new_clock (int type) +void timerlist_free (QEMUTimerList *tl) +{Assert that
active_timersis empty.
OK
Quoted 8 lines
qemu-timer.c QEMUClock *qemu_new_clock(int type) · line 302
Quoted 4 lines
qemu-timer.c +4
-0+bool qemu_clock_use_for_deadline (QEMUClock *clock) +{ +return !(use_icount && (clock->type =QEMU_CLOCK_VIRTUAL )); +}Please use doc comments (see
include/object/qom.hfor example doc comment syntax). No idea why this function is needed or what it will be used for.
I will comment it, but it mostly does what it says in the tin. Per Paolo's comment, the vm_clock should not be used for calculation of deadlines to ppoll etc. if use_icount is true, because it's not actually in nanoseconds; rather qemu_notify() or aio_notify() get called by the vm cpu thread when the relevant instruction counter is exceeded.
Quoted 1 line
Also, should it be '==' instead of '='?
Good catch!
Signature · 1 line
Alex Bligh
Re: [Qemu-devel] [RFC] [PATCHv6 08/16] aio / timers: Add QEMUTimerListGroup to AioContext
In reply to Stefan HajnocziQuoted 9 lines · --On 6 August 2013 14:30:43 +0200 Stefan Hajnoczi <[email protected]> wrote:
tests/test-aio.c int main(int argc, char **argv) · line 633
Quoted 6 lines · Alex Bligh
tests/test-aio.c +2
-0@@ -628,6 +629,8 @@ int main(int argc, char **argv) {GSource *src; +init_clocks (); +Why add this call now?
Because otherwise make check SEGVs after the patch.
Quoted 2 lines
Are there any other programs where this should
be added too?
I believe not (at least make check does not fail anything).
Signature · 1 line
Alex Bligh
Re: [Qemu-devel] [RFC] [PATCHv6 09/16] aio / timers: Add a notify callback to QEMUTimerList
In reply to Stefan HajnocziQuoted 6 lines · --On 6 August 2013 14:34:00 +0200 Stefan Hajnoczi <[email protected]> wrote:
async.c void aio_notify(AioContext *ctx) · line 239
Quoted 3 lines
async.c +3
-0+static void aio_timerlist_notify (void *opaque) +{ +aio_notify ((AioContext *)opaque);void * is automatically converted to any pointer type. No need for an explicit cast, a C compiler doesn't emit a warning here.
OK
Signature · 1 line
Alex Bligh
Re: [Qemu-devel] [RFC] [PATCHv6 10/16] aio / timers: aio_ctx_prepare sets timeout from AioContext timers
In reply to Stefan HajnocziStefan,
Quoted 12 lines · --On 6 August 2013 14:40:00 +0200 Stefan Hajnoczi <[email protected]> wrote:
async.c aio_ctx_check(GSource *source) · line 193
Quoted 7 lines · Alex Bligh
async.c +1
-1@@ -180,7 +189,7 @@ aio_ctx_check(GSource *source)return true; } } -return aio_pending (ctx); +return aio_pending (ctx) || (timerlistgroup_deadline_ns (ctx->tlg) >=0 );Now we always dispatch if there is a timer? Should this comparison be
timerlistgroup_deadline_ns(ctx->tlg)== 0 instead to only dispatch expired timers?
I think you are right.
Signature · 1 line
Alex Bligh
Re: [Qemu-devel] [RFC] [PATCHv6 05/16] aio / timers: Split QEMUClock into QEMUClock and QEMUTimerList
In reply to Stefan HajnocziQuoted 2 lines · --On 6 August 2013 14:26:33 +0200 Stefan Hajnoczi <[email protected]> wrote:
Please use doc comments (see
include/object/qom.hfor example doc comment syntax).
Should these go in the .h (per include/qom/object.h) or the .c (per your comment)?
Signature · 1 line
Alex Bligh
Re: [Qemu-devel] [RFC] [PATCHv6 01/16] aio / timers: add qemu-timer.c utility functions
In reply to Alex BlighQuoted 6 lines · Alex Bligh
--On 6 August 2013 14:02:18 +0200 Stefan Hajnoczi <[email protected]> wrote:
My preference would be to move these toqemu_clock_deadline_ns(without theINT32_MAXcheck) and delete the oldqemu_clock_deadlineroutine entirely, but I don't really understand the full set of circumstances in which the qtest routines are meant to work.
Okay, that's excellent. It would be great to move to a single function.
The way qtest works is that it executes QEMU in a mode that does not run guest code. Instead of running guest code it listens for commands over a socket. The wire protocol can peek/poke memory, notify of interrupts, and warp the clock.
There are test cases that use qtest to test emulated devices.
When qtest either steps the clock or sets it to a completely new value using qtest_clock_warp() it runs all vm_clock timers that should expire before the new time.
Does this help?
Quoted 9 lines
Quoted 3 lines
Please include
an explanation of whyqemu_timeout_ns_to_ms()will be needed in the future (there are no callers in this patch).You mean in the commit text as well as the following?
+/* Transition function to convert a nanosecond timeout to ms
+ * This is used where a system does not support ppoll
+ */
Usually a doc comment is enough since it explains what the function does. If it's a low-level function is may be necessary to give more context in the commit description.
Stefan
Re: [Qemu-devel] [RFC] [PATCHv6 01/16] aio / timers: add qemu-timer.c utility functions
Stefan,
Quoted 14 lines · --On 6 August 2013 15:59:11 +0200 Stefan Hajnoczi <[email protected]> wrote:
Quoted 6 lines
--On 6 August 2013 14:02:18 +0200 Stefan Hajnoczi <[email protected]> wrote:
My preference would be to move these toqemu_clock_deadline_ns(without theINT32_MAXcheck) and delete the oldqemu_clock_deadlineroutine entirely, but I don't really understand the full set of circumstances in which the qtest routines are meant to work.Okay, that's excellent. It would be great to move to a single function.
The way qtest works is that it executes QEMU in a mode that does not run guest code. Instead of running guest code it listens for commands over a socket. The wire protocol can peek/poke memory, notify of interrupts, and warp the clock.
Quoted 7 lines
There are test cases that use qtest to test emulated devices.
When qtest either steps the clock or sets it to a completely new value using
qtest_clock_warp()it runs allvm_clocktimers that should expire before the new time.Does this help?
Nearly :-)
How do I actually run the code (i.e. how do I test whether I've broken it)? I take it that's something different from just 'make check'?
Signature · 1 line
Alex Bligh
Re: [Qemu-devel] [RFC] [PATCHv6 05/16] aio / timers: Split QEMUClock into QEMUClock and QEMUTimerList
In reply to Alex BlighQuoted 10 lines · Alex Bligh
Stefan,
Quoted 7 lines
Although in the short time it's easier to keep the old API where QEMUClock also acts as a timer list, I don't think it's a good idea to do that.
It makes timers harder to understand for everyone because we now have timerlists but an extra layer to sort of make QEMUClock like a timerlist.
Quoted 5 lines
I think I disagree here.
At the very least we should put the conversion to use the new API into a separate patch (possibly a separate patch set). It's fantastically intrusive.
Yes, it should be a separate patch.
Quoted 5 lines
However it touches so much (it touches just about every driver) that it's going to be really hard for people to maintain a driver that works with two versions of qemu. This is really useful (having just backported the modern ceph driver to qemu 1.0 and 1.3). Moreover it's going to break any developer with an existing non-upstreamed branch
That's true and there are other instances of this like the multi-queue networking or qemu_get_clock_ns(). But upstream cannot be held back because of downstreams and especially not because of out-of-tree branches.
Quoted 7 lines
The change is pretty mechanical, so what I'd suggest is that we keep the old API around too (as deprecated) for a little while. At some stage we can fix existing code and after that point not accept patches that use the existing API.
Even if the period is just a month (i.e. the old API goes before 1.7), why break things unnecessarily?
Nothing upstream breaks. Only out-of-tree code breaks but that's life.
What's important is that upstream will be clean and easy to understand or debug. Given how undocumented the QEMU codebase is, leaving legacy API layers around just does more to confuse new contributors.
That's why I'd really like to transition now instead of leaving things in a more complex state than before.
Quoted 9 lines
Quoted 2 lines
What is the point of this change? It's not clear how using qemu_clocks[] is an improvement over
rt_clock,vm_clock, andhost_clock.Because you can iterate through all the clocks with a
for()loop as is done in six places inqemu-timer.cby the end of the patch series (look forQEMU_CLOCK_MAX). This also allows us to use a timerlistgroup (a set of timerlists, one for each clock) so each AioContext can have a clock of each type, as Paolo requested in response to v4.
Later in the patch series I realized how this gets used. Thanks for explaining.
We end up with:
AioContext->tlg and default_timerlistgroup.
Regarding naming, I think default_timerlistgroup should be called main_loop_timerlistgroup instead. The meaning of "default" is not obvious.
Now let's think about how callers will create QEMUTimers:
1. AioContext
timer_new(ctx->tlg[QEMU_CLOCK_RT], SCALE_NS, cb, opaque);
Or with a wrapper:
QEMUTimer *aio_new_timer(AioContext *ctx, QEMUClockType type, int scale,
QEMUTimerCB *cb, void *opaque)
{
return timer_new(ctx->tlg[type], scale, cb, opaque);
}
aio_new_timer(ctx, QEMU_CLOCK_RT, SCALE_NS, cb, opaque);2. main-loop
/* without legacy qemu_timer_new() */ timer_new(main_loop_tlg[QEMU_CLOCK_RT], SCALE_NS, cb, opaque);
Or with a wrapper:
QEMUTimer *qemu_new_timer(QEMUClockType type, int scale,
QEMUTimerCB *cb, void *opaque)
{
return timer_new(main_loop_tlg[type], scale, cb, opaque);
}
qemu_new_timer(QEMU_CLOCK_RT, SCALE_NS, cb, opaque);Is this what you have in mind too?
Quoted 9 lines
Quoted 1 line
Or in other words: why is
timerlist_newnecessary?I think that question is entirely orthogonal. This generates a new timerlist. In v4 each AioContext had its own timerlist. Now it has its own timerlistgroup, with one timerlist of each clock type. The constructor
timerlist_newis there to initialise the timerlist which broadly is amalloc(), setting ->clock, and inserting it onto the list of timerlists associated with that clock. How would we avoid this if we still want multiple timerlists per clock?
I think I'm okay with the array indexing. Anyway, here were my thoughts:
I guess I was thinking about keeping global clock sources for rt, vm, and host. Then you always use timerlist_new_from_clock() and you don't need timerlist_new() at all.
But this doesn't allow for the array indexing that you do in TimerListGroup later. I didn't know that at this point in the patch series.
Quoted 9 lines
Quoted 6 lines
qemu-timer.c line 79
Quoted 4 lines
struct QEMUTimer {
int64_texpire_time; /* in nanoseconds */qemu-timer.c +1
-1-QEMUClock *clock; +QEMUTimerList *tl;'
timer_list' is easier to read than just 'tl'.It caused a pile of line wrap issues which made the patch harder to read, so I shortened it. I can put it back if you like.
Are you sure it's the QEMUTimer->tl field that causes line wraps?
I took a quick look and it seemed like only the QEMUTimerList *tl function argument to the deadline functions could cause line wrap. The argument variable is unrelated and can stay short since it has a very narrow context - the reader can be expected to remember the tl argument while reading the code for the function.
Quoted 9 lines
Quoted 8 lines
qemu-timer.c QEMUClock *qemu_new_clock(int type) · line 302
Quoted 4 lines
qemu-timer.c +4
-0+bool qemu_clock_use_for_deadline (QEMUClock *clock) +{ +return !(use_icount && (clock->type =QEMU_CLOCK_VIRTUAL )); +}Please use doc comments (see
include/object/qom.hfor example doc comment syntax). No idea why this function is needed or what it will be used for.
Quoted 5 lines
I will comment it, but it mostly does what it says in the tin. Per Paolo's comment, the
vm_clockshould not be used for calculation of deadlines to ppoll etc. ifuse_icountis true, because it's not actually in nanoseconds; ratherqemu_notify()oraio_notify()get called by the vm cpu thread when the relevant instruction counter is exceeded.
I didn't know that but the explanation makes sense. Definitely something that could be in a comment. Perhaps its best to introduce this small helper function in the patch that actually calls it.
Stefan
Re: [Qemu-devel] [RFC] [PATCHv6 05/16] aio / timers: Split QEMUClock into QEMUClock and QEMUTimerList
In reply to Alex BlighQuoted 8 lines · Alex Bligh
--On 6 August 2013 14:26:33 +0200 Stefan Hajnoczi
Quoted 2 lines · <[email protected]> wrote:
Please use doc comments (see
include/object/qom.hfor example doc comment syntax).Should these go in the .h (per
include/qom/object.h) or the .c (per your comment)?
I don't think there is a strict rule.
I like putting doc comments in the .c file for non-external APIs. That way it's easier to remember that the doc comment needs to be updated if the function is modified.
Stefan
Re: [Qemu-devel] [RFC] [PATCHv6 08/16] aio / timers: Add QEMUTimerListGroup to AioContext
In reply to Alex BlighQuoted 10 lines · Alex Bligh
--On 6 August 2013 14:30:43 +0200 Stefan Hajnoczi
Quoted 5 lines · <[email protected]> wrote:
Quoted 4 lines · Alex Bligh
tests/test-aio.c +0
-0@@ -628,6 +629,8 @@ int main(int argc, char **argv) {GSource *src;
Quoted 6 lines
Quoted 4 lines
Review int main(int argc, char **argv) · line 633
Quoted 2 lines
(quoted diff) +2
-0+ init_clocks(); +Why add this call now?
Because otherwise make check SEGVs after the patch.
It wasn't clear from the patch why there would be a crash. I looked deeper and timerlistgroup_init() calls qemu_get_clock() indirectly, so we need to make sure that qemu_clocks[] is initialized to avoid a NULL pointer dereference.
Now I'm confident that these init_clocks() are needed in this patch.
Quoted 4 lines
Quoted 2 lines
Are there any other programs where this should
be added too?I believe not (at least make check does not fail anything).
I have checked that qemu-io, qemu-img, and qemu-nbd are okay because they call qemu_init_main_loop(), which calls init_clocks().
Stefan
Re: [Qemu-devel] [RFC] [PATCHv6 05/16] aio / timers: Split QEMUClock into QEMUClock and QEMUTimerList
In reply to Stefan HajnocziStefan,
Quoted 7 lines
Quoted 5 lines
I think I disagree here.
At the very least we should put the conversion to use the new API into a separate patch (possibly a separate patch set). It's fantastically intrusive.
Yes, it should be a separate patch.
...
Quoted 11 lines
Quoted 2 lines
Even if the period is just a month (i.e. the old API goes before 1.7), why break things unnecessarily?
Nothing upstream breaks. Only out-of-tree code breaks but that's life.
What's important is that upstream will be clean and easy to understand or debug. Given how undocumented the QEMU codebase is, leaving legacy API layers around just does more to confuse new contributors.
That's why I'd really like to transition now instead of leaving things in a more complex state than before.
OK. I'm just concerned about the potential fall out. If that's what everyone wants, I will do a monster patch to fix this up. Need that be part of this series? I can't help thinking it would be better to have the series applied first.
Quoted 7 lines
We end up with:
AioContext->tlganddefault_timerlistgroup.Regarding naming, I think
default_timerlistgroupshould be calledmain_loop_timerlistgroupinstead. The meaning of "default" is not obvious.
I agree. I will change this.
Quoted 8 lines
Now let's think about how callers will create QEMUTimers:
1. AioContext
timer_new(ctx->tlg[QEMU_CLOCK_RT], SCALE_NS, cb, opaque);Or with a wrapper:
Quoted 7 lines
QEMUTimer *aio_new_timer(AioContext *ctx, QEMUClockType type, int
scale, QEMUTimerCB *cb, void *opaque){ return timer_new(ctx->tlg[type], scale, cb, opaque);}
aio_new_timer(ctx, QEMU_CLOCK_RT, SCALE_NS, cb, opaque);
I was actually thinking about adding that wrapper anyway.
Do you think we need to wrap timer_mod, timer_del, timer_free etc. to make aio_timer_mod and so forth, even though they are a straight pass through?
Quoted 15 lines
2. main-loop
/* without legacy qemu_timer_new() */ timer_new(main_loop_tlg[QEMU_CLOCK_RT], SCALE_NS, cb, opaque);Or with a wrapper: QEMUTimer *qemu_new_timer(QEMUClockType type, int scale, QEMUTimerCB *cb, void *opaque) { return timer_new(main_loop_tlg[type], scale, cb, opaque); } qemu_new_timer(QEMU_CLOCK_RT, SCALE_NS, cb, opaque);
Quoted 1 line
Is this what you have in mind too?
Yes.
Certainly qemu_timer_new (as opposed to qemu_new_timer) would be a good addition.
Quoted 3 lines
But this doesn't allow for the array indexing that you do in TimerListGroup later. I didn't know that at this point in the patch series.
Yep. I'll leave that if that's OK.
Quoted 12 lines
Quoted 9 lines
Quoted 6 lines
qemu-timer.c line 79
Quoted 4 lines
struct QEMUTimer {
int64_texpire_time; /* in nanoseconds */qemu-timer.c +1
-1-QEMUClock *clock; +QEMUTimerList *tl;'
timer_list' is easier to read than just 'tl'.It caused a pile of line wrap issues which made the patch harder to read, so I shortened it. I can put it back if you like.
Are you sure it's the
QEMUTimer->tlfield that causes line wraps?
Quoted 7 lines
I took a quick look and it seemed like only the QEMUTimerList *tl function argument to the deadline functions could cause line wrap. The argument variable is unrelated and can stay short since it has a very narrow context - the reader can be expected to remember the tl argument while reading the code for the function.
From memory it was lots of ->tl expanding within the code the issue
rather than the arguments to functions. I'll try again. To be honest it's probably easier to change tl to timer_list throughout.
Quoted 16 lines
Quoted 10 lines
Quoted 9 lines
qemu-timer.c QEMUClock *qemu_new_clock(int type) · line 302
Quoted 5 lines
qemu-timer.c +4
-0+bool qemu_clock_use_for_deadline (QEMUClock *clock) +{ +return !(use_icount && (clock->type =QEMU_CLOCK_VIRTUAL )); +}Please use doc comments (see
include/object/qom.hfor example doc comment syntax). No idea why this function is needed or what it will be used for.Quoted 5 lines
I will comment it, but it mostly does what it says in the tin. Per Paolo's comment, the
vm_clockshould not be used for calculation of deadlines to ppoll etc. ifuse_icountis true, because it's not actually in nanoseconds; ratherqemu_notify()oraio_notify()get called by the vm cpu thread when the relevant instruction counter is exceeded.
Quoted 3 lines
I didn't know that but the explanation makes sense. Definitely something that could be in a comment. Perhaps its best to introduce this small helper function in the patch that actually calls it.
It's preparation for the next patch. I will add a comment in the commit message.
Signature · 1 line
Alex Bligh
Re: [Qemu-devel] [RFC] [PATCHv6 08/16] aio / timers: Add QEMUTimerListGroup to AioContext
In reply to Stefan HajnocziStefan,
Quoted 6 lines · --On 6 August 2013 16:45:12 +0200 Stefan Hajnoczi <[email protected]> wrote:
Quoted 1 line
Because otherwise make check SEGVs after the patch.
It wasn't clear from the patch why there would be a crash. I looked deeper and
timerlistgroup_init()callsqemu_get_clock()indirectly, so we need to make sure that qemu_clocks[] is initialized to avoid a NULL pointer dereference.
Actually now I recall v4 had:
@@ -215,6 +216,12 @@ AioContext *aio_context_new(void)
aio_set_event_notifier(ctx, &ctx->notifier,
(EventNotifierHandler *)
event_notifier_test_and_clear, NULL);
+ /* Assert if we don't have rt_clock yet. If you see this assertion
+ * it means you are using AioContext without having first called
+ * init_clocks() in main().
+ */+ assert(rt_clock);
+ ctx->tl = qemu_new_timerlist(rt_clock);
The equivalent in v7 would be an assert in timerlist_new_from_clock to check 'clock' is non-NULL. I shall put that in as the reason for this SEGV is non-obvious.
Signature · 1 line
Alex Bligh
Re: [Qemu-devel] [RFC] [PATCHv6 00/16] aio / timers: Add AioContext timers and use ppoll
In reply to Alex BlighQuoted 114 lines · Alex Bligh
This patch series adds support for timers attached to an AioContext clock which get called within
aio_poll.In doing so it removes alarm timers and moves to use ppoll where possible.
This patch set 'sort of' passes make check (see below for caveat) including a new test harness for the aio timers, but has not been tested much beyond that. In particular, the win32 changes have not even been compile tested. Equally, alterations to
use_icountare untested.Caveat: I have had to alter
tests/test-aio.cso the following error no longer occurs.ERROR:tests/test-aio.c:346:test_wait_event_notifier_noflush: assertion failed: (
aio_poll(ctx, false))As gar as I can tell, this check was incorrect, in that it checking
aio_pollmakes progress when in fact it should not make progress. I fixed an issue whereaio_pollwas (as far as I can tell) wrongly returning true on a timeout, and that generated this error.Note also the comment on patch 15 in relation to a possible bug in
cpus.c.Changes since v5:
* Rebase onto master (b9ac5d9)
* Fix spacing in typedef QEMUTimerList
* Rename 'QEMUClocks' extern to 'qemu_clocks'Changes since v4:
* Renameqemu_timerlist_functions totimer_list(per Paolo Bonzini)
* Rename qemu_timer_.*timerlist.* totimer_(per Paolo Bonzini)
* Use enum for QEMUClockType
* Put clocks into an array; remove global variables
* Introduce QEMUTimerListGroup - a timeliest of each type
* Add a QEMUTimerListGroup to AioContext
* Use a callback on timer modification, rather than binding in AioContext into the timeliest
* Makecpus.citerate over all timerlists when it does a notify
* Makecpus.cicount timeout use soonest timeout
across all timerlistsChanges since v3:
* Split up QEMUClock and QEMUClock list
* Improve commenting
* Fix comment invl.c
* Changetest/test-aio.cto reflect correct behaviour inaio_poll.Changes since v2:
* Reordered to remove alarm timers last
* Addedprctl(PR_SET_TIMERSLACK, 1, ...)
* Renamedqemu_g_poll_nstoqemu_poll_ns
* Moved declaration of above & drop glib types
* Do not use a global list of qemu clocks
* Add AioContext * to QEMUClock
* Split up conversion to use ppoll and timers
* Indentation fix
* Fixaio_win32.caio_pollto return progress
*aio_notify/qemu_notifywhen timers are modified
* change comment in deprecation of clock optionsAlex Bligh (16):
aio / timers: addqemu-timer.cutility functions
aio / timers: add ppoll support withqemu_poll_ns
aio / timers: Addprctl(PR_SET_TIMERSLACK, 1, ...)to reduce timer
slack
aio / timers: Makeqemu_run_timersandqemu_run_all_timersreturn
progress
aio / timers: Split QEMUClock into QEMUClock and QEMUTimerList aio / timers: Untangle include files
aio / timers: Add QEMUTimerListGroup and helper functions aio / timers: Add QEMUTimerListGroup to AioContext
aio / timers: Add a notify callback to QEMUTimerList aio / timers:aio_ctx_preparesets timeout from AioContext timers aio / timers: Convertaio_pollto use AioContext timers' deadline aio / timers: Convert mainloop to use timeout
aio / timers: On timer modification,qemu_notifyoraio_notifyaio / timers: Use all timerlists in icount warp calculations aio / timers: Remove alarm timers
aio / timers: Add test harness for AioContext timersaio-posix.c | 20 +- aio-win32.c | 22 +- async.c | 20 +- configure | 37 +++ cpus.c | 44 ++- dma-helpers.c | 1 + hw/dma/xilinx_axidma.c | 1 + hw/timer/arm_timer.c | 1 + hw/timer/grlib_gptimer.c | 2 + hw/timer/imx_epit.c | 1 + hw/timer/imx_gpt.c | 1 + hw/timer/lm32_timer.c | 1 + hw/timer/puv3_ost.c | 1 + hw/timer/slavio_timer.c | 1 + hw/timer/xilinx_timer.c | 1 + hw/usb/hcd-uhci.c | 1 + include/block/aio.h | 4 + include/block/block_int.h | 1 + include/block/coroutine.h | 2 + include/qemu/timer.h | 122 ++++++- main-loop.c | 49 ++- migration-exec.c | 1 + migration-fd.c | 1 + migration-tcp.c | 1 + migration-unix.c | 1 + migration.c | 1 + nbd.c | 1 + net/net.c | 1 + net/socket.c | 1 + qemu-coroutine-io.c | 1 + qemu-io-cmds.c | 1 + qemu-nbd.c | 1 + qemu-timer.c | 803 ++++++++++++++++-----------------------------
Quoted 75 lines
slirp/misc.c | 1 + tests/test-aio.c | 141 +++++++- tests/test-thread-pool.c | 3 + thread-pool.c | 1 + ui/vnc-auth-vencrypt.c | 2 +- ui/vnc-ws.c | 1 + vl.c | 4 +- 40 files changed, 736 insertions(+), 564 deletions(-)Signature · 65 lines
1.7.9.5 *** BLRUB STOP *** Alex Bligh (16): aio / timers: add qemu-timer.c utility functions aio / timers: add ppoll support with qemu_poll_ns aio / timers: Add prctl(PR_SET_TIMERSLACK, 1, ...) to reduce timer slack aio / timers: Make qemu_run_timers and qemu_run_all_timers return progress aio / timers: Split QEMUClock into QEMUClock and QEMUTimerList aio / timers: Untangle include files aio / timers: Add QEMUTimerListGroup and helper functions aio / timers: Add QEMUTimerListGroup to AioContext aio / timers: Add a notify callback to QEMUTimerList aio / timers: aio_ctx_prepare sets timeout from AioContext timers aio / timers: Convert aio_poll to use AioContext timers' deadline aio / timers: Convert mainloop to use timeout aio / timers: On timer modification, qemu_notify or aio_notify aio / timers: Use all timerlists in icount warp calculations aio / timers: Remove alarm timers aio / timers: Add test harness for AioContext timers aio-posix.c | 20 +- aio-win32.c | 22 +- async.c | 20 +- configure | 37 +++ cpus.c | 44 ++- dma-helpers.c | 1 + hw/dma/xilinx_axidma.c | 1 + hw/timer/arm_timer.c | 1 + hw/timer/grlib_gptimer.c | 2 + hw/timer/imx_epit.c | 1 + hw/timer/imx_gpt.c | 1 + hw/timer/lm32_timer.c | 1 + hw/timer/puv3_ost.c | 1 + hw/timer/slavio_timer.c | 1 + hw/timer/xilinx_timer.c | 1 + hw/usb/hcd-uhci.c | 1 + include/block/aio.h | 4 + include/block/block_int.h | 1 + include/block/coroutine.h | 2 + include/qemu/timer.h | 122 ++++++- main-loop.c | 49 ++- migration-exec.c | 1 + migration-fd.c | 1 + migration-tcp.c | 1 + migration-unix.c | 1 + migration.c | 1 + nbd.c | 1 + net/net.c | 1 + net/socket.c | 1 + qemu-coroutine-io.c | 1 + qemu-io-cmds.c | 1 + qemu-nbd.c | 1 + qemu-timer.c | 803 ++++++++++++++++----------------------------- slirp/misc.c | 1 + tests/test-aio.c | 141 +++++++- tests/test-thread-pool.c | 3 + thread-pool.c | 1 + ui/vnc-auth-vencrypt.c | 2 +- ui/vnc-ws.c | 1 + vl.c | 4 +- 40 files changed, 736 insertions(+), 564 deletions(-)
For anyone wishing to review this series, here's a diagram showing the new relationships and a summary of this series:
http://vmsplice.net/~stefan/timerlist.jpg
TimerList is the list of QEMUTimers which are pending on a QEMUClock clock source.
Previously QEMUClock contained this list inline but the goal is to let AioContexts have their own independent timer lists. This makes timers much more thread-friendly since we don't work on global lists and callbacks are dispatched in the same thread's AioContext where they were created.
TimerListGroup holds a timerlist for each clock source ("rt", "vm", and "host"). The main loop has the default TimerListGroup. Each AioContext has its own TimerListGroup.
The other major change is that QEMU now uses the event loop's poll() timeout value instead of a separate OS-specific timer mechanism (aka the "alarm timer").
Stefan
Re: [Qemu-devel] [RFC] [PATCHv5 06/16] aio / timers: Untangle include files
In reply to Stefan HajnocziStefan,
Quoted 5 lines · --On 6 August 2013 12:10:17 +0200 Stefan Hajnoczi <[email protected]> wrote:
Fails to build
ui/vnc-auth-sasl.cPlease make sure that your ./configure output shows most optional dependencies are available (you need to install development packages for these libraries). Otherwise you will not see build breakage.
I think I have caught most of these I can test on Ubuntu Precise in my v7 patch. I enabled all options I could, and installed all the dev options I could for linux. Some things would not build, despite installing the development libraries (by 'not build' I mostly mean configure would not pick them up when passed --enable-foo with the obvious dev libraries installed). These are:
host big endian no gprof enabled no sparse enabled no strip binaries no profiler no static build no SDL support no mingw32 support no vde support no RDMA support no spice support no (/) usb net redir no libiscsi support no seccomp support no
GlusterFS support no
libssh2 support no
Signature · 1 line
Alex Bligh
Re: [Qemu-devel] [RFC] [PATCHv6 05/16] aio / timers: Split QEMUClock into QEMUClock and QEMUTimerList
In reply to Stefan HajnocziStefan,
Quoted 5 lines · --On 6 August 2013 16:29:40 +0200 Stefan Hajnoczi <[email protected]> wrote:
Quoted 3 lines
At the very least we should put the conversion to use the new API into a separate patch (possibly a separate patch set). It's fantastically intrusive.
Yes, it should be a separate patch.
OK - this separate patch is not in the v7 series not least because I want to ensure we all agree on the new API before I try to write some perl to convert the lot automagically.
Other than that, I think I have picked up everything we talked about for v7.
Signature · 1 line
Alex Bligh
Re: [Qemu-devel] [RFC] [PATCHv5 06/16] aio / timers: Untangle include files
In reply to Alex BlighQuoted 9 lines · Alex Bligh
--On 6 August 2013 12:10:17 +0200 Stefan Hajnoczi
Quoted 5 lines · <[email protected]> wrote:
Fails to build
ui/vnc-auth-sasl.cPlease make sure that your ./configure output shows most optional dependencies are available (you need to install development packages for these libraries). Otherwise you will not see build breakage.
Quoted 6 lines
I think I have caught most of these I can test on Ubuntu Precise in my v7 patch. I enabled all options I could, and installed all the dev options I could for linux. Some things would not build, despite installing the development libraries (by 'not build' I mostly mean configure would not pick them up when passed --enable-foo with the obvious dev libraries installed). These are:
Some tips on which packages to install:
Quoted 6 lines
host big endian no gprof enabled no sparse enabled no strip binaries no profiler no static build no
All the ones above are okay, fine to ignore.
Quoted 1 line
SDL support no
Do you have libsdl1.2-dev installed?
Quoted 1 line
mingw32 support no
Ok.
Quoted 1 line
vde support no
You need libvde-dev and/or libvdeplug2-dev.
Quoted 1 line
RDMA support no
Ok.
Quoted 1 line
spice support no (/)
libspice-protocol-dev and/or libspice-server-dev (can't remember).
Quoted 4 lines
usb net redir no libiscsi support no seccomp support noGlusterFS support no
Ok.
Quoted 1 line
libssh2 support no
libssh2-1-dev
Re: [Qemu-devel] [RFC] [PATCHv6 01/16] aio / timers: add qemu-timer.c utility functions
In reply to Alex BlighQuoted 25 lines · Alex Bligh
Stefan,
--On 6 August 2013 15:59:11 +0200 Stefan Hajnoczi
Quoted 20 lines · <[email protected]> wrote:
Quoted 6 lines
--On 6 August 2013 14:02:18 +0200 Stefan Hajnoczi <[email protected]> wrote:
My preference would be to move these toqemu_clock_deadline_ns(without theINT32_MAXcheck) and delete the oldqemu_clock_deadlineroutine entirely, but I don't really understand the full set of circumstances in which the qtest routines are meant to work.Okay, that's excellent. It would be great to move to a single function.
The way qtest works is that it executes QEMU in a mode that does not run guest code. Instead of running guest code it listens for commands over a socket. The wire protocol can peek/poke memory, notify of interrupts, and warp the clock.
There are test cases that use qtest to test emulated devices.
When qtest either steps the clock or sets it to a completely new value using
qtest_clock_warp()it runs allvm_clocktimers that should expire before the new time.
Quoted 6 lines
Quoted 1 line
Does this help?
Nearly :-)
How do I actually run the code (i.e. how do I test whether I've broken it)? I take it that's something different from just 'make check'?
make check includes qtest test cases like rtc-test, i440fx-test, fdc-test, and others. As long as they continue to pass all is good.
Stefan
Re: [Qemu-devel] [RFC] [PATCHv6 05/16] aio / timers: Split QEMUClock into QEMUClock and QEMUTimerList
In reply to Alex BlighQuoted 51 lines · Alex Bligh
Stefan,
Quoted 7 lines
Quoted 5 lines
I think I disagree here.
At the very least we should put the conversion to use the new API into a separate patch (possibly a separate patch set). It's fantastically intrusive.
Yes, it should be a separate patch.
...
Quoted 11 lines
Quoted 2 lines
Even if the period is just a month (i.e. the old API goes before 1.7), why break things unnecessarily?
Nothing upstream breaks. Only out-of-tree code breaks but that's life.
What's important is that upstream will be clean and easy to understand or debug. Given how undocumented the QEMU codebase is, leaving legacy API layers around just does more to confuse new contributors.
That's why I'd really like to transition now instead of leaving things in a more complex state than before.
OK. I'm just concerned about the potential fall out. If that's what everyone wants, I will do a monster patch to fix this up. Need that be part of this series? I can't help thinking it would be better to have the series applied first.
Quoted 7 lines
We end up with:
AioContext->tlganddefault_timerlistgroup.Regarding naming, I think
default_timerlistgroupshould be calledmain_loop_timerlistgroupinstead. The meaning of "default" is not obvious.I agree. I will change this.
Quoted 14 lines
Now let's think about how callers will create QEMUTimers:
1. AioContext
timer_new(ctx->tlg[QEMU_CLOCK_RT], SCALE_NS, cb, opaque);Or with a wrapper:
QEMUTimer *aio_new_timer(AioContext *ctx, QEMUClockType type, int
scale, QEMUTimerCB *cb, void *opaque){ return timer_new(ctx->tlg[type], scale, cb, opaque);}
Quoted 7 lines
Quoted 1 line
aio_new_timer(ctx, QEMU_CLOCK_RT, SCALE_NS, cb, opaque);I was actually thinking about adding that wrapper anyway.
Do you think we need to wrap
timer_mod,timer_del,timer_freeetc. to makeaio_timer_modand so forth, even though they are a straight pass through?
Those wrappers are not necessary. Once the caller has their QEMUTimer pointer they should use the QEMUTimer APIs directly.
Quoted 13 lines
Quoted 13 lines
2. main-loop
/* without legacy qemu_timer_new() */ timer_new(main_loop_tlg[QEMU_CLOCK_RT], SCALE_NS, cb, opaque);Or with a wrapper:
QEMUTimer *qemu_new_timer(QEMUClockType type, int scale, QEMUTimerCB *cb, void *opaque) { return timer_new(main_loop_tlg[type], scale, cb, opaque); }
Quoted 8 lines
Quoted 3 lines
qemu_new_timer(QEMU_CLOCK_RT, SCALE_NS, cb, opaque);Is this what you have in mind too?
Yes.
Certainly
qemu_timer_new(as opposed toqemu_new_timer) would be a good addition.
Okay, great. This makes the conversion from legacy QEMUClock functions pretty straightforward. It can be done mechanically in a single big patch that converts the tree.
Quoted 5 lines
Quoted 3 lines
But this doesn't allow for the array indexing that you do in TimerListGroup later. I didn't know that at this point in the patch series.
Yep. I'll leave that if that's OK.
Yes, I'm convinced now that the it's worth having.
Quoted 18 lines
Quoted 17 lines
Quoted 9 lines
Quoted 6 lines
qemu-timer.c line 79
Quoted 4 lines
struct QEMUTimer {
int64_texpire_time; /* in nanoseconds */qemu-timer.c +1
-1-QEMUClock *clock; +QEMUTimerList *tl;'
timer_list' is easier to read than just 'tl'.It caused a pile of line wrap issues which made the patch harder to read, so I shortened it. I can put it back if you like.
Are you sure it's the
QEMUTimer->tlfield that causes line wraps?I took a quick look and it seemed like only the QEMUTimerList *tl function argument to the deadline functions could cause line wrap. The argument variable is unrelated and can stay short since it has a very narrow context - the reader can be expected to remember the tl argument while reading the code for the function.
Quoted 3 lines
Quoted 1 line
From memory it was lots of ->tl expanding within the code the issue
rather than the arguments to functions. I'll try again. To be honest it's probably easier to change tl to
timer_listthroughout.
If you convert both that's good too.
Quoted 16 lines
Quoted 16 lines
Quoted 15 lines
Quoted 9 lines
qemu-timer.c QEMUClock *qemu_new_clock(int type) · line 302
Quoted 5 lines
qemu-timer.c +4
-0+bool qemu_clock_use_for_deadline (QEMUClock *clock) +{ +return !(use_icount && (clock->type =QEMU_CLOCK_VIRTUAL )); +}Please use doc comments (see
include/object/qom.hfor example doc comment syntax). No idea why this function is needed or what it will be used for.I will comment it, but it mostly does what it says in the tin. Per Paolo's comment, the
vm_clockshould not be used for calculation of deadlines to ppoll etc. ifuse_icountis true, because it's not actually in nanoseconds; ratherqemu_notify()oraio_notify()get called by the vm cpu thread when the relevant instruction counter is exceeded.
Quoted 6 lines
Quoted 3 lines
I didn't know that but the explanation makes sense. Definitely something that could be in a comment. Perhaps its best to introduce this small helper function in the patch that actually calls it.
It's preparation for the next patch. I will add a comment in the commit message.
Thanks!
Re: [Qemu-devel] [RFC] [PATCHv6 08/16] aio / timers: Add QEMUTimerListGroup to AioContext
In reply to Alex BlighQuoted 23 lines · Alex Bligh
--On 6 August 2013 16:45:12 +0200 Stefan Hajnoczi
Quoted 6 lines · <[email protected]> wrote:
Quoted 1 line
Because otherwise make check SEGVs after the patch.
It wasn't clear from the patch why there would be a crash. I looked deeper and
timerlistgroup_init()callsqemu_get_clock()indirectly, so we need to make sure that qemu_clocks[] is initialized to avoid a NULL pointer dereference.Actually now I recall v4 had:
(quoted diff) +6
-0@@ -215,6 +216,12 @@ AioContext *aio_context_new(void) aio_set_event_notifier(ctx, &ctx->notifier, (EventNotifierHandler *) event_notifier_test_and_clear, NULL); + /* Assert if we don't have rt_clock yet. If you see this assertion + * it means you are using AioContext without having first called + * init_clocks() in main(). + */ + assert(rt_clock); + ctx->tl = qemu_new_timerlist(rt_clock);
Quoted 3 lines
The equivalent in v7 would be an assert in
timerlist_new_from_clockto check 'clock' is non-NULL. I shall put that in as the reason for this SEGV is non-obvious.
Nice, the comment makes the SEGV clear.
Re: [Qemu-devel] [RFC] [PATCHv5 06/16] aio / timers: Untangle include files
In reply to Stefan HajnocziStefan,
Quoted 9 lines
Some tips on which packages to install:
Quoted 6 lines
host big endian no gprof enabled no sparse enabled no strip binaries no profiler no static build no
Quoted 5 lines
All the ones above are okay, fine to ignore.
Quoted 1 line
SDL support no
Do you have libsdl1.2-dev installed?
ii libsdl1.2-dev 1.2.14-6.4ubuntu3
but still doesn't work
Quoted 3 lines
Quoted 1 line
vde support no
You need libvde-dev and/or libvdeplug2-dev.
ii libvde-dev 2.2.3-3build2
but still doesn't work
Quoted 3 lines
Quoted 1 line
spice support no (/)
libspice-protocol-dev and/or libspice-server-dev (can't remember).
ii gir1.2-spice-client-glib-2.0 0.9-0ubuntu1 GObject for communicating with Spice servers (GObject-Introspection) ii gir1.2-spice-client-gtk-2.0 0.9-0ubuntu1 GTK2 widget for SPICE clients (GObject-Introspection) ii gir1.2-spice-client-gtk-3.0 0.9-0ubuntu1 GTK3 widget for SPICE clients (GObject-Introspection) ii libspice-client-glib-2.0-1 0.9-0ubuntu1 GObject for communicating with Spice servers (runtime library) ii libspice-client-glib-2.0-dev 0.9-0ubuntu1 GObject for communicating with Spice servers (development files) ii libspice-client-gtk-2.0-1 0.9-0ubuntu1 GTK2 widget for SPICE clients (runtime library) ii libspice-client-gtk-2.0-dev 0.9-0ubuntu1 GTK2 widget for SPICE clients (development files) ii libspice-client-gtk-3.0-1 0.9-0ubuntu1 GTK3 widget for SPICE clients (runtime library) ii libspice-client-gtk-3.0-dev 0.9-0ubuntu1 GTK3 widget for SPICE clients (development files) ii libspice-protocol-dev 0.10.1-1 SPICE protocol headers ii libspice-server-dev 0.10.0-1 Header files and development documentation for spice-server ii libspice-server1 0.10.0-1 Implements the server side of the SPICE protocol
but still doesn't work
Quoted 10 lines
Quoted 4 lines
usb net redir no libiscsi support no seccomp support noGlusterFS support no
Ok.
Quoted 1 line
libssh2 support no
libssh2-1-dev
I thought I'd installed that but I hadn't. I have now and it builds fine with v7.
Signature · 1 line
Alex Bligh
Re: [Qemu-devel] [RFC] [PATCHv6 01/16] aio / timers: add qemu-timer.c utility functions
In reply to Stefan HajnocziQuoted 5 lines · --On 7 August 2013 09:21:17 +0200 Stefan Hajnoczi <[email protected]> wrote:
Quoted 2 lines
How do I actually run the code (i.e. how do I test whether I've broken it)? I take it that's something different from just 'make check'?
make check includes qtest test cases like rtc-test, i440fx-test, fdc-test, and others. As long as they continue to pass all is good.
Oh great. We're fine then.
Signature · 1 line
Alex Bligh
Re: [Qemu-devel] [RFC] [PATCHv6 05/16] aio / timers: Split QEMUClock into QEMUClock and QEMUTimerList
In reply to Stefan HajnocziQuoted 6 lines · --On 7 August 2013 09:27:36 +0200 Stefan Hajnoczi <[email protected]> wrote:
Quoted 3 lines
Do you think we need to wrap
timer_mod,timer_del,timer_freeetc. to makeaio_timer_modand so forth, even though they are a straight pass through?Those wrappers are not necessary. Once the caller has their QEMUTimer pointer they should use the QEMUTimer APIs directly.
Good. That's the approach I took in v7. I'm progressing well with a perl program to do automatic conversion.
Signature · 1 line
Alex Bligh
Re: [Qemu-devel] [RFC] [PATCHv6 00/16] aio / timers: Add AioContext timers and use ppoll
In reply to Stefan HajnocziQuoted 7 lines
For anyone wishing to review this series, here's a diagram showing the new relationships and a summary of this series:
http://vmsplice.net/~stefan/timerlist.jpg
TimerList is the list of QEMUTimers which are pending on a QEMUClock clock source.
I just started reviewing this and the diagram may be different in v7, anyway: why do you need a default TimerListGroup? I would have thought it is enough to use the default AioContext's own TLG.
Paolo
Quoted 16 lines
Previously QEMUClock contained this list inline but the goal is to let AioContexts have their own independent timer lists. This makes timers much more thread-friendly since we don't work on global lists and callbacks are dispatched in the same thread's AioContext where they were created.
TimerListGroup holds a timerlist for each clock source ("rt", "vm", and "host"). The main loop has the default TimerListGroup. Each AioContext has its own TimerListGroup.
The other major change is that QEMU now uses the event loop's
poll()timeout value instead of a separate OS-specific timer mechanism (aka the "alarm timer").Stefan
Re: [Qemu-devel] [RFC] [PATCHv6 00/16] aio / timers: Add AioContext timers and use ppoll
Paolo,
Quoted 10 lines · --On 7 August 2013 11:43:58 -0400 Paolo Bonzini <[email protected]> wrote:
Quoted 7 lines
For anyone wishing to review this series, here's a diagram showing the new relationships and a summary of this series:
http://vmsplice.net/~stefan/timerlist.jpg
TimerList is the list of QEMUTimers which are pending on a QEMUClock clock source.
I just started reviewing this and the diagram may be different in v7,
No the data structures are the same, just bits renamed.
Quoted 2 lines
anyway: why do you need a default TimerListGroup? I would have thought it is enough to use the default AioContext's own TLG.
The default TimerListGroup has been renamed main_loop_tlg (or similar) in order to make this clearer.
Right now, we have a peculiar double set of polling and waiting, an inner one in AioContext and an outer one in main_loop. Existing timer users may not be safe to be called within the inner AioContext and may expect to be run only from main_loop. Also, I believe there are some binaries that don't even have an AioContext at the moment, or may not have them at all times when timers are needed.
I understand this may be simplified in the future, in which case if we always (in every binary) have a default AioContext AND it exists early enough, we can remove the main_loop_tlg and make the qemu_ functions uses the default AioContext. That's far from a huge change but I'd rather not do it is part of this series as I suspect there is the risk of breakage.
Signature · 1 line
Alex Bligh
Re: [Qemu-devel] [RFC] [PATCHv5 06/16] aio / timers: Untangle include files
In reply to Alex BlighQuoted 56 lines · Alex Bligh
Quoted 9 lines
Some tips on which packages to install:
Quoted 6 lines
host big endian no gprof enabled no sparse enabled no strip binaries no profiler no static build noQuoted 5 lines
All the ones above are okay, fine to ignore.
Quoted 1 line
SDL support no
Do you have libsdl1.2-dev installed?
ii libsdl1.2-dev 1.2.14-6.4ubuntu3
but still doesn't work
Quoted 3 lines
Quoted 1 line
vde support no
You need libvde-dev and/or libvdeplug2-dev.
ii libvde-dev 2.2.3-3build2
but still doesn't work
Quoted 3 lines
Quoted 1 line
spice support no (/)
libspice-protocol-dev and/or libspice-server-dev (can't remember).
ii gir1.2-spice-client-glib-2.0 0.9-0ubuntu1 GObject
for communicating with Spice servers (GObject-Introspection)
ii gir1.2-spice-client-gtk-2.0 0.9-0ubuntu1 GTK2
widget for SPICE clients (GObject-Introspection)
ii gir1.2-spice-client-gtk-3.0 0.9-0ubuntu1 GTK3
widget for SPICE clients (GObject-Introspection)
ii libspice-client-glib-2.0-1 0.9-0ubuntu1 GObject
for communicating with Spice servers (runtime library)
ii libspice-client-glib-2.0-dev 0.9-0ubuntu1 GObject
for communicating with Spice servers (development files)
ii libspice-client-gtk-2.0-1 0.9-0ubuntu1 GTK2
widget for SPICE clients (runtime library)
ii libspice-client-gtk-2.0-dev 0.9-0ubuntu1 GTK2
widget for SPICE clients (development files)
ii libspice-client-gtk-3.0-1 0.9-0ubuntu1 GTK3
widget for SPICE clients (runtime library)
ii libspice-client-gtk-3.0-dev 0.9-0ubuntu1 GTK3
widget for SPICE clients (development files)
ii libspice-protocol-dev 0.10.1-1 SPICE
protocol headers
ii libspice-server-dev 0.10.0-1 Header files
and development documentation for spice-server
ii libspice-server1 0.10.0-1 Implements
the server side of the SPICE protocol
Quoted 1 line
but still doesn't work
Spice can be tricky because you need a sufficiently new version of the headers.
In general the process of troubleshooting this is to run ./configure and then chase up the unexpected "no" results in config.log where you can see the compile test that failed.
Stefan