Check memory pressure and take action if needed
(&self)
| 212 | |
| 213 | /// Check memory pressure and take action if needed |
| 214 | pub fn check_memory_pressure(&self) { |
| 215 | if !self.config.enable_pressure_monitoring { |
| 216 | return; |
| 217 | } |
| 218 | |
| 219 | // Rate limit checks |
| 220 | { |
| 221 | let last_check = *self.last_check.read(); |
| 222 | if last_check.elapsed() < self.config.check_interval { |
| 223 | return; |
| 224 | } |
| 225 | } |
| 226 | |
| 227 | let memory_stats = global_memory_monitor().get_stats(); |
| 228 | let current_memory_mb = memory_stats.total_bytes() as f64 / (1024.0 * 1024.0); |
| 229 | |
| 230 | // Update last check time |
| 231 | { |
| 232 | let mut last_check = self.last_check.write(); |
| 233 | *last_check = Instant::now(); |
| 234 | } |
| 235 | |
| 236 | // Update stats |
| 237 | { |
| 238 | let mut stats = self.stats.write(); |
| 239 | stats.pressure_checks += 1; |
| 240 | stats.last_check_time = Some(Instant::now()); |
| 241 | stats.current_memory_pressure = memory_stats.pressure_level; |
| 242 | stats.total_memory_bytes = memory_stats.total_bytes() as usize; |
| 243 | } |
| 244 | |
| 245 | // Determine action based on memory usage and pressure |
| 246 | let action = if current_memory_mb > self.config.critical_threshold_mb as f64 { |
| 247 | MemoryAction::CriticalEviction |
| 248 | } else if current_memory_mb > self.config.pressure_threshold_mb as f64 { |
| 249 | MemoryAction::PressureEviction |
| 250 | } else { |
| 251 | match memory_stats.pressure_level { |
| 252 | MemoryPressure::Critical => MemoryAction::CriticalEviction, |
| 253 | MemoryPressure::High => MemoryAction::PressureEviction, |
| 254 | MemoryPressure::Medium => MemoryAction::AdaptiveTtl, |
| 255 | MemoryPressure::Low => MemoryAction::None, |
| 256 | } |
| 257 | }; |
| 258 | |
| 259 | self.execute_memory_action(action, current_memory_mb); |
| 260 | } |
| 261 | |
| 262 | fn execute_memory_action(&self, action: MemoryAction, current_memory_mb: f64) { |
| 263 | match action { |
no test coverage detected