optirs-gpu 0.3.2

OptiRS GPU acceleration and multi-GPU optimization
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
// GPU memory management modules
//
// This module provides advanced GPU memory management capabilities including
// garbage collection, prefetching, eviction policies, and defragmentation.

pub mod defragmentation;
pub mod eviction_policies;
pub mod garbage_collection;
pub mod prefetching;

use std::collections::HashMap;
use std::ffi::c_void;
use std::time::{Duration, Instant};

use crate::memory::management::eviction_policies::EvictionConfig;

pub use garbage_collection::{
    GCConfig, GCStats, GarbageCollectionEngine, GarbageCollector, GenerationalCollector,
    IncrementalCollector, MarkSweepCollector, ReferenceTracker,
};

pub use prefetching::{
    AccessHistoryTracker, PrefetchCache, PrefetchConfig, PrefetchStrategy, PrefetchingEngine,
    SequentialPrefetcher, StridePrefetcher,
};

pub use eviction_policies::{
    ARCPolicy, ClockPolicy, EvictionEngine, EvictionPerformanceMonitor, EvictionPolicy, FIFOPolicy,
    LFUPolicy, LRUPolicy, MemoryRegion, WorkloadAwarePolicy,
};

pub use defragmentation::{
    CompactionAlgorithm, CompactionStrategy, DefragConfig, DefragError, DefragmentationEngine,
    SlidingCompactionStrategy, ThreadSafeDefragmentationEngine, TwoPointerCompactionStrategy,
};

/// Unified memory management configuration
#[derive(Debug, Clone)]
pub struct MemoryManagementConfig {
    /// Garbage collection configuration
    pub gc_config: GCConfig,
    /// Prefetching configuration  
    pub prefetch_config: PrefetchConfig,
    /// Defragmentation configuration
    pub defrag_config: DefragConfig,
    /// Enable background management
    pub enable_background_management: bool,
    /// Management thread count
    pub management_threads: usize,
    /// Memory pressure threshold
    pub memory_pressure_threshold: f64,
    /// Performance monitoring interval
    pub monitoring_interval: Duration,
}

impl Default for MemoryManagementConfig {
    fn default() -> Self {
        Self {
            gc_config: GCConfig::default(),
            prefetch_config: PrefetchConfig::default(),
            defrag_config: DefragConfig::default(),
            enable_background_management: true,
            management_threads: 2,
            memory_pressure_threshold: 0.8,
            monitoring_interval: Duration::from_millis(100),
        }
    }
}

/// Integrated memory management system
pub struct IntegratedMemoryManager {
    /// Garbage collection engine
    gc_engine: GarbageCollectionEngine,
    /// Prefetching engine
    prefetch_engine: PrefetchingEngine,
    /// Eviction engine
    eviction_engine: EvictionEngine,
    /// Defragmentation engine
    defrag_engine: DefragmentationEngine,
    /// Configuration
    config: MemoryManagementConfig,
    /// Management statistics
    stats: ManagementStats,
    /// Background management enabled
    background_enabled: bool,
}

/// Memory management statistics
#[derive(Debug, Clone, Default)]
pub struct ManagementStats {
    pub gc_collections: u64,
    pub objects_collected: u64,
    pub bytes_freed_by_gc: u64,
    pub prefetch_requests: u64,
    pub prefetch_hits: u64,
    pub prefetch_accuracy: f64,
    pub evictions_performed: u64,
    pub bytes_evicted: u64,
    pub defragmentation_cycles: u64,
    pub fragmentation_reduced: usize,
    pub total_management_time: Duration,
    pub memory_pressure_events: u64,
}

impl IntegratedMemoryManager {
    /// Create new integrated memory manager
    pub fn new(config: MemoryManagementConfig) -> Self {
        let gc_engine = GarbageCollectionEngine::new(config.gc_config.clone());
        let prefetch_engine = PrefetchingEngine::new(config.prefetch_config.clone());
        let eviction_engine = EvictionEngine::new(EvictionConfig::default());
        let defrag_engine = DefragmentationEngine::new(config.defrag_config.clone());

        Self {
            gc_engine,
            prefetch_engine,
            eviction_engine,
            defrag_engine,
            config,
            stats: ManagementStats::default(),
            background_enabled: false,
        }
    }

    /// Start background memory management
    pub fn start_background_management(&mut self) -> Result<(), MemoryManagementError> {
        if !self.config.enable_background_management {
            return Err(MemoryManagementError::BackgroundManagementDisabled);
        }

        self.background_enabled = true;
        Ok(())
    }

    /// Stop background memory management
    pub fn stop_background_management(&mut self) {
        self.background_enabled = false;
    }

    /// Run garbage collection
    ///
    /// `memory_regions` describes the caller's current view of live
    /// allocations, but it is intentionally not registered with
    /// [`GarbageCollectionEngine`]: that engine only selects a collector for
    /// a region when `region.utilization < mark_threshold` (see
    /// `MarkSweepCollector::can_collect`), while every region built by the
    /// current caller is a single allocation at 100% utilization. Feeding
    /// such regions in would turn every call into a hard
    /// `GCError::NoSuitableCollector` instead of today's harmless no-op.
    /// Tracked as a finding rather than force-integrated; see the crate's
    /// lint/unwrap sweep notes.
    pub fn run_garbage_collection(
        &mut self,
        _memory_regions: &HashMap<usize, MemoryRegion>,
    ) -> Result<usize, MemoryManagementError> {
        let start_time = Instant::now();

        let gc_results = self
            .gc_engine
            .collect()
            .map_err(|e| MemoryManagementError::GarbageCollectionFailed(format!("{:?}", e)))?;
        let bytes_freed: usize = gc_results.iter().map(|r| r.bytes_collected).sum();

        self.stats.gc_collections += 1;
        self.stats.bytes_freed_by_gc += bytes_freed as u64;
        self.stats.total_management_time += start_time.elapsed();

        Ok(bytes_freed)
    }

    /// Perform prefetch operation
    ///
    /// Records the access with the [`PrefetchingEngine`] so its access-history
    /// tracker and pattern strategies see real data; the returned bool
    /// reflects whether this access was itself a hit against data the engine
    /// had already prefetched (a genuine cache hit), not a fabricated value.
    pub fn prefetch(
        &mut self,
        address: *mut c_void,
        size: usize,
        access_pattern: Option<&str>,
    ) -> Result<bool, MemoryManagementError> {
        let start_time = Instant::now();

        let access_type = match access_pattern {
            Some(pattern) if pattern.eq_ignore_ascii_case("write") => {
                prefetching::AccessType::Write
            }
            Some(pattern) if pattern.eq_ignore_ascii_case("read") => prefetching::AccessType::Read,
            _ => prefetching::AccessType::ReadWrite,
        };

        let hits_before = self.prefetch_engine.get_stats().successful_prefetches;
        self.prefetch_engine
            .record_access(prefetching::MemoryAccess {
                address: address as usize,
                size,
                timestamp: Instant::now(),
                access_type,
                context_id: 0,
                kernel_id: None,
            });
        let prefetched = self.prefetch_engine.get_stats().successful_prefetches > hits_before;

        self.stats.prefetch_requests += 1;
        if prefetched {
            self.stats.prefetch_hits += 1;
        }

        self.stats.prefetch_accuracy =
            self.stats.prefetch_hits as f64 / self.stats.prefetch_requests as f64;
        self.stats.total_management_time += start_time.elapsed();

        Ok(prefetched)
    }

    /// Perform memory eviction
    ///
    /// Registers `memory_regions` with the [`EvictionEngine`] (most-pressured,
    /// then largest, region first) and evicts real objects from it via the
    /// engine's active policy until `target_bytes` have been reclaimed or
    /// there is nothing left to evict. The returned count is the true sum of
    /// evicted object sizes, not an estimate.
    pub fn evict_memory(
        &mut self,
        target_bytes: usize,
        memory_regions: &HashMap<usize, MemoryRegion>,
    ) -> Result<usize, MemoryManagementError> {
        let start_time = Instant::now();
        let mut bytes_evicted = 0usize;

        let mut ordered: Vec<&MemoryRegion> = memory_regions.values().collect();
        ordered.sort_by(|a, b| {
            b.pressure
                .partial_cmp(&a.pressure)
                .unwrap_or(std::cmp::Ordering::Equal)
                .then_with(|| b.size.cmp(&a.size))
        });

        for region in ordered {
            if bytes_evicted >= target_bytes {
                break;
            }

            self.eviction_engine.register_region(
                region.base_addr,
                region.size,
                region.region_type.clone(),
            );
            for object in region.objects.values() {
                self.eviction_engine
                    .add_object(region.base_addr, object.clone())
                    .map_err(|e| MemoryManagementError::EvictionFailed(format!("{:?}", e)))?;
            }

            let victims = self
                .eviction_engine
                .evict(region.base_addr, target_bytes - bytes_evicted)
                .map_err(|e| MemoryManagementError::EvictionFailed(format!("{:?}", e)))?;

            for victim_addr in &victims {
                if let Some(object) = region.objects.get(victim_addr) {
                    bytes_evicted += object.size;
                }
            }
        }

        self.stats.evictions_performed += 1;
        self.stats.bytes_evicted += bytes_evicted as u64;
        self.stats.total_management_time += start_time.elapsed();

        Ok(bytes_evicted)
    }

    /// Run defragmentation
    ///
    /// `memory_regions` is intentionally not registered with
    /// [`DefragmentationEngine`]: both built-in compaction strategies require
    /// `MemoryLayoutTracker::get_total_free_space() > 0` to be eligible
    /// (`can_handle`), but the current caller only ever reports one region
    /// per live allocation with no free space (region size == its single
    /// object's size). Registering that data would not change the outcome
    /// (`DefragError::NoSuitableStrategy` either way) and free-space
    /// tracking would need to be added at the caller first. Tracked as a
    /// finding rather than force-integrated; see the crate's lint/unwrap
    /// sweep notes.
    pub fn defragment(
        &mut self,
        _memory_regions: &HashMap<usize, MemoryRegion>,
    ) -> Result<usize, MemoryManagementError> {
        let start_time = Instant::now();

        let compaction_result = self
            .defrag_engine
            .defragment()
            .map_err(|e| MemoryManagementError::DefragmentationFailed(format!("{:?}", e)))?;

        let fragmentation_reduced = compaction_result.bytes_moved;

        self.stats.defragmentation_cycles += 1;
        self.stats.fragmentation_reduced += fragmentation_reduced;
        self.stats.total_management_time += start_time.elapsed();

        Ok(fragmentation_reduced)
    }

    /// Check memory pressure and trigger appropriate management
    pub fn handle_memory_pressure(
        &mut self,
        memory_usage_ratio: f64,
        memory_regions: &HashMap<usize, MemoryRegion>,
    ) -> Result<(), MemoryManagementError> {
        if memory_usage_ratio > self.config.memory_pressure_threshold {
            self.stats.memory_pressure_events += 1;

            // Try garbage collection first
            let _ = self.run_garbage_collection(memory_regions)?;

            // If still under pressure, try eviction
            if memory_usage_ratio > 0.9 {
                let target_eviction =
                    (memory_usage_ratio - self.config.memory_pressure_threshold) * 1_000_000.0; // Estimate bytes
                let _ = self.evict_memory(target_eviction as usize, memory_regions)?;
            }

            // If severely fragmented, run defragmentation
            if memory_usage_ratio > 0.95 {
                let _ = self.defragment(memory_regions)?;
            }
        }

        Ok(())
    }

    /// Update access patterns for adaptive management
    ///
    /// Feeds the access into the [`PrefetchingEngine`]'s access-history
    /// tracker (mapped from this module's coarse [`AccessType`] to
    /// [`prefetching::AccessType`]) so its pattern strategies observe real
    /// traffic instead of being permanently starved of data.
    pub fn update_access_pattern(
        &mut self,
        address: *mut c_void,
        size: usize,
        access_type: AccessType,
    ) -> Result<(), MemoryManagementError> {
        let mapped_type = match access_type {
            AccessType::Read | AccessType::Sequential => prefetching::AccessType::Read,
            AccessType::Write => prefetching::AccessType::Write,
            AccessType::ReadWrite | AccessType::Random => prefetching::AccessType::ReadWrite,
        };

        self.prefetch_engine
            .record_access(prefetching::MemoryAccess {
                address: address as usize,
                size,
                timestamp: Instant::now(),
                access_type: mapped_type,
                context_id: 0,
                kernel_id: None,
            });

        Ok(())
    }

    /// Get management statistics
    pub fn get_stats(&self) -> &ManagementStats {
        &self.stats
    }

    /// Get garbage collection stats
    pub fn get_gc_stats(&self) -> GCStats {
        self.gc_engine.get_stats().clone()
    }

    /// Get prefetch performance
    pub fn get_prefetch_performance(&self) -> PrefetchPerformance {
        PrefetchPerformance {
            requests: self.stats.prefetch_requests,
            hits: self.stats.prefetch_hits,
            accuracy: self.stats.prefetch_accuracy,
            // FEATURE STATUS (v1.0.0): Prefetch cache size tracking pending
            // Will be implemented when prefetch_engine.get_cache_size() is available (v1.1.0+)
            cache_size: 0,
        }
    }

    /// Optimize management policies based on workload
    pub fn optimize_policies(&mut self) -> Result<(), MemoryManagementError> {
        // FEATURE STATUS (v1.0.0): Adaptive policy optimization pending
        //
        // The individual policy engines (GC, eviction, prefetch) are functional,
        // but automatic policy selection based on workload analysis requires
        // runtime profiling capabilities planned for v1.1.0.
        //
        // PLANNED (v1.1.0+):
        // - let access_patterns = self.prefetch_engine.analyze_access_patterns();
        // - self.gc_engine.set_preferred_strategy("generational");
        // - self.eviction_engine.set_active_policy("lru");
        //
        // For v1.0.0, users can manually configure policies through the
        // MemoryManagementConfig during initialization.

        Ok(())
    }
}

/// Memory access types for pattern tracking
#[derive(Debug, Clone)]
pub enum AccessType {
    Read,
    Write,
    ReadWrite,
    Sequential,
    Random,
}

/// Prefetch performance metrics
#[derive(Debug, Clone)]
pub struct PrefetchPerformance {
    pub requests: u64,
    pub hits: u64,
    pub accuracy: f64,
    pub cache_size: usize,
}

/// Access pattern analysis
#[derive(Debug, Clone)]
pub struct AccessPatterns {
    pub temporal_locality: f64,
    pub spatial_locality: f64,
    pub frequency_based: bool,
    pub stride_patterns: Vec<i64>,
}

/// Memory management errors
#[derive(Debug, Clone)]
pub enum MemoryManagementError {
    GarbageCollectionFailed(String),
    PrefetchFailed(String),
    EvictionFailed(String),
    DefragmentationFailed(String),
    BackgroundManagementDisabled,
    InvalidConfiguration(String),
    InternalError(String),
}

impl std::fmt::Display for MemoryManagementError {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            MemoryManagementError::GarbageCollectionFailed(msg) => {
                write!(f, "Garbage collection failed: {}", msg)
            }
            MemoryManagementError::PrefetchFailed(msg) => write!(f, "Prefetch failed: {}", msg),
            MemoryManagementError::EvictionFailed(msg) => write!(f, "Eviction failed: {}", msg),
            MemoryManagementError::DefragmentationFailed(msg) => {
                write!(f, "Defragmentation failed: {}", msg)
            }
            MemoryManagementError::BackgroundManagementDisabled => {
                write!(f, "Background management is disabled")
            }
            MemoryManagementError::InvalidConfiguration(msg) => {
                write!(f, "Invalid configuration: {}", msg)
            }
            MemoryManagementError::InternalError(msg) => write!(f, "Internal error: {}", msg),
        }
    }
}

impl std::error::Error for MemoryManagementError {}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_integrated_manager_creation() {
        let config = MemoryManagementConfig::default();
        let manager = IntegratedMemoryManager::new(config);
        assert!(!manager.background_enabled);
    }

    #[test]
    fn test_background_management() {
        let config = MemoryManagementConfig::default();
        let mut manager = IntegratedMemoryManager::new(config);
        let result = manager.start_background_management();
        assert!(result.is_ok());
        assert!(manager.background_enabled);
    }

    #[test]
    fn test_evict_memory_reclaims_real_bytes() {
        use super::eviction_policies::{CacheObject, ObjectPriority, ObjectType, RegionType};

        let config = MemoryManagementConfig::default();
        let mut manager = IntegratedMemoryManager::new(config);

        let object_size = 4096usize;
        let mut objects = HashMap::new();
        objects.insert(
            0x1000,
            CacheObject {
                address: 0x1000,
                size: object_size,
                created_at: Instant::now(),
                last_access: Instant::now(),
                access_count: 1,
                access_frequency: 1.0,
                priority: ObjectPriority::Normal,
                kernel_context: None,
                object_type: ObjectType::Data,
                eviction_cost: 1.0,
                replacement_cost: 1.0,
            },
        );
        let mut memory_regions = HashMap::new();
        memory_regions.insert(
            0x1000,
            MemoryRegion {
                base_addr: 0x1000,
                size: object_size,
                objects,
                region_type: RegionType::Buffer,
                pressure: 1.0,
                last_eviction: None,
            },
        );

        let evicted = manager
            .evict_memory(object_size, &memory_regions)
            .expect("eviction against a registered region should succeed");
        assert_eq!(evicted, object_size);
        assert_eq!(manager.get_stats().bytes_evicted, object_size as u64);
        assert_eq!(manager.get_stats().evictions_performed, 1);
    }

    #[test]
    fn test_evict_memory_empty_regions_evicts_nothing() {
        let config = MemoryManagementConfig::default();
        let mut manager = IntegratedMemoryManager::new(config);

        let evicted = manager
            .evict_memory(4096, &HashMap::new())
            .expect("eviction over no regions should still succeed");
        assert_eq!(evicted, 0);
    }

    #[test]
    fn test_prefetch_records_access_and_updates_stats() {
        let config = MemoryManagementConfig::default();
        let mut manager = IntegratedMemoryManager::new(config);

        let result = manager.prefetch(std::ptr::null_mut(), 128, Some("sequential"));
        assert!(result.is_ok());
        assert_eq!(manager.get_stats().prefetch_requests, 1);
    }

    #[test]
    fn test_update_access_pattern_feeds_prefetch_engine() {
        let config = MemoryManagementConfig::default();
        let mut manager = IntegratedMemoryManager::new(config);

        // Four consecutive forward accesses (64 bytes apart, same "thread")
        // form a sequential run of length >= SequentialConfig's
        // min_sequence_length (3), which should make the engine's
        // SequentialPrefetcher strategy fire and queue real requests -- proof
        // that the access data actually reaches the prefetching engine
        // rather than being dropped on the floor.
        let base = 0x10000usize;
        for i in 0..4u64 {
            let ptr = (base + i as usize * 64) as *mut std::ffi::c_void;
            let result = manager.update_access_pattern(ptr, 64, AccessType::Sequential);
            assert!(result.is_ok());
        }

        assert!(manager.prefetch_engine.get_stats().total_requests > 0);
    }

    #[test]
    fn test_stats_initialization() {
        let config = MemoryManagementConfig::default();
        let manager = IntegratedMemoryManager::new(config);
        let stats = manager.get_stats();
        assert_eq!(stats.gc_collections, 0);
        assert_eq!(stats.prefetch_requests, 0);
    }
}