2012-10-25 16:47:42 -06:00
|
|
|
/*
|
|
|
|
* drivers/cpufreq/cpufreq_governor.h
|
|
|
|
*
|
|
|
|
* Header file for CPUFreq governors common code
|
|
|
|
*
|
|
|
|
* Copyright (C) 2001 Russell King
|
|
|
|
* (C) 2003 Venkatesh Pallipadi <venkatesh.pallipadi@intel.com>.
|
|
|
|
* (C) 2003 Jun Nakajima <jun.nakajima@intel.com>
|
|
|
|
* (C) 2009 Alexander Clouter <alex@digriz.org.uk>
|
|
|
|
* (c) 2012 Viresh Kumar <viresh.kumar@linaro.org>
|
|
|
|
*
|
|
|
|
* 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.
|
|
|
|
*/
|
|
|
|
|
2013-04-02 06:26:15 -06:00
|
|
|
#ifndef _CPUFREQ_GOVERNOR_H
|
|
|
|
#define _CPUFREQ_GOVERNOR_H
|
2012-10-25 16:47:42 -06:00
|
|
|
|
|
|
|
#include <linux/cpufreq.h>
|
2013-08-06 11:23:03 -06:00
|
|
|
#include <linux/kernel_stat.h>
|
|
|
|
#include <linux/module.h>
|
2012-10-25 16:47:42 -06:00
|
|
|
#include <linux/mutex.h>
|
|
|
|
|
|
|
|
/*
|
|
|
|
* The polling frequency depends on the capability of the processor. Default
|
|
|
|
* polling frequency is 1000 times the transition latency of the processor. The
|
2013-08-26 12:42:21 -06:00
|
|
|
* governor will work on any processor with transition latency <= 10ms, using
|
2012-10-25 16:47:42 -06:00
|
|
|
* appropriate sampling rate.
|
|
|
|
*
|
2013-08-26 12:42:21 -06:00
|
|
|
* For CPUs with transition latency > 10ms (mostly drivers with CPUFREQ_ETERNAL)
|
|
|
|
* this governor will not work. All times here are in us (micro seconds).
|
2012-10-25 16:47:42 -06:00
|
|
|
*/
|
|
|
|
#define MIN_SAMPLING_RATE_RATIO (2)
|
|
|
|
#define LATENCY_MULTIPLIER (1000)
|
2013-02-26 02:37:24 -07:00
|
|
|
#define MIN_LATENCY_MULTIPLIER (20)
|
2012-10-25 16:47:42 -06:00
|
|
|
#define TRANSITION_LATENCY_LIMIT (10 * 1000 * 1000)
|
|
|
|
|
|
|
|
/* Ondemand Sampling types */
|
|
|
|
enum {OD_NORMAL_SAMPLE, OD_SUB_SAMPLE};
|
|
|
|
|
2013-03-27 09:58:58 -06:00
|
|
|
/*
|
|
|
|
* Macro for creating governors sysfs routines
|
|
|
|
*
|
|
|
|
* - gov_sys: One governor instance per whole system
|
|
|
|
* - gov_pol: One governor instance per policy
|
|
|
|
*/
|
|
|
|
|
|
|
|
/* Create attributes */
|
|
|
|
#define gov_sys_attr_ro(_name) \
|
|
|
|
static struct global_attr _name##_gov_sys = \
|
|
|
|
__ATTR(_name, 0444, show_##_name##_gov_sys, NULL)
|
|
|
|
|
|
|
|
#define gov_sys_attr_rw(_name) \
|
|
|
|
static struct global_attr _name##_gov_sys = \
|
|
|
|
__ATTR(_name, 0644, show_##_name##_gov_sys, store_##_name##_gov_sys)
|
|
|
|
|
|
|
|
#define gov_pol_attr_ro(_name) \
|
|
|
|
static struct freq_attr _name##_gov_pol = \
|
|
|
|
__ATTR(_name, 0444, show_##_name##_gov_pol, NULL)
|
|
|
|
|
|
|
|
#define gov_pol_attr_rw(_name) \
|
|
|
|
static struct freq_attr _name##_gov_pol = \
|
|
|
|
__ATTR(_name, 0644, show_##_name##_gov_pol, store_##_name##_gov_pol)
|
|
|
|
|
|
|
|
#define gov_sys_pol_attr_rw(_name) \
|
|
|
|
gov_sys_attr_rw(_name); \
|
|
|
|
gov_pol_attr_rw(_name)
|
|
|
|
|
|
|
|
#define gov_sys_pol_attr_ro(_name) \
|
|
|
|
gov_sys_attr_ro(_name); \
|
|
|
|
gov_pol_attr_ro(_name)
|
|
|
|
|
|
|
|
/* Create show/store routines */
|
|
|
|
#define show_one(_gov, file_name) \
|
|
|
|
static ssize_t show_##file_name##_gov_sys \
|
2012-10-25 16:47:42 -06:00
|
|
|
(struct kobject *kobj, struct attribute *attr, char *buf) \
|
|
|
|
{ \
|
2013-03-27 09:58:58 -06:00
|
|
|
struct _gov##_dbs_tuners *tuners = _gov##_dbs_cdata.gdbs_data->tuners; \
|
|
|
|
return sprintf(buf, "%u\n", tuners->file_name); \
|
|
|
|
} \
|
|
|
|
\
|
2013-06-19 02:49:33 -06:00
|
|
|
static ssize_t show_##file_name##_gov_pol \
|
2013-03-27 09:58:58 -06:00
|
|
|
(struct cpufreq_policy *policy, char *buf) \
|
|
|
|
{ \
|
|
|
|
struct dbs_data *dbs_data = policy->governor_data; \
|
|
|
|
struct _gov##_dbs_tuners *tuners = dbs_data->tuners; \
|
|
|
|
return sprintf(buf, "%u\n", tuners->file_name); \
|
|
|
|
}
|
|
|
|
|
|
|
|
#define store_one(_gov, file_name) \
|
|
|
|
static ssize_t store_##file_name##_gov_sys \
|
2013-06-19 02:49:33 -06:00
|
|
|
(struct kobject *kobj, struct attribute *attr, const char *buf, size_t count) \
|
2013-03-27 09:58:58 -06:00
|
|
|
{ \
|
|
|
|
struct dbs_data *dbs_data = _gov##_dbs_cdata.gdbs_data; \
|
|
|
|
return store_##file_name(dbs_data, buf, count); \
|
|
|
|
} \
|
|
|
|
\
|
|
|
|
static ssize_t store_##file_name##_gov_pol \
|
|
|
|
(struct cpufreq_policy *policy, const char *buf, size_t count) \
|
|
|
|
{ \
|
|
|
|
struct dbs_data *dbs_data = policy->governor_data; \
|
|
|
|
return store_##file_name(dbs_data, buf, count); \
|
2012-10-25 16:47:42 -06:00
|
|
|
}
|
|
|
|
|
2013-03-27 09:58:58 -06:00
|
|
|
#define show_store_one(_gov, file_name) \
|
|
|
|
show_one(_gov, file_name); \
|
|
|
|
store_one(_gov, file_name)
|
|
|
|
|
|
|
|
/* create helper routines */
|
2012-10-25 16:47:42 -06:00
|
|
|
#define define_get_cpu_dbs_routines(_dbs_info) \
|
2015-06-19 05:48:03 -06:00
|
|
|
static struct cpu_dbs_info *get_cpu_cdbs(int cpu) \
|
2012-10-25 16:47:42 -06:00
|
|
|
{ \
|
|
|
|
return &per_cpu(_dbs_info, cpu).cdbs; \
|
|
|
|
} \
|
|
|
|
\
|
|
|
|
static void *get_cpu_dbs_info_s(int cpu) \
|
|
|
|
{ \
|
|
|
|
return &per_cpu(_dbs_info, cpu); \
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Abbreviations:
|
|
|
|
* dbs: used as a shortform for demand based switching It helps to keep variable
|
|
|
|
* names smaller, simpler
|
|
|
|
* cdbs: common dbs
|
2013-02-27 22:38:00 -07:00
|
|
|
* od_*: On-demand governor
|
2012-10-25 16:47:42 -06:00
|
|
|
* cs_*: Conservative governor
|
|
|
|
*/
|
|
|
|
|
|
|
|
/* Per cpu structures */
|
2015-06-19 05:48:03 -06:00
|
|
|
struct cpu_dbs_info {
|
2012-10-25 16:51:21 -06:00
|
|
|
u64 prev_cpu_idle;
|
|
|
|
u64 prev_cpu_wall;
|
|
|
|
u64 prev_cpu_nice;
|
cpufreq: governor: Be friendly towards latency-sensitive bursty workloads
Cpufreq governors like the ondemand governor calculate the load on the CPU
periodically by employing deferrable timers. A deferrable timer won't fire
if the CPU is completely idle (and there are no other timers to be run), in
order to avoid unnecessary wakeups and thus save CPU power.
However, the load calculation logic is agnostic to all this, and this can
lead to the problem described below.
Time (ms) CPU 1
100 Task-A running
110 Governor's timer fires, finds load as 100% in the last
10ms interval and increases the CPU frequency.
110.5 Task-A running
120 Governor's timer fires, finds load as 100% in the last
10ms interval and increases the CPU frequency.
125 Task-A went to sleep. With nothing else to do, CPU 1
went completely idle.
200 Task-A woke up and started running again.
200.5 Governor's deferred timer (which was originally programmed
to fire at time 130) fires now. It calculates load for the
time period 120 to 200.5, and finds the load is almost zero.
Hence it decreases the CPU frequency to the minimum.
210 Governor's timer fires, finds load as 100% in the last
10ms interval and increases the CPU frequency.
So, after the workload woke up and started running, the frequency was suddenly
dropped to absolute minimum, and after that, there was an unnecessary delay of
10ms (sampling period) to increase the CPU frequency back to a reasonable value.
And this pattern repeats for every wake-up-from-cpu-idle for that workload.
This can be quite undesirable for latency- or response-time sensitive bursty
workloads. So we need to fix the governor's logic to detect such wake-up-from-
cpu-idle scenarios and start the workload at a reasonably high CPU frequency.
One extreme solution would be to fake a load of 100% in such scenarios. But
that might lead to undesirable side-effects such as frequency spikes (which
might also need voltage changes) especially if the previous frequency happened
to be very low.
We just want to avoid the stupidity of dropping down the frequency to a minimum
and then enduring a needless (and long) delay before ramping it up back again.
So, let us simply carry forward the previous load - that is, let us just pretend
that the 'load' for the current time-window is the same as the load for the
previous window. That way, the frequency and voltage will continue to be set
to whatever values they were set at previously. This means that bursty workloads
will get a chance to influence the CPU frequency at which they wake up from
cpu-idle, based on their past execution history. Thus, they might be able to
avoid suffering from slow wakeups and long response-times.
However, we should take care not to over-do this. For example, such a "copy
previous load" logic will benefit cases like this: (where # represents busy
and . represents idle)
##########.........#########.........###########...........##########........
but it will be detrimental in cases like the one shown below, because it will
retain the high frequency (copied from the previous interval) even in a mostly
idle system:
##########.........#.................#.....................#...............
(i.e., the workload finished and the remaining tasks are such that their busy
periods are smaller than the sampling interval, which causes the timer to
always get deferred. So, this will make the copy-previous-load logic copy
the initial high load to subsequent idle periods over and over again, thus
keeping the frequency high unnecessarily).
So, we modify this copy-previous-load logic such that it is used only once
upon every wakeup-from-idle. Thus if we have 2 consecutive idle periods, the
previous load won't get blindly copied over; cpufreq will freshly evaluate the
load in the second idle interval, thus ensuring that the system comes back to
its normal state.
[ The right way to solve this whole problem is to teach the CPU frequency
governors to also track load on a per-task basis, not just a per-CPU basis,
and then use both the data sources intelligently to set the appropriate
frequency on the CPUs. But that involves redesigning the cpufreq subsystem,
so this patch should make the situation bearable until then. ]
Experimental results:
+-------------------+
I ran a modified version of ebizzy (called 'sleeping-ebizzy') that sleeps in
between its execution such that its total utilization can be a user-defined
value, say 10% or 20% (higher the utilization specified, lesser the amount of
sleeps injected). This ebizzy was run with a single-thread, tied to CPU 8.
Behavior observed with tracing (sample taken from 40% utilization runs):
------------------------------------------------------------------------
Without patch:
~~~~~~~~~~~~~~
kworker/8:2-12137 416.335742: cpu_frequency: state=2061000 cpu_id=8
kworker/8:2-12137 416.335744: sched_switch: prev_comm=kworker/8:2 ==> next_comm=ebizzy
<...>-40753 416.345741: sched_switch: prev_comm=ebizzy ==> next_comm=kworker/8:2
kworker/8:2-12137 416.345744: cpu_frequency: state=4123000 cpu_id=8
kworker/8:2-12137 416.345746: sched_switch: prev_comm=kworker/8:2 ==> next_comm=ebizzy
<...>-40753 416.355738: sched_switch: prev_comm=ebizzy ==> next_comm=kworker/8:2
<snip> --------------------------------------------------------------------- <snip>
<...>-40753 416.402202: sched_switch: prev_comm=ebizzy ==> next_comm=swapper/8
<idle>-0 416.502130: sched_switch: prev_comm=swapper/8 ==> next_comm=ebizzy
<...>-40753 416.505738: sched_switch: prev_comm=ebizzy ==> next_comm=kworker/8:2
kworker/8:2-12137 416.505739: cpu_frequency: state=2061000 cpu_id=8
kworker/8:2-12137 416.505741: sched_switch: prev_comm=kworker/8:2 ==> next_comm=ebizzy
<...>-40753 416.515739: sched_switch: prev_comm=ebizzy ==> next_comm=kworker/8:2
kworker/8:2-12137 416.515742: cpu_frequency: state=4123000 cpu_id=8
kworker/8:2-12137 416.515744: sched_switch: prev_comm=kworker/8:2 ==> next_comm=ebizzy
Observation: Ebizzy went idle at 416.402202, and started running again at
416.502130. But cpufreq noticed the long idle period, and dropped the frequency
at 416.505739, only to increase it back again at 416.515742, realizing that the
workload is in-fact CPU bound. Thus ebizzy needlessly ran at the lowest frequency
for almost 13 milliseconds (almost 1 full sample period), and this pattern
repeats on every sleep-wakeup. This could hurt latency-sensitive workloads quite
a lot.
With patch:
~~~~~~~~~~~
kworker/8:2-29802 464.832535: cpu_frequency: state=2061000 cpu_id=8
<snip> --------------------------------------------------------------------- <snip>
kworker/8:2-29802 464.962538: sched_switch: prev_comm=kworker/8:2 ==> next_comm=ebizzy
<...>-40738 464.972533: sched_switch: prev_comm=ebizzy ==> next_comm=kworker/8:2
kworker/8:2-29802 464.972536: cpu_frequency: state=4123000 cpu_id=8
kworker/8:2-29802 464.972538: sched_switch: prev_comm=kworker/8:2 ==> next_comm=ebizzy
<...>-40738 464.982531: sched_switch: prev_comm=ebizzy ==> next_comm=kworker/8:2
<snip> --------------------------------------------------------------------- <snip>
kworker/8:2-29802 465.022533: sched_switch: prev_comm=kworker/8:2 ==> next_comm=ebizzy
<...>-40738 465.032531: sched_switch: prev_comm=ebizzy ==> next_comm=kworker/8:2
kworker/8:2-29802 465.032532: sched_switch: prev_comm=kworker/8:2 ==> next_comm=ebizzy
<...>-40738 465.035797: sched_switch: prev_comm=ebizzy ==> next_comm=swapper/8
<idle>-0 465.240178: sched_switch: prev_comm=swapper/8 ==> next_comm=ebizzy
<...>-40738 465.242533: sched_switch: prev_comm=ebizzy ==> next_comm=kworker/8:2
kworker/8:2-29802 465.242535: sched_switch: prev_comm=kworker/8:2 ==> next_comm=ebizzy
<...>-40738 465.252531: sched_switch: prev_comm=ebizzy ==> next_comm=kworker/8:2
Observation: Ebizzy went idle at 465.035797, and started running again at
465.240178. Since ebizzy was the only real workload running on this CPU,
cpufreq retained the frequency at 4.1Ghz throughout the run of ebizzy, no
matter how many times ebizzy slept and woke-up in-between. Thus, ebizzy
got the 10ms worth of 4.1 Ghz benefit during every sleep-wakeup (as compared
to the run without the patch) and this boost gave a modest improvement in total
throughput, as shown below.
Sleeping-ebizzy records-per-second:
-----------------------------------
Utilization Without patch With patch Difference (Absolute and % values)
10% 274767 277046 + 2279 (+0.829%)
20% 543429 553484 + 10055 (+1.850%)
40% 1090744 1107959 + 17215 (+1.578%)
60% 1634908 1662018 + 27110 (+1.658%)
A rudimentary and somewhat approximately latency-sensitive workload such as
sleeping-ebizzy itself showed a consistent, noticeable performance improvement
with this patch. Hence, workloads that are truly latency-sensitive will benefit
quite a bit from this change. Moreover, this is an overall win-win since this
patch does not hurt power-savings at all (because, this patch does not reduce
the idle time or idle residency; and the high frequency of the CPU when it goes
to cpu-idle does not affect/hurt the power-savings of deep idle states).
Signed-off-by: Srivatsa S. Bhat <srivatsa.bhat@linux.vnet.ibm.com>
Reviewed-by: Gautham R. Shenoy <ego@linux.vnet.ibm.com>
Acked-by: Viresh Kumar <viresh.kumar@linaro.org>
Signed-off-by: Rafael J. Wysocki <rafael.j.wysocki@intel.com>
2014-06-07 14:41:43 -06:00
|
|
|
/*
|
2014-06-09 02:51:24 -06:00
|
|
|
* Used to keep track of load in the previous interval. However, when
|
|
|
|
* explicitly set to zero, it is used as a flag to ensure that we copy
|
|
|
|
* the previous load to the current interval only once, upon the first
|
|
|
|
* wake-up from idle.
|
cpufreq: governor: Be friendly towards latency-sensitive bursty workloads
Cpufreq governors like the ondemand governor calculate the load on the CPU
periodically by employing deferrable timers. A deferrable timer won't fire
if the CPU is completely idle (and there are no other timers to be run), in
order to avoid unnecessary wakeups and thus save CPU power.
However, the load calculation logic is agnostic to all this, and this can
lead to the problem described below.
Time (ms) CPU 1
100 Task-A running
110 Governor's timer fires, finds load as 100% in the last
10ms interval and increases the CPU frequency.
110.5 Task-A running
120 Governor's timer fires, finds load as 100% in the last
10ms interval and increases the CPU frequency.
125 Task-A went to sleep. With nothing else to do, CPU 1
went completely idle.
200 Task-A woke up and started running again.
200.5 Governor's deferred timer (which was originally programmed
to fire at time 130) fires now. It calculates load for the
time period 120 to 200.5, and finds the load is almost zero.
Hence it decreases the CPU frequency to the minimum.
210 Governor's timer fires, finds load as 100% in the last
10ms interval and increases the CPU frequency.
So, after the workload woke up and started running, the frequency was suddenly
dropped to absolute minimum, and after that, there was an unnecessary delay of
10ms (sampling period) to increase the CPU frequency back to a reasonable value.
And this pattern repeats for every wake-up-from-cpu-idle for that workload.
This can be quite undesirable for latency- or response-time sensitive bursty
workloads. So we need to fix the governor's logic to detect such wake-up-from-
cpu-idle scenarios and start the workload at a reasonably high CPU frequency.
One extreme solution would be to fake a load of 100% in such scenarios. But
that might lead to undesirable side-effects such as frequency spikes (which
might also need voltage changes) especially if the previous frequency happened
to be very low.
We just want to avoid the stupidity of dropping down the frequency to a minimum
and then enduring a needless (and long) delay before ramping it up back again.
So, let us simply carry forward the previous load - that is, let us just pretend
that the 'load' for the current time-window is the same as the load for the
previous window. That way, the frequency and voltage will continue to be set
to whatever values they were set at previously. This means that bursty workloads
will get a chance to influence the CPU frequency at which they wake up from
cpu-idle, based on their past execution history. Thus, they might be able to
avoid suffering from slow wakeups and long response-times.
However, we should take care not to over-do this. For example, such a "copy
previous load" logic will benefit cases like this: (where # represents busy
and . represents idle)
##########.........#########.........###########...........##########........
but it will be detrimental in cases like the one shown below, because it will
retain the high frequency (copied from the previous interval) even in a mostly
idle system:
##########.........#.................#.....................#...............
(i.e., the workload finished and the remaining tasks are such that their busy
periods are smaller than the sampling interval, which causes the timer to
always get deferred. So, this will make the copy-previous-load logic copy
the initial high load to subsequent idle periods over and over again, thus
keeping the frequency high unnecessarily).
So, we modify this copy-previous-load logic such that it is used only once
upon every wakeup-from-idle. Thus if we have 2 consecutive idle periods, the
previous load won't get blindly copied over; cpufreq will freshly evaluate the
load in the second idle interval, thus ensuring that the system comes back to
its normal state.
[ The right way to solve this whole problem is to teach the CPU frequency
governors to also track load on a per-task basis, not just a per-CPU basis,
and then use both the data sources intelligently to set the appropriate
frequency on the CPUs. But that involves redesigning the cpufreq subsystem,
so this patch should make the situation bearable until then. ]
Experimental results:
+-------------------+
I ran a modified version of ebizzy (called 'sleeping-ebizzy') that sleeps in
between its execution such that its total utilization can be a user-defined
value, say 10% or 20% (higher the utilization specified, lesser the amount of
sleeps injected). This ebizzy was run with a single-thread, tied to CPU 8.
Behavior observed with tracing (sample taken from 40% utilization runs):
------------------------------------------------------------------------
Without patch:
~~~~~~~~~~~~~~
kworker/8:2-12137 416.335742: cpu_frequency: state=2061000 cpu_id=8
kworker/8:2-12137 416.335744: sched_switch: prev_comm=kworker/8:2 ==> next_comm=ebizzy
<...>-40753 416.345741: sched_switch: prev_comm=ebizzy ==> next_comm=kworker/8:2
kworker/8:2-12137 416.345744: cpu_frequency: state=4123000 cpu_id=8
kworker/8:2-12137 416.345746: sched_switch: prev_comm=kworker/8:2 ==> next_comm=ebizzy
<...>-40753 416.355738: sched_switch: prev_comm=ebizzy ==> next_comm=kworker/8:2
<snip> --------------------------------------------------------------------- <snip>
<...>-40753 416.402202: sched_switch: prev_comm=ebizzy ==> next_comm=swapper/8
<idle>-0 416.502130: sched_switch: prev_comm=swapper/8 ==> next_comm=ebizzy
<...>-40753 416.505738: sched_switch: prev_comm=ebizzy ==> next_comm=kworker/8:2
kworker/8:2-12137 416.505739: cpu_frequency: state=2061000 cpu_id=8
kworker/8:2-12137 416.505741: sched_switch: prev_comm=kworker/8:2 ==> next_comm=ebizzy
<...>-40753 416.515739: sched_switch: prev_comm=ebizzy ==> next_comm=kworker/8:2
kworker/8:2-12137 416.515742: cpu_frequency: state=4123000 cpu_id=8
kworker/8:2-12137 416.515744: sched_switch: prev_comm=kworker/8:2 ==> next_comm=ebizzy
Observation: Ebizzy went idle at 416.402202, and started running again at
416.502130. But cpufreq noticed the long idle period, and dropped the frequency
at 416.505739, only to increase it back again at 416.515742, realizing that the
workload is in-fact CPU bound. Thus ebizzy needlessly ran at the lowest frequency
for almost 13 milliseconds (almost 1 full sample period), and this pattern
repeats on every sleep-wakeup. This could hurt latency-sensitive workloads quite
a lot.
With patch:
~~~~~~~~~~~
kworker/8:2-29802 464.832535: cpu_frequency: state=2061000 cpu_id=8
<snip> --------------------------------------------------------------------- <snip>
kworker/8:2-29802 464.962538: sched_switch: prev_comm=kworker/8:2 ==> next_comm=ebizzy
<...>-40738 464.972533: sched_switch: prev_comm=ebizzy ==> next_comm=kworker/8:2
kworker/8:2-29802 464.972536: cpu_frequency: state=4123000 cpu_id=8
kworker/8:2-29802 464.972538: sched_switch: prev_comm=kworker/8:2 ==> next_comm=ebizzy
<...>-40738 464.982531: sched_switch: prev_comm=ebizzy ==> next_comm=kworker/8:2
<snip> --------------------------------------------------------------------- <snip>
kworker/8:2-29802 465.022533: sched_switch: prev_comm=kworker/8:2 ==> next_comm=ebizzy
<...>-40738 465.032531: sched_switch: prev_comm=ebizzy ==> next_comm=kworker/8:2
kworker/8:2-29802 465.032532: sched_switch: prev_comm=kworker/8:2 ==> next_comm=ebizzy
<...>-40738 465.035797: sched_switch: prev_comm=ebizzy ==> next_comm=swapper/8
<idle>-0 465.240178: sched_switch: prev_comm=swapper/8 ==> next_comm=ebizzy
<...>-40738 465.242533: sched_switch: prev_comm=ebizzy ==> next_comm=kworker/8:2
kworker/8:2-29802 465.242535: sched_switch: prev_comm=kworker/8:2 ==> next_comm=ebizzy
<...>-40738 465.252531: sched_switch: prev_comm=ebizzy ==> next_comm=kworker/8:2
Observation: Ebizzy went idle at 465.035797, and started running again at
465.240178. Since ebizzy was the only real workload running on this CPU,
cpufreq retained the frequency at 4.1Ghz throughout the run of ebizzy, no
matter how many times ebizzy slept and woke-up in-between. Thus, ebizzy
got the 10ms worth of 4.1 Ghz benefit during every sleep-wakeup (as compared
to the run without the patch) and this boost gave a modest improvement in total
throughput, as shown below.
Sleeping-ebizzy records-per-second:
-----------------------------------
Utilization Without patch With patch Difference (Absolute and % values)
10% 274767 277046 + 2279 (+0.829%)
20% 543429 553484 + 10055 (+1.850%)
40% 1090744 1107959 + 17215 (+1.578%)
60% 1634908 1662018 + 27110 (+1.658%)
A rudimentary and somewhat approximately latency-sensitive workload such as
sleeping-ebizzy itself showed a consistent, noticeable performance improvement
with this patch. Hence, workloads that are truly latency-sensitive will benefit
quite a bit from this change. Moreover, this is an overall win-win since this
patch does not hurt power-savings at all (because, this patch does not reduce
the idle time or idle residency; and the high frequency of the CPU when it goes
to cpu-idle does not affect/hurt the power-savings of deep idle states).
Signed-off-by: Srivatsa S. Bhat <srivatsa.bhat@linux.vnet.ibm.com>
Reviewed-by: Gautham R. Shenoy <ego@linux.vnet.ibm.com>
Acked-by: Viresh Kumar <viresh.kumar@linaro.org>
Signed-off-by: Rafael J. Wysocki <rafael.j.wysocki@intel.com>
2014-06-07 14:41:43 -06:00
|
|
|
*/
|
2014-06-09 02:51:24 -06:00
|
|
|
unsigned int prev_load;
|
2015-06-19 05:48:05 -06:00
|
|
|
struct cpufreq_policy *policy;
|
2015-06-19 05:48:01 -06:00
|
|
|
struct delayed_work dwork;
|
2012-10-25 16:47:42 -06:00
|
|
|
/*
|
|
|
|
* percpu mutex that serializes governor limit change with gov_dbs_timer
|
|
|
|
* invocation. We do not want gov_dbs_timer to run when user is changing
|
|
|
|
* the governor or limits.
|
|
|
|
*/
|
|
|
|
struct mutex timer_mutex;
|
2012-12-27 07:55:40 -07:00
|
|
|
ktime_t time_stamp;
|
2012-10-25 16:47:42 -06:00
|
|
|
};
|
|
|
|
|
|
|
|
struct od_cpu_dbs_info_s {
|
2015-06-19 05:48:03 -06:00
|
|
|
struct cpu_dbs_info cdbs;
|
2012-10-25 16:47:42 -06:00
|
|
|
struct cpufreq_frequency_table *freq_table;
|
|
|
|
unsigned int freq_lo;
|
|
|
|
unsigned int freq_lo_jiffies;
|
|
|
|
unsigned int freq_hi_jiffies;
|
|
|
|
unsigned int rate_mult;
|
|
|
|
unsigned int sample_type:1;
|
|
|
|
};
|
|
|
|
|
|
|
|
struct cs_cpu_dbs_info_s {
|
2015-06-19 05:48:03 -06:00
|
|
|
struct cpu_dbs_info cdbs;
|
2012-10-25 16:47:42 -06:00
|
|
|
unsigned int down_skip;
|
|
|
|
unsigned int requested_freq;
|
|
|
|
unsigned int enable:1;
|
|
|
|
};
|
|
|
|
|
2013-08-26 12:42:21 -06:00
|
|
|
/* Per policy Governors sysfs tunables */
|
2012-10-25 16:47:42 -06:00
|
|
|
struct od_dbs_tuners {
|
2013-08-05 00:58:02 -06:00
|
|
|
unsigned int ignore_nice_load;
|
2012-10-25 16:47:42 -06:00
|
|
|
unsigned int sampling_rate;
|
|
|
|
unsigned int sampling_down_factor;
|
|
|
|
unsigned int up_threshold;
|
|
|
|
unsigned int powersave_bias;
|
|
|
|
unsigned int io_is_busy;
|
|
|
|
};
|
|
|
|
|
|
|
|
struct cs_dbs_tuners {
|
2013-08-05 00:58:02 -06:00
|
|
|
unsigned int ignore_nice_load;
|
2012-10-25 16:47:42 -06:00
|
|
|
unsigned int sampling_rate;
|
|
|
|
unsigned int sampling_down_factor;
|
|
|
|
unsigned int up_threshold;
|
|
|
|
unsigned int down_threshold;
|
|
|
|
unsigned int freq_step;
|
|
|
|
};
|
|
|
|
|
2013-08-26 12:42:21 -06:00
|
|
|
/* Common Governor data across policies */
|
2013-03-27 09:58:58 -06:00
|
|
|
struct dbs_data;
|
|
|
|
struct common_dbs_data {
|
2012-10-25 16:47:42 -06:00
|
|
|
/* Common across governors */
|
|
|
|
#define GOV_ONDEMAND 0
|
|
|
|
#define GOV_CONSERVATIVE 1
|
|
|
|
int governor;
|
2013-03-27 09:58:58 -06:00
|
|
|
struct attribute_group *attr_group_gov_sys; /* one governor - system */
|
|
|
|
struct attribute_group *attr_group_gov_pol; /* one governor - policy */
|
2012-10-25 16:47:42 -06:00
|
|
|
|
2013-10-02 02:43:18 -06:00
|
|
|
/*
|
|
|
|
* Common data for platforms that don't set
|
|
|
|
* CPUFREQ_HAVE_GOVERNOR_PER_POLICY
|
|
|
|
*/
|
2013-03-27 09:58:58 -06:00
|
|
|
struct dbs_data *gdbs_data;
|
2012-10-25 16:47:42 -06:00
|
|
|
|
2015-06-19 05:48:03 -06:00
|
|
|
struct cpu_dbs_info *(*get_cpu_cdbs)(int cpu);
|
2012-10-25 16:47:42 -06:00
|
|
|
void *(*get_cpu_dbs_info_s)(int cpu);
|
|
|
|
void (*gov_dbs_timer)(struct work_struct *work);
|
|
|
|
void (*gov_check_cpu)(int cpu, unsigned int load);
|
2015-06-03 04:27:11 -06:00
|
|
|
int (*init)(struct dbs_data *dbs_data, bool notify);
|
|
|
|
void (*exit)(struct dbs_data *dbs_data, bool notify);
|
2012-10-25 16:47:42 -06:00
|
|
|
|
|
|
|
/* Governor specific ops, see below */
|
|
|
|
void *gov_ops;
|
cpufreq: governor: Serialize governor callbacks
There are several races reported in cpufreq core around governors (only
ondemand and conservative) by different people.
There are at least two race scenarios present in governor code:
(a) Concurrent access/updates of governor internal structures.
It is possible that fields such as 'dbs_data->usage_count', etc. are
accessed simultaneously for different policies using same governor
structure (i.e. CPUFREQ_HAVE_GOVERNOR_PER_POLICY flag unset). And
because of this we can dereference bad pointers.
For example consider a system with two CPUs with separate 'struct
cpufreq_policy' instances. CPU0 governor: ondemand and CPU1: powersave.
CPU0 switching to powersave and CPU1 to ondemand:
CPU0 CPU1
store* store*
cpufreq_governor_exit() cpufreq_governor_init()
dbs_data = cdata->gdbs_data;
if (!--dbs_data->usage_count)
kfree(dbs_data);
dbs_data->usage_count++;
*Bad pointer dereference*
There are other races possible between EXIT and START/STOP/LIMIT as
well. Its really complicated.
(b) Switching governor state in bad sequence:
For example trying to switch a governor to START state, when the
governor is in EXIT state. There are some checks present in
__cpufreq_governor() but they aren't sufficient as they compare events
against 'policy->governor_enabled', where as we need to take governor's
state into account, which can be used by multiple policies.
These two issues need to be solved separately and the responsibility
should be properly divided between cpufreq and governor core.
The first problem is more about the governor core, as it needs to
protect its structures properly. And the second problem should be fixed
in cpufreq core instead of governor, as its all about sequence of
events.
This patch is trying to solve only the first problem.
There are two types of data we need to protect,
- 'struct common_dbs_data': No matter what, there is going to be a
single copy of this per governor.
- 'struct dbs_data': With CPUFREQ_HAVE_GOVERNOR_PER_POLICY flag set, we
will have per-policy copy of this data, otherwise a single copy.
Because of such complexities, the mutex present in 'struct dbs_data' is
insufficient to solve our problem. For example we need to protect
fetching of 'dbs_data' from different structures at the beginning of
cpufreq_governor_dbs(), to make sure it isn't currently being updated.
This can be fixed if we can guarantee serialization of event parsing
code for an individual governor. This is best solved with a mutex per
governor, and the placeholder for that is 'struct common_dbs_data'.
And so this patch moves the mutex from 'struct dbs_data' to 'struct
common_dbs_data' and takes it at the beginning and drops it at the end
of cpufreq_governor_dbs().
Tested with and without following configuration options:
CONFIG_LOCKDEP_SUPPORT=y
CONFIG_DEBUG_RT_MUTEXES=y
CONFIG_DEBUG_PI_LIST=y
CONFIG_DEBUG_SPINLOCK=y
CONFIG_DEBUG_MUTEXES=y
CONFIG_DEBUG_LOCK_ALLOC=y
CONFIG_PROVE_LOCKING=y
CONFIG_LOCKDEP=y
CONFIG_DEBUG_ATOMIC_SLEEP=y
Signed-off-by: Viresh Kumar <viresh.kumar@linaro.org>
Reviewed-by: Preeti U Murthy <preeti@linux.vnet.ibm.com>
Signed-off-by: Rafael J. Wysocki <rafael.j.wysocki@intel.com>
2015-06-03 04:27:13 -06:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Protects governor's data (struct dbs_data and struct common_dbs_data)
|
|
|
|
*/
|
|
|
|
struct mutex mutex;
|
2012-10-25 16:47:42 -06:00
|
|
|
};
|
|
|
|
|
2013-08-26 12:42:21 -06:00
|
|
|
/* Governor Per policy data */
|
2013-03-27 09:58:58 -06:00
|
|
|
struct dbs_data {
|
|
|
|
struct common_dbs_data *cdata;
|
|
|
|
unsigned int min_sampling_rate;
|
2013-04-30 08:32:17 -06:00
|
|
|
int usage_count;
|
2013-03-27 09:58:58 -06:00
|
|
|
void *tuners;
|
|
|
|
};
|
|
|
|
|
2012-10-25 16:47:42 -06:00
|
|
|
/* Governor specific ops, will be passed to dbs_data->gov_ops */
|
|
|
|
struct od_ops {
|
|
|
|
void (*powersave_bias_init_cpu)(int cpu);
|
|
|
|
unsigned int (*powersave_bias_target)(struct cpufreq_policy *policy,
|
|
|
|
unsigned int freq_next, unsigned int relation);
|
2013-08-06 11:23:05 -06:00
|
|
|
void (*freq_increase)(struct cpufreq_policy *policy, unsigned int freq);
|
2012-10-25 16:47:42 -06:00
|
|
|
};
|
|
|
|
|
|
|
|
static inline int delay_for_sampling_rate(unsigned int sampling_rate)
|
|
|
|
{
|
|
|
|
int delay = usecs_to_jiffies(sampling_rate);
|
|
|
|
|
|
|
|
/* We want all CPUs to do sampling nearly on same jiffy */
|
|
|
|
if (num_online_cpus() > 1)
|
|
|
|
delay -= jiffies % delay;
|
|
|
|
|
|
|
|
return delay;
|
|
|
|
}
|
|
|
|
|
2013-03-27 09:58:58 -06:00
|
|
|
#define declare_show_sampling_rate_min(_gov) \
|
|
|
|
static ssize_t show_sampling_rate_min_gov_sys \
|
|
|
|
(struct kobject *kobj, struct attribute *attr, char *buf) \
|
|
|
|
{ \
|
|
|
|
struct dbs_data *dbs_data = _gov##_dbs_cdata.gdbs_data; \
|
|
|
|
return sprintf(buf, "%u\n", dbs_data->min_sampling_rate); \
|
|
|
|
} \
|
|
|
|
\
|
|
|
|
static ssize_t show_sampling_rate_min_gov_pol \
|
|
|
|
(struct cpufreq_policy *policy, char *buf) \
|
|
|
|
{ \
|
|
|
|
struct dbs_data *dbs_data = policy->governor_data; \
|
|
|
|
return sprintf(buf, "%u\n", dbs_data->min_sampling_rate); \
|
|
|
|
}
|
|
|
|
|
2014-01-03 02:17:41 -07:00
|
|
|
extern struct mutex cpufreq_governor_lock;
|
|
|
|
|
2012-10-25 16:47:42 -06:00
|
|
|
void dbs_check_cpu(struct dbs_data *dbs_data, int cpu);
|
2015-06-19 05:48:03 -06:00
|
|
|
bool need_load_eval(struct cpu_dbs_info *cdbs, unsigned int sampling_rate);
|
2013-03-27 09:58:58 -06:00
|
|
|
int cpufreq_governor_dbs(struct cpufreq_policy *policy,
|
|
|
|
struct common_dbs_data *cdata, unsigned int event);
|
2013-02-26 23:54:03 -07:00
|
|
|
void gov_queue_work(struct dbs_data *dbs_data, struct cpufreq_policy *policy,
|
|
|
|
unsigned int delay, bool all_cpus);
|
2013-04-02 08:56:56 -06:00
|
|
|
void od_register_powersave_bias_handler(unsigned int (*f)
|
|
|
|
(struct cpufreq_policy *, unsigned int, unsigned int),
|
|
|
|
unsigned int powersave_bias);
|
|
|
|
void od_unregister_powersave_bias_handler(void);
|
2013-04-02 06:26:15 -06:00
|
|
|
#endif /* _CPUFREQ_GOVERNOR_H */
|