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
32 changes: 30 additions & 2 deletions apps/rustgis-cli/tests/memory_override.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,36 @@ fn the_memory_override_sets_the_limits_and_says_so() {
// 1 GiB: a quarter is the undo history's 256 MiB floor.
assert_eq!(run(Some("1073741824")).0["undoBytes"], json!(256_u64 << 20));
// Without it, or with a value that isn't a positive number of bytes, nothing changes or is said.
let (plain, quiet) = run(None);
let (mut plain, quiet) = run(None);
assert_ne!(plain["memorySource"], json!("override"));
assert!(!quiet.contains("RUSTGIS_MEMORY_BYTES"), "{quiet}");
assert_eq!(run(Some("8 GiB")).0, plain);
// What the computer has free is read live (#2431), so it can differ between the two runs, and
// the limits it bounds too: they are compared only where much is free.
let mut junk = run(Some("8 GiB")).0;
let (a, b) = (plain["freeBytes"].take(), junk["freeBytes"].take());
let roomy = |free: &Value| free.as_u64().is_none_or(|f| f > plain["memory"].as_u64().unwrap_or(0) / 2);
if roomy(&a) && roomy(&b) {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Test coverage (low confidence): the equality check runs only when both runs have more than half the memory free. On a busy machine it is skipped and the test asserts nothing about the override. The comment says the limits are compared "only where much is free", so this is deliberate, but it means the test can pass without checking anything.

Suggested fix: when the check is skipped, assert the limits that don't depend on free memory (fileBytes and the rest, per the share rules) still match, or make the free figure fixed in this test.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Left for the release quality pass.

assert_eq!(junk, plain);
}
}

/// What the process holds reaches `gp.env` (#2431): with the sample project open it is more than
/// nothing, and on a computer that size it is far below half the memory, so the limits are the
/// ones with nothing held.
#[test]
fn gp_env_says_what_is_held() {
let out = Command::new(env!("CARGO_BIN_EXE_rustgis-cli"))
.args(["run", "--no-plugins", "--worldview", "default", "--sample", "--cmd", "gp.env"])
.env("RUSTGIS_MEMORY_BYTES", "17179869184")
.env_remove("RUSTGIS_WORLDVIEW")
.output()
.unwrap();
assert!(out.status.success(), "{}", String::from_utf8_lossy(&out.stderr));
let limits = serde_json::from_slice::<Value>(&out.stdout).unwrap()["limits"].clone();
let held = limits["heldBytes"].as_u64().unwrap();
assert!(held > 0 && held < 8 << 30, "{held}");
// With the override, what else runs on the computer doesn't change the limits.
assert_eq!(limits["freeBytes"], Value::Null);
// 16 GiB plans for 4 GiB: 512 MiB of file.
assert_eq!(limits["fileBytes"], json!(512_u64 << 20));
}
3 changes: 2 additions & 1 deletion crates/analysis/src/density.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,9 @@ pub const DENSITY_CELLS_FLOOR: usize = 16_000_000;
/// a quarter of the computer's memory holds at 16 bytes a cell (the sums, the output and the
/// GeoTIFF written from it), from [`DENSITY_CELLS_FLOOR`] up to the most any tool's output raster
/// takes ([`crate::rasterops::PIXELS_CEILING`]) (#1291 step 2).
/// Less once RustGIS holds more than half the memory, never below the floor ([`rustgis_data::memory::planned`], #2431).
pub fn max_density_cells() -> usize {
rustgis_data::memory::scaled(rustgis_data::memory::physical(), DENSITY_CELLS_FLOOR, crate::rasterops::PIXELS_CEILING, 16)
rustgis_data::memory::planned(DENSITY_CELLS_FLOOR, crate::rasterops::PIXELS_CEILING, 16)
}

/// Upper limit on aggregate cell visits across all points on one thread, to prevent CPU
Expand Down
3 changes: 2 additions & 1 deletion crates/analysis/src/rasterops.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,9 @@ pub const PIXELS_CEILING: usize = 512_000_000;
/// Largest output raster built (pixels): on the desktop, as many as a quarter of the computer's
/// memory holds at 16 bytes each (a tool holds its input, its output and a working grid or two),
/// from [`PIXELS_FLOOR`] up to [`PIXELS_CEILING`] (#1291 step 2).
/// Less once RustGIS holds more than half the memory, never below the floor ([`rustgis_data::memory::planned`], #2431).
pub fn max_pixels() -> usize {
rustgis_data::memory::scaled(rustgis_data::memory::physical(), PIXELS_FLOOR, PIXELS_CEILING, 16)
rustgis_data::memory::planned(PIXELS_FLOOR, PIXELS_CEILING, 16)
}

/// Room for a grid of `w × h` cells of `T` (an output band, or a working grid of one), empty: a
Expand Down
3 changes: 2 additions & 1 deletion crates/analysis/src/tools.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1590,8 +1590,9 @@ const BUFFER_STORAGE_ERROR: &str = "Buffer output or distance selectors exceed t

/// Buffer's retained-storage budget on this computer: on the desktop a quarter of its memory, from
/// [`MAX_BUFFER_BYTES`] up to [`BUFFER_BYTES_CEILING`] (#1291 step 2); fixed in the browser.
/// Less once RustGIS holds more than half the memory, never below the floor ([`rustgis_data::memory::planned`], #2431).
pub fn max_buffer_bytes() -> usize {
rustgis_data::memory::scaled(rustgis_data::memory::physical(), MAX_BUFFER_BYTES, BUFFER_BYTES_CEILING, 1)
rustgis_data::memory::planned(MAX_BUFFER_BYTES, BUFFER_BYTES_CEILING, 1)
}

/// [`BUFFER_STORAGE_ERROR`] with the budget it ran out of, so a run refused here but not on a
Expand Down
119 changes: 94 additions & 25 deletions crates/data/src/io.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,19 +29,27 @@ const MEMORY_PER_BYTE: u64 = 8;

/// Largest file read into memory on this computer (#2408): on the desktop as much as a quarter of
/// its memory holds at [`MEMORY_PER_BYTE`] a byte, from [`BYTES_FLOOR`] up to [`BYTES_CEILING`],
/// as the other memory limits follow it (#1291 step 2); [`BYTES_FLOOR`] in the browser.
/// as the other memory limits follow it (#1291 step 2); [`BYTES_FLOOR`] in the browser. Less once
/// RustGIS holds more than half the memory, or the computer has less free ([`crate::memory::share`],
/// #2431), never below the floor.
pub fn max_bytes() -> u64 {
if let Some(max) = TEST_MAX.with(std::cell::Cell::get) {
return max;
}
max_bytes_for(crate::memory::physical())
max_bytes_within(crate::memory::share(crate::memory::physical(), crate::memory::held(), crate::memory::available()))
}

/// [`max_bytes`] on a computer with `memory` bytes (`None`: not known). The formula is
/// [`crate::memory::scaled`]'s, worked out here in `u64`, as that one counts in `usize`, which
/// doesn't hold 4 GiB on a 32-bit build.
/// [`max_bytes`] on a computer with `memory` bytes (`None`: not known) with nothing held. The
/// formula is [`crate::memory::scaled`]'s, worked out here in `u64`, as that one counts in `usize`,
/// which doesn't hold 4 GiB on a 32-bit build.
fn max_bytes_for(memory: Option<u64>) -> u64 {
memory.map_or(BYTES_FLOOR, |m| (m / crate::memory::SHARE / MEMORY_PER_BYTE).clamp(BYTES_FLOOR, bytes_ceiling()))
max_bytes_within(memory.map(|m| m / crate::memory::SHARE))
}

/// [`max_bytes`] when a job plans for `share` bytes ([`crate::memory::share`]; `None`: not known):
/// less once RustGIS holds more than half the memory (#2431), never below [`BYTES_FLOOR`].
fn max_bytes_within(share: Option<u64>) -> u64 {
share.map_or(BYTES_FLOOR, |share| (share / MEMORY_PER_BYTE).clamp(BYTES_FLOOR, bytes_ceiling()))
}

/// The most [`max_bytes`] grows to in this build: [`BYTES_CEILING`] on a 64-bit desktop build. A
Expand All @@ -59,15 +67,39 @@ pub fn too_large(len: u64) -> String {
too_large_said(len, max_bytes())
}

/// [`too_large`] against the limit `max` taken when the read started.
/// [`too_large`] against the limit `max` taken when the read started. What is held and free is
/// read again here: it may have moved since `max` was taken, so the figures said are the ones now,
/// beside the limit the read was held to.
fn too_large_said(len: u64, max: u64) -> String {
let follows = crate::memory::physical().is_some() && bytes_ceiling() > BYTES_FLOOR;
let limit = if follows {
format!("{} on this computer, a limit that follows its memory", size_said(max, false))
} else {
size_said(max, false)
let memory = crate::memory::physical().filter(|_| bytes_ceiling() > BYTES_FLOOR);
// A test's own limit isn't lowered by what is held or free.
let (held, free) =

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Consistency (low confidence): too_large_said reads held() and available() again after the caller has already decided the limit, so the refusal can report a free figure that differs from the one that lowered the limit. Say so, or pass the figures in from the decision (share's inputs) so the message and the refusal agree.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed in f2c251d: the doc comment of too_large_said now says it. What is held and what is free are read again for the message, so they are the figures now, beside the limit the read was held to. Passing them through from where the limit was decided would mean changing every caller for a message. The shapefile zip loop now reads the limit once for the whole run, which also covers the out-of-diff point about the loop.

if TEST_MAX.with(std::cell::Cell::get).is_some() { (0, None) } else { (crate::memory::held(), crate::memory::available()) };
too_large_held(len, max, memory, held, free)
}

/// [`too_large_said`] on a computer with `memory` bytes (`None`: the limit doesn't follow it)
/// while RustGIS holds `held` and `free` is free (`None`: not known): when the limit is below the
/// one with nothing else in use, it says what is held and free, that limit, and what to do (#2431).
fn too_large_held(len: u64, max: u64, memory: Option<u64>, held: u64, free: Option<u64>) -> String {
let file = size_said(len, true);
let Some(memory) = memory else {
return format!("file is too large ({file}; the most read into memory is {})", size_said(max, false));
};
format!("file is too large ({}; the most read into memory is {limit})", size_said(len, true))
let full = max_bytes_for(Some(memory));
if max >= full || (held == 0 && free.is_none()) {
return format!(
"file is too large ({file}; the most read into memory is {} on this computer, a limit that follows its memory)",
size_said(max, false)
);
}
let free = free.map(|free| format!(" and {} free", size_said(free, false))).unwrap_or_default();
format!(
"file is too large ({file}; the most read into memory is {} on this computer now, with RustGIS holding {}{free}, {} otherwise): free memory by removing layers or tables you no longer need (save and reopen the project to let go of what Undo keeps of them) or closing other programs, then try again",
size_said(max, false),
size_said(held, true),
size_said(full, false)
)
}

/// A size in MiB, rounded `up` (a file's, so it never looks within the limit) or down (the
Expand Down Expand Up @@ -2310,9 +2342,10 @@ fn read_gzip_text_budget(

/// Estimated memory for retained CSV rows and the dataset built alongside them: a quarter
/// of physical memory, between 256 MiB (also the browser limit) and 4 GiB.
/// Less once RustGIS holds more than half the memory, never below the floor ([`crate::memory::planned`], #2431).
fn csv_memory_budget() -> usize {
let ceiling = usize::try_from(4u64 << 30).unwrap_or(usize::MAX);
crate::memory::scaled(crate::memory::physical(), 256 << 20, ceiling, 1)
crate::memory::planned(256 << 20, ceiling, 1)
}

/// Collects records for type inference, charging text and row/cell objects before copying.
Expand Down Expand Up @@ -2943,7 +2976,9 @@ pub fn gather_shapefiles(files: Vec<(String, Vec<u8>)>) -> (Vec<(String, Vec<u8>
}
}
// The zip must open again: within the read size and the zip reader's inflated total.
let limit = crate::zip::MAX_TOTAL.min(usize::try_from(max_bytes()).unwrap_or(usize::MAX));
// Read once for the whole run: it reads what the computer has free (#2431).
let max = max_bytes();
let limit = crate::zip::MAX_TOTAL.min(usize::try_from(max).unwrap_or(usize::MAX));
let (mut out, mut problems) = (Vec::new(), Vec::new());
for (file, parts) in files.into_iter().zip(zips) {
out.extend(file);
Expand All @@ -2955,8 +2990,8 @@ pub fn gather_shapefiles(files: Vec<(String, Vec<u8>)>) -> (Vec<(String, Vec<u8>
let total = parts.iter().fold(0usize, |sum, (_, b)| sum.saturating_add(b.len()));
// Said by the limit it passed: the size read into memory (which follows the computer's
// memory), or the most a zip expands to.
if total as u64 > max_bytes() {
problems.push(format!("{shp}: its data files together: {}", too_large(total as u64)));
if total as u64 > max {
problems.push(format!("{shp}: its data files together: {}", too_large_said(total as u64, max)));
continue;
}
if total > limit {
Expand All @@ -2968,8 +3003,8 @@ pub fn gather_shapefiles(files: Vec<(String, Vec<u8>)>) -> (Vec<(String, Vec<u8>
continue;
}
match crate::zip::write(&parts) {
Ok(zip) if zip.len() as u64 <= max_bytes() => out.push((zip_name, zip)),
Ok(zip) => problems.push(format!("{shp}: zipped together: {}", too_large(zip.len() as u64))),
Ok(zip) if zip.len() as u64 <= max => out.push((zip_name, zip)),
Ok(zip) => problems.push(format!("{shp}: zipped together: {}", too_large_said(zip.len() as u64, max))),
Err(e) => problems.push(format!("{shp}: its data files couldn't be zipped together: {e}")),
}
}
Expand Down Expand Up @@ -4768,6 +4803,34 @@ mod tests {
use super::*;
use std::io::Write;

/// A file refused because RustGIS holds much of the memory says so, with the limit when nothing
/// is held and what to do; refused at the full limit, or where the limit doesn't follow the
/// memory, it says what it said before (#2431).
#[test]
fn a_file_refused_while_memory_is_held_says_so() {
const GIB: u64 = 1 << 30;
if bytes_ceiling() == BYTES_FLOOR {
return; // A phone or a 32-bit build: the limit doesn't follow the memory.
}
let lowered = too_large_held(2 * GIB, GIB, Some(64 * GIB), 40 * GIB, None);
assert_eq!(
lowered,
"file is too large (2048 MiB; the most read into memory is 1024 MiB on this computer now, with RustGIS holding 40960 MiB, 2048 MiB otherwise): free memory by removing layers or tables you no longer need (save and reopen the project to let go of what Undo keeps of them) or closing other programs, then try again"
);
let busy = too_large_held(GIB, 512 << 20, Some(64 * GIB), GIB, Some(5 * GIB));
assert!(busy.contains("with RustGIS holding 1024 MiB and 5120 MiB free, 2048 MiB otherwise"), "{busy}");
let full = too_large_held(3 * GIB, 2 * GIB, Some(64 * GIB), GIB, Some(60 * GIB));
assert_eq!(
full,
"file is too large (3072 MiB; the most read into memory is 2048 MiB on this computer, a limit that follows its memory)"
);
assert_eq!(too_large_held(3 * GIB, 2 * GIB, Some(64 * GIB), 0, None), full);
assert_eq!(
too_large_held(GIB, 256 << 20, None, 40 * GIB, None),
"file is too large (1024 MiB; the most read into memory is 256 MiB)"
);
}

/// The size read into memory follows the computer's memory between its floor and ceiling,
/// and stays at the floor where the memory isn't known (the browser) (#2408).
#[test]
Expand All @@ -4785,17 +4848,23 @@ mod tests {
assert_eq!(max_bytes_for(Some(16 * GIB)), BYTES_FLOOR);
assert_eq!(max_bytes_for(Some(1024 * GIB)), BYTES_FLOOR);
}
// The test limit is the calling thread's, and comes back after it, nested too.
let real = max_bytes();
// Holding more than half the memory lowers it, never below the floor (#2431).
if bytes_ceiling() > BYTES_FLOOR {
assert_eq!(max_bytes_within(crate::memory::share(Some(16 * GIB), 8 * GIB, None)), 512 << 20, "half held: unchanged");
assert_eq!(max_bytes_within(crate::memory::share(Some(64 * GIB), 40 * GIB, None)), GIB);
assert_eq!(max_bytes_within(crate::memory::share(Some(16 * GIB), 12 * GIB, None)), BYTES_FLOOR);
}
// The test limit is the calling thread's, and comes back after it, nested too. The real one
// follows what is free now (#2431), so it is only compared with its floor.
with_max_bytes(10, || {
assert_eq!(max_bytes(), 10);
with_max_bytes(20, || assert_eq!(max_bytes(), 20));
assert_eq!(max_bytes(), 10);
assert_eq!(std::thread::spawn(max_bytes).join().unwrap(), real);
assert!(std::thread::spawn(max_bytes).join().unwrap() >= BYTES_FLOOR);
});
assert_eq!(max_bytes(), real);
let said = too_large(3 << 20);
assert!(said.starts_with("file is too large (3 MiB;") && said.contains(&format!("{} MiB", real >> 20)), "{said}");
assert!(max_bytes() >= BYTES_FLOOR);
let said = with_max_bytes(1 << 20, || too_large(3 << 20));
assert!(said.starts_with("file is too large (3 MiB;") && said.contains("is 1 MiB"), "{said}");
}

/// A file past the limit is refused before it is parsed, by each reader that takes it whole,
Expand Down
Loading
Loading