From: Yang Zhang <yang.zhang...@gmail.com>

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

Signed-off-by: Quan Xu <quan....@gmail.com>
Signed-off-by: Yang Zhang <yang.zhang...@gmail.com>
Cc: Paolo Bonzini <pbonz...@redhat.com>
Cc: Thomas Gleixner <t...@linutronix.de>
Cc: Ingo Molnar <mi...@redhat.com>
Cc: "H. Peter Anvin" <h...@zytor.com>
Cc: x...@kernel.org
Cc: k...@vger.kernel.org
Cc: linux-ker...@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

_______________________________________________
Virtualization mailing list
Virtualization@lists.linux-foundation.org
https://lists.linuxfoundation.org/mailman/listinfo/virtualization

Reply via email to