mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
* [PATCH 0/2]  Fix and optimize some comments in the CPCC EM model
@ 2025-04-09  9:49 Yaxiong Tian
  2025-04-09  9:51 ` [PATCH 1/2] cpufreq: cppc: Update and opt comment for cost calculation method Yaxiong Tian
  2025-04-09  9:51 ` [PATCH 2/2] cpufreq: cppc: Add comments for CPPC_EM_COST_GAP calculation Yaxiong Tian
  0 siblings, 2 replies; 3+ messages in thread
From: Yaxiong Tian @ 2025-04-09  9:49 UTC (permalink / raw)
  To: rafael, viresh.kumar, Pierre.Gondois; +Cc: linux-pm, linux-kernel, Yaxiong Tian

From: Yaxiong Tian <tianyaxiong@kylinos.cn>

Commit <740fcdc2c20ec> ("cpufreq: CPPC: Register EM based on efficiency 
class information") introduced a very good feature. However, since commit
<1b600da510735a> ("PM: EM: Optimize em_cpu_energy() and remove division"), 
the energy calculation formula in the EM model has changed. Therefore, some
comments need to be updated and corrected to avoid misunderstandings.

I carefully analyzed the code theoretically and found that the new energy 
calculation formula doesn't affect the current code - in fact, it has brought 
some improvements. For example, consider four efficiency classes (1, 2, 3, 4). 
Under the old logic, there were cases where tasks would be scheduled to class 4 
CPUs instead of class 3 CPUs. This happened because the old energy calculation 
formula divided by CPU capacity, and since class 3 CPUs have lower capacity than 
class 4, it sometimes went against the original intention.

Considering that the comments about CPPC_EM_COST_GAP might be confusing, I've 
added some analytical comments before CPPC_EM_COST_GAP in the latest code to 
help other developers better understand. 

Of course, this is just my personal understanding - please point out if 
there are any mistakes.

Yaxiong Tian (2):
  cpufreq: cppc: Update and opt comment for cost calculation method
  cpufreq: cppc: Add comments for CPPC_EM_COST_GAP calculation

 drivers/cpufreq/cppc_cpufreq.c | 44 ++++++++++++++++++++++++++++++----
 1 file changed, 40 insertions(+), 4 deletions(-)

-- 
2.25.1


^ permalink raw reply	[flat|nested] 3+ messages in thread

* [PATCH 1/2] cpufreq: cppc: Update and opt comment for cost calculation method
  2025-04-09  9:49 [PATCH 0/2] Fix and optimize some comments in the CPCC EM model Yaxiong Tian
@ 2025-04-09  9:51 ` Yaxiong Tian
  2025-04-09  9:51 ` [PATCH 2/2] cpufreq: cppc: Add comments for CPPC_EM_COST_GAP calculation Yaxiong Tian
  1 sibling, 0 replies; 3+ messages in thread
From: Yaxiong Tian @ 2025-04-09  9:51 UTC (permalink / raw)
  To: rafael, viresh.kumar, Pierre.Gondois; +Cc: linux-pm, linux-kernel, Yaxiong Tian

From: Yaxiong Tian <tianyaxiong@kylinos.cn>

commit <1b600da510735a> ("PM: EM: Optimize em_cpu_energy() and remove division")
updated the energy calculation method, so the original comment needs to be updated
and opt.

Signed-off-by: Yaxiong Tian  <tianyaxiong@kylinos.cn>
---
 drivers/cpufreq/cppc_cpufreq.c | 15 ++++++++++++---
 1 file changed, 12 insertions(+), 3 deletions(-)

diff --git a/drivers/cpufreq/cppc_cpufreq.c b/drivers/cpufreq/cppc_cpufreq.c
index b3d74f9adcf0..756034b21682 100644
--- a/drivers/cpufreq/cppc_cpufreq.c
+++ b/drivers/cpufreq/cppc_cpufreq.c
@@ -379,9 +379,18 @@ static unsigned int get_perf_level_count(struct cpufreq_policy *policy)
 	return 1 + max_cap / CPPC_EM_CAP_STEP - min_cap / CPPC_EM_CAP_STEP;
 }
 
-/*
- * The cost is defined as:
- *   cost = power * max_frequency / frequency
+/**
+ * compute_cost - Calculate the cost for a given CPU and performance level step
+ * @cpu:  Target CPU
+ * @step: Performance level step
+ *
+ * CPPC uses a linear cost model since it only provides relative efficiency ratios:
+ *   Base cost (per CPU):    CPPC_EM_COST_GAP * efficiency_class
+ *   Step cost (per level):  step * CPPC_EM_COST_STEP
+ *
+ * Lower cost implies higher efficiency. The model ensures:
+ *   1. Higher efficiency CPUs (low efficiency_class) always have lower cost.
+ *   2. Higher performance levels (larger step) linearly increase cost.
  */
 static inline unsigned long compute_cost(int cpu, int step)
 {
-- 
2.25.1


^ permalink raw reply	[flat|nested] 3+ messages in thread

* [PATCH 2/2] cpufreq: cppc: Add comments for CPPC_EM_COST_GAP calculation
  2025-04-09  9:49 [PATCH 0/2] Fix and optimize some comments in the CPCC EM model Yaxiong Tian
  2025-04-09  9:51 ` [PATCH 1/2] cpufreq: cppc: Update and opt comment for cost calculation method Yaxiong Tian
@ 2025-04-09  9:51 ` Yaxiong Tian
  1 sibling, 0 replies; 3+ messages in thread
From: Yaxiong Tian @ 2025-04-09  9:51 UTC (permalink / raw)
  To: rafael, viresh.kumar, Pierre.Gondois; +Cc: linux-pm, linux-kernel, Yaxiong Tian

From: Yaxiong Tian <tianyaxiong@kylinos.cn>

The existing comments about CPPC_EM_COST_GAP were too brief. To help other
developers better understand its design rationale, this commit adds detailed
explanations about it.

Signed-off-by: Yaxiong Tian <tianyaxiong@kylinos.cn>
---
 drivers/cpufreq/cppc_cpufreq.c | 29 ++++++++++++++++++++++++++++-
 1 file changed, 28 insertions(+), 1 deletion(-)

diff --git a/drivers/cpufreq/cppc_cpufreq.c b/drivers/cpufreq/cppc_cpufreq.c
index 756034b21682..c8e281ca95c0 100644
--- a/drivers/cpufreq/cppc_cpufreq.c
+++ b/drivers/cpufreq/cppc_cpufreq.c
@@ -358,7 +358,34 @@ static void cppc_cpufreq_register_em(struct cpufreq_policy *policy);
 #define CPPC_EM_CAP_STEP	(20)
 /* Increase the cost value by CPPC_EM_COST_STEP every performance state. */
 #define CPPC_EM_COST_STEP	(1)
-/* Add a cost gap correspnding to the energy of 4 CPUs. */
+
+/*
+ * In the current EM (Energy Model), energy consumption is calculated as:
+ *   ps->cost * sum_util;
+ *
+ * For CPPC, the cost calculation uses a linear model: cost = a + b * step.
+ *
+ * For a task A with utilization 'util' in a performance domain (pd):
+ * Before placement:
+ *   energy_pd_before = a * sum_util_pd + b * step * sum_util_pd;
+ * After placement:
+ *   energy_pd_after = a * (sum_util_pd + util) + b * (sum_util_pd + util) *
+ *                    (step + util/CPPC_EM_CAP_STEP);
+ * Energy difference:
+ *   energy_diff = a * util + b * util * step_pd +
+ *                 b * (sum_util_pd + util) * util/cap_step
+ *
+ * The placement decision depends on the bias 'a' and slope 'b'. Considering the
+ * extreme case where little cores are nearly full and big cores are nearly idle:
+ *   energy_diff_pd = a_diff * util - b * util * step_pd_small -
+ *                 b * util * sum_util_pd_small/cap_step
+ *
+ * Ignoring smaller terms, the key condition becomes:
+ *   a_diff > b * sum_util_pd_small / step_cap
+ *
+ * Therefore, for a pd domain with 4 CPUs, for ensures the scheduler will prefer
+ * placing it.we can configure accordingly.
+ */
 #define CPPC_EM_COST_GAP	(4 * SCHED_CAPACITY_SCALE * CPPC_EM_COST_STEP \
 				/ CPPC_EM_CAP_STEP)
 
-- 
2.25.1


^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2025-04-09 10:05 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2025-04-09  9:49 [PATCH 0/2] Fix and optimize some comments in the CPCC EM model Yaxiong Tian
2025-04-09  9:51 ` [PATCH 1/2] cpufreq: cppc: Update and opt comment for cost calculation method Yaxiong Tian
2025-04-09  9:51 ` [PATCH 2/2] cpufreq: cppc: Add comments for CPPC_EM_COST_GAP calculation Yaxiong Tian

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®