diff mbox series

[v1,1/2] qemu-timer: gracefully handle the end of time

Message ID 20200728141005.28664-2-alex.bennee@linaro.org
State New
Headers show
Series clean-ups for sleep=off behaviour | expand

Commit Message

Alex Bennée July 28, 2020, 2:10 p.m. UTC
The previous behaviour was rather user hostile as timers set for
INT64_MAX would continually re-arm leaving the system locked in a loop
and the console unavailable. With this patch we detect the situation
and gracefully suspend the machine.

NB: we need a proper fix before the 23rd century.

Signed-off-by: Alex Bennée <alex.bennee@linaro.org>

---
 include/sysemu/cpus.h |  2 ++
 softmmu/cpus.c        | 13 +++++++++++++
 util/qemu-timer.c     | 16 ++++++++++++++++
 stubs/Makefile.objs   |  1 +
 4 files changed, 32 insertions(+)

-- 
2.20.1

Comments

Paolo Bonzini July 28, 2020, 2:42 p.m. UTC | #1
On 28/07/20 16:10, Alex Bennée wrote:
> +    /*

> +     * Check to see if we have run out of time. Most of our time

> +     * sources are nanoseconds since epoch (some time around the fall

> +     * of Babylon 5, the start of the Enterprises five year mission

> +     * and just before the arrival of the great evil ~ 2262CE).

> +     * Although icount based time is ns since the start of emulation

> +     * it is able to skip forward if the device is sleeping (think IoT

> +     * device with a very long heartbeat). Either way we don't really

> +     * handle running out of time so lets catch it and report it here.

> +     */

> +    if (current_time == INT64_MAX) {

> +        qemu_handle_outa_time();

> +        goto out;

> +    }

> +


Doing this here is a bit dangerous, I'd rather do nothing here and
detect the situation in cpus.c where we can do
qemu_system_shutdown_request() (and also do nothing).

Paolo
Alex Bennée July 28, 2020, 4:08 p.m. UTC | #2
Paolo Bonzini <pbonzini@redhat.com> writes:

> On 28/07/20 16:10, Alex Bennée wrote:

>> +    /*

>> +     * Check to see if we have run out of time. Most of our time

>> +     * sources are nanoseconds since epoch (some time around the fall

>> +     * of Babylon 5, the start of the Enterprises five year mission

>> +     * and just before the arrival of the great evil ~ 2262CE).

>> +     * Although icount based time is ns since the start of emulation

>> +     * it is able to skip forward if the device is sleeping (think IoT

>> +     * device with a very long heartbeat). Either way we don't really

>> +     * handle running out of time so lets catch it and report it here.

>> +     */

>> +    if (current_time == INT64_MAX) {

>> +        qemu_handle_outa_time();

>> +        goto out;

>> +    }

>> +

>

> Doing this here is a bit dangerous, I'd rather do nothing here and

> detect the situation in cpus.c where we can do

> qemu_system_shutdown_request() (and also do nothing).


You mean in notify_aio_contexts()? Sure we can do that.

I also figured it might be worth cleaning up the return progress stuff
because AFAICT no one seems to care.

>

> Paolo



-- 
Alex Bennée
Paolo Bonzini July 28, 2020, 4:58 p.m. UTC | #3
On 28/07/20 18:08, Alex Bennée wrote:
> 

> Paolo Bonzini <pbonzini@redhat.com> writes:

> 

>> On 28/07/20 16:10, Alex Bennée wrote:

>>> +    /*

>>> +     * Check to see if we have run out of time. Most of our time

>>> +     * sources are nanoseconds since epoch (some time around the fall

>>> +     * of Babylon 5, the start of the Enterprises five year mission

>>> +     * and just before the arrival of the great evil ~ 2262CE).

>>> +     * Although icount based time is ns since the start of emulation

>>> +     * it is able to skip forward if the device is sleeping (think IoT

>>> +     * device with a very long heartbeat). Either way we don't really

>>> +     * handle running out of time so lets catch it and report it here.

>>> +     */

>>> +    if (current_time == INT64_MAX) {

>>> +        qemu_handle_outa_time();

>>> +        goto out;

>>> +    }

>>> +

>>

>> Doing this here is a bit dangerous, I'd rather do nothing here and

>> detect the situation in cpus.c where we can do

>> qemu_system_shutdown_request() (and also do nothing).

> 

> You mean in notify_aio_contexts()? Sure we can do that.


Yes, that would work.  I think qemu_clock_deadline_ns_all would already
return -1 so that you'd have a zero-instruction budget from
tcg_get_icount_limit.

Paolo
diff mbox series

Patch

diff --git a/include/sysemu/cpus.h b/include/sysemu/cpus.h
index 3c1da6a0183..7da3a5b60b5 100644
--- a/include/sysemu/cpus.h
+++ b/include/sysemu/cpus.h
@@ -15,6 +15,8 @@  void configure_icount(QemuOpts *opts, Error **errp);
 extern int use_icount;
 extern int icount_align_option;
 
+void qemu_handle_outa_time(void);
+
 /* drift information for info jit command */
 extern int64_t max_delay;
 extern int64_t max_advance;
diff --git a/softmmu/cpus.c b/softmmu/cpus.c
index a802e899abb..46b6f346370 100644
--- a/softmmu/cpus.c
+++ b/softmmu/cpus.c
@@ -657,6 +657,19 @@  static bool shift_state_needed(void *opaque)
     return use_icount == 2;
 }
 
+/* Handler for reaching the end of time */
+void qemu_handle_outa_time(void)
+{
+    static bool reported = false;
+    if (runstate_is_running()) {
+        if (!reported) {
+            error_report("ran out of time, suspending emulation");
+            reported = true;
+        }
+        vm_stop(RUN_STATE_PAUSED);
+    }
+}
+
 /*
  * Subsection for warp timer migration is optional, because may not be created
  */
diff --git a/util/qemu-timer.c b/util/qemu-timer.c
index f62b4feecdb..06f6818eb3d 100644
--- a/util/qemu-timer.c
+++ b/util/qemu-timer.c
@@ -540,6 +540,22 @@  bool timerlist_run_timers(QEMUTimerList *timer_list)
      * done".
      */
     current_time = qemu_clock_get_ns(timer_list->clock->type);
+
+    /*
+     * Check to see if we have run out of time. Most of our time
+     * sources are nanoseconds since epoch (some time around the fall
+     * of Babylon 5, the start of the Enterprises five year mission
+     * and just before the arrival of the great evil ~ 2262CE).
+     * Although icount based time is ns since the start of emulation
+     * it is able to skip forward if the device is sleeping (think IoT
+     * device with a very long heartbeat). Either way we don't really
+     * handle running out of time so lets catch it and report it here.
+     */
+    if (current_time == INT64_MAX) {
+        qemu_handle_outa_time();
+        goto out;
+    }
+
     qemu_mutex_lock(&timer_list->active_timers_lock);
     while ((ts = timer_list->active_timers)) {
         if (!timer_expired_ns(ts, current_time)) {
diff --git a/stubs/Makefile.objs b/stubs/Makefile.objs
index d42046afe4e..d363d8b551b 100644
--- a/stubs/Makefile.objs
+++ b/stubs/Makefile.objs
@@ -1,5 +1,6 @@ 
 stub-obj-y += blk-commit-all.o
 stub-obj-y += cmos.o
+stub-obj-y += cpus.o
 stub-obj-y += cpu-get-clock.o
 stub-obj-y += cpu-get-icount.o
 stub-obj-y += dump.o