From 521768b5b8d294fddecd00a3983ea5a46234bf63 Mon Sep 17 00:00:00 2001 From: Tyler Smith <74526874+tylertms@users.noreply.github.com> Date: Thu, 12 Nov 2026 08:27:42 -0500 Subject: [PATCH] perf(storage): take a busy core's pending host disk completion signal at its device release and hand the step to a PE that sleeps waiting for an interrupt --- crates/stela-boot-core/src/staged_disk.rs | 24 +++++++ .../stela-boot-core/src/staged_disk/tests.rs | 12 ++++ crates/stela-boot/src/engine/parallel.rs | 4 ++ crates/stela-boot/src/engine/run.rs | 14 ++-- crates/stela-boot/src/engine/storage.rs | 27 ++++++++ .../src/machine/engine/parallel.rs | 10 +++ .../machine/engine/parallel/shared_devices.rs | 16 +++++ .../src/machine/engine/parallel/tests.rs | 66 +++++++++++++++++++ 8 files changed, 168 insertions(+), 5 deletions(-) diff --git a/crates/stela-boot-core/src/staged_disk.rs b/crates/stela-boot-core/src/staged_disk.rs index d9be617e67..43565dc580 100644 --- a/crates/stela-boot-core/src/staged_disk.rs +++ b/crates/stela-boot-core/src/staged_disk.rs @@ -478,6 +478,30 @@ pub fn signalled(doorbells: u64, seen: u64, owed: bool, work: bool, taken: bool) (doorbells != seen || owed) && work && !taken } +/// The completion signals a core that runs guest code handed to a sleeping +/// core: each doorbell count once. +#[derive(Debug, Default)] +pub struct SignalHandOff { + /// The doorbell count of the last signal handed on. + handed: AtomicU64, +} + +impl SignalHandOff { + pub const fn new() -> Self { + Self { + handed: AtomicU64::new(0), + } + } + + /// Whether a release hands on the signal behind doorbell count + /// `doorbells`: no step has seen it (`seen`, the count at the last + /// step), no release handed it on yet, and no core is inside the host + /// drivers (`taken`), whose step looks at the disk itself. + pub fn take(&self, doorbells: u64, seen: u64, taken: bool) -> bool { + doorbells != seen && !taken && self.handed.swap(doorbells, Ordering::AcqRel) != doorbells + } +} + impl BlockDevice for StagedDisk { fn identity(&self) -> &Identity { &self.identity diff --git a/crates/stela-boot-core/src/staged_disk/tests.rs b/crates/stela-boot-core/src/staged_disk/tests.rs index 577ae16607..373bb346ac 100644 --- a/crates/stela-boot-core/src/staged_disk/tests.rs +++ b/crates/stela-boot-core/src/staged_disk/tests.rs @@ -814,3 +814,15 @@ fn a_waiting_core_steps_for_a_new_doorbell_only_with_disk_work() { assert!(signalled(4, 4, true, true, false)); assert!(!signalled(4, 4, true, true, true)); } + +/// A busy core hands each completion signal on once, only while no step has +/// seen it and nobody is in the host drivers. +#[test] +fn a_busy_core_hands_each_completion_signal_on_once() { + let handed = SignalHandOff::new(); + assert!(!handed.take(4, 4, false), "seen by a step"); + assert!(!handed.take(5, 4, true), "the drivers' holder looks itself"); + assert!(handed.take(5, 4, false)); + assert!(!handed.take(5, 4, false), "handed on already"); + assert!(handed.take(6, 4, false), "a later signal"); +} diff --git a/crates/stela-boot/src/engine/parallel.rs b/crates/stela-boot/src/engine/parallel.rs index 763e904330..5246f113c5 100644 --- a/crates/stela-boot/src/engine/parallel.rs +++ b/crates/stela-boot/src/engine/parallel.rs @@ -78,6 +78,10 @@ impl Kick for Doorbells { fn release_due(&self) -> bool { super::storage::release_due() } + + fn signal_due(&self) -> bool { + super::storage::signal_due() + } } /// What every PE's machine shares with the home machine's settings. diff --git a/crates/stela-boot/src/engine/run.rs b/crates/stela-boot/src/engine/run.rs index 9d958a76e9..0550209357 100644 --- a/crates/stela-boot/src/engine/run.rs +++ b/crates/stela-boot/src/engine/run.rs @@ -189,8 +189,10 @@ fn compression_report(compression: &[step_timing::Compression; 2], tsc_hz: u64) each(operation.write), )); } - output::arguments(format_args!("{line} -")); + output::arguments(format_args!( + "{line} +" + )); } /// The exits of `exits` past the first `shown`, summed, if any. @@ -1897,17 +1899,19 @@ impl Heartbeat { } // By call since the last beat: the releases that did the host's // work (a staged disk's step) on the calling core and its - // microseconds there, and those that handed it to a sleeping PE. - let mut releases = String::from("DEVICE RELEASES stepped/us/handed"); + // microseconds there, those that handed it to a sleeping PE, and + // those that handed it a host disk's completion signal. + let mut releases = String::from("DEVICE RELEASES stepped/us/handed/signals"); for (index, call) in Call::ALL.iter().enumerate() { let (now, then) = (×[index], &self.lock_times[index]); releases.push_str(&format!( - " {}={}/{}/{}", + " {}={}/{}/{}/{}", call.name(), now.stepped.wrapping_sub(then.stepped), u128::from(now.step.wrapping_sub(then.step)) * 1_000_000 / u128::from(host::COUNTER_HZ), now.handed.wrapping_sub(then.handed), + now.signals.wrapping_sub(then.signals), )); } self.lock_times = times; diff --git a/crates/stela-boot/src/engine/storage.rs b/crates/stela-boot/src/engine/storage.rs index aa1f2c8036..564a218d40 100644 --- a/crates/stela-boot/src/engine/storage.rs +++ b/crates/stela-boot/src/engine/storage.rs @@ -1114,6 +1114,33 @@ pub(super) fn signalled() -> bool { ) } +/// The doorbell count of the last completion signal a release handed on +/// ([`signal_due`]). +static HANDED_SIGNALS: staged_disk::SignalHandOff = staged_disk::SignalHandOff::new(); + +/// After a device call released the devices' lock: while a staged disk has +/// commands on its host disk, take the interrupts this core held pending +/// while its guest ran, a host disk's completion signal among them, and +/// whether one came that a sleeping PE should take ([`signalled`]), once per +/// signal. Its guest goes on; the PE's host service steps the disk. +pub(super) fn signal_due() -> bool { + if !staged_workers().any(DiskWorker::work) + || super::host::in_interrupt() + || host_drivers_held_here() + { + return false; + } + // SAFETY: outside any handler and the host drivers, holding no lock a + // handler takes; the handlers only record what came. The NOP takes + // STI's shadow, so pending interrupts run before the CLI. + unsafe { asm!("sti", "nop", "cli", options(nomem, nostack)) }; + HANDED_SIGNALS.take( + super::host::doorbells(), + SEEN_DOORBELLS.load(Ordering::Relaxed), + host_drivers_taken(), + ) +} + fn staged_workers() -> impl Iterator { // SAFETY: `stage` stores only leaked workers, which live for the boot. STAGED diff --git a/crates/stela-engine/src/machine/engine/parallel.rs b/crates/stela-engine/src/machine/engine/parallel.rs index 8bde66ef0c..86ec702275 100644 --- a/crates/stela-engine/src/machine/engine/parallel.rs +++ b/crates/stela-engine/src/machine/engine/parallel.rs @@ -81,6 +81,16 @@ pub trait Kick: Send + Sync { fn release_due(&self) -> bool { false } + /// Runs on the processor that just released the devices' lock: whether + /// a host interrupt it takes now brought host work no one has been told + /// of (a host disk's completion signal, which a core running guest code + /// holds pending until it opens an interrupt window). The release then + /// hands that work to a PE that sleeps waiting for an interrupt + /// ([`Hub::hand_off_service`]); with none asleep it waits for the + /// host's own service between slices. True once for each signal. + fn signal_due(&self) -> bool { + false + } } /// The halt-poll after a wait that took `blocked` counts, from `poll`, at diff --git a/crates/stela-engine/src/machine/engine/parallel/shared_devices.rs b/crates/stela-engine/src/machine/engine/parallel/shared_devices.rs index 977061a121..e21e9037df 100644 --- a/crates/stela-engine/src/machine/engine/parallel/shared_devices.rs +++ b/crates/stela-engine/src/machine/engine/parallel/shared_devices.rs @@ -26,6 +26,9 @@ pub(super) struct Board { pub(super) stepped: [AtomicU64; Call::ALL.len()], pub(super) step_ticks: [AtomicU64; Call::ALL.len()], pub(super) handed: [AtomicU64; Call::ALL.len()], + /// By [`Call`], the releases that handed a host interrupt's work + /// ([`Kick::signal_due`]) to a sleeping PE. + pub(super) signals: [AtomicU64; Call::ALL.len()], /// By PE, the ticks of host work its releases did on its core, which a /// device write's settle reports apart ([`super::super::WriteSettled`]). pub(super) released: Box<[AtomicU64]>, @@ -408,8 +411,10 @@ impl Drop for Held<'_, D> { // host service, and goes on with its guest; with none asleep it does // the work itself, as before. let hub = &self.board.hub; + let mut handed = false; if hub.serves_released(self.pe) && hub.kick.release_due() { if hub.hand_off_service(self.pe) { + handed = true; self.board.handed[call].fetch_add(1, Ordering::Relaxed); } else { let start = hub.ticks(); @@ -425,6 +430,13 @@ impl Drop for Held<'_, D> { } } } + // A host interrupt this core held pending while its guest ran (a + // host disk's completion signal) goes to a PE that sleeps waiting + // for an interrupt, which takes it at its host service, rather than + // waiting for this core's slice end. Nothing to do here otherwise. + if !handed && hub.kick.signal_due() && hub.hand_off_service(self.pe) { + self.board.signals[call].fetch_add(1, Ordering::Relaxed); + } } } @@ -450,6 +462,8 @@ pub struct LockTimes { pub stepped: u64, pub step: u64, pub handed: u64, + /// Releases that handed a host interrupt's work to a sleeping PE. + pub signals: u64, } /// The board's devices, shared by every PE's machine. @@ -579,6 +593,7 @@ impl SharedDevices { stepped: Default::default(), step_ticks: Default::default(), handed: Default::default(), + signals: Default::default(), released: (0..pes).map(|_| AtomicU64::new(0)).collect(), now: AtomicU64::new(0), pins: (0..pes).map(|_| AtomicU8::new(UNLATCHED)).collect(), @@ -705,6 +720,7 @@ impl SharedDevices { stepped: self.board.stepped[call].load(Ordering::Relaxed), step: self.board.step_ticks[call].load(Ordering::Relaxed), handed: self.board.handed[call].load(Ordering::Relaxed), + signals: self.board.signals[call].load(Ordering::Relaxed), longest: self.board.longest[call].swap(0, Ordering::Relaxed), }) } diff --git a/crates/stela-engine/src/machine/engine/parallel/tests.rs b/crates/stela-engine/src/machine/engine/parallel/tests.rs index 0f25755923..b2b225a3a9 100644 --- a/crates/stela-engine/src/machine/engine/parallel/tests.rs +++ b/crates/stela-engine/src/machine/engine/parallel/tests.rs @@ -3334,6 +3334,72 @@ fn released_host_work_goes_to_a_pe_that_sleeps_waiting_for_an_interrupt() { assert_eq!(releases(&home), (1, 1), "counted as handed off"); } +/// A host interrupt a busy PE took at its release (a host disk's completion +/// signal) wakes a PE that sleeps waiting for an interrupt, at that release +/// on the model clock rather than at the busy PE's slice end; with none +/// asleep the busy PE leaves it to the host's service, and work its own +/// release handed on already is not handed twice. +#[test] +fn a_busy_pes_completion_signal_wakes_a_sleeping_pe_at_its_release() { + #[derive(Default)] + struct Signals { + kicked: Mutex>, + signal: AtomicBool, + due: AtomicBool, + now: AtomicU64, + } + struct Signalled(Arc); + impl Kick for Signalled { + fn kick(&self, pe: usize) { + let now = self.0.now.load(Ordering::SeqCst); + self.0.kicked.lock().unwrap().push((pe, now)); + } + fn ticks(&self) -> u64 { + self.0.now.load(Ordering::SeqCst) + } + fn release_due(&self) -> bool { + self.0.due.load(Ordering::SeqCst) + } + fn signal_due(&self) -> bool { + self.0.signal.swap(false, Ordering::SeqCst) + } + } + let signals = Arc::new(Signals::default()); + let hub = Hub::new(3, Signalled(Arc::clone(&signals))); + for pe in 0..3 { + hub.set_live(pe, true); + } + let home = SharedDevices::new(Pending::default(), Arc::clone(&hub)); + let mut worker = home.worker(); + let handed = |home: &SharedDevices| { + let times = home.lock_times()[Call::Access as usize]; + (times.handed, times.signals) + }; + // PE 1 runs its guest; nobody sleeps: the signal waits for the service. + signals.now.store(1_000, Ordering::SeqCst); + signals.signal.store(true, Ordering::SeqCst); + assert_eq!(worker.read(1, 0x40, 8, 1_000), Ok(0)); + assert!(signals.kicked.lock().unwrap().is_empty()); + assert!(!hub.service.load(Ordering::SeqCst)); + // PE 2 sleeps waiting for an interrupt: the next signal wakes it at the + // release that took it, at 2_500, not at PE 1's slice end. + hub.inboxes[2].sleeping.store(true, Ordering::SeqCst); + hub.inboxes[2].serves.store(true, Ordering::SeqCst); + signals.now.store(2_500, Ordering::SeqCst); + signals.signal.store(true, Ordering::SeqCst); + assert_eq!(worker.read(1, 0x40, 8, 2_500), Ok(0)); + assert_eq!(*signals.kicked.lock().unwrap(), [(2, 2_500)]); + assert!(hub.take_service()); + assert_eq!(handed(&home), (0, 1)); + // A release that handed its own work on already wakes nobody twice. + hub.inboxes[1].outside.store(true, Ordering::SeqCst); + signals.due.store(true, Ordering::SeqCst); + signals.signal.store(true, Ordering::SeqCst); + assert_eq!(worker.read(1, 0x40, 8, 3_000), Ok(0)); + assert_eq!(signals.kicked.lock().unwrap().len(), 2); + assert_eq!(handed(&home), (1, 1)); +} + /// A core whose halt waits on a doorbell never polls before it halts; any /// other polls as Linux's haltpoll governor fits it, at most `most`. #[test]