Quan Xu
2017-Nov-13 10:27 UTC
[PATCH RFC v3 6/6] KVM guest: introduce smart idle poll algorithm
From: Yang Zhang <yang.zhang.wz at gmail.com> using smart idle poll to reduce the useless poll when system is idle. Signed-off-by: Quan Xu <quan.xu0 at gmail.com> Signed-off-by: Yang Zhang <yang.zhang.wz at gmail.com> Cc: Paolo Bonzini <pbonzini at redhat.com> Cc: Thomas Gleixner <tglx at linutronix.de> Cc: Ingo Molnar <mingo at redhat.com> Cc: "H. Peter Anvin" <hpa at zytor.com> Cc: x86 at kernel.org Cc: kvm at vger.kernel.org Cc: linux-kernel at vger.kernel.org --- arch/x86/kernel/kvm.c | 47 +++++++++++++++++++++++++++++++++++++++++++++++ 1 files changed, 47 insertions(+), 0 deletions(-) diff --git a/arch/x86/kernel/kvm.c b/arch/x86/kernel/kvm.c index 2a6e402..8bb6d55 100644 --- a/arch/x86/kernel/kvm.c +++ b/arch/x86/kernel/kvm.c @@ -37,6 +37,7 @@ #include <linux/debugfs.h> #include <linux/nmi.h> #include <linux/swait.h> +#include <linux/tick.h> #include <asm/timer.h> #include <asm/cpu.h> #include <asm/traps.h> @@ -365,11 +366,57 @@ static void kvm_guest_cpu_init(void) kvm_register_steal_time(); } +static unsigned int grow_poll_ns(unsigned int old, unsigned int grow, + unsigned int max) +{ + unsigned int val; + + /* set base poll time to 10000ns */ + if (old == 0 && grow) + return 10000; + + val = old * grow; + if (val > max) + val = max; + + return val; +} + +static unsigned int shrink_poll_ns(unsigned int old, unsigned int shrink) +{ + if (shrink == 0) + return 0; + + return old / shrink; +} + +static void kvm_idle_update_poll_duration(ktime_t idle) +{ + unsigned long poll_duration = this_cpu_read(poll_duration_ns); + + /* so far poll duration is based on nohz */ + if (idle == -1ULL) + return; + + if (poll_duration && idle > paravirt_poll_threshold_ns) + poll_duration = shrink_poll_ns(poll_duration, + paravirt_poll_shrink); + else if (poll_duration < paravirt_poll_threshold_ns && + idle < paravirt_poll_threshold_ns) + poll_duration = grow_poll_ns(poll_duration, paravirt_poll_grow, + paravirt_poll_threshold_ns); + + this_cpu_write(poll_duration_ns, poll_duration); +} + static void kvm_idle_poll(void) { unsigned long poll_duration = this_cpu_read(poll_duration_ns); + ktime_t idle = tick_nohz_get_last_idle_length(); ktime_t start, cur, stop; + kvm_idle_update_poll_duration(idle); + start = cur = ktime_get(); stop = ktime_add_ns(ktime_get(), poll_duration); -- 1.7.1
Possibly Parallel Threads
- [PATCH RFC v3 6/6] KVM guest: introduce smart idle poll algorithm
- [PATCH RFC v3 4/6] Documentation: Add three sysctls for smart idle poll
- [PATCH RFC v3 4/6] Documentation: Add three sysctls for smart idle poll
- [PATCH RFC v3 4/6] Documentation: Add three sysctls for smart idle poll
- [PATCH RFC v3 4/6] Documentation: Add three sysctls for smart idle poll