[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[Qemu-devel] [RFC] [PATCHv4 05/13] aio / timers: Split QEMUClock into QE
From: |
Alex Bligh |
Subject: |
[Qemu-devel] [RFC] [PATCHv4 05/13] aio / timers: Split QEMUClock into QEMUClock and QEMUTimerList |
Date: |
Fri, 26 Jul 2013 19:37:34 +0100 |
Split 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.
Signed-off-by: Alex Bligh <address@hidden>
---
include/qemu/timer.h | 26 +++++++++
qemu-timer.c | 150 +++++++++++++++++++++++++++++++++++++++-----------
2 files changed, 143 insertions(+), 33 deletions(-)
diff --git a/include/qemu/timer.h b/include/qemu/timer.h
index a1f2ac8..e627033 100644
--- a/include/qemu/timer.h
+++ b/include/qemu/timer.h
@@ -16,6 +16,7 @@
#define QEMU_CLOCK_HOST 2
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
@@ -38,11 +39,19 @@ extern QEMUClock *host_clock;
QEMUClock *qemu_new_clock(int type);
void qemu_free_clock(QEMUClock *clock);
+QEMUTimerList *qemu_new_timerlist(QEMUClock *clock);
+void qemu_free_timerlist(QEMUTimerList *tl);
int64_t qemu_get_clock_ns(QEMUClock *clock);
int64_t qemu_clock_has_timers(QEMUClock *clock);
int64_t qemu_clock_expired(QEMUClock *clock);
int64_t qemu_clock_deadline(QEMUClock *clock);
int64_t qemu_clock_deadline_ns(QEMUClock *clock);
+int64_t qemu_timerlist_has_timers(QEMUTimerList *tl);
+int64_t qemu_timerlist_expired(QEMUTimerList *tl);
+int64_t qemu_timerlist_deadline(QEMUTimerList *tl);
+int64_t qemu_timerlist_deadline_ns(QEMUTimerList *tl);
+QEMUClock *qemu_timerlist_get_clock(QEMUTimerList *tl);
+QEMUTimerList *qemu_clock_get_default_timerlist(QEMUClock *clock);
int qemu_timeout_ns_to_ms(int64_t ns);
int qemu_poll_ns(GPollFD *fds, uint nfds, int64_t timeout);
void qemu_clock_enable(QEMUClock *clock, bool enabled);
@@ -54,6 +63,8 @@ void qemu_unregister_clock_reset_notifier(QEMUClock *clock,
QEMUTimer *qemu_new_timer(QEMUClock *clock, int scale,
QEMUTimerCB *cb, void *opaque);
+QEMUTimer *qemu_new_timer_timerlist(QEMUTimerList *tl, int scale,
+ QEMUTimerCB *cb, void *opaque);
void qemu_free_timer(QEMUTimer *ts);
void qemu_del_timer(QEMUTimer *ts);
void qemu_mod_timer_ns(QEMUTimer *ts, int64_t expire_time);
@@ -63,6 +74,7 @@ bool qemu_timer_expired(QEMUTimer *timer_head, int64_t
current_time);
uint64_t qemu_timer_expire_time_ns(QEMUTimer *ts);
bool qemu_run_timers(QEMUClock *clock);
+bool qemu_run_timerlist(QEMUTimerList *tl);
bool qemu_run_all_timers(void);
void configure_alarms(char const *opt);
void init_clocks(void);
@@ -87,12 +99,26 @@ static inline QEMUTimer *qemu_new_timer_ns(QEMUClock
*clock, QEMUTimerCB *cb,
return qemu_new_timer(clock, SCALE_NS, cb, opaque);
}
+static inline QEMUTimer *qemu_new_timer_timerlist_ns(QEMUTimerList *tl,
+ QEMUTimerCB *cb,
+ void *opaque)
+{
+ return qemu_new_timer_timerlist(tl, SCALE_NS, cb, opaque);
+}
+
static inline QEMUTimer *qemu_new_timer_ms(QEMUClock *clock, QEMUTimerCB *cb,
void *opaque)
{
return qemu_new_timer(clock, SCALE_MS, cb, opaque);
}
+static inline QEMUTimer *qemu_new_timer_timerlist_ms(QEMUTimerList *tl,
+ QEMUTimerCB *cb,
+ void *opaque)
+{
+ return qemu_new_timer_timerlist(tl, SCALE_MS, cb, opaque);
+}
+
static inline int64_t qemu_get_clock_ms(QEMUClock *clock)
{
return qemu_get_clock_ns(clock) / SCALE_MS;
diff --git a/qemu-timer.c b/qemu-timer.c
index 714bc92..211c379 100644
--- a/qemu-timer.c
+++ b/qemu-timer.c
@@ -49,7 +49,8 @@
/* timers */
struct QEMUClock {
- QEMUTimer *active_timers;
+ QEMUTimerList *default_timerlist;
+ QLIST_HEAD(, QEMUTimerList) timerlists;
NotifierList reset_notifiers;
int64_t last;
@@ -58,9 +59,22 @@ struct QEMUClock {
bool enabled;
};
+/* 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 *tl;
QEMUTimerCB *cb;
void *opaque;
QEMUTimer *next;
@@ -93,21 +107,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;
}
@@ -244,14 +262,37 @@ QEMUClock *qemu_new_clock(int type)
clock->enabled = true;
clock->last = INT64_MIN;
notifier_list_init(&clock->reset_notifiers);
+ clock->default_timerlist = qemu_new_timerlist(clock);
return clock;
}
void qemu_free_clock(QEMUClock *clock)
{
+ qemu_free_timerlist(clock->default_timerlist);
g_free(clock);
}
+QEMUTimerList *qemu_new_timerlist(QEMUClock *clock)
+{
+ QEMUTimerList *tl;
+
+ tl = g_malloc0(sizeof(QEMUTimerList));
+ tl->clock = clock;
+ QLIST_INSERT_HEAD(&clock->timerlists, tl, list);
+ return tl;
+}
+
+void qemu_free_timerlist(QEMUTimerList *tl)
+{
+ if (tl->clock) {
+ QLIST_REMOVE(tl, list);
+ if (tl->clock->default_timerlist == tl) {
+ tl->clock->default_timerlist = NULL;
+ }
+ }
+ g_free(tl);
+}
+
void qemu_clock_enable(QEMUClock *clock, bool enabled)
{
bool old = clock->enabled;
@@ -261,24 +302,34 @@ void qemu_clock_enable(QEMUClock *clock, bool enabled)
}
}
+int64_t qemu_timerlist_has_timers(QEMUTimerList *tl)
+{
+ return !!tl->active_timers;
+}
+
int64_t qemu_clock_has_timers(QEMUClock *clock)
{
- return !!clock->active_timers;
+ return qemu_timerlist_has_timers(clock->default_timerlist);
+}
+
+int64_t qemu_timerlist_expired(QEMUTimerList *tl)
+{
+ return (tl->active_timers &&
+ tl->active_timers->expire_time < qemu_get_clock_ns(tl->clock));
}
int64_t qemu_clock_expired(QEMUClock *clock)
{
- return (clock->active_timers &&
- clock->active_timers->expire_time < qemu_get_clock_ns(clock));
+ return qemu_timerlist_expired(clock->default_timerlist);
}
-int64_t qemu_clock_deadline(QEMUClock *clock)
+int64_t qemu_timerlist_deadline(QEMUTimerList *tl)
{
/* 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 (tl->clock->enabled && tl->active_timers) {
+ delta = tl->active_timers->expire_time - qemu_get_clock_ns(tl->clock);
}
if (delta < 0) {
delta = 0;
@@ -286,20 +337,25 @@ int64_t qemu_clock_deadline(QEMUClock *clock)
return delta;
}
+int64_t qemu_clock_deadline(QEMUClock *clock)
+{
+ return qemu_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 qemu_timerlist_deadline_ns(QEMUTimerList *tl)
{
int64_t delta;
- if (!clock->enabled || !clock->active_timers) {
+ if (!tl->clock->enabled || !tl->active_timers) {
return -1;
}
- delta = clock->active_timers->expire_time - qemu_get_clock_ns(clock);
+ delta = tl->active_timers->expire_time - qemu_get_clock_ns(tl->clock);
if (delta <= 0) {
return 0;
@@ -308,6 +364,21 @@ int64_t qemu_clock_deadline_ns(QEMUClock *clock)
return delta;
}
+int64_t qemu_clock_deadline_ns(QEMUClock *clock)
+{
+ return qemu_timerlist_deadline_ns(clock->default_timerlist);
+}
+
+QEMUClock *qemu_timerlist_get_clock(QEMUTimerList *tl)
+{
+ return tl->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 +427,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 *qemu_new_timer_timerlist(QEMUTimerList *tl, int scale,
+ QEMUTimerCB *cb, void *opaque)
{
QEMUTimer *ts;
ts = g_malloc0(sizeof(QEMUTimer));
- ts->clock = clock;
+ ts->tl = tl;
ts->cb = cb;
ts->opaque = opaque;
ts->scale = scale;
return ts;
}
+QEMUTimer *qemu_new_timer(QEMUClock *clock, int scale,
+ QEMUTimerCB *cb, void *opaque)
+{
+ return qemu_new_timer_timerlist(clock->default_timerlist,
+ scale, cb, opaque);
+}
+
void qemu_free_timer(QEMUTimer *ts)
{
g_free(ts);
@@ -381,7 +459,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->tl->active_timers;
for(;;) {
t = *pt;
if (!t)
@@ -405,7 +483,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->tl->active_timers;
for(;;) {
t = *pt;
if (!qemu_timer_expired_ns(t, expire_time)) {
@@ -418,12 +496,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->tl->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->tl->clock);
if (use_icount) {
qemu_notify_event();
}
@@ -438,7 +516,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->tl->active_timers; t != NULL; t = t->next) {
if (t == ts) {
return true;
}
@@ -451,23 +529,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 qemu_run_timerlist(QEMUTimerList *tl)
{
QEMUTimer *ts;
int64_t current_time;
bool progress = false;
- if (!clock->enabled)
+ if (!tl->clock->enabled) {
return progress;
+ }
- current_time = qemu_get_clock_ns(clock);
+ current_time = qemu_get_clock_ns(tl->clock);
for(;;) {
- ts = clock->active_timers;
+ ts = tl->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;
+ tl->active_timers = ts->next;
ts->next = NULL;
/* run the callback (the timer list can be modified) */
@@ -477,6 +556,11 @@ bool qemu_run_timers(QEMUClock *clock)
return progress;
}
+bool qemu_run_timers(QEMUClock *clock)
+{
+ return qemu_run_timerlist(clock->default_timerlist);
+}
+
int64_t qemu_get_clock_ns(QEMUClock *clock)
{
int64_t now, last;
--
1.7.9.5
- [Qemu-devel] [RFC] [PATCHv4 03/13] aio / timers: Add prctl(PR_SET_TIMERSLACK, 1, ...) to reduce timer slack, (continued)
- [Qemu-devel] [RFC] [PATCHv4 03/13] aio / timers: Add prctl(PR_SET_TIMERSLACK, 1, ...) to reduce timer slack, Alex Bligh, 2013/07/26
- [Qemu-devel] [RFC] [PATCHv4 01/13] aio / timers: add qemu-timer.c utility functions, Alex Bligh, 2013/07/26
- [Qemu-devel] [RFC] [PATCHv4 04/13] aio / timers: Make qemu_run_timers and qemu_run_all_timers return progress, Alex Bligh, 2013/07/26
- [Qemu-devel] [RFC] [PATCHv4 02/13] aio / timers: add ppoll support with qemu_poll_ns, Alex Bligh, 2013/07/26
- [Qemu-devel] [RFC] [PATCHv4 06/13] aio / timers: Add a QEMUTimerList to AioContext, Alex Bligh, 2013/07/26
- [Qemu-devel] [RFC] [PATCHv4 08/13] aio / timers: aio_ctx_prepare sets timeout from AioContext timers, Alex Bligh, 2013/07/26
- [Qemu-devel] [RFC] [PATCHv4 07/13] aio / timers: Add an AioContext pointer to QEMUTimerList, Alex Bligh, 2013/07/26
- [Qemu-devel] [RFC] [PATCHv4 11/13] aio / timers: on timer modification, qemu_notify or aio_notify, Alex Bligh, 2013/07/26
- [Qemu-devel] [RFC] [PATCHv4 09/13] aio / timers: Convert aio_poll to use AioContext timers' deadline, Alex Bligh, 2013/07/26
- [Qemu-devel] [RFC] [PATCHv4 10/13] aio / timers: Convert mainloop to use timeout, Alex Bligh, 2013/07/26
- [Qemu-devel] [RFC] [PATCHv4 05/13] aio / timers: Split QEMUClock into QEMUClock and QEMUTimerList,
Alex Bligh <=
- [Qemu-devel] [RFC] [PATCHv4 12/13] aio / timers: Remove alarm timers, Alex Bligh, 2013/07/26
- [Qemu-devel] [RFC] [PATCHv4 13/13] aio / timers: Add test harness for AioContext timers, Alex Bligh, 2013/07/26