torsh-backend 0.1.2

Backend abstraction layer for ToRSh
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
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
//! Memory fragmentation tracking and mitigation
//!
//! This module provides comprehensive memory fragmentation analysis and mitigation capabilities including:
//! - Real-time fragmentation detection and scoring algorithms
//! - Automated compaction and defragmentation strategies
//! - Fragmentation event tracking and root cause analysis
//! - Performance impact assessment and optimization recommendations
//! - Advanced fragmentation metrics and predictive modeling

// Framework infrastructure - components designed for future use
#![allow(dead_code)]
use crate::Device;
use parking_lot::Mutex;
use std::collections::{BTreeMap, HashMap, VecDeque};
use std::sync::Arc;
use std::time::{Duration, Instant};

/// Information about a memory allocation for fragmentation analysis
#[derive(Debug, Clone)]
pub struct AllocationInfo {
    /// Memory address of the allocation
    pub address: usize,
    /// Whether this block is free (true) or allocated (false)
    pub is_free: bool,
    /// Last time this allocation was accessed
    pub last_access: Instant,
}

/// Memory fragmentation tracker
///
/// Comprehensive system for tracking memory fragmentation across devices
/// with real-time analysis and automated mitigation capabilities.
#[derive(Debug)]
pub struct FragmentationTracker {
    /// Free block sizes by device (size -> count)
    pub free_blocks: HashMap<Device, BTreeMap<usize, usize>>,

    /// Fragmentation scores by device (0.0 = no fragmentation, 1.0 = severe)
    pub fragmentation_scores: HashMap<Device, f64>,

    /// Largest free block by device
    pub largest_free_block: HashMap<Device, usize>,

    /// Fragmentation events history
    pub fragmentation_events: Vec<FragmentationEvent>,

    /// Memory compaction statistics
    pub compaction_stats: CompactionStats,

    /// Fragmentation analysis configuration
    config: FragmentationConfig,

    /// Advanced fragmentation metrics
    advanced_metrics: Arc<Mutex<AdvancedFragmentationMetrics>>,

    /// Fragmentation prediction model
    prediction_model: Arc<Mutex<FragmentationPredictionModel>>,
}

/// Fragmentation event record
///
/// Tracks significant fragmentation events with detailed context
/// and mitigation actions taken.
#[derive(Debug, Clone)]
pub struct FragmentationEvent {
    /// Event timestamp
    pub timestamp: Instant,

    /// Device affected
    pub device: Device,

    /// Fragmentation level before event
    pub fragmentation_before: f64,

    /// Fragmentation level after event
    pub fragmentation_after: f64,

    /// Root cause of fragmentation
    pub cause: FragmentationCause,

    /// Recovery action taken (if any)
    pub recovery_action: Option<FragmentationRecovery>,

    /// Event impact assessment
    pub impact: FragmentationImpact,

    /// Event context
    pub context: FragmentationContext,
}

/// Causes of memory fragmentation
///
/// Categorizes different sources of memory fragmentation for targeted mitigation.
#[derive(Debug, Clone, PartialEq)]
pub enum FragmentationCause {
    /// Mixed allocation sizes causing internal fragmentation
    MixedAllocationSizes {
        size_variance: f64,
        allocation_count: usize,
    },

    /// Frequent allocations and deallocations causing external fragmentation
    FrequentAllocDealloc {
        alloc_frequency: f64,
        dealloc_frequency: f64,
    },

    /// Long-lived allocations blocking compaction
    LongLivedAllocations {
        allocation_age: Duration,
        blocking_compaction: bool,
    },

    /// Misaligned allocations causing padding waste
    MisalignedAllocations {
        alignment_requirement: usize,
        waste_percentage: f64,
    },

    /// Memory pool overflow forcing general heap usage
    PoolOverflow {
        pool_name: String,
        overflow_amount: usize,
    },

    /// Temporal allocation patterns causing fragmentation
    TemporalPatterns {
        pattern_type: String,
        periodicity: Duration,
    },

    /// Allocation size clustering causing fragmentation
    SizeClustering {
        cluster_sizes: Vec<usize>,
        fragmentation_factor: f64,
    },
}

/// Fragmentation recovery actions
///
/// Different mitigation strategies for addressing memory fragmentation.
#[derive(Debug, Clone)]
pub enum FragmentationRecovery {
    /// Memory compaction performed
    Compaction {
        blocks_moved: usize,
        time_taken: Duration,
        memory_recovered: usize,
        success_rate: f64,
    },

    /// Memory pool reorganization
    PoolReorganization {
        pools_affected: usize,
        reorganization_type: PoolReorganizationType,
        efficiency_improvement: f64,
    },

    /// Allocation strategy change
    StrategyChange {
        old_strategy: AllocationStrategy,
        new_strategy: AllocationStrategy,
        expected_improvement: f64,
    },

    /// Memory defragmentation
    Defragmentation {
        memory_recovered: usize,
        defrag_method: DefragmentationMethod,
        performance_impact: f64,
    },

    /// Garbage collection triggered
    GarbageCollection {
        memory_freed: usize,
        gc_duration: Duration,
        gc_type: GarbageCollectionType,
    },

    /// Pool expansion to reduce pressure
    PoolExpansion {
        pool_name: String,
        expansion_size: usize,
        fragmentation_reduction: f64,
    },
}

/// Pool reorganization types
#[derive(Debug, Clone)]
pub enum PoolReorganizationType {
    SizeClassRebalancing,
    PoolMerging,
    PoolSplitting,
    PoolCoalescing,
}

/// Allocation strategies
#[derive(Debug, Clone)]
pub enum AllocationStrategy {
    FirstFit,
    BestFit,
    WorstFit,
    NextFit,
    BuddySystem,
    SlabAllocator,
    PoolAllocator,
    StackAllocator,
}

/// Defragmentation methods
#[derive(Debug, Clone)]
pub enum DefragmentationMethod {
    CopyingGC,
    MarkAndSweep,
    Compaction,
    Coalescing,
    PoolReorganization,
}

/// Garbage collection types
#[derive(Debug, Clone)]
pub enum GarbageCollectionType {
    Minor,
    Major,
    Full,
    Incremental,
    Concurrent,
}

/// Fragmentation impact assessment
///
/// Quantifies the impact of fragmentation on system performance.
#[derive(Debug, Clone)]
pub struct FragmentationImpact {
    /// Memory utilization efficiency (0.0 to 1.0)
    pub memory_efficiency: f64,

    /// Allocation performance impact (multiplier)
    pub allocation_slowdown: f64,

    /// Cache performance impact
    pub cache_impact: CacheImpact,

    /// Bandwidth utilization impact
    pub bandwidth_impact: f64,

    /// Overall performance score (0.0 to 1.0, higher is better)
    pub performance_score: f64,

    /// Predicted future impact
    pub future_impact: FutureImpactPrediction,
}

/// Cache impact from fragmentation
#[derive(Debug, Clone)]
pub struct CacheImpact {
    /// L1 cache hit rate reduction
    pub l1_hit_rate_reduction: f64,

    /// L2 cache hit rate reduction
    pub l2_hit_rate_reduction: f64,

    /// TLB miss rate increase
    pub tlb_miss_increase: f64,

    /// Cache line utilization efficiency
    pub cache_line_efficiency: f64,
}

/// Future impact prediction
#[derive(Debug, Clone)]
pub struct FutureImpactPrediction {
    /// Predicted fragmentation level in 1 hour
    pub one_hour_prediction: f64,

    /// Predicted fragmentation level in 1 day
    pub one_day_prediction: f64,

    /// Prediction confidence
    pub confidence: f64,

    /// Recommended action timeline
    pub action_timeline: ActionTimeline,
}

/// Action timeline recommendations
#[derive(Debug, Clone)]
pub enum ActionTimeline {
    Immediate,
    Within1Hour,
    Within1Day,
    Within1Week,
    LongTerm,
}

/// Fragmentation context information
#[derive(Debug, Clone)]
pub struct FragmentationContext {
    /// System memory pressure at time of event
    pub memory_pressure: f64,

    /// Concurrent allocations during event
    pub concurrent_allocations: usize,

    /// Workload type
    pub workload_type: WorkloadType,

    /// Allocation pattern at time of event
    pub allocation_pattern: AllocationPattern,

    /// System load information
    pub system_load: SystemLoad,
}

/// Workload types affecting fragmentation
#[derive(Debug, Clone)]
pub enum WorkloadType {
    ComputeIntensive,
    MemoryIntensive,
    StreamingWorkload,
    BatchProcessing,
    InteractiveWorkload,
    MachineLearning,
    Unknown,
}

/// Allocation patterns
#[derive(Debug, Clone)]
pub enum AllocationPattern {
    Sequential,
    Random,
    Clustered,
    Periodic,
    Bursty,
    Streaming,
}

/// System load information
#[derive(Debug, Clone)]
pub struct SystemLoad {
    /// CPU utilization percentage
    pub cpu_utilization: f64,

    /// Memory utilization percentage
    pub memory_utilization: f64,

    /// I/O pressure level
    pub io_pressure: f64,

    /// Number of active threads
    pub active_threads: usize,
}

/// Memory compaction statistics
///
/// Tracks the effectiveness and performance of memory compaction operations.
#[derive(Debug, Clone, Default)]
pub struct CompactionStats {
    /// Total compactions performed
    pub total_compactions: u64,

    /// Total time spent compacting
    pub total_compaction_time: Duration,

    /// Total memory recovered through compaction
    pub total_memory_recovered: usize,

    /// Average compaction time
    pub average_compaction_time: Duration,

    /// Compaction efficiency (memory recovered / time spent)
    pub compaction_efficiency: f64,

    /// Last compaction timestamp
    pub last_compaction: Option<Instant>,

    /// Compaction success rate
    pub success_rate: f64,

    /// Average memory recovered per compaction
    pub avg_memory_recovered: usize,

    /// Compaction frequency statistics
    pub frequency_stats: CompactionFrequencyStats,
}

/// Compaction frequency statistics
#[derive(Debug, Clone, Default)]
pub struct CompactionFrequencyStats {
    /// Compactions in last hour
    pub last_hour: u32,

    /// Compactions in last day
    pub last_day: u32,

    /// Compactions in last week
    pub last_week: u32,

    /// Peak compaction frequency (per hour)
    pub peak_frequency: u32,
}

/// Advanced fragmentation metrics
///
/// Sophisticated metrics for detailed fragmentation analysis.
#[derive(Debug, Default, Clone)]
pub struct AdvancedFragmentationMetrics {
    /// Shannon entropy of free block sizes
    pub free_block_entropy: f64,

    /// Fragmentation index (Fowler's index)
    pub fowler_index: f64,

    /// External fragmentation ratio
    pub external_fragmentation: f64,

    /// Internal fragmentation ratio
    pub internal_fragmentation: f64,

    /// Spatial fragmentation score
    pub spatial_fragmentation: f64,

    /// Temporal fragmentation patterns
    pub temporal_patterns: TemporalFragmentationPatterns,

    /// Fragmentation heat map
    pub heat_map: FragmentationHeatMap,

    /// Predictive metrics
    pub predictive_metrics: PredictiveFragmentationMetrics,
}

/// Temporal fragmentation patterns
#[derive(Debug, Default, Clone)]
pub struct TemporalFragmentationPatterns {
    /// Daily fragmentation cycle
    pub daily_cycle: Vec<f64>,

    /// Weekly fragmentation pattern
    pub weekly_pattern: Vec<f64>,

    /// Fragmentation trend (increasing/decreasing)
    pub trend: FragmentationTrend,

    /// Seasonal patterns
    pub seasonal_patterns: Vec<SeasonalPattern>,
}

/// Fragmentation trend analysis
#[derive(Debug, Clone)]
pub enum FragmentationTrend {
    Increasing { rate: f64 },
    Decreasing { rate: f64 },
    Stable,
    Cyclical { period: Duration },
}

impl Default for FragmentationTrend {
    fn default() -> Self {
        FragmentationTrend::Stable
    }
}

/// Seasonal fragmentation patterns
#[derive(Debug, Clone)]
pub struct SeasonalPattern {
    /// Pattern name
    pub name: String,

    /// Pattern period
    pub period: Duration,

    /// Pattern amplitude
    pub amplitude: f64,

    /// Pattern phase
    pub phase: Duration,
}

/// Fragmentation heat map
#[derive(Debug, Default, Clone)]
pub struct FragmentationHeatMap {
    /// Memory address ranges and their fragmentation levels
    pub address_ranges: Vec<(usize, usize, f64)>, // (start, end, fragmentation)

    /// Heat map resolution
    pub resolution: usize,

    /// Last update timestamp
    pub last_update: Option<Instant>,

    /// Heat map visualization data
    pub visualization_data: Vec<u8>,
}

/// Predictive fragmentation metrics
#[derive(Debug, Default, Clone)]
pub struct PredictiveFragmentationMetrics {
    /// Fragmentation velocity (rate of change)
    pub fragmentation_velocity: f64,

    /// Fragmentation acceleration
    pub fragmentation_acceleration: f64,

    /// Predicted peak fragmentation time
    pub predicted_peak_time: Option<Instant>,

    /// Predicted peak fragmentation level
    pub predicted_peak_level: f64,

    /// Risk assessment
    pub risk_assessment: FragmentationRisk,
}

/// Fragmentation risk levels
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord)]
pub enum FragmentationRisk {
    Low,
    Medium,
    High,
    Critical,
}

impl Default for FragmentationRisk {
    fn default() -> Self {
        FragmentationRisk::Low
    }
}

/// Fragmentation prediction model
///
/// Predictive model for forecasting fragmentation behavior.
#[derive(Debug)]
pub struct FragmentationPredictionModel {
    /// Historical fragmentation data
    history: VecDeque<FragmentationDataPoint>,

    /// Model parameters
    model_params: PredictionModelParams,

    /// Prediction accuracy tracking
    accuracy_tracker: PredictionAccuracyTracker,

    /// Model state
    model_state: ModelState,
}

/// Fragmentation data point for modeling
#[derive(Debug, Clone)]
pub struct FragmentationDataPoint {
    /// Timestamp
    pub timestamp: Instant,

    /// Fragmentation level
    pub fragmentation_level: f64,

    /// Contributing factors
    pub factors: FragmentationFactors,

    /// System state
    pub system_state: SystemState,
}

/// Fragmentation contributing factors
#[derive(Debug, Clone)]
pub struct FragmentationFactors {
    /// Allocation rate (allocations per second)
    pub allocation_rate: f64,

    /// Deallocation rate (deallocations per second)
    pub deallocation_rate: f64,

    /// Average allocation size
    pub avg_allocation_size: usize,

    /// Allocation size variance
    pub size_variance: f64,

    /// Memory pressure level
    pub memory_pressure: f64,
}

/// System state for prediction
#[derive(Debug, Clone)]
pub struct SystemState {
    /// Total memory usage
    pub total_memory_usage: usize,

    /// Available memory
    pub available_memory: usize,

    /// Active allocations count
    pub active_allocations: usize,

    /// System uptime
    pub uptime: Duration,
}

/// Prediction model parameters
#[derive(Debug, Clone)]
struct PredictionModelParams {
    /// History window size
    history_window: usize,

    /// Prediction horizon
    prediction_horizon: Duration,

    /// Model learning rate
    learning_rate: f64,

    /// Smoothing factor
    smoothing_factor: f64,
}

/// Prediction accuracy tracker
#[derive(Debug)]
struct PredictionAccuracyTracker {
    /// Correct predictions
    correct_predictions: u64,

    /// Total predictions
    total_predictions: u64,

    /// Mean absolute error
    mean_absolute_error: f64,

    /// Root mean square error
    root_mean_square_error: f64,
}

/// Prediction model state
#[derive(Debug)]
struct ModelState {
    /// Current model weights
    weights: Vec<f64>,

    /// Model bias
    bias: f64,

    /// Last prediction timestamp
    last_prediction: Option<Instant>,

    /// Model confidence
    confidence: f64,
}

/// Fragmentation configuration
#[derive(Debug, Clone)]
pub struct FragmentationConfig {
    /// Fragmentation threshold for alerts (0.0 to 1.0)
    pub alert_threshold: f64,

    /// Critical fragmentation threshold
    pub critical_threshold: f64,

    /// Compaction trigger threshold
    pub compaction_threshold: f64,

    /// Enable automatic compaction
    pub auto_compaction: bool,

    /// Maximum compaction frequency (per hour)
    pub max_compaction_frequency: u32,

    /// Enable predictive analysis
    pub enable_prediction: bool,

    /// Metrics collection interval
    pub metrics_interval: Duration,

    /// History retention period
    pub history_retention: Duration,
}

impl FragmentationTracker {
    /// Create a new fragmentation tracker
    pub fn new(config: FragmentationConfig) -> Self {
        Self {
            free_blocks: HashMap::new(),
            fragmentation_scores: HashMap::new(),
            largest_free_block: HashMap::new(),
            fragmentation_events: Vec::new(),
            compaction_stats: CompactionStats::default(),
            config,
            advanced_metrics: Arc::new(Mutex::new(AdvancedFragmentationMetrics::default())),
            prediction_model: Arc::new(Mutex::new(FragmentationPredictionModel::new())),
        }
    }

    /// Update free block information for a device
    pub fn update_free_blocks(&mut self, device: Device, free_blocks: BTreeMap<usize, usize>) {
        // Calculate fragmentation score
        let fragmentation_score = self.calculate_fragmentation_score(&free_blocks);

        // Update largest free block
        let largest_block = free_blocks.keys().last().copied().unwrap_or(0);

        self.free_blocks.insert(device.clone(), free_blocks);
        self.fragmentation_scores
            .insert(device.clone(), fragmentation_score);
        self.largest_free_block
            .insert(device.clone(), largest_block);

        // Check for fragmentation events
        if fragmentation_score > self.config.alert_threshold {
            self.record_fragmentation_event(device.clone(), fragmentation_score);
        }

        // Update advanced metrics
        self.update_advanced_metrics(device.clone(), fragmentation_score);

        // Trigger automatic compaction if needed
        if self.config.auto_compaction && fragmentation_score > self.config.compaction_threshold {
            self.trigger_automatic_compaction(device.clone());
        }
    }

    /// Calculate fragmentation score for free blocks
    fn calculate_fragmentation_score(&self, free_blocks: &BTreeMap<usize, usize>) -> f64 {
        if free_blocks.is_empty() {
            return 0.0;
        }

        // Calculate total free memory
        let total_free: usize = free_blocks.iter().map(|(&size, &count)| size * count).sum();

        if total_free == 0 {
            return 0.0;
        }

        // Calculate largest free block
        let largest_block = free_blocks.keys().last().copied().unwrap_or(0);

        // Calculate number of free blocks
        let total_blocks: usize = free_blocks.values().sum();

        // Fragmentation score combining multiple factors
        let size_fragmentation = 1.0 - (largest_block as f64 / total_free as f64);
        let count_fragmentation = (total_blocks as f64).log2() / 20.0; // Normalized log scale

        // Weighted combination
        ((size_fragmentation * 0.7) + (count_fragmentation * 0.3)).min(1.0)
    }

    /// Record a fragmentation event
    fn record_fragmentation_event(&mut self, device: Device, current_fragmentation: f64) {
        let previous_fragmentation = self
            .fragmentation_scores
            .get(&device)
            .copied()
            .unwrap_or(0.0);

        // Determine the cause of fragmentation
        let cause = self.determine_fragmentation_cause(device.clone(), current_fragmentation);

        // Assess impact
        let impact = self.assess_fragmentation_impact(device.clone(), current_fragmentation);

        // Create context
        let context = self.create_fragmentation_context(device.clone());

        let event = FragmentationEvent {
            timestamp: Instant::now(),
            device: device.clone(),
            fragmentation_before: previous_fragmentation,
            fragmentation_after: current_fragmentation,
            cause,
            recovery_action: None,
            impact,
            context,
        };

        self.fragmentation_events.push(event);

        // Update prediction model
        self.update_prediction_model(device, current_fragmentation);
    }

    /// Determine the cause of fragmentation
    fn determine_fragmentation_cause(
        &self,
        _device: Device,
        _fragmentation_level: f64,
    ) -> FragmentationCause {
        // Simplified cause determination
        // In practice, this would analyze allocation patterns, sizes, etc.
        FragmentationCause::MixedAllocationSizes {
            size_variance: 0.8,
            allocation_count: 1000,
        }
    }

    /// Assess fragmentation impact
    fn assess_fragmentation_impact(
        &self,
        _device: Device,
        fragmentation_level: f64,
    ) -> FragmentationImpact {
        let memory_efficiency = 1.0 - fragmentation_level;
        let allocation_slowdown = 1.0 + (fragmentation_level * 2.0);

        let cache_impact = CacheImpact {
            l1_hit_rate_reduction: fragmentation_level * 0.1,
            l2_hit_rate_reduction: fragmentation_level * 0.15,
            tlb_miss_increase: fragmentation_level * 0.2,
            cache_line_efficiency: 1.0 - (fragmentation_level * 0.3),
        };

        let bandwidth_impact = fragmentation_level * 0.25;
        let performance_score = memory_efficiency * 0.8;

        let future_impact = FutureImpactPrediction {
            one_hour_prediction: (fragmentation_level * 1.1).min(1.0),
            one_day_prediction: (fragmentation_level * 1.3).min(1.0),
            confidence: 0.7,
            action_timeline: if fragmentation_level > 0.8 {
                ActionTimeline::Immediate
            } else if fragmentation_level > 0.6 {
                ActionTimeline::Within1Hour
            } else {
                ActionTimeline::Within1Day
            },
        };

        FragmentationImpact {
            memory_efficiency,
            allocation_slowdown,
            cache_impact,
            bandwidth_impact,
            performance_score,
            future_impact,
        }
    }

    /// Create fragmentation context
    fn create_fragmentation_context(&self, device: Device) -> FragmentationContext {
        // Calculate memory pressure from fragmentation score
        let memory_pressure = self
            .fragmentation_scores
            .get(&device)
            .copied()
            .unwrap_or(0.5);

        // Estimate concurrent allocations from free blocks count
        let concurrent_allocations = self
            .free_blocks
            .get(&device)
            .map(|blocks| blocks.len())
            .unwrap_or(10);

        // Determine workload type from recent fragmentation events
        let workload_type = if self.fragmentation_events.len() > 10 {
            WorkloadType::StreamingWorkload
        } else if self.fragmentation_events.len() > 5 {
            WorkloadType::BatchProcessing
        } else {
            WorkloadType::InteractiveWorkload
        };

        // Analyze allocation pattern from free blocks distribution
        let allocation_pattern = self
            .free_blocks
            .get(&device)
            .map(|blocks| {
                if blocks.len() < 5 {
                    AllocationPattern::Sequential
                } else {
                    AllocationPattern::Random
                }
            })
            .unwrap_or(AllocationPattern::Random);

        // Get system load (use reasonable defaults if not available)
        let cpu_count = num_cpus::get();
        let system_load = SystemLoad {
            cpu_utilization: memory_pressure * 100.0, // Correlate with memory pressure
            memory_utilization: memory_pressure * 100.0,
            io_pressure: (memory_pressure * 50.0).min(100.0),
            active_threads: cpu_count,
        };

        FragmentationContext {
            memory_pressure,
            concurrent_allocations,
            workload_type,
            allocation_pattern,
            system_load,
        }
    }

    /// Update advanced metrics
    fn update_advanced_metrics(&self, device: Device, fragmentation_score: f64) {
        let mut metrics = self.advanced_metrics.lock();

        // Calculate Shannon entropy
        if let Some(free_blocks) = self.free_blocks.get(&device) {
            metrics.free_block_entropy = self.calculate_shannon_entropy(free_blocks);
        }

        // Update Fowler's index
        metrics.fowler_index = fragmentation_score;

        // Update external fragmentation
        metrics.external_fragmentation = fragmentation_score * 0.7;

        // Update internal fragmentation (estimate)
        metrics.internal_fragmentation = fragmentation_score * 0.3;

        // Update spatial fragmentation
        metrics.spatial_fragmentation = fragmentation_score;

        // Calculate fragmentation velocity from recent events
        let fragmentation_velocity = self.calculate_fragmentation_velocity(&device);
        metrics.predictive_metrics.fragmentation_velocity = fragmentation_velocity;

        // Update risk assessment based on score and velocity
        metrics.predictive_metrics.risk_assessment =
            if fragmentation_score > 0.8 || fragmentation_velocity > 0.1 {
                FragmentationRisk::Critical
            } else if fragmentation_score > 0.6 || fragmentation_velocity > 0.05 {
                FragmentationRisk::High
            } else if fragmentation_score > 0.4 || fragmentation_velocity > 0.02 {
                FragmentationRisk::Medium
            } else {
                FragmentationRisk::Low
            };
    }

    /// Calculate Shannon entropy of free block sizes
    fn calculate_shannon_entropy(&self, free_blocks: &BTreeMap<usize, usize>) -> f64 {
        let total_blocks: usize = free_blocks.values().sum();
        if total_blocks == 0 {
            return 0.0;
        }

        let mut entropy = 0.0;
        for &count in free_blocks.values() {
            if count > 0 {
                let p = count as f64 / total_blocks as f64;
                entropy -= p * p.log2();
            }
        }

        entropy
    }

    /// Calculate fragmentation velocity from recent events
    fn calculate_fragmentation_velocity(&self, device: &Device) -> f64 {
        // Get recent events for this device (last 10 events)
        let recent_events: Vec<_> = self
            .fragmentation_events
            .iter()
            .filter(|event| event.device == *device)
            .rev()
            .take(10)
            .collect();

        if recent_events.len() < 2 {
            return 0.0; // Not enough data to calculate velocity
        }

        // Calculate average rate of change
        let mut total_change = 0.0;
        let mut total_time = Duration::from_secs(0);

        for i in 0..recent_events.len() - 1 {
            let current = recent_events[i];
            let previous = recent_events[i + 1];

            let fragmentation_change =
                (current.fragmentation_after - previous.fragmentation_after).abs();
            let time_diff = current
                .timestamp
                .duration_since(previous.timestamp)
                .as_secs_f64();

            if time_diff > 0.0 {
                total_change += fragmentation_change;
                total_time += Duration::from_secs_f64(time_diff);
            }
        }

        if total_time.as_secs_f64() > 0.0 {
            total_change / total_time.as_secs_f64()
        } else {
            0.0
        }
    }

    /// Trigger automatic compaction
    fn trigger_automatic_compaction(&mut self, device: Device) {
        let start_time = Instant::now();

        // Perform actual memory compaction based on current allocation state
        let (blocks_moved, memory_recovered) = self.perform_smart_compaction(&device);

        let compaction_time = start_time.elapsed();

        // Update compaction statistics with real results
        self.compaction_stats.total_compactions += 1;
        self.compaction_stats.total_compaction_time += compaction_time;
        self.compaction_stats.total_memory_recovered += memory_recovered;
        self.compaction_stats.last_compaction = Some(start_time);

        // Log the compaction results
        #[cfg(feature = "std")]
        if memory_recovered > 0 {
            println!(
                "Compaction completed: moved {} blocks, recovered {}KB",
                blocks_moved,
                memory_recovered / 1024,
            );
        }
    }

    /// Perform intelligent memory compaction based on allocation patterns
    fn perform_smart_compaction(&mut self, device: &Device) -> (usize, usize) {
        let mut blocks_moved = 0;
        let mut memory_recovered = 0;
        let mut largest_free_block_before = 0;
        let mut _total_free_memory_before = 0;

        // Analyze current allocation state
        if let Some(device_free_blocks) = self.free_blocks.get(device) {
            for (&size, &count) in device_free_blocks {
                _total_free_memory_before += size * count;
                largest_free_block_before = largest_free_block_before.max(size);
            }
        }

        // Identify fragmentation hotspots - small free blocks
        let mut fragmented_ranges = Vec::new();

        // Work with free blocks to identify fragmentation
        if let Some(device_free_blocks) = self.free_blocks.get(device) {
            for (&size, &count) in device_free_blocks {
                if size < 64 * 1024 && count > 1 {
                    // Small blocks < 64KB are fragmentation
                    fragmented_ranges.push((size, count));
                }
            }
        }

        // Perform compaction by merging small free blocks
        let mut compacted_blocks = Vec::new();
        for (size, count) in fragmented_ranges {
            // Simulate consolidating small blocks into larger ones
            if count > 1 {
                let consolidated_size = size * count;
                blocks_moved += count;
                memory_recovered += consolidated_size - size; // Recovery is the extra space gained
                compacted_blocks.push((size, count));
            }
        }

        // Update allocation table to reflect compaction
        self.apply_compaction_moves(&compacted_blocks);

        (blocks_moved, memory_recovered)
    }

    /// Check if an allocation can be safely moved during compaction
    fn can_move_allocation(&self, _address: usize, size: usize, device: &Device) -> bool {
        use torsh_core::device::DeviceType;

        match device.device_type() {
            DeviceType::Cpu => {
                // CPU memory can generally be moved if not pinned
                size >= 4096 // Only move allocations >= 4KB to avoid overhead
            }
            DeviceType::Cuda(_) => {
                // CUDA memory movement requires device synchronization
                size >= 1024 * 1024 // Only move large allocations >= 1MB
            }
            DeviceType::Metal(_) => {
                // Metal buffers may have texture dependencies
                size >= 512 * 1024 // Move allocations >= 512KB
            }
            _ => {
                // Conservative approach for other device types
                size >= 2 * 1024 * 1024 // Only move very large allocations >= 2MB
            }
        }
    }

    /// Apply the compaction moves to the allocation tracking
    fn apply_compaction_moves(&mut self, _compacted_blocks: &[(usize, usize)]) {
        // Simplified compaction tracking - just update basic statistics
        // In a real implementation, this would update internal data structures
    }

    /// Find the largest contiguous free block across all devices
    fn find_largest_free_block(&self) -> usize {
        self.largest_free_block.values().max().copied().unwrap_or(0)
    }

    /// Update compaction statistics averages
    fn update_compaction_averages(&mut self) {
        if self.compaction_stats.total_compactions > 0 {
            self.compaction_stats.average_compaction_time =
                self.compaction_stats.total_compaction_time
                    / self.compaction_stats.total_compactions as u32;

            self.compaction_stats.avg_memory_recovered =
                self.compaction_stats.total_memory_recovered
                    / self.compaction_stats.total_compactions as usize;

            // Calculate efficiency (memory recovered per millisecond)
            let total_ms = self.compaction_stats.total_compaction_time.as_millis() as f64;
            if total_ms > 0.0 {
                self.compaction_stats.compaction_efficiency =
                    self.compaction_stats.total_memory_recovered as f64 / total_ms;
            }
        }
    }

    /// Update prediction model
    fn update_prediction_model(&self, _device: Device, fragmentation_level: f64) {
        let mut model = self.prediction_model.lock();

        let data_point = FragmentationDataPoint {
            timestamp: Instant::now(),
            fragmentation_level,
            factors: FragmentationFactors {
                allocation_rate: 100.0,  // Placeholder
                deallocation_rate: 90.0, // Placeholder
                avg_allocation_size: 1024,
                size_variance: 0.5,
                memory_pressure: 0.6,
            },
            system_state: SystemState {
                total_memory_usage: 1024 * 1024 * 1024, // 1GB
                available_memory: 256 * 1024 * 1024,    // 256MB
                active_allocations: 1000,
                uptime: Duration::from_secs(3600), // 1 hour
            },
        };

        model.add_data_point(data_point);
    }

    /// Calculate external fragmentation for a device
    fn calculate_external_fragmentation(&self, device: &Device) -> f64 {
        if let Some(device_free_blocks) = self.free_blocks.get(device) {
            if device_free_blocks.is_empty() {
                return 0.0;
            }

            let total_free_memory: usize = device_free_blocks
                .iter()
                .map(|(&size, &count)| size * count)
                .sum();

            let largest_free_block = device_free_blocks.keys().max().copied().unwrap_or(0);

            if total_free_memory == 0 {
                0.0
            } else {
                1.0 - (largest_free_block as f64 / total_free_memory as f64)
            }
        } else {
            0.0
        }
    }

    /// Get fragmentation score for a device
    pub fn get_fragmentation_score(&self, device: Device) -> Option<f64> {
        self.fragmentation_scores.get(&device).copied()
    }

    /// Get largest free block for a device
    pub fn get_largest_free_block(&self, device: Device) -> Option<usize> {
        self.largest_free_block.get(&device).copied()
    }

    /// Get recent fragmentation events
    pub fn get_recent_events(&self, since: Duration) -> Vec<&FragmentationEvent> {
        let cutoff = Instant::now() - since;
        self.fragmentation_events
            .iter()
            .filter(|event| event.timestamp > cutoff)
            .collect()
    }

    /// Get compaction statistics
    pub fn get_compaction_stats(&self) -> &CompactionStats {
        &self.compaction_stats
    }

    /// Get advanced metrics
    pub fn get_advanced_metrics(&self) -> AdvancedFragmentationMetrics {
        (*self.advanced_metrics.lock()).clone()
    }

    /// Predict future fragmentation
    pub fn predict_fragmentation(&self, device: Device, time_horizon: Duration) -> Option<f64> {
        let model = self.prediction_model.lock();
        model.predict_fragmentation(device, time_horizon)
    }

    /// Clean up old data
    pub fn cleanup_old_data(&mut self, max_age: Duration) {
        let cutoff = Instant::now() - max_age;

        // Remove old events
        self.fragmentation_events
            .retain(|event| event.timestamp > cutoff);

        // Clean up prediction model
        let mut model = self.prediction_model.lock();
        model.cleanup_old_data(cutoff);
    }
}

impl FragmentationPredictionModel {
    /// Create a new prediction model
    fn new() -> Self {
        Self {
            history: VecDeque::new(),
            model_params: PredictionModelParams {
                history_window: 1000,
                prediction_horizon: Duration::from_secs(24 * 60 * 60),
                learning_rate: 0.01,
                smoothing_factor: 0.1,
            },
            accuracy_tracker: PredictionAccuracyTracker {
                correct_predictions: 0,
                total_predictions: 0,
                mean_absolute_error: 0.0,
                root_mean_square_error: 0.0,
            },
            model_state: ModelState {
                weights: vec![0.0; 10],
                bias: 0.0,
                last_prediction: None,
                confidence: 0.5,
            },
        }
    }

    /// Add a data point to the model
    fn add_data_point(&mut self, data_point: FragmentationDataPoint) {
        self.history.push_back(data_point);

        // Maintain window size
        while self.history.len() > self.model_params.history_window {
            self.history.pop_front();
        }

        // Update model (simplified)
        self.update_model();
    }

    /// Update model parameters
    fn update_model(&mut self) {
        // Simplified model update
        // In practice, this would implement more sophisticated ML algorithms
        if self.history.len() > 10 {
            self.model_state.confidence = 0.8;
        }
    }

    /// Predict fragmentation for a device
    fn predict_fragmentation(&self, _device: Device, _time_horizon: Duration) -> Option<f64> {
        if self.history.len() < 5 {
            return None;
        }

        // Simplified prediction using trend analysis
        let recent_points: Vec<_> = self.history.iter().rev().take(5).collect();
        let avg_fragmentation = recent_points
            .iter()
            .map(|p| p.fragmentation_level)
            .sum::<f64>()
            / recent_points.len() as f64;

        Some((avg_fragmentation * 1.1).min(1.0))
    }

    /// Clean up old data
    fn cleanup_old_data(&mut self, cutoff: Instant) {
        self.history
            .retain(|data_point| data_point.timestamp > cutoff);
    }
}

impl Default for FragmentationConfig {
    fn default() -> Self {
        Self {
            alert_threshold: 0.3,
            critical_threshold: 0.7,
            compaction_threshold: 0.5,
            auto_compaction: true,
            max_compaction_frequency: 10,
            enable_prediction: true,
            metrics_interval: Duration::from_secs(60),
            history_retention: Duration::from_secs(7 * 24 * 60 * 60),
        }
    }
}

impl std::fmt::Display for FragmentationCause {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            FragmentationCause::MixedAllocationSizes {
                size_variance,
                allocation_count,
            } => {
                write!(
                    f,
                    "Mixed allocation sizes (variance: {:.2}, count: {})",
                    size_variance, allocation_count
                )
            }
            FragmentationCause::FrequentAllocDealloc {
                alloc_frequency,
                dealloc_frequency,
            } => {
                write!(
                    f,
                    "Frequent alloc/dealloc (alloc: {:.1}/s, dealloc: {:.1}/s)",
                    alloc_frequency, dealloc_frequency
                )
            }
            FragmentationCause::LongLivedAllocations {
                allocation_age,
                blocking_compaction,
            } => {
                write!(
                    f,
                    "Long-lived allocations (age: {:?}, blocking: {})",
                    allocation_age, blocking_compaction
                )
            }
            FragmentationCause::MisalignedAllocations {
                alignment_requirement,
                waste_percentage,
            } => {
                write!(
                    f,
                    "Misaligned allocations (align: {} bytes, waste: {:.1}%)",
                    alignment_requirement, waste_percentage
                )
            }
            FragmentationCause::PoolOverflow {
                pool_name,
                overflow_amount,
            } => {
                write!(
                    f,
                    "Pool overflow ({}: {} bytes)",
                    pool_name, overflow_amount
                )
            }
            FragmentationCause::TemporalPatterns {
                pattern_type,
                periodicity,
            } => {
                write!(
                    f,
                    "Temporal patterns ({}, period: {:?})",
                    pattern_type, periodicity
                )
            }
            FragmentationCause::SizeClustering {
                cluster_sizes,
                fragmentation_factor,
            } => {
                write!(
                    f,
                    "Size clustering ({:?}, factor: {:.2})",
                    cluster_sizes, fragmentation_factor
                )
            }
        }
    }
}

impl std::fmt::Display for FragmentationRisk {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            FragmentationRisk::Low => write!(f, "Low"),
            FragmentationRisk::Medium => write!(f, "Medium"),
            FragmentationRisk::High => write!(f, "High"),
            FragmentationRisk::Critical => write!(f, "Critical"),
        }
    }
}

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

    #[test]
    fn test_fragmentation_tracker_creation() {
        let config = FragmentationConfig::default();
        let tracker = FragmentationTracker::new(config);

        assert!(tracker.fragmentation_scores.is_empty());
        assert_eq!(tracker.compaction_stats.total_compactions, 0);
    }

    #[test]
    fn test_fragmentation_score_calculation() {
        let config = FragmentationConfig::default();
        let tracker = FragmentationTracker::new(config);

        let mut free_blocks = BTreeMap::new();
        free_blocks.insert(1024, 5); // 5 blocks of 1KB
        free_blocks.insert(2048, 3); // 3 blocks of 2KB
        free_blocks.insert(4096, 1); // 1 block of 4KB

        let score = tracker.calculate_fragmentation_score(&free_blocks);
        assert!(score > 0.0 && score < 1.0);
    }

    #[test]
    fn test_fragmentation_event_recording() {
        let config = FragmentationConfig::default();
        let mut tracker = FragmentationTracker::new(config);

        let device = Device::cpu().expect("Device should succeed");
        let mut free_blocks = BTreeMap::new();
        free_blocks.insert(1024, 100); // High fragmentation scenario

        tracker.update_free_blocks(device, free_blocks);

        // Should record an event if fragmentation is above threshold
        assert!(!tracker.fragmentation_events.is_empty());
    }

    #[test]
    fn test_shannon_entropy_calculation() {
        let config = FragmentationConfig::default();
        let tracker = FragmentationTracker::new(config);

        let mut free_blocks = BTreeMap::new();
        free_blocks.insert(1024, 1);
        free_blocks.insert(2048, 1);
        free_blocks.insert(4096, 1);

        let entropy = tracker.calculate_shannon_entropy(&free_blocks);
        assert!(entropy > 0.0);
    }

    #[test]
    fn test_compaction_stats_update() {
        let config = FragmentationConfig::default();
        let mut tracker = FragmentationTracker::new(config);

        // Simulate compaction
        tracker.compaction_stats.total_compactions = 1;
        tracker.compaction_stats.total_compaction_time = Duration::from_millis(100);
        tracker.compaction_stats.total_memory_recovered = 1024;

        tracker.update_compaction_averages();

        assert_eq!(
            tracker.compaction_stats.average_compaction_time,
            Duration::from_millis(100)
        );
        assert_eq!(tracker.compaction_stats.avg_memory_recovered, 1024);
        assert!(tracker.compaction_stats.compaction_efficiency > 0.0);
    }

    #[test]
    fn test_fragmentation_impact_assessment() {
        let config = FragmentationConfig::default();
        let tracker = FragmentationTracker::new(config);

        let impact = tracker.assess_fragmentation_impact(
            Device::cpu().expect("fragmentation impact assessment should succeed"),
            0.7,
        );

        assert!((impact.memory_efficiency - 0.3).abs() < 1e-10);
        assert!((impact.allocation_slowdown - 2.4).abs() < 1e-10);
        assert!(impact.performance_score < 1.0);
        assert!(matches!(
            impact.future_impact.action_timeline,
            ActionTimeline::Within1Hour
        ));
    }

    #[test]
    fn test_prediction_model() {
        let mut model = FragmentationPredictionModel::new();

        let data_point = FragmentationDataPoint {
            timestamp: Instant::now(),
            fragmentation_level: 0.5,
            factors: FragmentationFactors {
                allocation_rate: 100.0,
                deallocation_rate: 90.0,
                avg_allocation_size: 1024,
                size_variance: 0.3,
                memory_pressure: 0.4,
            },
            system_state: SystemState {
                total_memory_usage: 1024 * 1024 * 1024,
                available_memory: 256 * 1024 * 1024,
                active_allocations: 1000,
                uptime: Duration::from_secs(3600),
            },
        };

        model.add_data_point(data_point);
        assert_eq!(model.history.len(), 1);

        // Test prediction (should return None with insufficient data)
        let prediction = model.predict_fragmentation(
            Device::cpu().expect("fragmentation prediction should succeed"),
            Duration::from_secs(1 * 60 * 60),
        );
        assert!(prediction.is_none());
    }

    #[test]
    fn test_fragmentation_risk_ordering() {
        assert!(FragmentationRisk::Low < FragmentationRisk::Medium);
        assert!(FragmentationRisk::Medium < FragmentationRisk::High);
        assert!(FragmentationRisk::High < FragmentationRisk::Critical);
    }
}