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

This commit is contained in:
Tyler Smith committed 2026-11-12 10:42:08 -05:00
1 parent 0f95d33907
commit 521768b5b8
8 files changed
+168 -5

No files matched your search

+24
View File
@@ -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
@@ -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");
}
+4
View File
@@ -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.
+9 -5
View File
@@ -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) = (&times[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;
+27
View File
@@ -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<Item = &'static DiskWorker> {
// SAFETY: `stage` stores only leaked workers, which live for the boot.
STAGED
@@ -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
@@ -26,6 +26,9 @@ pub(super) struct Board<D> {
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<D: Devices> 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<D: Devices> 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<D: Devices> SharedDevices<D> {
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<D: Devices> SharedDevices<D> {
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),
})
}
@@ -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<Vec<(usize, u64)>>,
signal: AtomicBool,
due: AtomicBool,
now: AtomicU64,
}
struct Signalled(Arc<Signals>);
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<Pending>| {
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]