Message ID | 20221207101705.9460-3-lukasz.luba@arm.com |
---|---|
State | New |
Headers |
Return-Path: <linux-kernel-owner@vger.kernel.org> Delivered-To: ouuuleilei@gmail.com Received: by 2002:adf:f944:0:0:0:0:0 with SMTP id q4csp96652wrr; Wed, 7 Dec 2022 02:24:05 -0800 (PST) X-Google-Smtp-Source: AA0mqf4RXRqefzyUZW5EB6XAbUjPxlCp0VPoX4N/+BOYY5CCXcgWYVlZLHFutgvvUpIzEbBcep1K X-Received: by 2002:a63:cd09:0:b0:476:d44d:355 with SMTP id i9-20020a63cd09000000b00476d44d0355mr63656040pgg.289.1670408645568; Wed, 07 Dec 2022 02:24:05 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1670408645; cv=none; d=google.com; s=arc-20160816; b=b4aiEI64uSFvCLWwI67USIuzgd3WrS+7yWM3jAucSMXrsjfm+vaP+7Sr5FDc7znKGl nRPlE+Spi06Twpj6CZ9wNrvIC091790JIKSDBgLGygFVc7UbdJShLJ+P+DSdLzd3Kw25 expfUHJ0acByIQ5auOpZimTDEM5X9tn2CDhfB1gQjjf0eEiBGu3pX6MglW7CRpPvPRa5 xMwXOl3KCdYmwTaLkKIvZKDPB8hoO5Q4doM5StteszDtS2G5iu/U/IlvOYeUWlOwzMic M/7NHUU015YliUrVABlWhg/Jr/zbb8VhuPpKOQ2TdjYzz1FPW7iJ77AEIeLRFTcRytbf UaAQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:references:in-reply-to:message-id:date:subject :cc:to:from; bh=of4EF5HOby1WjanvafN7EK9H8PQAF18boJlQ0vJ7PAI=; b=TU4mVflM4K2T2z7r5uZiJpZ9kZgJl1Dfh6kIyk7Ku+zRPO5AfRCV6MGCyrN8Z+M8KL g51N/mivARoHl99zstzZvzC1JdQ9DSCjCfXRw43SJ6Wu+UDJaxEV9YCw9QplPGLc3iT9 w2MDDzdHFX1RwLK0hzYi6RMoD/EHlK6cRrqGaWny8fSd2Rro/DG5ar92Ymhf4k4BLNW3 PI7vGLeAPRL7BAVCHHdzdE4gG3XZz7ESlWa2WRN/PY5gcLhiSvr7iSr+XXK7zbZeRTGT iA9ePg6RbVqKmY7PrPn/F8j2kKplT2GrbdWI55DyT4YBSP/DeTFtPq9jJ03EoFl+rILX 7Hug== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=arm.com Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id c19-20020a056a000ad300b0056e770672c1si22061618pfl.305.2022.12.07.02.23.51; Wed, 07 Dec 2022 02:24:05 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=arm.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230295AbiLGKRp (ORCPT <rfc822;b08248@gmail.com> + 99 others); Wed, 7 Dec 2022 05:17:45 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:45766 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230383AbiLGKR3 (ORCPT <rfc822;linux-kernel@vger.kernel.org>); Wed, 7 Dec 2022 05:17:29 -0500 Received: from foss.arm.com (foss.arm.com [217.140.110.172]) by lindbergh.monkeyblade.net (Postfix) with ESMTP id A456F419A8; Wed, 7 Dec 2022 02:17:28 -0800 (PST) Received: from usa-sjc-imap-foss1.foss.arm.com (unknown [10.121.207.14]) by usa-sjc-mx-foss1.foss.arm.com (Postfix) with ESMTP id 22D4DED1; Wed, 7 Dec 2022 02:17:35 -0800 (PST) Received: from e123648.arm.com (unknown [10.57.7.234]) by usa-sjc-imap-foss1.foss.arm.com (Postfix) with ESMTPA id 2A6D63F73B; Wed, 7 Dec 2022 02:17:25 -0800 (PST) From: Lukasz Luba <lukasz.luba@arm.com> To: linux-kernel@vger.kernel.org, linux-pm@vger.kernel.org, rafael@kernel.org Cc: lukasz.luba@arm.com, viresh.kumar@linaro.org, dietmar.eggemann@arm.com, vincent.guittot@linaro.org, saravanak@google.com, wusamuel@google.com, isaacmanjarres@google.com, kernel-team@android.com, juri.lelli@redhat.com, peterz@infradead.org, mingo@redhat.com, rostedt@goodmis.org, bsegall@google.com, mgorman@suse.de Subject: [PATCH v2 2/2] cpufreq: schedutil: Optimize operations with single max CPU capacity Date: Wed, 7 Dec 2022 10:17:05 +0000 Message-Id: <20221207101705.9460-3-lukasz.luba@arm.com> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20221207101705.9460-1-lukasz.luba@arm.com> References: <20221207101705.9460-1-lukasz.luba@arm.com> X-Spam-Status: No, score=-4.2 required=5.0 tests=BAYES_00,RCVD_IN_DNSWL_MED, SPF_HELO_NONE,SPF_NONE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: <linux-kernel.vger.kernel.org> X-Mailing-List: linux-kernel@vger.kernel.org X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1751550415805720738?= X-GMAIL-MSGID: =?utf-8?q?1751550415805720738?= |
Series |
cpufreq: schedutil: Optimize operations in hot path frequency switch
|
|
Commit Message
Lukasz Luba
Dec. 7, 2022, 10:17 a.m. UTC
The max CPU capacity is the same for all CPUs sharing frequency domain
and thus 'policy' object. There is a way to avoid heavy operations
in a loop for each CPU by leveraging this knowledge. Thus, simplify
the looping code in the sugov_next_freq_shared() and drop heavy
multiplications. Instead, use simple max() to get the highest utilization
from these CPUs. This is useful for platforms with many (4 or 6) little
CPUs.
The max CPU capacity must be fetched every time we are called, due to
difficulties during the policy setup, where we are not able to get the
normalized CPU capacity at the right time.
The stored value in sugov_policy::max is also than used in
sugov_iowait_apply() to calculate the right boost. Thus, that field is
useful to have in that sugov_policy struct.
Signed-off-by: Lukasz Luba <lukasz.luba@arm.com>
---
kernel/sched/cpufreq_schedutil.c | 22 +++++++++++-----------
1 file changed, 11 insertions(+), 11 deletions(-)
Comments
On 07-12-22, 10:17, Lukasz Luba wrote: > The max CPU capacity is the same for all CPUs sharing frequency domain > and thus 'policy' object. There is a way to avoid heavy operations > in a loop for each CPU by leveraging this knowledge. Thus, simplify > the looping code in the sugov_next_freq_shared() and drop heavy > multiplications. Instead, use simple max() to get the highest utilization > from these CPUs. This is useful for platforms with many (4 or 6) little > CPUs. > > The max CPU capacity must be fetched every time we are called, due to > difficulties during the policy setup, where we are not able to get the > normalized CPU capacity at the right time. > > The stored value in sugov_policy::max is also than used in > sugov_iowait_apply() to calculate the right boost. Thus, that field is > useful to have in that sugov_policy struct. > > Signed-off-by: Lukasz Luba <lukasz.luba@arm.com> > --- > kernel/sched/cpufreq_schedutil.c | 22 +++++++++++----------- > 1 file changed, 11 insertions(+), 11 deletions(-) Looks okay to me. Acked-by: Viresh Kumar <viresh.kumar@linaro.org>
On Wed, 7 Dec 2022 at 11:17, Lukasz Luba <lukasz.luba@arm.com> wrote: > > The max CPU capacity is the same for all CPUs sharing frequency domain > and thus 'policy' object. There is a way to avoid heavy operations > in a loop for each CPU by leveraging this knowledge. Thus, simplify > the looping code in the sugov_next_freq_shared() and drop heavy > multiplications. Instead, use simple max() to get the highest utilization > from these CPUs. This is useful for platforms with many (4 or 6) little > CPUs. > > The max CPU capacity must be fetched every time we are called, due to > difficulties during the policy setup, where we are not able to get the > normalized CPU capacity at the right time. > > The stored value in sugov_policy::max is also than used in > sugov_iowait_apply() to calculate the right boost. Thus, that field is > useful to have in that sugov_policy struct. > > Signed-off-by: Lukasz Luba <lukasz.luba@arm.com> > --- > kernel/sched/cpufreq_schedutil.c | 22 +++++++++++----------- > 1 file changed, 11 insertions(+), 11 deletions(-) > > diff --git a/kernel/sched/cpufreq_schedutil.c b/kernel/sched/cpufreq_schedutil.c > index c19d6de67b7a..f9881f3d9488 100644 > --- a/kernel/sched/cpufreq_schedutil.c > +++ b/kernel/sched/cpufreq_schedutil.c > @@ -158,10 +158,8 @@ static unsigned int get_next_freq(struct sugov_policy *sg_policy, > > static void sugov_get_util(struct sugov_cpu *sg_cpu) > { > - struct sugov_policy *sg_policy = sg_cpu->sg_policy; > struct rq *rq = cpu_rq(sg_cpu->cpu); > > - sg_policy->max = arch_scale_cpu_capacity(sg_cpu->cpu); > sg_cpu->bw_dl = cpu_bw_dl(rq); > sg_cpu->util = effective_cpu_util(sg_cpu->cpu, cpu_util_cfs(sg_cpu->cpu), > FREQUENCY_UTIL, NULL); > @@ -317,6 +315,8 @@ static inline void ignore_dl_rate_limit(struct sugov_cpu *sg_cpu) > static inline bool sugov_update_single_common(struct sugov_cpu *sg_cpu, > u64 time, unsigned int flags) > { > + struct sugov_policy *sg_policy = sg_cpu->sg_policy; > + > sugov_iowait_boost(sg_cpu, time, flags); > sg_cpu->last_update = time; > > @@ -325,6 +325,9 @@ static inline bool sugov_update_single_common(struct sugov_cpu *sg_cpu, > if (!sugov_should_update_freq(sg_cpu->sg_policy, time)) > return false; > > + /* Fetch the latest CPU capcity to avoid stale data */ > + sg_policy->max = arch_scale_cpu_capacity(sg_cpu->cpu); > + > sugov_get_util(sg_cpu); > sugov_iowait_apply(sg_cpu, time); > > @@ -414,25 +417,22 @@ static unsigned int sugov_next_freq_shared(struct sugov_cpu *sg_cpu, u64 time) > { > struct sugov_policy *sg_policy = sg_cpu->sg_policy; > struct cpufreq_policy *policy = sg_policy->policy; > - unsigned long util = 0, max = 1; > + unsigned long util = 0; > unsigned int j; > > + /* Fetch the latest CPU capcity to avoid stale data */ > + sg_policy->max = arch_scale_cpu_capacity(sg_cpu->cpu); > + > for_each_cpu(j, policy->cpus) { > struct sugov_cpu *j_sg_cpu = &per_cpu(sugov_cpu, j); > - unsigned long j_util, j_max; > > sugov_get_util(j_sg_cpu); > sugov_iowait_apply(j_sg_cpu, time); > - j_util = j_sg_cpu->util; > - j_max = j_sg_cpu->max; > > - if (j_util * max > j_max * util) { > - util = j_util; > - max = j_max; > - } With the code removed above, max is only used in 2 places: - sugov_iowait_apply - map_util_freq I wonder if it would be better to just call arch_scale_cpu_capacity() in these 2 places instead of saving a copy in sg_policy and then reading it twice. arch_scaleu_cpu_capacity is already a per_cpu variable so accessing it should be pretty cheap. Thought ? > + util = max(j_sg_cpu->util, util); > } > > - return get_next_freq(sg_policy, util, max); > + return get_next_freq(sg_policy, util, sg_policy->max); > } > > static void > -- > 2.17.1 >
On 12/8/22 04:09, Viresh Kumar wrote: > On 07-12-22, 10:17, Lukasz Luba wrote: >> The max CPU capacity is the same for all CPUs sharing frequency domain >> and thus 'policy' object. There is a way to avoid heavy operations >> in a loop for each CPU by leveraging this knowledge. Thus, simplify >> the looping code in the sugov_next_freq_shared() and drop heavy >> multiplications. Instead, use simple max() to get the highest utilization >> from these CPUs. This is useful for platforms with many (4 or 6) little >> CPUs. >> >> The max CPU capacity must be fetched every time we are called, due to >> difficulties during the policy setup, where we are not able to get the >> normalized CPU capacity at the right time. >> >> The stored value in sugov_policy::max is also than used in >> sugov_iowait_apply() to calculate the right boost. Thus, that field is >> useful to have in that sugov_policy struct. >> >> Signed-off-by: Lukasz Luba <lukasz.luba@arm.com> >> --- >> kernel/sched/cpufreq_schedutil.c | 22 +++++++++++----------- >> 1 file changed, 11 insertions(+), 11 deletions(-) > > Looks okay to me. > > Acked-by: Viresh Kumar <viresh.kumar@linaro.org> > Thank you Viresh! As Rafael said, it will wait after 6.2-rc1 is out. Regards, Lukasz
On 12/8/22 08:37, Vincent Guittot wrote: > On Wed, 7 Dec 2022 at 11:17, Lukasz Luba <lukasz.luba@arm.com> wrote: >> >> The max CPU capacity is the same for all CPUs sharing frequency domain >> and thus 'policy' object. There is a way to avoid heavy operations >> in a loop for each CPU by leveraging this knowledge. Thus, simplify >> the looping code in the sugov_next_freq_shared() and drop heavy >> multiplications. Instead, use simple max() to get the highest utilization >> from these CPUs. This is useful for platforms with many (4 or 6) little >> CPUs. >> >> The max CPU capacity must be fetched every time we are called, due to >> difficulties during the policy setup, where we are not able to get the >> normalized CPU capacity at the right time. >> >> The stored value in sugov_policy::max is also than used in >> sugov_iowait_apply() to calculate the right boost. Thus, that field is >> useful to have in that sugov_policy struct. >> >> Signed-off-by: Lukasz Luba <lukasz.luba@arm.com> >> --- >> kernel/sched/cpufreq_schedutil.c | 22 +++++++++++----------- >> 1 file changed, 11 insertions(+), 11 deletions(-) >> >> diff --git a/kernel/sched/cpufreq_schedutil.c b/kernel/sched/cpufreq_schedutil.c >> index c19d6de67b7a..f9881f3d9488 100644 >> --- a/kernel/sched/cpufreq_schedutil.c >> +++ b/kernel/sched/cpufreq_schedutil.c >> @@ -158,10 +158,8 @@ static unsigned int get_next_freq(struct sugov_policy *sg_policy, >> >> static void sugov_get_util(struct sugov_cpu *sg_cpu) >> { >> - struct sugov_policy *sg_policy = sg_cpu->sg_policy; >> struct rq *rq = cpu_rq(sg_cpu->cpu); >> >> - sg_policy->max = arch_scale_cpu_capacity(sg_cpu->cpu); >> sg_cpu->bw_dl = cpu_bw_dl(rq); >> sg_cpu->util = effective_cpu_util(sg_cpu->cpu, cpu_util_cfs(sg_cpu->cpu), >> FREQUENCY_UTIL, NULL); >> @@ -317,6 +315,8 @@ static inline void ignore_dl_rate_limit(struct sugov_cpu *sg_cpu) >> static inline bool sugov_update_single_common(struct sugov_cpu *sg_cpu, >> u64 time, unsigned int flags) >> { >> + struct sugov_policy *sg_policy = sg_cpu->sg_policy; >> + >> sugov_iowait_boost(sg_cpu, time, flags); >> sg_cpu->last_update = time; >> >> @@ -325,6 +325,9 @@ static inline bool sugov_update_single_common(struct sugov_cpu *sg_cpu, >> if (!sugov_should_update_freq(sg_cpu->sg_policy, time)) >> return false; >> >> + /* Fetch the latest CPU capcity to avoid stale data */ >> + sg_policy->max = arch_scale_cpu_capacity(sg_cpu->cpu); >> + >> sugov_get_util(sg_cpu); >> sugov_iowait_apply(sg_cpu, time); >> >> @@ -414,25 +417,22 @@ static unsigned int sugov_next_freq_shared(struct sugov_cpu *sg_cpu, u64 time) >> { >> struct sugov_policy *sg_policy = sg_cpu->sg_policy; >> struct cpufreq_policy *policy = sg_policy->policy; >> - unsigned long util = 0, max = 1; >> + unsigned long util = 0; >> unsigned int j; >> >> + /* Fetch the latest CPU capcity to avoid stale data */ >> + sg_policy->max = arch_scale_cpu_capacity(sg_cpu->cpu); >> + >> for_each_cpu(j, policy->cpus) { >> struct sugov_cpu *j_sg_cpu = &per_cpu(sugov_cpu, j); >> - unsigned long j_util, j_max; >> >> sugov_get_util(j_sg_cpu); >> sugov_iowait_apply(j_sg_cpu, time); >> - j_util = j_sg_cpu->util; >> - j_max = j_sg_cpu->max; >> >> - if (j_util * max > j_max * util) { >> - util = j_util; >> - max = j_max; >> - } > > With the code removed above, max is only used in 2 places: > - sugov_iowait_apply > - map_util_freq > > I wonder if it would be better to just call arch_scale_cpu_capacity() > in these 2 places instead of saving a copy in sg_policy and then > reading it twice. The sugov_iowait_apply() is called in that loop, so probably I will add a new argument to that call and just feed it with the capacity value from one CPU, which was read before the loop. So, similarly what is in this patch. Otherwise, all of those per-cpu capacity vars would be accessed inside the sugov_iowait_apply() with sg_cpu->cpu. > > arch_scaleu_cpu_capacity is already a per_cpu variable so accessing it > should be pretty cheap. Yes and no, as you said this is per-cpu variable and would access them from one CPU, which is running that loop. They will have different pages and addresses so cache lines on that CPU. to avoiding trashing a cache lines on this running CPU let's read that capacity once, before the loop. Let's use the new arg to pass that value via one of the registers. In such, only one cache line would have to fetch that data into. So I thought this simple sg_policy->max would do the trick w/o a lot of hassle. > > Thought ? > I can change that and drop the sg_policy->max and call differently those capacity values. I will have to unfortunately drop Viresh's ACKs, since this will be a way different code. Thanks Vincent for the suggestion. Do you want me to go further with such approach and send a v3?
On Thu, 8 Dec 2022 at 11:06, Lukasz Luba <lukasz.luba@arm.com> wrote: > > > > On 12/8/22 08:37, Vincent Guittot wrote: > > On Wed, 7 Dec 2022 at 11:17, Lukasz Luba <lukasz.luba@arm.com> wrote: > >> > >> The max CPU capacity is the same for all CPUs sharing frequency domain > >> and thus 'policy' object. There is a way to avoid heavy operations > >> in a loop for each CPU by leveraging this knowledge. Thus, simplify > >> the looping code in the sugov_next_freq_shared() and drop heavy > >> multiplications. Instead, use simple max() to get the highest utilization > >> from these CPUs. This is useful for platforms with many (4 or 6) little > >> CPUs. > >> > >> The max CPU capacity must be fetched every time we are called, due to > >> difficulties during the policy setup, where we are not able to get the > >> normalized CPU capacity at the right time. > >> > >> The stored value in sugov_policy::max is also than used in > >> sugov_iowait_apply() to calculate the right boost. Thus, that field is > >> useful to have in that sugov_policy struct. > >> > >> Signed-off-by: Lukasz Luba <lukasz.luba@arm.com> > >> --- > >> kernel/sched/cpufreq_schedutil.c | 22 +++++++++++----------- > >> 1 file changed, 11 insertions(+), 11 deletions(-) > >> > >> diff --git a/kernel/sched/cpufreq_schedutil.c b/kernel/sched/cpufreq_schedutil.c > >> index c19d6de67b7a..f9881f3d9488 100644 > >> --- a/kernel/sched/cpufreq_schedutil.c > >> +++ b/kernel/sched/cpufreq_schedutil.c > >> @@ -158,10 +158,8 @@ static unsigned int get_next_freq(struct sugov_policy *sg_policy, > >> > >> static void sugov_get_util(struct sugov_cpu *sg_cpu) > >> { > >> - struct sugov_policy *sg_policy = sg_cpu->sg_policy; > >> struct rq *rq = cpu_rq(sg_cpu->cpu); > >> > >> - sg_policy->max = arch_scale_cpu_capacity(sg_cpu->cpu); > >> sg_cpu->bw_dl = cpu_bw_dl(rq); > >> sg_cpu->util = effective_cpu_util(sg_cpu->cpu, cpu_util_cfs(sg_cpu->cpu), > >> FREQUENCY_UTIL, NULL); > >> @@ -317,6 +315,8 @@ static inline void ignore_dl_rate_limit(struct sugov_cpu *sg_cpu) > >> static inline bool sugov_update_single_common(struct sugov_cpu *sg_cpu, > >> u64 time, unsigned int flags) > >> { > >> + struct sugov_policy *sg_policy = sg_cpu->sg_policy; > >> + > >> sugov_iowait_boost(sg_cpu, time, flags); > >> sg_cpu->last_update = time; > >> > >> @@ -325,6 +325,9 @@ static inline bool sugov_update_single_common(struct sugov_cpu *sg_cpu, > >> if (!sugov_should_update_freq(sg_cpu->sg_policy, time)) > >> return false; > >> > >> + /* Fetch the latest CPU capcity to avoid stale data */ > >> + sg_policy->max = arch_scale_cpu_capacity(sg_cpu->cpu); > >> + > >> sugov_get_util(sg_cpu); > >> sugov_iowait_apply(sg_cpu, time); > >> > >> @@ -414,25 +417,22 @@ static unsigned int sugov_next_freq_shared(struct sugov_cpu *sg_cpu, u64 time) > >> { > >> struct sugov_policy *sg_policy = sg_cpu->sg_policy; > >> struct cpufreq_policy *policy = sg_policy->policy; > >> - unsigned long util = 0, max = 1; > >> + unsigned long util = 0; > >> unsigned int j; > >> > >> + /* Fetch the latest CPU capcity to avoid stale data */ > >> + sg_policy->max = arch_scale_cpu_capacity(sg_cpu->cpu); > >> + > >> for_each_cpu(j, policy->cpus) { > >> struct sugov_cpu *j_sg_cpu = &per_cpu(sugov_cpu, j); > >> - unsigned long j_util, j_max; > >> > >> sugov_get_util(j_sg_cpu); > >> sugov_iowait_apply(j_sg_cpu, time); > >> - j_util = j_sg_cpu->util; > >> - j_max = j_sg_cpu->max; > >> > >> - if (j_util * max > j_max * util) { > >> - util = j_util; > >> - max = j_max; > >> - } > > > > With the code removed above, max is only used in 2 places: > > - sugov_iowait_apply > > - map_util_freq > > > > I wonder if it would be better to just call arch_scale_cpu_capacity() > > in these 2 places instead of saving a copy in sg_policy and then > > reading it twice. > > The sugov_iowait_apply() is called in that loop, so probably I will > add a new argument to that call and just feed it with the capacity value > from one CPU, which was read before the loop. So, similarly what is in > this patch. Otherwise, all of those per-cpu capacity vars would be > accessed inside the sugov_iowait_apply() with sg_cpu->cpu. Yes make sense > > > > > arch_scaleu_cpu_capacity is already a per_cpu variable so accessing it > > should be pretty cheap. > > Yes and no, as you said this is per-cpu variable and would access them > from one CPU, which is running that loop. They will have different pages > and addresses so cache lines on that CPU. to avoiding trashing a cache > lines on this running CPU let's read that capacity once, before the > loop. Let's use the new arg to pass that value via one of the > registers. In such, only one cache line would have to fetch that data > into. > > So I thought this simple sg_policy->max would do the trick w/o a lot > of hassle. For the shared mode, everything is located in sugov_next_freq_shared so you don't need to save the max value with your proposal above to change sugov_iowait_apply interface. This should be doable as well for single mode > > > > Thought ? > > > > I can change that and drop the sg_policy->max and call differently > those capacity values. I will have to unfortunately drop Viresh's ACKs, > since this will be a way different code. > > Thanks Vincent for the suggestion. Do you want me to go further with > such approach and send a v3? Don't know what Rafael and Viresh think but it seems that we don't need to save the return of arch_scale_cpu_capacity in ->max field but directly use it
On 12/8/22 10:31, Vincent Guittot wrote: > On Thu, 8 Dec 2022 at 11:06, Lukasz Luba <lukasz.luba@arm.com> wrote: >> >> >> >> On 12/8/22 08:37, Vincent Guittot wrote: >>> On Wed, 7 Dec 2022 at 11:17, Lukasz Luba <lukasz.luba@arm.com> wrote: >>>> >>>> The max CPU capacity is the same for all CPUs sharing frequency domain >>>> and thus 'policy' object. There is a way to avoid heavy operations >>>> in a loop for each CPU by leveraging this knowledge. Thus, simplify >>>> the looping code in the sugov_next_freq_shared() and drop heavy >>>> multiplications. Instead, use simple max() to get the highest utilization >>>> from these CPUs. This is useful for platforms with many (4 or 6) little >>>> CPUs. >>>> >>>> The max CPU capacity must be fetched every time we are called, due to >>>> difficulties during the policy setup, where we are not able to get the >>>> normalized CPU capacity at the right time. >>>> >>>> The stored value in sugov_policy::max is also than used in >>>> sugov_iowait_apply() to calculate the right boost. Thus, that field is >>>> useful to have in that sugov_policy struct. >>>> >>>> Signed-off-by: Lukasz Luba <lukasz.luba@arm.com> >>>> --- >>>> kernel/sched/cpufreq_schedutil.c | 22 +++++++++++----------- >>>> 1 file changed, 11 insertions(+), 11 deletions(-) >>>> >>>> diff --git a/kernel/sched/cpufreq_schedutil.c b/kernel/sched/cpufreq_schedutil.c >>>> index c19d6de67b7a..f9881f3d9488 100644 >>>> --- a/kernel/sched/cpufreq_schedutil.c >>>> +++ b/kernel/sched/cpufreq_schedutil.c >>>> @@ -158,10 +158,8 @@ static unsigned int get_next_freq(struct sugov_policy *sg_policy, >>>> >>>> static void sugov_get_util(struct sugov_cpu *sg_cpu) >>>> { >>>> - struct sugov_policy *sg_policy = sg_cpu->sg_policy; >>>> struct rq *rq = cpu_rq(sg_cpu->cpu); >>>> >>>> - sg_policy->max = arch_scale_cpu_capacity(sg_cpu->cpu); >>>> sg_cpu->bw_dl = cpu_bw_dl(rq); >>>> sg_cpu->util = effective_cpu_util(sg_cpu->cpu, cpu_util_cfs(sg_cpu->cpu), >>>> FREQUENCY_UTIL, NULL); >>>> @@ -317,6 +315,8 @@ static inline void ignore_dl_rate_limit(struct sugov_cpu *sg_cpu) >>>> static inline bool sugov_update_single_common(struct sugov_cpu *sg_cpu, >>>> u64 time, unsigned int flags) >>>> { >>>> + struct sugov_policy *sg_policy = sg_cpu->sg_policy; >>>> + >>>> sugov_iowait_boost(sg_cpu, time, flags); >>>> sg_cpu->last_update = time; >>>> >>>> @@ -325,6 +325,9 @@ static inline bool sugov_update_single_common(struct sugov_cpu *sg_cpu, >>>> if (!sugov_should_update_freq(sg_cpu->sg_policy, time)) >>>> return false; >>>> >>>> + /* Fetch the latest CPU capcity to avoid stale data */ >>>> + sg_policy->max = arch_scale_cpu_capacity(sg_cpu->cpu); >>>> + >>>> sugov_get_util(sg_cpu); >>>> sugov_iowait_apply(sg_cpu, time); >>>> >>>> @@ -414,25 +417,22 @@ static unsigned int sugov_next_freq_shared(struct sugov_cpu *sg_cpu, u64 time) >>>> { >>>> struct sugov_policy *sg_policy = sg_cpu->sg_policy; >>>> struct cpufreq_policy *policy = sg_policy->policy; >>>> - unsigned long util = 0, max = 1; >>>> + unsigned long util = 0; >>>> unsigned int j; >>>> >>>> + /* Fetch the latest CPU capcity to avoid stale data */ >>>> + sg_policy->max = arch_scale_cpu_capacity(sg_cpu->cpu); >>>> + >>>> for_each_cpu(j, policy->cpus) { >>>> struct sugov_cpu *j_sg_cpu = &per_cpu(sugov_cpu, j); >>>> - unsigned long j_util, j_max; >>>> >>>> sugov_get_util(j_sg_cpu); >>>> sugov_iowait_apply(j_sg_cpu, time); >>>> - j_util = j_sg_cpu->util; >>>> - j_max = j_sg_cpu->max; >>>> >>>> - if (j_util * max > j_max * util) { >>>> - util = j_util; >>>> - max = j_max; >>>> - } >>> >>> With the code removed above, max is only used in 2 places: >>> - sugov_iowait_apply >>> - map_util_freq >>> >>> I wonder if it would be better to just call arch_scale_cpu_capacity() >>> in these 2 places instead of saving a copy in sg_policy and then >>> reading it twice. >> >> The sugov_iowait_apply() is called in that loop, so probably I will >> add a new argument to that call and just feed it with the capacity value >> from one CPU, which was read before the loop. So, similarly what is in >> this patch. Otherwise, all of those per-cpu capacity vars would be >> accessed inside the sugov_iowait_apply() with sg_cpu->cpu. > > Yes make sense > >> >>> >>> arch_scaleu_cpu_capacity is already a per_cpu variable so accessing it >>> should be pretty cheap. >> >> Yes and no, as you said this is per-cpu variable and would access them >> from one CPU, which is running that loop. They will have different pages >> and addresses so cache lines on that CPU. to avoiding trashing a cache >> lines on this running CPU let's read that capacity once, before the >> loop. Let's use the new arg to pass that value via one of the >> registers. In such, only one cache line would have to fetch that data >> into. >> >> So I thought this simple sg_policy->max would do the trick w/o a lot >> of hassle. > > For the shared mode, everything is located in sugov_next_freq_shared > so you don't need to save the max value with your proposal above to > change sugov_iowait_apply interface. > > This should be doable as well for single mode > >>> >>> Thought ? >>> >> >> I can change that and drop the sg_policy->max and call differently >> those capacity values. I will have to unfortunately drop Viresh's ACKs, >> since this will be a way different code. >> >> Thanks Vincent for the suggestion. Do you want me to go further with >> such approach and send a v3? > > Don't know what Rafael and Viresh think but it seems that we don't > need to save the return of arch_scale_cpu_capacity in ->max field but > directly use it Yes I agree, we don't need to, but I will have to modify a few function calls and args. So IMO we have agreed. I won't call the call arch_scale_cpu_capacity() in these 2 places, but I will make it with the local var and data fetched as little as possible.
On Thu, 8 Dec 2022 at 11:56, Lukasz Luba <lukasz.luba@arm.com> wrote: > > > > On 12/8/22 10:31, Vincent Guittot wrote: > > On Thu, 8 Dec 2022 at 11:06, Lukasz Luba <lukasz.luba@arm.com> wrote: > >> > >> > >> > >> On 12/8/22 08:37, Vincent Guittot wrote: > >>> On Wed, 7 Dec 2022 at 11:17, Lukasz Luba <lukasz.luba@arm.com> wrote: > >>>> > >>>> The max CPU capacity is the same for all CPUs sharing frequency domain > >>>> and thus 'policy' object. There is a way to avoid heavy operations > >>>> in a loop for each CPU by leveraging this knowledge. Thus, simplify > >>>> the looping code in the sugov_next_freq_shared() and drop heavy > >>>> multiplications. Instead, use simple max() to get the highest utilization > >>>> from these CPUs. This is useful for platforms with many (4 or 6) little > >>>> CPUs. > >>>> > >>>> The max CPU capacity must be fetched every time we are called, due to > >>>> difficulties during the policy setup, where we are not able to get the > >>>> normalized CPU capacity at the right time. > >>>> > >>>> The stored value in sugov_policy::max is also than used in > >>>> sugov_iowait_apply() to calculate the right boost. Thus, that field is > >>>> useful to have in that sugov_policy struct. > >>>> > >>>> Signed-off-by: Lukasz Luba <lukasz.luba@arm.com> > >>>> --- > >>>> kernel/sched/cpufreq_schedutil.c | 22 +++++++++++----------- > >>>> 1 file changed, 11 insertions(+), 11 deletions(-) > >>>> > >>>> diff --git a/kernel/sched/cpufreq_schedutil.c b/kernel/sched/cpufreq_schedutil.c > >>>> index c19d6de67b7a..f9881f3d9488 100644 > >>>> --- a/kernel/sched/cpufreq_schedutil.c > >>>> +++ b/kernel/sched/cpufreq_schedutil.c > >>>> @@ -158,10 +158,8 @@ static unsigned int get_next_freq(struct sugov_policy *sg_policy, > >>>> > >>>> static void sugov_get_util(struct sugov_cpu *sg_cpu) > >>>> { > >>>> - struct sugov_policy *sg_policy = sg_cpu->sg_policy; > >>>> struct rq *rq = cpu_rq(sg_cpu->cpu); > >>>> > >>>> - sg_policy->max = arch_scale_cpu_capacity(sg_cpu->cpu); > >>>> sg_cpu->bw_dl = cpu_bw_dl(rq); > >>>> sg_cpu->util = effective_cpu_util(sg_cpu->cpu, cpu_util_cfs(sg_cpu->cpu), > >>>> FREQUENCY_UTIL, NULL); > >>>> @@ -317,6 +315,8 @@ static inline void ignore_dl_rate_limit(struct sugov_cpu *sg_cpu) > >>>> static inline bool sugov_update_single_common(struct sugov_cpu *sg_cpu, > >>>> u64 time, unsigned int flags) > >>>> { > >>>> + struct sugov_policy *sg_policy = sg_cpu->sg_policy; > >>>> + > >>>> sugov_iowait_boost(sg_cpu, time, flags); > >>>> sg_cpu->last_update = time; > >>>> > >>>> @@ -325,6 +325,9 @@ static inline bool sugov_update_single_common(struct sugov_cpu *sg_cpu, > >>>> if (!sugov_should_update_freq(sg_cpu->sg_policy, time)) > >>>> return false; > >>>> > >>>> + /* Fetch the latest CPU capcity to avoid stale data */ > >>>> + sg_policy->max = arch_scale_cpu_capacity(sg_cpu->cpu); > >>>> + > >>>> sugov_get_util(sg_cpu); > >>>> sugov_iowait_apply(sg_cpu, time); > >>>> > >>>> @@ -414,25 +417,22 @@ static unsigned int sugov_next_freq_shared(struct sugov_cpu *sg_cpu, u64 time) > >>>> { > >>>> struct sugov_policy *sg_policy = sg_cpu->sg_policy; > >>>> struct cpufreq_policy *policy = sg_policy->policy; > >>>> - unsigned long util = 0, max = 1; > >>>> + unsigned long util = 0; > >>>> unsigned int j; > >>>> > >>>> + /* Fetch the latest CPU capcity to avoid stale data */ > >>>> + sg_policy->max = arch_scale_cpu_capacity(sg_cpu->cpu); > >>>> + > >>>> for_each_cpu(j, policy->cpus) { > >>>> struct sugov_cpu *j_sg_cpu = &per_cpu(sugov_cpu, j); > >>>> - unsigned long j_util, j_max; > >>>> > >>>> sugov_get_util(j_sg_cpu); > >>>> sugov_iowait_apply(j_sg_cpu, time); > >>>> - j_util = j_sg_cpu->util; > >>>> - j_max = j_sg_cpu->max; > >>>> > >>>> - if (j_util * max > j_max * util) { > >>>> - util = j_util; > >>>> - max = j_max; > >>>> - } > >>> > >>> With the code removed above, max is only used in 2 places: > >>> - sugov_iowait_apply > >>> - map_util_freq > >>> > >>> I wonder if it would be better to just call arch_scale_cpu_capacity() > >>> in these 2 places instead of saving a copy in sg_policy and then > >>> reading it twice. > >> > >> The sugov_iowait_apply() is called in that loop, so probably I will > >> add a new argument to that call and just feed it with the capacity value > >> from one CPU, which was read before the loop. So, similarly what is in > >> this patch. Otherwise, all of those per-cpu capacity vars would be > >> accessed inside the sugov_iowait_apply() with sg_cpu->cpu. > > > > Yes make sense > > > >> > >>> > >>> arch_scaleu_cpu_capacity is already a per_cpu variable so accessing it > >>> should be pretty cheap. > >> > >> Yes and no, as you said this is per-cpu variable and would access them > >> from one CPU, which is running that loop. They will have different pages > >> and addresses so cache lines on that CPU. to avoiding trashing a cache > >> lines on this running CPU let's read that capacity once, before the > >> loop. Let's use the new arg to pass that value via one of the > >> registers. In such, only one cache line would have to fetch that data > >> into. > >> > >> So I thought this simple sg_policy->max would do the trick w/o a lot > >> of hassle. > > > > For the shared mode, everything is located in sugov_next_freq_shared > > so you don't need to save the max value with your proposal above to > > change sugov_iowait_apply interface. > > > > This should be doable as well for single mode > > > >>> > >>> Thought ? > >>> > >> > >> I can change that and drop the sg_policy->max and call differently > >> those capacity values. I will have to unfortunately drop Viresh's ACKs, > >> since this will be a way different code. > >> > >> Thanks Vincent for the suggestion. Do you want me to go further with > >> such approach and send a v3? > > > > Don't know what Rafael and Viresh think but it seems that we don't > > need to save the return of arch_scale_cpu_capacity in ->max field but > > directly use it > > Yes I agree, we don't need to, but I will have to modify a few function > calls and args. > > So IMO we have agreed. I won't call the call arch_scale_cpu_capacity() > in these 2 places, but I will make it with the local var and data > fetched as little as possible. yes
diff --git a/kernel/sched/cpufreq_schedutil.c b/kernel/sched/cpufreq_schedutil.c index c19d6de67b7a..f9881f3d9488 100644 --- a/kernel/sched/cpufreq_schedutil.c +++ b/kernel/sched/cpufreq_schedutil.c @@ -158,10 +158,8 @@ static unsigned int get_next_freq(struct sugov_policy *sg_policy, static void sugov_get_util(struct sugov_cpu *sg_cpu) { - struct sugov_policy *sg_policy = sg_cpu->sg_policy; struct rq *rq = cpu_rq(sg_cpu->cpu); - sg_policy->max = arch_scale_cpu_capacity(sg_cpu->cpu); sg_cpu->bw_dl = cpu_bw_dl(rq); sg_cpu->util = effective_cpu_util(sg_cpu->cpu, cpu_util_cfs(sg_cpu->cpu), FREQUENCY_UTIL, NULL); @@ -317,6 +315,8 @@ static inline void ignore_dl_rate_limit(struct sugov_cpu *sg_cpu) static inline bool sugov_update_single_common(struct sugov_cpu *sg_cpu, u64 time, unsigned int flags) { + struct sugov_policy *sg_policy = sg_cpu->sg_policy; + sugov_iowait_boost(sg_cpu, time, flags); sg_cpu->last_update = time; @@ -325,6 +325,9 @@ static inline bool sugov_update_single_common(struct sugov_cpu *sg_cpu, if (!sugov_should_update_freq(sg_cpu->sg_policy, time)) return false; + /* Fetch the latest CPU capcity to avoid stale data */ + sg_policy->max = arch_scale_cpu_capacity(sg_cpu->cpu); + sugov_get_util(sg_cpu); sugov_iowait_apply(sg_cpu, time); @@ -414,25 +417,22 @@ static unsigned int sugov_next_freq_shared(struct sugov_cpu *sg_cpu, u64 time) { struct sugov_policy *sg_policy = sg_cpu->sg_policy; struct cpufreq_policy *policy = sg_policy->policy; - unsigned long util = 0, max = 1; + unsigned long util = 0; unsigned int j; + /* Fetch the latest CPU capcity to avoid stale data */ + sg_policy->max = arch_scale_cpu_capacity(sg_cpu->cpu); + for_each_cpu(j, policy->cpus) { struct sugov_cpu *j_sg_cpu = &per_cpu(sugov_cpu, j); - unsigned long j_util, j_max; sugov_get_util(j_sg_cpu); sugov_iowait_apply(j_sg_cpu, time); - j_util = j_sg_cpu->util; - j_max = j_sg_cpu->max; - if (j_util * max > j_max * util) { - util = j_util; - max = j_max; - } + util = max(j_sg_cpu->util, util); } - return get_next_freq(sg_policy, util, max); + return get_next_freq(sg_policy, util, sg_policy->max); } static void