From patchwork Thu Jul 2 16:36:46 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: 490756 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 588CA1402A9 for ; Fri, 3 Jul 2015 02:37:50 +1000 (AEST) Received: from localhost ([::1]:37784 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1ZAhUi-0001qB-9F for incoming@patchwork.ozlabs.org; Thu, 02 Jul 2015 12:37:48 -0400 Received: from eggs.gnu.org ([2001:4830:134:3::10]:43603) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1ZAhU0-0000bZ-W7 for qemu-devel@nongnu.org; Thu, 02 Jul 2015 12:37:06 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1ZAhTs-0005DR-6E for qemu-devel@nongnu.org; Thu, 02 Jul 2015 12:37:04 -0400 Received: from e18.ny.us.ibm.com ([129.33.205.208]:39150) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1ZAhTs-0005Cw-3Z for qemu-devel@nongnu.org; Thu, 02 Jul 2015 12:36:56 -0400 Received: from /spool/local by e18.ny.us.ibm.com with IBM ESMTP SMTP Gateway: Authorized Use Only! Violators will be prosecuted for from ; Thu, 2 Jul 2015 12:36:57 -0400 Received: from d01dlp03.pok.ibm.com (9.56.250.168) by e18.ny.us.ibm.com (146.89.104.205) with IBM ESMTP SMTP Gateway: Authorized Use Only! Violators will be prosecuted; Thu, 2 Jul 2015 12:36:56 -0400 X-Helo: d01dlp03.pok.ibm.com X-MailFrom: jjherne@linux.vnet.ibm.com X-RcptTo: qemu-devel@nongnu.org Received: from b01cxnp22035.gho.pok.ibm.com (b01cxnp22035.gho.pok.ibm.com [9.57.198.25]) by d01dlp03.pok.ibm.com (Postfix) with ESMTP id 282FCC9003C for ; Thu, 2 Jul 2015 12:28:00 -0400 (EDT) Received: from d01av04.pok.ibm.com (d01av04.pok.ibm.com [9.56.224.64]) by b01cxnp22035.gho.pok.ibm.com (8.14.9/8.14.9/NCO v10.0) with ESMTP id t62GarBF53411964 for ; Thu, 2 Jul 2015 16:36:53 GMT Received: from d01av04.pok.ibm.com (localhost [127.0.0.1]) by d01av04.pok.ibm.com (8.14.4/8.14.4/NCO v10.0 AVout) with ESMTP id t62Gap3H032074 for ; Thu, 2 Jul 2015 12:36:53 -0400 Received: from jason-ThinkPad-W530.endicott.ibm.com (jason-thinkpad-w530.endicott.ibm.com [9.60.75.219]) by d01av04.pok.ibm.com (8.14.4/8.14.4/NCO v10.0 AVin) with ESMTP id t62GaoDu031895; Thu, 2 Jul 2015 12:36:51 -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, pbonzini@redhat.com Date: Thu, 2 Jul 2015 12:36:46 -0400 Message-Id: <1435855010-30882-2-git-send-email-jjherne@linux.vnet.ibm.com> X-Mailer: git-send-email 1.9.1 In-Reply-To: <1435855010-30882-1-git-send-email-jjherne@linux.vnet.ibm.com> References: <1435855010-30882-1-git-send-email-jjherne@linux.vnet.ibm.com> X-TM-AS-MML: disable X-Content-Scanned: Fidelis XPS MAILER x-cbid: 15070216-0045-0000-0000-0000009BB4BB X-detected-operating-system: by eggs.gnu.org: GNU/Linux 3.x X-Received-From: 129.33.205.208 Cc: "Jason J. Herne" Subject: [Qemu-devel] [PATCH v4 1/5] 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 set function and provide a percentage of throttle time. Signed-off-by: Jason J. Herne Reviewed-by: Matthew Rosato --- cpus.c | 66 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ include/qom/cpu.h | 38 ++++++++++++++++++++++++++++++++ 2 files changed, 104 insertions(+) diff --git a/cpus.c b/cpus.c index de6469f..6f86da0 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 10 + bool cpu_is_stopped(CPUState *cpu) { return cpu->stopped || !runstate_is_running(); @@ -486,10 +494,68 @@ static const VMStateDescription vmstate_timers = { } }; +static void cpu_throttle_thread(void *opaque) +{ + double pct = (double)throttle_percentage/100; + double throttle_ratio = pct / (1 - pct); + long sleeptime_ms = (long)(throttle_ratio * CPU_THROTTLE_TIMESLICE); + + if (!throttle_percentage) { + return; + } + + qemu_mutex_unlock_iothread(); + g_usleep(sleeptime_ms * 1000); /* Convert ms to us for usleep call */ + qemu_mutex_lock_iothread(); +} + +static void cpu_throttle_timer_tick(void *opaque) +{ + CPUState *cpu; + + /* Stop the timer if needed */ + if (!throttle_percentage) { + return; + } + CPU_FOREACH(cpu) { + async_run_on_cpu(cpu, cpu_throttle_thread, NULL); + } + + timer_mod(throttle_timer, qemu_clock_get_ms(QEMU_CLOCK_VIRTUAL_RT) + + CPU_THROTTLE_TIMESLICE); +} + +void cpu_throttle_set(int new_throttle_pct) +{ + /* Ensure throttle percentage is within valid range */ + new_throttle_pct = MIN(new_throttle_pct, CPU_THROTTLE_PCT_MAX); + throttle_percentage = MAX(new_throttle_pct, CPU_THROTTLE_PCT_MIN); + + timer_mod(throttle_timer, qemu_clock_get_ms(QEMU_CLOCK_VIRTUAL_RT) + + CPU_THROTTLE_TIMESLICE); +} + +void cpu_throttle_stop(void) +{ + throttle_percentage = 0; +} + +bool cpu_throttle_active(void) +{ + return (throttle_percentage != 0); +} + +int cpu_throttle_get_percentage(void) +{ + return throttle_percentage; +} + void cpu_ticks_init(void) { seqlock_init(&timers_state.vm_clock_seqlock, NULL); vmstate_register(NULL, 0, &vmstate_timers, &timers_state); + throttle_timer = timer_new_ms(QEMU_CLOCK_VIRTUAL_RT, + cpu_throttle_timer_tick, NULL); } void configure_icount(QemuOpts *opts, Error **errp) diff --git a/include/qom/cpu.h b/include/qom/cpu.h index 39f0f19..56eb964 100644 --- a/include/qom/cpu.h +++ b/include/qom/cpu.h @@ -553,6 +553,44 @@ CPUState *qemu_get_cpu(int index); */ bool cpu_exists(int64_t id); +/** + * cpu_throttle_set: + * @new_throttle_pct: Percent of sleep time to running time. + * Valid range is 1 to 99. + * + * Throttles all vcpus by forcing them to sleep for the given percentage of + * time. A throttle_percentage of 50 corresponds to a 50% duty cycle roughly. + * (example: 10ms sleep for every 10ms awake). + * + * cpu_throttle_set can be called as needed to adjust new_throttle_pct. + * Once the throttling starts, it will remain in effect until cpu_throttle_stop + * is called. + */ +void cpu_throttle_set(int new_throttle_pct); + +/** + * cpu_throttle_stop: + * + * Stops the vcpu throttling started by cpu_throttle_set. + */ +void cpu_throttle_stop(void); + +/** + * cpu_throttle_active: + * + * Returns %true if the vcpus are currently being throttled, %false otherwise. + */ +bool cpu_throttle_active(void); + +/** + * cpu_throttle_get_percentage: + * + * Returns the vcpu throttle percentage. See cpu_throttle_set for details. + * + * Returns The throttle percentage in range 1 to 99. + */ +int cpu_throttle_get_percentage(void); + #ifndef CONFIG_USER_ONLY typedef void (*CPUInterruptHandler)(CPUState *, int);