replay: the new runtime's backend rounds each buffer's place to its recorded granule
The backend packed buffers first fit at Metal's 256-byte alignment, so a program's write past a buffer's end landed in the next buffer. A recorded stream's program did that: an append list of 4000 records took about 870 more on one play, and the overrun went 17340 bytes into twelve 4-byte counters and the start of a 48000-byte buffer that the player had packed behind it. The recording had given each of those buffers its own 32 KiB-granular range, so the same overrun landed in the list's own slack there. The clobbered counters then feed the next rounds; a 780M page fault past a 64 MB buffer and a hang on a 3090 follow them (expected, the proof plays pending). Each buffer's place now spans its length rounded up to its recorded granule: the largest power of two that divides its recorded gpuAddress, from 256 bytes to 64 KiB, and 256 bytes where the stream records no address (the player's own staging included). Both copies of a two-copy Managed buffer take it. A buffer past the class's block length takes a block of its own of the rounded span. The backend's new "places by recorded granule" line counts the places by granule and gives the most bytes of slack held at once. Test: rt_replay's buffers_keep_the_slack_of_their_recorded_granule plays two 4-byte buffers recorded at addresses of 32 KiB and 64 KiB granules and reads back their relocated addresses from a third: they lie apart by at least the lower one's granule (256 bytes apart before). docs/REPLAY-STREAM.md says how the backend places buffers.
This commit is contained in:
1 parent
d434af0e67
commit
40e135c308
5 files changed
+150
-28
No files matched your search
@@ -4,7 +4,7 @@
|
||||
//! names a skipped object, are skipped and counted.
|
||||
|
||||
use super::super::args::{arg, data, index, list, range, refuse, uint, variant, Fields};
|
||||
use super::{failed, Held, NewRuntime, Object};
|
||||
use super::{failed, granule, Held, NewRuntime, Object};
|
||||
use crate::replay::player::{Made, PlayError};
|
||||
use crate::replay::stream::Call;
|
||||
use crate::replay::value::Value;
|
||||
@@ -421,8 +421,11 @@ impl NewRuntime {
|
||||
return Ok(());
|
||||
}
|
||||
let completed = self.completed();
|
||||
let place =
|
||||
(self.places.take(rt::MemoryClass::Shared, len, completed)).map_err(failed(call))?;
|
||||
// The player's own staging: no recorded slack.
|
||||
let place = (self
|
||||
.places
|
||||
.take(rt::MemoryClass::Shared, len, granule(None), completed))
|
||||
.map_err(failed(call))?;
|
||||
let staging = self
|
||||
.device
|
||||
.new_buffer(place.block, place.range.start, len, None);
|
||||
|
||||
@@ -27,7 +27,7 @@ use crate::gpu::wait::SubmissionBackend;
|
||||
use crate::gpu::DeviceType;
|
||||
use metal2vulkan::pipeline::{LinkedFunction, LinkedFunctions};
|
||||
use metal2vulkan_rt as rt;
|
||||
use places::{class_index, Place, Places};
|
||||
use places::{class_index, granule, Place, Places};
|
||||
use std::collections::{BTreeMap, HashMap, HashSet, VecDeque};
|
||||
use std::ops::Range;
|
||||
use std::sync::Arc;
|
||||
@@ -904,7 +904,9 @@ impl NewRuntime {
|
||||
self.unwritten.made(call.result, None);
|
||||
}
|
||||
let completed = self.completed();
|
||||
let place = (self.places.take(class, length, completed)).map_err(failed(call))?;
|
||||
// Each copy's place keeps the slack the buffer had in the recording.
|
||||
let recorded = granule(call.answer("gpuAddress").and_then(Value::as_address));
|
||||
let place = (self.places.take(class, length, recorded, completed)).map_err(failed(call))?;
|
||||
let (start, block) = (place.range.start, place.block);
|
||||
let buffer = match mode {
|
||||
// A two-copy stream's Managed buffer on a one-copy device: the GPU's copy here, the
|
||||
@@ -914,7 +916,7 @@ impl NewRuntime {
|
||||
let gpu = gpu.map_err(failed(call))?;
|
||||
write_into(&gpu, 0, bytes)?;
|
||||
let cpu_place =
|
||||
(self.places.take(class, length, completed)).map_err(failed(call))?;
|
||||
(self.places.take(class, length, recorded, completed)).map_err(failed(call))?;
|
||||
let at = (cpu_place.block, cpu_place.range.start);
|
||||
let cpu = self.device.new_buffer(at.0, at.1, length, Some(0));
|
||||
let cpu = cpu.map_err(failed(call))?;
|
||||
@@ -2230,6 +2232,7 @@ impl Backend for NewRuntime {
|
||||
lines.push(self.seconds.line());
|
||||
}
|
||||
lines.push(self.asides.line());
|
||||
lines.push(self.places.line());
|
||||
lines.push(self.unwritten.line());
|
||||
lines.push(self.indices.line());
|
||||
lines
|
||||
|
||||
@@ -1,22 +1,43 @@
|
||||
//! The places of the stream's own buffers in the blocks the backend takes: each memory class's
|
||||
//! pooled blocks (`MemoryClass::block_len`), first fit at Metal's 256-byte buffer alignment, and
|
||||
//! a block of its own for a buffer past the class's block length. A buffer's place is free again
|
||||
//! a block of its own for a buffer past the class's block length. A place spans its buffer's
|
||||
//! length rounded up to the buffer's recorded granule ([`granule`]), so the bytes past a buffer
|
||||
//! are the slack it had in the recording. A buffer's place is free again
|
||||
//! only once every command buffer that named the buffer has completed, as Metal keeps a released
|
||||
//! buffer for the work that names it: those committed before its release, and those that named it
|
||||
//! before its release and are committed after it (a command buffer released uncommitted runs
|
||||
//! nothing, and holds nothing from then).
|
||||
|
||||
use metal2vulkan_rt as rt;
|
||||
use std::collections::BTreeMap;
|
||||
use std::ops::Range;
|
||||
|
||||
/// Where a buffer's place starts: Metal's buffers lie at multiples of 256 bytes.
|
||||
/// Where a buffer's place starts, and the least granule: Metal's buffers lie at multiples of 256
|
||||
/// bytes.
|
||||
const ALIGN: u64 = 256;
|
||||
|
||||
/// A buffer's place: its block, its bytes there, and the block's handle where the block is its
|
||||
/// own.
|
||||
/// The most a recorded granule is taken as: past it, an address's alignment says more about where
|
||||
/// the guest's allocator started than about the granule it carves at.
|
||||
const MOST_GRANULE: u64 = 64 << 10;
|
||||
|
||||
/// The granule of a buffer the stream recorded at `address`: the largest power of two that
|
||||
/// divides the address, from Metal's 256-byte alignment up to 64 KiB, and 256 bytes for a buffer
|
||||
/// with no recorded address (or the player's own). Its place spans its length rounded up to the
|
||||
/// granule: a program that writes past a buffer's end writes into the slack the buffer had in the
|
||||
/// recording, as the recording's program did, and not into the next buffer.
|
||||
pub(super) fn granule(address: Option<u64>) -> u64 {
|
||||
match address {
|
||||
Some(address) if address != 0 => (1 << address.trailing_zeros()).clamp(ALIGN, MOST_GRANULE),
|
||||
_ => ALIGN,
|
||||
}
|
||||
}
|
||||
|
||||
/// A buffer's place: its block, its buffer's bytes there, the bytes the place spans from their
|
||||
/// start (the buffer's, then its slack), and the block's handle where the block is its own.
|
||||
pub(super) struct Place {
|
||||
pub(super) block: rt::BlockId,
|
||||
pub(super) range: Range<u64>,
|
||||
span: u64,
|
||||
own: Option<rt::Block>,
|
||||
}
|
||||
|
||||
@@ -36,6 +57,10 @@ pub(super) struct Places {
|
||||
waiting: Waiting<Place>,
|
||||
/// The bytes of the blocks held, pooled and own, by class (`class_index`).
|
||||
bytes: [u64; 2],
|
||||
/// The places taken, by granule.
|
||||
granules: BTreeMap<u64, u64>,
|
||||
/// The bytes of slack the places hold now, and the most they held.
|
||||
slack: (u64, u64),
|
||||
}
|
||||
|
||||
/// `class`'s index in a pair of per-class values: Private, then Shared.
|
||||
@@ -54,6 +79,8 @@ impl Places {
|
||||
held: Vec::new(),
|
||||
waiting: Waiting::default(),
|
||||
bytes: [0; 2],
|
||||
granules: BTreeMap::new(),
|
||||
slack: (0, 0),
|
||||
}
|
||||
}
|
||||
|
||||
@@ -63,25 +90,44 @@ impl Places {
|
||||
self.bytes
|
||||
}
|
||||
|
||||
/// A place of `len` bytes of `class`, once every place whose commits have all completed
|
||||
/// (`completed`, the commits that have) is free again.
|
||||
/// The `places` line: the places taken by granule ([`granule`]), and the most bytes of slack
|
||||
/// held at once.
|
||||
pub(super) fn line(&self) -> String {
|
||||
let granules: Vec<String> = (self.granules.iter())
|
||||
.map(|(granule, count)| format!("{granule} B {count}"))
|
||||
.collect();
|
||||
format!(
|
||||
"places by recorded granule: {}; most slack held {} bytes",
|
||||
granules.join(", "),
|
||||
self.slack.1
|
||||
)
|
||||
}
|
||||
|
||||
/// A place of `len` bytes of `class`, spanning `len` rounded up to `granule` ([`granule`]),
|
||||
/// once every place whose commits have all completed (`completed`, the commits that have) is
|
||||
/// free again.
|
||||
pub(super) fn take(
|
||||
&mut self,
|
||||
class: rt::MemoryClass,
|
||||
len: u64,
|
||||
granule: u64,
|
||||
completed: u64,
|
||||
) -> Result<Place, rt::Error> {
|
||||
self.free_completed(completed)?;
|
||||
if len > class.block_len() {
|
||||
let block = self.device.take_block(class, len)?;
|
||||
self.bytes[class_index(class)] += len;
|
||||
let span = len.next_multiple_of(granule.max(ALIGN));
|
||||
*self.granules.entry(granule.max(ALIGN)).or_default() += 1;
|
||||
self.slack.0 += span - len;
|
||||
self.slack.1 = self.slack.1.max(self.slack.0);
|
||||
if span > class.block_len() {
|
||||
let block = self.device.take_block(class, span)?;
|
||||
self.bytes[class_index(class)] += span;
|
||||
return Ok(Place {
|
||||
block: block.id,
|
||||
range: 0..len,
|
||||
span,
|
||||
own: Some(block),
|
||||
});
|
||||
}
|
||||
let span = len.next_multiple_of(ALIGN);
|
||||
for block in self
|
||||
.blocks
|
||||
.iter_mut()
|
||||
@@ -91,6 +137,7 @@ impl Places {
|
||||
return Ok(Place {
|
||||
block: block.info.id,
|
||||
range: range.start..range.start + len,
|
||||
span,
|
||||
own: None,
|
||||
});
|
||||
}
|
||||
@@ -105,6 +152,7 @@ impl Places {
|
||||
Ok(Place {
|
||||
block,
|
||||
range: range.start..range.start + len,
|
||||
span,
|
||||
own: None,
|
||||
})
|
||||
}
|
||||
@@ -135,17 +183,17 @@ impl Places {
|
||||
continue;
|
||||
}
|
||||
let (place, _) = self.held.swap_remove(at);
|
||||
self.slack.0 -= place.span - (place.range.end - place.range.start);
|
||||
if let Some(own) = place.own {
|
||||
let class = own.class;
|
||||
// The runtime keeps the block for the command buffers that use it.
|
||||
self.device.release_block(own)?;
|
||||
self.bytes[class_index(class)] -= place.range.end;
|
||||
self.bytes[class_index(class)] -= place.span;
|
||||
continue;
|
||||
}
|
||||
let span = (place.range.end - place.range.start).next_multiple_of(ALIGN);
|
||||
let start = place.range.start;
|
||||
if let Some(block) = self.blocks.iter_mut().find(|b| b.info.id == place.block) {
|
||||
give_to(&mut block.free, start..start + span);
|
||||
give_to(&mut block.free, start..start + place.span);
|
||||
}
|
||||
}
|
||||
Ok(())
|
||||
|
||||
@@ -28,6 +28,9 @@
|
||||
//! - A sampler state placed at an index a held commit owes (its command buffer waits for an event
|
||||
//! only a later record signals) takes it without waiting where its descriptor is the one placed
|
||||
//! there last, and otherwise stops the play naming what holds the commit.
|
||||
//! - Buffers keep the slack of their recorded granule: two recorded at addresses of 32 KiB and
|
||||
//! 64 KiB granules lie apart by at least the lower one's granule, as the recording's addresses
|
||||
//! that a third buffer holds come back from the player relocated.
|
||||
//!
|
||||
//! The new runtime's device is opened behind the hang tripwire, as every device open in this crate
|
||||
//! is (`gpu.rs`), its kernels kept in a cache the test owns ([`ScratchCache`]).
|
||||
@@ -96,13 +99,25 @@ fn flagged(
|
||||
/// `path` played on the new runtime's device, its report and the backend's lines printed; the
|
||||
/// play's end unwrapped.
|
||||
fn play(test: &str, path: &Path) -> (Report, Vec<String>) {
|
||||
let (report, lines, _) = played(test, path, &[]);
|
||||
let (report, lines, ()) = play_then(test, path, |_| ());
|
||||
(report, lines)
|
||||
}
|
||||
|
||||
/// `path` played as [`play`] plays it, and the index (the low half of the `gpuResourceID`) the
|
||||
/// runtime then gives each of `objects`.
|
||||
fn played(test: &str, path: &Path, objects: &[u64]) -> (Report, Vec<String>, Vec<u32>) {
|
||||
play_then(test, path, |backend| {
|
||||
let mut index = |object: &u64| backend.resource_id(*object).unwrap().expect("an ID") as u32;
|
||||
objects.iter().map(&mut index).collect()
|
||||
})
|
||||
}
|
||||
|
||||
/// `path` played as [`play`] plays it, then `then` of the backend.
|
||||
fn play_then<T>(
|
||||
test: &str,
|
||||
path: &Path,
|
||||
then: impl FnOnce(&mut NewRuntime) -> T,
|
||||
) -> (Report, Vec<String>, T) {
|
||||
let cache = ScratchCache::new().unwrap();
|
||||
let mut backend = NewRuntime::new(rt_device(cache.path()), gpu::TEST_DEVICE).unwrap();
|
||||
let mut reader = StreamReader::open(&[path.to_path_buf()], None).unwrap();
|
||||
@@ -112,9 +127,8 @@ fn played(test: &str, path: &Path, objects: &[u64]) -> (Report, Vec<String>, Vec
|
||||
eprintln!("REPLAY {test} {line}");
|
||||
}
|
||||
ended.unwrap();
|
||||
let mut index = |object: &u64| backend.resource_id(*object).unwrap().expect("an ID") as u32;
|
||||
let indices = objects.iter().map(&mut index).collect();
|
||||
(report, lines, indices)
|
||||
let then = then(&mut backend);
|
||||
(report, lines, then)
|
||||
}
|
||||
|
||||
/// A sampler descriptor's map: the default descriptor, its resource index `index` where given.
|
||||
@@ -717,3 +731,52 @@ fn a_buffer_placed_in_a_block_taken_again_reads_zero() {
|
||||
"each block zeroed at its making: {zeroed}"
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn buffers_keep_the_slack_of_their_recorded_granule() {
|
||||
let dir = ScratchDir::new("rt-replay-granule").unwrap();
|
||||
let path = written(&dir, |w| {
|
||||
w.call("MTLCreateSystemDefaultDevice", 0, 1, &[], &[])?;
|
||||
let made = "newBufferWithLength:options:";
|
||||
let at = |address, length| {
|
||||
[
|
||||
("gpuAddress", Value::Address(address)),
|
||||
("gpuLength", Value::Uint(length)),
|
||||
]
|
||||
};
|
||||
// #2 and #3, 4 bytes each, recorded 32 KiB apart: granules of 32 KiB and 64 KiB.
|
||||
for (id, address) in [(2, 0x48000), (3, 0x50000)] {
|
||||
w.call(
|
||||
made,
|
||||
1,
|
||||
id,
|
||||
&[Value::Uint(4), Value::Uint(0)],
|
||||
&at(address, 4),
|
||||
)?;
|
||||
}
|
||||
// #4 holds their recorded addresses, which the player puts there as the runtime's.
|
||||
w.call(
|
||||
made,
|
||||
1,
|
||||
4,
|
||||
&[Value::Uint(16), Value::Uint(0)],
|
||||
&at(0x60000, 16),
|
||||
)?;
|
||||
let words = [0x48000u64, 0x50000].iter().flat_map(|a| a.to_le_bytes());
|
||||
let write = [Value::Uint(0), Value::Bytes(words.collect())];
|
||||
w.call("m2v.write", 4, 0, &write, &[])
|
||||
});
|
||||
let (_, lines, words) = play_then("granule", &path, |backend| backend.read(4).unwrap());
|
||||
let word = |at: usize| u64::from_le_bytes(words[at..at + 8].try_into().unwrap());
|
||||
let (first, second) = (word(0), word(8));
|
||||
let apart = match first < second {
|
||||
true => second - first >= 32 << 10,
|
||||
false => first - second >= 64 << 10,
|
||||
};
|
||||
assert!(apart, "#2 at {first:#x}, #3 at {second:#x}: {lines:?}");
|
||||
let places = lines
|
||||
.iter()
|
||||
.find(|line| line.starts_with("places by recorded granule: "));
|
||||
let places = places.unwrap_or_else(|| panic!("a places line: {lines:?}"));
|
||||
assert!(places.contains("32768 B 1, 65536 B 2"), "{places}");
|
||||
}
|
||||
+10
-5
@@ -582,12 +582,17 @@ counted from 0), completes the work committed so far, reports as at the stream's
|
||||
The counts are among its executed counters. A play with any of them above zero is no gate
|
||||
result: `report.txt` says so on a `skipped` line. It places buffers of their own, and the GPU
|
||||
copies of two-copy Managed buffers, in the runtime's blocks of 16 MiB of either class (a longer
|
||||
one in a block of its own), a range freed for reuse once every command buffer committed before
|
||||
its release completed, zeroes a block or heap of the stream once at its making (as the live
|
||||
one in a block of its own), each place its length rounded up to the buffer's recorded granule:
|
||||
the largest power of two that divides its recorded `gpuAddress`, from 256 bytes to 64 KiB (256
|
||||
bytes where it has none). A program that writes past a buffer's end then writes into the slack
|
||||
the buffer had in the recording, not into the next buffer; the backend's `places by recorded
|
||||
granule` line counts the places by granule and gives the most bytes of slack held at once. A
|
||||
place is free for reuse once every command buffer committed before its buffer's release
|
||||
completed. The backend zeroes a block or heap of the stream once at its making (as the live
|
||||
system zeroes a heap's memory once: a buffer placed there reads zero where nothing wrote it,
|
||||
though the runtime may take a released block's bytes again), gives
|
||||
the runtime back a block or heap of the stream at its release (`release_block`), and gives
|
||||
textures and sampler states indices from the whole of each index class (section 5): its
|
||||
though the runtime may take a released block's bytes again), gives the runtime back a block or
|
||||
heap of the stream at its release (`release_block`), and gives textures and sampler states
|
||||
indices from the whole of each index class (section 5): its
|
||||
`indices named N: ...` line counts the objects placed at the index the stream named, those
|
||||
whose named index the runtime refused, the indices the program reserved, the placements
|
||||
that waited for an index's holders' commits, with the time waited, and the sampler states placed
|
||||
|
||||
Reference in new issue
Block a user