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
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
// Memory eviction policies for GPU memory management
//
// This module provides sophisticated eviction strategies to manage limited
// GPU memory efficiently by determining which data should be removed when
// memory pressure occurs.

use std::collections::{BTreeMap, HashMap, HashSet, VecDeque};
use std::sync::{Arc, RwLock};
use std::time::{Duration, Instant};

/// Main eviction engine that manages multiple eviction policies
pub struct EvictionEngine {
    /// Configuration
    config: EvictionConfig,
    /// Statistics
    stats: EvictionStats,
    /// Available eviction policies
    policies: HashMap<String, Box<dyn EvictionPolicy>>,
    /// Currently active policy
    active_policy: String,
    /// Memory regions under management
    memory_regions: HashMap<usize, MemoryRegion>,
    /// Performance monitor
    performance_monitor: EvictionPerformanceMonitor,
    /// Policy selection history
    policy_history: VecDeque<PolicySelection>,
}

/// Eviction configuration
#[derive(Debug, Clone)]
pub struct EvictionConfig {
    /// Enable automatic eviction
    pub auto_eviction: bool,
    /// Memory pressure threshold to trigger eviction
    pub pressure_threshold: f64,
    /// Enable adaptive policy selection
    pub enable_adaptive: bool,
    /// Enable performance monitoring
    pub enable_monitoring: bool,
    /// Default eviction policy
    pub default_policy: String,
    /// Policy switching threshold
    pub policy_switch_threshold: f64,
    /// Minimum eviction batch size
    pub min_batch_size: usize,
    /// Maximum eviction batch size
    pub max_batch_size: usize,
    /// Enable workload-aware eviction
    pub workload_aware: bool,
    /// GPU kernel context consideration
    pub kernel_context_weight: f64,
}

impl Default for EvictionConfig {
    fn default() -> Self {
        Self {
            auto_eviction: true,
            pressure_threshold: 0.85,
            enable_adaptive: true,
            enable_monitoring: true,
            default_policy: "LRU".to_string(),
            policy_switch_threshold: 0.1,
            min_batch_size: 1,
            max_batch_size: 64,
            workload_aware: true,
            kernel_context_weight: 0.3,
        }
    }
}

/// Eviction statistics
#[derive(Debug, Clone, Default)]
pub struct EvictionStats {
    /// Total evictions performed
    pub total_evictions: u64,
    /// Total bytes evicted
    pub total_bytes_evicted: u64,
    /// Total objects evicted
    pub total_objects_evicted: u64,
    /// Average eviction time
    pub average_eviction_time: Duration,
    /// Eviction accuracy (correctly evicted items)
    pub eviction_accuracy: f64,
    /// Policy performance scores
    pub policy_scores: HashMap<String, f64>,
    /// Memory pressure events
    pub pressure_events: u64,
    /// Adaptive policy switches
    pub policy_switches: u64,
}

/// Memory region for eviction management
#[derive(Debug, Clone)]
pub struct MemoryRegion {
    /// Base address
    pub base_addr: usize,
    /// Region size
    pub size: usize,
    /// Cached objects in this region
    pub objects: HashMap<usize, CacheObject>,
    /// Region type (cache, buffer, etc.)
    pub region_type: RegionType,
    /// Current memory pressure
    pub pressure: f64,
    /// Last eviction time
    pub last_eviction: Option<Instant>,
}

/// Types of memory regions
#[derive(Debug, Clone, PartialEq)]
pub enum RegionType {
    Cache,
    Buffer,
    Texture,
    Constant,
    Shared,
    Global,
}

/// Cached object representation
#[derive(Debug, Clone)]
pub struct CacheObject {
    /// Object address
    pub address: usize,
    /// Object size
    pub size: usize,
    /// Creation time
    pub created_at: Instant,
    /// Last access time
    pub last_access: Instant,
    /// Access count
    pub access_count: u32,
    /// Access frequency (accesses per second)
    pub access_frequency: f64,
    /// Object priority
    pub priority: ObjectPriority,
    /// GPU kernel context
    pub kernel_context: Option<u32>,
    /// Object type
    pub object_type: ObjectType,
    /// Eviction cost (higher = more expensive to evict)
    pub eviction_cost: f64,
    /// Replacement cost (higher = more expensive to reload)
    pub replacement_cost: f64,
}

/// Object priority levels
#[derive(Debug, Clone, PartialEq, Ord, PartialOrd, Eq)]
pub enum ObjectPriority {
    Low,
    Normal,
    High,
    Critical,
}

/// Object type classification
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub enum ObjectType {
    Data,
    Texture,
    Constant,
    Instruction,
    Temporary,
    Persistent,
    Critical,
}

impl CacheObject {
    /// Update access information
    pub fn update_access(&mut self) {
        self.access_count += 1;
        let now = Instant::now();
        let time_since_creation = now.duration_since(self.created_at).as_secs_f64();

        if time_since_creation > 0.0 {
            self.access_frequency = self.access_count as f64 / time_since_creation;
        }

        self.last_access = now;
    }

    /// Calculate object utility score for eviction decisions
    pub fn calculate_utility(&self) -> f64 {
        let age_factor = self.last_access.elapsed().as_secs_f64();
        let frequency_factor = self.access_frequency;
        let priority_factor = match self.priority {
            ObjectPriority::Critical => 10.0,
            ObjectPriority::High => 5.0,
            ObjectPriority::Normal => 1.0,
            ObjectPriority::Low => 0.5,
        };

        let size_factor = 1.0 / (self.size as f64).sqrt();

        // Higher utility = less likely to be evicted
        (frequency_factor * priority_factor * size_factor) / (age_factor + 1.0)
    }
}

/// Eviction policy trait
pub trait EvictionPolicy: Send + Sync {
    fn name(&self) -> &str;
    fn select_victims(&mut self, region: &MemoryRegion, target_bytes: usize) -> Vec<usize>;
    fn update_access(&mut self, address: usize, object: &CacheObject);
    fn add_object(&mut self, address: usize, object: &CacheObject);
    fn remove_object(&mut self, address: usize);
    fn get_statistics(&self) -> PolicyStats;
    fn configure(&mut self, config: &EvictionConfig);
    fn reset(&mut self);
}

/// Policy statistics
#[derive(Debug, Clone, Default)]
pub struct PolicyStats {
    pub evictions: u64,
    pub bytes_evicted: u64,
    pub average_latency: Duration,
    pub accuracy_score: f64,
    pub hit_rate: f64,
}

/// LRU (Least Recently Used) eviction policy
pub struct LRUPolicy {
    /// LRU order tracking
    lru_order: VecDeque<usize>,
    /// Address to position mapping
    address_map: HashMap<usize, usize>,
    /// Statistics
    stats: PolicyStats,
}

impl Default for LRUPolicy {
    fn default() -> Self {
        Self::new()
    }
}

impl LRUPolicy {
    pub fn new() -> Self {
        Self {
            lru_order: VecDeque::new(),
            address_map: HashMap::new(),
            stats: PolicyStats::default(),
        }
    }

    fn move_to_end(&mut self, address: usize) {
        if let Some(&pos) = self.address_map.get(&address) {
            if pos < self.lru_order.len() {
                self.lru_order.remove(pos);
                self.lru_order.push_back(address);
                self.update_positions();
            }
        }
    }

    fn update_positions(&mut self) {
        self.address_map.clear();
        for (pos, &addr) in self.lru_order.iter().enumerate() {
            self.address_map.insert(addr, pos);
        }
    }
}

impl EvictionPolicy for LRUPolicy {
    fn name(&self) -> &str {
        "LRU"
    }

    fn select_victims(&mut self, region: &MemoryRegion, target_bytes: usize) -> Vec<usize> {
        let mut victims = Vec::new();
        let mut bytes_selected = 0;

        // Start from least recently used
        for &address in &self.lru_order {
            if let Some(object) = region.objects.get(&address) {
                victims.push(address);
                bytes_selected += object.size;

                if bytes_selected >= target_bytes {
                    break;
                }
            }
        }

        self.stats.evictions += victims.len() as u64;
        self.stats.bytes_evicted += bytes_selected as u64;

        victims
    }

    fn update_access(&mut self, address: usize, _object: &CacheObject) {
        self.move_to_end(address);
    }

    fn add_object(&mut self, address: usize, _object: &CacheObject) {
        if !self.address_map.contains_key(&address) {
            self.lru_order.push_back(address);
            self.address_map.insert(address, self.lru_order.len() - 1);
        }
    }

    fn remove_object(&mut self, address: usize) {
        if let Some(&pos) = self.address_map.get(&address) {
            if pos < self.lru_order.len() {
                self.lru_order.remove(pos);
                self.address_map.remove(&address);
                self.update_positions();
            }
        }
    }

    fn get_statistics(&self) -> PolicyStats {
        self.stats.clone()
    }

    fn configure(&mut self, _config: &EvictionConfig) {
        // LRU typically doesn't need configuration
    }

    fn reset(&mut self) {
        self.lru_order.clear();
        self.address_map.clear();
        self.stats = PolicyStats::default();
    }
}

/// LFU (Least Frequently Used) eviction policy
pub struct LFUPolicy {
    /// Frequency tracking
    frequency_map: HashMap<usize, u32>,
    /// Frequency buckets for efficient selection
    frequency_buckets: BTreeMap<u32, HashSet<usize>>,
    /// Statistics
    stats: PolicyStats,
}

impl Default for LFUPolicy {
    fn default() -> Self {
        Self::new()
    }
}

impl LFUPolicy {
    pub fn new() -> Self {
        Self {
            frequency_map: HashMap::new(),
            frequency_buckets: BTreeMap::new(),
            stats: PolicyStats::default(),
        }
    }

    fn update_frequency(&mut self, address: usize) {
        let old_freq = self.frequency_map.get(&address).copied().unwrap_or(0);
        let new_freq = old_freq + 1;

        // Remove from old bucket
        if old_freq > 0 {
            if let Some(bucket) = self.frequency_buckets.get_mut(&old_freq) {
                bucket.remove(&address);
                if bucket.is_empty() {
                    self.frequency_buckets.remove(&old_freq);
                }
            }
        }

        // Add to new bucket
        self.frequency_buckets
            .entry(new_freq)
            .or_default()
            .insert(address);
        self.frequency_map.insert(address, new_freq);
    }
}

impl EvictionPolicy for LFUPolicy {
    fn name(&self) -> &str {
        "LFU"
    }

    fn select_victims(&mut self, region: &MemoryRegion, target_bytes: usize) -> Vec<usize> {
        let mut victims = Vec::new();
        let mut bytes_selected = 0;

        // Select from lowest frequency buckets first
        for addresses in self.frequency_buckets.values() {
            for &address in addresses {
                if let Some(object) = region.objects.get(&address) {
                    victims.push(address);
                    bytes_selected += object.size;

                    if bytes_selected >= target_bytes {
                        break;
                    }
                }
            }

            if bytes_selected >= target_bytes {
                break;
            }
        }

        self.stats.evictions += victims.len() as u64;
        self.stats.bytes_evicted += bytes_selected as u64;

        victims
    }

    fn update_access(&mut self, address: usize, _object: &CacheObject) {
        self.update_frequency(address);
    }

    fn add_object(&mut self, address: usize, _object: &CacheObject) {
        self.update_frequency(address);
    }

    fn remove_object(&mut self, address: usize) {
        if let Some(freq) = self.frequency_map.remove(&address) {
            if let Some(bucket) = self.frequency_buckets.get_mut(&freq) {
                bucket.remove(&address);
                if bucket.is_empty() {
                    self.frequency_buckets.remove(&freq);
                }
            }
        }
    }

    fn get_statistics(&self) -> PolicyStats {
        self.stats.clone()
    }

    fn configure(&mut self, _config: &EvictionConfig) {
        // LFU typically doesn't need configuration
    }

    fn reset(&mut self) {
        self.frequency_map.clear();
        self.frequency_buckets.clear();
        self.stats = PolicyStats::default();
    }
}

/// FIFO (First In, First Out) eviction policy
pub struct FIFOPolicy {
    /// Insertion order tracking
    insertion_order: VecDeque<usize>,
    /// Statistics
    stats: PolicyStats,
}

impl Default for FIFOPolicy {
    fn default() -> Self {
        Self::new()
    }
}

impl FIFOPolicy {
    pub fn new() -> Self {
        Self {
            insertion_order: VecDeque::new(),
            stats: PolicyStats::default(),
        }
    }
}

impl EvictionPolicy for FIFOPolicy {
    fn name(&self) -> &str {
        "FIFO"
    }

    fn select_victims(&mut self, region: &MemoryRegion, target_bytes: usize) -> Vec<usize> {
        let mut victims = Vec::new();
        let mut bytes_selected = 0;

        // Select oldest insertions first
        for &address in &self.insertion_order {
            if let Some(object) = region.objects.get(&address) {
                victims.push(address);
                bytes_selected += object.size;

                if bytes_selected >= target_bytes {
                    break;
                }
            }
        }

        self.stats.evictions += victims.len() as u64;
        self.stats.bytes_evicted += bytes_selected as u64;

        victims
    }

    fn update_access(&mut self, _address: usize, _object: &CacheObject) {
        // FIFO doesn't consider access patterns
    }

    fn add_object(&mut self, address: usize, _object: &CacheObject) {
        self.insertion_order.push_back(address);
    }

    fn remove_object(&mut self, address: usize) {
        if let Some(pos) = self
            .insertion_order
            .iter()
            .position(|&addr| addr == address)
        {
            self.insertion_order.remove(pos);
        }
    }

    fn get_statistics(&self) -> PolicyStats {
        self.stats.clone()
    }

    fn configure(&mut self, _config: &EvictionConfig) {
        // FIFO typically doesn't need configuration
    }

    fn reset(&mut self) {
        self.insertion_order.clear();
        self.stats = PolicyStats::default();
    }
}

/// Clock (Second Chance) eviction policy
pub struct ClockPolicy {
    /// Circular list of objects
    clock_list: Vec<ClockEntry>,
    /// Address to index mapping
    address_map: HashMap<usize, usize>,
    /// Clock hand position
    clock_hand: usize,
    /// Statistics
    stats: PolicyStats,
}

/// Clock entry
#[derive(Debug, Clone)]
struct ClockEntry {
    address: usize,
    reference_bit: bool,
}

impl Default for ClockPolicy {
    fn default() -> Self {
        Self::new()
    }
}

impl ClockPolicy {
    pub fn new() -> Self {
        Self {
            clock_list: Vec::new(),
            address_map: HashMap::new(),
            clock_hand: 0,
            stats: PolicyStats::default(),
        }
    }

    fn advance_clock(&mut self) -> Option<usize> {
        if self.clock_list.is_empty() {
            return None;
        }

        let start_pos = self.clock_hand;

        loop {
            let entry = &mut self.clock_list[self.clock_hand];

            if entry.reference_bit {
                // Give second chance
                entry.reference_bit = false;
            } else {
                // Victim found
                let victim = entry.address;
                self.clock_hand = (self.clock_hand + 1) % self.clock_list.len();
                return Some(victim);
            }

            self.clock_hand = (self.clock_hand + 1) % self.clock_list.len();

            if self.clock_hand == start_pos {
                // Full cycle completed, all had reference bits set
                break;
            }
        }

        // If all had reference bits, just return first one
        if !self.clock_list.is_empty() {
            Some(self.clock_list[0].address)
        } else {
            None
        }
    }
}

impl EvictionPolicy for ClockPolicy {
    fn name(&self) -> &str {
        "Clock"
    }

    fn select_victims(&mut self, region: &MemoryRegion, target_bytes: usize) -> Vec<usize> {
        let mut victims = Vec::new();
        let mut bytes_selected = 0;

        while bytes_selected < target_bytes {
            if let Some(victim_addr) = self.advance_clock() {
                if let Some(object) = region.objects.get(&victim_addr) {
                    victims.push(victim_addr);
                    bytes_selected += object.size;
                }
            } else {
                break;
            }
        }

        self.stats.evictions += victims.len() as u64;
        self.stats.bytes_evicted += bytes_selected as u64;

        victims
    }

    fn update_access(&mut self, address: usize, _object: &CacheObject) {
        if let Some(&index) = self.address_map.get(&address) {
            if index < self.clock_list.len() {
                self.clock_list[index].reference_bit = true;
            }
        }
    }

    fn add_object(&mut self, address: usize, _object: &CacheObject) {
        let entry = ClockEntry {
            address,
            reference_bit: true,
        };

        self.clock_list.push(entry);
        self.address_map.insert(address, self.clock_list.len() - 1);
    }

    fn remove_object(&mut self, address: usize) {
        if let Some(&index) = self.address_map.get(&address) {
            if index < self.clock_list.len() {
                self.clock_list.remove(index);
                self.address_map.remove(&address);

                // Update all subsequent indices
                for i in index..self.clock_list.len() {
                    let addr = self.clock_list[i].address;
                    self.address_map.insert(addr, i);
                }

                // Adjust clock hand
                if self.clock_hand > index {
                    self.clock_hand -= 1;
                } else if self.clock_hand >= self.clock_list.len() && !self.clock_list.is_empty() {
                    self.clock_hand = 0;
                }
            }
        }
    }

    fn get_statistics(&self) -> PolicyStats {
        self.stats.clone()
    }

    fn configure(&mut self, _config: &EvictionConfig) {
        // Clock typically doesn't need configuration
    }

    fn reset(&mut self) {
        self.clock_list.clear();
        self.address_map.clear();
        self.clock_hand = 0;
        self.stats = PolicyStats::default();
    }
}

/// Adaptive Replacement Cache (ARC) policy
pub struct ARCPolicy {
    /// T1: Recent cache misses
    t1: VecDeque<usize>,
    /// T2: Recent cache hits
    t2: VecDeque<usize>,
    /// B1: Ghost entries for T1
    b1: VecDeque<usize>,
    /// B2: Ghost entries for T2
    b2: VecDeque<usize>,
    /// Adaptation parameter
    p: usize,
    /// Cache capacity
    capacity: usize,
    /// Statistics
    stats: PolicyStats,
}

impl ARCPolicy {
    pub fn new(capacity: usize) -> Self {
        Self {
            t1: VecDeque::new(),
            t2: VecDeque::new(),
            b1: VecDeque::new(),
            b2: VecDeque::new(),
            p: 0,
            capacity,
            stats: PolicyStats::default(),
        }
    }

    fn replace(&mut self, address: usize) -> Option<usize> {
        let t1_len = self.t1.len();

        if t1_len > 0 && (t1_len > self.p || (self.b2.contains(&address) && t1_len == self.p)) {
            // Remove from T1
            if let Some(victim) = self.t1.pop_front() {
                self.b1.push_back(victim);
                if self.b1.len() > self.capacity {
                    self.b1.pop_front();
                }
                return Some(victim);
            }
        } else {
            // Remove from T2
            if let Some(victim) = self.t2.pop_front() {
                self.b2.push_back(victim);
                if self.b2.len() > self.capacity {
                    self.b2.pop_front();
                }
                return Some(victim);
            }
        }

        None
    }

    fn adapt(&mut self, address: usize) {
        let delta = if self.b1.len() >= self.b2.len() {
            1
        } else {
            self.b2.len() / self.b1.len().max(1)
        };

        if self.b1.contains(&address) {
            self.p = (self.p + delta).min(self.capacity);
        } else if self.b2.contains(&address) {
            self.p = self.p.saturating_sub(delta);
        }
    }
}

impl EvictionPolicy for ARCPolicy {
    fn name(&self) -> &str {
        "ARC"
    }

    fn select_victims(&mut self, region: &MemoryRegion, target_bytes: usize) -> Vec<usize> {
        let mut victims = Vec::new();
        let mut bytes_selected = 0;

        while bytes_selected < target_bytes {
            if let Some(victim_addr) = self.replace(0) {
                // Simplified
                if let Some(object) = region.objects.get(&victim_addr) {
                    victims.push(victim_addr);
                    bytes_selected += object.size;
                }
            } else {
                break;
            }
        }

        self.stats.evictions += victims.len() as u64;
        self.stats.bytes_evicted += bytes_selected as u64;

        victims
    }

    fn update_access(&mut self, address: usize, _object: &CacheObject) {
        // Simplified ARC access handling
        if self.t1.contains(&address) {
            // Move from T1 to T2
            if let Some(pos) = self.t1.iter().position(|&addr| addr == address) {
                self.t1.remove(pos);
                self.t2.push_back(address);
            }
        } else if self.t2.contains(&address) {
            // Move to end of T2
            if let Some(pos) = self.t2.iter().position(|&addr| addr == address) {
                self.t2.remove(pos);
                self.t2.push_back(address);
            }
        }
    }

    fn add_object(&mut self, address: usize, _object: &CacheObject) {
        if self.b1.contains(&address) {
            self.adapt(address);
            self.b1.retain(|&addr| addr != address);
            self.t2.push_back(address);
        } else if self.b2.contains(&address) {
            self.adapt(address);
            self.b2.retain(|&addr| addr != address);
            self.t2.push_back(address);
        } else {
            self.t1.push_back(address);
        }
    }

    fn remove_object(&mut self, address: usize) {
        self.t1.retain(|&addr| addr != address);
        self.t2.retain(|&addr| addr != address);
        self.b1.retain(|&addr| addr != address);
        self.b2.retain(|&addr| addr != address);
    }

    fn get_statistics(&self) -> PolicyStats {
        self.stats.clone()
    }

    fn configure(&mut self, _config: &EvictionConfig) {
        // ARC adapts automatically
    }

    fn reset(&mut self) {
        self.t1.clear();
        self.t2.clear();
        self.b1.clear();
        self.b2.clear();
        self.p = 0;
        self.stats = PolicyStats::default();
    }
}

/// Workload-aware eviction policy
pub struct WorkloadAwarePolicy {
    /// Base policy to extend
    base_policy: Box<dyn EvictionPolicy>,
    /// Kernel context weights
    kernel_weights: HashMap<u32, f64>,
    /// Object type priorities
    type_priorities: HashMap<ObjectType, f64>,
    /// Statistics
    stats: PolicyStats,
}

impl WorkloadAwarePolicy {
    pub fn new(base_policy: Box<dyn EvictionPolicy>) -> Self {
        let mut type_priorities = HashMap::new();
        type_priorities.insert(ObjectType::Critical, 10.0);
        type_priorities.insert(ObjectType::Persistent, 5.0);
        type_priorities.insert(ObjectType::Data, 2.0);
        type_priorities.insert(ObjectType::Texture, 1.5);
        type_priorities.insert(ObjectType::Constant, 1.0);
        type_priorities.insert(ObjectType::Temporary, 0.5);

        Self {
            base_policy,
            kernel_weights: HashMap::new(),
            type_priorities,
            stats: PolicyStats::default(),
        }
    }

    fn calculate_eviction_priority(&self, object: &CacheObject) -> f64 {
        let mut priority = object.calculate_utility();

        // Apply object type priority
        if let Some(&type_priority) = self.type_priorities.get(&object.object_type) {
            priority *= type_priority;
        }

        // Apply kernel context weight
        if let Some(kernel_id) = object.kernel_context {
            if let Some(&weight) = self.kernel_weights.get(&kernel_id) {
                priority *= weight;
            }
        }

        // Apply object priority
        let priority_multiplier = match object.priority {
            ObjectPriority::Critical => 100.0,
            ObjectPriority::High => 10.0,
            ObjectPriority::Normal => 1.0,
            ObjectPriority::Low => 0.1,
        };

        priority * priority_multiplier
    }
}

impl EvictionPolicy for WorkloadAwarePolicy {
    fn name(&self) -> &str {
        "WorkloadAware"
    }

    fn select_victims(&mut self, region: &MemoryRegion, target_bytes: usize) -> Vec<usize> {
        // Calculate priorities for all objects
        let mut object_priorities: Vec<(usize, f64)> = region
            .objects
            .iter()
            .map(|(&addr, obj)| (addr, self.calculate_eviction_priority(obj)))
            .collect();

        // Sort by priority (lowest first = best eviction candidates)
        object_priorities
            .sort_by(|a, b| a.1.partial_cmp(&b.1).unwrap_or(std::cmp::Ordering::Equal));

        let mut victims = Vec::new();
        let mut bytes_selected = 0;

        for (address, _priority) in object_priorities {
            if let Some(object) = region.objects.get(&address) {
                victims.push(address);
                bytes_selected += object.size;

                if bytes_selected >= target_bytes {
                    break;
                }
            }
        }

        self.stats.evictions += victims.len() as u64;
        self.stats.bytes_evicted += bytes_selected as u64;

        victims
    }

    fn update_access(&mut self, address: usize, object: &CacheObject) {
        self.base_policy.update_access(address, object);

        // Update kernel weights based on access patterns
        if let Some(kernel_id) = object.kernel_context {
            let weight = self.kernel_weights.entry(kernel_id).or_insert(1.0);
            *weight = (*weight * 0.9 + 1.1).min(10.0); // Increase weight for active kernels
        }
    }

    fn add_object(&mut self, address: usize, object: &CacheObject) {
        self.base_policy.add_object(address, object);
    }

    fn remove_object(&mut self, address: usize) {
        self.base_policy.remove_object(address);
    }

    fn get_statistics(&self) -> PolicyStats {
        let mut stats = self.stats.clone();
        let base_stats = self.base_policy.get_statistics();

        // Combine statistics
        stats.evictions += base_stats.evictions;
        stats.bytes_evicted += base_stats.bytes_evicted;

        stats
    }

    fn configure(&mut self, config: &EvictionConfig) {
        self.base_policy.configure(config);
    }

    fn reset(&mut self) {
        self.base_policy.reset();
        self.kernel_weights.clear();
        self.stats = PolicyStats::default();
    }
}

/// Performance monitoring for eviction policies
pub struct EvictionPerformanceMonitor {
    /// Performance history
    history: VecDeque<EvictionPerformance>,
    /// Policy performance tracking
    policy_performance: HashMap<String, Vec<f64>>,
    /// Configuration
    config: MonitorConfig,
}

/// Eviction performance sample
#[derive(Debug, Clone)]
pub struct EvictionPerformance {
    pub timestamp: Instant,
    pub policy_name: String,
    pub eviction_time: Duration,
    pub bytes_evicted: usize,
    pub objects_evicted: usize,
    pub accuracy_score: f64,
}

/// Monitor configuration
#[derive(Debug, Clone)]
pub struct MonitorConfig {
    pub history_size: usize,
    pub performance_window: usize,
    pub enable_adaptive: bool,
}

impl Default for MonitorConfig {
    fn default() -> Self {
        Self {
            history_size: 1000,
            performance_window: 100,
            enable_adaptive: true,
        }
    }
}

impl EvictionPerformanceMonitor {
    pub fn new(config: MonitorConfig) -> Self {
        Self {
            history: VecDeque::with_capacity(config.history_size),
            policy_performance: HashMap::new(),
            config,
        }
    }

    /// Record eviction performance
    pub fn record_performance(&mut self, performance: EvictionPerformance) {
        self.history.push_back(performance.clone());
        if self.history.len() > self.config.history_size {
            self.history.pop_front();
        }

        // Update policy performance tracking
        let scores = self
            .policy_performance
            .entry(performance.policy_name.clone())
            .or_default();

        scores.push(performance.accuracy_score);
        if scores.len() > self.config.performance_window {
            scores.remove(0);
        }
    }

    /// Get best performing policy
    pub fn get_best_policy(&self) -> Option<String> {
        if !self.config.enable_adaptive {
            return None;
        }

        let mut best_policy = None;
        let mut best_score = 0.0;

        for (policy_name, scores) in &self.policy_performance {
            if scores.len() >= 5 {
                // Minimum samples required
                let avg_score = scores.iter().sum::<f64>() / scores.len() as f64;
                if avg_score > best_score {
                    best_score = avg_score;
                    best_policy = Some(policy_name.clone());
                }
            }
        }

        best_policy
    }
}

/// Policy selection record
#[derive(Debug, Clone)]
pub struct PolicySelection {
    pub timestamp: Instant,
    pub policy_name: String,
    pub reason: String,
    pub performance_score: f64,
}

impl EvictionEngine {
    pub fn new(config: EvictionConfig) -> Self {
        let mut policies: HashMap<String, Box<dyn EvictionPolicy>> = HashMap::new();

        // Add built-in policies
        policies.insert("LRU".to_string(), Box::new(LRUPolicy::new()));
        policies.insert("LFU".to_string(), Box::new(LFUPolicy::new()));
        policies.insert("FIFO".to_string(), Box::new(FIFOPolicy::new()));
        policies.insert("Clock".to_string(), Box::new(ClockPolicy::new()));
        policies.insert("ARC".to_string(), Box::new(ARCPolicy::new(1000)));

        if config.workload_aware {
            let base_policy = Box::new(LRUPolicy::new());
            policies.insert(
                "WorkloadAware".to_string(),
                Box::new(WorkloadAwarePolicy::new(base_policy)),
            );
        }

        let active_policy = config.default_policy.clone();
        let performance_monitor = EvictionPerformanceMonitor::new(MonitorConfig::default());

        Self {
            config,
            stats: EvictionStats::default(),
            policies,
            active_policy,
            memory_regions: HashMap::new(),
            performance_monitor,
            policy_history: VecDeque::with_capacity(100),
        }
    }

    /// Register a memory region
    pub fn register_region(&mut self, base_addr: usize, size: usize, region_type: RegionType) {
        let region = MemoryRegion {
            base_addr,
            size,
            objects: HashMap::new(),
            region_type,
            pressure: 0.0,
            last_eviction: None,
        };

        self.memory_regions.insert(base_addr, region);
    }

    /// Add object to tracking
    pub fn add_object(
        &mut self,
        region_addr: usize,
        object: CacheObject,
    ) -> Result<(), EvictionError> {
        let region = self
            .memory_regions
            .get_mut(&region_addr)
            .ok_or_else(|| EvictionError::RegionNotFound("Region not registered".to_string()))?;

        // Add to all policies
        for policy in self.policies.values_mut() {
            policy.add_object(object.address, &object);
        }

        region.objects.insert(object.address, object);
        Ok(())
    }

    /// Update object access
    pub fn update_access(
        &mut self,
        region_addr: usize,
        object_addr: usize,
    ) -> Result<(), EvictionError> {
        let region = self
            .memory_regions
            .get_mut(&region_addr)
            .ok_or_else(|| EvictionError::RegionNotFound("Region not found".to_string()))?;

        if let Some(object) = region.objects.get_mut(&object_addr) {
            object.update_access();

            // Update all policies
            for policy in self.policies.values_mut() {
                policy.update_access(object_addr, object);
            }
        }

        Ok(())
    }

    /// Check if eviction is needed
    pub fn should_evict(&self, region_addr: usize) -> bool {
        if let Some(region) = self.memory_regions.get(&region_addr) {
            region.pressure > self.config.pressure_threshold
        } else {
            false
        }
    }

    /// Perform eviction
    pub fn evict(
        &mut self,
        region_addr: usize,
        target_bytes: usize,
    ) -> Result<Vec<usize>, EvictionError> {
        let region = self
            .memory_regions
            .get(&region_addr)
            .ok_or_else(|| EvictionError::RegionNotFound("Region not found".to_string()))?;

        let start_time = Instant::now();

        // Select policy (adaptive if enabled)
        let policy_name = if self.config.enable_adaptive {
            self.performance_monitor
                .get_best_policy()
                .unwrap_or_else(|| self.active_policy.clone())
        } else {
            self.active_policy.clone()
        };

        let victims = if let Some(policy) = self.policies.get_mut(&policy_name) {
            policy.select_victims(region, target_bytes)
        } else {
            return Err(EvictionError::PolicyNotFound(
                "Policy not available".to_string(),
            ));
        };

        let eviction_time = start_time.elapsed();

        // Remove evicted objects
        if let Some(region) = self.memory_regions.get_mut(&region_addr) {
            for &victim_addr in &victims {
                region.objects.remove(&victim_addr);

                // Remove from all policies
                for policy in self.policies.values_mut() {
                    policy.remove_object(victim_addr);
                }
            }

            region.last_eviction = Some(Instant::now());
        }

        // Update statistics
        self.stats.total_evictions += 1;
        self.stats.total_objects_evicted += victims.len() as u64;

        let total_eviction_time = self.stats.average_eviction_time.as_nanos() as u64
            * (self.stats.total_evictions - 1)
            + eviction_time.as_nanos() as u64;
        self.stats.average_eviction_time =
            Duration::from_nanos(total_eviction_time / self.stats.total_evictions);

        // Record performance
        let performance = EvictionPerformance {
            timestamp: start_time,
            policy_name: policy_name.clone(),
            eviction_time,
            bytes_evicted: target_bytes,
            objects_evicted: victims.len(),
            accuracy_score: 0.8, // Would be calculated based on future access patterns
        };

        self.performance_monitor.record_performance(performance);

        Ok(victims)
    }

    /// Switch active policy
    pub fn switch_policy(&mut self, policy_name: String) -> Result<(), EvictionError> {
        if !self.policies.contains_key(&policy_name) {
            return Err(EvictionError::PolicyNotFound(
                "Policy not available".to_string(),
            ));
        }

        let selection = PolicySelection {
            timestamp: Instant::now(),
            policy_name: policy_name.clone(),
            reason: "Manual switch".to_string(),
            performance_score: 0.0,
        };

        self.policy_history.push_back(selection);
        if self.policy_history.len() > 100 {
            self.policy_history.pop_front();
        }

        self.active_policy = policy_name;
        self.stats.policy_switches += 1;

        Ok(())
    }

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

    /// Get policy statistics
    pub fn get_policy_stats(&self) -> HashMap<String, PolicyStats> {
        self.policies
            .iter()
            .map(|(name, policy)| (name.clone(), policy.get_statistics()))
            .collect()
    }
}

/// Eviction errors
#[derive(Debug, Clone)]
pub enum EvictionError {
    RegionNotFound(String),
    PolicyNotFound(String),
    EvictionFailed(String),
    InvalidConfiguration(String),
}

impl std::fmt::Display for EvictionError {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            EvictionError::RegionNotFound(msg) => write!(f, "Region not found: {}", msg),
            EvictionError::PolicyNotFound(msg) => write!(f, "Policy not found: {}", msg),
            EvictionError::EvictionFailed(msg) => write!(f, "Eviction failed: {}", msg),
            EvictionError::InvalidConfiguration(msg) => write!(f, "Invalid configuration: {}", msg),
        }
    }
}

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

/// Thread-safe eviction engine wrapper
pub struct ThreadSafeEvictionEngine {
    engine: Arc<RwLock<EvictionEngine>>,
}

impl ThreadSafeEvictionEngine {
    pub fn new(config: EvictionConfig) -> Self {
        Self {
            engine: Arc::new(RwLock::new(EvictionEngine::new(config))),
        }
    }

    pub fn should_evict(&self, region_addr: usize) -> bool {
        let engine = self.engine.read().unwrap_or_else(|e| e.into_inner());
        engine.should_evict(region_addr)
    }

    pub fn evict(
        &self,
        region_addr: usize,
        target_bytes: usize,
    ) -> Result<Vec<usize>, EvictionError> {
        let mut engine = self.engine.write().unwrap_or_else(|e| e.into_inner());
        engine.evict(region_addr, target_bytes)
    }

    pub fn add_object(&self, region_addr: usize, object: CacheObject) -> Result<(), EvictionError> {
        let mut engine = self.engine.write().unwrap_or_else(|e| e.into_inner());
        engine.add_object(region_addr, object)
    }

    pub fn get_stats(&self) -> EvictionStats {
        let engine = self.engine.read().unwrap_or_else(|e| e.into_inner());
        engine.get_stats().clone()
    }
}

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

    #[test]
    fn test_eviction_engine_creation() {
        let config = EvictionConfig::default();
        let engine = EvictionEngine::new(config);
        assert!(!engine.policies.is_empty());
    }

    #[test]
    fn test_lru_policy() {
        let mut policy = LRUPolicy::new();
        assert_eq!(policy.name(), "LRU");

        let object = CacheObject {
            address: 0x1000,
            size: 64,
            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,
        };

        policy.add_object(0x1000, &object);
        assert_eq!(policy.lru_order.len(), 1);
    }

    #[test]
    fn test_cache_object_utility() {
        let object = CacheObject {
            address: 0x1000,
            size: 64,
            created_at: Instant::now() - Duration::from_secs(10),
            last_access: Instant::now() - Duration::from_secs(1),
            access_count: 5,
            access_frequency: 0.5,
            priority: ObjectPriority::High,
            kernel_context: Some(100),
            object_type: ObjectType::Data,
            eviction_cost: 1.0,
            replacement_cost: 2.0,
        };

        let utility = object.calculate_utility();
        assert!(utility > 0.0);
    }

    #[test]
    fn test_thread_safe_engine() {
        let config = EvictionConfig::default();
        let engine = ThreadSafeEvictionEngine::new(config);

        let stats = engine.get_stats();
        assert_eq!(stats.total_evictions, 0);
    }
}