feat(scheduler-core): implement what-if branching and simulation output recording (task 11.5)

Add sim_record.c (scheduler_t-free .replay writer: ReplayHeader + loaded
SnapshotEntry once, then per-event pass-through and per-session
ReplaySession finalization) and wire --sim-alt-events event-source
selection into sim_loop.c's drain loop, coexisting with task 11.2's
stdout default and task 11.4's --sim-verify. Extract scheduler_t-free
halves out of recording_session.c's begin/end so sim-side code can reuse
the decision-encoding path without gaining an xce_scheduler_t* dependency,
preserving Req 5 AC 17 isolation. Per Req 5 AC 16/19.
This commit is contained in:
Rongsong Shen 2026-08-07 02:42:50 +00:00
parent a6083d803e
commit 564408e967
Signed by: rshen
SSH key fingerprint: SHA256:8Vf2pZ3h6rJa7EhGK+WaAQ9Mta/apZvteW6g9U/WKKk
14 changed files with 1750 additions and 91 deletions

View file

@ -505,16 +505,18 @@ sched_sim_event_loop_test = executable(
# 17, "Simulation isolation"): "a build-time or structural check confirms no
# controller-facing symbol is reachable from the simulation driver's compiled
# object", plus the AC's own "confirmed by the struct definition itself, not
# runtime checks alone" clause. Same source-text-scan + nm(1) link-time-proof
# genre as sched_core_isolation_test above, PLUS a third, narrower structural
# layer scanning ONLY xce_sched_sim_driver_t's own struct body text for
# forbidden field types -- see the test's own header comment for why a
# comment-stripping pass is required first (sim.h's own boundary doc comment
# legitimately names xcecli_ctx_t/xce_cluster_config_t in prose). link_with
# establishes the build-order dependency on sched_sim_isolation_lib (parent
# meson.build -- sim.c AND sim_loop.c compiled together, isolated from the
# rest of the scheduler); XCE_SIM_*_PATH bake in absolute paths the same way
# XCE_CORE_*_PATH does above.
# runtime checks alone" clause. Extended by task 11.5 to also cover the new
# sim_record.c (--sim-output writer) sibling file. Same source-text-scan +
# nm(1) link-time-proof genre as sched_core_isolation_test above, PLUS a
# third, narrower structural layer scanning ONLY xce_sched_sim_driver_t's own
# struct body text for forbidden field types -- see the test's own header
# comment for why a comment-stripping pass is required first (sim.h's own
# boundary doc comment legitimately names xcecli_ctx_t/xce_cluster_config_t
# in prose). link_with establishes the build-order dependency on
# sched_sim_isolation_lib (parent meson.build -- sim.c + sim_loop.c +
# sim_record.c compiled together, isolated from the rest of the scheduler);
# XCE_SIM_*_PATH bake in absolute paths the same way XCE_CORE_*_PATH does
# above.
sched_sim_isolation_test = executable(
'sched_sim_isolation_test',
['test_sim_isolation.c'],
@ -530,6 +532,9 @@ sched_sim_isolation_test = executable(
'-DXCE_SIM_LOOP_C_PATH="'
+ (meson.current_source_dir() / '..' / 'src' / 'sim_loop.c')
+ '"',
'-DXCE_SIM_RECORD_C_PATH="'
+ (meson.current_source_dir() / '..' / 'src' / 'sim_record.c')
+ '"',
'-DXCE_SIM_LIB_PATH="'
+ sched_sim_isolation_lib.full_path()
+ '"'],
@ -566,6 +571,45 @@ sched_sim_verify_test = executable(
dependencies: [scheduler_test_deps],
install: false)
# Contract test for what-if event-source branching (scheduler-core spec,
# task 11.5, Req 5 AC 16): running with --sim-alt-events produces decisions
# driven by the alternate events, not the base file's original events. A
# baseline run (no --sim-alt-events) proves the base file's own events drive
# scheduling by default; a --sim-alt-events run against the SAME base file's
# snapshot proves the alternate file's events drive it instead, and that the
# base file's own events are never read once --sim-alt-events is set. Same
# white-box #include pattern as sched_sim_verify_test (core.c, core_clock.c,
# core_events.c, core_session.c, decision.c, plugin.c, plugins/builtin_fcfs.c,
# recording.c, sim.c, sim_loop.c, sim_verify.c, snapshot.c/snapshot_wire.c/
# snapshot_load.c) -- sim_verify.c is required at link time even though this
# file never sets --sim-verify (sim_loop.c externs its two entry points
# unconditionally, same reason sched_sim_event_loop_test needs it).
sched_sim_alt_events_test = executable(
'sched_sim_alt_events_test',
['test_sim_alt_events.c'],
include_directories:[incdir],
dependencies: [scheduler_test_deps],
install: false)
# Contract test for the --sim-output `.replay` writer (scheduler-core spec,
# task 11.5, Req 5 AC 19): the output file, when requested, is decodable by
# the same reader used for normal-mode recordings (test_record_lifecycle.c's
# own capn_getp(capn_root(&c), 0, 1) + read_X()/decode_X() technique, reused
# verbatim here), with full ReplayHeader -> SnapshotEntry -> ReplayEvent* ->
# ReplaySession -> ReplayEvent{endSession} stream symmetry (Req 5 AC 19's own
# symmetry table). A second test proves --sim-output composes with
# --sim-alt-events: the output reflects the alternate run's own events and
# decision, not the base file's. Same white-box #include pattern as
# sched_sim_verify_test PLUS ../src/recording_session.c (the scheduler_t-free
# session-accumulator entry points sim_loop.c now calls) PLUS the new
# ../src/sim_record.c (the code under test).
sched_sim_record_output_test = executable(
'sched_sim_record_output_test',
['test_sim_record_output.c'],
include_directories:[incdir],
dependencies: [scheduler_test_deps],
install: false)
# Register as meson tests so `meson test --suite scheduler` runs them all.
# detect_leaks=0 matches the suite convention (fixtures are not torn down).
test('scheduler test', scheduler_test, suite: 'scheduler',
@ -647,3 +691,9 @@ test('scheduler sim isolation test',
test('scheduler sim verify test',
sched_sim_verify_test, suite: 'scheduler',
env: ['ASAN_OPTIONS=detect_leaks=0'])
test('scheduler sim alt events test',
sched_sim_alt_events_test, suite: 'scheduler',
env: ['ASAN_OPTIONS=detect_leaks=0'])
test('scheduler sim record output test',
sched_sim_record_output_test, suite: 'scheduler',
env: ['ASAN_OPTIONS=detect_leaks=0'])

View file

@ -0,0 +1,485 @@
/*
* test_sim_alt_events.c - contract test for what-if event-source branching
* (scheduler-core spec, task 11.5, Req 5 AC 16).
*
* Observable under test (task 11.5's own text, task text's Observable (a)):
* - running with an alternate-events file produces decisions driven by
* the alternate events, not the base file's original events.
*
* Two tests:
* - a baseline run against the base .replay file ALONE (no
* --sim-alt-events) confirms its own event stream drives a PEND
* decision for the BASE job, and NOT the alt job.
* - a what-if run against the SAME base file's snapshot, but with
* --sim-alt-events pointed at a separate events-only stream, confirms
* the opposite: a PEND decision for the ALT job, and NOT the base job.
* Together these prove the switch is real (not merely "some decision
* appears"): the DRIVING SOURCE genuinely changed.
*
* Fixture strategy: the SAME real QUEUE_UPDATE+JOBS_PUSH-driving-the-real-
* builtin_fcfs-picker technique test_sim_verify.c's own integration-level
* fixture helpers establish (build_fixture_scheduler() records
* schedPolicy="builtin_fcfs" so sim_register_default_picker() resolves the
* real plugin) -- since core.c's own sched_job_is_ready()/
* sched_queue_is_ready() are unconditional `return 1` stubs (task 2.4's own
* documented, still-live scope note), any queue/job pair immediately
* becomes a next_job() candidate, so each run deterministically produces
* exactly one live PEND decision via make_pendreason() for whichever jobid
* that run's own event stream pushed. Decisions are observed by
* redirecting STDOUT_FILENO around sched_sim_driver_run(), the same
* dup()/dup2() capture technique test_sim_verify.c's own
* run_sim_and_capture_stdout() establishes (sim_print_decision()'s
* "[sim] PEND jobid=%llu" line, sim_loop.c).
*
* The alt-events file itself is a BARE sequence of ReplayEvent/ReplaySession
* messages (no ReplayHeader, no SnapshotEntry) -- Req 5 AC 16's own text:
* "the driver loads the snapshot from the base file, then replays events
* from the alt-events file (same .replay capnp stream format)". Once
* --sim-alt-events is set, sched_sim_driver_run()'s main loop reads
* EXCLUSIVELY from it from message 1 onward -- the same shape-based
* message dispatch (msg_root_has_shape(), never position-based beyond the
* base file's own first two messages) that already governs every message
* after a SnapshotEntry in a full `.replay` stream, so an alt-events file
* that is simply "the tail of a .replay stream" needs no header/snapshot
* prefix of its own.
*
* White-box #include of the same TU set test_sim_verify.c uses (see that
* file's own header comment) -- core.c/core_clock.c/core_events.c/
* core_session.c/decision.c/plugin.c/plugins/builtin_fcfs.c/recording.c/
* recording_session.c/sim.c/sim_loop.c/sim_record.c/sim_verify.c/
* snapshot.c/snapshot_load.c/snapshot_wire.c. sim_verify.c AND
* recording_session.c/sim_record.c are all required at link time
* (sched_sim_driver_run() externs sim_decision_eq()/sim_log_mismatch() and
* sched_record_session_ctx_new()/_finalize()/sim_record_write_header_and_
* snapshot()/sim_record_write_event() unconditionally) even though this
* file never sets --sim-verify or --sim-output, same reason
* test_sim_event_loop.c's own header comment documents.
*/
#include <errno.h>
#include <limits.h>
#include <stdatomic.h>
#include <stdbool.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <unity/unity.h>
#include "controller.capnp.h"
#include "kvmgr.h"
#include "scheduler.h"
#include "xstring.h"
/* Pull in the real functions under test into this TU (white-box #include,
* same convention as test_sim_verify.c). */
#include "../src/core.c"
#include "../src/core_clock.c"
#include "../src/core_events.c"
#include "../src/core_session.c"
#include "../src/decision.c"
#include "../src/plugin.c"
#include "../src/plugins/builtin_fcfs.c"
#include "../src/recording.c"
#include "../src/recording_session.c"
#include "../src/sim.c"
#include "../src/sim_loop.c"
#include "../src/sim_record.c"
#include "../src/sim_verify.c"
#include "../src/snapshot.c"
#include "../src/snapshot_load.c"
#include "../src/snapshot_wire.c"
void kill_sched_job(void* arg) { (void)arg; }
void kill_sched_queue(void* arg) { (void)arg; }
void kill_sched_worker(void* arg) { (void)arg; }
void kill_sched_user(void* arg) { (void)arg; }
void kill_sched_project(void* arg) { (void)arg; }
void kill_sched_resval(xce_val_t* val) { (void)val; }
void sched_worker_release_host_ctx(xce_sched_worker_t* w) { (void)w; }
int sched_fill_limit_requests(xce_scheduler_t* sched, xvec_idval_t* vec,
xlist_t* limits)
{
(void)sched;
(void)vec;
(void)limits;
return 0;
}
/* Real reimplementation of schedobj.c's sched_resolve_job_picker -- same
* copy test_sim_verify.c/test_sim_event_loop.c/test_sim_startup.c each
* carry (schedobj.c is not linked here: it would need plugin_register(),
* which requires a genuine xce_scheduler_t* neither this file's fixtures
* nor sim_loop.c's own core-scoped registration path have, Req 5 AC 17). */
int sched_resolve_job_picker(xce_sched_core_t* core, xce_sched_plugin_t** out)
{
xce_sched_plugin_t* plugin = NULL;
int rc = 0;
if ((core == NULL) || (out == NULL)) {
return -EINVAL;
}
*out = NULL;
if (core->job_pick_plugin == NULL) {
return -EINVAL;
}
lock(core->plugins, RTTI_LOCK_READ);
rc = key_find(core->plugins, core->job_pick_plugin, (void**)&plugin);
unlock(core->plugins);
if (rc <= 0) {
return (rc < 0) ? rc : -ENOENT;
}
if ((plugin == NULL) || (plugin->picker_iface == NULL)
|| (plugin->picker_iface->next_job == NULL)
|| (plugin->picker_iface->begin_session == NULL)
|| (plugin->picker_iface->end_session == NULL)
|| (plugin->picker_iface->on_dispatch == NULL)
|| (plugin->picker_iface->on_pend == NULL)) {
return -EINVAL;
}
*out = plugin;
return 0;
}
void setUp(void) { return; }
void tearDown(void) { return; }
/* ---------------------------------------------------------------------- */
/* Fixture helpers -- same techniques test_sim_verify.c already */
/* establishes for its own integration-level tests. */
/* ---------------------------------------------------------------------- */
static void make_tmp_dir(char* out, size_t out_len, const char* suffix)
{
char tmpl[PATH_MAX];
snprintf(tmpl, sizeof(tmpl), "/tmp/sched_altevents%s_XXXXXX", suffix);
TEST_ASSERT_NOT_NULL(mkdtemp(tmpl));
TEST_ASSERT_TRUE(strlen(tmpl) < out_len);
strcpy(out, tmpl);
}
static void build_fixture_scheduler(xce_scheduler_t* sched, char* record_dir)
{
int rc;
memset(sched, 0, sizeof(*sched));
mylock_init(&(sched->record_lock));
rc = xce_sched_core_init(&(sched->core));
TEST_ASSERT_EQUAL_INT(0, rc);
sched->core.job_pick_plugin = (char*)"builtin_fcfs";
sched->record_dir = record_dir;
}
static FILE* write_header_and_snapshot(char* out_path, size_t out_path_len,
const char* suffix)
{
char record_dir[PATH_MAX];
xce_scheduler_t sched;
int rc;
make_tmp_dir(record_dir, sizeof(record_dir), suffix);
build_fixture_scheduler(&sched, record_dir);
rc = sched_record_start(&sched, "fixture.replay");
TEST_ASSERT_EQUAL_INT(0, rc);
TEST_ASSERT_TRUE(strlen(sched.record_file_path) < out_path_len);
strcpy(out_path, sched.record_file_path);
return sched.record_file;
}
static void append_queue_update_event(FILE* f, uint64_t ts,
const char* queue_name)
{
xce_queue_t queue;
replay_event_t src;
struct capn c;
struct capn_segment* cs;
ReplayEvent_ptr p;
memset(&queue, 0, sizeof(queue));
queue.queue = (char*)queue_name;
queue.status = QUEUE_STAT_OPEN;
queue.priority = 1;
memset(&src, 0, sizeof(src));
src.ts = ts;
src.body_which = ReplayEvent_body_queueUpdate;
src.body.queue_update = &queue;
capn_init_malloc(&c);
cs = capn_root(&c).seg;
encode_ReplayEvent_ptr(cs, &p, &src);
capn_setp(capn_root(&c), 0, p.p);
TEST_ASSERT_EQUAL_INT(0, write_capnp_message(f, &c));
capn_free(&c);
}
static void append_jobs_push_event(FILE* f, uint64_t ts, uint64_t jobid,
const char* queue_name)
{
job_submit_data_t submit;
xce_job_t job;
xce_job_t* jobs[1];
xce_jobs_data_t jobs_data;
replay_event_t src;
struct capn c;
struct capn_segment* cs;
ReplayEvent_ptr p;
memset(&submit, 0, sizeof(submit));
submit.queue = (char*)queue_name;
submit.user = (char*)"alice";
submit.mintasks = 1;
submit.maxtasks = 1;
memset(&job, 0, sizeof(job));
job.jobid = jobid;
job.state = JOB_STAT_PEND;
job.submit = &submit;
jobs[0] = &job;
memset(&jobs_data, 0, sizeof(jobs_data));
jobs_data.n_total = 1;
jobs_data.count = 1;
jobs_data.jobs = jobs;
memset(&src, 0, sizeof(src));
src.ts = ts;
src.body_which = ReplayEvent_body_jobsPush;
src.body.jobs_push = &jobs_data;
capn_init_malloc(&c);
cs = capn_root(&c).seg;
encode_ReplayEvent_ptr(cs, &p, &src);
capn_setp(capn_root(&c), 0, p.p);
TEST_ASSERT_EQUAL_INT(0, write_capnp_message(f, &c));
capn_free(&c);
}
static void append_replay_session(FILE* f, uint64_t begin_round)
{
struct capn c;
struct capn_segment* cs;
ReplaySession_ptr sp;
capn_init_malloc(&c);
cs = capn_root(&c).seg;
sp = new_ReplaySession(cs);
ReplaySession_set_beginRound(sp, begin_round);
capn_setp(capn_root(&c), 0, sp.p);
TEST_ASSERT_EQUAL_INT(0, write_capnp_message(f, &c));
capn_free(&c);
}
static void append_end_of_stream_marker(FILE* f)
{
replay_event_t src;
struct capn c;
struct capn_segment* cs;
ReplayEvent_ptr p;
memset(&src, 0, sizeof(src));
src.body_which = ReplayEvent_body_endSession;
capn_init_malloc(&c);
cs = capn_root(&c).seg;
encode_ReplayEvent_ptr(cs, &p, &src);
capn_setp(capn_root(&c), 0, p.p);
TEST_ASSERT_EQUAL_INT(0, write_capnp_message(f, &c));
capn_free(&c);
}
/* Runs sched_sim_driver_run() with optional --sim-alt-events, capturing
* everything written to stdout during the call -- same dup()/dup2()
* technique test_sim_verify.c's own run_sim_and_capture_stdout()
* establishes, extended with an `alt_path` parameter (NULL to omit
* --sim-alt-events entirely). */
static char* run_sim_and_capture_stdout(const char* base_path,
const char* alt_path, int* out_rc)
{
sched_sim_args_t args;
xce_sched_sim_driver_t sim;
char tmp[] = "/tmp/xce_altevents_run_XXXXXX";
int fd = mkstemp(tmp);
int saved_stdout;
off_t len;
char* buf;
TEST_ASSERT_TRUE_MESSAGE(fd >= 0, "mkstemp for stdout capture must "
"succeed");
fflush(stdout);
saved_stdout = dup(STDOUT_FILENO);
TEST_ASSERT_TRUE_MESSAGE(saved_stdout >= 0, "dup(STDOUT_FILENO) must "
"succeed");
TEST_ASSERT_TRUE_MESSAGE(dup2(fd, STDOUT_FILENO) >= 0,
"dup2 redirect of STDOUT_FILENO must succeed");
memset(&args, 0, sizeof(args));
args.replay_path = base_path;
args.sim_start_ts = SCHED_SIM_START_TS_LATEST;
args.sim_speed = 0; /* frozen -- deterministic, no wall-clock wait */
args.alt_events_path = alt_path;
*out_rc = sched_sim_driver_run(&sim, &args);
fflush(stdout);
dup2(saved_stdout, STDOUT_FILENO);
close(saved_stdout);
len = lseek(fd, 0, SEEK_CUR);
TEST_ASSERT_TRUE(len >= 0);
buf = (char*)calloc(1, (size_t)len + 1);
TEST_ASSERT_NOT_NULL(buf);
TEST_ASSERT_TRUE(lseek(fd, 0, SEEK_SET) == 0);
if (len > 0) {
ssize_t nread = read(fd, buf, (size_t)len);
TEST_ASSERT_TRUE(nread == len);
}
close(fd);
unlink(tmp);
return buf;
}
#define BASE_JOBID 777ULL
#define ALT_JOBID 888ULL
/* ---------------------------------------------------------------------- */
/* Test 1: baseline -- no --sim-alt-events, the base file's OWN events */
/* drive scheduling. */
/* ---------------------------------------------------------------------- */
static void test_baseline_run_uses_base_file_events(void)
{
char base_jobid_marker[32];
char alt_jobid_marker[32];
char path[PATH_MAX];
FILE* f;
int rc;
char* captured;
snprintf(base_jobid_marker, sizeof(base_jobid_marker), "jobid=%llu",
(unsigned long long)BASE_JOBID);
snprintf(alt_jobid_marker, sizeof(alt_jobid_marker), "jobid=%llu",
(unsigned long long)ALT_JOBID);
f = write_header_and_snapshot(path, sizeof(path), "_baseline");
TEST_ASSERT_NOT_NULL(f);
append_queue_update_event(f, 1000ULL, "normal");
append_jobs_push_event(f, 1500ULL, BASE_JOBID, "normal");
append_replay_session(f, 2000ULL);
append_end_of_stream_marker(f);
fclose(f);
captured = run_sim_and_capture_stdout(path, NULL, &rc);
TEST_ASSERT_EQUAL_INT(0, rc);
TEST_ASSERT_NOT_NULL_MESSAGE(
strstr(captured, base_jobid_marker),
"baseline run (no --sim-alt-events) must produce a decision for "
"the base file's own jobid");
TEST_ASSERT_NULL_MESSAGE(
strstr(captured, alt_jobid_marker),
"baseline run must never produce a decision for a jobid it never "
"saw");
free(captured);
printf("test_baseline_run_uses_base_file_events: PASS\n");
}
/* ---------------------------------------------------------------------- */
/* Test 2: what-if -- --sim-alt-events set, the ALTERNATE file's events */
/* drive scheduling instead (Req 5 AC 16's own Observable). */
/* ---------------------------------------------------------------------- */
static void test_alt_events_run_uses_alt_file_events_not_base(void)
{
char base_jobid_marker[32];
char alt_jobid_marker[32];
char base_path[PATH_MAX];
char alt_path[PATH_MAX];
FILE* base_f;
FILE* alt_f;
int rc;
char* captured;
snprintf(base_jobid_marker, sizeof(base_jobid_marker), "jobid=%llu",
(unsigned long long)BASE_JOBID);
snprintf(alt_jobid_marker, sizeof(alt_jobid_marker), "jobid=%llu",
(unsigned long long)ALT_JOBID);
/* Base file: header + empty snapshot, then its OWN (never-to-be-read,
* once --sim-alt-events is set) events for BASE_JOBID -- present to
* prove the switch is real: if alt-events were silently ignored, this
* run would fall back to seeing BASE_JOBID here too. */
base_f = write_header_and_snapshot(base_path, sizeof(base_path),
"_whatif_base");
TEST_ASSERT_NOT_NULL(base_f);
append_queue_update_event(base_f, 1000ULL, "normal");
append_jobs_push_event(base_f, 1500ULL, BASE_JOBID, "normal");
append_replay_session(base_f, 2000ULL);
append_end_of_stream_marker(base_f);
fclose(base_f);
/* Alt-events file: a BARE ReplayEvent/ReplaySession stream (no
* ReplayHeader, no SnapshotEntry -- this file's own header comment) for
* a DIFFERENT job, ALT_JOBID, on a queue the alt stream creates for
* itself (the loaded snapshot has zero queues). */
TEST_ASSERT_TRUE(snprintf(alt_path, sizeof(alt_path), "%s.alt", base_path)
< (int)sizeof(alt_path));
alt_f = fopen(alt_path, "w");
TEST_ASSERT_NOT_NULL(alt_f);
append_queue_update_event(alt_f, 3000ULL, "altqueue");
append_jobs_push_event(alt_f, 3500ULL, ALT_JOBID, "altqueue");
append_replay_session(alt_f, 4000ULL);
append_end_of_stream_marker(alt_f);
fclose(alt_f);
captured = run_sim_and_capture_stdout(base_path, alt_path, &rc);
TEST_ASSERT_EQUAL_INT(0, rc);
TEST_ASSERT_NOT_NULL_MESSAGE(
strstr(captured, alt_jobid_marker),
"--sim-alt-events run must produce a decision for the ALTERNATE "
"file's own jobid");
TEST_ASSERT_NULL_MESSAGE(
strstr(captured, base_jobid_marker),
"--sim-alt-events run must NOT produce a decision for the base "
"file's original jobid -- the base file's own events must never "
"be read once --sim-alt-events is set");
free(captured);
printf("test_alt_events_run_uses_alt_file_events_not_base: PASS\n");
}
int main(void)
{
UNITY_BEGIN();
RUN_TEST(test_baseline_run_uses_base_file_events);
RUN_TEST(test_alt_events_run_uses_alt_file_events_not_base);
return UNITY_END();
}

View file

@ -60,7 +60,13 @@
* extern functions defined there; this white-box TU must include it too or
* those calls are undefined references at link time, even though neither
* test in this file exercises --sim-verify itself -- that is
* test_sim_verify.c's own job) PLUS
* test_sim_verify.c's own job) PLUS ../src/recording_session.c/
* ../src/sim_record.c (task 11.5's sibling files -- same reasoning:
* sched_sim_driver_run() now unconditionally references
* sched_record_session_ctx_new()/_finalize()/sim_record_write_header_and_
* snapshot()/sim_record_write_event() as extern functions, undefined
* references at link time otherwise, even though this file never sets
* --sim-output either) PLUS
* ../src/snapshot.c/snapshot_wire.c/snapshot_load.c (task 8.1, needed by
* both the fixture-building and sim.c's own snapshot load).
*/
@ -89,8 +95,10 @@
#include "../src/plugin.c"
#include "../src/plugins/builtin_fcfs.c"
#include "../src/recording.c"
#include "../src/recording_session.c"
#include "../src/sim.c"
#include "../src/sim_loop.c"
#include "../src/sim_record.c"
#include "../src/sim_verify.c"
#include "../src/snapshot.c"
#include "../src/snapshot_load.c"
@ -444,13 +452,20 @@ test_process_replay_event_advances_virtual_clock_to_exact_event_ts_in_file_order
struct capn c;
capn_ptr root;
xce_core_event_t ev;
int record_rc = 0;
int translated;
TEST_ASSERT_EQUAL_INT(0, capn_init_fp(&c, f, 0));
TEST_ASSERT_TRUE(msg_root_has_shape(&c, &root, ReplayEvent_word_count,
ReplayEvent_pointer_count));
translated = sim_process_replay_event_message(&sim, &c, root, &ev);
/* record_rc: task 11.5's new --sim-output pass-through out-param
* (sim_loop.c) -- always 0 here, sim.record_file is NULL for this
* whole fixture (no --sim-output involved in this task's own
* Observable). */
translated
= sim_process_replay_event_message(&sim, &c, root, &ev, &record_rc);
TEST_ASSERT_EQUAL_INT(0, record_rc);
TEST_ASSERT_EQUAL_INT(1, translated);
TEST_ASSERT_EQUAL_INT(XCE_CORE_EVENT_JOB_STATE_CHANGE, ev.type);

View file

@ -4,10 +4,15 @@
* or structural check confirms no controller-facing symbol is reachable
* from the simulation driver's compiled object" -- plus the AC's own
* "confirmed by the struct definition itself, not runtime checks alone"
* clause. Scope: sim.h + sim.c + sim_loop.c together (task 11.1 + task
* 11.2's sibling file) -- NOT a re-audit of core.c (task 4.1's own
* clause. Scope: sim.h + sim.c + sim_loop.c + sim_record.c together (task
* 11.1 + task 11.2's sibling file + task 11.5's sibling file, the
* --sim-output writer) -- NOT a re-audit of core.c (task 4.1's own
* territory, test_core_isolation.c) and NOT plugin.c/builtin_fcfs.c (shared
* normal-mode infra, out of this task's boundary).
* normal-mode infra, out of this task's boundary). sim_verify.c (task 11.4)
* is likewise out of scope here -- it is never compiled into
* sched_sim_isolation_lib below and carries no isolation-sensitive logic of
* its own (pure struct comparison, no I/O); extending this guard to it was
* not part of task 11.4's own text and is left as-is.
*
* Three layers, extending test_core_isolation.c's two-layer pattern (same
* genre: source-text scans of the real, current on-disk files, not a frozen
@ -74,9 +79,13 @@
#error \
"XCE_SIM_LOOP_C_PATH must be defined by meson.build to sim_loop.c's absolute path"
#endif
#ifndef XCE_SIM_RECORD_C_PATH
#error \
"XCE_SIM_RECORD_C_PATH must be defined by meson.build to sim_record.c's absolute path"
#endif
#ifndef XCE_SIM_LIB_PATH
#error \
"XCE_SIM_LIB_PATH must be defined by meson.build to the absolute path of sim.c+sim_loop.c's isolated static_library() archive"
"XCE_SIM_LIB_PATH must be defined by meson.build to the absolute path of sim.c+sim_loop.c+sim_record.c's isolated static_library() archive"
#endif
/* Forbidden headers (design.md "Allowed Dependencies", same list
@ -346,81 +355,100 @@ void tearDown(void) { return; }
* failure mode this whole test exists to avoid). */
static void test_sim_source_paths_are_readable(void)
{
char* h = read_whole_file(XCE_SIM_H_PATH);
char* c = read_whole_file(XCE_SIM_C_PATH);
char* loop = read_whole_file(XCE_SIM_LOOP_C_PATH);
char* h = read_whole_file(XCE_SIM_H_PATH);
char* c = read_whole_file(XCE_SIM_C_PATH);
char* loop = read_whole_file(XCE_SIM_LOOP_C_PATH);
char* record = read_whole_file(XCE_SIM_RECORD_C_PATH);
TEST_ASSERT_NOT_NULL_MESSAGE(h, XCE_SIM_H_PATH);
TEST_ASSERT_NOT_NULL_MESSAGE(c, XCE_SIM_C_PATH);
TEST_ASSERT_NOT_NULL_MESSAGE(loop, XCE_SIM_LOOP_C_PATH);
TEST_ASSERT_NOT_NULL_MESSAGE(record, XCE_SIM_RECORD_C_PATH);
free(h);
free(c);
free(loop);
free(record);
}
static void test_sim_never_includes_forbidden_headers(void)
{
char* h_raw = read_whole_file(XCE_SIM_H_PATH);
char* c_raw = read_whole_file(XCE_SIM_C_PATH);
char* loop_raw = read_whole_file(XCE_SIM_LOOP_C_PATH);
char* h_raw = read_whole_file(XCE_SIM_H_PATH);
char* c_raw = read_whole_file(XCE_SIM_C_PATH);
char* loop_raw = read_whole_file(XCE_SIM_LOOP_C_PATH);
char* record_raw = read_whole_file(XCE_SIM_RECORD_C_PATH);
char* h;
char* c;
char* loop;
char* record;
TEST_ASSERT_NOT_NULL(h_raw);
TEST_ASSERT_NOT_NULL(c_raw);
TEST_ASSERT_NOT_NULL(loop_raw);
TEST_ASSERT_NOT_NULL(record_raw);
h = strip_c_comments(h_raw);
c = strip_c_comments(c_raw);
loop = strip_c_comments(loop_raw);
h = strip_c_comments(h_raw);
c = strip_c_comments(c_raw);
loop = strip_c_comments(loop_raw);
record = strip_c_comments(record_raw);
TEST_ASSERT_NOT_NULL(h);
TEST_ASSERT_NOT_NULL(c);
TEST_ASSERT_NOT_NULL(loop);
TEST_ASSERT_NOT_NULL(record);
assert_no_forbidden_includes(h, "sim.h");
assert_no_forbidden_includes(c, "sim.c");
assert_no_forbidden_includes(loop, "sim_loop.c");
assert_no_forbidden_includes(record, "sim_record.c");
free(h_raw);
free(c_raw);
free(loop_raw);
free(record_raw);
free(h);
free(c);
free(loop);
free(record);
}
static void test_sim_never_calls_forbidden_symbols(void)
{
char* h_raw = read_whole_file(XCE_SIM_H_PATH);
char* c_raw = read_whole_file(XCE_SIM_C_PATH);
char* loop_raw = read_whole_file(XCE_SIM_LOOP_C_PATH);
char* h_raw = read_whole_file(XCE_SIM_H_PATH);
char* c_raw = read_whole_file(XCE_SIM_C_PATH);
char* loop_raw = read_whole_file(XCE_SIM_LOOP_C_PATH);
char* record_raw = read_whole_file(XCE_SIM_RECORD_C_PATH);
char* h;
char* c;
char* loop;
char* record;
TEST_ASSERT_NOT_NULL(h_raw);
TEST_ASSERT_NOT_NULL(c_raw);
TEST_ASSERT_NOT_NULL(loop_raw);
TEST_ASSERT_NOT_NULL(record_raw);
h = strip_c_comments(h_raw);
c = strip_c_comments(c_raw);
loop = strip_c_comments(loop_raw);
h = strip_c_comments(h_raw);
c = strip_c_comments(c_raw);
loop = strip_c_comments(loop_raw);
record = strip_c_comments(record_raw);
TEST_ASSERT_NOT_NULL(h);
TEST_ASSERT_NOT_NULL(c);
TEST_ASSERT_NOT_NULL(loop);
TEST_ASSERT_NOT_NULL(record);
assert_no_forbidden_symbols(h, "sim.h");
assert_no_forbidden_symbols(c, "sim.c");
assert_no_forbidden_symbols(loop, "sim_loop.c");
assert_no_forbidden_symbols(record, "sim_record.c");
free(h_raw);
free(c_raw);
free(loop_raw);
free(record_raw);
free(h);
free(c);
free(loop);
free(record);
}
/*
@ -464,8 +492,9 @@ static void test_sim_driver_struct_has_no_forbidden_fields(void)
/*
* test_sim_object_has_no_forbidden_undefined_symbols - the literal
* build-time/link-time proof: sim.c + sim_loop.c, compiled together in
* isolation into sched_sim_isolation_lib (meson.build), must not reference
* build-time/link-time proof: sim.c + sim_loop.c + sim_record.c, compiled
* together in isolation into sched_sim_isolation_lib (meson.build), must
* not reference
* (as an undefined external symbol in the compiled archive) anything
* matching the forbidden prefixes above. Independent of what any header
* transitively declares -- reflects only what this driver's compiled code
@ -494,8 +523,8 @@ static void test_sim_object_has_no_forbidden_undefined_symbols(void)
/* posix format for an undefined symbol: "<name> U" (no
* value/size columns). Skip anything that doesn't parse as
* exactly that -- e.g. a blank line, or an archive member header
* some nm versions print (this archive has two member objects,
* sim.c.o and sim_loop.c.o). */
* some nm versions print (this archive has three member objects,
* sim.c.o, sim_loop.c.o, and sim_record.c.o). */
if (sscanf(line, "%511s %c", symbol, &type) != 2) {
continue;
}

View file

@ -0,0 +1,613 @@
/*
* test_sim_record_output.c - contract test for the --sim-output `.replay`
* writer (scheduler-core spec, task 11.5, Req 5 AC 19).
*
* Observable under test (task 11.5's own text, task text's Observable (b)):
* - the output file, when requested, is decodable by the same reader
* used for normal-mode recordings.
*
* "The same reader" is not a separate module anywhere in this codebase --
* both normal-mode recording's own contract test
* (test_record_lifecycle.c's test_full_cycle_start_event_session_stop_
* produces_expected_stream) and simulation's own production code
* (sim.c/sim_loop.c's msg_root_has_shape() + read_X()/decode_X()) decode a
* `.replay` stream the identical way: capn_init_fp() one message at a time,
* capn_getp(capn_root(&c), 0, 1) to reach the payload pointer (this
* codebase's own universal "real payload lives at pointer slot 0 of the
* implicit root struct" convention, sim.c's own file header comment), then
* the generated read_X()/decode_X() pair from controller.capnp.h. This
* file decodes its own --sim-output file with EXACTLY that same sequence
* (test_record_lifecycle.c's own literal technique, reused verbatim, not
* reinvented) -- proving the output stream is genuinely readable by the
* one true `.replay` reader this codebase has, not merely "non-empty".
*
* Two tests:
* - decodability + full structural symmetry: ReplayHeader (sim-run
* metadata, no cluster) -> SnapshotEntry (the loaded snapshot, same ts
* the driver itself used) -> ReplayEvent (pass-through, x2) ->
* ReplaySession (the live decision the real builtin_fcfs picker
* produced) -> ReplayEvent{endSession} -> EOF. Matches Req 5 AC 19's
* own symmetry table row-for-row.
* - composition with --sim-alt-events (Req 5 AC 19's own "What-if
* comparison" use case): the SAME run, but with --sim-alt-events set,
* produces an output file whose pass-through events and recorded
* decision reflect the ALTERNATE jobid, not the base file's original
* one -- proving the two task-11.5 features compose, not merely
* coexist independently.
*
* Fixture strategy: same real QUEUE_UPDATE+JOBS_PUSH-driving-the-real-
* builtin_fcfs-picker technique test_sim_verify.c/test_sim_alt_events.c
* already establish.
*
* White-box #include of the same TU set test_sim_verify.c uses (see that
* file's own header comment) PLUS ../src/recording_session.c (the
* scheduler_t-free session-accumulator entry points sim_loop.c now calls)
* PLUS the new ../src/sim_record.c (the code under test).
*/
#include <errno.h>
#include <limits.h>
#include <stdatomic.h>
#include <stdbool.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <unity/unity.h>
#include "controller.capnp.h"
#include "kvmgr.h"
#include "scheduler.h"
#include "xstring.h"
/* Pull in the real functions under test into this TU (white-box #include,
* same convention as test_sim_verify.c). */
#include "../src/core.c"
#include "../src/core_clock.c"
#include "../src/core_events.c"
#include "../src/core_session.c"
#include "../src/decision.c"
#include "../src/plugin.c"
#include "../src/plugins/builtin_fcfs.c"
#include "../src/recording.c"
#include "../src/recording_session.c"
#include "../src/sim.c"
#include "../src/sim_loop.c"
#include "../src/sim_record.c"
#include "../src/sim_verify.c"
#include "../src/snapshot.c"
#include "../src/snapshot_load.c"
#include "../src/snapshot_wire.c"
void kill_sched_job(void* arg) { (void)arg; }
void kill_sched_queue(void* arg) { (void)arg; }
void kill_sched_worker(void* arg) { (void)arg; }
void kill_sched_user(void* arg) { (void)arg; }
void kill_sched_project(void* arg) { (void)arg; }
void kill_sched_resval(xce_val_t* val) { (void)val; }
void sched_worker_release_host_ctx(xce_sched_worker_t* w) { (void)w; }
int sched_fill_limit_requests(xce_scheduler_t* sched, xvec_idval_t* vec,
xlist_t* limits)
{
(void)sched;
(void)vec;
(void)limits;
return 0;
}
int sched_resolve_job_picker(xce_sched_core_t* core, xce_sched_plugin_t** out)
{
xce_sched_plugin_t* plugin = NULL;
int rc = 0;
if ((core == NULL) || (out == NULL)) {
return -EINVAL;
}
*out = NULL;
if (core->job_pick_plugin == NULL) {
return -EINVAL;
}
lock(core->plugins, RTTI_LOCK_READ);
rc = key_find(core->plugins, core->job_pick_plugin, (void**)&plugin);
unlock(core->plugins);
if (rc <= 0) {
return (rc < 0) ? rc : -ENOENT;
}
if ((plugin == NULL) || (plugin->picker_iface == NULL)
|| (plugin->picker_iface->next_job == NULL)
|| (plugin->picker_iface->begin_session == NULL)
|| (plugin->picker_iface->end_session == NULL)
|| (plugin->picker_iface->on_dispatch == NULL)
|| (plugin->picker_iface->on_pend == NULL)) {
return -EINVAL;
}
*out = plugin;
return 0;
}
void setUp(void) { return; }
void tearDown(void) { return; }
/* ---------------------------------------------------------------------- */
/* Fixture helpers -- same techniques test_sim_verify.c/ */
/* test_sim_alt_events.c already establish. */
/* ---------------------------------------------------------------------- */
static void make_tmp_dir(char* out, size_t out_len, const char* suffix)
{
char tmpl[PATH_MAX];
snprintf(tmpl, sizeof(tmpl), "/tmp/sched_recordout%s_XXXXXX", suffix);
TEST_ASSERT_NOT_NULL(mkdtemp(tmpl));
TEST_ASSERT_TRUE(strlen(tmpl) < out_len);
strcpy(out, tmpl);
}
static void build_fixture_scheduler(xce_scheduler_t* sched, char* record_dir)
{
int rc;
memset(sched, 0, sizeof(*sched));
mylock_init(&(sched->record_lock));
rc = xce_sched_core_init(&(sched->core));
TEST_ASSERT_EQUAL_INT(0, rc);
sched->core.job_pick_plugin = (char*)"builtin_fcfs";
sched->record_dir = record_dir;
}
static FILE* write_header_and_snapshot(char* out_path, size_t out_path_len,
const char* suffix)
{
char record_dir[PATH_MAX];
xce_scheduler_t sched;
int rc;
make_tmp_dir(record_dir, sizeof(record_dir), suffix);
build_fixture_scheduler(&sched, record_dir);
rc = sched_record_start(&sched, "fixture.replay");
TEST_ASSERT_EQUAL_INT(0, rc);
TEST_ASSERT_TRUE(strlen(sched.record_file_path) < out_path_len);
strcpy(out_path, sched.record_file_path);
return sched.record_file;
}
static void append_queue_update_event(FILE* f, uint64_t ts,
const char* queue_name)
{
xce_queue_t queue;
replay_event_t src;
struct capn c;
struct capn_segment* cs;
ReplayEvent_ptr p;
memset(&queue, 0, sizeof(queue));
queue.queue = (char*)queue_name;
queue.status = QUEUE_STAT_OPEN;
queue.priority = 1;
memset(&src, 0, sizeof(src));
src.ts = ts;
src.body_which = ReplayEvent_body_queueUpdate;
src.body.queue_update = &queue;
capn_init_malloc(&c);
cs = capn_root(&c).seg;
encode_ReplayEvent_ptr(cs, &p, &src);
capn_setp(capn_root(&c), 0, p.p);
TEST_ASSERT_EQUAL_INT(0, write_capnp_message(f, &c));
capn_free(&c);
}
static void append_jobs_push_event(FILE* f, uint64_t ts, uint64_t jobid,
const char* queue_name)
{
job_submit_data_t submit;
xce_job_t job;
xce_job_t* jobs[1];
xce_jobs_data_t jobs_data;
replay_event_t src;
struct capn c;
struct capn_segment* cs;
ReplayEvent_ptr p;
memset(&submit, 0, sizeof(submit));
submit.queue = (char*)queue_name;
submit.user = (char*)"alice";
submit.mintasks = 1;
submit.maxtasks = 1;
memset(&job, 0, sizeof(job));
job.jobid = jobid;
job.state = JOB_STAT_PEND;
job.submit = &submit;
jobs[0] = &job;
memset(&jobs_data, 0, sizeof(jobs_data));
jobs_data.n_total = 1;
jobs_data.count = 1;
jobs_data.jobs = jobs;
memset(&src, 0, sizeof(src));
src.ts = ts;
src.body_which = ReplayEvent_body_jobsPush;
src.body.jobs_push = &jobs_data;
capn_init_malloc(&c);
cs = capn_root(&c).seg;
encode_ReplayEvent_ptr(cs, &p, &src);
capn_setp(capn_root(&c), 0, p.p);
TEST_ASSERT_EQUAL_INT(0, write_capnp_message(f, &c));
capn_free(&c);
}
static void append_replay_session(FILE* f, uint64_t begin_round)
{
struct capn c;
struct capn_segment* cs;
ReplaySession_ptr sp;
capn_init_malloc(&c);
cs = capn_root(&c).seg;
sp = new_ReplaySession(cs);
ReplaySession_set_beginRound(sp, begin_round);
capn_setp(capn_root(&c), 0, sp.p);
TEST_ASSERT_EQUAL_INT(0, write_capnp_message(f, &c));
capn_free(&c);
}
static void append_end_of_stream_marker(FILE* f)
{
replay_event_t src;
struct capn c;
struct capn_segment* cs;
ReplayEvent_ptr p;
memset(&src, 0, sizeof(src));
src.body_which = ReplayEvent_body_endSession;
capn_init_malloc(&c);
cs = capn_root(&c).seg;
encode_ReplayEvent_ptr(cs, &p, &src);
capn_setp(capn_root(&c), 0, p.p);
TEST_ASSERT_EQUAL_INT(0, write_capnp_message(f, &c));
capn_free(&c);
}
#define BASE_JOBID 777ULL
#define ALT_JOBID 888ULL
/* ---------------------------------------------------------------------- */
/* Test 1: --sim-output produces a file decodable by the same reader used */
/* for normal-mode recordings, with full ReplayHeader -> SnapshotEntry -> */
/* ReplayEvent* -> ReplaySession -> ReplayEvent{endSession} symmetry. */
/* ---------------------------------------------------------------------- */
static void test_output_file_is_decodable_with_full_stream_symmetry(void)
{
char base_path[PATH_MAX];
char out_path[PATH_MAX];
FILE* base_f;
int tmp_fd;
sched_sim_args_t args;
xce_sched_sim_driver_t sim;
int rc;
FILE* f;
struct capn c;
base_f = write_header_and_snapshot(base_path, sizeof(base_path),
"_decode_base");
TEST_ASSERT_NOT_NULL(base_f);
append_queue_update_event(base_f, 1000ULL, "normal");
append_jobs_push_event(base_f, 1500ULL, BASE_JOBID, "normal");
append_replay_session(base_f, 2000ULL);
append_end_of_stream_marker(base_f);
fclose(base_f);
snprintf(out_path, sizeof(out_path), "/tmp/xce_simoutput_decode_XXXXXX");
tmp_fd = mkstemp(out_path);
TEST_ASSERT_TRUE(tmp_fd >= 0);
close(tmp_fd);
memset(&args, 0, sizeof(args));
args.replay_path = base_path;
args.sim_start_ts = SCHED_SIM_START_TS_LATEST;
args.sim_speed = 0; /* frozen -- deterministic snapshot_ts */
args.output_path = out_path;
rc = sched_sim_driver_run(&sim, &args);
TEST_ASSERT_EQUAL_INT(0, rc);
f = fopen(out_path, "r");
TEST_ASSERT_NOT_NULL(f);
/* msg 1: ReplayHeader -- sim-run metadata, no cluster, sched_policy
* "builtin_fcfs" (this fixture's own recorded policy). Same
* read_ReplayHeader()/decode_ReplayHeader() technique
* test_record_lifecycle.c's own decode helpers use. */
{
ReplayHeader_ptr p;
struct ReplayHeader wire;
replay_header_t dst;
TEST_ASSERT_EQUAL_INT(0, capn_init_fp(&c, f, 0));
p.p = capn_getp(capn_root(&c), 0, 1);
read_ReplayHeader(&wire, p);
memset(&dst, 0, sizeof(dst));
decode_ReplayHeader(&dst, &wire);
TEST_ASSERT_EQUAL_STRING("1.0", dst.recorder_version);
TEST_ASSERT_NOT_NULL(dst.sched_policy);
TEST_ASSERT_EQUAL_STRING("builtin_fcfs", dst.sched_policy);
TEST_ASSERT_NULL_MESSAGE(
dst.cluster,
"simulation's own ReplayHeader must carry no cluster context "
"(Req 5 AC 17)");
free_ReplayHeader(&dst);
capn_free(&c);
}
/* msg 2: SnapshotEntry -- the loaded snapshot's own ts, verbatim
* (sim.snapshot_ts, stamped by sched_sim_driver_start()). */
{
SnapshotEntry_ptr p;
struct SnapshotEntry wire;
replay_snapshot_entry_t dst;
TEST_ASSERT_EQUAL_INT(0, capn_init_fp(&c, f, 0));
p.p = capn_getp(capn_root(&c), 0, 1);
read_SnapshotEntry(&wire, p);
memset(&dst, 0, sizeof(dst));
decode_SnapshotEntry(&dst, &wire);
TEST_ASSERT_EQUAL_UINT64(sim.snapshot_ts, dst.ts);
free_SnapshotEntry(&dst);
capn_free(&c);
}
/* msg 3: ReplayEvent{queueUpdate} pass-through. */
{
ReplayEvent_ptr p;
struct ReplayEvent wire;
replay_event_t dst;
TEST_ASSERT_EQUAL_INT(0, capn_init_fp(&c, f, 0));
p.p = capn_getp(capn_root(&c), 0, 1);
read_ReplayEvent(&wire, p);
memset(&dst, 0, sizeof(dst));
decode_ReplayEvent(&dst, &wire);
TEST_ASSERT_EQUAL_INT((int)ReplayEvent_body_queueUpdate,
(int)dst.body_which);
TEST_ASSERT_EQUAL_UINT64(1000ULL, dst.ts);
TEST_ASSERT_NOT_NULL(dst.body.queue_update);
TEST_ASSERT_EQUAL_STRING("normal", dst.body.queue_update->queue);
free_ReplayEvent(&dst);
capn_free(&c);
}
/* msg 4: ReplayEvent{jobsPush} pass-through -- BASE_JOBID (this run
* never set --sim-alt-events). */
{
ReplayEvent_ptr p;
struct ReplayEvent wire;
replay_event_t dst;
TEST_ASSERT_EQUAL_INT(0, capn_init_fp(&c, f, 0));
p.p = capn_getp(capn_root(&c), 0, 1);
read_ReplayEvent(&wire, p);
memset(&dst, 0, sizeof(dst));
decode_ReplayEvent(&dst, &wire);
TEST_ASSERT_EQUAL_INT((int)ReplayEvent_body_jobsPush,
(int)dst.body_which);
TEST_ASSERT_EQUAL_UINT64(1500ULL, dst.ts);
TEST_ASSERT_NOT_NULL(dst.body.jobs_push);
TEST_ASSERT_EQUAL_INT(1, dst.body.jobs_push->count);
TEST_ASSERT_EQUAL_UINT64(BASE_JOBID,
dst.body.jobs_push->jobs[0]->jobid);
free_ReplayEvent(&dst);
capn_free(&c);
}
/* msg 5: ReplaySession -- the LIVE decision the real builtin_fcfs
* picker produced (a PEND for BASE_JOBID -- core.c's own
* sched_job_is_ready()/sched_queue_is_ready() unconditional `return 1`
* stubs make it an immediate candidate). */
{
ReplaySession_ptr p;
replay_session_t* dst = NULL;
TEST_ASSERT_EQUAL_INT(0, capn_init_fp(&c, f, 0));
p.p = capn_getp(capn_root(&c), 0, 1);
decode_ReplaySession_ptr(&dst, p);
TEST_ASSERT_NOT_NULL(dst);
TEST_ASSERT_EQUAL_UINT64(2000ULL, dst->begin_round);
TEST_ASSERT_EQUAL_INT(1, dst->n_decisions);
TEST_ASSERT_EQUAL_INT((int)ReplayDecision_body_pend,
(int)dst->decisions[0]->body_which);
TEST_ASSERT_NOT_NULL(dst->decisions[0]->body.pend);
TEST_ASSERT_EQUAL_UINT64(BASE_JOBID,
dst->decisions[0]->body.pend->jobid);
free_ReplaySession_ptr(&dst);
capn_free(&c);
}
/* msg 6: the whole-stream termination marker, ReplayEvent{endSession}
* -- pass-through of the base file's OWN terminator (Req 5 AC 20), so
* the output stream is self-terminated exactly like a normal-mode
* recording. */
{
ReplayEvent_ptr p;
struct ReplayEvent wire;
replay_event_t dst;
TEST_ASSERT_EQUAL_INT(0, capn_init_fp(&c, f, 0));
p.p = capn_getp(capn_root(&c), 0, 1);
read_ReplayEvent(&wire, p);
memset(&dst, 0, sizeof(dst));
decode_ReplayEvent(&dst, &wire);
TEST_ASSERT_EQUAL_INT((int)ReplayEvent_body_endSession,
(int)dst.body_which);
free_ReplayEvent(&dst);
capn_free(&c);
}
/* Exactly 6 messages -- nothing more to read. */
TEST_ASSERT_NOT_EQUAL(0, capn_init_fp(&c, f, 0));
fclose(f);
unlink(out_path);
printf("test_output_file_is_decodable_with_full_stream_symmetry: "
"PASS\n");
}
/* ---------------------------------------------------------------------- */
/* Test 2: --sim-output composes with --sim-alt-events (Req 5 AC 19's own */
/* "What-if comparison" use case) -- the output reflects the ALTERNATE */
/* events, not the base file's original ones. */
/* ---------------------------------------------------------------------- */
static void test_output_file_reflects_alt_events_not_base_when_both_set(void)
{
char base_path[PATH_MAX];
char alt_path[PATH_MAX];
char out_path[PATH_MAX];
FILE* base_f;
FILE* alt_f;
int tmp_fd;
sched_sim_args_t args;
xce_sched_sim_driver_t sim;
int rc;
FILE* f;
struct capn c;
base_f = write_header_and_snapshot(base_path, sizeof(base_path),
"_decode_whatif_base");
TEST_ASSERT_NOT_NULL(base_f);
append_queue_update_event(base_f, 1000ULL, "normal");
append_jobs_push_event(base_f, 1500ULL, BASE_JOBID, "normal");
append_replay_session(base_f, 2000ULL);
append_end_of_stream_marker(base_f);
fclose(base_f);
TEST_ASSERT_TRUE(snprintf(alt_path, sizeof(alt_path), "%s.alt", base_path)
< (int)sizeof(alt_path));
alt_f = fopen(alt_path, "w");
TEST_ASSERT_NOT_NULL(alt_f);
append_queue_update_event(alt_f, 3000ULL, "altqueue");
append_jobs_push_event(alt_f, 3500ULL, ALT_JOBID, "altqueue");
append_replay_session(alt_f, 4000ULL);
append_end_of_stream_marker(alt_f);
fclose(alt_f);
snprintf(out_path, sizeof(out_path), "/tmp/xce_simoutput_whatif_XXXXXX");
tmp_fd = mkstemp(out_path);
TEST_ASSERT_TRUE(tmp_fd >= 0);
close(tmp_fd);
memset(&args, 0, sizeof(args));
args.replay_path = base_path;
args.sim_start_ts = SCHED_SIM_START_TS_LATEST;
args.sim_speed = 0;
args.alt_events_path = alt_path;
args.output_path = out_path;
rc = sched_sim_driver_run(&sim, &args);
TEST_ASSERT_EQUAL_INT(0, rc);
f = fopen(out_path, "r");
TEST_ASSERT_NOT_NULL(f);
/* msg 1: ReplayHeader; msg 2: SnapshotEntry -- skipped here, already
* proven decodable by the previous test; this test's own focus is
* WHICH jobid the pass-through events and the recorded decision carry. */
TEST_ASSERT_EQUAL_INT(0, capn_init_fp(&c, f, 0));
capn_free(&c);
TEST_ASSERT_EQUAL_INT(0, capn_init_fp(&c, f, 0));
capn_free(&c);
/* msg 3: ReplayEvent{queueUpdate} -- the ALT stream's own "altqueue",
* not the base stream's "normal". */
{
ReplayEvent_ptr p;
struct ReplayEvent wire;
replay_event_t dst;
TEST_ASSERT_EQUAL_INT(0, capn_init_fp(&c, f, 0));
p.p = capn_getp(capn_root(&c), 0, 1);
read_ReplayEvent(&wire, p);
memset(&dst, 0, sizeof(dst));
decode_ReplayEvent(&dst, &wire);
TEST_ASSERT_EQUAL_INT((int)ReplayEvent_body_queueUpdate,
(int)dst.body_which);
TEST_ASSERT_EQUAL_UINT64(3000ULL, dst.ts);
TEST_ASSERT_EQUAL_STRING("altqueue", dst.body.queue_update->queue);
free_ReplayEvent(&dst);
capn_free(&c);
}
/* msg 4: ReplayEvent{jobsPush} -- ALT_JOBID, not BASE_JOBID. */
{
ReplayEvent_ptr p;
struct ReplayEvent wire;
replay_event_t dst;
TEST_ASSERT_EQUAL_INT(0, capn_init_fp(&c, f, 0));
p.p = capn_getp(capn_root(&c), 0, 1);
read_ReplayEvent(&wire, p);
memset(&dst, 0, sizeof(dst));
decode_ReplayEvent(&dst, &wire);
TEST_ASSERT_EQUAL_INT((int)ReplayEvent_body_jobsPush,
(int)dst.body_which);
TEST_ASSERT_EQUAL_UINT64(ALT_JOBID, dst.body.jobs_push->jobs[0]->jobid);
free_ReplayEvent(&dst);
capn_free(&c);
}
/* msg 5: ReplaySession -- the live decision reflects ALT_JOBID. */
{
ReplaySession_ptr p;
replay_session_t* dst = NULL;
TEST_ASSERT_EQUAL_INT(0, capn_init_fp(&c, f, 0));
p.p = capn_getp(capn_root(&c), 0, 1);
decode_ReplaySession_ptr(&dst, p);
TEST_ASSERT_NOT_NULL(dst);
TEST_ASSERT_EQUAL_UINT64(4000ULL, dst->begin_round);
TEST_ASSERT_EQUAL_INT(1, dst->n_decisions);
TEST_ASSERT_EQUAL_UINT64(ALT_JOBID,
dst->decisions[0]->body.pend->jobid);
free_ReplaySession_ptr(&dst);
capn_free(&c);
}
fclose(f);
unlink(out_path);
printf("test_output_file_reflects_alt_events_not_base_when_both_set: "
"PASS\n");
}
int main(void)
{
UNITY_BEGIN();
RUN_TEST(test_output_file_is_decodable_with_full_stream_symmetry);
RUN_TEST(test_output_file_reflects_alt_events_not_base_when_both_set);
return UNITY_END();
}

View file

@ -58,8 +58,11 @@
* test_pend_decision_round_trip() already prove works.
*
* White-box #include of the same TU set test_sim_event_loop.c uses (see
* that file's own header comment) PLUS ../src/sim_verify.c (the code under
* test).
* that file's own header comment, including task 11.5's
* recording_session.c/sim_record.c -- required at link time by
* sched_sim_driver_run()'s unconditional externs regardless of whether
* --sim-output is ever set in a given test) PLUS ../src/sim_verify.c (the
* code under test).
*/
#include <errno.h>
#include <limits.h>
@ -89,8 +92,10 @@
#include "../src/plugin.c"
#include "../src/plugins/builtin_fcfs.c"
#include "../src/recording.c"
#include "../src/recording_session.c"
#include "../src/sim.c"
#include "../src/sim_loop.c"
#include "../src/sim_record.c"
#include "../src/sim_verify.c"
#include "../src/snapshot.c"
#include "../src/snapshot_load.c"