Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Warn when most of the RAM has been used up by Rerun #1651

Merged
merged 4 commits into from
Mar 22, 2023
Merged
Show file tree
Hide file tree
Changes from 3 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
24 changes: 24 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions crates/re_memory/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ smallvec = "1.10"
[target.'cfg(not(target_arch = "wasm32"))'.dependencies]
backtrace = "0.3"
memory-stats = "1.0"
sysinfo = { version = "0.28.3", default-features = false }

# web dependencies:
[target.'cfg(target_arch = "wasm32")'.dependencies]
Expand Down
3 changes: 2 additions & 1 deletion crates/re_memory/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ mod allocation_tracker;
mod memory_history;
mod memory_limit;
mod memory_use;
mod ram_warner;
pub mod util;

#[cfg(not(target_arch = "wasm32"))]
Expand All @@ -23,7 +24,7 @@ use backtrace_web::Backtrace;

pub use {
accounting_allocator::AccountingAllocator, memory_history::MemoryHistory,
memory_limit::MemoryLimit, memory_use::MemoryUse,
memory_limit::MemoryLimit, memory_use::MemoryUse, ram_warner::*,
};

/// Number of allocation and their total size.
Expand Down
61 changes: 61 additions & 0 deletions crates/re_memory/src/ram_warner.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
/// Amount of available RAM on this machine.
#[cfg(not(target_arch = "wasm32"))]
pub fn total_ram_in_bytes() -> u64 {
use sysinfo::SystemExt as _;

let mut sys = sysinfo::System::new_all();
sys.refresh_all();

let total_memory = sys.total_memory();

re_log::debug!(
"Total RAM: {}",
re_format::format_bytes(sys.total_memory() as _)
);

total_memory
}

/// Amount of available RAM on this machine.
#[cfg(target_arch = "wasm32")]
pub fn total_ram_in_bytes() -> u64 {
1_u64 << 32
}

// ----------------------------------------------------------------------------

pub struct RamLimitWarner {
total_ram_in_bytes: u64,
limit: u64,
has_warned: bool,
}

impl RamLimitWarner {
pub fn warn_at_fraction_of_max(fraction: f32) -> Self {
let total_ram_in_bytes = total_ram_in_bytes();
let limit = (fraction as f64 * total_ram_in_bytes as f64).round() as _;
Self {
total_ram_in_bytes,
limit,
has_warned: false,
}
}

/// Warns if we have exceeded the limit.
pub fn update(&mut self) {
if !self.has_warned {
let used = crate::MemoryUse::capture();
let used = used.counted.or(used.resident);
if let Some(used) = used {
if 0 <= used && self.limit <= used as u64 {
self.has_warned = true;
re_log::warn!(
"RAM usage is {} (with a total of {} system RAM). You may want to start Rerun with the --memory-limit flag to limit RAM usage.",
re_format::format_bytes(used as _),
re_format::format_bytes(self.total_ram_in_bytes as _),
);
}
}
}
}
}
2 changes: 1 addition & 1 deletion crates/re_viewer/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -99,8 +99,8 @@ rfd = "0.11"
serde = { version = "1", features = ["derive"] }
slotmap = { version = "1.0.6", features = ["serde"] }
smallvec = { version = "1.10", features = ["serde"] }
time = { workspace = true, default-features = false, features = ["formatting"] }
thiserror.workspace = true
time = { workspace = true, default-features = false, features = ["formatting"] }
uuid = { version = "1.1", features = ["serde", "v4", "js"] }
vec1 = "1.8"
wgpu.workspace = true
Expand Down
7 changes: 7 additions & 0 deletions crates/re_viewer/src/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ const MAX_ZOOM_FACTOR: f32 = 4.0;
pub struct App {
build_info: re_build_info::BuildInfo,
startup_options: StartupOptions,
ram_limit_warner: re_memory::RamLimitWarner,
re_ui: re_ui::ReUi,

/// Listens to the local text log stream
Expand Down Expand Up @@ -123,6 +124,7 @@ impl App {
Self {
build_info,
startup_options,
ram_limit_warner: re_memory::RamLimitWarner::warn_at_fraction_of_max(0.75),
re_ui,
text_log_rx,
component_ui_registry: Default::default(),
Expand Down Expand Up @@ -413,6 +415,11 @@ impl eframe::App for App {
fn update(&mut self, egui_ctx: &egui::Context, frame: &mut eframe::Frame) {
let frame_start = Instant::now();

if self.startup_options.memory_limit.limit.is_none() {
// we only warn about high memory usage if the user hasn't specified a limit
self.ram_limit_warner.update();
}

if self.icon_status == AppIconStatus::NotSetTryAgain {
self.icon_status = setup_app_icon();
}
Expand Down
8 changes: 7 additions & 1 deletion crates/re_viewer/src/web.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,13 @@ pub async fn start(
Box::new(move |cc| {
let build_info = re_build_info::build_info!();
let app_env = crate::AppEnvironment::Web;
let startup_options = crate::StartupOptions::default();
let startup_options = crate::StartupOptions {
memory_limit: re_memory::MemoryLimit {
// On wasm32 we only have 4GB of memory to play around with.
limit: Some(3_500_000_000),
},
..Default::default()
};
let re_ui = crate::customize_eframe(cc);
let url = url.unwrap_or_else(|| get_url(&cc.integration_info));

Expand Down