From 3204a5de6be6506d8d6742e662ba3eb3764b1924 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lo=C3=AFc=20Gr=C3=A9goire?= Date: Thu, 20 Aug 2026 20:38:37 -0400 Subject: [PATCH] ke/sched: make soft affinity tunable --- src/kern/sched.zig | 20 +++++++++++++++----- 1 file changed, 15 insertions(+), 5 deletions(-) diff --git a/src/kern/sched.zig b/src/kern/sched.zig index 28f714c..c0d803f 100644 --- a/src/kern/sched.zig +++ b/src/kern/sched.zig @@ -106,6 +106,11 @@ const interactivity_threshold = 30; const scaling_factor = 50; const preempt_threshold = @intFromEnum(ke.Priority.LowRealtime); const balance_interval = @as(u64, config.sched_balance_interval); +const sched_migration_cost = ke.Tunable( + u64, + 500, + "sched_migration_cost_us", +); const steal_threshold = 1; const pct_window_target = 10 * std.time.us_per_s; const pct_window_max = 11 * std.time.us_per_s; @@ -752,8 +757,9 @@ fn insert_in_queue(cpu: *PerCpu, td: *ke.Thread, preempted: bool) void { td.runq_idx = @intCast(prio); } else if (td.priority_class() == .Timeshare) { // Insert in calendar queue. - // The insertion index is determined by insidx and the the priority of the thread, - // higher priority threads will be put closer to insidx, which ensures that they are ran more frequently. + // The insertion index is determined by insidx and the the priority of + // the thread, higher priority threads will be put closer to insidx, + // which ensures that they are ran more frequently in proportion. var idx = (cpu.insidx + (@intFromEnum(ke.Priority.HighBatch) - td.priority)) % runqueues_n; @@ -764,7 +770,11 @@ fn insert_in_queue(cpu: *PerCpu, td: *ke.Thread, preempted: bool) void { cpu.calendar_queue.status |= (@as(u64, 1) << @intCast(idx)); - insert_in_list(&cpu.calendar_queue.queues[idx], &td.runq_link, false); + insert_in_list( + &cpu.calendar_queue.queues[idx], + &td.runq_link, + false, + ); td.runq = &cpu.calendar_queue; td.runq_idx = @intCast(idx); } else { @@ -915,8 +925,8 @@ fn pick_cpu(td: *ke.Thread, slept: u64) u32 { const curcpu = ke.cpu.current(); if (td.last_cpu) |cpu| { - // If we haven't been on this CPU in the last second, don't bother. - const ran_recently = slept <= std.time.us_per_s; + // If we haven't been on this CPU recently, don't bother. + const ran_recently = slept <= sched_migration_cost.load(); const sched_cpu = percpu.remote(cpu); // If we can preempt and have ran recently on this CPU, run on it.