From patchwork Tue Jun 2 17:46:47 2015 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "Jason J. Herne" X-Patchwork-Id: 479641 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Received: from lists.gnu.org (lists.gnu.org [IPv6:2001:4830:134:3::11]) (using TLSv1 with cipher AES256-SHA (256/256 bits)) (No client certificate requested) by ozlabs.org (Postfix) with ESMTPS id 565601412E7 for ; Wed, 3 Jun 2015 03:48:23 +1000 (AEST) Received: from localhost ([::1]:60272 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1YzqIX-0005Wq-HO for incoming@patchwork.ozlabs.org; Tue, 02 Jun 2015 13:48:21 -0400 Received: from eggs.gnu.org ([2001:4830:134:3::10]:60554) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1YzqHT-0003f5-32 for qemu-devel@nongnu.org; Tue, 02 Jun 2015 13:47:16 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1YzqHP-0006cX-SC for qemu-devel@nongnu.org; Tue, 02 Jun 2015 13:47:15 -0400 Received: from e39.co.us.ibm.com ([32.97.110.160]:45759) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1YzqHP-0006cN-M0 for qemu-devel@nongnu.org; Tue, 02 Jun 2015 13:47:11 -0400 Received: from /spool/local by e39.co.us.ibm.com with IBM ESMTP SMTP Gateway: Authorized Use Only! Violators will be prosecuted for from ; Tue, 2 Jun 2015 11:47:11 -0600 Received: from d01dlp02.pok.ibm.com (9.56.250.167) by e39.co.us.ibm.com (192.168.1.139) with IBM ESMTP SMTP Gateway: Authorized Use Only! Violators will be prosecuted; Tue, 2 Jun 2015 11:46:51 -0600 Received: from b01cxnp23032.gho.pok.ibm.com (b01cxnp23032.gho.pok.ibm.com [9.57.198.27]) by d01dlp02.pok.ibm.com (Postfix) with ESMTP id 75D516E803F for ; Tue, 2 Jun 2015 13:38:38 -0400 (EDT) Received: from d01av01.pok.ibm.com (d01av01.pok.ibm.com [9.56.224.215]) by b01cxnp23032.gho.pok.ibm.com (8.14.9/8.14.9/NCO v10.0) with ESMTP id t52HkogK51380400 for ; Tue, 2 Jun 2015 17:46:51 GMT Received: from d01av01.pok.ibm.com (localhost [127.0.0.1]) by d01av01.pok.ibm.com (8.14.4/8.14.4/NCO v10.0 AVout) with ESMTP id t52HkobT021256 for ; Tue, 2 Jun 2015 13:46:50 -0400 Received: from jason-ThinkPad-W530.endicott.ibm.com (jason-thinkpad-w530.endicott.ibm.com [9.60.75.219]) by d01av01.pok.ibm.com (8.14.4/8.14.4/NCO v10.0 AVin) with ESMTP id t52HknEc021141; Tue, 2 Jun 2015 13:46:50 -0400 From: "Jason J. Herne" To: afaerber@suse.de, amit.shah@redhat.com, dgilbert@redhat.com, borntraeger@de.ibm.com, quintela@redhat.com, qemu-devel@nongnu.org Date: Tue, 2 Jun 2015 13:46:47 -0400 Message-Id: <1433267209-9882-2-git-send-email-jjherne@linux.vnet.ibm.com> X-Mailer: git-send-email 1.9.1 In-Reply-To: <1433267209-9882-1-git-send-email-jjherne@linux.vnet.ibm.com> References: <1433267209-9882-1-git-send-email-jjherne@linux.vnet.ibm.com> X-TM-AS-MML: disable X-Content-Scanned: Fidelis XPS MAILER x-cbid: 15060217-0033-0000-0000-000004B2F38B X-detected-operating-system: by eggs.gnu.org: GNU/Linux 3.x X-Received-From: 32.97.110.160 Cc: "Jason J. Herne" Subject: [Qemu-devel] [PATCH v2 1/3] cpu: Provide vcpu throttling interface X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.14 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org Sender: qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org 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 start function and provide a ratio of sleep time to normal execution time. Signed-off-by: Jason J. Herne Reviewed-by: Matthew Rosato --- cpus.c | 62 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ include/qom/cpu.h | 46 +++++++++++++++++++++++++++++++++++++++++ 2 files changed, 108 insertions(+) diff --git a/cpus.c b/cpus.c index de6469f..7568357 100644 --- a/cpus.c +++ b/cpus.c @@ -64,6 +64,9 @@ #endif /* CONFIG_LINUX */ +/* Number of ms between cpu throttle operations */ +#define CPU_THROTTLE_TIMESLICE 10 + static CPUState *next_cpu; int64_t max_delay; int64_t max_advance; @@ -919,6 +922,65 @@ static void qemu_kvm_wait_io_event(CPUState *cpu) qemu_wait_io_event_common(cpu); } +static void cpu_throttle_thread(void *opq) +{ + CPUState *cpu = (CPUState *)opq; + long sleeptime_ms = (long)(cpu->throttle_ratio * CPU_THROTTLE_TIMESLICE); + + /* Stop the timer if needed */ + if (cpu->throttle_timer_stop) { + timer_del(cpu->throttle_timer); + timer_free(cpu->throttle_timer); + cpu->throttle_timer = NULL; + return; + } + + qemu_mutex_unlock_iothread(); + g_usleep(sleeptime_ms * 1000); /* Convert ms to us for usleep call */ + qemu_mutex_lock_iothread(); + + timer_mod(cpu->throttle_timer, qemu_clock_get_ms(QEMU_CLOCK_REALTIME) + + CPU_THROTTLE_TIMESLICE); +} + +static void cpu_throttle_timer_pop(void *opq) +{ + CPUState *cpu = (CPUState *)opq; + + async_run_on_cpu(cpu, cpu_throttle_thread, cpu); +} + +void cpu_throttle_start(CPUState *cpu, float throttle_ratio) +{ + assert(throttle_ratio > 0); + cpu->throttle_ratio = throttle_ratio; + + if (!cpu_throttle_active(cpu)) { + cpu->throttle_timer = timer_new_ms(QEMU_CLOCK_REALTIME, + cpu_throttle_timer_pop, cpu); + timer_mod(cpu->throttle_timer, qemu_clock_get_ms(QEMU_CLOCK_REALTIME) + + CPU_THROTTLE_TIMESLICE); + cpu->throttle_timer_stop = false; + } +} + +void cpu_throttle_stop(CPUState *cpu) +{ + assert(cpu_throttle_active(cpu)); + cpu->throttle_timer_stop = true; +} + +bool cpu_throttle_active(CPUState *cpu) +{ + return (cpu->throttle_timer != NULL); +} + +float cpu_throttle_get_ratio(CPUState *cpu) +{ + assert(cpu_throttle_active(cpu)); + return cpu->throttle_ratio; +} + static void *qemu_kvm_cpu_thread_fn(void *arg) { CPUState *cpu = arg; diff --git a/include/qom/cpu.h b/include/qom/cpu.h index 39f0f19..9d16e6a 100644 --- a/include/qom/cpu.h +++ b/include/qom/cpu.h @@ -310,6 +310,11 @@ struct CPUState { uint32_t can_do_io; int32_t exception_index; /* used by m68k TCG */ + /* vcpu throttling controls */ + QEMUTimer *throttle_timer; + bool throttle_timer_stop; + float throttle_ratio; + /* Note that this is accessed at the start of every TB via a negative offset from AREG0. Leave this field at the end so as to make the (absolute value) offset as small as possible. This reduces code @@ -553,6 +558,47 @@ CPUState *qemu_get_cpu(int index); */ bool cpu_exists(int64_t id); +/** + * cpu_throttle_start: + * @cpu: The vcpu to throttle + * + * Throttles a vcpu by forcing it to sleep. The duration of the sleep is a + * ratio of sleep time to running time. A ratio of 1.0 corresponds to a 50% + * duty cycle (example: 10ms sleep for every 10ms awake). + * + * cpu_throttle_start can be called as needed to adjust the throttle ratio. + * Once the throttling starts, it will remain in effect until cpu_throttle_stop + * is called. + */ +void cpu_throttle_start(CPUState *cpu, float throttle_ratio); + +/** + * cpu_throttle_stop: + * @cpu: The vcpu to stop throttling + * + * Stops the vcpu throttling started by cpu_throttle_start. + */ +void cpu_throttle_stop(CPUState *cpu); + +/** + * cpu_throttle_active: + * @cpu: The vcpu to check + * + * Returns %true if this vcpu is currently being throttled, %false otherwise. + */ +bool cpu_throttle_active(CPUState *cpu); + +/** + * cpu_throttle_get_ratio: + * @cpu: The vcpu whose throttle ratio to return. + * + * Returns the ratio being used to throttle this vcpu. See cpu_throttle_start + * for details. + * + * Returns The ratio being used to throttle this vcpu. + */ +float cpu_throttle_get_ratio(CPUState *cpu); + #ifndef CONFIG_USER_ONLY typedef void (*CPUInterruptHandler)(CPUState *, int);