Skip to contentSkip to conversation

allow tools to use the QEMU main loop

PATCH Superseded Paolo Bonzini 2 participants 23 messages Oct 11, 2011 – Oct 18, 2011 Kbuild

v2 of 3 Superseded Latest is v3 →

lore
Share & tools
lore
#0 lore raw

v2 cover allow tools to use the QEMU main loop

This patch series makes the QEMU main loop usable out of the executable, and especially in tools and possibly unit tests. The series already starts using the refactored main loop in qemu-nbd.

This is cleaner because it avoids introducing partial transitions to GIOChannel. Interfacing with the glib main loop is still possible.

The main loop code is currently split in cpus.c and vl.c. Moving it to a new file is easy; the problem is that the main loop depends on the timer infrastructure in qemu-timer.c, and that file currently contains the implementation of icount and the vm_clock. This is bad for the perspective of linking qemu-timer.c into the tools. Luckily, it is relatively easy to untie them and move them out of the way. This is what the largest part of the series does (patches 1-9).

Patches 10-13 complete the refactoring and cleanup some surrounding code.

v1->v2
Rebased

Paolo Bonzini (13):
remove unused function
qemu-timer: remove active_timers array
qemu-timer: move common code to qemu_rearm_alarm_timer
qemu-timer: more clock functions
qemu-timer: move icount to cpus.c
qemu-timer: do not refer to runstate_is_running()
qemu-timer: use atexit for quit_timers
qemu-timer: move more stuff out of qemu-timer.c
qemu-timer: do not use RunState change handlers create main-loop.h
create main-loop.c
Revert to a hand-made select loop
simplify main loop functions

 Makefile.objs    |    2 +-
 async.c          |    1 +
 cpus.c           |  496 ++++++++++++++++++++++++++++++------------------------
 cpus.h           |    3 +-
 exec-all.h       |   14 ++
 exec.c           |    3 -
 hw/mac_dbdma.c   |    5 -
 hw/mac_dbdma.h   |    1 -
 iohandler.c      |   54 +------
 main-loop.c      |  494 +++++++++++++++++++++++++++++++++++++++++++++++++++++
 main-loop.h      |   73 ++++++++
 os-win32.c       |  123 --------------
 qemu-char.h      |   12 +--
 qemu-common.h    |   15 +--
 qemu-os-posix.h  |    4 -
 qemu-os-win32.h  |   15 +--
 qemu-timer.c     |  489 ++++++++++-------------------------------------------
 qemu-timer.h     |   30 +---
 savevm.c         |   25 +++
 slirp/libslirp.h |   11 --
 sysemu.h         |    3 +-
 vl.c             |  189 ++++-----------------
 22 files changed, 1027 insertions(+), 1035 deletions(-)
 create mode 100644 main-loop.c
 create mode 100644 main-loop.h
Signature · 1 line
1.7.6
#1 lore raw

PATCH v2 1/13 remove unused function

---
 hw/mac_dbdma.c |    5 -----
 hw/mac_dbdma.h |    1 -
 2 files changed, 0 insertions(+), 6 deletions(-)
hw/mac_dbdma.c +0 -5
index 5affdd1..1791ec1 100644
@@ -661,11 +661,6 @@ void DBDMA_register_channel(void *dbdma, int nchan, qemu_irq irq,
     ch->io.channel = ch;
 }
 
-void DBDMA_schedule(void)
-{
-    qemu_notify_event();
-}
-
 static void
 dbdma_control_write(DBDMA_channel *ch)
 {
hw/mac_dbdma.h +0 -1
index 933e17c..6d1abe6 100644
@@ -41,5 +41,4 @@ struct DBDMA_io {
 void DBDMA_register_channel(void *dbdma, int nchan, qemu_irq irq,
                             DBDMA_rw rw, DBDMA_flush flush,
                             void *opaque);
-void DBDMA_schedule(void);
 void* DBDMA_init (MemoryRegion **dbdma_mem);
Signature · 1 line
1.7.6
Reviewed-by on v2 1/13
#2 lore raw

Re: [Qemu-devel] [PATCH 01/13] remove unused function

Quoted 1 line · Paolo Bonzini

Regards,

Anthony Liguori

Quoted 31 lines
---
  hw/mac_dbdma.c |    5 -----
  hw/mac_dbdma.h |    1 -
  2 files changed, 0 insertions(+), 6 deletions(-)
hw/mac_dbdma.c +0 -5
index 5affdd1..1791ec1 100644
@@ -661,11 +661,6 @@ void DBDMA_register_channel(void *dbdma, int nchan, qemu_irq irq,
      ch->io.channel = ch;
  }
 
-void DBDMA_schedule(void)
-{
-    qemu_notify_event();
-}
-
  static void
  dbdma_control_write(DBDMA_channel *ch)
  {
hw/mac_dbdma.h +0 -1
index 933e17c..6d1abe6 100644
@@ -41,5 +41,4 @@ struct DBDMA_io {
  void DBDMA_register_channel(void *dbdma, int nchan, qemu_irq irq,
                              DBDMA_rw rw, DBDMA_flush flush,
                              void *opaque);
-void DBDMA_schedule(void);
  void* DBDMA_init (MemoryRegion **dbdma_mem);
#3 lore raw

PATCH v2 2/13 qemu-timer: remove active_timers array

In reply to Paolo Bonzini

Embed the list in the QEMUClock instead.

---
 qemu-timer.c |   59 +++++++++++++++++++++++++++------------------------------
 1 files changed, 28 insertions(+), 31 deletions(-)
qemu-timer.c +28 -31
index ad1fc8b..acf7a15 100644
@@ -134,6 +134,7 @@ struct QEMUClock {
     int enabled;
 
     QEMUTimer *warp_timer;
+    QEMUTimer *active_timers;
 
     NotifierList reset_notifiers;
     int64_t last;
@@ -352,14 +353,10 @@ next:
     }
 }
 
-#define QEMU_NUM_CLOCKS 3
-
 QEMUClock *rt_clock;
 QEMUClock *vm_clock;
 QEMUClock *host_clock;
 
-static QEMUTimer *active_timers[QEMU_NUM_CLOCKS];
-
 static QEMUClock *qemu_new_clock(int type)
 {
     QEMUClock *clock;
@@ -403,7 +400,7 @@ static void icount_warp_rt(void *opaque)
             int64_t delta = cur_time - cur_icount;
             qemu_icount_bias += MIN(warp_delta, delta);
         }
-        if (qemu_timer_expired(active_timers[QEMU_CLOCK_VIRTUAL],
+        if (qemu_timer_expired(vm_clock->active_timers,
                                qemu_get_clock_ns(vm_clock))) {
             qemu_notify_event();
         }
@@ -434,7 +431,7 @@ void qemu_clock_warp(QEMUClock *clock)
      * the earliest vm_clock timer.
      */
     icount_warp_rt(NULL);
-    if (!all_cpu_threads_idle() || !active_timers[clock->type]) {
+    if (!all_cpu_threads_idle() || !clock->active_timers) {
         qemu_del_timer(clock->warp_timer);
         return;
     }
@@ -489,7 +486,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 = &active_timers[ts->clock->type];
+    pt = &ts->clock->active_timers;
     for(;;) {
         t = *pt;
         if (!t)
@@ -513,7 +510,7 @@ static 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 = &active_timers[ts->clock->type];
+    pt = &ts->clock->active_timers;
     for(;;) {
         t = *pt;
         if (!qemu_timer_expired_ns(t, expire_time)) {
@@ -526,7 +523,7 @@ static void qemu_mod_timer_ns(QEMUTimer *ts, int64_t expire_time)
     *pt = ts;
 
     /* Rearm if necessary  */
-    if (pt == &active_timers[ts->clock->type]) {
+    if (pt == &ts->clock->active_timers) {
         if (!alarm_timer->pending) {
             qemu_rearm_alarm_timer(alarm_timer);
         }
@@ -548,7 +545,7 @@ void qemu_mod_timer(QEMUTimer *ts, int64_t expire_time)
 int qemu_timer_pending(QEMUTimer *ts)
 {
     QEMUTimer *t;
-    for(t = active_timers[ts->clock->type]; t != NULL; t = t->next) {
+    for (t = ts->clock->active_timers; t != NULL; t = t->next) {
         if (t == ts)
             return 1;
     }
@@ -569,7 +566,7 @@ static void qemu_run_timers(QEMUClock *clock)
         return;
 
     current_time = qemu_get_clock_ns(clock);
-    ptimer_head = &active_timers[clock->type];
+    ptimer_head = &clock->active_timers;
     for(;;) {
         ts = *ptimer_head;
         if (!qemu_timer_expired_ns(ts, current_time)) {
@@ -773,8 +770,8 @@ int64_t qemu_next_icount_deadline(void)
     int64_t delta = INT32_MAX;
 
     assert(use_icount);
-    if (active_timers[QEMU_CLOCK_VIRTUAL]) {
-        delta = active_timers[QEMU_CLOCK_VIRTUAL]->expire_time -
+    if (vm_clock->active_timers) {
+        delta = vm_clock->active_timers->expire_time -
                      qemu_get_clock_ns(vm_clock);
     }
 
@@ -789,20 +786,20 @@ static int64_t qemu_next_alarm_deadline(void)
     int64_t delta;
     int64_t rtdelta;
 
-    if (!use_icount && active_timers[QEMU_CLOCK_VIRTUAL]) {
-        delta = active_timers[QEMU_CLOCK_VIRTUAL]->expire_time -
+    if (!use_icount && vm_clock->active_timers) {
+        delta = vm_clock->active_timers->expire_time -
                      qemu_get_clock_ns(vm_clock);
     } else {
         delta = INT32_MAX;
     }
-    if (active_timers[QEMU_CLOCK_HOST]) {
-        int64_t hdelta = active_timers[QEMU_CLOCK_HOST]->expire_time -
+    if (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 (active_timers[QEMU_CLOCK_REALTIME]) {
-        rtdelta = (active_timers[QEMU_CLOCK_REALTIME]->expire_time -
+    if (rt_clock->active_timers) {
+        rtdelta = (rt_clock->active_timers->expire_time -
                  qemu_get_clock_ns(rt_clock));
         if (rtdelta < delta)
             delta = rtdelta;
@@ -871,9 +868,9 @@ static void dynticks_rearm_timer(struct qemu_alarm_timer *t)
     int64_t current_ns;
 
     assert(alarm_has_dynticks(t));
-    if (!active_timers[QEMU_CLOCK_REALTIME] &&
-        !active_timers[QEMU_CLOCK_VIRTUAL] &&
-        !active_timers[QEMU_CLOCK_HOST])
+    if (!rt_clock->active_timers &&
+        !vm_clock->active_timers &&
+        !host_clock->active_timers)
         return;
 
     nearest_delta_ns = qemu_next_alarm_deadline();
@@ -925,9 +922,9 @@ static void unix_rearm_timer(struct qemu_alarm_timer *t)
     int err;
 
     assert(alarm_has_dynticks(t));
-    if (!active_timers[QEMU_CLOCK_REALTIME] &&
-        !active_timers[QEMU_CLOCK_VIRTUAL] &&
-        !active_timers[QEMU_CLOCK_HOST])
+    if (!rt_clock->active_timers &&
+        !vm_clock->active_timers &&
+        !host_clock->active_timers)
         return;
 
     nearest_delta_ns = qemu_next_alarm_deadline();
@@ -1022,9 +1019,9 @@ static void mm_rearm_timer(struct qemu_alarm_timer *t)
     int nearest_delta_ms;
 
     assert(alarm_has_dynticks(t));
-    if (!active_timers[QEMU_CLOCK_REALTIME] &&
-        !active_timers[QEMU_CLOCK_VIRTUAL] &&
-        !active_timers[QEMU_CLOCK_HOST]) {
+    if (!rt_clock->active_timers &&
+        !vm_clock->active_timers &&
+        !host_clock->active_timers) {
         return;
     }
 
@@ -1092,9 +1089,9 @@ static void win32_rearm_timer(struct qemu_alarm_timer *t)
     BOOLEAN success;
 
     assert(alarm_has_dynticks(t));
-    if (!active_timers[QEMU_CLOCK_REALTIME] &&
-        !active_timers[QEMU_CLOCK_VIRTUAL] &&
-        !active_timers[QEMU_CLOCK_HOST])
+    if (!rt_clock->active_timers &&
+        !vm_clock->active_timers &&
+        !host_clock->active_timers)
         return;
 
     nearest_delta_ms = (qemu_next_alarm_deadline() + 999999) / 1000000;
Signature · 1 line
1.7.6
Reviewed-by on v2 2/13
#4 lore raw

Re: [Qemu-devel] [PATCH 02/13] qemu-timer: remove active_timers array

Quoted 3 lines · Paolo Bonzini

Embed the list in the QEMUClock instead.

Regards,

Anthony Liguori

Quoted 184 lines
---
  qemu-timer.c |   59 +++++++++++++++++++++++++++------------------------------
  1 files changed, 28 insertions(+), 31 deletions(-)
qemu-timer.c +28 -31
index ad1fc8b..acf7a15 100644
@@ -134,6 +134,7 @@ struct QEMUClock {
      int enabled;
 
      QEMUTimer *warp_timer;
+    QEMUTimer *active_timers;
 
      NotifierList reset_notifiers;
      int64_t last;
@@ -352,14 +353,10 @@ next:
      }
  }
 
-#define QEMU_NUM_CLOCKS 3
-
  QEMUClock *rt_clock;
  QEMUClock *vm_clock;
  QEMUClock *host_clock;
 
-static QEMUTimer *active_timers[QEMU_NUM_CLOCKS];
-
  static QEMUClock *qemu_new_clock(int type)
  {
      QEMUClock *clock;
@@ -403,7 +400,7 @@ static void icount_warp_rt(void *opaque)
              int64_t delta = cur_time - cur_icount;
              qemu_icount_bias += MIN(warp_delta, delta);
          }
-        if (qemu_timer_expired(active_timers[QEMU_CLOCK_VIRTUAL],
+        if (qemu_timer_expired(vm_clock->active_timers,
                                 qemu_get_clock_ns(vm_clock))) {
              qemu_notify_event();
          }
@@ -434,7 +431,7 @@ void qemu_clock_warp(QEMUClock *clock)
       * the earliest vm_clock timer.
       */
      icount_warp_rt(NULL);
-    if (!all_cpu_threads_idle() || !active_timers[clock->type]) {
+    if (!all_cpu_threads_idle() || !clock->active_timers) {
          qemu_del_timer(clock->warp_timer);
          return;
      }
@@ -489,7 +486,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 =&active_timers[ts->clock->type];
+    pt =&ts->clock->active_timers;
      for(;;) {
          t = *pt;
          if (!t)
@@ -513,7 +510,7 @@ static 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 =&active_timers[ts->clock->type];
+    pt =&ts->clock->active_timers;
      for(;;) {
          t = *pt;
          if (!qemu_timer_expired_ns(t, expire_time)) {
@@ -526,7 +523,7 @@ static void qemu_mod_timer_ns(QEMUTimer *ts, int64_t expire_time)
      *pt = ts;
 
      /* Rearm if necessary  */
-    if (pt ==&active_timers[ts->clock->type]) {
+    if (pt ==&ts->clock->active_timers) {
          if (!alarm_timer->pending) {
              qemu_rearm_alarm_timer(alarm_timer);
          }
@@ -548,7 +545,7 @@ void qemu_mod_timer(QEMUTimer *ts, int64_t expire_time)
  int qemu_timer_pending(QEMUTimer *ts)
  {
      QEMUTimer *t;
-    for(t = active_timers[ts->clock->type]; t != NULL; t = t->next) {
+    for (t = ts->clock->active_timers; t != NULL; t = t->next) {
          if (t == ts)
              return 1;
      }
@@ -569,7 +566,7 @@ static void qemu_run_timers(QEMUClock *clock)
          return;
 
      current_time = qemu_get_clock_ns(clock);
-    ptimer_head =&active_timers[clock->type];
+    ptimer_head =&clock->active_timers;
      for(;;) {
          ts = *ptimer_head;
          if (!qemu_timer_expired_ns(ts, current_time)) {
@@ -773,8 +770,8 @@ int64_t qemu_next_icount_deadline(void)
      int64_t delta = INT32_MAX;
 
      assert(use_icount);
-    if (active_timers[QEMU_CLOCK_VIRTUAL]) {
-        delta = active_timers[QEMU_CLOCK_VIRTUAL]->expire_time -
+    if (vm_clock->active_timers) {
+        delta = vm_clock->active_timers->expire_time -
                       qemu_get_clock_ns(vm_clock);
      }
 
@@ -789,20 +786,20 @@ static int64_t qemu_next_alarm_deadline(void)
      int64_t delta;
      int64_t rtdelta;
 
-    if (!use_icount&&  active_timers[QEMU_CLOCK_VIRTUAL]) {
-        delta = active_timers[QEMU_CLOCK_VIRTUAL]->expire_time -
+    if (!use_icount&&  vm_clock->active_timers) {
+        delta = vm_clock->active_timers->expire_time -
                       qemu_get_clock_ns(vm_clock);
      } else {
          delta = INT32_MAX;
      }
-    if (active_timers[QEMU_CLOCK_HOST]) {
-        int64_t hdelta = active_timers[QEMU_CLOCK_HOST]->expire_time -
+    if (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 (active_timers[QEMU_CLOCK_REALTIME]) {
-        rtdelta = (active_timers[QEMU_CLOCK_REALTIME]->expire_time -
+    if (rt_clock->active_timers) {
+        rtdelta = (rt_clock->active_timers->expire_time -
                   qemu_get_clock_ns(rt_clock));
          if (rtdelta<  delta)
              delta = rtdelta;
@@ -871,9 +868,9 @@ static void dynticks_rearm_timer(struct qemu_alarm_timer *t)
      int64_t current_ns;
 
      assert(alarm_has_dynticks(t));
-    if (!active_timers[QEMU_CLOCK_REALTIME]&&
-        !active_timers[QEMU_CLOCK_VIRTUAL]&&
-        !active_timers[QEMU_CLOCK_HOST])
+    if (!rt_clock->active_timers&&
+        !vm_clock->active_timers&&
+        !host_clock->active_timers)
          return;
 
      nearest_delta_ns = qemu_next_alarm_deadline();
@@ -925,9 +922,9 @@ static void unix_rearm_timer(struct qemu_alarm_timer *t)
      int err;
 
      assert(alarm_has_dynticks(t));
-    if (!active_timers[QEMU_CLOCK_REALTIME]&&
-        !active_timers[QEMU_CLOCK_VIRTUAL]&&
-        !active_timers[QEMU_CLOCK_HOST])
+    if (!rt_clock->active_timers&&
+        !vm_clock->active_timers&&
+        !host_clock->active_timers)
          return;
 
      nearest_delta_ns = qemu_next_alarm_deadline();
@@ -1022,9 +1019,9 @@ static void mm_rearm_timer(struct qemu_alarm_timer *t)
      int nearest_delta_ms;
 
      assert(alarm_has_dynticks(t));
-    if (!active_timers[QEMU_CLOCK_REALTIME]&&
-        !active_timers[QEMU_CLOCK_VIRTUAL]&&
-        !active_timers[QEMU_CLOCK_HOST]) {
+    if (!rt_clock->active_timers&&
+        !vm_clock->active_timers&&
+        !host_clock->active_timers) {
          return;
      }
 
@@ -1092,9 +1089,9 @@ static void win32_rearm_timer(struct qemu_alarm_timer *t)
      BOOLEAN success;
 
      assert(alarm_has_dynticks(t));
-    if (!active_timers[QEMU_CLOCK_REALTIME]&&
-        !active_timers[QEMU_CLOCK_VIRTUAL]&&
-        !active_timers[QEMU_CLOCK_HOST])
+    if (!rt_clock->active_timers&&
+        !vm_clock->active_timers&&
+        !host_clock->active_timers)
          return;
 
      nearest_delta_ms = (qemu_next_alarm_deadline() + 999999) / 1000000;
#5 lore raw

PATCH v2 3/13 qemu-timer: move common code to qemu_rearm_alarm_timer

In reply to Paolo Bonzini
---
 qemu-timer.c |  129 ++++++++++++++++++++++++----------------------------------
 1 files changed, 53 insertions(+), 76 deletions(-)
qemu-timer.c +53 -76
index acf7a15..e2551f3 100644
@@ -153,7 +153,7 @@ struct qemu_alarm_timer {
     char const *name;
     int (*start)(struct qemu_alarm_timer *t);
     void (*stop)(struct qemu_alarm_timer *t);
-    void (*rearm)(struct qemu_alarm_timer *t);
+    void (*rearm)(struct qemu_alarm_timer *t, int64_t nearest_delta_ns);
 #if defined(__linux__)
     int fd;
     timer_t timer;
@@ -181,12 +181,46 @@ static inline int alarm_has_dynticks(struct qemu_alarm_timer *t)
     return !!t->rearm;
 }
 
+static int64_t qemu_next_alarm_deadline(void)
+{
+    int64_t delta;
+    int64_t rtdelta;
+
+    if (!use_icount && 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) {
+        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) {
+        rtdelta = (rt_clock->active_timers->expire_time -
+                 qemu_get_clock_ns(rt_clock));
+        if (rtdelta < delta) {
+            delta = rtdelta;
+        }
+    }
+
+    return delta;
+}
+
 static void qemu_rearm_alarm_timer(struct qemu_alarm_timer *t)
 {
-    if (!alarm_has_dynticks(t))
+    int64_t nearest_delta_ns;
+    assert(alarm_has_dynticks(t));
+    if (!rt_clock->active_timers &&
+        !vm_clock->active_timers &&
+        !host_clock->active_timers) {
         return;
-
-    t->rearm(t);
+    }
+    nearest_delta_ns = qemu_next_alarm_deadline();
+    t->rearm(t, nearest_delta_ns);
 }
 
 /* TODO: MIN_TIMER_REARM_NS should be optimized */
@@ -196,23 +230,23 @@ static void qemu_rearm_alarm_timer(struct qemu_alarm_timer *t)
 
 static int mm_start_timer(struct qemu_alarm_timer *t);
 static void mm_stop_timer(struct qemu_alarm_timer *t);
-static void mm_rearm_timer(struct qemu_alarm_timer *t);
+static void mm_rearm_timer(struct qemu_alarm_timer *t, int64_t delta);
 
 static int win32_start_timer(struct qemu_alarm_timer *t);
 static void win32_stop_timer(struct qemu_alarm_timer *t);
-static void win32_rearm_timer(struct qemu_alarm_timer *t);
+static void win32_rearm_timer(struct qemu_alarm_timer *t, int64_t delta);
 
 #else
 
 static int unix_start_timer(struct qemu_alarm_timer *t);
 static void unix_stop_timer(struct qemu_alarm_timer *t);
-static void unix_rearm_timer(struct qemu_alarm_timer *t);
+static void unix_rearm_timer(struct qemu_alarm_timer *t, int64_t delta);
 
 #ifdef __linux__
 
 static int dynticks_start_timer(struct qemu_alarm_timer *t);
 static void dynticks_stop_timer(struct qemu_alarm_timer *t);
-static void dynticks_rearm_timer(struct qemu_alarm_timer *t);
+static void dynticks_rearm_timer(struct qemu_alarm_timer *t, int64_t delta);
 
 #endif /* __linux__ */
 
@@ -715,8 +749,6 @@ 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(PVOID lpParam, BOOLEAN unused)
 #else
@@ -781,33 +813,6 @@ int64_t qemu_next_icount_deadline(void)
     return delta;
 }
 
-static int64_t qemu_next_alarm_deadline(void)
-{
-    int64_t delta;
-    int64_t rtdelta;
-
-    if (!use_icount && 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) {
-        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) {
-        rtdelta = (rt_clock->active_timers->expire_time -
-                 qemu_get_clock_ns(rt_clock));
-        if (rtdelta < delta)
-            delta = rtdelta;
-    }
-
-    return delta;
-}
-
 #if defined(__linux__)
 
 #include "compatfd.h"
@@ -860,20 +865,13 @@ static void dynticks_stop_timer(struct qemu_alarm_timer *t)
     timer_delete(host_timer);
 }
 
-static void dynticks_rearm_timer(struct qemu_alarm_timer *t)
+static void dynticks_rearm_timer(struct qemu_alarm_timer *t,
+                                 int64_t nearest_delta_ns)
 {
     timer_t host_timer = t->timer;
     struct itimerspec timeout;
-    int64_t nearest_delta_ns = INT64_MAX;
     int64_t current_ns;
 
-    assert(alarm_has_dynticks(t));
-    if (!rt_clock->active_timers &&
-        !vm_clock->active_timers &&
-        !host_clock->active_timers)
-        return;
-
-    nearest_delta_ns = qemu_next_alarm_deadline();
     if (nearest_delta_ns < MIN_TIMER_REARM_NS)
         nearest_delta_ns = MIN_TIMER_REARM_NS;
 
@@ -915,19 +913,12 @@ static int unix_start_timer(struct qemu_alarm_timer *t)
     return 0;
 }
 
-static void unix_rearm_timer(struct qemu_alarm_timer *t)
+static void unix_rearm_timer(struct qemu_alarm_timer *t,
+                             int64_t nearest_delta_ns)
 {
     struct itimerval itv;
-    int64_t nearest_delta_ns = INT64_MAX;
     int err;
 
-    assert(alarm_has_dynticks(t));
-    if (!rt_clock->active_timers &&
-        !vm_clock->active_timers &&
-        !host_clock->active_timers)
-        return;
-
-    nearest_delta_ns = qemu_next_alarm_deadline();
     if (nearest_delta_ns < MIN_TIMER_REARM_NS)
         nearest_delta_ns = MIN_TIMER_REARM_NS;
 
@@ -1014,23 +1005,14 @@ static void mm_stop_timer(struct qemu_alarm_timer *t)
     timeEndPeriod(mm_period);
 }
 
-static void mm_rearm_timer(struct qemu_alarm_timer *t)
+static void mm_rearm_timer(struct qemu_alarm_timer *t, int64_t delta)
 {
-    int nearest_delta_ms;
-
-    assert(alarm_has_dynticks(t));
-    if (!rt_clock->active_timers &&
-        !vm_clock->active_timers &&
-        !host_clock->active_timers) {
-        return;
-    }
-
-    timeKillEvent(mm_timer);
-
-    nearest_delta_ms = (qemu_next_alarm_deadline() + 999999) / 1000000;
+    int nearest_delta_ms = (delta + 999999) / 1000000;
     if (nearest_delta_ms < 1) {
         nearest_delta_ms = 1;
     }
+
+    timeKillEvent(mm_timer);
     mm_timer = timeSetEvent(nearest_delta_ms,
                             mm_period,
                             mm_alarm_handler,
@@ -1082,19 +1064,14 @@ static void win32_stop_timer(struct qemu_alarm_timer *t)
     }
 }
 
-static void win32_rearm_timer(struct qemu_alarm_timer *t)
+static void win32_rearm_timer(struct qemu_alarm_timer *t,
+                              int64_t nearest_delta_ns)
 {
     HANDLE hTimer = t->timer;
     int nearest_delta_ms;
     BOOLEAN success;
 
-    assert(alarm_has_dynticks(t));
-    if (!rt_clock->active_timers &&
-        !vm_clock->active_timers &&
-        !host_clock->active_timers)
-        return;
-
-    nearest_delta_ms = (qemu_next_alarm_deadline() + 999999) / 1000000;
+    nearest_delta_ms = (nearest_delta_ns + 999999) / 1000000;
     if (nearest_delta_ms < 1) {
         nearest_delta_ms = 1;
     }
Signature · 1 line
1.7.6
Reviewed-by on v2 3/13
#6 lore raw

Re: [Qemu-devel] [PATCH 03/13] qemu-timer: move common code to qemu_rearm_alarm_timer

Quoted 1 line · Paolo Bonzini

Regards,

Anthony Liguori

Quoted 234 lines
---
  qemu-timer.c |  129 ++++++++++++++++++++++++----------------------------------
  1 files changed, 53 insertions(+), 76 deletions(-)
qemu-timer.c +53 -76
index acf7a15..e2551f3 100644
@@ -153,7 +153,7 @@ struct qemu_alarm_timer {
      char const *name;
      int (*start)(struct qemu_alarm_timer *t);
      void (*stop)(struct qemu_alarm_timer *t);
-    void (*rearm)(struct qemu_alarm_timer *t);
+    void (*rearm)(struct qemu_alarm_timer *t, int64_t nearest_delta_ns);
  #if defined(__linux__)
      int fd;
      timer_t timer;
@@ -181,12 +181,46 @@ static inline int alarm_has_dynticks(struct qemu_alarm_timer *t)
      return !!t->rearm;
  }
 
+static int64_t qemu_next_alarm_deadline(void)
+{
+    int64_t delta;
+    int64_t rtdelta;
+
+    if (!use_icount&&  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) {
+        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) {
+        rtdelta = (rt_clock->active_timers->expire_time -
+                 qemu_get_clock_ns(rt_clock));
+        if (rtdelta<  delta) {
+            delta = rtdelta;
+        }
+    }
+
+    return delta;
+}
+
  static void qemu_rearm_alarm_timer(struct qemu_alarm_timer *t)
  {
-    if (!alarm_has_dynticks(t))
+    int64_t nearest_delta_ns;
+    assert(alarm_has_dynticks(t));
+    if (!rt_clock->active_timers&&
+        !vm_clock->active_timers&&
+        !host_clock->active_timers) {
          return;
-
-    t->rearm(t);
+    }
+    nearest_delta_ns = qemu_next_alarm_deadline();
+    t->rearm(t, nearest_delta_ns);
  }
 
  /* TODO: MIN_TIMER_REARM_NS should be optimized */
@@ -196,23 +230,23 @@ static void qemu_rearm_alarm_timer(struct qemu_alarm_timer *t)
 
  static int mm_start_timer(struct qemu_alarm_timer *t);
  static void mm_stop_timer(struct qemu_alarm_timer *t);
-static void mm_rearm_timer(struct qemu_alarm_timer *t);
+static void mm_rearm_timer(struct qemu_alarm_timer *t, int64_t delta);
 
  static int win32_start_timer(struct qemu_alarm_timer *t);
  static void win32_stop_timer(struct qemu_alarm_timer *t);
-static void win32_rearm_timer(struct qemu_alarm_timer *t);
+static void win32_rearm_timer(struct qemu_alarm_timer *t, int64_t delta);
 
  #else
 
  static int unix_start_timer(struct qemu_alarm_timer *t);
  static void unix_stop_timer(struct qemu_alarm_timer *t);
-static void unix_rearm_timer(struct qemu_alarm_timer *t);
+static void unix_rearm_timer(struct qemu_alarm_timer *t, int64_t delta);
 
  #ifdef __linux__
 
  static int dynticks_start_timer(struct qemu_alarm_timer *t);
  static void dynticks_stop_timer(struct qemu_alarm_timer *t);
-static void dynticks_rearm_timer(struct qemu_alarm_timer *t);
+static void dynticks_rearm_timer(struct qemu_alarm_timer *t, int64_t delta);
 
  #endif /* __linux__ */
 
@@ -715,8 +749,6 @@ 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(PVOID lpParam, BOOLEAN unused)
  #else
@@ -781,33 +813,6 @@ int64_t qemu_next_icount_deadline(void)
      return delta;
  }
 
-static int64_t qemu_next_alarm_deadline(void)
-{
-    int64_t delta;
-    int64_t rtdelta;
-
-    if (!use_icount&&  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) {
-        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) {
-        rtdelta = (rt_clock->active_timers->expire_time -
-                 qemu_get_clock_ns(rt_clock));
-        if (rtdelta<  delta)
-            delta = rtdelta;
-    }
-
-    return delta;
-}
-
  #if defined(__linux__)
 
  #include "compatfd.h"
@@ -860,20 +865,13 @@ static void dynticks_stop_timer(struct qemu_alarm_timer *t)
      timer_delete(host_timer);
  }
 
-static void dynticks_rearm_timer(struct qemu_alarm_timer *t)
+static void dynticks_rearm_timer(struct qemu_alarm_timer *t,
+                                 int64_t nearest_delta_ns)
  {
      timer_t host_timer = t->timer;
      struct itimerspec timeout;
-    int64_t nearest_delta_ns = INT64_MAX;
      int64_t current_ns;
 
-    assert(alarm_has_dynticks(t));
-    if (!rt_clock->active_timers&&
-        !vm_clock->active_timers&&
-        !host_clock->active_timers)
-        return;
-
-    nearest_delta_ns = qemu_next_alarm_deadline();
      if (nearest_delta_ns<  MIN_TIMER_REARM_NS)
          nearest_delta_ns = MIN_TIMER_REARM_NS;
 
@@ -915,19 +913,12 @@ static int unix_start_timer(struct qemu_alarm_timer *t)
      return 0;
  }
 
-static void unix_rearm_timer(struct qemu_alarm_timer *t)
+static void unix_rearm_timer(struct qemu_alarm_timer *t,
+                             int64_t nearest_delta_ns)
  {
      struct itimerval itv;
-    int64_t nearest_delta_ns = INT64_MAX;
      int err;
 
-    assert(alarm_has_dynticks(t));
-    if (!rt_clock->active_timers&&
-        !vm_clock->active_timers&&
-        !host_clock->active_timers)
-        return;
-
-    nearest_delta_ns = qemu_next_alarm_deadline();
      if (nearest_delta_ns<  MIN_TIMER_REARM_NS)
          nearest_delta_ns = MIN_TIMER_REARM_NS;
 
@@ -1014,23 +1005,14 @@ static void mm_stop_timer(struct qemu_alarm_timer *t)
      timeEndPeriod(mm_period);
  }
 
-static void mm_rearm_timer(struct qemu_alarm_timer *t)
+static void mm_rearm_timer(struct qemu_alarm_timer *t, int64_t delta)
  {
-    int nearest_delta_ms;
-
-    assert(alarm_has_dynticks(t));
-    if (!rt_clock->active_timers&&
-        !vm_clock->active_timers&&
-        !host_clock->active_timers) {
-        return;
-    }
-
-    timeKillEvent(mm_timer);
-
-    nearest_delta_ms = (qemu_next_alarm_deadline() + 999999) / 1000000;
+    int nearest_delta_ms = (delta + 999999) / 1000000;
      if (nearest_delta_ms<  1) {
          nearest_delta_ms = 1;
      }
+
+    timeKillEvent(mm_timer);
      mm_timer = timeSetEvent(nearest_delta_ms,
                              mm_period,
                              mm_alarm_handler,
@@ -1082,19 +1064,14 @@ static void win32_stop_timer(struct qemu_alarm_timer *t)
      }
  }
 
-static void win32_rearm_timer(struct qemu_alarm_timer *t)
+static void win32_rearm_timer(struct qemu_alarm_timer *t,
+                              int64_t nearest_delta_ns)
  {
      HANDLE hTimer = t->timer;
      int nearest_delta_ms;
      BOOLEAN success;
 
-    assert(alarm_has_dynticks(t));
-    if (!rt_clock->active_timers&&
-        !vm_clock->active_timers&&
-        !host_clock->active_timers)
-        return;
-
-    nearest_delta_ms = (qemu_next_alarm_deadline() + 999999) / 1000000;
+    nearest_delta_ms = (nearest_delta_ns + 999999) / 1000000;
      if (nearest_delta_ms<  1) {
          nearest_delta_ms = 1;
      }
#7 lore raw

PATCH v2 4/13 qemu-timer: more clock functions

In reply to Paolo Bonzini

These will be used when moving icount accounting to cpus.c.

---
 qemu-timer.c |   25 +++++++++++++++++++++++++
 qemu-timer.h |    3 +++
 2 files changed, 28 insertions(+), 0 deletions(-)
qemu-timer.c +25 -0
index e2551f3..ebb5089 100644
@@ -495,6 +495,31 @@ void qemu_clock_warp(QEMUClock *clock)
     }
 }
 
+int64_t qemu_clock_has_timers(QEMUClock *clock)
+{
+    return !!clock->active_timers;
+}
+
+int64_t qemu_clock_expired(QEMUClock *clock)
+{
+    return (clock->active_timers &&
+            clock->active_timers->expire_time < qemu_get_clock_ns(clock));
+}
+
+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) {
+        delta = clock->active_timers->expire_time - qemu_get_clock_ns(clock);
+    }
+    if (delta < 0) {
+        delta = 0;
+    }
+    return delta;
+}
+
 QEMUTimer *qemu_new_timer(QEMUClock *clock, int scale,
                           QEMUTimerCB *cb, void *opaque)
 {
qemu-timer.h +3 -0
index 0a43469..4578075 100644
@@ -38,6 +38,9 @@ extern QEMUClock *vm_clock;
 extern QEMUClock *host_clock;
 
 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);
 void qemu_clock_enable(QEMUClock *clock, int enabled);
 void qemu_clock_warp(QEMUClock *clock);
 
Signature · 1 line
1.7.6
Reviewed-by on v2 4/13
#8 lore raw

Re: [Qemu-devel] [PATCH 04/13] qemu-timer: more clock functions

Quoted 3 lines · Paolo Bonzini

These will be used when moving icount accounting to cpus.c.

Regards,

Anthony Liguori

Quoted 55 lines
---
  qemu-timer.c |   25 +++++++++++++++++++++++++
  qemu-timer.h |    3 +++
  2 files changed, 28 insertions(+), 0 deletions(-)
qemu-timer.c +25 -0
index e2551f3..ebb5089 100644
@@ -495,6 +495,31 @@ void qemu_clock_warp(QEMUClock *clock)
      }
  }
 
+int64_t qemu_clock_has_timers(QEMUClock *clock)
+{
+    return !!clock->active_timers;
+}
+
+int64_t qemu_clock_expired(QEMUClock *clock)
+{
+    return (clock->active_timers&&
+            clock->active_timers->expire_time<  qemu_get_clock_ns(clock));
+}
+
+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) {
+        delta = clock->active_timers->expire_time - qemu_get_clock_ns(clock);
+    }
+    if (delta<  0) {
+        delta = 0;
+    }
+    return delta;
+}
+
  QEMUTimer *qemu_new_timer(QEMUClock *clock, int scale,
                            QEMUTimerCB *cb, void *opaque)
  {
qemu-timer.h +3 -0
index 0a43469..4578075 100644
@@ -38,6 +38,9 @@ extern QEMUClock *vm_clock;
  extern QEMUClock *host_clock;
 
  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);
  void qemu_clock_enable(QEMUClock *clock, int enabled);
  void qemu_clock_warp(QEMUClock *clock);
 
#9 lore raw

PATCH v2 5/13 qemu-timer: move icount to cpus.c

In reply to Paolo Bonzini

None of this is needed by tools, and most of it can even be made static inside cpus.c.

---
 cpus.c        |  295 +++++++++++++++++++++++++++++++++++++++++++++++++++++----
 exec-all.h    |   14 +++
 exec.c        |    3 -
 qemu-common.h |    4 +
 qemu-timer.c  |  279 -----------------------------------------------------
 qemu-timer.h  |   24 +-----
 6 files changed, 296 insertions(+), 323 deletions(-)
cpus.c +277 -18
index 8978779..986c900 100644
@@ -65,6 +65,281 @@
 static CPUState *next_cpu;
 
 /***********************************************************/
+/* guest cycle counter */
+
+/* Conversion factor from emulated instructions to virtual clock ticks.  */
+static int icount_time_shift;
+/* Arbitrarily pick 1MIPS as the minimum allowable speed.  */
+#define MAX_ICOUNT_SHIFT 10
+/* Compensate for varying guest execution speed.  */
+static int64_t qemu_icount_bias;
+static QEMUTimer *icount_rt_timer;
+static QEMUTimer *icount_vm_timer;
+static QEMUTimer *icount_warp_timer;
+static int64_t vm_clock_warp_start;
+static int64_t qemu_icount;
+
+typedef struct TimersState {
+    int64_t cpu_ticks_prev;
+    int64_t cpu_ticks_offset;
+    int64_t cpu_clock_offset;
+    int32_t cpu_ticks_enabled;
+    int64_t dummy;
+} TimersState;
+
+TimersState timers_state;
+
+/* Return the virtual CPU time, based on the instruction counter.  */
+int64_t cpu_get_icount(void)
+{
+    int64_t icount;
+    CPUState *env = cpu_single_env;;
+
+    icount = qemu_icount;
+    if (env) {
+        if (!can_do_io(env)) {
+            fprintf(stderr, "Bad clock read\n");
+        }
+        icount -= (env->icount_decr.u16.low + env->icount_extra);
+    }
+    return qemu_icount_bias + (icount << icount_time_shift);
+}
+
+/* return the host CPU cycle counter and handle stop/restart */
+int64_t cpu_get_ticks(void)
+{
+    if (use_icount) {
+        return cpu_get_icount();
+    }
+    if (!timers_state.cpu_ticks_enabled) {
+        return timers_state.cpu_ticks_offset;
+    } else {
+        int64_t ticks;
+        ticks = cpu_get_real_ticks();
+        if (timers_state.cpu_ticks_prev > ticks) {
+            /* Note: non increasing ticks may happen if the host uses
+               software suspend */
+            timers_state.cpu_ticks_offset += timers_state.cpu_ticks_prev - ticks;
+        }
+        timers_state.cpu_ticks_prev = ticks;
+        return ticks + timers_state.cpu_ticks_offset;
+    }
+}
+
+/* return the host CPU monotonic timer and handle stop/restart */
+int64_t cpu_get_clock(void)
+{
+    int64_t ti;
+    if (!timers_state.cpu_ticks_enabled) {
+        return timers_state.cpu_clock_offset;
+    } else {
+        ti = get_clock();
+        return ti + timers_state.cpu_clock_offset;
+    }
+}
+
+/* enable cpu_get_ticks() */
+void cpu_enable_ticks(void)
+{
+    if (!timers_state.cpu_ticks_enabled) {
+        timers_state.cpu_ticks_offset -= cpu_get_real_ticks();
+        timers_state.cpu_clock_offset -= get_clock();
+        timers_state.cpu_ticks_enabled = 1;
+    }
+}
+
+/* disable cpu_get_ticks() : the clock is stopped. You must not call
+   cpu_get_ticks() after that.  */
+void cpu_disable_ticks(void)
+{
+    if (timers_state.cpu_ticks_enabled) {
+        timers_state.cpu_ticks_offset = cpu_get_ticks();
+        timers_state.cpu_clock_offset = cpu_get_clock();
+        timers_state.cpu_ticks_enabled = 0;
+    }
+}
+
+/* Correlation between real and virtual time is always going to be
+   fairly approximate, so ignore small variation.
+   When the guest is idle real and virtual time will be aligned in
+   the IO wait loop.  */
+#define ICOUNT_WOBBLE (get_ticks_per_sec() / 10)
+
+static void icount_adjust(void)
+{
+    int64_t cur_time;
+    int64_t cur_icount;
+    int64_t delta;
+    static int64_t last_delta;
+    /* If the VM is not running, then do nothing.  */
+    if (!runstate_is_running()) {
+        return;
+    }
+    cur_time = cpu_get_clock();
+    cur_icount = qemu_get_clock_ns(vm_clock);
+    delta = cur_icount - cur_time;
+    /* FIXME: This is a very crude algorithm, somewhat prone to oscillation.  */
+    if (delta > 0
+        && last_delta + ICOUNT_WOBBLE < delta * 2
+        && icount_time_shift > 0) {
+        /* The guest is getting too far ahead.  Slow time down.  */
+        icount_time_shift--;
+    }
+    if (delta < 0
+        && last_delta - ICOUNT_WOBBLE > delta * 2
+        && icount_time_shift < MAX_ICOUNT_SHIFT) {
+        /* The guest is getting too far behind.  Speed time up.  */
+        icount_time_shift++;
+    }
+    last_delta = delta;
+    qemu_icount_bias = cur_icount - (qemu_icount << icount_time_shift);
+}
+
+static void icount_adjust_rt(void *opaque)
+{
+    qemu_mod_timer(icount_rt_timer,
+                   qemu_get_clock_ms(rt_clock) + 1000);
+    icount_adjust();
+}
+
+static void icount_adjust_vm(void *opaque)
+{
+    qemu_mod_timer(icount_vm_timer,
+                   qemu_get_clock_ns(vm_clock) + get_ticks_per_sec() / 10);
+    icount_adjust();
+}
+
+static int64_t qemu_icount_round(int64_t count)
+{
+    return (count + (1 << icount_time_shift) - 1) >> icount_time_shift;
+}
+
+static void icount_warp_rt(void *opaque)
+{
+    if (vm_clock_warp_start == -1) {
+        return;
+    }
+
+    if (runstate_is_running()) {
+        int64_t clock = qemu_get_clock_ns(rt_clock);
+        int64_t warp_delta = clock - vm_clock_warp_start;
+        if (use_icount == 1) {
+            qemu_icount_bias += warp_delta;
+        } else {
+            /*
+             * In adaptive mode, do not let the vm_clock run too
+             * far ahead of real time.
+             */
+            int64_t cur_time = cpu_get_clock();
+            int64_t cur_icount = qemu_get_clock_ns(vm_clock);
+            int64_t delta = cur_time - cur_icount;
+            qemu_icount_bias += MIN(warp_delta, delta);
+        }
+        if (qemu_clock_expired(vm_clock)) {
+            qemu_notify_event();
+        }
+    }
+    vm_clock_warp_start = -1;
+}
+
+void qemu_clock_warp(QEMUClock *clock)
+{
+    int64_t deadline;
+
+    /*
+     * There are too many global variables to make the "warp" behavior
+     * applicable to other clocks.  But a clock argument removes the
+     * need for if statements all over the place.
+     */
+    if (clock != vm_clock || !use_icount) {
+        return;
+    }
+
+    /*
+     * If the CPUs have been sleeping, advance the vm_clock timer now.  This
+     * ensures that the deadline for the timer is computed correctly below.
+     * This also makes sure that the insn counter is synchronized before the
+     * CPU starts running, in case the CPU is woken by an event other than
+     * the earliest vm_clock timer.
+     */
+    icount_warp_rt(NULL);
+    if (!all_cpu_threads_idle() || !qemu_clock_has_timers(vm_clock)) {
+        qemu_del_timer(icount_warp_timer);
+        return;
+    }
+
+    vm_clock_warp_start = qemu_get_clock_ns(rt_clock);
+    deadline = qemu_clock_deadline(vm_clock);
+    if (deadline > 0) {
+        /*
+         * Ensure the vm_clock proceeds even when the virtual CPU goes to
+         * sleep.  Otherwise, the CPU might be waiting for a future timer
+         * interrupt to wake it up, but the interrupt never comes because
+         * the vCPU isn't running any insns and thus doesn't advance the
+         * vm_clock.
+         *
+         * An extreme solution for this problem would be to never let VCPUs
+         * sleep in icount mode if there is a pending vm_clock timer; rather
+         * time could just advance to the next vm_clock event.  Instead, we
+         * do stop VCPUs and only advance vm_clock after some "real" time,
+         * (related to the time left until the next event) has passed.  This
+         * rt_clock timer will do this.  This avoids that the warps are too
+         * visible externally---for example, you will not be sending network
+         * packets continously instead of every 100ms.
+         */
+        qemu_mod_timer(icount_warp_timer, vm_clock_warp_start + deadline);
+    } else {
+        qemu_notify_event();
+    }
+}
+
+static const VMStateDescription vmstate_timers = {
+    .name = "timer",
+    .version_id = 2,
+    .minimum_version_id = 1,
+    .minimum_version_id_old = 1,
+    .fields      = (VMStateField[]) {
+        VMSTATE_INT64(cpu_ticks_offset, TimersState),
+        VMSTATE_INT64(dummy, TimersState),
+        VMSTATE_INT64_V(cpu_clock_offset, TimersState, 2),
+        VMSTATE_END_OF_LIST()
+    }
+};
+
+void configure_icount(const char *option)
+{
+    vmstate_register(NULL, 0, &vmstate_timers, &timers_state);
+    if (!option) {
+        return;
+    }
+
+    icount_warp_timer = qemu_new_timer_ns(rt_clock, icount_warp_rt, NULL);
+    if (strcmp(option, "auto") != 0) {
+        icount_time_shift = strtol(option, NULL, 0);
+        use_icount = 1;
+        return;
+    }
+
+    use_icount = 2;
+
+    /* 125MIPS seems a reasonable initial guess at the guest speed.
+       It will be corrected fairly quickly anyway.  */
+    icount_time_shift = 3;
+
+    /* Have both realtime and virtual time triggers for speed adjustment.
+       The realtime trigger catches emulated time passing too slowly,
+       the virtual time trigger catches emulated time passing too fast.
+       Realtime triggers occur even when idle, so use them less frequently
+       than VM triggers.  */
+    icount_rt_timer = qemu_new_timer_ms(rt_clock, icount_adjust_rt, NULL);
+    qemu_mod_timer(icount_rt_timer,
+                   qemu_get_clock_ms(rt_clock) + 1000);
+    icount_vm_timer = qemu_new_timer_ns(vm_clock, icount_adjust_vm, NULL);
+    qemu_mod_timer(icount_vm_timer,
+                   qemu_get_clock_ns(vm_clock) + get_ticks_per_sec() / 10);
+}
+
+/***********************************************************/
 void hw_error(const char *fmt, ...)
 {
     va_list ap;
@@ -686,7 +961,7 @@ static void *qemu_tcg_cpu_thread_fn(void *arg)
 
     while (1) {
         cpu_exec_all();
-        if (use_icount && qemu_next_icount_deadline() <= 0) {
+        if (use_icount && qemu_clock_deadline(vm_clock) <= 0) {
             qemu_notify_event();
         }
         qemu_tcg_wait_io_event();
@@ -903,7 +1178,7 @@ static int tcg_cpu_exec(CPUState *env)
         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_next_icount_deadline());
+        count = qemu_icount_round(qemu_clock_deadline(vm_clock));
         qemu_icount += count;
         decr = (count > 0xffff) ? 0xffff : count;
         count -= decr;
@@ -995,22 +1270,6 @@ void set_cpu_log_filename(const char *optarg)
     cpu_set_log_filename(optarg);
 }
 
-/* Return the virtual CPU time, based on the instruction counter.  */
-int64_t cpu_get_icount(void)
-{
-    int64_t icount;
-    CPUState *env = cpu_single_env;;
-
-    icount = qemu_icount;
-    if (env) {
-        if (!can_do_io(env)) {
-            fprintf(stderr, "Bad clock read\n");
-        }
-        icount -= (env->icount_decr.u16.low + env->icount_extra);
-    }
-    return qemu_icount_bias + (icount << icount_time_shift);
-}
-
 void list_cpus(FILE *f, fprintf_function cpu_fprintf, const char *optarg)
 {
     /* XXX: implement xxx_cpu_list for targets that still miss it */
exec-all.h +14 -0
index 1120f84..72ef246 100644
@@ -356,4 +356,18 @@ extern int singlestep;
 /* cpu-exec.c */
 extern volatile sig_atomic_t exit_request;
 
+/* Deterministic execution requires that IO only be performed on the last
+   instruction of a TB so that interrupts take effect immediately.  */
+static inline int can_do_io(CPUState *env)
+{
+    if (!use_icount) {
+        return 1;
+    }
+    /* If not executing code then assume we are ok.  */
+    if (!env->current_tb) {
+        return 1;
+    }
+    return env->can_do_io != 0;
+}
+
 #endif
exec.c +0 -3
index d0cbf15..9dc4edb 100644
@@ -125,9 +125,6 @@ CPUState *cpu_single_env;
    1 = Precise instruction counting.
    2 = Adaptive rate instruction counting.  */
 int use_icount = 0;
-/* Current instruction counter.  While executing translated code this may
-   include some instructions that have not yet been executed.  */
-int64_t qemu_icount;
 
 typedef struct PageDesc {
     /* list of TBs intersecting this ram page */
qemu-common.h +4 -0
index b1b8053..b1d2f0f 100644
@@ -96,6 +96,10 @@ static inline char *realpath(const char *path, char *resolved_path)
 }
 #endif
 
+/* icount */
+void configure_icount(const char *option);
+extern int use_icount;
+
 /* FIXME: Remove NEED_CPU_H.  */
 #ifndef NEED_CPU_H
 
qemu-timer.c +0 -279

315 lines not on this page · raw message

qemu-timer.h +1 -23
index 4578075..ce576b9 100644
@@ -58,9 +58,7 @@ int qemu_timer_expired(QEMUTimer *timer_head, int64_t current_time);
 
 void qemu_run_all_timers(void);
 int qemu_alarm_pending(void);
-int64_t qemu_next_icount_deadline(void);
 void configure_alarms(char const *opt);
-void configure_icount(const char *option);
 int qemu_calculate_timeout(void);
 void init_clocks(void);
 int init_timer_alarm(void);
@@ -153,12 +151,8 @@ void ptimer_run(ptimer_state *s, int oneshot);
 void ptimer_stop(ptimer_state *s);
 
 /* icount */
-int64_t qemu_icount_round(int64_t count);
-extern int64_t qemu_icount;
-extern int use_icount;
-extern int icount_time_shift;
-extern int64_t qemu_icount_bias;
 int64_t cpu_get_icount(void);
+int64_t cpu_get_clock(void);
 
 /*******************************************/
 /* host CPU ticks (if available) */
@@ -314,22 +308,6 @@ static inline int64_t cpu_get_real_ticks (void)
 }
 #endif
 
-#ifdef NEED_CPU_H
-/* Deterministic execution requires that IO only be performed on the last
-   instruction of a TB so that interrupts take effect immediately.  */
-static inline int can_do_io(CPUState *env)
-{
-    if (!use_icount)
-        return 1;
-
-    /* If not executing code then assume we are ok.  */
-    if (!env->current_tb)
-        return 1;
-
-    return env->can_do_io != 0;
-}
-#endif
-
 #ifdef CONFIG_PROFILER
 static inline int64_t profile_getclock(void)
 {
Signature · 1 line
1.7.6
#10 lore raw

PATCH v2 6/13 qemu-timer: do not refer to runstate_is_running()

In reply to Paolo Bonzini
---
 cpus.c       |    1 +
 qemu-timer.c |    5 +----
 2 files changed, 2 insertions(+), 4 deletions(-)
cpus.c +1 -0
index 986c900..64b6943 100644
@@ -1059,6 +1059,7 @@ void pause_all_vcpus(void)
 {
     CPUState *penv = first_cpu;
 
+    qemu_clock_enable(vm_clock, false);
     while (penv) {
         penv->stop = 1;
         qemu_cpu_kick(penv);
qemu-timer.c +1 -4
index 8129af6..d8507e3 100644
@@ -504,10 +504,7 @@ void qemu_run_all_timers(void)
     }
 
     /* vm time timers */
-    if (runstate_is_running()) {
-        qemu_run_timers(vm_clock);
-    }
-
+    qemu_run_timers(vm_clock);
     qemu_run_timers(rt_clock);
     qemu_run_timers(host_clock);
 }
Signature · 1 line
1.7.6
#11 lore raw

PATCH v2 7/13 qemu-timer: use atexit for quit_timers

In reply to Paolo Bonzini
---
 qemu-timer.c |   15 ++++++++-------
 qemu-timer.h |    1 -
 vl.c         |    1 -
 3 files changed, 8 insertions(+), 9 deletions(-)
qemu-timer.c +8 -7
index d8507e3..7fa81e1 100644
@@ -840,6 +840,13 @@ static void alarm_timer_on_change_state_rearm(void *opaque, int running,
         qemu_rearm_alarm_timer((struct qemu_alarm_timer *) opaque);
 }
 
+static void quit_timers(void)
+{
+    struct qemu_alarm_timer *t = alarm_timer;
+    alarm_timer = NULL;
+    t->stop(t);
+}
+
 int init_timer_alarm(void)
 {
     struct qemu_alarm_timer *t = NULL;
@@ -859,6 +866,7 @@ int init_timer_alarm(void)
     }
 
     /* first event is at time 0 */
+    atexit(quit_timers);
     t->pending = 1;
     alarm_timer = t;
     qemu_add_vm_change_state_handler(alarm_timer_on_change_state_rearm, t);
@@ -869,13 +877,6 @@ fail:
     return err;
 }
 
-void quit_timers(void)
-{
-    struct qemu_alarm_timer *t = alarm_timer;
-    alarm_timer = NULL;
-    t->stop(t);
-}
-
 int qemu_calculate_timeout(void)
 {
     return 1000;
qemu-timer.h +0 -1
index ce576b9..b4ea201 100644
@@ -62,7 +62,6 @@ void configure_alarms(char const *opt);
 int qemu_calculate_timeout(void);
 void init_clocks(void);
 int init_timer_alarm(void);
-void quit_timers(void);
 
 int64_t cpu_get_ticks(void);
 void cpu_enable_ticks(void);
vl.c +0 -1
index dbf7778..99f8ced 100644
@@ -3561,7 +3561,6 @@ int main(int argc, char **argv, char **envp)
     os_setup_post();
 
     main_loop();
-    quit_timers();
     net_cleanup();
     res_free();
 
Signature · 1 line
1.7.6
#12 lore raw

PATCH v2 8/13 qemu-timer: move more stuff out of qemu-timer.c

In reply to Paolo Bonzini
---
 qemu-timer.c |   35 ++++-------------------------------
 qemu-timer.h |    2 ++
 savevm.c     |   25 +++++++++++++++++++++++++
 vl.c         |    1 +
 4 files changed, 32 insertions(+), 31 deletions(-)
qemu-timer.c +4 -31
index 7fa81e1..58926dd 100644
@@ -266,11 +266,8 @@ static QEMUClock *qemu_new_clock(int type)
     clock = g_malloc0(sizeof(QEMUClock));
     clock->type = type;
     clock->enabled = 1;
+    clock->last = INT64_MIN;
     notifier_list_init(&clock->reset_notifiers);
-    /* required to detect & report backward jumps */
-    if (type == QEMU_CLOCK_HOST) {
-        clock->last = get_clock_realtime();
-    }
     return clock;
 }
 
@@ -344,7 +341,7 @@ void qemu_del_timer(QEMUTimer *ts)
 
 /* modify the current timer so that it will be fired when current_time
    >= expire_time. The corresponding callback will be called. */
-static void qemu_mod_timer_ns(QEMUTimer *ts, int64_t expire_time)
+void qemu_mod_timer_ns(QEMUTimer *ts, int64_t expire_time)
 {
     QEMUTimer **pt, *t;
 
@@ -378,8 +375,6 @@ static void qemu_mod_timer_ns(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);
@@ -464,33 +459,11 @@ void init_clocks(void)
     rt_clock = qemu_new_clock(QEMU_CLOCK_REALTIME);
     vm_clock = qemu_new_clock(QEMU_CLOCK_VIRTUAL);
     host_clock = qemu_new_clock(QEMU_CLOCK_HOST);
-
-    rtc_clock = host_clock;
 }
 
-/* save a timer */
-void qemu_put_timer(QEMUFile *f, QEMUTimer *ts)
+uint64_t qemu_timer_expire_time_ns(QEMUTimer *ts)
 {
-    uint64_t expire_time;
-
-    if (qemu_timer_pending(ts)) {
-        expire_time = ts->expire_time;
-    } else {
-        expire_time = -1;
-    }
-    qemu_put_be64(f, expire_time);
-}
-
-void qemu_get_timer(QEMUFile *f, QEMUTimer *ts)
-{
-    uint64_t expire_time;
-
-    expire_time = qemu_get_be64(f);
-    if (expire_time != -1) {
-        qemu_mod_timer_ns(ts, expire_time);
-    } else {
-        qemu_del_timer(ts);
-    }
+    return qemu_timer_pending(ts) ? ts->expire_time : -1;
 }
 
 void qemu_run_all_timers(void)
qemu-timer.h +2 -0
index b4ea201..9f4ffed 100644
@@ -52,9 +52,11 @@ QEMUTimer *qemu_new_timer(QEMUClock *clock, 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);
 void qemu_mod_timer(QEMUTimer *ts, int64_t expire_time);
 int qemu_timer_pending(QEMUTimer *ts);
 int qemu_timer_expired(QEMUTimer *timer_head, int64_t current_time);
+uint64_t qemu_timer_expire_time_ns(QEMUTimer *ts);
 
 void qemu_run_all_timers(void);
 int qemu_alarm_pending(void);
savevm.c +25 -0
index bf4d0e7..58f123e 100644
@@ -81,6 +81,7 @@
 #include "migration.h"
 #include "qemu_socket.h"
 #include "qemu-queue.h"
+#include "qemu-timer.h"
 #include "cpus.h"
 
 #define SELF_ANNOUNCE_ROUNDS 5
@@ -674,6 +675,30 @@ uint64_t qemu_get_be64(QEMUFile *f)
     return v;
 }
 
+
+/* timer */
+
+void qemu_put_timer(QEMUFile *f, QEMUTimer *ts)
+{
+    uint64_t expire_time;
+
+    expire_time = qemu_timer_expire_time_ns(ts);
+    qemu_put_be64(f, expire_time);
+}
+
+void qemu_get_timer(QEMUFile *f, QEMUTimer *ts)
+{
+    uint64_t expire_time;
+
+    expire_time = qemu_get_be64(f);
+    if (expire_time != -1) {
+        qemu_mod_timer_ns(ts, expire_time);
+    } else {
+        qemu_del_timer(ts);
+    }
+}
+
+
 /* bool */
 
 static int get_bool(QEMUFile *f, void *pv, size_t size)
vl.c +1 -0
index 99f8ced..3726b74 100644
@@ -2313,6 +2313,7 @@ int main(int argc, char **argv, char **envp)
     runstate_init();
 
     init_clocks();
+    rtc_clock = host_clock;
 
     qemu_cache_utils_init(envp);
 
Signature · 1 line
1.7.6
#13 lore raw

PATCH v2 9/13 qemu-timer: do not use RunState change handlers

In reply to Paolo Bonzini
---
 qemu-timer.c |   12 ++++--------
 1 files changed, 4 insertions(+), 8 deletions(-)
qemu-timer.c +4 -8
index 58926dd..f11a28d 100644
@@ -273,7 +273,11 @@ static QEMUClock *qemu_new_clock(int type)
 
 void qemu_clock_enable(QEMUClock *clock, int enabled)
 {
+    bool old = clock->enabled;
     clock->enabled = enabled;
+    if (enabled && !old) {
+        qemu_rearm_alarm_timer(alarm_timer);
+    }
 }
 
 int64_t qemu_clock_has_timers(QEMUClock *clock)
@@ -806,13 +810,6 @@ static void win32_rearm_timer(struct qemu_alarm_timer *t,
 
 #endif /* _WIN32 */
 
-static void alarm_timer_on_change_state_rearm(void *opaque, int running,
-                                              RunState state)
-{
-    if (running)
-        qemu_rearm_alarm_timer((struct qemu_alarm_timer *) opaque);
-}
-
 static void quit_timers(void)
 {
     struct qemu_alarm_timer *t = alarm_timer;
@@ -842,7 +839,6 @@ int init_timer_alarm(void)
     atexit(quit_timers);
     t->pending = 1;
     alarm_timer = t;
-    qemu_add_vm_change_state_handler(alarm_timer_on_change_state_rearm, t);
 
     return 0;
 
Signature · 1 line
1.7.6
#14 lore raw

PATCH v2 10/13 create main-loop.h

In reply to Paolo Bonzini
---
 async.c       |    1 +
 cpus.c        |    7 +----
 cpus.h        |    1 -
 main-loop.h   |   73 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 qemu-char.h   |   12 +--------
 qemu-common.h |   11 --------
 sysemu.h      |    3 +-
 vl.c          |    1 +
 8 files changed, 78 insertions(+), 31 deletions(-)
 create mode 100644 main-loop.h
async.c +1 -0
index ca13962..332d511 100644
@@ -24,6 +24,7 @@
 
 #include "qemu-common.h"
 #include "qemu-aio.h"
+#include "main-loop.h"
 
 /* Anchor of the list of Bottom Halves belonging to the context */
 static struct QEMUBH *first_bh;
cpus.c +1 -6
index 64b6943..aabb7c1 100644
@@ -33,17 +33,12 @@
 
 #include "qemu-thread.h"
 #include "cpus.h"
+#include "main-loop.h"
 
 #ifndef _WIN32
 #include "compatfd.h"
 #endif
 
-#ifdef SIGRTMIN
-#define SIG_IPI (SIGRTMIN+4)
-#else
-#define SIG_IPI SIGUSR1
-#endif
-
 #ifdef CONFIG_LINUX
 
 #include <sys/prctl.h>
cpus.h +0 -1
index 5885885..4ccf986 100644
@@ -2,7 +2,6 @@
 #define QEMU_CPUS_H
 
 /* cpus.c */
-int qemu_init_main_loop(void);
 void qemu_main_loop_start(void);
 void resume_all_vcpus(void);
 void pause_all_vcpus(void);
main-loop.h new file +73 -0

73 lines not on this page · raw message

qemu-char.h +1 -11
index eebbdd8..7efcf99 100644
@@ -7,6 +7,7 @@
 #include "qemu-config.h"
 #include "qobject.h"
 #include "qstring.h"
+#include "main-loop.h"
 
 /* character device */
 
@@ -237,15 +238,4 @@ void qemu_chr_close_mem(CharDriverState *chr);
 QString *qemu_chr_mem_to_qs(CharDriverState *chr);
 size_t qemu_chr_mem_osize(const CharDriverState *chr);
 
-/* async I/O support */
-
-int qemu_set_fd_handler2(int fd,
-                         IOCanReadHandler *fd_read_poll,
-                         IOHandler *fd_read,
-                         IOHandler *fd_write,
-                         void *opaque);
-int qemu_set_fd_handler(int fd,
-                        IOHandler *fd_read,
-                        IOHandler *fd_write,
-                        void *opaque);
 #endif
qemu-common.h +0 -11
index b1d2f0f..23794a3 100644
@@ -218,14 +218,6 @@ int qemu_sendv(int sockfd, struct iovec *iov, int len, int iov_offset);
 
 void QEMU_NORETURN hw_error(const char *fmt, ...) GCC_FMT_ATTR(1, 2);
 
-/* IO callbacks.  */
-typedef void IOReadHandler(void *opaque, const uint8_t *buf, int size);
-typedef int IOCanReadHandler(void *opaque);
-typedef void IOHandler(void *opaque);
-
-void qemu_iohandler_fill(int *pnfds, fd_set *readfds, fd_set *writefds, fd_set *xfds);
-void qemu_iohandler_poll(fd_set *readfds, fd_set *writefds, fd_set *xfds, int rc);
-
 struct ParallelIOArg {
     void *buffer;
     int count;
@@ -287,9 +279,6 @@ void cpu_exec_init_all(void);
 void cpu_save(QEMUFile *f, void *opaque);
 int cpu_load(QEMUFile *f, void *opaque, int version_id);
 
-/* Force QEMU to process pending events */
-void qemu_notify_event(void);
-
 /* Unblock cpu */
 void qemu_cpu_kick(void *env);
 void qemu_cpu_kick_self(void);
sysemu.h +1 -2
index a889d90..81d2323 100644
@@ -8,6 +8,7 @@
 #include "qemu-timer.h"
 #include "qapi-types.h"
 #include "notify.h"
+#include "main-loop.h"
 
 /* vl.c */
 
@@ -63,8 +64,6 @@ void do_info_snapshots(Monitor *mon);
 
 void qemu_announce_self(void);
 
-int main_loop_wait(int nonblocking);
-
 bool qemu_savevm_state_blocked(Monitor *mon);
 int qemu_savevm_state_begin(Monitor *mon, QEMUFile *f, int blk_enable,
                             int shared);
vl.c +1 -0
index 3726b74..22a1d9e 100644
@@ -148,6 +148,7 @@ int main(int argc, char **argv)
 #include "qemu-objects.h"
 #include "qemu-options.h"
 #include "qmp-commands.h"
+#include "main-loop.h"
 #ifdef CONFIG_VIRTFS
 #include "fsdev/qemu-fsdev.h"
 #endif
Signature · 1 line
1.7.6
#15 lore raw

PATCH v2 11/13 create main-loop.c

In reply to Paolo Bonzini
---
 Makefile.objs    |    2 +-
 cpus.c           |  188 +--------------------
 cpus.h           |    1 +
 main-loop.c      |  494 ++++++++++++++++++++++++++++++++++++++++++++++++++++++
 os-win32.c       |  123 --------------
 qemu-os-posix.h  |    4 -
 qemu-os-win32.h  |   15 +--
 slirp/libslirp.h |   11 --
 vl.c             |  123 +--------------
 9 files changed, 499 insertions(+), 462 deletions(-)
 create mode 100644 main-loop.c
Makefile.objs +1 -1
index 09527cb..7ee7dc5 100644
@@ -81,7 +81,7 @@ common-obj-y += $(oslib-obj-y)
 common-obj-$(CONFIG_WIN32) += os-win32.o
 common-obj-$(CONFIG_POSIX) += os-posix.o
 
-common-obj-y += tcg-runtime.o host-utils.o
+common-obj-y += tcg-runtime.o host-utils.o main-loop.o
 common-obj-y += irq.o ioport.o input.o
 common-obj-$(CONFIG_PTIMER) += ptimer.o
 common-obj-$(CONFIG_MAX7310) += max7310.o
cpus.c +1 -187

224 lines not on this page · raw message

cpus.h +1 -0
index 4ccf986..7422584 100644
@@ -2,6 +2,7 @@
 #define QEMU_CPUS_H
 
 /* cpus.c */
+void qemu_init_cpu_loop(void);
 void qemu_main_loop_start(void);
 void resume_all_vcpus(void);
 void pause_all_vcpus(void);
main-loop.c new file +494 -0

494 lines not on this page · raw message

os-win32.c +0 -123

129 lines not on this page · raw message

qemu-os-posix.h +0 -4
index 81fd9ab..920499d 100644
@@ -26,10 +26,6 @@
 #ifndef QEMU_OS_POSIX_H
 #define QEMU_OS_POSIX_H
 
-static inline void os_host_main_loop_wait(int *timeout)
-{
-}
-
 void os_set_line_buffering(void);
 void os_set_proc_name(const char *s);
 void os_setup_signal_handling(void);
qemu-os-win32.h +1 -14
index 8a069d7..84e4d35 100644
@@ -28,26 +28,13 @@
 
 #include <windows.h>
 #include <winsock2.h>
+#include "main-loop.h"
 
 /* Declaration of ffs() is missing in MinGW's strings.h. */
 int ffs(int i);
 
 /* Polling handling */
 
-/* return TRUE if no sleep should be done afterwards */
-typedef int PollingFunc(void *opaque);
-
-int qemu_add_polling_cb(PollingFunc *func, void *opaque);
-void qemu_del_polling_cb(PollingFunc *func, void *opaque);
-
-/* Wait objects handling */
-typedef void WaitObjectFunc(void *opaque);
-
-int qemu_add_wait_object(HANDLE handle, WaitObjectFunc *func, void *opaque);
-void qemu_del_wait_object(HANDLE handle, WaitObjectFunc *func, void *opaque);
-
-void os_host_main_loop_wait(int *timeout);
-
 static inline void os_setup_signal_handling(void) {}
 static inline void os_daemonize(void) {}
 static inline void os_setup_post(void) {}
slirp/libslirp.h +0 -11
index a755123..890fd86 100644
@@ -3,8 +3,6 @@
 
 #include "qemu-common.h"
 
-#ifdef CONFIG_SLIRP
-
 struct Slirp;
 typedef struct Slirp Slirp;
 
@@ -44,13 +42,4 @@ void slirp_socket_recv(Slirp *slirp, struct in_addr guest_addr,
 size_t slirp_socket_can_recv(Slirp *slirp, struct in_addr guest_addr,
                              int guest_port);
 
-#else /* !CONFIG_SLIRP */
-
-static inline void slirp_select_fill(int *pnfds, fd_set *readfds,
-                                     fd_set *writefds, fd_set *xfds) { }
-
-static inline void slirp_select_poll(fd_set *readfds, fd_set *writefds,
-                                     fd_set *xfds, int select_error) { }
-#endif /* !CONFIG_SLIRP */
-
 #endif
vl.c +1 -122

135 lines not on this page · raw message

Signature · 1 line
1.7.6
#16 lore raw

PATCH v2 12/13 Revert to a hand-made select loop

In reply to Paolo Bonzini

This reverts commit c82dc29a9112f34e0a51cad9a412cf6d9d05dfb2 and 4d88a2ac8643265108ef1fb47ceee5d7b28e19f2.

---
 iohandler.c |   54 +-----------------------------------------------------
 1 files changed, 1 insertions(+), 53 deletions(-)
iohandler.c +1 -53
index 4cc1c5a..4deae1e 100644
@@ -80,64 +80,12 @@ int qemu_set_fd_handler2(int fd,
     return 0;
 }
 
-typedef struct IOTrampoline
-{
-    GIOChannel *chan;
-    IOHandler *fd_read;
-    IOHandler *fd_write;
-    void *opaque;
-    guint tag;
-} IOTrampoline;
-
-static gboolean fd_trampoline(GIOChannel *chan, GIOCondition cond, gpointer opaque)
-{
-    IOTrampoline *tramp = opaque;
-
-    if ((cond & G_IO_IN) && tramp->fd_read) {
-        tramp->fd_read(tramp->opaque);
-    }
-
-    if ((cond & G_IO_OUT) && tramp->fd_write) {
-        tramp->fd_write(tramp->opaque);
-    }
-
-    return TRUE;
-}
-
 int qemu_set_fd_handler(int fd,
                         IOHandler *fd_read,
                         IOHandler *fd_write,
                         void *opaque)
 {
-    static IOTrampoline fd_trampolines[FD_SETSIZE];
-    IOTrampoline *tramp = &fd_trampolines[fd];
-
-    if (tramp->tag != 0) {
-        g_io_channel_unref(tramp->chan);
-        g_source_remove(tramp->tag);
-        tramp->tag = 0;
-    }
-
-    if (fd_read || fd_write || opaque) {
-        GIOCondition cond = 0;
-
-        tramp->fd_read = fd_read;
-        tramp->fd_write = fd_write;
-        tramp->opaque = opaque;
-
-        if (fd_read) {
-            cond |= G_IO_IN | G_IO_ERR;
-        }
-
-        if (fd_write) {
-            cond |= G_IO_OUT | G_IO_ERR;
-        }
-
-        tramp->chan = g_io_channel_unix_new(fd);
-        tramp->tag = g_io_add_watch(tramp->chan, cond, fd_trampoline, tramp);
-    }
-
-    return 0;
+    return qemu_set_fd_handler2(fd, NULL, fd_read, fd_write, opaque);
 }
 
 void qemu_iohandler_fill(int *pnfds, fd_set *readfds, fd_set *writefds, fd_set *xfds)
Signature · 1 line
1.7.6
#17 lore raw

PATCH v2 13/13 simplify main loop functions

In reply to Paolo Bonzini

Provide a clean example of how to use the main loop in the tools.

---
 cpus.c |    5 ----
 cpus.h |    1 -
 vl.c   |   79 +++++++++++++++++++++++++++++++++------------------------------
 3 files changed, 41 insertions(+), 44 deletions(-)
cpus.c +0 -5
index ead18af..b494e73 100644
@@ -627,11 +627,6 @@ void qemu_init_cpu_loop(void)
     qemu_thread_get_self(&io_thread);
 }
 
-void qemu_main_loop_start(void)
-{
-    resume_all_vcpus();
-}
-
 void run_on_cpu(CPUState *env, void (*func)(void *data), void *data)
 {
     struct qemu_work_item wi;
cpus.h +0 -1
index 7422584..3525375 100644
@@ -3,7 +3,6 @@
 
 /* cpus.c */
 void qemu_init_cpu_loop(void);
-void qemu_main_loop_start(void);
 void resume_all_vcpus(void);
 void pause_all_vcpus(void);
 void cpu_stop_current(void);
vl.c +41 -38

104 lines not on this page · raw message

Signature · 1 line
1.7.6
#18 lore raw
In reply to Paolo Bonzini
Quoted 3 lines · Paolo Bonzini

This patch series makes the QEMU main loop usable out of the executable, and especially in tools and possibly unit tests. The series already starts using the refactored main loop in qemu-nbd.

What patch does this? I don't see any changes to qemu-nbd in this series.

Regards,

Anthony Liguori

Quoted 59 lines

This is cleaner because it avoids introducing partial transitions to GIOChannel. Interfacing with the glib main loop is still possible.

The main loop code is currently split in cpus.c and vl.c. Moving it to a new file is easy; the problem is that the main loop depends on the timer infrastructure in qemu-timer.c, and that file currently contains the implementation of icount and the vm_clock. This is bad for the perspective of linking qemu-timer.c into the tools. Luckily, it is relatively easy to untie them and move them out of the way. This is what the largest part of the series does (patches 1-9).

Patches 10-13 complete the refactoring and cleanup some surrounding code.

v1->v2
Rebased

Paolo Bonzini (13):
remove unused function
qemu-timer: remove active_timers array
qemu-timer: move common code to qemu_rearm_alarm_timer
qemu-timer: more clock functions
qemu-timer: move icount to cpus.c
qemu-timer: do not refer to runstate_is_running()
qemu-timer: use atexit for quit_timers
qemu-timer: move more stuff out of qemu-timer.c
qemu-timer: do not use RunState change handlers create main-loop.h
create main-loop.c
Revert to a hand-made select loop
simplify main loop functions

  Makefile.objs    |    2 +-
  async.c          |    1 +
  cpus.c           |  496 ++++++++++++++++++++++++++++++------------------------
  cpus.h           |    3 +-
  exec-all.h       |   14 ++
  exec.c           |    3 -
  hw/mac_dbdma.c   |    5 -
  hw/mac_dbdma.h   |    1 -
  iohandler.c      |   54 +------
  main-loop.c      |  494 +++++++++++++++++++++++++++++++++++++++++++++++++++++
  main-loop.h      |   73 ++++++++
  os-win32.c       |  123 --------------
  qemu-char.h      |   12 +--
  qemu-common.h    |   15 +--
  qemu-os-posix.h  |    4 -
  qemu-os-win32.h  |   15 +--
  qemu-timer.c     |  489 ++++++++++-------------------------------------------
  qemu-timer.h     |   30 +---
  savevm.c         |   25 +++
  slirp/libslirp.h |   11 --
  sysemu.h         |    3 +-
  vl.c             |  189 ++++-----------------
  22 files changed, 1027 insertions(+), 1035 deletions(-)

create mode 100644 main-loop.c
create mode 100644 main-loop.h

#19 lore raw
Quoted 4 lines · Anthony Liguori
Quoted 2 lines

The series already
starts using the refactored main loop in qemu-nbd.

What patch does this? I don't see any changes to qemu-nbd in this series.

Leftover from RFC. Right now, this is just a chainsaw cleanup. However, Kevin has plans to use the main loop everywhere (making qemu-io and qemu-img one huge coroutine).

Paolo

#20 lore raw
Quoted 9 lines · Paolo Bonzini
Quoted 4 lines · Anthony Liguori
Quoted 2 lines

The series already
starts using the refactored main loop in qemu-nbd.

What patch does this? I don't see any changes to qemu-nbd in this series.

Leftover from RFC. Right now, this is just a chainsaw cleanup. However, Kevin has plans to use the main loop everywhere (making qemu-io and qemu-img one huge coroutine).

Can you add something to docs that explains how to use the new main loop?

Regards,

Anthony Liguori

Quoted 2 lines

Paolo

#21 lore raw
Quoted 7 lines · Anthony Liguori
Quoted 5 lines

Leftover from RFC. Right now, this is just a chainsaw cleanup. However, Kevin
has plans to use the main loop everywhere (making qemu-io and qemu-img one huge
coroutine).

Can you add something to docs that explains how to use the new main loop?

Yes, of course.

Paolo

#22 lore raw
Quoted 10 lines · Paolo Bonzini
Quoted 7 lines · Anthony Liguori
Quoted 5 lines

Leftover from RFC. Right now, this is just a chainsaw cleanup. However, Kevin
has plans to use the main loop everywhere (making qemu-io and qemu-img one huge
coroutine).

Can you add something to docs that explains how to use the new main loop?

Yes, of course.

Is it okay to just move more functions to main-loop.h (bottom halves, lock/unlock iothread, child watch) and add doc comments there?

If so, I can send the 2 updated patches (main-loop.h/main-loop.c) for review, and push again to github.

Paolo