Received: from eggs.gnu.org ([2001:4830:134:3::10]:37452) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1ZZcp0-0007do-CB for qemu-devel@nongnu.org; Wed, 09 Sep 2015 06:41:47 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1ZZcox-0003EB-NB for qemu-devel@nongnu.org; Wed, 09 Sep 2015 06:41:46 -0400 Received: from mx1.redhat.com ([209.132.183.28]:45317) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1ZZcox-00039Z-Hs for qemu-devel@nongnu.org; Wed, 09 Sep 2015 06:41:43 -0400 From: Juan Quintela In-Reply-To: <1441732357-11861-2-git-send-email-jjherne@linux.vnet.ibm.com> (Jason J. Herne's message of "Tue, 8 Sep 2015 13:12:33 -0400") References: <1441732357-11861-1-git-send-email-jjherne@linux.vnet.ibm.com> <1441732357-11861-2-git-send-email-jjherne@linux.vnet.ibm.com> Date: Wed, 09 Sep 2015 12:41:40 +0200 Message-ID: <87egi77u3f.fsf@neno.neno> MIME-Version: 1.0 Content-Type: text/plain Subject: Re: [Qemu-devel] [PATCH v7 1/5] cpu: Provide vcpu throttling interface Reply-To: quintela@redhat.com List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , To: "Jason J. Herne" Cc: qemu-devel@nongnu.org, dgilbert@redhat.com, borntraeger@de.ibm.com, amit.shah@redhat.com, pbonzini@redhat.com, afaerber@suse.de "Jason J. Herne" wrote: > Provide a method to throttle guest cpu execution. CPUState is augmented with > timeout controls and throttle start/stop functions. To throttle the guest cpu > the caller simply has to call the throttle set function and provide a percentage > of throttle time. > > Signed-off-by: Jason J. Herne > Reviewed-by: Matthew Rosato > --- > cpus.c | 78 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ > include/qom/cpu.h | 42 ++++++++++++++++++++++++++++++ > 2 files changed, 120 insertions(+) > > diff --git a/cpus.c b/cpus.c > index de6469f..b5ff9c9 100644 > --- a/cpus.c > +++ b/cpus.c > @@ -68,6 +68,14 @@ static CPUState *next_cpu; > int64_t max_delay; > int64_t max_advance; > > +/* vcpu throttling controls */ > +static QEMUTimer *throttle_timer; > +static unsigned int throttle_percentage; > + > +#define CPU_THROTTLE_PCT_MIN 1 > +#define CPU_THROTTLE_PCT_MAX 99 > +#define CPU_THROTTLE_TIMESLICE_NS 10000000 > + > bool cpu_is_stopped(CPUState *cpu) > { > return cpu->stopped || !runstate_is_running(); > @@ -486,10 +494,80 @@ static const VMStateDescription vmstate_timers = { > } > }; > > +static void cpu_throttle_thread(void *opaque) > +{ > + CPUState *cpu = opaque; > + double pct; > + double throttle_ratio; > + long sleeptime_ns; > + > + if (!cpu_throttle_get_percentage()) { cpu_throotle_active()? > + return; > + } > + > + pct = (double)cpu_throttle_get_percentage()/100; > + throttle_ratio = pct / (1 - pct); > + sleeptime_ns = (long)(throttle_ratio * CPU_THROTTLE_TIMESLICE_NS); > + > + qemu_mutex_unlock_iothread(); > + atomic_set(&cpu->throttle_thread_scheduled, 0); > + g_usleep(sleeptime_ns / 1000); /* Convert ns to us for usleep call */ > + qemu_mutex_lock_iothread(); Why is this thread safe? qemu_mutex_lock_iothread() is protecting (at least) cpu_work_first on each cpu. How can we be sure that _nothing_ will change that while we are waiting? A fast look through the tree don't show anything that runs here that drops the lock. I am missing something? > +} > + > +static void cpu_throttle_timer_tick(void *opaque) > +{ > + CPUState *cpu; > + double pct; > + > + /* Stop the timer if needed */ > + if (!cpu_throttle_get_percentage()) { cpu_throotle_active()? agree with rest of the changes. Later, Juan.