Skip to content

Commit e57c60b

Browse files
committed
Move unpack_ram() from dist to diskio
1 parent bdedd50 commit e57c60b

File tree

2 files changed

+66
-63
lines changed

2 files changed

+66
-63
lines changed

src/diskio/mod.rs

Lines changed: 62 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -51,20 +51,24 @@
5151
// loss or errors in this model.
5252
// f) data gathering: record (name, bytes, start, duration)
5353
// write to disk afterwards as a csv file?
54-
pub(crate) mod immediate;
55-
#[cfg(test)]
56-
mod test;
57-
pub(crate) mod threaded;
58-
use threaded::PoolReference;
59-
6054
use std::io::{self, Write};
6155
use std::ops::{Deref, DerefMut};
6256
use std::path::{Path, PathBuf};
57+
use std::sync::OnceLock;
6358
use std::sync::mpsc::Receiver;
6459
use std::time::{Duration, Instant};
6560
use std::{fmt::Debug, fs::OpenOptions};
6661

6762
use anyhow::Result;
63+
use tracing::{error, trace, warn};
64+
65+
use crate::utils::units::Size;
66+
67+
pub(crate) mod immediate;
68+
#[cfg(test)]
69+
mod test;
70+
pub(crate) mod threaded;
71+
use threaded::PoolReference;
6872

6973
/// Carries the implementation specific data for complete file transfers into the executor.
7074
#[derive(Debug)]
@@ -449,3 +453,55 @@ pub(crate) fn get_executor<'a>(
449453
n => Box::new(threaded::Threaded::new(n, ram_budget)),
450454
}
451455
}
456+
457+
pub(crate) fn unpack_ram(io_chunk_size: usize, budget: Option<usize>) -> usize {
458+
const RAM_ALLOWANCE_FOR_RUSTUP_AND_BUFFERS: usize = 200 * 1024 * 1024;
459+
let minimum_ram = io_chunk_size * 2;
460+
461+
let default_max_unpack_ram = match effective_limits::memory_limit() {
462+
Ok(effective)
463+
if effective as usize > minimum_ram + RAM_ALLOWANCE_FOR_RUSTUP_AND_BUFFERS =>
464+
{
465+
effective as usize - RAM_ALLOWANCE_FOR_RUSTUP_AND_BUFFERS
466+
}
467+
Ok(_) => minimum_ram,
468+
Err(error) => {
469+
error!("can't determine memory limit: {error}");
470+
minimum_ram
471+
}
472+
};
473+
474+
let unpack_ram = match budget {
475+
Some(budget) => {
476+
if budget < minimum_ram {
477+
warn!(
478+
"Ignoring RUSTUP_UNPACK_RAM ({}) less than minimum of {}.",
479+
budget, minimum_ram
480+
);
481+
minimum_ram
482+
} else if budget > default_max_unpack_ram {
483+
warn!(
484+
"Ignoring RUSTUP_UNPACK_RAM ({}) greater than detected available RAM of {}.",
485+
budget, default_max_unpack_ram
486+
);
487+
default_max_unpack_ram
488+
} else {
489+
budget
490+
}
491+
}
492+
None => {
493+
if RAM_NOTICE_SHOWN.set(()).is_ok() {
494+
trace!(size = %Size::new(default_max_unpack_ram), "unpacking components in memory");
495+
}
496+
default_max_unpack_ram
497+
}
498+
};
499+
500+
if minimum_ram > unpack_ram {
501+
panic!("RUSTUP_UNPACK_RAM must be larger than {minimum_ram}");
502+
} else {
503+
unpack_ram
504+
}
505+
}
506+
507+
static RAM_NOTICE_SHOWN: OnceLock<()> = OnceLock::new();

src/dist/component/package.rs

Lines changed: 4 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -7,21 +7,21 @@ use std::io::{self, ErrorKind as IOErrorKind, Read};
77
use std::mem;
88
use std::ops::Deref;
99
use std::path::{Path, PathBuf};
10-
use std::sync::OnceLock;
1110

1211
use anyhow::{Context, Result, anyhow, bail};
1312
use tar::EntryType;
14-
use tracing::{error, trace, warn};
13+
use tracing::warn;
1514

16-
use crate::diskio::{CompletedIo, Executor, FileBuffer, IO_CHUNK_SIZE, Item, Kind, get_executor};
15+
use crate::diskio::{
16+
CompletedIo, Executor, FileBuffer, IO_CHUNK_SIZE, Item, Kind, get_executor, unpack_ram,
17+
};
1718
use crate::dist::component::components::{ComponentPart, ComponentPartKind, Components};
1819
use crate::dist::component::transaction::Transaction;
1920
use crate::dist::download::DownloadCfg;
2021
use crate::dist::manifest::CompressionKind;
2122
use crate::dist::temp;
2223
use crate::errors::RustupError;
2324
use crate::utils;
24-
use crate::utils::units::Size;
2525

2626
/// The current metadata revision used by rust-installer
2727
pub(crate) const INSTALLER_VERSION: &str = "3";
@@ -152,59 +152,6 @@ impl<P: Deref<Target = Path>> DirectoryPackage<P> {
152152
}
153153
}
154154

155-
// Probably this should live in diskio but ¯\_(ツ)_/¯
156-
fn unpack_ram(io_chunk_size: usize, budget: Option<usize>) -> usize {
157-
const RAM_ALLOWANCE_FOR_RUSTUP_AND_BUFFERS: usize = 200 * 1024 * 1024;
158-
let minimum_ram = io_chunk_size * 2;
159-
160-
let default_max_unpack_ram = match effective_limits::memory_limit() {
161-
Ok(effective)
162-
if effective as usize > minimum_ram + RAM_ALLOWANCE_FOR_RUSTUP_AND_BUFFERS =>
163-
{
164-
effective as usize - RAM_ALLOWANCE_FOR_RUSTUP_AND_BUFFERS
165-
}
166-
Ok(_) => minimum_ram,
167-
Err(error) => {
168-
error!("can't determine memory limit: {error}");
169-
minimum_ram
170-
}
171-
};
172-
173-
let unpack_ram = match budget {
174-
Some(budget) => {
175-
if budget < minimum_ram {
176-
warn!(
177-
"Ignoring RUSTUP_UNPACK_RAM ({}) less than minimum of {}.",
178-
budget, minimum_ram
179-
);
180-
minimum_ram
181-
} else if budget > default_max_unpack_ram {
182-
warn!(
183-
"Ignoring RUSTUP_UNPACK_RAM ({}) greater than detected available RAM of {}.",
184-
budget, default_max_unpack_ram
185-
);
186-
default_max_unpack_ram
187-
} else {
188-
budget
189-
}
190-
}
191-
None => {
192-
if RAM_NOTICE_SHOWN.set(()).is_ok() {
193-
trace!(size = %Size::new(default_max_unpack_ram), "unpacking components in memory");
194-
}
195-
default_max_unpack_ram
196-
}
197-
};
198-
199-
if minimum_ram > unpack_ram {
200-
panic!("RUSTUP_UNPACK_RAM must be larger than {minimum_ram}");
201-
} else {
202-
unpack_ram
203-
}
204-
}
205-
206-
static RAM_NOTICE_SHOWN: OnceLock<()> = OnceLock::new();
207-
208155
/// Handle the async result of io operations
209156
/// Replaces op.result with Ok(())
210157
fn filter_result(op: &mut CompletedIo) -> io::Result<()> {

0 commit comments

Comments
 (0)