Skip to contentSkip to conversation

iowait and idle fixes in /proc/stat

PATCH In review Tom Hromatka 2 participants 5 messages Sep 15, 2020 – Sep 24, 2020

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

v2 cover iowait and idle fixes in /proc/stat

A customer is using /proc/stat to track cpu usage in a VM and noted that the iowait and idle times behave strangely when a cpu goes offline and comes back online.

This patchset addresses two issues that can cause iowait and idle to fluctuate up and down. With these changes, cpu iowait and idle now only increase.

Changes from v1 to v2:
* Cleaned up commit messages
* Clarified code comments
* Further optimized the logic in fs/proc/stat.c

Tom Hromatka (2):
tick-sched: Do not clear the iowait and idle times /proc/stat: Simplify iowait and idle calculations when cpu is offline

 fs/proc/stat.c           | 26 ++------------------------
 kernel/time/tick-sched.c |  9 +++++++++
 2 files changed, 11 insertions(+), 24 deletions(-)
Signature · 1 line
2.25.4
#1 lore raw

PATCH v2 1/2 tick-sched: Do not clear the iowait and idle times

Prior to this commit, the cpu idle and iowait data in /proc/stat were cleared when a CPU goes down. When the CPU came back online, both idle and iowait times were restarted from 0.

This commit preserves the CPU's idle and iowait values when a CPU goes offline and comes back online.

---
 kernel/time/tick-sched.c | 9 +++++++++
 1 file changed, 9 insertions(+)
kernel/time/tick-sched.c +9 -0
index f0199a4ba1ad..4fbf67171dde 100644
@@ -1383,13 +1383,22 @@ void tick_setup_sched_timer(void)
 void tick_cancel_sched_timer(int cpu)
 {
 	struct tick_sched *ts = &per_cpu(tick_cpu_sched, cpu);
+	ktime_t idle_sleeptime, iowait_sleeptime;
 
 # ifdef CONFIG_HIGH_RES_TIMERS
 	if (ts->sched_timer.base)
 		hrtimer_cancel(&ts->sched_timer);
 # endif
 
+	/*
+	 * Preserve idle and iowait sleep times accross a CPU offline/online
+	 * sequence as they are accumulative.
+	 */
+	idle_sleeptime = ts->idle_sleeptime;
+	iowait_sleeptime = ts->iowait_sleeptime;
 	memset(ts, 0, sizeof(*ts));
+	ts->idle_sleeptime = idle_sleeptime;
+	ts->iowait_sleeptime = iowait_sleeptime;
 }
 #endif
 
Signature · 1 line
2.25.4
#2 lore raw

Re: [PATCH v2 1/2] tick-sched: Do not clear the iowait and idle times

Quoted 3 lines · Tom Hromatka

Prior to this commit, the cpu idle and iowait data in /proc/stat were cleared when a CPU goes down. When the CPU came back online, both idle and iowait times were restarted from 0.

Starting a commit message with 'Prior to this commit' is pointless. Describe the factual problem which made you come up with this change.

Quoted 3 lines

This commit preserves the CPU's idle and iowait values when a CPU goes offline and comes back online.

'This commit does' is just a variation of 'This patch does'.

git grep 'This patch' Documentation/process/

Thanks,

tglx

#3 lore raw

PATCH v2 2/2 /proc/stat: Simplify iowait and idle calculations when cpu is offline

In reply to Tom Hromatka

Prior to this commit, the cpu idle and iowait data in /proc/stat used different data sources based upon whether the CPU was online or not. This would cause spikes in the cpu idle and iowait data.

This patch uses the same data source, get_cpu_{idle,iowait}_time_us(), whether the CPU is online or not.

This patch and the preceding patch, "tick-sched: Do not clear the iowait and idle times", ensure that the cpu idle and iowait data are always increasing.

---
 fs/proc/stat.c | 26 ++------------------------
 1 file changed, 2 insertions(+), 24 deletions(-)
fs/proc/stat.c +2 -24
index 46b3293015fe..198f3c50cb91 100644
@@ -47,34 +47,12 @@ static u64 get_iowait_time(struct kernel_cpustat *kcs, int cpu)
 
 static u64 get_idle_time(struct kernel_cpustat *kcs, int cpu)
 {
-	u64 idle, idle_usecs = -1ULL;
-
-	if (cpu_online(cpu))
-		idle_usecs = get_cpu_idle_time_us(cpu, NULL);
-
-	if (idle_usecs == -1ULL)
-		/* !NO_HZ or cpu offline so we can rely on cpustat.idle */
-		idle = kcs->cpustat[CPUTIME_IDLE];
-	else
-		idle = idle_usecs * NSEC_PER_USEC;
-
-	return idle;
+	return get_cpu_idle_time_us(cpu, NULL) * NSEC_PER_USEC;
 }
 
 static u64 get_iowait_time(struct kernel_cpustat *kcs, int cpu)
 {
-	u64 iowait, iowait_usecs = -1ULL;
-
-	if (cpu_online(cpu))
-		iowait_usecs = get_cpu_iowait_time_us(cpu, NULL);
-
-	if (iowait_usecs == -1ULL)
-		/* !NO_HZ or cpu offline so we can rely on cpustat.iowait */
-		iowait = kcs->cpustat[CPUTIME_IOWAIT];
-	else
-		iowait = iowait_usecs * NSEC_PER_USEC;
-
-	return iowait;
+	return get_cpu_iowait_time_us(cpu, NULL) * NSEC_PER_USEC;
 }
 
 #endif
Signature · 1 line
2.25.4
#4 lore raw

Re: [PATCH v2 2/2] /proc/stat: Simplify iowait and idle calculations when cpu is offline

Quoted 3 lines · Tom Hromatka

Prior to this commit, the cpu idle and iowait data in /proc/stat used different data sources based upon whether the CPU was online or not. This would cause spikes in the cpu idle and iowait data.

This would not cause spikes. It _causes_ these times to go backwards and start over from 0. That's something completely different than a spike.

Please describe problems precisely.

Quoted 6 lines

This patch uses the same data source, get_cpu_{idle,iowait}_time_us(), whether the CPU is online or not.

This patch and the preceding patch, "tick-sched: Do not clear the iowait and idle times", ensure that the cpu idle and iowait data are always increasing.

So now you have a mixture of 'This commit and this patch'. Oh well.

Aside of that the ordering of your changelog is backwards. Something like this:

The CPU idle and iowait times in /proc/stats are inconsistent accross CPU hotplug.

The reason is that for NOHZ active systems the core accounting of CPU idle and iowait times used to be reset when a CPU was unplugged. The /proc/stat code tries to work around that by using the corresponding member of kernel_cpustat when the CPU is offline.

This works as long as the CPU stays offline, but when it is onlined again then the accounting is taken from the NOHZ core data again which started over from 0 causing both times to go backwards.

The HOHZ core has been fixed to preserve idle and iowait times accross CPU unplug, so the broken workaround is not longer required.

Hmm?

But...

Quoted 9 lines
fs/proc/stat.c +0 -3
@@ -47,34 +47,12 @@ static u64 get_iowait_time(struct kernel_cpustat *kcs, int cpu)
 
 static u64 get_idle_time(struct kernel_cpustat *kcs, int cpu)
 {
-	u64 idle, idle_usecs = -1ULL;
-
-	if (cpu_online(cpu))

fs/proc/stat.c static u64 get_iowait_time(struct kernel_cpustat *kcs, int cpu) · line 50

Quoted 10 lines
fs/proc/stat.c +1 -9
-		idle_usecs = get_cpu_idle_time_us(cpu, NULL);
-
-	if (idle_usecs == -1ULL)
-		/* !NO_HZ or cpu offline so we can rely on cpustat.idle */
-		idle = kcs->cpustat[CPUTIME_IDLE];
-	else
-		idle = idle_usecs * NSEC_PER_USEC;
-
-	return idle;
+	return get_cpu_idle_time_us(cpu, NULL) * NSEC_PER_USEC;

Q: How is this supposed to work on !NO_HZ systems or in case that NOHZ
has been disabled at boot time via command line option or lack of hardware?

A: Not at all.

Hint #1: You removed the following comment:

	/* !NO_HZ or cpu offline so we can rely on cpustat.idle */

Hint #2: There is more than one valid kernel configuration. '
Hint #3: Command line options and hardware features have side effects

Hint #4: git grep 'get_cpu_.*_time_us'

Thanks,

tglx