Skip to content

Commit 3b45b4f

Browse files
Frederic WeisbeckerThomas Gleixner
authored andcommitted
sched/cputime: Provide get_cpu_[idle|iowait]_time_us() off-case
The last reason why get_cpu_idle/iowait_time_us() may return -1 now is if the config doesn't support nohz. The ad-hoc replacement solution by cpufreq is to compute jiffies minus the whole busy cputime. Although the intention should provide a coherent low resolution estimation of the idle and iowait time, the implementation is buggy because jiffies don't start at 0. Just provide instead a real get_cpu_[idle|iowait]_time_us() offcase. Signed-off-by: Frederic Weisbecker <frederic@kernel.org> Signed-off-by: Thomas Gleixner <tglx@kernel.org> Tested-by: Shrikanth Hegde <sshegde@linux.ibm.com> Link: https://patch.msgid.link/20260508131647.43868-14-frederic@kernel.org
1 parent 127b2eb commit 3b45b4f

4 files changed

Lines changed: 13 additions & 35 deletions

File tree

drivers/cpufreq/cpufreq.c

Lines changed: 1 addition & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -130,38 +130,11 @@ struct kobject *get_governor_parent_kobj(struct cpufreq_policy *policy)
130130
}
131131
EXPORT_SYMBOL_GPL(get_governor_parent_kobj);
132132

133-
static inline u64 get_cpu_idle_time_jiffy(unsigned int cpu, u64 *wall)
134-
{
135-
struct kernel_cpustat kcpustat;
136-
u64 cur_wall_time;
137-
u64 idle_time;
138-
u64 busy_time;
139-
140-
cur_wall_time = jiffies64_to_nsecs(get_jiffies_64());
141-
142-
kcpustat_cpu_fetch(&kcpustat, cpu);
143-
144-
busy_time = kcpustat.cpustat[CPUTIME_USER];
145-
busy_time += kcpustat.cpustat[CPUTIME_SYSTEM];
146-
busy_time += kcpustat.cpustat[CPUTIME_IRQ];
147-
busy_time += kcpustat.cpustat[CPUTIME_SOFTIRQ];
148-
busy_time += kcpustat.cpustat[CPUTIME_STEAL];
149-
busy_time += kcpustat.cpustat[CPUTIME_NICE];
150-
151-
idle_time = cur_wall_time - busy_time;
152-
if (wall)
153-
*wall = div_u64(cur_wall_time, NSEC_PER_USEC);
154-
155-
return div_u64(idle_time, NSEC_PER_USEC);
156-
}
157-
158133
u64 get_cpu_idle_time(unsigned int cpu, u64 *wall, int io_busy)
159134
{
160135
u64 idle_time = get_cpu_idle_time_us(cpu, io_busy ? wall : NULL);
161136

162-
if (idle_time == -1ULL)
163-
return get_cpu_idle_time_jiffy(cpu, wall);
164-
else if (!io_busy)
137+
if (!io_busy)
165138
idle_time += get_cpu_iowait_time_us(cpu, wall);
166139

167140
return idle_time;

include/linux/kernel_stat.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,9 @@ static inline bool kcpustat_idle_dyntick(void)
133133
}
134134
#endif /* CONFIG_NO_HZ_COMMON */
135135

136+
extern u64 get_cpu_idle_time_us(int cpu, u64 *last_update_time);
137+
extern u64 get_cpu_iowait_time_us(int cpu, u64 *last_update_time);
138+
136139
/* Fetch cputime values when vtime is disabled on a CPU */
137140
static inline u64 kcpustat_field_default(enum cpu_usage_stat usage, int cpu)
138141
{

include/linux/tick.h

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -139,8 +139,6 @@ extern bool tick_nohz_idle_got_tick(void);
139139
extern ktime_t tick_nohz_get_next_hrtimer(void);
140140
extern ktime_t tick_nohz_get_sleep_length(ktime_t *delta_next);
141141
extern unsigned long tick_nohz_get_idle_calls_cpu(int cpu);
142-
extern u64 get_cpu_idle_time_us(int cpu, u64 *last_update_time);
143-
extern u64 get_cpu_iowait_time_us(int cpu, u64 *last_update_time);
144142
#else /* !CONFIG_NO_HZ_COMMON */
145143
#define tick_nohz_enabled (0)
146144
static inline bool tick_nohz_is_active(void) { return false; }
@@ -162,8 +160,6 @@ static inline ktime_t tick_nohz_get_sleep_length(ktime_t *delta_next)
162160
*delta_next = TICK_NSEC;
163161
return *delta_next;
164162
}
165-
static inline u64 get_cpu_idle_time_us(int cpu, u64 *unused) { return -1; }
166-
static inline u64 get_cpu_iowait_time_us(int cpu, u64 *unused) { return -1; }
167163
#endif /* !CONFIG_NO_HZ_COMMON */
168164

169165
/*

kernel/sched/cputime.c

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -522,6 +522,13 @@ u64 kcpustat_field_iowait(int cpu)
522522
nr_iowait_cpu(cpu), ktime_get());
523523
}
524524
EXPORT_SYMBOL_GPL(kcpustat_field_iowait);
525+
#else
526+
static u64 kcpustat_field_dyntick(int cpu, enum cpu_usage_stat idx,
527+
bool compute_delta, ktime_t now)
528+
{
529+
return kcpustat_cpu(cpu).cpustat[idx];
530+
}
531+
#endif /* CONFIG_NO_HZ_COMMON */
525532

526533
static u64 get_cpu_sleep_time_us(int cpu, enum cpu_usage_stat idx,
527534
bool compute_delta, u64 *last_update_time)
@@ -557,7 +564,7 @@ static u64 get_cpu_sleep_time_us(int cpu, enum cpu_usage_stat idx,
557564
* This time is measured via accounting rather than sampling,
558565
* and is as accurate as ktime_get() is.
559566
*
560-
* Return: -1 if generic vtime is enabled, else total idle time of the @cpu
567+
* Return: total idle time of the @cpu
561568
*/
562569
u64 get_cpu_idle_time_us(int cpu, u64 *last_update_time)
563570
{
@@ -581,15 +588,14 @@ EXPORT_SYMBOL_GPL(get_cpu_idle_time_us);
581588
* This time is measured via accounting rather than sampling,
582589
* and is as accurate as ktime_get() is.
583590
*
584-
* Return: -1 if generic vtime is enabled, else total iowait time of @cpu
591+
* Return: total iowait time of @cpu
585592
*/
586593
u64 get_cpu_iowait_time_us(int cpu, u64 *last_update_time)
587594
{
588595
return get_cpu_sleep_time_us(cpu, CPUTIME_IOWAIT,
589596
nr_iowait_cpu(cpu), last_update_time);
590597
}
591598
EXPORT_SYMBOL_GPL(get_cpu_iowait_time_us);
592-
#endif /* CONFIG_NO_HZ_COMMON */
593599

594600
/*
595601
* Use precise platform statistics if available:

0 commit comments

Comments
 (0)