Skip to content

Commit b7eaf1a

Browse files
committed
cpufreq: schedutil: Avoid reducing frequency of busy CPUs prematurely
The way the schedutil governor uses the PELT metric causes it to underestimate the CPU utilization in some cases. That can be easily demonstrated by running kernel compilation on a Sandy Bridge Intel processor, running turbostat in parallel with it and looking at the values written to the MSR_IA32_PERF_CTL register. Namely, the expected result would be that when all CPUs were 100% busy, all of them would be requested to run in the maximum P-state, but observation shows that this clearly isn't the case. The CPUs run in the maximum P-state for a while and then are requested to run slower and go back to the maximum P-state after a while again. That causes the actual frequency of the processor to visibly oscillate below the sustainable maximum in a jittery fashion which clearly is not desirable. That has been attributed to CPU utilization metric updates on task migration that cause the total utilization value for the CPU to be reduced by the utilization of the migrated task. If that happens, the schedutil governor may see a CPU utilization reduction and will attempt to reduce the CPU frequency accordingly right away. That may be premature, though, for example if the system is generally busy and there are other runnable tasks waiting to be run on that CPU already. This is unlikely to be an issue on systems where cpufreq policies are shared between multiple CPUs, because in those cases the policy utilization is computed as the maximum of the CPU utilization values over the whole policy and if that turns out to be low, reducing the frequency for the policy most likely is a good idea anyway. On systems with one CPU per policy, however, it may affect performance adversely and even lead to increased energy consumption in some cases. On those systems it may be addressed by taking another utilization metric into consideration, like whether or not the CPU whose frequency is about to be reduced has been idle recently, because if that's not the case, the CPU is likely to be busy in the near future and its frequency should not be reduced. To that end, use the counter of idle calls in the timekeeping code. Namely, make the schedutil governor look at that counter for the current CPU every time before its frequency is about to be reduced. If the counter has not changed since the previous iteration of the governor computations for that CPU, the CPU has been busy for all that time and its frequency should not be decreased, so if the new frequency would be lower than the one set previously, the governor will skip the frequency update. Signed-off-by: Rafael J. Wysocki <rafael.j.wysocki@intel.com> Acked-by: Peter Zijlstra (Intel) <peterz@infradead.org> Acked-by: Viresh Kumar <viresh.kumar@linaro.org> Reviewed-by: Joel Fernandes <joelaf@google.com>
1 parent cba1dfb commit b7eaf1a

3 files changed

Lines changed: 40 additions & 0 deletions

File tree

‎include/linux/tick.h‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,7 @@ extern void tick_nohz_idle_enter(void);
117117
extern void tick_nohz_idle_exit(void);
118118
extern void tick_nohz_irq_exit(void);
119119
extern ktime_t tick_nohz_get_sleep_length(void);
120+
extern unsigned long tick_nohz_get_idle_calls(void);
120121
extern u64 get_cpu_idle_time_us(int cpu, u64 *last_update_time);
121122
extern u64 get_cpu_iowait_time_us(int cpu, u64 *last_update_time);
122123
#else /* !CONFIG_NO_HZ_COMMON */

‎kernel/sched/cpufreq_schedutil.c‎

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,11 @@ struct sugov_cpu {
6161
unsigned long util;
6262
unsigned long max;
6363
unsigned int flags;
64+
65+
/* The field below is for single-CPU policies only. */
66+
#ifdef CONFIG_NO_HZ_COMMON
67+
unsigned long saved_idle_calls;
68+
#endif
6469
};
6570

6671
static DEFINE_PER_CPU(struct sugov_cpu, sugov_cpu);
@@ -192,6 +197,19 @@ static void sugov_iowait_boost(struct sugov_cpu *sg_cpu, unsigned long *util,
192197
sg_cpu->iowait_boost >>= 1;
193198
}
194199

200+
#ifdef CONFIG_NO_HZ_COMMON
201+
static bool sugov_cpu_is_busy(struct sugov_cpu *sg_cpu)
202+
{
203+
unsigned long idle_calls = tick_nohz_get_idle_calls();
204+
bool ret = idle_calls == sg_cpu->saved_idle_calls;
205+
206+
sg_cpu->saved_idle_calls = idle_calls;
207+
return ret;
208+
}
209+
#else
210+
static inline bool sugov_cpu_is_busy(struct sugov_cpu *sg_cpu) { return false; }
211+
#endif /* CONFIG_NO_HZ_COMMON */
212+
195213
static void sugov_update_single(struct update_util_data *hook, u64 time,
196214
unsigned int flags)
197215
{
@@ -200,19 +218,28 @@ static void sugov_update_single(struct update_util_data *hook, u64 time,
200218
struct cpufreq_policy *policy = sg_policy->policy;
201219
unsigned long util, max;
202220
unsigned int next_f;
221+
bool busy;
203222

204223
sugov_set_iowait_boost(sg_cpu, time, flags);
205224
sg_cpu->last_update = time;
206225

207226
if (!sugov_should_update_freq(sg_policy, time))
208227
return;
209228

229+
busy = sugov_cpu_is_busy(sg_cpu);
230+
210231
if (flags & SCHED_CPUFREQ_RT_DL) {
211232
next_f = policy->cpuinfo.max_freq;
212233
} else {
213234
sugov_get_util(&util, &max);
214235
sugov_iowait_boost(sg_cpu, &util, &max);
215236
next_f = get_next_freq(sg_policy, util, max);
237+
/*
238+
* Do not reduce the frequency if the CPU has not been idle
239+
* recently, as the reduction is likely to be premature then.
240+
*/
241+
if (busy && next_f < sg_policy->next_freq)
242+
next_f = sg_policy->next_freq;
216243
}
217244
sugov_update_commit(sg_policy, time, next_f);
218245
}

‎kernel/time/tick-sched.c‎

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -993,6 +993,18 @@ ktime_t tick_nohz_get_sleep_length(void)
993993
return ts->sleep_length;
994994
}
995995

996+
/**
997+
* tick_nohz_get_idle_calls - return the current idle calls counter value
998+
*
999+
* Called from the schedutil frequency scaling governor in scheduler context.
1000+
*/
1001+
unsigned long tick_nohz_get_idle_calls(void)
1002+
{
1003+
struct tick_sched *ts = this_cpu_ptr(&tick_cpu_sched);
1004+
1005+
return ts->idle_calls;
1006+
}
1007+
9961008
static void tick_nohz_account_idle_ticks(struct tick_sched *ts)
9971009
{
9981010
#ifndef CONFIG_VIRT_CPU_ACCOUNTING_NATIVE

0 commit comments

Comments
 (0)