prevent QEMU from waking up needlessly
v5 cover prevent QEMU from waking up needlessly
Hi all,
this small patch series prevents QEMU from waking up needlessly on Xen several times a second in order to check some timers.
I would like some comments (or some acks ;-) on patch #4 and #5.
The first patch stops QEMU from emulating the PIT on Xen, the second patch disables the rtc_clock entirely.
The third patch makes use of a new mechanism to receive buffered io event notifications from Xen, so that QEMU doesn't need to check the buffered io page for data 10 times a sec for the entire life of the VM.
The fourth patch fixes win32_rearm_timer and mm_rearm_timer that risk an overflow if INT64_MAX is passed as delta.
The fifth patch changes qemu_next_alarm_deadline only to check the expire time of a clock if it is enabled.
Finally the last patch makes main_loop_wait wait indefinitely on select unless we actually need a timeout.
Changes in v5:
- remove the last patch to increase the timeout to 1h, replace it with a patch to wait indefinitely on select unless we need a timeout.
Changes in v4:
- do not initialize pcspk on xen;
- disable rtc_clock only when it points to the host_clock (the default);
- make sure it compiles on older xen versions.
Changes in v3:
- added a new patch to fix win32_rearm_timer and mm_rearm_timer, that risk an overflow if INT64_MAX is passed as delta.
Shortlog and diffstat follow:
async.c | 2 +- hw/pc.c | 9 ++++++--- main-loop.c | 21 ++++++++++++--------- main-loop.h | 2 +- qemu-timer.c | 33 +++++++++++++++++---------------- qemu-timer.h | 1 - qemu-tool.c | 4 ++++ slirp/libslirp.h | 1 + slirp/slirp.c | 7 +++++++ xen-all.c | 49 +++++++++++++++++++++++++++++++++++++++++++------ 10 files changed, 92 insertions(+), 37 deletions(-)
Stefano Stabellini (6):
xen: do not initialize the interval timer and PCSPK emulator
xen: disable rtc_clock
xen: introduce an event channel for buffered io event notifications
timers: the rearm function should be able to handle delta = INT64_MAX
qemu_next_alarm_deadline: check the expire time of a clock only if it is enabled
main_loop_wait: block indefinitely
A git tree, based on 9d4df9c02866f39d3eef105033091f367cc7c29e, is available here:
git://xenbits.xen.org/people/sstabellini/qemu-dm.git timers-5
Cheers,
Stefano
PATCH v5 1/6 xen: do not initialize the interval timer and PCSPK emulator
PIT and PCSPK are emulated by the hypervisor so we don't need to emulate them in Qemu: this patch prevents Qemu from waking up needlessly at PIT_FREQ on Xen.
--- hw/pc.c | 9 ++++++--- 1 files changed, 6 insertions(+), 3 deletions(-)
hw/pc.c +6 -3
@@ -44,6 +44,7 @@#include "ui/qemu-spice.h" #include "memory.h" #include "exec-memory.h" +#include "arch_init.h" /* output Bochs bios info messages */ //#define DEBUG_BIOS
@@ -1139,7 +1140,7 @@ void pc_basic_device_init(ISABus *isa_bus, qemu_irq *gsi,DriveInfo *fd[MAX_FD ];qemu_irq rtc_irq =NULL ;qemu_irq *a20_line; -ISADevice *i8042, *port92, *vmmouse, *pit; +ISADevice *i8042, *port92, *vmmouse, *pit =NULL ;qemu_irq *cpu_exit_irq;register_ioport_write (0x80 ,1 ,1 , ioport80_write,NULL );
@@ -1160,8 +1161,10 @@ void pc_basic_device_init(ISABus *isa_bus, qemu_irq *gsi,qemu_register_boot_set (pc_boot_set, *rtc_state); - pit =pit_init (isa_bus,0x40 ,0 ); -pcspk_init (pit); +if (!xen_available ()) { + pit =pit_init (isa_bus,0x40 ,0 ); +pcspk_init (pit); + }for (i =0 ; i <MAX_SERIAL_PORTS ; i++) {if (serial_hds[i]) {
Signature · 1 line
1.7.2.5
PATCH v5 2/6 xen: disable rtc_clock
In reply to Stefano Stabellinirtc_clock is only used by the RTC emulator (mc146818rtc.c), however Xen has its own RTC emulator in the hypervisor so we can disable it.
--- xen-all.c | 4 ++++ 1 files changed, 4 insertions(+), 0 deletions(-)
xen-all.c +4 -0
@@ -514,6 +514,10 @@ void xen_vcpu_init(void)qemu_register_reset (xen_reset_vcpu, first_cpu);xen_reset_vcpu (first_cpu); } +/* if rtc_clock is left to default (host_clock), disable it */ +if (rtc_clock == host_clock) { +qemu_clock_enable (rtc_clock, false); + } }/* get the ioreq packets from share mem */
Signature · 1 line
1.7.2.5
PATCH v5 3/6 xen: introduce an event channel for buffered io event notifications
In reply to Stefano StabelliniUse the newly introduced HVM_PARAM_BUFIOREQ_EVTCHN to receive notifications for buffered io events.
After the first notification is received leave the event channel masked and setup a timer to process the rest of the batch.
Once we have completed processing the batch, unmask the event channel and delete the timer.
--- xen-all.c | 45 +++++++++++++++++++++++++++++++++++++++------ 1 files changed, 39 insertions(+), 6 deletions(-)
xen-all.c +39 -6
@@ -59,6 +59,9 @@ static inline ioreq_t *xen_vcpu_ioreq(shared_iopage_t *shared_page, int vcpu) }# define FMT_ioreq_size "u"#endif +#ifndef HVM_PARAM_BUFIOREQ_EVTCHN +#define HVM_PARAM_BUFIOREQ_EVTCHN 26 +#endif #define BUFFER_IO_MAX_DELAY 100
@@ -77,6 +80,8 @@ typedef struct XenIOState {QEMUTimer *buffered_io_timer;/* the evtchn port for polling the notification, */ evtchn_port_t *ioreq_local_port; +/* evtchn local port for buffered io */ +evtchn_port_t bufioreq_local_port;/* the evtchn fd for polling */ XenEvtchn xce_handle;/* which vcpu we are serving */
@@ -549,6 +554,12 @@ static ioreq_t *cpu_get_ioreq(XenIOState *state)evtchn_port_t port; port =xc_evtchn_pending (state->xce_handle); +if (port == state->bufioreq_local_port) { +qemu_mod_timer (state->buffered_io_timer, +BUFFER_IO_MAX_DELAY +qemu_get_clock_ms (rt_clock)); +return NULL ; + } +if (port !=-1 ) {for (i =0 ; i < smp_cpus; i++) {if (state->ioreq_local_port[i] == port) {
@@ -697,16 +708,18 @@ static void handle_ioreq(ioreq_t *req) } } -static void handle_buffered_iopage (XenIOState *state) +static int handle_buffered_iopage (XenIOState *state) {buf_ioreq_t *buf_req =NULL ;ioreq_t req;int qw;if (!state->buffered_io_page) { -return ; +return 0 ; } +memset (&req,0x00 ,sizeof (req)); +while (state->buffered_io_page->read_pointer != state->buffered_io_page->write_pointer) { buf_req = &state->buffered_io_page->buf_ioreq[ state->buffered_io_page->read_pointer %IOREQ_BUFFER_SLOT_NUM ];
@@ -731,15 +744,21 @@ static void handle_buffered_iopage(XenIOState *state)xen_mb (); state->buffered_io_page->read_pointer += qw ?2 :1 ; } + +return req.count; }static void handle_buffered_io (void *opaque) {XenIOState *state = opaque; -handle_buffered_iopage (state); -qemu_mod_timer (state->buffered_io_timer, -BUFFER_IO_MAX_DELAY +qemu_get_clock_ms (rt_clock)); +if (handle_buffered_iopage (state)) { +qemu_mod_timer (state->buffered_io_timer, +BUFFER_IO_MAX_DELAY +qemu_get_clock_ms (rt_clock)); + }else { +qemu_del_timer (state->buffered_io_timer); +xc_evtchn_unmask (state->xce_handle, state->bufioreq_local_port); + } }static void cpu_handle_ioreq (void *opaque)
@@ -869,7 +888,6 @@ static void xen_main_loop_prepare(XenIOState *state) state->buffered_io_timer =qemu_new_timer_ms (rt_clock, handle_buffered_io, state); -qemu_mod_timer (state->buffered_io_timer,qemu_get_clock_ms (rt_clock));if (evtchn_fd !=-1 ) {qemu_set_fd_handler (evtchn_fd, cpu_handle_ioreq,NULL , state);
@@ -921,6 +939,7 @@ int xen_hvm_init(void) {int i, rc;unsigned long ioreq_pfn; +unsigned long bufioreq_evtchn;XenIOState *state; state =g_malloc0 (sizeof (XenIOState));
@@ -970,6 +989,20 @@ int xen_hvm_init(void) state->ioreq_local_port[i] = rc; } + rc =xc_get_hvm_param (xen_xc, xen_domid,HVM_PARAM_BUFIOREQ_EVTCHN , + &bufioreq_evtchn); +if (rc <0 ) { +fprintf (stderr,"failed to get HVM_PARAM_BUFIOREQ_EVTCHN\n" ); +return -1 ; + } + rc =xc_evtchn_bind_interdomain (state->xce_handle, xen_domid, + (uint32_t )bufioreq_evtchn); +if (rc ==-1 ) { +fprintf (stderr,"bind interdomain ioctl error %d\n" , errno); +return -1 ; + } + state->bufioreq_local_port = rc; +/* Init RAM management */ xen_map_cache_init ();xen_ram_init (ram_size);
Signature · 1 line
1.7.2.5
PATCH v5 4/6 timers: the rearm function should be able to handle delta = INT64_MAX
In reply to Stefano StabelliniFix win32_rearm_timer and mm_rearm_timer: they should be able to handle INT64_MAX as a delta parameter without overflowing.
Also, the next deadline in ms should be calculated rounding down rather than up (see unix_rearm_timer and dynticks_rearm_timer).
Finally ChangeTimerQueueTimer takes an unsigned long and timeSetEvent takes an unsigned int as delta, so cast the ms delta to the appropriate unsigned integer.
--- qemu-timer.c | 18 +++++++++++++----- 1 files changed, 13 insertions(+), 5 deletions(-)
qemu-timer.c +13 -5
@@ -696,13 +696,17 @@ static void mm_stop_timer(struct qemu_alarm_timer *t)static void mm_rearm_timer (struct qemu_alarm_timer *t,int64_t delta) { -int nearest_delta_ms = (delta +999999 ) /1000000 ; +int64_t nearest_delta_ms = delta /1000000 ;if (nearest_delta_ms <1 ) { nearest_delta_ms =1 ; } +/* UINT_MAX can be 32 bit */ +if (nearest_delta_ms >UINT_MAX ) { + nearest_delta_ms =UINT_MAX ; + }timeKillEvent (mm_timer); - mm_timer = timeSetEvent(nearest_delta_ms, + mm_timer = timeSetEvent((unsigned int) nearest_delta_ms, mm_period, mm_alarm_handler, (DWORD_PTR)t,
@@ -757,16 +761,20 @@ static void win32_rearm_timer(struct qemu_alarm_timer *t, int64_t nearest_delta_ns) {HANDLE hTimer = t->timer; -int nearest_delta_ms; +int64_t nearest_delta_ms;BOOLEAN success; - nearest_delta_ms = (nearest_delta_ns +999999 ) /1000000 ; + nearest_delta_ms = nearest_delta_ns /1000000 ;if (nearest_delta_ms <1 ) { nearest_delta_ms =1 ; } +/* ULONG_MAX can be 32 bit */ +if (nearest_delta_ms >ULONG_MAX ) { + nearest_delta_ms =ULONG_MAX ; + } success =ChangeTimerQueueTimer (NULL , hTimer, - nearest_delta_ms, + (unsigned long ) nearest_delta_ms,3600000 );if (!success) {
Signature · 1 line
1.7.2.5
PATCH v5 5/6 qemu_next_alarm_deadline: check the expire time of a clock only if it is enabled
In reply to Stefano StabelliniAlso delta in qemu_next_alarm_deadline is a 64 bit value so set the default to INT64_MAX instead of INT32_MAX.
--- qemu-timer.c | 10 ++++------ 1 files changed, 4 insertions(+), 6 deletions(-)
qemu-timer.c +4 -6
@@ -106,23 +106,21 @@ static inline int alarm_has_dynticks(struct qemu_alarm_timer *t)static int64_t qemu_next_alarm_deadline (void ) { -int64_t delta; +int64_t delta =INT64_MAX ;int64_t rtdelta; -if (!use_icount && vm_clock->active_timers) { +if (!use_icount && vm_clock->enabled && vm_clock->active_timers) { delta = vm_clock->active_timers->expire_time -qemu_get_clock_ns (vm_clock); - }else { - delta =INT32_MAX ; } -if (host_clock->active_timers) { +if (host_clock->enabled && host_clock->active_timers) {int64_t hdelta = host_clock->active_timers->expire_time -qemu_get_clock_ns (host_clock);if (hdelta < delta) { delta = hdelta; } } -if (rt_clock->active_timers) { +if (rt_clock->enabled && rt_clock->active_timers) { rtdelta = (rt_clock->active_timers->expire_time -qemu_get_clock_ns (rt_clock));if (rtdelta < delta) {
Signature · 1 line
1.7.2.5
PATCH v5 6/6 main_loop_wait: block indefinitely
In reply to Stefano Stabellini- remove qemu_calculate_timeout;
- explicitly size timeout to uint32_t;
- introduce slirp_update_timeout;
- pass NULL as timeout argument to select in case timeout is the maximum value;
--- async.c | 2 +- main-loop.c | 21 ++++++++++++--------- main-loop.h | 2 +- qemu-timer.c | 5 ----- qemu-timer.h | 1 - qemu-tool.c | 4 ++++ slirp/libslirp.h | 1 + slirp/slirp.c | 7 +++++++ 8 files changed, 26 insertions(+), 17 deletions(-)
async.c +1 -1
@@ -120,7 +120,7 @@ void qemu_bh_delete(QEMUBH *bh) bh->deleted =1 ; } -void qemu_bh_update_timeout (int *timeout) +void qemu_bh_update_timeout (uint32_t *timeout) {QEMUBH *bh;
main-loop.c +12 -9
@@ -369,7 +369,7 @@ void qemu_del_wait_object(HANDLE handle, WaitObjectFunc *func, void *opaque) } } -static void os_host_main_loop_wait (int *timeout) +static void os_host_main_loop_wait (uint32_t *timeout) {int ret, ret2, i;PollingEntry *pe;
@@ -413,7 +413,7 @@ static void os_host_main_loop_wait(int *timeout) *timeout = 0; }#else -static inline void os_host_main_loop_wait (int *timeout) +static inline void os_host_main_loop_wait (uint32_t *timeout) { }#endif
@@ -422,21 +422,17 @@ int main_loop_wait(int nonblocking) {fd_set rfds, wfds, xfds;int ret, nfds; -struct timeval tv; -int timeout; +struct timeval tv, *tvarg =NULL ; +uint32_t timeout =UINT32_MAX ;if (nonblocking) { timeout =0 ; }else { - timeout =qemu_calculate_timeout ();qemu_bh_update_timeout (&timeout); }os_host_main_loop_wait (&timeout); - tv.tv_sec = timeout /1000 ; - tv.tv_usec = (timeout %1000 ) *1000 ; -/* poll any events */ /* XXX: separate device handlers from system ones */ nfds =-1 ;
@@ -445,16 +441,23 @@ int main_loop_wait(int nonblocking)FD_ZERO (&xfds);#ifdef CONFIG_SLIRP +slirp_update_timeout (&timeout);slirp_select_fill (&nfds, &rfds, &wfds, &xfds);#endif qemu_iohandler_fill (&nfds, &rfds, &wfds, &xfds);glib_select_fill (&nfds, &rfds, &wfds, &xfds, &tv); +if (timeout <UINT32_MAX ) { + tvarg = &tv; + tv.tv_sec = timeout /1000 ; + tv.tv_usec = (timeout %1000 ) *1000 ; + } +if (timeout >0 ) {qemu_mutex_unlock_iothread (); } - ret =select (nfds +1 , &rfds, &wfds, &xfds, &tv); + ret =select (nfds +1 , &rfds, &wfds, &xfds, tvarg);if (timeout >0 ) {qemu_mutex_lock_iothread ();
main-loop.h +1 -1
@@ -364,6 +364,6 @@ void qemu_iohandler_poll(fd_set *readfds, fd_set *writefds, fd_set *xfds, int rcvoid qemu_bh_schedule_idle (QEMUBH *bh);int qemu_bh_poll (void ); -void qemu_bh_update_timeout (int *timeout); +void qemu_bh_update_timeout (uint32_t *timeout);#endif
qemu-timer.c +0 -5
@@ -821,8 +821,3 @@ fail:return err; } -int qemu_calculate_timeout (void ) -{ -return 1000 ; -} -
qemu-timer.h +0 -1
@@ -62,7 +62,6 @@ uint64_t qemu_timer_expire_time_ns(QEMUTimer *ts);void qemu_run_all_timers (void );int qemu_alarm_pending (void );void configure_alarms (char const *opt); -int qemu_calculate_timeout (void );void init_clocks (void );int init_timer_alarm (void );
qemu-tool.c +4 -0
@@ -91,6 +91,10 @@ int qemu_init_main_loop(void)return main_loop_init (); } +void slirp_update_timeout (uint32_t *timeout) +{ +} +void slirp_select_fill (int *pnfds,fd_set *readfds,fd_set *writefds,fd_set *xfds) {
slirp/libslirp.h +1 -0
@@ -15,6 +15,7 @@ Slirp *slirp_init(int restricted, struct in_addr vnetwork, struct in_addr vnameserver, void *opaque);void slirp_cleanup (Slirp *slirp); +void slirp_update_timeout (uint32_t *timeout);void slirp_select_fill (int *pnfds,fd_set *readfds,fd_set *writefds,fd_set *xfds);
slirp/slirp.c +7 -0
@@ -255,6 +255,13 @@ void slirp_cleanup(Slirp *slirp)#define CONN_CANFRCV (so) (((so)->so_state & (SS_FCANTRCVMORE|SS_ISFCONNECTED)) == SS_ISFCONNECTED)#define UPD_NFDS (x) if (nfds < (x)) nfds = (x) +void slirp_update_timeout (uint32_t *timeout) +{ +if (!QTAILQ_EMPTY (&slirp_instances)) { + *timeout =MIN (1000 , *timeout); + } +} +void slirp_select_fill (int *pnfds,fd_set *readfds,fd_set *writefds,fd_set *xfds) {
Signature · 1 line
1.7.2.5
Hi stefano,
I see the hypervisor has supported the event channel for buffered io, but both qemu upstream and qemu-xen lack of the support. If those patches still pending in qemu upstream, can we adopt them to qemu-xen firstly? It really benefit power efficiency greatly.
best regards
yang
Quoted 96 lines
-----Original Message-----
From: [email protected] [mailto:[email protected]] On Behalf Of Stefano Stabellini
Sent: Tuesday, February 14, 2012 11:06 PM
To: [email protected]
Cc: [email protected]; Stefano Stabellini; Jan Kiszka; Avi Kivity; Anthony Liguori; Paolo Bonzini
Subject: [Xen-devel] [PATCH v5 0/6] prevent QEMU from waking up needlesslyHi all,
this small patch series prevents QEMU from waking up needlessly on Xen several times a second in order to check some timers.I would like some comments (or some acks ;-) on patch #4 and #5.
The first patch stops QEMU from emulating the PIT on Xen, the second patch disables the
rtc_clockentirely.The third patch makes use of a new mechanism to receive buffered io event notifications from Xen, so that QEMU doesn't need to check the buffered io page for data 10 times a sec for the entire life of the VM.
The fourth patch fixes
win32_rearm_timerandmm_rearm_timerthat risk an overflow ifINT64_MAXis passed as delta.The fifth patch changes
qemu_next_alarm_deadlineonly to check the expire time of a clock if it is enabled.Finally the last patch makes
main_loop_waitwait indefinitely on select unless we actually need a timeout.Changes in v5:
- remove the last patch to increase the timeout to 1h, replace it with a patch to wait indefinitely on select unless we need a timeout.
Changes in v4:
- do not initialize pcspk on xen;
- disable
rtc_clockonly when it points to thehost_clock(the default);- make sure it compiles on older xen versions.
Changes in v3:
- added a new patch to fix
win32_rearm_timerandmm_rearm_timer, that risk an overflow ifINT64_MAXis passed as delta.Shortlog and diffstat follow:
async.c | 2 +- hw/pc.c | 9 ++++++--- main-loop.c | 21 ++++++++++++--------- main-loop.h | 2 +- qemu-timer.c | 33 +++++++++++++++++---------------- qemu-timer.h | 1 - qemu-tool.c | 4 ++++ slirp/libslirp.h | 1 + slirp/slirp.c | 7 +++++++ xen-all.c | 49 +++++++++++++++++++++++++++++++++++++++++++------ 10 files changed, 92 insertions(+), 37 deletions(-)Stefano Stabellini (6):
xen: do not initialize the interval timer and PCSPK emulator
xen: disablertc_clock
xen: introduce an event channel for buffered io event notifications
timers: the rearm function should be able to handle delta =INT64_MAX
qemu_next_alarm_deadline: check the expire time of a clock only if it is
enabled
main_loop_wait: block indefinitelyA git tree, based on
9d4df9c02866f39d3eef105033091f367cc7c29e, is available here:
git://xenbits.xen.org/people/sstabellini/qemu-dm.gittimers-5Cheers,
Stefano
_______________________________________________
Xen-devel mailing list
[email protected]
http://lists.xensource.com/xen-devel
Quoted 3 lines · Yang Z
Hi stefano,
I see the hypervisor has supported the event channel for buffered io, but both qemu upstream and qemu-xen lack of the support. If those patches still pending in qemu upstream, can we adopt them to qemu-xen firstly? It really benefit power efficiency greatly.
I agree, I think we should repost a qemu-xen-traditional version of those patches before xen 4.2
Quoted 7 lines · Stefano Stabellini
Quoted 3 lines · Yang Z
Hi stefano,
I see the hypervisor has supported the event channel for buffered io, but both qemu upstream and qemu-xen lack of the support. If those patches still pending in qemu upstream, can we adopt them to qemu-xen firstly? It really benefit power efficiency greatly.
I agree, I think we should repost a qemu-xen-traditional version of those patches before xen 4.2
My mistake: the qemu-xen-traditional version of this patch series has already been posted and committed, see:
Quoted 17 lines
-----Original Message-----
From: Stefano Stabellini [mailto:[email protected]]
Sent: Friday, March 16, 2012 8:36 PM
To: Stefano Stabellini
Cc: Zhang, Yang Z; [email protected]
Subject: RE: [Xen-devel] [PATCH v5 0/6] prevent QEMU from waking up needlesslyQuoted 4 lines · Stefano Stabellini
Quoted 3 lines · Yang Z
Hi stefano,
I see the hypervisor has supported the event channel for buffered io, but
both qemu upstream and qemu-xen lack of the support. If those patches still pending in qemu upstream, can we adopt them to qemu-xen firstly? It really benefit power efficiency greatly.
Quoted 1 line
Quoted 7 lines
Quoted 2 lines
I agree, I think we should repost a qemu-xen-traditional version of those patches before xen 4.2
My mistake: the qemu-xen-traditional version of this patch series has already been posted and committed, see:
Sorry, still cannot find the patch 1. Seems only patch 2 and patch 3 were applied.
best regards
yang
Quoted 17 lines · Yang Z
Quoted 17 lines
-----Original Message-----
From: Stefano Stabellini [mailto:[email protected]]
Sent: Friday, March 16, 2012 8:36 PM
To: Stefano Stabellini
Cc: Zhang, Yang Z; [email protected]
Subject: RE: [Xen-devel] [PATCH v5 0/6] prevent QEMU from waking up needlesslyQuoted 4 lines · Stefano Stabellini
Quoted 3 lines · Yang Z
Hi stefano,
I see the hypervisor has supported the event channel for buffered io, but
both qemu upstream and qemu-xen lack of the support. If those patches still pending in qemu upstream, can we adopt them to qemu-xen firstly? It really benefit power efficiency greatly.
Quoted 1 line
Quoted 9 lines
Quoted 7 lines
Quoted 2 lines
I agree, I think we should repost a qemu-xen-traditional version of those patches before xen 4.2
My mistake: the qemu-xen-traditional version of this patch series has already been posted and committed, see:
Sorry, still cannot find the patch 1. Seems only patch 2 and patch 3 were applied.
you are right, patch #1 has not been applied!
http://marc.info/?l=xen-devel&m=132162531406171
CC'ing Ian Jackson.