Received: by 2002:a05:6a10:206:0:0:0:0 with SMTP id 6csp598203pxj; Wed, 2 Jun 2021 07:00:12 -0700 (PDT) X-Google-Smtp-Source: ABdhPJz4OuS/VV88/oWu4wVEjAf3Xm76uaBveL8gJa2tgSFFbnqWNczQ/w4iMh8X6jbc2hPuJW0U X-Received: by 2002:a05:6402:48f:: with SMTP id k15mr38063427edv.262.1622642412652; Wed, 02 Jun 2021 07:00:12 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1622642412; cv=none; d=google.com; s=arc-20160816; b=pHQZZpirG/oMGly3YKSb3bd5W6DKNWiPlgTf2Iz5sSpRIfUHk7K7FFZH9Nt0wEnjw7 iAK55MEuSGcDCOVE5jFgqDmwsI/mD7jQ7Edqrw52KfgLWB3UEgB9BamcEAaEcNpsxADW jWH3UFqXMFAIazghqnmIR1L3JiVZw5VWaf/rO9F9yJTd3xa03EMAK5MDf3lGQr2+4wMN qqHrtXyI/eZRMy1lb6qGOSPvo3AnF/eMxgox2kmiwZ80ujxjPnZL82jUkMlQrgePIYtG nH5ejDy9NJpnNQEgXgIqBL0PnRWDGEYWy37x+if982bLKa5PYEQayZXoMPEURTqqZSnx bdWQ== 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=4LPXpavdnBTrGrccaQyhY0JZTn+aSIfkeEObRLEQlcg=; b=C72nBk6AdcJaTwkGvVirsyBewhSje4MEnHoDhssDi2iUWZkw3NSo/JU9SOdVss5x0y ghQXquFQPJaF6cRft3S/9yORnzplx8m5Adf53S/+/X4B6Od/RuW3bR2cnumW5AZ46hEl DWCnQTFobUhJg4TB85oG8gdDEWmDOZuN9AkBq+wty3ZJXas/NbUWDnxEBtilcITLAHtX QJgQr3j9O4UFZJ/rAqUBfhHJ7GIL89JZ3f3kXsdrj+L/GzJA68rrIuBzQYJFIpGxGfrS DJb94K9E1KWPGjDtf83rXGfBV1t/UpUolVYe4e/AByehJ+M3AJ9YxzzaXDQVTrLCJNFd 9KUQ== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=arm.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id ga2si18962828ejb.249.2021.06.02.06.59.46; Wed, 02 Jun 2021 07:00:12 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 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 S230092AbhFBN63 (ORCPT + 99 others); Wed, 2 Jun 2021 09:58:29 -0400 Received: from foss.arm.com ([217.140.110.172]:45398 "EHLO foss.arm.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229654AbhFBN61 (ORCPT ); Wed, 2 Jun 2021 09:58:27 -0400 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 6B79512FC; Wed, 2 Jun 2021 06:56:44 -0700 (PDT) Received: from e123648.arm.com (unknown [10.57.1.174]) by usa-sjc-imap-foss1.foss.arm.com (Postfix) with ESMTPA id CD9A93F73D; Wed, 2 Jun 2021 06:56:40 -0700 (PDT) From: Lukasz Luba To: linux-kernel@vger.kernel.org Cc: linux-pm@vger.kernel.org, peterz@infradead.org, rjw@rjwysocki.net, viresh.kumar@linaro.org, vincent.guittot@linaro.org, qperret@google.com, dietmar.eggemann@arm.com, vincent.donnefort@arm.com, lukasz.luba@arm.com, mingo@redhat.com, juri.lelli@redhat.com, rostedt@goodmis.org, segall@google.com, mgorman@suse.de, bristot@redhat.com Subject: [PATCH 2/2] sched/cpufreq: Consider reduced CPU capacity in energy calculation Date: Wed, 2 Jun 2021 14:56:09 +0100 Message-Id: <20210602135609.10867-3-lukasz.luba@arm.com> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20210602135609.10867-1-lukasz.luba@arm.com> References: <20210602135609.10867-1-lukasz.luba@arm.com> Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Energy Aware Scheduling (EAS) needs to predict the decisions made by SchedUtil. The map_util_freq() exists to do that. There are corner cases where the max allowed frequency might be reduced (due to thermal). SchedUtil as a CPUFreq governor, is aware of that but EAS is not. This patch aims to address it. SchedUtil stores the maximum allowed frequency in 'sugov_policy::next_freq' field. EAS has to predict that value, which is the real used frequency. That value is made after a call to cpufreq_driver_resolve_freq() which clamps to the CPUFreq policy limits. In the existing code EAS is not able to predict that real frequency. This leads to energy estimation errors. To avoid wrong energy estimation in EAS (due to frequency miss prediction) make sure that the step which calculates Performance Domain frequency, is also aware of the allowed CPU capacity. Furthermore, modify map_util_freq() to not extend the frequency value. Instead, use map_util_perf() to extend the util value in both places: SchedUtil and EAS, but for EAS clamp it to max allowed CPU capacity. In the end, we achieve the same desirable behavior for both subsystems and alignment in regards to the real CPU frequency. Signed-off-by: Lukasz Luba --- include/linux/energy_model.h | 16 +++++++++++++--- include/linux/sched/cpufreq.h | 2 +- kernel/sched/cpufreq_schedutil.c | 1 + kernel/sched/fair.c | 2 +- 4 files changed, 16 insertions(+), 5 deletions(-) diff --git a/include/linux/energy_model.h b/include/linux/energy_model.h index 757fc60658fa..3f221dbf5f95 100644 --- a/include/linux/energy_model.h +++ b/include/linux/energy_model.h @@ -91,6 +91,8 @@ void em_dev_unregister_perf_domain(struct device *dev); * @pd : performance domain for which energy has to be estimated * @max_util : highest utilization among CPUs of the domain * @sum_util : sum of the utilization of all CPUs in the domain + * @allowed_cpu_cap : maximum allowed CPU capacity for the @pd, which + might reflect reduced frequency (due to thermal) * * This function must be used only for CPU devices. There is no validation, * i.e. if the EM is a CPU type and has cpumask allocated. It is called from @@ -100,7 +102,8 @@ void em_dev_unregister_perf_domain(struct device *dev); * a capacity state satisfying the max utilization of the domain. */ static inline unsigned long em_cpu_energy(struct em_perf_domain *pd, - unsigned long max_util, unsigned long sum_util) + unsigned long max_util, unsigned long sum_util, + unsigned long allowed_cpu_cap) { unsigned long freq, scale_cpu; struct em_perf_state *ps; @@ -112,11 +115,17 @@ static inline unsigned long em_cpu_energy(struct em_perf_domain *pd, /* * In order to predict the performance state, map the utilization of * the most utilized CPU of the performance domain to a requested - * frequency, like schedutil. + * frequency, like schedutil. Take also into account that the real + * frequency might be set lower (due to thermal capping). Thus, clamp + * max utilization to the allowed CPU capacity before calculating + * effective frequency. */ cpu = cpumask_first(to_cpumask(pd->cpus)); scale_cpu = arch_scale_cpu_capacity(cpu); ps = &pd->table[pd->nr_perf_states - 1]; + + max_util = map_util_perf(max_util); + max_util = min(max_util, allowed_cpu_cap); freq = map_util_freq(max_util, ps->frequency, scale_cpu); /* @@ -209,7 +218,8 @@ static inline struct em_perf_domain *em_pd_get(struct device *dev) return NULL; } static inline unsigned long em_cpu_energy(struct em_perf_domain *pd, - unsigned long max_util, unsigned long sum_util) + unsigned long max_util, unsigned long sum_util, + unsigned long allowed_cpu_cap) { return 0; } diff --git a/include/linux/sched/cpufreq.h b/include/linux/sched/cpufreq.h index 6205578ab6ee..bdd31ab93bc5 100644 --- a/include/linux/sched/cpufreq.h +++ b/include/linux/sched/cpufreq.h @@ -26,7 +26,7 @@ bool cpufreq_this_cpu_can_update(struct cpufreq_policy *policy); static inline unsigned long map_util_freq(unsigned long util, unsigned long freq, unsigned long cap) { - return (freq + (freq >> 2)) * util / cap; + return freq * util / cap; } static inline unsigned long map_util_perf(unsigned long util) diff --git a/kernel/sched/cpufreq_schedutil.c b/kernel/sched/cpufreq_schedutil.c index 4f09afd2f321..57124614363d 100644 --- a/kernel/sched/cpufreq_schedutil.c +++ b/kernel/sched/cpufreq_schedutil.c @@ -151,6 +151,7 @@ static unsigned int get_next_freq(struct sugov_policy *sg_policy, unsigned int freq = arch_scale_freq_invariant() ? policy->cpuinfo.max_freq : policy->cur; + util = map_util_perf(util); freq = map_util_freq(util, freq, max); if (freq == sg_policy->cached_raw_freq && !sg_policy->need_freq_update) diff --git a/kernel/sched/fair.c b/kernel/sched/fair.c index ca0a6f1408da..46064e18c287 100644 --- a/kernel/sched/fair.c +++ b/kernel/sched/fair.c @@ -6588,7 +6588,7 @@ compute_energy(struct task_struct *p, int dst_cpu, struct perf_domain *pd) max_util = max(max_util, cpu_util); } - return em_cpu_energy(pd->em_pd, max_util, sum_util); + return em_cpu_energy(pd->em_pd, max_util, sum_util, cpu_cap); } /* -- 2.17.1