memscope-rs 0.2.6

A memory tracking library for Rust applications.
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
//! Variable Registry - Simple HashMap-based variable name tracking
//!
//! This module provides a lightweight alternative to log-based tracking,
//! using a global HashMap to store variable address -> variable info mappings.

use crate::core::{MemScopeError, MemScopeResult};
use rayon::prelude::*;
use std::collections::HashMap;
use std::sync::{Arc, Mutex, OnceLock};

/// Variable information stored in registry
#[derive(Debug, Clone, serde::Serialize)]
pub struct VariableInfo {
    /// User-defined variable name
    pub var_name: String,
    /// Type name of the variable
    pub type_name: String,
    /// Timestamp when variable was registered
    pub timestamp: u64,
    /// Estimated size of the variable
    pub size: usize,
    /// Thread ID that created this variable
    pub thread_id: usize,
    /// Memory usage of this variable
    pub memory_usage: u64,
}

/// Global variable registry using HashMap for fast lookups
static GLOBAL_VARIABLE_REGISTRY: OnceLock<Arc<Mutex<HashMap<usize, VariableInfo>>>> =
    OnceLock::new();

/// Get or initialize the global variable registry
fn get_global_registry() -> Arc<Mutex<HashMap<usize, VariableInfo>>> {
    GLOBAL_VARIABLE_REGISTRY
        .get_or_init(|| Arc::new(Mutex::new(HashMap::new())))
        .clone()
}

/// Variable Registry - manages variable address to name mappings
pub struct VariableRegistry;

mod inference;

impl VariableRegistry {
    /// Register a variable with its address and information.
    ///
    /// # Thread Safety
    /// This function uses a try-lock on the global registry. If the lock is
    /// unavailable (high contention), registration is silently skipped.
    /// In debug builds, a warning is printed to stderr in this case.
    ///
    /// # Return Value
    /// Returns `Ok(())` on success. Note: due to lock-free design,
    /// registration failures are silently ignored to avoid blocking.
    pub fn register_variable(
        address: usize,
        var_name: String,
        type_name: String,
        size: usize,
    ) -> MemScopeResult<()> {
        let thread_id = {
            // Use a simple atomic counter for thread IDs instead of hash
            static THREAD_COUNTER: std::sync::atomic::AtomicUsize =
                std::sync::atomic::AtomicUsize::new(1);
            static THREAD_ID_MAP: std::sync::OnceLock<
                std::sync::Mutex<std::collections::HashMap<std::thread::ThreadId, usize>>,
            > = std::sync::OnceLock::new();

            let map = THREAD_ID_MAP
                .get_or_init(|| std::sync::Mutex::new(std::collections::HashMap::new()));
            let current_thread_id = std::thread::current().id();

            if let Ok(mut map) = map.try_lock() {
                *map.entry(current_thread_id).or_insert_with(|| {
                    THREAD_COUNTER.fetch_add(1, std::sync::atomic::Ordering::Relaxed)
                })
            } else {
                // Fallback if we can't get the lock
                1
            }
        };
        let timestamp = std::time::SystemTime::now()
            .duration_since(std::time::UNIX_EPOCH)
            .unwrap_or_default()
            .as_nanos() as u64;

        let var_info = VariableInfo {
            var_name,
            type_name,
            timestamp,
            size,
            thread_id,
            memory_usage: size as u64,
        };

        // Retry mechanism to prevent data loss under high contention
        let mut retry_count = 0;
        const MAX_RETRIES: usize = 5;
        const INITIAL_BACKOFF_MS: u64 = 1;

        loop {
            if let Ok(mut registry) = get_global_registry().try_lock() {
                registry.insert(address, var_info);
                break;
            } else {
                retry_count += 1;
                if retry_count >= MAX_RETRIES {
                    tracing::warn!(
                        "Variable registration for '{}' dropped after {} retries due to lock contention. ptr=0x{:x}, size={}",
                        var_info.var_name, MAX_RETRIES, address, size
                    );
                    break;
                }

                // Exponential backoff: 1ms, 2ms, 4ms, 8ms, 16ms
                let backoff_ms = INITIAL_BACKOFF_MS * (1 << (retry_count - 1));
                std::thread::sleep(std::time::Duration::from_millis(backoff_ms));
            }
        }

        Ok(())
    }

    /// Get variable information by address
    pub fn get_variable_info(address: usize) -> Option<VariableInfo> {
        if let Ok(registry) = get_global_registry().try_lock() {
            registry.get(&address).cloned()
        } else {
            None
        }
    }

    /// Mark a variable as destroyed with destruction timestamp
    pub fn mark_variable_destroyed(address: usize, destruction_time: u64) -> MemScopeResult<()> {
        // For now, we keep the variable in registry but could add destruction_time field
        // This method ensures the variable registry is aware of destruction events
        tracing::debug!(
            "Variable at address 0x{:x} destroyed at {}",
            address,
            destruction_time
        );
        Ok(())
    }

    /// Get all variable mappings
    pub fn get_all_variables() -> HashMap<usize, VariableInfo> {
        if let Ok(registry) = get_global_registry().try_lock() {
            registry.clone()
        } else {
            HashMap::new()
        }
    }

    /// Enhance tracker allocations with variable names from registry (optimized with parallel processing)
    pub fn enhance_allocations_with_registry(
        allocations: &[crate::core::types::AllocationInfo],
    ) -> Vec<serde_json::Value> {
        // Early return for small datasets
        if allocations.len() < 100 {
            return Self::enhance_allocations_sequential(allocations);
        }

        tracing::info!(
            "🚀 Processing {} allocations with parallel optimization...",
            allocations.len()
        );

        let registry = Self::get_all_variables();
        let start_time = std::time::Instant::now();

        // Convert to new system allocation types
        let allocations: Vec<crate::capture::types::AllocationInfo> =
            allocations.iter().map(|a| a.clone().into()).collect();

        // Use parallel processing for large datasets
        let enhanced: Vec<serde_json::Value> = allocations
            .par_iter()
            .map(|alloc| Self::classify_single_allocation(alloc, &registry))
            .collect();

        let duration = start_time.elapsed();
        tracing::info!(
            "✅ Parallel processing completed in {:?} ({:.2} allocs/ms)",
            duration,
            allocations.len() as f64 / duration.as_millis() as f64
        );

        enhanced
    }

    /// Sequential processing for small datasets
    fn enhance_allocations_sequential(
        allocations: &[crate::core::types::AllocationInfo],
    ) -> Vec<serde_json::Value> {
        let registry = Self::get_all_variables();

        // Convert to new system allocation types
        let allocations: Vec<crate::capture::types::AllocationInfo> =
            allocations.iter().map(|a| a.clone().into()).collect();

        allocations
            .iter()
            .map(|alloc| Self::classify_single_allocation(alloc, &registry))
            .collect()
    }

    /// Classify and enhance allocations with user/system distinction and scope information
    fn classify_and_enhance_allocations(
        allocations: &[crate::capture::types::allocation::AllocationInfo],
        registry: &HashMap<usize, VariableInfo>,
    ) -> Vec<serde_json::Value> {
        allocations
            .par_iter()
            .map(|alloc| Self::classify_single_allocation(alloc, registry))
            .collect()
    }

    /// Classify a single allocation as user or system with full context
    fn classify_single_allocation(
        alloc: &crate::capture::types::allocation::AllocationInfo,
        registry: &HashMap<usize, VariableInfo>,
    ) -> serde_json::Value {
        // Check if this is a user-tracked variable (highest priority)
        if let Some(var_info) = registry.get(&alloc.ptr) {
            return serde_json::json!({
                "ptr": alloc.ptr,
                "size": alloc.size,
                "timestamp_alloc": alloc.timestamp_alloc,
                "timestamp_dealloc": alloc.timestamp_dealloc,
                "variable_name": var_info.var_name,
                "type_name": var_info.type_name,
                "scope_name": Self::extract_scope_from_var_name(&var_info.var_name),
                "allocation_source": "user",
                "tracking_method": "track_var_macro",
                "registry_timestamp": var_info.timestamp,
                "registry_size": var_info.size,
                "lifetime_ms": alloc.timestamp_dealloc.map(|dealloc|
                    (dealloc.saturating_sub(alloc.timestamp_alloc)) / 1_000_000
                ),
                "current_age_ms": if alloc.timestamp_dealloc.is_none() {
                    // For active allocations, calculate how long they've been alive
                    let current_time = std::time::SystemTime::now()
                        .duration_since(std::time::UNIX_EPOCH)
                        .unwrap_or_default()
                        .as_nanos() as u64;
                    Some((current_time.saturating_sub(alloc.timestamp_alloc)) / 1_000_000)
                } else {
                    None
                },
                "is_active": alloc.timestamp_dealloc.is_none(),
                "is_leaked": alloc.is_leaked
            });
        }

        // Check if allocation has explicit variable information (user allocation)
        if let (Some(var_name), Some(type_name)) = (&alloc.var_name, &alloc.type_name) {
            return serde_json::json!({
                "ptr": alloc.ptr,
                "size": alloc.size,
                "timestamp_alloc": alloc.timestamp_alloc,
                "timestamp_dealloc": alloc.timestamp_dealloc,
                "variable_name": var_name,
                "type_name": type_name,
                "scope_name": alloc.scope_name.as_deref().unwrap_or("user_scope"),
                "allocation_source": "user",
                "tracking_method": "explicit_tracking",
                "lifetime_ms": alloc.timestamp_dealloc.map(|dealloc|
                    (dealloc.saturating_sub(alloc.timestamp_alloc)) / 1_000_000
                ),
                "current_age_ms": if alloc.timestamp_dealloc.is_none() {
                    let current_time = std::time::SystemTime::now()
                        .duration_since(std::time::UNIX_EPOCH)
                        .unwrap_or_default()
                        .as_nanos() as u64;
                    Some((current_time.saturating_sub(alloc.timestamp_alloc)) / 1_000_000)
                } else {
                    None
                },
                "is_active": alloc.timestamp_dealloc.is_none(),
                "is_leaked": alloc.is_leaked
            });
        }

        // This is a system allocation - apply smart inference
        let (inferred_var_name, inferred_type_name) = Self::infer_allocation_info_cached(alloc);
        let system_category = Self::categorize_system_allocation(alloc);

        serde_json::json!({
            "ptr": alloc.ptr,
            "size": alloc.size,
            "timestamp_alloc": alloc.timestamp_alloc,
            "timestamp_dealloc": alloc.timestamp_dealloc,
            "variable_name": inferred_var_name,
            "type_name": inferred_type_name,
            "scope_name": "system",
            "allocation_source": "system",
            "tracking_method": "automatic_inference",
            "system_category": system_category,
            "lifetime_ms": alloc.timestamp_dealloc.map(|dealloc|
                (dealloc.saturating_sub(alloc.timestamp_alloc)) / 1_000_000
            ),
            "current_age_ms": if alloc.timestamp_dealloc.is_none() {
                let current_time = std::time::SystemTime::now()
                    .duration_since(std::time::UNIX_EPOCH)
                    .unwrap_or_default()
                    .as_nanos() as u64;
                Some((current_time.saturating_sub(alloc.timestamp_alloc)) / 1_000_000)
            } else {
                None
            },
            "is_active": alloc.timestamp_dealloc.is_none(),
            "is_leaked": alloc.is_leaked
        })
    }

    /// Extract scope information from variable name and current scope context
    fn extract_scope_from_var_name(var_name: &str) -> String {
        // First, try to get the current scope from the scope tracker
        if let Some(current_scope) = Self::get_current_scope_name() {
            return current_scope;
        }

        // Fallback: Try to extract scope from variable name patterns
        if var_name.contains("::") {
            if let Some(scope_part) = var_name.split("::").next() {
                return scope_part.to_string();
            }
        }

        // Check for common scope patterns
        if var_name.starts_with("main_") {
            "main_function".to_string()
        } else if var_name.starts_with("test_") {
            "test_function".to_string()
        } else if var_name.starts_with("fn_") {
            "user_function".to_string()
        } else if var_name.contains("_vec")
            || var_name.contains("_string")
            || var_name.contains("_data")
            || var_name.starts_with("boxed_")
            || var_name.starts_with("rc_")
            || var_name.starts_with("arc_")
        {
            "user_scope".to_string()
        } else {
            // For user variables, default to user_scope instead of global
            "user_scope".to_string()
        }
    }

    /// Get the current scope name from the scope tracker or infer from call stack
    fn get_current_scope_name() -> Option<String> {
        // First try to get from scope tracker
        if let Some(scope_name) = Self::get_scope_from_tracker() {
            return Some(scope_name);
        }

        // Fallback: Try to infer scope from call stack
        Self::infer_scope_from_call_stack()
    }

    /// Get scope from the scope tracker
    fn get_scope_from_tracker() -> Option<String> {
        use crate::core::scope_tracker::get_global_scope_tracker;

        let scope_tracker = get_global_scope_tracker();
        let thread_id = format!("{:?}", std::thread::current().id());

        if let Some(thread_stack) = scope_tracker
            .scope_stack
            .read()
            .ok()
            .and_then(|s| s.get(&thread_id).cloned())
        {
            if let Some(current_scope_id) = thread_stack.last() {
                if let Some(scope_info) = scope_tracker
                    .active_scopes
                    .read()
                    .ok()
                    .and_then(|s| s.get(current_scope_id).cloned())
                {
                    return Some(scope_info.name);
                }
            }
        }

        None
    }

    /// Infer scope from call stack information
    fn infer_scope_from_call_stack() -> Option<String> {
        // Try to get function name from backtrace
        let backtrace = std::backtrace::Backtrace::capture();
        let backtrace_str = format!("{backtrace:?}");

        // Look for function names in the backtrace
        for line in backtrace_str.lines() {
            if line.contains("::main") {
                return Some("main_function".to_string());
            }
            if line.contains("test_") || line.contains("tests::") {
                return Some("test_function".to_string());
            }
            // Look for user-defined function patterns
            if let Some(func_name) = Self::extract_function_name_from_backtrace(line) {
                return Some(format!("function_{func_name}"));
            }
        }

        // If we can't determine the scope, use a more descriptive default
        Some("user_code_scope".to_string())
    }

    /// Extract function name from backtrace line
    fn extract_function_name_from_backtrace(line: &str) -> Option<String> {
        // Try to extract function names from common patterns
        if let Some(start) = line.find("::") {
            if let Some(end) = line[start + 2..].find("::") {
                let func_name = &line[start + 2..start + 2 + end];
                // Filter out common system functions
                if !func_name.starts_with("_")
                    && !func_name.contains("alloc")
                    && !func_name.contains("std")
                    && !func_name.contains("core")
                    && func_name.len() > 2
                {
                    return Some(func_name.to_string());
                }
            }
        }
        None
    }

    /// Categorize system allocations for better understanding
    fn categorize_system_allocation(
        alloc: &crate::capture::types::allocation::AllocationInfo,
    ) -> String {
        match alloc.size {
            1..=16 => "small_system_alloc",
            17..=64 => "medium_system_alloc",
            65..=1024 => "large_system_alloc",
            1025..=65536 => "buffer_allocation",
            _ => "huge_allocation",
        }
        .to_string()
    }

    /// Group allocations by scope for better organization
    fn group_by_scope(
        active: &[serde_json::Value],
        history: &[serde_json::Value],
    ) -> serde_json::Value {
        let mut scopes: HashMap<String, Vec<&serde_json::Value>> = HashMap::new();

        // Group active allocations
        for alloc in active {
            if let Some(scope) = alloc["scope_name"].as_str() {
                scopes.entry(scope.to_string()).or_default().push(alloc);
            }
        }

        // Group history allocations
        for alloc in history {
            if let Some(scope) = alloc["scope_name"].as_str() {
                scopes.entry(scope.to_string()).or_default().push(alloc);
            }
        }

        let scope_summary: HashMap<String, serde_json::Value> = scopes
            .into_iter()
            .map(|(scope_name, allocations)| {
                let total_size: u64 = allocations
                    .iter()
                    .map(|a| a["size"].as_u64().unwrap_or(0))
                    .sum();

                (
                    scope_name.clone(),
                    serde_json::json!({
                        "scope_name": scope_name,
                        "allocation_count": allocations.len(),
                        "total_size_bytes": total_size,
                        "allocations": allocations
                    }),
                )
            })
            .collect();

        serde_json::json!(scope_summary)
    }

    /// Get scope summary from registry
    fn get_scope_summary(registry: &HashMap<usize, VariableInfo>) -> serde_json::Value {
        let mut scope_counts: HashMap<String, usize> = HashMap::new();

        for var_info in registry.values() {
            let scope = Self::extract_scope_from_var_name(&var_info.var_name);
            *scope_counts.entry(scope).or_insert(0) += 1;
        }

        serde_json::json!(scope_counts)
    }

    /// Analyze lifecycle statistics for lifetime_ms patterns
    fn analyze_lifecycle_statistics(
        user_active: &[serde_json::Value],
        user_history: &[serde_json::Value],
        system_active: &[serde_json::Value],
        system_history: &[serde_json::Value],
    ) -> serde_json::Value {
        // Combine all allocations for analysis
        let all_user: Vec<&serde_json::Value> =
            user_active.iter().chain(user_history.iter()).collect();
        let all_system: Vec<&serde_json::Value> =
            system_active.iter().chain(system_history.iter()).collect();

        // Analyze user allocations - now all should have lifetime_ms values
        let user_lifetimes: Vec<u64> = all_user
            .iter()
            .filter_map(|a| a["lifetime_ms"].as_u64())
            .collect();

        let user_active_count = all_user
            .iter()
            .filter(|a| a["is_active"].as_bool().unwrap_or(false))
            .count();

        let user_deallocated_count = all_user
            .iter()
            .filter(|a| !a["timestamp_dealloc"].is_null())
            .count();

        // Analyze system allocations - now all should have lifetime_ms values
        let system_lifetimes: Vec<u64> = all_system
            .iter()
            .filter_map(|a| a["lifetime_ms"].as_u64())
            .collect();

        let system_active_count = all_system
            .iter()
            .filter(|a| a["is_active"].as_bool().unwrap_or(false))
            .count();

        let system_deallocated_count = all_system
            .iter()
            .filter(|a| !a["timestamp_dealloc"].is_null())
            .count();

        // A live allocation is not a leak: "no deallocation timestamp" only says the
        // block was still present when the snapshot was taken. Leak counts follow the
        // explicit `is_leaked` flag produced by the leak rules (plan A02).
        let user_leaked_count = all_user
            .iter()
            .filter(|a| a["is_leaked"].as_bool().unwrap_or(false))
            .count();
        let system_leaked_count = all_system
            .iter()
            .filter(|a| a["is_leaked"].as_bool().unwrap_or(false))
            .count();

        serde_json::json!({
            "user_allocations": {
                "total_count": all_user.len(),
                "active_count": user_active_count,
                "deallocated_count": user_deallocated_count,
                "leaked_count": user_leaked_count,
                "lifetime_stats": Self::calculate_lifetime_stats(&user_lifetimes),
                "average_lifetime_ms": if !user_lifetimes.is_empty() {
                    user_lifetimes.iter().sum::<u64>() / user_lifetimes.len() as u64
                } else { 0 },
                "max_lifetime_ms": user_lifetimes.iter().max().copied().unwrap_or(0),
                "min_lifetime_ms": user_lifetimes.iter().min().copied().unwrap_or(0)
            },
            "system_allocations": {
                "total_count": all_system.len(),
                "active_count": system_active_count,
                "deallocated_count": system_deallocated_count,
                "leaked_count": system_leaked_count,
                "lifetime_stats": Self::calculate_lifetime_stats(&system_lifetimes),
                "average_lifetime_ms": if !system_lifetimes.is_empty() {
                    system_lifetimes.iter().sum::<u64>() / system_lifetimes.len() as u64
                } else { 0 },
                "max_lifetime_ms": system_lifetimes.iter().max().copied().unwrap_or(0),
                "min_lifetime_ms": system_lifetimes.iter().min().copied().unwrap_or(0)
            },
            "comparison": {
                "user_vs_system_active_ratio": if system_active_count > 0 {
                    user_active_count as f64 / system_active_count as f64
                } else { 0.0 },
                "user_vs_system_lifetime_ratio": if !system_lifetimes.is_empty() && !user_lifetimes.is_empty() {
                    (user_lifetimes.iter().sum::<u64>() / user_lifetimes.len() as u64) as f64 /
                    (system_lifetimes.iter().sum::<u64>() / system_lifetimes.len() as u64) as f64
                } else { 0.0 }
            }
        })
    }

    /// Analyze deallocation patterns for timestamp_dealloc
    fn analyze_deallocation_patterns(
        user_active: &[serde_json::Value],
        user_history: &[serde_json::Value],
        system_active: &[serde_json::Value],
        system_history: &[serde_json::Value],
    ) -> serde_json::Value {
        let all_user: Vec<&serde_json::Value> =
            user_active.iter().chain(user_history.iter()).collect();
        let all_system: Vec<&serde_json::Value> =
            system_active.iter().chain(system_history.iter()).collect();

        // Analyze deallocation timestamps
        let user_dealloc_times: Vec<u64> = all_user
            .iter()
            .filter_map(|a| a["timestamp_dealloc"].as_u64())
            .collect();

        let system_dealloc_times: Vec<u64> = all_system
            .iter()
            .filter_map(|a| a["timestamp_dealloc"].as_u64())
            .collect();

        // Count null deallocations (active/leaked allocations)
        let user_null_dealloc = all_user
            .iter()
            .filter(|a| a["timestamp_dealloc"].is_null())
            .count();

        let system_null_dealloc = all_system
            .iter()
            .filter(|a| a["timestamp_dealloc"].is_null())
            .count();

        serde_json::json!({
            "user_deallocations": {
                "total_deallocated": user_dealloc_times.len(),
                "still_active": user_null_dealloc,
                "deallocation_rate": if !all_user.is_empty() {
                    user_dealloc_times.len() as f64 / all_user.len() as f64 * 100.0
                } else { 0.0 },
                "earliest_dealloc": user_dealloc_times.iter().min().copied(),
                "latest_dealloc": user_dealloc_times.iter().max().copied(),
                "deallocation_timespan_ms": if user_dealloc_times.len() > 1 {
                    user_dealloc_times.iter().max().unwrap_or(&0) -
                    user_dealloc_times.iter().min().unwrap_or(&0)
                } else { 0 }
            },
            "system_deallocations": {
                "total_deallocated": system_dealloc_times.len(),
                "still_active": system_null_dealloc,
                "deallocation_rate": if !all_system.is_empty() {
                    system_dealloc_times.len() as f64 / all_system.len() as f64 * 100.0
                } else { 0.0 },
                "earliest_dealloc": system_dealloc_times.iter().min().copied(),
                "latest_dealloc": system_dealloc_times.iter().max().copied(),
                "deallocation_timespan_ms": if system_dealloc_times.len() > 1 {
                    system_dealloc_times.iter().max().unwrap_or(&0) -
                    system_dealloc_times.iter().min().unwrap_or(&0)
                } else { 0 }
            },
            "memory_leak_analysis": {
                "user_potential_leaks": user_null_dealloc,
                "system_potential_leaks": system_null_dealloc,
                "total_potential_leaks": user_null_dealloc + system_null_dealloc,
                "user_leak_percentage": if !all_user.is_empty() {
                    user_null_dealloc as f64 / all_user.len() as f64 * 100.0
                } else { 0.0 },
                "system_leak_percentage": if !all_system.is_empty() {
                    system_null_dealloc as f64 / all_system.len() as f64 * 100.0
                } else { 0.0 }
            }
        })
    }

    /// Calculate detailed lifetime statistics
    fn calculate_lifetime_stats(lifetimes: &[u64]) -> serde_json::Value {
        if lifetimes.is_empty() {
            return serde_json::json!({
                "count": 0,
                "categories": {
                    "very_short": 0,    // < 1ms
                    "short": 0,         // 1-10ms
                    "medium": 0,        // 10-100ms
                    "long": 0,          // 100-1000ms
                    "very_long": 0      // > 1000ms
                }
            });
        }

        let mut very_short = 0;
        let mut short = 0;
        let mut medium = 0;
        let mut long = 0;
        let mut very_long = 0;

        for &lifetime in lifetimes {
            match lifetime {
                0..=1 => very_short += 1,
                2..=10 => short += 1,
                11..=100 => medium += 1,
                101..=1000 => long += 1,
                _ => very_long += 1,
            }
        }

        serde_json::json!({
            "count": lifetimes.len(),
            "categories": {
                "very_short": very_short,
                "short": short,
                "medium": medium,
                "long": long,
                "very_long": very_long
            },
            "percentiles": {
                "p50": Self::calculate_percentile(lifetimes, 50.0),
                "p90": Self::calculate_percentile(lifetimes, 90.0),
                "p95": Self::calculate_percentile(lifetimes, 95.0),
                "p99": Self::calculate_percentile(lifetimes, 99.0)
            }
        })
    }

    /// Calculate percentile for lifetime analysis
    fn calculate_percentile(sorted_values: &[u64], percentile: f64) -> u64 {
        if sorted_values.is_empty() {
            return 0;
        }

        let mut values = sorted_values.to_vec();
        values.sort_unstable();

        let index = (percentile / 100.0 * (values.len() - 1) as f64) as usize;
        values[index.min(values.len() - 1)]
    }

    /// Generate comprehensive export data with clear separation of system vs user allocations
    pub fn generate_comprehensive_export(
        tracker: &crate::core::tracker::MemoryTracker,
    ) -> MemScopeResult<serde_json::Value> {
        let start_time = std::time::Instant::now();
        tracing::info!(
            "🔄 Starting comprehensive export generation with allocation classification..."
        );

        // Get tracker data
        let active_allocations = tracker.get_active_allocations().map_err(|e| {
            MemScopeError::error(
                "variable_registry",
                "generate_comprehensive_export",
                e.to_string(),
            )
        })?;
        let allocation_history = tracker.get_active_allocations().map_err(|e| {
            MemScopeError::error(
                "variable_registry",
                "generate_comprehensive_export",
                e.to_string(),
            )
        })?;
        let memory_by_type = tracker.get_memory_by_type().map_err(|e| {
            MemScopeError::error(
                "variable_registry",
                "generate_comprehensive_export",
                e.to_string(),
            )
        })?;
        let stats = tracker.get_stats().map_err(|e| {
            MemScopeError::error(
                "variable_registry",
                "generate_comprehensive_export",
                e.to_string(),
            )
        })?;
        let registry = Self::get_all_variables();

        tracing::info!(
            "📊 Data loaded: {} active, {} history, {} registry entries",
            active_allocations.len(),
            allocation_history.len(),
            registry.len()
        );

        // Filter out very small allocations to reduce processing overhead
        let filtered_active: Vec<_> = if active_allocations.len() > 10000 {
            active_allocations
                .into_iter()
                .filter(|alloc| alloc.size >= 8)
                .collect()
        } else {
            active_allocations
        };

        let filtered_history: Vec<_> = if allocation_history.len() > 50000 {
            allocation_history
                .into_iter()
                .filter(|alloc| alloc.size >= 8)
                .collect()
        } else {
            allocation_history
        };

        // Convert to new system allocation types
        let filtered_active: Vec<crate::capture::types::AllocationInfo> = filtered_active
            .into_iter()
            .map(|a: crate::capture::backends::core_types::AllocationInfo| a.into())
            .collect();
        let filtered_history: Vec<crate::capture::types::AllocationInfo> = filtered_history
            .into_iter()
            .map(|a: crate::capture::backends::core_types::AllocationInfo| a.into())
            .collect();

        // Classify and enhance allocations in parallel
        let (classified_active, classified_history) = rayon::join(
            || Self::classify_and_enhance_allocations(&filtered_active, &registry),
            || Self::classify_and_enhance_allocations(&filtered_history, &registry),
        );

        // Separate user and system allocations
        let (user_active, system_active): (Vec<_>, Vec<_>) = classified_active
            .into_iter()
            .partition(|alloc| alloc["allocation_source"] == "user");

        let (user_history, system_history): (Vec<_>, Vec<_>) = classified_history
            .into_iter()
            .partition(|alloc| alloc["allocation_source"] == "user");

        // Group user variables by scope
        let user_scopes = Self::group_by_scope(&user_active, &user_history);

        // Build comprehensive result with clear separation
        let comprehensive_data = serde_json::json!({
            "memory_analysis": {
                "user_allocations": {
                    "active": user_active,
                    "history": user_history,
                    "by_scope": user_scopes,
                    "total_count": user_active.len() + user_history.len()
                },
                "system_allocations": {
                    "active": system_active,
                    "history": system_history,
                    "total_count": system_active.len() + system_history.len()
                },
                "memory_by_type": memory_by_type,
                "statistics": {
                    "overall": stats,
                    "user_vs_system": {
                        "user_active_count": user_active.len(),
                        "system_active_count": system_active.len(),
                        "user_total_size": user_active.iter()
                            .map(|a| a["size"].as_u64().unwrap_or(0))
                            .sum::<u64>(),
                        "system_total_size": system_active.iter()
                            .map(|a| a["size"].as_u64().unwrap_or(0))
                            .sum::<u64>()
                    },
                    "lifecycle_analysis": Self::analyze_lifecycle_statistics(&user_active, &user_history, &system_active, &system_history),
                    "deallocation_analysis": Self::analyze_deallocation_patterns(&user_active, &user_history, &system_active, &system_history)
                }
            },
            "variable_registry": {
                "total_variables": registry.len(),
                "user_variables": registry.values().collect::<Vec<_>>(),
                "scope_summary": Self::get_scope_summary(&registry)
            },
            "export_metadata": {
                "timestamp": std::time::SystemTime::now()
                    .duration_since(std::time::UNIX_EPOCH)
                    .unwrap_or_default()
                    .as_secs(),
                "total_allocations": user_active.len() + user_history.len() + system_active.len() + system_history.len(),
                "processing_time_ms": start_time.elapsed().as_millis(),
                "classification_features": [
                    "user_vs_system_separation",
                    "scope_based_grouping",
                    "allocation_source_tracking",
                    "enhanced_type_inference"
                ]
            }
        });

        let total_time = start_time.elapsed();
        tracing::info!(
            "✅ Export completed in {:?} - User: {}, System: {}",
            total_time,
            user_active.len() + user_history.len(),
            system_active.len() + system_history.len()
        );

        Ok(comprehensive_data)
    }

    /// Clear all variable registrations
    pub fn clear_registry() -> MemScopeResult<()> {
        if let Ok(mut registry) = get_global_registry().try_lock() {
            registry.clear();
        }
        Ok(())
    }

    /// Get registry statistics
    pub fn get_stats() -> (usize, usize) {
        if let Ok(registry) = get_global_registry().try_lock() {
            let total = registry.len();
            let recent = registry
                .values()
                .filter(|v| {
                    let now = std::time::SystemTime::now()
                        .duration_since(std::time::UNIX_EPOCH)
                        .unwrap_or_default()
                        .as_nanos() as u64;
                    now - v.timestamp < 1_000_000_000 // Last 1 second
                })
                .count();
            (total, recent)
        } else {
            (0, 0)
        }
    }
}

#[cfg(test)]
mod tests;