From 3c09be8643e9b6e0fd428d92c7b3d727c509d8df Mon Sep 17 00:00:00 2001 From: Wynn Drahorad Date: Thu, 26 Nov 2026 14:06:05 -0600 Subject: [PATCH 1/8] Report KV pages held by active requests `/metrics` reports cached and free KV pages but not the pages live requests hold, so an operator cannot compute the KV share in use by requests, and pages reserved for their future growth are invisible: they are neither free, cached, nor written. Issue #537 asks for the pages held by active requests. A per-scrape scan of the checkpoint graph is not acceptable because `Cache::stats` deliberately avoids scanning `nodes`, so the cache keeps a maintained page count and the scheduler adds the counts of its live slots. `PrefixCheckpointCache::pin_request` and `unpin_request` record direct request pins and transitive request pins per checkpoint and keep `request_pinned_pages` at the tail-page count of checkpoints whose transitive request pin count is nonzero, so a page shared through a pinned ancestor counts once for every request that references it. Compaction asserts that the removed ancestor and its heir agree on request pins, and a checkpoint leaves `nodes` only at zero pins. The scheduler now takes every request-owned pin, and the ticket-parent pins that belong to host-store admission, through the request API, and reports `request_pages` as live slots' private pages plus the cache count and `reserved_pages` as live slots' reserved pages. `Metrics` stores both values and renders `flare_request_kv_pages` and `flare_reserved_kv_pages` in the Prometheus text only; the JSON snapshot is unchanged. `docs/api.md` defines both gauges and states that dividing the first by `flare_total_kv_pages` excludes idle cached prefixes and snapshot backing pages. The claimed result is visibility only: the KV pages held by active requests and the pages reserved for their growth become scrapeable, and the reserved gauge explains the remainder of non-free pages. No allocation, eviction, or scheduling decision changes. Evidence: `cargo test --locked --release --manifest-path helios/Cargo.toml --target-dir ~/.cache/hyperflare-target --features runtime-core,server --lib -- cache:: scheduler:: server::metrics` passed 508 tests. That run exercises the extended `Cache::stats` drift assertion, which compares the maintained request-pinned page count with a full scan of every checkpoint on every stats read, so every cache and scheduler test checks the counter. `request_pins_count_each_shared_page_once` asserts that two request pins on branches sharing an ancestor count three pages, that plain pins leave the count unchanged, that releasing a checkpoint without a request pin is an error, and that the count returns to zero. `request_kv_pages_count_shared_prefix_pages_once` asserts both gauges against the requests' own private and reserved pages after a shared prefix, and that both reach zero after retirement while cached pages stay nonzero. `counters_expose_fixed_names_without_request_payloads` covers the new gauges' types and values. Windows builds and the rest of the helios suite are left to CI. Signed-off-by: Wynn Drahorad Design-Signed-Off-By: Claude Opus 5.5 (omp agent); design-review=2e5b --- docs/api.md | 9 ++++ helios/src/cache.rs | 61 +++++++++++++++++++++++++- helios/src/cache/lifecycle_tests.rs | 3 ++ helios/src/cache/tests.rs | 24 +++++++++++ helios/src/scheduler.rs | 47 ++++++++++++++++---- helios/src/scheduler/tests.rs | 66 +++++++++++++++++++++++++++++ helios/src/server/metrics.rs | 18 ++++++++ 7 files changed, 218 insertions(+), 10 deletions(-) diff --git a/docs/api.md b/docs/api.md index 8eb8dbb3b..958676dec 100644 --- a/docs/api.md +++ b/docs/api.md @@ -295,6 +295,15 @@ text includes speculative and proposal counters by batch, width, or depth, graph launches by kind, embedding row-cache counters, HTTP admission gauges, and uptime. +`flare_request_kv_pages` counts the distinct KV pages that active requests +reference: their private pages plus the tail pages of the prefix checkpoints they +pin, each page counted once even when several requests reference it. +`flare_reserved_kv_pages` counts pages reserved for active requests' future +growth that are neither free, cached, nor written yet. Both are gauges read from +scheduler state on each scrape. Dividing `flare_request_kv_pages` by +`flare_total_kv_pages` gives the KV share held by live requests; idle cached +prefixes and snapshot backing pages are outside that share. + The route returns one JSON object instead when the query contains `format=json`, or when the `Accept` header gives `application/json` a higher quality than `text/plain`; at equal quality the earlier media range wins. `format=prometheus` diff --git a/helios/src/cache.rs b/helios/src/cache.rs index 057f0747b..ead33185e 100644 --- a/helios/src/cache.rs +++ b/helios/src/cache.rs @@ -79,6 +79,10 @@ pub struct Checkpoint { child_count: usize, direct_pins: u64, pins: u64, + /// Direct pins held by active requests, a subset of `direct_pins`. + direct_request_pins: u64, + /// Request pins held on this checkpoint or a descendant, a subset of `pins`. + request_pins: u64, accessed: u64, role: RetentionRole, } @@ -181,6 +185,8 @@ pub struct CacheStats { pub published_checkpoints: usize, pub pending_checkpoints: usize, pub cached_pages: usize, + /// Distinct tail pages of checkpoints pinned by active requests. + pub request_pinned_pages: usize, pub retired_pages: usize, pub retired_snapshots: usize, pub owned_pages: usize, @@ -213,6 +219,9 @@ pub struct PrefixCheckpointCache { ready_checkpoints: usize, unready_checkpoints: usize, tail_pages: usize, + /// Tail pages of checkpoints with `request_pins > 0`, kept as pins change so + /// `stats` does not scan `nodes`. + request_pinned_pages: usize, } impl PrefixCheckpointCache { @@ -241,6 +250,7 @@ impl PrefixCheckpointCache { ready_checkpoints: 0, unready_checkpoints: 0, tail_pages: 0, + request_pinned_pages: 0, }) } @@ -448,6 +458,8 @@ impl PrefixCheckpointCache { child_count: 0, direct_pins: 0, pins: 0, + direct_request_pins: 0, + request_pins: 0, accessed: use_epoch, role, }; @@ -533,6 +545,40 @@ impl PrefixCheckpointCache { Ok(()) } + /// Pins a checkpoint on behalf of an active request. Request pins also track the + /// pages the request keeps referenced: its private pages plus the pinned + /// ancestors' pages, each page counted once. + pub fn pin_request(&mut self, id: CheckpointId) -> Result<()> { + self.pin(id)?; + self.nodes.get_mut(&id).unwrap().direct_request_pins += 1; + let chain = self.chain(id)?; + for ancestor in chain { + let node = self.nodes.get_mut(&ancestor).unwrap(); + node.request_pins += 1; + if node.request_pins == 1 { + self.request_pinned_pages += node.tail_pages.len(); + } + } + Ok(()) + } + + pub fn unpin_request(&mut self, id: CheckpointId) -> Result<()> { + if self.checkpoint(id)?.direct_request_pins == 0 { + return Err("checkpoint has no request pin to release".into()); + } + self.unpin(id)?; + self.nodes.get_mut(&id).unwrap().direct_request_pins -= 1; + let chain = self.chain(id)?; + for ancestor in chain { + let node = self.nodes.get_mut(&ancestor).unwrap(); + node.request_pins -= 1; + if node.request_pins == 0 { + self.request_pinned_pages -= node.tail_pages.len(); + } + } + Ok(()) + } + /// Record GPU reads before submission; pinning alone does not track GPU use. pub fn mark_used(&mut self, id: CheckpointId, through: Fence) -> Result<()> { if !self.checkpoint(id)?.ready { @@ -577,6 +623,8 @@ impl PrefixCheckpointCache { debug_assert_eq!(node.direct_pins, 0); debug_assert_eq!(node.child_count, 1); debug_assert_eq!(node.pins, child.pins); + // Both nodes are counted or neither is, and the pages move into the counted heir. + debug_assert_eq!(node.request_pins, child.request_pins); let heir = child.id; let mut pages = node.tail_pages; pages.append(&mut child.tail_pages); @@ -821,11 +869,21 @@ impl PrefixCheckpointCache { // Tests check the kept counts against a full scan on every read. #[cfg(test)] assert_eq!( - (self.ready_checkpoints, self.unready_checkpoints, self.tail_pages), + ( + self.ready_checkpoints, + self.unready_checkpoints, + self.tail_pages, + self.request_pinned_pages + ), ( self.nodes.values().filter(|node| node.ready).count(), self.nodes.values().filter(|node| !node.ready).count(), self.nodes.values().map(|node| node.tail_pages.len()).sum::(), + self.nodes + .values() + .filter(|node| node.request_pins > 0) + .map(|node| node.tail_pages.len()) + .sum::(), ), "cache counts drifted from the checkpoints they count" ); @@ -833,6 +891,7 @@ impl PrefixCheckpointCache { published_checkpoints: self.ready_checkpoints, pending_checkpoints: self.unready_checkpoints, cached_pages: self.tail_pages, + request_pinned_pages: self.request_pinned_pages, retired_pages: self .retirement .iter() diff --git a/helios/src/cache/lifecycle_tests.rs b/helios/src/cache/lifecycle_tests.rs index 05de00924..a14e0e18e 100644 --- a/helios/src/cache/lifecycle_tests.rs +++ b/helios/src/cache/lifecycle_tests.rs @@ -74,6 +74,8 @@ impl Fixture { child_count: node.child_count, direct_pins: node.direct_pins, pins: node.pins, + direct_request_pins: node.direct_request_pins, + request_pins: node.request_pins, accessed: node.accessed, role: node.role, }, @@ -90,6 +92,7 @@ impl Fixture { ready_checkpoints: cache.ready_checkpoints, unready_checkpoints: cache.unready_checkpoints, tail_pages: cache.tail_pages, + request_pinned_pages: cache.request_pinned_pages, }, ids: self.ids, histories: self.histories.clone(), diff --git a/helios/src/cache/tests.rs b/helios/src/cache/tests.rs index af16ec6d4..186b7949b 100644 --- a/helios/src/cache/tests.rs +++ b/helios/src/cache/tests.rs @@ -347,6 +347,30 @@ fn duplicate_publication_retains_unused_resources_with_caller() { assert_eq!(cache.stats().owned_pages, 2); } +#[test] +fn request_pins_count_each_shared_page_once() { + let mut cache = cache(); + let left = history(2, 0); + let mut right = history(1, 0); + right.append(&[999; 64], &[]).unwrap(); + let parent = created(&mut cache, &left, &proposal(None, 64, &[0], 0, 0)); + let left_id = created(&mut cache, &left, &proposal(Some(parent), 128, &[1], 1, 0)); + let right_id = created(&mut cache, &right, &proposal(Some(parent), 128, &[2], 2, 0)); + cache.pin_request(left_id).unwrap(); + cache.pin_request(right_id).unwrap(); + // Both requests pin the shared parent, whose page counts once for both. + assert_eq!(cache.stats().request_pinned_pages, 3); + cache.pin(right_id).unwrap(); + assert_eq!(cache.stats().request_pinned_pages, 3); + assert!(cache.unpin_request(parent).is_err()); + cache.unpin_request(left_id).unwrap(); + assert_eq!(cache.stats().request_pinned_pages, 2); + cache.unpin(right_id).unwrap(); + assert_eq!(cache.stats().request_pinned_pages, 2); + cache.unpin_request(right_id).unwrap(); + assert_eq!(cache.stats().request_pinned_pages, 0); +} + #[test] fn branching_pins_ancestors_and_prevents_direct_ancestor_unpin() { let mut cache = cache(); diff --git a/helios/src/scheduler.rs b/helios/src/scheduler.rs index 00e0457c2..049fb9f1b 100644 --- a/helios/src/scheduler.rs +++ b/helios/src/scheduler.rs @@ -126,6 +126,10 @@ pub struct SchedulerStats { pub snapshot_backing_pages: usize, pub cached_checkpoints: usize, pub cached_pages: usize, + /// Distinct KV pages active requests reference: private pages plus their pinned prefix-checkpoint pages. + pub request_pages: usize, + /// KV pages reserved for active requests' growth and not yet written. + pub reserved_pages: usize, pub host_lookups: u64, pub host_hits: u64, pub host_reused_tokens: u64, @@ -1129,6 +1133,19 @@ impl RequestScheduler { snapshot_capacity: self.limits.snapshots as usize, cached_checkpoints: cache.published_checkpoints, cached_pages: cache.cached_pages, + request_pages: self + .slots + .iter() + .filter_map(|slot| slot.request.as_ref()) + .map(|request| request.private_pages.len()) + .sum::() + + cache.request_pinned_pages, + reserved_pages: self + .slots + .iter() + .filter_map(|slot| slot.request.as_ref()) + .map(|request| request.reserved_pages.len()) + .sum(), inbound_waiting: if self.tickets.is_empty() { 0 } else { @@ -1583,7 +1600,11 @@ impl RequestScheduler { None }; if let Some(id) = pinned { - self.cache.pin(id)?; + if inbound.is_none() { + self.cache.pin_request(id)?; + } else { + self.cache.pin(id)?; + } } let execution_limit = self.execution_limit(spec.prompt.len(), spec.max_new_tokens); let reserved_count = self.admission_pages( @@ -1594,7 +1615,11 @@ impl RequestScheduler { ); if reserved_count + (consumed / page_tokens) as usize > self.limits.pages as usize { if let Some(id) = pinned { - self.cache.unpin(id)?; + if inbound.is_none() { + self.cache.unpin_request(id)?; + } else { + self.cache.unpin(id)?; + } } return Err(crate::Error::admission( crate::AdmissionError::Context, @@ -1616,7 +1641,11 @@ impl RequestScheduler { self.ensure_resources(reserved_count + ticket_pages, ticket_snapshots) } { if let Some(id) = pinned { - self.cache.unpin(id)?; + if inbound.is_none() { + self.cache.unpin_request(id)?; + } else { + self.cache.unpin(id)?; + } } return Err(error); } @@ -1833,7 +1862,7 @@ impl RequestScheduler { let shared_pages = self.cache.page_chain(id)?; for handle in ticket.waiters { self.request(handle)?; - self.cache.pin(id)?; + self.cache.pin_request(id)?; let request = self.request_mut(handle)?; request.prefix = Some(id); request.prefix_tokens = key.consumed_tokens; @@ -2890,9 +2919,9 @@ impl RequestScheduler { return Err("completed boundary copy was not GPU-ready".into()); } self.record_publication(id); - self.cache.pin(id)?; + self.cache.pin_request(id)?; if let Some(parent) = self.request(handle)?.prefix { - self.cache.unpin(parent)?; + self.cache.unpin_request(parent)?; } let request = self.request_mut(handle)?; request @@ -2910,8 +2939,8 @@ impl RequestScheduler { if let PublishResult::Existing { id, ready: true } = result && matches!(capture.resume, RequestPhase::Snapshot | RequestPhase::Finishing) { - self.cache.pin(id)?; - if let Some(parent) = self.request(handle)?.prefix { self.cache.unpin(parent)?; } + self.cache.pin_request(id)?; + if let Some(parent) = self.request(handle)?.prefix { self.cache.unpin_request(parent)?; } let snapshot = self.cache.checkpoint(id)?.snapshot(); let shared_pages = self.cache.page_chain(id)?; let request = self.request_mut(handle)?; @@ -3078,7 +3107,7 @@ impl RequestScheduler { if !publish || request.error_code.is_some() { self.record_publication(prefix); } - self.cache.unpin(prefix)?; + self.cache.unpin_request(prefix)?; } if request.preempted && let Some( diff --git a/helios/src/scheduler/tests.rs b/helios/src/scheduler/tests.rs index da16005de..f5aa8c9e0 100644 --- a/helios/src/scheduler/tests.rs +++ b/helios/src/scheduler/tests.rs @@ -977,6 +977,72 @@ fn appended_conversation_outlives_snapshot_capacity_without_losing_prefix_pages( assert_eq!(scheduler.free_snapshot_count(), 3); } +#[test] +fn request_kv_pages_count_shared_prefix_pages_once() { + let mut scheduler = scheduler(2, 8, 5); + let seed = scheduler.admit(spec(64, 1)).unwrap(); + init(&mut scheduler, 1); + let step = scheduler.plan().unwrap(); + scheduler.commit(&step, Fence(2)).unwrap(); + scheduler + .complete( + Fence(2), + &records(seed, 2, 64, 1, 77, 2), + Default::default(), + ) + .unwrap(); + freeze(&mut scheduler, seed, 3); + scheduler.finish(seed).unwrap(); + let first = scheduler.admit(spec(65, 4)).unwrap(); + let second = scheduler.admit(spec(65, 4)).unwrap(); + init(&mut scheduler, 4); + let step = scheduler.plan().unwrap(); + scheduler.commit(&step, Fence(5)).unwrap(); + scheduler + .complete( + Fence(5), + &records(first, 5, 65, 1, 88, 1), + Default::default(), + ) + .unwrap(); + retain_prompt(&mut scheduler, 6); + let step = scheduler.plan().unwrap(); + assert_eq!(step.kind, GraphKind::Prefill); + assert_eq!(step.lanes[0].request, second); + scheduler.commit(&step, Fence(7)).unwrap(); + scheduler + .complete( + Fence(7), + &records(second, 7, 65, 1, 90, 1), + Default::default(), + ) + .unwrap(); + let (shared, first_private, second_private, first_reserved, second_reserved) = { + let first_request = scheduler.request(first).unwrap(); + let second_request = scheduler.request(second).unwrap(); + assert_eq!(first_request.shared_pages, second_request.shared_pages); + assert!(!first_request.shared_pages.is_empty()); + ( + first_request.shared_pages.len(), + first_request.private_pages.len(), + second_request.private_pages.len(), + first_request.reserved_pages.len(), + second_request.reserved_pages.len(), + ) + }; + let stats = scheduler.stats(); + assert_eq!(stats.request_pages, shared + first_private + second_private); + assert_eq!(stats.reserved_pages, first_reserved + second_reserved); + // Retirement releases the request pin and drains the slot for both a completed + // and a cancelled request; cancellation keeps this check independent of the + // terminal-snapshot plan. + scheduler.cancel(first).unwrap(); + scheduler.cancel(second).unwrap(); + let stats = scheduler.stats(); + assert_eq!((stats.request_pages, stats.reserved_pages), (0, 0)); + assert!(stats.cached_pages > 0); +} + #[test] fn branching_requests_own_distinct_tail_pages_and_terminal_snapshots() { let mut scheduler = scheduler(2, 8, 5); diff --git a/helios/src/server/metrics.rs b/helios/src/server/metrics.rs index a44f7c0f3..06d6ae6d4 100644 --- a/helios/src/server/metrics.rs +++ b/helios/src/server/metrics.rs @@ -261,6 +261,8 @@ pub(super) struct Metrics { snapshot_backing_pages: AtomicU64, cached_checkpoints: AtomicU64, cached_pages: AtomicU64, + request_pages: AtomicU64, + reserved_pages: AtomicU64, pub pending: AtomicU64, host_lookups: AtomicU64, host_hits: AtomicU64, @@ -511,6 +513,8 @@ impl Metrics { ), (&self.cached_checkpoints, stats.cached_checkpoints as u64), (&self.cached_pages, stats.cached_pages as u64), + (&self.request_pages, stats.request_pages as u64), + (&self.reserved_pages, stats.reserved_pages as u64), ] { target.store(value, Ordering::Relaxed); } @@ -1296,6 +1300,16 @@ impl Metrics { "Pages owned by immutable checkpoints.", &self.cached_pages, ), + ( + "request_kv_pages", + "Distinct KV pages referenced by active requests: private pages plus pinned prefix-checkpoint pages, each counted once.", + &self.request_pages, + ), + ( + "reserved_kv_pages", + "KV pages reserved for active requests' growth and not yet written.", + &self.reserved_pages, + ), ( "pending_requests", "Requests waiting inside the model scheduler, excluding its ingress channel.", @@ -1760,6 +1774,8 @@ mod tests { reused_tokens: 192, free_pages: 10, cached_pages: 3, + request_pages: 4, + reserved_pages: 2, host_lookups: 4, host_hits: 1, host_reused_tokens: 640, @@ -1803,6 +1819,8 @@ mod tests { ("free_kv_tokens", 1280), ("cached_kv_pages", 3), ("cached_kv_tokens", 384), + ("request_kv_pages", 4), + ("reserved_kv_pages", 2), ("host_cache_slots_in_use", 3), ("host_cache_slots_total", 8), ("host_cache_intake_slots", 2), From 50314ef36d0f977f7c6938dde565d3e847b201e0 Mon Sep 17 00:00:00 2001 From: Wynn Drahorad Date: Thu, 26 Nov 2026 22:56:26 -0600 Subject: [PATCH 2/8] Keep page gauges out of the copy-out retry key Review of the previous commit found four defects. `SchedulerStats` derives `PartialEq` and is embedded whole in the deferred-copy-out retry key (`CopyoutResources`), so the two new page gauges changed that key whenever a decode crossed a page boundary, re-offering host copy-outs with no cache-resource movement and contradicting the claim that no scheduling decision changes. The scheduler repeated the request-versus-ticket pin choice at three admission sites. `retire` emptied the request slot before releasing its request pin, so a failed release would drop the request instead of leaving it inspectable. And the new documentation described the gauges as read from scheduler state on each scrape, described request-pinned pages as double counted, and omitted the pages that retirement and admission tickets still hold. The retry key now builds `SchedulerStats` with `request_pages` and `reserved_pages` zeroed, since those move without any cache resource changing. `pin_prefix` and `unpin_prefix` take the pin kind once from the admission path and replace the three repeated branches, keeping the ticket-parent pin ahead of `ensure_resources`. `retire` releases the request pin before taking the request out of the slot. `docs/api.md` states that the worker publishes the gauges on each scheduler pass, that request-pinned checkpoint pages also appear in `flare_cached_kv_pages`, that neither gauge counts pages draining through retirement or an in-flight restore, and that `flare_request_kv_pages + flare_reserved_kv_pages` is the figure comparable to another runtime's used KV cache; the `pin_request` comment now describes only the cache's half of the count, since private pages live in the scheduler. The claimed result is unchanged: visibility into request-held and reserved KV pages, with no allocation, eviction, or scheduling decision affected. The retry key fix restores that claim. Evidence: `cargo test --locked --release --manifest-path helios/Cargo.toml --target-dir ~/.cache/hyperflare-target --features runtime-core,server --lib -- cache:: scheduler:: server::metrics` passed 508 tests after these changes, which includes the extended `Cache::stats` drift assertion over the request-pinned count and the scheduler lifecycle models that exercise admission, compaction, retirement, and the deferred-copy-out path. Windows builds and the rest of the helios suite remain with CI. Signed-off-by: Wynn Drahorad Design-Signed-Off-By: Claude Opus 5.5 (omp agent); design-review=2e5b --- docs/api.md | 15 ++++++++--- helios/src/cache.rs | 6 ++--- helios/src/scheduler.rs | 50 +++++++++++++++++++++--------------- helios/src/server/runtime.rs | 9 ++++++- 4 files changed, 51 insertions(+), 29 deletions(-) diff --git a/docs/api.md b/docs/api.md index 958676dec..c4f60603d 100644 --- a/docs/api.md +++ b/docs/api.md @@ -299,10 +299,17 @@ uptime. reference: their private pages plus the tail pages of the prefix checkpoints they pin, each page counted once even when several requests reference it. `flare_reserved_kv_pages` counts pages reserved for active requests' future -growth that are neither free, cached, nor written yet. Both are gauges read from -scheduler state on each scrape. Dividing `flare_request_kv_pages` by -`flare_total_kv_pages` gives the KV share held by live requests; idle cached -prefixes and snapshot backing pages are outside that share. +growth that are neither free, cached, nor written yet. The worker publishes both +on every scheduler pass and a scrape reads the last published values. Pages of +request-pinned checkpoints also appear in `flare_cached_kv_pages` because those +checkpoints stay cached; the request gauge adds the requests' private pages to +them. Neither gauge counts pages still draining through retirement or an +admission ticket's in-flight restore. Reserved pages are outside the request +gauge, so `flare_request_kv_pages + flare_reserved_kv_pages` is the figure +comparable to another runtime's used KV cache. Dividing either gauge by +`flare_total_kv_pages` excludes idle cached prefixes and snapshot backing pages, +and the test-only assertion in `Cache::stats` compares the maintained +request-pinned count with a full scan of every checkpoint on each read. The route returns one JSON object instead when the query contains `format=json`, or when the `Accept` header gives `application/json` a higher quality than diff --git a/helios/src/cache.rs b/helios/src/cache.rs index ead33185e..4623c3fdc 100644 --- a/helios/src/cache.rs +++ b/helios/src/cache.rs @@ -545,9 +545,9 @@ impl PrefixCheckpointCache { Ok(()) } - /// Pins a checkpoint on behalf of an active request. Request pins also track the - /// pages the request keeps referenced: its private pages plus the pinned - /// ancestors' pages, each page counted once. + /// Pins a checkpoint on behalf of an active request. Request pins also count the + /// tail pages of the pinned checkpoints, each page once; a request's own private + /// pages are counted by the scheduler, which holds them. pub fn pin_request(&mut self, id: CheckpointId) -> Result<()> { self.pin(id)?; self.nodes.get_mut(&id).unwrap().direct_request_pins += 1; diff --git a/helios/src/scheduler.rs b/helios/src/scheduler.rs index 049fb9f1b..264e10233 100644 --- a/helios/src/scheduler.rs +++ b/helios/src/scheduler.rs @@ -1599,12 +1599,9 @@ impl RequestScheduler { } else { None }; + let request_pin = inbound.is_none(); if let Some(id) = pinned { - if inbound.is_none() { - self.cache.pin_request(id)?; - } else { - self.cache.pin(id)?; - } + self.pin_prefix(id, request_pin)?; } let execution_limit = self.execution_limit(spec.prompt.len(), spec.max_new_tokens); let reserved_count = self.admission_pages( @@ -1615,11 +1612,7 @@ impl RequestScheduler { ); if reserved_count + (consumed / page_tokens) as usize > self.limits.pages as usize { if let Some(id) = pinned { - if inbound.is_none() { - self.cache.unpin_request(id)?; - } else { - self.cache.unpin(id)?; - } + self.unpin_prefix(id, request_pin)?; } return Err(crate::Error::admission( crate::AdmissionError::Context, @@ -1641,11 +1634,7 @@ impl RequestScheduler { self.ensure_resources(reserved_count + ticket_pages, ticket_snapshots) } { if let Some(id) = pinned { - if inbound.is_none() { - self.cache.unpin_request(id)?; - } else { - self.cache.unpin(id)?; - } + self.unpin_prefix(id, request_pin)?; } return Err(error); } @@ -1998,6 +1987,23 @@ impl RequestScheduler { } } + /// Pins `id` for an active request, or for a host admission ticket's parent. + fn pin_prefix(&mut self, id: CheckpointId, request: bool) -> Result<()> { + if request { + self.cache.pin_request(id) + } else { + self.cache.pin(id) + } + } + + fn unpin_prefix(&mut self, id: CheckpointId, request: bool) -> Result<()> { + if request { + self.cache.unpin_request(id) + } else { + self.cache.unpin(id) + } + } + /// Pins a GPU-ready checkpoint for the worker's copy-out; eviction and /// compaction skip it until `unpin_checkpoint`. pub fn pin_checkpoint(&mut self, id: CheckpointId) -> Result<()> { @@ -3091,6 +3097,14 @@ impl RequestScheduler { self.reused.push(self.cache.checkpoint(id)?.prefix()); } } + // Release the request pin before the slot is emptied, so a failed release + // leaves the request inspectable instead of dropping it silently. + if let Some(prefix) = self.request(handle)?.prefix { + if !publish || self.request(handle)?.error_code.is_some() { + self.record_publication(prefix); + } + self.cache.unpin_request(prefix)?; + } let mut request = self.slots[handle.slot as usize].request.take().unwrap(); if let Some(guard) = request.anchor_guard.take() { self.release_snapshot(guard); } let mut snapshot = request.terminal_snapshot; @@ -3103,12 +3117,6 @@ impl RequestScheduler { .drain(..publication.as_ref().unwrap().tail_pages.len()); snapshot = None; } - if let Some(prefix) = request.prefix { - if !publish || request.error_code.is_some() { - self.record_publication(prefix); - } - self.cache.unpin_request(prefix)?; - } if request.preempted && let Some( PublishResult::Created { id, ready: true } diff --git a/helios/src/server/runtime.rs b/helios/src/server/runtime.rs index 6e61dc23f..23eca0509 100644 --- a/helios/src/server/runtime.rs +++ b/helios/src/server/runtime.rs @@ -2776,7 +2776,14 @@ impl<'a> Worker<'a> { } let resources = CopyoutResources { completed: self.scheduler.completed(), - scheduler: self.scheduler.stats(), + // Decode moves request and reserved page counts without moving cache + // resources; zeroing them keeps deferred host copy-outs keyed on the + // cache state they actually depend on. + scheduler: crate::scheduler::SchedulerStats { + request_pages: 0, + reserved_pages: 0, + ..self.scheduler.stats() + }, exports: self.exports.len(), next: next_admission(&self.scheduler, &self.parked, &self.pending) .map(|next| (next.prompt_tokens, next.max_new_tokens, next.growable)), From 88c8cf8d18bbed1d0d89144c3d964db5a2981721 Mon Sep 17 00:00:00 2001 From: Wynn Drahorad Date: Fri, 27 Nov 2026 15:44:30 -0600 Subject: [PATCH 3/8] Fix KV-page docs ratio and fold duplicated page tests The KV-page paragraph in `docs/api.md` offered two incompatible ratios for one measurement, blamed division for pages the numerators never contained, cited a `#[cfg(test)]` assertion in operator documentation, and claimed publication on every scheduler pass. Rewrite it around one ratio, `(flare_request_kv_pages + flare_reserved_kv_pages) / flare_total_kv_pages`, name the population as requests owning a model slot with restore-ticket pages excluded, state that request-pinned checkpoint tail pages count in both the request and cached gauges, and give the actual cadence: passing scheduler passes, startup, and the post-shutdown drain. `pin_request`/`unpin_request` walked the checkpoint chain twice, once through `pin`/`unpin` and again for request accounting, allocating two chain vectors per request pin and release. One private `pin_with`/`unpin_with` pair now walks it once, keeps the readiness, refcount, and direct-pin guards, and adds a `debug_assert!` at the request-pin 1->0 transition that moves `request_pinned_pages`. Pin behavior is unchanged. The scheduler test duplicating the branching-request fixture and the cache test duplicating the branching-pin fixture are folded into those tests. They now also assert the request/reserved gauges, the cancellation drain, the shared page counted once, and that a plain copy-out pin does not move the request gauge. `SchedulerStats` active, request, and reserved counts now come from one pass over slots instead of three. Validation: `cargo check --locked --manifest-path helios/Cargo.toml --features server` passed. `cargo test --locked --manifest-path helios/Cargo.toml --features runtime-core,server --lib -- server:: scheduler:: cache::` reported 814 passed, 1 failed (the known `messages_image_hints_end_after_expansion_for_text_and_segmented_templates`, which needs FLARE_TEST_TEMPLATE_DIR), 11 ignored, 852 filtered out; the two merged tests pass. Limitation: no behavior change is claimed, and no live scrape measured the ratio. Signed-off-by: Wynn Drahorad Design-Signed-Off-By: Claude Opus 5.5 (omp agent); design-review=2e5b --- docs/api.md | 28 ++++++------ helios/src/cache.rs | 67 ++++++++++++++++------------ helios/src/cache/tests.rs | 41 ++++++----------- helios/src/scheduler.rs | 29 +++++------- helios/src/scheduler/tests.rs | 83 +++++++---------------------------- 5 files changed, 93 insertions(+), 155 deletions(-) diff --git a/docs/api.md b/docs/api.md index c4f60603d..b58b72603 100644 --- a/docs/api.md +++ b/docs/api.md @@ -295,21 +295,19 @@ text includes speculative and proposal counters by batch, width, or depth, graph launches by kind, embedding row-cache counters, HTTP admission gauges, and uptime. -`flare_request_kv_pages` counts the distinct KV pages that active requests -reference: their private pages plus the tail pages of the prefix checkpoints they -pin, each page counted once even when several requests reference it. -`flare_reserved_kv_pages` counts pages reserved for active requests' future -growth that are neither free, cached, nor written yet. The worker publishes both -on every scheduler pass and a scrape reads the last published values. Pages of -request-pinned checkpoints also appear in `flare_cached_kv_pages` because those -checkpoints stay cached; the request gauge adds the requests' private pages to -them. Neither gauge counts pages still draining through retirement or an -admission ticket's in-flight restore. Reserved pages are outside the request -gauge, so `flare_request_kv_pages + flare_reserved_kv_pages` is the figure -comparable to another runtime's used KV cache. Dividing either gauge by -`flare_total_kv_pages` excludes idle cached prefixes and snapshot backing pages, -and the test-only assertion in `Cache::stats` compares the maintained -request-pinned count with a full scan of every checkpoint on each read. +`flare_request_kv_pages` counts the distinct KV pages that requests owning a +model slot reference: their private pages plus the tail pages of the prefix +checkpoints they pin, each page counted once even when several requests +reference it. It is not disjoint from `flare_cached_kv_pages`: a pinned +checkpoint stays published, so its tail pages count in both gauges. Neither +gauge counts pages draining after a request retires, or a restore ticket's +in-flight pages. `flare_reserved_kv_pages` counts pages reserved for those +requests' growth that are neither free, cached, nor written yet. The worker +publishes both when a scheduler pass makes progress, and again at startup and +after the shutdown drain; a scrape reads the last published values. +`(flare_request_kv_pages + flare_reserved_kv_pages)` over +`flare_total_kv_pages` is the share comparable to vLLM's `kv_cache_usage_perc`; +idle cached prefixes and snapshot backing pages are outside it. The route returns one JSON object instead when the query contains `format=json`, or when the `Accept` header gives `application/json` a higher quality than diff --git a/helios/src/cache.rs b/helios/src/cache.rs index 4623c3fdc..dcfb9e11b 100644 --- a/helios/src/cache.rs +++ b/helios/src/cache.rs @@ -516,7 +516,7 @@ impl PrefixCheckpointCache { Ok(pages) } - pub fn pin(&mut self, id: CheckpointId) -> Result<()> { + fn pin_with(&mut self, id: CheckpointId, request: bool) -> Result<()> { if !self.checkpoint(id)?.ready { return Err("cannot pin a checkpoint before GPU readiness".into()); } @@ -527,56 +527,65 @@ impl PrefixCheckpointCache { return Err("checkpoint reference count exhausted".into()); } self.nodes.get_mut(&id).unwrap().direct_pins += 1; + if request { + self.nodes.get_mut(&id).unwrap().direct_request_pins += 1; + } for ancestor in chain { - self.nodes.get_mut(&ancestor).unwrap().pins += 1; + let node = self.nodes.get_mut(&ancestor).unwrap(); + node.pins += 1; + if request { + node.request_pins += 1; + if node.request_pins == 1 { + self.request_pinned_pages += node.tail_pages.len(); + } + } } Ok(()) } - pub fn unpin(&mut self, id: CheckpointId) -> Result<()> { + fn unpin_with(&mut self, id: CheckpointId, request: bool) -> Result<()> { + if request && self.checkpoint(id)?.direct_request_pins == 0 { + return Err("checkpoint has no request pin to release".into()); + } if self.checkpoint(id)?.direct_pins == 0 { return Err("checkpoint has no direct pin to release".into()); } let chain = self.chain(id)?; self.nodes.get_mut(&id).unwrap().direct_pins -= 1; + if request { + self.nodes.get_mut(&id).unwrap().direct_request_pins -= 1; + } for ancestor in chain { - self.nodes.get_mut(&ancestor).unwrap().pins -= 1; + let node = self.nodes.get_mut(&ancestor).unwrap(); + node.pins -= 1; + if request { + debug_assert!(node.request_pins > 0); + node.request_pins -= 1; + if node.request_pins == 0 { + self.request_pinned_pages -= node.tail_pages.len(); + } + } } Ok(()) } + pub fn pin(&mut self, id: CheckpointId) -> Result<()> { + self.pin_with(id, false) + } + + pub fn unpin(&mut self, id: CheckpointId) -> Result<()> { + self.unpin_with(id, false) + } + /// Pins a checkpoint on behalf of an active request. Request pins also count the /// tail pages of the pinned checkpoints, each page once; a request's own private /// pages are counted by the scheduler, which holds them. pub fn pin_request(&mut self, id: CheckpointId) -> Result<()> { - self.pin(id)?; - self.nodes.get_mut(&id).unwrap().direct_request_pins += 1; - let chain = self.chain(id)?; - for ancestor in chain { - let node = self.nodes.get_mut(&ancestor).unwrap(); - node.request_pins += 1; - if node.request_pins == 1 { - self.request_pinned_pages += node.tail_pages.len(); - } - } - Ok(()) + self.pin_with(id, true) } pub fn unpin_request(&mut self, id: CheckpointId) -> Result<()> { - if self.checkpoint(id)?.direct_request_pins == 0 { - return Err("checkpoint has no request pin to release".into()); - } - self.unpin(id)?; - self.nodes.get_mut(&id).unwrap().direct_request_pins -= 1; - let chain = self.chain(id)?; - for ancestor in chain { - let node = self.nodes.get_mut(&ancestor).unwrap(); - node.request_pins -= 1; - if node.request_pins == 0 { - self.request_pinned_pages -= node.tail_pages.len(); - } - } - Ok(()) + self.unpin_with(id, true) } /// Record GPU reads before submission; pinning alone does not track GPU use. diff --git a/helios/src/cache/tests.rs b/helios/src/cache/tests.rs index 186b7949b..eab4a156a 100644 --- a/helios/src/cache/tests.rs +++ b/helios/src/cache/tests.rs @@ -348,7 +348,7 @@ fn duplicate_publication_retains_unused_resources_with_caller() { } #[test] -fn request_pins_count_each_shared_page_once() { +fn branching_pins_ancestors_and_prevents_direct_ancestor_unpin() { let mut cache = cache(); let left = history(2, 0); let mut right = history(1, 0); @@ -356,39 +356,23 @@ fn request_pins_count_each_shared_page_once() { let parent = created(&mut cache, &left, &proposal(None, 64, &[0], 0, 0)); let left_id = created(&mut cache, &left, &proposal(Some(parent), 128, &[1], 1, 0)); let right_id = created(&mut cache, &right, &proposal(Some(parent), 128, &[2], 2, 0)); + assert_eq!(cache.page_chain(left_id).unwrap(), [PageId(0), PageId(1)]); + assert_eq!(cache.page_chain(right_id).unwrap(), [PageId(0), PageId(2)]); cache.pin_request(left_id).unwrap(); cache.pin_request(right_id).unwrap(); // Both requests pin the shared parent, whose page counts once for both. assert_eq!(cache.stats().request_pinned_pages, 3); - cache.pin(right_id).unwrap(); - assert_eq!(cache.stats().request_pinned_pages, 3); - assert!(cache.unpin_request(parent).is_err()); - cache.unpin_request(left_id).unwrap(); - assert_eq!(cache.stats().request_pinned_pages, 2); - cache.unpin(right_id).unwrap(); - assert_eq!(cache.stats().request_pinned_pages, 2); - cache.unpin_request(right_id).unwrap(); - assert_eq!(cache.stats().request_pinned_pages, 0); -} - -#[test] -fn branching_pins_ancestors_and_prevents_direct_ancestor_unpin() { - let mut cache = cache(); - let left = history(2, 0); - let mut right = history(1, 0); - right.append(&[999; 64], &[]).unwrap(); - let parent = created(&mut cache, &left, &proposal(None, 64, &[0], 0, 0)); - let left_id = created(&mut cache, &left, &proposal(Some(parent), 128, &[1], 1, 0)); - let right_id = created(&mut cache, &right, &proposal(Some(parent), 128, &[2], 2, 0)); - assert_eq!(cache.page_chain(left_id).unwrap(), [PageId(0), PageId(1)]); - assert_eq!(cache.page_chain(right_id).unwrap(), [PageId(0), PageId(2)]); - cache.pin(left_id).unwrap(); - cache.pin(right_id).unwrap(); assert_eq!(cache.checkpoint(parent).unwrap().pin_count(), 2); assert!(cache.unpin(parent).is_err()); assert_eq!(cache.checkpoint(parent).unwrap().pin_count(), 2); - cache.unpin(left_id).unwrap(); + cache.unpin_request(left_id).unwrap(); assert_eq!(cache.checkpoint(parent).unwrap().pin_count(), 1); + assert_eq!(cache.stats().request_pinned_pages, 2); + // A plain pin for host copy-out neither adds nor removes request-pinned pages. + cache.pin(right_id).unwrap(); + assert_eq!(cache.stats().request_pinned_pages, 2); + cache.unpin(right_id).unwrap(); + assert_eq!(cache.stats().request_pinned_pages, 2); let evicted = cache.evict(EvictionTarget { pages: 99, snapshots: 99, @@ -400,7 +384,10 @@ fn branching_pins_ancestors_and_prevents_direct_ancestor_unpin() { assert_eq!(cache.checkpoint(right_id).unwrap().parent(), None); assert_eq!(cache.page_chain(right_id).unwrap(), [PageId(0), PageId(2)]); assert_eq!(cache.lookup(&right).unwrap().checkpoint, Some(right_id)); - cache.unpin(right_id).unwrap(); + // The shared parent's page moved into the pinned heir and stays counted once. + assert_eq!(cache.stats().request_pinned_pages, 2); + cache.unpin_request(right_id).unwrap(); + assert_eq!(cache.stats().request_pinned_pages, 0); let evicted = cache.evict(EvictionTarget { pages: 2, snapshots: 2, diff --git a/helios/src/scheduler.rs b/helios/src/scheduler.rs index 264e10233..7529cd8df 100644 --- a/helios/src/scheduler.rs +++ b/helios/src/scheduler.rs @@ -1119,12 +1119,16 @@ impl RequestScheduler { } pub fn stats(&self) -> SchedulerStats { let cache = self.cache.stats(); + let mut active_requests = 0; + let mut request_pages = 0; + let mut reserved_pages = 0; + for request in self.slots.iter().filter_map(|slot| slot.request.as_ref()) { + active_requests += 1; + request_pages += request.private_pages.len(); + reserved_pages += request.reserved_pages.len(); + } SchedulerStats { - active_requests: self - .slots - .iter() - .filter(|slot| slot.request.is_some()) - .count(), + active_requests, free_slots: self.free_slot_count(), retiring_slots: self.slots.iter().filter(|slot| slot.retiring).count(), free_pages: self.free_pages.len(), @@ -1133,19 +1137,8 @@ impl RequestScheduler { snapshot_capacity: self.limits.snapshots as usize, cached_checkpoints: cache.published_checkpoints, cached_pages: cache.cached_pages, - request_pages: self - .slots - .iter() - .filter_map(|slot| slot.request.as_ref()) - .map(|request| request.private_pages.len()) - .sum::() - + cache.request_pinned_pages, - reserved_pages: self - .slots - .iter() - .filter_map(|slot| slot.request.as_ref()) - .map(|request| request.reserved_pages.len()) - .sum(), + request_pages: request_pages + cache.request_pinned_pages, + reserved_pages, inbound_waiting: if self.tickets.is_empty() { 0 } else { diff --git a/helios/src/scheduler/tests.rs b/helios/src/scheduler/tests.rs index f5aa8c9e0..203e4c3f1 100644 --- a/helios/src/scheduler/tests.rs +++ b/helios/src/scheduler/tests.rs @@ -978,7 +978,7 @@ fn appended_conversation_outlives_snapshot_capacity_without_losing_prefix_pages( } #[test] -fn request_kv_pages_count_shared_prefix_pages_once() { +fn branching_requests_own_distinct_tail_pages_and_terminal_snapshots() { let mut scheduler = scheduler(2, 8, 5); let seed = scheduler.admit(spec(64, 1)).unwrap(); init(&mut scheduler, 1); @@ -1017,25 +1017,23 @@ fn request_kv_pages_count_shared_prefix_pages_once() { Default::default(), ) .unwrap(); - let (shared, first_private, second_private, first_reserved, second_reserved) = { - let first_request = scheduler.request(first).unwrap(); - let second_request = scheduler.request(second).unwrap(); - assert_eq!(first_request.shared_pages, second_request.shared_pages); - assert!(!first_request.shared_pages.is_empty()); - ( - first_request.shared_pages.len(), - first_request.private_pages.len(), - second_request.private_pages.len(), - first_request.reserved_pages.len(), - second_request.reserved_pages.len(), - ) - }; + let first_request = scheduler.request(first).unwrap(); + let second_request = scheduler.request(second).unwrap(); + assert_eq!(first_request.shared_pages, second_request.shared_pages); + assert!(!first_request.shared_pages.is_empty()); + assert_ne!(first_request.private_pages, second_request.private_pages); + assert!(first_request.terminal_snapshot.is_none() && second_request.terminal_snapshot.is_none()); + let (shared, private, reserved) = ( + first_request.shared_pages.len(), + first_request.private_pages.len() + second_request.private_pages.len(), + first_request.reserved_pages.len() + second_request.reserved_pages.len(), + ); let stats = scheduler.stats(); - assert_eq!(stats.request_pages, shared + first_private + second_private); - assert_eq!(stats.reserved_pages, first_reserved + second_reserved); - // Retirement releases the request pin and drains the slot for both a completed - // and a cancelled request; cancellation keeps this check independent of the - // terminal-snapshot plan. + assert_eq!(stats.request_pages, shared + private); + assert_eq!(stats.reserved_pages, reserved); + // Cancellation retires both requests: the shared page is counted once while + // both are live, and retirement releases the request pins before the slots + // empty, so both gauges drain. scheduler.cancel(first).unwrap(); scheduler.cancel(second).unwrap(); let stats = scheduler.stats(); @@ -1043,53 +1041,6 @@ fn request_kv_pages_count_shared_prefix_pages_once() { assert!(stats.cached_pages > 0); } -#[test] -fn branching_requests_own_distinct_tail_pages_and_terminal_snapshots() { - let mut scheduler = scheduler(2, 8, 5); - let seed = scheduler.admit(spec(64, 1)).unwrap(); - init(&mut scheduler, 1); - let step = scheduler.plan().unwrap(); - scheduler.commit(&step, Fence(2)).unwrap(); - scheduler - .complete( - Fence(2), - &records(seed, 2, 64, 1, 77, 2), - Default::default(), - ) - .unwrap(); - freeze(&mut scheduler, seed, 3); - scheduler.finish(seed).unwrap(); - let first = scheduler.admit(spec(65, 4)).unwrap(); - let second = scheduler.admit(spec(65, 4)).unwrap(); - init(&mut scheduler, 4); - let step = scheduler.plan().unwrap(); - scheduler.commit(&step, Fence(5)).unwrap(); - scheduler - .complete( - Fence(5), - &records(first, 5, 65, 1, 88, 1), - Default::default(), - ) - .unwrap(); - retain_prompt(&mut scheduler, 6); - let step = scheduler.plan().unwrap(); - assert_eq!(step.kind, GraphKind::Prefill); - assert_eq!(step.lanes[0].request, second); - scheduler.commit(&step, Fence(7)).unwrap(); - scheduler - .complete( - Fence(7), - &records(second, 7, 65, 1, 90, 1), - Default::default(), - ) - .unwrap(); - let first = scheduler.request(first).unwrap(); - let second = scheduler.request(second).unwrap(); - assert_eq!(first.shared_pages, second.shared_pages); - assert_ne!(first.private_pages, second.private_pages); - assert!(first.terminal_snapshot.is_none() && second.terminal_snapshot.is_none()); -} - #[test] fn host_uploaded_decode_waits_for_its_previous_sample() { let mut scheduler = scheduler(1, 8, 2); From e72ee70d263301e7dd7f019afbe0d8f909c94a25 Mon Sep 17 00:00:00 2001 From: Wynn Drahorad Date: Sat, 28 Nov 2026 16:38:17 -0600 Subject: [PATCH 4/8] Name scheduler pin kinds at admission and count stats in one slot pass Admission chose between a request pin and a ticket-parent pin through a private `pin_prefix`/`unpin_prefix` pair taking the kind as a bare `bool`, a third way to name a pin kind beside the cache's `pin_request`/`pin` methods and the `pin_checkpoint` pair. The three admission sites now call the named cache methods directly, selected by `request_pin`, and the pair is deleted. `SchedulerStats` construction walks the slot array once, counting active requests, retiring slots, anchor guards, inbound-phase requests, and request and reserved pages together. `retire` reuses the prefix and failure state it binds before publication instead of re-reading the slot before the request-pin release. Pin and release behavior and every reported statistic are unchanged. Validation: `cargo check --locked --manifest-path helios/Cargo.toml --features server` passed. `cargo test --locked --manifest-path helios/Cargo.toml --features runtime-core,server --lib -- server:: scheduler:: cache::` reported 809 passed, 1 failed (`messages_image_hints_end_after_expansion_for_text_and_segmented_templates`, which needs FLARE_TEST_TEMPLATE_DIR), 11 ignored, 852 filtered out. Signed-off-by: Wynn Drahorad Design-Signed-Off-By: enterprise/deepseek-v4.1-flash (omp agent); design-review=2e5b --- helios/src/scheduler.rs | 61 ++++++++++++++--------------------------- 1 file changed, 21 insertions(+), 40 deletions(-) diff --git a/helios/src/scheduler.rs b/helios/src/scheduler.rs index 7529cd8df..7379c2963 100644 --- a/helios/src/scheduler.rs +++ b/helios/src/scheduler.rs @@ -1120,37 +1120,34 @@ impl RequestScheduler { pub fn stats(&self) -> SchedulerStats { let cache = self.cache.stats(); let mut active_requests = 0; + let mut retiring_slots = 0; + let mut checkpoint_anchor_guards = 0; + let mut inbound = 0; let mut request_pages = 0; let mut reserved_pages = 0; - for request in self.slots.iter().filter_map(|slot| slot.request.as_ref()) { - active_requests += 1; - request_pages += request.private_pages.len(); - reserved_pages += request.reserved_pages.len(); + for slot in &self.slots { + retiring_slots += slot.retiring as usize; + if let Some(request) = &slot.request { + active_requests += 1; + checkpoint_anchor_guards += request.anchor_guard.is_some() as usize; + inbound += (request.phase == RequestPhase::Inbound) as usize; + request_pages += request.private_pages.len(); + reserved_pages += request.reserved_pages.len(); + } } SchedulerStats { active_requests, free_slots: self.free_slot_count(), - retiring_slots: self.slots.iter().filter(|slot| slot.retiring).count(), + retiring_slots, free_pages: self.free_pages.len(), free_snapshots: self.free_snapshots.len(), - checkpoint_anchor_guards: self.slots.iter().filter_map(|slot| slot.request.as_ref()).filter(|request| request.anchor_guard.is_some()).count(), + checkpoint_anchor_guards, snapshot_capacity: self.limits.snapshots as usize, cached_checkpoints: cache.published_checkpoints, cached_pages: cache.cached_pages, request_pages: request_pages + cache.request_pinned_pages, reserved_pages, - inbound_waiting: if self.tickets.is_empty() { - 0 - } else { - self.slots - .iter() - .filter(|slot| { - slot.request - .as_ref() - .is_some_and(|request| request.phase == RequestPhase::Inbound) - }) - .count() - }, + inbound_waiting: if self.tickets.is_empty() { 0 } else { inbound }, ..self.stats } } @@ -1594,7 +1591,7 @@ impl RequestScheduler { }; let request_pin = inbound.is_none(); if let Some(id) = pinned { - self.pin_prefix(id, request_pin)?; + if request_pin { self.cache.pin_request(id)? } else { self.cache.pin(id)? } } let execution_limit = self.execution_limit(spec.prompt.len(), spec.max_new_tokens); let reserved_count = self.admission_pages( @@ -1605,7 +1602,7 @@ impl RequestScheduler { ); if reserved_count + (consumed / page_tokens) as usize > self.limits.pages as usize { if let Some(id) = pinned { - self.unpin_prefix(id, request_pin)?; + if request_pin { self.cache.unpin_request(id)? } else { self.cache.unpin(id)? } } return Err(crate::Error::admission( crate::AdmissionError::Context, @@ -1627,7 +1624,7 @@ impl RequestScheduler { self.ensure_resources(reserved_count + ticket_pages, ticket_snapshots) } { if let Some(id) = pinned { - self.unpin_prefix(id, request_pin)?; + if request_pin { self.cache.unpin_request(id)? } else { self.cache.unpin(id)? } } return Err(error); } @@ -1980,23 +1977,6 @@ impl RequestScheduler { } } - /// Pins `id` for an active request, or for a host admission ticket's parent. - fn pin_prefix(&mut self, id: CheckpointId, request: bool) -> Result<()> { - if request { - self.cache.pin_request(id) - } else { - self.cache.pin(id) - } - } - - fn unpin_prefix(&mut self, id: CheckpointId, request: bool) -> Result<()> { - if request { - self.cache.unpin_request(id) - } else { - self.cache.unpin(id) - } - } - /// Pins a GPU-ready checkpoint for the worker's copy-out; eviction and /// compaction skip it until `unpin_checkpoint`. pub fn pin_checkpoint(&mut self, id: CheckpointId) -> Result<()> { @@ -3062,6 +3042,7 @@ impl RequestScheduler { && (publish || request.consumed_tokens > request.admission_reused_tokens || request.generated_tokens > 0); let prefix = request.prefix; + let failed = request.error_code.is_some(); let result = match &publication { Some(proposal) => { let history = request.history.clone(); @@ -3092,8 +3073,8 @@ impl RequestScheduler { } // Release the request pin before the slot is emptied, so a failed release // leaves the request inspectable instead of dropping it silently. - if let Some(prefix) = self.request(handle)?.prefix { - if !publish || self.request(handle)?.error_code.is_some() { + if let Some(prefix) = prefix { + if !publish || failed { self.record_publication(prefix); } self.cache.unpin_request(prefix)?; From de9aa94e03833de01b2d44a798411c9ae02f9770 Mon Sep 17 00:00:00 2001 From: Wynn Drahorad Date: Sat, 28 Nov 2026 16:38:26 -0600 Subject: [PATCH 5/8] Test the request-pin release guard and look pinned nodes up once `unpin_request` rejects a release on a checkpoint that holds no direct request pin, which keeps a double release from corrupting `request_pins` and the request-pinned page count. No test reached that guard. The branching-pin cache test now asserts that releasing a request pin on the shared parent, which holds only descendant request pins, is an error. `pin_with` and `unpin_with` fetch the pinned node once for both direct counters, and `unpin_with` reads the checkpoint once for both release guards. The `debug_assert!` before the ancestor `request_pins` decrement is removed: debug builds already panic on `u64` underflow, and the test-only drift assertion in `Cache::stats` checks the request-pinned page count on every read. Pin behavior is unchanged; the request-pin guard is covered by a test. Validation: `cargo check --locked --manifest-path helios/Cargo.toml --features server` passed. `cargo test --locked --manifest-path helios/Cargo.toml --features runtime-core,server --lib -- server:: scheduler:: cache::` reported 809 passed, 1 failed (`messages_image_hints_end_after_expansion_for_text_and_segmented_templates`, which needs FLARE_TEST_TEMPLATE_DIR), 11 ignored, 852 filtered out. Signed-off-by: Wynn Drahorad Design-Signed-Off-By: enterprise/deepseek-v4.1-flash (omp agent); design-review=2e5b --- helios/src/cache.rs | 20 +++++++++----------- helios/src/cache/tests.rs | 1 + 2 files changed, 10 insertions(+), 11 deletions(-) diff --git a/helios/src/cache.rs b/helios/src/cache.rs index dcfb9e11b..f19e82c85 100644 --- a/helios/src/cache.rs +++ b/helios/src/cache.rs @@ -526,10 +526,9 @@ impl PrefixCheckpointCache { { return Err("checkpoint reference count exhausted".into()); } - self.nodes.get_mut(&id).unwrap().direct_pins += 1; - if request { - self.nodes.get_mut(&id).unwrap().direct_request_pins += 1; - } + let node = self.nodes.get_mut(&id).unwrap(); + node.direct_pins += 1; + node.direct_request_pins += request as u64; for ancestor in chain { let node = self.nodes.get_mut(&ancestor).unwrap(); node.pins += 1; @@ -544,22 +543,21 @@ impl PrefixCheckpointCache { } fn unpin_with(&mut self, id: CheckpointId, request: bool) -> Result<()> { - if request && self.checkpoint(id)?.direct_request_pins == 0 { + let checkpoint = self.checkpoint(id)?; + if request && checkpoint.direct_request_pins == 0 { return Err("checkpoint has no request pin to release".into()); } - if self.checkpoint(id)?.direct_pins == 0 { + if checkpoint.direct_pins == 0 { return Err("checkpoint has no direct pin to release".into()); } let chain = self.chain(id)?; - self.nodes.get_mut(&id).unwrap().direct_pins -= 1; - if request { - self.nodes.get_mut(&id).unwrap().direct_request_pins -= 1; - } + let node = self.nodes.get_mut(&id).unwrap(); + node.direct_pins -= 1; + node.direct_request_pins -= request as u64; for ancestor in chain { let node = self.nodes.get_mut(&ancestor).unwrap(); node.pins -= 1; if request { - debug_assert!(node.request_pins > 0); node.request_pins -= 1; if node.request_pins == 0 { self.request_pinned_pages -= node.tail_pages.len(); diff --git a/helios/src/cache/tests.rs b/helios/src/cache/tests.rs index eab4a156a..a9bd3c7be 100644 --- a/helios/src/cache/tests.rs +++ b/helios/src/cache/tests.rs @@ -365,6 +365,7 @@ fn branching_pins_ancestors_and_prevents_direct_ancestor_unpin() { assert_eq!(cache.checkpoint(parent).unwrap().pin_count(), 2); assert!(cache.unpin(parent).is_err()); assert_eq!(cache.checkpoint(parent).unwrap().pin_count(), 2); + assert!(cache.unpin_request(parent).is_err()); cache.unpin_request(left_id).unwrap(); assert_eq!(cache.checkpoint(parent).unwrap().pin_count(), 1); assert_eq!(cache.stats().request_pinned_pages, 2); From 10fc65ec82a137afb19e5381b46faf51eca58580 Mon Sep 17 00:00:00 2001 From: Wynn Drahorad Date: Sat, 28 Nov 2026 16:38:26 -0600 Subject: [PATCH 6/8] Tighten KV-page gauge prose and the gauge test The `flare_request_kv_pages` and `flare_reserved_kv_pages` descriptions named their population as "active requests" in code but "requests owning a model slot" in `docs/api.md`, and described reserved pages as "not yet written", although `commit` moves a reservation into a request's private pages before the GPU writes it. The `docs/api.md` paragraph also documented draining pages and publication cadence, which no other page gauge there describes. The `SchedulerStats` doc comments, the metric HELP text, and `docs/api.md` use "requests owning a model slot" and define reserved pages as pages set aside for growth that no request holds yet and that `flare_cached_kv_pages` does not count. The draining and cadence sentences are removed from `docs/api.md`. The copy-out retry key comment in `runtime.rs` is one line. The branching-request scheduler test inlines its page sums into the gauge assertions and drops a comment that restated them. Gauge values, assertions, and the documented usage ratio are unchanged. Validation: `cargo check --locked --manifest-path helios/Cargo.toml --features server` passed. `cargo test --locked --manifest-path helios/Cargo.toml --features runtime-core,server --lib -- server:: scheduler:: cache::` reported 809 passed, 1 failed (`messages_image_hints_end_after_expansion_for_text_and_segmented_templates`, which needs FLARE_TEST_TEMPLATE_DIR), 11 ignored, 852 filtered out. Signed-off-by: Wynn Drahorad Design-Signed-Off-By: enterprise/deepseek-v4.1-flash (omp agent); design-review=2e5b --- docs/api.md | 10 ++++------ helios/src/scheduler.rs | 4 ++-- helios/src/scheduler/tests.rs | 18 ++++++++---------- helios/src/server/metrics.rs | 4 ++-- helios/src/server/runtime.rs | 4 +--- 5 files changed, 17 insertions(+), 23 deletions(-) diff --git a/docs/api.md b/docs/api.md index b58b72603..5c31f48b2 100644 --- a/docs/api.md +++ b/docs/api.md @@ -299,12 +299,10 @@ uptime. model slot reference: their private pages plus the tail pages of the prefix checkpoints they pin, each page counted once even when several requests reference it. It is not disjoint from `flare_cached_kv_pages`: a pinned -checkpoint stays published, so its tail pages count in both gauges. Neither -gauge counts pages draining after a request retires, or a restore ticket's -in-flight pages. `flare_reserved_kv_pages` counts pages reserved for those -requests' growth that are neither free, cached, nor written yet. The worker -publishes both when a scheduler pass makes progress, and again at startup and -after the shutdown drain; a scrape reads the last published values. +checkpoint stays published, so its tail pages count in both gauges. +`flare_reserved_kv_pages` counts pages set aside for those requests' growth +that no request holds a page for yet, and which are not counted in +`flare_cached_kv_pages`. `(flare_request_kv_pages + flare_reserved_kv_pages)` over `flare_total_kv_pages` is the share comparable to vLLM's `kv_cache_usage_perc`; idle cached prefixes and snapshot backing pages are outside it. diff --git a/helios/src/scheduler.rs b/helios/src/scheduler.rs index 7379c2963..ab5448932 100644 --- a/helios/src/scheduler.rs +++ b/helios/src/scheduler.rs @@ -126,9 +126,9 @@ pub struct SchedulerStats { pub snapshot_backing_pages: usize, pub cached_checkpoints: usize, pub cached_pages: usize, - /// Distinct KV pages active requests reference: private pages plus their pinned prefix-checkpoint pages. + /// Distinct KV pages requests owning a model slot reference: private pages plus their pinned prefix-checkpoint pages. pub request_pages: usize, - /// KV pages reserved for active requests' growth and not yet written. + /// KV pages set aside for those requests' growth that no request holds a page for yet. pub reserved_pages: usize, pub host_lookups: u64, pub host_hits: u64, diff --git a/helios/src/scheduler/tests.rs b/helios/src/scheduler/tests.rs index 203e4c3f1..35cd5f613 100644 --- a/helios/src/scheduler/tests.rs +++ b/helios/src/scheduler/tests.rs @@ -1023,17 +1023,15 @@ fn branching_requests_own_distinct_tail_pages_and_terminal_snapshots() { assert!(!first_request.shared_pages.is_empty()); assert_ne!(first_request.private_pages, second_request.private_pages); assert!(first_request.terminal_snapshot.is_none() && second_request.terminal_snapshot.is_none()); - let (shared, private, reserved) = ( - first_request.shared_pages.len(), - first_request.private_pages.len() + second_request.private_pages.len(), - first_request.reserved_pages.len() + second_request.reserved_pages.len(), - ); let stats = scheduler.stats(); - assert_eq!(stats.request_pages, shared + private); - assert_eq!(stats.reserved_pages, reserved); - // Cancellation retires both requests: the shared page is counted once while - // both are live, and retirement releases the request pins before the slots - // empty, so both gauges drain. + assert_eq!( + stats.request_pages, + first_request.shared_pages.len() + first_request.private_pages.len() + second_request.private_pages.len() + ); + assert_eq!( + stats.reserved_pages, + first_request.reserved_pages.len() + second_request.reserved_pages.len() + ); scheduler.cancel(first).unwrap(); scheduler.cancel(second).unwrap(); let stats = scheduler.stats(); diff --git a/helios/src/server/metrics.rs b/helios/src/server/metrics.rs index 06d6ae6d4..84ca669bb 100644 --- a/helios/src/server/metrics.rs +++ b/helios/src/server/metrics.rs @@ -1302,12 +1302,12 @@ impl Metrics { ), ( "request_kv_pages", - "Distinct KV pages referenced by active requests: private pages plus pinned prefix-checkpoint pages, each counted once.", + "Distinct KV pages referenced by requests owning a model slot: private pages plus pinned prefix-checkpoint pages, each counted once.", &self.request_pages, ), ( "reserved_kv_pages", - "KV pages reserved for active requests' growth and not yet written.", + "KV pages set aside for the growth of requests owning a model slot that no request holds a page for yet, and which are not counted in flare_cached_kv_pages.", &self.reserved_pages, ), ( diff --git a/helios/src/server/runtime.rs b/helios/src/server/runtime.rs index 23eca0509..2f857c48f 100644 --- a/helios/src/server/runtime.rs +++ b/helios/src/server/runtime.rs @@ -2776,9 +2776,7 @@ impl<'a> Worker<'a> { } let resources = CopyoutResources { completed: self.scheduler.completed(), - // Decode moves request and reserved page counts without moving cache - // resources; zeroing them keeps deferred host copy-outs keyed on the - // cache state they actually depend on. + // Decode moves the page counts without moving cache resources. scheduler: crate::scheduler::SchedulerStats { request_pages: 0, reserved_pages: 0, From 9a7da18e9501825ce5a62d1244d345cfa618887e Mon Sep 17 00:00:00 2001 From: Wynn Drahorad Date: Sun, 29 Nov 2026 17:11:20 -0600 Subject: [PATCH 7/8] Reject cross-kind pin releases and validate before publication A plain `unpin` released a checkpoint whose only direct pin was a request pin: it decremented the direct and total pin counts while leaving the request pin and the checkpoint's pages in `request_pinned_pages`. The checkpoint then looked unreferenced to eviction and compaction with `request_pins > pins`, so retiring it leaked the request-pinned page count and compacting it broke the parent/heir request-pin equality. Separately, `RequestScheduler::retire` released the request pin after `publish_with_retention` had moved the terminal snapshot and tail pages into the cache, so a release error left the cache owning resources the caller also still owned; the comment there claimed the request stayed inspectable, although publication had ended that. A plain release now requires a direct pin beyond the checkpoint's request pins, and the request-release guard is shared through `ensure_request_pin`. `retire` validates the request pin before `publish_with_retention`, so a missing pin is reported while the request still owns everything, and `retire_leaf` asserts that a retired leaf holds no pins, request pins included. The cache tests reject a plain unpin on a request-only checkpoint, a request unpin on an ancestor holding only descendant request pins, and a request unpin on a plain-only pin, and assert that every rejection leaves `CacheStats` and the pin counts unchanged. A scheduler test drops a restore request's prefix pin and asserts that `retire` fails before recording or publishing anything, then succeeds once the pin is restored. Cross-kind releases no longer unbalance the request-pin counters, and a request-pin release can no longer fail after publication has committed the request's resources to the cache. `cargo test --locked --release --manifest-path helios/Cargo.toml --features runtime-core,server` passed the cache and scheduler tests, including the three new tests. The full suite reported 1725 passed, 14 failed, 46 ignored, 0 filtered out in 16.76 s. Six failures need `FLARE_TEST_TEMPLATE_DIR`, which this machine has not prepared, including `server::protocol::decode::native_prompt_tests::messages_image_hints_end_after_expansion_for_text_and_segmented_templates`. Eight `engram` tests failed with `register Engram staging: Cannot allocate memory (os error 12)` while the suite ran in parallel on a host with 6 GiB free; `engram::reader::tests::equal_offsets_in_separate_files_never_coalesce` passes when run alone. Signed-off-by: Wynn Drahorad Design-Signed-Off-By: enterprise/deepseek-v4.1-flash (omp agent); design-review=2e5b --- helios/src/cache.rs | 28 ++++++++++++++++++++++------ helios/src/cache/tests.rs | 31 +++++++++++++++++++++++++++++++ helios/src/scheduler.rs | 10 ++++++++-- helios/src/scheduler/tests.rs | 22 ++++++++++++++++++++++ 4 files changed, 83 insertions(+), 8 deletions(-) diff --git a/helios/src/cache.rs b/helios/src/cache.rs index f19e82c85..96803e51a 100644 --- a/helios/src/cache.rs +++ b/helios/src/cache.rs @@ -543,12 +543,15 @@ impl PrefixCheckpointCache { } fn unpin_with(&mut self, id: CheckpointId, request: bool) -> Result<()> { - let checkpoint = self.checkpoint(id)?; - if request && checkpoint.direct_request_pins == 0 { - return Err("checkpoint has no request pin to release".into()); - } - if checkpoint.direct_pins == 0 { - return Err("checkpoint has no direct pin to release".into()); + if request { + self.ensure_request_pin(id)?; + } else { + let checkpoint = self.checkpoint(id)?; + // Request pins are a subset of direct pins, so a plain release needs + // a direct pin that is not a request pin. + if checkpoint.direct_pins == checkpoint.direct_request_pins { + return Err("checkpoint has no direct pin to release".into()); + } } let chain = self.chain(id)?; let node = self.nodes.get_mut(&id).unwrap(); @@ -586,6 +589,16 @@ impl PrefixCheckpointCache { self.unpin_with(id, true) } + /// Fails unless `id` holds a direct request pin, so a caller can settle a + /// release before committing state that a failed release would leave + /// half-applied. + pub(crate) fn ensure_request_pin(&self, id: CheckpointId) -> Result<()> { + if self.checkpoint(id)?.direct_request_pins == 0 { + return Err("checkpoint has no request pin to release".into()); + } + Ok(()) + } + /// Record GPU reads before submission; pinning alone does not track GPU use. pub fn mark_used(&mut self, id: CheckpointId, through: Fence) -> Result<()> { if !self.checkpoint(id)?.ready { @@ -600,6 +613,9 @@ impl PrefixCheckpointCache { fn retire_leaf(&mut self, id: CheckpointId) -> RetiredResources { let node = self.nodes.remove(&id).unwrap(); + // Retirement requires an unreferenced leaf, and a request pin is a pin. + debug_assert_eq!(node.pins, 0); + debug_assert_eq!(node.request_pins, 0); self.uncount(&node); self.prefixes.remove(&node.prefix); if let Some(parent) = node.parent { diff --git a/helios/src/cache/tests.rs b/helios/src/cache/tests.rs index a9bd3c7be..c89b26566 100644 --- a/helios/src/cache/tests.rs +++ b/helios/src/cache/tests.rs @@ -403,6 +403,37 @@ fn branching_pins_ancestors_and_prevents_direct_ancestor_unpin() { ); } +#[test] +fn cross_kind_unpins_are_rejected_without_changing_counts() { + let mut cache = cache(); + let root = history(1, 0); + let mut child = root.clone(); + child.append(&[888; 64], &[]).unwrap(); + let parent = created(&mut cache, &root, &proposal(None, 64, &[0], 0, 0)); + let child_id = created(&mut cache, &child, &proposal(Some(parent), 128, &[1], 1, 0)); + // Request pins are direct pins, so a plain unpin must not release one. + cache.pin_request(child_id).unwrap(); + let pinned = cache.stats(); + assert!(cache.unpin(child_id).is_err()); + assert_eq!(cache.stats(), pinned); + assert_eq!(cache.checkpoint(child_id).unwrap().pin_count(), 1); + assert_eq!(cache.stats().request_pinned_pages, 2); + // A request unpin cannot reach an ancestor that holds no direct request pin. + assert!(cache.unpin_request(parent).is_err()); + assert_eq!(cache.stats(), pinned); + assert_eq!(cache.checkpoint(parent).unwrap().pin_count(), 1); + cache.unpin_request(child_id).unwrap(); + assert_eq!(cache.stats().request_pinned_pages, 0); + // A request unpin cannot release a plain pin either. + cache.pin(child_id).unwrap(); + let plain = cache.stats(); + assert!(cache.unpin_request(child_id).is_err()); + assert_eq!(cache.stats(), plain); + assert_eq!(cache.checkpoint(child_id).unwrap().pin_count(), 1); + cache.unpin(child_id).unwrap(); + assert_eq!(cache.checkpoint(child_id).unwrap().pin_count(), 0); +} + #[test] fn ancestor_compaction_preserves_pins_page_order_and_pending_gpu_reads() { let mut cache = cache(); diff --git a/helios/src/scheduler.rs b/helios/src/scheduler.rs index ab5448932..4a43d41fe 100644 --- a/helios/src/scheduler.rs +++ b/helios/src/scheduler.rs @@ -3043,6 +3043,12 @@ impl RequestScheduler { || request.generated_tokens > 0); let prefix = request.prefix; let failed = request.error_code.is_some(); + // Publication moves the terminal snapshot and tail pages into the cache and + // updates recency. Settle the request-pin release first, while a failure + // still leaves the request owning everything. + if let Some(prefix) = prefix { + self.cache.ensure_request_pin(prefix)?; + } let result = match &publication { Some(proposal) => { let history = request.history.clone(); @@ -3071,12 +3077,12 @@ impl RequestScheduler { self.reused.push(self.cache.checkpoint(id)?.prefix()); } } - // Release the request pin before the slot is emptied, so a failed release - // leaves the request inspectable instead of dropping it silently. if let Some(prefix) = prefix { if !publish || failed { self.record_publication(prefix); } + // Publication does not touch the prefix's request pin, so the guard + // above still holds. self.cache.unpin_request(prefix)?; } let mut request = self.slots[handle.slot as usize].request.take().unwrap(); diff --git a/helios/src/scheduler/tests.rs b/helios/src/scheduler/tests.rs index 35cd5f613..3895da24d 100644 --- a/helios/src/scheduler/tests.rs +++ b/helios/src/scheduler/tests.rs @@ -2591,6 +2591,28 @@ fn disabled_publication_tracking_keeps_gpu_reuse_without_allocating_a_notificati assert_eq!(scheduler.tickets.capacity(), 0); } +#[test] +fn retire_rejects_a_missing_request_pin_before_recording_the_retired_prefix() { + let mut scheduler = host_scheduler(1, 4, 2); + let checkpoint = seed_checkpoint(&mut scheduler, 1); + scheduler.take_publications(); + let hit = scheduler.admit(spec(64, 2)).unwrap(); + assert_eq!(scheduler.request(hit).unwrap().prefix(), Some(checkpoint)); + // Drop the pin the request took, so the release has nothing to settle. + scheduler.cache.unpin_request(checkpoint).unwrap(); + let before = scheduler.cache().stats(); + assert!(scheduler.cancel(hit).is_err()); + assert_eq!(scheduler.cache().stats(), before); + assert!(scheduler.take_publications().is_empty()); + // Restoring the pin lets the same request retire. + scheduler.cache.pin_request(checkpoint).unwrap(); + scheduler.cancel(hit).unwrap(); + assert_eq!( + scheduler.cache().checkpoint(checkpoint).unwrap().pin_count(), + 0 + ); +} + #[test] fn inbound_request_waits_unscheduled_then_restores_like_a_gpu_prefix_hit() { let mut scheduler = host_scheduler(2, 8, 4); From e65d1583308612003b7a9ca5720a4e2684d1272e Mon Sep 17 00:00:00 2001 From: Wynn Drahorad Date: Sun, 29 Nov 2026 17:11:24 -0600 Subject: [PATCH 8/8] Describe reserved KV pages by the writes they cover `flare_reserved_kv_pages` was documented as pages set aside for requests' "growth", but admission also reserves the unprefilled prompt pages, the decode budget, and the draft pages. The `docs/api.md` paragraph, the `SchedulerStats` doc comment, and the metric HELP text therefore described a narrower population than the gauge counts. All three now describe the gauge as pages set aside for requests owning a model slot's upcoming prefill, decode, and draft writes: pages no request holds yet and that `flare_cached_kv_pages` does not count. Gauge names, values, and their usage ratio are unchanged. The documented meaning matches what `admission_pages` reserves, so a reader comparing the gauge with vLLM's `kv_cache_usage_perc` sees which upcoming writes the reservation covers. `cargo test --locked --release --manifest-path helios/Cargo.toml --features runtime-core,server --lib -- server::` passed, including the gauge test that asserts the `flare_reserved_kv_pages` value. Signed-off-by: Wynn Drahorad Design-Signed-Off-By: enterprise/deepseek-v4.1-flash (omp agent); design-review=2e5b --- docs/api.md | 6 +++--- helios/src/scheduler.rs | 3 ++- helios/src/server/metrics.rs | 2 +- 3 files changed, 6 insertions(+), 5 deletions(-) diff --git a/docs/api.md b/docs/api.md index 5c31f48b2..25dd2db37 100644 --- a/docs/api.md +++ b/docs/api.md @@ -300,9 +300,9 @@ model slot reference: their private pages plus the tail pages of the prefix checkpoints they pin, each page counted once even when several requests reference it. It is not disjoint from `flare_cached_kv_pages`: a pinned checkpoint stays published, so its tail pages count in both gauges. -`flare_reserved_kv_pages` counts pages set aside for those requests' growth -that no request holds a page for yet, and which are not counted in -`flare_cached_kv_pages`. +`flare_reserved_kv_pages` counts pages set aside for those requests' upcoming +prefill, decode, and draft writes that no request holds a page for yet, and +which are not counted in `flare_cached_kv_pages`. `(flare_request_kv_pages + flare_reserved_kv_pages)` over `flare_total_kv_pages` is the share comparable to vLLM's `kv_cache_usage_perc`; idle cached prefixes and snapshot backing pages are outside it. diff --git a/helios/src/scheduler.rs b/helios/src/scheduler.rs index 4a43d41fe..3555e51bc 100644 --- a/helios/src/scheduler.rs +++ b/helios/src/scheduler.rs @@ -128,7 +128,8 @@ pub struct SchedulerStats { pub cached_pages: usize, /// Distinct KV pages requests owning a model slot reference: private pages plus their pinned prefix-checkpoint pages. pub request_pages: usize, - /// KV pages set aside for those requests' growth that no request holds a page for yet. + /// KV pages set aside for those requests' upcoming prefill, decode, and draft + /// writes that no request holds a page for yet. pub reserved_pages: usize, pub host_lookups: u64, pub host_hits: u64, diff --git a/helios/src/server/metrics.rs b/helios/src/server/metrics.rs index 84ca669bb..4b60b8cbf 100644 --- a/helios/src/server/metrics.rs +++ b/helios/src/server/metrics.rs @@ -1307,7 +1307,7 @@ impl Metrics { ), ( "reserved_kv_pages", - "KV pages set aside for the growth of requests owning a model slot that no request holds a page for yet, and which are not counted in flare_cached_kv_pages.", + "KV pages set aside for upcoming prefill, decode, and draft writes of requests owning a model slot: not yet held by a request and not counted in flare_cached_kv_pages.", &self.reserved_pages, ), (