Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -341,6 +341,9 @@ Fill the reusable DXIL cache ahead of a run with the read-only package wrapper.
- `XCL_MUTE_TTS`: the title gets no Windows speech synthesis: its XAccessibility requests are
answered by XCL's own object, which has closed captions and high contrast only. Off by
default; any value except empty, `0`, `off` or `false` (in any case) turns it on.
- `XCL_RAM_MB=<MB>` / `XCL_RAM_MB=auto`: a hard maximum for the process's working set (unset: none);
Windows then takes the pages used least out of RAM as it grows past it. `auto` keeps
`XCL_RAM_FREE_MB` (6144 unless given) of the system's RAM available. Commit is unchanged.
- `XCL_FRAME_INTERVAL_US`: changes the console frame clock. Unset, the title's own frame
interval (`SetFrameIntervalX`) is kept. `0`, `off` or `uncapped` removes the cap, so
`WaitFrameEventX` returns at once with a fresh token. A number of microseconds is an upper
Expand Down
1 change: 1 addition & 0 deletions crates/xcl/src/host.rs
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ pub fn host_main(boot_path: &Path) -> i32 {
std::env::set_var("XCL_UMA_MB", boot.config.uma_mb.to_string());
}
crate::state::set_boot(boot.clone());
crate::ram_budget::start();
if let Err(e) = crate::hle::d3d12x::present::preparation::before_guest(&boot) {
crate::xlog_error!("Shader preparation: {e}");
eprintln!("xcl: {e}");
Expand Down
1 change: 1 addition & 0 deletions crates/xcl/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ pub mod loader;
pub mod modules;
pub mod pe;
pub mod preflight;
pub mod ram_budget;
pub mod report;
pub mod resolver;
pub mod rpf;
Expand Down
194 changes: 194 additions & 0 deletions crates/xcl/src/ram_budget.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,194 @@
//! A budget for the RAM the process holds (`XCL_RAM_MB`).
//!
//! A console title's GPU memory is its only copy of what is in it. Here the title's pages are
//! RAM, and what the GPU reads of them is copied to video memory: a retail title's world held
//! 5 GB of RAM in pages whose copy was on the card (3.1 GB of streamed physical pages, 1.6 GB
//! of GPU heaps, the coherent window), of 13.8 GB in all, and most of those pages are not
//! touched again once they are uploaded. Windows only takes such pages out of a process when
//! RAM is short for the whole system – on a 32 GB PC with 15 GB in use by other programs that
//! was at under 4 GB free, where a guard had ended the run.
//!
//! With a budget the process's working set has a hard maximum: Windows takes the pages used
//! least out of it as it grows past that. They stay in RAM (the standby and modified lists) while
//! nothing else needs it, and a page touched again comes back with a soft fault; what the
//! system then needs is written to the page file. The process's commit is as before.
//!
//! `XCL_RAM_MB=<MB>`: that maximum. `XCL_RAM_MB=auto`: the maximum follows the RAM the system
//! has available, a second at a time, so that `XCL_RAM_FREE_MB` (6144 unless given) stay
//! available. Unset: no budget.

use windows_sys::Win32::System::ProcessStatus::{K32GetProcessMemoryInfo, PROCESS_MEMORY_COUNTERS};
use windows_sys::Win32::System::SystemInformation::{GlobalMemoryStatusEx, MEMORYSTATUSEX};
use windows_sys::Win32::System::Memory::SetProcessWorkingSetSizeEx;
use windows_sys::Win32::System::Threading::GetCurrentProcess;

const QUOTA_LIMITS_HARDWS_MIN_DISABLE: u32 = 0x2;
const QUOTA_LIMITS_HARDWS_MAX_ENABLE: u32 = 0x4;
const QUOTA_LIMITS_HARDWS_MAX_DISABLE: u32 = 0x8;

const MB: u64 = 1 << 20;
/// no budget under this: the title's own threads' pages alone are more
const LEAST: u64 = 4096 * MB;
/// the most an automatic budget moves in one step (a second): pages taken out are available
/// only once they are written to the page file, and the next step would take them out again
const STEP: u64 = 512 * MB;

#[derive(Clone, Copy, Debug, PartialEq, Eq)]
enum Setting {
Fixed(u64),
/// keep this many bytes available
Auto(u64),
}

fn setting_of(ram: Option<&str>, free: Option<&str>) -> Option<Setting> {
let ram = ram?.trim();
if ram.eq_ignore_ascii_case("auto") {
// (a number of MB too large for bytes: the default)
let free = free.and_then(|v| v.trim().parse::<u64>().ok()).and_then(|mb| mb.checked_mul(MB)).unwrap_or(6144 * MB);
return Some(Setting::Auto(free));
}
ram.parse::<u64>().ok().filter(|&mb| mb > 0).and_then(|mb| mb.checked_mul(MB)).map(|bytes| Setting::Fixed(bytes.max(LEAST)))
}

/// The next maximum of an automatic budget: the working set `ws` may grow by what is available
/// beyond `keep`, and has to shrink by what is missing; `before` (None: no maximum yet) moves
/// by `STEP` at most. None: no maximum is needed (more than `STEP` to spare), also when one was
/// set before (RAM came back).
fn next_max(before: Option<u64>, ws: u64, available: u64, keep: u64) -> Option<u64> {
if available.saturating_sub(keep) > STEP {
return None;
}
let want = ws.saturating_add(available).saturating_sub(keep).max(LEAST);
match before {
None => Some(want.max(ws.saturating_sub(STEP))),
Some(b) => Some(want.clamp(b.saturating_sub(STEP), b.saturating_add(STEP)).max(LEAST)),
}
}

fn working_set() -> u64 {
unsafe {
let mut c: PROCESS_MEMORY_COUNTERS = std::mem::zeroed();
c.cb = std::mem::size_of_val(&c) as u32;
K32GetProcessMemoryInfo(GetCurrentProcess(), &mut c, c.cb);
c.WorkingSetSize as u64
}
}

/// The RAM the system has available now, in bytes.
pub(crate) fn available() -> u64 {
unsafe {
let mut m: MEMORYSTATUSEX = std::mem::zeroed();
m.dwLength = std::mem::size_of_val(&m) as u32;
GlobalMemoryStatusEx(&mut m);
m.ullAvailPhys
}
}

/// The hard maximum of the working set is `max` (None: none). False if Windows refuses.
fn set_max(max: Option<u64>) -> bool {
let (bytes, flags) = match max {
Some(m) => (m as usize, QUOTA_LIMITS_HARDWS_MIN_DISABLE | QUOTA_LIMITS_HARDWS_MAX_ENABLE),
None => (usize::MAX >> 1, QUOTA_LIMITS_HARDWS_MIN_DISABLE | QUOTA_LIMITS_HARDWS_MAX_DISABLE),
};
// (the minimum is not a hard one: a number below the maximum)
unsafe { SetProcessWorkingSetSizeEx(GetCurrentProcess(), 64 << 20, bytes.max(128 << 20), flags) != 0 }
}

/// Start the budget `XCL_RAM_MB` asks for (nothing without it).
pub fn start() {
let Some(setting) = setting_of(std::env::var("XCL_RAM_MB").ok().as_deref(), std::env::var("XCL_RAM_FREE_MB").ok().as_deref()) else { return };
match setting {
Setting::Fixed(max) => {
let ok = set_max(Some(max));
crate::xlog_info!("RAM budget: XCL_RAM_MB – the process's working set is kept under {} MB{}", max / MB, if ok { "" } else { " (Windows refused)" });
}
Setting::Auto(keep) => {
crate::xlog_info!("RAM budget: XCL_RAM_MB=auto – the process's working set is kept so that {} MB of RAM stay available", keep / MB);
let _ = std::thread::Builder::new().name("xcl-ram-budget".into()).spawn(move || {
let mut max: Option<u64> = None;
let mut ticks = 0u32;
loop {
std::thread::sleep(std::time::Duration::from_secs(1));
let (ws, avail) = (working_set(), available());
let next = next_max(max, ws, avail, keep);
if next != max {
if !set_max(next) {
crate::xlog_warn!("RAM budget: Windows refused a working set maximum of {:?} MB", next.map(|m| m / MB));
return;
}
if max.is_none() || next.is_none() {
crate::xlog_info!(
"RAM budget: {} MB of RAM available with a working set of {} MB: {}",
avail / MB,
ws / MB,
next.map_or("no maximum".to_string(), |m| format!("a maximum of {} MB from here on", m / MB))
);
}
max = next;
}
ticks += 1;
if ticks % 20 == 0 {
if let Some(m) = max {
crate::xlog_info!("RAM budget: working set {} MB under a maximum of {} MB, {} MB of RAM available", ws / MB, m / MB, avail / MB);
}
}
}
});
}
}
}

#[cfg(test)]
mod tests {
use super::*;

#[test]
fn the_setting_is_a_number_of_megabytes_or_auto() {
assert_eq!(setting_of(None, None), None);
assert_eq!(setting_of(Some("0"), None), None);
assert_eq!(setting_of(Some("lots"), None), None);
assert_eq!(setting_of(Some(" 10000 "), None), Some(Setting::Fixed(10000 * MB)));
assert_eq!(setting_of(Some("100"), None), Some(Setting::Fixed(LEAST)), "not under the least");
assert_eq!(setting_of(Some("auto"), None), Some(Setting::Auto(6144 * MB)));
assert_eq!(setting_of(Some("AUTO"), Some("8000")), Some(Setting::Auto(8000 * MB)));
}

#[test]
fn an_automatic_maximum_follows_the_ram_available() {
let keep = 6144 * MB;
// plenty available: no maximum
assert_eq!(next_max(None, 5000 * MB, 12000 * MB, keep), None);
// what is available comes near what is kept: the working set may grow by the difference
assert_eq!(next_max(None, 9000 * MB, 6500 * MB, keep), Some(9356 * MB));
// less available than kept: the working set has to shrink by what is missing, a step at most
assert_eq!(next_max(Some(12000 * MB), 12000 * MB, 5944 * MB, keep), Some(11800 * MB));
assert_eq!(next_max(Some(12000 * MB), 12000 * MB, 4000 * MB, keep), Some(12000 * MB - STEP));
assert_eq!(next_max(None, 12000 * MB, 4000 * MB, keep), Some(12000 * MB - STEP));
// RAM comes back: the maximum goes up again, a step at a time
assert_eq!(next_max(Some(10000 * MB), 10000 * MB, 6400 * MB, keep), Some(10256 * MB));
assert_eq!(next_max(Some(8000 * MB), 10000 * MB, 6600 * MB, keep), Some(8000 * MB + STEP));
// and more than a step to spare again: no maximum
assert_eq!(next_max(Some(10000 * MB), 10000 * MB, 9000 * MB, keep), None);
assert_eq!(next_max(Some(LEAST), LEAST, 16_384 * MB, keep), None);
// never under the least
assert_eq!(next_max(Some(LEAST), 4096 * MB, 1000 * MB, keep), Some(LEAST));
}

/// A number of MB too large for bytes is no setting (`XCL_RAM_MB`) or the default reserve
/// (`XCL_RAM_FREE_MB`), and a reserve as large as can be does not overflow.
#[test]
fn oversized_megabyte_settings_do_not_overflow() {
assert_eq!(setting_of(Some("17592186044416"), None), None);
assert_eq!(setting_of(Some("auto"), Some("17592186044416")), Some(Setting::Auto(6144 * MB)));
let largest = (u64::MAX / MB) * MB;
assert_eq!(next_max(None, LEAST, 0, largest), Some(LEAST));
assert_eq!(next_max(Some(u64::MAX), u64::MAX, 0, 0), Some(u64::MAX));
}

/// Windows takes a hard maximum for this process's working set, and lifts it again.
#[test]
fn windows_takes_a_working_set_maximum_and_lifts_it() {
assert!(set_max(Some(working_set() + 4096 * MB)));
assert!(set_max(None));
}
}
Loading