scirs2-core 0.4.2

Core utilities and common functionality for SciRS2 (scirs2-core)
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
//! Memory metrics snapshot system
//!
//! This module provides functionality for capturing, saving, and comparing
//! memory usage snapshots to track changes and identify potential leaks.

use std::collections::HashMap;
#[cfg(feature = "memory_metrics")]
use std::fs::File;
use std::io;
#[cfg(feature = "memory_metrics")]
use std::io::{Read, Write};
use std::path::Path;
use std::time::{Duration, SystemTime, UNIX_EPOCH};

#[cfg(feature = "memory_metrics")]
#[cfg(feature = "serialization")]
use serde::{Deserialize, Serialize};
#[cfg(feature = "memory_metrics")]
use serde_json::Value as JsonValue;

use crate::memory::metrics::collector::MemoryReport;
use crate::memory::metrics::generate_memory_report;

/// A snapshot of memory usage at a point in time
#[derive(Debug, Clone)]
#[cfg_attr(feature = "memory_metrics", derive(Serialize, Deserialize))]
pub struct MemorySnapshot {
    /// Unique identifier for the snapshot
    pub id: String,

    /// Timestamp when the snapshot was taken
    pub timestamp: u64,

    /// Description of the snapshot
    pub description: String,

    /// Memory report at the time of the snapshot
    pub report: SnapshotReport,

    /// Additional metadata
    pub metadata: HashMap<String, String>,
}

/// A simplified version of MemoryReport for snapshots
#[derive(Debug, Clone)]
#[cfg_attr(feature = "memory_metrics", derive(Serialize, Deserialize))]
pub struct SnapshotReport {
    /// Total current memory usage across all components
    pub total_current_usage: usize,

    /// Total peak memory usage across all components
    pub total_peak_usage: usize,

    /// Total number of allocations across all components
    pub total_allocation_count: usize,

    /// Total bytes allocated across all components
    pub total_allocated_bytes: usize,

    /// Component-specific statistics
    pub component_stats: HashMap<String, SnapshotComponentStats>,

    /// Duration since tracking started
    pub duration_ms: u64,
}

/// A simplified version of ComponentMemoryStats for snapshots
#[derive(Debug, Clone)]
#[cfg_attr(feature = "memory_metrics", derive(Serialize, Deserialize))]
pub struct SnapshotComponentStats {
    /// Current memory usage
    pub current_usage: usize,

    /// Peak memory usage
    pub peak_usage: usize,

    /// Number of allocations
    pub allocation_count: usize,

    /// Total bytes allocated
    pub total_allocated: usize,

    /// Average allocation size
    pub avg_allocation_size: f64,
}

impl MemorySnapshot {
    /// Create a new memory snapshot
    pub fn new(id: impl Into<String>, description: impl Into<String>) -> Self {
        // Get current report
        let report = generate_memory_report();

        // Get current time as unix timestamp
        let timestamp = SystemTime::now()
            .duration_since(UNIX_EPOCH)
            .unwrap_or_else(|_| Duration::from_secs(0))
            .as_secs();

        Self {
            id: id.into(),
            timestamp,
            description: description.into(),
            report: SnapshotReport::from(report),
            metadata: HashMap::new(),
        }
    }

    /// Create a new memory snapshot from a specific memory report
    pub fn from_report(
        id: impl Into<String>,
        description: impl Into<String>,
        report: MemoryReport,
    ) -> Self {
        // Get current time as unix timestamp
        let timestamp = SystemTime::now()
            .duration_since(UNIX_EPOCH)
            .unwrap_or_else(|_| Duration::from_secs(0))
            .as_secs();

        Self {
            id: id.into(),
            timestamp,
            description: description.into(),
            report: SnapshotReport::from(report),
            metadata: HashMap::new(),
        }
    }

    /// Add metadata to the snapshot
    pub fn with_metadata(mut self, key: impl Into<String>, value: impl Into<String>) -> Self {
        self.metadata.insert(key.into(), value.into());
        self
    }

    /// Save the snapshot to a file
    #[cfg(feature = "memory_metrics")]
    pub fn save_to_file(&self, path: impl AsRef<Path>) -> io::Result<()> {
        let json = serde_json::to_string_pretty(self)?;
        let mut file = File::create(path)?;
        file.write_all(json.as_bytes())?;
        Ok(())
    }

    /// Save the snapshot to a file - stub when memory_metrics is disabled
    #[cfg(not(feature = "memory_metrics"))]
    pub fn save_to_file(&self, path: impl AsRef<Path>) -> io::Result<()> {
        Ok(())
    }

    /// Load a snapshot from a file
    #[cfg(feature = "memory_metrics")]
    pub fn load_from_file(path: impl AsRef<Path>) -> io::Result<Self> {
        let mut file = File::open(path)?;
        let mut contents = String::new();
        file.read_to_string(&mut contents)?;
        let snapshot = serde_json::from_str(&contents)?;
        Ok(snapshot)
    }

    /// Load a snapshot from a file - stub when memory_metrics is disabled
    #[cfg(not(feature = "memory_metrics"))]
    pub fn load_from_file(path: impl AsRef<Path>) -> io::Result<Self> {
        Ok(Self::new("stub_id", "stub_description"))
    }

    /// Compare this snapshot with another snapshot
    pub fn compare(&self, other: &MemorySnapshot) -> SnapshotDiff {
        SnapshotDiff::new(self, other)
    }

    /// Export the snapshot as JSON
    #[cfg(feature = "memory_metrics")]
    pub fn to_json(&self) -> JsonValue {
        serde_json::to_value(self).unwrap_or(JsonValue::Null)
    }

    /// Export the snapshot as JSON - stub when memory_metrics is disabled
    #[cfg(not(feature = "memory_metrics"))]
    pub fn to_json(&self) -> String {
        "{}".to_string()
    }
}

/// Conversion from MemoryReport to SnapshotReport
impl From<MemoryReport> for SnapshotReport {
    fn from(report: MemoryReport) -> Self {
        let mut component_stats = HashMap::new();

        for (name, stats) in report.component_stats {
            component_stats.insert(
                name,
                SnapshotComponentStats {
                    current_usage: stats.current_usage,
                    peak_usage: stats.peak_usage,
                    allocation_count: stats.allocation_count,
                    total_allocated: stats.total_allocated,
                    avg_allocation_size: stats.avg_allocation_size,
                },
            );
        }

        Self {
            total_current_usage: report.total_current_usage,
            total_peak_usage: report.total_peak_usage,
            total_allocation_count: report.total_allocation_count,
            total_allocated_bytes: report.total_allocated_bytes,
            component_stats,
            duration_ms: report.duration.as_millis() as u64,
        }
    }
}

/// The difference between two memory snapshots
#[derive(Debug, Clone)]
#[cfg_attr(feature = "memory_metrics", derive(Serialize, Deserialize))]
pub struct SnapshotDiff {
    /// First snapshot ID
    pub first_id: String,

    /// Second snapshot ID
    pub second_id: String,

    /// Time elapsed between snapshots in milliseconds
    pub elapsed_ms: u64,

    /// Change in total current usage
    pub current_usage_delta: isize,

    /// Change in total peak usage
    pub peak_usage_delta: isize,

    /// Change in total allocation count
    pub allocation_count_delta: isize,

    /// New components that weren't in the first snapshot
    pub new_components: Vec<String>,

    /// Components that were in the first snapshot but not in the second
    pub removed_components: Vec<String>,

    /// Changes in component statistics
    pub component_changes: HashMap<String, ComponentStatsDiff>,
}

/// The difference in memory statistics for a component
#[derive(Debug, Clone)]
#[cfg_attr(feature = "memory_metrics", derive(Serialize, Deserialize))]
pub struct ComponentStatsDiff {
    /// Change in current usage
    pub current_usage_delta: isize,

    /// Change in peak usage
    pub peak_usage_delta: isize,

    /// Change in allocation count
    pub allocation_count_delta: isize,

    /// Change in total allocated bytes
    pub total_allocated_delta: isize,

    /// Potential memory leak (positive current usage delta)
    pub potential_leak: bool,
}

impl SnapshotDiff {
    /// Create a new snapshot diff by comparing two snapshots
    pub fn new(first: &MemorySnapshot, second: &MemorySnapshot) -> Self {
        let elapsed_ms = second.timestamp.saturating_sub(first.timestamp) * 1000;

        let current_usage_delta =
            second.report.total_current_usage as isize - first.report.total_current_usage as isize;
        let peak_usage_delta =
            second.report.total_peak_usage as isize - first.report.total_peak_usage as isize;
        let allocation_count_delta = second.report.total_allocation_count as isize
            - first.report.total_allocation_count as isize;

        // Find new and removed components
        let mut new_components = Vec::new();
        let mut removed_components = Vec::new();

        for component in second.report.component_stats.keys() {
            if !first.report.component_stats.contains_key(component) {
                new_components.push(component.clone());
            }
        }

        for component in first.report.component_stats.keys() {
            if !second.report.component_stats.contains_key(component) {
                removed_components.push(component.clone());
            }
        }

        // Calculate component-specific changes
        let mut component_changes = HashMap::new();

        for (component, second_stats) in &second.report.component_stats {
            if let Some(first_stats) = first.report.component_stats.get(component) {
                let current_usage_delta =
                    second_stats.current_usage as isize - first_stats.current_usage as isize;
                let peak_usage_delta =
                    second_stats.peak_usage as isize - first_stats.peak_usage as isize;
                let allocation_count_delta =
                    second_stats.allocation_count as isize - first_stats.allocation_count as isize;
                let total_allocated_delta =
                    second_stats.total_allocated as isize - first_stats.total_allocated as isize;

                // Detect potential leaks (positive current usage regardless of allocations)
                // In our test environment, we might have allocations but still detect leaks
                let potential_leak = current_usage_delta > 0;

                component_changes.insert(
                    component.clone(),
                    ComponentStatsDiff {
                        current_usage_delta,
                        peak_usage_delta,
                        allocation_count_delta,
                        total_allocated_delta,
                        potential_leak,
                    },
                );
            }
        }

        Self {
            first_id: first.id.clone(),
            second_id: second.id.clone(),
            elapsed_ms,
            current_usage_delta,
            peak_usage_delta,
            allocation_count_delta,
            new_components,
            removed_components,
            component_changes,
        }
    }

    /// Format the diff as a string
    pub fn format(&self) -> String {
        use crate::memory::metrics::format_bytes;

        let mut output = String::new();

        output.push_str(&format!(
            "Memory Snapshot Diff: {} -> {}\n",
            self.first_id, self.second_id
        ));
        output.push_str(&format!(
            "Time elapsed: {elapsed}ms\n\n",
            elapsed = self.elapsed_ms
        ));

        // Overall changes
        let current_delta_prefix = if self.current_usage_delta >= 0 {
            "+"
        } else {
            ""
        };
        let peak_delta_prefix = if self.peak_usage_delta >= 0 { "+" } else { "" };
        let alloc_delta_prefix = if self.allocation_count_delta >= 0 {
            "+"
        } else {
            ""
        };

        output.push_str(&format!(
            "Total Current Usage: {}{}B ({})\n",
            current_delta_prefix,
            self.current_usage_delta,
            format_bytes(self.current_usage_delta.unsigned_abs())
        ));

        output.push_str(&format!(
            "Total Peak Usage: {}{}B ({})\n",
            peak_delta_prefix,
            self.peak_usage_delta,
            format_bytes(self.peak_usage_delta.unsigned_abs())
        ));

        output.push_str(&format!(
            "Total Allocations: {}{}\n\n",
            alloc_delta_prefix, self.allocation_count_delta
        ));

        // New components
        if !self.new_components.is_empty() {
            output.push_str("New Components:\n");
            for component in &self.new_components {
                output.push_str(&format!("  + {component}\n"));
            }
            output.push('\n');
        }

        // Removed components
        if !self.removed_components.is_empty() {
            output.push_str("Removed Components:\n");
            for component in &self.removed_components {
                output.push_str(&format!("  - {component}\n"));
            }
            output.push('\n');
        }

        // Component changes
        if !self.component_changes.is_empty() {
            output.push_str("Component Changes:\n");

            // Sort components by current usage delta (descending)
            let mut components: Vec<_> = self.component_changes.iter().collect();
            components.sort_by_key(|b| std::cmp::Reverse(b.1.current_usage_delta));

            for (component, diff) in components {
                let current_prefix = if diff.current_usage_delta >= 0 {
                    "+"
                } else {
                    ""
                };
                let peak_prefix = if diff.peak_usage_delta >= 0 { "+" } else { "" };
                let alloc_prefix = if diff.allocation_count_delta >= 0 {
                    "+"
                } else {
                    ""
                };
                let total_prefix = if diff.total_allocated_delta >= 0 {
                    "+"
                } else {
                    ""
                };

                output.push_str(&format!("  {component}"));

                if diff.potential_leak {
                    output.push_str(" [POTENTIAL LEAK]");
                }

                output.push('\n');

                output.push_str(&format!(
                    "    Current: {}{}B ({})\n",
                    current_prefix,
                    diff.current_usage_delta,
                    format_bytes(diff.current_usage_delta.unsigned_abs())
                ));

                output.push_str(&format!(
                    "    Peak: {}{}B ({})\n",
                    peak_prefix,
                    diff.peak_usage_delta,
                    format_bytes(diff.peak_usage_delta.unsigned_abs())
                ));

                output.push_str(&format!(
                    "    Allocations: {}{}\n",
                    alloc_prefix, diff.allocation_count_delta
                ));

                output.push_str(&format!(
                    "    Total Allocated: {}{}B ({})\n",
                    total_prefix,
                    diff.total_allocated_delta,
                    format_bytes(diff.total_allocated_delta.unsigned_abs())
                ));
            }
        }

        output
    }

    /// Export the diff as JSON
    #[cfg(feature = "memory_metrics")]
    pub fn to_json(&self) -> JsonValue {
        serde_json::to_value(self).unwrap_or(JsonValue::Null)
    }

    /// Export the diff as JSON - stub when memory_metrics is disabled
    #[cfg(not(feature = "memory_metrics"))]
    pub fn to_json(&self) -> String {
        "{}".to_string()
    }

    /// Check if there are any potential memory leaks
    pub fn has_potential_leaks(&self) -> bool {
        self.component_changes
            .values()
            .any(|diff| diff.potential_leak)
    }

    /// Get a list of components with potential memory leaks
    pub fn get_potential_leak_components(&self) -> Vec<String> {
        self.component_changes
            .iter()
            .filter_map(|(component, diff)| {
                if diff.potential_leak {
                    Some(component.clone())
                } else {
                    None
                }
            })
            .collect()
    }

    /// Generate a visualization of the memory usage changes
    #[cfg(feature = "memory_visualization")]
    pub fn visualize(&self) -> String {
        use crate::memory::metrics::format_bytes;

        let mut visualization = String::new();
        visualization.push_str(&format!(
            "Memory Snapshot Visualization: {}{}\n\n",
            self.first_id, self.second_id
        ));

        // Create basic text-based visualization
        // Header
        visualization.push_str(
            "Component                |   Current Usage |     Peak Usage |   Allocations\n",
        );
        visualization.push_str(
            "-------------------------|----------------|----------------|----------------\n",
        );

        // Sort components by current usage delta (descending)
        let mut components: Vec<_> = self.component_changes.iter().collect();
        components.sort_by_key(|item| std::cmp::Reverse(item.1.current_usage_delta));

        // Table rows
        for (component, diff) in components {
            let component_name = if component.len() > 25 {
                format!("{}...", &component[0..22])
            } else {
                component.clone()
            };

            let current_prefix = if diff.current_usage_delta > 0 {
                "+"
            } else {
                ""
            };
            let peak_prefix = if diff.peak_usage_delta > 0 { "+" } else { "" };
            let alloc_prefix = if diff.allocation_count_delta > 0 {
                "+"
            } else {
                ""
            };
            let leak_flag = if diff.potential_leak { "⚠️ " } else { "" };

            visualization.push_str(&format!(
                "{}{:<23} | {:>14} | {:>14} | {:>14}\n",
                leak_flag,
                component_name,
                format!(
                    "{}{}",
                    current_prefix,
                    format_bytes(diff.current_usage_delta.unsigned_abs())
                ),
                format!(
                    "{}{}",
                    peak_prefix,
                    format_bytes(diff.peak_usage_delta.unsigned_abs())
                ),
                format!(
                    "{}{}",
                    alloc_prefix,
                    diff.allocation_count_delta.unsigned_abs()
                )
            ));
        }

        // Summary
        visualization.push_str("\nLegend:\n");
        visualization.push_str("⚠️ = Potential memory leak\n");

        visualization
    }
}

/// Memory snapshots manager
pub struct SnapshotManager {
    /// List of snapshots
    snapshots: Vec<MemorySnapshot>,
}

impl SnapshotManager {
    /// Create a new snapshot manager
    pub fn new() -> Self {
        Self {
            snapshots: Vec::new(),
        }
    }

    /// Take a new snapshot
    pub fn take_snapshot(
        &mut self,
        id: impl Into<String>,
        description: impl Into<String>,
    ) -> MemorySnapshot {
        let snapshot = MemorySnapshot::new(id, description);
        self.snapshots.push(snapshot.clone());
        snapshot
    }

    /// Get a snapshot by ID
    pub fn get_snapshot(&self, id: &str) -> Option<&MemorySnapshot> {
        self.snapshots.iter().find(|s| s.id == id)
    }

    /// Compare two snapshots
    pub fn compare_snapshots(&self, first_id: &str, secondid: &str) -> Option<SnapshotDiff> {
        let first = self.get_snapshot(first_id)?;
        let second = self.get_snapshot(secondid)?;
        Some(first.compare(second))
    }

    /// Save all snapshots to a directory
    pub fn save_all(&self, dir: impl AsRef<Path>) -> io::Result<()> {
        let dir = dir.as_ref();

        if !dir.exists() {
            std::fs::create_dir_all(dir)?;
        }

        for snapshot in &self.snapshots {
            let filename = format!("snapshot_{id}.json", id = snapshot.id);
            let path = dir.join(filename);
            snapshot.save_to_file(path)?;
        }

        Ok(())
    }

    /// Load all snapshots from a directory
    pub fn load_all(&mut self, dir: impl AsRef<Path>) -> io::Result<()> {
        let dir = dir.as_ref();

        if !dir.exists() {
            return Ok(());
        }

        for entry in std::fs::read_dir(dir)? {
            let entry = entry?;
            let path = entry.path();

            if path.extension().and_then(|s| s.to_str()) == Some("json") {
                if let Ok(snapshot) = MemorySnapshot::load_from_file(&path) {
                    self.snapshots.push(snapshot);
                }
            }
        }

        Ok(())
    }

    /// Get all snapshots
    pub fn get_snapshots(&self) -> &[MemorySnapshot] {
        &self.snapshots
    }

    /// Clear all snapshots
    pub fn clear(&mut self) {
        self.snapshots.clear();
    }
}

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

/// Global snapshot manager instance
static GLOBAL_SNAPSHOT_MANAGER: once_cell::sync::Lazy<std::sync::Mutex<SnapshotManager>> =
    once_cell::sync::Lazy::new(|| std::sync::Mutex::new(SnapshotManager::new()));

/// Get the global snapshot manager instance
#[allow(dead_code)]
pub fn global_snapshot_manager() -> &'static std::sync::Mutex<SnapshotManager> {
    &GLOBAL_SNAPSHOT_MANAGER
}

/// Take a snapshot using the global snapshot manager
#[allow(dead_code)]
pub fn take_snapshot(id: impl Into<String>, description: impl Into<String>) -> MemorySnapshot {
    let mut manager = match global_snapshot_manager().lock() {
        Ok(guard) => guard,
        Err(poisoned) => {
            // Still use the poisoned lock by recovering the guard
            poisoned.into_inner()
        }
    };
    let snapshot = manager.take_snapshot(id, description);
    snapshot.clone()
}

/// Compare two snapshots using the global snapshot manager
#[allow(dead_code)]
pub fn compare_snapshots(first_id: &str, secondid: &str) -> Option<SnapshotDiff> {
    let manager = match global_snapshot_manager().lock() {
        Ok(guard) => guard,
        Err(poisoned) => {
            // Still use the poisoned lock by recovering the guard
            poisoned.into_inner()
        }
    };
    manager.compare_snapshots(first_id, secondid)
}

/// Save all snapshots to a directory using the global snapshot manager
#[allow(dead_code)]
pub fn save_all_snapshots(dir: impl AsRef<Path>) -> io::Result<()> {
    let manager = match global_snapshot_manager().lock() {
        Ok(guard) => guard,
        Err(poisoned) => {
            // Still use the poisoned lock by recovering the guard
            poisoned.into_inner()
        }
    };
    manager.save_all(dir)
}

/// Load all snapshots from a directory using the global snapshot manager
#[allow(dead_code)]
pub fn load_all_snapshots(dir: impl AsRef<Path>) -> io::Result<()> {
    let mut manager = match global_snapshot_manager().lock() {
        Ok(guard) => guard,
        Err(poisoned) => {
            // Still use the poisoned lock by recovering the guard
            poisoned.into_inner()
        }
    };
    manager.load_all(dir)
}

/// Clear all snapshots using the global snapshot manager
#[allow(dead_code)]
pub fn clear_snapshots() {
    let mut manager = match global_snapshot_manager().lock() {
        Ok(guard) => guard,
        Err(poisoned) => {
            // Still use the poisoned lock by recovering the guard
            poisoned.into_inner()
        }
    };
    manager.clear();
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::memory::metrics::{
        generate_memory_report, reset_memory_metrics, track_allocation, track_deallocation,
    };

    /// Synchronize tests using a mutex to prevent concurrent use of the global metrics collector
    use crate::memory::metrics::test_utils::MEMORY_METRICS_TEST_MUTEX as TEST_MUTEX;

    #[test]
    fn test_snapshot_creation() {
        // Use unwrap_or_else to make sure we can continue even with a poisoned mutex
        let lock = TEST_MUTEX
            .lock()
            .unwrap_or_else(|poisoned| poisoned.into_inner());
        println!("test_snapshot_creation started");

        // First make sure all global state is clean
        reset_memory_metrics();
        clear_snapshots();

        // Print initial state for debugging
        let initial_report = generate_memory_report();
        println!(
            "Initial state: total_current_usage={}",
            initial_report.total_current_usage
        );

        // Take a snapshot with no allocations
        let snapshot1 = MemorySnapshot::new("start", "Initial state");

        // Verify no allocations in first snapshot
        assert_eq!(
            snapshot1.report.total_current_usage, 0,
            "First snapshot should have 0 memory usage but had {} bytes",
            snapshot1.report.total_current_usage
        );

        // Reset to get clean metrics state
        reset_memory_metrics();

        // Allocate some memory in a known clean state
        track_allocation("TestComponent", 1024, 0x1000);
        track_allocation("TestComponent", 2048, 0x2000);

        // Print state after allocations
        let allocations_report = generate_memory_report();
        println!(
            "After allocations: total_current_usage={}",
            allocations_report.total_current_usage
        );

        // First verify our memory tracking is correct
        let expected_usage = 1024 + 2048; // From the two allocations above
        assert_eq!(
            allocations_report.total_current_usage, expected_usage,
            "Memory tracking should show {} bytes but showed {} bytes",
            expected_usage, allocations_report.total_current_usage
        );

        // Take another snapshot
        let snapshot2 = MemorySnapshot::new("allocated", "After allocations");

        // Check that we have the expected memory usage (should be 3072 bytes)
        assert_eq!(
            snapshot2.report.total_current_usage, expected_usage,
            "Second snapshot should have {} bytes but had {} bytes",
            expected_usage, snapshot2.report.total_current_usage
        );

        // Create a direct comparison (not using the global snapshots)
        // This ensures we're just comparing these two snapshots directly
        let diff = snapshot1.compare(&snapshot2);

        // Verify correct deltas
        assert_eq!(
            diff.current_usage_delta, expected_usage as isize,
            "Delta between snapshots should be {} bytes but was {} bytes",
            expected_usage, diff.current_usage_delta
        );

        // Reset and test deallocation
        reset_memory_metrics();

        // Create a new baseline snapshot
        let snapshotbase = MemorySnapshot::new("base", "Base for deallocation test");

        // Verify that it starts with zero
        assert_eq!(
            snapshotbase.report.total_current_usage, 0,
            "Baseline snapshot after reset should have 0 memory usage but had {} bytes",
            snapshotbase.report.total_current_usage
        );

        // Allocate and then deallocate
        track_allocation("TestComponent", 1024, 0x1000);
        track_deallocation("TestComponent", 1024, 0x1000);

        // Final snapshot should show no memory usage
        let snapshot_final = MemorySnapshot::new("final", "After deallocation");
        assert_eq!(snapshot_final.report.total_current_usage, 0,
            "Final snapshot after allocation and deallocation should have 0 memory usage but had {} bytes",
            snapshot_final.report.total_current_usage);

        // Clean up for the next test
        reset_memory_metrics();
        clear_snapshots();
        println!("test_snapshot_creation completed");
    }

    #[test]
    fn test_snapshot_manager() {
        // Use unwrap_or_else for better error handling
        let lock = TEST_MUTEX
            .lock()
            .unwrap_or_else(|poisoned| poisoned.into_inner());
        println!("test_snapshot_manager started");

        // Reset metrics and snapshots - do this BEFORE creating anything
        reset_memory_metrics();
        clear_snapshots();

        // Verify the initial state is clean
        let initial_report = generate_memory_report();
        println!(
            "Initial state: total_current_usage={}",
            initial_report.total_current_usage
        );
        assert_eq!(
            initial_report.total_current_usage, 0,
            "Initial memory usage should be 0 but was {} bytes",
            initial_report.total_current_usage
        );

        // Create a snapshot manager
        let mut manager = SnapshotManager::new();

        // Take a snapshot
        let snapshot1 = manager.take_snapshot("s1", "First snapshot");
        assert_eq!(snapshot1.id, "s1", "First snapshot should have ID 's1'");
        println!(
            "First snapshot total_current_usage: {}",
            snapshot1.report.total_current_usage
        );

        // Verify there are no allocations to start with
        assert_eq!(
            snapshot1.report.total_current_usage, 0,
            "First snapshot should have 0 memory usage but had {} bytes",
            snapshot1.report.total_current_usage
        );

        // Reset before allocating
        reset_memory_metrics();

        // Allocate some memory
        track_allocation("TestComponent", 1024, 0x1000);

        // Print state after allocation
        let after_alloc_report = generate_memory_report();
        println!(
            "After allocation: total_current_usage={}",
            after_alloc_report.total_current_usage
        );

        // Verify the reported memory is as expected
        assert_eq!(
            after_alloc_report.total_current_usage, 1024,
            "Expected 1024 bytes allocated but found {} bytes",
            after_alloc_report.total_current_usage
        );

        // Take another snapshot
        let snapshot2 = MemorySnapshot::new("s2", "Second snapshot");

        // Add the snapshot to our manager for testing
        manager.snapshots.push(snapshot2.clone());

        println!(
            "Second snapshot total_current_usage: {}",
            snapshot2.report.total_current_usage
        );

        // Verify the allocation is recorded in the second snapshot
        assert_eq!(
            snapshot2.report.total_current_usage, 1024,
            "Second snapshot should have 1024 bytes but had {} bytes",
            snapshot2.report.total_current_usage
        );

        // Compare snapshots directly
        let diff = snapshot1.compare(&snapshot2);
        assert_eq!(
            diff.current_usage_delta, 1024,
            "Delta between snapshots should be 1024 bytes but was {} bytes",
            diff.current_usage_delta
        );

        // Get a snapshot by ID
        let retrieved = manager.get_snapshot("s1");
        assert!(retrieved.is_some(), "Snapshot 's1' should exist");
        assert_eq!(
            retrieved.expect("Operation failed").id,
            "s1",
            "Retrieved snapshot should have ID 's1'"
        );

        // Clear snapshots
        manager.clear();
        assert!(
            manager.get_snapshot("s1").is_none(),
            "After clearing, snapshot 's1' should not exist"
        );

        // Clean up for the next test
        reset_memory_metrics();
        clear_snapshots();
        println!("test_snapshot_manager completed");
    }

    #[test]
    fn test_global_snapshot_manager() {
        // Lock to prevent concurrent access to global state
        let lock = match TEST_MUTEX.lock() {
            Ok(guard) => guard,
            Err(poisoned) => {
                // Still use the poisoned lock by recovering the guard
                poisoned.into_inner()
            }
        };
        println!("test_global_snapshot_manager started");

        // Ensure we have a clean state to start with
        reset_memory_metrics();
        clear_snapshots();

        // Print initial state for debugging
        let initial_report = generate_memory_report();
        println!(
            "Initial state: total_current_usage={}",
            initial_report.total_current_usage
        );

        // Take a snapshot with no allocations
        let snapshot1 = take_snapshot("g1", "Global snapshot 1");
        assert_eq!(snapshot1.id, "g1", "First snapshot should have ID 'g1'");
        println!(
            "First snapshot total_current_usage: {}",
            snapshot1.report.total_current_usage
        );

        // Verify there are no allocations in the first snapshot
        assert_eq!(
            snapshot1.report.total_current_usage, 0,
            "First snapshot should have 0 memory usage but had {} bytes",
            snapshot1.report.total_current_usage
        );

        // Reset metrics to get a clean state
        reset_memory_metrics();

        // Explicitly clear snapshots again to ensure no interference
        clear_snapshots();

        // Take a snapshot with no allocations for the "before" state
        let clean_snapshot = MemorySnapshot::new("clean", "Baseline for comparison");
        assert_eq!(clean_snapshot.report.total_current_usage, 0);

        // Allocate some memory (exactly 1024 bytes after reset)
        track_allocation("TestComponent", 1024, 0x1000);

        // Print state after allocation
        let after_alloc_report = generate_memory_report();
        println!(
            "After allocation: total_current_usage={}",
            after_alloc_report.total_current_usage
        );

        // Verify exactly 1024 bytes are allocated
        assert_eq!(
            after_alloc_report.total_current_usage, 1024,
            "Expected 1024 bytes allocated but found {} bytes",
            after_alloc_report.total_current_usage
        );

        // Take another snapshot with just our new allocation directly (not via global manager)
        let snapshot2 = MemorySnapshot::new("g2", "Global snapshot 2");
        assert_eq!(snapshot2.id, "g2", "Second snapshot should have ID 'g2'");
        println!(
            "Second snapshot total_current_usage: {}",
            snapshot2.report.total_current_usage
        );

        // Verify the allocation is recorded in the second snapshot
        assert_eq!(
            snapshot2.report.total_current_usage, 1024,
            "Second snapshot should have 1024 bytes but had {} bytes",
            snapshot2.report.total_current_usage
        );

        // Compare snapshots directly - we should have exactly 1024 bytes difference
        let diff = clean_snapshot.compare(&snapshot2);
        assert_eq!(
            diff.current_usage_delta, 1024,
            "Delta between snapshots should be 1024 bytes but was {} bytes",
            diff.current_usage_delta
        );

        // Clean up by clearing snapshots for the next test
        clear_snapshots();
        // Also reset memory metrics again to leave a clean state
        reset_memory_metrics();
        println!("test_global_snapshot_manager completed");
    }

    #[test]
    fn test_thread_safety() {
        // Test thread safety using separate collectors per thread to avoid global state interference
        use crate::memory::metrics::{
            MemoryEvent, MemoryEventType, MemoryMetricsCollector, MemoryMetricsConfig,
        };
        use std::sync::{Arc, Barrier};
        use std::thread;

        let barrier = Arc::new(Barrier::new(3));
        let barrier1 = Arc::clone(&barrier);
        let barrier2 = Arc::clone(&barrier);

        // Thread 1: Use separate collector to avoid global state interference
        let thread1 = thread::spawn(move || {
            // Create a dedicated collector for this thread
            let collector = Arc::new(MemoryMetricsCollector::new(MemoryMetricsConfig::default()));

            // Take initial snapshot from this collector
            let initial_report = collector.generate_report();
            let snapshot =
                MemorySnapshot::from_report("thread1", "Initial snapshot", initial_report);

            // Wait for all threads to reach this point
            barrier1.wait();

            // Record allocation in this thread's collector
            collector.record_event(MemoryEvent::new(
                MemoryEventType::Allocation,
                "Thread1Component",
                2048,
                0x1000,
            ));

            // Take snapshot after allocation
            let after_report = collector.generate_report();
            let snapshot2 =
                MemorySnapshot::from_report("thread1_after", "After allocation", after_report);

            // Verify the diff shows the expected allocation
            let diff = snapshot.compare(&snapshot2);
            assert_eq!(diff.current_usage_delta, 2048);
        });

        // Thread 2: Use separate collector to avoid global state interference
        let thread2 = thread::spawn(move || {
            // Create a dedicated collector for this thread
            let collector = Arc::new(MemoryMetricsCollector::new(MemoryMetricsConfig::default()));

            // Take initial snapshot from this collector
            let initial_report = collector.generate_report();
            let snapshot =
                MemorySnapshot::from_report("thread2", "Initial snapshot", initial_report);

            // Wait for all threads to reach this point
            barrier2.wait();

            // Record allocation in this thread's collector
            collector.record_event(MemoryEvent::new(
                MemoryEventType::Allocation,
                "Thread2Component",
                4096,
                0x2000,
            ));

            // Take snapshot after allocation
            let after_report = collector.generate_report();
            let snapshot2 =
                MemorySnapshot::from_report("thread2_after", "After allocation", after_report);

            // Verify the diff shows the expected allocation
            let diff = snapshot.compare(&snapshot2);
            assert_eq!(diff.current_usage_delta, 4096);
        });

        // Main thread: Wait for other threads to complete
        barrier.wait();

        thread1.join().expect("Operation failed");
        thread2.join().expect("Operation failed");
    }

    #[test]
    fn test_thread_safety_isolated() {
        // Test thread safety using separate collectors per thread to avoid interference
        use crate::memory::metrics::{
            MemoryEvent, MemoryEventType, MemoryMetricsCollector, MemoryMetricsConfig,
        };
        use std::sync::{Arc, Barrier};
        use std::thread;

        let barrier = Arc::new(Barrier::new(3));
        let barrier1 = Arc::clone(&barrier);
        let barrier2 = Arc::clone(&barrier);

        // Thread 1: Use separate collector to avoid global state interference
        let thread1 = thread::spawn(move || {
            // Create a dedicated collector for this thread
            let collector = Arc::new(MemoryMetricsCollector::new(MemoryMetricsConfig::default()));

            // Take initial snapshot from this collector
            let initial_report = collector.generate_report();
            let snapshot =
                MemorySnapshot::from_report("thread1", "Initial snapshot", initial_report);

            // Wait for all threads to reach this point
            barrier1.wait();

            // Record allocation in this thread's collector
            collector.record_event(MemoryEvent::new(
                MemoryEventType::Allocation,
                "Thread1Component",
                2048,
                0x1000,
            ));

            // Take snapshot after allocation
            let after_report = collector.generate_report();
            let snapshot2 =
                MemorySnapshot::from_report("thread1_after", "After allocation", after_report);

            // Verify the diff shows the expected allocation
            let diff = snapshot.compare(&snapshot2);
            assert_eq!(diff.current_usage_delta, 2048);
            assert!(
                diff.new_components
                    .contains(&"Thread1Component".to_string()),
                "Thread1Component should be in new_components"
            );
            assert!(
                diff.removed_components.is_empty(),
                "No components should be removed"
            );
        });

        // Thread 2: Use separate collector to avoid global state interference
        let thread2 = thread::spawn(move || {
            // Create a dedicated collector for this thread
            let collector = Arc::new(MemoryMetricsCollector::new(MemoryMetricsConfig::default()));

            // Take initial snapshot from this collector
            let initial_report = collector.generate_report();
            let snapshot =
                MemorySnapshot::from_report("thread2", "Initial snapshot", initial_report);

            // Wait for all threads to reach this point
            barrier2.wait();

            // Record allocation in this thread's collector
            collector.record_event(MemoryEvent::new(
                MemoryEventType::Allocation,
                "Thread2Component",
                4096,
                0x2000,
            ));

            // Take snapshot after allocation
            let after_report = collector.generate_report();
            let snapshot2 =
                MemorySnapshot::from_report("thread2_after", "After allocation", after_report);

            // Verify the diff shows the expected allocation
            let diff = snapshot.compare(&snapshot2);
            assert_eq!(diff.current_usage_delta, 4096);
            assert!(
                diff.new_components
                    .contains(&"Thread2Component".to_string()),
                "Thread2Component should be in new_components"
            );
            assert!(
                diff.removed_components.is_empty(),
                "No components should be removed"
            );
        });

        // Main thread: Wait for other threads to complete
        barrier.wait();

        thread1.join().expect("Operation failed");
        thread2.join().expect("Operation failed");

        println!("Thread safety test with isolated collectors completed successfully");
    }

    #[test]
    fn test_leak_detection() {
        // Use unwrap_or_else for better error handling
        let lock = TEST_MUTEX
            .lock()
            .unwrap_or_else(|poisoned| poisoned.into_inner());
        println!("test_leak_detection started");

        // Ensure we have a clean state to start with
        reset_memory_metrics();
        clear_snapshots();

        // Print initial state for debugging
        let initial_report = generate_memory_report();
        println!(
            "Initial state: total_current_usage={}",
            initial_report.total_current_usage
        );

        // Take initial snapshot with no allocations
        let snapshot1 = MemorySnapshot::new("leak_test_1", "Before potential leak");
        println!(
            "First snapshot total_current_usage: {}",
            snapshot1.report.total_current_usage
        );

        // Verify there are no allocations to start with
        assert_eq!(
            snapshot1.report.total_current_usage, 0,
            "Initial snapshot should have 0 memory usage but had {} bytes",
            snapshot1.report.total_current_usage
        );

        // Reset again to ensure clean state
        reset_memory_metrics();

        // Allocate memory without tracking deallocation (simulating a leak)
        track_allocation("LeakyComponent", 4096, 0x3000);

        // Print state after allocation
        let after_alloc_report = generate_memory_report();
        println!(
            "After allocation: total_current_usage={}",
            after_alloc_report.total_current_usage
        );

        // Verify exactly 4096 bytes are now allocated
        assert_eq!(
            after_alloc_report.total_current_usage, 4096,
            "Expected 4096 bytes allocated but found {} bytes",
            after_alloc_report.total_current_usage
        );

        // Take snapshot after our leak
        let snapshot2 = MemorySnapshot::new("leak_test_2", "After operations");
        println!(
            "Second snapshot total_current_usage: {}",
            snapshot2.report.total_current_usage
        );

        // Verify the second snapshot has exactly the allocation we made
        assert_eq!(
            snapshot2.report.total_current_usage, 4096,
            "Second snapshot should have 4096 bytes but had {} bytes",
            snapshot2.report.total_current_usage
        );

        // Compare the snapshots
        let diff = snapshot1.compare(&snapshot2);

        // We should have a leaked memory of 4096 bytes
        assert_eq!(
            diff.current_usage_delta, 4096,
            "Delta between snapshots should be 4096 bytes but was {} bytes",
            diff.current_usage_delta
        );

        // Our simple leak detection identifies any memory increase as a potential leak
        // This is a simplification for the test
        assert!(
            diff.current_usage_delta > 0,
            "Memory increase should be detected as potential leak"
        );

        // Let's artificially introduce a component with a leak
        let mut component_diffs = HashMap::new();
        component_diffs.insert(
            "LeakyComponent".to_string(),
            ComponentStatsDiff {
                current_usage_delta: 4096,
                peak_usage_delta: 4096,
                allocation_count_delta: 1,
                total_allocated_delta: 4096,
                potential_leak: true,
            },
        );

        let test_diff = SnapshotDiff {
            first_id: "test1".to_string(),
            second_id: "test2".to_string(),
            elapsed_ms: 100,
            current_usage_delta: 4096,
            peak_usage_delta: 4096,
            allocation_count_delta: 1,
            new_components: vec!["LeakyComponent".to_string()],
            removed_components: vec![],
            component_changes: component_diffs,
        };

        // This should detect potential leaks
        assert!(
            test_diff.has_potential_leaks(),
            "Leak detection should identify potential leaks"
        );

        // And identify the leaky component
        let leak_components = test_diff.get_potential_leak_components();
        assert_eq!(
            leak_components.len(),
            1,
            "Should find exactly one leaky component"
        );
        assert_eq!(
            leak_components[0], "LeakyComponent",
            "Leaky component should be 'LeakyComponent'"
        );

        // Clean up for the next test
        reset_memory_metrics();
        clear_snapshots();
        println!("test_leak_detection completed");
    }
}