cpuidle: Predict the next events with the IO latencies
Subsystems (4)
cover cpuidle: Predict the next events with the IO latencies
This patchset is not intended to be merged upstream as it is. It is a proof of concept giving the rough idea of the concept.
In the discussions on how to make the scheduler energy aware, we tried to make the different PM subsystems to communicate with the scheduler.
We realized that some code is duplicated across the PM subsystems and the scheduler leading to an inconsistent way to integrate the PM informations.
Ingo Molnar put a line in the sand [1] and clearly worded that no more PM stuff will be integrated into the scheduler until the different PM blocks are not redesigned to be part of the scheduler.
This patchset is a sub part of this integration work. How to integrate Cpuidle with the scheduler ?
The idea is to get rid of the governors and let the scheduler to tell the Cpuidle framework : "I expect to sleep <x> nsec and I have a <y> nsec latency requirement" as stated by Peter Zijlstra [2].
How to achieve this ?
We want to prevent to just move code around and put the prediction of the next event inside the scheduler directly with a legacy menu governor. After investigating, it appears the menu governor is not behaving in a stable way, it is erratic. Using the IO latencies + the timers give much better results than the menu governor which takes into account all the source of wakeups [3].
After discussing at the LPC2014 Dusseldorf, it appears the idea is good but the approach is wrong. The latency tracking must be done at the device level, per device and not in the task as what is doing this patchset.
Any comment, suggestion or help is welcome !
If I missed anyone who may be interested in this feature, please let me know.
Thanks.
-- Daniel
[1] http://lwn.net/Articles/552885/
[2] https://lkml.org/lkml/2013/11/11/353
[3] http://events.linuxfoundation.org/sites/events/files/slides/IOlatencyPrediction.pdf
Daniel Lezcano (10):
sched: add io latency framework
cpuidle: Checking the zero latency inside the governors does not make
sense.
sched: idle: cpudidle: Pass the latency req from idle.c
sched: idle: Compute next timer event and pass it the cpuidle
framework
cpuidle: Remove unused headers for tick
sched: idle: Add io latency information for the next event
cpuidle: Add a simple select governor
cpuidle: select: hack - increase rating to have this governor as
default
cpuidle: sysfs: Add per cpu idle state prediction statistics
sched: io_latency: Tracking via buckets
drivers/cpuidle/Kconfig | 4 + drivers/cpuidle/cpuidle.c | 17 +- drivers/cpuidle/governors/Makefile | 1 + drivers/cpuidle/governors/ladder.c | 11 +- drivers/cpuidle/governors/menu.c | 15 +- drivers/cpuidle/governors/select.c | 55 +++++ drivers/cpuidle/sysfs.c | 156 +++++++++++++ include/linux/cpuidle.h | 22 +- include/linux/sched.h | 21 ++ init/Kconfig | 11 + kernel/exit.c | 1 + kernel/fork.c | 5 +- kernel/sched/Makefile | 1 + kernel/sched/core.c | 7 + kernel/sched/idle.c | 27 ++- kernel/sched/io_latency.c | 441 +++++++++++++++++++++++++++++++++++++ kernel/sched/io_latency.h | 38 ++++ 17 files changed, 800 insertions(+), 33 deletions(-) create mode 100644 drivers/cpuidle/governors/select.c create mode 100644 kernel/sched/io_latency.c create mode 100644 kernel/sched/io_latency.h
Signature · 1 line
1.9.1
PATCH 1/10 sched: add io latency framework
In order to have a good prediction of when will occur the next event, the cpuidle menu governor does some statistics about the occurences of an event waking up a cpu. For more details, refer to the menu.c's header file located in drivers/cpuidle/governors.
A part of the prediction is taking into account the number of pending IO on the cpu and depending on this number it will use a 'magic' number to force the selection of shallow states. It makes sense and provided a good improvement in terms of system latencies for server. Unfortunately there are some drawbacks of this approach. The first one is the empirical approach, based on measurements for a specific hardware and architecture giving the magic 'performance multiplier' which may not fit well for different architecture as well as new hardware which evolve during time. The second one is the mix of all the wakeup sources making impossible to track when a task is migrated across the cpus. And the last one, is the lack of correctly tracking what is happening on the system.
In order to improve that, we can classify three kind of events:
1. totally predictable events : this is the case for the timers
2. partially predictable events : for example: hard disk accesses with sleep time which are more or less inside a reasonable interval, the same for SSD or SD-card
3. difficult to predict events : network incoming packet, keyboard or mouse event. These ones need a statistical approach.
At this moment, 1., 2. and 3. are all mixed in the governors statistics.
This patchset provides a simplified version of an io latency tracking mechanism in order to separate and improve the category 2, that is the partially predictable events. As the scheduler is a good place to measure how long a task is blocked in an IO, all the code of this patchset is tied with it.
The sched entity and the io latency tracking share the same design:
There is a rb tree per cpu. Each time a task is blocked on an IO, it is inserted into the tree. When the IO is complete and the task is woken up, its avg latency is updated with the time spent to wait the IO and it is removed from the tree. The next time, it will be inserted into the tree again in case of io_schedule.
If there are several tasks blocked on an IO, the left most node of the tree is the minimal latency, in other words it gives for the IO the next event. This information may need to be balanced regarding the number of pendings IO (the more the disk is accessing, the slower it is).
By splitting the three kind of categories we can guess more accurately the next event on the system in conjunction with the next timer and some simplified statistics from the menu governor. Furthermore, that has the benefit to take into account a task migration as the information is embedded with it.
This is a simplified version because the tracking could be greatly improved by a polynomial regression like the sched entity tracking and the latencies should also be per device but that implies a bigger impact as the different callers of io_schedule have to provide the dev_t parameter. A too complex patch won't help the understanding.
--- include/linux/sched.h | 13 ++++ init/Kconfig | 11 +++ kernel/fork.c | 4 +- kernel/sched/Makefile | 1 + kernel/sched/core.c | 6 ++ kernel/sched/io_latency.c | 174 ++++++++++++++++++++++++++++++++++++++++++++++ kernel/sched/io_latency.h | 38 ++++++++++ 7 files changed, 246 insertions(+), 1 deletion(-) create mode 100644 kernel/sched/io_latency.c create mode 100644 kernel/sched/io_latency.h
include/linux/sched.h +13 -0
@@ -1221,6 +1221,16 @@ enum perf_event_task_context { perf_nr_task_contexts, }; + +#ifdef CONFIG_SCHED_IO_LATENCY +struct io_latency_node { +struct rb_node node; +unsigned int avg_latency; +ktime_t start_time; +ktime_t end_time; +}; +#endif +struct task_struct {volatile long state;/* -1 unrunnable, 0 runnable, >0 stopped */ void *stack;
@@ -1644,6 +1654,9 @@ struct task_struct {unsigned int sequential_io;unsigned int sequential_io_avg;#endif +#ifdef CONFIG_SCHED_IO_LATENCY +struct io_latency_node io_latency; +#endif };/* Future-safe accessor for struct task_struct's cpus_allowed. */
init/Kconfig +11 -0
@@ -1228,6 +1228,17 @@ config SCHED_AUTOGROUP desktop applications. Task group autogeneration is currently based upon task session. +config SCHED_IO_LATENCY + bool "IO latency tracking for the scheduler" + depends on SMP + help + This option tracks for each task the io latency average time it has + been blocked on for each cpu. It helps to have more information + regarding how long a cpu will be idle and to take better scheduling + decision. + + If unsure, say Y. + config SYSFS_DEPRECATED bool "Enable deprecated sysfs features to support old userspace tools" depends on SYSFS
kernel/fork.c +3 -1
@@ -345,7 +345,9 @@ static struct task_struct *dup_task_struct(struct task_struct *orig)#endif tsk->splice_pipe =NULL ; tsk->task_frag.page =NULL ; - +#ifdef CONFIG_SCHED_IO_LATENCY + tsk->io_latency.avg_latency =0 ; +#endif account_kernel_stack (ti,1 );return tsk;
kernel/sched/Makefile +1 -0
@@ -19,3 +19,4 @@ obj-$(CONFIG_SCHED_AUTOGROUP) += auto_group.o obj-$(CONFIG_SCHEDSTATS) += stats.o obj-$(CONFIG_SCHED_DEBUG) += debug.o obj-$(CONFIG_CGROUP_CPUACCT) += cpuacct.o +obj-$(CONFIG_SCHED_IO_LATENCY) += io_latency.o
kernel/sched/core.c +6 -0
@@ -84,6 +84,7 @@#endif #include "sched.h" +#include "io_latency.h" #include "../workqueue_internal.h" #include "../smpboot.h"
@@ -4338,7 +4339,9 @@ void __sched io_schedule(void)atomic_inc (&rq->nr_iowait);blk_flush_plug (current); current->in_iowait =1 ; +io_latency_begin (rq, current);schedule (); +io_latency_end (rq, current); current->in_iowait =0 ;atomic_dec (&rq->nr_iowait);delayacct_blkio_end ();
@@ -4354,7 +4357,9 @@ long __sched io_schedule_timeout(long timeout)atomic_inc (&rq->nr_iowait);blk_flush_plug (current); current->in_iowait =1 ; +io_latency_begin (rq, current); ret =schedule_timeout (timeout); +io_latency_end (rq, current); current->in_iowait =0 ;atomic_dec (&rq->nr_iowait);delayacct_blkio_end ();
@@ -7030,6 +7035,7 @@ void __init sched_init(void)#endif init_rq_hrtick (rq);atomic_set (&rq->nr_iowait,0 ); +io_latency_init (rq); }set_load_weight (&init_task);
kernel/sched/io_latency.c new file +174 -0
174 lines not on this page · raw message
kernel/sched/io_latency.h new file +38 -0
@@ -0,0 +1,38 @@ +/* +* Copyright (c) 2014 ARM/Linaro +* +* Author: Daniel Lezcano <[email protected]> +* +* This program is free software; you can redistribute it and/or modify +* it under the terms of the GNU General Public License version 2 as +* published by the Free Software Foundation. +* +* Maintainer: Daniel Lezcano <[email protected]> +*/ + +#ifdef CONFIG_SCHED_IO_LATENCY +extern void io_latency_init (struct rq *rq); +extern int io_latency_begin (struct rq *rq,struct task_struct *tsk); +extern void io_latency_end (struct rq *rq,struct task_struct *tsk); +extern int io_latency_get_sleep_length (struct rq *rq); +#else +static inline void io_latency_init (struct rq *rq) +{ + ; +} + +static inline int io_latency_begin (struct rq *rq,struct task_struct *tsk) +{ +return 0 ; +} + +static inline void io_latency_end (struct rq *rq,struct task_struct *tsk) +{ + ; +} + +static inline int io_latency_get_sleep_length (struct rq *rq) +{ +return 0 ; +} +#endif
Signature · 1 line
1.9.1
Re: [RFD PATCH 01/10] sched: add io latency framework
Quoted 5 lines
There is a rb tree per cpu. Each time a task is blocked on an IO, it is inserted into the tree. When the IO is complete and the task is woken up, its avg latency is updated with the time spent to wait the IO and it is removed from the tree. The next time, it will be inserted into the tree again in case of
io_schedule.
Is there an assumption built-in here that the device interrupt is targeted at the same CPU as where the task is queued?
thanks,
Len Brown, Intel Open Source Technology Center
Peter Zijlstra
maintainerRe: [RFD PATCH 01/10] sched: add io latency framework
Quoted 8 lines · Len Brown
Quoted 5 lines
There is a rb tree per cpu. Each time a task is blocked on an IO, it is inserted into the tree. When the IO is complete and the task is woken up, its avg latency is updated with the time spent to wait the IO and it is removed from the tree. The next time, it will be inserted into the tree again in case of
io_schedule.Is there an assumption built-in here that the device interrupt is targeted at the same CPU as where the task is queued?
Yes, I pointed out that this is unlikely to be true during his presentation in DUS.
My suggestion was to track interrupts per device and use the IRQ routing to map them to CPUs.
The benchmarking of the approach was done on a UP (or rather, everything affinity bound to cpu0) which did obviously not expose this particular issue.
PATCH 2/10 cpuidle: Checking the zero latency inside the governors does not make sense.
In reply to Daniel LezcanoIf the zero latency is required, we don't want to invoke any cpuidle code at all.
Move the check within the governors and do the check before selecting the state in order to fallback to the default idle function.
--- drivers/cpuidle/governors/ladder.c | 6 ------ drivers/cpuidle/governors/menu.c | 4 ---- kernel/sched/idle.c | 8 ++++++++ 3 files changed, 8 insertions(+), 10 deletions(-)
drivers/cpuidle/governors/ladder.c +0 -6
@@ -71,12 +71,6 @@ static int ladder_select_state(struct cpuidle_driver *drv,int last_residency, last_idx = ldev->last_state_idx;int latency_req =pm_qos_request (PM_QOS_CPU_DMA_LATENCY ); -/* Special case when user has set very strict latency requirement */ -if (unlikely (latency_req ==0 )) { -ladder_do_selection (ldev, last_idx,0 ); -return 0 ; - } - last_state = &ldev->states[last_idx];if (drv->states[last_idx].flags &CPUIDLE_FLAG_TIME_VALID ) {
drivers/cpuidle/governors/menu.c +0 -4
@@ -302,10 +302,6 @@ static int menu_select(struct cpuidle_driver *drv, struct cpuidle_device *dev) data->last_state_idx =CPUIDLE_DRIVER_STATE_START -1 ; -/* Special case when user has set very strict latency requirement */ -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 ());
kernel/sched/idle.c +8 -0
@@ -78,6 +78,7 @@ static void cpuidle_idle_call(void) {struct cpuidle_device *dev =__this_cpu_read (cpuidle_devices);struct cpuidle_driver *drv =cpuidle_get_cpu_driver (dev); +int latency_req =pm_qos_request (PM_QOS_CPU_DMA_LATENCY );int next_state, entered_state;unsigned int broadcast;
@@ -104,6 +105,13 @@ static void cpuidle_idle_call(void)rcu_idle_enter ();/* +* The latency requirement does not allow any latency, jump to +* the default idle function +*/ +if (latency_req ==0 ) + goto use_default; + +/* * Ask the cpuidle framework to choose a convenient idle state. * Fall back to the default arch idle method on errors. */
Signature · 1 line
1.9.1
Re: [RFD PATCH 02/10] cpuidle: Checking the zero latency inside the governors does not make sense.
after the patch '[V2,1/5] sched: idle: cpuidle: Check the latency req before idle' is applied,
this one becomes a no-op.
indeed, they'd conflict both logically and physically. physically the check for latency_req == 0 was already removed from the governors.
logically, the upper level, cpu_idle_loop() already invokes cpu_idle_poll() directly
when latency_req is 0. Indeed, that would prevent the code in this patch from running
at all, since cpu_idle_call() is no longer invoked in that case. Further, why would
you propose invoking "use_default" instead of cpu_idle_poll() in the latency_req == 0 case?
thanks,
Len Brown, Intel Open Source Technology Center
PATCH 3/10 sched: idle: cpudidle: Pass the latency req from idle.c
In reply to Daniel LezcanoAs we get the latency_req from cpuidle_idle_call, just pass it to the cpuidle layer instead of duplicating the code across the governors.
That has the benefit of moving little by little the different timings we want to integrate with the scheduler near this one.
--- drivers/cpuidle/cpuidle.c | 5 +++-- drivers/cpuidle/governors/ladder.c | 3 +-- drivers/cpuidle/governors/menu.c | 4 ++-- include/linux/cpuidle.h | 7 ++++--- kernel/sched/idle.c | 3 ++- 5 files changed, 12 insertions(+), 10 deletions(-)
drivers/cpuidle/cpuidle.c +3 -2
@@ -158,7 +158,8 @@ int cpuidle_enter_state(struct cpuidle_device *dev, struct cpuidle_driver *drv,* * Returns the index of the idle state. */ -int cpuidle_select (struct cpuidle_driver *drv,struct cpuidle_device *dev) +int cpuidle_select (struct cpuidle_driver *drv,struct cpuidle_device *dev, +int latency_req) {if (off || !initialized)return -ENODEV ;
@@ -169,7 +170,7 @@ int cpuidle_select(struct cpuidle_driver *drv, struct cpuidle_device *dev)if (unlikely (use_deepest_state))return cpuidle_find_deepest_state (drv, dev); -return cpuidle_curr_governor->select (drv, dev); +return cpuidle_curr_governor->select (drv, dev, latency_req); }/**
drivers/cpuidle/governors/ladder.c +1 -2
@@ -64,12 +64,11 @@ static inline void ladder_do_selection(struct ladder_device *ldev,* @dev: the CPU */ static int ladder_select_state (struct cpuidle_driver *drv, -struct cpuidle_device *dev) +struct cpuidle_device *dev,int latency_req) {struct ladder_device *ldev = &__get_cpu_var (ladder_devices);struct ladder_device_state *last_state;int last_residency, last_idx = ldev->last_state_idx; -int latency_req =pm_qos_request (PM_QOS_CPU_DMA_LATENCY ); last_state = &ldev->states[last_idx];
drivers/cpuidle/governors/menu.c +2 -2
@@ -287,10 +287,10 @@ again:* @drv: cpuidle driver containing state data * @dev: the CPU */ -static int menu_select (struct cpuidle_driver *drv,struct cpuidle_device *dev) +static int menu_select (struct cpuidle_driver *drv,struct cpuidle_device *dev, +int latency_req) {struct menu_device *data = &__get_cpu_var (menu_devices); -int latency_req =pm_qos_request (PM_QOS_CPU_DMA_LATENCY );int i;unsigned int interactivity_req;unsigned long nr_iowaiters, cpu_load;
include/linux/cpuidle.h +4 -3
@@ -122,7 +122,7 @@ struct cpuidle_driver {extern void disable_cpuidle (void );extern int cpuidle_select (struct cpuidle_driver *drv, -struct cpuidle_device *dev); +struct cpuidle_device *dev,int latency_req);extern int cpuidle_enter (struct cpuidle_driver *drv,struct cpuidle_device *dev,int index);extern void cpuidle_reflect (struct cpuidle_device *dev,int index);
@@ -150,7 +150,7 @@ extern struct cpuidle_driver *cpuidle_get_cpu_driver(struct cpuidle_device *dev)#else static inline void disable_cpuidle (void ) { }static inline int cpuidle_select (struct cpuidle_driver *drv, -struct cpuidle_device *dev) +struct cpuidle_device *dev,int latency_req) {return -ENODEV ; }static inline int cpuidle_enter (struct cpuidle_driver *drv,struct cpuidle_device *dev,int index)
@@ -205,7 +205,8 @@ struct cpuidle_governor { struct cpuidle_device *dev);int (*select) (struct cpuidle_driver *drv, -struct cpuidle_device *dev); +struct cpuidle_device *dev, +int latency_req);void (*reflect) (struct cpuidle_device *dev,int index);struct module *owner;
kernel/sched/idle.c +2 -1
@@ -5,6 +5,7 @@#include <linux/cpu.h> #include <linux/cpuidle.h> #include <linux/tick.h> +#include <linux/pm_qos.h> #include <linux/mm.h> #include <linux/stackprotector.h>
@@ -115,7 +116,7 @@ static void cpuidle_idle_call(void)* Ask the cpuidle framework to choose a convenient idle state. * Fall back to the default arch idle method on errors. */ - next_state =cpuidle_select (drv, dev); + next_state =cpuidle_select (drv, dev, latency_req);if (next_state <0 ) { use_default:/*
Signature · 1 line
1.9.1
PATCH 4/10 sched: idle: Compute next timer event and pass it the cpuidle framework
In reply to Daniel LezcanoFollowing the logic of the previous patch, retrieve from the idle task the expected timer sleep duration and pass it to the cpuidle framework.
--- drivers/cpuidle/cpuidle.c | 4 ++-- drivers/cpuidle/governors/ladder.c | 3 ++- drivers/cpuidle/governors/menu.c | 8 ++------ include/linux/cpuidle.h | 8 +++++--- kernel/sched/idle.c | 11 +++++++++-- 5 files changed, 20 insertions(+), 14 deletions(-)
drivers/cpuidle/cpuidle.c +2 -2
@@ -159,7 +159,7 @@ int cpuidle_enter_state(struct cpuidle_device *dev, struct cpuidle_driver *drv,* Returns the index of the idle state. */ int cpuidle_select (struct cpuidle_driver *drv,struct cpuidle_device *dev, -int latency_req) +int latency_req,int next_event) {if (off || !initialized)return -ENODEV ;
@@ -170,7 +170,7 @@ int cpuidle_select(struct cpuidle_driver *drv, struct cpuidle_device *dev,if (unlikely (use_deepest_state))return cpuidle_find_deepest_state (drv, dev); -return cpuidle_curr_governor->select (drv, dev, latency_req); +return cpuidle_curr_governor->select (drv, dev, latency_req, next_event); }/**
drivers/cpuidle/governors/ladder.c +2 -1
@@ -64,7 +64,8 @@ static inline void ladder_do_selection(struct ladder_device *ldev,* @dev: the CPU */ static int ladder_select_state (struct cpuidle_driver *drv, -struct cpuidle_device *dev,int latency_req) +struct cpuidle_device *dev, +int latency_req,int next_event) {struct ladder_device *ldev = &__get_cpu_var (ladder_devices);struct ladder_device_state *last_state;
drivers/cpuidle/governors/menu.c +2 -6
@@ -13,10 +13,6 @@#include <linux/kernel.h> #include <linux/cpuidle.h> #include <linux/pm_qos.h> -#include <linux/time.h> -#include <linux/ktime.h> -#include <linux/hrtimer.h> -#include <linux/tick.h> #include <linux/sched.h> #include <linux/math64.h> #include <linux/module.h>
@@ -288,7 +284,7 @@ again:* @dev: the CPU */ static int menu_select (struct cpuidle_driver *drv,struct cpuidle_device *dev, -int latency_req) +int latency_req,int next_event) {struct menu_device *data = &__get_cpu_var (menu_devices);int i;
@@ -303,7 +299,7 @@ static int menu_select(struct cpuidle_driver *drv, struct cpuidle_device *dev, data->last_state_idx =CPUIDLE_DRIVER_STATE_START -1 ;/* determine the expected residency time, round up */ - data->next_timer_us =ktime_to_us (tick_nohz_get_sleep_length ()); + data->next_timer_us = next_event;get_iowait_load (&nr_iowaiters, &cpu_load); data->bucket =which_bucket (data->next_timer_us, nr_iowaiters);
include/linux/cpuidle.h +5 -3
@@ -122,7 +122,8 @@ struct cpuidle_driver {extern void disable_cpuidle (void );extern int cpuidle_select (struct cpuidle_driver *drv, -struct cpuidle_device *dev,int latency_req); +struct cpuidle_device *dev, +int latency_req,int next_event);extern int cpuidle_enter (struct cpuidle_driver *drv,struct cpuidle_device *dev,int index);extern void cpuidle_reflect (struct cpuidle_device *dev,int index);
@@ -150,7 +151,8 @@ extern struct cpuidle_driver *cpuidle_get_cpu_driver(struct cpuidle_device *dev)#else static inline void disable_cpuidle (void ) { }static inline int cpuidle_select (struct cpuidle_driver *drv, -struct cpuidle_device *dev,int latency_req) +struct cpuidle_device *dev, +int latency_req,int next_event) {return -ENODEV ; }static inline int cpuidle_enter (struct cpuidle_driver *drv,struct cpuidle_device *dev,int index)
@@ -206,7 +208,7 @@ struct cpuidle_governor {int (*select) (struct cpuidle_driver *drv,struct cpuidle_device *dev, -int latency_req); +int latency_req,int next_event);void (*reflect) (struct cpuidle_device *dev,int index);struct module *owner;
kernel/sched/idle.c +9 -2
@@ -79,8 +79,8 @@ static void cpuidle_idle_call(void) {struct cpuidle_device *dev =__this_cpu_read (cpuidle_devices);struct cpuidle_driver *drv =cpuidle_get_cpu_driver (dev); +struct timespec t;int latency_req =pm_qos_request (PM_QOS_CPU_DMA_LATENCY ); -int next_state, entered_state;unsigned int broadcast;/*
@@ -112,11 +112,18 @@ static void cpuidle_idle_call(void)if (latency_req ==0 ) goto use_default; + t =ktime_to_timespec (tick_nohz_get_sleep_length ()); + +/* +* The next timer event for this in us +*/ + next_event = t.tv_sec *USEC_PER_SEC + t.tv_nsec /NSEC_PER_USEC ; +/* * Ask the cpuidle framework to choose a convenient idle state. * Fall back to the default arch idle method on errors. */ - next_state =cpuidle_select (drv, dev, latency_req); + next_state =cpuidle_select (drv, dev, latency_req, next_event);if (next_state <0 ) { use_default:/*
Signature · 1 line
1.9.1
PATCH 5/10 cpuidle: Remove unused headers for tick
In reply to Daniel LezcanoMoving around the different functions dealing with the time made the time headers no longer necessary in cpuidle.c.
Remove them.
--- drivers/cpuidle/cpuidle.c | 4 ---- 1 file changed, 4 deletions(-)
drivers/cpuidle/cpuidle.c +0 -4
@@ -8,16 +8,12 @@* This code is licenced under the GPL. */ -#include <linux/clockchips.h> #include <linux/kernel.h> #include <linux/mutex.h> -#include <linux/sched.h> #include <linux/notifier.h> #include <linux/pm_qos.h> #include <linux/cpu.h> #include <linux/cpuidle.h> -#include <linux/ktime.h> -#include <linux/hrtimer.h> #include <linux/module.h> #include <trace/events/power.h>
Signature · 1 line
1.9.1
PATCH 6/10 sched: idle: Add io latency information for the next event
In reply to Daniel LezcanoAs we want to improve the sleep duration estimation, the IO latency expected duration is passed to the cpuidle framework. The governors will have to deal with if they are interested in this information.
--- drivers/cpuidle/cpuidle.c | 4 ++-- drivers/cpuidle/governors/ladder.c | 3 ++- drivers/cpuidle/governors/menu.c | 5 +++-- include/linux/cpuidle.h | 12 +++++++++--- kernel/sched/idle.c | 27 ++++++++++++++++----------- 5 files changed, 32 insertions(+), 19 deletions(-)
drivers/cpuidle/cpuidle.c +2 -2
@@ -155,7 +155,7 @@ int cpuidle_enter_state(struct cpuidle_device *dev, struct cpuidle_driver *drv,* Returns the index of the idle state. */ int cpuidle_select (struct cpuidle_driver *drv,struct cpuidle_device *dev, -int latency_req,int next_event) +struct cpuidle_times *times) {if (off || !initialized)return -ENODEV ;
@@ -166,7 +166,7 @@ int cpuidle_select(struct cpuidle_driver *drv, struct cpuidle_device *dev,if (unlikely (use_deepest_state))return cpuidle_find_deepest_state (drv, dev); -return cpuidle_curr_governor->select (drv, dev, latency_req, next_event); +return cpuidle_curr_governor->select (drv, dev, times); }/**
drivers/cpuidle/governors/ladder.c +2 -1
@@ -65,10 +65,11 @@ static inline void ladder_do_selection(struct ladder_device *ldev,*/ static int ladder_select_state (struct cpuidle_driver *drv,struct cpuidle_device *dev, -int latency_req,int next_event) +struct cpuidle_times *times) {struct ladder_device *ldev = &__get_cpu_var (ladder_devices);struct ladder_device_state *last_state; +int latency_req = times->latency_req;int last_residency, last_idx = ldev->last_state_idx; last_state = &ldev->states[last_idx];
drivers/cpuidle/governors/menu.c +3 -2
@@ -284,9 +284,10 @@ again:* @dev: the CPU */ static int menu_select (struct cpuidle_driver *drv,struct cpuidle_device *dev, -int latency_req,int next_event) +struct cpuidle_times *times) {struct menu_device *data = &__get_cpu_var (menu_devices); +int latency_req = times->latency_req;int i;unsigned int interactivity_req;unsigned long nr_iowaiters, cpu_load;
@@ -299,7 +300,7 @@ static int menu_select(struct cpuidle_driver *drv, struct cpuidle_device *dev, data->last_state_idx =CPUIDLE_DRIVER_STATE_START -1 ;/* determine the expected residency time, round up */ - data->next_timer_us = next_event; + data->next_timer_us = times->next_timer_event;get_iowait_load (&nr_iowaiters, &cpu_load); data->bucket =which_bucket (data->next_timer_us, nr_iowaiters);
include/linux/cpuidle.h +9 -3
@@ -83,6 +83,12 @@ struct cpuidle_device {#endif }; +struct cpuidle_times { +unsigned int latency_req; +unsigned int next_timer_event; +unsigned int next_io_event; +}; + DECLARE_PER_CPU(struct cpuidle_device *, cpuidle_devices); DECLARE_PER_CPU(struct cpuidle_device, cpuidle_dev);
@@ -123,7 +129,7 @@ extern void disable_cpuidle(void);extern int cpuidle_select (struct cpuidle_driver *drv,struct cpuidle_device *dev, -int latency_req,int next_event); +struct cpuidle_times *times);extern int cpuidle_enter (struct cpuidle_driver *drv,struct cpuidle_device *dev,int index);extern void cpuidle_reflect (struct cpuidle_device *dev,int index);
@@ -152,7 +158,7 @@ extern struct cpuidle_driver *cpuidle_get_cpu_driver(struct cpuidle_device *dev)static inline void disable_cpuidle (void ) { }static inline int cpuidle_select (struct cpuidle_driver *drv,struct cpuidle_device *dev, -int latency_req,int next_event) +struct cpuidle_times *times) {return -ENODEV ; }static inline int cpuidle_enter (struct cpuidle_driver *drv,struct cpuidle_device *dev,int index)
@@ -208,7 +214,7 @@ struct cpuidle_governor {int (*select) (struct cpuidle_driver *drv,struct cpuidle_device *dev, -int latency_req,int next_event); +struct cpuidle_times *times);void (*reflect) (struct cpuidle_device *dev,int index);struct module *owner;
kernel/sched/idle.c +16 -11
@@ -4,7 +4,7 @@#include <linux/sched.h> #include <linux/cpu.h> #include <linux/cpuidle.h> -#include <linux/tick.h> +#include <linux/ktime.h> #include <linux/pm_qos.h> #include <linux/mm.h> #include <linux/stackprotector.h>
@@ -14,6 +14,7 @@#include <trace/events/power.h> #include "sched.h" +#include "io_latency.h" static int __read_mostly cpu_idle_force_poll;
@@ -79,9 +80,9 @@ static void cpuidle_idle_call(void) {struct cpuidle_device *dev =__this_cpu_read (cpuidle_devices);struct cpuidle_driver *drv =cpuidle_get_cpu_driver (dev); -struct timespec t; -int latency_req =pm_qos_request (PM_QOS_CPU_DMA_LATENCY ); -unsigned int broadcast; +struct cpuidle_times times; +int next_state, entered_state; +bool broadcast;/* * Check if the idle task must be rescheduled. If it is the
@@ -105,25 +106,29 @@ static void cpuidle_idle_call(void)*/ rcu_idle_enter (); + times.latency_req =pm_qos_request (PM_QOS_CPU_DMA_LATENCY );/* * The latency requirement does not allow any latency, jump to -* the default idle function +* the default idle function without entering the cpuidle code */ -if (latency_req ==0 ) +if (times.latency_req ==0 ) goto use_default; - t =ktime_to_timespec (tick_nohz_get_sleep_length ()); +/* +* Retrieve the next timer event +*/ + times.next_timer_event =ktime_to_us (tick_nohz_get_sleep_length ()); -/* -* The next timer event for this in us +/* +* Retrieve the next IO guessed event */ - next_event = t.tv_sec *USEC_PER_SEC + t.tv_nsec /NSEC_PER_USEC ; + times.next_io_event =io_latency_get_sleep_length (this_rq ());/* * Ask the cpuidle framework to choose a convenient idle state. * Fall back to the default arch idle method on errors. */ - next_state =cpuidle_select (drv, dev, latency_req, next_event); + next_state =cpuidle_select (drv, dev, ×);if (next_state <0 ) { use_default:/*
Signature · 1 line
1.9.1
PATCH 7/10 cpuidle: Add a simple select governor
In reply to Daniel LezcanoThis simple governor takes into account the predictable events: the timer sleep duration and the next expected IO sleep duration. By mixing both it deduced what idle state fits better. This governor must be extended to a statistical approach to predict all the other events.
The main purpose of this governor is to handle the guessed next events in a categorized way:
1. deterministic events : timers
2. guessed events : IOs
3. predictable events : keystroke, incoming network packet, ...
This governor is aimed to be moved later near the scheduler, so this one can inspect/inject more informations and act proactively rather than reactively.
--- drivers/cpuidle/Kconfig | 4 +++ drivers/cpuidle/governors/Makefile | 1 + drivers/cpuidle/governors/select.c | 55 ++++++++++++++++++++++++++++++++++++++ 3 files changed, 60 insertions(+) create mode 100644 drivers/cpuidle/governors/select.c
drivers/cpuidle/Kconfig +4 -0
@@ -25,6 +25,10 @@ config CPU_IDLE_GOV_MENU bool "Menu governor (for tickless system)" default y +config CPU_IDLE_GOV_SELECT + bool "Select governor (for tickless system)" + default y + menu "ARM CPU Idle Drivers" depends on ARM source "drivers/cpuidle/Kconfig.arm"
drivers/cpuidle/governors/Makefile +1 -0
@@ -4,3 +4,4 @@ obj-$(CONFIG_CPU_IDLE_GOV_LADDER) += ladder.o obj-$(CONFIG_CPU_IDLE_GOV_MENU) += menu.o +obj-$(CONFIG_CPU_IDLE_GOV_SELECT) += select.o
drivers/cpuidle/governors/select.c new file +55 -0
55 lines not on this page · raw message
Signature · 1 line
1.9.1
PATCH 8/10 cpuidle: select: hack - increase rating to have this governor as default
In reply to Daniel Lezcano--- drivers/cpuidle/governors/select.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/cpuidle/governors/select.c +1 -1
@@ -41,7 +41,7 @@ static int select(struct cpuidle_driver *drv, struct cpuidle_device *dev,static struct cpuidle_governor select_governor = { .name ="select" , - .rating =10 , + .rating =30 , .select = select, .owner =THIS_MODULE , };
Signature · 1 line
1.9.1
PATCH 9/10 cpuidle: sysfs: Add per cpu idle state prediction statistics
In reply to Daniel Lezcano--- drivers/cpuidle/cpuidle.c | 8 +++ drivers/cpuidle/sysfs.c | 156 ++++++++++++++++++++++++++++++++++++++++++++++ include/linux/cpuidle.h | 7 ++- 3 files changed, 170 insertions(+), 1 deletion(-)
drivers/cpuidle/cpuidle.c +8 -0
@@ -139,6 +139,14 @@ int cpuidle_enter_state(struct cpuidle_device *dev, struct cpuidle_driver *drv,*/ dev->states_usage[entered_state].time += dev->last_residency; dev->states_usage[entered_state].usage++; +if (diff < dev->last_residency) +atomic_inc (&dev->over_estimate); +else if (entered_state < (drv->state_count -1 ) && + dev->last_residency < + drv->states[entered_state +1 ].target_residency) +atomic_inc (&dev->under_estimate); +else +atomic_inc (&dev->right_estimate); }else { dev->last_residency =0 ; }
drivers/cpuidle/sysfs.c +156 -0
174 lines not on this page · raw message
include/linux/cpuidle.h +6 -1
@@ -44,7 +44,6 @@ struct cpuidle_state {int power_usage;/* in mW */ unsigned int target_residency;/* in US */ bool disabled;/* disabled on all CPUs */ -int (*enter) (struct cpuidle_device *dev,struct cpuidle_driver *drv,int index);
@@ -62,6 +61,7 @@ struct cpuidle_state {struct cpuidle_device_kobj ;struct cpuidle_state_kobj ;struct cpuidle_driver_kobj ; +struct cpuidle_stats_kobj ;struct cpuidle_device {unsigned int registered:1 ;
@@ -74,8 +74,13 @@ struct cpuidle_device {struct cpuidle_state_kobj *kobjs[CPUIDLE_STATE_MAX ];struct cpuidle_driver_kobj *kobj_driver;struct cpuidle_device_kobj *kobj_dev; +struct cpuidle_stats_kobj *kobj_stats;struct list_head device_list; +atomic_t right_estimate; +atomic_t under_estimate; +atomic_t over_estimate; +#ifdef CONFIG_ARCH_NEEDS_CPU_IDLE_COUPLED int safe_state_index;cpumask_t coupled_cpus;
Signature · 1 line
1.9.1
PATCH 10/10 sched: io_latency: Tracking via buckets
In reply to Daniel LezcanoIt was recently added in the energy aware scheduler kernel tree the io latency tracking mechanism. The purpose of this framework is to provide a way to predict the IO latencies, in other words try to guess how long we will be sleeping on waiting an IO. When the cpu goes idle, we know how long is the sleep duration with the timer but then we rely on some statistics in the menu governor, which is part of the cpuidle framework for other wakes up.
The io latency tracking will provide an additional information about the length of the expected sleep time, which combined with the timer duration should give us a more accurate prediction.
The first step of the io latency tracking was simply using a sliding average of the values, which is not really accurate as it is not immune against IOs ping pong or big variations.
In order to improve that, each latency is grouped into a bucket which represent an interval of latency and for each bucket a sliding average is computed.
Why ? Because we don't want to take all the latencies and compute the statistics on them. It does not make sense, takes a lot of memory, computation time, for finally a result which is mathematically impossible to resolve. It is better to use intervals to group the small variations of the latencies. For example. 186us, 123us, 134us can fall into the bucket [100 - 199].
The size of the bucket is the bucket interval and represent the resolution of the statistic model. Eg with a bucket interval of 1us, it leads us to do statitics on all numbers, with of course a bad prediction because the number of latencies is big. A big interval can give better statistics, but can give us a misprediction as the interval is larger.
Choosing the size of the bucket interval vs the idle sleep time is the tradeoff to find. With a 200us bucket interval, the measurements show we still have good predictions, less mispredictions and cover the idle state target residency.
The buckets are dynamically created and stored into a list. A new bucket is added at the end of the list.
This list is always moving depending on the number of successives hits a bucket will have. The more a bucket is successively hit, the more it will be the first element of the list.
The guessed next latency, which is a bucket (understand it will be between eg. 200us and 300us, with a bucket interval of 100us), is retrieved from the list. Each bucket present in the list will mark a score, the more the hits a bucket has, the bigger score it has. *But* this is weighted by the position in the list. The first elements will have more weight than the last ones. This position is dynamically changed when a bucket is hit several times.
Example the following latencies:
10, 100, 100, 100, 100, 100, 10, 10
We will have two buckets: 0 and 1.
10 => bucket0(1) 100 => bucket0(1), bucket1(1) 100 => bucket0(1), bucket1(2) 100 => bucket0(1), bucket1(3) 100 => bucket0(1), bucket1(4) * 100 => bucket1(5), bucket0(1) 10 => bucket1(5), bucket0(2) 10 => bucket1(5), bucket0(3)
At (*), bucket1 reached 5 successive hits at has been move at the beginning of the list and bucket0 became the second one.
--- include/linux/sched.h | 8 ++ kernel/exit.c | 1 + kernel/fork.c | 1 + kernel/sched/core.c | 3 +- kernel/sched/io_latency.c | 309 ++++++++++++++++++++++++++++++++++++++++++---- kernel/sched/io_latency.h | 8 +- 6 files changed, 304 insertions(+), 26 deletions(-)
include/linux/sched.h +8 -0
@@ -1228,7 +1228,15 @@ struct io_latency_node {unsigned int avg_latency;ktime_t start_time;ktime_t end_time; +struct list_head bucket_list; }; + +void exit_io_latency (struct task_struct *tsk); +#else +static inline void exit_io_latency (struct task_struct *tsk) +{ + ; +}#endif struct task_struct {
kernel/exit.c +1 -0
@@ -757,6 +757,7 @@ void do_exit(long code)exit_task_namespaces (tsk);exit_task_work (tsk);exit_thread (); +exit_io_latency (tsk);/* * Flush inherited counters to the parent - before the parent
kernel/fork.c +1 -0
@@ -347,6 +347,7 @@ static struct task_struct *dup_task_struct(struct task_struct *orig) tsk->task_frag.page =NULL ;#ifdef CONFIG_SCHED_IO_LATENCY tsk->io_latency.avg_latency =0 ; +INIT_LIST_HEAD (&tsk->io_latency.bucket_list);#endif account_kernel_stack (ti,1 );
kernel/sched/core.c +2 -1
@@ -6961,6 +6961,8 @@ void __init sched_init(void)autogroup_init (&init_task);#endif /* CONFIG_CGROUP_SCHED */ + +io_latency_init ();for_each_possible_cpu (i) {struct rq *rq;
@@ -7035,7 +7037,6 @@ void __init sched_init(void)#endif init_rq_hrtick (rq);atomic_set (&rq->nr_iowait,0 ); -io_latency_init (rq); }set_load_weight (&init_task);
kernel/sched/io_latency.c +288 -21
354 lines not on this page · raw message
kernel/sched/io_latency.h +4 -4
@@ -11,12 +11,12 @@*/ #ifdef CONFIG_SCHED_IO_LATENCY -extern void io_latency_init (struct rq *rq); +extern void io_latency_init (void );extern int io_latency_begin (struct rq *rq,struct task_struct *tsk);extern void io_latency_end (struct rq *rq,struct task_struct *tsk); -extern int io_latency_get_sleep_length (struct rq *rq); +extern s64 io_latency_get_sleep_length (struct rq *rq);#else -static inline void io_latency_init (struct rq *rq) +static inline void io_latency_init (void ) { ; }
@@ -31,7 +31,7 @@ static inline void io_latency_end(struct rq *rq, struct task_struct *tsk) ; } -static inline int io_latency_get_sleep_length (struct rq *rq) +static inline s64 io_latency_get_sleep_length (struct rq *rq) {return 0 ; }
Signature · 1 line
1.9.1
Peter Zijlstra
maintainerRe: [RFD PATCH 10/10] sched: io_latency: Tracking via buckets
Quoted 11 lines · Daniel Lezcano
The size of the bucket is the bucket interval and represent the resolution of the statistic model. Eg with a bucket interval of 1us, it leads us to do statitics on all numbers, with of course a bad prediction because the number of latencies is big. A big interval can give better statistics, but can give us a misprediction as the interval is larger.
Choosing the size of the bucket interval vs the idle sleep time is the tradeoff to find. With a 200us bucket interval, the measurements show we still have good predictions, less mispredictions and cover the idle state target residency.
For the record:
The suggestion in DUS was to align the bucket sizes with the various break even times of the actual C states.
I forgot who suggested this; it might have been Morten; but it seems to make sense.
Re: [RFD PATCH 10/10] sched: io_latency: Tracking via buckets
Quoted 13 lines · Peter Zijlstra
Quoted 11 lines · Daniel Lezcano
The size of the bucket is the bucket interval and represent the resolution of the statistic model. Eg with a bucket interval of 1us, it leads us to do statitics on all numbers, with of course a bad prediction because the number of latencies is big. A big interval can give better statistics, but can give us a misprediction as the interval is larger.
Choosing the size of the bucket interval vs the idle sleep time is the tradeoff to find. With a 200us bucket interval, the measurements show we still have good predictions, less mispredictions and cover the idle state target residency.
Quoted 7 lines
For the record:
The suggestion in DUS was to align the bucket sizes with the various break even times of the actual C states.
I forgot who suggested this; it might have been Morten; but it seems to make sense.
I suggested that a while ago too. But IIRC Daniel envisioned some possible additional use for those buckets in the future e.g.. a measure of how many tasks can be packed on a CPU knowing their sleep periods.
However, if the IO latency can be predicted at the driver level with good accuracy (it normally should) then there is no need to do any kind of averaging and bucketing since the driver should be able to provide a relatively precise number almost every time.
Nicolas
Peter Zijlstra
maintainerQuoted 4 lines · Daniel Lezcano
After discussing at the LPC2014 Dusseldorf, it appears the idea is good but the approach is wrong. The latency tracking must be done at the device level, per device and not in the task as what is doing this patchset.
end_page_writeback()->test_clear_page_writeback()->__bdi_writeout_inc()
Is the IO completion path that has a fair amount of statistics in, maybe some of that is usable.
Quoted 10 lines · Peter Zijlstra
Quoted 4 lines · Daniel Lezcano
After discussing at the LPC2014 Dusseldorf, it appears the idea is good but the approach is wrong. The latency tracking must be done at the device level, per device and not in the task as what is doing this patchset.
end_page_writeback()->test_clear_page_writeback()->__bdi_writeout_inc()
Is the IO completion path that has a fair amount of statistics in, maybe some of that is usable.
I had a look and I don't think it does what we need. The statistics in there are about estimating the device throughput for throttling writers. That doesn't help much for predicting when the next interrupt for the device is going to fire (some devices can do scatter-gather, some don't, etc.)
And this doesn't cover the read path.
Nicolas