grafeo_common/memory/buffer/mod.rs
1//! Centralized memory management with pressure handling.
2//!
3//! When you need to control total memory usage across the database (not just
4//! individual allocations), this is what you use. The [`BufferManager`] tracks
5//! all allocations, monitors memory pressure, and can trigger spilling when
6//! you're running low.
7//!
8//! # Architecture
9//!
10//! ```text
11//! ┌────────────────────────────────────────────────────────────┐
12//! │ BufferManager │
13//! │ ┌──────────────┬──────────────┬──────────────┬──────────┐ │
14//! │ │ GraphStorage │ IndexBuffers │ Execution │ Spill │ │
15//! │ │ │ │ Buffers │ Staging │ │
16//! │ └──────────────┴──────────────┴──────────────┴──────────┘ │
17//! │ │ │
18//! │ Pressure Thresholds: │ │
19//! │ < 70% Normal │ │
20//! │ 70-85% Moderate (evict cold) │
21//! │ 85-95% High (aggressive evict/spill) │
22//! │ > 95% Critical (block allocations) │
23//! └────────────────────────────────────────────────────────────┘
24//! ```
25//!
26//! # Usage
27//!
28//! ```ignore
29//! use grafeo_common::memory::buffer::{BufferManager, MemoryRegion};
30//!
31//! // Create with default config (75% of system RAM)
32//! let manager = BufferManager::with_defaults();
33//!
34//! // Or with specific budget
35//! let manager = BufferManager::with_budget(1024 * 1024 * 100); // 100MB
36//!
37//! // Allocate memory
38//! if let Some(grant) = manager.try_allocate(1024, MemoryRegion::ExecutionBuffers) {
39//! // Use the memory...
40//! // Memory is automatically released when grant is dropped
41//! }
42//!
43//! // Check pressure level
44//! let level = manager.pressure_level();
45//! if level.should_spill() {
46//! // Trigger spilling for spillable operators
47//! }
48//! ```
49
50mod consumer;
51mod grant;
52mod manager;
53mod region;
54mod stats;
55
56pub use consumer::{ConsumerStats, MemoryConsumer, SpillError, priorities};
57pub use grant::{CompositeGrant, GrantReleaser, MemoryGrant};
58pub use manager::{BufferManager, BufferManagerConfig};
59pub use region::MemoryRegion;
60pub use stats::{BufferStats, PressureLevel};