feat(scheduler-sim-live-allocation): make capacity checks core-safe (task 1.3)

Re-types sched_meet_host_selection(), sched_check_shared_satisfy()
(+its _ helper), sched_check_host_satisfy() (+its _ helper), and the
private unsatisfy_() helper they share, from xce_scheduler_t* to
xce_sched_core_t*, in resource.c.

Fixes every call site this forces (a C signature migration cannot
keep the build green otherwise): sched_try_alloc_on_host_() (2 sites,
already partially touched by task 1.2), host_iter.c's
sched_host_iter_next() (2 sites), and parjob_allocator.c/
seqjob_allocator.c's my_alloc() (1 site each) -- each fix is a minimal
&(sched->core) argument wrap, no algorithm change, preserving
host_iter/parjob/seqjob's own allocation algorithm exactly as-is.

Adds test_core_safe_allocation_chain_driver_parity, proving all three
functions produce identical grant/deny results whether given
normal-mode-shaped or simulation-mode-shaped core state.

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01RLSNJhNoULvVcUhXEFafQC
This commit is contained in:
Rongsong Shen 2026-09-18 12:42:11 +00:00
parent e8866641a6
commit 55173fca27
No known key found for this signature in database
GPG key ID: 5BCDAAE0123E9FE2
13 changed files with 345 additions and 53 deletions

View file

@ -12,6 +12,12 @@
* This also brings in scheduler.h (with the real xce_scheduler_t) and all deps. */
#include "../src/resource.c"
/* scheduler-sim-live-allocation spec, task 1.3: sim.h only depends on
* core.h (+ stdbool/stdint/stdio, per its own header comment), so it is
* safe to pull in here just for xce_sched_sim_driver_t -- needed to build
* a simulation-mode-shaped container for the driver-parity test below. */
#include "sim.h"
/* Stubs for symbols used by resource.c functions not under test.
* sched_get_worker/sched_get_queue relocated to core.h (scheduler-core
* spec, task 4.1) -- now operate on xce_sched_core_t*, not
@ -167,7 +173,7 @@ test_meet_host_selection_null_select_passes(void)
memset(&w, 0, sizeof(w));
zarray_init(&w.resvals, 4, NULL);
int rc = sched_meet_host_selection(&sched, &w, NULL);
int rc = sched_meet_host_selection(&(sched.core), &w, NULL);
TEST_ASSERT_EQUAL_INT(1, rc);
zarray_fini(&w.resvals);
@ -190,7 +196,7 @@ test_meet_host_selection_always_true_passes(void)
TEST_ASSERT_NOT_NULL(root);
xce_select_t sel = { .root = root };
int rc = sched_meet_host_selection(&sched, &w, &sel);
int rc = sched_meet_host_selection(&(sched.core), &w, &sel);
TEST_ASSERT_EQUAL_INT(1, rc);
sched_worker_release_host_ctx(&w);
@ -218,7 +224,7 @@ test_meet_host_selection_always_false_fails(void)
TEST_ASSERT_NOT_NULL(root);
xce_select_t sel = { .root = root };
int rc = sched_meet_host_selection(&sched, &w, &sel);
int rc = sched_meet_host_selection(&(sched.core), &w, &sel);
TEST_ASSERT_EQUAL_INT(0, rc);
sched_worker_release_host_ctx(&w);
@ -323,7 +329,7 @@ test_meet_host_selection_numeric_match(void)
v8.u.fvalue = 8.0f;
zarray_put(&w8.resvals, 0, &v8);
TEST_ASSERT_EQUAL_INT(1, sched_meet_host_selection(&sched, &w8, &sel));
TEST_ASSERT_EQUAL_INT(1, sched_meet_host_selection(&(sched.core), &w8, &sel));
/* Worker with 2.0 at res_idx=0 — must fail */
xce_sched_worker_t w2;
@ -336,7 +342,7 @@ test_meet_host_selection_numeric_match(void)
v2.u.fvalue = 2.0f;
zarray_put(&w2.resvals, 0, &v2);
TEST_ASSERT_EQUAL_INT(0, sched_meet_host_selection(&sched, &w2, &sel));
TEST_ASSERT_EQUAL_INT(0, sched_meet_host_selection(&(sched.core), &w2, &sel));
sched_worker_release_host_ctx(&w8);
sched_worker_release_host_ctx(&w2);
@ -382,7 +388,7 @@ test_meet_host_selection_string_match(void)
vgpu.u.svalue = "gpu";
zarray_put(&wgpu.resvals, 1, &vgpu);
TEST_ASSERT_EQUAL_INT(1, sched_meet_host_selection(&sched, &wgpu, &sel));
TEST_ASSERT_EQUAL_INT(1, sched_meet_host_selection(&(sched.core), &wgpu, &sel));
/* Worker with "cpu" at res_idx=1 — must fail */
xce_sched_worker_t wcpu;
@ -395,7 +401,7 @@ test_meet_host_selection_string_match(void)
vcpu.u.svalue = "cpu";
zarray_put(&wcpu.resvals, 1, &vcpu);
TEST_ASSERT_EQUAL_INT(0, sched_meet_host_selection(&sched, &wcpu, &sel));
TEST_ASSERT_EQUAL_INT(0, sched_meet_host_selection(&(sched.core), &wcpu, &sel));
sched_worker_release_host_ctx(&wgpu);
sched_worker_release_host_ctx(&wcpu);
@ -477,6 +483,248 @@ test_dead_pushmsg_symbols_absent(void)
TEST_PASS();
}
/* ── scheduler-sim-live-allocation task 1.3: Core-Safe Allocation Chain ── */
/*
* sched_meet_host_selection()/sched_check_shared_satisfy()/
* sched_check_host_satisfy() are re-typed to take xce_sched_core_t* core
* directly instead of a normal-mode-only xce_scheduler_t* sched, so they
* must be callable identically from either driver's core state --
* xce_scheduler_t (this file's own `sched` variables above) and
* xce_sched_sim_driver_t (sim.h) both embed xce_sched_core_t core as their
* first member (scheduler.h / sim.h). This test drives IDENTICAL inputs
* through all three functions against &(normal.core) and &(sim.core) and
* asserts identical results -- proving driver-agnosticism, not merely a
* retyped signature.
*
* RED (before this task): fails to compile -- the three functions take
* xce_scheduler_t*, so passing xce_sched_core_t* directly is an
* incompatible-pointer-type error (this toolchain treats
* -Wincompatible-pointer-types as fatal, not a warning).
*
* No sched_sim_driver_run() call here -- this fixture never spawns a
* thread against xce_sched_sim_driver_t, only reads/writes its embedded
* `core` synchronously within this function's own lifetime, so bug-1598's
* stack-use-after-return hazard (a stack-local sim driver outliving a
* never-joined thread) does not apply.
*/
static void init_test_resmgr(xce_sched_core_t* core)
{
kvmgr_params_t resmgr_params = { .key_type = KVMGR_KEY_STRING,
.nslots = 8,
.delta = 4,
.datakiller = NULL };
core->resmgr = makeInstance(&xkvmgr_type, &resmgr_params);
TEST_ASSERT_NOT_NULL(core->resmgr);
}
static void
test_core_safe_allocation_chain_driver_parity(void)
{
xce_scheduler_t sched_normal;
xce_sched_sim_driver_t sim_shape;
xce_sched_core_t* core_normal = &(sched_normal.core);
xce_sched_core_t* core_sim = &(sim_shape.core);
memset(&sched_normal, 0, sizeof(sched_normal));
memset(&sim_shape, 0, sizeof(sim_shape));
init_test_resmgr(core_normal);
init_test_resmgr(core_sim);
/* --- sched_meet_host_selection(): NULL select always passes -------- */
{
xce_sched_worker_t w;
int rc_normal, rc_sim;
memset(&w, 0, sizeof(w));
zarray_init(&w.resvals, 4, NULL);
rc_normal = sched_meet_host_selection(core_normal, &w, NULL);
rc_sim = sched_meet_host_selection(core_sim, &w, NULL);
TEST_ASSERT_EQUAL_INT(1, rc_normal);
TEST_ASSERT_EQUAL_INT_MESSAGE(
rc_normal, rc_sim,
"sched_meet_host_selection() must return identically for a "
"normal-mode-shaped and a simulation-mode-shaped core");
zarray_fini(&w.resvals);
}
/* --- sched_check_shared_satisfy(): identical grant/deny ------------- */
{
xvec_t uncomitted_n, uncomitted_s;
xvec_idval_t wanted_n, wanted_s, unmeet_n, unmeet_s;
xce_val_t total, inuse;
int rc_normal, rc_sim;
zarray_init(&core_normal->sharedInst, 4, NULL);
zarray_init(&core_normal->sharedInUse, 4, NULL);
zarray_init(&core_sim->sharedInst, 4, NULL);
zarray_init(&core_sim->sharedInUse, 4, NULL);
memset(&total, 0, sizeof(total));
total.which = Value_u_fvalue;
total.u.fvalue = 10.0f;
memset(&inuse, 0, sizeof(inuse));
inuse.which = Value_u_fvalue;
inuse.u.fvalue = 3.0f;
zarray_put(&core_normal->sharedInst, 0, &total);
zarray_put(&core_normal->sharedInUse, 0, &inuse);
zarray_put(&core_sim->sharedInst, 0, &total);
zarray_put(&core_sim->sharedInUse, 0, &inuse);
/* Grant: wants 5.0, only 3.0 in use out of 10.0 total. */
xvec_init(&uncomitted_n, 1, sizeof(float));
xvec_idval_init(&wanted_n, 1);
xvec_idval_store(&wanted_n, 0, 5.0f);
xvec_init(&uncomitted_s, 1, sizeof(float));
xvec_idval_init(&wanted_s, 1);
xvec_idval_store(&wanted_s, 0, 5.0f);
rc_normal = sched_check_shared_satisfy(core_normal, &uncomitted_n,
&wanted_n, &unmeet_n);
rc_sim = sched_check_shared_satisfy(core_sim, &uncomitted_s,
&wanted_s, &unmeet_s);
TEST_ASSERT_EQUAL_INT(1, rc_normal);
TEST_ASSERT_EQUAL_INT_MESSAGE(
rc_normal, rc_sim,
"sched_check_shared_satisfy() must grant identically for a "
"normal-mode-shaped and a simulation-mode-shaped core");
xvec_fini(&uncomitted_n);
xvec_idval_fini(&wanted_n);
xvec_idval_fini(&unmeet_n);
xvec_fini(&uncomitted_s);
xvec_idval_fini(&wanted_s);
xvec_idval_fini(&unmeet_s);
/* Deny: wants 8.0, only 7.0 of headroom remains (10.0 - 3.0). */
xvec_init(&uncomitted_n, 1, sizeof(float));
xvec_idval_init(&wanted_n, 1);
xvec_idval_store(&wanted_n, 0, 8.0f);
xvec_init(&uncomitted_s, 1, sizeof(float));
xvec_idval_init(&wanted_s, 1);
xvec_idval_store(&wanted_s, 0, 8.0f);
rc_normal = sched_check_shared_satisfy(core_normal, &uncomitted_n,
&wanted_n, &unmeet_n);
rc_sim = sched_check_shared_satisfy(core_sim, &uncomitted_s,
&wanted_s, &unmeet_s);
TEST_ASSERT_EQUAL_INT(0, rc_normal);
TEST_ASSERT_EQUAL_INT_MESSAGE(
rc_normal, rc_sim,
"sched_check_shared_satisfy() must deny identically for a "
"normal-mode-shaped and a simulation-mode-shaped core");
xvec_fini(&uncomitted_n);
xvec_idval_fini(&wanted_n);
xvec_idval_fini(&unmeet_n);
xvec_fini(&uncomitted_s);
xvec_idval_fini(&wanted_s);
xvec_idval_fini(&unmeet_s);
zarray_fini(&core_normal->sharedInst);
zarray_fini(&core_normal->sharedInUse);
zarray_fini(&core_sim->sharedInst);
zarray_fini(&core_sim->sharedInUse);
}
/* --- sched_check_host_satisfy(): identical grant/deny --------------- */
{
xce_sched_worker_t w_normal, w_sim;
xvec_t uncomitted_n, uncomitted_s;
xvec_idval_t wanted_n, wanted_s, unmeet_n, unmeet_s;
xce_val_t total, inuse;
int rc_normal, rc_sim;
memset(&w_normal, 0, sizeof(w_normal));
memset(&w_sim, 0, sizeof(w_sim));
zarray_init(&w_normal.resvals, 4, NULL);
zarray_init(&w_normal.resInUse, 4, NULL);
zarray_init(&w_sim.resvals, 4, NULL);
zarray_init(&w_sim.resInUse, 4, NULL);
memset(&total, 0, sizeof(total));
total.which = Value_u_fvalue;
total.u.fvalue = 10.0f;
memset(&inuse, 0, sizeof(inuse));
inuse.which = Value_u_fvalue;
inuse.u.fvalue = 3.0f;
zarray_put(&w_normal.resvals, 0, &total);
zarray_put(&w_normal.resInUse, 0, &inuse);
zarray_put(&w_sim.resvals, 0, &total);
zarray_put(&w_sim.resInUse, 0, &inuse);
/* Grant */
xvec_init(&uncomitted_n, 1, sizeof(float));
xvec_idval_init(&wanted_n, 1);
xvec_idval_store(&wanted_n, 0, 5.0f);
xvec_init(&uncomitted_s, 1, sizeof(float));
xvec_idval_init(&wanted_s, 1);
xvec_idval_store(&wanted_s, 0, 5.0f);
rc_normal = sched_check_host_satisfy(core_normal, &w_normal,
&uncomitted_n, &wanted_n,
&unmeet_n);
rc_sim = sched_check_host_satisfy(core_sim, &w_sim, &uncomitted_s,
&wanted_s, &unmeet_s);
TEST_ASSERT_EQUAL_INT(1, rc_normal);
TEST_ASSERT_EQUAL_INT_MESSAGE(
rc_normal, rc_sim,
"sched_check_host_satisfy() must grant identically for a "
"normal-mode-shaped and a simulation-mode-shaped core");
xvec_fini(&uncomitted_n);
xvec_idval_fini(&wanted_n);
xvec_idval_fini(&unmeet_n);
xvec_fini(&uncomitted_s);
xvec_idval_fini(&wanted_s);
xvec_idval_fini(&unmeet_s);
/* Deny */
xvec_init(&uncomitted_n, 1, sizeof(float));
xvec_idval_init(&wanted_n, 1);
xvec_idval_store(&wanted_n, 0, 8.0f);
xvec_init(&uncomitted_s, 1, sizeof(float));
xvec_idval_init(&wanted_s, 1);
xvec_idval_store(&wanted_s, 0, 8.0f);
rc_normal = sched_check_host_satisfy(core_normal, &w_normal,
&uncomitted_n, &wanted_n,
&unmeet_n);
rc_sim = sched_check_host_satisfy(core_sim, &w_sim, &uncomitted_s,
&wanted_s, &unmeet_s);
TEST_ASSERT_EQUAL_INT(0, rc_normal);
TEST_ASSERT_EQUAL_INT_MESSAGE(
rc_normal, rc_sim,
"sched_check_host_satisfy() must deny identically for a "
"normal-mode-shaped and a simulation-mode-shaped core");
xvec_fini(&uncomitted_n);
xvec_idval_fini(&wanted_n);
xvec_idval_fini(&unmeet_n);
xvec_fini(&uncomitted_s);
xvec_idval_fini(&wanted_s);
xvec_idval_fini(&unmeet_s);
zarray_fini(&w_normal.resvals);
zarray_fini(&w_normal.resInUse);
zarray_fini(&w_sim.resvals);
zarray_fini(&w_sim.resInUse);
}
destroy(core_normal->resmgr);
destroy(core_sim->resmgr);
}
#define TEST(x) \
do { \
if (check_test(&arguments, #x)) { \
@ -505,6 +753,7 @@ int main(int argc, char* argv[])
TEST(test_new_sched_op_enum_values_exist);
TEST(test_sched_update_existing_queue_null_payload);
TEST(test_dead_pushmsg_symbols_absent);
TEST(test_core_safe_allocation_chain_driver_parity);
return UNITY_END();
}