Skip to content

Commit f09e1a2

Browse files
joshdongregkh
authored andcommitted
fs/proc/uptime.c: Fix idle time reporting in /proc/uptime
[ Upstream commit a130e8f ] /proc/uptime reports idle time by reading the CPUTIME_IDLE field from the per-cpu kcpustats. However, on NO_HZ systems, idle time is not continually updated on idle cpus, leading this value to appear incorrectly small. /proc/stat performs an accounting update when reading idle time; we can use the same approach for uptime. With this patch, /proc/stat and /proc/uptime now agree on idle time. Additionally, the following shows idle time tick up consistently on an idle machine: (while true; do cat /proc/uptime; sleep 1; done) | awk '{print $2-prev; prev=$2}' Reported-by: Luigi Rizzo <lrizzo@google.com> Signed-off-by: Josh Don <joshdon@google.com> Signed-off-by: Peter Zijlstra (Intel) <peterz@infradead.org> Reviewed-by: Eric Dumazet <edumazet@google.com> Link: https://lkml.kernel.org/r/20210827165438.3280779-1-joshdon@google.com Signed-off-by: Sasha Levin <sashal@kernel.org>
1 parent 6e242c5 commit f09e1a2

3 files changed

Lines changed: 12 additions & 7 deletions

File tree

‎fs/proc/stat.c‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323

2424
#ifdef arch_idle_time
2525

26-
static u64 get_idle_time(struct kernel_cpustat *kcs, int cpu)
26+
u64 get_idle_time(struct kernel_cpustat *kcs, int cpu)
2727
{
2828
u64 idle;
2929

@@ -45,7 +45,7 @@ static u64 get_iowait_time(struct kernel_cpustat *kcs, int cpu)
4545

4646
#else
4747

48-
static u64 get_idle_time(struct kernel_cpustat *kcs, int cpu)
48+
u64 get_idle_time(struct kernel_cpustat *kcs, int cpu)
4949
{
5050
u64 idle, idle_usecs = -1ULL;
5151

‎fs/proc/uptime.c‎

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,18 +12,22 @@ static int uptime_proc_show(struct seq_file *m, void *v)
1212
{
1313
struct timespec64 uptime;
1414
struct timespec64 idle;
15-
u64 nsec;
15+
u64 idle_nsec;
1616
u32 rem;
1717
int i;
1818

19-
nsec = 0;
20-
for_each_possible_cpu(i)
21-
nsec += (__force u64) kcpustat_cpu(i).cpustat[CPUTIME_IDLE];
19+
idle_nsec = 0;
20+
for_each_possible_cpu(i) {
21+
struct kernel_cpustat kcs;
22+
23+
kcpustat_cpu_fetch(&kcs, i);
24+
idle_nsec += get_idle_time(&kcs, i);
25+
}
2226

2327
ktime_get_boottime_ts64(&uptime);
2428
timens_add_boottime(&uptime);
2529

26-
idle.tv_sec = div_u64_rem(nsec, NSEC_PER_SEC, &rem);
30+
idle.tv_sec = div_u64_rem(idle_nsec, NSEC_PER_SEC, &rem);
2731
idle.tv_nsec = rem;
2832
seq_printf(m, "%lu.%02lu %lu.%02lu\n",
2933
(unsigned long) uptime.tv_sec,

‎include/linux/kernel_stat.h‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,7 @@ extern void account_system_index_time(struct task_struct *, u64,
103103
enum cpu_usage_stat);
104104
extern void account_steal_time(u64);
105105
extern void account_idle_time(u64);
106+
extern u64 get_idle_time(struct kernel_cpustat *kcs, int cpu);
106107

107108
#ifdef CONFIG_VIRT_CPU_ACCOUNTING_NATIVE
108109
static inline void account_process_tick(struct task_struct *tsk, int user)

0 commit comments

Comments
 (0)