#[cfg(any(target_os = "linux", windows, target_os = "freebsd", target_os = "macos"))]
fn main() {
use std::io::Write;
use std::time::Instant;
use subetha_cxc::kernel_async_ring::{open_for_async_read, KernelAsyncRing};
const N: usize = 64;
const BLOCK: usize = 512;
println!("=== KernelAsyncRing batched async-read E2E (io_uring / IoRing / aio) ===");
let path = std::env::temp_dir().join(format!("karing_demo_{}", std::process::id()));
{
let mut f = std::fs::File::create(&path).expect("create");
for i in 0..N {
let mut blk = [i as u8; BLOCK];
blk[..8].copy_from_slice(&(i as u64).to_le_bytes());
f.write_all(&blk).expect("write block");
}
f.flush().expect("flush");
}
println!("[init] wrote {N} blocks x {BLOCK} bytes = {} KiB", N * BLOCK / 1024);
let mut ring = match KernelAsyncRing::new((N as u32).next_power_of_two()) {
Ok(r) => r,
Err(e) => {
println!("kernel async ring unavailable: {e}");
#[cfg(target_os = "linux")]
println!(" needs a kernel with io_uring (5.1+); some containers disable it.");
#[cfg(windows)]
println!(" needs a Windows build with IoRing (Win11 21H2+).");
#[cfg(target_os = "freebsd")]
println!(" needs kqueue (always present); this is unexpected.");
std::fs::remove_file(&path).ok();
return;
}
};
let file = open_for_async_read(&path).expect("open_for_async_read");
println!("[init] kernel ring created, file opened for async I/O");
let mut bufs: Vec<Vec<u8>> = (0..N).map(|_| vec![0u8; BLOCK]).collect();
let mut got = [false; N];
let mut reaped = 0usize;
let mut next = 0usize;
let mut inflight = 0usize;
let mut waves = 0u32;
let deadline = Instant::now();
let t0 = Instant::now();
while reaped < N {
while next < N {
let ptr = bufs[next].as_mut_ptr();
let r = unsafe {
ring.prepare_read(&file, ptr, BLOCK as u32, (next * BLOCK) as u64, next as u64)
};
match r {
Ok(()) => {
inflight += 1;
next += 1;
}
Err(e) if e.kind() == std::io::ErrorKind::WouldBlock => break,
Err(e) => panic!("prepare_read: {e}"),
}
}
assert!(inflight > 0, "no in-flight ops but {reaped}/{N} reaped");
ring.submit_and_wait(1).expect("submit_and_wait");
waves += 1;
while let Some(c) = ring.reap() {
let idx = c.user_data as usize;
let n = c.bytes.expect("read op succeeded");
assert_eq!(n, BLOCK, "block {idx}: short read {n}");
let blk = &bufs[idx];
let stamp = u64::from_le_bytes(blk[..8].try_into().unwrap());
assert_eq!(stamp, idx as u64, "block {idx}: wrong stamp {stamp}");
assert_eq!(blk[BLOCK - 1], idx as u8, "block {idx}: wrong fill");
assert!(!got[idx], "block {idx}: duplicate completion");
got[idx] = true;
reaped += 1;
inflight -= 1;
}
assert!(deadline.elapsed().as_secs() < 30, "timed out reaping: {reaped}/{N}");
}
let elapsed = t0.elapsed();
println!("[submit] {reaped} ops completed across {waves} submit/drain wave(s)");
drop(file);
std::fs::remove_file(&path).ok();
assert!(got.iter().all(|&b| b), "every block completed exactly once");
println!();
println!("=== Result ===");
println!(" blocks: {N} (all completed exactly once, in-tag verified)");
println!(" bytes: {} KiB via {N} kernel async reads", N * BLOCK / 1024);
println!(" elapsed: {elapsed:?}");
println!(" integrity: PASS (batched submit -> kernel -> completion ring)");
}
#[cfg(not(any(target_os = "linux", windows, target_os = "freebsd", target_os = "macos")))]
fn main() {
eprintln!("kernel_async_ring_demo needs io_uring (Linux), IoRing (Windows), \
or POSIX aio (FreeBSD / macOS).");
}