提交 4a998740 编写于 作者: P Paolo Bonzini

add a generic scaling mechanism for timers

This enables rt_clock timers to use nanosecond resolution, just by
using the _ns functions; there is really no reason to forbid that.

Migrated timers are all using vm_clock (of course; but I checked that
anyway) so the timers in the savevm files are already in nanosecond
resolution.  So this patch makes no change to the migration format.
Signed-off-by: NPaolo Bonzini <pbonzini@redhat.com>
上级 74475455
...@@ -153,12 +153,12 @@ void cpu_disable_ticks(void) ...@@ -153,12 +153,12 @@ void cpu_disable_ticks(void)
struct QEMUClock { struct QEMUClock {
int type; int type;
int enabled; int enabled;
/* XXX: add frequency */
}; };
struct QEMUTimer { struct QEMUTimer {
QEMUClock *clock; QEMUClock *clock;
int64_t expire_time; int64_t expire_time; /* in nanoseconds */
int scale;
QEMUTimerCB *cb; QEMUTimerCB *cb;
void *opaque; void *opaque;
struct QEMUTimer *next; struct QEMUTimer *next;
...@@ -386,7 +386,8 @@ void qemu_clock_enable(QEMUClock *clock, int enabled) ...@@ -386,7 +386,8 @@ void qemu_clock_enable(QEMUClock *clock, int enabled)
clock->enabled = enabled; clock->enabled = enabled;
} }
QEMUTimer *qemu_new_timer(QEMUClock *clock, QEMUTimerCB *cb, void *opaque) QEMUTimer *qemu_new_timer(QEMUClock *clock, int scale,
QEMUTimerCB *cb, void *opaque)
{ {
QEMUTimer *ts; QEMUTimer *ts;
...@@ -394,6 +395,7 @@ QEMUTimer *qemu_new_timer(QEMUClock *clock, QEMUTimerCB *cb, void *opaque) ...@@ -394,6 +395,7 @@ QEMUTimer *qemu_new_timer(QEMUClock *clock, QEMUTimerCB *cb, void *opaque)
ts->clock = clock; ts->clock = clock;
ts->cb = cb; ts->cb = cb;
ts->opaque = opaque; ts->opaque = opaque;
ts->scale = scale;
return ts; return ts;
} }
...@@ -424,7 +426,7 @@ void qemu_del_timer(QEMUTimer *ts) ...@@ -424,7 +426,7 @@ void qemu_del_timer(QEMUTimer *ts)
/* modify the current timer so that it will be fired when current_time /* modify the current timer so that it will be fired when current_time
>= expire_time. The corresponding callback will be called. */ >= expire_time. The corresponding callback will be called. */
void qemu_mod_timer(QEMUTimer *ts, int64_t expire_time) static void qemu_mod_timer_ns(QEMUTimer *ts, int64_t expire_time)
{ {
QEMUTimer **pt, *t; QEMUTimer **pt, *t;
...@@ -457,6 +459,13 @@ void qemu_mod_timer(QEMUTimer *ts, int64_t expire_time) ...@@ -457,6 +459,13 @@ void qemu_mod_timer(QEMUTimer *ts, int64_t expire_time)
} }
} }
/* modify the current timer so that it will be fired when current_time
>= expire_time. The corresponding callback will be called. */
void qemu_mod_timer(QEMUTimer *ts, int64_t expire_time)
{
qemu_mod_timer_ns(ts, expire_time * ts->scale);
}
int qemu_timer_pending(QEMUTimer *ts) int qemu_timer_pending(QEMUTimer *ts)
{ {
QEMUTimer *t; QEMUTimer *t;
...@@ -471,7 +480,7 @@ int qemu_timer_expired(QEMUTimer *timer_head, int64_t current_time) ...@@ -471,7 +480,7 @@ int qemu_timer_expired(QEMUTimer *timer_head, int64_t current_time)
{ {
if (!timer_head) if (!timer_head)
return 0; return 0;
return (timer_head->expire_time <= current_time); return (timer_head->expire_time <= current_time * timer_head->scale);
} }
static void qemu_run_timers(QEMUClock *clock) static void qemu_run_timers(QEMUClock *clock)
...@@ -482,7 +491,7 @@ static void qemu_run_timers(QEMUClock *clock) ...@@ -482,7 +491,7 @@ static void qemu_run_timers(QEMUClock *clock)
if (!clock->enabled) if (!clock->enabled)
return; return;
current_time = qemu_get_clock (clock); current_time = qemu_get_clock_ns(clock);
ptimer_head = &active_timers[clock->type]; ptimer_head = &active_timers[clock->type];
for(;;) { for(;;) {
ts = *ptimer_head; ts = *ptimer_head;
...@@ -559,7 +568,7 @@ void qemu_get_timer(QEMUFile *f, QEMUTimer *ts) ...@@ -559,7 +568,7 @@ void qemu_get_timer(QEMUFile *f, QEMUTimer *ts)
expire_time = qemu_get_be64(f); expire_time = qemu_get_be64(f);
if (expire_time != -1) { if (expire_time != -1) {
qemu_mod_timer(ts, expire_time); qemu_mod_timer_ns(ts, expire_time);
} else { } else {
qemu_del_timer(ts); qemu_del_timer(ts);
} }
...@@ -717,7 +726,7 @@ static int64_t qemu_next_alarm_deadline(void) ...@@ -717,7 +726,7 @@ static int64_t qemu_next_alarm_deadline(void)
delta = hdelta; delta = hdelta;
} }
if (active_timers[QEMU_CLOCK_REALTIME]) { if (active_timers[QEMU_CLOCK_REALTIME]) {
rtdelta = (active_timers[QEMU_CLOCK_REALTIME]->expire_time * 1000000 - rtdelta = (active_timers[QEMU_CLOCK_REALTIME]->expire_time -
qemu_get_clock_ns(rt_clock)); qemu_get_clock_ns(rt_clock));
if (rtdelta < delta) if (rtdelta < delta)
delta = rtdelta; delta = rtdelta;
......
...@@ -41,7 +41,8 @@ int64_t qemu_get_clock(QEMUClock *clock); ...@@ -41,7 +41,8 @@ int64_t qemu_get_clock(QEMUClock *clock);
int64_t qemu_get_clock_ns(QEMUClock *clock); int64_t qemu_get_clock_ns(QEMUClock *clock);
void qemu_clock_enable(QEMUClock *clock, int enabled); void qemu_clock_enable(QEMUClock *clock, int enabled);
QEMUTimer *qemu_new_timer(QEMUClock *clock, QEMUTimerCB *cb, void *opaque); QEMUTimer *qemu_new_timer(QEMUClock *clock, int scale,
QEMUTimerCB *cb, void *opaque);
void qemu_free_timer(QEMUTimer *ts); void qemu_free_timer(QEMUTimer *ts);
void qemu_del_timer(QEMUTimer *ts); void qemu_del_timer(QEMUTimer *ts);
void qemu_mod_timer(QEMUTimer *ts, int64_t expire_time); void qemu_mod_timer(QEMUTimer *ts, int64_t expire_time);
...@@ -61,15 +62,13 @@ void quit_timers(void); ...@@ -61,15 +62,13 @@ void quit_timers(void);
static inline QEMUTimer *qemu_new_timer_ns(QEMUClock *clock, QEMUTimerCB *cb, static inline QEMUTimer *qemu_new_timer_ns(QEMUClock *clock, QEMUTimerCB *cb,
void *opaque) void *opaque)
{ {
assert(clock != rt_clock); return qemu_new_timer(clock, SCALE_NS, cb, opaque);
return qemu_new_timer(clock, cb, opaque);
} }
static inline QEMUTimer *qemu_new_timer_ms(QEMUClock *clock, QEMUTimerCB *cb, static inline QEMUTimer *qemu_new_timer_ms(QEMUClock *clock, QEMUTimerCB *cb,
void *opaque) void *opaque)
{ {
assert(clock == rt_clock); return qemu_new_timer(clock, SCALE_MS, cb, opaque);
return qemu_new_timer(clock, cb, opaque);
} }
static inline int64_t qemu_get_clock_ms(QEMUClock *clock) static inline int64_t qemu_get_clock_ms(QEMUClock *clock)
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册