sched: Rewrite per entity runnable load average tracking
v6 cover sched: Rewrite per entity runnable load average tracking
Many thanks to PeterZ for his review, to Dietmar, and to Fengguang for 0Day and LKP.
v6 changes:
Based on v3.18-rc2.
- Unify decay_load 32 and 64 bits by mul_u64_u32_shr
- Add force option in update_tg_load_avg
- Read real-time cfs's load_avg for calc_tg_weight
- Have tg_load_avg_contrib ifdef CONFIG_FAIR_GROUP_SCHED
- Bug fix
v5 changes:
Thank Peter intensively for reviewing this patchset in detail and all his comments. And Mike for general and cgroup pipe-test. Morten, Ben, and Vincent in the discussion.
- Remove dead task and task group load_avg
- Do not update trivial delta to task_group load_avg (threshold 1/64 old_contrib)
- mul_u64_u32_shr() is used in decay_load, so on 64bit, load_sum can afford about 4353082796 (=2^64/47742/88761) entities with the highest weight (=88761) always runnable, greater than previous theoretical maximum 132845
- Various code efficiency and style changes
We carried out some performance tests (thanks to Fengguang and his LKP). The results are shown as follows. The patchset (including threepatches) is on top of mainline v3.16-rc5. We may report more perf numbers later.
Overall, this rewrite has better performance, and reduced net overhead in load average tracking, flat efficiency in multi-layer cgroup pipe-test.
v4 changes:
Thanks to Morten, Ben, and Fengguang for v4 revision.
- Insert memory barrier before writing cfs_rq->load_last_update_copy.
- Fix typos.
v3 changes:
Many thanks to Ben for v3 revision.
Regarding the overflow issue, we now have for both entity and cfs_rq:
struct sched_avg {
.....
u64 load_sum;
unsigned long load_avg;
.....
};Given the weight for both entity and cfs_rq is:
struct load_weight {
unsigned long weight;
.....
};So, load_sum's max is 47742 * load.weight (which is unsigned long), then on 32bit, it is absolutly safe. On 64bit, with unsigned long being 64bit, but we can afford about 4353082796 (=2^64/47742/88761) entities with the highest weight (=88761) always runnable, even considering we may multiply 1<<15 in decay_load64, we can still support 132845 (=4353082796/2^15) always runnable, which should be acceptible.
load_avg = load_sum / 47742 = load.weight (which is unsigned long), so it should be perfectly safe for both entity (even with arbitrary user group share) and cfs_rq on both 32bit and 64bit. Originally, we saved this division, but have to get it back because of the overflow issue on 32bit (actually load average itself is safe from overflow, but the rest of the code referencing it always uses long, such as cpu_load, etc., which prevents it from saving).
- Fix overflow issue both for entity and cfs_rq on both 32bit and 64bit.
- Track all entities (both task and group entity) due to group entity's clock issue. This actually improves code simplicity.
- Make a copy of cfs_rq sched_avg's last_update_time, to read an intact 64bit variable on 32bit machine when in data race (hope I did it right).
- Minor fixes and code improvement.
v2 changes:
Thanks to PeterZ and Ben for their help in fixing the issues and improving the quality, and Fengguang and his 0Day in finding compile errors in different configurations for version 2.
- Batch update the tg->load_avg, making sure it is up-to-date before update_cfs_shares
- Remove migrating task from the old CPU/cfs_rq, and do so with atomic operations
Yuyang Du (3):
sched: Remove update_rq_runnable_avg
sched: Rewrite per entity runnable load average tracking
sched: Remove task and group entity load_avg when they are dead
include/linux/sched.h | 21 +- kernel/sched/debug.c | 29 +-- kernel/sched/fair.c | 584 ++++++++++++++++--------------------------------- kernel/sched/proc.c | 2 +- kernel/sched/sched.h | 24 +- 5 files changed, 215 insertions(+), 445 deletions(-)
Signature · 1 line
1.7.9.5
PATCH v6 1/3 sched: Remove update_rq_runnable_avg
The current rq->avg is not made use of anywhere, and the code is in fair scheduler's critical path, so remove it.
--- kernel/sched/debug.c | 7 +------ kernel/sched/fair.c | 24 ++++-------------------- kernel/sched/sched.h | 2 -- 3 files changed, 5 insertions(+), 28 deletions(-)
kernel/sched/debug.c +1 -6
@@ -68,13 +68,8 @@ static void print_cfs_group_stats(struct seq_file *m, int cpu, struct task_group#define PN (F ) \ SEQ_printf(m, " .%-30s: %lld.%06ld\n", #F, SPLIT_NS((long long)F)) -if (!se) { -struct sched_avg *avg = &cpu_rq (cpu)->avg; -P (avg->runnable_avg_sum); -P (avg->runnable_avg_period); +if (!se)return ; - } -PN (se->exec_start);PN (se->vruntime);
kernel/sched/fair.c +4 -20
@@ -2459,18 +2459,12 @@ static inline void __update_group_entity_contrib(struct sched_entity *se) } } -static inline void update_rq_runnable_avg (struct rq *rq,int runnable) -{ -__update_entity_runnable_avg (rq_clock_task (rq), &rq->avg, runnable); -__update_tg_runnable_avg (&rq->avg, &rq->cfs); -}#else /* CONFIG_FAIR_GROUP_SCHED */ static inline void __update_cfs_rq_tg_load_contrib (struct cfs_rq *cfs_rq,int force_update) {}static inline void __update_tg_runnable_avg (struct sched_avg *sa,struct cfs_rq *cfs_rq) {}static inline void __update_group_entity_contrib (struct sched_entity *se) {} -static inline void update_rq_runnable_avg (struct rq *rq,int runnable) {}#endif /* CONFIG_FAIR_GROUP_SCHED */ static inline void __update_task_entity_contrib (struct sched_entity *se)
@@ -2643,7 +2637,6 @@ static inline void dequeue_entity_load_avg(struct cfs_rq *cfs_rq,*/ void idle_enter_fair (struct rq *this_rq) { -update_rq_runnable_avg (this_rq,1 ); }/*
@@ -2653,7 +2646,6 @@ void idle_enter_fair(struct rq *this_rq)*/ void idle_exit_fair (struct rq *this_rq) { -update_rq_runnable_avg (this_rq,0 ); }static int idle_balance (struct rq *this_rq);
@@ -2662,7 +2654,6 @@ static int idle_balance(struct rq *this_rq);static inline void update_entity_load_avg (struct sched_entity *se,int update_cfs_rq) {} -static inline void update_rq_runnable_avg (struct rq *rq,int runnable) {}static inline void enqueue_entity_load_avg (struct cfs_rq *cfs_rq,struct sched_entity *se,int wakeup) {}
@@ -3976,10 +3967,9 @@ enqueue_task_fair(struct rq *rq, struct task_struct *p, int flags)update_entity_load_avg (se,1 ); } -if (!se) { -update_rq_runnable_avg (rq, rq->nr_running); +if (!se)add_nr_running (rq,1 ); - } +hrtick_update (rq); }
@@ -4037,10 +4027,9 @@ static void dequeue_task_fair(struct rq *rq, struct task_struct *p, int flags)update_entity_load_avg (se,1 ); } -if (!se) { +if (!se)sub_nr_running (rq,1 ); -update_rq_runnable_avg (rq,1 ); - } +hrtick_update (rq); }
@@ -5527,9 +5516,6 @@ static void __update_blocked_averages_cpu(struct task_group *tg, int cpu)*/ if (!se->avg.runnable_avg_sum && !cfs_rq->nr_running)list_del_leaf_cfs_rq (cfs_rq); - }else { -struct rq *rq =rq_of (cfs_rq); -update_rq_runnable_avg (rq, rq->nr_running); } }
@@ -7507,8 +7493,6 @@ static void task_tick_fair(struct rq *rq, struct task_struct *curr, int queued)if (numabalancing_enabled)task_tick_numa (rq, curr); - -update_rq_runnable_avg (rq,1 ); }/*
kernel/sched/sched.h +0 -2
@@ -553,8 +553,6 @@ struct rq {#ifdef CONFIG_FAIR_GROUP_SCHED /* list of leaf cfs_rq on this cpu: */ struct list_head leaf_cfs_rq_list; - -struct sched_avg avg;#endif /* CONFIG_FAIR_GROUP_SCHED */ /*
Signature · 1 line
1.7.9.5
PATCH v6 2/3 sched: Rewrite per entity runnable load average tracking
In reply to Yuyang DuThe idea of per entity runnable load average (let runnable time contribute to load weight) was proposed by Paul Turner, and it is still followed by this rewrite. This rewrite is done due to the following ends:
1. cfs_rq's load average (namely runnable_load_avg and blocked_load_avg) is updated at the granularity of one entity at one time, which results in the cfs_rq load average is partially updated or asynchronous across its entities: at any time, only one entity is up to date and contributes to the cfs_rq, all other entities are effectively lagging behind.
2. cfs_rq load average is different between top rq->cfs_rq and other task_group's per CPU cfs_rqs in whether or not blocked_load_average contributes to the load.
3. How task_group's load is calculated is complex.
This rewrite tackles these by:
1. Combine runnable and blocked load averages for cfs_rq. And track cfs_rq's load average as a whole and is used as such.
2. Track task entity load average for carrying it between CPUs in migration, group cfs_rq and its own entity load averages are tracked for update_cfs_shares and task_h_load calc. task_group's load_avg is aggregated from its per CPU cfs_rq's load_avg, which is aggregated from its sched_entities (both task and group entity). Group entity's weight is proportional to its own cfs_rq's load_avg / task_group's load_avg.
3. All task, cfs_rq/group_entity, and task_group have simple, consistent, up-to-date, and synchronized load_avg.
This rewrite in principle is equivalent to the previous in functionality, but significantly reduces code coplexity and hence increases efficiency and clarity. In addition, the new load_avg is much more smooth/continuous (no abrupt jumping ups and downs) and decayed/updated more quickly and synchronously to reflect the load dynamic. As a result, we have less load tracking overhead and better performance.
--- include/linux/sched.h | 21 +- kernel/sched/debug.c | 22 +- kernel/sched/fair.c | 549 ++++++++++++++++--------------------------------- kernel/sched/proc.c | 2 +- kernel/sched/sched.h | 22 +- 5 files changed, 200 insertions(+), 416 deletions(-)
include/linux/sched.h +13 -8
@@ -1071,16 +1071,21 @@ struct load_weight {u32 inv_weight; }; +/* +* The load_avg represents an infinite geometric series. The 64 bit +* load_sum can: +* 1) for cfs_rq, afford 4353082796 (=2^64/47742/88761) entities with +* the highest weight (=88761) always runnable, we should not overflow +* 2) for entity, support any load.weight always runnable +*/ struct sched_avg {/* -* These sums represent an infinite geometric series and so are bound -* above by 1024/(1-y). Thus we only need a u32 to store them for all -* choices of y < 1-2^(-32)*1024. +* The load_avg represents an infinite geometric series. */ -u32 runnable_avg_sum, runnable_avg_period; -u64 last_runnable_update; -s64 decay_count; -unsigned long load_avg_contrib; +u64 last_update_time; +u64 load_sum; +unsigned long load_avg; +u32 period_contrib; };#ifdef CONFIG_SCHEDSTATS
@@ -1146,7 +1151,7 @@ struct sched_entity {#endif #ifdef CONFIG_SMP -/* Per-entity load-tracking */ +/* Per entity load average tracking */ struct sched_avg avg;#endif };
kernel/sched/debug.c +4 -18
@@ -88,10 +88,7 @@ static void print_cfs_group_stats(struct seq_file *m, int cpu, struct task_group#endif P (se->load.weight);#ifdef CONFIG_SMP -P (se->avg.runnable_avg_sum); -P (se->avg.runnable_avg_period); -P (se->avg.load_avg_contrib); -P (se->avg.decay_count); +P (se->my_q->avg.load_avg);#endif #undef PN#undef P
@@ -205,19 +202,11 @@ 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_load_avg" , - cfs_rq->runnable_load_avg); -SEQ_printf (m," .%-30s: %ld\n" ,"blocked_load_avg" , - cfs_rq->blocked_load_avg); +SEQ_printf (m," .%-30s: %lu\n" ,"load_avg" , + cfs_rq->avg.load_avg);#ifdef CONFIG_FAIR_GROUP_SCHED -SEQ_printf (m," .%-30s: %ld\n" ,"tg_load_contrib" , - cfs_rq->tg_load_contrib); -SEQ_printf (m," .%-30s: %d\n" ,"tg_runnable_contrib" , - cfs_rq->tg_runnable_contrib);SEQ_printf (m," .%-30s: %ld\n" ,"tg_load_avg" ,atomic_long_read (&cfs_rq->tg->load_avg)); -SEQ_printf (m," .%-30s: %d\n" ,"tg->runnable_avg" , -atomic_read (&cfs_rq->tg->runnable_avg));#endif #endif #ifdef CONFIG_CFS_BANDWIDTH
@@ -622,10 +611,7 @@ void proc_sched_show_task(struct task_struct *p, struct seq_file *m)P (se.load.weight);#ifdef CONFIG_SMP -P (se.avg.runnable_avg_sum); -P (se.avg.runnable_avg_period); -P (se.avg.load_avg_contrib); -P (se.avg.decay_count); +P (se.avg.load_avg);#endif P (policy);P (prio);
kernel/sched/fair.c +173 -376
844 lines not on this page · raw message
kernel/sched/proc.c +1 -1
@@ -497,7 +497,7 @@ static void __update_cpu_load(struct rq *this_rq, unsigned long this_load,#ifdef CONFIG_SMP static inline unsigned long get_rq_runnable_load (struct rq *rq) { -return rq->cfs.runnable_load_avg; +return rq->cfs.avg.load_avg; }#else static inline unsigned long get_rq_runnable_load (struct rq *rq)
kernel/sched/sched.h +9 -13
@@ -218,7 +218,6 @@ struct task_group {#ifdef CONFIG_SMP atomic_long_t load_avg; -atomic_t runnable_avg;#endif #endif
@@ -339,21 +338,18 @@ struct cfs_rq {#ifdef CONFIG_SMP /* -* CFS Load tracking -* Under CFS, load is tracked on a per-entity basis and aggregated up. -* This allows for the description of both thread and group usage (in -* the FAIR_GROUP_SCHED case). +* CFS load tracking */ -unsigned long runnable_load_avg, blocked_load_avg; -atomic64_t decay_counter; -u64 last_decay; -atomic_long_t removed_load; - +struct sched_avg avg;#ifdef CONFIG_FAIR_GROUP_SCHED -/* Required to track per-cpu representation of a task_group */ -u32 tg_runnable_contrib; -unsigned long tg_load_contrib; +unsigned long tg_load_avg_contrib; +#endif +atomic_long_t removed_load_avg; +#ifndef CONFIG_64BIT +u64 load_last_update_time_copy; +#endif +#ifdef CONFIG_FAIR_GROUP_SCHED /* * h_load = weight * f(tg) *
Signature · 1 line
1.7.9.5
PATCH v6 3/3 sched: Remove task and group entity load_avg when they are dead
In reply to Yuyang DuWhen task exits or group is destroyed, the entity's load_avg should be removed from its parent cfs_rq's load_avg. Otherwise, it will take time for the parent cfs_rq to decay the dead entity's load_avg to 0, which is not desired.
--- kernel/sched/fair.c | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-)
kernel/sched/fair.c +10 -1
@@ -4444,6 +4444,11 @@ static void migrate_task_rq_fair(struct task_struct *p, int next_cpu)/* We have migrated, no longer consider this task hot */ p->se.exec_start =0 ; } + +static void task_dead_fair (struct task_struct *p) +{ +remove_entity_load_avg (&p->se); +}#endif /* CONFIG_SMP */ static unsigned long
@@ -7520,8 +7525,11 @@ void free_fair_sched_group(struct task_group *tg)for_each_possible_cpu (i) {if (tg->cfs_rq)kfree (tg->cfs_rq[i]); -if (tg->se) +if (tg->se) { +if (tg->se[i]) +remove_entity_load_avg (tg->se[i]);kfree (tg->se[i]); + } }kfree (tg->cfs_rq);
@@ -7707,6 +7715,7 @@ const struct sched_class fair_sched_class = { .rq_offline = rq_offline_fair, .task_waking = task_waking_fair, + .task_dead = task_dead_fair, #endif .set_curr_task = set_curr_task_fair,
Signature · 1 line
1.7.9.5
Ping...
Quoted 12 lines · Yuyang Du
Many thanks to PeterZ for his review, to Dietmar, and to Fengguang for 0Day and LKP.
v6 changes:
Based on v3.18-rc2.
- Unify
decay_load32 and 64 bits bymul_u64_u32_shr
- Add force option inupdate_tg_load_avg
- Read real-time cfs'sload_avgforcalc_tg_weight
- Havetg_load_avg_contribifdefCONFIG_FAIR_GROUP_SCHED
- Bug fix