From: Morten Rasmussen <morten.rasmussen@arm.com>
To: Peter Zijlstra <peterz@infradead.org>
Cc: mingo@kernel.org, linux-kernel@vger.kernel.org, tj@kernel.org,
josef@toxicpanda.com, torvalds@linux-foundation.org,
vincent.guittot@linaro.org, efault@gmx.de, pjt@google.com,
clm@fb.com, dietmar.eggemann@arm.com, bsegall@google.com,
yuyang.du@intel.com
Subject: Re: [PATCH -v2 02/18] sched/fair: Add comment to calc_cfs_shares()
Date: Thu, 28 Sep 2017 11:03:03 +0100 [thread overview]
Message-ID: <20170928100303.GA962@e105550-lin.cambridge.arm.com> (raw)
In-Reply-To: <20170901132748.083733695@infradead.org>
On Fri, Sep 01, 2017 at 03:21:01PM +0200, Peter Zijlstra wrote:
> Explain the magic equation in calc_cfs_shares() a bit better.
>
> Signed-off-by: Peter Zijlstra (Intel) <peterz@infradead.org>
> ---
> kernel/sched/fair.c | 61 ++++++++++++++++++++++++++++++++++++++++++++++++++++
> 1 file changed, 61 insertions(+)
>
> --- a/kernel/sched/fair.c
> +++ b/kernel/sched/fair.c
> @@ -2707,6 +2707,67 @@ account_entity_dequeue(struct cfs_rq *cf
>
> #ifdef CONFIG_FAIR_GROUP_SCHED
> # ifdef CONFIG_SMP
> +/*
> + * All this does is approximate the hierarchical proportion which includes that
> + * global sum we all love to hate.
> + *
> + * That is, the weight of a group entity, is the proportional share of the
> + * group weight based on the group runqueue weights. That is:
> + *
> + * tg->weight * grq->load.weight
> + * ge->load.weight = ----------------------------- (1)
> + * \Sum grq->load.weight
> + *
> + * Now, because computing that sum is prohibitively expensive to compute (been
> + * there, done that) we approximate it with this average stuff. The average
> + * moves slower and therefore the approximation is cheaper and more stable.
> + *
> + * So instead of the above, we substitute:
> + *
> + * grq->load.weight -> grq->avg.load_avg (2)
> + *
> + * which yields the following:
> + *
> + * tg->weight * grq->avg.load_avg
> + * ge->load.weight = ------------------------------ (3)
> + * tg->load_avg
> + *
> + * Where: tg->load_avg ~= \Sum grq->avg.load_avg
> + *
> + * That is shares_avg, and it is right (given the approximation (2)).
> + *
> + * The problem with it is that because the average is slow -- it was designed
> + * to be exactly that of course -- this leads to transients in boundary
> + * conditions. In specific, the case where the group was idle and we start the
> + * one task. It takes time for our CPU's grq->avg.load_avg to build up,
> + * yielding bad latency etc..
> + *
> + * Now, in that special case (1) reduces to:
> + *
> + * tg->weight * grq->load.weight
> + * ge->load.weight = ----------------------------- = tg>weight (4)
> + * grp->load.weight
Should it be "grq->load.weight" in the denominator of (4)?
And "tg->weight" at the end?
> + *
> + * That is, the sum collapses because all other CPUs are idle; the UP scenario.
Shouldn't (3) collapse in the same way too in this special case? In
theory it should reduce to:
tg->weight * grq->avg.load_avg
ge->load.weight = ------------------------------
grq->avg.load_avg
But I can see many reasons why it won't happen in practice if things
aren't perfectly up-to-date. If tg->load_avg and grq->avg.load_avg in
(3) aren't in sync, or there are stale contributions to tg->load_avg
from other cpus then (3) can return anything between 0 and tg->weight.
> + *
> + * So what we do is modify our approximation (3) to approach (4) in the (near)
> + * UP case, like:
> + *
> + * ge->load.weight =
> + *
> + * tg->weight * grq->load.weight
> + * --------------------------------------------------- (5)
> + * tg->load_avg - grq->avg.load_avg + grq->load.weight
> + *
> + *
> + * And that is shares_weight and is icky. In the (near) UP case it approaches
> + * (4) while in the normal case it approaches (3). It consistently
> + * overestimates the ge->load.weight and therefore:
> + *
> + * \Sum ge->load.weight >= tg->weight
> + *
> + * hence icky!
IIUC, if grq->avg.load_avg > grq->load.weight, i.e. you have blocked
tasks, you can end up with underestimating the ge->load.weight for some
of the group entities lead to \Sum ge->load.weight < tg->weight.
Let's take a simple example:
Two cpus, one task group with three tasks in it: An always-running task
on both cpus, and an additional periodic task currently blocked on cpu 0
(contributing 512 to grq->avg.load_avg on cpu 0).
tg->weight = 1024
tg->load_avg = 2560
\Sum grq->load.weight = 2048
cpu 0 1 \Sum
grq->avg.load_avg 1536 1024
grq->load.weight 1024 1024
ge->load_weight (1) 512 512 1024 >= tg->weight
ge->load_weight (3) 614 410 1024 >= tg->weight
ge->load_weight (5) 512 410 922 < tg->weight
So with (5) we are missing 102 worth of ge->load.weight.
If (1), the instantaneous ge->load.weight, is what we want, then
ge->load.weight of cpu 1 is underestimated, if (3), shares_avg, is the
goal, then ge->load.weight of cpu 0 is underestimated.
The "missing" ge->load.weight can get much larger if the blocked task
had higher priority.
Another thing is that we are loosing a bit of the nice stability that
(3) provides if you have periodic tasks.
I'm not sure if we can do better than (5), I'm just trying to understand
how the approximation will behave and make sure we understand the
implications.
Morten
next prev parent reply other threads:[~2017-09-28 10:03 UTC|newest]
Thread overview: 57+ messages / expand[flat|nested] mbox.gz Atom feed top
2017-09-01 13:20 [PATCH -v2 00/18] sched/fair: A bit of a cgroup/PELT overhaul Peter Zijlstra
2017-09-01 13:21 ` [PATCH -v2 01/18] sched/fair: Clean up calc_cfs_shares() Peter Zijlstra
2017-09-01 13:21 ` [PATCH -v2 02/18] sched/fair: Add comment to calc_cfs_shares() Peter Zijlstra
2017-09-28 10:03 ` Morten Rasmussen [this message]
2017-09-29 11:35 ` Peter Zijlstra
2017-09-29 13:03 ` Morten Rasmussen
2017-09-01 13:21 ` [PATCH -v2 03/18] sched/fair: Cure calc_cfs_shares() vs reweight_entity() Peter Zijlstra
2017-09-29 9:04 ` Morten Rasmussen
2017-09-29 11:38 ` Peter Zijlstra
2017-09-29 13:00 ` Morten Rasmussen
2017-09-01 13:21 ` [PATCH -v2 04/18] sched/fair: Remove se->load.weight from se->avg.load_sum Peter Zijlstra
2017-09-29 15:26 ` Morten Rasmussen
2017-09-29 16:39 ` Peter Zijlstra
2017-09-01 13:21 ` [PATCH -v2 05/18] sched/fair: Change update_load_avg() arguments Peter Zijlstra
2017-09-01 13:21 ` [PATCH -v2 06/18] sched/fair: Move enqueue migrate handling Peter Zijlstra
2017-09-01 13:21 ` [PATCH -v2 07/18] sched/fair: Rename {en,de}queue_entity_load_avg() Peter Zijlstra
2017-09-01 13:21 ` [PATCH -v2 08/18] sched/fair: Introduce {en,de}queue_load_avg() Peter Zijlstra
2017-09-01 13:21 ` [PATCH -v2 09/18] sched/fair: More accurate reweight_entity() Peter Zijlstra
2017-09-01 13:21 ` [PATCH -v2 10/18] sched/fair: Use reweight_entity() for set_user_nice() Peter Zijlstra
2017-09-01 13:21 ` [PATCH -v2 11/18] sched/fair: Rewrite cfs_rq->removed_*avg Peter Zijlstra
2017-09-01 13:21 ` [PATCH -v2 12/18] sched/fair: Rewrite PELT migration propagation Peter Zijlstra
2017-10-09 8:08 ` Morten Rasmussen
2017-10-09 9:45 ` Peter Zijlstra
2017-10-18 12:45 ` Morten Rasmussen
2017-10-30 13:35 ` Peter Zijlstra
2017-10-09 15:03 ` Vincent Guittot
2017-10-09 15:29 ` Vincent Guittot
2017-10-10 7:29 ` Peter Zijlstra
2017-10-10 7:44 ` Vincent Guittot
2017-10-13 15:22 ` Vincent Guittot
2017-10-13 20:41 ` Peter Zijlstra
2017-10-15 12:01 ` Vincent Guittot
2017-10-16 13:55 ` Vincent Guittot
2017-10-19 15:04 ` Vincent Guittot
2017-10-30 17:20 ` Peter Zijlstra
2017-10-31 11:14 ` Vincent Guittot
2017-10-31 15:01 ` Peter Zijlstra
2017-10-31 16:38 ` Vincent Guittot
2017-11-16 14:09 ` [PATCH v3] sched: Update runnable propagation rule Vincent Guittot
2017-11-16 14:21 ` [PATCH v4] " Vincent Guittot
2017-12-06 11:40 ` Peter Zijlstra
2017-12-06 17:10 ` Ingo Molnar
2017-12-06 20:29 ` [tip:sched/core] sched/fair: Update and fix the " tip-bot for Vincent Guittot
2017-09-01 13:21 ` [PATCH -v2 13/18] sched/fair: Propagate an effective runnable_load_avg Peter Zijlstra
2017-10-02 17:46 ` Dietmar Eggemann
2017-10-03 8:50 ` Peter Zijlstra
2017-10-03 9:29 ` Dietmar Eggemann
2017-10-03 12:26 ` Dietmar Eggemann
2017-09-01 13:21 ` [PATCH -v2 14/18] sched/fair: Synchonous PELT detach on load-balance migrate Peter Zijlstra
2017-09-01 13:21 ` [PATCH -v2 15/18] sched/fair: Align PELT windows between cfs_rq and its se Peter Zijlstra
2017-10-04 19:27 ` Dietmar Eggemann
2017-10-06 13:02 ` Peter Zijlstra
2017-10-09 12:15 ` Dietmar Eggemann
2017-10-09 12:19 ` Peter Zijlstra
2017-09-01 13:21 ` [PATCH -v2 16/18] sched/fair: More accurate async detach Peter Zijlstra
2017-09-01 13:21 ` [PATCH -v2 17/18] sched/fair: Calculate runnable_weight slightly differently Peter Zijlstra
2017-09-01 13:21 ` [PATCH -v2 18/18] sched/fair: Update calc_group_*() comments Peter Zijlstra
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20170928100303.GA962@e105550-lin.cambridge.arm.com \
--to=morten.rasmussen@arm.com \
--cc=bsegall@google.com \
--cc=clm@fb.com \
--cc=dietmar.eggemann@arm.com \
--cc=efault@gmx.de \
--cc=josef@toxicpanda.com \
--cc=linux-kernel@vger.kernel.org \
--cc=mingo@kernel.org \
--cc=peterz@infradead.org \
--cc=pjt@google.com \
--cc=tj@kernel.org \
--cc=torvalds@linux-foundation.org \
--cc=vincent.guittot@linaro.org \
--cc=yuyang.du@intel.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox
all inboxes | Powered by JetHome®