Files
LithosAnanke/kernel/tests/test_stadium_ops.c
T
Claude 046ce0be65 feat(v4): the mesh boot to spec -- Hera born empty in the kernel's fabric; births, kills, every node stepping together (#189)
ENGINE step 4 Phase 1, Tasks 5b-5d, and the edge cases (Captain Bob, 2026-11-13: "Build it to proper
spec"; "Check for the edge cases"; "Yes, add it to v4-checks").

- v4/system/mesh.c, v4/include/v4/mesh.h: the nucleus assembled by the ROM and given to Hera as F18
  cells; Hera born empty at place 0, the kernel on port 0, the console on port 1; told she is 10, in the
  registry, the kernel's words, FORTH-79, sealed, POST, the kernel's capsules. NODE-BORN / NODE-KILL in
  the owner's room.
- No scheduler: v4_mesh_tell returns once the node told says how its text ended, and
  v4_mesh_step / v4_mesh_busy let the host step the fabric while it waits, so nodes run between
  lines as during them. A text left "waiting" is owed its end: the next line ends it "Interrupted"
  and is then run (MESH.md 7b.6).
- kernel: sk_v4.c boots through the mesh; its idle steps the fabric while a node can run, and halts
  only when none can (#151 kept). stadium_return_quota and stadium_ops_forget on a kill.
- Tests: v4/tests/test_mesh_boot.c (32 checks: the boot, births, kills, rebirth, a live number, a
  fault, no such node, Hera not killing herself, no room, a line too long, a node running for ever,
  a wait broken by the next line), run by `make -C v4 test` and under ASan+UBSan by `sanitize`
  (v4-checks); kernel/tests quota return; scripts/qemu_session.py births() and the edges on bare
  metal.
- Docs: ENGINE.md 6 "who runs next" struck (settled: no scheduler); the Phase 1 plan; FABRIC-3
  XLVIII.23; .claude/memory/rules.md: alpha.5 closed to new work.
- Evidence: v4-checks 80 programs under the sanitizers, 0 failures; the hosted builds alike; two
  stopped v4-checks runs labelled STOPPED; three development amd64 boots labelled.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_017RzZgrf7V7Zk2iczBXDRbm
2026-11-13 17:00:08 +00:00

109 lines
5.6 KiB
C

/* SPDX-License-Identifier: LicenseRef-Starship-1.0
* Copyright (c) 2023-2026 Robert A. James
*/
/* test_stadium_ops.c -- kernel/src/vm/stadium_ops.c on the host (#189, plan
* 2026-11-13-engine-step4-phase1, Task 3): HEAT-OP into the Stadium (Captain Bob, 2026-11-12: "Add
* HEAT-OP ... Both"; 2026-11-13: "Per heartbeat tick"). A node's 32 opcodes are patrons; at a charge,
* each opcode's retirements since the last are charged as that many of the word layer's dispatches --
* the same quantum, the same floor on the reservoir -- and a resident one cools by the ticks since,
* as a word does. Conservation holds throughout.
*/
#include "harness.h"
#include "starkernel/vm/stadium.h"
#include "starkernel/vm/stadium_ops.h"
#include "starkernel/session.h"
#include "starkernel/vm_uuid.h"
#include "starkernel/kmalloc.h"
#include "starkernel/q48_16.h"
#include "starforth_config.h"
#include <stdarg.h>
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
void *kmalloc(size_t size) { return calloc(1, size); }
void kfree(void *ptr) { free(ptr); }
kmalloc_stats_t kmalloc_get_stats(void)
{
kmalloc_stats_t s;
memset(&s, 0, sizeof s);
s.free_bytes = 512u * 1024u * 1024u; /* room for more than one quota, as on the boot (50 VM slots) */
return s;
}
void log_message(int level, const char *fmt, ...) { (void)level; (void)fmt; }
void sk_hal_panic(const char *message) { printf("PANIC: %s\n", message); exit(2); }
int blk_flush(uint32_t block_num) { (void)block_num; return 0; }
int main(void)
{
VMUuid key = stadium_node_key(10);
uint64_t counts[32], before, h20, r, q = (uint64_t)STADIUM_WORD_HEAT_QUANTUM;
unsigned k, hot = 0;
CHECK(stadium_boot_init_for(key) == 0 && session_boot_init() == 0 && stadium_birth_node(key, "Hera") == 0,
"a Stadium with the node born in it");
stadium_ops_init();
memset(counts, 0, sizeof counts);
stadium_ops_charge(key, counts, 1);
for (k = 0; k < 32; k++) if (stadium_ops_heat(key, k) != 0) hot++;
CHECK(hot == 0 && stadium_reservoir_peek(key) == Q48_ONE, "no retirements, no heat: the reservoir holds 1.0");
counts[20] = 5; /* + retired five times */
before = stadium_reservoir_peek(key);
stadium_ops_charge(key, counts, 2);
h20 = stadium_ops_heat(key, 20);
CHECK(h20 == 5u * q && stadium_reservoir_peek(key) == before - 5u * q,
"five retirements of + are five dispatches' heat, pulled from the reservoir (%llu)", (unsigned long long)h20);
CHECK(stadium_ops_heat(key, 21) == 0, "and no other opcode is heated");
CHECK(stadium_conserved(key), "conservation holds");
stadium_ops_charge(key, counts, 12); /* ten ticks later, nothing retired */
CHECK(stadium_ops_heat(key, 20) < h20 && stadium_reservoir_peek(key) > before - 5u * q,
"with nothing retired, + cools over ten ticks, back to the reservoir");
CHECK(stadium_conserved(key), "conservation holds");
counts[24] = (uint64_t)1 << 40; /* dup, very many times */
stadium_ops_charge(key, counts, 13);
r = stadium_reservoir_peek(key);
CHECK(r >= Q48_ONE / 3, "a flood of retirements leaves the reservoir its floor, a third (%llu)", (unsigned long long)r);
CHECK(stadium_ops_heat(key, 24) > 0 && stadium_conserved(key), "dup is heated, and conservation holds");
CHECK(stadium_ops_heat(vm_uuid_hera(), 20) == 0, "a key with no quota has no opcode heat");
/* more wanted than the reservoir has: shared by retirements, not by the order the opcodes are
* charged in -- a low-numbered opcode retired a little does not take it all */
{
VMUuid k2 = stadium_node_key(12);
uint64_t c2[32], h0, h5;
CHECK(stadium_grant_quota(k2, key) == 0, "a second node, its own quota");
memset(c2, 0, sizeof c2);
c2[0] = 1000000; /* ; retired a million times */
c2[5] = 3000000; /* next three million */
stadium_ops_charge(k2, c2, 20);
h0 = stadium_ops_heat(k2, 0); h5 = stadium_ops_heat(k2, 5);
CHECK(h0 > 0 && h5 > 0, "both opcodes are heated (; %llu, next %llu)", (unsigned long long)h0, (unsigned long long)h5);
CHECK(h5 > 2u * h0 && h5 < 4u * h0, "in proportion to their retirements, about 3 to 1");
CHECK(stadium_reservoir_peek(k2) >= Q48_ONE / 3 && stadium_conserved(k2), "the floor kept, conservation held");
}
/* a node killed: its quota given back (#189, Phase 1 Task 5c) -- its patrons evicted, its cells
* to the node it came from, its slot free for the next birth; its opcode counts forgotten */
{
VMUuid k2 = stadium_node_key(12), k3 = stadium_node_key(13);
uint64_t c3[32];
stadium_ops_forget(k2);
CHECK(stadium_return_quota(k2, key) == 0, "node 12's quota is given back to node 10");
CHECK(stadium_quota_slot_for_vm(k2) < 0 && stadium_ops_heat(k2, 5) == 0, "node 12 has no quota, and no opcode heat");
CHECK(stadium_conserved(key), "node 10 conserved after taking its cells back");
CHECK(stadium_return_quota(key, key) != 0 && stadium_return_quota(k2, key) != 0, "a quota is not given back to itself, or twice");
CHECK(stadium_grant_quota(k3, key) == 0, "the slot is free: node 13 is granted one");
memset(c3, 0, sizeof c3);
c3[7] = 2;
stadium_ops_charge(k3, c3, 30);
CHECK(stadium_ops_heat(k3, 7) == 2u * (uint64_t)STADIUM_WORD_HEAT_QUANTUM && stadium_ops_heat(k3, 5) == 0,
"node 13 starts with no counts of node 12's: two retirements, two quanta");
}
return DONE();
}