Skip to contentSkip to conversation

Correct alarm deadline computation

PATCH In review Paolo Bonzini 2 participants 3 messages Jan 29, 2011 – Jan 30, 2011

lore
Share & tools
lore
#0 lore raw

PATCH 1/2 Correct alarm deadline computation

When the QEMU_CLOCK_HOST clock was added, computation of its deadline was added to qemu_next_deadline, which is correct but incomplete.

I noticed this while trying to make sense of the rules whereby qemu_next_deadline_dyntick is computed, which miss QEMU_CLOCK_HOST when use_icount is true. Looking at the history showed this to be just an oversight, as the next patch shows clearly.

This patch inlines qemu_next_deadline into qemu_next_deadline_dyntick, and corrects the logic so that only QEMU_CLOCK_VIRTUAL is skipped for use_icount == true.

---
 qemu-timer.c |   15 +++++++++++----
 1 files changed, 11 insertions(+), 4 deletions(-)
qemu-timer.c +11 -4
index db1ec49..174fd0c 100644
@@ -724,11 +724,18 @@ static uint64_t qemu_next_deadline_dyntick(void)
     int64_t delta;
     int64_t rtdelta;
 
-    if (use_icount)
+    if (!use_icount && active_timers[QEMU_CLOCK_VIRTUAL]) {
+        delta = active_timers[QEMU_CLOCK_VIRTUAL]->expire_time -
+                     qemu_get_clock(vm_clock);
+    } else {
         delta = INT32_MAX;
-    else
-        delta = (qemu_next_deadline() + 999) / 1000;
-
+    }
+    if (active_timers[QEMU_CLOCK_HOST]) {
+        int64_t hdelta = active_timers[QEMU_CLOCK_HOST]->expire_time -
+                 qemu_get_clock(host_clock);
+        if (hdelta < delta)
+            delta = hdelta;
+    }
     if (active_timers[QEMU_CLOCK_REALTIME]) {
         rtdelta = (active_timers[QEMU_CLOCK_REALTIME]->expire_time -
                  qemu_get_clock(rt_clock))*1000;
Signature · 1 line
1.7.3.4
#1 lore raw

PATCH 2/2 Unify alarm deadline computation

Now that qemu_next_deadline_dyntick uses the correct formula, we can simplify the code in host_alarm_handler and eliminate useless duplication.

Also, qemu_next_deadline_dyntick is always used in signed arithmetic, so change its type.

---
 qemu-timer.c |   28 +++++++++++-----------------
 roms/vgabios |    2 +-
 2 files changed, 12 insertions(+), 18 deletions(-)
qemu-timer.c +11 -17
index 174fd0c..1842d7a 100644
@@ -635,6 +635,8 @@ void qemu_run_all_timers(void)
     qemu_run_timers(host_clock);
 }
 
+static int64_t qemu_next_alarm_deadline(void);
+
 #ifdef _WIN32
 static void CALLBACK host_alarm_handler(UINT uTimerID, UINT uMsg,
                                         DWORD_PTR dwUser, DWORD_PTR dw1,
@@ -677,14 +679,7 @@ static void host_alarm_handler(int host_signum)
     }
 #endif
     if (alarm_has_dynticks(t) ||
-        (!use_icount &&
-            qemu_timer_expired(active_timers[QEMU_CLOCK_VIRTUAL],
-                               qemu_get_clock(vm_clock))) ||
-        qemu_timer_expired(active_timers[QEMU_CLOCK_REALTIME],
-                           qemu_get_clock(rt_clock)) ||
-        qemu_timer_expired(active_timers[QEMU_CLOCK_HOST],
-                           qemu_get_clock(host_clock))) {
-
+        qemu_next_alarm_deadline () <= 0) {
         t->expired = alarm_has_dynticks(t);
         t->pending = 1;
         qemu_notify_event();
@@ -715,11 +710,7 @@ int64_t qemu_next_deadline(void)
 
 #ifndef _WIN32
 
-#if defined(__linux__)
-
-#define RTC_FREQ 1024
-
-static uint64_t qemu_next_deadline_dyntick(void)
+static int64_t qemu_next_alarm_deadline(void)
 {
     int64_t delta;
     int64_t rtdelta;
@@ -743,12 +734,13 @@ static uint64_t qemu_next_deadline_dyntick(void)
             delta = rtdelta;
     }
 
-    if (delta < MIN_TIMER_REARM_US)
-        delta = MIN_TIMER_REARM_US;
-
     return delta;
 }
 
+#if defined(__linux__)
+
+#define RTC_FREQ 1024
+
 static void enable_sigio_timer(int fd)
 {
     struct sigaction act;
@@ -903,7 +895,9 @@ static void dynticks_rearm_timer(struct qemu_alarm_timer *t)
         !active_timers[QEMU_CLOCK_HOST])
         return;
 
-    nearest_delta_us = qemu_next_deadline_dyntick();
+    nearest_delta_us = qemu_next_alarm_deadline();
+    if (nearest_delta_us < MIN_TIMER_REARM_US)
+        nearest_delta_us = MIN_TIMER_REARM_US;
 
     /* check whether a timer is already running */
     if (timer_gettime(host_timer, &timeout)) {
Signature · 1 line
1.7.3.4
#2 lore raw
In reply to Paolo Bonzini
Quoted 26 lines · Paolo Bonzini

When the QEMU_CLOCK_HOST clock was added, computation of its deadline was added to qemu_next_deadline, which is correct but incomplete.

I noticed this while trying to make sense of the rules whereby qemu_next_deadline_dyntick is computed, which miss QEMU_CLOCK_HOST when use_icount is true. Looking at the history showed this to be just an oversight, as the next patch shows clearly.

This patch inlines qemu_next_deadline into qemu_next_deadline_dyntick, and corrects the logic so that only QEMU_CLOCK_VIRTUAL is skipped for use_icount == true.

---
 qemu-timer.c |   15 +++++++++++----
 1 files changed, 11 insertions(+), 4 deletions(-)
qemu-timer.c +0 -0
index db1ec49..174fd0c 100644
@@ -724,11 +724,18 @@ static uint64_t qemu_next_deadline_dyntick(void)
     int64_t delta;
     int64_t rtdelta;

qemu-timer.c static uint64_t qemu_next_deadline_dyntick(void) · line 731

Quoted 10 lines
qemu-timer.c +4 -4
 
-    if (use_icount)
+    if (!use_icount && active_timers[QEMU_CLOCK_VIRTUAL]) {
+        delta = active_timers[QEMU_CLOCK_VIRTUAL]->expire_time -
+                     qemu_get_clock(vm_clock);
+    } else {
         delta = INT32_MAX;
-    else
-        delta = (qemu_next_deadline() + 999) / 1000;
-

This computation handled the fact that the value is returned in ns, and we want us.

qemu-timer.c static uint64_t qemu_next_deadline_dyntick(void) · line 738

Quoted 7 lines
qemu-timer.c +7 -0
+    }
+    if (active_timers[QEMU_CLOCK_HOST]) {
+        int64_t hdelta = active_timers[QEMU_CLOCK_HOST]->expire_time -
+                 qemu_get_clock(host_clock);
+        if (hdelta < delta)
+            delta = hdelta;
+    }

And this doesn't appear anymore after your changes.

qemu-timer.c static uint64_t qemu_next_deadline_dyntick(void) · line 741

Quoted 3 lines
qemu-timer.c +0 -0
     if (active_timers[QEMU_CLOCK_REALTIME]) {
         rtdelta = (active_timers[QEMU_CLOCK_REALTIME]->expire_time -
                  qemu_get_clock(rt_clock))*1000;

Here we multiply because the value is returned in ms and we want us.

IMHO we should just use qemu_get_clock_ns() instead, replace MIN_TIMER_REARM_US by MIN_TIMER_REARM_NS, and return a ns value instead. Of course dynticks_rearm_timer() has to be adjusted, but it should not be problematic given it uses ns internally.

Otherwise good catch, the issue is real, and the way to fix it looks correct.

Signature · 2 lines
Aurelien Jarno	                        GPG: 1024D/F1BCDB73
[email protected]                 http://www.aurel32.net