Skip to contentSkip to conversation

implement a new icount_no_rt mode

PATCH In review Victor CLEMENT 2 participants 10 messages May 27, 2015 – May 29, 2015

lore
Share & tools
lore
#0 lore raw

cover implement a new icount_no_rt mode

This patch adds a new icount_no_rt mode set by the new 'rt' parameter of the icount option.
When using icount_no_rt mode, the QEMU_VIRTUAL_CLOCK will be running at the maximal possible speed by warping the CPU sleep times to the soonest virtual clock deadline.
The goal is to get deterministic execution times by preventing potential latencies from qemu execution on the host machine.
The virtual cpus sleep time will be calculated but not spent, hence the virtual time will not be realistic when looking from the host point of view (ex. a 'sleep 10' on the guest will return almost instantly while the guest clock wil act like 10s have really passed)

This new mode will be used for testing real-time applications inside qemu. It makes the virtual clock "purely" virtual by removing any reference to host clocks in the calculation of QEMU_VIRTUAL_CLOCK value. Thus, some emulated devices which use other clocks as reference will not work in this mode. The virtual time would be running too fast compared to the actual "real" time.

To use this mode, one need to modify some emulators to simulate scenario using the virtual clock and report some events using the virtual clock too.
This kind of changes cannot be generic as the scenario will depend on the tested application, so modified emulators are not part of this patch set.

Victor CLEMENT (3):
icount: implement a new icount_no_rt mode without real time cpu
sleeping
icount: add rt parameter to the -icount option to toogle icount_no_rt
mode
icount: print a warning if there is no more deadline in no_rt mode

 cpus.c          | 85 +++++++++++++++++++++++++++++++++++++++------------------
 qemu-options.hx | 12 ++++++--
 vl.c            |  3 ++
 3 files changed, 72 insertions(+), 28 deletions(-)
Signature · 1 line
2.3.7
#1 lore raw

PATCH 1/3 icount: implement a new icount_no_rt mode without real time cpu sleeping

In this new icount_no_rt mode, the QEMU_VIRTUAL_CLOCK runs at the maximum possible speed by warping the sleep times of the virtual cpu to the soonest clock deadline. The virtual clock will be updated only according the instruction counter.

---
 cpus.c | 64 ++++++++++++++++++++++++++++++++++++++++------------------------
 1 file changed, 40 insertions(+), 24 deletions(-)
cpus.c +40 -24
index de6469f..012d14b 100644
@@ -105,6 +105,7 @@ static bool all_cpu_threads_idle(void)
 
 /* Protected by TimersState seqlock */
 
+static bool icount_no_rt;
 static int64_t vm_clock_warp_start = -1;
 /* Conversion factor from emulated instructions to virtual clock ticks.  */
 static int icount_time_shift;
@@ -393,15 +394,18 @@ void qemu_clock_warp(QEMUClockType type)
         return;
     }
 
-    /*
-     * If the CPUs have been sleeping, advance QEMU_CLOCK_VIRTUAL 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 QEMU_CLOCK_VIRTUAL timer.
-     */
-    icount_warp_rt(NULL);
-    timer_del(icount_warp_timer);
+    if (!icount_no_rt) {
+        /*
+         * If the CPUs have been sleeping, advance QEMU_CLOCK_VIRTUAL 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 QEMU_CLOCK_VIRTUAL timer.
+         */
+        icount_warp_rt(NULL);
+        timer_del(icount_warp_timer);
+    }
     if (!all_cpu_threads_idle()) {
         return;
     }
@@ -425,23 +429,35 @@ void qemu_clock_warp(QEMUClockType type)
          * interrupt to wake it up, but the interrupt never comes because
          * the vCPU isn't running any insns and thus doesn't advance the
          * QEMU_CLOCK_VIRTUAL.
-         *
-         * An extreme solution for this problem would be to never let VCPUs
-         * sleep in icount mode if there is a pending QEMU_CLOCK_VIRTUAL
-         * timer; rather time could just advance to the next QEMU_CLOCK_VIRTUAL
-         * event.  Instead, we do stop VCPUs and only advance QEMU_CLOCK_VIRTUAL
-         * after some "real" time, (related to the time left until the next
-         * event) has passed. The QEMU_CLOCK_VIRTUAL_RT clock will do this.
-         * This avoids that the warps are visible externally; for example,
-         * you will not be sending network packets continuously instead of
-         * every 100ms.
          */
-        seqlock_write_lock(&timers_state.vm_clock_seqlock);
-        if (vm_clock_warp_start == -1 || vm_clock_warp_start > clock) {
-            vm_clock_warp_start = clock;
+        if (icount_no_rt) {
+            /*
+             * We never let VCPUs sleep in async icount mode.
+             * If there is a pending QEMU_CLOCK_VIRTUAL timer we just advance
+             * to the next QEMU_CLOCK_VIRTUAL event and notify it.
+             * It is useful when we want a deterministic execution time,
+             * isolated from host latencies.
+             */
+            seqlock_write_lock(&timers_state.vm_clock_seqlock);
+            timers_state.qemu_icount_bias += deadline;
+            seqlock_write_unlock(&timers_state.vm_clock_seqlock);
+            qemu_clock_notify(QEMU_CLOCK_VIRTUAL);
+        } else {
+            /*
+             * We do stop VCPUs and only advance QEMU_CLOCK_VIRTUAL after some
+             * "real" time, (related to the time left until the next event) has
+             * passed. The QEMU_CLOCK_VIRTUAL_RT clock will do this.
+             * This avoids that the warps are visible externally; for example,
+             * you will not be sending network packets continuously instead of
+             * every 100ms.
+             */
+            seqlock_write_lock(&timers_state.vm_clock_seqlock);
+            if (vm_clock_warp_start == -1 || vm_clock_warp_start > clock) {
+                vm_clock_warp_start = clock;
+            }
+            seqlock_write_unlock(&timers_state.vm_clock_seqlock);
+            timer_mod_anticipate(icount_warp_timer, clock + deadline);
         }
-        seqlock_write_unlock(&timers_state.vm_clock_seqlock);
-        timer_mod_anticipate(icount_warp_timer, clock + deadline);
     } else if (deadline == 0) {
         qemu_clock_notify(QEMU_CLOCK_VIRTUAL);
     }
Signature · 1 line
2.3.7
#2 lore raw

Re: [Qemu-devel] [PATCH 1/3] icount: implement a new icount_no_rt mode without real time cpu sleeping

Quoted 15 lines · Victor CLEMENT

In this new icount_no_rt mode, the QEMU_VIRTUAL_CLOCK runs at the maximum possible speed by warping the sleep times of the virtual cpu to the soonest clock deadline. The virtual clock will be updated only according the instruction counter.

---
 cpus.c | 64 ++++++++++++++++++++++++++++++++++++++++------------------------
 1 file changed, 40 insertions(+), 24 deletions(-)
cpus.c +0 -0
index de6469f..012d14b 100644

cpus.c static bool all_cpu_threads_idle(void) · line 108

Quoted 4 lines
cpus.c +1 -0
@@ -105,6 +105,7 @@ static bool all_cpu_threads_idle(void)
 
 /* Protected by TimersState seqlock */
 
+static bool icount_no_rt;

It is somewhat hard to read the code due to double negations. What about "-icount sleep=[yes|no]" and naming the variable "icount_sleep"?

Quoted 42 lines
cpus.c +12 -16
 static int64_t vm_clock_warp_start = -1;
 /* Conversion factor from emulated instructions to virtual clock ticks.  */
 static int icount_time_shift;
@@ -393,15 +394,18 @@ void qemu_clock_warp(QEMUClockType type)
         return;
     }
 
-    /*
-     * If the CPUs have been sleeping, advance QEMU_CLOCK_VIRTUAL 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 QEMU_CLOCK_VIRTUAL timer.
-     */
-    icount_warp_rt(NULL);
-    timer_del(icount_warp_timer);
+    if (!icount_no_rt) {
+        /*
+         * If the CPUs have been sleeping, advance QEMU_CLOCK_VIRTUAL 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 QEMU_CLOCK_VIRTUAL timer.
+         */
+        icount_warp_rt(NULL);
+        timer_del(icount_warp_timer);
+    }
     if (!all_cpu_threads_idle()) {
         return;
     }
@@ -425,23 +429,35 @@ void qemu_clock_warp(QEMUClockType type)
          * interrupt to wake it up, but the interrupt never comes because
          * the vCPU isn't running any insns and thus doesn't advance the
          * QEMU_CLOCK_VIRTUAL.
-         *
-         * An extreme solution for this problem would be to never let VCPUs
-         * sleep in icount mode if there is a pending QEMU_CLOCK_VIRTUAL
-         * timer; rather time could just advance to the next QEMU_CLOCK_VIRTUAL
-         * event.  Instead, we do stop VCPUs and only advance QEMU_CLOCK_VIRTUAL
-         * after some "real" time, (related to the time left until the next
-         * event) has passed. The QEMU_CLOCK_VIRTUAL_RT clock will do this.

cpus.c void qemu_clock_warp(QEMUClockType type) · line 435

Quoted 10 lines
cpus.c +3 -6
-         * This avoids that the warps are visible externally; for example,
-         * you will not be sending network packets continuously instead of
-         * every 100ms.
          */
-        seqlock_write_lock(&timers_state.vm_clock_seqlock);
-        if (vm_clock_warp_start == -1 || vm_clock_warp_start > clock) {
-            vm_clock_warp_start = clock;
+        if (icount_no_rt) {
+            /*
+             * We never let VCPUs sleep in async icount mode.

s/async icount/sleep=no/

?

Otherwise the series looks good.

Paolo

Quoted 31 lines
cpus.c +24 -2
+             * If there is a pending QEMU_CLOCK_VIRTUAL timer we just advance
+             * to the next QEMU_CLOCK_VIRTUAL event and notify it.
+             * It is useful when we want a deterministic execution time,
+             * isolated from host latencies.
+             */
+            seqlock_write_lock(&timers_state.vm_clock_seqlock);
+            timers_state.qemu_icount_bias += deadline;
+            seqlock_write_unlock(&timers_state.vm_clock_seqlock);
+            qemu_clock_notify(QEMU_CLOCK_VIRTUAL);
+        } else {
+            /*
+             * We do stop VCPUs and only advance QEMU_CLOCK_VIRTUAL after some
+             * "real" time, (related to the time left until the next event) has
+             * passed. The QEMU_CLOCK_VIRTUAL_RT clock will do this.
+             * This avoids that the warps are visible externally; for example,
+             * you will not be sending network packets continuously instead of
+             * every 100ms.
+             */
+            seqlock_write_lock(&timers_state.vm_clock_seqlock);
+            if (vm_clock_warp_start == -1 || vm_clock_warp_start > clock) {
+                vm_clock_warp_start = clock;
+            }
+            seqlock_write_unlock(&timers_state.vm_clock_seqlock);
+            timer_mod_anticipate(icount_warp_timer, clock + deadline);
         }
-        seqlock_write_unlock(&timers_state.vm_clock_seqlock);
-        timer_mod_anticipate(icount_warp_timer, clock + deadline);
     } else if (deadline == 0) {
         qemu_clock_notify(QEMU_CLOCK_VIRTUAL);
     }
#3 lore raw

Re: [Qemu-devel] [PATCH 1/3] icount: implement a new icount_no_rt mode without real time cpu sleeping

----- Mail original -----

Quoted 28 lines

De: "Paolo Bonzini" <[email protected]>
À: "Victor CLEMENT" <[email protected]>, [email protected]
Cc: "francois guerret" <[email protected]>
Envoyé: Mercredi 27 Mai 2015 14:52:00
Objet: Re: [Qemu-devel] [PATCH 1/3] icount: implement a new icount_no_rt mode without real time cpu sleeping

Quoted 19 lines · Victor CLEMENT

In this new icount_no_rt mode, the QEMU_VIRTUAL_CLOCK runs at the maximum possible speed by warping the sleep times of the virtual cpu to the
soonest clock deadline. The virtual clock will be updated only according
the instruction counter.

---
 cpus.c | 64
 ++++++++++++++++++++++++++++++++++++++++------------------------
 1 file changed, 40 insertions(+), 24 deletions(-)
cpus.c +0 -0
index de6469f..012d14b 100644
@@ -105,6 +105,7 @@ static bool all_cpu_threads_idle(void)
 
Quoted 7 lines

cpus.c static bool all_cpu_threads_idle(void) · line 108

Quoted 3 lines
cpus.c +1 -0
 /* Protected by TimersState seqlock */
 
+static bool icount_no_rt;

It is somewhat hard to read the code due to double negations. What about "-icount sleep=[yes|no]" and naming the variable "icount_sleep"?

You are right, the "nort" can be ambiguous. icount_sleep seems clear. Thanks for the suggestion.

Quoted 8 lines

[...]

cpus.c void qemu_clock_warp(QEMUClockType type) · line 435

Quoted 3 lines
cpus.c +3 -0
+        if (icount_no_rt) {
+            /*
+             * We never let VCPUs sleep in async icount mode.

s/async icount/sleep=no/

?

Right ! My bad...

Quoted 4 lines

Otherwise the series looks good.

Paolo

Ok, I sumbit the v2 with icount_sleep naming.

Victor

Quoted 43 lines
Quoted 41 lines

+ * If there is a pending QEMU_CLOCK_VIRTUAL timer we
just advance

cpus.c +2 -0
+             * to the next QEMU_CLOCK_VIRTUAL event and notify it.
+             * It is useful when we want a deterministic execution

time,

cpus.c +9 -0
+             * isolated from host latencies.
+             */
+            seqlock_write_lock(&timers_state.vm_clock_seqlock);
+            timers_state.qemu_icount_bias += deadline;
+            seqlock_write_unlock(&timers_state.vm_clock_seqlock);
+            qemu_clock_notify(QEMU_CLOCK_VIRTUAL);
+        } else {
+            /*
+             * We do stop VCPUs and only advance

QEMU_CLOCK_VIRTUAL after some
+ * "real" time, (related to the time left until the
next event) has
+ * passed. The QEMU_CLOCK_VIRTUAL_RT clock will do
this.
+ * This avoids that the warps are visible externally;
for example,
+ * you will not be sending network packets
continuously instead of

cpus.c +4 -0
+             * every 100ms.
+             */
+            seqlock_write_lock(&timers_state.vm_clock_seqlock);
+            if (vm_clock_warp_start == -1 || vm_clock_warp_start >

clock) {

cpus.c +4 -0
+                vm_clock_warp_start = clock;
+            }
+            seqlock_write_unlock(&timers_state.vm_clock_seqlock);
+            timer_mod_anticipate(icount_warp_timer, clock +
deadline);
         }
cpus.c +0 -2
-        seqlock_write_unlock(&timers_state.vm_clock_seqlock);
-        timer_mod_anticipate(icount_warp_timer, clock + deadline);
     } else if (deadline == 0) {
         qemu_clock_notify(QEMU_CLOCK_VIRTUAL);
     }
#4 lore raw

PATCH 2/3 icount: add rt parameter to the -icount option to toogle icount_no_rt mode

In reply to Victor CLEMENT

The 'rt' parameter sets the icount_no_rt mode, which is not enabled by default. To enable it, add the 'nort' parameter (or 'rt=off') to the qemu -icount option.

---
 cpus.c          | 15 +++++++++++++--
 qemu-options.hx | 12 ++++++++++--
 vl.c            |  3 +++
 3 files changed, 26 insertions(+), 4 deletions(-)
cpus.c +13 -2
index 012d14b..eac3e19 100644
@@ -520,9 +520,18 @@ void configure_icount(QemuOpts *opts, Error **errp)
         }
         return;
     }
+
+    icount_no_rt = !qemu_opt_get_bool(opts, "rt", true);
+    if (!icount_no_rt) {
+        icount_warp_timer = timer_new_ns(QEMU_CLOCK_VIRTUAL_RT,
+                                         icount_warp_rt, NULL);
+    }
+
     icount_align_option = qemu_opt_get_bool(opts, "align", false);
-    icount_warp_timer = timer_new_ns(QEMU_CLOCK_VIRTUAL_RT,
-                                     icount_warp_rt, NULL);
+
+    if (icount_align_option && icount_no_rt) {
+        error_setg(errp, "align and nort options are incompatible");
+    }
     if (strcmp(option, "auto") != 0) {
         errno = 0;
         icount_time_shift = strtol(option, &rem_str, 0);
@@ -533,6 +542,8 @@ void configure_icount(QemuOpts *opts, Error **errp)
         return;
     } else if (icount_align_option) {
         error_setg(errp, "shift=auto and align=on are incompatible");
+    } else if (icount_no_rt) {
+        error_setg(errp, "shift=auto and nort are incompatible");
     }
 
     use_icount = 2;
qemu-options.hx +10 -2
index ec356f6..531bed7 100644
@@ -3086,9 +3086,10 @@ re-inject them.
 ETEXI
 
 DEF("icount", HAS_ARG, QEMU_OPTION_icount, \
-    "-icount [shift=N|auto][,align=on|off]\n" \
+    "-icount [shift=N|auto][,align=on|off][,nort]\n" \
     "                enable virtual instruction counter with 2^N clock ticks per\n" \
-    "                instruction and enable aligning the host and virtual clocks\n", QEMU_ARCH_ALL)
+    "                instruction, enable aligning the host and virtual clocks\n" \
+    "                or disable real time cpu sleeping\n", QEMU_ARCH_ALL)
 STEXI
 @item -icount [shift=@var{N}|auto]
 @findex -icount
@@ -3097,6 +3098,13 @@ instruction every 2^@var{N} ns of virtual time.  If @code{auto} is specified
 then the virtual cpu speed will be automatically adjusted to keep virtual
 time within a few seconds of real time.
 
+When the virtual cpu is sleeping, the virtual time will advance at default
+speed unless @option{nort} is specified.
+With @option{nort}, the virtual time will jump to the next timer deadline
+instantly whenever the virtual cpu goes to sleep mode and will not advance
+if no timer is enabled. This behavior give deterministic execution times from
+the guest point of view.
+
 Note that while this option can give deterministic behavior, it does not
 provide cycle accurate emulation.  Modern CPUs contain superscalar out of
 order cores with complex cache hierarchies.  The number of instructions
vl.c +3 -0
index 15bccc4..0c377e8 100644
@@ -470,6 +470,9 @@ static QemuOptsList qemu_icount_opts = {
         }, {
             .name = "align",
             .type = QEMU_OPT_BOOL,
+        }, {
+            .name = "rt",
+            .type = QEMU_OPT_BOOL,
         },
         { /* end of list */ }
     },
Signature · 1 line
2.3.7
#5 lore raw

PATCH 3/3 icount: print a warning if there is no more deadline in no_rt mode

In reply to Victor CLEMENT

While qemu is running in icount_no_rt mode, a warning will be printed when no timer deadline is set.
As this mode is intended for getting deterministic virtual time, if no timer is set on the virtual clock this determinism is broken.

---
 cpus.c | 6 ++++++
 1 file changed, 6 insertions(+)
cpus.c +6 -0
index eac3e19..c358c2e 100644
@@ -419,6 +419,12 @@ void qemu_clock_warp(QEMUClockType type)
     clock = qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL_RT);
     deadline = qemu_clock_deadline_ns_all(QEMU_CLOCK_VIRTUAL);
     if (deadline < 0) {
+        static bool notified;
+        if (icount_no_rt && !notified) {
+            fprintf(stderr, "cpus: WARNING: icount no_rt enabled and no more \
+active timer\n");
+            notified = true;
+        }
         return;
     }
 
Signature · 1 line
2.3.7
#6 lore raw
In reply to Victor CLEMENT
Quoted 6 lines · Victor CLEMENT

To use this mode, one need to modify some emulators to simulate scenario using the virtual clock and report some events using the virtual clock too.
This kind of changes cannot be generic as the scenario will depend on the tested application, so modified emulators are not part of this patch set.

There should be no need to modify device models if you use "-rtc clock=vm". Or are there other cases besides RTC devices?

Paolo

#7 lore raw

----- Mail original -----

Quoted 10 lines

De: "Paolo Bonzini" <[email protected]>
À: "Victor CLEMENT" <[email protected]>, [email protected]
Cc: "francois guerret" <[email protected]>
Envoyé: Mercredi 27 Mai 2015 14:52:55
Objet: Re: [Qemu-devel] [PATCH 0/3] implement a new icount_no_rt mode

[...] Or are there other cases besides RTC devices?

Paolo

For the normal use case of QEmu, No. But we want to use QEmu to test hard real time application against simulated external stimuli.

So basically, Qemu will be used to run the embedded software, but the external interfaces (GPIO, SPI, CAN bus...) will be controlled by a basic simulator playing some predefined scenario. Also we will record some traces to validate the behavior.

The scenario engine and the trace recorder will need to use the virtual clock, so that all dates are based on the same clock.

Again, at this stage we think this scenario engine will not be generic so the code will not be ready for integration in qemu.

Our goal is to be able to run a real time application (under an RTOS) and test it with some scenario :
For instance our application could monitor a GPIO and send a specific CAN frame within a specified delay.
In that case, the scenario will control the GPIO input and the recorder will trace both the GPIO and the CAN bus. So by analysing the traces we will be able to check that the software meets its timing constraints.

This example is simple but still shows the kind of problems we want to solve.

I hope this enlighten you on our motivations.

Best regards,

Victor Clément & Julien Viard de Galbert

#8 lore raw
Quoted 11 lines · Victor Clement

----- Mail original -----

Quoted 7 lines

De: "Paolo Bonzini" <[email protected]>
À: "Victor CLEMENT" <[email protected]>, [email protected]
Cc: "francois guerret" <[email protected]>
Envoyé: Mercredi 27 Mai 2015 14:52:55
Objet: Re: [Qemu-devel] [PATCH 0/3] implement a new icount_no_rt mode

[...] Or are there other cases besides RTC devices?

The scenario engine and the trace recorder will need to use the virtual clock, so that all dates are based on the same clock.

Right, but there is no reason for these to use any other clock than the virtual clock.

Quoted 1 line

I hope this enlighten you on our motivations.

I understood your motivations; I'm still not really sure what other modifications you need in order to achieve determinism.

Paolo

#9 lore raw

----- Mail original -----

Quoted 28 lines

De: "Paolo Bonzini" <[email protected]>
À: "Victor Clement" <[email protected]>, [email protected]
Cc: "françois Guerret" <[email protected]>, "Julien Viard de Galbert" <[email protected]> Envoyé: Vendredi 29 Mai 2015 13:14:38
Objet: Re: [Qemu-devel] [PATCH 0/3] implement a new icount_no_rt mode

Quoted 9 lines · Victor Clement

----- Mail original -----

Quoted 8 lines

De: "Paolo Bonzini" <[email protected]>
À: "Victor CLEMENT" <[email protected]>, [email protected]
Cc: "francois guerret" <[email protected]> Envoyé: Mercredi 27 Mai 2015 14:52:55
Objet: Re: [Qemu-devel] [PATCH 0/3] implement a new icount_no_rt mode

Quoted 5 lines
Quoted 1 line

[...] Or are there other cases besides RTC devices?

The scenario engine and the trace recorder will need to use the virtual
clock, so that all dates are based on the same clock.

Right, but there is no reason for these to use any other clock than the
virtual clock.

Quoted 7 lines
Quoted 1 line

I hope this enlighten you on our motivations.

I understood your motivations; I'm still not really sure what other modifications you need in order to achieve determinism.

Paolo

Normally none for determinism.

The only modification we need for our use case is to add some interfaces with the scenario engine and the trace recorder.

Victor