Files
scx-upstream/scheds/rust/scx_cake/bench/pool_regressions.c
RitzDaCat 426a1b98d0 scx_cake: retire pool counts, rank cores at start, bound deep queues
Pool count. A task counted into the wake pool could stop being runnable
without being served: sched_ext bypass during a freezer cycle dequeues
it and can refill its slice, which wiped the pool tag kept in the
slice's low bits. Both pool counters then stayed above zero with no
work left and idle CPUs kept kicking themselves: 72 self-kicks in eight
quiet seconds in a four-CPU 7.3.5 guest. Keep the pool number in the
task's groove and give it back in ops.quiescent on a dequeue that is not
a sleep. Six freezer cycles then drained both counters to zero with no
self-kick. The groove now serves every host, so ops.init_task creates it
on every host.

Deep queues. A search of a queue 64 or more tasks deep looks at eight
tasks and moves one the CPU can run. When the same head has waited
2.5 s and still cannot be served, cake exits and names the queue, CPU
and PID, and the kernel takes the work back.

Placement. The preferred LLC and the core ranks are read once at start:
capacity scaled by policy and firmware limits, then preferred-core
hints. The once-a-second re-read of the X3D mode is gone, so restart
cake after changing it. The loader samples the topology again when a
hotplug lands during discovery.

Homes. A task whose affinity no longer allows its fixed home gives it up
at its next sample. A wake whose home is held by a higher class claims
an idle CPU it may use, the home's LLC first. ops.cpu_release kicks an
idle CPU that can run the head of the released CPU's queue.

Slice grant. The grant's low five bits are cleared for the pool tag and
the compute threshold is even, so the +1 on the doubled quotient never
changed a grant. Drop it: cake_admit_direct goes from 121 to 108
instructions.

--handoff-ns takes 32 to 750000 ns.

ShadowGate on a 9800X3D, 7.3.5, 720p Low, four quiet launches a build.
This work without the storage fix under it against the parent commit:
601.96 and 593.81 fps, frame p99 1.866 and 1.879 ms, input p99 1.734 and
1.749 ms, three of four pairs ahead. This commit against the same work
without the fix, in a second run: 604.35 and 603.25 fps.
2026-11-20 12:54:51 -06:00

161 lines
4.6 KiB
C

/* Build with pool_regressions.py, which includes the current Cake callbacks. */
#include <assert.h>
#include <stdbool.h>
#include <stdio.h>
#include "intf.h"
#define CAKE_RMW_FULL(op, ptr, value) __atomic_fetch_##op(ptr, value, __ATOMIC_SEQ_CST)
#define cake_stat_inc(site) ((void)(site))
#define __noinline __attribute__((noinline))
#define SCX_DEQ_SLEEP 1
#define SCX_DSQ_GLOBAL (1ULL << 63)
struct cake_groove { u16 pool_tag; };
struct task_struct {
struct { u64 slice; } scx;
struct cake_groove storage;
u64 verdict_slice;
bool allocation_failure;
};
struct cake_llc_slot { u64 pending, unserved; };
static struct { struct cake_llc_slot pool[MAX_LLCS]; } cake;
static u32 depth[MAX_LLCS], kicks[MAX_LLCS];
static int cake_grooves, storage_reads;
static struct cake_groove *cake_groove_of(struct task_struct *p)
{
return p->allocation_failure ? NULL : &p->storage;
}
static struct cake_groove *bpf_task_storage_get(int *map, struct task_struct *p,
void *value, u64 flags)
{
assert(map == &cake_grooves && !value && !flags);
storage_reads++;
return p->allocation_failure ? NULL : &p->storage;
}
/* Each modeled CPU belongs to its own LLC. */
static u32 cake_llc_of(s32 cpu) { return (u32)cpu; }
static u64 cake_pool_dsq(u32 llc) { return LLC_WAKE_DSQ_BASE + llc; }
static void cake_wake_mark_set(u32 llc) { (void)llc; }
static u64 fallback_flags;
static bool cake_dsq_insert(struct task_struct *p, u64 dsq, u64 slice, u64 flags)
{
assert(dsq == SCX_DSQ_GLOBAL && !p->verdict_slice);
p->verdict_slice = slice;
fallback_flags = flags;
return true;
}
static bool cake_dsq_insert_vtime(struct task_struct *p, u64 dsq, u64 slice,
u64 vtime, u64 flags)
{
assert(dsq >= LLC_WAKE_DSQ_BASE && !p->verdict_slice);
(void)vtime;
(void)flags;
p->verdict_slice = slice;
return true;
}
static void land(struct task_struct *p, u32 llc)
{
assert(p->verdict_slice);
p->scx.slice = p->verdict_slice;
p->verdict_slice = 0;
depth[llc]++;
}
static bool cake_move_to_local(u64 dsq, s32 cpu, s32 count)
{
u32 llc = dsq - LLC_WAKE_DSQ_BASE;
assert(cpu == (s32)llc && count == (s32)depth[llc]);
if (!depth[llc])
return false;
depth[llc]--;
return true;
}
static void scx_bpf_kick_cpu(s32 cpu, u64 flags)
{
assert(flags == 0);
kicks[cake_llc_of(cpu)]++;
}
#include "callbacks.h"
static void test_repool(void)
{
struct task_struct task = {};
cake_pool_insert(&task, 0, SLICE_NS, 0, 0);
land(&task, 0);
depth[0]--;
task.scx.slice = SLICE_NS;
cake_pool_seen(&task);
cake_pool_insert(&task, 1, SLICE_NS, 0, 0);
assert(!cake.pool[0].pending && cake.pool[1].pending == 1);
cake_idle_interlock(0);
assert(!cake.pool[0].unserved && !kicks[0]);
land(&task, 1);
assert(cake_pool_serve(cake_pool_dsq(1), 1, 1, depth[1]));
cake_pool_seen(&task);
assert(!cake.pool[1].pending && !cake.pool[1].unserved);
}
int main(void)
{
struct task_struct canceled = {}, overwritten = {}, real = {}, unpooled = {};
struct task_struct unsupported = { .allocation_failure = true };
cake_pool_seen(&unpooled);
cake_quiescent(&unpooled, SCX_DEQ_SLEEP);
assert(!storage_reads);
/* An insert verdict precedes kernel publication. Preserve this idle wake. */
cake_pool_insert(&real, 1, SLICE_NS, 0, 0);
cake_idle_interlock(1);
assert(!depth[1] && kicks[1] == 1);
cake_pool_insert(&canceled, 0, SLICE_NS, 0, 0);
land(&canceled, 0);
cake_pool_insert(&overwritten, 0, SLICE_NS, 0, 0);
land(&overwritten, 0);
cake_quiescent(&canceled, 0);
depth[0]--;
canceled.scx.slice = SLICE_NS;
cake_quiescent(&canceled, 0);
assert(cake.pool[0].pending == 1);
overwritten.scx.slice = SLICE_NS + 2;
cake_quiescent(&overwritten, 0);
depth[0]--;
assert(overwritten.scx.slice == SLICE_NS + 2);
for (int i = 0; i < 100; i++)
cake_idle_interlock(0);
assert(!kicks[0] && !cake.pool[0].pending && !cake.pool[0].unserved);
assert(cake.pool[1].pending == 1 && cake.pool[1].unserved == 1);
/* Kernel slice residue can look exactly like the other pool's token. */
unpooled.scx.slice = SLICE_NS + 2;
cake_pool_seen(&unpooled);
cake_quiescent(&unpooled, 0);
assert(cake.pool[1].pending == 1);
land(&real, 1);
assert(cake_pool_serve(cake_pool_dsq(1), 1, 1, depth[1]));
cake_pool_seen(&real);
cake_quiescent(&real, 0);
cake_idle_interlock(1);
assert(!depth[1] && kicks[1] == 1);
assert(!cake.pool[1].pending && !cake.pool[1].unserved);
/* A store allocation failure still grants the task a dispatchable queue. */
cake_pool_insert(&unsupported, 0, SLICE_NS, 0, 42);
assert(unsupported.verdict_slice == SLICE_NS && fallback_flags == 42);
assert(!cake.pool[0].pending && !cake.pool[0].unserved);
test_repool();
puts("Pool cancellation, deferred publication, slice residue, requeue and allocation fallback passed.");
}