Introduct cpu idle prediction functionality
v2 cover Introduct cpu idle prediction functionality
We found under some latency intensive workloads, short idle periods occurs very common, then idle entry and exit path starts to dominate, so it's important to optimize them. To determine the short idle pattern, we need to figure out how long of the coming idle and the threshold of the short idle interval.
A cpu idle prediction functionality is introduced in this proposal to catch the short idle pattern.
Firstly, we check the IRQ timings subsystem, if there is an event coming soon.
-- https://lwn.net/Articles/691297/
Secondly, we check the idle statistics of scheduler, if it's likely we'll go into a short idle.
-- https://patchwork.kernel.org/patch/2839221/
Thirdly, we predict the next idle interval by using the prediction fucntionality in the idle governor if it has.
For the threshold of the short idle interval, we record the timestamps of the idle entry, and multiply by a tunable parameter at here:
-- /proc/sys/kernel/fast_idle_ratio
We use the output of the idle prediction to skip turning tick off if a short idle is determined in this proposal. Reprogramming hardware timer twice(off and on) is expensive for a very short idle. There are some potential optimizations can be done according to the same indicator.
I observed when system is idle, the idle predictor reports 20/s long idle and ZERO fast idle on one CPU. And when the workload is running, the idle predictor reports 72899/s fast idle and ZERO long idle on the same CPU.
Aubrey Li (8):
cpuidle: menu: extract prediction functionality
cpuidle: record the overhead of idle entry
cpuidle: add a new predict interface
tick/nohz: keep tick on for a fast idle
timers: keep sleep length updated as needed
cpuidle: make fast idle threshold tunable
cpuidle: introduce irq timing to make idle prediction
cpuidle: introduce run queue average idle to make idle prediction
drivers/cpuidle/Kconfig | 1 + drivers/cpuidle/cpuidle.c | 109 +++++++++++++++++++++++++++++++++++++++ drivers/cpuidle/governors/menu.c | 69 ++++++++++++++++--------- include/linux/cpuidle.h | 21 ++++++++ kernel/sched/idle.c | 14 ++++- kernel/sysctl.c | 12 +++++ kernel/time/tick-sched.c | 7 +++ 7 files changed, 209 insertions(+), 24 deletions(-)
Signature · 1 line
2.7.4
RFC PATCH v2 1/8 cpuidle: menu: extract prediction functionality
There are several factors in the menu governor to predict the next idle interval:
- the next timer
- the recent idle interval history
- the corrected idle interval pattern
These factors are common enough to be extracted to be one function.
--- drivers/cpuidle/governors/menu.c | 64 +++++++++++++++++++++++++--------------- 1 file changed, 40 insertions(+), 24 deletions(-)
drivers/cpuidle/governors/menu.c +40 -24
@@ -276,6 +276,45 @@ static unsigned int get_typical_interval(struct menu_device *data) }/** +* menu_predict - predict the coming idle interval +* +* Return the predicted coming idle interval in micro-second +*/ +static unsigned int menu_predict (void ) +{ +struct menu_device *data =this_cpu_ptr (&menu_devices); +unsigned int expected_interval; +int cpu =smp_processor_id (); + +if (!data) +return UINT_MAX ; + +/* determine the expected residency time, round up */ + data->next_timer_us =ktime_to_us (tick_nohz_get_sleep_length ()); + + data->bucket =which_bucket (data->next_timer_us,nr_iowait_cpu (cpu)); + +/* +* Force the result of multiplication to be 64 bits even if both +* operands are 32 bits. +* Make sure to round up for half microseconds. +*/ + data->predicted_us =DIV_ROUND_CLOSEST_ULL ((uint64_t ) + data->next_timer_us * data->correction_factor[data->bucket], +RESOLUTION *DECAY ); + + expected_interval =get_typical_interval (data); + expected_interval =min (expected_interval, data->next_timer_us); + +/* +* Use the lowest expected idle interval to pick the idle state. +*/ + data->predicted_us =min (data->predicted_us, expected_interval); + +return data->predicted_us; +} + +/** * menu_select - selects the next idle state to enter * @drv: cpuidle driver containing state data * @dev: the CPU
@@ -289,7 +328,6 @@ static int menu_select(struct cpuidle_driver *drv, struct cpuidle_device *dev)int first_idx;int idx;unsigned int interactivity_req; -unsigned int expected_interval;unsigned long nr_iowaiters, cpu_load;int resume_latency =dev_pm_qos_raw_read_value (device);
@@ -306,24 +344,6 @@ static int menu_select(struct cpuidle_driver *drv, struct cpuidle_device *dev)if (unlikely (latency_req ==0 ))return 0 ; -/* determine the expected residency time, round up */ - data->next_timer_us =ktime_to_us (tick_nohz_get_sleep_length ()); - -get_iowait_load (&nr_iowaiters, &cpu_load); - data->bucket =which_bucket (data->next_timer_us, nr_iowaiters); - -/* -* Force the result of multiplication to be 64 bits even if both -* operands are 32 bits. -* Make sure to round up for half microseconds. -*/ - data->predicted_us =DIV_ROUND_CLOSEST_ULL ((uint64_t )data->next_timer_us * - data->correction_factor[data->bucket], -RESOLUTION *DECAY ); - - expected_interval =get_typical_interval (data); - expected_interval =min (expected_interval, data->next_timer_us); -if (CPUIDLE_DRIVER_STATE_START >0 ) {struct cpuidle_state *s = &drv->states[CPUIDLE_DRIVER_STATE_START ];unsigned int polling_threshold;
@@ -345,14 +365,10 @@ static int menu_select(struct cpuidle_driver *drv, struct cpuidle_device *dev) }/* -* Use the lowest expected idle interval to pick the idle state. -*/ - data->predicted_us =min (data->predicted_us, expected_interval); - -/* * Use the performance multiplier and the user-configurable * latency_req to determine the maximum exit latency. */ +get_iowait_load (&nr_iowaiters, &cpu_load); interactivity_req = data->predicted_us /performance_multiplier (nr_iowaiters, cpu_load);if (latency_req > interactivity_req) latency_req = interactivity_req;
Signature · 1 line
2.7.4
RFC PATCH v2 2/8 cpuidle: record the overhead of idle entry
In reply to Aubrey LiRecord the overhead of idle entry in micro-second
--- drivers/cpuidle/cpuidle.c | 33 +++++++++++++++++++++++++++++++++ include/linux/cpuidle.h | 14 ++++++++++++++ kernel/sched/idle.c | 8 +++++++- 3 files changed, 54 insertions(+), 1 deletion(-)
drivers/cpuidle/cpuidle.c +33 -0
@@ -302,6 +302,39 @@ void cpuidle_reflect(struct cpuidle_device *dev, int index) cpuidle_curr_governor->reflect (dev, index); } +/* cpuidle_entry_start - record idle entry start */ +void cpuidle_entry_start (void ) +{ +struct cpuidle_device *dev =cpuidle_get_device (); + +if (dev) + dev->idle_stat.entry_start =local_clock (); +} + +/* +* cpuidle_entry_end - record idle entry end, and maintain +* the entry overhead average in micro-second +*/ +void cpuidle_entry_end (void ) +{ +struct cpuidle_device *dev =cpuidle_get_device (); +u64 overhead; +s64 diff; + +if (dev) { + dev->idle_stat.entry_end =local_clock (); + overhead =div_u64 (dev->idle_stat.entry_end - + dev->idle_stat.entry_start,NSEC_PER_USEC ); + diff = overhead - dev->idle_stat.overhead; + dev->idle_stat.overhead += diff >>3 ; +/* +* limit overhead to 1us +*/ +if (dev->idle_stat.overhead ==0 ) + dev->idle_stat.overhead =1 ; + } +} +/** * cpuidle_install_idle_handler - installs the cpuidle idle loop handler */
include/linux/cpuidle.h +14 -0
@@ -72,6 +72,15 @@ struct cpuidle_device_kobj;struct cpuidle_state_kobj ;struct cpuidle_driver_kobj ; +struct cpuidle_stat { +u64 entry_start;/* nanosecond */ +u64 entry_end;/* nanosecond */ +u64 overhead;/* nanosecond */ +unsigned int predicted_us;/* microsecond */ +bool predicted;/* ever predicted? */ +bool fast_idle;/* fast idle? */ +}; +struct cpuidle_device {unsigned int registered:1 ;unsigned int enabled:1 ;
@@ -89,6 +98,7 @@ struct cpuidle_device {cpumask_t coupled_cpus;struct cpuidle_coupled *coupled;#endif +struct cpuidle_stat idle_stat; }; DECLARE_PER_CPU(struct cpuidle_device *, cpuidle_devices);
@@ -131,6 +141,8 @@ extern bool cpuidle_not_available(struct cpuidle_driver *drv,extern int cpuidle_select (struct cpuidle_driver *drv,struct cpuidle_device *dev); +extern void cpuidle_entry_start (void ); +extern void cpuidle_entry_end (void );extern int cpuidle_enter (struct cpuidle_driver *drv,struct cpuidle_device *dev,int index);extern void cpuidle_reflect (struct cpuidle_device *dev,int index);
@@ -164,6 +176,8 @@ static inline bool cpuidle_not_available(struct cpuidle_driver *drv,static inline int cpuidle_select (struct cpuidle_driver *drv,struct cpuidle_device *dev) {return -ENODEV ; } +static inline void cpuidle_entry_start (void ) { } +static inline void cpuidle_entry_end (void ) { }static inline int cpuidle_enter (struct cpuidle_driver *drv,struct cpuidle_device *dev,int index) {return -ENODEV ; }
kernel/sched/idle.c +7 -1
@@ -210,6 +210,12 @@ static void cpuidle_idle_call(void)static void do_idle (void ) {/* +* we record idle entry overhead now, so any deferrable items +* in idle entry path need to be placed between cpuidle_entry_start() +* and cpuidle_entry_end() +*/ +cpuidle_entry_start (); +/* * If the arch has a polling bit, we maintain an invariant: * * Our polling bit is clear if we're not scheduled (i.e. if rq->curr !=
@@ -217,10 +223,10 @@ static void do_idle(void)* then setting need_resched is guaranteed to cause the CPU to * reschedule. */ -__current_set_polling ();quiet_vmstat ();tick_nohz_idle_enter (); +cpuidle_entry_end ();while (!need_resched ()) {check_pgt_cache ();
Signature · 1 line
2.7.4
RFC PATCH v2 3/8 cpuidle: add a new predict interface
In reply to Aubrey LiFor the governor has predict functionality, add a new predict interface in cpuidle framework to call and use it.
--- drivers/cpuidle/cpuidle.c | 34 ++++++++++++++++++++++++++++++++++ drivers/cpuidle/governors/menu.c | 7 +++++++ include/linux/cpuidle.h | 3 +++ kernel/sched/idle.c | 1 + 4 files changed, 45 insertions(+)
drivers/cpuidle/cpuidle.c +34 -0
@@ -336,6 +336,40 @@ void cpuidle_entry_end(void) }/** +* cpuidle_predict - predict whether the coming idle is a fast idle or not +*/ +void cpuidle_predict (void ) +{ +struct cpuidle_device *dev =cpuidle_get_device (); +unsigned int overhead_threshold; + +if (!dev) +return ; + + overhead_threshold = dev->idle_stat.overhead; + +if (cpuidle_curr_governor->predict) { + dev->idle_stat.predicted_us = cpuidle_curr_governor->predict (); +/* +* notify idle governor to avoid reduplicative +* prediction computation +*/ + dev->idle_stat.predicted = true; +if (dev->idle_stat.predicted_us < overhead_threshold) { +/* +* notify tick subsystem to keep ticking +* for the coming idle +*/ + dev->idle_stat.fast_idle = true; + }else + dev->idle_stat.fast_idle = false; + }else { + dev->idle_stat.predicted = false; + dev->idle_stat.fast_idle = false; + } +} + +/** * cpuidle_install_idle_handler - installs the cpuidle idle loop handler */ void cpuidle_install_idle_handler (void )
drivers/cpuidle/governors/menu.c +7 -0
@@ -344,6 +344,12 @@ static int menu_select(struct cpuidle_driver *drv, struct cpuidle_device *dev)if (unlikely (latency_req ==0 ))return 0 ; +/*don't predict again if idle framework already did it */ +if (!dev->idle_stat.predicted) +menu_predict (); +else + dev->idle_stat.predicted = false; +if (CPUIDLE_DRIVER_STATE_START >0 ) {struct cpuidle_state *s = &drv->states[CPUIDLE_DRIVER_STATE_START ];unsigned int polling_threshold;
@@ -518,6 +524,7 @@ static struct cpuidle_governor menu_governor = { .enable = menu_enable_device, .select = menu_select, .reflect = menu_reflect, + .predict = menu_predict, };/**
include/linux/cpuidle.h +3 -0
@@ -143,6 +143,7 @@ extern int cpuidle_select(struct cpuidle_driver *drv, struct cpuidle_device *dev);extern void cpuidle_entry_start (void );extern void cpuidle_entry_end (void ); +extern void cpuidle_predict (void );extern int cpuidle_enter (struct cpuidle_driver *drv,struct cpuidle_device *dev,int index);extern void cpuidle_reflect (struct cpuidle_device *dev,int index);
@@ -178,6 +179,7 @@ static inline int cpuidle_select(struct cpuidle_driver *drv, {return -ENODEV ; }static inline void cpuidle_entry_start (void ) { }static inline void cpuidle_entry_end (void ) { } +static inline void cpuidle_predict (void ) { }static inline int cpuidle_enter (struct cpuidle_driver *drv,struct cpuidle_device *dev,int index) {return -ENODEV ; }
@@ -255,6 +257,7 @@ struct cpuidle_governor {int (*select) (struct cpuidle_driver *drv,struct cpuidle_device *dev);void (*reflect) (struct cpuidle_device *dev,int index); +unsigned int (*predict)(void ); };#ifdef CONFIG_CPU_IDLE
kernel/sched/idle.c +1 -0
@@ -225,6 +225,7 @@ static void do_idle(void)*/ __current_set_polling ();quiet_vmstat (); +cpuidle_predict ();tick_nohz_idle_enter ();cpuidle_entry_end ();
Signature · 1 line
2.7.4
RFC PATCH v2 5/8 timers: keep sleep length updated as needed
In reply to Aubrey Lisleep length indicates how long we'll be idle. Currently, it's updated only when tick nohz enters. These patch series make a new requirement with tick, so we should keep sleep length updated as needed
--- kernel/time/tick-sched.c | 3 +++ 1 file changed, 3 insertions(+)
kernel/time/tick-sched.c +3 -0
@@ -1008,8 +1008,11 @@ void tick_nohz_irq_exit(void)*/ ktime_t tick_nohz_get_sleep_length (void ) { +struct clock_event_device *dev =__this_cpu_read (tick_cpu_device.evtdev);struct tick_sched *ts =this_cpu_ptr (&tick_cpu_sched); + ts->sleep_length =ktime_sub (dev->next_event,ktime_get ()); +return ts->sleep_length; }
Signature · 1 line
2.7.4
RFC PATCH v2 6/8 cpuidle: make fast idle threshold tunable
In reply to Aubrey LiAdd a knob to make fast idle threshold tunable
--- drivers/cpuidle/cpuidle.c | 3 ++- include/linux/cpuidle.h | 1 + kernel/sysctl.c | 12 ++++++++++++ 3 files changed, 15 insertions(+), 1 deletion(-)
drivers/cpuidle/cpuidle.c +2 -1
@@ -35,6 +35,7 @@ LIST_HEAD(cpuidle_detected_devices);static int enabled_devices;static int off__read_mostly ;static int initialized__read_mostly ; +int sysctl_fast_idle_ratio =10 ;int cpuidle_disabled (void ) {
@@ -346,7 +347,7 @@ void cpuidle_predict(void)if (!dev)return ; - overhead_threshold = dev->idle_stat.overhead; + overhead_threshold = dev->idle_stat.overhead * sysctl_fast_idle_ratio;if (cpuidle_curr_governor->predict) { dev->idle_stat.predicted_us = cpuidle_curr_governor->predict ();
include/linux/cpuidle.h +1 -0
@@ -24,6 +24,7 @@ struct module;struct cpuidle_device ;struct cpuidle_driver ; +extern int sysctl_fast_idle_ratio;/**************************** * CPUIDLE DEVICE INTERFACE *
kernel/sysctl.c +12 -0
@@ -67,6 +67,7 @@#include <linux/kexec.h> #include <linux/bpf.h> #include <linux/mount.h> +#include <linux/cpuidle.h> #include <linux/uaccess.h> #include <asm/processor.h>
@@ -1229,6 +1230,17 @@ static struct ctl_table kern_table[] = { .extra2 = &one, },#endif +#ifdef CONFIG_CPU_IDLE + { + .procname = "fast_idle_ratio", + .data = &sysctl_fast_idle_ratio, + .maxlen = sizeof(int), + .mode = 0644, + .proc_handler = proc_dointvec_minmax, + .extra1 = &one, + .extra2 = &one_hundred, + }, +#endif { } };
Signature · 1 line
2.7.4
RFC PATCH v2 7/8 cpuidle: introduce irq timing to make idle prediction
In reply to Aubrey LiIntroduce irq timings output as a factor to predict the duration of the coming idle
--- drivers/cpuidle/Kconfig | 1 + drivers/cpuidle/cpuidle.c | 17 ++++++++++++++++- 2 files changed, 17 insertions(+), 1 deletion(-)
drivers/cpuidle/Kconfig +1 -0
@@ -5,6 +5,7 @@ config CPU_IDLE default y if ACPI || PPC_PSERIES select CPU_IDLE_GOV_LADDER if (!NO_HZ && !NO_HZ_IDLE) select CPU_IDLE_GOV_MENU if (NO_HZ || NO_HZ_IDLE) + select IRQ_TIMINGS help CPU idle is a generic framework for supporting software-controlled idle processor power management. It includes modular cross-platform
drivers/cpuidle/cpuidle.c +16 -1
@@ -22,6 +22,7 @@#include <linux/module.h> #include <linux/suspend.h> #include <linux/tick.h> +#include <linux/interrupt.h> #include <trace/events/power.h> #include "cpuidle.h"
@@ -342,13 +343,27 @@ void cpuidle_entry_end(void)void cpuidle_predict (void ) {struct cpuidle_device *dev =cpuidle_get_device (); -unsigned int overhead_threshold; +unsigned int idle_interval, overhead_threshold; +u64 now, next_evt;if (!dev)return ; overhead_threshold = dev->idle_stat.overhead * sysctl_fast_idle_ratio; +/* +* check irq timings if the next event is coming soon +*/ + now =local_clock (); +local_irq_disable (); + next_evt =irq_timings_next_event (now); +local_irq_enable (); + idle_interval =div_u64 (next_evt - now,NSEC_PER_USEC ); +if (idle_interval < overhead_threshold) { + dev->idle_stat.fast_idle = true; +return ; + } +if (cpuidle_curr_governor->predict) { dev->idle_stat.predicted_us = cpuidle_curr_governor->predict ();/*
Signature · 1 line
2.7.4
RFC PATCH v2 8/8 cpuidle: introduce run queue average idle to make idle prediction
In reply to Aubrey LiIntroduce run queue average idle in scheduler as a factor to make idle prediction
--- drivers/cpuidle/cpuidle.c | 12 ++++++++++++ include/linux/cpuidle.h | 1 + kernel/sched/idle.c | 5 +++++ 3 files changed, 18 insertions(+)
drivers/cpuidle/cpuidle.c +12 -0
@@ -364,6 +364,18 @@ void cpuidle_predict(void)return ; } +/* +* check scheduler if the coming idle is likely a fast idle +*/ + idle_interval =div_u64 (sched_idle_avg (),NSEC_PER_USEC ); +if (idle_interval < overhead_threshold) { + dev->idle_stat.fast_idle = true; +return ; + } + +/* +* check the idle governor if the coming idle is likely a fast idle +*/ if (cpuidle_curr_governor->predict) { dev->idle_stat.predicted_us = cpuidle_curr_governor->predict ();/*
include/linux/cpuidle.h +1 -0
@@ -234,6 +234,7 @@ static inline void cpuidle_use_deepest_state(bool enable)/* kernel/sched/idle.c */ extern void sched_idle_set_state (struct cpuidle_state *idle_state);extern void default_idle_call (void ); +extern u64 sched_idle_avg (void );#ifdef CONFIG_ARCH_NEEDS_CPU_IDLE_COUPLED void cpuidle_coupled_parallel_barrier (struct cpuidle_device *dev,atomic_t *a);
kernel/sched/idle.c +5 -0
@@ -30,6 +30,11 @@ void sched_idle_set_state(struct cpuidle_state *idle_state)idle_set_state (this_rq (), idle_state); } +u64 sched_idle_avg (void ) +{ +return this_rq ()->avg_idle; +} +static int __read_mostly cpu_idle_force_poll;void cpu_idle_poll_ctrl (bool enable)
Signature · 1 line
2.7.4
RFC PATCH v2 4/8 tick/nohz: keep tick on for a fast idle
In reply to Aubrey LiIf the next idle is expected to be a fast idle, we should keep tick on before going into idle
--- drivers/cpuidle/cpuidle.c | 14 ++++++++++++++ include/linux/cpuidle.h | 2 ++ kernel/time/tick-sched.c | 4 ++++ 3 files changed, 20 insertions(+)
drivers/cpuidle/cpuidle.c +14 -0
@@ -370,6 +370,20 @@ void cpuidle_predict(void) }/** +* cpuidle_fast_idle - predict whether or not the coming idle is a fast idle +* This function can be called in irq exit path, make it as soon as possible +*/ +bool cpuidle_fast_idle (void ) +{ +struct cpuidle_device *dev =cpuidle_get_device (); + +if (!dev) +return false; + +return dev->idle_stat.fast_idle; +} + +/** * cpuidle_install_idle_handler - installs the cpuidle idle loop handler */ void cpuidle_install_idle_handler (void )
include/linux/cpuidle.h +2 -0
@@ -144,6 +144,7 @@ extern int cpuidle_select(struct cpuidle_driver *drv,extern void cpuidle_entry_start (void );extern void cpuidle_entry_end (void );extern void cpuidle_predict (void ); +extern bool cpuidle_fast_idle (void );extern int cpuidle_enter (struct cpuidle_driver *drv,struct cpuidle_device *dev,int index);extern void cpuidle_reflect (struct cpuidle_device *dev,int index);
@@ -180,6 +181,7 @@ static inline int cpuidle_select(struct cpuidle_driver *drv,static inline void cpuidle_entry_start (void ) { }static inline void cpuidle_entry_end (void ) { }static inline void cpuidle_predict (void ) { } +static inline void cpuidle_fast_idle (void ) {return false; }static inline int cpuidle_enter (struct cpuidle_driver *drv,struct cpuidle_device *dev,int index) {return -ENODEV ; }
kernel/time/tick-sched.c +4 -0
@@ -27,6 +27,7 @@#include <linux/irq_work.h> #include <linux/posix-timers.h> #include <linux/context_tracking.h> +#include <linux/cpuidle.h> #include <asm/irq_regs.h>
@@ -916,6 +917,9 @@ static bool can_stop_idle_tick(int cpu, struct tick_sched *ts)return false; } +if (cpuidle_fast_idle ()) +return false; +return true; }
Signature · 1 line
2.7.4
Re: [RFC PATCH v2 1/8] cpuidle: menu: extract prediction functionality
In reply to Aubrey LiQuoted 8 lines · CEST Aubrey Li
There are several factors in the menu governor to predict the next idle interval:
- the next timer
- the recent idle interval history
- the corrected idle interval pattern
These factors are common enough to be extracted to be one function.Signed-off-by Aubrey Li <[email protected]>
This patch alone would break things AFAICS, because it removes code from menu_select() without a replacement (and menu_predict() is never called just yet).
Please always do your best to ensure that things will work after *every* patch in a series.
Quoted 104 lines
--- drivers/cpuidle/governors/menu.c | 64 +++++++++++++++++++++++++--------------- 1 file changed, 40 insertions(+), 24 deletions(-)drivers/cpuidle/governors/menu.c +40
-24@@ -276,6 +276,45 @@ static unsigned int get_typical_interval(struct menu_device *data) }/** +* menu_predict - predict the coming idle interval +* +* Return the predicted coming idle interval in micro-second +*/ +static unsigned int menu_predict (void ) +{ +struct menu_device *data =this_cpu_ptr (&menu_devices); +unsigned int expected_interval; +int cpu =smp_processor_id (); + +if (!data) +return UINT_MAX ; + +/* determine the expected residency time, round up */ + data->next_timer_us =ktime_to_us (tick_nohz_get_sleep_length ()); + + data->bucket =which_bucket (data->next_timer_us,nr_iowait_cpu (cpu)); + +/* +* Force the result of multiplication to be 64 bits even if both +* operands are 32 bits. +* Make sure to round up for half microseconds. +*/ + data->predicted_us =DIV_ROUND_CLOSEST_ULL ((uint64_t ) + data->next_timer_us * data->correction_factor[data->bucket], +RESOLUTION *DECAY ); + + expected_interval =get_typical_interval (data); + expected_interval =min (expected_interval, data->next_timer_us); + +/* +* Use the lowest expected idle interval to pick the idle state. +*/ + data->predicted_us =min (data->predicted_us, expected_interval); + +return data->predicted_us; +} + +/** * menu_select - selects the next idle state to enter * @drv: cpuidle driver containing state data * @dev: the CPU @@ -289,7 +328,6 @@ static int menu_select(struct cpuidle_driver *drv, struct cpuidle_device *dev)int first_idx;int idx;unsigned int interactivity_req; -unsigned int expected_interval;unsigned long nr_iowaiters, cpu_load;int resume_latency =dev_pm_qos_raw_read_value (device);@@ -306,24 +344,6 @@ static int menu_select(struct cpuidle_driver *drv, struct cpuidle_device *dev)if (unlikely (latency_req ==0 ))return 0 ; -/* determine the expected residency time, round up */ - data->next_timer_us =ktime_to_us (tick_nohz_get_sleep_length ()); - -get_iowait_load (&nr_iowaiters, &cpu_load); - data->bucket =which_bucket (data->next_timer_us, nr_iowaiters); - -/* -* Force the result of multiplication to be 64 bits even if both -* operands are 32 bits. -* Make sure to round up for half microseconds. -*/ - data->predicted_us =DIV_ROUND_CLOSEST_ULL ((uint64_t )data->next_timer_us * - data->correction_factor[data->bucket], -RESOLUTION *DECAY ); - - expected_interval =get_typical_interval (data); - expected_interval =min (expected_interval, data->next_timer_us); -if (CPUIDLE_DRIVER_STATE_START >0 ) {struct cpuidle_state *s = &drv->states[CPUIDLE_DRIVER_STATE_START ];unsigned int polling_threshold;@@ -345,14 +365,10 @@ static int menu_select(struct cpuidle_driver *drv, struct cpuidle_device *dev) }/* -* Use the lowest expected idle interval to pick the idle state. -*/ - data->predicted_us =min (data->predicted_us, expected_interval); - -/* * Use the performance multiplier and the user-configurable * latency_req to determine the maximum exit latency. */ +get_iowait_load (&nr_iowaiters, &cpu_load); interactivity_req = data->predicted_us /performance_multiplier (nr_iowaiters, cpu_load);if (latency_req > interactivity_req) latency_req = interactivity_req;
Re: [RFC PATCH v2 2/8] cpuidle: record the overhead of idle entry
In reply to Aubrey LiQuoted 2 lines · CEST Aubrey Li
Record the overhead of idle entry in micro-second
What is this needed for?
Quoted 12 lines
Signed-off-by Aubrey Li <[email protected]>--- drivers/cpuidle/cpuidle.c | 33 +++++++++++++++++++++++++++++++++ include/linux/cpuidle.h | 14 ++++++++++++++ kernel/sched/idle.c | 8 +++++++- 3 files changed, 54 insertions(+), 1 deletion(-)drivers/cpuidle/cpuidle.c +0
-0
drivers/cpuidle/cpuidle.c void cpuidle_reflect(struct cpuidle_device *dev, int index) · line 305
Quoted 4 lines
drivers/cpuidle/cpuidle.c +1
-0@@ -302,6 +302,39 @@ void cpuidle_reflect(struct cpuidle_device *dev, int index) cpuidle_curr_governor->reflect (dev, index); } +/* cpuidle_entry_start - record idle entry start */
A proper kerneldoc, please.
drivers/cpuidle/cpuidle.c void cpuidle_reflect(struct cpuidle_device *dev, int index) · line 317
Quoted 12 lines
drivers/cpuidle/cpuidle.c +12
-0+void cpuidle_entry_start (void ) +{ +struct cpuidle_device *dev =cpuidle_get_device (); + +if (dev) + dev->idle_stat.entry_start =local_clock (); +} + +/* +* cpuidle_entry_end - record idle entry end, and maintain +* the entry overhead average in micro-second +*/
A proper kerneldoc, please.
drivers/cpuidle/cpuidle.c void cpuidle_reflect(struct cpuidle_device *dev, int index) · line 327
Quoted 10 lines
drivers/cpuidle/cpuidle.c +10
-0+void cpuidle_entry_end (void ) +{ +struct cpuidle_device *dev =cpuidle_get_device (); +u64 overhead; +s64 diff; + +if (dev) { + dev->idle_stat.entry_end =local_clock (); + overhead =div_u64 (dev->idle_stat.entry_end - + dev->idle_stat.entry_start,NSEC_PER_USEC );
Is the conversion really necessary?
If so, then why?
And if there is a good reason, what about using right shift to do an approximate conversion to avoid the extra division here?
drivers/cpuidle/cpuidle.c void cpuidle_reflect(struct cpuidle_device *dev, int index) · line 329
Quoted 2 lines
drivers/cpuidle/cpuidle.c +2
-0+ diff = overhead - dev->idle_stat.overhead; + dev->idle_stat.overhead += diff >>3 ;
Can you please explain what happens in the two lines above?
Quoted 16 lines
drivers/cpuidle/cpuidle.c +8
-0+/* +* limit overhead to 1us +*/ +if (dev->idle_stat.overhead ==0 ) + dev->idle_stat.overhead =1 ; + } +} +/** * cpuidle_install_idle_handler - installs the cpuidle idle loop handler */ include/linux/cpuidle.h +0
-0
include/linux/cpuidle.h struct cpuidle_device_kobj; · line 81
Quoted 10 lines
include/linux/cpuidle.h +7
-0@@ -72,6 +72,15 @@ struct cpuidle_device_kobj;struct cpuidle_state_kobj ;struct cpuidle_driver_kobj ; +struct cpuidle_stat { +u64 entry_start;/* nanosecond */ +u64 entry_end;/* nanosecond */ +u64 overhead;/* nanosecond */ +unsigned int predicted_us;/* microsecond */ +bool predicted;/* ever predicted? */ +bool fast_idle;/* fast idle? */
Some of the fields here are never used in the code after this patch.
Also it would be good to add a comment describing the meaning of the fields.
Quoted 61 lines
include/linux/cpuidle.h +7
-0+}; +struct cpuidle_device {unsigned int registered:1 ;unsigned int enabled:1 ;@@ -89,6 +98,7 @@ struct cpuidle_device {cpumask_t coupled_cpus;struct cpuidle_coupled *coupled;#endif +struct cpuidle_stat idle_stat; }; DECLARE_PER_CPU(struct cpuidle_device *, cpuidle_devices);@@ -131,6 +141,8 @@ extern bool cpuidle_not_available(struct cpuidle_driver *drv,extern int cpuidle_select (struct cpuidle_driver *drv,struct cpuidle_device *dev); +extern void cpuidle_entry_start (void ); +extern void cpuidle_entry_end (void );extern int cpuidle_enter (struct cpuidle_driver *drv,struct cpuidle_device *dev,int index);extern void cpuidle_reflect (struct cpuidle_device *dev,int index);@@ -164,6 +176,8 @@ static inline bool cpuidle_not_available(struct cpuidle_driver *drv,static inline int cpuidle_select (struct cpuidle_driver *drv,struct cpuidle_device *dev) {return -ENODEV ; } +static inline void cpuidle_entry_start (void ) { } +static inline void cpuidle_entry_end (void ) { }static inline int cpuidle_enter (struct cpuidle_driver *drv,struct cpuidle_device *dev,int index) {return -ENODEV ; }kernel/sched/idle.c +7
-1@@ -210,6 +210,12 @@ static void cpuidle_idle_call(void)static void do_idle (void ) {/* +* we record idle entry overhead now, so any deferrable items +* in idle entry path need to be placed between cpuidle_entry_start() +* and cpuidle_entry_end() +*/ +cpuidle_entry_start (); +/* * If the arch has a polling bit, we maintain an invariant: * * Our polling bit is clear if we're not scheduled (i.e. if rq->curr != @@ -217,10 +223,10 @@ static void do_idle(void)* then setting need_resched is guaranteed to cause the CPU to * reschedule. */ -__current_set_polling ();quiet_vmstat ();tick_nohz_idle_enter (); +cpuidle_entry_end ();while (!need_resched ()) {check_pgt_cache ();
Re: [RFC PATCH v2 3/8] cpuidle: add a new predict interface
In reply to Aubrey LiQuoted 2 lines · CEST Aubrey Li
For the governor has predict functionality, add a new predict interface in cpuidle framework to call and use it.
Care to describe how it is intended to work?
Also this patch uses data structures introduced in the previous one (and the previous one didn't use them), so maybe merge the two?
Quoted 23 lines
--- drivers/cpuidle/cpuidle.c | 34 ++++++++++++++++++++++++++++++++++ drivers/cpuidle/governors/menu.c | 7 +++++++ include/linux/cpuidle.h | 3 +++ kernel/sched/idle.c | 1 + 4 files changed, 45 insertions(+)drivers/cpuidle/cpuidle.c +8
-0@@ -336,6 +336,40 @@ void cpuidle_entry_end(void) }/** +* cpuidle_predict - predict whether the coming idle is a fast idle or not +*/ +void cpuidle_predict (void ) +{ +struct cpuidle_device *dev =cpuidle_get_device (); +unsigned int overhead_threshold; + +if (!dev)
drivers/cpuidle/cpuidle.c void cpuidle_entry_end(void) · line 356
Quoted 10 lines
drivers/cpuidle/cpuidle.c +10
-0+return ; + + overhead_threshold = dev->idle_stat.overhead; + +if (cpuidle_curr_governor->predict) { + dev->idle_stat.predicted_us = cpuidle_curr_governor->predict (); +/* +* notify idle governor to avoid reduplicative +* prediction computation +*/
This comment is hard to understand.
What does it mean, really?
drivers/cpuidle/cpuidle.c void cpuidle_entry_end(void) · line 365
Quoted 9 lines
drivers/cpuidle/cpuidle.c +9
-0+ dev->idle_stat.predicted = true; +if (dev->idle_stat.predicted_us < overhead_threshold) { +/* +* notify tick subsystem to keep ticking +* for the coming idle +*/ + dev->idle_stat.fast_idle = true; + }else + dev->idle_stat.fast_idle = false;
What about
dev->idle_stat.fast_idle = dev->idle_stat.predicted_us < overhead_threshold;
Also why don't you use dev->idle_stat.overhead directly here?
And what is the basic idea here? Why do we compare the predicted idle duration with the entry/exit overhead from the previous cycle (if I understood this correctly, that is)?
Quoted 15 lines
drivers/cpuidle/cpuidle.c +7
-0+ }else { + dev->idle_stat.predicted = false; + dev->idle_stat.fast_idle = false; + } +} + +/** * cpuidle_install_idle_handler - installs the cpuidle idle loop handler */ void cpuidle_install_idle_handler (void )drivers/cpuidle/governors/menu.c +0
-0
drivers/cpuidle/governors/menu.c static int menu_select(struct cpuidle_driver *drv, struct cpuidle_device *dev) · line 351
Quoted 8 lines
drivers/cpuidle/governors/menu.c +5
-0@@ -344,6 +344,12 @@ static int menu_select(struct cpuidle_driver *drv, struct cpuidle_device *dev)if (unlikely (latency_req ==0 ))return 0 ; +/*don't predict again if idle framework already did it */ +if (!dev->idle_stat.predicted) +menu_predict (); +else + dev->idle_stat.predicted = false;
We provide the callback which is going to be used by the core if present, so why would the core not use it after all?
Quoted 53 lines
drivers/cpuidle/governors/menu.c +2
-0+if (CPUIDLE_DRIVER_STATE_START >0 ) {struct cpuidle_state *s = &drv->states[CPUIDLE_DRIVER_STATE_START ];unsigned int polling_threshold;@@ -518,6 +524,7 @@ static struct cpuidle_governor menu_governor = { .enable = menu_enable_device, .select = menu_select, .reflect = menu_reflect, + .predict = menu_predict, };/** include/linux/cpuidle.h +3
-0@@ -143,6 +143,7 @@ extern int cpuidle_select(struct cpuidle_driver *drv, struct cpuidle_device *dev);extern void cpuidle_entry_start (void );extern void cpuidle_entry_end (void ); +extern void cpuidle_predict (void );extern int cpuidle_enter (struct cpuidle_driver *drv,struct cpuidle_device *dev,int index);extern void cpuidle_reflect (struct cpuidle_device *dev,int index);@@ -178,6 +179,7 @@ static inline int cpuidle_select(struct cpuidle_driver *drv, {return -ENODEV ; }static inline void cpuidle_entry_start (void ) { }static inline void cpuidle_entry_end (void ) { } +static inline void cpuidle_predict (void ) { }static inline int cpuidle_enter (struct cpuidle_driver *drv,struct cpuidle_device *dev,int index) {return -ENODEV ; }@@ -255,6 +257,7 @@ struct cpuidle_governor {int (*select) (struct cpuidle_driver *drv,struct cpuidle_device *dev);void (*reflect) (struct cpuidle_device *dev,int index); +unsigned int (*predict)(void ); };#ifdef CONFIG_CPU_IDLE kernel/sched/idle.c +1
-0@@ -225,6 +225,7 @@ static void do_idle(void)*/ __current_set_polling ();quiet_vmstat (); +cpuidle_predict ();tick_nohz_idle_enter ();cpuidle_entry_end ();
Re: [RFC PATCH v2 4/8] tick/nohz: keep tick on for a fast idle
In reply to Aubrey LiQuoted 4 lines · CEST Aubrey Li
If the next idle is expected to be a fast idle, we should keep tick on before going into idle
Signed-off-by Aubrey Li <[email protected]>
This also can be merged with the previous patch (and the [2/8]) IMO.
Quoted 15 lines
--- drivers/cpuidle/cpuidle.c | 14 ++++++++++++++ include/linux/cpuidle.h | 2 ++ kernel/time/tick-sched.c | 4 ++++ 3 files changed, 20 insertions(+)drivers/cpuidle/cpuidle.c +1
-0@@ -370,6 +370,20 @@ void cpuidle_predict(void) }/** +* cpuidle_fast_idle - predict whether or not the coming idle is a fast idle
drivers/cpuidle/cpuidle.c void cpuidle_predict(void) · line 383
Quoted 10 lines
drivers/cpuidle/cpuidle.c +10
-0+* This function can be called in irq exit path, make it as soon as possible +*/ +bool cpuidle_fast_idle (void ) +{ +struct cpuidle_device *dev =cpuidle_get_device (); + +if (!dev) +return false; + +return dev->idle_stat.fast_idle;
return dev && dev->idle_stat.fast_idle;
Quoted 39 lines
drivers/cpuidle/cpuidle.c +3
-0+} + +/** * cpuidle_install_idle_handler - installs the cpuidle idle loop handler */ void cpuidle_install_idle_handler (void )include/linux/cpuidle.h +2
-0@@ -144,6 +144,7 @@ extern int cpuidle_select(struct cpuidle_driver *drv,extern void cpuidle_entry_start (void );extern void cpuidle_entry_end (void );extern void cpuidle_predict (void ); +extern bool cpuidle_fast_idle (void );extern int cpuidle_enter (struct cpuidle_driver *drv,struct cpuidle_device *dev,int index);extern void cpuidle_reflect (struct cpuidle_device *dev,int index);@@ -180,6 +181,7 @@ static inline int cpuidle_select(struct cpuidle_driver *drv,static inline void cpuidle_entry_start (void ) { }static inline void cpuidle_entry_end (void ) { }static inline void cpuidle_predict (void ) { } +static inline void cpuidle_fast_idle (void ) {return false; }static inline int cpuidle_enter (struct cpuidle_driver *drv,struct cpuidle_device *dev,int index) {return -ENODEV ; }kernel/time/tick-sched.c +1
-0@@ -27,6 +27,7 @@#include <linux/irq_work.h> #include <linux/posix-timers.h> #include <linux/context_tracking.h> +#include <linux/cpuidle.h> #include <asm/irq_regs.h>
kernel/time/tick-sched.c static bool can_stop_idle_tick(int cpu, struct tick_sched *ts) · line 923
Quoted 7 lines
kernel/time/tick-sched.c +3
-0@@ -916,6 +917,9 @@ static bool can_stop_idle_tick(int cpu, struct tick_sched *ts)return false; } +if (cpuidle_fast_idle ()) +return false; +return true;
return !cpuidle_fast_idle();
kernel/time/tick-sched.c static bool can_stop_idle_tick(int cpu, struct tick_sched *ts) · line 925
Quoted 3 lines
kernel/time/tick-sched.c +0
-0}
And IMO there is quite a bit too much marketing in the "fast_idle" name, as it seems all about avoiding to stop the tick if the predicted idle duration is short enough.
Re: [RFC PATCH v2 5/8] timers: keep sleep length updated as needed
In reply to Aubrey LiQuoted 3 lines · CEST Aubrey Li
sleep length indicates how long we'll be idle. Currently, it's updated only when tick nohz enters. These patch series make a new requirement with tick, so we should keep sleep length updated as needed
So what exactly would be the problem with leaving things as they are?
Quoted 11 lines
--- kernel/time/tick-sched.c | 3 +++ 1 file changed, 3 insertions(+)kernel/time/tick-sched.c +0
-0@@ -1008,8 +1008,11 @@ void tick_nohz_irq_exit(void)*/
kernel/time/tick-sched.c void tick_nohz_irq_exit(void) · line 1018
Quoted 10 lines
kernel/time/tick-sched.c +3
-0ktime_t tick_nohz_get_sleep_length (void ) { +struct clock_event_device *dev =__this_cpu_read (tick_cpu_device.evtdev);struct tick_sched *ts =this_cpu_ptr (&tick_cpu_sched); + ts->sleep_length =ktime_sub (dev->next_event,ktime_get ()); +return ts->sleep_length; }
I probably wouldn't do it this way ...
Re: [RFC PATCH v2 6/8] cpuidle: make fast idle threshold tunable
In reply to Aubrey LiQuoted 3 lines · CEST Aubrey Li
Add a knob to make fast idle threshold tunable
Signed-off-by Aubrey Li <[email protected]>
I first of all am not sure about the need to add a tunable for this at all in the first place.
Quoted 59 lines
--- drivers/cpuidle/cpuidle.c | 3 ++- include/linux/cpuidle.h | 1 + kernel/sysctl.c | 12 ++++++++++++ 3 files changed, 15 insertions(+), 1 deletion(-)drivers/cpuidle/cpuidle.c +2
-1@@ -35,6 +35,7 @@ LIST_HEAD(cpuidle_detected_devices);static int enabled_devices;static int off__read_mostly ;static int initialized__read_mostly ; +int sysctl_fast_idle_ratio =10 ;int cpuidle_disabled (void ) {@@ -346,7 +347,7 @@ void cpuidle_predict(void)if (!dev)return ; - overhead_threshold = dev->idle_stat.overhead; + overhead_threshold = dev->idle_stat.overhead * sysctl_fast_idle_ratio;if (cpuidle_curr_governor->predict) { dev->idle_stat.predicted_us = cpuidle_curr_governor->predict ();include/linux/cpuidle.h +1
-0@@ -24,6 +24,7 @@ struct module;struct cpuidle_device ;struct cpuidle_driver ; +extern int sysctl_fast_idle_ratio;/**************************** * CPUIDLE DEVICE INTERFACE * kernel/sysctl.c +5
-0@@ -67,6 +67,7 @@#include <linux/kexec.h> #include <linux/bpf.h> #include <linux/mount.h> +#include <linux/cpuidle.h> #include <linux/uaccess.h> #include <asm/processor.h> @@ -1229,6 +1230,17 @@ static struct ctl_table kern_table[] = { .extra2 = &one, },#endif +#ifdef CONFIG_CPU_IDLE + { + .procname = "fast_idle_ratio", + .data = &sysctl_fast_idle_ratio,
kernel/sysctl.c static struct ctl_table kern_table[] = { · line 1246
Quoted 10 lines
kernel/sysctl.c +7
-0+ .maxlen = sizeof(int), + .mode = 0644, + .proc_handler = proc_dointvec_minmax, + .extra1 = &one, + .extra2 = &one_hundred, + }, +#endif { } };
And if there is a good enough reason to add it, shouldn't the tunable be there in the cpuidle framework?
Re: [RFC PATCH v2 7/8] cpuidle: introduce irq timing to make idle prediction
In reply to Aubrey LiQuoted 44 lines · CEST Aubrey Li
Introduce irq timings output as a factor to predict the duration of the coming idle
Signed-off-by Aubrey Li <[email protected]>--- drivers/cpuidle/Kconfig | 1 + drivers/cpuidle/cpuidle.c | 17 ++++++++++++++++- 2 files changed, 17 insertions(+), 1 deletion(-)drivers/cpuidle/Kconfig +1
-0@@ -5,6 +5,7 @@ config CPU_IDLE default y if ACPI || PPC_PSERIES select CPU_IDLE_GOV_LADDER if (!NO_HZ && !NO_HZ_IDLE) select CPU_IDLE_GOV_MENU if (NO_HZ || NO_HZ_IDLE) + select IRQ_TIMINGS help CPU idle is a generic framework for supporting software-controlled idle processor power management. It includes modular cross-platformdrivers/cpuidle/cpuidle.c +3
-1@@ -22,6 +22,7 @@#include <linux/module.h> #include <linux/suspend.h> #include <linux/tick.h> +#include <linux/interrupt.h> #include <trace/events/power.h> #include "cpuidle.h" @@ -342,13 +343,27 @@ void cpuidle_entry_end(void)void cpuidle_predict (void ) {struct cpuidle_device *dev =cpuidle_get_device (); -unsigned int overhead_threshold; +unsigned int idle_interval, overhead_threshold; +u64 now, next_evt;if (!dev)return ;
drivers/cpuidle/cpuidle.c void cpuidle_entry_end(void) · line 361
Quoted 10 lines
drivers/cpuidle/cpuidle.c +8
-0overhead_threshold = dev->idle_stat.overhead * sysctl_fast_idle_ratio; +/* +* check irq timings if the next event is coming soon +*/ + now =local_clock (); +local_irq_disable (); + next_evt =irq_timings_next_event (now); +local_irq_enable (); + idle_interval =div_u64 (next_evt - now,NSEC_PER_USEC );
Another division ...
Quoted 9 lines
drivers/cpuidle/cpuidle.c +5
-0+if (idle_interval < overhead_threshold) { + dev->idle_stat.fast_idle = true; +return ; + } +if (cpuidle_curr_governor->predict) { dev->idle_stat.predicted_us = cpuidle_curr_governor->predict ();/*
Re: [RFC PATCH v2 8/8] cpuidle: introduce run queue average idle to make idle prediction
In reply to Aubrey LiQuoted 15 lines · CEST Aubrey Li
Introduce run queue average idle in scheduler as a factor to make idle prediction
Signed-off-by Aubrey Li <[email protected]>--- drivers/cpuidle/cpuidle.c | 12 ++++++++++++ include/linux/cpuidle.h | 1 + kernel/sched/idle.c | 5 +++++ 3 files changed, 18 insertions(+)drivers/cpuidle/cpuidle.c +0
-0
drivers/cpuidle/cpuidle.c void cpuidle_predict(void) · line 370
Quoted 7 lines
drivers/cpuidle/cpuidle.c +4
-0@@ -364,6 +364,18 @@ void cpuidle_predict(void)return ; } +/* +* check scheduler if the coming idle is likely a fast idle +*/ + idle_interval =div_u64 (sched_idle_avg (),NSEC_PER_USEC );
And one more division ...
Quoted 30 lines
drivers/cpuidle/cpuidle.c +8
-0+if (idle_interval < overhead_threshold) { + dev->idle_stat.fast_idle = true; +return ; + } + +/* +* check the idle governor if the coming idle is likely a fast idle +*/ if (cpuidle_curr_governor->predict) { dev->idle_stat.predicted_us = cpuidle_curr_governor->predict ();/* include/linux/cpuidle.h +1
-0@@ -234,6 +234,7 @@ static inline void cpuidle_use_deepest_state(bool enable)/* kernel/sched/idle.c */ extern void sched_idle_set_state (struct cpuidle_state *idle_state);extern void default_idle_call (void ); +extern u64 sched_idle_avg (void );#ifdef CONFIG_ARCH_NEEDS_CPU_IDLE_COUPLED void cpuidle_coupled_parallel_barrier (struct cpuidle_device *dev,atomic_t *a);kernel/sched/idle.c +0
-0@@ -30,6 +30,11 @@ void sched_idle_set_state(struct cpuidle_state *idle_state)idle_set_state (this_rq (), idle_state);
kernel/sched/idle.c void sched_idle_set_state(struct cpuidle_state *idle_state) · line 40
Quoted 10 lines
kernel/sched/idle.c +5
-0} +u64 sched_idle_avg (void ) +{ +return this_rq ()->avg_idle; +} +static int __read_mostly cpu_idle_force_poll;void cpu_idle_poll_ctrl (bool enable)
You could easily combine this patch with the previous one IMO.
Quoted 43 lines · CEST Aubrey Li
We found under some latency intensive workloads, short idle periods occurs very common, then idle entry and exit path starts to dominate, so it's important to optimize them. To determine the short idle pattern, we need to figure out how long of the coming idle and the threshold of the short idle interval.
A cpu idle prediction functionality is introduced in this proposal to catch the short idle pattern.
Firstly, we check the IRQ timings subsystem, if there is an event coming soon.
-- https://lwn.net/Articles/691297/Secondly, we check the idle statistics of scheduler, if it's likely we'll go into a short idle.
-- https://patchwork.kernel.org/patch/2839221/Thirdly, we predict the next idle interval by using the prediction fucntionality in the idle governor if it has.
For the threshold of the short idle interval, we record the timestamps of the idle entry, and multiply by a tunable parameter at here:
--/proc/sys/kernel/fast_idle_ratioWe use the output of the idle prediction to skip turning tick off if a short idle is determined in this proposal. Reprogramming hardware timer twice(off and on) is expensive for a very short idle. There are some potential optimizations can be done according to the same indicator.
I observed when system is idle, the idle predictor reports 20/s long idle and ZERO fast idle on one CPU. And when the workload is running, the idle predictor reports 72899/s fast idle and ZERO long idle on the same CPU.
Aubrey Li (8):
cpuidle: menu: extract prediction functionality
cpuidle: record the overhead of idle entry
cpuidle: add a new predict interface
tick/nohz: keep tick on for a fast idle
timers: keep sleep length updated as needed
cpuidle: make fast idle threshold tunable
cpuidle: introduce irq timing to make idle prediction
cpuidle: introduce run queue average idle to make idle prediction
Quoted 9 lines
drivers/cpuidle/Kconfig | 1 + drivers/cpuidle/cpuidle.c | 109 +++++++++++++++++++++++++++++++++++++++ drivers/cpuidle/governors/menu.c | 69 ++++++++++++++++--------- include/linux/cpuidle.h | 21 ++++++++ kernel/sched/idle.c | 14 ++++- kernel/sysctl.c | 12 +++++ kernel/time/tick-sched.c | 7 +++ 7 files changed, 209 insertions(+), 24 deletions(-)
Overall, it looks like you could avoid stopping the tick every time the predicted idle duration is not longer than the tick interval in the first place.
Why don't you do that?
Re: [RFC PATCH v2 3/8] cpuidle: add a new predict interface
In reply to Aubrey LiQuoted 112 lines · CEST Aubrey Li
For the governor has predict functionality, add a new predict interface in cpuidle framework to call and use it.
--- drivers/cpuidle/cpuidle.c | 34 ++++++++++++++++++++++++++++++++++ drivers/cpuidle/governors/menu.c | 7 +++++++ include/linux/cpuidle.h | 3 +++ kernel/sched/idle.c | 1 + 4 files changed, 45 insertions(+)drivers/cpuidle/cpuidle.c +34
-0@@ -336,6 +336,40 @@ void cpuidle_entry_end(void) }/** +* cpuidle_predict - predict whether the coming idle is a fast idle or not +*/ +void cpuidle_predict (void ) +{ +struct cpuidle_device *dev =cpuidle_get_device (); +unsigned int overhead_threshold; + +if (!dev) +return ; + + overhead_threshold = dev->idle_stat.overhead; + +if (cpuidle_curr_governor->predict) { + dev->idle_stat.predicted_us = cpuidle_curr_governor->predict (); +/* +* notify idle governor to avoid reduplicative +* prediction computation +*/ + dev->idle_stat.predicted = true; +if (dev->idle_stat.predicted_us < overhead_threshold) { +/* +* notify tick subsystem to keep ticking +* for the coming idle +*/ + dev->idle_stat.fast_idle = true; + }else + dev->idle_stat.fast_idle = false; + }else { + dev->idle_stat.predicted = false; + dev->idle_stat.fast_idle = false; + } +} + +/** * cpuidle_install_idle_handler - installs the cpuidle idle loop handler */ void cpuidle_install_idle_handler (void )drivers/cpuidle/governors/menu.c +7
-0@@ -344,6 +344,12 @@ static int menu_select(struct cpuidle_driver *drv, struct cpuidle_device *dev)if (unlikely (latency_req ==0 ))return 0 ; +/*don't predict again if idle framework already did it */ +if (!dev->idle_stat.predicted) +menu_predict (); +else + dev->idle_stat.predicted = false; +if (CPUIDLE_DRIVER_STATE_START >0 ) {struct cpuidle_state *s = &drv->states[CPUIDLE_DRIVER_STATE_START ];unsigned int polling_threshold;@@ -518,6 +524,7 @@ static struct cpuidle_governor menu_governor = { .enable = menu_enable_device, .select = menu_select, .reflect = menu_reflect, + .predict = menu_predict, };/** include/linux/cpuidle.h +3
-0@@ -143,6 +143,7 @@ extern int cpuidle_select(struct cpuidle_driver *drv, struct cpuidle_device *dev);extern void cpuidle_entry_start (void );extern void cpuidle_entry_end (void ); +extern void cpuidle_predict (void );extern int cpuidle_enter (struct cpuidle_driver *drv,struct cpuidle_device *dev,int index);extern void cpuidle_reflect (struct cpuidle_device *dev,int index);@@ -178,6 +179,7 @@ static inline int cpuidle_select(struct cpuidle_driver *drv, {return -ENODEV ; }static inline void cpuidle_entry_start (void ) { }static inline void cpuidle_entry_end (void ) { } +static inline void cpuidle_predict (void ) { }static inline int cpuidle_enter (struct cpuidle_driver *drv,struct cpuidle_device *dev,int index) {return -ENODEV ; }@@ -255,6 +257,7 @@ struct cpuidle_governor {int (*select) (struct cpuidle_driver *drv,struct cpuidle_device *dev);void (*reflect) (struct cpuidle_device *dev,int index); +unsigned int (*predict)(void ); };#ifdef CONFIG_CPU_IDLE kernel/sched/idle.c +0
-0
kernel/sched/idle.c static void do_idle(void) · line 228
Quoted 4 lines
kernel/sched/idle.c +1
-0@@ -225,6 +225,7 @@ static void do_idle(void)*/ __current_set_polling ();quiet_vmstat (); +cpuidle_predict ();
One more question here.
This changes the cpuidle code ordering such that if the ->predict callback is present, the governor prediction will run before tick_nohz_idle_enter(), whereas without that callback it runs in cpuidle_idle_call().
Is that actually going to work correctly for the menu governor, in particular?
Quoted 4 lines
kernel/sched/idle.c +0
-0tick_nohz_idle_enter ();cpuidle_entry_end ();
Re: [RFC PATCH v2 1/8] cpuidle: menu: extract prediction functionality
In reply to Rafael J. WysockiQuoted 8 lines · Rafael J. Wysocki
Quoted 7 lines · CEST Aubrey Li
There are several factors in the menu governor to predict the next idle interval:
- the next timer
- the recent idle interval history
- the corrected idle interval pattern
These factors are common enough to be extracted to be one function.
Quoted 8 lines
Quoted 1 line
Signed-off-by Aubrey Li <[email protected]>This patch alone would break things AFAICS, because it removes code from
menu_select()without a replacement (andmenu_predict()is never called just yet).Please always do your best to ensure that things will work after *every* patch in a series.
okay, I'll correct this in the next version.
Thanks,
-Aubrey
Re: [RFC PATCH v2 2/8] cpuidle: record the overhead of idle entry
In reply to Rafael J. WysockiQuoted 5 lines · Rafael J. Wysocki
Quoted 2 lines · CEST Aubrey Li
Record the overhead of idle entry in micro-second
What is this needed for?
We need to figure out how long of a idle is a short idle and recording the overhead is for this purpose. The short idle threshold is based on this overhead.
Quoted 14 lines
drivers/cpuidle/cpuidle.c void cpuidle_reflect(struct cpuidle_device *dev, int index) · line 327
Quoted 11 lines
drivers/cpuidle/cpuidle.c +10
-0+void cpuidle_entry_end (void ) +{ +struct cpuidle_device *dev =cpuidle_get_device (); +u64 overhead; +s64 diff; + +if (dev) { + dev->idle_stat.entry_end =local_clock (); + overhead =div_u64 (dev->idle_stat.entry_end - + dev->idle_stat.entry_start,NSEC_PER_USEC );Is the conversion really necessary?
Quoted 1 line
If so, then why?
We can choose nano-second and micro-second. Given that workload results in the short idle pattern, I think micro-second is good enough for the real workload.
Another reason is that prediction from idle governor is micro-second, so I convert it for comparing purpose.
Quoted 3 lines
And if there is a good reason, what about using right shift to do an approximate conversion to avoid the extra division here?
Sure >> 10 works for me as I don't think here precision is a big deal.
Quoted 5 lines
drivers/cpuidle/cpuidle.c void cpuidle_reflect(struct cpuidle_device *dev, int index) · line 329
Quoted 3 lines
drivers/cpuidle/cpuidle.c +2
-0+ diff = overhead - dev->idle_stat.overhead; + dev->idle_stat.overhead += diff >>3 ;Can you please explain what happens in the two lines above?
Online average computing algorithm, stolen from update_avg() @ kernel/sched/core.c.
Quoted 30 lines
Quoted 17 lines
drivers/cpuidle/cpuidle.c +8
-0+/* +* limit overhead to 1us +*/ +if (dev->idle_stat.overhead ==0 ) + dev->idle_stat.overhead =1 ; + } +} +/** * cpuidle_install_idle_handler - installs the cpuidle idle loop handler */ include/linux/cpuidle.h +0
-0include/linux/cpuidle.h struct cpuidle_device_kobj; · line 81
Quoted 10 lines
include/linux/cpuidle.h +7
-0@@ -72,6 +72,15 @@ struct cpuidle_device_kobj;struct cpuidle_state_kobj ;struct cpuidle_driver_kobj ; +struct cpuidle_stat { +u64 entry_start;/* nanosecond */ +u64 entry_end;/* nanosecond */ +u64 overhead;/* nanosecond */ +unsigned int predicted_us;/* microsecond */ +bool predicted;/* ever predicted? */ +bool fast_idle;/* fast idle? */ Some of the fields here are never used in the code after this patch.
Quoted 3 lines
Also it would be good to add a comment describing the meaning of the fields.
okay, will add in the next version.
Thanks,
-Aubrey
Re: [RFC PATCH v2 4/8] tick/nohz: keep tick on for a fast idle
In reply to Rafael J. WysockiQuoted 8 lines · Rafael J. Wysocki
Quoted 4 lines · CEST Aubrey Li
If the next idle is expected to be a fast idle, we should keep tick on before going into idle
Signed-off-by Aubrey Li <[email protected]>This also can be merged with the previous patch (and the [2/8]) IMO.
okay, will merge in the next version.
Quoted 13 lines
Quoted 13 lines
--- drivers/cpuidle/cpuidle.c | 14 ++++++++++++++ include/linux/cpuidle.h | 2 ++ kernel/time/tick-sched.c | 4 ++++ 3 files changed, 20 insertions(+)drivers/cpuidle/cpuidle.c +0
-0@@ -370,6 +370,20 @@ void cpuidle_predict(void) }
Quoted 14 lines
drivers/cpuidle/cpuidle.c void cpuidle_predict(void) · line 383
Quoted 12 lines
drivers/cpuidle/cpuidle.c +11
-0/** +* cpuidle_fast_idle - predict whether or not the coming idle is a fast idle +* This function can be called in irq exit path, make it as soon as possible +*/ +bool cpuidle_fast_idle (void ) +{ +struct cpuidle_device *dev =cpuidle_get_device (); + +if (!dev) +return false; + +return dev->idle_stat.fast_idle;return dev &&
dev->idle_stat.fast_idle;
Thanks!
Quoted 11 lines
drivers/cpuidle/cpuidle.c static bool can_stop_idle_tick(int cpu, struct tick_sched *ts) · line 923
Quoted 9 lines
drivers/cpuidle/cpuidle.c +3
-0@@ -916,6 +917,9 @@ static bool can_stop_idle_tick(int cpu, struct tick_sched *ts)return false; } +if (cpuidle_fast_idle ()) +return false; +return true;return !cpuidle_fast_idle();
And thanks!
Quoted 9 lines
drivers/cpuidle/cpuidle.c static bool can_stop_idle_tick(int cpu, struct tick_sched *ts) · line 925
Quoted 4 lines
drivers/cpuidle/cpuidle.c +0
-0}And IMO there is quite a bit too much marketing in the "
fast_idle" name, as it seems all about avoiding to stop the tick if the predicted idle duration is short enough.
okay, and what's your suggestion? :)
I'll try to move quiet_vmstat() into the normal idle branch if this patch series are reasonable. Is fast_idle a good indication for it?
Thanks,
-Aubrey
Re: [RFC PATCH v2 4/8] tick/nohz: keep tick on for a fast idle
Quoted 3 lines · Aubrey
I'll try to move
quiet_vmstat()into the normal idle branch if this patch series are reasonable. Isfast_idlea good indication for it?
see x86_tip 62cb1188ed86 sched/idle: Move quiet_vmstate() into the NOHZ code
-Mike
Re: [RFC PATCH v2 4/8] tick/nohz: keep tick on for a fast idle
Quoted 6 lines · Mike Galbraith
Quoted 3 lines · Aubrey
I'll try to move
quiet_vmstat()into the normal idle branch if this patch series are reasonable. Isfast_idlea good indication for it?see
x86_tip62cb1188ed86sched/idle: Movequiet_vmstate()into the NOHZ code
It looks like this commit makes tick stop critical as it can be invoked in interrupt exit path?
->smp_apic_timer_interrupt
-->irq_exit
--->tick_nohz_irq_exit
---->__tick_nohz_idle_enter
----->tick_nohz_stop_sched_tick
------>quiet_vmstat
Thanks,
-Aubrey
Re: [RFC PATCH v2 6/8] cpuidle: make fast idle threshold tunable
In reply to Rafael J. WysockiQuoted 7 lines · Rafael J. Wysocki
Quoted 3 lines · CEST Aubrey Li
Add a knob to make fast idle threshold tunable
Signed-off-by Aubrey Li <[email protected]>I first of all am not sure about the need to add a tunable for this at all in the first place.
Actually I think a fixed value(10) might be good enough but not quite sure if there is a requirement to tune it for different scenario, for example even if the predicted idle interval is 100x overhead, I still want a fast path for a better benchmark score?
Quoted 13 lines
Quoted 13 lines
kernel/sysctl.c +9
-0@@ -1229,6 +1230,17 @@ static struct ctl_table kern_table[] = { .extra2 = &one, },#endif +#ifdef CONFIG_CPU_IDLE + { + .procname = "fast_idle_ratio", + .data = &sysctl_fast_idle_ratio, + .maxlen = sizeof(int), + .mode = 0644, + .proc_handler = proc_dointvec_minmax, + .extra1 = &one, + .extra2 = &one_hundred,
Quoted 9 lines
kernel/sysctl.c static struct ctl_table kern_table[] = { · line 1246
Quoted 5 lines
kernel/sysctl.c +2
-0+ }, +#endif { } };And if there is a good enough reason to add it, shouldn't the tunable be there in the cpuidle framework?
sure, if it makes sense, I'll move it into cpuidle/sysfs.c
Thanks,
-Aubrey
Re: [RFC PATCH v2 7/8] cpuidle: introduce irq timing to make idle prediction
In reply to Rafael J. WysockiQuoted 17 lines · Rafael J. Wysocki
Quoted 16 lines · CEST Aubrey Li
Introduce irq timings output as a factor to predict the duration of the coming idle
drivers/cpuidle/cpuidle.c +2
-1@@ -342,13 +343,27 @@ void cpuidle_entry_end(void)void cpuidle_predict (void ) {struct cpuidle_device *dev =cpuidle_get_device (); -unsigned int overhead_threshold; +unsigned int idle_interval, overhead_threshold; +u64 now, next_evt;if (!dev)return ; overhead_threshold = dev->idle_stat.overhead * sysctl_fast_idle_ratio;
Quoted 11 lines
drivers/cpuidle/cpuidle.c void cpuidle_entry_end(void) · line 361
Quoted 8 lines
drivers/cpuidle/cpuidle.c +8
-0+/* +* check irq timings if the next event is coming soon +*/ + now =local_clock (); +local_irq_disable (); + next_evt =irq_timings_next_event (now); +local_irq_enable (); + idle_interval =div_u64 (next_evt - now,NSEC_PER_USEC );Another division ...
okay, will replace with >>10 if micro-second is agreed to be the comparing unit
Thanks,
-Aubrey
Re: [RFC PATCH v2 4/8] tick/nohz: keep tick on for a fast idle
In reply to Li, AubreyQuoted 10 lines · Aubrey
Quoted 6 lines · Mike Galbraith
Quoted 3 lines · Aubrey
I'll try to move
quiet_vmstat()into the normal idle branch if this patch series are reasonable. Isfast_idlea good indication for it?see
x86_tip62cb1188ed86sched/idle: Movequiet_vmstate()into the NOHZ codeIt looks like this commit makes tick stop critical as it can be invoked in interrupt exit path?
do_idle() ain't critical? It is in my book. Hopefully, you're about to make that idle_stat.fast_idle thingy liberal enough that we cease mucking about with the tick on every microscopic idle (and I can then trash my years old local patch to avoid needlessly eating that cost).
-Mike
Re: [RFC PATCH v2 4/8] tick/nohz: keep tick on for a fast idle
Quoted 9 lines · Mike Galbraith
Quoted 8 lines · Aubrey
Quoted 6 lines · Mike Galbraith
Quoted 3 lines · Aubrey
I'll try to move
quiet_vmstat()into the normal idle branch if this patch series are reasonable. Isfast_idlea good indication for it?see
x86_tip62cb1188ed86sched/idle: Movequiet_vmstate()into the NOHZ code
Quoted 8 lines
Quoted 2 lines
It looks like this commit makes tick stop critical as it can be invoked in interrupt exit path?
do_idle()ain't critical? It is in my book. Hopefully, you're about to make that idle_stat.fast_idle thingy liberal enough that we cease mucking about with the tick on every microscopic idle (and I can then trash my years old local patch to avoid needlessly eating that cost).
Welcome any feedback of other patch set, Mike, :)
Thanks,
-Aubrey
Re: [RFC PATCH v2 5/8] timers: keep sleep length updated as needed
In reply to Rafael J. WysockiQuoted 6 lines · Rafael J. Wysocki
Quoted 3 lines · CEST Aubrey Li
sleep length indicates how long we'll be idle. Currently, it's updated only when tick nohz enters. These patch series make a new requirement with tick, so we should keep sleep length updated as needed
So what exactly would be the problem with leaving things as they are?
Previously ts->sleep_length is only updated when tick is stopped.
As follows, in
__tick_nohz_idle_enter()
{
if (can_stop_idle_tick() /* return true */) {
tick_nohz_stop_sched_tick()
|
|-----> update sleep_length
}
}Now ts->sleep_length is required out of tick_nohz_idle_enter(), so we want to update sleep_length every time we read it
If we leave it unchanged, the prediction could read a sleep_length long time ago if the system keep ticking.
Quoted 16 lines
Quoted 16 lines
--- kernel/time/tick-sched.c | 3 +++ 1 file changed, 3 insertions(+)kernel/time/tick-sched.c +1
-0@@ -1008,8 +1008,11 @@ void tick_nohz_irq_exit(void)*/ ktime_t tick_nohz_get_sleep_length (void ) { +struct clock_event_device *dev =__this_cpu_read (tick_cpu_device.evtdev);struct tick_sched *ts =this_cpu_ptr (&tick_cpu_sched);
Quoted 10 lines
kernel/time/tick-sched.c void tick_nohz_irq_exit(void) · line 1018
Quoted 6 lines
kernel/time/tick-sched.c +2
-0+ ts->sleep_length =ktime_sub (dev->next_event,ktime_get ()); +return ts->sleep_length; }I probably wouldn't do it this way ...
May I know the detailed thoughts?
Thanks,
-Aubrey
Quoted 54 lines · Rafael J. Wysocki
Quoted 43 lines · CEST Aubrey Li
We found under some latency intensive workloads, short idle periods occurs very common, then idle entry and exit path starts to dominate, so it's important to optimize them. To determine the short idle pattern, we need to figure out how long of the coming idle and the threshold of the short idle interval.
A cpu idle prediction functionality is introduced in this proposal to catch the short idle pattern.
Firstly, we check the IRQ timings subsystem, if there is an event coming soon.
-- https://lwn.net/Articles/691297/Secondly, we check the idle statistics of scheduler, if it's likely we'll go into a short idle.
-- https://patchwork.kernel.org/patch/2839221/Thirdly, we predict the next idle interval by using the prediction fucntionality in the idle governor if it has.
For the threshold of the short idle interval, we record the timestamps of the idle entry, and multiply by a tunable parameter at here:
--/proc/sys/kernel/fast_idle_ratioWe use the output of the idle prediction to skip turning tick off if a short idle is determined in this proposal. Reprogramming hardware timer twice(off and on) is expensive for a very short idle. There are some potential optimizations can be done according to the same indicator.
I observed when system is idle, the idle predictor reports 20/s long idle and ZERO fast idle on one CPU. And when the workload is running, the idle predictor reports 72899/s fast idle and ZERO long idle on the same CPU.
Aubrey Li (8):
cpuidle: menu: extract prediction functionality
cpuidle: record the overhead of idle entry
cpuidle: add a new predict interface
tick/nohz: keep tick on for a fast idle
timers: keep sleep length updated as needed
cpuidle: make fast idle threshold tunable
cpuidle: introduce irq timing to make idle prediction
cpuidle: introduce run queue average idle to make idle predictionQuoted 9 lines
drivers/cpuidle/Kconfig | 1 + drivers/cpuidle/cpuidle.c | 109 +++++++++++++++++++++++++++++++++++++++ drivers/cpuidle/governors/menu.c | 69 ++++++++++++++++--------- include/linux/cpuidle.h | 21 ++++++++ kernel/sched/idle.c | 14 ++++- kernel/sysctl.c | 12 +++++ kernel/time/tick-sched.c | 7 +++ 7 files changed, 209 insertions(+), 24 deletions(-)
Quoted 4 lines
Overall, it looks like you could avoid stopping the tick every time the predicted idle duration is not longer than the tick interval in the first place.
Quoted 1 line
Why don't you do that?
I didn't catch this.
Are you suggesting?
if(!cpu_stat.fast_idle)
tick_nohz_idle_enter()
Or you concern why the threshold can't simply be tick interval?
For the first, can_stop_idle_tick() is a better place to skip tick-off IMHO. For the latter, if the threshold is close/equal to the tick, it's quite possible the next event is the tick and no other else event.
Thanks,
-Aubrey
Re: [RFC PATCH v2 3/8] cpuidle: add a new predict interface
In reply to Rafael J. WysockiQuoted 8 lines · Rafael J. Wysocki
Quoted 2 lines · CEST Aubrey Li
For the governor has predict functionality, add a new predict interface in cpuidle framework to call and use it.
Care to describe how it is intended to work?
Also this patch uses data structures introduced in the previous one (and the previous one didn't use them), so maybe merge the two?
okay, will refine in the next version.
Quoted 37 lines
Quoted 24 lines
--- drivers/cpuidle/cpuidle.c | 34 ++++++++++++++++++++++++++++++++++ drivers/cpuidle/governors/menu.c | 7 +++++++ include/linux/cpuidle.h | 3 +++ kernel/sched/idle.c | 1 + 4 files changed, 45 insertions(+)drivers/cpuidle/cpuidle.c +8
-0@@ -336,6 +336,40 @@ void cpuidle_entry_end(void) }/** +* cpuidle_predict - predict whether the coming idle is a fast idle or not +*/ +void cpuidle_predict (void ) +{ +struct cpuidle_device *dev =cpuidle_get_device (); +unsigned int overhead_threshold; + +if (!dev)drivers/cpuidle/cpuidle.c void cpuidle_entry_end(void) · line 356
Quoted 10 lines
drivers/cpuidle/cpuidle.c +10
-0+return ; + + overhead_threshold = dev->idle_stat.overhead; + +if (cpuidle_curr_governor->predict) { + dev->idle_stat.predicted_us = cpuidle_curr_governor->predict (); +/* +* notify idle governor to avoid reduplicative +* prediction computation +*/ This comment is hard to understand.
Quoted 2 lines
What does it mean, really?
predict() does a lot of computation. If it's called in cpuidle_predict(), we don't want it to be called in menu governor again. So here I use a flag to tell menu governor if predict computation is already done for the coming idle this time.
Quoted 12 lines
drivers/cpuidle/cpuidle.c void cpuidle_entry_end(void) · line 365
Quoted 9 lines
drivers/cpuidle/cpuidle.c +9
-0+ dev->idle_stat.predicted = true; +if (dev->idle_stat.predicted_us < overhead_threshold) { +/* +* notify tick subsystem to keep ticking +* for the coming idle +*/ + dev->idle_stat.fast_idle = true; + }else + dev->idle_stat.fast_idle = false;What about
Quoted 1 line
dev->idle_stat.fast_idle = dev->idle_stat.predicted_us < overhead_threshold;
Ack
Quoted 2 lines
Also why don't you use
dev->idle_stat.overheaddirectly here?
Yeah, I should merge a few patches, because dev->idle_stat.overhead is referenced many times, for irq timings, for scheduler idle statistics and for idle governor.
Quoted 4 lines
And what is the basic idea here? Why do we compare the predicted idle duration with the entry/exit overhead from the previous cycle (if I understood this correctly, that is)?
entry/exit overhead is an average here, and the variance should not be big.
Quoted 19 lines
Quoted 19 lines
drivers/cpuidle/cpuidle.c +7
-0+ }else { + dev->idle_stat.predicted = false; + dev->idle_stat.fast_idle = false; + } +} + +/** * cpuidle_install_idle_handler - installs the cpuidle idle loop handler */ void cpuidle_install_idle_handler (void )drivers/cpuidle/governors/menu.c +0
-0@@ -344,6 +344,12 @@ static int menu_select(struct cpuidle_driver *drv, struct cpuidle_device *dev)if (unlikely (latency_req ==0 ))return 0 ;
Quoted 8 lines
drivers/cpuidle/governors/menu.c static int menu_select(struct cpuidle_driver *drv, struct cpuidle_device *dev) · line 351
Quoted 5 lines
drivers/cpuidle/governors/menu.c +5
-0+/*don't predict again if idle framework already did it */ +if (!dev->idle_stat.predicted) +menu_predict (); +else + dev->idle_stat.predicted = false;We provide the callback which is going to be used by the core if present, so why would the core not use it after all?
There is a case that in the loop
while (!need_resched()) {
cpuidle_idle_call()
}
The CPU receives a timer interrupt and wake up and find nothing to be scheduled and back to call menu then mwait to sleep again.
Under this case, cpuidle_predict() is not reached but the idle data for the prediction needs to be updated.
Thanks,
-Aubrey
Re: [RFC PATCH v2 3/8] cpuidle: add a new predict interface
In reply to Rafael J. WysockiQuoted 12 lines · Rafael J. Wysocki
kernel/sched/idle.c static void do_idle(void) · line 228
Quoted 9 lines
kernel/sched/idle.c +1
-0@@ -225,6 +225,7 @@ static void do_idle(void)*/ __current_set_polling ();quiet_vmstat (); +cpuidle_predict ();One more question here.
Quoted 6 lines
This changes the cpuidle code ordering such that if the ->predict callback is present, the governor prediction will run before
tick_nohz_idle_enter(), whereas without that callback it runs incpuidle_idle_call().Is that actually going to work correctly for the menu governor, in particular?
This depends on how prediction works, patch 1/8 has the details.
The first factor we get is the next clock event from tick device, this is not related to cpuidle call.
The other two factors are derived from idle interval history, the data is already maintained in menu_device data structure.
I'm not sure if this can address your concern, or anything else I can provide to help this.
Thanks,
-Aubrey
Re: [RFC PATCH v2 5/8] timers: keep sleep length updated as needed
In reply to Li, AubreyQuoted 12 lines · Aubrey
Quoted 6 lines · Rafael J. Wysocki
Quoted 3 lines · CEST Aubrey Li
sleep length indicates how long we'll be idle. Currently, it's updated only when tick nohz enters. These patch series make a new requirement with tick, so we should keep sleep length updated as needed
So what exactly would be the problem with leaving things as they are?
Previously
ts->sleep_lengthis only updated when tick is stopped.As follows, in
Quoted 8 lines
__tick_nohz_idle_enter(){ if (can_stop_idle_tick() /* return true */) { tick_nohz_stop_sched_tick() | |-----> update sleep_length } }
Which is logical, because the tick will get in the way if we don't stop it, won't it?
Quoted 6 lines
Now
ts->sleep_lengthis required out oftick_nohz_idle_enter(), so we want to updatesleep_lengthevery time we read itIf we leave it unchanged, the prediction could read a
sleep_lengthlong time ago if the system keep ticking.
Well, but does it make sense to estimate the sleep length without stopping the tick?
Quoted 26 lines
Quoted 15 lines
Quoted 15 lines
--- kernel/time/tick-sched.c | 3 +++ 1 file changed, 3 insertions(+)kernel/time/tick-sched.c +1
-0@@ -1008,8 +1008,11 @@ void tick_nohz_irq_exit(void)*/ ktime_t tick_nohz_get_sleep_length (void ) { +struct clock_event_device *dev =__this_cpu_read (tick_cpu_device.evtdev);struct tick_sched *ts =this_cpu_ptr (&tick_cpu_sched);Quoted 10 lines
kernel/time/tick-sched.c void tick_nohz_irq_exit(void) · line 1018
Quoted 6 lines
kernel/time/tick-sched.c +2
-0+ ts->sleep_length =ktime_sub (dev->next_event,ktime_get ()); +return ts->sleep_length; }I probably wouldn't do it this way ...
Quoted 1 line
May I know the detailed thoughts?
That depends on the answer above. :-)
Thanks,
Rafael
Re: [RFC PATCH v2 6/8] cpuidle: make fast idle threshold tunable
In reply to Li, AubreyQuoted 6 lines · Aubrey
Quoted 5 lines · Rafael J. Wysocki
Quoted 3 lines · CEST Aubrey Li
Add a knob to make fast idle threshold tunable
Signed-off-by Aubrey Li <[email protected]>
Quoted 7 lines
Quoted 2 lines
I first of all am not sure about the need to add a tunable for this at all in the first place.
Actually I think a fixed value(10) might be good enough but not quite sure if there is a requirement to tune it for different scenario, for example even if the predicted idle interval is 100x overhead, I still want a fast path for a better benchmark score?
Any new tunables make the test matrix expand considerably, so it generally is better to err on the conservative side with adding them.
Thanks,
Rafael
Re: [RFC PATCH v2 2/8] cpuidle: record the overhead of idle entry
In reply to Li, AubreyQuoted 10 lines · Aubrey
Quoted 5 lines · Rafael J. Wysocki
Quoted 2 lines · CEST Aubrey Li
Record the overhead of idle entry in micro-second
What is this needed for?
We need to figure out how long of a idle is a short idle and recording the overhead is for this purpose. The short idle threshold is based on this overhead.
I don't really understand this statement.
Pretent I'm not familiar with this stuff and try to explain it to me. :-)
Quoted 29 lines
Quoted 14 lines
drivers/cpuidle/cpuidle.c void cpuidle_reflect(struct cpuidle_device *dev, int index) · line 327
Quoted 11 lines
drivers/cpuidle/cpuidle.c +10
-0+void cpuidle_entry_end (void ) +{ +struct cpuidle_device *dev =cpuidle_get_device (); +u64 overhead; +s64 diff; + +if (dev) { + dev->idle_stat.entry_end =local_clock (); + overhead =div_u64 (dev->idle_stat.entry_end - + dev->idle_stat.entry_start,NSEC_PER_USEC );Is the conversion really necessary?
Quoted 1 line
If so, then why?
We can choose nano-second and micro-second. Given that workload results in the short idle pattern, I think micro-second is good enough for the real workload.
Another reason is that prediction from idle governor is micro-second, so I convert it for comparing purpose.
Quoted 3 lines
And if there is a good reason, what about using right shift to do an approximate conversion to avoid the extra division here?
Sure >> 10 works for me as I don't think here precision is a big deal.
Quoted 1 line
Quoted 6 lines
Quoted 4 lines
drivers/cpuidle/cpuidle.c void cpuidle_reflect(struct cpuidle_device *dev, int index) · line 329
Quoted 2 lines
drivers/cpuidle/cpuidle.c +2
-0+ diff = overhead - dev->idle_stat.overhead; + dev->idle_stat.overhead += diff >>3 ;Can you please explain what happens in the two lines above?
Online average computing algorithm, stolen from
update_avg()@kernel/sched/core.c.
OK
Maybe care to add a comment to that effect?
Thanks,
Rafael
Quoted 60 lines · Aubrey
Quoted 54 lines · Rafael J. Wysocki
Quoted 43 lines · CEST Aubrey Li
We found under some latency intensive workloads, short idle periods occurs very common, then idle entry and exit path starts to dominate, so it's important to optimize them. To determine the short idle pattern, we need to figure out how long of the coming idle and the threshold of the short idle interval.
A cpu idle prediction functionality is introduced in this proposal to catch the short idle pattern.
Firstly, we check the IRQ timings subsystem, if there is an event coming soon.
-- https://lwn.net/Articles/691297/Secondly, we check the idle statistics of scheduler, if it's likely we'll go into a short idle.
-- https://patchwork.kernel.org/patch/2839221/Thirdly, we predict the next idle interval by using the prediction fucntionality in the idle governor if it has.
For the threshold of the short idle interval, we record the timestamps of the idle entry, and multiply by a tunable parameter at here:
--/proc/sys/kernel/fast_idle_ratioWe use the output of the idle prediction to skip turning tick off if a short idle is determined in this proposal. Reprogramming hardware timer twice(off and on) is expensive for a very short idle. There are some potential optimizations can be done according to the same indicator.
I observed when system is idle, the idle predictor reports 20/s long idle and ZERO fast idle on one CPU. And when the workload is running, the idle predictor reports 72899/s fast idle and ZERO long idle on the same CPU.
Aubrey Li (8):
cpuidle: menu: extract prediction functionality
cpuidle: record the overhead of idle entry
cpuidle: add a new predict interface
tick/nohz: keep tick on for a fast idle
timers: keep sleep length updated as needed
cpuidle: make fast idle threshold tunable
cpuidle: introduce irq timing to make idle prediction
cpuidle: introduce run queue average idle to make idle predictionQuoted 9 lines
drivers/cpuidle/Kconfig | 1 + drivers/cpuidle/cpuidle.c | 109 +++++++++++++++++++++++++++++++++++++++ drivers/cpuidle/governors/menu.c | 69 ++++++++++++++++--------- include/linux/cpuidle.h | 21 ++++++++ kernel/sched/idle.c | 14 ++++- kernel/sysctl.c | 12 +++++ kernel/time/tick-sched.c | 7 +++ 7 files changed, 209 insertions(+), 24 deletions(-)Quoted 4 lines
Overall, it looks like you could avoid stopping the tick every time the predicted idle duration is not longer than the tick interval in the first place.
Quoted 1 line
Why don't you do that?
Quoted 8 lines
I didn't catch this.
Are you suggesting?
if(!cpu_stat.fast_idle)
tick_nohz_idle_enter()Or you concern why the threshold can't simply be tick interval?
That I guess.
Quoted 3 lines
For the first,
can_stop_idle_tick()is a better place to skip tick-off IMHO. For the latter, if the threshold is close/equal to the tick, it's quite possible the next event is the tick and no other else event.
Well, I don't quite get that.
What's the reasoning here?
Thanks,
Rafael
Re: [RFC PATCH v2 5/8] timers: keep sleep length updated as needed
In reply to Rafael J. WysockiQuoted 22 lines · Rafael J. Wysocki
Quoted 12 lines · Aubrey
Quoted 6 lines · Rafael J. Wysocki
Quoted 3 lines · CEST Aubrey Li
sleep length indicates how long we'll be idle. Currently, it's updated only when tick nohz enters. These patch series make a new requirement with tick, so we should keep sleep length updated as needed
So what exactly would be the problem with leaving things as they are?
Previously
ts->sleep_lengthis only updated when tick is stopped.As follows, in
Quoted 8 lines
__tick_nohz_idle_enter(){ if (can_stop_idle_tick() /* return true */) { tick_nohz_stop_sched_tick() | |-----> update sleep_length } }
Quoted 3 lines
Which is logical, because the tick will get in the way if we don't stop it, won't it?
The scenario here is, tick(4ms) is too long for a short idle(e.g. 4us). And there could be hundreds of short idle within one tick interval.
Quoted 9 lines
Quoted 6 lines
Now
ts->sleep_lengthis required out oftick_nohz_idle_enter(), so we want to updatesleep_lengthevery time we read itIf we leave it unchanged, the prediction could read a
sleep_lengthlong time ago if the system keep ticking.Well, but does it make sense to estimate the sleep length without stopping the tick?
For example, for the first short idle, we just turned tick off last time, so we may get the sleep length = 3900us. Then we keep tick on, and the 100th short idle comes, then the original sleep length is still 3900us(not updated), but actually it should be e.g. 100us.
Quoted 27 lines
Quoted 27 lines
Quoted 26 lines
Quoted 12 lines
--- kernel/time/tick-sched.c | 3 +++ 1 file changed, 3 insertions(+)kernel/time/tick-sched.c +0
-0@@ -1008,8 +1008,11 @@ void tick_nohz_irq_exit(void)*/ kernel/time/tick-sched.c void tick_nohz_irq_exit(void) · line 1018
Quoted 10 lines
kernel/time/tick-sched.c +3
-0ktime_t tick_nohz_get_sleep_length (void ) { +struct clock_event_device *dev =__this_cpu_read (tick_cpu_device.evtdev);struct tick_sched *ts =this_cpu_ptr (&tick_cpu_sched); + ts->sleep_length =ktime_sub (dev->next_event,ktime_get ()); +return ts->sleep_length; }I probably wouldn't do it this way ...
Quoted 3 lines
Quoted 1 line
May I know the detailed thoughts?
That depends on the answer above. :-)
Does the above explanation address the concern?
Thanks,
-Aubrey
Re: [RFC PATCH v2 6/8] cpuidle: make fast idle threshold tunable
In reply to Rafael J. WysockiQuoted 10 lines · Rafael J. Wysocki
Quoted 9 lines · Aubrey
Quoted 7 lines · Rafael J. Wysocki
Quoted 3 lines · CEST Aubrey Li
Add a knob to make fast idle threshold tunable
Signed-off-by Aubrey Li <[email protected]>I first of all am not sure about the need to add a tunable for this at all in the first place.
Quoted 8 lines
Quoted 4 lines
Actually I think a fixed value(10) might be good enough but not quite sure if there is a requirement to tune it for different scenario, for example even if the predicted idle interval is 100x overhead, I still want a fast path for a better benchmark score?
Any new tunables make the test matrix expand considerably, so it generally is better to err on the conservative side with adding them.
Okay, it's fine for me without it. I'll remove in the next version.
Thanks,
-Aubrey
Re: [RFC PATCH v2 2/8] cpuidle: record the overhead of idle entry
In reply to Rafael J. WysockiQuoted 8 lines · Rafael J. Wysocki
Quoted 7 lines · Aubrey
Quoted 5 lines · Rafael J. Wysocki
Quoted 2 lines · CEST Aubrey Li
Record the overhead of idle entry in micro-second
What is this needed for?
Quoted 8 lines
Quoted 3 lines
We need to figure out how long of a idle is a short idle and recording the overhead is for this purpose. The short idle threshold is based on this overhead.
I don't really understand this statement.
Pretent I'm not familiar with this stuff and try to explain it to me. :-)
Okay, let me try, :-)
Today what we did in idle loop as follows:
do_idle {
idle_entry {
- deferrable stuff like quiet_vmstat
- turn off tick(without looking at historical/predicted idle interval)
- rcu idle enter, c-state selection, etc
}
idle_call {
- poll or halt or mwait
}
idle_exit {
- rcu idle exit
- restore the tick if tick is stopped before enter idle
}
}And we already measured idle_entry and idle_exit costs several micro-seconds, say 10us.
Now if idle_call is 1000us, much larger than idle_entry and idle_exit, we can ignore the time cost in idle_entry and idle_exit.
But for some workloads with short idle pattern, like netperf, the idle_call is 2us, then idle_entry and idle_exit start to dominate. If we can reduce the time in idle_entry and idle_exit, we then get better workload performance significantly.
Modem high-speed network and low-latency I/O like Nvme disk has this requirement. Mike's patch was made several years ago though I don't know the details. Here is an article related to this.
https://cacm.acm.org/magazines/2017/4/215032-attack-of-the-killer-microseconds/fulltext
Quoted 34 lines
Quoted 34 lines
Quoted 14 lines
drivers/cpuidle/cpuidle.c void cpuidle_reflect(struct cpuidle_device *dev, int index) · line 327
Quoted 11 lines
drivers/cpuidle/cpuidle.c +10
-0+void cpuidle_entry_end (void ) +{ +struct cpuidle_device *dev =cpuidle_get_device (); +u64 overhead; +s64 diff; + +if (dev) { + dev->idle_stat.entry_end =local_clock (); + overhead =div_u64 (dev->idle_stat.entry_end - + dev->idle_stat.entry_start,NSEC_PER_USEC );Is the conversion really necessary?
Quoted 1 line
If so, then why?
We can choose nano-second and micro-second. Given that workload results in the short idle pattern, I think micro-second is good enough for the real workload.
Another reason is that prediction from idle governor is micro-second, so I convert it for comparing purpose.
Quoted 3 lines
And if there is a good reason, what about using right shift to do an approximate conversion to avoid the extra division here?
Sure >> 10 works for me as I don't think here precision is a big deal.
Quoted 5 lines
drivers/cpuidle/cpuidle.c void cpuidle_reflect(struct cpuidle_device *dev, int index) · line 329
Quoted 3 lines
drivers/cpuidle/cpuidle.c +2
-0+ diff = overhead - dev->idle_stat.overhead; + dev->idle_stat.overhead += diff >>3 ;Can you please explain what happens in the two lines above?
Quoted 5 lines
Quoted 1 line
Online average computing algorithm, stolen from
update_avg()@kernel/sched/core.c.OK
Maybe care to add a comment to that effect?
Sure, I'll add in the next version.
Thanks,
-Aubrey
Quoted 4 lines · Rafael J. Wysocki
Quoted 2 lines · Aubrey
Or you concern why the threshold can't simply be tick interval?
Quoted 8 lines
That I guess.
Quoted 2 lines
For the latter, if the threshold is close/equal to the tick, it's quite possible the next event is the tick and no other else event.
Well, I don't quite get that.
What's the reasoning here?
if we set the threshold to the tick interval, when the system keeps the tick on once, it will have no chance to turn if off forever.
Because we turn on the tick, and count in the time of idle entry and idle exit, the idle interval always < tick interval, that means idle is always a short idle.
Thanks,
-Aubrey
Hi,
Thanks Rafael's comments against V2. I'd like to ping here to see which direction this proposal should go and what fundamental change this proposal should make.
I'm also open to any suggestions if my proposal is not on the right way.
Thanks,
-Aubrey
Quoted 52 lines · Aubrey Li
We found under some latency intensive workloads, short idle periods occurs very common, then idle entry and exit path starts to dominate, so it's important to optimize them. To determine the short idle pattern, we need to figure out how long of the coming idle and the threshold of the short idle interval.
A cpu idle prediction functionality is introduced in this proposal to catch the short idle pattern.
Firstly, we check the IRQ timings subsystem, if there is an event coming soon.
-- https://lwn.net/Articles/691297/Secondly, we check the idle statistics of scheduler, if it's likely we'll go into a short idle.
-- https://patchwork.kernel.org/patch/2839221/Thirdly, we predict the next idle interval by using the prediction fucntionality in the idle governor if it has.
For the threshold of the short idle interval, we record the timestamps of the idle entry, and multiply by a tunable parameter at here:
--/proc/sys/kernel/fast_idle_ratioWe use the output of the idle prediction to skip turning tick off if a short idle is determined in this proposal. Reprogramming hardware timer twice(off and on) is expensive for a very short idle. There are some potential optimizations can be done according to the same indicator.
I observed when system is idle, the idle predictor reports 20/s long idle and ZERO fast idle on one CPU. And when the workload is running, the idle predictor reports 72899/s fast idle and ZERO long idle on the same CPU.
Aubrey Li (8):
cpuidle: menu: extract prediction functionality
cpuidle: record the overhead of idle entry
cpuidle: add a new predict interface
tick/nohz: keep tick on for a fast idle
timers: keep sleep length updated as needed
cpuidle: make fast idle threshold tunable
cpuidle: introduce irq timing to make idle prediction
cpuidle: introduce run queue average idle to make idle predictiondrivers/cpuidle/Kconfig | 1 + drivers/cpuidle/cpuidle.c | 109 +++++++++++++++++++++++++++++++++++++++ drivers/cpuidle/governors/menu.c | 69 ++++++++++++++++--------- include/linux/cpuidle.h | 21 ++++++++ kernel/sched/idle.c | 14 ++++- kernel/sysctl.c | 12 +++++ kernel/time/tick-sched.c | 7 +++ 7 files changed, 209 insertions(+), 24 deletions(-)
Rafael J. Wysocki
maintainerQuoted 7 lines · Aubrey
Hi,
Thanks Rafael's comments against V2. I'd like to ping here to see which direction this proposal should go and what fundamental change this proposal should make.
I'm also open to any suggestions if my proposal is not on the right way.
To me (and I discussed that with Peter briefly last month in Prague) the way to go would be to avoid stopping the scheduler tick until we are entirely sure that it should be stopped. Which is when the governor (any governor BTW, not just menu) chooses the target idle state for the CPU and the target residency of that state is long enough for the tick to be stopped ("long enough" here means that if the tick is not stopped, the time spent by the CPU in the target idle state will be shorter than the target residency). Note that this is totally independent on what governor is used and it doesn't involve any "refinements" or "improvements" of the prediction whatever.
Of course, for the governor to make the state selection, it generally will have to know when the next non-tick timer is going to expire and getting that information to it will require some effort.
Thanks,
Rafael