sched,fair: flatten CPU controller runqueues
v5 cover sched,fair: flatten CPU controller runqueues
The current implementation of the CPU controller uses hierarchical runqueues, where on wakeup a task is enqueued on its group's runqueue, the group is enqueued on the runqueue of the group above it, etc.
This increases a fairly large amount of overhead for workloads that do a lot of wakeups a second, especially given that the default systemd hierarchy is 2 or 3 levels deep.
This patch series is an attempt at reducing that overhead, by placing all the tasks on the same runqueue, and scaling the task priority by the priority of the group, which is calculated periodically.
My main TODO items for the next period of time are likely going to be testing, testing, and testing. I hope to find and flush out any corner case I can find, and make sure performance does not regress with any workloads, and hopefully improves some.
Other TODO items:
- More code cleanups.
- Remove some more now unused code.
- Reimplement CONFIG_CFS_BANDWIDTH.
Plan for the CONFIG_CFS_BANDWIDTH reimplementation:
- When a cgroup gets throttled, mark the cgroup and its children as throttled.
- When pick_next_entity finds a task that is on a throttled cgroup, stash it on the cgroup runqueue (which is not used for runnable tasks any more). Leave the vruntime unchanged, and adjust that runqueue's vruntime to be that of the left-most task.
- When a cgroup gets unthrottled, and has tasks on it, place it on a vruntime ordered heap separate from the main runqueue.
- Have pick_next_task_fair grab one task off that heap every time it is called, and the min vruntime of that heap is lower than the vruntime of the CPU's cfs_rq (or the CPU has no other runnable tasks).
- Place that selected task on the CPU's cfs_rq, renormalizing its vruntime with the GENTLE_FAIR_SLEEPERS logic. That should help interleave the already runnable tasks with the recently unthrottled group, and prevent thundering herd issues.
- If the group gets throttled again before all of its task had a chance to run, vruntime sorting ensures all the tasks in the throttled cgroup get a chance to run over time.
Changes from v4:
- drop patch that removes __sched_slice (Vincent Guittot)
- remove se->depth (Dietmar Eggemann)
- various other small cleanups and improvements suggested by people
Changes from v3:
- replace max_h_load with another hacky idea to ramp up the task_se_h_weight; I believe this new idea is wrong as well, but it will hopefully inspire a better solution (thanks to Peter Zijlstra)
- fix the ordering inside enqueue_task_fair to get task weights set up right (thanks to Peter Zijlstra)
- change wakeup_preempt_entity to reduce the number of task preemptions, hopefully resulting in behavior closer to what people configure in sysctl
- various other small cleanups and fixes
Changes from v2:
- fixed the web server performance regression, in a way vaguely similar to what Josef Bacik suggested (blame me for the implementation)
- removed some code duplication so the diffstat is redder than before
- propagate sum_exec_runtime up the tree, in preparation for CFS_BANDWIDTH
- small cleanups left and right
Changes from v1:
- use task_se_h_weight instead of task_se_h_load in calc_delta_fair and sched_slice, this seems to improve performance a little, but I still have some remaining regression to chase with our web server workload
- implement a number of the changes suggested by Dietmar Eggemann (still holding out for a better name for group_cfs_rq_of_parent)
This series applies on top of 5.2
include/linux/sched.h | 8 kernel/sched/core.c | 3 kernel/sched/debug.c | 15 kernel/sched/fair.c | 821 ++++++++++++++++++++------------------------------ kernel/sched/pelt.c | 68 +--- kernel/sched/pelt.h | 2 kernel/sched/sched.h | 9 7 files changed, 367 insertions(+), 559 deletions(-)
PATCH v5 2/15 sched: change /proc/sched_debug fields
Remove some fields from /proc/sched_debug that are removed from sched_entity in a subsequent patch, and add h_load, which comes in very handy to debug CPU controller weight distribution.
--- kernel/sched/debug.c | 11 ++--------- 1 file changed, 2 insertions(+), 9 deletions(-)
kernel/sched/debug.c +2 -9
@@ -416,11 +416,9 @@ static void print_cfs_group_stats(struct seq_file *m, int cpu, struct task_group }P (se->load.weight); -P (se->runnable_weight);#ifdef CONFIG_SMP P (se->avg.load_avg);P (se->avg.util_avg); -P (se->avg.runnable_load_avg);#endif #undef PN_SCHEDSTAT
@@ -538,7 +536,6 @@ void print_cfs_rq(struct seq_file *m, int cpu, struct cfs_rq *cfs_rq)SEQ_printf (m," .%-30s: %d\n" ,"nr_running" , cfs_rq->nr_running);SEQ_printf (m," .%-30s: %ld\n" ,"load" , cfs_rq->load.weight);#ifdef CONFIG_SMP -SEQ_printf (m," .%-30s: %ld\n" ,"runnable_weight" , cfs_rq->runnable_weight);SEQ_printf (m," .%-30s: %lu\n" ,"load_avg" , cfs_rq->avg.load_avg); SEQ_printf(m, " .%-30s: %lu\n", "runnable_load_avg",
@@ -547,17 +544,15 @@ void print_cfs_rq(struct seq_file *m, int cpu, struct cfs_rq *cfs_rq) cfs_rq->avg.util_avg);SEQ_printf (m," .%-30s: %u\n" ,"util_est_enqueued" , cfs_rq->avg.util_est.enqueued); -SEQ_printf (m," .%-30s: %ld\n" ,"removed.load_avg" , - cfs_rq->removed.load_avg);SEQ_printf (m," .%-30s: %ld\n" ,"removed.util_avg" , cfs_rq->removed.util_avg); -SEQ_printf (m," .%-30s: %ld\n" ,"removed.runnable_sum" , - cfs_rq->removed.runnable_sum);#ifdef CONFIG_FAIR_GROUP_SCHED SEQ_printf (m," .%-30s: %lu\n" ,"tg_load_avg_contrib" , cfs_rq->tg_load_avg_contrib);SEQ_printf (m," .%-30s: %ld\n" ,"tg_load_avg" ,atomic_long_read (&cfs_rq->tg->load_avg)); +SEQ_printf (m," .%-30s: %lu\n" ,"h_load" , + cfs_rq->h_load);#endif #endif #ifdef CONFIG_CFS_BANDWIDTH
@@ -961,10 +956,8 @@ void proc_sched_show_task(struct task_struct *p, struct pid_namespace *ns, "nr_involuntary_switches", (long long)p->nivcsw);P (se.load.weight); -P (se.runnable_weight);#ifdef CONFIG_SMP P (se.avg.load_sum); -P (se.avg.runnable_load_sum);P (se.avg.util_sum);P (se.avg.load_avg);P (se.avg.runnable_load_avg);
Signature · 1 line
2.20.1
PATCH v5 3/15 sched,fair: redefine runnable_load_avg as the sum of task_h_load
In reply to Rik van RielThe runnable_load magic is used to quickly propagate information about runnable tasks up the hierarchy of runqueues. The runnable_load_avg is mostly used for the load balancing code, which only examines the value at the root cfs_rq.
Redefine the root cfs_rq runnable_load_avg to be the sum of task_h_loads of the runnable tasks. This works because the hierarchical runnable_load of a task is already equal to the task_se_h_load today. This provides enough information to the load balancer.
The runnable_load_avg of the cgroup cfs_rqs does not appear to be used for anything, so don't bother calculating those.
This removes one of the things that the code currently traverses the cgroup hierarchy for, and getting rid of it brings us one step closer to a flat runqueue for the CPU controller.
--- include/linux/sched.h | 3 +- kernel/sched/core.c | 2 - kernel/sched/debug.c | 1 + kernel/sched/fair.c | 125 +++++++++++++----------------------------- kernel/sched/pelt.c | 64 ++++++--------------- kernel/sched/sched.h | 6 -- 6 files changed, 56 insertions(+), 145 deletions(-)
include/linux/sched.h +1 -2
@@ -391,7 +391,6 @@ struct util_est {struct sched_avg {u64 last_update_time;u64 load_sum; -u64 runnable_load_sum;u32 util_sum;u32 period_contrib;unsigned long load_avg;
@@ -439,7 +438,6 @@ struct sched_statistics {struct sched_entity {/* For load-balancing: */ struct load_weight load; -unsigned long runnable_weight;struct rb_node run_node;struct list_head group_node;unsigned int on_rq;
@@ -455,6 +453,7 @@ struct sched_entity {#ifdef CONFIG_FAIR_GROUP_SCHED int depth; +unsigned long enqueued_h_load;struct sched_entity *parent;/* rq on which this entity is (to be) queued: */ struct cfs_rq *cfs_rq;
kernel/sched/core.c +0 -2
@@ -744,7 +744,6 @@ static void set_load_weight(struct task_struct *p, bool update_load)if (task_has_idle_policy (p)) { load->weight =scale_load (WEIGHT_IDLEPRIO ); load->inv_weight =WMULT_IDLEPRIO ; - p->se.runnable_weight = load->weight;return ; }
@@ -757,7 +756,6 @@ static void set_load_weight(struct task_struct *p, bool update_load) }else { load->weight =scale_load (sched_prio_to_weight[prio]); load->inv_weight = sched_prio_to_wmult[prio]; - p->se.runnable_weight = load->weight; } }
kernel/sched/debug.c +1 -0
@@ -962,6 +962,7 @@ void proc_sched_show_task(struct task_struct *p, struct pid_namespace *ns,P (se.avg.load_avg);P (se.avg.runnable_load_avg);P (se.avg.util_avg); +P (se.enqueued_h_load);P (se.avg.last_update_time);P (se.avg.util_est.ewma);P (se.avg.util_est.enqueued);
kernel/sched/fair.c +38 -87
236 lines not on this page · raw message
kernel/sched/pelt.c +16 -48
190 lines not on this page · raw message
kernel/sched/sched.h +0 -6
@@ -487,7 +487,6 @@ struct cfs_bandwidth { };/* CFS-related fields in a runqueue */ struct cfs_rq {struct load_weight load; -unsigned long runnable_weight;unsigned int nr_running;unsigned int h_nr_running;
@@ -700,11 +699,6 @@ static inline long se_weight(struct sched_entity *se)return scale_load_down (se->load.weight); } -static inline long se_runnable (struct sched_entity *se) -{ -return scale_load_down (se->runnable_weight); -} -static inline bool sched_asym_prefer (int a,int b) {return arch_asym_cpu_priority (a) >arch_asym_cpu_priority (b);
Signature · 1 line
2.20.1
PATCH v5 11/15 sched,fair: flatten update_curr functionality
In reply to Rik van RielMake it clear that update_curr only works on tasks any more.
There is no need for task_tick_fair to call it on every sched entity up the hierarchy, so move the call out of entity_tick.
--- kernel/sched/fair.c | 24 ++++++++++++------------ 1 file changed, 12 insertions(+), 12 deletions(-)
kernel/sched/fair.c +12 -12
@@ -888,12 +888,15 @@ static void update_tg_load_avg(struct cfs_rq *cfs_rq, int force)static void update_curr (struct cfs_rq *cfs_rq) {struct sched_entity *curr = cfs_rq->curr; +struct task_struct *curtask;u64 now =rq_clock_task (rq_of (cfs_rq));u64 delta_exec;if (unlikely (!curr))return ; +SCHED_WARN_ON (!entity_is_task (curr)); + delta_exec = now - curr->exec_start;if (unlikely ((s64 )delta_exec <=0 ))return ;
@@ -909,13 +912,10 @@ static void update_curr(struct cfs_rq *cfs_rq) curr->vruntime +=calc_delta_fair (delta_exec, curr);update_min_vruntime (cfs_rq); -if (entity_is_task (curr)) { -struct task_struct *curtask =task_of (curr); - -trace_sched_stat_runtime (curtask, delta_exec, curr->vruntime); -cgroup_account_cputime (curtask, delta_exec); -account_group_exec_runtime (curtask, delta_exec); - } + curtask =task_of (curr); +trace_sched_stat_runtime (curtask, delta_exec, curr->vruntime); +cgroup_account_cputime (curtask, delta_exec); +account_group_exec_runtime (curtask, delta_exec);account_cfs_rq_runtime (cfs_rq, delta_exec); }
@@ -4212,11 +4212,6 @@ static void put_prev_entity(struct cfs_rq *cfs_rq, struct sched_entity *prev)static void entity_tick (struct cfs_rq *cfs_rq,struct sched_entity *curr,int queued) { -/* -* Update run-time statistics of the 'current'. -*/ -update_curr (cfs_rq); -/* * Ensure that runnable average is periodically updated. */
@@ -10048,6 +10043,11 @@ static void task_tick_fair(struct rq *rq, struct task_struct *curr, int queued)struct cfs_rq *cfs_rq;struct sched_entity *se = &curr->se; +/* +* Update run-time statistics of the 'current'. +*/ +update_curr (&rq->cfs); +for_each_sched_entity (se) { cfs_rq =group_cfs_rq_of_parent (se);entity_tick (cfs_rq, se, queued);
Signature · 1 line
2.20.1
PATCH v5 10/15 sched,fair: flatten hierarchical runqueues
In reply to Rik van RielFlatten the hierarchical runqueues into just the per CPU rq.cfs runqueue.
Iteration of the sched_entity hierarchy is rate limited to once per jiffy per sched_entity, which is a smaller change than it seems, because load average adjustments were already rate limited to once per jiffy before this patch series.
This patch breaks CONFIG_CFS_BANDWIDTH. The plan for that is to park tasks from throttled cgroups onto their cgroup runqueues, and slowly (using the GENTLE_FAIR_SLEEPERS) wake them back up, in vruntime order, once the cgroup gets unthrottled, to prevent thundering herd issues.
--- include/linux/sched.h | 2 + kernel/sched/fair.c | 502 ++++++++++++++---------------------------- kernel/sched/pelt.c | 6 +- kernel/sched/pelt.h | 2 +- kernel/sched/sched.h | 2 +- 5 files changed, 170 insertions(+), 344 deletions(-)
include/linux/sched.h +2 -0
@@ -453,6 +453,8 @@ struct sched_entity {#ifdef CONFIG_FAIR_GROUP_SCHED int depth;unsigned long enqueued_h_load; +unsigned long enqueued_h_weight; +struct load_weight h_load;struct sched_entity *parent;/* rq on which this entity is (to be) queued: */ struct cfs_rq *cfs_rq;
kernel/sched/fair.c +163 -339
844 lines not on this page · raw message
kernel/sched/pelt.c +3 -3
@@ -251,10 +251,10 @@ int __update_load_avg_blocked_se(u64 now, struct sched_entity *se)return 0 ; } -int __update_load_avg_se (u64 now,struct cfs_rq *cfs_rq,struct sched_entity *se) +int __update_load_avg_se (u64 now,struct cfs_rq *cfs_rq,struct sched_entity *se,bool load,bool running) { -if (___update_load_sum (now, &se->avg, !!se->on_rq, - cfs_rq->curr == se)) { +if (___update_load_sum (now, &se->avg, (!!se->on_rq || load), + (cfs_rq->curr == se) || running)) {___update_load_avg (&se->avg,se_weight (se));cfs_se_util_change (&se->avg);
kernel/sched/pelt.h +1 -1
@@ -2,7 +2,7 @@#include "sched-pelt.h" int __update_load_avg_blocked_se (u64 now,struct sched_entity *se); -int __update_load_avg_se (u64 now,struct cfs_rq *cfs_rq,struct sched_entity *se); +int __update_load_avg_se (u64 now,struct cfs_rq *cfs_rq,struct sched_entity *se,bool load,bool running);int __update_load_avg_cfs_rq (u64 now,struct cfs_rq *cfs_rq);int update_rt_rq_load_avg (u64 now,struct rq *rq,int running);int update_dl_rq_load_avg (u64 now,struct rq *rq,int running);
kernel/sched/sched.h +1 -1
@@ -1443,7 +1443,7 @@ static inline void set_task_rq(struct task_struct *p, unsigned int cpu)#ifdef CONFIG_FAIR_GROUP_SCHED set_task_rq_fair (&p->se, p->se.cfs_rq, tg->cfs_rq[cpu]); - p->se.cfs_rq = tg->cfs_rq[cpu]; + p->se.cfs_rq = &cpu_rq (cpu)->cfs; p->se.parent = tg->se[cpu];#endif
Signature · 1 line
2.20.1
PATCH v5 1/15 sched: introduce task_se_h_load helper
In reply to Rik van RielSometimes the hierarchical load of a sched_entity needs to be calculated. Rename task_h_load to task_se_h_load, and directly pass a sched_entity to that function.
Move the function declaration up above where it will be used later.
No functional changes.
--- kernel/sched/fair.c | 28 ++++++++++++++-------------- 1 file changed, 14 insertions(+), 14 deletions(-)
kernel/sched/fair.c +14 -14
@@ -242,6 +242,7 @@ static u64 __calc_delta(u64 delta_exec, unsigned long weight, struct load_weightconst struct sched_class fair_sched_class; +static unsigned long task_se_h_load (struct sched_entity *se);/************************************************************** * CFS operations on generic schedulable entities:
@@ -706,7 +707,6 @@ static u64 sched_vslice(struct cfs_rq *cfs_rq, struct sched_entity *se)#ifdef CONFIG_SMP static int select_idle_sibling (struct task_struct *p,int prev_cpu,int cpu); -static unsigned long task_h_load (struct task_struct *p);static unsigned long capacity_of (int cpu);/* Give new sched_entity start runnable values to heavy its load in infant time */
@@ -1668,7 +1668,7 @@ static void task_numa_compare(struct task_numa_env *env,/* * In the overloaded case, try and keep the load balanced. */ - load =task_h_load (env->p) -task_h_load (cur); + load =task_se_h_load (&env->p->se) -task_se_h_load (&cur->se);if (!load) goto assign;
@@ -1706,7 +1706,7 @@ static void task_numa_find_cpu(struct task_numa_env *env,bool maymove = false;int cpu; - load =task_h_load (env->p); + load =task_se_h_load (&env->p->se); dst_load = env->dst_stats.load + load; src_load = env->src_stats.load - load;
@@ -3389,7 +3389,7 @@ static inline void add_tg_cfs_propagate(struct cfs_rq *cfs_rq, long runnable_sum* avg. The immediate corollary is that all (fair) tasks must be attached, see * post_init_entity_util_avg(). * -* cfs_rq->avg is used for task_h_load() and update_cfs_share() for example. +* cfs_rq->avg is used for task_se_h_load() and update_cfs_share() for example. * * Returns true if the load decayed or we removed load. *
@@ -3522,7 +3522,7 @@ static inline void update_load_avg(struct cfs_rq *cfs_rq, struct sched_entity *s/* * Track task load average for carrying it to new CPU after migrated, and -* track group sched_entity load average for task_h_load calc in migration +* track group sched_entity load average for task_se_h_load calc in migration */ if (se->avg.last_update_time && !(flags &SKIP_AGE_LOAD ))__update_load_avg_se (now, cfs_rq, se);
@@ -3751,7 +3751,7 @@ static inline void update_misfit_status(struct task_struct *p, struct rq *rq) return; } - rq->misfit_task_load =task_h_load (p); + rq->misfit_task_load =task_se_h_load (&p->se); }#else /* CONFIG_SMP */
@@ -5739,7 +5739,7 @@ wake_affine_weight(struct sched_domain *sd, struct task_struct *p, this_eff_load =target_load (this_cpu, sd->wake_idx);if (sync) { -unsigned long current_load =task_h_load (current); +unsigned long current_load =task_se_h_load (¤t->se);if (current_load > this_eff_load)return this_cpu;
@@ -5747,7 +5747,7 @@ wake_affine_weight(struct sched_domain *sd, struct task_struct *p, this_eff_load -= current_load; } - task_load =task_h_load (p); + task_load =task_se_h_load (&p->se); this_eff_load += task_load;if (sched_feat (WA_BIAS ))
@@ -7600,7 +7600,7 @@ static int detach_tasks(struct lb_env *env)if (!can_migrate_task (p, env)) goto next; - load =task_h_load (p); + load =task_se_h_load (&p->se);if (sched_feat (LB_MIN ) && load <16 && !env->sd->nr_balance_failed) goto next;
@@ -7833,12 +7833,12 @@ static void update_cfs_rq_h_load(struct cfs_rq *cfs_rq) } } -static unsigned long task_h_load (struct task_struct *p) +static unsigned long task_se_h_load (struct sched_entity *se) { -struct cfs_rq *cfs_rq =task_cfs_rq (p); +struct cfs_rq *cfs_rq =cfs_rq_of (se);update_cfs_rq_h_load (cfs_rq); -return div64_ul (p->se.avg.load_avg * cfs_rq->h_load, +return div64_ul (se->avg.load_avg * cfs_rq->h_load,cfs_rq_load_avg (cfs_rq) +1 ); }#else
@@ -7865,9 +7865,9 @@ static inline void update_blocked_averages(int cpu) rq_unlock_irqrestore(rq, &rf); } -static unsigned long task_h_load (struct task_struct *p) +static unsigned long task_se_h_load (struct sched_entity *se) { -return p->se.avg.load_avg; +return se->avg.load_avg; }#endif
Signature · 1 line
2.20.1
PATCH v5 15/15 sched,fair: remove se->depth
In reply to Rik van RielRemove se->depth and the code that touches it, since we no longer need any of that.
--- include/linux/sched.h | 1 - kernel/sched/fair.c | 50 ++----------------------------------------- 2 files changed, 2 insertions(+), 49 deletions(-)
include/linux/sched.h +0 -1
@@ -451,7 +451,6 @@ struct sched_entity {struct sched_statistics statistics;#ifdef CONFIG_FAIR_GROUP_SCHED -int depth;unsigned long enqueued_h_load;unsigned long enqueued_h_weight;u64 propagated_exec_runtime;
kernel/sched/fair.c +2 -48
77 lines not on this page · raw message
Signature · 1 line
2.20.1
PATCH v5 6/15 sched,cfs: use explicit cfs_rq of parent se helper
In reply to Rik van RielUse an explicit "cfs_rq of parent sched_entity" helper in a few strategic places, where cfs_rq_of(se) may no longer point at the right runqueue once we flatten the hierarchical cgroup runqueues.
No functional change.
--- kernel/sched/fair.c | 17 +++++++++++++---- 1 file changed, 13 insertions(+), 4 deletions(-)
kernel/sched/fair.c +13 -4
@@ -276,6 +276,15 @@ static inline struct cfs_rq *group_cfs_rq(struct sched_entity *grp)return grp->my_q; } +/* runqueue owned by the parent entity; the root cfs_rq for a top level se */ +static inline struct cfs_rq *group_cfs_rq_of_parent (struct sched_entity *se) +{ +if (se->parent) +return group_cfs_rq (se->parent); + +return cfs_rq_of (se); +} +static inline bool list_add_leaf_cfs_rq (struct cfs_rq *cfs_rq) {struct rq *rq =rq_of (cfs_rq);
@@ -3319,7 +3328,7 @@ static inline int propagate_entity_load_avg(struct sched_entity *se) gcfs_rq->propagate =0 ; - cfs_rq =cfs_rq_of (se); + cfs_rq =group_cfs_rq_of_parent (se);add_tg_cfs_propagate (cfs_rq, gcfs_rq->prop_runnable_sum);
@@ -7796,7 +7805,7 @@ static void update_cfs_rq_h_load(struct cfs_rq *cfs_rq)WRITE_ONCE (cfs_rq->h_load_next,NULL );for_each_sched_entity (se) { - cfs_rq =cfs_rq_of (se); + cfs_rq =group_cfs_rq_of_parent (se);WRITE_ONCE (cfs_rq->h_load_next, se);if (cfs_rq->last_h_load_update == now)break ;
@@ -7819,7 +7828,7 @@ static void update_cfs_rq_h_load(struct cfs_rq *cfs_rq)static unsigned long task_se_h_load (struct sched_entity *se) { -struct cfs_rq *cfs_rq =cfs_rq_of (se); +struct cfs_rq *cfs_rq =group_cfs_rq_of_parent (se);update_cfs_rq_h_load (cfs_rq); return div64_ul(se->avg.load_avg * cfs_rq->h_load,
@@ -10166,7 +10175,7 @@ static void task_tick_fair(struct rq *rq, struct task_struct *curr, int queued)struct sched_entity *se = &curr->se;for_each_sched_entity (se) { - cfs_rq =cfs_rq_of (se); + cfs_rq =group_cfs_rq_of_parent (se);entity_tick (cfs_rq, se, queued); }
Signature · 1 line
2.20.1
PATCH v5 9/15 sched,fair: add helper functions for flattened runqueue
In reply to Rik van RielAdd helper functions to make the flattened runqueue patch a little smaller.
The task_se_h_weight function is similar to task_se_h_load, but scales the task weight by the group weight, without taking the task's duty cycle into account.
The task_se_in_cgroup helper is functionally identical to parent_entity, but directly calling a function with that name obscures what the other code is trying to use it for, and would make the code harder to understand.
--- kernel/sched/fair.c | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+)
kernel/sched/fair.c +31 -0
@@ -243,6 +243,7 @@ static u64 __calc_delta(u64 delta_exec, unsigned long weight, struct load_weightconst struct sched_class fair_sched_class;static unsigned long task_se_h_load (struct sched_entity *se); +static unsigned long task_se_h_weight (struct sched_entity *se);/************************************************************** * CFS operations on generic schedulable entities:
@@ -431,6 +432,12 @@ static inline struct sched_entity *parent_entity(struct sched_entity *se)return se->parent; } +/* Is this (task) sched_entity in a non-root cgroup? */ +static inline bool task_se_in_cgroup (struct sched_entity *se) +{ +return parent_entity (se); +} +static void find_matching_se (struct sched_entity **se,struct sched_entity **pse) {
@@ -513,6 +520,11 @@ static inline struct sched_entity *parent_entity(struct sched_entity *se)return NULL ; } +static inline bool task_se_in_cgroup (struct sched_entity *se) +{ +return false; +} +static inline void find_matching_se (struct sched_entity **se,struct sched_entity **pse) {
@@ -7853,6 +7865,20 @@ static void update_cfs_rq_h_load(struct cfs_rq *cfs_rq) } } +static unsigned long task_se_h_weight (struct sched_entity *se) +{ +struct cfs_rq *cfs_rq; + +if (!task_se_in_cgroup (se)) +return se->load.weight; + + cfs_rq =group_cfs_rq_of_parent (se); +update_cfs_rq_h_load (cfs_rq); + +/* Reduce the load.weight by the h_load of the group the task is in. */ +return (cfs_rq->h_load * se->load.weight) >>SCHED_FIXEDPOINT_SHIFT ; +} +static unsigned long task_se_h_load (struct sched_entity *se) {struct cfs_rq *cfs_rq =group_cfs_rq_of_parent (se);
@@ -7889,6 +7915,11 @@ static unsigned long task_se_h_load(struct sched_entity *se) {return se->avg.load_avg; } + +static unsigned long task_se_h_weight (struct sched_entity *se) +{ +return se->load.weight; +}#endif /********** Helpers for find_busiest_group ************************/
Signature · 1 line
2.20.1
PATCH v5 4/15 sched,fair: move runnable_load_avg to cfs_rq
In reply to Rik van RielSince only the root cfs_rq runnable_load_avg field is used any more, we can move the field from struct sched_avg, which every sched_entity has one of, directly into the struct cfs_rq, of which we have way fewer.
No functional changes.
--- include/linux/sched.h | 1 - kernel/sched/debug.c | 3 +-- kernel/sched/fair.c | 8 ++++---- kernel/sched/sched.h | 1 + 4 files changed, 6 insertions(+), 7 deletions(-)
include/linux/sched.h +0 -1
@@ -394,7 +394,6 @@ struct sched_avg {u32 util_sum;u32 period_contrib;unsigned long load_avg; -unsigned long runnable_load_avg;unsigned long util_avg;struct util_est util_est; }____cacheline_aligned ;
kernel/sched/debug.c +1 -2
@@ -539,7 +539,7 @@ void print_cfs_rq(struct seq_file *m, int cpu, struct cfs_rq *cfs_rq)SEQ_printf (m," .%-30s: %lu\n" ,"load_avg" , cfs_rq->avg.load_avg);SEQ_printf (m," .%-30s: %lu\n" ,"runnable_load_avg" , - cfs_rq->avg.runnable_load_avg); + cfs_rq->runnable_load_avg);SEQ_printf (m," .%-30s: %lu\n" ,"util_avg" , cfs_rq->avg.util_avg); SEQ_printf(m, " .%-30s: %u\n", "util_est_enqueued",
@@ -960,7 +960,6 @@ void proc_sched_show_task(struct task_struct *p, struct pid_namespace *ns,P (se.avg.load_sum);P (se.avg.util_sum);P (se.avg.load_avg); -P (se.avg.runnable_load_avg);P (se.avg.util_avg);P (se.enqueued_h_load);P (se.avg.last_update_time);
kernel/sched/fair.c +4 -4
@@ -2768,7 +2768,7 @@ enqueue_runnable_load_avg(struct cfs_rq *cfs_rq, struct sched_entity *se)struct cfs_rq *root_cfs_rq = &cfs_rq->rq->cfs; se->enqueued_h_load =task_se_h_load (se); - root_cfs_rq->avg.runnable_load_avg += se->enqueued_h_load; + root_cfs_rq->runnable_load_avg += se->enqueued_h_load; } }
@@ -2777,7 +2777,7 @@ dequeue_runnable_load_avg(struct cfs_rq *cfs_rq, struct sched_entity *se) {if (entity_is_task (se)) {struct cfs_rq *root_cfs_rq = &cfs_rq->rq->cfs; -sub_positive (&root_cfs_rq->avg.runnable_load_avg, +sub_positive (&root_cfs_rq->runnable_load_avg, se->enqueued_h_load); } }
@@ -2795,7 +2795,7 @@ update_runnable_load_avg(struct sched_entity *se) new_h_load =task_se_h_load (se); delta = new_h_load - se->enqueued_h_load; - root_cfs_rq->avg.runnable_load_avg += delta; + root_cfs_rq->runnable_load_avg += delta; se->enqueued_h_load = new_h_load; }
@@ -3561,7 +3561,7 @@ static void remove_entity_load_avg(struct sched_entity *se)static inline unsigned long cfs_rq_runnable_load_avg (struct cfs_rq *cfs_rq) { -return cfs_rq->avg.runnable_load_avg; +return cfs_rq->runnable_load_avg; }static inline unsigned long cfs_rq_load_avg (struct cfs_rq *cfs_rq)
kernel/sched/sched.h +1 -0
@@ -516,6 +516,7 @@ struct cfs_rq {* CFS load tracking */ struct sched_avg avg; +unsigned long runnable_load_avg;#ifndef CONFIG_64BIT u64 load_last_update_time_copy;#endif
Signature · 1 line
2.20.1
PATCH v5 8/15 sched,fair: refactor enqueue/dequeue_entity
In reply to Rik van RielRefactor enqueue_entity, dequeue_entity, and update_load_avg, in order to split out the things we still want to happen at every level in the cgroup hierarchy with a flat runqueue from the things we only need to happen once.
No functional changes.
--- kernel/sched/fair.c | 64 +++++++++++++++++++++++++++++---------------- 1 file changed, 42 insertions(+), 22 deletions(-)
kernel/sched/fair.c +42 -22
@@ -3518,7 +3518,7 @@ static void detach_entity_load_avg(struct cfs_rq *cfs_rq, struct sched_entity *s#define DO_ATTACH 0x4/* Update task and its cfs_rq load average */ -static inline void update_load_avg (struct cfs_rq *cfs_rq,struct sched_entity *se,int flags) +static inline bool update_load_avg (struct cfs_rq *cfs_rq,struct sched_entity *se,int flags) {u64 now =cfs_rq_clock_pelt (cfs_rq);int decayed;
@@ -3547,6 +3547,8 @@ static inline void update_load_avg(struct cfs_rq *cfs_rq, struct sched_entity *s }else if (decayed && (flags &UPDATE_TG ))update_tg_load_avg (cfs_rq,0 ); + +return decayed; }#ifndef CONFIG_64BIT
@@ -3763,9 +3765,10 @@ static inline void update_misfit_status(struct task_struct *p, struct rq *rq)#define SKIP_AGE_LOAD 0x0#define DO_ATTACH 0x0 -static inline void update_load_avg (struct cfs_rq *cfs_rq,struct sched_entity *se,int not_used1) +static inline bool update_load_avg (struct cfs_rq *cfs_rq,struct sched_entity *se,int not_used1) {cfs_rq_util_change (cfs_rq,0 ); +return false; }static inline void remove_entity_load_avg (struct sched_entity *se) {}
@@ -3888,6 +3891,24 @@ static inline void check_schedstat_required(void)* CPU and an up-to-date min_vruntime on the destination CPU. */ +static bool +enqueue_entity_groups (struct cfs_rq *cfs_rq,struct sched_entity *se,int flags) +{ +/* +* When enqueuing a sched_entity, we must: +* - Update loads to have both entity and cfs_rq synced with now. +* - Add its load to cfs_rq->runnable_avg +* - For group_entity, update its weight to reflect the new share of +* its group cfs_rq +* - Add its new weight to cfs_rq->load.weight +*/ +if (!update_load_avg (cfs_rq, se,UPDATE_TG |DO_ATTACH )) +return false; + +update_cfs_group (se); +return true; +} +static void enqueue_entity (struct cfs_rq *cfs_rq,struct sched_entity *se,int flags) {
@@ -3912,16 +3933,6 @@ enqueue_entity(struct cfs_rq *cfs_rq, struct sched_entity *se, int flags)if (renorm && !curr) se->vruntime += cfs_rq->min_vruntime; -/* -* When enqueuing a sched_entity, we must: -* - Update loads to have both entity and cfs_rq synced with now. -* - Add its load to cfs_rq->runnable_avg -* - For group_entity, update its weight to reflect the new share of -* its group cfs_rq -* - Add its new weight to cfs_rq->load.weight -*/ -update_load_avg (cfs_rq, se,UPDATE_TG |DO_ATTACH ); -update_cfs_group (se);enqueue_runnable_load_avg (cfs_rq, se);account_entity_enqueue (cfs_rq, se);
@@ -3988,14 +3999,9 @@ static void clear_buddies(struct cfs_rq *cfs_rq, struct sched_entity *se) static __always_inline void return_cfs_rq_runtime(struct cfs_rq *cfs_rq); -static void -dequeue_entity (struct cfs_rq *cfs_rq,struct sched_entity *se,int flags) +static bool +dequeue_entity_groups (struct cfs_rq *cfs_rq,struct sched_entity *se,int flags) { -/* -* Update run-time statistics of the 'current'. -*/ -update_curr (cfs_rq); -/* * When dequeuing a sched_entity, we must: * - Update loads to have both entity and cfs_rq synced with now.
@@ -4004,7 +4010,21 @@ dequeue_entity(struct cfs_rq *cfs_rq, struct sched_entity *se, int flags)* - For group entity, update its weight to reflect the new share * of its group cfs_rq. */ -update_load_avg (cfs_rq, se,UPDATE_TG ); +if (!update_load_avg (cfs_rq, se,UPDATE_TG )) +return false; +update_cfs_group (se); + +return true; +} + +static void +dequeue_entity (struct cfs_rq *cfs_rq,struct sched_entity *se,int flags) +{ +/* +* Update run-time statistics of the 'current'. +*/ +update_curr (cfs_rq); +dequeue_runnable_load_avg (cfs_rq, se);update_stats_dequeue (cfs_rq, se, flags);
@@ -4028,8 +4048,6 @@ dequeue_entity(struct cfs_rq *cfs_rq, struct sched_entity *se, int flags)/* return excess runtime on last dequeue */ return_cfs_rq_runtime (cfs_rq); -update_cfs_group (se); -/* * Now advance min_vruntime if @se was the entity holding it back, * except when: DEQUEUE_SAVE && !DEQUEUE_MOVE, in this case we'll be
@@ -5194,6 +5212,7 @@ enqueue_task_fair(struct rq *rq, struct task_struct *p, int flags)if (se->on_rq)break ; cfs_rq =cfs_rq_of (se); +enqueue_entity_groups (cfs_rq, se, flags);enqueue_entity (cfs_rq, se, flags);/*
@@ -5276,6 +5295,7 @@ static void dequeue_task_fair(struct rq *rq, struct task_struct *p, int flags)for_each_sched_entity (se) { cfs_rq =cfs_rq_of (se); +dequeue_entity_groups (cfs_rq, se, flags);dequeue_entity (cfs_rq, se, flags);/*
Signature · 1 line
2.20.1
PATCH v5 7/15 sched,cfs: fix zero length timeslice calculation
In reply to Rik van RielThe way the time slice length is currently calculated, not only do high priority tasks get longer time slices than low priority tasks, but due to fixed point math, low priority tasks could end up with a zero length time slice. This can lead to cache thrashing and other inefficiencies.
Cap the minimum time slice length to sysctl_sched_min_granularity.
Tasks that end up getting a time slice length too long for their relative priority will simply end up having their vruntime advanced much faster than other tasks, resulting in them receiving time slices less frequently.
--- kernel/sched/fair.c | 7 +++++++ 1 file changed, 7 insertions(+)
kernel/sched/fair.c +7 -0
@@ -732,6 +732,13 @@ static u64 sched_slice(struct cfs_rq *cfs_rq, struct sched_entity *se) } slice =__calc_delta (slice, se->load.weight, load); } + +/* +* To avoid cache thrashing, run at least sysctl_sched_min_granularity. +* The vruntime of a low priority task advances faster; those tasks +* will simply get time slices less frequently. +*/ + slice =max_t (u64, slice, sysctl_sched_min_granularity);return slice; }
Signature · 1 line
2.20.1
PATCH v5 12/15 sched,fair: propagate sum_exec_runtime up the hierarchy
In reply to Rik van RielNow that enqueue_task_fair and dequeue_task_fair no longer iterate up the hierarchy all the time, a method to lazily propagate sum_exec_runtime up the hierarchy is necessary.
Once a tick, propagate the newly accumulated exec_runtime up the hierarchy, and feed it into CFS bandwidth control.
Remove the pointless call to account_cfs_rq_runtime from update_curr, which is always called with a root cfs_rq.
--- include/linux/sched.h | 1 + kernel/sched/core.c | 1 + kernel/sched/fair.c | 22 ++++++++++++++++++++-- 3 files changed, 22 insertions(+), 2 deletions(-)
include/linux/sched.h +1 -0
@@ -454,6 +454,7 @@ struct sched_entity {int depth;unsigned long enqueued_h_load;unsigned long enqueued_h_weight; +u64 propagated_exec_runtime;struct load_weight h_load;struct sched_entity *parent;/* rq on which this entity is (to be) queued: */
kernel/sched/core.c +1 -0
@@ -2137,6 +2137,7 @@ static void __sched_fork(unsigned long clone_flags, struct task_struct *p)INIT_LIST_HEAD (&p->se.group_node);#ifdef CONFIG_FAIR_GROUP_SCHED + p->se.propagated_exec_runtime =0 ; p->se.cfs_rq =NULL ;#endif
kernel/sched/fair.c +20 -2
@@ -916,8 +916,6 @@ static void update_curr(struct cfs_rq *cfs_rq)trace_sched_stat_runtime (curtask, delta_exec, curr->vruntime);cgroup_account_cputime (curtask, delta_exec);account_group_exec_runtime (curtask, delta_exec); - -account_cfs_rq_runtime (cfs_rq, delta_exec); }static void update_curr_fair (struct rq *rq)
@@ -3430,6 +3428,20 @@ static inline bool skip_blocked_update(struct sched_entity *se) return true; } +static void propagate_exec_runtime (struct cfs_rq *cfs_rq, +struct sched_entity *se) +{ +struct sched_entity *parent = se->parent; +u64 diff = se->sum_exec_runtime - se->propagated_exec_runtime; + +if (parent) { + parent->sum_exec_runtime += diff; +account_cfs_rq_runtime (cfs_rq, diff); + } + + se->propagated_exec_runtime = se->sum_exec_runtime; +} +#else /* CONFIG_FAIR_GROUP_SCHED */ static inline void update_tg_load_avg (struct cfs_rq *cfs_rq,int force) {}
@@ -3441,6 +3453,11 @@ static inline int propagate_entity_load_avg(struct sched_entity *se)static inline void add_tg_cfs_propagate (struct cfs_rq *cfs_rq,long runnable_sum) {} +static void propagate_exec_runtime (struct cfs_rq *cfs_rq, +struct sched_entity *se); +{ +} +#endif /* CONFIG_FAIR_GROUP_SCHED */ /**
@@ -10175,6 +10192,7 @@ static void propagate_entity_cfs_rq(struct sched_entity *se, int flags)if (!(flags &DO_ATTACH ))break ; +propagate_exec_runtime (cfs_rq, se);update_cfs_group (se); } }
Signature · 1 line
2.20.1
PATCH v5 13/15 sched,fair: ramp up task_se_h_weight quickly
In reply to Rik van RielThe code in update_cfs_group / calc_group_shares has some logic to quickly ramp up the load when a task has just started running in a cgroup, in order to get sane values for the cgroup se->load.weight.
This code adds a similar hack to task_se_h_weight.
However, THIS CODE IS WRONG, since it does not do things hierarchically.
I am wondering a few things here:
1) Should I have something similar to the logic in calc_group_shares in update_cfs_rq_h_load?
2) If so, should I also use that fast-ramp-up value for task_h_load, to prevent the load balancer from thinking it is moving zero weight tasks around?
3) If update_cfs_rq_h_load is the wrong place, where should I be calculating a hierarchical group weight value, instead?
--- kernel/sched/fair.c | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-)
kernel/sched/fair.c +6 -1
@@ -7690,6 +7690,7 @@ static void update_cfs_rq_h_load(struct cfs_rq *cfs_rq)static unsigned long task_se_h_weight (struct sched_entity *se) { +unsigned long group_load;struct cfs_rq *cfs_rq;if (!task_se_in_cgroup (se))
@@ -7698,8 +7699,12 @@ static unsigned long task_se_h_weight(struct sched_entity *se) cfs_rq =group_cfs_rq_of_parent (se);update_cfs_rq_h_load (cfs_rq); +/* Ramp up quickly to keep h_weight sane. */ + group_load =max (scale_load_down (se->parent->load.weight), + cfs_rq->h_load); +/* Reduce the load.weight by the h_load of the group the task is in. */ -return (cfs_rq->h_load * se->load.weight) >>SCHED_FIXEDPOINT_SHIFT ; +return (group_load * se->load.weight) >>SCHED_FIXEDPOINT_SHIFT ; }static unsigned long task_se_h_load (struct sched_entity *se)
Signature · 1 line
2.20.1
PATCH v5 14/15 sched,fair: scale vdiff in wakeup_preempt_entity
In reply to Rik van RielWhen a task wakes back up after having gone to sleep, place_entity will limit the vruntime difference between min_vruntime and the woken up task to half of sysctl_sched_latency.
The code in wakeup_preempt_entity calculates how much vruntime a time slice for the woken up task represents, in wakeup_gran.
It then assumes that all the vruntime used since the task went to sleep was used by the currently running task (which has its vruntime scaled by calc_delta_fair, as well).
However, that assumption is not necessarily true, and the vruntime may have advanced at different rates, pushed ahead by different tasks on the CPU. This becomes more visible when the CPU controller is enabled.
This leads to the symptom that a high priority woken up task is likely to preempt whatever is running, even if the currently running task is of equal or higher priority than the woken up task!
Scaling the vdiff down if the currently running task is also high priority solves that symptom.
This is not the correct thing to do if all of the vruntime was accumulated by the current task, or other tasks at similar priority, and already scaled by the same priority, but I do not have any better ideas on how to tackle the "task X got preempted by task Y of the same priority" issue that system administrators try to resolve by setting the sched_wakeup_granularity sysctl variable to a larger value than half of sysctl_sched_latency...
--- kernel/sched/fair.c | 1 + 1 file changed, 1 insertion(+)
kernel/sched/fair.c +1 -0
@@ -6792,6 +6792,7 @@ wakeup_preempt_entity(struct sched_entity *curr, struct sched_entity *se)if (vdiff <=0 )return -1 ; + vdiff =min ((u64 )vdiff,calc_delta_fair (vdiff, curr)); gran =wakeup_gran (se);if (vdiff > gran)return 1 ;
Signature · 1 line
2.20.1
PATCH v5 5/15 sched,fair: remove cfs_rqs from leaf_cfs_rq_list bottom up
In reply to Rik van RielReducing the overhead of the CPU controller is achieved by not walking all the sched_entities every time a task is enqueued or dequeued.
One of the things being checked every single time is whether the cfs_rq is on the rq->leaf_cfs_rq_list.
By only removing a cfs_rq from the list once it no longer has children on the list, we can avoid walking the sched_entity hierarchy if the bottom cfs_rq is on the list, once the runqueues have been flattened.
--- kernel/sched/fair.c | 35 ++++++++++++++++++++++++++++++++++- 1 file changed, 34 insertions(+), 1 deletion(-)
kernel/sched/fair.c +34 -1
@@ -369,6 +369,39 @@ static inline void assert_list_leaf_cfs_rq(struct rq *rq)SCHED_WARN_ON (rq->tmp_alone_branch != &rq->leaf_cfs_rq_list); } +/* +* Because list_add_leaf_cfs_rq always places a child cfs_rq on the list +* immediately before a parent cfs_rq, and cfs_rqs are removed from the list +* bottom-up, we only have to test whether the cfs_rq before us on the list +* is our child. +*/ +static inline bool child_cfs_rq_on_list (struct cfs_rq *cfs_rq) +{ +struct cfs_rq *prev_cfs_rq; +struct list_head *prev; + + prev = cfs_rq->leaf_cfs_rq_list.prev; + prev_cfs_rq = container_of(prev, struct cfs_rq, leaf_cfs_rq_list); + +return (prev_cfs_rq->tg->parent == cfs_rq->tg); +} + +/* +* Remove a cfs_rq from the list if it has no children on the list. +* The scheduler iterates over the list regularly; if conditions for +* removal are still true, we'll get to this cfs_rq in the future. +*/ +static inline void list_del_leaf_cfs_rq_bottom (struct cfs_rq *cfs_rq) +{ +if (!cfs_rq->on_list) +return ; + +if (child_cfs_rq_on_list (cfs_rq)) +return ; + +list_del_leaf_cfs_rq (cfs_rq); +} +/* Iterate thr' all leaf cfs_rq's on a runqueue */ #define for_each_leaf_cfs_rq_safe (rq, cfs_rq, pos) \ list_for_each_entry_safe(cfs_rq, pos, &rq->leaf_cfs_rq_list, \
@@ -7723,7 +7756,7 @@ static void update_blocked_averages(int cpu)* decayed cfs_rqs linger on the list. */ if (cfs_rq_is_decayed (cfs_rq)) -list_del_leaf_cfs_rq (cfs_rq); +list_del_leaf_cfs_rq_bottom (cfs_rq);/* Don't need periodic decay once load/util_avg are null */ if (cfs_rq_has_blocked (cfs_rq))
Signature · 1 line
2.20.1