Of 281 ms the title's threads spent in the write-fault handler over 14 frames of play, 252 ms were waits for the guards' lock (timeline tl.190; tl.130 and tl2's tl.185 the same): faults queued behind other faults and behind refreshes guarding, up to 5 ms, while a fault's own work took 8 microseconds at the median. With the switch a fault takes the block's bit in OPENING (whoever finds it set waits up to 100 microseconds for that fault, then goes the locked way), tells the other views of the page as written when the block is guarded with them (ALIASED, a mirror of Guards::aliased: TOLD and BESIDE set, PUBLISHED cleared), clears the block's PUBLISHED, sets its BESIDE, changes the protection itself and queues the block. The next holder of the lock settles the guards' notes before anything else (guards -> settle). Until then the holders of the lock treat OPENING and TOLD blocks as written and leave them as they are (guard), publish takes its bit back if it finds one, and protect_alias does not count an OPENING block as read-only for another view. OPENING's doc comment gives the argument. Also: BESIDE is forgotten when the mapping of a block changes (the block is told as written then anyway; a bit left behind kept a hole mapped at the address from being known by its bits). asked_for! moves to switch.rs for both files. Diagnostics: the line of the trackers' lock waits counts faults opened under the lock (waiting, holding), without it, and those that went the locked way after all; the fault book has What::Unlocked. Tests: the single-thread tests whose writes fault, again with faults that take no lock; the many-askers test with all and half the writers' faults taking no lock; a fault settled by the next holder; what holders do with an unsettled block; eight threads faulting on the same blocks at once through two views; ranges of bits cleared. Co-Authored-By: Claude Opus 5.5 <[email protected]>
99 lines
3.0 KiB
Rust
99 lines
3.0 KiB
Rust
//! The `VICTORY_*` on/off switches (`VICTORY_D3D12_DEBUG=1` and its siblings).
|
|
|
|
use std::ffi::OsStr;
|
|
|
|
/// Whether a switch's value turns it on: it is set, and is not `0`.
|
|
///
|
|
/// A switch used to be on whenever its variable existed, so `VICTORY_D3D12_WARP=0` (typed to switch it off, or left
|
|
/// over from comparing two runs) still selected the software adapter. Any other value keeps the old meaning.
|
|
fn value_is_on(value: Option<&OsStr>) -> bool {
|
|
value.is_some_and(|v| v != "0")
|
|
}
|
|
|
|
/// Whether the environment variable `name` switches something on.
|
|
pub(crate) fn on(name: &str) -> bool {
|
|
value_is_on(std::env::var_os(name).as_deref())
|
|
}
|
|
|
|
/// A switch that is on unless its variable is `0`, read once; with
|
|
/// `VICTORY_AB` naming it (diagnostics builds, `crate::ab`), on and off while
|
|
/// the title runs.
|
|
#[cfg(windows)]
|
|
#[macro_export]
|
|
#[doc(hidden)]
|
|
macro_rules! toggled {
|
|
($name:literal) => {{
|
|
#[cfg(feature = "diagnostics")]
|
|
{
|
|
static BIT: std::sync::OnceLock<Result<bool, u32>> = std::sync::OnceLock::new();
|
|
match BIT.get_or_init(|| match $crate::ab::bit($name) {
|
|
Some(bit) => Err(bit),
|
|
None => Ok(std::env::var_os($name).is_none_or(|v| v != "0")),
|
|
}) {
|
|
Ok(on) => *on,
|
|
Err(bit) => $crate::ab::on(*bit),
|
|
}
|
|
}
|
|
#[cfg(not(feature = "diagnostics"))]
|
|
{
|
|
static ON: std::sync::OnceLock<bool> = std::sync::OnceLock::new();
|
|
*ON.get_or_init(|| std::env::var_os($name).is_none_or(|v| v != "0"))
|
|
}
|
|
}};
|
|
}
|
|
|
|
/// A switch that is off unless its variable is `1`, read once; with
|
|
/// `VICTORY_AB` naming it (diagnostics builds), on and off while the title
|
|
/// runs.
|
|
#[cfg(windows)]
|
|
#[macro_export]
|
|
#[doc(hidden)]
|
|
macro_rules! asked_for {
|
|
($name:literal) => {{
|
|
static ASKED: std::sync::OnceLock<Result<bool, u32>> = std::sync::OnceLock::new();
|
|
let asked = ASKED.get_or_init(|| {
|
|
#[cfg(feature = "diagnostics")]
|
|
if let Some(bit) = $crate::ab::bit($name) {
|
|
return Err(bit);
|
|
}
|
|
Ok(std::env::var_os($name).is_some_and(|v| v == "1"))
|
|
});
|
|
match *asked {
|
|
Ok(on) => on,
|
|
#[cfg(feature = "diagnostics")]
|
|
Err(bit) => $crate::ab::on(bit),
|
|
#[cfg(not(feature = "diagnostics"))]
|
|
Err(_) => false,
|
|
}
|
|
}};
|
|
}
|
|
|
|
#[cfg(test)]
|
|
mod tests {
|
|
use super::value_is_on;
|
|
use std::ffi::OsStr;
|
|
|
|
#[test]
|
|
fn unset_is_off() {
|
|
assert!(!value_is_on(None));
|
|
}
|
|
|
|
#[test]
|
|
fn zero_is_off() {
|
|
assert!(!value_is_on(Some(OsStr::new("0"))));
|
|
}
|
|
|
|
#[test]
|
|
fn one_is_on() {
|
|
assert!(value_is_on(Some(OsStr::new("1"))));
|
|
}
|
|
|
|
#[test]
|
|
fn other_values_keep_their_old_meaning() {
|
|
// Documented as `=1`; anything else that was on before stays on: only `0` is new.
|
|
for value in ["", "true", "yes", "2", "00", " 0", "0 "] {
|
|
assert!(value_is_on(Some(OsStr::new(value))), "{value:?}");
|
|
}
|
|
}
|
|
}
|