Skip to main content

ringkernel_core/resource/
mod.rs

1//! Resource guard module for preventing system overload.
2//!
3//! This module provides safeguards to prevent out-of-memory conditions
4//! during large-scale operations.
5//!
6//! # Example
7//!
8//! ```
9//! use ringkernel_core::resource::{ResourceGuard, MemoryEstimate};
10//!
11//! let guard = ResourceGuard::new();
12//! let estimate = MemoryEstimate::new()
13//!     .with_primary(1024 * 1024)  // 1 MB
14//!     .with_auxiliary(512 * 1024); // 512 KB
15//!
16//! if !guard.can_allocate(estimate.total_bytes()) {
17//!     panic!("Insufficient memory");
18//! }
19//!
20//! // Or use the trait-based estimator
21//! struct MyWorkload { elements: usize }
22//! impl ringkernel_core::resource::MemoryEstimator for MyWorkload {
23//!     fn estimate(&self) -> MemoryEstimate {
24//!         MemoryEstimate::new().with_primary((self.elements * 64) as u64)
25//!     }
26//!     fn name(&self) -> &str { "MyWorkload" }
27//! }
28//! ```
29
30mod error;
31mod estimate;
32mod guard;
33mod system;
34
35pub use error::{ResourceError, ResourceResult};
36pub use estimate::{LinearEstimator, MemoryEstimate, MemoryEstimator};
37pub use guard::{global_guard, ReservationGuard, ResourceGuard};
38pub use system::{get_available_memory, get_total_memory};
39
40/// Default maximum memory usage (4 GB).
41pub const DEFAULT_MAX_MEMORY_BYTES: u64 = 4 * 1024 * 1024 * 1024;
42
43/// Safety margin for system memory (leave 1 GB free).
44pub const SYSTEM_MEMORY_MARGIN: u64 = 1024 * 1024 * 1024;