Received: from eggs.gnu.org ([2001:4830:134:3::10]:49846) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1bhTUP-0005vc-BG for qemu-devel@nongnu.org; Tue, 06 Sep 2016 23:25:30 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1bhTUL-00063M-4O for qemu-devel@nongnu.org; Tue, 06 Sep 2016 23:25:28 -0400 Received: from mail-pa0-x242.google.com ([2607:f8b0:400e:c03::242]:36217) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1bhTUK-00063G-Tv for qemu-devel@nongnu.org; Tue, 06 Sep 2016 23:25:25 -0400 Received: by mail-pa0-x242.google.com with SMTP id ez1so186014pab.3 for ; Tue, 06 Sep 2016 20:25:24 -0700 (PDT) Sender: Richard Henderson References: <1470929064-4092-1-git-send-email-alex.bennee@linaro.org> <1470929064-4092-15-git-send-email-alex.bennee@linaro.org> From: Richard Henderson Message-ID: <59f45c89-fac5-2221-161f-cdd94f6bf65c@twiddle.net> Date: Tue, 6 Sep 2016 20:25:22 -0700 MIME-Version: 1.0 In-Reply-To: <1470929064-4092-15-git-send-email-alex.bennee@linaro.org> Content-Type: text/plain; charset=utf-8; format=flowed Content-Transfer-Encoding: 8bit Subject: Re: [Qemu-devel] [RFC v4 14/28] tcg: add kick timer for single-threaded vCPU emulation List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , To: =?UTF-8?Q?Alex_Benn=c3=a9e?= , mttcg@greensocs.com, qemu-devel@nongnu.org, fred.konrad@greensocs.com, a.rigo@virtualopensystems.com, cota@braap.org, bobby.prani@gmail.com, nikunj@linux.vnet.ibm.com Cc: peter.maydell@linaro.org, claudio.fontana@huawei.com, Peter Crosthwaite , jan.kiszka@siemens.com, mark.burton@greensocs.com, serge.fdrv@gmail.com, pbonzini@redhat.com On 08/11/2016 08:24 AM, Alex Bennée wrote: > 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 > > --- > v2 > - re-base fixes > - get_ticks_per_sec() -> NANOSECONDS_PER_SEC > v3 > - add define for TCG_KICK_FREQ > - fix checkpatch warning > v4 > - wrap next calc in inline qemu_tcg_next_kick() instead of macro > --- > cpus.c | 32 ++++++++++++++++++++++++++++++++ > 1 file changed, 32 insertions(+) > > diff --git a/cpus.c b/cpus.c > index b5b45b8..8c49d6c 100644 > --- a/cpus.c > +++ b/cpus.c > @@ -1185,9 +1185,34 @@ static void deal_with_unplugged_cpus(void) > } > } > > +/* 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 void qemu_cpu_kick_no_halt(void); > + > +#define TCG_KICK_PERIOD (NANOSECONDS_PER_SECOND / 10) > + > +static inline int64_t qemu_tcg_next_kick(void) > +{ > + return qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL) + TCG_KICK_PERIOD; > +} > + > +static void kick_tcg_thread(void *opaque) > +{ > + QEMUTimer *self = *(QEMUTimer **) opaque; > + timer_mod(self, qemu_tcg_next_kick()); > + qemu_cpu_kick_no_halt(); > +} > + > static void *qemu_tcg_cpu_thread_fn(void *arg) > { > CPUState *cpu = arg; > + QEMUTimer *kick_timer; > > rcu_register_thread(); > > @@ -1211,6 +1236,13 @@ 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); I'm not especially keen on this pointer to local variable thing. Perhaps better as kick_timer = timer_new_ns(..., NULL); kick_timer->opaque = kick_timer; and avoid the indirection in kick_tcg_thread. Also, we should shut down the timer when the cpu is removed, surely? r~