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

From: Tom Hromatka
Date: Tue Sep 15 2020 - 15:38:10 EST


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.

Signed-off-by: Tom Hromatka <tom.hromatka@xxxxxxxxxx>
---
fs/proc/stat.c | 26 ++------------------------
1 file changed, 2 insertions(+), 24 deletions(-)

diff --git a/fs/proc/stat.c b/fs/proc/stat.c
index 46b3293015fe..198f3c50cb91 100644
--- a/fs/proc/stat.c
+++ b/fs/proc/stat.c
@@ -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
--
2.25.4