Skip to content

Commit

Permalink
Warn when most of the RAM has been used up by Rerun (#1651)
Browse files Browse the repository at this point in the history
* Warn when most of the RAM has been used up by Rerun

Unless the `--memory-limit` is already set, we now warn and suggest
setting it when Rerun is using 75% of all available RAM by itself.

* On web, always purge memory after 3.5 GB

* typos fix

* fix clppy
  • Loading branch information
emilk committed Mar 22, 2023
1 parent 9901e7c commit 9aaf909
Show file tree
Hide file tree
Showing 7 changed files with 102 additions and 3 deletions.
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 @@ -127,6 +128,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 @@ -416,6 +418,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
7 changes: 6 additions & 1 deletion crates/re_viewer/src/web.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,12 @@ 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),
},
};
let re_ui = crate::customize_eframe(cc);
let url = url.unwrap_or_else(|| get_url(&cc.integration_info));

Expand Down

1 comment on commit 9aaf909

@github-actions
Copy link

Choose a reason for hiding this comment

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

Rust Benchmark

Benchmark suite Current: 9aaf909 Previous: 9901e7c Ratio
datastore/insert/batch/rects/insert 548688 ns/iter (± 7436) 563454 ns/iter (± 4845) 0.97
datastore/latest_at/batch/rects/query 1807 ns/iter (± 16) 1857 ns/iter (± 48) 0.97
datastore/latest_at/missing_components/primary 282 ns/iter (± 3) 287 ns/iter (± 1) 0.98
datastore/latest_at/missing_components/secondaries 439 ns/iter (± 6) 437 ns/iter (± 2) 1.00
datastore/range/batch/rects/query 146855 ns/iter (± 1931) 149290 ns/iter (± 1139) 0.98
mono_points_arrow/generate_message_bundles 46132975 ns/iter (± 1443112) 43609337 ns/iter (± 885437) 1.06
mono_points_arrow/generate_messages 132702640 ns/iter (± 1609242) 124759779 ns/iter (± 1189980) 1.06
mono_points_arrow/encode_log_msg 162860909 ns/iter (± 1079071) 153567144 ns/iter (± 711968) 1.06
mono_points_arrow/encode_total 345313535 ns/iter (± 2123134) 327263578 ns/iter (± 1352920) 1.06
mono_points_arrow/decode_log_msg 179544658 ns/iter (± 2553683) 174877558 ns/iter (± 860106) 1.03
mono_points_arrow/decode_message_bundles 69382529 ns/iter (± 1167959) 63007452 ns/iter (± 929547) 1.10
mono_points_arrow/decode_total 247364764 ns/iter (± 3315660) 235272471 ns/iter (± 1431107) 1.05
batch_points_arrow/generate_message_bundles 325291 ns/iter (± 4644) 337269 ns/iter (± 769) 0.96
batch_points_arrow/generate_messages 6090 ns/iter (± 90) 6406 ns/iter (± 15) 0.95
batch_points_arrow/encode_log_msg 359632 ns/iter (± 3756) 369890 ns/iter (± 979) 0.97
batch_points_arrow/encode_total 712768 ns/iter (± 8325) 727174 ns/iter (± 2359) 0.98
batch_points_arrow/decode_log_msg 341582 ns/iter (± 3051) 349355 ns/iter (± 875) 0.98
batch_points_arrow/decode_message_bundles 1995 ns/iter (± 29) 2129 ns/iter (± 11) 0.94
batch_points_arrow/decode_total 349094 ns/iter (± 3911) 355917 ns/iter (± 714) 0.98
arrow_mono_points/insert 6878657573 ns/iter (± 26567516) 6151564119 ns/iter (± 12179130) 1.12
arrow_mono_points/query 1719835 ns/iter (± 21660) 1766371 ns/iter (± 11900) 0.97
arrow_batch_points/insert 2583408 ns/iter (± 27076) 2662872 ns/iter (± 25356) 0.97
arrow_batch_points/query 15791 ns/iter (± 223) 16144 ns/iter (± 116) 0.98
arrow_batch_vecs/insert 41536 ns/iter (± 538) 42411 ns/iter (± 317) 0.98
arrow_batch_vecs/query 376999 ns/iter (± 5739) 387146 ns/iter (± 3875) 0.97
tuid/Tuid::random 33 ns/iter (± 0) 34 ns/iter (± 0) 0.97

This comment was automatically generated by workflow using github-action-benchmark.

Please sign in to comment.