aio / timers: Add AioContext timers and use ppoll
Subsystems (5)
v7 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 18 in relation to a possible bug in cpus.c.
Changes since v6:
* Fix build failure in vnc-auth-sasl.c
* Split first patch into 3
* Add assert on timerlist_free
* Fix ==/= error on qemu_clock_use_for_deadline
* Remove unnecessary cast in aio_timerlist_notify
* Fix bad deadline comparison in aio_ctx_check
* Add assert to timerlist_new_from_clock to check init_clocks
* Use timer_list not tl
* Change default_timerlistgroup to main_loop_timerlistgroup
* Add comment on commit for qemu_clock_use_for_deadline
* Fixed various include file issues
* Convert *_has_timers and *_has_expired to return bool
* Make loop variable consistent when looping through clock types
* Add documentation to existing qemu_timer calls
* Remove qemu_clock_deadline and move to qemu_clock_deadline_ns
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 (22):
aio / timers: Add qemu_clock_free and expose qemu_clock_new and clock
types
aio / timers: add qemu-timer.c utility functions
aio / timers: Consistent treatment of disabled clocks for deadlines 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: Add aio_timer_new wrapper
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: Introduce new API qemu_timer_new and friends aio / timers: Use all timerlists in icount warp calculations aio / timers: Add documentation and new format calls
aio / timers: Remove alarm timers
aio / timers: Add test harness for AioContext timers
aio / timers: Remove legacy qemu_clock_deadline &
qemu_timerlist_deadline
aio-posix.c | 20 +- aio-win32.c | 22 +- async.c | 20 +- configure | 37 ++ cpus.c | 46 ++- dma-helpers.c | 1 + hw/dma/xilinx_axidma.c | 1 + hw/timer/arm_timer.c | 1 + hw/timer/exynos4210_mct.c | 1 + hw/timer/exynos4210_pwm.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/sh_timer.c | 1 + hw/timer/slavio_timer.c | 1 + hw/timer/xilinx_timer.c | 1 + hw/tpm/tpm_tis.c | 1 + hw/usb/hcd-uhci.c | 1 + include/block/aio.h | 23 ++ include/block/block_int.h | 1 + include/block/coroutine.h | 2 + include/qemu/timer.h | 857 +++++++++++++++++++++++++++++++++++++++++++-- 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 +++++++++++++++--------------------------- qtest.c | 2 +- slirp/misc.c | 1 + tests/test-aio.c | 144 +++++++- tests/test-thread-pool.c | 3 + thread-pool.c | 1 + ui/vnc-auth-sasl.h | 1 + ui/vnc-auth-vencrypt.c | 2 +- ui/vnc-ws.c | 1 + vl.c | 4 +- 46 files changed, 1480 insertions(+), 586 deletions(-)
Signature · 1 line
1.7.9.5
RFC PATCH v7 1/22 aio / timers: Add qemu_clock_free and expose qemu_clock_new and clock types
Add qemu_clock_free and expose qemu_new_clock as qemu_clock_new.
Expose clock types.
--- include/qemu/timer.h | 22 ++++++++++++++++++++++ qemu-timer.c | 11 ++++++----- 2 files changed, 28 insertions(+), 5 deletions(-)
include/qemu/timer.h +22 -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,6 +36,24 @@ extern QEMUClock *vm_clock;the virtual clock. */ extern QEMUClock *host_clock; +/** +* qemu_clock_new: +* @type: the type of clock to be created +* +* Create a new clock of type @type +* +* Returns: A pointer to the clock object +*/ +QEMUClock *qemu_clock_new (int type); + +/** +* qemu_clock_free: +* @clock: the clock to free +* +* Free clock object @clock +*/ +void qemu_clock_free (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);
qemu-timer.c +6 -5
@@ -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_clock_new (int type) {QEMUClock *clock;
@@ -243,6 +239,11 @@ static QEMUClock *qemu_new_clock(int type)return clock; } +void qemu_clock_free (QEMUClock *clock) +{ +g_free (clock); +} +void qemu_clock_enable (QEMUClock *clock,bool enabled) {bool old = clock->enabled;
Signature · 1 line
1.7.9.5
Re: [Qemu-devel] [RFC] [PATCHv7 01/22] aio / timers: Add qemu_clock_free and expose qemu_clock_new and clock types
Quoted 9 lines · Alex Bligh
Add
qemu_clock_freeand exposeqemu_new_clockasqemu_clock_new.Expose clock types.
Signed-off-by Alex Bligh <[email protected]>--- include/qemu/timer.h | 22 ++++++++++++++++++++++ qemu-timer.c | 11 ++++++----- 2 files changed, 28 insertions(+), 5 deletions(-)
Regarding doc comments: if the function is self-documenting then the doc comment might not add much. I think in those cases it's okay to omit details about arguments or return values if they are clear from the function prototype.
Re: [Qemu-devel] [RFC] [PATCHv7 01/22] aio / timers: Add qemu_clock_free and expose qemu_clock_new and clock types
Quoted 4 lines · --On 7 August 2013 13:31:07 +0200 Stefan Hajnoczi <[email protected]> wrote:
Regarding doc comments: if the function is self-documenting then the doc comment might not add much. I think in those cases it's okay to omit details about arguments or return values if they are clear from the function prototype.
Given I've done them for every function, I hope they don't cause any active harm. I was concerned that with an API change some things that one might have thought were obvious are in fact not obvious (e.g. timer_del vs. timer_free).
Signature · 1 line
Alex Bligh
Re: [Qemu-devel] [RFC] [PATCHv7 01/22] aio / timers: Add qemu_clock_free and expose qemu_clock_new and clock types
Quoted 9 lines · Alex Bligh
--On 7 August 2013 13:31:07 +0200 Stefan Hajnoczi
Quoted 4 lines · <[email protected]> wrote:
Regarding doc comments: if the function is self-documenting then the doc comment might not add much. I think in those cases it's okay to omit details about arguments or return values if they are clear from the function prototype.
Quoted 4 lines
Given I've done them for every function, I hope they don't cause any active harm. I was concerned that with an API change some things that one might have thought were obvious are in fact not obvious (e.g.
timer_delvs.timer_free).
No, they definitely do not harm.
Paolo
Re: [Qemu-devel] [RFC] [PATCHv7 01/22] aio / timers: Add qemu_clock_free and expose qemu_clock_new and clock types
In reply to Alex BlighOn Wed, Aug 07, 2013 at 12:48:55AM +0100, Alex Bligh wrote:
I forgot to mention that there's no need to change the doc comments you've already written. I just wanted to share that to save you work in the future.
Re: [Qemu-devel] [RFC] [PATCHv7 01/22] aio / timers: Add qemu_clock_free and expose qemu_clock_new and clock types
In reply to Alex BlighQuoted 36 lines · Alex Bligh
Add
qemu_clock_freeand exposeqemu_new_clockasqemu_clock_new.Expose clock types.
Signed-off-by Alex Bligh <[email protected]>--- include/qemu/timer.h | 22 ++++++++++++++++++++++ qemu-timer.c | 11 ++++++----- 2 files changed, 28 insertions(+), 5 deletions(-)include/qemu/timer.h +11
-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,6 +36,24 @@ extern QEMUClock *vm_clock;the virtual clock. */ extern QEMUClock *host_clock; +/** +* qemu_clock_new: +* @type: the type of clock to be created +* +* Create a new clock of type @type +* +* Returns: A pointer to the clock object
include/qemu/timer.h extern QEMUClock *vm_clock; · line 55
Quoted 10 lines
include/qemu/timer.h +10
-0+*/ +QEMUClock *qemu_clock_new (int type); + +/** +* qemu_clock_free: +* @clock: the clock to free +* +* Free clock object @clock +*/ +void qemu_clock_free (QEMUClock *clock);
Go through this series, I can not find the caller of it. I think as clock source, the QEMUClock can not be destroyed, except exit(2), so could we let exit to reclaim the resource? Or at least, this interface should not be public.
Quoted 43 lines
include/qemu/timer.h +1
-0+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 +6
-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_clock_new (int type) {QEMUClock *clock;@@ -243,6 +239,11 @@ static QEMUClock *qemu_new_clock(int type)return clock; } +void qemu_clock_free (QEMUClock *clock) +{ +g_free (clock); +} +void qemu_clock_enable (QEMUClock *clock,bool enabled) {bool old = clock->enabled; --1.7.9.5
Re: [Qemu-devel] [RFC] [PATCHv7 01/22] aio / timers: Add qemu_clock_free and expose qemu_clock_new and clock types
Pingfan,
Quoted 4 lines · --On 8 August 2013 14:21:34 +0800 liu ping fan <[email protected]> wrote:
Go through this series, I can not find the caller of it. I think as clock source, the QEMUClock can not be destroyed, except exit(2), so could we let exit to reclaim the resource? Or at least, this interface should not be public.
Yes I think this a holdover from when I allowed multiple qemu clocks to avoid changing the API.
Signature · 1 line
Alex Bligh
RFC PATCH v7 2/22 aio / timers: add qemu-timer.c utility functions
In reply to Alex BlighAdd 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 | 42 ++++++++++++++++++++++++++++++++++++++++++ qemu-timer.c | 50 ++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 92 insertions(+)
include/qemu/timer.h +42 -0
@@ -58,6 +58,29 @@ 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); + +/** +* qemu_clock_deadline_ns: +* @clock: the clock to operate on +* +* Calculate the timeout of the earliest expiring timer +* in nanoseconds, or -1 if no timer is set to expire. +* +* Returns: time until expiry in nanoseconds or -1 +*/ +int64_t qemu_clock_deadline_ns (QEMUClock *clock); + +/** +* qemu_timeout_ns_to_ms: +* @ns: nanosecond timeout value +* +* Convert a nanosecond timeout value (or -1) to +* a millisecond value (or -1), always rounding up. +* +* Returns: millisecond timeout value +*/ +int qemu_timeout_ns_to_ms (int64_t ns); +void qemu_clock_enable (QEMUClock *clock,bool enabled);void qemu_clock_warp (QEMUClock *clock);
@@ -85,6 +108,25 @@ int64_t cpu_get_ticks(void);void cpu_enable_ticks (void );void cpu_disable_ticks (void ); +/** +* qemu_soonest_timeout: +* @timeout1: first timeout in nanoseconds (or -1 for infinite) +* @timeout2: second timeout in nanoseconds (or -1 for infinite) +* +* Calculates the soonest of two timeout values. -1 means infinite, which +* is later than any other value. +* +* Returns: soonest timeout value in nanoseconds (or -1 for infinite) +*/ +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 +50 -0
56 lines not on this page · raw message
Signature · 1 line
1.7.9.5
RFC PATCH v7 3/22 aio / timers: Consistent treatment of disabled clocks for deadlines
In reply to Alex BlighMake treatment of disabled clocks consistent in deadline calculation
--- qemu-timer.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
qemu-timer.c +1 -1
@@ -269,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 ) {
Signature · 1 line
1.7.9.5
RFC PATCH v7 4/22 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 | 12 ++++++++++++ qemu-timer.c | 24 ++++++++++++++++++++++++ 3 files changed, 55 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 +12 -0
@@ -81,6 +81,18 @@ int64_t qemu_clock_deadline_ns(QEMUClock *clock);*/ int qemu_timeout_ns_to_ms (int64_t ns); +/** +* qemu_poll_ns: +* @fds: Array of file descriptors +* @nfds: number of file descriptors +* @timeout: timeout in nanoseconds +* +* Perform a poll like g_poll but with a timeout in nanoseconds. +* See g_poll documentation for further details. +* +* Returns: number of fds ready +*/ +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 v7 5/22 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.
--- qemu-timer.c | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-)
qemu-timer.c +10 -3
@@ -41,6 +41,10 @@#include <poll.h> #endif +#ifdef CONFIG_PRCTL_PR_SET_TIMERSLACK +#include <sys/prctl.h> +#endif +/***********************************************************/ /* timers */
@@ -508,10 +512,13 @@ void qemu_unregister_clock_reset_notifier(QEMUClock *clock, Notifier *notifier)void init_clocks (void ) {if (!rt_clock) { - rt_clock =qemu_new_clock (QEMU_CLOCK_REALTIME ); - vm_clock =qemu_new_clock (QEMU_CLOCK_VIRTUAL ); - host_clock =qemu_new_clock (QEMU_CLOCK_HOST ); + rt_clock =qemu_clock_new (QEMU_CLOCK_REALTIME ); + vm_clock =qemu_clock_new (QEMU_CLOCK_VIRTUAL ); + host_clock =qemu_clock_new (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 v7 6/22 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 | 21 +++++++++++++++++++-- qemu-timer.c | 18 ++++++++++++------ 2 files changed, 31 insertions(+), 8 deletions(-)
include/qemu/timer.h +19 -2
@@ -110,8 +110,25 @@ 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 ); +/** +* qemu_run_timers: +* @clock: clock on which to operate +* +* Run all the timers associated with a clock. +* +* Returns: true if any timer ran. +*/ +bool qemu_run_timers (QEMUClock *clock); + +/** +* qemu_run_all_timers: +* +* Run all the timers associated with every clock. +* +* Returns: true if any timer ran. +*/ +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 v7 7/22 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.
Add qemu_clock_use_for_deadline to indicate whether a particular clock should be used for deadline calculations. When use_icount is true, vm_clock should not be used for deadline calculations as it does not contain a nanosecond count. Instead, icount timeouts come from the execution thread doing aio_notify or qemu_notify as appropriate. This function is used in the next patch.
--- include/qemu/timer.h | 408 +++++++++++++++++++++++++++++++++++++++++++++++--- qemu-timer.c | 201 +++++++++++++++++++------ 2 files changed, 538 insertions(+), 71 deletions(-)
include/qemu/timer.h +384 -24
@@ -11,30 +11,71 @@#define SCALE_US 1000#define SCALE_NS 1 -#define QEMU_CLOCK_REALTIME 0 -#define QEMU_CLOCK_VIRTUAL 1 -#define QEMU_CLOCK_HOST 2 +/** +* QEMUClockType: +* +* The following clock types are available: +* +* @QEMU_CLOCK_REALTIME: Real time clock +* +* 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. +* +* Formerly rt_clock +* +* @QEMU_CLOCK_VIRTUAL: virtual clock +* +* 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). +* +* Formerly vm_clock +* +* @QEMU_CLOCK_HOST: host clock +* +* 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. +* +* Formerly host_clock +*/ + +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); -/* 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; +extern QEMUClock *qemu_clocks[QEMU_CLOCK_MAX ]; -/* 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; +/** +* qemu_get_clock: +* @type: type of clock +* +* Translate a clock type into a pointer to QEMUClock object. +* +* Returns: a pointer to the QEMUClock object +*/ +static inline QEMUClock *qemu_get_clock (QEMUClockType type) +{ +return qemu_clocks[type]; +} -/* 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; +/* These three clocks are maintained here with separate variable +* names for compatibility only. +*/ +#define rt_clock (qemu_get_clock(QEMU_CLOCK_REALTIME)) +#define vm_clock (qemu_get_clock(QEMU_CLOCK_VIRTUAL)) +#define host_clock (qemu_get_clock(QEMU_CLOCK_HOST))/** * qemu_clock_new:
@@ -44,7 +85,7 @@ extern QEMUClock *host_clock;* * Returns: A pointer to the clock object */ -QEMUClock *qemu_clock_new (int type); +QEMUClock *qemu_clock_new (QEMUClockType type);/** * qemu_clock_free:
@@ -55,8 +96,8 @@ QEMUClock *qemu_clock_new(int type);void qemu_clock_free (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); +bool qemu_clock_has_timers (QEMUClock *clock); +bool qemu_clock_expired (QEMUClock *clock);int64_t qemu_clock_deadline (QEMUClock *clock);/**
@@ -71,6 +112,124 @@ int64_t qemu_clock_deadline(QEMUClock *clock);int64_t qemu_clock_deadline_ns (QEMUClock *clock);/** +* qemu_clock_use_for_deadline: +* @clock: the clock to operate on +* +* Determine whether a clock should be used for deadline +* calculations. Some clocks, for instance vm_clock with +* use_icount set, do not count in nanoseconds. Such clocks +* are not used for deadline calculations, and are presumed +* to interrupt any poll using qemu_notify/aio_notify +* etc. +* +* Returns: true if the clock runs in nanoseconds and +* should be used for a deadline. +*/ +bool qemu_clock_use_for_deadline (QEMUClock *clock); + +/** +* qemu_clock_get_default_timerlist: +* @clock: the clock to operate on +* +* Return the default timer list assocatiated with a clock. +* +* Returns: the default timer list +*/ +QEMUTimerList *qemu_clock_get_default_timerlist (QEMUClock *clock); + +/** +* timerlist_new: +* @type: the clock type to associate with the timerlist +* +* Create a new timerlist associated with the clock of +* type @type. +* +* Returns: a pointer to the QEMUTimerList created +*/ +QEMUTimerList *timerlist_new (QEMUClockType type); + +/** +* timerlist_free: +* @timer_list: the timer list to free +* +* Frees a timer_list. It must have no active timers. +*/ +void timerlist_free (QEMUTimerList *timer_list); + +/** +* timerlist_has_timers: +* @timer_list: the timer list to operate on +* +* Determine whether a timer list has active timers +* +* Returns: true if the timer list has timers. +*/ +bool timerlist_has_timers (QEMUTimerList *timer_list); + +/** +* timerlist_expired: +* @timer_list: the timer list to operate on +* +* Determine whether a timer list has any timers which +* are expired. +* +* Returns: true if the timer list has timers which +* have expired. +*/ +bool timerlist_expired (QEMUTimerList *timer_list); + +/** +* timerlist_deadline: +* @timer_list: the timer list to operate on +* +* Determine the deadline for a timer_list. This is +* a legacy function which returns INT32_MAX if the +* timer list has no timers or if the earliest timer +* expires later than INT32_MAX nanoseconds away. +* +* Returns: the number of nanoseconds until the earliest +* timer expires or INT32_MAX in the situations listed +* above +*/ +int64_t timerlist_deadline (QEMUTimerList *timer_list); + +/** +* timerlist_deadline_ns: +* @timer_list: the timer list to operate on +* +* Determine the deadline for a timer_list, i.e. +* the number of nanoseconds until the first timer +* expires. Return -1 if there are no timers. +* +* Returns: the number of nanoseconds until the earliest +* timer expires -1 if none +*/ +int64_t timerlist_deadline_ns (QEMUTimerList *timer_list); + +/** +* timerlist_getclock: +* @timer_list: the timer list to operate on +* +* Read the clock value associated with a timer list. +* The clock value is normally in nanoseconds, but may +* not be in some instances (e.g. vm_clock with use_icount). +* +* Returns: the value of the clock associated with the +* timer list. +*/ +QEMUClock *timerlist_get_clock (QEMUTimerList *timer_list); + +/** +* timerlist_run_timers: +* @timer_list: the timer list to use +* +* Call all expired timers associated with the timer list. +* +* Returns: true if any timer expired +*/ +bool timerlist_run_timers (QEMUTimerList *timer_list); + +/** * qemu_timeout_ns_to_ms: * @ns: nanosecond timeout value *
@@ -102,6 +261,21 @@ void qemu_unregister_clock_reset_notifier(QEMUClock *clock,QEMUTimer *qemu_new_timer (QEMUClock *clock,int scale,QEMUTimerCB *cb,void *opaque); + +/** +* timer_new: +* @timer_list: the timer list to attach the timer to +* @scale: the scale value for the tiemr +* @cb: the callback to be called when the timer expires +* @opaque: the opaque pointer to be passed to the callback +* +* Creeate a new timer and associate it with @timer_list. +* +* Returns: a pointer to the timer +*/ +QEMUTimer *timer_new (QEMUTimerList *timer_list,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);
@@ -110,11 +284,101 @@ 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 */ + +/** +* timer_free: +* @ts: the timer +* +* Free a timer (it must not be on the active list) +*/ +static inline void timer_free (QEMUTimer *ts) +{ +qemu_free_timer (ts); +} + +/** +* timer_del: +* @ts: the timer +* +* Delete a timer from the active list. +*/ +static inline void timer_del (QEMUTimer *ts) +{ +qemu_del_timer (ts); +} + +/** +* timer_mod_ns: +* @ts: the timer +* @expire_time: the expiry time in nanoseconds +* +* Modify a timer to expire at @expire_time +*/ +static inline void timer_mod_ns (QEMUTimer *ts,int64_t expire_time) +{ +qemu_mod_timer_ns (ts, expire_time); +} + +/** +* timer_mod: +* @ts: the timer +* @expire_time: the expire time in the units associated with the timer +* +* Modify a timer to expiry at @expire_time, taking into +* account the scale associated with the timer. +*/ +static inline void timer_mod (QEMUTimer *ts,int64_t expire_timer) +{ +qemu_mod_timer (ts, expire_timer); +} + +/** +* timer_pending: +* @ts: the timer +* +* Determines whether a timer is pending (i.e. is on the +* active list of timers, whether or not it has not yet expired). +* +* Returns: true if the timer is pending +*/ +static inline bool timer_pending (QEMUTimer *ts) +{ +return qemu_timer_pending (ts); +} + +/** +* timer_expired: +* @ts: the timer +* +* Determines whether a timer has expired. +* +* Returns: true if the timer has expired +*/ +static inline bool timer_expired (QEMUTimer *timer_head,int64_t current_time) +{ +return qemu_timer_expired (timer_head, current_time); +} + +/** +* timer_expire_time_ns: +* @ts: the timer +* +* Determine the expiry time of a timer +* +* Returns: the expiry time in nanoseconds +*/ +static inline uint64_t timer_expire_time_ns (QEMUTimer *ts) +{ +return qemu_timer_expire_time_ns (ts); +} +/** * qemu_run_timers: * @clock: clock on which to operate * -* Run all the timers associated with a clock. +* Run all the timers associated with the default timer list +* of a clock. * * Returns: true if any timer ran. */
@@ -123,7 +387,8 @@ bool qemu_run_timers(QEMUClock *clock);/** * qemu_run_all_timers: * -* Run all the timers associated with every clock. +* Run all the timers associated with the default timer list +* of every clock. * * Returns: true if any timer ran. */
@@ -156,18 +421,113 @@ static inline int64_t qemu_soonest_timeout(int64_t timeout1, int64_t timeout2)return ((uint64_t ) timeout1 < (uint64_t ) timeout2) ? timeout1 : timeout2; } +/** +* qemu_new_timer_ns: +* @clock: the clock to associate with the timer +* @callback: the callback to call when the timer expires +* @opaque: the opaque pointer to pass to the callback +* +* Create a new timer with nanosecond scale on the default timer list +* associated with the clock. +* +* Returns: a pointer to the newly created timer +*/ static inline QEMUTimer *qemu_new_timer_ns (QEMUClock *clock,QEMUTimerCB *cb,void *opaque) {return qemu_new_timer (clock,SCALE_NS , cb, opaque); } -static inline QEMUTimer *qemu_new_timer_ms (QEMUClock *clock,QEMUTimerCB *cb, +/** +* timer_new_ns: +* @timer_list: the timer list to associate with the timer +* @callback: the callback to call when the timer expires +* @opaque: the opaque pointer to pass to the callback +* +* Create a new timer with nanosecond scale on the timer list +* specified. +* +* Returns: a pointer to the newly created timer +*/ +static inline QEMUTimer *timer_new_ns (QEMUTimerList *timer_list, +QEMUTimerCB *cb, +void *opaque) +{ +return timer_new (timer_list,SCALE_NS , cb, opaque); +} + +/** +* qemu_new_timer_us: +* @clock: the clock to associate with the timer +* @callback: the callback to call when the timer expires +* @opaque: the opaque pointer to pass to the callback +* +* Create a new timer with microsecond scale on the default timer list +* associated with the clock. +* +* Returns: a pointer to the newly created timer +*/ +static inline QEMUTimer *qemu_new_timer_us (QEMUClock *clock, +QEMUTimerCB *cb, +void *opaque) +{ +return qemu_new_timer (clock,SCALE_US , cb, opaque); +} + +/** +* timer_new_us: +* @timer_list: the timer list to associate with the timer +* @callback: the callback to call when the timer expires +* @opaque: the opaque pointer to pass to the callback +* +* Create a new timer with microsecond scale on the timer list +* specified. +* +* Returns: a pointer to the newly created timer +*/ +static inline QEMUTimer *timer_new_us (QEMUTimerList *timer_list, +QEMUTimerCB *cb, +void *opaque) +{ +return timer_new (timer_list,SCALE_US , cb, opaque); +} + +/** +* qemu_new_timer_ms: +* @clock: the clock to associate with the timer +* @callback: the callback to call when the timer expires +* @opaque: the opaque pointer to pass to the callback +* +* Create a new timer with millisecond scale on the default timer list +* associated with the clock. +* +* Returns: a pointer to the newly created timer +*/ +static inline QEMUTimer *qemu_new_timer_ms (QEMUClock *clock, +QEMUTimerCB *cb,void *opaque) {return qemu_new_timer (clock,SCALE_MS , cb, opaque); } +/** +* timer_new_ms: +* @timer_list: the timer list to associate with the timer +* @callback: the callback to call when the timer expires +* @opaque: the opaque pointer to pass to the callback +* +* Create a new timer with millisecond scale on the timer list +* specified. +* +* Returns: a pointer to the newly created timer +*/ +static inline QEMUTimer *timer_new_ms (QEMUTimerList *timer_list, +QEMUTimerCB *cb, +void *opaque) +{ +return timer_new (timer_list,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 +154 -47
369 lines not on this page · raw message
Signature · 1 line
1.7.9.5
Re: [Qemu-devel] [RFC] [PATCHv7 07/22] aio / timers: Split QEMUClock into QEMUClock and QEMUTimerList
Quoted 9 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.
Quoted 7 lines
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
I think that as clock event source, QemuClock can not be created arbitrary, i.e only three instance allowed. So what about comment "Fold *_clock into qemu_clocks[]"
Quoted 897 lines
fix
qemu_next_alarm_deadlineas it's going to be deleted.Add
qemu_clock_use_for_deadlineto indicate whether a particular clock should be used for deadline calculations. Whenuse_icountis true,vm_clockshould not be used for deadline calculations as it does not contain a nanosecond count. Instead, icount timeouts come from the execution thread doingaio_notifyorqemu_notifyas appropriate. This function is used in the next patch.Signed-off-by Alex Bligh <[email protected]>--- include/qemu/timer.h | 408 +++++++++++++++++++++++++++++++++++++++++++++++--- qemu-timer.c | 201 +++++++++++++++++++------ 2 files changed, 538 insertions(+), 71 deletions(-)include/qemu/timer.h +384
-24@@ -11,30 +11,71 @@#define SCALE_US 1000#define SCALE_NS 1 -#define QEMU_CLOCK_REALTIME 0 -#define QEMU_CLOCK_VIRTUAL 1 -#define QEMU_CLOCK_HOST 2 +/** +* QEMUClockType: +* +* The following clock types are available: +* +* @QEMU_CLOCK_REALTIME: Real time clock +* +* 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. +* +* Formerly rt_clock +* +* @QEMU_CLOCK_VIRTUAL: virtual clock +* +* 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). +* +* Formerly vm_clock +* +* @QEMU_CLOCK_HOST: host clock +* +* 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. +* +* Formerly host_clock +*/ + +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); -/* 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; +extern QEMUClock *qemu_clocks[QEMU_CLOCK_MAX ]; -/* 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; +/** +* qemu_get_clock: +* @type: type of clock +* +* Translate a clock type into a pointer to QEMUClock object. +* +* Returns: a pointer to the QEMUClock object +*/ +static inline QEMUClock *qemu_get_clock (QEMUClockType type) +{ +return qemu_clocks[type]; +} -/* 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; +/* These three clocks are maintained here with separate variable +* names for compatibility only. +*/ +#define rt_clock (qemu_get_clock(QEMU_CLOCK_REALTIME)) +#define vm_clock (qemu_get_clock(QEMU_CLOCK_VIRTUAL)) +#define host_clock (qemu_get_clock(QEMU_CLOCK_HOST))/** * qemu_clock_new: @@ -44,7 +85,7 @@ extern QEMUClock *host_clock;* * Returns: A pointer to the clock object */ -QEMUClock *qemu_clock_new (int type); +QEMUClock *qemu_clock_new (QEMUClockType type);/** * qemu_clock_free: @@ -55,8 +96,8 @@ QEMUClock *qemu_clock_new(int type);void qemu_clock_free (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); +bool qemu_clock_has_timers (QEMUClock *clock); +bool qemu_clock_expired (QEMUClock *clock);int64_t qemu_clock_deadline (QEMUClock *clock);/** @@ -71,6 +112,124 @@ int64_t qemu_clock_deadline(QEMUClock *clock);int64_t qemu_clock_deadline_ns (QEMUClock *clock);/** +* qemu_clock_use_for_deadline: +* @clock: the clock to operate on +* +* Determine whether a clock should be used for deadline +* calculations. Some clocks, for instance vm_clock with +* use_icount set, do not count in nanoseconds. Such clocks +* are not used for deadline calculations, and are presumed +* to interrupt any poll using qemu_notify/aio_notify +* etc. +* +* Returns: true if the clock runs in nanoseconds and +* should be used for a deadline. +*/ +bool qemu_clock_use_for_deadline (QEMUClock *clock); + +/** +* qemu_clock_get_default_timerlist: +* @clock: the clock to operate on +* +* Return the default timer list assocatiated with a clock. +* +* Returns: the default timer list +*/ +QEMUTimerList *qemu_clock_get_default_timerlist (QEMUClock *clock); + +/** +* timerlist_new: +* @type: the clock type to associate with the timerlist +* +* Create a new timerlist associated with the clock of +* type @type. +* +* Returns: a pointer to the QEMUTimerList created +*/ +QEMUTimerList *timerlist_new (QEMUClockType type); + +/** +* timerlist_free: +* @timer_list: the timer list to free +* +* Frees a timer_list. It must have no active timers. +*/ +void timerlist_free (QEMUTimerList *timer_list); + +/** +* timerlist_has_timers: +* @timer_list: the timer list to operate on +* +* Determine whether a timer list has active timers +* +* Returns: true if the timer list has timers. +*/ +bool timerlist_has_timers (QEMUTimerList *timer_list); + +/** +* timerlist_expired: +* @timer_list: the timer list to operate on +* +* Determine whether a timer list has any timers which +* are expired. +* +* Returns: true if the timer list has timers which +* have expired. +*/ +bool timerlist_expired (QEMUTimerList *timer_list); + +/** +* timerlist_deadline: +* @timer_list: the timer list to operate on +* +* Determine the deadline for a timer_list. This is +* a legacy function which returns INT32_MAX if the +* timer list has no timers or if the earliest timer +* expires later than INT32_MAX nanoseconds away. +* +* Returns: the number of nanoseconds until the earliest +* timer expires or INT32_MAX in the situations listed +* above +*/ +int64_t timerlist_deadline (QEMUTimerList *timer_list); + +/** +* timerlist_deadline_ns: +* @timer_list: the timer list to operate on +* +* Determine the deadline for a timer_list, i.e. +* the number of nanoseconds until the first timer +* expires. Return -1 if there are no timers. +* +* Returns: the number of nanoseconds until the earliest +* timer expires -1 if none +*/ +int64_t timerlist_deadline_ns (QEMUTimerList *timer_list); + +/** +* timerlist_getclock: +* @timer_list: the timer list to operate on +* +* Read the clock value associated with a timer list. +* The clock value is normally in nanoseconds, but may +* not be in some instances (e.g. vm_clock with use_icount). +* +* Returns: the value of the clock associated with the +* timer list. +*/ +QEMUClock *timerlist_get_clock (QEMUTimerList *timer_list); + +/** +* timerlist_run_timers: +* @timer_list: the timer list to use +* +* Call all expired timers associated with the timer list. +* +* Returns: true if any timer expired +*/ +bool timerlist_run_timers (QEMUTimerList *timer_list); + +/** * qemu_timeout_ns_to_ms: * @ns: nanosecond timeout value * @@ -102,6 +261,21 @@ void qemu_unregister_clock_reset_notifier(QEMUClock *clock,QEMUTimer *qemu_new_timer (QEMUClock *clock,int scale,QEMUTimerCB *cb,void *opaque); + +/** +* timer_new: +* @timer_list: the timer list to attach the timer to +* @scale: the scale value for the tiemr +* @cb: the callback to be called when the timer expires +* @opaque: the opaque pointer to be passed to the callback +* +* Creeate a new timer and associate it with @timer_list. +* +* Returns: a pointer to the timer +*/ +QEMUTimer *timer_new (QEMUTimerList *timer_list,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);@@ -110,11 +284,101 @@ 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 */ + +/** +* timer_free: +* @ts: the timer +* +* Free a timer (it must not be on the active list) +*/ +static inline void timer_free (QEMUTimer *ts) +{ +qemu_free_timer (ts); +} + +/** +* timer_del: +* @ts: the timer +* +* Delete a timer from the active list. +*/ +static inline void timer_del (QEMUTimer *ts) +{ +qemu_del_timer (ts); +} + +/** +* timer_mod_ns: +* @ts: the timer +* @expire_time: the expiry time in nanoseconds +* +* Modify a timer to expire at @expire_time +*/ +static inline void timer_mod_ns (QEMUTimer *ts,int64_t expire_time) +{ +qemu_mod_timer_ns (ts, expire_time); +} + +/** +* timer_mod: +* @ts: the timer +* @expire_time: the expire time in the units associated with the timer +* +* Modify a timer to expiry at @expire_time, taking into +* account the scale associated with the timer. +*/ +static inline void timer_mod (QEMUTimer *ts,int64_t expire_timer) +{ +qemu_mod_timer (ts, expire_timer); +} + +/** +* timer_pending: +* @ts: the timer +* +* Determines whether a timer is pending (i.e. is on the +* active list of timers, whether or not it has not yet expired). +* +* Returns: true if the timer is pending +*/ +static inline bool timer_pending (QEMUTimer *ts) +{ +return qemu_timer_pending (ts); +} + +/** +* timer_expired: +* @ts: the timer +* +* Determines whether a timer has expired. +* +* Returns: true if the timer has expired +*/ +static inline bool timer_expired (QEMUTimer *timer_head,int64_t current_time) +{ +return qemu_timer_expired (timer_head, current_time); +} + +/** +* timer_expire_time_ns: +* @ts: the timer +* +* Determine the expiry time of a timer +* +* Returns: the expiry time in nanoseconds +*/ +static inline uint64_t timer_expire_time_ns (QEMUTimer *ts) +{ +return qemu_timer_expire_time_ns (ts); +} +/** * qemu_run_timers: * @clock: clock on which to operate * -* Run all the timers associated with a clock. +* Run all the timers associated with the default timer list +* of a clock. * * Returns: true if any timer ran. */ @@ -123,7 +387,8 @@ bool qemu_run_timers(QEMUClock *clock);/** * qemu_run_all_timers: * -* Run all the timers associated with every clock. +* Run all the timers associated with the default timer list +* of every clock. * * Returns: true if any timer ran. */ @@ -156,18 +421,113 @@ static inline int64_t qemu_soonest_timeout(int64_t timeout1, int64_t timeout2)return ((uint64_t ) timeout1 < (uint64_t ) timeout2) ? timeout1 : timeout2; } +/** +* qemu_new_timer_ns: +* @clock: the clock to associate with the timer +* @callback: the callback to call when the timer expires +* @opaque: the opaque pointer to pass to the callback +* +* Create a new timer with nanosecond scale on the default timer list +* associated with the clock. +* +* Returns: a pointer to the newly created timer +*/ static inline QEMUTimer *qemu_new_timer_ns (QEMUClock *clock,QEMUTimerCB *cb,void *opaque) {return qemu_new_timer (clock,SCALE_NS , cb, opaque); } -static inline QEMUTimer *qemu_new_timer_ms (QEMUClock *clock,QEMUTimerCB *cb, +/** +* timer_new_ns: +* @timer_list: the timer list to associate with the timer +* @callback: the callback to call when the timer expires +* @opaque: the opaque pointer to pass to the callback +* +* Create a new timer with nanosecond scale on the timer list +* specified. +* +* Returns: a pointer to the newly created timer +*/ +static inline QEMUTimer *timer_new_ns (QEMUTimerList *timer_list, +QEMUTimerCB *cb, +void *opaque) +{ +return timer_new (timer_list,SCALE_NS , cb, opaque); +} + +/** +* qemu_new_timer_us: +* @clock: the clock to associate with the timer +* @callback: the callback to call when the timer expires +* @opaque: the opaque pointer to pass to the callback +* +* Create a new timer with microsecond scale on the default timer list +* associated with the clock. +* +* Returns: a pointer to the newly created timer +*/ +static inline QEMUTimer *qemu_new_timer_us (QEMUClock *clock, +QEMUTimerCB *cb, +void *opaque) +{ +return qemu_new_timer (clock,SCALE_US , cb, opaque); +} + +/** +* timer_new_us: +* @timer_list: the timer list to associate with the timer +* @callback: the callback to call when the timer expires +* @opaque: the opaque pointer to pass to the callback +* +* Create a new timer with microsecond scale on the timer list +* specified. +* +* Returns: a pointer to the newly created timer +*/ +static inline QEMUTimer *timer_new_us (QEMUTimerList *timer_list, +QEMUTimerCB *cb, +void *opaque) +{ +return timer_new (timer_list,SCALE_US , cb, opaque); +} + +/** +* qemu_new_timer_ms: +* @clock: the clock to associate with the timer +* @callback: the callback to call when the timer expires +* @opaque: the opaque pointer to pass to the callback +* +* Create a new timer with millisecond scale on the default timer list +* associated with the clock. +* +* Returns: a pointer to the newly created timer +*/ +static inline QEMUTimer *qemu_new_timer_ms (QEMUClock *clock, +QEMUTimerCB *cb,void *opaque) {return qemu_new_timer (clock,SCALE_MS , cb, opaque); } +/** +* timer_new_ms: +* @timer_list: the timer list to associate with the timer +* @callback: the callback to call when the timer expires +* @opaque: the opaque pointer to pass to the callback +* +* Create a new timer with millisecond scale on the timer list +* specified. +* +* Returns: a pointer to the newly created timer +*/ +static inline QEMUTimer *timer_new_ms (QEMUTimerList *timer_list, +QEMUTimerCB *cb, +void *opaque) +{ +return timer_new (timer_list,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 +154
-48@@ -49,18 +49,34 @@/* timers */ struct QEMUClock { -QEMUTimer *active_timers; +QEMUTimerList *default_timerlist; + QLIST_HEAD(, QEMUTimerList) timerlists;NotifierList reset_notifiers;int64_t last; -int type; +QEMUClockType type;bool enabled; }; +QEMUClock *qemu_clocks[QEMU_CLOCK_MAX ]; + +/* A QEMUTimerList is a list of timers attached to a clock. More +* than one QEMUTimerList can be attached to each clock, for instance +* used by different AioContexts / threads. Each clock also has +* a list of the QEMUTimerLists associated with it, in order that +* reenabling the clock can call all the notifiers. +*/ + +struct QEMUTimerList { +QEMUClock *clock; +QEMUTimer *active_timers; +QLIST_ENTRY (QEMUTimerList) list; +}; +struct QEMUTimer {int64_t expire_time;/* in nanoseconds */ -QEMUClock *clock; +QEMUTimerList *timer_list;QEMUTimerCB *cb;void *opaque;QEMUTimer *next;@@ -93,21 +109,25 @@ static int64_t qemu_next_alarm_deadline(void) {int64_t delta =INT64_MAX ;int64_t rtdelta; +int64_t hdelta; -if (!use_icount && vm_clock->enabled && vm_clock->active_timers) { - delta = vm_clock->active_timers->expire_time - -qemu_get_clock_ns (vm_clock); +if (!use_icount && vm_clock->enabled && + vm_clock->default_timerlist->active_timers) { + delta = vm_clock->default_timerlist->active_timers->expire_time - +qemu_get_clock_ns (vm_clock); } -if (host_clock->enabled && host_clock->active_timers) { -int64_t hdelta = host_clock->active_timers->expire_time - -qemu_get_clock_ns (host_clock); +if (host_clock->enabled && + host_clock->default_timerlist->active_timers) { + hdelta = host_clock->default_timerlist->active_timers->expire_time - +qemu_get_clock_ns (host_clock);if (hdelta < delta) { delta = hdelta; } } -if (rt_clock->enabled && rt_clock->active_timers) { - rtdelta = (rt_clock->active_timers->expire_time - -qemu_get_clock_ns (rt_clock)); +if (rt_clock->enabled && + rt_clock->default_timerlist->active_timers) { + rtdelta = (rt_clock->default_timerlist->active_timers->expire_time - +qemu_get_clock_ns (rt_clock));if (rtdelta < delta) { delta = rtdelta; }@@ -231,11 +251,42 @@ next: } } -QEMUClock *rt_clock; -QEMUClock *vm_clock; -QEMUClock *host_clock; +static QEMUTimerList *timerlist_new_from_clock (QEMUClock *clock) +{ +QEMUTimerList *timer_list; + +/* Assert if we do not have a clock. If you see this +* assertion in means that the clocks have not been +* initialised before a timerlist is needed. This +* normally happens if an AioContext is used before +* init_clocks() is called within main(). +*/ +assert (clock); + + timer_list =g_malloc0 (sizeof (QEMUTimerList)); + timer_list->clock = clock; +QLIST_INSERT_HEAD (&clock->timerlists, timer_list, list); +return timer_list; +} + +QEMUTimerList *timerlist_new (QEMUClockType type) +{ +return timerlist_new_from_clock (qemu_get_clock (type)); +} + +void timerlist_free (QEMUTimerList *timer_list) +{ +assert (!timerlist_has_timers (timer_list)); +if (timer_list->clock) { +QLIST_REMOVE (timer_list, list); +if (timer_list->clock->default_timerlist == timer_list) { + timer_list->clock->default_timerlist =NULL ; + } + } +g_free (timer_list); +} -QEMUClock *qemu_clock_new (int type) +QEMUClock *qemu_clock_new (QEMUClockType type) {QEMUClock *clock;@@ -244,14 +295,21 @@ QEMUClock *qemu_clock_new(int type) clock->enabled = true; clock->last =INT64_MIN ;notifier_list_init (&clock->reset_notifiers); + clock->default_timerlist =timerlist_new_from_clock (clock);return clock; }void qemu_clock_free (QEMUClock *clock) { +timerlist_free (clock->default_timerlist);g_free (clock); } +bool qemu_clock_use_for_deadline (QEMUClock *clock) +{ +return !(use_icount && (clock->type ==QEMU_CLOCK_VIRTUAL )); +} +void qemu_clock_enable (QEMUClock *clock,bool enabled) {bool old = clock->enabled;@@ -261,24 +319,36 @@ void qemu_clock_enable(QEMUClock *clock, bool enabled) } } -int64_t qemu_clock_has_timers (QEMUClock *clock) +bool timerlist_has_timers (QEMUTimerList *timer_list) { -return !!clock->active_timers; +return !!timer_list->active_timers; } -int64_t qemu_clock_expired (QEMUClock *clock) +bool qemu_clock_has_timers (QEMUClock *clock) { -return (clock->active_timers && - clock->active_timers->expire_time <qemu_get_clock_ns (clock)); +return timerlist_has_timers (clock->default_timerlist); } -int64_t qemu_clock_deadline (QEMUClock *clock) +bool timerlist_expired (QEMUTimerList *timer_list) +{ +return (timer_list->active_timers && + timer_list->active_timers->expire_time < +qemu_get_clock_ns (timer_list->clock)); +} + +bool qemu_clock_expired (QEMUClock *clock) +{ +return timerlist_expired (clock->default_timerlist); +} + +int64_t timerlist_deadline (QEMUTimerList *timer_list) {/* To avoid problems with overflow limit this to 2^32. */ int64_t delta =INT32_MAX ; -if (clock->enabled && clock->active_timers) { - delta = clock->active_timers->expire_time -qemu_get_clock_ns (clock); +if (timer_list->clock->enabled && timer_list->active_timers) { + delta = timer_list->active_timers->expire_time - +qemu_get_clock_ns (timer_list->clock); }if (delta <0 ) { delta =0 ;@@ -286,20 +356,26 @@ int64_t qemu_clock_deadline(QEMUClock *clock)return delta; } +int64_t qemu_clock_deadline (QEMUClock *clock) +{ +return timerlist_deadline (clock->default_timerlist); +} +/* * 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 timerlist_deadline_ns (QEMUTimerList *timer_list) {int64_t delta; -if (!clock->enabled || !clock->active_timers) { +if (!timer_list->clock->enabled || !timer_list->active_timers) {return -1 ; } - delta = clock->active_timers->expire_time -qemu_get_clock_ns (clock); + delta = timer_list->active_timers->expire_time - +qemu_get_clock_ns (timer_list->clock);if (delta <=0 ) {return 0 ;@@ -308,6 +384,21 @@ int64_t qemu_clock_deadline_ns(QEMUClock *clock)return delta; } +int64_t qemu_clock_deadline_ns (QEMUClock *clock) +{ +return timerlist_deadline_ns (clock->default_timerlist); +} + +QEMUClock *timerlist_get_clock (QEMUTimerList *timer_list) +{ +return timer_list->clock; +} + +QEMUTimerList *qemu_clock_get_default_timerlist (QEMUClock *clock) +{ +return clock->default_timerlist; +} +/* Transition function to convert a nanosecond timeout to ms * This is used where a system does not support ppoll */ @@ -356,19 +447,26 @@ int qemu_poll_ns(GPollFD *fds, uint nfds, int64_t timeout) } -QEMUTimer *qemu_new_timer (QEMUClock *clock,int scale, -QEMUTimerCB *cb,void *opaque) +QEMUTimer *timer_new (QEMUTimerList *timer_list,int scale, +QEMUTimerCB *cb,void *opaque) {QEMUTimer *ts; ts =g_malloc0 (sizeof (QEMUTimer)); - ts->clock = clock; + ts->timer_list = timer_list; ts->cb = cb; ts->opaque = opaque; ts->scale = scale;return ts; } +QEMUTimer *qemu_new_timer (QEMUClock *clock,int scale, +QEMUTimerCB *cb,void *opaque) +{ +return timer_new (clock->default_timerlist, + scale, cb, opaque); +} +void qemu_free_timer (QEMUTimer *ts) {g_free (ts);@@ -381,7 +479,7 @@ void qemu_del_timer(QEMUTimer *ts)/* NOTE: this code must be signal safe because qemu_timer_expired() can be called from a signal. */ - pt = &ts->clock->active_timers; + pt = &ts->timer_list->active_timers;for (;;) { t = *pt;if (!t)@@ -405,7 +503,7 @@ void qemu_mod_timer_ns(QEMUTimer *ts, int64_t expire_time)/* add the timer in the sorted list */ /* NOTE: this code must be signal safe because qemu_timer_expired() can be called from a signal. */ - pt = &ts->clock->active_timers; + pt = &ts->timer_list->active_timers;for (;;) { t = *pt;if (!qemu_timer_expired_ns (t, expire_time)) {@@ -418,12 +516,12 @@ void qemu_mod_timer_ns(QEMUTimer *ts, int64_t expire_time) *pt = ts;/* Rearm if necessary */ -if (pt == &ts->clock->active_timers) { +if (pt == &ts->timer_list->active_timers) {if (!alarm_timer->pending) {qemu_rearm_alarm_timer (alarm_timer); }/* Interrupt execution to force deadline recalculation. */ -qemu_clock_warp (ts->clock); +qemu_clock_warp (ts->timer_list->clock);if (use_icount) {qemu_notify_event (); }@@ -438,7 +536,7 @@ void qemu_mod_timer(QEMUTimer *ts, int64_t expire_time)bool qemu_timer_pending (QEMUTimer *ts) {QEMUTimer *t; -for (t = ts->clock->active_timers; t !=NULL ; t = t->next) { +for (t = ts->timer_list->active_timers; t !=NULL ; t = t->next) {if (t == ts) {return true; }@@ -451,23 +549,24 @@ bool qemu_timer_expired(QEMUTimer *timer_head, int64_t current_time)return qemu_timer_expired_ns (timer_head, current_time * timer_head->scale); } -bool qemu_run_timers (QEMUClock *clock) +bool timerlist_run_timers (QEMUTimerList *timer_list) {QEMUTimer *ts;int64_t current_time;bool progress = false; -if (!clock->enabled) +if (!timer_list->clock->enabled) {return progress; + } - current_time =qemu_get_clock_ns (clock); + current_time =qemu_get_clock_ns (timer_list->clock);for (;;) { - ts = clock->active_timers; + ts = timer_list->active_timers;if (!qemu_timer_expired_ns (ts, current_time)) {break ; }/* remove timer from the list before calling the callback */ - clock->active_timers = ts->next; + timer_list->active_timers = ts->next; ts->next =NULL ;/* run the callback (the timer list can be modified) */ @@ -477,6 +576,11 @@ bool qemu_run_timers(QEMUClock *clock)return progress; } +bool qemu_run_timers (QEMUClock *clock) +{ +return timerlist_run_timers (clock->default_timerlist); +} +int64_t qemu_get_clock_ns (QEMUClock *clock) {int64_t now, last;@@ -514,11 +618,13 @@ void qemu_unregister_clock_reset_notifier(QEMUClock *clock, Notifier *notifier)void init_clocks (void ) { -if (!rt_clock) { - rt_clock =qemu_clock_new (QEMU_CLOCK_REALTIME ); - vm_clock =qemu_clock_new (QEMU_CLOCK_VIRTUAL ); - host_clock =qemu_clock_new (QEMU_CLOCK_HOST ); +QEMUClockType type; +for (type =0 ; type <QEMU_CLOCK_MAX ; type++) { +if (!qemu_clocks[type]) { + qemu_clocks[type] =qemu_clock_new (type); + } } +#ifdef CONFIG_PRCTL_PR_SET_TIMERSLACK prctl (PR_SET_TIMERSLACK ,1 ,0 ,0 ,0 );#endif @@ -535,9 +641,10 @@ bool qemu_run_all_timers(void) alarm_timer->pending = false;/* vm time timers */ - progress |=qemu_run_timers (vm_clock); - progress |=qemu_run_timers (rt_clock); - progress |=qemu_run_timers (host_clock); +QEMUClockType type; +for (type =0 ; type <QEMU_CLOCK_MAX ; type++) { + progress |=qemu_run_timers (qemu_get_clock (type)); + }/* rearm timer, if not periodic */ if (alarm_timer->expired) { --1.7.9.5
RFC PATCH v7 8/22 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/exynos4210_mct.c | 1 + hw/timer/exynos4210_pwm.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/sh_timer.c | 1 + hw/timer/slavio_timer.c | 1 + hw/timer/xilinx_timer.c | 1 + hw/tpm/tpm_tis.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-sasl.h | 1 + ui/vnc-auth-vencrypt.c | 2 +- ui/vnc-ws.c | 1 + 34 files changed, 35 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/exynos4210_mct.c +1 -0
@@ -54,6 +54,7 @@#include "hw/sysbus.h" #include "qemu/timer.h" +#include "qemu/main-loop.h" #include "qemu-common.h" #include "hw/ptimer.h"
hw/timer/exynos4210_pwm.c +1 -0
@@ -23,6 +23,7 @@#include "hw/sysbus.h" #include "qemu/timer.h" #include "qemu-common.h" +#include "qemu/main-loop.h" #include "hw/ptimer.h" #include "hw/arm/exynos4210.h"
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/sh_timer.c +1 -0
@@ -11,6 +11,7 @@#include "hw/hw.h" #include "hw/sh4/sh.h" #include "qemu/timer.h" +#include "qemu/main-loop.h" #include "exec/address-spaces.h" #include "hw/ptimer.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/tpm/tpm_tis.c +1 -0
@@ -28,6 +28,7 @@#include "hw/pci/pci_ids.h" #include "tpm_tis.h" #include "qemu-common.h" +#include "qemu/main-loop.h" /*#define DEBUG_TIS */
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-sasl.h +1 -0
@@ -33,6 +33,7 @@ typedef struct VncStateSASL VncStateSASL;typedef struct VncDisplaySASL VncDisplaySASL ;#include "qemu/acl.h" +#include "qemu/main-loop.h" struct VncStateSASL {sasl_conn_t *conn;
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 v7 9/22 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 | 45 +++++++++++++++++++++++++++++++++++++++++++++ qemu-timer.c | 41 +++++++++++++++++++++++++++++++++++++++++ 2 files changed, 86 insertions(+)
include/qemu/timer.h +45 -0
@@ -52,8 +52,10 @@ typedef enum {typedef struct QEMUClock QEMUClock ;typedef struct QEMUTimerList QEMUTimerList ; +typedef QEMUTimerList *QEMUTimerListGroup [QEMU_CLOCK_MAX ];typedef void QEMUTimerCB (void *opaque); +extern QEMUTimerListGroup main_loop_tlg;extern QEMUClock *qemu_clocks[QEMU_CLOCK_MAX ];/**
@@ -229,6 +231,49 @@ QEMUClock *timerlist_get_clock(QEMUTimerList *timer_list);bool timerlist_run_timers (QEMUTimerList *timer_list);/** +* timerlistgroup_init: +* @tlg: the timer list group +* +* Initialise a timer list group. This must already be +* allocated in memory and zeroed. +*/ +void timerlistgroup_init (QEMUTimerListGroup tlg); + +/** +* timerlistgroup_deinit: +* @tlg: the timer list group +* +* Deinitialise a timer list group. This must already be +* initialised. Note the memory is not freed. +*/ +void timerlistgroup_deinit (QEMUTimerListGroup tlg); + +/** +* timerlistgroup_run_timers: +* @tlg: the timer list group +* +* Run the timers associated with a timer list group. +* This will run timers on multiple clocks. +* +* Returns: true if any timer callback ran +*/ +bool timerlistgroup_run_timers (QEMUTimerListGroup tlg); + +/** +* timerlistgroup_deadline_ns +* @tlg: the timer list group +* +* Determine the deadline of the soonest timer to +* expire associated with any timer list linked to +* the timer list group. Only clocks suitable for +* deadline calculation are included. +* +* Returns: the deadline in nanoseconds or -1 if no +* timers are to expire. +*/ +int64_t timerlistgroup_deadline_ns (QEMUTimerListGroup tlg); + +/** * qemu_timeout_ns_to_ms: * @ns: nanosecond timeout value *
qemu-timer.c +41 -0
59 lines not on this page · raw message
Signature · 1 line
1.7.9.5
RFC PATCH v7 10/22 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 v7 11/22 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 | 35 ++++++++++++++++++++++++++++++++--- qemu-timer.c | 24 ++++++++++++++++++++++-- 3 files changed, 60 insertions(+), 6 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 (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 +32 -3
@@ -54,6 +54,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 main_loop_tlg;extern QEMUClock *qemu_clocks[QEMU_CLOCK_MAX ];
@@ -231,13 +232,41 @@ QEMUClock *timerlist_get_clock(QEMUTimerList *timer_list);bool timerlist_run_timers (QEMUTimerList *timer_list);/** +* timerlist_set_notify_cb: +* @timer_list: the timer list to use +* @cb: the callback to call on notification +* @opaque: the opaque pointer to pass to the callback +* +* Set the notify callback for a timer list. The notifier +* callback is called when the clock is reenabled or a timer +* on the list is modified. +*/ +void timerlist_set_notify_cb (QEMUTimerList *timer_list, +QEMUTimerListNotifyCB *cb,void *opaque); + +/** +* timerlist_notify: +* @timer_list: the timer list to use +* +* call the notifier callback associated with the timer list. +*/ +void timerlist_notify (QEMUTimerList *timer_list); + +/** * timerlistgroup_init: * @tlg: the timer list group +* @cb: the callback to call when a notify is required +* @opaque: the opaque pointer to be passed to the callback. * * Initialise a timer list group. This must already be -* allocated in memory and zeroed. -*/ -void timerlistgroup_init (QEMUTimerListGroup tlg); +* allocated in memory and zeroed. The notifier callback is +* called whenever a clock in the timer list group is +* reenabled or whenever a timer associated with any timer +* list is modified. If @cb is specified as null, qemu_notify() +* is used instead. +*/ +void timerlistgroup_init (QEMUTimerListGroup tlg, +QEMUTimerListNotifyCB *cb,void *opaque);/** * timerlistgroup_deinit:
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 {
@@ -400,6 +402,22 @@ QEMUTimerList *qemu_clock_get_default_timerlist(QEMUClock *clock)return clock->default_timerlist; } +void timerlist_set_notify_cb (QEMUTimerList *timer_list, +QEMUTimerListNotifyCB *cb,void *opaque) +{ + timer_list->notify_cb = cb; + timer_list->notify_opaque = opaque; +} + +void timerlist_notify (QEMUTimerList *timer_list) +{ +if (timer_list->notify_cb) { + timer_list->notify_cb (timer_list->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 */
@@ -524,7 +542,7 @@ void qemu_mod_timer_ns(QEMUTimer *ts, int64_t expire_time)/* Interrupt execution to force deadline recalculation. */ qemu_clock_warp (ts->timer_list->clock);if (use_icount) { -qemu_notify_event (); +timerlist_notify (ts->timer_list); } } }
@@ -582,11 +600,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 type;for (type =0 ; type <QEMU_CLOCK_MAX ; type++) { tlg[type] =timerlist_new (type); +timerlist_set_notify_cb (tlg[type], cb, opaque); } }
Signature · 1 line
1.7.9.5
RFC PATCH v7 12/22 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 v7 13/22 aio / timers: Add aio_timer_new wrapper
In reply to Alex BlighAdd aio_timer_new wrapper function.
--- include/block/aio.h | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+)
include/block/aio.h +19 -0
@@ -255,4 +255,23 @@ void qemu_aio_set_fd_handler(int fd, void *opaque);#endif +/** +* aio_timer_new: +* @ctx: the aio context +* @type: the clock type +* @scale: the scale +* @cb: the callback to call on timer expiry +* @opaque: the opaque pointer to pass to the callback +* +* Generate a new timer attached to the context @ctx. +* +* Returns: a pointer to the new timer +*/ +static inline QEMUTimer *aio_timer_new (AioContext *ctx,QEMUClockType type, +int scale, +QEMUTimerCB *cb,void *opaque) +{ +return timer_new (ctx->tlg[type], scale, cb, opaque); +} +#endif
Signature · 1 line
1.7.9.5
RFC PATCH v7 14/22 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->timer_list); + } +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 v7 15/22 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 ( + main_loop_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 v7 16/22 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 | 9 +++++++++ qemu-timer.c | 13 ++++++++++--- 2 files changed, 19 insertions(+), 3 deletions(-)
include/qemu/timer.h +9 -0
@@ -140,6 +140,15 @@ bool qemu_clock_use_for_deadline(QEMUClock *clock);QEMUTimerList *qemu_clock_get_default_timerlist (QEMUClock *clock);/** +* qemu_clock_nofify: +* @clock: the clock to operate on +* +* Call the notifier callback connected with the default timer +* list linked to the clock, or qemu_notify() if none. +*/ +void qemu_clock_notify (QEMUClock *clock); + +/** * timerlist_new: * @type: the clock type to associate with the timerlist *
qemu-timer.c +10 -3
@@ -313,11 +313,20 @@ bool qemu_clock_use_for_deadline(QEMUClock *clock)return !(use_icount && (clock->type ==QEMU_CLOCK_VIRTUAL )); } +void qemu_clock_notify (QEMUClock *clock) +{ +QEMUTimerList *timer_list; + QLIST_FOREACH(timer_list, &clock->timerlists, list) { +timerlist_notify (timer_list); + } +} +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); } }
@@ -541,9 +550,7 @@ void qemu_mod_timer_ns(QEMUTimer *ts, int64_t expire_time) }/* Interrupt execution to force deadline recalculation. */ qemu_clock_warp (ts->timer_list->clock); -if (use_icount) { -timerlist_notify (ts->timer_list); - } +timerlist_notify (ts->timer_list); } }
Signature · 1 line
1.7.9.5
RFC PATCH v7 17/22 aio / timers: Introduce new API qemu_timer_new and friends
In reply to Alex BlighIntroduce new API for creating timers - qemu_timer_new and _ns, _ms, _us derivatives.
--- include/qemu/timer.h | 69 ++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 69 insertions(+)
include/qemu/timer.h +69 -0
@@ -358,6 +358,24 @@ QEMUTimer *qemu_new_timer(QEMUClock *clock, int scale,QEMUTimer *timer_new (QEMUTimerList *timer_list,int scale,QEMUTimerCB *cb,void *opaque); +/** +* qemu_timer_new: +* @type: the clock type to use +* @scale: the scale value for the tiemr +* @cb: the callback to be called when the timer expires +* @opaque: the opaque pointer to be passed to the callback +* +* Creeate a new timer and associate it with the default +* timer list for the clock type @type. +* +* Returns: a pointer to the timer +*/ +static inline QEMUTimer *qemu_timer_new (QEMUClockType type,int scale, +QEMUTimerCB *cb,void *opaque) +{ +return timer_new (main_loop_tlg[type], scale, cb, opaque); +} +void qemu_free_timer (QEMUTimer *ts);void qemu_del_timer (QEMUTimer *ts);void qemu_mod_timer_ns (QEMUTimer *ts,int64_t expire_time);
@@ -521,6 +539,23 @@ static inline QEMUTimer *qemu_new_timer_ns(QEMUClock *clock, QEMUTimerCB *cb, }/** +* qemu_timer_new_ns: +* @clock: the clock to associate with the timer +* @callback: the callback to call when the timer expires +* @opaque: the opaque pointer to pass to the callback +* +* Create a new timer with nanosecond scale on the default timer list +* associated with the clock. +* +* Returns: a pointer to the newly created timer +*/ +static inline QEMUTimer *qemu_timer_new_ns (QEMUClockType type,QEMUTimerCB *cb, +void *opaque) +{ +return qemu_timer_new (type,SCALE_NS , cb, opaque); +} + +/** * timer_new_ns: * @timer_list: the timer list to associate with the timer * @callback: the callback to call when the timer expires
@@ -557,6 +592,23 @@ static inline QEMUTimer *qemu_new_timer_us(QEMUClock *clock, }/** +* qemu_timer_new_us: +* @clock: the clock to associate with the timer +* @callback: the callback to call when the timer expires +* @opaque: the opaque pointer to pass to the callback +* +* Create a new timer with microsecond scale on the default timer list +* associated with the clock. +* +* Returns: a pointer to the newly created timer +*/ +static inline QEMUTimer *qemu_timer_new_us (QEMUClockType type,QEMUTimerCB *cb, +void *opaque) +{ +return qemu_timer_new (type,SCALE_US , cb, opaque); +} + +/** * timer_new_us: * @timer_list: the timer list to associate with the timer * @callback: the callback to call when the timer expires
@@ -593,6 +645,23 @@ static inline QEMUTimer *qemu_new_timer_ms(QEMUClock *clock, }/** +* qemu_timer_new_ms: +* @clock: the clock to associate with the timer +* @callback: the callback to call when the timer expires +* @opaque: the opaque pointer to pass to the callback +* +* Create a new timer with millisecond scale on the default timer list +* associated with the clock. +* +* Returns: a pointer to the newly created timer +*/ +static inline QEMUTimer *qemu_timer_new_ms (QEMUClockType type,QEMUTimerCB *cb, +void *opaque) +{ +return qemu_timer_new (type,SCALE_MS , cb, opaque); +} + +/** * timer_new_ms: * @timer_list: the timer list to associate with the timer * @callback: the callback to call when the timer expires
Signature · 1 line
1.7.9.5
RFC PATCH v7 18/22 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 | 46 +++++++++++++++++++++++++++++++++++++--------- include/qemu/timer.h | 13 +++++++++++++ qemu-timer.c | 16 ++++++++++++++++ qtest.c | 2 +- 4 files changed, 67 insertions(+), 10 deletions(-)
cpus.c +37 -9
@@ -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 ;
@@ -273,13 +273,13 @@ void qtest_clock_warp(int64_t dest)int64_t clock =qemu_get_clock_ns (vm_clock);assert (qtest_enabled ());while (clock < dest) { -int64_t deadline =qemu_clock_deadline (vm_clock); +int64_t deadline =qemu_clock_deadline_ns_all (vm_clock);int64_t warp =MIN (dest - clock, deadline); qemu_icount_bias += warp;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 +13 -0
@@ -107,6 +107,7 @@ int64_t qemu_clock_deadline(QEMUClock *clock);* @clock: the clock to operate on * * Calculate the timeout of the earliest expiring timer +* on the default timer list associated with the clock * in nanoseconds, or -1 if no timer is set to expire. * * Returns: time until expiry in nanoseconds or -1
@@ -130,6 +131,18 @@ int64_t qemu_clock_deadline_ns(QEMUClock *clock);bool qemu_clock_use_for_deadline (QEMUClock *clock);/** +* qemu_clock_use_for_deadline: +* @clock: the clock to operate on +* +* Calculate the deadline across all timer lists associated +* with a clock (as opposed to just the default one) +* in nanoseconds, or -1 if no timer is set to expire. +* +* Returns: time until expiry in nanoseconds or -1 +*/ +int64_t qemu_clock_deadline_ns_all (QEMUClock *clock); + +/** * qemu_clock_get_default_timerlist: * @clock: the clock to operate on *
qemu-timer.c +16 -0
@@ -401,6 +401,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 *timer_list; + QLIST_FOREACH(timer_list, &clock->timerlists, list) { + deadline =qemu_soonest_timeout (deadline, +timerlist_deadline_ns (timer_list)); + } +return deadline; +} +QEMUClock *timerlist_get_clock (QEMUTimerList *timer_list) {return timer_list->clock;
qtest.c +1 -1
@@ -412,7 +412,7 @@ static void qtest_process_command(CharDriverState *chr, gchar **words)if (words[1 ]) { ns =strtoll (words[1 ],NULL ,0 ); }else { - ns =qemu_clock_deadline (vm_clock); + ns =qemu_clock_deadline_ns_all (vm_clock); }qtest_clock_warp (qemu_get_clock_ns (vm_clock) + ns);qtest_send_prefix (chr);
Signature · 1 line
1.7.9.5
RFC PATCH v7 19/22 aio / timers: Add documentation and new format calls
In reply to Alex BlighAdd documentation for existing qemu timer calls. Add new format calls of the format qemu_timer_XXX rather than qemu_XXX_timer for consistency.
--- configure | 18 +++++ include/qemu/timer.h | 215 +++++++++++++++++++++++++++++++++++++++++++++++++- 2 files changed, 231 insertions(+), 2 deletions(-)
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
include/qemu/timer.h +213 -2
262 lines not on this page · raw message
Signature · 1 line
1.7.9.5
RFC PATCH v7 20/22 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 | 3 - main-loop.c | 4 - qemu-timer.c | 500 +------------------------------------------------- vl.c | 4 +- 4 files changed, 4 insertions(+), 507 deletions(-)
include/qemu/timer.h +0 -3
@@ -712,9 +712,6 @@ bool qemu_run_timers(QEMUClock *clock);*/ bool qemu_run_all_timers (void ); -void configure_alarms (char const *opt); -int init_timer_alarm (void ); -/** * initclocks: *
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 v7 21/22 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 | 137 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 137 insertions(+)
tests/test-aio.c +137 -0
@@ -32,6 +32,15 @@ typedef struct {int max; } BHTestData; +typedef struct { +QEMUTimer *timer; +QEMUTimerList *timer_list; +int n; +int max; +int64_t ns; +AioContext *ctx; +}TimerTestData ; +static void bh_test_cb (void *opaque) {BHTestData *data = opaque;
@@ -40,6 +49,26 @@ 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->timer_list)) + + 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 +370,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 , + .timer_list = 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.timer_list,SCALE_NS , timer_test_cb, &data); +qemu_mod_timer (data.timer, +qemu_get_clock_ns (timerlist_get_clock (data.timer_list)) + + 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 +710,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 , + .timer_list = 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.timer_list,SCALE_NS , timer_test_cb, &data); + expiry =qemu_get_clock_ns (timerlist_get_clock (data.timer_list)) + + 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.timer_list)) <= 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 +786,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 +801,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 v7 22/22 aio / timers: Remove legacy qemu_clock_deadline & qemu_timerlist_deadline
In reply to Alex BlighRemove qemu_clock_deadline and qemu_timerlist_deadline now we are using the ns functions throughout.
--- include/qemu/timer.h | 16 ---------------- qemu-timer.c | 20 -------------------- 2 files changed, 36 deletions(-)
include/qemu/timer.h +0 -16
@@ -144,7 +144,6 @@ bool qemu_clock_has_timers(QEMUClock *clock);* an expired timer */ bool qemu_clock_expired (QEMUClock *clock); -int64_t qemu_clock_deadline (QEMUClock *clock);/** * qemu_clock_deadline_ns:
@@ -247,21 +246,6 @@ bool timerlist_has_timers(QEMUTimerList *timer_list);bool timerlist_expired (QEMUTimerList *timer_list);/** -* timerlist_deadline: -* @timer_list: the timer list to operate on -* -* Determine the deadline for a timer_list. This is -* a legacy function which returns INT32_MAX if the -* timer list has no timers or if the earliest timer -* expires later than INT32_MAX nanoseconds away. -* -* Returns: the number of nanoseconds until the earliest -* timer expires or INT32_MAX in the situations listed -* above -*/ -int64_t timerlist_deadline (QEMUTimerList *timer_list); - -/** * timerlist_deadline_ns: * @timer_list: the timer list to operate on *
qemu-timer.c +0 -20
@@ -185,26 +185,6 @@ bool qemu_clock_expired(QEMUClock *clock)return timerlist_expired (clock->default_timerlist); } -int64_t timerlist_deadline (QEMUTimerList *timer_list) -{ -/* To avoid problems with overflow limit this to 2^32. */ -int64_t delta =INT32_MAX ; - -if (timer_list->clock->enabled && timer_list->active_timers) { - delta = timer_list->active_timers->expire_time - -qemu_get_clock_ns (timer_list->clock); - } -if (delta <0 ) { - delta =0 ; - } -return delta; -} - -int64_t qemu_clock_deadline (QEMUClock *clock) -{ -return timerlist_deadline (clock->default_timerlist); -} -/* * As above, but return -1 for no deadline, and do not cap to 2^32 * as we know the result is always positive.
Signature · 1 line
1.7.9.5
Pingfan has given me an idea here. I can convert the API over to solely use QEMUClockType and not even expose struct QEMUClock *. I'll do this in v8. I have that working here and the API is much cleaner. Now I just need to reengineer it into git.
Alex
Quoted 161 lines · --On 7 August 2013 00:48:54 +0100 Alex Bligh <[email protected]> wrote:
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 18 in relation to a possible bug in
cpus.c.Changes since v6:
* Fix build failure invnc-auth-sasl.c
* Split first patch into 3
* Add assert ontimerlist_free
* Fix ==/= error onqemu_clock_use_for_deadline
* Remove unnecessary cast inaio_timerlist_notify
* Fix bad deadline comparison inaio_ctx_check
* Add assert totimerlist_new_from_clockto checkinit_clocks
* Usetimer_listnot tl
* Changedefault_timerlistgrouptomain_loop_timerlistgroup
* Add comment on commit forqemu_clock_use_for_deadline
* Fixed various include file issues
* Convert*_has_timersand*_has_expiredto return bool
* Make loop variable consistent when looping through clock types
* Add documentation to existingqemu_timercalls
* Removeqemu_clock_deadlineand move toqemu_clock_deadline_nsChanges 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 (22):
aio / timers: Addqemu_clock_freeand exposeqemu_clock_newand clock
types
aio / timers: addqemu-timer.cutility functions
aio / timers: Consistent treatment of disabled clocks for deadlines 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: Addaio_timer_newwrapper
aio / timers: Convertaio_pollto use AioContext timers' deadline aio / timers: Convert mainloop to use timeout
aio / timers: On timer modification,qemu_notifyoraio_notifyaio / timers: Introduce new APIqemu_timer_newand friends aio / timers: Use all timerlists in icount warp calculations aio / timers: Add documentation and new format calls
aio / timers: Remove alarm timers
aio / timers: Add test harness for AioContext timers
aio / timers: Remove legacyqemu_clock_deadline&
qemu_timerlist_deadlineaio-posix.c | 20 +- aio-win32.c | 22 +- async.c | 20 +- configure | 37 ++ cpus.c | 46 ++- dma-helpers.c | 1 + hw/dma/xilinx_axidma.c | 1 + hw/timer/arm_timer.c | 1 + hw/timer/exynos4210_mct.c | 1 + hw/timer/exynos4210_pwm.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/sh_timer.c | 1 + hw/timer/slavio_timer.c | 1 + hw/timer/xilinx_timer.c | 1 + hw/tpm/tpm_tis.c | 1 + hw/usb/hcd-uhci.c | 1 + include/block/aio.h | 23 ++ include/block/block_int.h | 1 + include/block/coroutine.h | 2 + include/qemu/timer.h | 857 +++++++++++++++++++++++++++++++++++++++++++-- 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 +++++++++++++++--------------------------- qtest.c | 2 +- slirp/misc.c | 1 + tests/test-aio.c | 144 +++++++- tests/test-thread-pool.c | 3 + thread-pool.c | 1 + ui/vnc-auth-sasl.h | 1 + ui/vnc-auth-vencrypt.c | 2 +- ui/vnc-ws.c | 1 + vl.c | 4 +- 46 files changed, 1480 insertions(+), 586 deletions(-)--
1.7.9.5
Signature · 1 line
Alex Bligh