Skip to contentSkip to conversation

cpufreq: schedutil: Fix and optimization

PATCH In review Rafael J. Wysocki 9 participants 71 messages Mar 19, 2017 – May 8, 2017 1 message not in the archive

Subsystems (3)
lore
Share & tools
lore
#0 lore raw

cover cpufreq: schedutil: Fix and optimization

Hi,

Patch [1/2] in this series fixes an initialization issue in schedutil.

Patch [2/2] is an optimization, but it also might be regarded as a fix or at least a problem workaround.

Namely, while playing with the passive mode in intel_pstate lately I noticed that schedutil evidently underestimated CPU utilization at high loads. I guess it has been known for a while, but this is the first time I've seen that so clearly (actually by looking at the values written to the P-state request register and not via frequencies).

After some investigation I can quite confidently attribute that to a PELT metric issue, as everything else has been ruled out with high confidence, and hence the patch (which also works around the problem adding even more condifence to the observation that PELT underestimates CPU utilization at least sometimes).

Thanks,
Rafael

#1 lore raw

PATCH 1/2 cpufreq: schedutil: Fix per-CPU structure initialization in sugov_start()

From: Rafael J. Wysocki <[email protected]>

sugov_start() only initializes struct sugov_cpu per-CPU structures for shared policies, but it should do that for single-CPU policies too.

That in particular makes the IO-wait boost mechanism work in the cases when cpufreq policies correspond to individual CPUs.

Fixes 21ca6d2c52f8 (cpufreq: schedutil: Add iowait boosting)
---
 kernel/sched/cpufreq_schedutil.c |   20 +++++++-------------
 1 file changed, 7 insertions(+), 13 deletions(-)
kernel/sched/cpufreq_schedutil.c +7 -13
Index: linux-pm/kernel/sched/cpufreq_schedutil.c
===================================================================
@@ -577,20 +577,14 @@ static int sugov_start(struct cpufreq_po
 	for_each_cpu(cpu, policy->cpus) {
 		struct sugov_cpu *sg_cpu = &per_cpu(sugov_cpu, cpu);
 
+		memset(sg_cpu, 0, sizeof(*sg_cpu));
 		sg_cpu->sg_policy = sg_policy;
-		if (policy_is_shared(policy)) {
-			sg_cpu->util = 0;
-			sg_cpu->max = 0;
-			sg_cpu->flags = SCHED_CPUFREQ_RT;
-			sg_cpu->last_update = 0;
-			sg_cpu->iowait_boost = 0;
-			sg_cpu->iowait_boost_max = policy->cpuinfo.max_freq;
-			cpufreq_add_update_util_hook(cpu, &sg_cpu->update_util,
-						     sugov_update_shared);
-		} else {
-			cpufreq_add_update_util_hook(cpu, &sg_cpu->update_util,
-						     sugov_update_single);
-		}
+		sg_cpu->flags = SCHED_CPUFREQ_RT;
+		sg_cpu->iowait_boost_max = policy->cpuinfo.max_freq;
+		cpufreq_add_update_util_hook(cpu, &sg_cpu->update_util,
+					     policy_is_shared(policy) ?
+							sugov_update_shared :
+							sugov_update_single);
 	}
 	return 0;
 }
maintainer Acked-by on v1 1/2
#2 lore raw

Re: [PATCH 1/2] cpufreq: schedutil: Fix per-CPU structure initialization in sugov_start()

Quoted 10 lines · Rafael J. Wysocki

From: Rafael J. Wysocki <[email protected]>

sugov_start() only initializes struct sugov_cpu per-CPU structures for shared policies, but it should do that for single-CPU policies too.

That in particular makes the IO-wait boost mechanism work in the cases when cpufreq policies correspond to individual CPUs.

Fixes 21ca6d2c52f8 (cpufreq: schedutil: Add iowait boosting)
Cc 4.9+ <[email protected]> # 4.9+
Signature · 1 line
viresh
#3 lore raw

Re: [PATCH 1/2] cpufreq: schedutil: Fix per-CPU structure initialization in sugov_start()

Quoted 6 lines · Viresh Kumar
Quoted 5 lines · Rafael J. Wysocki

From: Rafael J. Wysocki <[email protected]>

sugov_start() only initializes struct sugov_cpu per-CPU structures for shared policies, but it should do that for single-CPU policies too.

Quoted 7 lines
Quoted 5 lines

That in particular makes the IO-wait boost mechanism work in the cases when cpufreq policies correspond to individual CPUs.

Fixes 21ca6d2c52f8 (cpufreq: schedutil: Add iowait boosting)
Cc 4.9+ <[email protected]> # 4.9+

"stable" checks Fixes: tags too, but yes.

Quoted 1 line

Thanks,
Rafael

#4 lore raw

RFC PATCH 2/2 cpufreq: schedutil: Force max frequency on busy CPUs

In reply to Rafael J. Wysocki

From: Rafael J. Wysocki <[email protected]>

The PELT metric used by the schedutil governor underestimates the CPU utilization in some cases. The reason for that may be time spent in interrupt handlers and similar which is not accounted for by PELT.

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.

To work around this issue use the observation that, from the schedutil governor's perspective, CPUs that are never idle should always run at the maximum frequency and make that happen.

To that end, add a counter of idle calls to struct sugov_cpu and modify cpuidle_idle_call() to increment that counter every time it is about to put the given CPU into an idle state. Next, make the schedutil governor look at that counter for the current CPU every time before it is about to start heavy computations. If the counter has not changed for over SUGOV_BUSY_THRESHOLD time (equal to 50 ms), the CPU has not been idle for at least that long and the governor will choose the maximum frequency for it without looking at the PELT metric at all.

---
 include/linux/sched/cpufreq.h    |    6 ++++++
 kernel/sched/cpufreq_schedutil.c |   38 ++++++++++++++++++++++++++++++++++++--
 kernel/sched/idle.c              |    3 +++
 3 files changed, 45 insertions(+), 2 deletions(-)
kernel/sched/cpufreq_schedutil.c +36 -2
Index: linux-pm/kernel/sched/cpufreq_schedutil.c
===================================================================
@@ -20,6 +20,7 @@
 #include "sched.h"
 
 #define SUGOV_KTHREAD_PRIORITY	50
+#define SUGOV_BUSY_THRESHOLD	(50 * NSEC_PER_MSEC)
 
 struct sugov_tunables {
 	struct gov_attr_set attr_set;
@@ -55,6 +56,9 @@ struct sugov_cpu {
 
 	unsigned long iowait_boost;
 	unsigned long iowait_boost_max;
+	unsigned long idle_calls;
+	unsigned long saved_idle_calls;
+	u64 busy_start;
 	u64 last_update;
 
 	/* The fields below are only needed when sharing a policy. */
@@ -192,6 +196,34 @@ static void sugov_iowait_boost(struct su
 	sg_cpu->iowait_boost >>= 1;
 }
 
+void cpufreq_schedutil_idle_call(void)
+{
+	struct sugov_cpu *sg_cpu = this_cpu_ptr(&sugov_cpu);
+
+	sg_cpu->idle_calls++;
+}
+
+static bool sugov_cpu_is_busy(struct sugov_cpu *sg_cpu)
+{
+	if (sg_cpu->idle_calls != sg_cpu->saved_idle_calls) {
+		sg_cpu->busy_start = 0;
+		return false;
+	}
+
+	if (!sg_cpu->busy_start) {
+		sg_cpu->busy_start = sg_cpu->last_update;
+		return false;
+	}
+
+	return sg_cpu->last_update - sg_cpu->busy_start > SUGOV_BUSY_THRESHOLD;
+}
+
+static void sugov_save_idle_calls(struct sugov_cpu *sg_cpu)
+{
+	if (!sg_cpu->busy_start)
+		sg_cpu->saved_idle_calls = sg_cpu->idle_calls;
+}
+
 static void sugov_update_single(struct update_util_data *hook, u64 time,
 				unsigned int flags)
 {
@@ -207,7 +239,7 @@ static void sugov_update_single(struct u
 	if (!sugov_should_update_freq(sg_policy, time))
 		return;
 
-	if (flags & SCHED_CPUFREQ_RT_DL) {
+	if ((flags & SCHED_CPUFREQ_RT_DL) || sugov_cpu_is_busy(sg_cpu)) {
 		next_f = policy->cpuinfo.max_freq;
 	} else {
 		sugov_get_util(&util, &max);
@@ -215,6 +247,7 @@ static void sugov_update_single(struct u
 		next_f = get_next_freq(sg_policy, util, max);
 	}
 	sugov_update_commit(sg_policy, time, next_f);
+	sugov_save_idle_calls(sg_cpu);
 }
 
 static unsigned int sugov_next_freq_shared(struct sugov_cpu *sg_cpu)
@@ -278,12 +311,13 @@ static void sugov_update_shared(struct u
 	sg_cpu->last_update = time;
 
 	if (sugov_should_update_freq(sg_policy, time)) {
-		if (flags & SCHED_CPUFREQ_RT_DL)
+		if ((flags & SCHED_CPUFREQ_RT_DL) || sugov_cpu_is_busy(sg_cpu))
 			next_f = sg_policy->policy->cpuinfo.max_freq;
 		else
 			next_f = sugov_next_freq_shared(sg_cpu);
 
 		sugov_update_commit(sg_policy, time, next_f);
+		sugov_save_idle_calls(sg_cpu);
 	}
 
 	raw_spin_unlock(&sg_policy->update_lock);
kernel/sched/idle.c +3 -0
Index: linux-pm/kernel/sched/idle.c
===================================================================
@@ -151,6 +151,9 @@ static void cpuidle_idle_call(void)
 	 */
 	rcu_idle_enter();
 
+	/* Notify the schedutil cpufreq governor that we are entering idle. */
+	cpufreq_schedutil_idle_call();
+
 	if (cpuidle_not_available(drv, dev)) {
 		default_idle_call();
 		goto exit_idle;
include/linux/sched/cpufreq.h +6 -0
Index: linux-pm/include/linux/sched/cpufreq.h
===================================================================
@@ -24,4 +24,10 @@ void cpufreq_add_update_util_hook(int cp
 void cpufreq_remove_update_util_hook(int cpu);
 #endif /* CONFIG_CPU_FREQ */
 
+#ifdef CONFIG_CPU_FREQ_GOV_SCHEDUTIL
+extern void cpufreq_schedutil_idle_call(void);
+#else
+static inline void cpufreq_schedutil_idle_call(void) {}
+#endif /* CONFIG_CPU_FREQ_GOV_SCHEDUTIL */
+
 #endif /* _LINUX_SCHED_CPUFREQ_H */
#5 lore raw

Re: [RFC][PATCH 2/2] cpufreq: schedutil: Force max frequency on busy CPUs

Quoted 6 lines · Rafael J. Wysocki

From: Rafael J. Wysocki <[email protected]>

The PELT metric used by the schedutil governor underestimates the CPU utilization in some cases. The reason for that may be time spent in interrupt handlers and similar which is not accounted for by PELT.

Quoted 11 lines

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.

In case you are wondering about the actual numbers, attached are two turbostat log files from two runs of the same workload, without (before.txt) and with (after.txt) the patch applied.

The workload is essentially "make -j 5" in the kernel source tree and the machine has an SSD storage and a quad-core Intel Sandy Bridge processor. The P-states available for each core are between 8 and 31 (0x1f) corresponding to 800 MHz and 3.1 GHz, respectively. All cores can run sustainably at 2.9 GHz at the same time, although that is not a guaranteed sustainable frequency (it may be dropped occasionally for thermal reasons, for example).

The interesting columns are Bzy_MHz (and specifically the rows with "-" under CPU that correspond to the entire processor), which is the avreage frequency between iterations based on the numbers read from feedback registers, and the rightmost one, which is the values written to the P-state request register (the 3rd and 4th hex digits from the right represent the requested P-state).

The turbostat data collection ran every 2 seconds and I looked at the last 30 iterations in each case corresponding to about 1 minute of the workload run during which all of the cores were around 100% busy.

Now, if you look at after.txt (the run with the patch applied), you'll notice that during those last 30 iterations P-state 31 (0x1f) had been requested on all cores pretty much 100% of the time (meaning: as expected in that case) and the average processor frequency (computed by taking the average from all of the 30 "-" rows) was 2899.33 MHz (apparently, the hardware decided to drop it from 2.9 GHz occasionally).

In the before.txt case (without the patch) the average frequency over the last 30 iterations was 2896.90 MHz which is about 0.8% slower than with the patch applied (on the average). That already is quite a measurable difference, but it would have been much worse if the processor had not coordinated P-states in hardware (such that if any core requested 31, the processor would pick that one or close to it for the entire package regardless of the requests from the other cores).

Namely, if you look at the P-states requested for different cores (during the last 30 iterations of the before.txt run), which essentially is what should be used according to the governor, the average of them is 27.25 (almost 4 bins lower than the maximum) and the standard deviation is 6, so it is not like they are a little off occasionally. At least some of them are way off most of the time.

Honestly, if the processor had been capable of doing per-core P-states, that would have been a disaster and there are customers who wouldn't look at schedutil again after being confronted with these numbers.

So this is rather serious.

BTW, both intel_pstate in the active mode and ondemand request 0x1f on all cores for that workload, like in the after.txt case.

Thanks,
Rafael

CPUID(7): No-SGX

     CPU Avg_MHz   Busy% Bzy_MHz TSC_MHz   MSR 0x199
       -      93   11.26     826    2494  0x00000000
       0      61    7.56     808    2494  0x00000800
       2      90   10.87     832    2494  0x00000800
       1     130   15.79     822    2494  0x00000800
       3      90   10.83     837    2494  0x00000800
     CPU Avg_MHz   Busy% Bzy_MHz TSC_MHz   MSR 0x199
       -      54    6.63     811    2494  0x00000000
       0      46    5.68     816    2494  0x00000800
       2      55    6.87     804    2494  0x00000800
       1      79    9.77     806    2494  0x00000800
       3      35    4.20     828    2494  0x00000800
     CPU Avg_MHz   Busy% Bzy_MHz TSC_MHz   MSR 0x199
       -     305   36.58     835    2494  0x00000000
       0     332   39.69     838    2494  0x00000800
       2     307   36.95     833    2494  0x00000800
       1     248   29.82     832    2494  0x00000800
       3     333   39.87     836    2494  0x00000c00
     CPU Avg_MHz   Busy% Bzy_MHz TSC_MHz   MSR 0x199
       -     357   38.08     940    2495  0x00000000
       0     424   36.72    1158    2495  0x00000800
       2     312   36.27     862    2495  0x00000800
       1     407   46.72     874    2494  0x00000800
       3     284   32.62     874    2494  0x00000800
     CPU Avg_MHz   Busy% Bzy_MHz TSC_MHz   MSR 0x199
       -    2017   92.64    2183    2494  0x00000000
       0    2041   94.65    2162    2494  0x00000800
       2    2032   93.65    2175    2494  0x00001200
       1    2024   92.33    2197    2494  0x00001f00
       3    1972   89.92    2198    2494  0x00001f00
     CPU Avg_MHz   Busy% Bzy_MHz TSC_MHz   MSR 0x199
       -    2455   99.21    2480    2494  0x00000000
       0    2450   98.95    2482    2494  0x00000900
       2    2457   99.29    2480    2494  0x00000900
       1    2453   99.11    2481    2494  0x00001000
       3    2461   99.49    2479    2494  0x00000d00
     CPU Avg_MHz   Busy% Bzy_MHz TSC_MHz   MSR 0x199
       -    2308   99.14    2333    2494  0x00000000
       0    2309   99.04    2337    2494  0x00001f00
       2    2307   99.12    2333    2494  0x00001200
       1    2305   99.03    2333    2494  0x00001600
       3    2310   99.39    2329    2494  0x00001f00
     CPU Avg_MHz   Busy% Bzy_MHz TSC_MHz   MSR 0x199
       -    2613   99.22    2639    2494  0x00000000
       0    2614   99.32    2638    2494  0x00000800
       2    2589   98.19    2643    2494  0x00001200
       1    2623   99.60    2640    2494  0x00001800
       3    2624   99.76    2637    2494  0x00000a00
     CPU Avg_MHz   Busy% Bzy_MHz TSC_MHz   MSR 0x199
       -    2687   98.70    2729    2494  0x00000000
       0    2700   99.24    2727    2494  0x00000800
       2    2707   99.48    2728    2494  0x00001f00
       1    2643   97.09    2728    2494  0x00001c00
       3    2699   99.01    2733    2494  0x00001f00
     CPU Avg_MHz   Busy% Bzy_MHz TSC_MHz   MSR 0x199
       -    2893   99.98    2900    2494  0x00000000
       0    2892   99.96    2900    2494  0x00001f00
       2    2892   99.96    2900    2494  0x00001f00
       1    2893  100.00    2900    2494  0x00001f00
       3    2893  100.00    2900    2494  0x00001f00
     CPU Avg_MHz   Busy% Bzy_MHz TSC_MHz   MSR 0x199
       -    2893  100.00    2900    2494  0x00000000
       0    2893  100.00    2900    2494  0x00001b00
       2    2893  100.00    2900    2494  0x00001800
       1    2893  100.00    2900    2494  0x00001f00
       3    2893  100.00    2900    2494  0x00000800
     CPU Avg_MHz   Busy% Bzy_MHz TSC_MHz   MSR 0x199
       -    2893  100.00    2900    2494  0x00000000
       0    2893  100.00    2900    2494  0x00001a00
       2    2893  100.00    2900    2494  0x00000d00
       1    2893  100.00    2900    2494  0x00001f00
       3    2893  100.00    2900    2494  0x00001f00
     CPU Avg_MHz   Busy% Bzy_MHz TSC_MHz   MSR 0x199
       -    2893  100.00    2900    2494  0x00000000
       0    2893  100.00    2900    2494  0x00001900
       2    2893  100.00    2900    2494  0x00001f00
       1    2893  100.00    2900    2494  0x00001f00
       3    2893  100.00    2900    2494  0x00001f00
     CPU Avg_MHz   Busy% Bzy_MHz TSC_MHz   MSR 0x199
       -    2886  100.00    2893    2494  0x00000000
       0    2886  100.00    2893    2494  0x00001f00
       2    2886  100.00    2893    2494  0x00001f00
       1    2886  100.00    2893    2494  0x00001900
       3    2886  100.00    2893    2494  0x00001f00
     CPU Avg_MHz   Busy% Bzy_MHz TSC_MHz   MSR 0x199
       -    2893  100.00    2900    2494  0x00000000
       0    2893  100.00    2900    2494  0x00001b00
       2    2893  100.00    2900    2494  0x00001f00
       1    2893  100.00    2900    2494  0x00001f00
       3    2893  100.00    2900    2494  0x00001f00
     CPU Avg_MHz   Busy% Bzy_MHz TSC_MHz   MSR 0x199
       -    2893  100.00    2900    2494  0x00000000
       0    2893  100.00    2900    2494  0x00001c00
       2    2893  100.00    2900    2494  0x00001f00
       1    2893  100.00    2900    2494  0x00001f00
       3    2893  100.00    2900    2494  0x00001f00
     CPU Avg_MHz   Busy% Bzy_MHz TSC_MHz   MSR 0x199
       -    2893  100.00    2900    2494  0x00000000
       0    2893  100.00    2900    2494  0x00001f00
       2    2893  100.00    2900    2494  0x00001f00
       1    2893  100.00    2900    2494  0x00001900
       3    2893  100.00    2900    2494  0x00001f00
     CPU Avg_MHz   Busy% Bzy_MHz TSC_MHz   MSR 0x199
       -    2890   99.99    2897    2494  0x00000000
       0    2890  100.00    2897    2494  0x00001f00
       2    2890  100.00    2897    2494  0x00001800
       1    2890  100.00    2897    2494  0x00000e00
       3    2889   99.97    2897    2494  0x00001f00
     CPU Avg_MHz   Busy% Bzy_MHz TSC_MHz   MSR 0x199
       -    2893  100.00    2900    2494  0x00000000
       0    2893  100.00    2900    2494  0x00001f00
       2    2893  100.00    2900    2494  0x00001f00
       1    2893  100.00    2900    2494  0x00001f00
       3    2893  100.00    2900    2494  0x00001600
     CPU Avg_MHz   Busy% Bzy_MHz TSC_MHz   MSR 0x199
       -    2892  100.00    2898    2494  0x00000000
       0    2892  100.00    2898    2494  0x00001f00
       2    2892  100.00    2898    2494  0x00001f00
       1    2892  100.00    2898    2494  0x00000a00
       3    2892  100.00    2898    2494  0x00001f00
     CPU Avg_MHz   Busy% Bzy_MHz TSC_MHz   MSR 0x199
       -    2890  100.00    2897    2494  0x00000000
       0    2890  100.00    2897    2494  0x00001f00
       2    2890  100.00    2897    2494  0x00001f00
       1    2890  100.00    2897    2494  0x00001b00
       3    2890  100.00    2897    2494  0x00001f00
     CPU Avg_MHz   Busy% Bzy_MHz TSC_MHz   MSR 0x199
       -    2870   99.94    2878    2494  0x00000000
       0    2872  100.00    2878    2494  0x00001f00
       2    2867   99.84    2878    2494  0x00001f00
       1    2872  100.00    2878    2494  0x00001f00
       3    2870   99.92    2879    2494  0x00001200
     CPU Avg_MHz   Busy% Bzy_MHz TSC_MHz   MSR 0x199
       -    2892  100.00    2898    2494  0x00000000
       0    2892  100.00    2898    2494  0x00000f00
       2    2892  100.00    2898    2494  0x00001b00
       1    2892  100.00    2898    2494  0x00001f00
       3    2892  100.00    2898    2494  0x00001f00
     CPU Avg_MHz   Busy% Bzy_MHz TSC_MHz   MSR 0x199
       -    2891   99.94    2899    2494  0x00000000
       0    2893  100.00    2899    2494  0x00001f00
       2    2887   99.78    2899    2494  0x00001f00
       1    2893  100.00    2899    2494  0x00001f00
       3    2893  100.00    2899    2494  0x00001f00
     CPU Avg_MHz   Busy% Bzy_MHz TSC_MHz   MSR 0x199
       -    2893  100.00    2900    2494  0x00000000
       0    2893  100.00    2900    2494  0x00001d00
       2    2893  100.00    2900    2494  0x00001c00
       1    2893  100.00    2900    2494  0x00001f00
       3    2893  100.00    2900    2494  0x00001f00
     CPU Avg_MHz   Busy% Bzy_MHz TSC_MHz   MSR 0x199
       -    2889  100.00    2895    2494  0x00000000
       0    2889  100.00    2895    2494  0x00001f00
       2    2889  100.00    2895    2494  0x00001600
       1    2889  100.00    2895    2494  0x00001c00
       3    2889  100.00    2895    2494  0x00000b00
     CPU Avg_MHz   Busy% Bzy_MHz TSC_MHz   MSR 0x199
       -    2893  100.00    2900    2494  0x00000000
       0    2893  100.00    2900    2494  0x00001800
       2    2893  100.00    2900    2494  0x00001f00
       1    2893  100.00    2900    2494  0x00001f00
       3    2893  100.00    2900    2494  0x00001f00
     CPU Avg_MHz   Busy% Bzy_MHz TSC_MHz   MSR 0x199
       -    2893  100.00    2900    2494  0x00000000
       0    2893  100.00    2900    2494  0x00001f00
       2    2893  100.00    2900    2494  0x00001600
       1    2893  100.00    2900    2494  0x00001f00
       3    2893  100.00    2900    2494  0x00000800
     CPU Avg_MHz   Busy% Bzy_MHz TSC_MHz   MSR 0x199
       -    2893   99.99    2900    2494  0x00000000
       0    2893  100.00    2900    2494  0x00001f00
       2    2893  100.00    2900    2494  0x00001f00
       1    2893  100.00    2900    2494  0x00001200
       3    2892   99.96    2900    2494  0x00001300
     CPU Avg_MHz   Busy% Bzy_MHz TSC_MHz   MSR 0x199
       -    2891   99.99    2898    2494  0x00000000
       0    2892  100.00    2898    2494  0x00001f00
       2    2891   99.97    2898    2494  0x00001f00
       1    2892  100.00    2898    2494  0x00001f00
       3    2892  100.00    2898    2494  0x00001f00
     CPU Avg_MHz   Busy% Bzy_MHz TSC_MHz   MSR 0x199
       -    2892   99.99    2899    2494  0x00000000
       0    2892  100.00    2899    2494  0x00001f00
       2    2892  100.00    2899    2494  0x00001100
       1    2891   99.96    2899    2494  0x00001f00
       3    2892  100.00    2899    2494  0x00001f00
     CPU Avg_MHz   Busy% Bzy_MHz TSC_MHz   MSR 0x199
       -    2886  100.00    2893    2494  0x00000000
       0    2886  100.00    2893    2494  0x00000f00
       2    2886  100.00    2893    2494  0x00000900
       1    2886  100.00    2893    2494  0x00001300
       3    2886  100.00    2893    2494  0x00001f00
     CPU Avg_MHz   Busy% Bzy_MHz TSC_MHz   MSR 0x199
       -    2892  100.00    2898    2495  0x00000000
       0    2892  100.00    2898    2495  0x00001f00
       2    2892  100.00    2898    2495  0x00001700
       1    2892  100.00    2898    2494  0x00001f00
       3    2892  100.00    2898    2494  0x00001f00
     CPU Avg_MHz   Busy% Bzy_MHz TSC_MHz   MSR 0x199
       -    2893  100.00    2900    2494  0x00000000
       0    2893  100.00    2900    2494  0x00001f00
       2    2893  100.00    2900    2494  0x00001f00
       1    2893  100.00    2900    2494  0x00001700
       3    2893  100.00    2900    2494  0x00001f00
     CPU Avg_MHz   Busy% Bzy_MHz TSC_MHz   MSR 0x199
       -    2893  100.00    2900    2494  0x00000000
       0    2893  100.00    2900    2494  0x00000d00
       2    2893  100.00    2900    2494  0x00001f00
       1    2893  100.00    2900    2494  0x00001f00
       3    2893  100.00    2900    2494  0x00001f00
     CPU Avg_MHz   Busy% Bzy_MHz TSC_MHz   MSR 0x199
       -    2892  100.00    2899    2494  0x00000000
       0    2892   99.98    2899    2494  0x00001f00
       2    2892  100.00    2899    2494  0x00001f00
       1    2892  100.00    2899    2494  0x00001f00
       3    2892  100.00    2899    2494  0x00001f00
     CPU Avg_MHz   Busy% Bzy_MHz TSC_MHz   MSR 0x199
       -    2879  100.00    2885    2494  0x00000000
       0    2879  100.00    2885    2494  0x00001500
       2    2879  100.00    2885    2494  0x00000900
       1    2878   99.99    2885    2494  0x00001f00
       3    2879  100.00    2885    2494  0x00001f00
     CPU Avg_MHz   Busy% Bzy_MHz TSC_MHz   MSR 0x199
       -    2883  100.00    2889    2494  0x00000000
       0    2883  100.00    2889    2494  0x00001f00
       2    2883  100.00    2889    2494  0x00001f00
       1    2883  100.00    2889    2494  0x00001f00
       3    2883  100.00    2889    2494  0x00001600
     CPU Avg_MHz   Busy% Bzy_MHz TSC_MHz   MSR 0x199
       -    2893  100.00    2900    2494  0x00000000
       0    2893  100.00    2900    2494  0x00001f00
       2    2893  100.00    2900    2494  0x00001400
       1    2893  100.00    2900    2494  0x00001b00
       3    2893  100.00    2900    2494  0x00001f00
     CPU Avg_MHz   Busy% Bzy_MHz TSC_MHz   MSR 0x199
       -    2893  100.00    2900    2494  0x00000000
       0    2893  100.00    2900    2494  0x00001100
       2    2893  100.00    2900    2494  0x00001000
       1    2893  100.00    2900    2494  0x00001f00
       3    2893   99.98    2900    2494  0x00001f00
     CPU Avg_MHz   Busy% Bzy_MHz TSC_MHz   MSR 0x199
       -    2893  100.00    2900    2494  0x00000000
       0    2893  100.00    2900    2494  0x00001300
       2    2893  100.00    2900    2494  0x00001f00
       1    2893  100.00    2900    2494  0x00001500
       3    2893  100.00    2900    2494  0x00001f00
     CPU Avg_MHz   Busy% Bzy_MHz TSC_MHz   MSR 0x199
       -    2893  100.00    2899    2494  0x00000000
       0    2893  100.00    2899    2494  0x00001f00
       2    2893  100.00    2899    2494  0x00001c00
       1    2893  100.00    2899    2494  0x00001f00
       3    2893  100.00    2899    2494  0x00001c00
     CPU Avg_MHz   Busy% Bzy_MHz TSC_MHz   MSR 0x199
       -    2893  100.00    2900    2494  0x00000000
       0    2893  100.00    2900    2494  0x00001f00
       2    2893  100.00    2900    2494  0x00001f00
       1    2893  100.00    2900    2494  0x00001b00
       3    2893  100.00    2900    2494  0x00001d00
     CPU Avg_MHz   Busy% Bzy_MHz TSC_MHz   MSR 0x199
       -    2879  100.00    2886    2494  0x00000000
       0    2879  100.00    2886    2494  0x00001200
       2    2879  100.00    2886    2494  0x00001f00
       1    2879  100.00    2886    2494  0x00001f00
       3    2879  100.00    2886    2494  0x00001f00
     CPU Avg_MHz   Busy% Bzy_MHz TSC_MHz   MSR 0x199
       -    2893   99.99    2900    2494  0x00000000
       0    2893  100.00    2900    2494  0x00001f00
       2    2893  100.00    2900    2494  0x00001f00
       1    2892   99.98    2900    2494  0x00001300
       3    2893  100.00    2900    2494  0x00001000
     CPU Avg_MHz   Busy% Bzy_MHz TSC_MHz   MSR 0x199
       -    2892  100.00    2898    2495  0x00000000
       0    2892  100.00    2898    2495  0x00001f00
       2    2892  100.00    2898    2495  0x00000800
       1    2892  100.00    2898    2495  0x00001b00
       3    2892  100.00    2898    2494  0x00001600
     CPU Avg_MHz   Busy% Bzy_MHz TSC_MHz   MSR 0x199
       -    2893   99.99    2900    2494  0x00000000
       0    2893  100.00    2900    2494  0x00001f00
       2    2893  100.00    2900    2494  0x00001b00
       1    2892   99.96    2900    2494  0x00001f00
       3    2893   99.99    2900    2494  0x00001f00
     CPU Avg_MHz   Busy% Bzy_MHz TSC_MHz   MSR 0x199
       -    2891   99.99    2898    2494  0x00000000
       0    2892  100.00    2898    2494  0x00001a00
       2    2892  100.00    2898    2494  0x00001f00
       1    2892  100.00    2898    2494  0x00001f00
       3    2890   99.95    2898    2494  0x00001f00
     CPU Avg_MHz   Busy% Bzy_MHz TSC_MHz   MSR 0x199
       -    2893  100.00    2900    2494  0x00000000
       0    2893  100.00    2900    2494  0x00001600
       2    2893  100.00    2900    2494  0x00001100
       1    2893  100.00    2900    2494  0x00001f00
       3    2893  100.00    2900    2494  0x00001f00
     CPU Avg_MHz   Busy% Bzy_MHz TSC_MHz   MSR 0x199
       -    2892  100.00    2898    2494  0x00000000
       0    2892  100.00    2898    2494  0x00001f00
       2    2892  100.00    2898    2494  0x00001f00
       1    2892  100.00    2898    2494  0x00001f00
       3    2892  100.00    2898    2494  0x00001c00
CPUID(7): No-SGX
     CPU Avg_MHz   Busy% Bzy_MHz TSC_MHz   MSR 0x199
       -     171   20.00     854    2496  0x00000000
       0     182   21.23     858    2496  0x00000800
       2     170   19.93     855    2496  0x00000800
       1     173   20.54     845    2496  0x00000800
       3     157   18.32     858    2494  0x00000800
     CPU Avg_MHz   Busy% Bzy_MHz TSC_MHz   MSR 0x199
       -     222   25.57     872    2494  0x00000000
       0     210   23.66     889    2494  0x00000800
       2     200   23.20     865    2494  0x00000800
       1     189   21.33     886    2494  0x00000800
       3     291   34.09     856    2494  0x00000800
     CPU Avg_MHz   Busy% Bzy_MHz TSC_MHz   MSR 0x199
       -     132   16.37     808    2494  0x00000000
       0     131   16.19     811    2494  0x00000800
       2     215   26.87     801    2494  0x00000800
       1     107   13.33     803    2494  0x00000800
       3      76    9.08     834    2494  0x00000800
     CPU Avg_MHz   Busy% Bzy_MHz TSC_MHz   MSR 0x199
       -     503   47.44    1063    2494  0x00000000
       0     510   48.60    1051    2494  0x00000a00
       2     482   45.17    1070    2494  0x00000800
       1     512   48.94    1048    2494  0x00000a00
       3     508   47.06    1082    2494  0x00000800
     CPU Avg_MHz   Busy% Bzy_MHz TSC_MHz   MSR 0x199
       -     447   40.48    1108    2494  0x00000000
       0     444   39.85    1117    2494  0x00000800
       2     422   38.66    1093    2494  0x00000a00
       1     440   38.68    1140    2494  0x00000800
       3     484   44.75    1084    2494  0x00000a00
     CPU Avg_MHz   Busy% Bzy_MHz TSC_MHz   MSR 0x199
       -     405   34.24    1185    2494  0x00000000
       0     422   33.63    1258    2494  0x00000800
       2     435   35.97    1211    2494  0x00000800
       1     296   26.52    1118    2494  0x00000800
       3     468   40.85    1147    2494  0x00000800
     CPU Avg_MHz   Busy% Bzy_MHz TSC_MHz   MSR 0x199
       -     155   19.07     813    2494  0x00000000
       0     161   19.92     809    2494  0x00000800
       2     156   19.31     810    2494  0x00000800
       1     119   14.79     809    2494  0x00000800
       3     183   22.26     823    2494  0x00000800
     CPU Avg_MHz   Busy% Bzy_MHz TSC_MHz   MSR 0x199
       -     632   36.32    1744    2494  0x00000000
       0    1237   58.35    2125    2494  0x00001f00
       2     503   31.16    1617    2494  0x00000800
       1     396   28.29    1403    2494  0x00000800
       3     392   27.50    1430    2494  0x00000800
     CPU Avg_MHz   Busy% Bzy_MHz TSC_MHz   MSR 0x199
       -     799   36.92    2168    2494  0x00000000
       0    1935   77.42    2505    2494  0x00000800
       2     386   22.32    1733    2494  0x00000800
       1     474   25.39    1870    2494  0x00000800
       3     400   22.53    1778    2494  0x00000800
     CPU Avg_MHz   Busy% Bzy_MHz TSC_MHz   MSR 0x199
       -    1856   82.07    2267    2494  0x00000000
       0    1850   82.38    2251    2494  0x00000900
       2    1836   81.44    2260    2494  0x00001500
       1    1850   80.66    2299    2494  0x00001700
       3    1889   83.80    2259    2494  0x00000a00
     CPU Avg_MHz   Busy% Bzy_MHz TSC_MHz   MSR 0x199
       -    2601   95.10    2741    2494  0x00000000
       0    2625   96.00    2740    2494  0x00001200
       2    2573   93.94    2746    2494  0x00001000
       1    2635   96.58    2734    2494  0x00000c00
       3    2570   93.87    2744    2494  0x00000b00
     CPU Avg_MHz   Busy% Bzy_MHz TSC_MHz   MSR 0x199
       -    2488   92.91    2684    2494  0x00000000
       0    2475   92.17    2692    2494  0x00000b00
       2    2525   94.49    2678    2494  0x00001c00
       1    2506   93.54    2685    2494  0x00001f00
       3    2446   91.43    2682    2494  0x00000800
     CPU Avg_MHz   Busy% Bzy_MHz TSC_MHz   MSR 0x199
       -    2508   94.52    2659    2494  0x00000000
       0    2551   96.11    2661    2494  0x00001200
       2    2525   95.10    2661    2494  0x00001f00
       1    2531   95.48    2657    2494  0x00001f00
       3    2423   91.40    2657    2494  0x00001400
     CPU Avg_MHz   Busy% Bzy_MHz TSC_MHz   MSR 0x199
       -    2616   94.99    2760    2494  0x00000000
       0    2577   93.80    2754    2494  0x00001f00
       2    2653   96.61    2753    2494  0x00001f00
       1    2606   94.38    2767    2494  0x00001f00
       3    2626   95.17    2765    2494  0x00001f00
     CPU Avg_MHz   Busy% Bzy_MHz TSC_MHz   MSR 0x199
       -    2851   98.63    2897    2494  0x00000000
       0    2823   97.66    2897    2494  0x00000f00
       2    2854   98.71    2898    2494  0x00001600
       1    2881   99.69    2897    2494  0x00001f00
       3    2845   98.44    2897    2494  0x00001900
     CPU Avg_MHz   Busy% Bzy_MHz TSC_MHz   MSR 0x199
       -    2871   99.23    2900    2494  0x00000000
       0    2884   99.68    2900    2494  0x00001900
       2    2868   99.11    2900    2494  0x00001f00
       1    2875   99.35    2900    2494  0x00001f00
       3    2858   98.78    2900    2494  0x00001f00
     CPU Avg_MHz   Busy% Bzy_MHz TSC_MHz   MSR 0x199
       -    2892   99.96    2900    2494  0x00000000
       0    2893   99.97    2900    2494  0x00001f00
       2    2892   99.93    2900    2494  0x00001f00
       1    2893  100.00    2900    2494  0x00001f00
       3    2892   99.94    2900    2494  0x00001f00
     CPU Avg_MHz   Busy% Bzy_MHz TSC_MHz   MSR 0x199
       -    2893   99.99    2900    2494  0x00000000
       0    2893  100.00    2900    2494  0x00001f00
       2    2893  100.00    2900    2494  0x00001f00
       1    2893  100.00    2900    2494  0x00001f00
       3    2893   99.97    2900    2494  0x00001f00
     CPU Avg_MHz   Busy% Bzy_MHz TSC_MHz   MSR 0x199
       -    2893  100.00    2900    2494  0x00000000
       0    2893  100.00    2900    2494  0x00001f00
       2    2893  100.00    2900    2494  0x00001f00
       1    2893  100.00    2900    2494  0x00001f00
       3    2893  100.00    2900    2494  0x00001f00
     CPU Avg_MHz   Busy% Bzy_MHz TSC_MHz   MSR 0x199
       -    2892  100.00    2898    2494  0x00000000
       0    2892  100.00    2898    2494  0x00001f00
       2    2892  100.00    2898    2494  0x00001f00
       1    2892  100.00    2898    2494  0x00001f00
       3    2892  100.00    2898    2494  0x00001f00
     CPU Avg_MHz   Busy% Bzy_MHz TSC_MHz   MSR 0x199
       -    2893   99.99    2900    2494  0x00000000
       0    2893  100.00    2900    2494  0x00001f00
       2    2893  100.00    2900    2494  0x00001f00
       1    2893  100.00    2900    2494  0x00001f00
       3    2893   99.97    2900    2494  0x00001f00
     CPU Avg_MHz   Busy% Bzy_MHz TSC_MHz   MSR 0x199
       -    2892  100.00    2898    2494  0x00000000
       0    2892  100.00    2898    2494  0x00001f00
       2    2892  100.00    2898    2494  0x00001f00
       1    2892  100.00    2898    2494  0x00001f00
       3    2892  100.00    2898    2494  0x00001f00
     CPU Avg_MHz   Busy% Bzy_MHz TSC_MHz   MSR 0x199
       -    2893  100.00    2900    2494  0x00000000
       0    2893  100.00    2900    2494  0x00001f00
       2    2893  100.00    2900    2494  0x00001f00
       1    2893  100.00    2900    2494  0x00001f00
       3    2893  100.00    2900    2494  0x00001f00
     CPU Avg_MHz   Busy% Bzy_MHz TSC_MHz   MSR 0x199
       -    2893  100.00    2900    2494  0x00000000
       0    2893  100.00    2900    2494  0x00001f00
       2    2893  100.00    2900    2494  0x00001f00
       1    2893  100.00    2900    2494  0x00001f00
       3    2893  100.00    2900    2494  0x00001f00
     CPU Avg_MHz   Busy% Bzy_MHz TSC_MHz   MSR 0x199
       -    2893   99.99    2900    2494  0x00000000
       0    2893  100.00    2900    2494  0x00001f00
       2    2893  100.00    2900    2494  0x00001f00
       1    2893  100.00    2900    2494  0x00001f00
       3    2893   99.98    2900    2494  0x00001f00
     CPU Avg_MHz   Busy% Bzy_MHz TSC_MHz   MSR 0x199
       -    2892  100.00    2898    2495  0x00000000
       0    2892  100.00    2898    2495  0x00001f00
       2    2892  100.00    2898    2495  0x00001f00
       1    2892  100.00    2898    2495  0x00001f00
       3    2892  100.00    2898    2494  0x00001f00
     CPU Avg_MHz   Busy% Bzy_MHz TSC_MHz   MSR 0x199
       -    2892  100.00    2898    2494  0x00000000
       0    2892  100.00    2898    2494  0x00001f00
       2    2892  100.00    2898    2494  0x00001f00
       1    2892  100.00    2898    2494  0x00001f00
       3    2892  100.00    2898    2494  0x00001f00
     CPU Avg_MHz   Busy% Bzy_MHz TSC_MHz   MSR 0x199
       -    2893  100.00    2900    2494  0x00000000
       0    2893  100.00    2900    2494  0x00001f00
       2    2893  100.00    2900    2494  0x00001f00
       1    2893  100.00    2900    2494  0x00001f00
       3    2893  100.00    2900    2494  0x00001f00
     CPU Avg_MHz   Busy% Bzy_MHz TSC_MHz   MSR 0x199
       -    2893   99.99    2900    2494  0x00000000
       0    2893  100.00    2900    2494  0x00001f00
       2    2893  100.00    2900    2494  0x00001f00
       1    2893  100.00    2900    2494  0x00001f00
       3    2892   99.94    2900    2494  0x00001f00
     CPU Avg_MHz   Busy% Bzy_MHz TSC_MHz   MSR 0x199
       -    2893  100.00    2900    2494  0x00000000
       0    2893  100.00    2900    2494  0x00001f00
       2    2893  100.00    2900    2494  0x00001f00
       1    2893  100.00    2900    2494  0x00001f00
       3    2893  100.00    2900    2494  0x00001f00
     CPU Avg_MHz   Busy% Bzy_MHz TSC_MHz   MSR 0x199
       -    2893  100.00    2900    2494  0x00000000
       0    2893  100.00    2900    2494  0x00001f00
       2    2893  100.00    2900    2494  0x00001f00
       1    2893  100.00    2900    2494  0x00001f00
       3    2893  100.00    2900    2494  0x00001f00
     CPU Avg_MHz   Busy% Bzy_MHz TSC_MHz   MSR 0x199
       -    2891   99.99    2898    2494  0x00000000
       0    2892  100.00    2898    2494  0x00001f00
       2    2892  100.00    2898    2494  0x00001f00
       1    2890   99.95    2898    2494  0x00001f00
       3    2892  100.00    2898    2494  0x00001f00
     CPU Avg_MHz   Busy% Bzy_MHz TSC_MHz   MSR 0x199
       -    2893   99.99    2900    2494  0x00000000
       0    2892   99.96    2900    2494  0x00001f00
       2    2893  100.00    2900    2494  0x00001f00
       1    2893  100.00    2900    2494  0x00001f00
       3    2893  100.00    2900    2494  0x00001f00
     CPU Avg_MHz   Busy% Bzy_MHz TSC_MHz   MSR 0x199
       -    2893   99.98    2900    2494  0x00000000
       0    2892   99.97    2900    2494  0x00001f00
       2    2893  100.00    2900    2494  0x00001f00
       1    2892   99.95    2900    2494  0x00001f00
       3    2893  100.00    2900    2494  0x00001f00
     CPU Avg_MHz   Busy% Bzy_MHz TSC_MHz   MSR 0x199
       -    2893  100.00    2900    2494  0x00000000
       0    2893  100.00    2900    2494  0x00001f00
       2    2893  100.00    2900    2494  0x00001f00
       1    2893  100.00    2900    2494  0x00001f00
       3    2893  100.00    2900    2494  0x00001f00
     CPU Avg_MHz   Busy% Bzy_MHz TSC_MHz   MSR 0x199
       -    2893   99.99    2900    2494  0x00000000
       0    2893  100.00    2900    2494  0x00001f00
       2    2893   99.98    2900    2494  0x00001f00
       1    2893  100.00    2900    2494  0x00001f00
       3    2893  100.00    2900    2494  0x00001f00
     CPU Avg_MHz   Busy% Bzy_MHz TSC_MHz   MSR 0x199
       -    2894  100.00    2900    2494  0x00000000
       0    2894  100.00    2900    2495  0x00001f00
       2    2893  100.00    2900    2494  0x00001f00
       1    2893  100.00    2900    2494  0x00001f00
       3    2893  100.00    2900    2494  0x00001f00
     CPU Avg_MHz   Busy% Bzy_MHz TSC_MHz   MSR 0x199
       -    2893  100.00    2900    2494  0x00000000
       0    2893  100.00    2900    2494  0x00001f00
       2    2893  100.00    2900    2494  0x00001f00
       1    2893  100.00    2900    2494  0x00001f00
       3    2893  100.00    2900    2494  0x00001f00
     CPU Avg_MHz   Busy% Bzy_MHz TSC_MHz   MSR 0x199
       -    2890  100.00    2897    2494  0x00000000
       0    2890  100.00    2897    2494  0x00001f00
       2    2890  100.00    2897    2494  0x00001f00
       1    2890  100.00    2897    2494  0x00001f00
       3    2890  100.00    2897    2494  0x00001f00
     CPU Avg_MHz   Busy% Bzy_MHz TSC_MHz   MSR 0x199
       -    2893  100.00    2900    2494  0x00000000
       0    2893  100.00    2900    2494  0x00001f00
       2    2893  100.00    2900    2494  0x00001f00
       1    2893  100.00    2900    2494  0x00001f00
       3    2893  100.00    2900    2494  0x00001f00
     CPU Avg_MHz   Busy% Bzy_MHz TSC_MHz   MSR 0x199
       -    2893   99.99    2900    2494  0x00000000
       0    2893  100.00    2900    2494  0x00001f00
       2    2893  100.00    2900    2494  0x00001f00
       1    2893  100.00    2900    2494  0x00001f00
       3    2892   99.96    2900    2494  0x00001f00
     CPU Avg_MHz   Busy% Bzy_MHz TSC_MHz   MSR 0x199
       -    2893  100.00    2900    2494  0x00000000
       0    2893  100.00    2900    2494  0x00001f00
       2    2893  100.00    2900    2494  0x00001f00
       1    2893  100.00    2900    2494  0x00001f00
       3    2893  100.00    2900    2494  0x00001f00
     CPU Avg_MHz   Busy% Bzy_MHz TSC_MHz   MSR 0x199
       -    2888   99.99    2895    2494  0x00000000
       0    2888  100.00    2895    2494  0x00001f00
       2    2888  100.00    2895    2494  0x00001f00
       1    2888  100.00    2895    2494  0x00001f00
       3    2888   99.98    2895    2494  0x00001f00
     CPU Avg_MHz   Busy% Bzy_MHz TSC_MHz   MSR 0x199
       -    2893  100.00    2900    2494  0x00000000
       0    2893  100.00    2900    2494  0x00001f00
       2    2893  100.00    2900    2494  0x00001f00
       1    2893  100.00    2900    2494  0x00001f00
       3    2893  100.00    2900    2494  0x00001f00
     CPU Avg_MHz   Busy% Bzy_MHz TSC_MHz   MSR 0x199
       -    2892  100.00    2898    2494  0x00000000
       0    2892  100.00    2898    2494  0x00001f00
       2    2892  100.00    2898    2494  0x00001f00
       1    2892  100.00    2898    2494  0x00001f00
       3    2892  100.00    2898    2494  0x00001f00
     CPU Avg_MHz   Busy% Bzy_MHz TSC_MHz   MSR 0x199
       -    2892  100.00    2898    2494  0x00000000
       0    2892  100.00    2898    2494  0x00001f00
       2    2892  100.00    2898    2494  0x00001f00
       1    2892  100.00    2898    2494  0x00001f00
       3    2892  100.00    2898    2494  0x00001f00
     CPU Avg_MHz   Busy% Bzy_MHz TSC_MHz   MSR 0x199
       -    2893  100.00    2900    2494  0x00000000
       0    2893  100.00    2900    2494  0x00001f00
       2    2893  100.00    2900    2494  0x00001f00
       1    2893  100.00    2900    2494  0x00001f00
       3    2893  100.00    2900    2494  0x00001f00
     CPU Avg_MHz   Busy% Bzy_MHz TSC_MHz   MSR 0x199
       -    2892  100.00    2898    2494  0x00000000
       0    2892  100.00    2898    2494  0x00001f00
       2    2892  100.00    2898    2494  0x00001f00
       1    2892  100.00    2898    2494  0x00001f00
       3    2892  100.00    2898    2494  0x00001f00
     CPU Avg_MHz   Busy% Bzy_MHz TSC_MHz   MSR 0x199
       -    2893  100.00    2900    2494  0x00000000
       0    2893  100.00    2900    2494  0x00001f00
       2    2893  100.00    2900    2494  0x00001f00
       1    2893  100.00    2900    2494  0x00001f00
       3    2893  100.00    2900    2494  0x00001f00
     CPU Avg_MHz   Busy% Bzy_MHz TSC_MHz   MSR 0x199
       -    2893  100.00    2900    2494  0x00000000
       0    2893  100.00    2900    2494  0x00001f00
       2    2893  100.00    2900    2494  0x00001f00
       1    2893  100.00    2900    2494  0x00001f00
       3    2893  100.00    2900    2494  0x00001f00
     CPU Avg_MHz   Busy% Bzy_MHz TSC_MHz   MSR 0x199
       -    2893  100.00    2900    2494  0x00000000
       0    2893  100.00    2900    2494  0x00001f00
       2    2893  100.00    2900    2494  0x00001f00
       1    2893  100.00    2900    2494  0x00001f00
       3    2893  100.00    2900    2494  0x00001f00
     CPU Avg_MHz   Busy% Bzy_MHz TSC_MHz   MSR 0x199
       -    2893  100.00    2900    2494  0x00000000
       0    2893  100.00    2900    2494  0x00001f00
       2    2893  100.00    2900    2494  0x00001f00
       1    2893  100.00    2900    2494  0x00001f00
       3    2893  100.00    2900    2494  0x00001f00
     CPU Avg_MHz   Busy% Bzy_MHz TSC_MHz   MSR 0x199
       -    2893  100.00    2900    2494  0x00000000
       0    2893  100.00    2900    2494  0x00001f00
       2    2893  100.00    2900    2494  0x00001f00
       1    2893   99.99    2900    2494  0x00001f00
       3    2893  100.00    2900    2494  0x00001f00
     CPU Avg_MHz   Busy% Bzy_MHz TSC_MHz   MSR 0x199
       -    2892   99.96    2900    2494  0x00000000
       0    2889   99.84    2900    2494  0x00001f00
       2    2893  100.00    2900    2494  0x00001f00
       1    2893  100.00    2900    2494  0x00001f00
       3    2893  100.00    2900    2494  0x00001f00
     CPU Avg_MHz   Busy% Bzy_MHz TSC_MHz   MSR 0x199
       -    2893  100.00    2900    2494  0x00000000
       0    2893  100.00    2900    2494  0x00001f00
       2    2893  100.00    2900    2494  0x00001f00
       1    2893  100.00    2900    2494  0x00001f00
       3    2893  100.00    2900    2494  0x00001f00
     CPU Avg_MHz   Busy% Bzy_MHz TSC_MHz   MSR 0x199
       -    2893  100.00    2900    2494  0x00000000
       0    2893  100.00    2900    2494  0x00001f00
       2    2893  100.00    2900    2494  0x00001f00
       1    2893  100.00    2900    2494  0x00001f00
       3    2893  100.00    2900    2494  0x00001f00
     CPU Avg_MHz   Busy% Bzy_MHz TSC_MHz   MSR 0x199
       -    2886   99.81    2898    2494  0x00000000
       0    2892  100.00    2898    2494  0x00001f00
       2    2890   99.95    2898    2494  0x00001f00
       1    2872   99.30    2898    2494  0x00001f00
       3    2891   99.98    2898    2494  0x00001f00
     CPU Avg_MHz   Busy% Bzy_MHz TSC_MHz   MSR 0x199
       -    2889   99.90    2898    2494  0x00000000
       0    2891   99.98    2898    2494  0x00001f00
       2    2892  100.00    2898    2494  0x00001f00
       1    2880   99.61    2898    2494  0x00001f00
       3    2892  100.00    2898    2494  0x00001f00
#6 lore raw

Re: [RFC][PATCH 2/2] cpufreq: schedutil: Force max frequency on busy CPUs

Quoted 47 lines · Rafael J. Wysocki
Quoted 6 lines · Rafael J. Wysocki

From: Rafael J. Wysocki <[email protected]>

The PELT metric used by the schedutil governor underestimates the CPU utilization in some cases. The reason for that may be time spent in interrupt handlers and similar which is not accounted for by PELT.

Quoted 11 lines

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.

In case you are wondering about the actual numbers, attached are two turbostat log files from two runs of the same workload, without (before.txt) and with (after.txt) the patch applied.

The workload is essentially "make -j 5" in the kernel source tree and the machine has an SSD storage and a quad-core Intel Sandy Bridge processor. The P-states available for each core are between 8 and 31 (0x1f) corresponding to 800 MHz and 3.1 GHz, respectively. All cores can run sustainably at 2.9 GHz at the same time, although that is not a guaranteed sustainable frequency (it may be dropped occasionally for thermal reasons, for example).

The interesting columns are Bzy_MHz (and specifically the rows with "-" under CPU that correspond to the entire processor), which is the avreage frequency between iterations based on the numbers read from feedback registers, and the rightmost one, which is the values written to the P-state request register (the 3rd and 4th hex digits from the right represent the requested P-state).

The turbostat data collection ran every 2 seconds and I looked at the last 30 iterations in each case corresponding to about 1 minute of the workload run during which all of the cores were around 100% busy.

Now, if you look at after.txt (the run with the patch applied), you'll notice that during those last 30 iterations P-state 31 (0x1f) had been requested on all cores pretty much 100% of the time (meaning: as expected in that case) and the average processor frequency (computed by taking the average from all of the 30 "-" rows) was 2899.33 MHz (apparently, the hardware decided to drop it from 2.9 GHz occasionally).

Quoted 3 lines

In the before.txt case (without the patch) the average frequency over the last 30 iterations was 2896.90 MHz which is about 0.8% slower than with the patch applied (on the average).

0.08% of course, sorry. Still visible, though. :-)

Thanks,
Rafael

maintainer
#7 lore raw

Re: [RFC][PATCH 2/2] cpufreq: schedutil: Force max frequency on busy CPUs

In reply to Rafael J. Wysocki
Quoted 3 lines · Rafael J. Wysocki

Honestly, if the processor had been capable of doing per-core P-states, that would have been a disaster and there are customers who wouldn't look at schedutil again after being confronted with these numbers.

This, I feel, is a bit overstated. We have bug, we fix them.

#8 lore raw

Re: [RFC][PATCH 2/2] cpufreq: schedutil: Force max frequency on busy CPUs

Quoted 7 lines · Peter Zijlstra
Quoted 3 lines · Rafael J. Wysocki

Honestly, if the processor had been capable of doing per-core P-states, that would have been a disaster and there are customers who wouldn't look at schedutil again after being confronted with these numbers.

This, I feel, is a bit overstated.

Maybe a bit, but I'm not really sure ...

Quoted 1 line

We have bug, we fix them.

Of course. :-)

maintainer
#9 lore raw

Re: [RFC][PATCH 2/2] cpufreq: schedutil: Force max frequency on busy CPUs

In reply to Rafael J. Wysocki
Quoted 22 lines · Rafael J. Wysocki

From: Rafael J. Wysocki <[email protected]>

The PELT metric used by the schedutil governor underestimates the CPU utilization in some cases. The reason for that may be time spent in interrupt handlers and similar which is not accounted for by PELT.

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.

To work around this issue use the observation that, from the schedutil governor's perspective, CPUs that are never idle should always run at the maximum frequency and make that happen.

Quoted 9 lines

To that end, add a counter of idle calls to struct sugov_cpu and modify cpuidle_idle_call() to increment that counter every time it is about to put the given CPU into an idle state. Next, make the schedutil governor look at that counter for the current CPU every time before it is about to start heavy computations. If the counter has not changed for over SUGOV_BUSY_THRESHOLD time (equal to 50 ms), the CPU has not been idle for at least that long and the governor will choose the maximum frequency for it without looking at the PELT metric at all.

Looks like we are fixing a PELT problem with a schedutil Hack :)

Quoted 49 lines
---
 include/linux/sched/cpufreq.h    |    6 ++++++
 kernel/sched/cpufreq_schedutil.c |   38 ++++++++++++++++++++++++++++++++++++--
 kernel/sched/idle.c              |    3 +++
 3 files changed, 45 insertions(+), 2 deletions(-)

Index: linux-pm/kernel/sched/cpufreq_schedutil.c
===================================================================

kernel/sched/cpufreq_schedutil.c +20 -0
@@ -20,6 +20,7 @@
 #include "sched.h"
 
 #define SUGOV_KTHREAD_PRIORITY	50
+#define SUGOV_BUSY_THRESHOLD	(50 * NSEC_PER_MSEC)
 
 struct sugov_tunables {
 	struct gov_attr_set attr_set;
@@ -55,6 +56,9 @@ struct sugov_cpu {
 
 	unsigned long iowait_boost;
 	unsigned long iowait_boost_max;
+	unsigned long idle_calls;
+	unsigned long saved_idle_calls;
+	u64 busy_start;
 	u64 last_update;
 
 	/* The fields below are only needed when sharing a policy. */
@@ -192,6 +196,34 @@ static void sugov_iowait_boost(struct su
 	sg_cpu->iowait_boost >>= 1;
 }
 
+void cpufreq_schedutil_idle_call(void)
+{
+	struct sugov_cpu *sg_cpu = this_cpu_ptr(&sugov_cpu);
+
+	sg_cpu->idle_calls++;
+}
+
+static bool sugov_cpu_is_busy(struct sugov_cpu *sg_cpu)
+{
+	if (sg_cpu->idle_calls != sg_cpu->saved_idle_calls) {
+		sg_cpu->busy_start = 0;
+		return false;
+	}
+
+	if (!sg_cpu->busy_start) {
+		sg_cpu->busy_start = sg_cpu->last_update;

kernel/sched/cpufreq_schedutil.c static void sugov_iowait_boost(struct su · line 224

Quoted 10 lines
kernel/sched/cpufreq_schedutil.c +10 -0
+		return false;
+	}
+
+	return sg_cpu->last_update - sg_cpu->busy_start > SUGOV_BUSY_THRESHOLD;
+}
+
+static void sugov_save_idle_calls(struct sugov_cpu *sg_cpu)
+{
+	if (!sg_cpu->busy_start)
+		sg_cpu->saved_idle_calls = sg_cpu->idle_calls;

Why aren't we doing this in sugov_cpu_is_busy() itself ? And isn't it possible for idle_calls to get incremented by this time?

Quoted 23 lines
kernel/sched/cpufreq_schedutil.c +4 -1
+}
+
 static void sugov_update_single(struct update_util_data *hook, u64 time,
 				unsigned int flags)
 {
@@ -207,7 +239,7 @@ static void sugov_update_single(struct u
 	if (!sugov_should_update_freq(sg_policy, time))
 		return;
 
-	if (flags & SCHED_CPUFREQ_RT_DL) {
+	if ((flags & SCHED_CPUFREQ_RT_DL) || sugov_cpu_is_busy(sg_cpu)) {
 		next_f = policy->cpuinfo.max_freq;
 	} else {
 		sugov_get_util(&util, &max);
@@ -215,6 +247,7 @@ static void sugov_update_single(struct u
 		next_f = get_next_freq(sg_policy, util, max);
 	}
 	sugov_update_commit(sg_policy, time, next_f);
+	sugov_save_idle_calls(sg_cpu);
 }
 
 static unsigned int sugov_next_freq_shared(struct sugov_cpu *sg_cpu)

kernel/sched/cpufreq_schedutil.c static void sugov_update_shared(struct u · line 314

Quoted 5 lines
kernel/sched/cpufreq_schedutil.c +1 -1
@@ -278,12 +311,13 @@ static void sugov_update_shared(struct u
 	sg_cpu->last_update = time;
 
 	if (sugov_should_update_freq(sg_policy, time)) {
-		if (flags & SCHED_CPUFREQ_RT_DL)
+		if ((flags & SCHED_CPUFREQ_RT_DL) || sugov_cpu_is_busy(sg_cpu))

What about others CPUs in this policy?

Quoted 9 lines
kernel/sched/cpufreq_schedutil.c +1 -0
 			next_f = sg_policy->policy->cpuinfo.max_freq;
 		else
 			next_f = sugov_next_freq_shared(sg_cpu);
 
 		sugov_update_commit(sg_policy, time, next_f);
+		sugov_save_idle_calls(sg_cpu);
 	}
 
 	raw_spin_unlock(&sg_policy->update_lock);
Signature · 1 line
viresh
maintainer
#10 lore raw

Re: [RFC][PATCH 2/2] cpufreq: schedutil: Force max frequency on busy CPUs

Quoted 6 lines · Viresh Kumar
Quoted 5 lines · Rafael J. Wysocki

From: Rafael J. Wysocki <[email protected]>

The PELT metric used by the schedutil governor underestimates the CPU utilization in some cases. The reason for that may be time spent in interrupt handlers and similar which is not accounted for by PELT.

Are you sure of the root cause described above (time stolen by irq handler) or is it just a hypotheses ? That would be good to be sure of the root cause
Furthermore, IIRC the time spent in irq context is also accounted as run time for the running cfs task but not RT and deadline task running time
So I'm not really aligned with the description of your problem: PELT metric underestimates the load of the CPU. The PELT is just about tracking CFS task utilization but not whole CPU utilization and according to your description of the problem (time stolen by irq), your problem doesn't come from an underestimation of CFS task but from time spent in something else but not accounted in the value used by schedutil

That would be good to be sure of what is running during this not accounted time and find a way to account them

Quoted 27 lines
Quoted 17 lines

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.

To work around this issue use the observation that, from the schedutil governor's perspective, CPUs that are never idle should always run at the maximum frequency and make that happen.

Quoted 9 lines

To that end, add a counter of idle calls to struct sugov_cpu and modify cpuidle_idle_call() to increment that counter every time it is about to put the given CPU into an idle state. Next, make the schedutil governor look at that counter for the current CPU every time before it is about to start heavy computations. If the counter has not changed for over SUGOV_BUSY_THRESHOLD time (equal to 50 ms), the CPU has not been idle for at least that long and the governor will choose the maximum frequency for it without looking at the PELT metric at all.

Quoted 1 line

Looks like we are fixing a PELT problem with a schedutil Hack :)

I would not say that it's a PELT problem (at least based on current description) but more that we don't track all
activities of CPU

Quoted 107 lines
Quoted 50 lines
---
 include/linux/sched/cpufreq.h    |    6 ++++++
 kernel/sched/cpufreq_schedutil.c |   38 ++++++++++++++++++++++++++++++++++++--
 kernel/sched/idle.c              |    3 +++
 3 files changed, 45 insertions(+), 2 deletions(-)

Index: linux-pm/kernel/sched/cpufreq_schedutil.c
===================================================================

kernel/sched/cpufreq_schedutil.c +20 -0
@@ -20,6 +20,7 @@
 #include "sched.h"
 
 #define SUGOV_KTHREAD_PRIORITY       50
+#define SUGOV_BUSY_THRESHOLD (50 * NSEC_PER_MSEC)
 
 struct sugov_tunables {
      struct gov_attr_set attr_set;
@@ -55,6 +56,9 @@ struct sugov_cpu {
 
      unsigned long iowait_boost;
      unsigned long iowait_boost_max;
+     unsigned long idle_calls;
+     unsigned long saved_idle_calls;
+     u64 busy_start;
      u64 last_update;
 
      /* The fields below are only needed when sharing a policy. */
@@ -192,6 +196,34 @@ static void sugov_iowait_boost(struct su
      sg_cpu->iowait_boost >>= 1;
 }
 
+void cpufreq_schedutil_idle_call(void)
+{
+     struct sugov_cpu *sg_cpu = this_cpu_ptr(&sugov_cpu);
+
+     sg_cpu->idle_calls++;
+}
+
+static bool sugov_cpu_is_busy(struct sugov_cpu *sg_cpu)
+{
+     if (sg_cpu->idle_calls != sg_cpu->saved_idle_calls) {
+             sg_cpu->busy_start = 0;
+             return false;
+     }
+
+     if (!sg_cpu->busy_start) {
+             sg_cpu->busy_start = sg_cpu->last_update;

kernel/sched/cpufreq_schedutil.c static void sugov_iowait_boost(struct su · line 224

Quoted 10 lines
kernel/sched/cpufreq_schedutil.c +10 -0
+             return false;
+     }
+
+     return sg_cpu->last_update - sg_cpu->busy_start > SUGOV_BUSY_THRESHOLD;
+}
+
+static void sugov_save_idle_calls(struct sugov_cpu *sg_cpu)
+{
+     if (!sg_cpu->busy_start)
+             sg_cpu->saved_idle_calls = sg_cpu->idle_calls;

Why aren't we doing this in sugov_cpu_is_busy() itself ? And isn't it possible for idle_calls to get incremented by this time?

Quoted 23 lines
kernel/sched/cpufreq_schedutil.c +4 -1
+}
+
 static void sugov_update_single(struct update_util_data *hook, u64 time,
                              unsigned int flags)
 {
@@ -207,7 +239,7 @@ static void sugov_update_single(struct u
      if (!sugov_should_update_freq(sg_policy, time))
              return;
 
-     if (flags & SCHED_CPUFREQ_RT_DL) {
+     if ((flags & SCHED_CPUFREQ_RT_DL) || sugov_cpu_is_busy(sg_cpu)) {
              next_f = policy->cpuinfo.max_freq;
      } else {
              sugov_get_util(&util, &max);
@@ -215,6 +247,7 @@ static void sugov_update_single(struct u
              next_f = get_next_freq(sg_policy, util, max);
      }
      sugov_update_commit(sg_policy, time, next_f);
+     sugov_save_idle_calls(sg_cpu);
 }
 
 static unsigned int sugov_next_freq_shared(struct sugov_cpu *sg_cpu)

kernel/sched/cpufreq_schedutil.c static void sugov_update_shared(struct u · line 314

Quoted 5 lines
kernel/sched/cpufreq_schedutil.c +1 -1
@@ -278,12 +311,13 @@ static void sugov_update_shared(struct u
      sg_cpu->last_update = time;
 
      if (sugov_should_update_freq(sg_policy, time)) {
-             if (flags & SCHED_CPUFREQ_RT_DL)
+             if ((flags & SCHED_CPUFREQ_RT_DL) || sugov_cpu_is_busy(sg_cpu))

What about others CPUs in this policy?

kernel/sched/cpufreq_schedutil.c static void sugov_update_shared(struct u · line 323

Quoted 9 lines
kernel/sched/cpufreq_schedutil.c +1 -0
                      next_f = sg_policy->policy->cpuinfo.max_freq;
              else
                      next_f = sugov_next_freq_shared(sg_cpu);
 
              sugov_update_commit(sg_policy, time, next_f);
+             sugov_save_idle_calls(sg_cpu);
      }
 
      raw_spin_unlock(&sg_policy->update_lock);

--
viresh

#11 lore raw

Re: [RFC][PATCH 2/2] cpufreq: schedutil: Force max frequency on busy CPUs

Quoted 8 lines · Vincent Guittot
Quoted 6 lines · Viresh Kumar
Quoted 5 lines · Rafael J. Wysocki

From: Rafael J. Wysocki <[email protected]>

The PELT metric used by the schedutil governor underestimates the CPU utilization in some cases. The reason for that may be time spent in interrupt handlers and similar which is not accounted for by PELT.

Quoted 6 lines

Are you sure of the root cause described above (time stolen by irq handler) or is it just a hypotheses ? That would be good to be sure of the root cause
Furthermore, IIRC the time spent in irq context is also accounted as run time for the running cfs task but not RT and deadline task running time

As long as the IRQ processing does not generate a context switch, which is happening (eventually) if the top half schedule some deferred work to be executed by a bottom half.

Thus, me too I would say that all the top half time is accounted in PELT, since the current task is still RUNNABLE/RUNNING.

Quoted 7 lines

So I'm not really aligned with the description of your problem: PELT metric underestimates the load of the CPU. The PELT is just about tracking CFS task utilization but not whole CPU utilization and according to your description of the problem (time stolen by irq), your problem doesn't come from an underestimation of CFS task but from time spent in something else but not accounted in the value used by schedutil

Quite likely. Indeed, it can really be that the CFS task is preempted because of some RT activity generated by the IRQ handler.

More in general, I've also noticed many suboptimal freq switches when RT tasks interleave with CFS ones, because of:
- relatively long down _and up_ throttling times
- the way schedutil's flags are tracked and updated
- the callsites from where we call schedutil updates

For example it can really happen that we are running at the highest OPP because of some RT activity. Then we switch back to a relatively low utilization CFS workload and then:
1. a tick happens which produces a frequency drop
2. right after a new IRQ starts a RT task

Well, because of the schedutil's throttling mechanism we can end up running at the reduce OPP for quite long before (eventually) going back to the higher OPP... if and only we are lucky enough to get a new RT task starting outside of a throttling window.

I guess that if we have quite a lot of IRQ->RT activations in a CPU with a relatively low CFS utilization, this unwanted frequency hopping is quite likely.

Quoted 2 lines

That would be good to be sure of what is running during this not accounted time and find a way to account them

Do we have any number to characterize the frequency of IRQ/RT activations?

We should notice also that, while CFS tasks are preempted by RT ones, the PELT signal decays. This can contribute on lowering even further the utilization demand from the CFS class.

Quoted 6 lines
Quoted 6 lines
Quoted 6 lines

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.

Can you share a trace with at least sched_switch events enabled? This can help on identify which workloads are there and how they interact with schedutil.

Quoted 3 lines
Quoted 3 lines
Quoted 3 lines

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

That's possibly exactly the pattern I've described above.

Quoted 6 lines
Quoted 6 lines
Quoted 6 lines

visibly oscillate below the sustainable maximum in a jittery fashion which clearly is not desirable.

To work around this issue use the observation that, from the schedutil governor's perspective, CPUs that are never idle should always run at the maximum frequency and make that happen.

Perhaps some of the patches I've posted in this series:
https://lkml.org/lkml/2017/3/2/385
can help on that side.

The pattern you report seems to me quite matching with the case "b" described in the cover letter. I've also shared a notebook:
https://gist.github.com/derkling/d6a21b459a18091b2b058668a550010d
which clearly shows that behavior happening in the first plot, cell named Out[15].

Did you had a chance to have a look at them?

It would be nice to know if they can provide some benefits to your use-case.

Quoted 10 lines
Quoted 10 lines
Quoted 9 lines

To that end, add a counter of idle calls to struct sugov_cpu and modify cpuidle_idle_call() to increment that counter every time it is about to put the given CPU into an idle state. Next, make the schedutil governor look at that counter for the current CPU every time before it is about to start heavy computations. If the counter has not changed for over SUGOV_BUSY_THRESHOLD time (equal to 50 ms), the CPU has not been idle for at least that long and the governor will choose the maximum frequency for it without looking at the PELT metric at all.

Quoted 5 lines
Quoted 1 line

Looks like we are fixing a PELT problem with a schedutil Hack :)

I would not say that it's a PELT problem (at least based on current description) but more that we don't track all
activities of CPU

Agree... with both comments.

Quoted 106 lines
Quoted 106 lines
Quoted 49 lines
---
 include/linux/sched/cpufreq.h    |    6 ++++++
 kernel/sched/cpufreq_schedutil.c |   38 ++++++++++++++++++++++++++++++++++++--
 kernel/sched/idle.c              |    3 +++
 3 files changed, 45 insertions(+), 2 deletions(-)

Index: linux-pm/kernel/sched/cpufreq_schedutil.c
===================================================================

kernel/sched/cpufreq_schedutil.c +20 -0
@@ -20,6 +20,7 @@
 #include "sched.h"
 
 #define SUGOV_KTHREAD_PRIORITY       50
+#define SUGOV_BUSY_THRESHOLD (50 * NSEC_PER_MSEC)
 
 struct sugov_tunables {
      struct gov_attr_set attr_set;
@@ -55,6 +56,9 @@ struct sugov_cpu {
 
      unsigned long iowait_boost;
      unsigned long iowait_boost_max;
+     unsigned long idle_calls;
+     unsigned long saved_idle_calls;
+     u64 busy_start;
      u64 last_update;
 
      /* The fields below are only needed when sharing a policy. */
@@ -192,6 +196,34 @@ static void sugov_iowait_boost(struct su
      sg_cpu->iowait_boost >>= 1;
 }
 
+void cpufreq_schedutil_idle_call(void)
+{
+     struct sugov_cpu *sg_cpu = this_cpu_ptr(&sugov_cpu);
+
+     sg_cpu->idle_calls++;
+}
+
+static bool sugov_cpu_is_busy(struct sugov_cpu *sg_cpu)
+{
+     if (sg_cpu->idle_calls != sg_cpu->saved_idle_calls) {
+             sg_cpu->busy_start = 0;
+             return false;
+     }
+
+     if (!sg_cpu->busy_start) {
+             sg_cpu->busy_start = sg_cpu->last_update;

kernel/sched/cpufreq_schedutil.c static void sugov_iowait_boost(struct su · line 224

Quoted 10 lines
kernel/sched/cpufreq_schedutil.c +10 -0
+             return false;
+     }
+
+     return sg_cpu->last_update - sg_cpu->busy_start > SUGOV_BUSY_THRESHOLD;
+}
+
+static void sugov_save_idle_calls(struct sugov_cpu *sg_cpu)
+{
+     if (!sg_cpu->busy_start)
+             sg_cpu->saved_idle_calls = sg_cpu->idle_calls;

Why aren't we doing this in sugov_cpu_is_busy() itself ? And isn't it possible for idle_calls to get incremented by this time?

Quoted 23 lines
kernel/sched/cpufreq_schedutil.c +4 -1
+}
+
 static void sugov_update_single(struct update_util_data *hook, u64 time,
                              unsigned int flags)
 {
@@ -207,7 +239,7 @@ static void sugov_update_single(struct u
      if (!sugov_should_update_freq(sg_policy, time))
              return;
 
-     if (flags & SCHED_CPUFREQ_RT_DL) {
+     if ((flags & SCHED_CPUFREQ_RT_DL) || sugov_cpu_is_busy(sg_cpu)) {
              next_f = policy->cpuinfo.max_freq;
      } else {
              sugov_get_util(&util, &max);
@@ -215,6 +247,7 @@ static void sugov_update_single(struct u
              next_f = get_next_freq(sg_policy, util, max);
      }
      sugov_update_commit(sg_policy, time, next_f);
+     sugov_save_idle_calls(sg_cpu);
 }
 
 static unsigned int sugov_next_freq_shared(struct sugov_cpu *sg_cpu)

kernel/sched/cpufreq_schedutil.c static void sugov_update_shared(struct u · line 314

Quoted 5 lines
kernel/sched/cpufreq_schedutil.c +1 -1
@@ -278,12 +311,13 @@ static void sugov_update_shared(struct u
      sg_cpu->last_update = time;
 
      if (sugov_should_update_freq(sg_policy, time)) {
-             if (flags & SCHED_CPUFREQ_RT_DL)
+             if ((flags & SCHED_CPUFREQ_RT_DL) || sugov_cpu_is_busy(sg_cpu))

What about others CPUs in this policy?

kernel/sched/cpufreq_schedutil.c static void sugov_update_shared(struct u · line 323

Quoted 9 lines
kernel/sched/cpufreq_schedutil.c +1 -0
                      next_f = sg_policy->policy->cpuinfo.max_freq;
              else
                      next_f = sugov_next_freq_shared(sg_cpu);
 
              sugov_update_commit(sg_policy, time, next_f);
+             sugov_save_idle_calls(sg_cpu);
      }
 
      raw_spin_unlock(&sg_policy->update_lock);

--
viresh

Signature · 3 lines
#include <best/regards.h>

Patrick Bellasi
#12 lore raw

Re: [RFC][PATCH 2/2] cpufreq: schedutil: Force max frequency on busy CPUs

Quoted 16 lines · Patrick Bellasi
Quoted 8 lines · Vincent Guittot
Quoted 6 lines · Viresh Kumar
Quoted 5 lines · Rafael J. Wysocki

From: Rafael J. Wysocki <[email protected]>

The PELT metric used by the schedutil governor underestimates the CPU utilization in some cases. The reason for that may be time spent in interrupt handlers and similar which is not accounted for by PELT.

Quoted 6 lines

Are you sure of the root cause described above (time stolen by irq handler) or is it just a hypotheses ? That would be good to be sure of the root cause
Furthermore, IIRC the time spent in irq context is also accounted as run time for the running cfs task but not RT and deadline task running time

Quoted 6 lines

As long as the IRQ processing does not generate a context switch, which is happening (eventually) if the top half schedule some deferred work to be executed by a bottom half.

Thus, me too I would say that all the top half time is accounted in PELT, since the current task is still RUNNABLE/RUNNING.

Sorry if I'm missing something but doesn't this depend on whether you have CONFIG_IRQ_TIME_ACCOUNTING enabled?

__update_load_avg uses rq->clock_task for deltas which I think shouldn't account IRQ time with that config option. So it should be quite possible for IRQ time spent to reduce the PELT signal right?

Quoted 18 lines
Quoted 8 lines

So I'm not really aligned with the description of your problem: PELT metric underestimates the load of the CPU. The PELT is just about tracking CFS task utilization but not whole CPU utilization and according to your description of the problem (time stolen by irq), your problem doesn't come from an underestimation of CFS task but from time spent in something else but not accounted in the value used by schedutil

Quite likely. Indeed, it can really be that the CFS task is preempted because of some RT activity generated by the IRQ handler.

More in general, I've also noticed many suboptimal freq switches when RT tasks interleave with CFS ones, because of:
- relatively long down _and up_ throttling times
- the way schedutil's flags are tracked and updated
- the callsites from where we call schedutil updates

Quoted 4 lines

For example it can really happen that we are running at the highest OPP because of some RT activity. Then we switch back to a relatively low utilization CFS workload and then:
1. a tick happens which produces a frequency drop

Any idea why this frequency drop would happen? Say a running CFS task gets preempted by RT task, the PELT signal shouldn't drop for the duration the CFS task is preempted because the task is runnable, so once the CFS task gets CPU back, schedutil should still maintain the capacity right?

Regards,
Joel

maintainer
#13 lore raw

Re: [RFC][PATCH 2/2] cpufreq: schedutil: Force max frequency on busy CPUs

Quoted 50 lines · Joel Fernandes
Quoted 16 lines · Patrick Bellasi
Quoted 8 lines · Vincent Guittot
Quoted 6 lines · Viresh Kumar
Quoted 5 lines · Rafael J. Wysocki

From: Rafael J. Wysocki <[email protected]>

The PELT metric used by the schedutil governor underestimates the CPU utilization in some cases. The reason for that may be time spent in interrupt handlers and similar which is not accounted for by PELT.

Quoted 6 lines

Are you sure of the root cause described above (time stolen by irq handler) or is it just a hypotheses ? That would be good to be sure of the root cause
Furthermore, IIRC the time spent in irq context is also accounted as run time for the running cfs task but not RT and deadline task running time

Quoted 6 lines

As long as the IRQ processing does not generate a context switch, which is happening (eventually) if the top half schedule some deferred work to be executed by a bottom half.

Thus, me too I would say that all the top half time is accounted in PELT, since the current task is still RUNNABLE/RUNNING.

Sorry if I'm missing something but doesn't this depend on whether you have CONFIG_IRQ_TIME_ACCOUNTING enabled?

__update_load_avg uses rq->clock_task for deltas which I think shouldn't account IRQ time with that config option. So it should be quite possible for IRQ time spent to reduce the PELT signal right?

Quoted 18 lines
Quoted 8 lines

So I'm not really aligned with the description of your problem: PELT metric underestimates the load of the CPU. The PELT is just about tracking CFS task utilization but not whole CPU utilization and according to your description of the problem (time stolen by irq), your problem doesn't come from an underestimation of CFS task but from time spent in something else but not accounted in the value used by schedutil

Quite likely. Indeed, it can really be that the CFS task is preempted because of some RT activity generated by the IRQ handler.

More in general, I've also noticed many suboptimal freq switches when RT tasks interleave with CFS ones, because of:
- relatively long down _and up_ throttling times
- the way schedutil's flags are tracked and updated
- the callsites from where we call schedutil updates

Quoted 8 lines
Quoted 4 lines

For example it can really happen that we are running at the highest OPP because of some RT activity. Then we switch back to a relatively low utilization CFS workload and then:
1. a tick happens which produces a frequency drop

Any idea why this frequency drop would happen? Say a running CFS task gets preempted by RT task, the PELT signal shouldn't drop for the duration the CFS task is preempted because the task is runnable, so

utilization only tracks the running state but not runnable state. Runnable state is tracked in load_avg

Quoted 5 lines

once the CFS task gets CPU back, schedutil should still maintain the capacity right?

Regards,
Joel

#14 lore raw

Re: [RFC][PATCH 2/2] cpufreq: schedutil: Force max frequency on busy CPUs

Hi Vincent,

On Thu, Mar 23, 2017 at 3:08 PM, Vincent Guittot
<[email protected]> wrote:
[..]

Quoted 23 lines
Quoted 23 lines
Quoted 22 lines
Quoted 8 lines

So I'm not really aligned with the description of your problem: PELT metric underestimates the load of the CPU. The PELT is just about tracking CFS task utilization but not whole CPU utilization and according to your description of the problem (time stolen by irq), your problem doesn't come from an underestimation of CFS task but from time spent in something else but not accounted in the value used by schedutil

Quite likely. Indeed, it can really be that the CFS task is preempted because of some RT activity generated by the IRQ handler.

More in general, I've also noticed many suboptimal freq switches when RT tasks interleave with CFS ones, because of:
- relatively long down _and up_ throttling times
- the way schedutil's flags are tracked and updated
- the callsites from where we call schedutil updates

For example it can really happen that we are running at the highest OPP because of some RT activity. Then we switch back to a relatively low utilization CFS workload and then:
1. a tick happens which produces a frequency drop

Quoted 6 lines
Quoted 3 lines

Any idea why this frequency drop would happen? Say a running CFS task gets preempted by RT task, the PELT signal shouldn't drop for the duration the CFS task is preempted because the task is runnable, so

utilization only tracks the running state but not runnable state. Runnable state is tracked in load_avg

Thanks. I got it now.

Correct me if I'm wrong but strictly speaking utilization for a cfs_rq (which drives the frequency for CFS) still tracks the blocked/runnable time of tasks although its decayed as time moves forward. Only when we migrate the rq of a cfs task is the util_avg contribution removed from the rq. But I can see now why running RT can decay this load tracking signal.

Regards,
Joel

maintainer
#15 lore raw

Re: [RFC][PATCH 2/2] cpufreq: schedutil: Force max frequency on busy CPUs

Quoted 35 lines · Joel Fernandes

Hi Vincent,

On Thu, Mar 23, 2017 at 3:08 PM, Vincent Guittot
<[email protected]> wrote:
[..]

Quoted 23 lines
Quoted 23 lines
Quoted 22 lines
Quoted 8 lines

So I'm not really aligned with the description of your problem: PELT metric underestimates the load of the CPU. The PELT is just about tracking CFS task utilization but not whole CPU utilization and according to your description of the problem (time stolen by irq), your problem doesn't come from an underestimation of CFS task but from time spent in something else but not accounted in the value used by schedutil

Quite likely. Indeed, it can really be that the CFS task is preempted because of some RT activity generated by the IRQ handler.

More in general, I've also noticed many suboptimal freq switches when RT tasks interleave with CFS ones, because of:
- relatively long down _and up_ throttling times
- the way schedutil's flags are tracked and updated
- the callsites from where we call schedutil updates

For example it can really happen that we are running at the highest OPP because of some RT activity. Then we switch back to a relatively low utilization CFS workload and then:
1. a tick happens which produces a frequency drop

Quoted 6 lines
Quoted 3 lines

Any idea why this frequency drop would happen? Say a running CFS task gets preempted by RT task, the PELT signal shouldn't drop for the duration the CFS task is preempted because the task is runnable, so

utilization only tracks the running state but not runnable state. Runnable state is tracked in load_avg

Quoted 8 lines

Thanks. I got it now.

Correct me if I'm wrong but strictly speaking utilization for a cfs_rq (which drives the frequency for CFS) still tracks the blocked/runnable time of tasks although its decayed as time moves forward. Only when we migrate the rq of a cfs task is the util_avg contribution removed from the rq. But I can see now why running RT can decay this load tracking signal.

Yes. you're right

Quoted 3 lines

Regards,
Joel

#16 lore raw

Re: [RFC][PATCH 2/2] cpufreq: schedutil: Force max frequency on busy CPUs

In reply to Vincent Guittot
Quoted 11 lines · Vincent Guittot
Quoted 6 lines · Viresh Kumar
Quoted 5 lines · Rafael J. Wysocki

From: Rafael J. Wysocki <[email protected]>

The PELT metric used by the schedutil governor underestimates the CPU utilization in some cases. The reason for that may be time spent in interrupt handlers and similar which is not accounted for by PELT.

Are you sure of the root cause described above (time stolen by irq handler) or is it just a hypotheses ? That would be good to be sure of the root cause

No, I'm not sure. That's why I said "may be". :-)

Quoted 3 lines

Furthermore, IIRC the time spent in irq context is also accounted as run time for the running cfs task but not RT and deadline task running time

OK

Anyway, the problem is that we have a 100% busy CPU which quite evidently is not reported as 100% busy by the metric we use.

Now, if I was sure about the root cause, I would fix it rather than suggest workarounds.

Quoted 7 lines

So I'm not really aligned with the description of your problem: PELT metric underestimates the load of the CPU. The PELT is just about tracking CFS task utilization but not whole CPU utilization and according to your description of the problem (time stolen by irq), your problem doesn't come from an underestimation of CFS task but from time spent in something else but not accounted in the value used by schedutil

That's fair enough, but the assumption was that PELT would be sufficient for that. To me, it clearly isn't, so the assumption was incorrect.

Quoted 2 lines

That would be good to be sure of what is running during this not accounted time and find a way to account them

Yes, I agree.

I'm not sure if I can carry out full investigation of that any time soon, however.

I sent this mostly to let everybody know that there was a problem and how it could be worked around. That's why it is an RFC.

Quoted 28 lines
Quoted 28 lines
Quoted 27 lines

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.

To work around this issue use the observation that, from the schedutil governor's perspective, CPUs that are never idle should always run at the maximum frequency and make that happen.

To that end, add a counter of idle calls to struct sugov_cpu and modify cpuidle_idle_call() to increment that counter every time it is about to put the given CPU into an idle state. Next, make the schedutil governor look at that counter for the current CPU every time before it is about to start heavy computations. If the counter has not changed for over SUGOV_BUSY_THRESHOLD time (equal to 50 ms), the CPU has not been idle for at least that long and the governor will choose the maximum frequency for it without looking at the PELT metric at all.

Quoted 5 lines
Quoted 1 line

Looks like we are fixing a PELT problem with a schedutil Hack :)

I would not say that it's a PELT problem (at least based on current description) but more that we don't track all
activities of CPU

I generally agree. PELT does what it does.

However, using PELT the way we do that in schedutil turns out to be problematic.

Thanks,
Rafael

maintainer
#17 lore raw

Re: [RFC][PATCH 2/2] cpufreq: schedutil: Force max frequency on busy CPUs

Quoted 37 lines · Rafael J. Wysocki
Quoted 11 lines · Vincent Guittot
Quoted 6 lines · Viresh Kumar
Quoted 5 lines · Rafael J. Wysocki

From: Rafael J. Wysocki <[email protected]>

The PELT metric used by the schedutil governor underestimates the CPU utilization in some cases. The reason for that may be time spent in interrupt handlers and similar which is not accounted for by PELT.

Are you sure of the root cause described above (time stolen by irq handler) or is it just a hypotheses ? That would be good to be sure of the root cause

No, I'm not sure. That's why I said "may be". :-)

Quoted 3 lines

Furthermore, IIRC the time spent in irq context is also accounted as run time for the running cfs task but not RT and deadline task running time

OK

Anyway, the problem is that we have a 100% busy CPU which quite evidently is not reported as 100% busy by the metric we use.

Now, if I was sure about the root cause, I would fix it rather than suggest workarounds.

Quoted 7 lines

So I'm not really aligned with the description of your problem: PELT metric underestimates the load of the CPU. The PELT is just about tracking CFS task utilization but not whole CPU utilization and according to your description of the problem (time stolen by irq), your problem doesn't come from an underestimation of CFS task but from time spent in something else but not accounted in the value used by schedutil

Quoted 2 lines

That's fair enough, but the assumption was that PELT would be sufficient for that. To me, it clearly isn't, so the assumption was incorrect.

So PELT is just about CFS utilization and we need metrics for other sched class as well
I know that Juri and other people are working on the dealine part

In current kernel, the only available metric is rt_avg (and scale_rt_capacity) which gives you the available compute capacity for CFS and in some way the amount of capacity used by RT and deadline. That would be interesting to see if the amount of capacity used by RT tasks (i assume that you don't have deadline task in your use case) is similar to the amount of underestimation of CFS

Quoted 7 lines
Quoted 3 lines

That would be good to be sure of what is running during this not accounted time and find a way to account them

Yes, I agree.

I'm not sure if I can carry out full investigation of that any time soon, however.

If you can share traces as Patrick proposed, we could estimate how much time/capacity is used by rt tasks and if this can explain the underestimation.

Quoted 3 lines

I sent this mostly to let everybody know that there was a problem and how it could be worked around. That's why it is an RFC.

Ok

Quoted 31 lines
Quoted 31 lines
Quoted 30 lines
Quoted 19 lines

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.

To work around this issue use the observation that, from the schedutil governor's perspective, CPUs that are never idle should always run at the maximum frequency and make that happen.

Quoted 9 lines

To that end, add a counter of idle calls to struct sugov_cpu and modify cpuidle_idle_call() to increment that counter every time it is about to put the given CPU into an idle state. Next, make the schedutil governor look at that counter for the current CPU every time before it is about to start heavy computations. If the counter has not changed for over SUGOV_BUSY_THRESHOLD time (equal to 50 ms), the CPU has not been idle for at least that long and the governor will choose the maximum frequency for it without looking at the PELT metric at all.

Looks like we are fixing a PELT problem with a schedutil Hack :)

Quoted 7 lines
Quoted 3 lines

I would not say that it's a PELT problem (at least based on current description) but more that we don't track all
activities of CPU

I generally agree. PELT does what it does.

However, using PELT the way we do that in schedutil turns out to be problematic.

yes. PELT is not enough but just part of the equation

Quoted 4 lines

Thanks,
Rafael

#18 lore raw

Re: [RFC][PATCH 2/2] cpufreq: schedutil: Force max frequency on busy CPUs

In reply to Viresh Kumar
Quoted 33 lines · Viresh Kumar
Quoted 22 lines · Rafael J. Wysocki

From: Rafael J. Wysocki <[email protected]>

The PELT metric used by the schedutil governor underestimates the CPU utilization in some cases. The reason for that may be time spent in interrupt handlers and similar which is not accounted for by PELT.

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.

To work around this issue use the observation that, from the schedutil governor's perspective, CPUs that are never idle should always run at the maximum frequency and make that happen.

Quoted 9 lines

To that end, add a counter of idle calls to struct sugov_cpu and modify cpuidle_idle_call() to increment that counter every time it is about to put the given CPU into an idle state. Next, make the schedutil governor look at that counter for the current CPU every time before it is about to start heavy computations. If the counter has not changed for over SUGOV_BUSY_THRESHOLD time (equal to 50 ms), the CPU has not been idle for at least that long and the governor will choose the maximum frequency for it without looking at the PELT metric at all.

Quoted 1 line

Looks like we are fixing a PELT problem with a schedutil Hack :)

Did you notice the "To work around this issue" phrase above? This is a workaround, not a fix.

But it is an optimization too, because avoiding heavy computations if they are not necessary should be a win in any case.

Quoted 17 lines
Quoted 17 lines
---
 include/linux/sched/cpufreq.h    |    6 ++++++
 kernel/sched/cpufreq_schedutil.c |   38 ++++++++++++++++++++++++++++++++++++--
 kernel/sched/idle.c              |    3 +++
 3 files changed, 45 insertions(+), 2 deletions(-)

Index: linux-pm/kernel/sched/cpufreq_schedutil.c
===================================================================

kernel/sched/cpufreq_schedutil.c +1 -0
@@ -20,6 +20,7 @@
 #include "sched.h"
 
 #define SUGOV_KTHREAD_PRIORITY	50
+#define SUGOV_BUSY_THRESHOLD	(50 * NSEC_PER_MSEC)
 
Quoted 8 lines

kernel/sched/cpufreq_schedutil.c struct sugov_cpu { · line 60

Quoted 8 lines
kernel/sched/cpufreq_schedutil.c +2 -0
 struct sugov_tunables {
 	struct gov_attr_set attr_set;
@@ -55,6 +56,9 @@ struct sugov_cpu {
 
 	unsigned long iowait_boost;
 	unsigned long iowait_boost_max;
+	unsigned long idle_calls;
+	unsigned long saved_idle_calls;

The above two could be unsigned int, BTW.

Quoted 28 lines
Quoted 28 lines
kernel/sched/cpufreq_schedutil.c +21 -0
+	u64 busy_start;
 	u64 last_update;
 
 	/* The fields below are only needed when sharing a policy. */
@@ -192,6 +196,34 @@ static void sugov_iowait_boost(struct su
 	sg_cpu->iowait_boost >>= 1;
 }
 
+void cpufreq_schedutil_idle_call(void)
+{
+	struct sugov_cpu *sg_cpu = this_cpu_ptr(&sugov_cpu);
+
+	sg_cpu->idle_calls++;
+}
+
+static bool sugov_cpu_is_busy(struct sugov_cpu *sg_cpu)
+{
+	if (sg_cpu->idle_calls != sg_cpu->saved_idle_calls) {
+		sg_cpu->busy_start = 0;
+		return false;
+	}
+
+	if (!sg_cpu->busy_start) {
+		sg_cpu->busy_start = sg_cpu->last_update;
+		return false;
+	}
+
+	return sg_cpu->last_update - sg_cpu->busy_start > SUGOV_BUSY_THRESHOLD;
Quoted 8 lines

kernel/sched/cpufreq_schedutil.c static void sugov_iowait_boost(struct su · line 224

Quoted 6 lines
kernel/sched/cpufreq_schedutil.c +6 -0
+}
+
+static void sugov_save_idle_calls(struct sugov_cpu *sg_cpu)
+{
+	if (!sg_cpu->busy_start)
+		sg_cpu->saved_idle_calls = sg_cpu->idle_calls;

Why aren't we doing this in sugov_cpu_is_busy() itself ?

No, we aren't.

sugov_cpu_is_busy() may not be called at all sometimes.

Quoted 1 line

And isn't it possible for idle_calls to get incremented by this time?

No, it isn't.

The idle loop does that and after it has disabled interrupts. If this code is running, we surely are not in there on the same CPU, are we?

Quoted 25 lines
Quoted 25 lines
kernel/sched/cpufreq_schedutil.c +4 -1
+}
+
 static void sugov_update_single(struct update_util_data *hook, u64 time,
 				unsigned int flags)
 {
@@ -207,7 +239,7 @@ static void sugov_update_single(struct u
 	if (!sugov_should_update_freq(sg_policy, time))
 		return;
 
-	if (flags & SCHED_CPUFREQ_RT_DL) {
+	if ((flags & SCHED_CPUFREQ_RT_DL) || sugov_cpu_is_busy(sg_cpu)) {
 		next_f = policy->cpuinfo.max_freq;
 	} else {
 		sugov_get_util(&util, &max);
@@ -215,6 +247,7 @@ static void sugov_update_single(struct u
 		next_f = get_next_freq(sg_policy, util, max);
 	}
 	sugov_update_commit(sg_policy, time, next_f);
+	sugov_save_idle_calls(sg_cpu);
 }
 
 static unsigned int sugov_next_freq_shared(struct sugov_cpu *sg_cpu)
@@ -278,12 +311,13 @@ static void sugov_update_shared(struct u
 	sg_cpu->last_update = time;
 
Quoted 5 lines

kernel/sched/cpufreq_schedutil.c static void sugov_update_shared(struct u · line 314

Quoted 3 lines
kernel/sched/cpufreq_schedutil.c +1 -1
 	if (sugov_should_update_freq(sg_policy, time)) {
-		if (flags & SCHED_CPUFREQ_RT_DL)
+		if ((flags & SCHED_CPUFREQ_RT_DL) || sugov_cpu_is_busy(sg_cpu))

What about others CPUs in this policy?

We'll check this for them when they get updated.

The point is that we can just bail out earlier here and avoid the heavy stuff, but we don't have to. This is opportunistic.

Quoted 11 lines

kernel/sched/cpufreq_schedutil.c static void sugov_update_shared(struct u · line 323

Quoted 9 lines
kernel/sched/cpufreq_schedutil.c +1 -0
 			next_f = sg_policy->policy->cpuinfo.max_freq;
 		else
 			next_f = sugov_next_freq_shared(sg_cpu);
 
 		sugov_update_commit(sg_policy, time, next_f);
+		sugov_save_idle_calls(sg_cpu);
 	}
 
 	raw_spin_unlock(&sg_policy->update_lock);

Thanks,
Rafael

maintainer
#19 lore raw

Re: [RFC][PATCH 2/2] cpufreq: schedutil: Force max frequency on busy CPUs

In reply to Rafael J. Wysocki
Quoted 22 lines · Rafael J. Wysocki

From: Rafael J. Wysocki <[email protected]>

The PELT metric used by the schedutil governor underestimates the CPU utilization in some cases. The reason for that may be time spent in interrupt handlers and similar which is not accounted for by PELT.

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.

To work around this issue use the observation that, from the schedutil governor's perspective, CPUs that are never idle should always run at the maximum frequency and make that happen.

Quoted 9 lines

To that end, add a counter of idle calls to struct sugov_cpu and modify cpuidle_idle_call() to increment that counter every time it is about to put the given CPU into an idle state. Next, make the schedutil governor look at that counter for the current CPU every time before it is about to start heavy computations. If the counter has not changed for over SUGOV_BUSY_THRESHOLD time (equal to 50 ms), the CPU has not been idle for at least that long and the governor will choose the maximum frequency for it without looking at the PELT metric at all.

Why the time limit?

#20 lore raw

Re: [RFC][PATCH 2/2] cpufreq: schedutil: Force max frequency on busy CPUs

Quoted 33 lines · Peter Zijlstra
Quoted 22 lines · Rafael J. Wysocki

From: Rafael J. Wysocki <[email protected]>

The PELT metric used by the schedutil governor underestimates the CPU utilization in some cases. The reason for that may be time spent in interrupt handlers and similar which is not accounted for by PELT.

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.

To work around this issue use the observation that, from the schedutil governor's perspective, CPUs that are never idle should always run at the maximum frequency and make that happen.

Quoted 9 lines

To that end, add a counter of idle calls to struct sugov_cpu and modify cpuidle_idle_call() to increment that counter every time it is about to put the given CPU into an idle state. Next, make the schedutil governor look at that counter for the current CPU every time before it is about to start heavy computations. If the counter has not changed for over SUGOV_BUSY_THRESHOLD time (equal to 50 ms), the CPU has not been idle for at least that long and the governor will choose the maximum frequency for it without looking at the PELT metric at all.

Quoted 1 line

Why the time limit?

One iteration appeared to be a bit too aggressive, but honestly I think I need to check again if this thing is regarded as viable at all.

maintainer
#21 lore raw

Re: [RFC][PATCH 2/2] cpufreq: schedutil: Force max frequency on busy CPUs

Quoted 34 lines · Rafael J. Wysocki
Quoted 33 lines · Peter Zijlstra
Quoted 31 lines · Rafael J. Wysocki

From: Rafael J. Wysocki <[email protected]>

The PELT metric used by the schedutil governor underestimates the CPU utilization in some cases. The reason for that may be time spent in interrupt handlers and similar which is not accounted for by PELT.

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.

To work around this issue use the observation that, from the schedutil governor's perspective, CPUs that are never idle should always run at the maximum frequency and make that happen.

To that end, add a counter of idle calls to struct sugov_cpu and modify cpuidle_idle_call() to increment that counter every time it is about to put the given CPU into an idle state. Next, make the schedutil governor look at that counter for the current CPU every time before it is about to start heavy computations. If the counter has not changed for over SUGOV_BUSY_THRESHOLD time (equal to 50 ms), the CPU has not been idle for at least that long and the governor will choose the maximum frequency for it without looking at the PELT metric at all.

Quoted 5 lines
Quoted 1 line

Why the time limit?

One iteration appeared to be a bit too aggressive, but honestly I think I need to check again if this thing is regarded as viable at all.

I don't hate the idea; if we don't hit idle; we shouldn't shift down. I just wonder if we don't already keep a idle-seqcount somewhere; NOHZ and RCU come to mind as things that might already use something like that.

#22 lore raw

Re: [RFC][PATCH 2/2] cpufreq: schedutil: Force max frequency on busy CPUs

Quoted 37 lines · Peter Zijlstra
Quoted 36 lines · Rafael J. Wysocki
Quoted 34 lines · Peter Zijlstra
Quoted 22 lines · Rafael J. Wysocki

From: Rafael J. Wysocki <[email protected]>

The PELT metric used by the schedutil governor underestimates the CPU utilization in some cases. The reason for that may be time spent in interrupt handlers and similar which is not accounted for by PELT.

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.

To work around this issue use the observation that, from the schedutil governor's perspective, CPUs that are never idle should always run at the maximum frequency and make that happen.

Quoted 9 lines

To that end, add a counter of idle calls to struct sugov_cpu and modify cpuidle_idle_call() to increment that counter every time it is about to put the given CPU into an idle state. Next, make the schedutil governor look at that counter for the current CPU every time before it is about to start heavy computations. If the counter has not changed for over SUGOV_BUSY_THRESHOLD time (equal to 50 ms), the CPU has not been idle for at least that long and the governor will choose the maximum frequency for it without looking at the PELT metric at all.

Why the time limit?

Quoted 7 lines
Quoted 3 lines

One iteration appeared to be a bit too aggressive, but honestly I think I need to check again if this thing is regarded as viable at all.

I don't hate the idea; if we don't hit idle; we shouldn't shift down. I just wonder if we don't already keep a idle-seqcount somewhere; NOHZ and RCU come to mind as things that might already use something like that.

Maybe the problem is not going down (e.g. when there are only small CFS tasks it makes perfectly sense) but instead not being fast enough on rampin-up when a new RT task is activated.

And this boils down to two main point:
1) throttling for up transitions perhaps is only harmful
2) the call sites for schedutils updates are not properly positioned in specific scheduler decision points.

The proposed patch is adding yet another throttling mechanism, perhaps on top of one which already needs to be improved.

Signature · 3 lines
#include <best/regards.h>

Patrick Bellasi
#23 lore raw

Re: [RFC][PATCH 2/2] cpufreq: schedutil: Force max frequency on busy CPUs

Quoted 50 lines · Patrick Bellasi
Quoted 37 lines · Peter Zijlstra
Quoted 36 lines · Rafael J. Wysocki
Quoted 34 lines · Peter Zijlstra
Quoted 22 lines · Rafael J. Wysocki

From: Rafael J. Wysocki <[email protected]>

The PELT metric used by the schedutil governor underestimates the CPU utilization in some cases. The reason for that may be time spent in interrupt handlers and similar which is not accounted for by PELT.

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.

To work around this issue use the observation that, from the schedutil governor's perspective, CPUs that are never idle should always run at the maximum frequency and make that happen.

Quoted 9 lines

To that end, add a counter of idle calls to struct sugov_cpu and modify cpuidle_idle_call() to increment that counter every time it is about to put the given CPU into an idle state. Next, make the schedutil governor look at that counter for the current CPU every time before it is about to start heavy computations. If the counter has not changed for over SUGOV_BUSY_THRESHOLD time (equal to 50 ms), the CPU has not been idle for at least that long and the governor will choose the maximum frequency for it without looking at the PELT metric at all.

Why the time limit?

Quoted 7 lines
Quoted 3 lines

One iteration appeared to be a bit too aggressive, but honestly I think I need to check again if this thing is regarded as viable at all.

I don't hate the idea; if we don't hit idle; we shouldn't shift down. I just wonder if we don't already keep a idle-seqcount somewhere; NOHZ and RCU come to mind as things that might already use something like that.

Maybe the problem is not going down (e.g. when there are only small CFS tasks it makes perfectly sense) but instead not being fast enough on rampin-up when a new RT task is activated.

Quoted 7 lines

And this boils down to two main point:
1) throttling for up transitions perhaps is only harmful
2) the call sites for schedutils updates are not properly positioned in specific scheduler decision points.

The proposed patch is adding yet another throttling mechanism, perhaps on top of one which already needs to be improved.

It is not throttling anything.

Thanks,
Rafael

#24 lore raw

Re: [RFC][PATCH 2/2] cpufreq: schedutil: Force max frequency on busy CPUs

Quoted 56 lines · Rafael J. Wysocki
Quoted 55 lines · Patrick Bellasi
Quoted 37 lines · Peter Zijlstra
Quoted 36 lines · Rafael J. Wysocki
Quoted 34 lines · Peter Zijlstra
Quoted 22 lines · Rafael J. Wysocki

From: Rafael J. Wysocki <[email protected]>

The PELT metric used by the schedutil governor underestimates the CPU utilization in some cases. The reason for that may be time spent in interrupt handlers and similar which is not accounted for by PELT.

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.

To work around this issue use the observation that, from the schedutil governor's perspective, CPUs that are never idle should always run at the maximum frequency and make that happen.

Quoted 9 lines

To that end, add a counter of idle calls to struct sugov_cpu and modify cpuidle_idle_call() to increment that counter every time it is about to put the given CPU into an idle state. Next, make the schedutil governor look at that counter for the current CPU every time before it is about to start heavy computations. If the counter has not changed for over SUGOV_BUSY_THRESHOLD time (equal to 50 ms), the CPU has not been idle for at least that long and the governor will choose the maximum frequency for it without looking at the PELT metric at all.

Why the time limit?

Quoted 7 lines
Quoted 3 lines

One iteration appeared to be a bit too aggressive, but honestly I think I need to check again if this thing is regarded as viable at all.

I don't hate the idea; if we don't hit idle; we shouldn't shift down. I just wonder if we don't already keep a idle-seqcount somewhere; NOHZ and RCU come to mind as things that might already use something like that.

Maybe the problem is not going down (e.g. when there are only small CFS tasks it makes perfectly sense) but instead not being fast enough on rampin-up when a new RT task is activated.

And this boils down to two main point:
1) throttling for up transitions perhaps is only harmful
2) the call sites for schedutils updates are not properly positioned in specific scheduler decision points.

Quoted 4 lines
Quoted 2 lines

The proposed patch is adding yet another throttling mechanism, perhaps on top of one which already needs to be improved.

It is not throttling anything.

It's a kind-of...

- if (flags & SCHED_CPUFREQ_RT_DL) {

+       if ((flags & SCHED_CPUFREQ_RT_DL) || sugov_cpu_is_busy(sg_cpu)) {
                next_f = policy->cpuinfo.max_freq;

This check disregard any signal the scheduler can provide.

A 60% CFS task with a 100ms period, with such a policy will end up running at the highest OPP for just 10% of its entire activation. Moreover, when it completes, we are likely to enter an idle OPP while still remaining at the highest OPP.

IMHO the ultimate goal of scheduitl should be that to be driven by the scheduler, which has (or can have) all the required information to support OPP selection.

If something is not working, well, then we should properly fix the signals and/or provide (at least) a per-task tunable interface.

Adding an hardcoded threshold is an easy fix but it will ultimately increase the complexity of the governor.

Signature · 3 lines
#include <best/regards.h>

Patrick Bellasi
#25 lore raw

Re: [RFC][PATCH 2/2] cpufreq: schedutil: Force max frequency on busy CPUs

In reply to Peter Zijlstra
Quoted 35 lines · Peter Zijlstra
Quoted 34 lines · Rafael J. Wysocki
Quoted 33 lines · Peter Zijlstra
Quoted 31 lines · Rafael J. Wysocki

From: Rafael J. Wysocki <[email protected]>

The PELT metric used by the schedutil governor underestimates the CPU utilization in some cases. The reason for that may be time spent in interrupt handlers and similar which is not accounted for by PELT.

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.

To work around this issue use the observation that, from the schedutil governor's perspective, CPUs that are never idle should always run at the maximum frequency and make that happen.

To that end, add a counter of idle calls to struct sugov_cpu and modify cpuidle_idle_call() to increment that counter every time it is about to put the given CPU into an idle state. Next, make the schedutil governor look at that counter for the current CPU every time before it is about to start heavy computations. If the counter has not changed for over SUGOV_BUSY_THRESHOLD time (equal to 50 ms), the CPU has not been idle for at least that long and the governor will choose the maximum frequency for it without looking at the PELT metric at all.

Quoted 7 lines
Quoted 5 lines
Quoted 1 line

Why the time limit?

One iteration appeared to be a bit too aggressive, but honestly I think I need to check again if this thing is regarded as viable at all.

I don't hate the idea; if we don't hit idle; we shouldn't shift down.

OK

Quoted 2 lines

I just wonder if we don't already keep a idle-seqcount somewhere; NOHZ and RCU come to mind as things that might already use something like that.

NOHZ does that, but I did't want this to artificially depend on NOHZ. That said, yes, we can use that one too.

#26 lore raw

RFC PATCH v2 2/2 cpufreq: schedutil: Avoid decreasing frequency of busy CPUs

In reply to Rafael J. Wysocki

From: Rafael J. Wysocki <[email protected]>

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.

To work around this issue use the observation that, from the schedutil governor's perspective, it does not make sense to decrease the frequency of a CPU that doesn't enter idle and avoid decreasing the frequency of busy CPUs.

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 it is about to set a new frequency for that CPU's policy. If the counter has not changed since the previous iteration, 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.

---

This is a slightly different approach (avoid decreasing frequency for busy CPUs instead of bumping if for them to the max upfront) and it works around the original problem too.

I tried to address a few Peter's comments here and the result doesn't seem to be too heavy-wieght.

Thanks,
Rafael

---
 include/linux/tick.h             |    1 +
 kernel/sched/cpufreq_schedutil.c |   28 ++++++++++++++++++++++++----
 kernel/time/tick-sched.c         |   12 ++++++++++++
 3 files changed, 37 insertions(+), 4 deletions(-)
kernel/sched/cpufreq_schedutil.c +24 -4
Index: linux-pm/kernel/sched/cpufreq_schedutil.c
===================================================================
@@ -56,6 +56,9 @@ struct sugov_cpu {
 	unsigned long iowait_boost;
 	unsigned long iowait_boost_max;
 	u64 last_update;
+#ifdef CONFIG_NO_HZ_COMMON
+	unsigned long saved_idle_calls;
+#endif
 
 	/* The fields below are only needed when sharing a policy. */
 	unsigned long util;
@@ -88,11 +91,28 @@ static bool sugov_should_update_freq(str
 	return delta_ns >= sg_policy->freq_update_delay_ns;
 }
 
-static void sugov_update_commit(struct sugov_policy *sg_policy, u64 time,
-				unsigned int next_freq)
+#ifdef CONFIG_NO_HZ_COMMON
+static bool sugov_cpu_is_busy(struct sugov_cpu *sg_cpu)
+{
+	unsigned long idle_calls = tick_nohz_get_idle_calls();
+	bool ret = idle_calls == sg_cpu->saved_idle_calls;
+
+	sg_cpu->saved_idle_calls = idle_calls;
+	return ret;
+}
+#else
+static inline bool sugov_cpu_is_busy(struct sugov_cpu *sg_cpu) { return false; }
+#endif /* CONFIG_NO_HZ_COMMON */
+
+static void sugov_update_commit(struct sugov_cpu *sg_cpu,
+				struct sugov_policy *sg_policy,
+				u64 time, unsigned int next_freq)
 {
 	struct cpufreq_policy *policy = sg_policy->policy;
 
+	if (sugov_cpu_is_busy(sg_cpu) && next_freq < sg_policy->next_freq)
+		next_freq = sg_policy->next_freq;
+
 	if (policy->fast_switch_enabled) {
 		if (sg_policy->next_freq == next_freq) {
 			trace_cpu_frequency(policy->cur, smp_processor_id());
@@ -214,7 +234,7 @@ static void sugov_update_single(struct u
 		sugov_iowait_boost(sg_cpu, &util, &max);
 		next_f = get_next_freq(sg_policy, util, max);
 	}
-	sugov_update_commit(sg_policy, time, next_f);
+	sugov_update_commit(sg_cpu, sg_policy, time, next_f);
 }
 
 static unsigned int sugov_next_freq_shared(struct sugov_cpu *sg_cpu)
@@ -283,7 +303,7 @@ static void sugov_update_shared(struct u
 		else
 			next_f = sugov_next_freq_shared(sg_cpu);
 
-		sugov_update_commit(sg_policy, time, next_f);
+		sugov_update_commit(sg_cpu, sg_policy, time, next_f);
 	}
 
 	raw_spin_unlock(&sg_policy->update_lock);
include/linux/tick.h +1 -0
Index: linux-pm/include/linux/tick.h
===================================================================
@@ -117,6 +117,7 @@ extern void tick_nohz_idle_enter(void);
 extern void tick_nohz_idle_exit(void);
 extern void tick_nohz_irq_exit(void);
 extern ktime_t tick_nohz_get_sleep_length(void);
+extern unsigned long tick_nohz_get_idle_calls(void);
 extern u64 get_cpu_idle_time_us(int cpu, u64 *last_update_time);
 extern u64 get_cpu_iowait_time_us(int cpu, u64 *last_update_time);
 #else /* !CONFIG_NO_HZ_COMMON */
kernel/time/tick-sched.c +12 -0
Index: linux-pm/kernel/time/tick-sched.c
===================================================================
@@ -993,6 +993,18 @@ ktime_t tick_nohz_get_sleep_length(void)
 	return ts->sleep_length;
 }
 
+/**
+ * tick_nohz_get_idle_calls - return the current idle calls counter value
+ *
+ * Called from the schedutil frequency scaling governor in scheduler context.
+ */
+unsigned long tick_nohz_get_idle_calls(void)
+{
+	struct tick_sched *ts = this_cpu_ptr(&tick_cpu_sched);
+
+	return ts->idle_calls;
+}
+
 static void tick_nohz_account_idle_ticks(struct tick_sched *ts)
 {
 #ifndef CONFIG_VIRT_CPU_ACCOUNTING_NATIVE
maintainer
#27 lore raw

Re: [RFC][PATCH v2 2/2] cpufreq: schedutil: Avoid decreasing frequency of busy CPUs

kernel/sched/cpufreq_schedutil.c static bool sugov_should_update_freq(str · line 115

Quoted 11 lines · Rafael J. Wysocki

Index: linux-pm/kernel/sched/cpufreq_schedutil.c

kernel/sched/cpufreq_schedutil.c +6 -0
+static void sugov_update_commit(struct sugov_cpu *sg_cpu,
+				struct sugov_policy *sg_policy,
+				u64 time, unsigned int next_freq)
 {
 	struct cpufreq_policy *policy = sg_policy->policy;
 
+	if (sugov_cpu_is_busy(sg_cpu) && next_freq < sg_policy->next_freq)
+		next_freq = sg_policy->next_freq;
+

In the earlier version you said that we want to be opportunistic and don't want to do heavy computation and so check only for current CPU.

But in this version, all those computations are already done by now. Why shouldn't we check all CPUs in the policy now? I am asking as we will still have the same problem, we are trying to work-around if the current CPU isn't busy but others sharing the policy are.

Also, why not return directly from within the if block? To run trace_cpu_frequency()?

I don't remember exactly, but why don't we run that for !fast-switch case? We can simplify the code a bit if we check for no freq change at the top of the routine.

Signature · 1 line
viresh
#28 lore raw

Re: [RFC][PATCH v2 2/2] cpufreq: schedutil: Avoid decreasing frequency of busy CPUs

Quoted 16 lines · Viresh Kumar

kernel/sched/cpufreq_schedutil.c static bool sugov_should_update_freq(str · line 115

Quoted 11 lines · Rafael J. Wysocki

Index: linux-pm/kernel/sched/cpufreq_schedutil.c

kernel/sched/cpufreq_schedutil.c +6 -0
+static void sugov_update_commit(struct sugov_cpu *sg_cpu,
+                             struct sugov_policy *sg_policy,
+                             u64 time, unsigned int next_freq)
 {
      struct cpufreq_policy *policy = sg_policy->policy;
 
+     if (sugov_cpu_is_busy(sg_cpu) && next_freq < sg_policy->next_freq)
+             next_freq = sg_policy->next_freq;
+

In the earlier version you said that we want to be opportunistic and don't want to do heavy computation and so check only for current CPU.

Quoted 4 lines

But in this version, all those computations are already done by now. Why shouldn't we check all CPUs in the policy now? I am asking as we will still have the same problem, we are trying to work-around if the current CPU isn't busy but others sharing the policy are.

This isn't the way I'm looking at that.

This is an easy (and relatively cheap) check to make for the *current* *CPU* and our frequency selection algorithm turns out to have problems, so it would be kind of unreasonable to not use the opportunity to fix up the value coming from it - if we can do that easily enough.

For the other CPUs in the policy that would require extra synchronization etc., so not that easy any more.

Quoted 2 lines

Also, why not return directly from within the if block? To run trace_cpu_frequency()?

Yes.

Quoted 2 lines

I don't remember exactly, but why don't we run that for !fast-switch case?

That's an interesting question.

We do that in the fast switch case, because otherwise utilities get confused if the frequency is not updated for a long enough time.

I'm not really sure why they don't get confused in the other case, though. [In that case the core calls trace_cpu_frequency() for us, but only if we actually run the async work.]

It looks like it wouldn't hurt to always run trace_cpu_frequency() when we want to bail out early for next_freq == sg_policy->next_freq.

Let me prepare a patch for that. :-)

Quoted 2 lines

We can simplify the code a bit if we check for no freq change at the top of the routine.

Right.

Thanks,
Rafael

maintainer
#29 lore raw

Re: [RFC][PATCH v2 2/2] cpufreq: schedutil: Avoid decreasing frequency of busy CPUs

In reply to Rafael J. Wysocki
Quoted 17 lines · Rafael J. Wysocki

From: Rafael J. Wysocki <[email protected]>

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.

Quoted 4 lines

To work around this issue use the observation that, from the schedutil governor's perspective, it does not make sense to decrease the frequency of a CPU that doesn't enter idle and avoid decreasing the frequency of busy CPUs.

I don't fully agree with that statement.
If there are 2 runnable tasks on CPU A and scheduler migrates the waiting task to another CPU B so CPU A is less loaded now, it makes sense to reduce the OPP. That's even for that purpose that we have decided to use scheduler metrics in cpufreq governor so we can adjust OPP immediately when tasks migrate.
That being said, i probably know why you see such OPP switches in your use case. When we migrate a task, we also migrate/remove its utilization from CPU.
If the CPU is not overloaded, it means that runnable tasks have all computation that they need and don't have any reason to use more when a task migrates to another CPU. so decreasing the OPP makes sense because the utilzation is decreasing
If the CPU is overloaded, it means that runnable tasks have to share CPU time and probably don't have all computations that they would like so when a task migrate, the remaining tasks on the CPU will increase their utilization and fill space left by the task that has just migrated. So the CPU's utilization will decrease when a task migrates (and as a result the OPP) but then its utilization will increase with remaining tasks running more time as well as the OPP

So you need to make the difference between this 2 cases: Is a CPU overloaded or not. You can't really rely on the utilization to detect that but you could take advantage of the load which take into account the waiting time of tasks

Vincent

Quoted 128 lines

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 it is about to set a new frequency for that CPU's policy. If the counter has not changed since the previous iteration, 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.

---

This is a slightly different approach (avoid decreasing frequency for busy CPUs instead of bumping if for them to the max upfront) and it works around the original problem too.

I tried to address a few Peter's comments here and the result doesn't seem to be too heavy-wieght.

Thanks,
Rafael

---
 include/linux/tick.h             |    1 +
 kernel/sched/cpufreq_schedutil.c |   28 ++++++++++++++++++++++++----
 kernel/time/tick-sched.c         |   12 ++++++++++++
 3 files changed, 37 insertions(+), 4 deletions(-)

Index: linux-pm/kernel/sched/cpufreq_schedutil.c
===================================================================

kernel/sched/cpufreq_schedutil.c +24 -4
@@ -56,6 +56,9 @@ struct sugov_cpu {
        unsigned long iowait_boost;
        unsigned long iowait_boost_max;
        u64 last_update;
+#ifdef CONFIG_NO_HZ_COMMON
+       unsigned long saved_idle_calls;
+#endif
 
        /* The fields below are only needed when sharing a policy. */
        unsigned long util;
@@ -88,11 +91,28 @@ static bool sugov_should_update_freq(str
        return delta_ns >= sg_policy->freq_update_delay_ns;
 }
 
-static void sugov_update_commit(struct sugov_policy *sg_policy, u64 time,
-                               unsigned int next_freq)
+#ifdef CONFIG_NO_HZ_COMMON
+static bool sugov_cpu_is_busy(struct sugov_cpu *sg_cpu)
+{
+       unsigned long idle_calls = tick_nohz_get_idle_calls();
+       bool ret = idle_calls == sg_cpu->saved_idle_calls;
+
+       sg_cpu->saved_idle_calls = idle_calls;
+       return ret;
+}
+#else
+static inline bool sugov_cpu_is_busy(struct sugov_cpu *sg_cpu) { return false; }
+#endif /* CONFIG_NO_HZ_COMMON */
+
+static void sugov_update_commit(struct sugov_cpu *sg_cpu,
+                               struct sugov_policy *sg_policy,
+                               u64 time, unsigned int next_freq)
 {
        struct cpufreq_policy *policy = sg_policy->policy;
 
+       if (sugov_cpu_is_busy(sg_cpu) && next_freq < sg_policy->next_freq)
+               next_freq = sg_policy->next_freq;
+
        if (policy->fast_switch_enabled) {
                if (sg_policy->next_freq == next_freq) {
                        trace_cpu_frequency(policy->cur, smp_processor_id());
@@ -214,7 +234,7 @@ static void sugov_update_single(struct u
                sugov_iowait_boost(sg_cpu, &util, &max);
                next_f = get_next_freq(sg_policy, util, max);
        }
-       sugov_update_commit(sg_policy, time, next_f);
+       sugov_update_commit(sg_cpu, sg_policy, time, next_f);
 }
 
 static unsigned int sugov_next_freq_shared(struct sugov_cpu *sg_cpu)
@@ -283,7 +303,7 @@ static void sugov_update_shared(struct u
                else
                        next_f = sugov_next_freq_shared(sg_cpu);
 
-               sugov_update_commit(sg_policy, time, next_f);
+               sugov_update_commit(sg_cpu, sg_policy, time, next_f);
        }
 
        raw_spin_unlock(&sg_policy->update_lock);

Index: linux-pm/include/linux/tick.h
===================================================================

include/linux/tick.h +1 -0
@@ -117,6 +117,7 @@ extern void tick_nohz_idle_enter(void);
 extern void tick_nohz_idle_exit(void);
 extern void tick_nohz_irq_exit(void);
 extern ktime_t tick_nohz_get_sleep_length(void);
+extern unsigned long tick_nohz_get_idle_calls(void);
 extern u64 get_cpu_idle_time_us(int cpu, u64 *last_update_time);
 extern u64 get_cpu_iowait_time_us(int cpu, u64 *last_update_time);
 #else /* !CONFIG_NO_HZ_COMMON */

Index: linux-pm/kernel/time/tick-sched.c
===================================================================

kernel/time/tick-sched.c +12 -0
@@ -993,6 +993,18 @@ ktime_t tick_nohz_get_sleep_length(void)
        return ts->sleep_length;
 }
 
+/**
+ * tick_nohz_get_idle_calls - return the current idle calls counter value
+ *
+ * Called from the schedutil frequency scaling governor in scheduler context.
+ */
+unsigned long tick_nohz_get_idle_calls(void)
+{
+       struct tick_sched *ts = this_cpu_ptr(&tick_cpu_sched);
+
+       return ts->idle_calls;
+}
+
 static void tick_nohz_account_idle_ticks(struct tick_sched *ts)
 {
 #ifndef CONFIG_VIRT_CPU_ACCOUNTING_NATIVE
#30 lore raw

Re: [RFC][PATCH v2 2/2] cpufreq: schedutil: Avoid decreasing frequency of busy CPUs

Quoted 44 lines · Vincent Guittot
Quoted 17 lines · Rafael J. Wysocki

From: Rafael J. Wysocki <[email protected]>

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.

Quoted 4 lines

To work around this issue use the observation that, from the schedutil governor's perspective, it does not make sense to decrease the frequency of a CPU that doesn't enter idle and avoid decreasing the frequency of busy CPUs.

I don't fully agree with that statement.
If there are 2 runnable tasks on CPU A and scheduler migrates the waiting task to another CPU B so CPU A is less loaded now, it makes sense to reduce the OPP. That's even for that purpose that we have decided to use scheduler metrics in cpufreq governor so we can adjust OPP immediately when tasks migrate.
That being said, i probably know why you see such OPP switches in your use case. When we migrate a task, we also migrate/remove its utilization from CPU.
If the CPU is not overloaded, it means that runnable tasks have all computation that they need and don't have any reason to use more when a task migrates to another CPU. so decreasing the OPP makes sense because the utilzation is decreasing
If the CPU is overloaded, it means that runnable tasks have to share CPU time and probably don't have all computations that they would like so when a task migrate, the remaining tasks on the CPU will increase their utilization and fill space left by the task that has just migrated. So the CPU's utilization will decrease when a task migrates (and as a result the OPP) but then its utilization will increase with remaining tasks running more time as well as the OPP

Quoted 4 lines

So you need to make the difference between this 2 cases: Is a CPU overloaded or not. You can't really rely on the utilization to detect that but you could take advantage of the load which take into account the waiting time of tasks

Right, we can use "overloaded" for the time being until we push the "overutilized" bits.

[...]

Quoted 5 lines

kernel/sched/cpufreq_schedutil.c static bool sugov_should_update_freq(str · line 98

Quoted 5 lines
kernel/sched/cpufreq_schedutil.c +5 -0
+#ifdef CONFIG_NO_HZ_COMMON
+static bool sugov_cpu_is_busy(struct sugov_cpu *sg_cpu)
+{
+       unsigned long idle_calls = tick_nohz_get_idle_calls();
+       bool ret = idle_calls == sg_cpu->saved_idle_calls;

Vincent: are you proposing something like this?

+ if (this_rq()->rd->overload)
+ return false;

Quoted 28 lines
Quoted 28 lines
kernel/sched/cpufreq_schedutil.c +15 -1
+
+       sg_cpu->saved_idle_calls = idle_calls;
+       return ret;
+}
+#else
+static inline bool sugov_cpu_is_busy(struct sugov_cpu *sg_cpu) { return false; }
+#endif /* CONFIG_NO_HZ_COMMON */
+
+static void sugov_update_commit(struct sugov_cpu *sg_cpu,
+                               struct sugov_policy *sg_policy,
+                               u64 time, unsigned int next_freq)
 {
        struct cpufreq_policy *policy = sg_policy->policy;
 
+       if (sugov_cpu_is_busy(sg_cpu) && next_freq < sg_policy->next_freq)
+               next_freq = sg_policy->next_freq;
+
        if (policy->fast_switch_enabled) {
                if (sg_policy->next_freq == next_freq) {
                        trace_cpu_frequency(policy->cur, smp_processor_id());
@@ -214,7 +234,7 @@ static void sugov_update_single(struct u
                sugov_iowait_boost(sg_cpu, &util, &max);
                next_f = get_next_freq(sg_policy, util, max);
        }
-       sugov_update_commit(sg_policy, time, next_f);
+       sugov_update_commit(sg_cpu, sg_policy, time, next_f);
 }
 

[...]

Signature · 3 lines
#include <best/regards.h>

Patrick Bellasi
maintainer
#31 lore raw

Re: [RFC][PATCH v2 2/2] cpufreq: schedutil: Avoid decreasing frequency of busy CPUs

In reply to Vincent Guittot
Quoted 28 lines · Vincent Guittot
Quoted 4 lines · Rafael J. Wysocki

To work around this issue use the observation that, from the schedutil governor's perspective, it does not make sense to decrease the frequency of a CPU that doesn't enter idle and avoid decreasing the frequency of busy CPUs.

I don't fully agree with that statement.
If there are 2 runnable tasks on CPU A and scheduler migrates the waiting task to another CPU B so CPU A is less loaded now, it makes sense to reduce the OPP. That's even for that purpose that we have decided to use scheduler metrics in cpufreq governor so we can adjust OPP immediately when tasks migrate.
That being said, i probably know why you see such OPP switches in your use case. When we migrate a task, we also migrate/remove its utilization from CPU.
If the CPU is not overloaded, it means that runnable tasks have all computation that they need and don't have any reason to use more when a task migrates to another CPU. so decreasing the OPP makes sense because the utilzation is decreasing
If the CPU is overloaded, it means that runnable tasks have to share CPU time and probably don't have all computations that they would like so when a task migrate, the remaining tasks on the CPU will increase their utilization and fill space left by the task that has just migrated. So the CPU's utilization will decrease when a task migrates (and as a result the OPP) but then its utilization will increase with remaining tasks running more time as well as the OPP

Quoted 4 lines

So you need to make the difference between this 2 cases: Is a CPU overloaded or not. You can't really rely on the utilization to detect that but you could take advantage of the load which take into account the waiting time of tasks

I'm confused. What two cases? You only list the overloaded case, but he does exactly that. Note that the lack of idle time is an exact equivalent of 100% utilized.

So even while we cannot currently detect the 100% utilized state through the running state tracking; because averages etc.. we can detect the lack of idle time.

maintainer
#32 lore raw

Re: [RFC][PATCH v2 2/2] cpufreq: schedutil: Avoid decreasing frequency of busy CPUs

Quoted 29 lines · Peter Zijlstra
Quoted 28 lines · Vincent Guittot
Quoted 4 lines · Rafael J. Wysocki

To work around this issue use the observation that, from the schedutil governor's perspective, it does not make sense to decrease the frequency of a CPU that doesn't enter idle and avoid decreasing the frequency of busy CPUs.

I don't fully agree with that statement.
If there are 2 runnable tasks on CPU A and scheduler migrates the waiting task to another CPU B so CPU A is less loaded now, it makes sense to reduce the OPP. That's even for that purpose that we have decided to use scheduler metrics in cpufreq governor so we can adjust OPP immediately when tasks migrate.
That being said, i probably know why you see such OPP switches in your use case. When we migrate a task, we also migrate/remove its utilization from CPU.
If the CPU is not overloaded, it means that runnable tasks have all computation that they need and don't have any reason to use more when a task migrates to another CPU. so decreasing the OPP makes sense because the utilzation is decreasing
If the CPU is overloaded, it means that runnable tasks have to share CPU time and probably don't have all computations that they would like so when a task migrate, the remaining tasks on the CPU will increase their utilization and fill space left by the task that has just migrated. So the CPU's utilization will decrease when a task migrates (and as a result the OPP) but then its utilization will increase with remaining tasks running more time as well as the OPP

Quoted 6 lines
Quoted 4 lines

So you need to make the difference between this 2 cases: Is a CPU overloaded or not. You can't really rely on the utilization to detect that but you could take advantage of the load which take into account the waiting time of tasks

I'm confused. What two cases? You only list the overloaded case, but he

overloaded vs not overloaded use case.
For the not overloaded case, it makes sense to immediately update to OPP to be aligned with the new utilization of the CPU even if it was not idle in the past couple of ticks

Quoted 6 lines

does exactly that. Note that the lack of idle time is an exact equivalent of 100% utilized.

So even while we cannot currently detect the 100% utilized state through the running state tracking; because averages etc.. we can detect the lack of idle time.

But after how much lack of idle time do we consider that we are overloaded ?

Quoted 1 line
maintainer
#33 lore raw

Re: [RFC][PATCH v2 2/2] cpufreq: schedutil: Avoid decreasing frequency of busy CPUs

Quoted 5 lines · Vincent Guittot

On 21 March 2017 at 14:22, Peter Zijlstra <[email protected]> wrote:

For the not overloaded case, it makes sense to immediately update to OPP to be aligned with the new utilization of the CPU even if it was not idle in the past couple of ticks

Yeah, but we cannot know. Also, who cares?

Quoted 8 lines
Quoted 6 lines

does exactly that. Note that the lack of idle time is an exact equivalent of 100% utilized.

So even while we cannot currently detect the 100% utilized state through the running state tracking; because averages etc.. we can detect the lack of idle time.

But after how much lack of idle time do we consider that we are overloaded ?

0 :-)

Note that utilization is an absolute metric, not a windowed one. That is, there is no actual time associated with it. Now, for practical purposes we end up using windowed things in many places,

maintainer
#34 lore raw

Re: [RFC][PATCH v2 2/2] cpufreq: schedutil: Avoid decreasing frequency of busy CPUs

Quoted 8 lines · Peter Zijlstra
Quoted 5 lines · Vincent Guittot

On 21 March 2017 at 14:22, Peter Zijlstra <[email protected]> wrote:

For the not overloaded case, it makes sense to immediately update to OPP to be aligned with the new utilization of the CPU even if it was not idle in the past couple of ticks

Yeah, but we cannot know. Also, who cares?

embedded system that doesn't want to stay at higest OPP if significant part of the utilzation has moved away as an example
AFAICT, schedutil tries to select the best OPP according to the current utilization of the CPU so if the utilization decreases, the OPP should also decrease

Quoted 16 lines
Quoted 9 lines
Quoted 7 lines

does exactly that. Note that the lack of idle time is an exact equivalent of 100% utilized.

So even while we cannot currently detect the 100% utilized state through the running state tracking; because averages etc.. we can detect the lack of idle time.

But after how much lack of idle time do we consider that we are overloaded ?

0 :-)

Note that utilization is an absolute metric, not a windowed one. That is, there is no actual time associated with it. Now, for practical purposes we end up using windowed things in many places,

#35 lore raw

Re: [RFC][PATCH v2 2/2] cpufreq: schedutil: Avoid decreasing frequency of busy CPUs

In reply to Peter Zijlstra
Quoted 12 lines · Peter Zijlstra
Quoted 5 lines · Vincent Guittot

On 21 March 2017 at 14:22, Peter Zijlstra <[email protected]> wrote:

For the not overloaded case, it makes sense to immediately update to OPP to be aligned with the new utilization of the CPU even if it was not idle in the past couple of ticks

Yeah, but we cannot know. Also, who cares?

Quoted 3 lines
Quoted 3 lines

does exactly that. Note that the lack of idle time is an exact equivalent of 100% utilized.

Quoted 7 lines
Quoted 5 lines
Quoted 3 lines

So even while we cannot currently detect the 100% utilized state through the running state tracking; because averages etc.. we can detect the lack of idle time.

But after how much lack of idle time do we consider that we are overloaded ?

0 :-)

If we should use "utilization" this time can be non 0 and it depends for example on how long PELT takes to build up a utilization value which marks the CPU as "overutilized"... thus we already have a suitable time at least for CFS tasks.

Quoted 4 lines

Note that utilization is an absolute metric, not a windowed one. That is, there is no actual time associated with it. Now, for practical purposes we end up using windowed things in many places,

Signature · 3 lines
#include <best/regards.h>

Patrick Bellasi

A message that is not in the archive (sent off-list or not yet received): CAKfTPtALorn7HNpz4LOfWWSc3u+9y5iHB5byzfTHGQXDA+tVJQ@mail.gmail.com

maintainer
#37 lore raw

Re: [RFC][PATCH v2 2/2] cpufreq: schedutil: Avoid decreasing frequency of busy CPUs

Quoted 9 lines · Vincent Guittot
Quoted 7 lines · Peter Zijlstra
Quoted 5 lines · Vincent Guittot

On 21 March 2017 at 14:22, Peter Zijlstra <[email protected]> wrote:

For the not overloaded case, it makes sense to immediately update to OPP to be aligned with the new utilization of the CPU even if it was not idle in the past couple of ticks

Quoted 8 lines
Quoted 2 lines

Yeah, but we cannot know. Also, who cares?

embedded system that doesn't want to stay at higest OPP if significant part of the utilzation has moved away as an example
AFAICT, schedutil tries to select the best OPP according to the current utilization of the CPU so if the utilization decreases, the OPP should also decrease

Sure I get that; but given the lack of crystal ball instructions we cannot know if this is the case or not.

And if we really dropped below 100% utilization, we should hit idle fairly soon.

maintainer
#38 lore raw

Re: [RFC][PATCH v2 2/2] cpufreq: schedutil: Avoid decreasing frequency of busy CPUs

Quoted 14 lines · Peter Zijlstra
Quoted 12 lines · Vincent Guittot
Quoted 9 lines · Peter Zijlstra
Quoted 5 lines · Vincent Guittot

On 21 March 2017 at 14:22, Peter Zijlstra <[email protected]> wrote:

For the not overloaded case, it makes sense to immediately update to OPP to be aligned with the new utilization of the CPU even if it was not idle in the past couple of ticks

Yeah, but we cannot know. Also, who cares?

Quoted 8 lines
Quoted 5 lines

embedded system that doesn't want to stay at higest OPP if significant part of the utilzation has moved away as an example
AFAICT, schedutil tries to select the best OPP according to the current utilization of the CPU so if the utilization decreases, the OPP should also decrease

Sure I get that; but given the lack of crystal ball instructions we cannot know if this is the case or not.

cfs_rq->avg.load_avg account the waiting time of CPU (in addition to the weight of task) so i was wondering if we can't use it to detect if we are in the overloaded case or not even if utilization is not mac capacity because we have just migrated a task (and its utilization) out

Quoted 3 lines

And if we really dropped below 100% utilization, we should hit idle fairly soon.

maintainer
#39 lore raw

Re: [RFC][PATCH v2 2/2] cpufreq: schedutil: Avoid decreasing frequency of busy CPUs

Quoted 21 lines · Vincent Guittot
Quoted 20 lines · Peter Zijlstra
Quoted 17 lines · Vincent Guittot
Quoted 9 lines · Peter Zijlstra
Quoted 5 lines · Vincent Guittot

On 21 March 2017 at 14:22, Peter Zijlstra <[email protected]> wrote:

For the not overloaded case, it makes sense to immediately update to OPP to be aligned with the new utilization of the CPU even if it was not idle in the past couple of ticks

Yeah, but we cannot know. Also, who cares?

embedded system that doesn't want to stay at higest OPP if significant part of the utilzation has moved away as an example
AFAICT, schedutil tries to select the best OPP according to the current utilization of the CPU so if the utilization decreases, the OPP should also decrease

Quoted 4 lines
Quoted 2 lines

Sure I get that; but given the lack of crystal ball instructions we cannot know if this is the case or not.

cfs_rq->avg.load_avg account the waiting time of CPU (in addition to

sorry i wanted to say the waiting time of tasks on the CPU

Quoted 10 lines

the weight of task) so i was wondering if we can't use it to detect if we are in the overloaded case or not even if utilization is not mac capacity because we have just migrated a task (and its utilization) out

Quoted 3 lines

And if we really dropped below 100% utilization, we should hit idle fairly soon.

#40 lore raw

Re: [RFC][PATCH v2 2/2] cpufreq: schedutil: Avoid decreasing frequency of busy CPUs

In reply to Vincent Guittot
Quoted 35 lines · Vincent Guittot
Quoted 34 lines · Peter Zijlstra
Quoted 32 lines · Vincent Guittot
Quoted 4 lines · Rafael J. Wysocki

To work around this issue use the observation that, from the schedutil governor's perspective, it does not make sense to decrease the frequency of a CPU that doesn't enter idle and avoid decreasing the frequency of busy CPUs.

I don't fully agree with that statement.
If there are 2 runnable tasks on CPU A and scheduler migrates the waiting task to another CPU B so CPU A is less loaded now, it makes sense to reduce the OPP. That's even for that purpose that we have decided to use scheduler metrics in cpufreq governor so we can adjust OPP immediately when tasks migrate.
That being said, i probably know why you see such OPP switches in your use case. When we migrate a task, we also migrate/remove its utilization from CPU.
If the CPU is not overloaded, it means that runnable tasks have all computation that they need and don't have any reason to use more when a task migrates to another CPU. so decreasing the OPP makes sense because the utilzation is decreasing
If the CPU is overloaded, it means that runnable tasks have to share CPU time and probably don't have all computations that they would like so when a task migrate, the remaining tasks on the CPU will increase their utilization and fill space left by the task that has just migrated. So the CPU's utilization will decrease when a task migrates (and as a result the OPP) but then its utilization will increase with remaining tasks running more time as well as the OPP

So you need to make the difference between this 2 cases: Is a CPU overloaded or not. You can't really rely on the utilization to detect that but you could take advantage of the load which take into account the waiting time of tasks

Quoted 6 lines
Quoted 1 line

I'm confused. What two cases? You only list the overloaded case, but he

overloaded vs not overloaded use case.
For the not overloaded case, it makes sense to immediately update to OPP to be aligned with the new utilization of the CPU even if it was not idle in the past couple of ticks

Yes, if the OPP (or P-state if you will) can be changed immediately. If it can't, conditions may change by the time we actually update it and in that case It'd be better to wait and see IMO.

In any case, the theory about migrating tasks made sense to me, so below is what I tested. It works, and besides it has a nice feature that I don't need to fetch for the timekeeping data. :-)

I only wonder if we want to do this or only prevent the frequency from decreasing in the overloaded case?

---
 kernel/sched/cpufreq_schedutil.c |    8 +++++---
 1 file changed, 5 insertions(+), 3 deletions(-)
kernel/sched/cpufreq_schedutil.c +5 -3
Index: linux-pm/kernel/sched/cpufreq_schedutil.c
===================================================================
@@ -61,6 +61,7 @@ struct sugov_cpu {
 	unsigned long util;
 	unsigned long max;
 	unsigned int flags;
+	bool overload;
 };
 
 static DEFINE_PER_CPU(struct sugov_cpu, sugov_cpu);
@@ -207,7 +208,7 @@ static void sugov_update_single(struct u
 	if (!sugov_should_update_freq(sg_policy, time))
 		return;
 
-	if (flags & SCHED_CPUFREQ_RT_DL) {
+	if ((flags & SCHED_CPUFREQ_RT_DL) || this_rq()->rd->overload) {
 		next_f = policy->cpuinfo.max_freq;
 	} else {
 		sugov_get_util(&util, &max);
@@ -242,7 +243,7 @@ static unsigned int sugov_next_freq_shar
 			j_sg_cpu->iowait_boost = 0;
 			continue;
 		}
-		if (j_sg_cpu->flags & SCHED_CPUFREQ_RT_DL)
+		if ((j_sg_cpu->flags & SCHED_CPUFREQ_RT_DL) || j_sg_cpu->overload)
 			return policy->cpuinfo.max_freq;
 
 		j_util = j_sg_cpu->util;
@@ -273,12 +274,13 @@ static void sugov_update_shared(struct u
 	sg_cpu->util = util;
 	sg_cpu->max = max;
 	sg_cpu->flags = flags;
+	sg_cpu->overload = this_rq()->rd->overload;
 
 	sugov_set_iowait_boost(sg_cpu, time, flags);
 	sg_cpu->last_update = time;
 
 	if (sugov_should_update_freq(sg_policy, time)) {
-		if (flags & SCHED_CPUFREQ_RT_DL)
+		if ((flags & SCHED_CPUFREQ_RT_DL) || sg_cpu->overload)
 			next_f = sg_policy->policy->cpuinfo.max_freq;
 		else
 			next_f = sugov_next_freq_shared(sg_cpu);
#41 lore raw

Re: [RFC][PATCH v2 2/2] cpufreq: schedutil: Avoid decreasing frequency of busy CPUs

Quoted 71 lines · Rafael J. Wysocki
Quoted 35 lines · Vincent Guittot
Quoted 34 lines · Peter Zijlstra
Quoted 32 lines · Vincent Guittot
Quoted 4 lines · Rafael J. Wysocki

To work around this issue use the observation that, from the schedutil governor's perspective, it does not make sense to decrease the frequency of a CPU that doesn't enter idle and avoid decreasing the frequency of busy CPUs.

I don't fully agree with that statement.
If there are 2 runnable tasks on CPU A and scheduler migrates the waiting task to another CPU B so CPU A is less loaded now, it makes sense to reduce the OPP. That's even for that purpose that we have decided to use scheduler metrics in cpufreq governor so we can adjust OPP immediately when tasks migrate.
That being said, i probably know why you see such OPP switches in your use case. When we migrate a task, we also migrate/remove its utilization from CPU.
If the CPU is not overloaded, it means that runnable tasks have all computation that they need and don't have any reason to use more when a task migrates to another CPU. so decreasing the OPP makes sense because the utilzation is decreasing
If the CPU is overloaded, it means that runnable tasks have to share CPU time and probably don't have all computations that they would like so when a task migrate, the remaining tasks on the CPU will increase their utilization and fill space left by the task that has just migrated. So the CPU's utilization will decrease when a task migrates (and as a result the OPP) but then its utilization will increase with remaining tasks running more time as well as the OPP

So you need to make the difference between this 2 cases: Is a CPU overloaded or not. You can't really rely on the utilization to detect that but you could take advantage of the load which take into account the waiting time of tasks

Quoted 6 lines
Quoted 1 line

I'm confused. What two cases? You only list the overloaded case, but he

overloaded vs not overloaded use case.
For the not overloaded case, it makes sense to immediately update to OPP to be aligned with the new utilization of the CPU even if it was not idle in the past couple of ticks

Yes, if the OPP (or P-state if you will) can be changed immediately. If it can't, conditions may change by the time we actually update it and in that case It'd be better to wait and see IMO.

In any case, the theory about migrating tasks made sense to me, so below is what I tested. It works, and besides it has a nice feature that I don't need to fetch for the timekeeping data. :-)

I only wonder if we want to do this or only prevent the frequency from decreasing in the overloaded case?

---
 kernel/sched/cpufreq_schedutil.c |    8 +++++---
 1 file changed, 5 insertions(+), 3 deletions(-)

Index: linux-pm/kernel/sched/cpufreq_schedutil.c
===================================================================

kernel/sched/cpufreq_schedutil.c +1 -0
@@ -61,6 +61,7 @@ struct sugov_cpu {
 	unsigned long util;
 	unsigned long max;
 	unsigned int flags;
+	bool overload;
 };
 
 static DEFINE_PER_CPU(struct sugov_cpu, sugov_cpu);

kernel/sched/cpufreq_schedutil.c static void sugov_update_single(struct u · line 212

Quoted 6 lines
kernel/sched/cpufreq_schedutil.c +1 -1
@@ -207,7 +208,7 @@ static void sugov_update_single(struct u
 	if (!sugov_should_update_freq(sg_policy, time))
 		return;
 
-	if (flags & SCHED_CPUFREQ_RT_DL) {
+	if ((flags & SCHED_CPUFREQ_RT_DL) || this_rq()->rd->overload) {
 		next_f = policy->cpuinfo.max_freq;

Isn't this going to max OPP every time we have more than 1 task in that CPU?

In that case it will not fit the case: we have two 10% tasks on that CPU.

Previous solution was better IMO, apart from using overloaded instead of overutilized (which is not yet there) :-/

Quoted 27 lines
kernel/sched/cpufreq_schedutil.c +3 -2
 	} else {
 		sugov_get_util(&util, &max);
@@ -242,7 +243,7 @@ static unsigned int sugov_next_freq_shar
 			j_sg_cpu->iowait_boost = 0;
 			continue;
 		}
-		if (j_sg_cpu->flags & SCHED_CPUFREQ_RT_DL)
+		if ((j_sg_cpu->flags & SCHED_CPUFREQ_RT_DL) || j_sg_cpu->overload)
 			return policy->cpuinfo.max_freq;
 
 		j_util = j_sg_cpu->util;
@@ -273,12 +274,13 @@ static void sugov_update_shared(struct u
 	sg_cpu->util = util;
 	sg_cpu->max = max;
 	sg_cpu->flags = flags;
+	sg_cpu->overload = this_rq()->rd->overload;
 
 	sugov_set_iowait_boost(sg_cpu, time, flags);
 	sg_cpu->last_update = time;
 
 	if (sugov_should_update_freq(sg_policy, time)) {
-		if (flags & SCHED_CPUFREQ_RT_DL)
+		if ((flags & SCHED_CPUFREQ_RT_DL) || sg_cpu->overload)
 			next_f = sg_policy->policy->cpuinfo.max_freq;
 		else
 			next_f = sugov_next_freq_shared(sg_cpu);
Signature · 3 lines
#include <best/regards.h>

Patrick Bellasi
#42 lore raw

Re: [RFC][PATCH v2 2/2] cpufreq: schedutil: Avoid decreasing frequency of busy CPUs

Quoted 75 lines · Patrick Bellasi
Quoted 74 lines · Rafael J. Wysocki
Quoted 35 lines · Vincent Guittot
Quoted 34 lines · Peter Zijlstra
Quoted 32 lines · Vincent Guittot
Quoted 4 lines · Rafael J. Wysocki

To work around this issue use the observation that, from the schedutil governor's perspective, it does not make sense to decrease the frequency of a CPU that doesn't enter idle and avoid decreasing the frequency of busy CPUs.

I don't fully agree with that statement.
If there are 2 runnable tasks on CPU A and scheduler migrates the waiting task to another CPU B so CPU A is less loaded now, it makes sense to reduce the OPP. That's even for that purpose that we have decided to use scheduler metrics in cpufreq governor so we can adjust OPP immediately when tasks migrate.
That being said, i probably know why you see such OPP switches in your use case. When we migrate a task, we also migrate/remove its utilization from CPU.
If the CPU is not overloaded, it means that runnable tasks have all computation that they need and don't have any reason to use more when a task migrates to another CPU. so decreasing the OPP makes sense because the utilzation is decreasing
If the CPU is overloaded, it means that runnable tasks have to share CPU time and probably don't have all computations that they would like so when a task migrate, the remaining tasks on the CPU will increase their utilization and fill space left by the task that has just migrated. So the CPU's utilization will decrease when a task migrates (and as a result the OPP) but then its utilization will increase with remaining tasks running more time as well as the OPP

So you need to make the difference between this 2 cases: Is a CPU overloaded or not. You can't really rely on the utilization to detect that but you could take advantage of the load which take into account the waiting time of tasks

Quoted 6 lines
Quoted 1 line

I'm confused. What two cases? You only list the overloaded case, but he

overloaded vs not overloaded use case.
For the not overloaded case, it makes sense to immediately update to OPP to be aligned with the new utilization of the CPU even if it was not idle in the past couple of ticks

Yes, if the OPP (or P-state if you will) can be changed immediately. If it can't, conditions may change by the time we actually update it and in that case It'd be better to wait and see IMO.

In any case, the theory about migrating tasks made sense to me, so below is what I tested. It works, and besides it has a nice feature that I don't need to fetch for the timekeeping data. :-)

I only wonder if we want to do this or only prevent the frequency from decreasing in the overloaded case?

---
 kernel/sched/cpufreq_schedutil.c |    8 +++++---
 1 file changed, 5 insertions(+), 3 deletions(-)

Index: linux-pm/kernel/sched/cpufreq_schedutil.c
===================================================================

kernel/sched/cpufreq_schedutil.c +1 -0
@@ -61,6 +61,7 @@ struct sugov_cpu {
 	unsigned long util;
 	unsigned long max;
 	unsigned int flags;
+	bool overload;
 };
 
 static DEFINE_PER_CPU(struct sugov_cpu, sugov_cpu);
@@ -207,7 +208,7 @@ static void sugov_update_single(struct u
 	if (!sugov_should_update_freq(sg_policy, time))
 		return;
 
Quoted 8 lines

kernel/sched/cpufreq_schedutil.c static void sugov_update_single(struct u · line 212

Quoted 3 lines
kernel/sched/cpufreq_schedutil.c +1 -1
-	if (flags & SCHED_CPUFREQ_RT_DL) {
+	if ((flags & SCHED_CPUFREQ_RT_DL) || this_rq()->rd->overload) {
 		next_f = policy->cpuinfo.max_freq;

Isn't this going to max OPP every time we have more than 1 task in that CPU?

In that case it will not fit the case: we have two 10% tasks on that CPU.

Good point.

Quoted 2 lines

Previous solution was better IMO, apart from using overloaded instead of overutilized (which is not yet there) :-/

OK, so the one below works too.

---
 kernel/sched/cpufreq_schedutil.c |   11 +++++++++++
 1 file changed, 11 insertions(+)
kernel/sched/cpufreq_schedutil.c +11 -0
Index: linux-pm/kernel/sched/cpufreq_schedutil.c
===================================================================
@@ -37,6 +37,7 @@ struct sugov_policy {
 	s64 freq_update_delay_ns;
 	unsigned int next_freq;
 	unsigned int cached_raw_freq;
+	bool overload;
 
 	/* The next fields are only needed if fast switch cannot be used. */
 	struct irq_work irq_work;
@@ -61,6 +62,7 @@ struct sugov_cpu {
 	unsigned long util;
 	unsigned long max;
 	unsigned int flags;
+	bool overload;
 };
 
 static DEFINE_PER_CPU(struct sugov_cpu, sugov_cpu);
@@ -93,6 +95,9 @@ static void sugov_update_commit(struct s
 {
 	struct cpufreq_policy *policy = sg_policy->policy;
 
+	if (sg_policy->overload && next_freq < sg_policy->next_freq)
+		next_freq = sg_policy->next_freq;
+
 	if (policy->fast_switch_enabled) {
 		if (sg_policy->next_freq == next_freq) {
 			trace_cpu_frequency(policy->cur, smp_processor_id());
@@ -207,6 +212,8 @@ static void sugov_update_single(struct u
 	if (!sugov_should_update_freq(sg_policy, time))
 		return;
 
+	sg_policy->overload = this_rq()->rd->overload;
+
 	if (flags & SCHED_CPUFREQ_RT_DL) {
 		next_f = policy->cpuinfo.max_freq;
 	} else {
@@ -225,6 +232,8 @@ static unsigned int sugov_next_freq_shar
 	unsigned long util = 0, max = 1;
 	unsigned int j;
 
+	sg_policy->overload = false;
+
 	for_each_cpu(j, policy->cpus) {
 		struct sugov_cpu *j_sg_cpu = &per_cpu(sugov_cpu, j);
 		unsigned long j_util, j_max;
@@ -253,6 +262,7 @@ static unsigned int sugov_next_freq_shar
 		}
 
 		sugov_iowait_boost(j_sg_cpu, &util, &max);
+		sg_policy->overload = sg_policy->overload || sg_cpu->overload;
 	}
 
 	return get_next_freq(sg_policy, util, max);
@@ -273,6 +283,7 @@ static void sugov_update_shared(struct u
 	sg_cpu->util = util;
 	sg_cpu->max = max;
 	sg_cpu->flags = flags;
+	sg_cpu->overload = this_rq()->rd->overload;
 
 	sugov_set_iowait_boost(sg_cpu, time, flags);
 	sg_cpu->last_update = time;
#43 lore raw

Re: [RFC][PATCH v2 2/2] cpufreq: schedutil: Avoid decreasing frequency of busy CPUs

Quoted 83 lines · Rafael J. Wysocki
Quoted 82 lines · Patrick Bellasi
Quoted 71 lines · Rafael J. Wysocki
Quoted 35 lines · Vincent Guittot
Quoted 34 lines · Peter Zijlstra
Quoted 32 lines · Vincent Guittot
Quoted 4 lines · Rafael J. Wysocki

To work around this issue use the observation that, from the schedutil governor's perspective, it does not make sense to decrease the frequency of a CPU that doesn't enter idle and avoid decreasing the frequency of busy CPUs.

I don't fully agree with that statement.
If there are 2 runnable tasks on CPU A and scheduler migrates the waiting task to another CPU B so CPU A is less loaded now, it makes sense to reduce the OPP. That's even for that purpose that we have decided to use scheduler metrics in cpufreq governor so we can adjust OPP immediately when tasks migrate.
That being said, i probably know why you see such OPP switches in your use case. When we migrate a task, we also migrate/remove its utilization from CPU.
If the CPU is not overloaded, it means that runnable tasks have all computation that they need and don't have any reason to use more when a task migrates to another CPU. so decreasing the OPP makes sense because the utilzation is decreasing
If the CPU is overloaded, it means that runnable tasks have to share CPU time and probably don't have all computations that they would like so when a task migrate, the remaining tasks on the CPU will increase their utilization and fill space left by the task that has just migrated. So the CPU's utilization will decrease when a task migrates (and as a result the OPP) but then its utilization will increase with remaining tasks running more time as well as the OPP

So you need to make the difference between this 2 cases: Is a CPU overloaded or not. You can't really rely on the utilization to detect that but you could take advantage of the load which take into account the waiting time of tasks

Quoted 6 lines
Quoted 1 line

I'm confused. What two cases? You only list the overloaded case, but he

overloaded vs not overloaded use case.
For the not overloaded case, it makes sense to immediately update to OPP to be aligned with the new utilization of the CPU even if it was not idle in the past couple of ticks

Yes, if the OPP (or P-state if you will) can be changed immediately. If it can't, conditions may change by the time we actually update it and in that case It'd be better to wait and see IMO.

In any case, the theory about migrating tasks made sense to me, so below is what I tested. It works, and besides it has a nice feature that I don't need to fetch for the timekeeping data. :-)

I only wonder if we want to do this or only prevent the frequency from decreasing in the overloaded case?

---
 kernel/sched/cpufreq_schedutil.c |    8 +++++---
 1 file changed, 5 insertions(+), 3 deletions(-)

Index: linux-pm/kernel/sched/cpufreq_schedutil.c
===================================================================

kernel/sched/cpufreq_schedutil.c +1 -0
@@ -61,6 +61,7 @@ struct sugov_cpu {
 	unsigned long util;
 	unsigned long max;
 	unsigned int flags;
+	bool overload;
 };
 
 static DEFINE_PER_CPU(struct sugov_cpu, sugov_cpu);

kernel/sched/cpufreq_schedutil.c static void sugov_update_single(struct u · line 212

Quoted 6 lines
kernel/sched/cpufreq_schedutil.c +1 -1
@@ -207,7 +208,7 @@ static void sugov_update_single(struct u
 	if (!sugov_should_update_freq(sg_policy, time))
 		return;
 
-	if (flags & SCHED_CPUFREQ_RT_DL) {
+	if ((flags & SCHED_CPUFREQ_RT_DL) || this_rq()->rd->overload) {
 		next_f = policy->cpuinfo.max_freq;

Isn't this going to max OPP every time we have more than 1 task in that CPU?

Quoted 8 lines
Quoted 1 line

In that case it will not fit the case: we have two 10% tasks on that CPU.

Good point.

Quoted 2 lines

Previous solution was better IMO, apart from using overloaded instead of overutilized (which is not yet there) :-/

OK, so the one below works too.

Admittedly, we could check the idle condition and the overload flag at the same time, though.

Let me try that too.

Thanks,
Rafael

maintainer
#44 lore raw

Re: [RFC][PATCH v2 2/2] cpufreq: schedutil: Avoid decreasing frequency of busy CPUs

In reply to Rafael J. Wysocki

kernel/sched/cpufreq_schedutil.c static void sugov_update_single(struct u · line 216

Quoted 6 lines · Rafael J. Wysocki
kernel/sched/cpufreq_schedutil.c +2 -0
@@ -207,6 +212,8 @@ static void sugov_update_single(struct u
 	if (!sugov_should_update_freq(sg_policy, time))
 		return;
 
+	sg_policy->overload = this_rq()->rd->overload;
+

Same problem as before; rd->overload is set if _any_ CPU in the root domain has more than 1 runnable task at a random point in history (when we ran the load balance tick -- and since that is the same tick used for timers, there's a bias to over-account there).

#45 lore raw

Re: [RFC][PATCH v2 2/2] cpufreq: schedutil: Avoid decreasing frequency of busy CPUs

Quoted 5 lines · Peter Zijlstra
Quoted 4 lines · Rafael J. Wysocki
(quoted diff) +0 -0
@@ -207,6 +212,8 @@ static void sugov_update_single(struct u
 	if (!sugov_should_update_freq(sg_policy, time))
 		return;
 
Quoted 7 lines

Review static void sugov_update_single(struct u · line 216

Quoted 2 lines
(quoted diff) +2 -0
+	sg_policy->overload = this_rq()->rd->overload;
+

Same problem as before; rd->overload is set if _any_ CPU in the root domain has more than 1 runnable task at a random point in history (when we ran the load balance tick -- and since that is the same tick used for timers, there's a bias to over-account there).

OK

What about the one below then?

It checks both the idle calls count and overload and only then it will prevent the frequency from being decreased.

It is sufficient for the case at hand.

I guess if rd->overload is not set, this means that none of the CPUs is oversubscribed and we just saturate the capacity in a one-task-per-CPU kind of fashion. Right?

---
 include/linux/tick.h             |    1 +
 kernel/sched/cpufreq_schedutil.c |   27 +++++++++++++++++++++++++++
 kernel/time/tick-sched.c         |   12 ++++++++++++
 3 files changed, 40 insertions(+)
kernel/sched/cpufreq_schedutil.c +27 -0
Index: linux-pm/kernel/sched/cpufreq_schedutil.c
===================================================================
@@ -37,6 +37,7 @@ struct sugov_policy {
 	s64 freq_update_delay_ns;
 	unsigned int next_freq;
 	unsigned int cached_raw_freq;
+	bool busy;
 
 	/* The next fields are only needed if fast switch cannot be used. */
 	struct irq_work irq_work;
@@ -56,11 +57,15 @@ struct sugov_cpu {
 	unsigned long iowait_boost;
 	unsigned long iowait_boost_max;
 	u64 last_update;
+#ifdef CONFIG_NO_HZ_COMMON
+	unsigned long saved_idle_calls;
+#endif
 
 	/* The fields below are only needed when sharing a policy. */
 	unsigned long util;
 	unsigned long max;
 	unsigned int flags;
+	bool busy;
 };
 
 static DEFINE_PER_CPU(struct sugov_cpu, sugov_cpu);
@@ -93,6 +98,9 @@ static void sugov_update_commit(struct s
 {
 	struct cpufreq_policy *policy = sg_policy->policy;
 
+	if (sg_policy->busy && next_freq < sg_policy->next_freq)
+		next_freq = sg_policy->next_freq;
+
 	if (policy->fast_switch_enabled) {
 		if (sg_policy->next_freq == next_freq) {
 			trace_cpu_frequency(policy->cur, smp_processor_id());
@@ -192,6 +200,19 @@ static void sugov_iowait_boost(struct su
 	sg_cpu->iowait_boost >>= 1;
 }
 
+#ifdef CONFIG_NO_HZ_COMMON
+static bool sugov_cpu_is_busy(struct sugov_cpu *sg_cpu)
+{
+	unsigned long idle_calls = tick_nohz_get_idle_calls();
+	bool not_idle = idle_calls == sg_cpu->saved_idle_calls;
+
+	sg_cpu->saved_idle_calls = idle_calls;
+	return not_idle && this_rq()->rd->overload;
+}
+#else
+static inline bool sugov_cpu_is_busy(struct sugov_cpu *sg_cpu) { return false; }
+#endif /* CONFIG_NO_HZ_COMMON */
+
 static void sugov_update_single(struct update_util_data *hook, u64 time,
 				unsigned int flags)
 {
@@ -207,6 +228,8 @@ static void sugov_update_single(struct u
 	if (!sugov_should_update_freq(sg_policy, time))
 		return;
 
+	sg_policy->busy = sugov_cpu_is_busy(sg_cpu);
+
 	if (flags & SCHED_CPUFREQ_RT_DL) {
 		next_f = policy->cpuinfo.max_freq;
 	} else {
@@ -225,6 +248,8 @@ static unsigned int sugov_next_freq_shar
 	unsigned long util = 0, max = 1;
 	unsigned int j;
 
+	sg_policy->busy = false;
+
 	for_each_cpu(j, policy->cpus) {
 		struct sugov_cpu *j_sg_cpu = &per_cpu(sugov_cpu, j);
 		unsigned long j_util, j_max;
@@ -253,6 +278,7 @@ static unsigned int sugov_next_freq_shar
 		}
 
 		sugov_iowait_boost(j_sg_cpu, &util, &max);
+		sg_policy->busy = sg_policy->busy || sg_cpu->busy;
 	}
 
 	return get_next_freq(sg_policy, util, max);
@@ -273,6 +299,7 @@ static void sugov_update_shared(struct u
 	sg_cpu->util = util;
 	sg_cpu->max = max;
 	sg_cpu->flags = flags;
+	sg_cpu->busy = sugov_cpu_is_busy(sg_cpu);
 
 	sugov_set_iowait_boost(sg_cpu, time, flags);
 	sg_cpu->last_update = time;
include/linux/tick.h +1 -0
Index: linux-pm/include/linux/tick.h
===================================================================
@@ -117,6 +117,7 @@ extern void tick_nohz_idle_enter(void);
 extern void tick_nohz_idle_exit(void);
 extern void tick_nohz_irq_exit(void);
 extern ktime_t tick_nohz_get_sleep_length(void);
+extern unsigned long tick_nohz_get_idle_calls(void);
 extern u64 get_cpu_idle_time_us(int cpu, u64 *last_update_time);
 extern u64 get_cpu_iowait_time_us(int cpu, u64 *last_update_time);
 #else /* !CONFIG_NO_HZ_COMMON */
kernel/time/tick-sched.c +12 -0
Index: linux-pm/kernel/time/tick-sched.c
===================================================================
@@ -993,6 +993,18 @@ ktime_t tick_nohz_get_sleep_length(void)
 	return ts->sleep_length;
 }
 
+/**
+ * tick_nohz_get_idle_calls - return the current idle calls counter value
+ *
+ * Called from the schedutil frequency scaling governor in scheduler context.
+ */
+unsigned long tick_nohz_get_idle_calls(void)
+{
+	struct tick_sched *ts = this_cpu_ptr(&tick_cpu_sched);
+
+	return ts->idle_calls;
+}
+
 static void tick_nohz_account_idle_ticks(struct tick_sched *ts)
 {
 #ifndef CONFIG_VIRT_CPU_ACCOUNTING_NATIVE
maintainer
#46 lore raw

Re: [RFC][PATCH v2 2/2] cpufreq: schedutil: Avoid decreasing frequency of busy CPUs

kernel/sched/cpufreq_schedutil.c static void sugov_iowait_boost(struct su · line 211

Quoted 8 lines · Rafael J. Wysocki
kernel/sched/cpufreq_schedutil.c +8 -0
+static bool sugov_cpu_is_busy(struct sugov_cpu *sg_cpu)
+{
+	unsigned long idle_calls = tick_nohz_get_idle_calls();
+	bool not_idle = idle_calls == sg_cpu->saved_idle_calls;
+
+	sg_cpu->saved_idle_calls = idle_calls;
+	return not_idle && this_rq()->rd->overload;
+}

So I really don't understand the rd->overload thing. What is it supposed to do here?

#47 lore raw

Re: [RFC][PATCH v2 2/2] cpufreq: schedutil: Avoid decreasing frequency of busy CPUs

Quoted 12 lines · Peter Zijlstra

kernel/sched/cpufreq_schedutil.c static void sugov_iowait_boost(struct su · line 211

Quoted 8 lines · Rafael J. Wysocki
kernel/sched/cpufreq_schedutil.c +8 -0
+static bool sugov_cpu_is_busy(struct sugov_cpu *sg_cpu)
+{
+	unsigned long idle_calls = tick_nohz_get_idle_calls();
+	bool not_idle = idle_calls == sg_cpu->saved_idle_calls;
+
+	sg_cpu->saved_idle_calls = idle_calls;
+	return not_idle && this_rq()->rd->overload;
+}

So I really don't understand the rd->overload thing. What is it supposed to do here?

The idea was that if the CPU was running one task saturating the capacity which then was migrated out of it, the frequency should still be reduced.

And since rd->overload covers all CPUs (in general) it kind of tells us whether or not there are other tasks to replace the migrated one any time soon.

However, if there are no tasks to replace the migrated one, the CPU will go idle quickly (as there are no taks to run on it), in which case keeping the current frequency on it shouldn't matter.

In all of the other cases keeping the current frequency is the right thing to do IMO.

So, it looks like checking this_rq()->rd->overload doesn't really help after all. :-)

Thanks,
Rafael

#48 lore raw

Re: [RFC][PATCH v2 2/2] cpufreq: schedutil: Avoid decreasing frequency of busy CPUs

In reply to Rafael J. Wysocki
Quoted 83 lines · Rafael J. Wysocki
Quoted 82 lines · Patrick Bellasi
Quoted 71 lines · Rafael J. Wysocki
Quoted 35 lines · Vincent Guittot
Quoted 34 lines · Peter Zijlstra
Quoted 32 lines · Vincent Guittot
Quoted 4 lines · Rafael J. Wysocki

To work around this issue use the observation that, from the schedutil governor's perspective, it does not make sense to decrease the frequency of a CPU that doesn't enter idle and avoid decreasing the frequency of busy CPUs.

I don't fully agree with that statement.
If there are 2 runnable tasks on CPU A and scheduler migrates the waiting task to another CPU B so CPU A is less loaded now, it makes sense to reduce the OPP. That's even for that purpose that we have decided to use scheduler metrics in cpufreq governor so we can adjust OPP immediately when tasks migrate.
That being said, i probably know why you see such OPP switches in your use case. When we migrate a task, we also migrate/remove its utilization from CPU.
If the CPU is not overloaded, it means that runnable tasks have all computation that they need and don't have any reason to use more when a task migrates to another CPU. so decreasing the OPP makes sense because the utilzation is decreasing
If the CPU is overloaded, it means that runnable tasks have to share CPU time and probably don't have all computations that they would like so when a task migrate, the remaining tasks on the CPU will increase their utilization and fill space left by the task that has just migrated. So the CPU's utilization will decrease when a task migrates (and as a result the OPP) but then its utilization will increase with remaining tasks running more time as well as the OPP

So you need to make the difference between this 2 cases: Is a CPU overloaded or not. You can't really rely on the utilization to detect that but you could take advantage of the load which take into account the waiting time of tasks

Quoted 6 lines
Quoted 1 line

I'm confused. What two cases? You only list the overloaded case, but he

overloaded vs not overloaded use case.
For the not overloaded case, it makes sense to immediately update to OPP to be aligned with the new utilization of the CPU even if it was not idle in the past couple of ticks

Yes, if the OPP (or P-state if you will) can be changed immediately. If it can't, conditions may change by the time we actually update it and in that case It'd be better to wait and see IMO.

In any case, the theory about migrating tasks made sense to me, so below is what I tested. It works, and besides it has a nice feature that I don't need to fetch for the timekeeping data. :-)

I only wonder if we want to do this or only prevent the frequency from decreasing in the overloaded case?

---
 kernel/sched/cpufreq_schedutil.c |    8 +++++---
 1 file changed, 5 insertions(+), 3 deletions(-)

Index: linux-pm/kernel/sched/cpufreq_schedutil.c
===================================================================

kernel/sched/cpufreq_schedutil.c +1 -0
@@ -61,6 +61,7 @@ struct sugov_cpu {
 	unsigned long util;
 	unsigned long max;
 	unsigned int flags;
+	bool overload;
 };
 
 static DEFINE_PER_CPU(struct sugov_cpu, sugov_cpu);

kernel/sched/cpufreq_schedutil.c static void sugov_update_single(struct u · line 212

Quoted 6 lines
kernel/sched/cpufreq_schedutil.c +1 -1
@@ -207,7 +208,7 @@ static void sugov_update_single(struct u
 	if (!sugov_should_update_freq(sg_policy, time))
 		return;
 
-	if (flags & SCHED_CPUFREQ_RT_DL) {
+	if ((flags & SCHED_CPUFREQ_RT_DL) || this_rq()->rd->overload) {
 		next_f = policy->cpuinfo.max_freq;

Isn't this going to max OPP every time we have more than 1 task in that CPU?

Quoted 8 lines
Quoted 1 line

In that case it will not fit the case: we have two 10% tasks on that CPU.

Good point.

Quoted 2 lines

Previous solution was better IMO, apart from using overloaded instead of overutilized (which is not yet there) :-/

OK, so the one below works too.

Better... just one minor comment.

kernel/sched/cpufreq_schedutil.c struct sugov_policy { · line 40

Quoted 13 lines
---
 kernel/sched/cpufreq_schedutil.c |   11 +++++++++++
 1 file changed, 11 insertions(+)

Index: linux-pm/kernel/sched/cpufreq_schedutil.c
===================================================================

kernel/sched/cpufreq_schedutil.c +1 -0
@@ -37,6 +37,7 @@ struct sugov_policy {
 	s64 freq_update_delay_ns;
 	unsigned int next_freq;
 	unsigned int cached_raw_freq;
+	bool overload;

Can we avoid using "overloaded" in favor of a more generic and schedutil specific name. Mainly because in the future we would probably like to switch from "overloaded" to "overutilized".

What about something like: "busy" ?

Quoted 22 lines
kernel/sched/cpufreq_schedutil.c +4 -0
 	/* The next fields are only needed if fast switch cannot be used. */
 	struct irq_work irq_work;
@@ -61,6 +62,7 @@ struct sugov_cpu {
 	unsigned long util;
 	unsigned long max;
 	unsigned int flags;
+	bool overload;
 };
 
 static DEFINE_PER_CPU(struct sugov_cpu, sugov_cpu);
@@ -93,6 +95,9 @@ static void sugov_update_commit(struct s
 {
 	struct cpufreq_policy *policy = sg_policy->policy;
 
+	if (sg_policy->overload && next_freq < sg_policy->next_freq)
+		next_freq = sg_policy->next_freq;
+
 	if (policy->fast_switch_enabled) {
 		if (sg_policy->next_freq == next_freq) {
 			trace_cpu_frequency(policy->cur, smp_processor_id());

kernel/sched/cpufreq_schedutil.c static void sugov_update_single(struct u · line 215

Quoted 4 lines
kernel/sched/cpufreq_schedutil.c +1 -0
@@ -207,6 +212,8 @@ static void sugov_update_single(struct u
 	if (!sugov_should_update_freq(sg_policy, time))
 		return;
 
+	sg_policy->overload = this_rq()->rd->overload;

And than we can move this bit into an inline function, something like e.g.:

   static inline bool sugov_this_cpu_is_busy()
   {
           return this_rq()->rd->overloaded
   }

Where in future we can easily switch from usage of "overloaded" to usage of "utilization".

Quoted 30 lines
kernel/sched/cpufreq_schedutil.c +5 -0
+
 	if (flags & SCHED_CPUFREQ_RT_DL) {
 		next_f = policy->cpuinfo.max_freq;
 	} else {
@@ -225,6 +232,8 @@ static unsigned int sugov_next_freq_shar
 	unsigned long util = 0, max = 1;
 	unsigned int j;
 
+	sg_policy->overload = false;
+
 	for_each_cpu(j, policy->cpus) {
 		struct sugov_cpu *j_sg_cpu = &per_cpu(sugov_cpu, j);
 		unsigned long j_util, j_max;
@@ -253,6 +262,7 @@ static unsigned int sugov_next_freq_shar
 		}
 
 		sugov_iowait_boost(j_sg_cpu, &util, &max);
+		sg_policy->overload = sg_policy->overload || sg_cpu->overload;
 	}
 
 	return get_next_freq(sg_policy, util, max);
@@ -273,6 +283,7 @@ static void sugov_update_shared(struct u
 	sg_cpu->util = util;
 	sg_cpu->max = max;
 	sg_cpu->flags = flags;
+	sg_cpu->overload = this_rq()->rd->overload;
 
 	sugov_set_iowait_boost(sg_cpu, time, flags);
 	sg_cpu->last_update = time;
Signature · 3 lines
#include <best/regards.h>

Patrick Bellasi
maintainer
#49 lore raw

Re: [RFC][PATCH v2 2/2] cpufreq: schedutil: Avoid decreasing frequency of busy CPUs

Seriously people, trim your replies.

Quoted 6 lines · Patrick Bellasi

And than we can move this bit into an inline function, something like e.g.:

   static inline bool sugov_this_cpu_is_busy()
   {
           return this_rq()->rd->overloaded
   }

No, that's just entirely and utterly wrong. It being in rd means its very much not about _this_ CPU in any way.

#50 lore raw

Re: [RFC][PATCH v2 2/2] cpufreq: schedutil: Avoid decreasing frequency of busy CPUs

Quoted 11 lines · Peter Zijlstra
Quoted 6 lines · Patrick Bellasi

And than we can move this bit into an inline function, something like e.g.:

   static inline bool sugov_this_cpu_is_busy()
   {
           return this_rq()->rd->overloaded
   }

No, that's just entirely and utterly wrong. It being in rd means its very much not about _this_ CPU in any way.

You right (of course), we cannot really use "this_" in the name of a function with such a code.

The suggestion here was at least to factor out whatever code we want to use to check if the current CPU has to be subject to a down-scaling constraint.

However, using rd->overload is not the best option, for the many reasons you explained in your previous comment. Thus, we should probably stay with the idle time tracking solution initially proposed by Rafael.

Sorry for the noise :-(

Signature · 3 lines
#include <best/regards.h>

Patrick Bellasi
maintainer
#51 lore raw

Re: [RFC][PATCH v2 2/2] cpufreq: schedutil: Avoid decreasing frequency of busy CPUs

In reply to Rafael J. Wysocki

kernel/sched/cpufreq_schedutil.c static void sugov_update_single(struct u · line 212

Quoted 2 lines · Rafael J. Wysocki
kernel/sched/cpufreq_schedutil.c +1 -0
+	if ((flags & SCHED_CPUFREQ_RT_DL) || this_rq()->rd->overload) {
 		next_f = policy->cpuinfo.max_freq;

So this I think is wrong; rd->overload is set if _any_ of the CPUs in the root domain is overloaded. And given the root domain is typically the _entire_ machine, this would have a tendency to run at max_freq far too often.

#52 lore raw

Re: [RFC][PATCH v2 2/2] cpufreq: schedutil: Avoid decreasing frequency of busy CPUs

In reply to Rafael J. Wysocki
Quoted 31 lines · Rafael J. Wysocki

From: Rafael J. Wysocki <[email protected]>

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.

To work around this issue use the observation that, from the schedutil governor's perspective, it does not make sense to decrease the frequency of a CPU that doesn't enter idle and avoid decreasing the frequency of busy CPUs.

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 it is about to set a new frequency for that CPU's policy. If the counter has not changed since the previous iteration, 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.

Quoted 6 lines

---

This is a slightly different approach (avoid decreasing frequency for busy CPUs instead of bumping if for them to the max upfront) and it works around the original problem too.

I like much better this version where we do not enforce max frequency as well as we removed the hardcoded time threshold. ;-)

Makes sense to me also to avoid down scaling until we don't hit an IDLE.

However, I also agree with Vincent's observation: this constraint should be there only for "overutilized" CPUs... but unfortunately, in mainline we are still missing that flag and thus we should probably use the "overloaded" one for the time being.

Quoted 2 lines

I tried to address a few Peter's comments here and the result doesn't seem to be too heavy-wieght.

Nice!

Quoted 108 lines

Thanks,
Rafael

---
 include/linux/tick.h             |    1 +
 kernel/sched/cpufreq_schedutil.c |   28 ++++++++++++++++++++++++----
 kernel/time/tick-sched.c         |   12 ++++++++++++
 3 files changed, 37 insertions(+), 4 deletions(-)

Index: linux-pm/kernel/sched/cpufreq_schedutil.c
===================================================================

kernel/sched/cpufreq_schedutil.c +24 -4
@@ -56,6 +56,9 @@ struct sugov_cpu {
 	unsigned long iowait_boost;
 	unsigned long iowait_boost_max;
 	u64 last_update;
+#ifdef CONFIG_NO_HZ_COMMON
+	unsigned long saved_idle_calls;
+#endif
 
 	/* The fields below are only needed when sharing a policy. */
 	unsigned long util;
@@ -88,11 +91,28 @@ static bool sugov_should_update_freq(str
 	return delta_ns >= sg_policy->freq_update_delay_ns;
 }
 
-static void sugov_update_commit(struct sugov_policy *sg_policy, u64 time,
-				unsigned int next_freq)
+#ifdef CONFIG_NO_HZ_COMMON
+static bool sugov_cpu_is_busy(struct sugov_cpu *sg_cpu)
+{
+	unsigned long idle_calls = tick_nohz_get_idle_calls();
+	bool ret = idle_calls == sg_cpu->saved_idle_calls;
+
+	sg_cpu->saved_idle_calls = idle_calls;
+	return ret;
+}
+#else
+static inline bool sugov_cpu_is_busy(struct sugov_cpu *sg_cpu) { return false; }
+#endif /* CONFIG_NO_HZ_COMMON */
+
+static void sugov_update_commit(struct sugov_cpu *sg_cpu,
+				struct sugov_policy *sg_policy,
+				u64 time, unsigned int next_freq)
 {
 	struct cpufreq_policy *policy = sg_policy->policy;
 
+	if (sugov_cpu_is_busy(sg_cpu) && next_freq < sg_policy->next_freq)
+		next_freq = sg_policy->next_freq;
+
 	if (policy->fast_switch_enabled) {
 		if (sg_policy->next_freq == next_freq) {
 			trace_cpu_frequency(policy->cur, smp_processor_id());
@@ -214,7 +234,7 @@ static void sugov_update_single(struct u
 		sugov_iowait_boost(sg_cpu, &util, &max);
 		next_f = get_next_freq(sg_policy, util, max);
 	}
-	sugov_update_commit(sg_policy, time, next_f);
+	sugov_update_commit(sg_cpu, sg_policy, time, next_f);
 }
 
 static unsigned int sugov_next_freq_shared(struct sugov_cpu *sg_cpu)
@@ -283,7 +303,7 @@ static void sugov_update_shared(struct u
 		else
 			next_f = sugov_next_freq_shared(sg_cpu);
 
-		sugov_update_commit(sg_policy, time, next_f);
+		sugov_update_commit(sg_cpu, sg_policy, time, next_f);
 	}
 
 	raw_spin_unlock(&sg_policy->update_lock);

Index: linux-pm/include/linux/tick.h
===================================================================

include/linux/tick.h +1 -0
@@ -117,6 +117,7 @@ extern void tick_nohz_idle_enter(void);
 extern void tick_nohz_idle_exit(void);
 extern void tick_nohz_irq_exit(void);
 extern ktime_t tick_nohz_get_sleep_length(void);
+extern unsigned long tick_nohz_get_idle_calls(void);
 extern u64 get_cpu_idle_time_us(int cpu, u64 *last_update_time);
 extern u64 get_cpu_iowait_time_us(int cpu, u64 *last_update_time);
 #else /* !CONFIG_NO_HZ_COMMON */

Index: linux-pm/kernel/time/tick-sched.c
===================================================================

kernel/time/tick-sched.c +12 -0
@@ -993,6 +993,18 @@ ktime_t tick_nohz_get_sleep_length(void)
 	return ts->sleep_length;
 }
 
+/**
+ * tick_nohz_get_idle_calls - return the current idle calls counter value
+ *
+ * Called from the schedutil frequency scaling governor in scheduler context.
+ */
+unsigned long tick_nohz_get_idle_calls(void)
+{
+	struct tick_sched *ts = this_cpu_ptr(&tick_cpu_sched);
+
+	return ts->idle_calls;
+}
+
 static void tick_nohz_account_idle_ticks(struct tick_sched *ts)
 {
 #ifndef CONFIG_VIRT_CPU_ACCOUNTING_NATIVE
Signature · 3 lines
#include <best/regards.h>

Patrick Bellasi
#53 lore raw

RFC PATCH v3 2/2 cpufreq: schedutil: Avoid reducing frequency of busy CPUs prematurely

In reply to Rafael J. Wysocki

From: Rafael J. Wysocki <[email protected]>

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.

---
 include/linux/tick.h             |    1 +
 kernel/sched/cpufreq_schedutil.c |   27 +++++++++++++++++++++++++++
 kernel/time/tick-sched.c         |   12 ++++++++++++
 3 files changed, 40 insertions(+)
kernel/sched/cpufreq_schedutil.c +27 -0
Index: linux-pm/kernel/sched/cpufreq_schedutil.c
===================================================================
@@ -61,6 +61,11 @@ struct sugov_cpu {
 	unsigned long util;
 	unsigned long max;
 	unsigned int flags;
+
+	/* The field below is for single-CPU policies only. */
+#ifdef CONFIG_NO_HZ_COMMON
+	unsigned long saved_idle_calls;
+#endif
 };
 
 static DEFINE_PER_CPU(struct sugov_cpu, sugov_cpu);
@@ -192,6 +197,19 @@ static void sugov_iowait_boost(struct su
 	sg_cpu->iowait_boost >>= 1;
 }
 
+#ifdef CONFIG_NO_HZ_COMMON
+static bool sugov_cpu_is_busy(struct sugov_cpu *sg_cpu)
+{
+	unsigned long idle_calls = tick_nohz_get_idle_calls();
+	bool ret = idle_calls == sg_cpu->saved_idle_calls;
+
+	sg_cpu->saved_idle_calls = idle_calls;
+	return ret;
+}
+#else
+static inline bool sugov_cpu_is_busy(struct sugov_cpu *sg_cpu) { return false; }
+#endif /* CONFIG_NO_HZ_COMMON */
+
 static void sugov_update_single(struct update_util_data *hook, u64 time,
 				unsigned int flags)
 {
@@ -200,6 +218,7 @@ static void sugov_update_single(struct u
 	struct cpufreq_policy *policy = sg_policy->policy;
 	unsigned long util, max;
 	unsigned int next_f;
+	bool busy;
 
 	sugov_set_iowait_boost(sg_cpu, time, flags);
 	sg_cpu->last_update = time;
@@ -207,12 +226,20 @@ static void sugov_update_single(struct u
 	if (!sugov_should_update_freq(sg_policy, time))
 		return;
 
+	busy = sugov_cpu_is_busy(sg_cpu);
+
 	if (flags & SCHED_CPUFREQ_RT_DL) {
 		next_f = policy->cpuinfo.max_freq;
 	} else {
 		sugov_get_util(&util, &max);
 		sugov_iowait_boost(sg_cpu, &util, &max);
 		next_f = get_next_freq(sg_policy, util, max);
+		/*
+		 * Do not reduce the frequency if the CPU has not been idle
+		 * recently, as the reduction is likely to be premature then.
+		 */
+		if (busy && next_f < sg_policy->next_freq)
+			next_f = sg_policy->next_freq;
 	}
 	sugov_update_commit(sg_policy, time, next_f);
 }
include/linux/tick.h +1 -0
Index: linux-pm/include/linux/tick.h
===================================================================
@@ -117,6 +117,7 @@ extern void tick_nohz_idle_enter(void);
 extern void tick_nohz_idle_exit(void);
 extern void tick_nohz_irq_exit(void);
 extern ktime_t tick_nohz_get_sleep_length(void);
+extern unsigned long tick_nohz_get_idle_calls(void);
 extern u64 get_cpu_idle_time_us(int cpu, u64 *last_update_time);
 extern u64 get_cpu_iowait_time_us(int cpu, u64 *last_update_time);
 #else /* !CONFIG_NO_HZ_COMMON */
kernel/time/tick-sched.c +12 -0
Index: linux-pm/kernel/time/tick-sched.c
===================================================================
@@ -993,6 +993,18 @@ ktime_t tick_nohz_get_sleep_length(void)
 	return ts->sleep_length;
 }
 
+/**
+ * tick_nohz_get_idle_calls - return the current idle calls counter value
+ *
+ * Called from the schedutil frequency scaling governor in scheduler context.
+ */
+unsigned long tick_nohz_get_idle_calls(void)
+{
+	struct tick_sched *ts = this_cpu_ptr(&tick_cpu_sched);
+
+	return ts->idle_calls;
+}
+
 static void tick_nohz_account_idle_ticks(struct tick_sched *ts)
 {
 #ifndef CONFIG_VIRT_CPU_ACCOUNTING_NATIVE
maintainer Acked-by on v3 2/2
#54 lore raw

Re: [RFC][PATCH v3 2/2] cpufreq: schedutil: Avoid reducing frequency of busy CPUs prematurely

Quoted 49 lines · Rafael J. Wysocki

From: Rafael J. Wysocki <[email protected]>

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.

Quoted 1 line

Right; this makes sense to me. Of course it would be good to have some more measurements on this, but in principle:

maintainer Acked-by on v3 2/2
#55 lore raw

Re: [RFC][PATCH v3 2/2] cpufreq: schedutil: Avoid reducing frequency of busy CPUs prematurely

In reply to Rafael J. Wysocki
Quoted 49 lines · Rafael J. Wysocki

From: Rafael J. Wysocki <[email protected]>

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.

Quoted 6 lines
---
 include/linux/tick.h             |    1 +
 kernel/sched/cpufreq_schedutil.c |   27 +++++++++++++++++++++++++++
 kernel/time/tick-sched.c         |   12 ++++++++++++
 3 files changed, 40 insertions(+)
Signature · 1 line
viresh
Reviewed-by on v3 2/2
#56 lore raw

Re: [RFC][PATCH v3 2/2] cpufreq: schedutil: Avoid reducing frequency of busy CPUs prematurely

In reply to Rafael J. Wysocki
Quoted 49 lines · Rafael J. Wysocki

From: Rafael J. Wysocki <[email protected]>

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.

Quoted 1 line

Makes sense,

Thanks,
Joel

#57 lore raw

Re: [RFC][PATCH v3 2/2] cpufreq: schedutil: Avoid reducing frequency of busy CPUs prematurely

In reply to Rafael J. Wysocki

Hi Rafael,

Quoted 1 line · Rafael J. Wysocki

From: Rafael J. Wysocki <[email protected]>

<snip>

Quoted 10 lines

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.

Quoted 5 lines

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

I have observed this issue even in the shared policy case (one clock domain for many CPUs). On migrate, the actual load update is split into two updates:

1. Add to removed_load on src_cpu (cpu_util(src_cpu) not updated yet)
2. Do wakeup on dst_cpu, add load to dst_cpu

Now if src_cpu manages to do a PELT update before 2. happens, ex: say a small periodic task woke up on src_cpu, it'll end up subtracting the removed_load from its utilization and issue a frequency update before 2. happens.

This causes a premature dip in frequency which doesn't get corrected until the next util update that fires after rate_limit_us. The dst_cpu freq. update from step 2. above gets rate limited in this scenario.

Quoted 44 lines

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.

---
 include/linux/tick.h             |    1 +
 kernel/sched/cpufreq_schedutil.c |   27 +++++++++++++++++++++++++++
 kernel/time/tick-sched.c         |   12 ++++++++++++
 3 files changed, 40 insertions(+)

Index: linux-pm/kernel/sched/cpufreq_schedutil.c
===================================================================

kernel/sched/cpufreq_schedutil.c +5 -0
@@ -61,6 +61,11 @@ struct sugov_cpu {
 	unsigned long util;
 	unsigned long max;
 	unsigned int flags;
+
+	/* The field below is for single-CPU policies only. */
+#ifdef CONFIG_NO_HZ_COMMON
+	unsigned long saved_idle_calls;
+#endif
 };
 
 static DEFINE_PER_CPU(struct sugov_cpu, sugov_cpu);
@@ -192,6 +197,19 @@ static void sugov_iowait_boost(struct su
 	sg_cpu->iowait_boost >>= 1;
 }

kernel/sched/cpufreq_schedutil.c static void sugov_iowait_boost(struct su · line 208

Quoted 10 lines
kernel/sched/cpufreq_schedutil.c +9 -0
 
+#ifdef CONFIG_NO_HZ_COMMON
+static bool sugov_cpu_is_busy(struct sugov_cpu *sg_cpu)
+{
+	unsigned long idle_calls = tick_nohz_get_idle_calls();
+	bool ret = idle_calls == sg_cpu->saved_idle_calls;
+
+	sg_cpu->saved_idle_calls = idle_calls;
+	return ret;
+}

Hm, sorry I am a bit confused perhaps you could help me understand the problem/solution better :)

Say we have the this simple case of only a single periodic task running on one CPU, wouldn't the PELT update on wakeup cause a frequency update which updates the sg_cpu->saved_idle_calls value here? That would then cause the frequency update on idle entry to always skip dropping frequency right?

If I am reading this correctly, the PELT update on the dequeue for the periodic task (in the scenario above) happens _before_ the idle_calls++ which is in tick_nohz_idle_enter.

Thanks!
-Sai

#58 lore raw

Re: [RFC][PATCH v3 2/2] cpufreq: schedutil: Avoid reducing frequency of busy CPUs prematurely

Quoted 4 lines · Sai Gurrappadi

Hm, sorry I am a bit confused perhaps you could help me understand the problem/solution better :)

Say we have the this simple case of only a single periodic task running on one CPU, wouldn't the PELT update on wakeup cause a frequency update which updates the sg_cpu->saved_idle_calls value here? That would then cause the frequency update on idle entry to always skip dropping frequency right?

Apologies, this example here is flawed because on task dequeue, its utilization isn't removed. There is no problem in this case...

-Sai

#59 lore raw

Re: [RFC][PATCH v3 2/2] cpufreq: schedutil: Avoid reducing frequency of busy CPUs prematurely

In reply to Sai Gurrappadi
Quoted 1 line · Sai Gurrappadi

Hi Rafael,

Hi,

Quoted 21 lines · Rafael J. Wysocki
Quoted 1 line

From: Rafael J. Wysocki <[email protected]>

<snip>

Quoted 10 lines

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.

Quoted 5 lines

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

Quoted 8 lines

I have observed this issue even in the shared policy case (one clock domain for many CPUs). On migrate, the actual load update is split into two updates:

1. Add to removed_load on src_cpu (cpu_util(src_cpu) not updated yet)
2. Do wakeup on dst_cpu, add load to dst_cpu

Now if src_cpu manages to do a PELT update before 2. happens, ex: say a small periodic task woke up on src_cpu, it'll end up subtracting the removed_load from its utilization and issue a frequency update before 2. happens.

This causes a premature dip in frequency which doesn't get corrected until the next util update that fires after rate_limit_us. The dst_cpu freq. update from step 2. above gets rate limited in this scenario.

Interesting, and this seems to be related to last_freq_update_time being per-policy (which it has to be, because frequency updates are per-policy too and that's what we need to rate-limit).

Does this happen often enough to be a real concern in practice on those configurations, though?

The other CPUs in the policy need to be either idle (so schedutil doesn't take them into account at all) or lightly utilized for that to happen, so that would affect workloads with one CPU hog type of task that is migrated from one CPU to another within a policy and that doesn't happen too often AFAICS.

Thanks,
Rafael

#60 lore raw

Re: [RFC][PATCH v3 2/2] cpufreq: schedutil: Avoid reducing frequency of busy CPUs prematurely

Quoted 31 lines · Rafael J. Wysocki
Quoted 1 line · Sai Gurrappadi

Hi Rafael,

Hi,

Quoted 26 lines · Rafael J. Wysocki
Quoted 1 line

From: Rafael J. Wysocki <[email protected]>

<snip>

Quoted 10 lines

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.

Quoted 5 lines

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

I have observed this issue even in the shared policy case (one clock domain for many CPUs). On migrate, the actual load update is split into two updates:

1. Add to removed_load on src_cpu (cpu_util(src_cpu) not updated yet)
2. Do wakeup on dst_cpu, add load to dst_cpu

Quoted 8 lines
Quoted 3 lines

Now if src_cpu manages to do a PELT update before 2. happens, ex: say a small periodic task woke up on src_cpu, it'll end up subtracting the removed_load from its utilization and issue a frequency update before 2. happens.

This causes a premature dip in frequency which doesn't get corrected until the next util update that fires after rate_limit_us. The dst_cpu freq. update from step 2. above gets rate limited in this scenario.

Interesting, and this seems to be related to last_freq_update_time being per-policy (which it has to be, because frequency updates are per-policy too and that's what we need to rate-limit).

Correct.

Quoted 8 lines

Does this happen often enough to be a real concern in practice on those configurations, though?

The other CPUs in the policy need to be either idle (so schedutil doesn't take them into account at all) or lightly utilized for that to happen, so that would affect workloads with one CPU hog type of task that is migrated from one CPU to another within a policy and that doesn't happen too often AFAICS.

So it is possible, even likely in some cases for a heavy CPU task to migrate on wakeup between the policy->cpus via select_idle_sibling() if the prev_cpu it was on was !idle on wakeup.

This style of heavy thread + lots of light work is a common pattern on Android (games, browsing, etc.) given how Android does its threading for ipc (Binder stuff) + its rendering/audio pipelines.

I unfortunately don't have any numbers atm though.

-Sai

#61 lore raw

Re: [RFC][PATCH v3 2/2] cpufreq: schedutil: Avoid reducing frequency of busy CPUs prematurely

In reply to Rafael J. Wysocki

Hi Rafael,

Quoted 17 lines · Rafael J. Wysocki

From: Rafael J. Wysocki <[email protected]>

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.

Quoted 9 lines

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.

Thinking out loud a bit, I wonder if what you really want to do is basically:

schedutil_cpu_util(cpu) = max(cpu_rq(cpu)->cfs.util_avg, total_cpu_util_avg);

Where total_cpu_util_avg tracks the average utilization of the CPU itself over time (% of time the CPU was busy) in the same PELT like manner. The difference here is that it doesn't change instantaneously as tasks migrate in/out but it decays/accumulates just like the per-entity util_avgs.

Over time, total_cpu_util_avg and cfs_rq(cpu)->util_avg will tend towards each other the lesser the amount of 'overlap' / overloading.

Yes, the above metric would 'overestimate' in case all tasks have migrated away and we are left with an idle CPU. A fix for that could be to just use the PELT value like so:

schedutil_cpu_util(cpu) = max(cpu_rq(cpu)->cfs.util_avg, idle_cpu(cpu) ? 0 : total_cpu_util_avg);

Note that the problem described here in the commit message doesn't need fully runnable threads, it just needs two threads to execute in parallel on the same CPU for a period of time. I don't think looking at just idle_calls necessarily covers all cases.

Thoughts?

Thanks,
-Sai

#62 lore raw

Re: [RFC][PATCH v3 2/2] cpufreq: schedutil: Avoid reducing frequency of busy CPUs prematurely

Quoted 38 lines · Sai Gurrappadi

Hi Rafael,

Quoted 17 lines · Rafael J. Wysocki

From: Rafael J. Wysocki <[email protected]>

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.

Quoted 9 lines

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.

Thinking out loud a bit, I wonder if what you really want to do is basically:

schedutil_cpu_util(cpu) = max(cpu_rq(cpu)->cfs.util_avg, total_cpu_util_avg);

Where total_cpu_util_avg tracks the average utilization of the CPU itself over time (% of time the CPU was busy) in the same PELT like manner. The difference here is that it doesn't change instantaneously as tasks migrate in/out but it decays/accumulates just like the per-entity util_avgs.

Over time, total_cpu_util_avg and cfs_rq(cpu)->util_avg will tend towards each other the lesser the amount of 'overlap' / overloading.

Quoted 7 lines

Yes, the above metric would 'overestimate' in case all tasks have migrated away and we are left with an idle CPU. A fix for that could be to just use the PELT value like so:

schedutil_cpu_util(cpu) = max(cpu_rq(cpu)->cfs.util_avg, idle_cpu(cpu) ? 0 : total_cpu_util_avg);

Note that the problem described here in the commit message doesn't need fully runnable threads, it just needs two threads to execute in parallel on the same CPU for a period of time. I don't think looking at just idle_calls necessarily covers all cases.

Thoughts?

Well, is the total_cpu_util_avg metric readily available?

Thanks,
Rafael

maintainer
#63 lore raw

Re: [RFC][PATCH v3 2/2] cpufreq: schedutil: Avoid reducing frequency of busy CPUs prematurely

In reply to Sai Gurrappadi
Quoted 30 lines · Sai Gurrappadi

Hi Rafael,

Quoted 17 lines · Rafael J. Wysocki

From: Rafael J. Wysocki <[email protected]>

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.

Quoted 9 lines

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.

Quoted 5 lines

Thinking out loud a bit, I wonder if what you really want to do is basically:

schedutil_cpu_util(cpu) = max(cpu_rq(cpu)->cfs.util_avg, total_cpu_util_avg);

Where total_cpu_util_avg tracks the average utilization of the CPU itself over time (% of time the CPU was busy) in the same PELT like manner. The difference here is that it doesn't change instantaneously as tasks migrate in/out but it decays/accumulates just like the per-entity util_avgs.

But we loose the interest of immediate decrease when tasks migrate. Instead of total_cpu_util_avg we should better track RT utilization in the same manner so with ongoing work for deadline we will have :
total_utilization = cfs.util_avg + rt's util_avg + deadline's util avg and we still take advantage of task migration effect

Quoted 13 lines

Over time, total_cpu_util_avg and cfs_rq(cpu)->util_avg will tend towards each other the lesser the amount of 'overlap' / overloading.

Yes, the above metric would 'overestimate' in case all tasks have migrated away and we are left with an idle CPU. A fix for that could be to just use the PELT value like so:

schedutil_cpu_util(cpu) = max(cpu_rq(cpu)->cfs.util_avg, idle_cpu(cpu) ? 0 : total_cpu_util_avg);

Note that the problem described here in the commit message doesn't need fully runnable threads, it just needs two threads to execute in parallel on the same CPU for a period of time. I don't think looking at just idle_calls necessarily covers all cases.

Thoughts?

Thanks,
-Sai

#64 lore raw

Re: [RFC][PATCH v3 2/2] cpufreq: schedutil: Avoid reducing frequency of busy CPUs prematurely

Hi Vincent,

Quoted 31 lines · Vincent Guittot
Quoted 30 lines · Sai Gurrappadi

Hi Rafael,

Quoted 26 lines · Rafael J. Wysocki

From: Rafael J. Wysocki <[email protected]>

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.

Quoted 7 lines
Quoted 5 lines

Thinking out loud a bit, I wonder if what you really want to do is basically:

schedutil_cpu_util(cpu) = max(cpu_rq(cpu)->cfs.util_avg, total_cpu_util_avg);

Where total_cpu_util_avg tracks the average utilization of the CPU itself over time (% of time the CPU was busy) in the same PELT like manner. The difference here is that it doesn't change instantaneously as tasks migrate in/out but it decays/accumulates just like the per-entity util_avgs.

But we loose the interest of immediate decrease when tasks migrate.

Indeed, this is not ideal.

Quoted 4 lines

Instead of total_cpu_util_avg we should better track RT utilization in the same manner so with ongoing work for deadline we will have :
total_utilization = cfs.util_avg + rt's util_avg + deadline's util avg and we still take advantage of task migration effect

I agree that we need better tracking for RT and DL tasks but that doesn't solve the overloaded case with more than one CFS thread sharing a CPU.

In the overloaded case, we care not just about the instant where the migrate happens but also subsequent windows where the PELT metric is slowly ramping up to reflect the real utilization of a task now that it has a CPU to itself.

Maybe there are better ways to solve that though :-)

Thanks,
-Sai

#65 lore raw

Re: [RFC][PATCH v3 2/2] cpufreq: schedutil: Avoid reducing frequency of busy CPUs prematurely

Quoted 49 lines · Sai Gurrappadi

Hi Vincent,

Quoted 31 lines · Vincent Guittot
Quoted 30 lines · Sai Gurrappadi

Hi Rafael,

Quoted 26 lines · Rafael J. Wysocki

From: Rafael J. Wysocki <[email protected]>

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.

Quoted 7 lines
Quoted 5 lines

Thinking out loud a bit, I wonder if what you really want to do is basically:

schedutil_cpu_util(cpu) = max(cpu_rq(cpu)->cfs.util_avg, total_cpu_util_avg);

Where total_cpu_util_avg tracks the average utilization of the CPU itself over time (% of time the CPU was busy) in the same PELT like manner. The difference here is that it doesn't change instantaneously as tasks migrate in/out but it decays/accumulates just like the per-entity util_avgs.

But we loose the interest of immediate decrease when tasks migrate.

Indeed, this is not ideal.

Quoted 4 lines

Instead of total_cpu_util_avg we should better track RT utilization in the same manner so with ongoing work for deadline we will have :
total_utilization = cfs.util_avg + rt's util_avg + deadline's util avg and we still take advantage of task migration effect

Quoted 5 lines

I agree that we need better tracking for RT and DL tasks but that doesn't solve the overloaded case with more than one CFS thread sharing a CPU.

In the overloaded case, we care not just about the instant where the migrate happens but also subsequent windows where the PELT metric is slowly ramping up to reflect the real utilization of a task now that it has a CPU to itself.

Maybe there are better ways to solve that though :-)

I wonder if it's viable to postpone the utilization update on both the source and target runqueues until the task has been fully migrated?

That would make the artificial utilization reductions go away.

Thanks,
Rafael

#66 lore raw

Re: [RFC][PATCH v3 2/2] cpufreq: schedutil: Avoid reducing frequency of busy CPUs prematurely

In reply to Rafael J. Wysocki

Hi Rafael,
2017-03-22 7:08 GMT+08:00 Rafael J. Wysocki <[email protected]>:

Quoted 26 lines

From: Rafael J. Wysocki <[email protected]>

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.

Quoted 3 lines

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

Sorry for one question maybe not associated with this patch. If the cpufreq policy is shared between multiple CPUs, the function intel_cpufreq_target() just updates IA32_PERF_CTL MSR of the cpu which is managing this policy, I wonder whether other cpus which are affected should also update their per-logical cpu's IA32_PERF_CTL MSR?

Regards,
Wanpeng Li

Quoted 128 lines

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.

---
 include/linux/tick.h             |    1 +
 kernel/sched/cpufreq_schedutil.c |   27 +++++++++++++++++++++++++++
 kernel/time/tick-sched.c         |   12 ++++++++++++
 3 files changed, 40 insertions(+)

Index: linux-pm/kernel/sched/cpufreq_schedutil.c
===================================================================

kernel/sched/cpufreq_schedutil.c +27 -0
@@ -61,6 +61,11 @@ struct sugov_cpu {
        unsigned long util;
        unsigned long max;
        unsigned int flags;
+
+       /* The field below is for single-CPU policies only. */
+#ifdef CONFIG_NO_HZ_COMMON
+       unsigned long saved_idle_calls;
+#endif
 };
 
 static DEFINE_PER_CPU(struct sugov_cpu, sugov_cpu);
@@ -192,6 +197,19 @@ static void sugov_iowait_boost(struct su
        sg_cpu->iowait_boost >>= 1;
 }
 
+#ifdef CONFIG_NO_HZ_COMMON
+static bool sugov_cpu_is_busy(struct sugov_cpu *sg_cpu)
+{
+       unsigned long idle_calls = tick_nohz_get_idle_calls();
+       bool ret = idle_calls == sg_cpu->saved_idle_calls;
+
+       sg_cpu->saved_idle_calls = idle_calls;
+       return ret;
+}
+#else
+static inline bool sugov_cpu_is_busy(struct sugov_cpu *sg_cpu) { return false; }
+#endif /* CONFIG_NO_HZ_COMMON */
+
 static void sugov_update_single(struct update_util_data *hook, u64 time,
                                unsigned int flags)
 {
@@ -200,6 +218,7 @@ static void sugov_update_single(struct u
        struct cpufreq_policy *policy = sg_policy->policy;
        unsigned long util, max;
        unsigned int next_f;
+       bool busy;
 
        sugov_set_iowait_boost(sg_cpu, time, flags);
        sg_cpu->last_update = time;
@@ -207,12 +226,20 @@ static void sugov_update_single(struct u
        if (!sugov_should_update_freq(sg_policy, time))
                return;
 
+       busy = sugov_cpu_is_busy(sg_cpu);
+
        if (flags & SCHED_CPUFREQ_RT_DL) {
                next_f = policy->cpuinfo.max_freq;
        } else {
                sugov_get_util(&util, &max);
                sugov_iowait_boost(sg_cpu, &util, &max);
                next_f = get_next_freq(sg_policy, util, max);
+               /*
+                * Do not reduce the frequency if the CPU has not been idle
+                * recently, as the reduction is likely to be premature then.
+                */
+               if (busy && next_f < sg_policy->next_freq)
+                       next_f = sg_policy->next_freq;
        }
        sugov_update_commit(sg_policy, time, next_f);
 }

Index: linux-pm/include/linux/tick.h
===================================================================

include/linux/tick.h +1 -0
@@ -117,6 +117,7 @@ extern void tick_nohz_idle_enter(void);
 extern void tick_nohz_idle_exit(void);
 extern void tick_nohz_irq_exit(void);
 extern ktime_t tick_nohz_get_sleep_length(void);
+extern unsigned long tick_nohz_get_idle_calls(void);
 extern u64 get_cpu_idle_time_us(int cpu, u64 *last_update_time);
 extern u64 get_cpu_iowait_time_us(int cpu, u64 *last_update_time);
 #else /* !CONFIG_NO_HZ_COMMON */

Index: linux-pm/kernel/time/tick-sched.c
===================================================================

kernel/time/tick-sched.c +12 -0
@@ -993,6 +993,18 @@ ktime_t tick_nohz_get_sleep_length(void)
        return ts->sleep_length;
 }
 
+/**
+ * tick_nohz_get_idle_calls - return the current idle calls counter value
+ *
+ * Called from the schedutil frequency scaling governor in scheduler context.
+ */
+unsigned long tick_nohz_get_idle_calls(void)
+{
+       struct tick_sched *ts = this_cpu_ptr(&tick_cpu_sched);
+
+       return ts->idle_calls;
+}
+
 static void tick_nohz_account_idle_ticks(struct tick_sched *ts)
 {
 #ifndef CONFIG_VIRT_CPU_ACCOUNTING_NATIVE
maintainer
#67 lore raw

Re: [RFC][PATCH v3 2/2] cpufreq: schedutil: Avoid reducing frequency of busy CPUs prematurely

Quoted 32 lines · Wanpeng Li

Hi Rafael,
2017-03-22 7:08 GMT+08:00 Rafael J. Wysocki <[email protected]>:

Quoted 26 lines

From: Rafael J. Wysocki <[email protected]>

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.

Quoted 3 lines

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

Quoted 5 lines

Sorry for one question maybe not associated with this patch. If the cpufreq policy is shared between multiple CPUs, the function intel_cpufreq_target() just updates IA32_PERF_CTL MSR of the cpu which is managing this policy, I wonder whether other cpus which are affected should also update their per-logical cpu's IA32_PERF_CTL MSR?

The CPUs share the policy when they share their freq/voltage rails and so changing perf state of one CPU should result in that changing for all the CPUs in that policy. Otherwise, they can't be considered to be part of the same policy.

That's why this code is changing it only for policy->cpu alone.

Signature · 1 line
viresh
#68 lore raw

Re: [RFC][PATCH v3 2/2] cpufreq: schedutil: Avoid reducing frequency of busy CPUs prematurely

2017-05-08 12:01 GMT+08:00 Viresh Kumar <[email protected]>:

Quoted 39 lines · Wanpeng Li
Quoted 32 lines

Hi Rafael,
2017-03-22 7:08 GMT+08:00 Rafael J. Wysocki <[email protected]>:

Quoted 26 lines

From: Rafael J. Wysocki <[email protected]>

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.

Quoted 3 lines

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

Quoted 5 lines

Sorry for one question maybe not associated with this patch. If the cpufreq policy is shared between multiple CPUs, the function intel_cpufreq_target() just updates IA32_PERF_CTL MSR of the cpu which is managing this policy, I wonder whether other cpus which are affected should also update their per-logical cpu's IA32_PERF_CTL MSR?

Quoted 6 lines

The CPUs share the policy when they share their freq/voltage rails and so changing perf state of one CPU should result in that changing for all the CPUs in that policy. Otherwise, they can't be considered to be part of the same policy.

That's why this code is changing it only for policy->cpu alone.

I see, thanks for the explanation.

Regards,
Wanpeng Li

#69 lore raw

Re: [RFC][PATCH v3 2/2] cpufreq: schedutil: Avoid reducing frequency of busy CPUs prematurely

In reply to Viresh Kumar
Quoted 39 lines · Viresh Kumar
Quoted 32 lines · Wanpeng Li

Hi Rafael,
2017-03-22 7:08 GMT+08:00 Rafael J. Wysocki <[email protected]>:

Quoted 26 lines

From: Rafael J. Wysocki <[email protected]>

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.

Quoted 3 lines

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

Quoted 5 lines

Sorry for one question maybe not associated with this patch. If the cpufreq policy is shared between multiple CPUs, the function intel_cpufreq_target() just updates IA32_PERF_CTL MSR of the cpu which is managing this policy, I wonder whether other cpus which are affected should also update their per-logical cpu's IA32_PERF_CTL MSR?

Quoted 4 lines

The CPUs share the policy when they share their freq/voltage rails and so changing perf state of one CPU should result in that changing for all the CPUs in that policy. Otherwise, they can't be considered to be part of the same policy.

To be entirely precise, this depends on the granularity of the HW interface.

If the interface is per-logical-CPU, we will use it this way for efficiency reasons and even if there is some coordination on the HW side, the information on how exactly it works usually is limited.

Thanks,
Rafael

#70 lore raw

Re: [RFC][PATCH v3 2/2] cpufreq: schedutil: Avoid reducing frequency of busy CPUs prematurely

2017-05-09 6:16 GMT+08:00 Rafael J. Wysocki <[email protected]>:

Quoted 45 lines · Viresh Kumar
Quoted 39 lines · Wanpeng Li
Quoted 32 lines

Hi Rafael,
2017-03-22 7:08 GMT+08:00 Rafael J. Wysocki <[email protected]>:

Quoted 26 lines

From: Rafael J. Wysocki <[email protected]>

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.

Quoted 3 lines

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

Quoted 5 lines

Sorry for one question maybe not associated with this patch. If the cpufreq policy is shared between multiple CPUs, the function intel_cpufreq_target() just updates IA32_PERF_CTL MSR of the cpu which is managing this policy, I wonder whether other cpus which are affected should also update their per-logical cpu's IA32_PERF_CTL MSR?

Quoted 4 lines

The CPUs share the policy when they share their freq/voltage rails and so changing perf state of one CPU should result in that changing for all the CPUs in that policy. Otherwise, they can't be considered to be part of the same policy.

Quoted 5 lines

To be entirely precise, this depends on the granularity of the HW interface.

If the interface is per-logical-CPU, we will use it this way for efficiency reasons and even if there is some coordination on the HW side, the information on how exactly it works usually is limited.

I check it on several Xeon servers on hand, however, I didn't find /sys/devices/system/cpu/cpufreq/policyx/affected_cpus can affect more than one logical cpu, so I guess most of Xeon servers are not support shared cpufreq policy, then which kind of boxes support that?

Regards,
Wanpeng Li

#71 lore raw

Re: [RFC][PATCH v3 2/2] cpufreq: schedutil: Avoid reducing frequency of busy CPUs prematurely

Quoted 48 lines · Wanpeng Li

2017-05-09 6:16 GMT+08:00 Rafael J. Wysocki <[email protected]>:

Quoted 47 lines · Viresh Kumar
Quoted 39 lines · Wanpeng Li
Quoted 32 lines

Hi Rafael,
2017-03-22 7:08 GMT+08:00 Rafael J. Wysocki <[email protected]>:

Quoted 26 lines

From: Rafael J. Wysocki <[email protected]>

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.

Quoted 3 lines

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

Quoted 5 lines

Sorry for one question maybe not associated with this patch. If the cpufreq policy is shared between multiple CPUs, the function intel_cpufreq_target() just updates IA32_PERF_CTL MSR of the cpu which is managing this policy, I wonder whether other cpus which are affected should also update their per-logical cpu's IA32_PERF_CTL MSR?

Quoted 4 lines

The CPUs share the policy when they share their freq/voltage rails and so changing perf state of one CPU should result in that changing for all the CPUs in that policy. Otherwise, they can't be considered to be part of the same policy.

To be entirely precise, this depends on the granularity of the HW interface.

Quoted 8 lines
Quoted 3 lines

If the interface is per-logical-CPU, we will use it this way for efficiency reasons and even if there is some coordination on the HW side, the information on how exactly it works usually is limited.

I check it on several Xeon servers on hand, however, I didn't find /sys/devices/system/cpu/cpufreq/policyx/affected_cpus can affect more than one logical cpu, so I guess most of Xeon servers are not support shared cpufreq policy, then which kind of boxes support that?

On Intel the interface for performance scaling is per-logical-CPU in general.

Thanks,
Rafael