mirror of
https://github.com/xemu-project/xemu.git
synced 2024-12-08 20:18:52 +00:00
33c38f8ca1
During migrations, after each iteration, cpu_throttle_set() is called, which irrespective of input, re-arms the timer according to value of new_throttle_pct. This causes cpu_throttle_thread() to be delayed in getting scheduled and consqeuntly lets guest run for more time than what the throttle value should allow. This leads to spikes in guest throughput at high cpu-throttle percentage whenever cpu_throttle_set() is called. A solution would be not to modify the timer immediately in cpu_throttle_set(), instead, only modify throttle_percentage so that the throttle would automatically adjust to the required percentage when cpu_throttle_timer_tick() is invoked. Manually tested the patch using following configuration: Guest: Centos7 (3.10.0-123.el7.x86_64) Total Memory - 64GB , CPUs - 16 Tool used - stress (1.0.4) Workload - stress --vm 32 --vm-bytes 1G --vm-keep Migration Parameters: Network Bandwidth - 500MBPS cpu-throttle-initial - 99 Results: With timer_mod(): fails to converge, continues indefinitely Without timer_mod(): converges in 249 sec Signed-off-by: Utkarsh Tripathi <utkarsh.tripathi@nutanix.com> Message-Id: <1609420384-119407-1-git-send-email-utkarsh.tripathi@nutanix.com> Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
130 lines
4.0 KiB
C
130 lines
4.0 KiB
C
/*
|
|
* QEMU System Emulator
|
|
*
|
|
* Copyright (c) 2003-2008 Fabrice Bellard
|
|
*
|
|
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
* of this software and associated documentation files (the "Software"), to deal
|
|
* in the Software without restriction, including without limitation the rights
|
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
* copies of the Software, and to permit persons to whom the Software is
|
|
* furnished to do so, subject to the following conditions:
|
|
*
|
|
* The above copyright notice and this permission notice shall be included in
|
|
* all copies or substantial portions of the Software.
|
|
*
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
|
|
* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
* THE SOFTWARE.
|
|
*/
|
|
|
|
#include "qemu/osdep.h"
|
|
#include "qemu-common.h"
|
|
#include "qemu/thread.h"
|
|
#include "hw/core/cpu.h"
|
|
#include "qemu/main-loop.h"
|
|
#include "sysemu/cpus.h"
|
|
#include "sysemu/cpu-throttle.h"
|
|
|
|
/* 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
|
|
|
|
static void cpu_throttle_thread(CPUState *cpu, run_on_cpu_data opaque)
|
|
{
|
|
double pct;
|
|
double throttle_ratio;
|
|
int64_t sleeptime_ns, endtime_ns;
|
|
|
|
if (!cpu_throttle_get_percentage()) {
|
|
return;
|
|
}
|
|
|
|
pct = (double)cpu_throttle_get_percentage() / 100;
|
|
throttle_ratio = pct / (1 - pct);
|
|
/* Add 1ns to fix double's rounding error (like 0.9999999...) */
|
|
sleeptime_ns = (int64_t)(throttle_ratio * CPU_THROTTLE_TIMESLICE_NS + 1);
|
|
endtime_ns = qemu_clock_get_ns(QEMU_CLOCK_REALTIME) + sleeptime_ns;
|
|
while (sleeptime_ns > 0 && !cpu->stop) {
|
|
if (sleeptime_ns > SCALE_MS) {
|
|
qemu_cond_timedwait_iothread(cpu->halt_cond,
|
|
sleeptime_ns / SCALE_MS);
|
|
} else {
|
|
qemu_mutex_unlock_iothread();
|
|
g_usleep(sleeptime_ns / SCALE_US);
|
|
qemu_mutex_lock_iothread();
|
|
}
|
|
sleeptime_ns = endtime_ns - qemu_clock_get_ns(QEMU_CLOCK_REALTIME);
|
|
}
|
|
qatomic_set(&cpu->throttle_thread_scheduled, 0);
|
|
}
|
|
|
|
static void cpu_throttle_timer_tick(void *opaque)
|
|
{
|
|
CPUState *cpu;
|
|
double pct;
|
|
|
|
/* Stop the timer if needed */
|
|
if (!cpu_throttle_get_percentage()) {
|
|
return;
|
|
}
|
|
CPU_FOREACH(cpu) {
|
|
if (!qatomic_xchg(&cpu->throttle_thread_scheduled, 1)) {
|
|
async_run_on_cpu(cpu, cpu_throttle_thread,
|
|
RUN_ON_CPU_NULL);
|
|
}
|
|
}
|
|
|
|
pct = (double)cpu_throttle_get_percentage() / 100;
|
|
timer_mod(throttle_timer, qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL_RT) +
|
|
CPU_THROTTLE_TIMESLICE_NS / (1 - pct));
|
|
}
|
|
|
|
void cpu_throttle_set(int new_throttle_pct)
|
|
{
|
|
/*
|
|
* boolean to store whether throttle is already active or not,
|
|
* before modifying throttle_percentage
|
|
*/
|
|
bool throttle_active = cpu_throttle_active();
|
|
|
|
/* Ensure throttle percentage is within valid range */
|
|
new_throttle_pct = MIN(new_throttle_pct, CPU_THROTTLE_PCT_MAX);
|
|
new_throttle_pct = MAX(new_throttle_pct, CPU_THROTTLE_PCT_MIN);
|
|
|
|
qatomic_set(&throttle_percentage, new_throttle_pct);
|
|
|
|
if (!throttle_active) {
|
|
cpu_throttle_timer_tick(NULL);
|
|
}
|
|
}
|
|
|
|
void cpu_throttle_stop(void)
|
|
{
|
|
qatomic_set(&throttle_percentage, 0);
|
|
}
|
|
|
|
bool cpu_throttle_active(void)
|
|
{
|
|
return (cpu_throttle_get_percentage() != 0);
|
|
}
|
|
|
|
int cpu_throttle_get_percentage(void)
|
|
{
|
|
return qatomic_read(&throttle_percentage);
|
|
}
|
|
|
|
void cpu_throttle_init(void)
|
|
{
|
|
throttle_timer = timer_new_ns(QEMU_CLOCK_VIRTUAL_RT,
|
|
cpu_throttle_timer_tick, NULL);
|
|
}
|