Streaming: mapping calls ask only for what they need; the thread that loads runs ahead; a streaming log
All of the title's loading goes through one thread of its own, which maps the pages of a batch and then submits it. Each page took up to four system calls (unmap, split, map, commit), each waiting for the address-space lock: 200-400 us a page in the world. No split of a placeholder that is the view's own, no commit of section pages that are committed (VICTORY_MAP_LEAN=0: as before); the thread, below normal priority as the title sets it, is lifted above it where it maps (VICTORY_STREAM_PRIORITY=0: not). VICTORY_DSTORAGE_LOG=<file>: every request, status entry and the cost of the mapping calls; VICTORY_STACKS names each thread's affinity. Co-Authored-By: Claude Opus 5.5 <[email protected]>
This commit is contained in:
1 parent
0abcb9fd89
commit
0c46a8cfe0
3 files changed
+345
-34
No files matched your search
@@ -151,6 +151,9 @@ extern "system" fn open_file(_: Factory, path: *const u16, iid: *const Guid, out
|
||||
let file = unsafe { std::fs::File::from_raw_handle(handle) };
|
||||
let files = Files::new(file, Io::chosen());
|
||||
let file = Instance::create(Object::new("dstorage file", &FILE_VTABLE), File { files });
|
||||
if stream_log::on() {
|
||||
stream_log::line(format_args!("F\t{:x}\t{kept}", file as usize));
|
||||
}
|
||||
WALLS_FILES.lock().unwrap_or_else(|e| e.into_inner()).push((file as usize, kept));
|
||||
put(out, file)
|
||||
}
|
||||
@@ -170,9 +173,10 @@ extern "system" fn create_status_array(
|
||||
"dstorage: status array"
|
||||
);
|
||||
let entries = (0..capacity).map(|_| AtomicI32::new(S_OK)).collect();
|
||||
let written = (0..capacity).map(|_| AtomicU64::new(0)).collect();
|
||||
let array = Instance::create(
|
||||
Object::new("dstorage status array", &STATUS_VTABLE),
|
||||
StatusArray { entries },
|
||||
StatusArray { entries, written },
|
||||
);
|
||||
put(out, array)
|
||||
}
|
||||
@@ -459,6 +463,9 @@ static FILE_VTABLE: Methods<8> = Methods([
|
||||
|
||||
struct StatusArray {
|
||||
entries: Box<[AtomicI32]>,
|
||||
/// For the log: when each entry was written ([`stream_log::now`]); 0
|
||||
/// once the title has seen it.
|
||||
written: Box<[AtomicU64]>,
|
||||
}
|
||||
|
||||
type Array = *mut Instance<StatusArray>;
|
||||
@@ -476,7 +483,15 @@ extern "system" fn status_result(this: Array, index: u32) -> HRESULT {
|
||||
/// The title polls this (entry 0, a `Sleep(1)` loop) and never
|
||||
/// reads the result itself.
|
||||
extern "system" fn is_complete(this: Array, index: u32) -> bool {
|
||||
status_result(this, index) != E_PENDING
|
||||
let done = status_result(this, index) != E_PENDING;
|
||||
if done && stream_log::on() {
|
||||
// SAFETY: as above.
|
||||
let written = &unsafe { &*this }.state.written;
|
||||
if let Some(at) = written.get(index as usize).map(|w| w.swap(0, Ordering::Relaxed)).filter(|&at| at != 0) {
|
||||
stream_log::line(format_args!("P\t{:x}\t{at}\t{}", this as usize, stream_log::now()));
|
||||
}
|
||||
}
|
||||
done
|
||||
}
|
||||
|
||||
static STATUS_VTABLE: Methods<5> = Methods([
|
||||
@@ -671,6 +686,83 @@ fn walls_request(request: &Request, result: &Result<(), HRESULT>, started: std::
|
||||
);
|
||||
}
|
||||
|
||||
/// The streaming log (`VICTORY_DSTORAGE_LOG=<file>`): a line for every
|
||||
/// request (`R`: queue, number, file, offset, source bytes, destination
|
||||
/// bytes, options, when enqueued, submitted, started, done, result), every
|
||||
/// status entry written (`S`: array, number, when enqueued, when written),
|
||||
/// the title's first look at it after (`P`: array, when written, when
|
||||
/// seen), every file opened (`F`), and each second what the title's mapping
|
||||
/// calls cost (`M`, `xmem::map_costs`). Times are milliseconds since the
|
||||
/// Unix epoch.
|
||||
pub(crate) mod stream_log {
|
||||
use super::*;
|
||||
use std::io::Write;
|
||||
|
||||
type Log = Mutex<std::io::BufWriter<std::fs::File>>;
|
||||
|
||||
fn log() -> Option<&'static Log> {
|
||||
static LOG: OnceLock<Option<Log>> = OnceLock::new();
|
||||
LOG.get_or_init(|| {
|
||||
let path = std::env::var_os("VICTORY_DSTORAGE_LOG")?;
|
||||
let file = std::fs::File::create(&path).ok()?;
|
||||
std::thread::spawn(|| {
|
||||
loop {
|
||||
std::thread::sleep(std::time::Duration::from_secs(1));
|
||||
if let Some(costs) = super::super::xmem::map_costs() {
|
||||
line(format_args!("M\t{}\t{costs}", now()));
|
||||
}
|
||||
if let Some(log) = log() {
|
||||
let _ = log.lock().unwrap_or_else(|e| e.into_inner()).flush();
|
||||
}
|
||||
}
|
||||
});
|
||||
Some(Mutex::new(std::io::BufWriter::with_capacity(1 << 20, file)))
|
||||
})
|
||||
.as_ref()
|
||||
}
|
||||
|
||||
pub(crate) fn on() -> bool {
|
||||
static ON: OnceLock<bool> = OnceLock::new();
|
||||
*ON.get_or_init(|| std::env::var_os("VICTORY_DSTORAGE_LOG").is_some())
|
||||
}
|
||||
|
||||
/// Milliseconds since the Unix epoch.
|
||||
pub(super) fn now() -> u64 {
|
||||
std::time::SystemTime::now()
|
||||
.duration_since(std::time::UNIX_EPOCH)
|
||||
.map_or(0, |t| t.as_millis() as u64)
|
||||
}
|
||||
|
||||
pub(super) fn line(text: std::fmt::Arguments) {
|
||||
if let Some(log) = log() {
|
||||
let _ = writeln!(log.lock().unwrap_or_else(|e| e.into_inner()), "{text}");
|
||||
}
|
||||
}
|
||||
|
||||
pub(super) fn request(job: &Job, started: std::time::Instant, result: &Result<(), HRESULT>) {
|
||||
if !on() {
|
||||
return;
|
||||
}
|
||||
let now = now();
|
||||
let ago = |t: Option<std::time::Instant>| now - t.map_or(0, |t| t.elapsed().as_millis() as u64);
|
||||
let r = &job.request;
|
||||
line(format_args!(
|
||||
"R\t{}\t{}\t{:x}\t{}\t{}\t{}\t{:x}\t{}\t{}\t{}\t{now}\t{:x}",
|
||||
job.lane,
|
||||
job.seq,
|
||||
r.source as usize,
|
||||
r.offset,
|
||||
r.source_size,
|
||||
r.destination_size,
|
||||
r.options,
|
||||
ago(job.note.enqueued),
|
||||
ago(job.note.submitted),
|
||||
ago(Some(started)),
|
||||
result.err().unwrap_or(0)
|
||||
));
|
||||
}
|
||||
}
|
||||
|
||||
/// A worker: runs the next read of the most deserving queue.
|
||||
fn work(scheduler: &'static Scheduler) {
|
||||
let mut lanes = scheduler.lock();
|
||||
@@ -684,6 +776,7 @@ fn work(scheduler: &'static Scheduler) {
|
||||
let traced = Traced::start(&job);
|
||||
let result = execute(&job.request);
|
||||
walls_request(&job.request, &result, started);
|
||||
stream_log::request(&job, started, &result);
|
||||
if let Some(traced) = traced {
|
||||
traced.end(&job, &result);
|
||||
}
|
||||
@@ -848,6 +941,11 @@ impl Lane {
|
||||
let first = std::mem::replace(&mut self.failures, later)
|
||||
.into_values()
|
||||
.next();
|
||||
if stream_log::on() {
|
||||
let at = stream_log::now();
|
||||
stream_log::line(format_args!("S\t{:x}\t{seq}\t{}\t{at}", status.array as usize, status.enqueued));
|
||||
status.written().store(at, Ordering::Relaxed);
|
||||
}
|
||||
status
|
||||
.entry()
|
||||
.store(first.unwrap_or(S_OK), Ordering::Release);
|
||||
@@ -965,6 +1063,7 @@ struct Note {
|
||||
impl Note {
|
||||
fn enqueued(request: &Request) -> Note {
|
||||
match trace() {
|
||||
0 if stream_log::on() => Note { enqueued: Some(std::time::Instant::now()), ..Note::default() },
|
||||
0 => Note::default(),
|
||||
_ => Note {
|
||||
enqueued: Some(std::time::Instant::now()),
|
||||
@@ -976,8 +1075,10 @@ impl Note {
|
||||
|
||||
fn submitted(&mut self, request: &Request, early: bool) {
|
||||
self.early = early;
|
||||
if trace() > 0 {
|
||||
if trace() > 0 || stream_log::on() {
|
||||
self.submitted = Some(std::time::Instant::now());
|
||||
}
|
||||
if trace() > 0 {
|
||||
self.at_submit = list_hash(request);
|
||||
}
|
||||
}
|
||||
@@ -1131,6 +1232,8 @@ impl Traced {
|
||||
struct Status {
|
||||
array: Array,
|
||||
index: u32,
|
||||
/// For the log.
|
||||
enqueued: u64,
|
||||
}
|
||||
|
||||
impl Drop for Status {
|
||||
@@ -1144,6 +1247,11 @@ impl Status {
|
||||
// SAFETY: we hold a reference; the index was checked when enqueued.
|
||||
&unsafe { &*self.array }.state.entries[self.index as usize]
|
||||
}
|
||||
|
||||
fn written(&self) -> &AtomicU64 {
|
||||
// SAFETY: as above.
|
||||
&unsafe { &*self.array }.state.written[self.index as usize]
|
||||
}
|
||||
}
|
||||
|
||||
extern "system" fn enqueue_request(this: QueueRef, request: *const Request) {
|
||||
@@ -1166,7 +1274,8 @@ extern "system" fn enqueue_status(this: QueueRef, array: Array, index: u32) {
|
||||
};
|
||||
entry.store(E_PENDING, Ordering::Release);
|
||||
add_ref_instance(array);
|
||||
queue(this, Command::Status(Status { array, index }));
|
||||
let enqueued = stream_log::now();
|
||||
queue(this, Command::Status(Status { array, index, enqueued }));
|
||||
}
|
||||
|
||||
/// Whether a queue submits itself once half of it is enqueued
|
||||
@@ -2162,7 +2271,7 @@ mod tests {
|
||||
let array = status_array(2).cast::<Instance<StatusArray>>();
|
||||
let status = |index| {
|
||||
add_ref_instance(array);
|
||||
Command::Status(Status { array, index })
|
||||
Command::Status(Status { array, index, enqueued: 0 })
|
||||
};
|
||||
read(&mut l, 0, 0, 16);
|
||||
read(&mut l, 0, 0, 16);
|
||||
|
||||
@@ -419,6 +419,33 @@ impl Section {
|
||||
}
|
||||
}
|
||||
|
||||
impl Section {
|
||||
/// Commits the pages from `index` on that a view at `base` shows, unless
|
||||
/// all of them are ([`lean`]): committed for one view is for all.
|
||||
fn commit_shown(&self, index: usize, base: usize, len: usize) -> bool {
|
||||
use std::sync::atomic::Ordering;
|
||||
let pages = index..(index + len / PAGE).min(self.pages);
|
||||
let bit = |p: usize| (&self.committed[p / 64], 1 << (p % 64));
|
||||
let done = || {
|
||||
pages.clone().all(|p| {
|
||||
let (word, bit) = bit(p);
|
||||
word.load(Ordering::Acquire) & bit != 0
|
||||
})
|
||||
};
|
||||
if lean() && done() {
|
||||
return true;
|
||||
}
|
||||
if !counted(Call::Commit, || commit(base, len)) {
|
||||
return false;
|
||||
}
|
||||
for p in pages {
|
||||
let (word, bit) = bit(p);
|
||||
word.fetch_or(bit, Ordering::AcqRel);
|
||||
}
|
||||
true
|
||||
}
|
||||
}
|
||||
|
||||
// SAFETY: a section handle may be used from any thread.
|
||||
unsafe impl Send for Section {}
|
||||
// SAFETY: as above.
|
||||
@@ -455,6 +482,9 @@ struct Slice {
|
||||
struct Physical {
|
||||
/// Mapped views by address: their length and what they show.
|
||||
views: BTreeMap<usize, (usize, Slice)>,
|
||||
/// Placeholders that are some view's own, left by its unmapping: address
|
||||
/// and length. The title may have joined one with others since.
|
||||
holes: HashMap<usize, usize>,
|
||||
}
|
||||
|
||||
/// The title's pages by number.
|
||||
@@ -893,6 +923,121 @@ pub(crate) fn pages_written(pages: &[usize], offset: usize, len: usize) {
|
||||
}
|
||||
}
|
||||
|
||||
/// What the title's mapping calls cost since the last asking, for the
|
||||
/// streaming log, counted only while that log is on: calls, pages,
|
||||
/// milliseconds in all, the longest one's and waiting for the lock; the same
|
||||
/// of the GPU's page-mapping copies ([`map_alias`]; calls and milliseconds);
|
||||
/// and by system call (unmap, split, map, commit) how many and their
|
||||
/// milliseconds.
|
||||
static MAPPING: [std::sync::atomic::AtomicU64; 15] = [const { std::sync::atomic::AtomicU64::new(0) }; 15];
|
||||
|
||||
pub(crate) fn map_costs() -> Option<String> {
|
||||
let n: [u64; 15] = std::array::from_fn(|i| MAPPING[i].swap(0, Ordering::Relaxed));
|
||||
let time = |i: usize| matches!(i, 2 | 3 | 4 | 6 | 8 | 10 | 12 | 14);
|
||||
(n[0] + n[5] > 0).then(|| {
|
||||
let all: Vec<String> = n
|
||||
.iter()
|
||||
.enumerate()
|
||||
.map(|(i, &n)| if time(i) { n / 1_000_000 } else { n })
|
||||
.map(|n| n.to_string())
|
||||
.collect();
|
||||
all.join("\t")
|
||||
})
|
||||
}
|
||||
|
||||
/// The system calls that change a mapping, as [`MAPPING`] counts them.
|
||||
#[derive(Clone, Copy)]
|
||||
enum Call {
|
||||
Unmap = 7,
|
||||
Split = 9,
|
||||
Map = 11,
|
||||
Commit = 13,
|
||||
}
|
||||
|
||||
/// Runs `f`, a system call, and counts it while the streaming log is on.
|
||||
fn counted<T>(call: Call, f: impl FnOnce() -> T) -> T {
|
||||
if !super::dstorage::stream_log::on() {
|
||||
return f();
|
||||
}
|
||||
let started = std::time::Instant::now();
|
||||
let done = f();
|
||||
MAPPING[call as usize].fetch_add(1, Ordering::Relaxed);
|
||||
MAPPING[call as usize + 1].fetch_add(started.elapsed().as_nanos() as u64, Ordering::Relaxed);
|
||||
done
|
||||
}
|
||||
|
||||
/// Counts a mapping call when dropped.
|
||||
struct Costed {
|
||||
started: std::time::Instant,
|
||||
/// `None`: a copy of mappings.
|
||||
pages: Option<usize>,
|
||||
waited: u64,
|
||||
}
|
||||
|
||||
impl Costed {
|
||||
fn begin(pages: Option<usize>) -> Option<Costed> {
|
||||
super::dstorage::stream_log::on().then(|| Costed { started: std::time::Instant::now(), pages, waited: 0 })
|
||||
}
|
||||
}
|
||||
|
||||
impl Drop for Costed {
|
||||
fn drop(&mut self) {
|
||||
let ns = self.started.elapsed().as_nanos() as u64;
|
||||
let Some(pages) = self.pages else {
|
||||
MAPPING[5].fetch_add(1, Ordering::Relaxed);
|
||||
MAPPING[6].fetch_add(ns, Ordering::Relaxed);
|
||||
return;
|
||||
};
|
||||
for (i, n) in [1, pages as u64, ns].into_iter().enumerate() {
|
||||
MAPPING[i].fetch_add(n, Ordering::Relaxed);
|
||||
}
|
||||
MAPPING[3].fetch_max(ns, Ordering::Relaxed);
|
||||
MAPPING[4].fetch_add(self.waited, Ordering::Relaxed);
|
||||
}
|
||||
}
|
||||
|
||||
/// Whether a mapping change asks the host only for what it needs: no split
|
||||
/// of a placeholder that is already the view's own, no commit of section
|
||||
/// pages that are committed (`VICTORY_MAP_LEAN=0`: both every time, as
|
||||
/// before).
|
||||
///
|
||||
/// All of the title's streaming goes through one thread, which maps the
|
||||
/// pages of a batch and then submits it. Each call waits for the process's
|
||||
/// one address-space lock, behind the guards' and the driver's: 200 to 400
|
||||
/// us a page in the world against 10 in a menu, 2 to 4 s of every 5 s of
|
||||
/// that thread (runs stream-1 and -2; all six stacks taken of it in the
|
||||
/// opening were in these calls), so that models came as fast as their pages
|
||||
/// could be mapped. A page took up to four calls (unmap, split, map,
|
||||
/// commit), twice: the title maps a page into its pool and then has the GPU
|
||||
/// map it into its tiled heap.
|
||||
fn lean() -> bool {
|
||||
static ON: LazyLock<bool> = LazyLock::new(|| std::env::var_os("VICTORY_MAP_LEAN").is_none_or(|v| v != "0"));
|
||||
*ON
|
||||
}
|
||||
|
||||
/// The title's streaming thread runs below normal priority, as do sixteen
|
||||
/// workers of ours when pipelines are made; on the console nothing else
|
||||
/// wants its core. Called where that thread maps and submits: lifts a
|
||||
/// thread that is below normal to above it (`VICTORY_STREAM_PRIORITY=0`:
|
||||
/// left as the title set it).
|
||||
pub(crate) fn lift_thread() {
|
||||
use windows_sys::Win32::System::Threading::{
|
||||
GetCurrentThread, GetThreadPriority, SetThreadPriority, THREAD_PRIORITY_ABOVE_NORMAL,
|
||||
};
|
||||
static ON: LazyLock<bool> =
|
||||
LazyLock::new(|| std::env::var_os("VICTORY_STREAM_PRIORITY").is_none_or(|v| v != "0"));
|
||||
if !*ON {
|
||||
return;
|
||||
}
|
||||
// SAFETY: plain calls on this thread.
|
||||
unsafe {
|
||||
let thread = GetCurrentThread();
|
||||
if matches!(GetThreadPriority(thread), -2..0) {
|
||||
SetThreadPriority(thread, THREAD_PRIORITY_ABOVE_NORMAL);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Maps `n` pages at `address`, in place of what is mapped there; null
|
||||
/// `pages` unmaps. Returns `address`, or null if a page or the host fails.
|
||||
pub extern "system" fn XMemMapPhysicalPages(
|
||||
@@ -905,7 +1050,11 @@ pub extern "system" fn XMemMapPhysicalPages(
|
||||
// the mapping call that holds them.
|
||||
// SAFETY: the title passes `n` page numbers, or null.
|
||||
let pages = (!pages.is_null()).then(|| unsafe { std::slice::from_raw_parts(pages, n) }.to_vec());
|
||||
let mut costed = Costed::begin(Some(n));
|
||||
let mut physical = physical();
|
||||
if let Some(costed) = &mut costed {
|
||||
costed.waited = costed.started.elapsed().as_nanos() as u64;
|
||||
}
|
||||
// Until the pages are mapped again: `unmap` takes whole views away and
|
||||
// maps what stays of them again, and a mapped view is a reservation
|
||||
// until it is committed. A read of title memory in place must not fall
|
||||
@@ -953,6 +1102,7 @@ pub extern "system" fn XMemMapPhysicalPagesScatter(
|
||||
if addresses.is_null() {
|
||||
return 0;
|
||||
}
|
||||
lift_thread();
|
||||
for i in 0..n {
|
||||
// SAFETY: the title passes `n` addresses and, unless null, `n` pages.
|
||||
let (address, page) = unsafe {
|
||||
@@ -979,6 +1129,7 @@ pub extern "system" fn XMemMapPhysicalPagesScatter(
|
||||
/// heap. Page by page; false if a source page is not mapped physical memory.
|
||||
pub fn map_alias(to: u64, from: u64, len: u64) -> bool {
|
||||
let (to, from, len) = (to as usize, from as usize, len as usize);
|
||||
let _costed = Costed::begin(None);
|
||||
let mut physical = physical();
|
||||
// As in `XMemMapPhysicalPages`: no read in place meets the gap.
|
||||
let _changing = victory_gpu::title_memory_remapping();
|
||||
@@ -1010,27 +1161,36 @@ pub fn map_alias(to: u64, from: u64, len: u64) -> bool {
|
||||
/// Maps `slice` and the slices after it over `[base, base + len)`, which
|
||||
/// must lie in one placeholder.
|
||||
fn map(physical: &mut Physical, base: usize, len: usize, slice: Slice) -> bool {
|
||||
// SAFETY: splits the placeholder around the range (failing harmlessly
|
||||
// when the range is the whole placeholder), then maps the section
|
||||
// there; both act only on placeholders.
|
||||
let view = unsafe {
|
||||
VirtualFree(
|
||||
base as *mut c_void,
|
||||
len,
|
||||
MEM_RELEASE | MEM_PRESERVE_PLACEHOLDER,
|
||||
);
|
||||
MapViewOfFile3(
|
||||
slice.section.handle,
|
||||
GetCurrentProcess(),
|
||||
base as *const c_void,
|
||||
(slice.index * PAGE) as u64,
|
||||
len,
|
||||
MEM_REPLACE_PLACEHOLDER,
|
||||
PAGE_READWRITE,
|
||||
std::ptr::null_mut(),
|
||||
0,
|
||||
)
|
||||
let map_view = || {
|
||||
// SAFETY: maps the section over a placeholder; acts only on one.
|
||||
counted(Call::Map, || unsafe {
|
||||
MapViewOfFile3(
|
||||
slice.section.handle,
|
||||
GetCurrentProcess(),
|
||||
base as *const c_void,
|
||||
(slice.index * PAGE) as u64,
|
||||
len,
|
||||
MEM_REPLACE_PLACEHOLDER,
|
||||
PAGE_READWRITE,
|
||||
std::ptr::null_mut(),
|
||||
0,
|
||||
)
|
||||
})
|
||||
};
|
||||
let own = physical.holes.remove(&base) == Some(len) && lean();
|
||||
let mut view = if own { map_view() } else { MEMORY_MAPPED_VIEW_ADDRESS { Value: std::ptr::null_mut() } };
|
||||
if view.Value as usize != base {
|
||||
// SAFETY: splits the placeholder around the range (failing
|
||||
// harmlessly when the range is the whole placeholder).
|
||||
counted(Call::Split, || unsafe {
|
||||
VirtualFree(
|
||||
base as *mut c_void,
|
||||
len,
|
||||
MEM_RELEASE | MEM_PRESERVE_PLACEHOLDER,
|
||||
)
|
||||
});
|
||||
view = map_view();
|
||||
}
|
||||
if view.Value as usize != base {
|
||||
tracing::warn!(
|
||||
address = format_args!("{base:#x}"),
|
||||
@@ -1040,7 +1200,7 @@ fn map(physical: &mut Physical, base: usize, len: usize, slice: Slice) -> bool {
|
||||
);
|
||||
return false;
|
||||
}
|
||||
if !commit(base, len) {
|
||||
if !slice.section.commit_shown(slice.index, base, len) {
|
||||
// SAFETY: the view just mapped, back to a placeholder.
|
||||
unsafe { UnmapViewOfFile2(GetCurrentProcess(), view, MEM_PRESERVE_PLACEHOLDER) };
|
||||
return false;
|
||||
@@ -1150,7 +1310,7 @@ fn unmap(physical: &mut Physical, base: usize, len: usize) {
|
||||
}
|
||||
victory_gpu::title_memory_unmapping(at, size);
|
||||
// SAFETY: a view we mapped; it becomes a placeholder again.
|
||||
unsafe {
|
||||
let gone = counted(Call::Unmap, || unsafe {
|
||||
UnmapViewOfFile2(
|
||||
GetCurrentProcess(),
|
||||
MEMORY_MAPPED_VIEW_ADDRESS {
|
||||
@@ -1158,7 +1318,10 @@ fn unmap(physical: &mut Physical, base: usize, len: usize) {
|
||||
},
|
||||
MEM_PRESERVE_PLACEHOLDER,
|
||||
)
|
||||
};
|
||||
});
|
||||
if gone != 0 && at >= base && at + size <= end {
|
||||
physical.holes.insert(at, size);
|
||||
}
|
||||
if at < base {
|
||||
map(physical, at, base - at, slice.clone());
|
||||
}
|
||||
@@ -1591,6 +1754,45 @@ mod tests {
|
||||
assert_eq!(XMemFreePhysicalPages(4, pages.as_ptr()), 1);
|
||||
}
|
||||
|
||||
/// Pages keep what they hold however often they are mapped, moved and
|
||||
/// unmapped without a commit or a split of their own.
|
||||
#[test]
|
||||
fn pages_mapped_again_hold_what_they_held() {
|
||||
let pages = allocate(3);
|
||||
let a = XMemVirtualAlloc(std::ptr::null_mut(), 8 * PAGE, MEM_RESERVE | PAGES_64K, 0, PAGE_READWRITE);
|
||||
assert!(!a.is_null());
|
||||
let at = |k: usize| a.wrapping_byte_add(k * PAGE);
|
||||
for round in 0..6_usize {
|
||||
for (k, page) in pages.iter().enumerate() {
|
||||
let to = at((round + 2 * k) % 8);
|
||||
assert_eq!(XMemMapPhysicalPages(to, 1, page), to);
|
||||
let first = to.cast::<u64>();
|
||||
// SAFETY: mapped read-write; the far end of the page too.
|
||||
unsafe {
|
||||
if round > 0 {
|
||||
assert_eq!(first.read(), (round * 16 + k - 16) as u64);
|
||||
assert_eq!(first.add(PAGE / 8 - 1).read(), k as u64);
|
||||
}
|
||||
first.write((round * 16 + k) as u64);
|
||||
first.add(PAGE / 8 - 1).write(k as u64);
|
||||
}
|
||||
}
|
||||
for k in 0..pages.len() {
|
||||
let to = at((round + 2 * k) % 8);
|
||||
assert_eq!(XMemMapPhysicalPages(to, 1, std::ptr::null()), to);
|
||||
assert_eq!(query(to).host.State, MEM_RESERVE);
|
||||
}
|
||||
// The title joins what it unmapped: the next view splits it anew.
|
||||
if round == 3 {
|
||||
// SAFETY: placeholders of ours, nothing mapped in them.
|
||||
unsafe {
|
||||
VirtualFree(a, 8 * PAGE, MEM_RELEASE | 1 /* MEM_COALESCE_PLACEHOLDERS */)
|
||||
};
|
||||
}
|
||||
}
|
||||
assert_eq!(XMemFreePhysicalPages(3, pages.as_ptr()), 1);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn mapping_no_pages_changes_nothing() {
|
||||
let pages = allocate(2);
|
||||
|
||||
@@ -109,9 +109,9 @@ pub fn dump_stacks() {
|
||||
if thread.is_null() {
|
||||
continue;
|
||||
}
|
||||
let (frames, (priority, base)) = frames_of(thread);
|
||||
let (frames, (priority, base, affinity)) = frames_of(thread);
|
||||
eprintln!(
|
||||
" thread {id} \"{}\" start {} priority {priority} (base {base})",
|
||||
" thread {id} \"{}\" start {} priority {priority} (base {base}) affinity {affinity:#x}",
|
||||
name_of(thread),
|
||||
locate(start_of(thread))
|
||||
);
|
||||
@@ -124,7 +124,7 @@ pub fn dump_stacks() {
|
||||
}
|
||||
|
||||
/// The thread's frames and its current and base scheduling priority (0-31).
|
||||
fn frames_of(thread: HANDLE) -> (Vec<u64>, (i32, i32)) {
|
||||
fn frames_of(thread: HANDLE) -> (Vec<u64>, (i32, i32, usize)) {
|
||||
let mut frames = Vec::with_capacity(MAX_FRAMES);
|
||||
let mut info: BasicInfo = BasicInfo {
|
||||
exit_status: 0,
|
||||
@@ -138,7 +138,7 @@ fn frames_of(thread: HANDLE) -> (Vec<u64>, (i32, i32)) {
|
||||
// the query and the read below.
|
||||
// SAFETY: a thread of this process that is not this one.
|
||||
if unsafe { SuspendThread(thread) } == u32::MAX {
|
||||
return (frames, (0, 0));
|
||||
return (frames, (0, 0, 0));
|
||||
}
|
||||
// SAFETY: writes `info`, whose layout and size are the system's.
|
||||
let status = unsafe {
|
||||
@@ -153,7 +153,7 @@ fn frames_of(thread: HANDLE) -> (Vec<u64>, (i32, i32)) {
|
||||
if status != 0 || info.teb.is_null() {
|
||||
// SAFETY: suspended above.
|
||||
unsafe { ResumeThread(thread) };
|
||||
return (frames, (0, 0));
|
||||
return (frames, (0, 0, 0));
|
||||
}
|
||||
// SAFETY: the TEB of a suspended thread of this process: its NT_TIB
|
||||
// holds the stack base at +8 and the stack limit at +0x10.
|
||||
@@ -170,7 +170,7 @@ fn frames_of(thread: HANDLE) -> (Vec<u64>, (i32, i32)) {
|
||||
}
|
||||
ResumeThread(thread);
|
||||
}
|
||||
(frames, (info.priority, info.base_priority))
|
||||
(frames, (info.priority, info.base_priority, info.affinity))
|
||||
}
|
||||
|
||||
fn start_of(thread: HANDLE) -> usize {
|
||||
|
||||
Reference in new issue
Block a user