Re: [PATCH v7 15/23] PM: EM: Optimize em_cpu_energy() and remove division

From: Hongyan Xia
Date: Wed Feb 07 2024 - 06:40:24 EST


On 17/01/2024 09:57, Lukasz Luba wrote:
The Energy Model (EM) can be modified at runtime which brings new
possibilities. The em_cpu_energy() is called by the Energy Aware Scheduler
(EAS) in its hot path. The energy calculation uses power value for
a given performance state (ps) and the CPU busy time as percentage for that
given frequency.

It is possible to avoid the division by 'scale_cpu' at runtime, because
EM is updated whenever new max capacity CPU is set in the system.

Use that feature and do the needed division during the calculation of the
coefficient 'ps->cost'. That enhanced 'ps->cost' value can be then just
multiplied simply by utilization:

pd_nrg = ps->cost * \Sum cpu_util

to get the needed energy for whole Performance Domain (PD).

With this optimization and earlier removal of map_util_freq(), the
em_cpu_energy() should run faster on the Big CPU by 1.43x and on the Little
CPU by 1.69x (RockPi 4B board).

Signed-off-by: Lukasz Luba <lukasz.luba@xxxxxxx>
---
include/linux/energy_model.h | 54 ++++++++++--------------------------
kernel/power/energy_model.c | 7 ++---
2 files changed, 17 insertions(+), 44 deletions(-)

diff --git a/include/linux/energy_model.h b/include/linux/energy_model.h
index 689d71f6b56f..aabfc26fcd31 100644
--- a/include/linux/energy_model.h
+++ b/include/linux/energy_model.h
[...]
@@ -208,8 +206,9 @@ static int em_compute_costs(struct device *dev, struct em_perf_state *table,
return -EINVAL;
}
} else {
- power_res = table[i].power;
- cost = div64_u64(fmax * power_res, table[i].frequency);
+ /* increase resolution of 'cost' precision */
+ power_res = table[i].power * 10;

NIT: Does this have to be 10, or something simple like << 3 (* 8) also does the job?

Although compiler these days often are clever enough to convert x * 10 into (x << 3) + (x << 1), and this is not on the hot path anyway, so just a NIT.

+ cost = power_res / table[i].performance;
}
table[i].cost = cost;

Reviewed-by: Hongyan Xia <hongyan.xia2@xxxxxxx>