[PATCH RFC v3 6/6] KVM guest: introduce smart idle poll algorithm

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



From: Yang Zhang <yang.zhang.wz@xxxxxxxxx>

using smart idle poll to reduce the useless poll when system is idle.

Signed-off-by: Quan Xu <quan.xu0@xxxxxxxxx>
Signed-off-by: Yang Zhang <yang.zhang.wz@xxxxxxxxx>
Cc: Paolo Bonzini <pbonzini@xxxxxxxxxx>
Cc: Thomas Gleixner <tglx@xxxxxxxxxxxxx>
Cc: Ingo Molnar <mingo@xxxxxxxxxx>
Cc: "H. Peter Anvin" <hpa@xxxxxxxxx>
Cc: x86@xxxxxxxxxx
Cc: kvm@xxxxxxxxxxxxxxx
Cc: linux-kernel@xxxxxxxxxxxxxxx
---
 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

_______________________________________________
Virtualization mailing list
Virtualization@xxxxxxxxxxxxxxxxxxxxxxxxxx
https://lists.linuxfoundation.org/mailman/listinfo/virtualization



[Index of Archives]     [KVM Development]     [Libvirt Development]     [Libvirt Users]     [CentOS Virtualization]     [Netdev]     [Ethernet Bridging]     [Linux Wireless]     [Kernel Newbies]     [Security]     [Linux for Hams]     [Netfilter]     [Bugtraq]     [Yosemite Forum]     [MIPS Linux]     [ARM Linux]     [Linux RAID]     [Linux Admin]     [Samba]

  Powered by Linux