fix(block): a shared disk without a command queue runs one piece of at most 64 KiB a look, where one look ran every waiting command whole on the looking processor
This commit is contained in:
1 parent
e8ec5df8cf
commit
bfcccd0719
3 files changed
+245
-13
No files matched your search
@@ -326,8 +326,9 @@ impl BlockDevice for Runs {
|
||||
self.collect(tag, |ticket| held.requests().finished.remove(&ticket))
|
||||
}
|
||||
|
||||
/// One of these runs' pieces has a result, or the disk has posted a
|
||||
/// completion: another owner's results never count.
|
||||
/// One of these runs' pieces has a result, the disk has posted a
|
||||
/// completion, or a look would run the next piece of a disk of depth 0:
|
||||
/// another owner's results never count.
|
||||
fn posted(&self) -> bool {
|
||||
let Some(mut disk) = self.disk.try_lock() else {
|
||||
return true;
|
||||
@@ -337,6 +338,7 @@ impl BlockDevice for Runs {
|
||||
.iter()
|
||||
.flat_map(|spread| &spread.pending)
|
||||
.any(|(ticket, _)| finished.contains_key(ticket))
|
||||
|| disk.runs_waiting()
|
||||
|| disk.posted()
|
||||
}
|
||||
|
||||
|
||||
@@ -36,6 +36,56 @@ pub(super) struct Requests {
|
||||
pub(super) next: u32,
|
||||
pub(super) stats: Stats,
|
||||
pub(super) busy_since: u64,
|
||||
/// A command a device of depth 0 runs piece by piece
|
||||
/// ([`SYNC_PIECE_BYTES`]): its ticket and the read data of its pieces
|
||||
/// so far. Its rest waits at the front of `waiting`.
|
||||
pub(super) partial: Option<(u32, Vec<u8>)>,
|
||||
}
|
||||
|
||||
/// The most a device that runs each command where it is asked (depth 0)
|
||||
/// moves in one call: a larger read, write or Write Zeroes runs in pieces,
|
||||
/// one a look, so a look holds its processor for one piece, not for the
|
||||
/// whole transfer or for every waiting command, as a slow stick or a
|
||||
/// spinning disk could take seconds for those. Linux bounds a request by
|
||||
/// `max_sectors` for the same reason.
|
||||
pub(super) const SYNC_PIECE_BYTES: usize = 64 << 10;
|
||||
|
||||
/// The first piece of `command` for a device of depth 0 with blocks of
|
||||
/// `block` bytes, leaving the rest in `command`; `None` when it runs whole.
|
||||
pub(super) fn first_piece(command: &mut Command, block: usize) -> Option<Command> {
|
||||
let most = (SYNC_PIECE_BYTES / block.max(1)).max(1);
|
||||
match command {
|
||||
Command::Read { lba, bytes } if *bytes > most * block => {
|
||||
let piece = Command::Read {
|
||||
lba: *lba,
|
||||
bytes: most * block,
|
||||
};
|
||||
*lba += most as u64;
|
||||
*bytes -= most * block;
|
||||
Some(piece)
|
||||
}
|
||||
Command::Write { lba, data, fua } if data.len() > most * block => {
|
||||
let rest = data.split_off(most * block);
|
||||
let piece = Command::Write {
|
||||
lba: *lba,
|
||||
data: core::mem::replace(data, rest),
|
||||
fua: *fua,
|
||||
};
|
||||
*lba += most as u64;
|
||||
Some(piece)
|
||||
}
|
||||
Command::WriteZeroes { lba, blocks, unmap } if *blocks > most as u64 => {
|
||||
let piece = Command::WriteZeroes {
|
||||
lba: *lba,
|
||||
blocks: most as u64,
|
||||
unmap: *unmap,
|
||||
};
|
||||
*lba += most as u64;
|
||||
*blocks -= most as u64;
|
||||
Some(piece)
|
||||
}
|
||||
_ => None,
|
||||
}
|
||||
}
|
||||
|
||||
/// A device command and its first ticket and read length. Additional tickets
|
||||
@@ -155,7 +205,8 @@ impl Held<'_> {
|
||||
.waiting
|
||||
.push_back((ticket, command, crate::Clock::ticks()));
|
||||
self.step(false);
|
||||
if !self.requests().waiting.is_empty() {
|
||||
// A device of depth 0 ran one piece in the first step: no more.
|
||||
if !self.requests().waiting.is_empty() && self.depth() > 0 {
|
||||
self.step(true);
|
||||
}
|
||||
if !more {
|
||||
@@ -227,8 +278,8 @@ impl Held<'_> {
|
||||
}
|
||||
|
||||
/// Collect the device's completed commands, then start those waiting
|
||||
/// while it has room. A device that takes no queued commands runs each
|
||||
/// here, its caller waiting.
|
||||
/// while it has room. A device that takes no queued commands runs one
|
||||
/// piece of the first here, its caller waiting.
|
||||
pub(super) fn advance(&mut self) {
|
||||
self.step(true);
|
||||
self.commit();
|
||||
@@ -336,8 +387,9 @@ impl Held<'_> {
|
||||
}
|
||||
|
||||
/// Start the waiting commands, in order, while the device has room. A
|
||||
/// device that takes no queued commands runs each here, its caller
|
||||
/// waiting.
|
||||
/// device that takes no queued commands runs the first waiting one
|
||||
/// here, its caller waiting, and only one piece of it
|
||||
/// ([`SYNC_PIECE_BYTES`]): the next look runs the next.
|
||||
fn start_waiting(&mut self, work: &mut profile::Work) {
|
||||
if self.requests().waiting.is_empty() {
|
||||
return;
|
||||
@@ -355,10 +407,8 @@ impl Held<'_> {
|
||||
return;
|
||||
};
|
||||
if !queued {
|
||||
let accounted = command.accounted(block);
|
||||
let result = self.direct(accounted, submitted, |device| device.execute(command));
|
||||
self.requests().finished.insert(ticket, result);
|
||||
continue;
|
||||
self.run_piece(ticket, command, submitted, block);
|
||||
return;
|
||||
}
|
||||
let first = match &command {
|
||||
Command::Read { bytes, .. } => *bytes,
|
||||
@@ -405,6 +455,64 @@ impl Held<'_> {
|
||||
}
|
||||
}
|
||||
|
||||
/// Run the first piece of waiting `command`, ticket `ticket`, on a
|
||||
/// device of depth 0, leaving its rest at the front of the queue; once
|
||||
/// the last piece ran or one failed, the ticket has its result.
|
||||
fn run_piece(&mut self, ticket: u32, mut command: Command, submitted: u64, block: usize) {
|
||||
let continued = self
|
||||
.requests()
|
||||
.partial
|
||||
.as_ref()
|
||||
.is_some_and(|(partial, _)| *partial == ticket);
|
||||
let piece = first_piece(&mut command, block);
|
||||
let (run, rest) = match piece {
|
||||
Some(piece) => (piece, Some(command)),
|
||||
None => (command, None),
|
||||
};
|
||||
let accounted = run.accounted(block);
|
||||
// A later piece counts from its own start, not the request's.
|
||||
let since = if continued {
|
||||
crate::Clock::ticks()
|
||||
} else {
|
||||
submitted
|
||||
};
|
||||
let result = self.direct(accounted, since, |device| device.execute(run));
|
||||
let requests = self.requests();
|
||||
let mut data = match requests.partial.take() {
|
||||
Some((partial, data)) if partial == ticket => Some(data),
|
||||
_ => None,
|
||||
};
|
||||
match (result, rest) {
|
||||
(Ok(read), Some(rest)) => {
|
||||
if let Some(read) = read {
|
||||
data.get_or_insert_default().extend_from_slice(&read);
|
||||
}
|
||||
requests.partial = Some((ticket, data.unwrap_or_default()));
|
||||
requests.waiting.push_front((ticket, rest, submitted));
|
||||
}
|
||||
(Ok(read), None) => {
|
||||
let result = match (data, read) {
|
||||
(Some(mut data), Some(read)) => {
|
||||
data.extend_from_slice(&read);
|
||||
Some(data)
|
||||
}
|
||||
(_, read) => read,
|
||||
};
|
||||
requests.finished.insert(ticket, Ok(result));
|
||||
}
|
||||
// A failed piece ends the request; its rest never runs.
|
||||
(Err(error), _) => {
|
||||
requests.finished.insert(ticket, Err(error));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Whether a look would run a piece of a waiting command on a device
|
||||
/// of depth 0, which has nothing of its own to post.
|
||||
pub(super) fn runs_waiting(&mut self) -> bool {
|
||||
self.depth() == 0 && !self.requests().waiting.is_empty()
|
||||
}
|
||||
|
||||
/// Ticket `ticket`'s result, once it has one.
|
||||
pub(super) fn take(&mut self, ticket: u32) -> Option<Finished> {
|
||||
self.advance();
|
||||
@@ -745,8 +853,9 @@ impl BlockDevice for Window {
|
||||
Some(finished)
|
||||
}
|
||||
|
||||
/// One of this window's commands has a result, or the disk has posted
|
||||
/// a completion: another window's results never count.
|
||||
/// One of this window's commands has a result, the disk has posted
|
||||
/// a completion, or a look would run the next piece of a disk of depth
|
||||
/// 0: another window's results never count.
|
||||
fn posted(&self) -> bool {
|
||||
let Some(mut disk) = self.disk.try_lock() else {
|
||||
return true;
|
||||
@@ -755,6 +864,7 @@ impl BlockDevice for Window {
|
||||
self.started
|
||||
.iter()
|
||||
.any(|ticket| finished.contains_key(ticket))
|
||||
|| disk.runs_waiting()
|
||||
|| disk.posted()
|
||||
}
|
||||
|
||||
|
||||
@@ -1311,3 +1311,123 @@ fn a_rotation_rate_reads_as_ata_word_217_and_scsi_vpd_b1_encode_it() {
|
||||
assert_eq!(alloc::format!("{}", Rotation::None), "none");
|
||||
assert_eq!(alloc::format!("{}", Rotation::default()), "unreported");
|
||||
}
|
||||
|
||||
/// A disk that runs each command where it is asked, as a card reader or a
|
||||
/// port without a command queue does, recording the bytes of each call: a
|
||||
/// slow stick or a spinning disk takes a time to match.
|
||||
struct Synchronous {
|
||||
disk: RamDisk,
|
||||
calls: Rc<core::cell::RefCell<Vec<usize>>>,
|
||||
}
|
||||
|
||||
impl BlockDevice for Synchronous {
|
||||
fn identity(&self) -> &Identity {
|
||||
&self.disk.identity
|
||||
}
|
||||
fn read(&mut self, lba: u64, buffer: &mut [u8]) -> Result<(), BlockError> {
|
||||
self.calls.borrow_mut().push(buffer.len());
|
||||
self.disk.read(lba, buffer)
|
||||
}
|
||||
fn write(&mut self, lba: u64, data: &[u8]) -> Result<(), BlockError> {
|
||||
self.calls.borrow_mut().push(data.len());
|
||||
self.disk.write(lba, data)
|
||||
}
|
||||
}
|
||||
|
||||
/// One look at a disk without a queue runs one piece of one command: a
|
||||
/// 1 MiB write queued behind another, then a read of both, never hold the
|
||||
/// looking processor for a whole transfer, or for every waiting command,
|
||||
/// as one look did before.
|
||||
#[test]
|
||||
fn a_disk_without_a_queue_runs_one_bounded_piece_a_look() {
|
||||
let calls: Rc<core::cell::RefCell<Vec<usize>>> = Rc::default();
|
||||
let disk = Shared::new(Box::new(Synchronous {
|
||||
disk: RamDisk::new("sync", 8192, 512),
|
||||
calls: calls.clone(),
|
||||
}));
|
||||
let mut window = disk.whole();
|
||||
let mebibyte = 1 << 20;
|
||||
let first: Vec<u8> = (0..mebibyte).map(|at| (at / 512) as u8).collect();
|
||||
let second: Vec<u8> = (0..mebibyte).map(|at| (at / 512 + 7) as u8).collect();
|
||||
let mut tags = Vec::new();
|
||||
for (lba, data) in [(0, first.clone()), (2048, second.clone())] {
|
||||
let before = calls.borrow().len();
|
||||
tags.push(
|
||||
window
|
||||
.start(Command::Write {
|
||||
lba,
|
||||
data,
|
||||
fua: false,
|
||||
})
|
||||
.unwrap(),
|
||||
);
|
||||
assert!(calls.borrow().len() - before <= 1);
|
||||
}
|
||||
let read = window
|
||||
.start(Command::Read {
|
||||
lba: 0,
|
||||
bytes: 2 * mebibyte,
|
||||
})
|
||||
.unwrap();
|
||||
tags.push(read);
|
||||
let mut results = BTreeMap::new();
|
||||
for _ in 0..1000 {
|
||||
if results.len() == tags.len() {
|
||||
break;
|
||||
}
|
||||
assert!(window.posted());
|
||||
for &tag in &tags {
|
||||
if results.contains_key(&tag) {
|
||||
continue;
|
||||
}
|
||||
let before = calls.borrow().len();
|
||||
if let Some(result) = window.finish(tag) {
|
||||
results.insert(tag, result);
|
||||
}
|
||||
assert!(calls.borrow().len() - before <= 1, "a look ran two pieces");
|
||||
}
|
||||
}
|
||||
assert!(!window.posted());
|
||||
assert!(calls.borrow().iter().all(|&bytes| bytes <= 64 << 10));
|
||||
assert_eq!(calls.borrow().len(), 4 * mebibyte / (64 << 10));
|
||||
assert_eq!(results[&tags[0]], Ok(None));
|
||||
assert_eq!(results[&tags[1]], Ok(None));
|
||||
let mut both = first;
|
||||
both.extend_from_slice(&second);
|
||||
assert_eq!(results[&read], Ok(Some(both)));
|
||||
}
|
||||
|
||||
/// A piece that fails ends its request, whose rest never runs, and the
|
||||
/// next request runs.
|
||||
#[test]
|
||||
fn a_failed_piece_ends_its_request_and_the_queue_goes_on() {
|
||||
let calls: Rc<core::cell::RefCell<Vec<usize>>> = Rc::default();
|
||||
let disk = Shared::new(Box::new(Synchronous {
|
||||
disk: RamDisk::new("short", 256, 512),
|
||||
calls: calls.clone(),
|
||||
}));
|
||||
// A window of the whole disk lets a read past its end through to the
|
||||
// device's own check, which fails the second piece.
|
||||
let mut window = disk.whole();
|
||||
window.identity.blocks = 1024;
|
||||
let past = window
|
||||
.start(Command::Read {
|
||||
lba: 0,
|
||||
bytes: 256 << 10,
|
||||
})
|
||||
.unwrap();
|
||||
let next = window.start(write(0, 9)).unwrap();
|
||||
let mut results = BTreeMap::new();
|
||||
for _ in 0..100 {
|
||||
for tag in [past, next] {
|
||||
if !results.contains_key(&tag)
|
||||
&& let Some(result) = window.finish(tag)
|
||||
{
|
||||
results.insert(tag, result);
|
||||
}
|
||||
}
|
||||
}
|
||||
assert!(results[&past].is_err());
|
||||
assert_eq!(results[&next], Ok(None));
|
||||
assert_eq!(*calls.borrow(), [64 << 10, 64 << 10, 64 << 10, 512]);
|
||||
}
|
||||
Reference in new issue
Block a user