Skip to content

Commit df77430

Browse files
shakeelbPeter Zijlstra
authored andcommitted
psi: Reduce calls to sched_clock() in psi
We noticed that the cost of psi increases with the increase in the levels of the cgroups. Particularly the cost of cpu_clock() sticks out as the kernel calls it multiple times as it traverses up the cgroup tree. This patch reduces the calls to cpu_clock(). Performed perf bench on Intel Broadwell with 3 levels of cgroup. Before the patch: $ perf bench sched all # Running sched/messaging benchmark... # 20 sender and receiver processes per group # 10 groups == 400 processes run Total time: 0.747 [sec] # Running sched/pipe benchmark... # Executed 1000000 pipe operations between two processes Total time: 3.516 [sec] 3.516689 usecs/op 284358 ops/sec After the patch: $ perf bench sched all # Running sched/messaging benchmark... # 20 sender and receiver processes per group # 10 groups == 400 processes run Total time: 0.640 [sec] # Running sched/pipe benchmark... # Executed 1000000 pipe operations between two processes Total time: 3.329 [sec] 3.329820 usecs/op 300316 ops/sec Signed-off-by: Shakeel Butt <shakeelb@google.com> Signed-off-by: Peter Zijlstra (Intel) <peterz@infradead.org> Acked-by: Johannes Weiner <hannes@cmpxchg.org> Link: https://lkml.kernel.org/r/20210321205156.4186483-1-shakeelb@google.com
1 parent 2a2f80f commit df77430

File tree

1 file changed

+10
-9
lines changed

1 file changed

+10
-9
lines changed

kernel/sched/psi.c

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -644,12 +644,10 @@ static void poll_timer_fn(struct timer_list *t)
644644
wake_up_interruptible(&group->poll_wait);
645645
}
646646

647-
static void record_times(struct psi_group_cpu *groupc, int cpu)
647+
static void record_times(struct psi_group_cpu *groupc, u64 now)
648648
{
649649
u32 delta;
650-
u64 now;
651650

652-
now = cpu_clock(cpu);
653651
delta = now - groupc->state_start;
654652
groupc->state_start = now;
655653

@@ -676,7 +674,7 @@ static void record_times(struct psi_group_cpu *groupc, int cpu)
676674
}
677675

678676
static void psi_group_change(struct psi_group *group, int cpu,
679-
unsigned int clear, unsigned int set,
677+
unsigned int clear, unsigned int set, u64 now,
680678
bool wake_clock)
681679
{
682680
struct psi_group_cpu *groupc;
@@ -696,7 +694,7 @@ static void psi_group_change(struct psi_group *group, int cpu,
696694
*/
697695
write_seqcount_begin(&groupc->seq);
698696

699-
record_times(groupc, cpu);
697+
record_times(groupc, now);
700698

701699
for (t = 0, m = clear; m; m &= ~(1 << t), t++) {
702700
if (!(m & (1 << t)))
@@ -788,12 +786,14 @@ void psi_task_change(struct task_struct *task, int clear, int set)
788786
struct psi_group *group;
789787
bool wake_clock = true;
790788
void *iter = NULL;
789+
u64 now;
791790

792791
if (!task->pid)
793792
return;
794793

795794
psi_flags_change(task, clear, set);
796795

796+
now = cpu_clock(cpu);
797797
/*
798798
* Periodic aggregation shuts off if there is a period of no
799799
* task changes, so we wake it back up if necessary. However,
@@ -806,7 +806,7 @@ void psi_task_change(struct task_struct *task, int clear, int set)
806806
wake_clock = false;
807807

808808
while ((group = iterate_groups(task, &iter)))
809-
psi_group_change(group, cpu, clear, set, wake_clock);
809+
psi_group_change(group, cpu, clear, set, now, wake_clock);
810810
}
811811

812812
void psi_task_switch(struct task_struct *prev, struct task_struct *next,
@@ -815,6 +815,7 @@ void psi_task_switch(struct task_struct *prev, struct task_struct *next,
815815
struct psi_group *group, *common = NULL;
816816
int cpu = task_cpu(prev);
817817
void *iter;
818+
u64 now = cpu_clock(cpu);
818819

819820
if (next->pid) {
820821
bool identical_state;
@@ -836,7 +837,7 @@ void psi_task_switch(struct task_struct *prev, struct task_struct *next,
836837
break;
837838
}
838839

839-
psi_group_change(group, cpu, 0, TSK_ONCPU, true);
840+
psi_group_change(group, cpu, 0, TSK_ONCPU, now, true);
840841
}
841842
}
842843

@@ -858,7 +859,7 @@ void psi_task_switch(struct task_struct *prev, struct task_struct *next,
858859

859860
iter = NULL;
860861
while ((group = iterate_groups(prev, &iter)) && group != common)
861-
psi_group_change(group, cpu, clear, set, true);
862+
psi_group_change(group, cpu, clear, set, now, true);
862863

863864
/*
864865
* TSK_ONCPU is handled up to the common ancestor. If we're tasked
@@ -867,7 +868,7 @@ void psi_task_switch(struct task_struct *prev, struct task_struct *next,
867868
if (sleep) {
868869
clear &= ~TSK_ONCPU;
869870
for (; group; group = iterate_groups(prev, &iter))
870-
psi_group_change(group, cpu, clear, set, true);
871+
psi_group_change(group, cpu, clear, set, now, true);
871872
}
872873
}
873874
}

0 commit comments

Comments
 (0)