Skip to content

Commit 4b603f1

Browse files
Shubhang KaushikPeter Zijlstra
authored andcommitted
sched: Update rq->avg_idle when a task is moved to an idle CPU
Currently, rq->idle_stamp is only used to calculate avg_idle during wakeups. This means other paths that move a task to an idle CPU such as fork/clone, execve, or migrations, do not end the CPU's idle status in the scheduler's eyes, leading to an inaccurate avg_idle. This patch introduces update_rq_avg_idle() to provide a more accurate measurement of CPU idle duration. By invoking this helper in put_prev_task_idle(), we ensure avg_idle is updated whenever a CPU stops being idle, regardless of how the new task arrived. Testing on an 80-core Ampere Altra (ARMv8) with 6.19-rc5 baseline: - Hackbench : +7.2% performance gain at 16 threads. - Schbench: Reduced p99.9 tail latencies at high concurrency. Signed-off-by: Shubhang Kaushik <shubhang@os.amperecomputing.com> Signed-off-by: Peter Zijlstra (Intel) <peterz@infradead.org> Reviewed-by: Vincent Guittot <vincent.guittot@linaro.org> Tested-by: Shubhang Kaushik <shubhang@os.amperecomputing.com> Link: https://patch.msgid.link/20260121-v8-patch-series-v8-1-b7f1cbee5055@os.amperecomputing.com
1 parent bb332a9 commit 4b603f1

3 files changed

Lines changed: 14 additions & 12 deletions

File tree

kernel/sched/core.c

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -3613,6 +3613,18 @@ static inline void ttwu_do_wakeup(struct task_struct *p)
36133613
trace_sched_wakeup(p);
36143614
}
36153615

3616+
void update_rq_avg_idle(struct rq *rq)
3617+
{
3618+
u64 delta = rq_clock(rq) - rq->idle_stamp;
3619+
u64 max = 2*rq->max_idle_balance_cost;
3620+
3621+
update_avg(&rq->avg_idle, delta);
3622+
3623+
if (rq->avg_idle > max)
3624+
rq->avg_idle = max;
3625+
rq->idle_stamp = 0;
3626+
}
3627+
36163628
static void
36173629
ttwu_do_activate(struct rq *rq, struct task_struct *p, int wake_flags,
36183630
struct rq_flags *rf)
@@ -3648,18 +3660,6 @@ ttwu_do_activate(struct rq *rq, struct task_struct *p, int wake_flags,
36483660
p->sched_class->task_woken(rq, p);
36493661
rq_repin_lock(rq, rf);
36503662
}
3651-
3652-
if (rq->idle_stamp) {
3653-
u64 delta = rq_clock(rq) - rq->idle_stamp;
3654-
u64 max = 2*rq->max_idle_balance_cost;
3655-
3656-
update_avg(&rq->avg_idle, delta);
3657-
3658-
if (rq->avg_idle > max)
3659-
rq->avg_idle = max;
3660-
3661-
rq->idle_stamp = 0;
3662-
}
36633663
}
36643664

36653665
/*

kernel/sched/idle.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -460,6 +460,7 @@ static void put_prev_task_idle(struct rq *rq, struct task_struct *prev, struct t
460460
{
461461
update_curr_idle(rq);
462462
scx_update_idle(rq, false, true);
463+
update_rq_avg_idle(rq);
463464
}
464465

465466
static void set_next_task_idle(struct rq *rq, struct task_struct *next, bool first)

kernel/sched/sched.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1670,6 +1670,7 @@ static inline struct cfs_rq *group_cfs_rq(struct sched_entity *grp)
16701670

16711671
#endif /* !CONFIG_FAIR_GROUP_SCHED */
16721672

1673+
extern void update_rq_avg_idle(struct rq *rq);
16731674
extern void update_rq_clock(struct rq *rq);
16741675

16751676
/*

0 commit comments

Comments
 (0)