Skip to content

Commit eff6c8c

Browse files
ChiWu-ZeroPeter Zijlstra
authored andcommitted
sched/core: Reduce cost of sched_move_task when config autogroup
Some sched_move_task calls are useless because that task_struct->sched_task_group maybe not changed (equals task_group of cpu_cgroup) when system enable autogroup. So do some checks in sched_move_task. sched_move_task eg: task A belongs to cpu_cgroup0 and autogroup0, it will always belong to cpu_cgroup0 when do_exit. So there is no need to do {de|en}queue. The call graph is as follow. do_exit sched_autogroup_exit_task sched_move_task dequeue_task sched_change_group A.sched_task_group = sched_get_task_group (=cpu_cgroup0) enqueue_task Performance results: =========================== 1. env cpu: bogomips=4600.00 kernel: 6.3.0-rc3 cpu_cgroup: 6:cpu,cpuacct:/user.slice 2. cmds do_exit script: for i in {0..10000}; do sleep 0 & done wait Run the above script, then use the following bpftrace cmd to get the cost of sched_move_task: bpftrace -e 'k:sched_move_task { @ts[tid] = nsecs; } kr:sched_move_task /@ts[tid]/ { @ns += nsecs - @ts[tid]; delete(@ts[tid]); }' 3. cost time(ns): without patch: 43528033 with patch: 18541416 diff:-24986617 -57.4% As the result show, the patch will save 57.4% in the scenario. Signed-off-by: wuchi <wuchi.zero@gmail.com> Signed-off-by: Peter Zijlstra (Intel) <peterz@infradead.org> Link: https://lkml.kernel.org/r/20230321064459.39421-1-wuchi.zero@gmail.com
1 parent 530bfad commit eff6c8c

File tree

1 file changed

+19
-3
lines changed

1 file changed

+19
-3
lines changed

kernel/sched/core.c

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10351,7 +10351,7 @@ void sched_release_group(struct task_group *tg)
1035110351
spin_unlock_irqrestore(&task_group_lock, flags);
1035210352
}
1035310353

10354-
static void sched_change_group(struct task_struct *tsk)
10354+
static struct task_group *sched_get_task_group(struct task_struct *tsk)
1035510355
{
1035610356
struct task_group *tg;
1035710357

@@ -10363,7 +10363,13 @@ static void sched_change_group(struct task_struct *tsk)
1036310363
tg = container_of(task_css_check(tsk, cpu_cgrp_id, true),
1036410364
struct task_group, css);
1036510365
tg = autogroup_task_group(tsk, tg);
10366-
tsk->sched_task_group = tg;
10366+
10367+
return tg;
10368+
}
10369+
10370+
static void sched_change_group(struct task_struct *tsk, struct task_group *group)
10371+
{
10372+
tsk->sched_task_group = group;
1036710373

1036810374
#ifdef CONFIG_FAIR_GROUP_SCHED
1036910375
if (tsk->sched_class->task_change_group)
@@ -10384,10 +10390,19 @@ void sched_move_task(struct task_struct *tsk)
1038410390
{
1038510391
int queued, running, queue_flags =
1038610392
DEQUEUE_SAVE | DEQUEUE_MOVE | DEQUEUE_NOCLOCK;
10393+
struct task_group *group;
1038710394
struct rq_flags rf;
1038810395
struct rq *rq;
1038910396

1039010397
rq = task_rq_lock(tsk, &rf);
10398+
/*
10399+
* Esp. with SCHED_AUTOGROUP enabled it is possible to get superfluous
10400+
* group changes.
10401+
*/
10402+
group = sched_get_task_group(tsk);
10403+
if (group == tsk->sched_task_group)
10404+
goto unlock;
10405+
1039110406
update_rq_clock(rq);
1039210407

1039310408
running = task_current(rq, tsk);
@@ -10398,7 +10413,7 @@ void sched_move_task(struct task_struct *tsk)
1039810413
if (running)
1039910414
put_prev_task(rq, tsk);
1040010415

10401-
sched_change_group(tsk);
10416+
sched_change_group(tsk, group);
1040210417

1040310418
if (queued)
1040410419
enqueue_task(rq, tsk, queue_flags);
@@ -10412,6 +10427,7 @@ void sched_move_task(struct task_struct *tsk)
1041210427
resched_curr(rq);
1041310428
}
1041410429

10430+
unlock:
1041510431
task_rq_unlock(rq, tsk, &rf);
1041610432
}
1041710433

0 commit comments

Comments
 (0)