[PATCH v5 22/24] virt/steal_monitor: Act on steal values at regular intervals

From: Shrikanth Hegde

Date: Thu Jun 25 2026 - 08:51:59 EST


This is the steal_monitor core functionality done in periodic work

- Calculate the steal_ratio. It is multiplied by 100 to consider the
fractional values of steal time. I.e 10 means 0.1% steal time.
- If steal value is higher than high threshold, call the method to reduce
the preferred CPUs.
- If steal value is lower or equal to low threshold, call the method to
increase the preferred CPUs.
- If the steal value is in between, no action is taken.
- Save the values for next delta calculations.

Signed-off-by: Shrikanth Hegde <sshegde@xxxxxxxxxxxxx>
---
v4->v5:
- Modified for steal_monitor

drivers/virt/steal_monitor/sm_core.c | 29 ++++++++++++++++++++++++++++
1 file changed, 29 insertions(+)

diff --git a/drivers/virt/steal_monitor/sm_core.c b/drivers/virt/steal_monitor/sm_core.c
index fac8f4d5dac7..641488a5a3b5 100644
--- a/drivers/virt/steal_monitor/sm_core.c
+++ b/drivers/virt/steal_monitor/sm_core.c
@@ -34,6 +34,33 @@ MODULE_PARM_DESC(low_threshold,

static void compute_preferred_cpus_work(struct work_struct *work)
{
+ u64 curr_steal, delta_steal, delta_ns, steal_ratio;
+ ktime_t now;
+
+ curr_steal = get_system_steal_time();
+ now = ktime_get();
+
+ /* get the deltas */
+ delta_steal = curr_steal > sm_core_ctx.prev_steal ?
+ curr_steal - sm_core_ctx.prev_steal : 0;
+ delta_ns = max_t(u64, ktime_to_ns(ktime_sub(now, sm_core_ctx.prev_time)), 1);
+
+ /* Update for next calculation */
+ sm_core_ctx.prev_steal = curr_steal;
+ sm_core_ctx.prev_time = now;
+
+ /* Multiply by 100 to consider the fractional values of steal time */
+ steal_ratio = (delta_steal * 100 * 100) /
+ (delta_ns * get_num_cpus_steal_ratio());
+
+ /* If the steal time values are high, reduce preferred CPUs */
+ if (steal_ratio > sm_core_ctx.high_threshold)
+ decrease_preferred_cpus(&sm_core_ctx);
+
+ /* If the steal time values are low, increase preferred CPUs */
+ if (steal_ratio <= sm_core_ctx.low_threshold)
+ increase_preferred_cpus(&sm_core_ctx);
+
/* At least one core is kept as preferred */
WARN_ON(cpumask_empty(cpu_preferred_mask));

@@ -54,6 +81,8 @@ static int __init steal_monitor_init(void)
sm_core_ctx.interval_ms, sm_core_ctx.high_threshold, sm_core_ctx.low_threshold);

INIT_DELAYED_WORK(&sm_core_ctx.work, compute_preferred_cpus_work);
+ sm_core_ctx.prev_steal = get_system_steal_time();
+ sm_core_ctx.prev_time = ktime_get();

schedule_delayed_work(&sm_core_ctx.work,
msecs_to_jiffies(sm_core_ctx.interval_ms));
--
2.47.3