Dissolve the M0 crates (core/io/proto/telemetry/hip/cli) into the approved service layout abi/kn/ld/cc/rt/lab/sp/nt/tm/gw (docs/ARCHITECTURE.md). - kn: Lean-verified control bus + service registry (bus_lifecycle.lean, 14 thms) and SlabRing/UMA broker; rt: PrefixState + hybrid checkpoint pool. - sp: faithful throughput-aware Adaptive-K port of clover_policy.h (cooldowns, probes, throughput guards) with scenario parity tests. - gw: strata-ctl-compatible wire emitters + serve session FSM (serve_session.lean, 10 thms); abi::serve accepts image= (Vision delta closed). - abi: pure contracts, checked JSON/VTC1/GEN parsers, zero-alloc sampler. - ld/cc: checked safetensors reader + write-once VTC1 writer / O_RDONLY reader. - tm: lock-free telemetry; nt: LyraRpc; lab shares the rt executor core. - Docs: ARCHITECTURE.md decision-complete spec (MoE-only scope, .vbundle, .vplan->CompiledPlan, wire contract, migration map); AGENTS.md Lean rule + tandem/review workflow. Verified live: cargo test --workspace -D warnings = 46/46 PASS; lean proofs = 34 theorems (slab_ring_dma 10, bus_lifecycle 14, serve_session 10), 0 sorryAx, standard axioms only; vetta-infer info on Inkling = 1244 tensors, 18.18 GiB, checked bounds.
163 lines
5.3 KiB
Rust
163 lines
5.3 KiB
Rust
//! Fence-guarded `SlabRing` DMA ownership state machine.
|
|
//!
|
|
//! Directly implements the transitions verified in `proofs/slab_ring_dma.lean`:
|
|
//! `Free -> IoInFlight -> GpuReady -> GpuInUse -> Free`.
|
|
|
|
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
|
|
pub enum SlotState {
|
|
Free,
|
|
IoInFlight { req_id: u64 },
|
|
GpuReady { tag: u64 },
|
|
GpuInUse { tag: u64, fence: u64 },
|
|
}
|
|
|
|
#[derive(Debug, Clone, PartialEq, Eq)]
|
|
pub enum SlabError {
|
|
OutOfBounds { idx: usize, cap: usize },
|
|
InvalidTransition { idx: usize, state: SlotState, op: &'static str },
|
|
TagMismatch { idx: usize, expected: u64, actual: u64 },
|
|
FenceMismatch { idx: usize, expected: u64, actual: u64 },
|
|
NoFreeSlot,
|
|
}
|
|
|
|
impl std::fmt::Display for SlabError {
|
|
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
|
write!(f, "{self:?}")
|
|
}
|
|
}
|
|
|
|
impl std::error::Error for SlabError {}
|
|
|
|
/// Tracks ownership of `N` page-aligned zero-copy UMA buffers shared between `io_uring` and HIP.
|
|
#[derive(Debug, Clone)]
|
|
pub struct SlabRing {
|
|
slots: Vec<SlotState>,
|
|
slot_bytes: usize,
|
|
}
|
|
|
|
impl SlabRing {
|
|
pub fn new(num_slots: usize, slot_bytes: usize) -> Self {
|
|
assert!(num_slots > 0, "SlabRing requires at least 1 slot");
|
|
Self {
|
|
slots: vec![SlotState::Free; num_slots],
|
|
slot_bytes,
|
|
}
|
|
}
|
|
|
|
pub fn len(&self) -> usize {
|
|
self.slots.len()
|
|
}
|
|
|
|
pub fn is_empty(&self) -> bool {
|
|
self.slots.is_empty()
|
|
}
|
|
|
|
pub fn slot_bytes(&self) -> usize {
|
|
self.slot_bytes
|
|
}
|
|
|
|
pub fn state(&self, idx: usize) -> Result<SlotState, SlabError> {
|
|
self.slots
|
|
.get(idx)
|
|
.copied()
|
|
.ok_or(SlabError::OutOfBounds { idx, cap: self.slots.len() })
|
|
}
|
|
|
|
pub fn find_free(&self) -> Option<usize> {
|
|
self.slots.iter().position(|&s| s == SlotState::Free)
|
|
}
|
|
|
|
pub fn submit_io(&mut self, idx: usize, req_id: u64) -> Result<(), SlabError> {
|
|
let s = self.state(idx)?;
|
|
if s != SlotState::Free {
|
|
return Err(SlabError::InvalidTransition { idx, state: s, op: "submit_io" });
|
|
}
|
|
self.slots[idx] = SlotState::IoInFlight { req_id };
|
|
Ok(())
|
|
}
|
|
|
|
pub fn complete_io(&mut self, idx: usize, req_id: u64, tag: u64) -> Result<(), SlabError> {
|
|
match self.state(idx)? {
|
|
SlotState::IoInFlight { req_id: cur } if cur == req_id => {
|
|
self.slots[idx] = SlotState::GpuReady { tag };
|
|
Ok(())
|
|
}
|
|
other => Err(SlabError::InvalidTransition { idx, state: other, op: "complete_io" }),
|
|
}
|
|
}
|
|
|
|
pub fn abort_io(&mut self, idx: usize, req_id: u64) -> Result<(), SlabError> {
|
|
match self.state(idx)? {
|
|
SlotState::IoInFlight { req_id: cur } if cur == req_id => {
|
|
self.slots[idx] = SlotState::Free;
|
|
Ok(())
|
|
}
|
|
other => Err(SlabError::InvalidTransition { idx, state: other, op: "abort_io" }),
|
|
}
|
|
}
|
|
|
|
pub fn launch_gpu(&mut self, idx: usize, expected_tag: u64, fence: u64) -> Result<(), SlabError> {
|
|
match self.state(idx)? {
|
|
SlotState::GpuReady { tag } if tag == expected_tag => {
|
|
self.slots[idx] = SlotState::GpuInUse { tag, fence };
|
|
Ok(())
|
|
}
|
|
SlotState::GpuReady { tag } => {
|
|
Err(SlabError::TagMismatch { idx, expected: expected_tag, actual: tag })
|
|
}
|
|
other => Err(SlabError::InvalidTransition { idx, state: other, op: "launch_gpu" }),
|
|
}
|
|
}
|
|
|
|
pub fn discard_ready(&mut self, idx: usize) -> Result<(), SlabError> {
|
|
match self.state(idx)? {
|
|
SlotState::GpuReady { .. } => {
|
|
self.slots[idx] = SlotState::Free;
|
|
Ok(())
|
|
}
|
|
other => Err(SlabError::InvalidTransition { idx, state: other, op: "discard_ready" }),
|
|
}
|
|
}
|
|
|
|
pub fn complete_gpu(&mut self, idx: usize, fence: u64) -> Result<(), SlabError> {
|
|
match self.state(idx)? {
|
|
SlotState::GpuInUse { fence: cur, .. } if cur == fence => {
|
|
self.slots[idx] = SlotState::Free;
|
|
Ok(())
|
|
}
|
|
SlotState::GpuInUse { fence: cur, .. } => {
|
|
Err(SlabError::FenceMismatch { idx, expected: cur, actual: fence })
|
|
}
|
|
other => Err(SlabError::InvalidTransition { idx, state: other, op: "complete_gpu" }),
|
|
}
|
|
}
|
|
}
|
|
|
|
#[cfg(test)]
|
|
mod tests {
|
|
use super::*;
|
|
|
|
#[test]
|
|
fn full_ping_pong_lifecycle_and_safety() {
|
|
let mut ring = SlabRing::new(3, 4 * 1024 * 1024);
|
|
let s0 = ring.find_free().unwrap();
|
|
assert_eq!(s0, 0);
|
|
|
|
// Cannot launch GPU before DMA completes
|
|
assert!(ring.launch_gpu(s0, 10, 1).is_err());
|
|
|
|
ring.submit_io(s0, 100).unwrap();
|
|
// Cannot launch GPU while IO is still in flight
|
|
assert!(ring.launch_gpu(s0, 10, 1).is_err());
|
|
|
|
ring.complete_io(s0, 100, 10).unwrap();
|
|
// Cannot overwrite with new IO while GpuReady or GpuInUse
|
|
assert!(ring.submit_io(s0, 101).is_err());
|
|
|
|
ring.launch_gpu(s0, 10, 42).unwrap();
|
|
assert!(ring.submit_io(s0, 102).is_err());
|
|
assert!(ring.complete_gpu(s0, 999).is_err()); // wrong fence rejected
|
|
ring.complete_gpu(s0, 42).unwrap();
|
|
assert_eq!(ring.state(s0).unwrap(), SlotState::Free);
|
|
}
|
|
} |