optirs_gpu/memory/management/
mod.rs

1// GPU memory management modules
2//
3// This module provides advanced GPU memory management capabilities including
4// garbage collection, prefetching, eviction policies, and defragmentation.
5
6pub mod defragmentation;
7pub mod eviction_policies;
8pub mod garbage_collection;
9pub mod prefetching;
10
11use std::collections::HashMap;
12use std::ffi::c_void;
13use std::time::{Duration, Instant};
14
15use crate::memory::management::eviction_policies::EvictionConfig;
16
17pub use garbage_collection::{
18    GCConfig, GCStats, GarbageCollectionEngine, GarbageCollector, GenerationalCollector,
19    IncrementalCollector, MarkSweepCollector, ReferenceTracker,
20};
21
22pub use prefetching::{
23    AccessHistoryTracker, PrefetchCache, PrefetchConfig, PrefetchStrategy, PrefetchingEngine,
24    SequentialPrefetcher, StridePrefetcher,
25};
26
27pub use eviction_policies::{
28    ARCPolicy, ClockPolicy, EvictionEngine, EvictionPerformanceMonitor, EvictionPolicy, FIFOPolicy,
29    LFUPolicy, LRUPolicy, MemoryRegion, WorkloadAwarePolicy,
30};
31
32pub use defragmentation::{
33    CompactionAlgorithm, CompactionStrategy, DefragConfig, DefragError, DefragmentationEngine,
34    SlidingCompactionStrategy, ThreadSafeDefragmentationEngine, TwoPointerCompactionStrategy,
35};
36
37/// Unified memory management configuration
38#[derive(Debug, Clone)]
39pub struct MemoryManagementConfig {
40    /// Garbage collection configuration
41    pub gc_config: GCConfig,
42    /// Prefetching configuration  
43    pub prefetch_config: PrefetchConfig,
44    /// Defragmentation configuration
45    pub defrag_config: DefragConfig,
46    /// Enable background management
47    pub enable_background_management: bool,
48    /// Management thread count
49    pub management_threads: usize,
50    /// Memory pressure threshold
51    pub memory_pressure_threshold: f64,
52    /// Performance monitoring interval
53    pub monitoring_interval: Duration,
54}
55
56impl Default for MemoryManagementConfig {
57    fn default() -> Self {
58        Self {
59            gc_config: GCConfig::default(),
60            prefetch_config: PrefetchConfig::default(),
61            defrag_config: DefragConfig::default(),
62            enable_background_management: true,
63            management_threads: 2,
64            memory_pressure_threshold: 0.8,
65            monitoring_interval: Duration::from_millis(100),
66        }
67    }
68}
69
70/// Integrated memory management system
71pub struct IntegratedMemoryManager {
72    /// Garbage collection engine
73    gc_engine: GarbageCollectionEngine,
74    /// Prefetching engine
75    prefetch_engine: PrefetchingEngine,
76    /// Eviction engine
77    eviction_engine: EvictionEngine,
78    /// Defragmentation engine
79    defrag_engine: DefragmentationEngine,
80    /// Configuration
81    config: MemoryManagementConfig,
82    /// Management statistics
83    stats: ManagementStats,
84    /// Background management enabled
85    background_enabled: bool,
86}
87
88/// Memory management statistics
89#[derive(Debug, Clone, Default)]
90pub struct ManagementStats {
91    pub gc_collections: u64,
92    pub objects_collected: u64,
93    pub bytes_freed_by_gc: u64,
94    pub prefetch_requests: u64,
95    pub prefetch_hits: u64,
96    pub prefetch_accuracy: f64,
97    pub evictions_performed: u64,
98    pub bytes_evicted: u64,
99    pub defragmentation_cycles: u64,
100    pub fragmentation_reduced: usize,
101    pub total_management_time: Duration,
102    pub memory_pressure_events: u64,
103}
104
105impl IntegratedMemoryManager {
106    /// Create new integrated memory manager
107    pub fn new(config: MemoryManagementConfig) -> Self {
108        let gc_engine = GarbageCollectionEngine::new(config.gc_config.clone());
109        let prefetch_engine = PrefetchingEngine::new(config.prefetch_config.clone());
110        let eviction_engine = EvictionEngine::new(EvictionConfig::default());
111        let defrag_engine = DefragmentationEngine::new(config.defrag_config.clone());
112
113        Self {
114            gc_engine,
115            prefetch_engine,
116            eviction_engine,
117            defrag_engine,
118            config,
119            stats: ManagementStats::default(),
120            background_enabled: false,
121        }
122    }
123
124    /// Start background memory management
125    pub fn start_background_management(&mut self) -> Result<(), MemoryManagementError> {
126        if !self.config.enable_background_management {
127            return Err(MemoryManagementError::BackgroundManagementDisabled);
128        }
129
130        self.background_enabled = true;
131        Ok(())
132    }
133
134    /// Stop background memory management
135    pub fn stop_background_management(&mut self) {
136        self.background_enabled = false;
137    }
138
139    /// Run garbage collection
140    pub fn run_garbage_collection(
141        &mut self,
142        memory_regions: &HashMap<usize, MemoryRegion>,
143    ) -> Result<usize, MemoryManagementError> {
144        let start_time = Instant::now();
145
146        let gc_results = self
147            .gc_engine
148            .collect()
149            .map_err(|e| MemoryManagementError::GarbageCollectionFailed(format!("{:?}", e)))?;
150        let bytes_freed: usize = gc_results.iter().map(|r| r.bytes_collected).sum();
151
152        self.stats.gc_collections += 1;
153        self.stats.bytes_freed_by_gc += bytes_freed as u64;
154        self.stats.total_management_time += start_time.elapsed();
155
156        Ok(bytes_freed)
157    }
158
159    /// Perform prefetch operation
160    pub fn prefetch(
161        &mut self,
162        address: *mut c_void,
163        size: usize,
164        access_pattern: Option<&str>,
165    ) -> Result<bool, MemoryManagementError> {
166        let start_time = Instant::now();
167
168        // FEATURE STATUS (v1.0.0): Prefetching engine integration pending
169        //
170        // The prefetching infrastructure is in place, but full hardware-level
171        // prefetch integration requires platform-specific optimizations that
172        // are planned for v1.1.0.
173        //
174        // For v1.0.0, this method tracks prefetch requests for monitoring
175        // purposes. Users can manually manage GPU memory prefetching using
176        // driver-specific APIs if needed.
177        //
178        // PLANNED (v1.1.0+): Full prefetch_engine integration with:
179        // - Hardware prefetch hints
180        // - Adaptive prefetch strategies
181        // - Cross-device prefetch coordination
182        let prefetched = false; // Will be true when prefetch_engine.prefetch() is implemented
183
184        self.stats.prefetch_requests += 1;
185        if prefetched {
186            self.stats.prefetch_hits += 1;
187        }
188
189        self.stats.prefetch_accuracy = self.stats.prefetch_requests.saturating_sub(1).max(1) as f64
190            / self.stats.prefetch_requests as f64;
191        self.stats.total_management_time += start_time.elapsed();
192
193        Ok(prefetched)
194    }
195
196    /// Perform memory eviction
197    pub fn evict_memory(&mut self, target_bytes: usize) -> Result<usize, MemoryManagementError> {
198        let start_time = Instant::now();
199
200        // FEATURE STATUS (v1.0.0): Eviction engine integration pending
201        //
202        // The eviction policy infrastructure (LRU, LFU, ARC, etc.) is in place,
203        // but full integration with driver-level memory eviction requires
204        // platform-specific implementations planned for v1.1.0.
205        //
206        // For v1.0.0, memory management should be handled through explicit
207        // buffer deallocation. This method tracks eviction requests for
208        // monitoring purposes.
209        //
210        // PLANNED (v1.1.0+): Full eviction_engine integration with:
211        // - Automatic victim selection based on policies
212        // - Driver-level memory eviction hints
213        // - Multi-device eviction coordination
214        // - Eviction cost prediction
215        let bytes_evicted = 0; // Will be non-zero when eviction_engine.evict() is implemented
216
217        self.stats.evictions_performed += 1;
218        self.stats.bytes_evicted += bytes_evicted as u64;
219        self.stats.total_management_time += start_time.elapsed();
220
221        Ok(bytes_evicted)
222    }
223
224    /// Run defragmentation
225    pub fn defragment(
226        &mut self,
227        memory_regions: &HashMap<usize, MemoryRegion>,
228    ) -> Result<usize, MemoryManagementError> {
229        let start_time = Instant::now();
230
231        let compaction_result = self
232            .defrag_engine
233            .defragment()
234            .map_err(|e| MemoryManagementError::DefragmentationFailed(format!("{:?}", e)))?;
235
236        let fragmentation_reduced = compaction_result.bytes_moved;
237
238        self.stats.defragmentation_cycles += 1;
239        self.stats.fragmentation_reduced += fragmentation_reduced;
240        self.stats.total_management_time += start_time.elapsed();
241
242        Ok(fragmentation_reduced)
243    }
244
245    /// Check memory pressure and trigger appropriate management
246    pub fn handle_memory_pressure(
247        &mut self,
248        memory_usage_ratio: f64,
249        memory_regions: &HashMap<usize, MemoryRegion>,
250    ) -> Result<(), MemoryManagementError> {
251        if memory_usage_ratio > self.config.memory_pressure_threshold {
252            self.stats.memory_pressure_events += 1;
253
254            // Try garbage collection first
255            let _ = self.run_garbage_collection(memory_regions)?;
256
257            // If still under pressure, try eviction
258            if memory_usage_ratio > 0.9 {
259                let target_eviction =
260                    (memory_usage_ratio - self.config.memory_pressure_threshold) * 1_000_000.0; // Estimate bytes
261                let _ = self.evict_memory(target_eviction as usize)?;
262            }
263
264            // If severely fragmented, run defragmentation
265            if memory_usage_ratio > 0.95 {
266                let _ = self.defragment(memory_regions)?;
267            }
268        }
269
270        Ok(())
271    }
272
273    /// Update access patterns for adaptive management
274    pub fn update_access_pattern(
275        &mut self,
276        address: *mut c_void,
277        size: usize,
278        access_type: AccessType,
279    ) -> Result<(), MemoryManagementError> {
280        // FEATURE STATUS (v1.0.0): Access pattern tracking pending
281        //
282        // The infrastructure for tracking access patterns is in place, but
283        // integration with the prefetching and eviction engines requires
284        // runtime access pattern analysis that is planned for v1.1.0.
285        //
286        // PLANNED (v1.1.0+):
287        // - self.prefetch_engine.update_access_history(address, size, access_type.clone());
288        // - self.eviction_engine.record_access(address, size, access_type);
289        //
290        // For v1.0.0, users can manage access patterns explicitly through
291        // careful buffer management and ordering.
292
293        Ok(())
294    }
295
296    /// Get management statistics
297    pub fn get_stats(&self) -> &ManagementStats {
298        &self.stats
299    }
300
301    /// Get garbage collection stats
302    pub fn get_gc_stats(&self) -> GCStats {
303        self.gc_engine.get_stats().clone()
304    }
305
306    /// Get prefetch performance
307    pub fn get_prefetch_performance(&self) -> PrefetchPerformance {
308        PrefetchPerformance {
309            requests: self.stats.prefetch_requests,
310            hits: self.stats.prefetch_hits,
311            accuracy: self.stats.prefetch_accuracy,
312            // FEATURE STATUS (v1.0.0): Prefetch cache size tracking pending
313            // Will be implemented when prefetch_engine.get_cache_size() is available (v1.1.0+)
314            cache_size: 0,
315        }
316    }
317
318    /// Optimize management policies based on workload
319    pub fn optimize_policies(&mut self) -> Result<(), MemoryManagementError> {
320        // FEATURE STATUS (v1.0.0): Adaptive policy optimization pending
321        //
322        // The individual policy engines (GC, eviction, prefetch) are functional,
323        // but automatic policy selection based on workload analysis requires
324        // runtime profiling capabilities planned for v1.1.0.
325        //
326        // PLANNED (v1.1.0+):
327        // - let access_patterns = self.prefetch_engine.analyze_access_patterns();
328        // - self.gc_engine.set_preferred_strategy("generational");
329        // - self.eviction_engine.set_active_policy("lru");
330        //
331        // For v1.0.0, users can manually configure policies through the
332        // MemoryManagementConfig during initialization.
333
334        Ok(())
335    }
336}
337
338/// Memory access types for pattern tracking
339#[derive(Debug, Clone)]
340pub enum AccessType {
341    Read,
342    Write,
343    ReadWrite,
344    Sequential,
345    Random,
346}
347
348/// Prefetch performance metrics
349#[derive(Debug, Clone)]
350pub struct PrefetchPerformance {
351    pub requests: u64,
352    pub hits: u64,
353    pub accuracy: f64,
354    pub cache_size: usize,
355}
356
357/// Access pattern analysis
358#[derive(Debug, Clone)]
359pub struct AccessPatterns {
360    pub temporal_locality: f64,
361    pub spatial_locality: f64,
362    pub frequency_based: bool,
363    pub stride_patterns: Vec<i64>,
364}
365
366/// Memory management errors
367#[derive(Debug, Clone)]
368pub enum MemoryManagementError {
369    GarbageCollectionFailed(String),
370    PrefetchFailed(String),
371    EvictionFailed(String),
372    DefragmentationFailed(String),
373    BackgroundManagementDisabled,
374    InvalidConfiguration(String),
375    InternalError(String),
376}
377
378impl std::fmt::Display for MemoryManagementError {
379    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
380        match self {
381            MemoryManagementError::GarbageCollectionFailed(msg) => {
382                write!(f, "Garbage collection failed: {}", msg)
383            }
384            MemoryManagementError::PrefetchFailed(msg) => write!(f, "Prefetch failed: {}", msg),
385            MemoryManagementError::EvictionFailed(msg) => write!(f, "Eviction failed: {}", msg),
386            MemoryManagementError::DefragmentationFailed(msg) => {
387                write!(f, "Defragmentation failed: {}", msg)
388            }
389            MemoryManagementError::BackgroundManagementDisabled => {
390                write!(f, "Background management is disabled")
391            }
392            MemoryManagementError::InvalidConfiguration(msg) => {
393                write!(f, "Invalid configuration: {}", msg)
394            }
395            MemoryManagementError::InternalError(msg) => write!(f, "Internal error: {}", msg),
396        }
397    }
398}
399
400impl std::error::Error for MemoryManagementError {}
401
402#[cfg(test)]
403mod tests {
404    use super::*;
405
406    #[test]
407    fn test_integrated_manager_creation() {
408        let config = MemoryManagementConfig::default();
409        let manager = IntegratedMemoryManager::new(config);
410        assert!(!manager.background_enabled);
411    }
412
413    #[test]
414    fn test_background_management() {
415        let config = MemoryManagementConfig::default();
416        let mut manager = IntegratedMemoryManager::new(config);
417        let result = manager.start_background_management();
418        assert!(result.is_ok());
419        assert!(manager.background_enabled);
420    }
421
422    #[test]
423    fn test_stats_initialization() {
424        let config = MemoryManagementConfig::default();
425        let manager = IntegratedMemoryManager::new(config);
426        let stats = manager.get_stats();
427        assert_eq!(stats.gc_collections, 0);
428        assert_eq!(stats.prefetch_requests, 0);
429    }
430}