Skip to content

Commit

Permalink
Fix #2229 detect available memory
Browse files Browse the repository at this point in the history
This doesn't implement streaming IO for low memory situations - we still
have the situation that low footprint situations will fail to install,
but while it is the case that rustc's memory footprint is lower than our
unpack footprint this is probably not urgent to fix, though I will get
around to it.

Being less aggressive about unpack buffer size though should reduce the
number of support tickets from folk in these cases, I hope.

We may end up getting tickets from folk with broken ulimit syscalls
though, who knows.
  • Loading branch information
rbtcollins committed Feb 22, 2020
1 parent 72bea7b commit 53497af
Show file tree
Hide file tree
Showing 8 changed files with 106 additions and 45 deletions.
99 changes: 64 additions & 35 deletions Cargo.lock

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 25,7 @@ chrono = "0.4"
clap = "2"
download = { path = "download" }
error-chain = "0.12"
effective-limits = "0.2"
flate2 = "1"
git-testament = "0.1.4"
home = "0.5"
Expand Down
40 changes: 30 additions & 10 deletions src/dist/component/package.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 8,7 @@ use crate::dist::component::transaction::*;
use crate::dist::temp;
use crate::errors::*;
use crate::utils::notifications::Notification;
use crate::utils::units;
use crate::utils::utils;

use std::collections::{HashMap, HashSet};
Expand Down Expand Up @@ -163,19 164,37 @@ struct MemoryBudget {
used: usize,
}

static mut RAM_NOTICE_SHOWN: bool = false;

// Probably this should live in diskio but ¯\_(ツ)_/¯
impl MemoryBudget {
fn new(max_file_size: usize) -> Self {
const DEFAULT_UNPACK_RAM: usize = 400 * 1024 * 1024;
let unpack_ram = if let Ok(budget_str) = env::var("RUSTUP_UNPACK_RAM") {
if let Ok(budget) = budget_str.parse::<usize>() {
budget
} else {
DEFAULT_UNPACK_RAM
fn new(max_file_size: usize, effective_max_ram: usize) -> Self {
const DEFAULT_UNPACK_RAM_MAX: usize = 500 * 1024 * 1024;
let ram_for_unpacking = effective_max_ram - (100 * 1024 * 1024);
let default_max_unpack_ram = std::cmp::min(DEFAULT_UNPACK_RAM_MAX, ram_for_unpacking);
let unpack_ram = match env::var("RUSTUP_UNPACK_RAM")
.ok()
.and_then(|budget_str| budget_str.parse::<usize>().ok())
{
Some(budget) => budget,
None => {
if !unsafe { RAM_NOTICE_SHOWN } {
eprintln!(
"Defaulting to {} unpack ram",
units::Size::new(
default_max_unpack_ram,
units::Unit::B,
units::UnitMode::Norm
)
);
unsafe {
RAM_NOTICE_SHOWN = true;
}
}
default_max_unpack_ram
}
} else {
DEFAULT_UNPACK_RAM
};

if max_file_size > unpack_ram {
panic!("RUSTUP_UNPACK_RAM must be larger than {}", max_file_size);
}
Expand Down Expand Up @@ -278,7 297,8 @@ fn unpack_without_first_dir<'a, R: Read>(
.entries()
.chain_err(|| ErrorKind::ExtractingPackage)?;
const MAX_FILE_SIZE: u64 = 200_000_000;
let mut budget = MemoryBudget::new(MAX_FILE_SIZE as usize);
let effective_max_ram = effective_limits::memory_limit()?;
let mut budget = MemoryBudget::new(MAX_FILE_SIZE as usize, effective_max_ram as usize);

let mut directories: HashMap<PathBuf, DirStatus> = HashMap::new();
// Path is presumed to exist. Call it a precondition.
Expand Down
1 change: 1 addition & 0 deletions src/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 22,7 @@ pub const TOOLSTATE_MSG: &str =
error_chain! {
links {
Download(download::Error, download::ErrorKind);
Limits(effective_limits::Error, effective_limits::ErrorKind);
}

foreign_links {
Expand Down
3 changes: 3 additions & 0 deletions tests/cli-exact.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 38,7 @@ info: downloading component 'rust-docs'
info: downloading component 'rust-std'
info: downloading component 'rustc'
info: installing component 'cargo'
Defaulting to 500.0 MiB unpack ram
info: installing component 'rust-docs'
info: installing component 'rust-std'
info: installing component 'rustc'
Expand Down Expand Up @@ -172,6 173,7 @@ info: downloading component 'rust-docs'
info: downloading component 'rust-std'
info: downloading component 'rustc'
info: installing component 'cargo'
Defaulting to 500.0 MiB unpack ram
info: installing component 'rust-docs'
info: installing component 'rust-std'
info: installing component 'rustc'
Expand Down Expand Up @@ -504,6 506,7 @@ fn cross_install_indicates_target() {
&format!(
r"info: downloading component 'rust-std' for '{0}'
info: installing component 'rust-std' for '{0}'
Defaulting to 500.0 MiB unpack ram
",
clitools::CROSS_ARCH1
),
Expand Down
5 changes: 5 additions & 0 deletions tests/cli-rustup.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 56,7 @@ info: removing previous version of component 'rust-docs'
info: removing previous version of component 'rust-std'
info: removing previous version of component 'rustc'
info: installing component 'cargo'
Defaulting to 500.0 MiB unpack ram
info: installing component 'rust-docs'
info: installing component 'rust-std'
info: installing component 'rustc'
Expand Down Expand Up @@ -96,6 97,7 @@ info: removing previous version of component 'rust-docs'
info: removing previous version of component 'rust-std'
info: removing previous version of component 'rustc'
info: installing component 'cargo'
Defaulting to 500.0 MiB unpack ram
info: installing component 'rust-docs'
info: installing component 'rust-std'
info: installing component 'rustc'
Expand Down Expand Up @@ -160,6 162,7 @@ info: removing previous version of component 'rust-docs'
info: removing previous version of component 'rust-std'
info: removing previous version of component 'rustc'
info: installing component 'cargo'
Defaulting to 500.0 MiB unpack ram
info: installing component 'rust-docs'
info: installing component 'rust-std'
info: installing component 'rustc'
Expand Down Expand Up @@ -230,6 233,7 @@ info: removing previous version of component 'rust-docs'
info: removing previous version of component 'rust-std'
info: removing previous version of component 'rustc'
info: installing component 'cargo'
Defaulting to 500.0 MiB unpack ram
info: installing component 'rust-docs'
info: installing component 'rust-std'
info: installing component 'rustc'
Expand Down Expand Up @@ -291,6 295,7 @@ info: downloading component 'rust-docs'
info: downloading component 'rust-std'
info: downloading component 'rustc'
info: installing component 'cargo'
Defaulting to 500.0 MiB unpack ram
info: installing component 'rust-docs'
info: installing component 'rust-std'
info: installing component 'rustc'
Expand Down
1 change: 1 addition & 0 deletions tests/cli-self-upd.rs
Original file line number Diff line number Diff line change
Expand Up @@ -883,6 883,7 @@ info: downloading component 'rust-docs'
info: downloading component 'rust-std'
info: downloading component 'rustc'
info: installing component 'cargo'
Defaulting to 500.0 MiB unpack ram
info: installing component 'rust-docs'
info: installing component 'rust-std'
info: installing component 'rustc'
Expand Down
1 change: 1 addition & 0 deletions tests/cli-v2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1342,6 1342,7 @@ info: downloading component 'cargo'
info: downloading component 'rust-docs'
info: downloading component 'rustc'
info: installing component 'cargo'
Defaulting to 500.0 MiB unpack ram
info: installing component 'rust-docs'
info: installing component 'rustc'
"
Expand Down

0 comments on commit 53497af

Please sign in to comment.