aien-sovereign-core-mv
AienOS host runtime: inference scheduling, paged KV state, the aien CLI and Spark verification tooling. Its inference engine is in alpha testing (DGX Spark GB10); Omega is the implementation destination.
Brought by drakestapleton
aien-sovereign-core-mv / crates / aien-scheduler / tests / dual_observer_overhead_bench.rs
| 1 | // SPDX-License-Identifier: Apache-2.0 |
| 2 | // Copyright 2026 Drake Stapleton (AIEN / aien-dev) |
| 3 | // |
| 4 | // Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | // you may not use this file except in compliance with the License. |
| 6 | // You may obtain a copy of the License at |
| 7 | // |
| 8 | // http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | // |
| 10 | // Unless required by applicable law or agreed to in writing, software |
| 11 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | // See the License for the specific language governing permissions and |
| 14 | // limitations under the License. |
| 15 | |
| 16 | //! Overhead of the `serve.admit_preempt` read-only tap (DUAL-3a). |
| 17 | //! |
| 18 | //! Manual benchmark, ignored by default (no timing assertion lives in CI): |
| 19 | //! |
| 20 | //! ```text |
| 21 | //! cargo test -p aien-scheduler --release --test dual_observer_overhead_bench -- --ignored --nocapture |
| 22 | //! ``` |
| 23 | //! |
| 24 | //! Three arms run the whole pre-registered workload set on fresh state: |
| 25 | //! |
| 26 | //! - `absent`: no observer installed (the production configuration); |
| 27 | //! - `counting`: `CountingObserver`, which serializes each record to count |
| 28 | //! bytes and keeps nothing (the cost of producing records); |
| 29 | //! - `recording`: `RecordingObserver`, which clones and keeps every record. |
| 30 | //! |
| 31 | //! Each arm runs `ROUNDS` rounds and reports the median wall time, decision |
| 32 | //! count, records emitted, serialized bytes emitted and heap allocations |
| 33 | //! (counted by a wrapping global allocator). The acceptance threshold is |
| 34 | //! pre-registered in the evidence receipt, not here. |
| 35 | |
| 36 | mod dual_common; |
| 37 | |
| 38 | use aien_scheduler::dual_observer::{CountingObserver, RecordingObserver}; |
| 39 | use dual_common::{build, run, scenarios}; |
| 40 | use std::alloc::{GlobalAlloc, Layout, System}; |
| 41 | use std::sync::atomic::{AtomicU64, Ordering::Relaxed}; |
| 42 | use std::sync::Arc; |
| 43 | use std::time::Instant; |
| 44 | |
| 45 | struct CountingAlloc; |
| 46 | |
| 47 | static ALLOCS: AtomicU64 = AtomicU64::new(0); |
| 48 | static ALLOC_BYTES: AtomicU64 = AtomicU64::new(0); |
| 49 | |
| 50 | unsafe impl GlobalAlloc for CountingAlloc { |
| 51 | unsafe fn alloc(&self, layout: Layout) -> *mut u8 { |
| 52 | ALLOCS.fetch_add(1, Relaxed); |
| 53 | ALLOC_BYTES.fetch_add(layout.size() as u64, Relaxed); |
| 54 | System.alloc(layout) |
| 55 | } |
| 56 | unsafe fn dealloc(&self, ptr: *mut u8, layout: Layout) { |
| 57 | System.dealloc(ptr, layout) |
| 58 | } |
| 59 | } |
| 60 | |
| 61 | #[global_allocator] |
| 62 | static GLOBAL: CountingAlloc = CountingAlloc; |
| 63 | |
| 64 | const ROUNDS: usize = 7; |
| 65 | |
| 66 | #[derive(Debug, Clone, Copy)] |
| 67 | struct Arm { |
| 68 | name: &'static str, |
| 69 | wall_ns_median: u128, |
| 70 | wall_ns_min: u128, |
| 71 | decisions: usize, |
| 72 | records: u64, |
| 73 | bytes: u64, |
| 74 | allocs: u64, |
| 75 | alloc_bytes: u64, |
| 76 | } |
| 77 | |
| 78 | fn median(v: &mut [u128]) -> u128 { |
| 79 | v.sort_unstable(); |
| 80 | v[v.len() / 2] |
| 81 | } |
| 82 | |
| 83 | fn arm(name: &'static str, mode: u8) -> Arm { |
| 84 | let set = scenarios(); |
| 85 | let mut walls = Vec::with_capacity(ROUNDS); |
| 86 | let mut decisions = 0; |
| 87 | let mut records = 0u64; |
| 88 | let mut bytes = 0u64; |
| 89 | let mut allocs = 0u64; |
| 90 | let mut alloc_bytes = 0u64; |
| 91 | for _ in 0..ROUNDS { |
| 92 | let mut round_decisions = 0; |
| 93 | let mut round_records = 0u64; |
| 94 | let mut round_bytes = 0u64; |
| 95 | let a0 = ALLOCS.load(Relaxed); |
| 96 | let b0 = ALLOC_BYTES.load(Relaxed); |
| 97 | let t0 = Instant::now(); |
| 98 | for s in &set { |
| 99 | let (mut scheduler, kv) = build(s); |
| 100 | let counting = Arc::new(CountingObserver::new()); |
| 101 | let recording = Arc::new(RecordingObserver::new()); |
| 102 | match mode { |
| 103 | 1 => scheduler.set_decision_observer(Some(counting.clone())), |
| 104 | 2 => scheduler.set_decision_observer(Some(recording.clone())), |
| 105 | _ => {} |
| 106 | } |
| 107 | let trace = run(s, &mut scheduler, &kv); |
| 108 | round_decisions += trace.decisions; |
| 109 | match mode { |
| 110 | 1 => { |
| 111 | round_records += counting.records(); |
| 112 | round_bytes += counting.bytes(); |
| 113 | } |
| 114 | 2 => { |
| 115 | let recs = recording.take(); |
| 116 | round_records += recs.len() as u64; |
| 117 | round_bytes += recs |
| 118 | .iter() |
| 119 | .map(|r| serde_json::to_vec(r).unwrap().len() as u64) |
| 120 | .sum::<u64>(); |
| 121 | } |
| 122 | _ => {} |
| 123 | } |
| 124 | } |
| 125 | let wall = t0.elapsed().as_nanos(); |
| 126 | let a1 = ALLOCS.load(Relaxed); |
| 127 | let b1 = ALLOC_BYTES.load(Relaxed); |
| 128 | walls.push(wall); |
| 129 | decisions = round_decisions; |
| 130 | records = round_records; |
| 131 | bytes = round_bytes; |
| 132 | allocs = a1 - a0; |
| 133 | alloc_bytes = b1 - b0; |
| 134 | } |
| 135 | Arm { |
| 136 | name, |
| 137 | wall_ns_median: median(&mut walls), |
| 138 | wall_ns_min: *walls.iter().min().unwrap(), |
| 139 | decisions, |
| 140 | records, |
| 141 | bytes, |
| 142 | allocs, |
| 143 | alloc_bytes, |
| 144 | } |
| 145 | } |
| 146 | |
| 147 | #[test] |
| 148 | #[ignore = "manual overhead benchmark; run with --ignored --nocapture in release"] |
| 149 | fn dual_observer_overhead() { |
| 150 | // Warm up once so the first arm does not pay for page faults alone. |
| 151 | let _ = arm("warmup", 0); |
| 152 | let absent = arm("absent", 0); |
| 153 | let counting = arm("counting", 1); |
| 154 | let recording = arm("recording", 2); |
| 155 | let absent2 = arm("absent_repeat", 0); |
| 156 | |
| 157 | println!("DUAL serve.admit_preempt observer overhead, {ROUNDS} rounds per arm, whole workload set per round"); |
| 158 | println!( |
| 159 | "{:<14} {:>14} {:>14} {:>10} {:>9} {:>12} {:>10} {:>12} {:>8}", |
| 160 | "arm", |
| 161 | "median_ms", |
| 162 | "min_ms", |
| 163 | "decisions", |
| 164 | "records", |
| 165 | "bytes", |
| 166 | "allocs", |
| 167 | "alloc_bytes", |
| 168 | "ratio" |
| 169 | ); |
| 170 | for a in [absent, counting, recording, absent2] { |
| 171 | println!( |
| 172 | "{:<14} {:>14.3} {:>14.3} {:>10} {:>9} {:>12} {:>10} {:>12} {:>8.3}", |
| 173 | a.name, |
| 174 | a.wall_ns_median as f64 / 1e6, |
| 175 | a.wall_ns_min as f64 / 1e6, |
| 176 | a.decisions, |
| 177 | a.records, |
| 178 | a.bytes, |
| 179 | a.allocs, |
| 180 | a.alloc_bytes, |
| 181 | a.wall_ns_median as f64 / absent.wall_ns_median as f64 |
| 182 | ); |
| 183 | } |
| 184 | // Deterministic facts (not timing): records equal decisions when a tap is on. |
| 185 | assert_eq!(counting.records, counting.decisions as u64); |
| 186 | assert_eq!(recording.records, recording.decisions as u64); |
| 187 | assert_eq!(absent.records, 0); |
| 188 | assert_eq!(absent.decisions, counting.decisions); |
| 189 | } |
| 190 | |
| 191 | /// Exploratory (NOT pre-registered) protocol added after the first run: arms |
| 192 | /// interleaved round by round so clock scaling and cache warmth hit every arm |
| 193 | /// alike, 50 rounds, medians. Reported in the receipt as post-hoc only; it |
| 194 | /// does not replace the pre-registered verdict above. |
| 195 | #[test] |
| 196 | #[ignore = "manual overhead benchmark (interleaved, exploratory); --ignored --nocapture in release"] |
| 197 | fn dual_observer_overhead_interleaved() { |
| 198 | const N: usize = 50; |
| 199 | let set = scenarios(); |
| 200 | let mut absent_w = Vec::with_capacity(N); |
| 201 | let mut counting_w = Vec::with_capacity(N); |
| 202 | let mut recording_w = Vec::with_capacity(N); |
| 203 | let mut absent_a = Vec::with_capacity(N); |
| 204 | let mut counting_a = Vec::with_capacity(N); |
| 205 | let mut recording_a = Vec::with_capacity(N); |
| 206 | let mut decisions = 0usize; |
| 207 | let mut records = 0u64; |
| 208 | let mut bytes = 0u64; |
| 209 | |
| 210 | let run_arm = |mode: u8, walls: &mut Vec<u128>, allocs: &mut Vec<u64>| { |
| 211 | let a0 = ALLOCS.load(Relaxed); |
| 212 | let t0 = Instant::now(); |
| 213 | let mut d = 0usize; |
| 214 | let mut r = 0u64; |
| 215 | let mut b = 0u64; |
| 216 | for s in &set { |
| 217 | let (mut scheduler, kv) = build(s); |
| 218 | let counting = Arc::new(CountingObserver::new()); |
| 219 | let recording = Arc::new(RecordingObserver::new()); |
| 220 | match mode { |
| 221 | 1 => scheduler.set_decision_observer(Some(counting.clone())), |
| 222 | 2 => scheduler.set_decision_observer(Some(recording.clone())), |
| 223 | _ => {} |
| 224 | } |
| 225 | let trace = run(s, &mut scheduler, &kv); |
| 226 | d += trace.decisions; |
| 227 | match mode { |
| 228 | 1 => { |
| 229 | r += counting.records(); |
| 230 | b += counting.bytes(); |
| 231 | } |
| 232 | 2 => { |
| 233 | let recs = recording.take(); |
| 234 | r += recs.len() as u64; |
| 235 | b += recs |
| 236 | .iter() |
| 237 | .map(|x| serde_json::to_vec(x).unwrap().len() as u64) |
| 238 | .sum::<u64>(); |
| 239 | } |
| 240 | _ => {} |
| 241 | } |
| 242 | } |
| 243 | walls.push(t0.elapsed().as_nanos()); |
| 244 | allocs.push(ALLOCS.load(Relaxed) - a0); |
| 245 | (d, r, b) |
| 246 | }; |
| 247 | |
| 248 | // Warm-up round for every arm, discarded. |
| 249 | let (mut w, mut a) = (Vec::new(), Vec::new()); |
| 250 | for mode in 0..3u8 { |
| 251 | run_arm(mode, &mut w, &mut a); |
| 252 | } |
| 253 | for _ in 0..N { |
| 254 | let (d, _, _) = run_arm(0, &mut absent_w, &mut absent_a); |
| 255 | decisions = d; |
| 256 | let (_, r, b) = run_arm(1, &mut counting_w, &mut counting_a); |
| 257 | records = r; |
| 258 | bytes = b; |
| 259 | run_arm(2, &mut recording_w, &mut recording_a); |
| 260 | } |
| 261 | let med = |v: &mut Vec<u128>| median(v); |
| 262 | let (ma, mc, mr) = ( |
| 263 | med(&mut absent_w), |
| 264 | med(&mut counting_w), |
| 265 | med(&mut recording_w), |
| 266 | ); |
| 267 | println!("DUAL serve.admit_preempt observer overhead, INTERLEAVED exploratory protocol, {N} rounds, median of per-round wall"); |
| 268 | println!( |
| 269 | "{:<10} {:>12} {:>12} {:>10} {:>9} {:>12} {:>10} {:>8}", |
| 270 | "arm", "median_ms", "min_ms", "decisions", "records", "bytes", "allocs", "ratio" |
| 271 | ); |
| 272 | for (name, w, al, rec, by) in [ |
| 273 | ("absent", &absent_w, &absent_a, 0u64, 0u64), |
| 274 | ("counting", &counting_w, &counting_a, records, bytes), |
| 275 | ("recording", &recording_w, &recording_a, records, bytes), |
| 276 | ] { |
| 277 | let m = { |
| 278 | let mut c = w.clone(); |
| 279 | median(&mut c) |
| 280 | }; |
| 281 | println!( |
| 282 | "{:<10} {:>12.3} {:>12.3} {:>10} {:>9} {:>12} {:>10} {:>8.3}", |
| 283 | name, |
| 284 | m as f64 / 1e6, |
| 285 | *w.iter().min().unwrap() as f64 / 1e6, |
| 286 | decisions, |
| 287 | rec, |
| 288 | by, |
| 289 | al[al.len() / 2], |
| 290 | m as f64 / ma as f64 |
| 291 | ); |
| 292 | } |
| 293 | let _ = (mc, mr); |
| 294 | } |