* [PATCH RFC v3 5/6] tick: get duration of the last idle loop
[not found] <1510568829-5645-1-git-send-email-quan.xu0@gmail.com>
@ 2017-11-13 10:27 ` Quan Xu
2017-11-13 10:27 ` [PATCH RFC v3 6/6] KVM guest: introduce smart idle poll algorithm Quan Xu
1 sibling, 0 replies; 2+ messages in thread
From: Quan Xu @ 2017-11-13 10:27 UTC (permalink / raw)
To: kvm, linux-doc, linux-fsdevel, linux-kernel, virtualization, x86,
xen-devel
Cc: Yang Zhang, Frederic Weisbecker, Thomas Gleixner, Quan Xu,
Ingo Molnar
From: Quan Xu <quan.xu0@gmail.com>
the last idle loop is from tick_nohz_idle_enter to tick_nohz_idle_exit.
Signed-off-by: Yang Zhang <yang.zhang.wz@gmail.com>
Signed-off-by: Quan Xu <quan.xu0@gmail.com>
Cc: Frederic Weisbecker <fweisbec@gmail.com>
Cc: Thomas Gleixner <tglx@linutronix.de>
Cc: Ingo Molnar <mingo@kernel.org>
Cc: linux-kernel@vger.kernel.org
---
include/linux/tick.h | 2 ++
kernel/time/tick-sched.c | 11 +++++++++++
kernel/time/tick-sched.h | 3 +++
3 files changed, 16 insertions(+), 0 deletions(-)
diff --git a/include/linux/tick.h b/include/linux/tick.h
index cf413b3..77ae46d 100644
--- a/include/linux/tick.h
+++ b/include/linux/tick.h
@@ -118,6 +118,7 @@ enum tick_dep_bits {
extern void tick_nohz_idle_exit(void);
extern void tick_nohz_irq_exit(void);
extern ktime_t tick_nohz_get_sleep_length(void);
+extern ktime_t tick_nohz_get_last_idle_length(void);
extern unsigned long tick_nohz_get_idle_calls(void);
extern u64 get_cpu_idle_time_us(int cpu, u64 *last_update_time);
extern u64 get_cpu_iowait_time_us(int cpu, u64 *last_update_time);
@@ -127,6 +128,7 @@ enum tick_dep_bits {
static inline void tick_nohz_idle_enter(void) { }
static inline void tick_nohz_idle_exit(void) { }
+static ktime_t tick_nohz_get_last_idle_length(void) { return -1; }
static inline ktime_t tick_nohz_get_sleep_length(void)
{
return NSEC_PER_SEC / HZ;
diff --git a/kernel/time/tick-sched.c b/kernel/time/tick-sched.c
index c7a899c..65c9cc0 100644
--- a/kernel/time/tick-sched.c
+++ b/kernel/time/tick-sched.c
@@ -548,6 +548,7 @@ static void tick_nohz_update_jiffies(ktime_t now)
else
ts->idle_sleeptime = ktime_add(ts->idle_sleeptime, delta);
ts->idle_entrytime = now;
+ ts->idle_length = delta;
}
if (last_update_time)
@@ -998,6 +999,16 @@ void tick_nohz_irq_exit(void)
}
/**
+ * tick_nohz_get_last_idle_length - return the length of the last idle loop
+ */
+ktime_t tick_nohz_get_last_idle_length(void)
+{
+ struct tick_sched *ts = this_cpu_ptr(&tick_cpu_sched);
+
+ return ts->idle_length;
+}
+
+/**
* tick_nohz_get_sleep_length - return the length of the current sleep
*
* Called from power state control code with interrupts disabled
diff --git a/kernel/time/tick-sched.h b/kernel/time/tick-sched.h
index 954b43d..2630cf9 100644
--- a/kernel/time/tick-sched.h
+++ b/kernel/time/tick-sched.h
@@ -39,6 +39,8 @@ enum tick_nohz_mode {
* @idle_sleeptime: Sum of the time slept in idle with sched tick stopped
* @iowait_sleeptime: Sum of the time slept in idle with sched tick stopped, with IO outstanding
* @sleep_length: Duration of the current idle sleep
+ * @idle_length: Duration of the last idle loop is from
+ * tick_nohz_idle_enter to tick_nohz_idle_exit.
* @do_timer_lst: CPU was the last one doing do_timer before going idle
*/
struct tick_sched {
@@ -59,6 +61,7 @@ struct tick_sched {
ktime_t idle_sleeptime;
ktime_t iowait_sleeptime;
ktime_t sleep_length;
+ ktime_t idle_length;
unsigned long last_jiffies;
u64 next_timer;
ktime_t idle_expires;
--
1.7.1
_______________________________________________
Xen-devel mailing list
Xen-devel@lists.xen.org
https://lists.xen.org/xen-devel
^ permalink raw reply related [flat|nested] 2+ messages in thread
* [PATCH RFC v3 6/6] KVM guest: introduce smart idle poll algorithm
[not found] <1510568829-5645-1-git-send-email-quan.xu0@gmail.com>
2017-11-13 10:27 ` [PATCH RFC v3 5/6] tick: get duration of the last idle loop Quan Xu
@ 2017-11-13 10:27 ` Quan Xu
1 sibling, 0 replies; 2+ messages in thread
From: Quan Xu @ 2017-11-13 10:27 UTC (permalink / raw)
To: kvm, linux-doc, linux-fsdevel, linux-kernel, virtualization, x86,
xen-devel
Cc: Yang Zhang, Quan Xu, Ingo Molnar, H. Peter Anvin, Paolo Bonzini,
Thomas Gleixner
From: Yang Zhang <yang.zhang.wz@gmail.com>
using smart idle poll to reduce the useless poll when system is idle.
Signed-off-by: Quan Xu <quan.xu0@gmail.com>
Signed-off-by: Yang Zhang <yang.zhang.wz@gmail.com>
Cc: Paolo Bonzini <pbonzini@redhat.com>
Cc: Thomas Gleixner <tglx@linutronix.de>
Cc: Ingo Molnar <mingo@redhat.com>
Cc: "H. Peter Anvin" <hpa@zytor.com>
Cc: x86@kernel.org
Cc: kvm@vger.kernel.org
Cc: linux-kernel@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
_______________________________________________
Xen-devel mailing list
Xen-devel@lists.xen.org
https://lists.xen.org/xen-devel
^ permalink raw reply related [flat|nested] 2+ messages in thread