diff mbox

[RFC,v2,09/11] tcg: add kick timer for single-threaded vCPU emulation

Message ID 1459870344-16773-10-git-send-email-alex.bennee@linaro.org (mailing list archive)
State New, archived
Headers show

Commit Message

Alex Bennée April 5, 2016, 3:32 p.m. UTC
Currently we rely on the side effect of the main loop grabbing the
iothread_mutex to give any long running basic block chains a kick to
ensure the next vCPU is scheduled. As this code is being re-factored and
rationalised we now do it explicitly here.

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

---
v2
  - re-base fixes
  - get_ticks_per_sec() -> NANOSECONDS_PER_SEC
---
 cpus.c | 27 +++++++++++++++++++++++++++
 1 file changed, 27 insertions(+)

Comments

Sergey Fedorov April 11, 2016, 9:39 p.m. UTC | #1
On 05/04/16 18:32, Alex Bennée wrote:
> +static void kick_tcg_thread(void *opaque)
> +{
> +    QEMUTimer *self = *(QEMUTimer **) opaque;
> +    timer_mod(self,
> +              qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL) +
> +              NANOSECONDS_PER_SECOND / 10);
> +    qemu_cpu_kick_no_halt();
> +}
>  

It would be nice to have some definition (e.g. macro) of TCG thread kick
period.

(snip)

> @@ -1179,6 +1198,14 @@ static void *qemu_tcg_cpu_thread_fn(void *arg)
>          }
>      }
>  
> +    /* Set to kick if we have to do more than one vCPU */
> +    if (CPU_NEXT(first_cpu)) {
> +        kick_timer = timer_new_ns(QEMU_CLOCK_VIRTUAL,  kick_tcg_thread, &kick_timer);
> +        timer_mod(kick_timer,
> +                  qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL) +
> +                  NANOSECONDS_PER_SECOND / 10);
> +    }
> +    

I think cpu_ticks_init() could be more natural place to put this
initialization in.

Kind regards,
Sergey
Alex Bennée June 2, 2016, 4 p.m. UTC | #2
Sergey Fedorov <serge.fdrv@gmail.com> writes:

> On 05/04/16 18:32, Alex Bennée wrote:
>> +static void kick_tcg_thread(void *opaque)
>> +{
>> +    QEMUTimer *self = *(QEMUTimer **) opaque;
>> +    timer_mod(self,
>> +              qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL) +
>> +              NANOSECONDS_PER_SECOND / 10);
>> +    qemu_cpu_kick_no_halt();
>> +}
>>
>
> It would be nice to have some definition (e.g. macro) of TCG thread kick
> period.

Will do.

>
> (snip)
>
>> @@ -1179,6 +1198,14 @@ static void *qemu_tcg_cpu_thread_fn(void *arg)
>>          }
>>      }
>>
>> +    /* Set to kick if we have to do more than one vCPU */
>> +    if (CPU_NEXT(first_cpu)) {
>> +        kick_timer = timer_new_ns(QEMU_CLOCK_VIRTUAL,  kick_tcg_thread, &kick_timer);
>> +        timer_mod(kick_timer,
>> +                  qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL) +
>> +                  NANOSECONDS_PER_SECOND / 10);
>> +    }
>> +
>
> I think cpu_ticks_init() could be more natural place to put this
> initialization in.

It would be but I need somewhere to keep kick_timer and doing it inside
the thread function keeps it nice and local.

>
> Kind regards,
> Sergey


--
Alex Bennée
Sergey Fedorov June 2, 2016, 4:05 p.m. UTC | #3
On 02/06/16 19:00, Alex Bennée wrote:
> Sergey Fedorov <serge.fdrv@gmail.com> writes:
>> On 05/04/16 18:32, Alex Bennée wrote:
>>> @@ -1179,6 +1198,14 @@ static void *qemu_tcg_cpu_thread_fn(void *arg)
>>>          }
>>>      }
>>>
>>> +    /* Set to kick if we have to do more than one vCPU */
>>> +    if (CPU_NEXT(first_cpu)) {
>>> +        kick_timer = timer_new_ns(QEMU_CLOCK_VIRTUAL,  kick_tcg_thread, &kick_timer);
>>> +        timer_mod(kick_timer,
>>> +                  qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL) +
>>> +                  NANOSECONDS_PER_SECOND / 10);
>>> +    }
>>> +
>> I think cpu_ticks_init() could be more natural place to put this
>> initialization in.
> It would be but I need somewhere to keep kick_timer and doing it inside
> the thread function keeps it nice and local.

Fair enough. By the way, this kick timer is only required for
round-robin single-threaded CPU loop, right?

Kind regards,
Sergey
diff mbox

Patch

diff --git a/cpus.c b/cpus.c
index 8d27fb0..e22bb77 100644
--- a/cpus.c
+++ b/cpus.c
@@ -1151,11 +1151,30 @@  static void *qemu_dummy_cpu_thread_fn(void *arg)
 #endif
 }
 
+/* Single-threaded TCG
+ *
+ * In the single-threaded case each vCPU is simulated in turn. If
+ * there is more than a single vCPU we create a simple timer to kick
+ * the vCPU and ensure we don't get stuck in a tight loop in one vCPU.
+ * This is done explicitly rather than relying on side-effects
+ * elsewhere.
+ */
 static int tcg_cpu_exec(CPUState *cpu);
+static void qemu_cpu_kick_no_halt(void);
+
+static void kick_tcg_thread(void *opaque)
+{
+    QEMUTimer *self = *(QEMUTimer **) opaque;
+    timer_mod(self,
+              qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL) +
+              NANOSECONDS_PER_SECOND / 10);
+    qemu_cpu_kick_no_halt();
+}
 
 static void *qemu_tcg_cpu_thread_fn(void *arg)
 {
     CPUState *cpu = arg;
+    QEMUTimer *kick_timer;
 
     rcu_register_thread();
 
@@ -1179,6 +1198,14 @@  static void *qemu_tcg_cpu_thread_fn(void *arg)
         }
     }
 
+    /* Set to kick if we have to do more than one vCPU */
+    if (CPU_NEXT(first_cpu)) {
+        kick_timer = timer_new_ns(QEMU_CLOCK_VIRTUAL,  kick_tcg_thread, &kick_timer);
+        timer_mod(kick_timer,
+                  qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL) +
+                  NANOSECONDS_PER_SECOND / 10);
+    }
+
     /* process any pending work */
     atomic_mb_set(&exit_request, 1);