memscope-rs 0.2.3

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
//! Snapshot types - Data structures for memory snapshots
//!
//! This module defines the core data structures used by the
//! SnapshotEngine for representing memory snapshots.

use crate::core::types::TrackKind;
use serde::{Deserialize, Serialize};
use std::collections::HashMap;

/// Active allocation information in a snapshot
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct ActiveAllocation {
    /// Memory pointer address (None for Container/Value types)
    pub ptr: Option<usize>,
    /// Allocation size in bytes
    pub size: usize,
    /// Memory allocation semantic role
    pub kind: TrackKind,
    /// Timestamp when this allocation was made
    pub allocated_at: u64,
    /// Optional variable name
    pub var_name: Option<String>,
    /// Optional type name
    pub type_name: Option<String>,
    /// Thread ID that made this allocation
    pub thread_id: u64,
    /// Optional call stack hash for clone detection
    pub call_stack_hash: Option<u64>,
    /// Module path (from module_path!())
    pub module_path: Option<String>,
    /// Stack pointer (for StackOwner types like Arc/Rc)
    pub stack_ptr: Option<usize>,
}

/// Memory statistics for a snapshot
#[derive(Debug, Clone, Serialize, Deserialize, Default)]
pub struct MemoryStats {
    /// Total number of allocations in the snapshot
    pub total_allocations: usize,
    /// Total number of reallocations
    pub total_reallocations: usize,
    /// Total number of deallocations
    pub total_deallocations: usize,
    /// Number of deallocations without matching allocations
    pub unmatched_deallocations: usize,
    /// Number of currently active allocations
    pub active_allocations: usize,
    /// Total bytes allocated
    pub total_allocated: usize,
    /// Total bytes deallocated
    pub total_deallocated: usize,
    /// Currently used memory (sum of active allocations)
    pub current_memory: usize,
    /// Peak memory usage observed
    pub peak_memory: usize,
}

/// Thread-specific memory statistics
#[derive(Debug, Clone, Serialize, Deserialize, Default)]
pub struct ThreadMemoryStats {
    /// Thread ID
    pub thread_id: u64,
    /// Number of allocations by this thread
    pub allocation_count: usize,
    /// Total bytes allocated by this thread
    pub total_allocated: usize,
    /// Total bytes deallocated by this thread
    pub total_deallocated: usize,
    /// Current memory usage by this thread
    pub current_memory: usize,
    /// Peak memory usage by this thread
    pub peak_memory: usize,
}

/// Memory snapshot - a point-in-time view of memory usage
#[derive(Debug, Clone, Serialize, Deserialize, Default)]
pub struct MemorySnapshot {
    /// Timestamp when this snapshot was taken
    pub timestamp: u64,
    /// Overall memory statistics
    pub stats: MemoryStats,
    /// Active allocations (ptr -> allocation info)
    pub active_allocations: HashMap<usize, ActiveAllocation>,
    /// Per-thread statistics
    pub thread_stats: HashMap<u64, ThreadMemoryStats>,
}

impl MemorySnapshot {
    /// Create a new empty snapshot
    pub fn new() -> Self {
        Self {
            timestamp: std::time::SystemTime::now()
                .duration_since(std::time::UNIX_EPOCH)
                .unwrap_or_default()
                .as_nanos() as u64,
            stats: MemoryStats::default(),
            active_allocations: HashMap::new(),
            thread_stats: HashMap::new(),
        }
    }

    /// Build a MemorySnapshot from a list of AllocationInfo (capture module type)
    pub fn from_allocation_infos(allocations: Vec<crate::capture::types::AllocationInfo>) -> Self {
        let mut snapshot = Self::new();
        let mut thread_stats: HashMap<u64, ThreadMemoryStats> = HashMap::new();
        let mut current_memory: usize = 0;

        for alloc in allocations {
            let thread_id = alloc.thread_id_u64;

            let active_alloc = ActiveAllocation {
                ptr: Some(alloc.ptr),
                size: alloc.size,
                kind: TrackKind::HeapOwner {
                    ptr: alloc.ptr,
                    size: alloc.size,
                },
                allocated_at: alloc.timestamp_alloc,
                var_name: alloc.var_name,
                type_name: alloc.type_name,
                thread_id,
                call_stack_hash: None,
                module_path: alloc.module_path,
                stack_ptr: None,
            };

            current_memory += alloc.size;

            snapshot.stats.total_allocations += 1;
            snapshot.stats.total_allocated += alloc.size;

            let thread_stat = thread_stats
                .entry(thread_id)
                .or_insert_with(|| ThreadMemoryStats {
                    thread_id,
                    allocation_count: 0,
                    total_allocated: 0,
                    total_deallocated: 0,
                    current_memory: 0,
                    peak_memory: 0,
                });

            thread_stat.allocation_count += 1;
            thread_stat.total_allocated += alloc.size;
            thread_stat.current_memory += alloc.size;
            if thread_stat.current_memory > thread_stat.peak_memory {
                thread_stat.peak_memory = thread_stat.current_memory;
            }

            snapshot.active_allocations.insert(alloc.ptr, active_alloc);
        }

        snapshot.stats.current_memory = current_memory;
        snapshot.stats.peak_memory = 0; // Cannot determine peak from current allocations only
        snapshot.stats.active_allocations = snapshot.active_allocations.len();
        snapshot.thread_stats = thread_stats;

        snapshot
    }

    /// Get the number of active allocations
    pub fn active_count(&self) -> usize {
        self.active_allocations.len()
    }

    /// Get the current memory usage
    pub fn current_memory(&self) -> usize {
        self.stats.current_memory
    }

    /// Get the peak memory usage
    pub fn peak_memory(&self) -> usize {
        self.stats.peak_memory
    }
}

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

    #[test]
    fn test_memory_snapshot_new() {
        let snapshot = MemorySnapshot::new();
        assert!(snapshot.timestamp > 0);
        assert_eq!(snapshot.stats.total_allocations, 0);
        assert!(snapshot.active_allocations.is_empty());
    }

    #[test]
    fn test_memory_snapshot_default() {
        let snapshot = MemorySnapshot::default();
        assert_eq!(snapshot.timestamp, 0);
        assert_eq!(snapshot.stats.total_allocations, 0);
    }

    #[test]
    fn test_active_allocation_creation() {
        let alloc = ActiveAllocation {
            ptr: Some(0x1000),
            size: 1024,
            kind: TrackKind::HeapOwner {
                ptr: 0x1000,
                size: 1024,
            },
            allocated_at: 1000,
            var_name: Some("test".to_string()),
            type_name: Some("Vec<u8>".to_string()),
            thread_id: 1,
            call_stack_hash: None,
            module_path: None,
            stack_ptr: None,
        };

        assert_eq!(alloc.ptr, Some(0x1000));
        assert_eq!(alloc.size, 1024);
        assert_eq!(alloc.var_name, Some("test".to_string()));
    }

    #[test]
    fn test_active_allocation_clone() {
        let alloc = ActiveAllocation {
            ptr: Some(0x1000),
            size: 1024,
            kind: TrackKind::HeapOwner {
                ptr: 0x1000,
                size: 1024,
            },
            allocated_at: 1000,
            var_name: None,
            type_name: None,
            thread_id: 1,
            call_stack_hash: None,
            module_path: None,
            stack_ptr: None,
        };

        let cloned = alloc.clone();
        assert_eq!(cloned.size, alloc.size);
    }

    #[test]
    fn test_active_allocation_debug() {
        let alloc = ActiveAllocation {
            ptr: Some(0x1000),
            size: 1024,
            kind: TrackKind::HeapOwner {
                ptr: 0x1000,
                size: 1024,
            },
            allocated_at: 1000,
            var_name: None,
            type_name: None,
            thread_id: 1,
            call_stack_hash: None,
            module_path: None,
            stack_ptr: None,
        };

        let debug_str = format!("{:?}", alloc);
        assert!(debug_str.contains("ActiveAllocation"));
    }

    #[test]
    fn test_memory_stats_default() {
        let stats = MemoryStats::default();
        assert_eq!(stats.total_allocations, 0);
        assert_eq!(stats.total_reallocations, 0);
        assert_eq!(stats.total_deallocations, 0);
        assert_eq!(stats.active_allocations, 0);
        assert_eq!(stats.current_memory, 0);
        assert_eq!(stats.peak_memory, 0);
    }

    #[test]
    fn test_memory_stats_clone() {
        let stats = MemoryStats {
            total_allocations: 100,
            total_reallocations: 10,
            total_deallocations: 50,
            unmatched_deallocations: 2,
            active_allocations: 50,
            total_allocated: 1024 * 1024,
            total_deallocated: 512 * 1024,
            current_memory: 512 * 1024,
            peak_memory: 1024 * 1024,
        };

        let cloned = stats.clone();
        assert_eq!(cloned.total_allocations, 100);
        assert_eq!(cloned.peak_memory, 1024 * 1024);
    }

    #[test]
    fn test_thread_memory_stats_default() {
        let stats = ThreadMemoryStats::default();
        assert_eq!(stats.thread_id, 0);
        assert_eq!(stats.allocation_count, 0);
        assert_eq!(stats.current_memory, 0);
    }

    #[test]
    fn test_thread_memory_stats_clone() {
        let stats = ThreadMemoryStats {
            thread_id: 1,
            allocation_count: 50,
            total_allocated: 4096,
            total_deallocated: 2048,
            current_memory: 2048,
            peak_memory: 4096,
        };

        let cloned = stats.clone();
        assert_eq!(cloned.thread_id, 1);
        assert_eq!(cloned.allocation_count, 50);
    }

    #[test]
    fn test_memory_snapshot_active_count() {
        let mut snapshot = MemorySnapshot::new();
        assert_eq!(snapshot.active_count(), 0);

        snapshot.active_allocations.insert(
            0x1000,
            ActiveAllocation {
                ptr: Some(0x1000),
                size: 1024,
                kind: TrackKind::HeapOwner {
                    ptr: 0x1000,
                    size: 1024,
                },
                allocated_at: 1000,
                var_name: None,
                type_name: None,
                thread_id: 1,
                call_stack_hash: None,
                module_path: None,
                stack_ptr: Some(0x2000), // Add stack_ptr field
            },
        );

        assert_eq!(snapshot.active_count(), 1);
    }

    #[test]
    fn test_memory_snapshot_current_memory() {
        let mut snapshot = MemorySnapshot::new();
        assert_eq!(snapshot.current_memory(), 0);

        snapshot.stats.current_memory = 4096;
        assert_eq!(snapshot.current_memory(), 4096);
    }

    #[test]
    fn test_memory_snapshot_peak_memory() {
        let mut snapshot = MemorySnapshot::new();
        assert_eq!(snapshot.peak_memory(), 0);

        snapshot.stats.peak_memory = 8192;
        assert_eq!(snapshot.peak_memory(), 8192);
    }

    #[test]
    fn test_memory_snapshot_serialization() {
        let snapshot = MemorySnapshot::new();

        let json = serde_json::to_string(&snapshot);
        assert!(json.is_ok());

        let deserialized: Result<MemorySnapshot, _> = serde_json::from_str(&json.unwrap());
        assert!(deserialized.is_ok());
    }

    #[test]
    fn test_active_allocation_serialization() {
        let alloc = ActiveAllocation {
            ptr: Some(0x1000),
            size: 1024,
            kind: TrackKind::HeapOwner {
                ptr: 0x1000,
                size: 1024,
            },
            allocated_at: 1000,
            var_name: Some("test".to_string()),
            type_name: Some("i32".to_string()),
            thread_id: 1,
            call_stack_hash: Some(12345),
            module_path: Some("test_module".to_string()),
            stack_ptr: None,
        };

        let json = serde_json::to_string(&alloc);
        assert!(json.is_ok());

        let deserialized: Result<ActiveAllocation, _> = serde_json::from_str(&json.unwrap());
        assert!(deserialized.is_ok());
    }

    #[test]
    fn test_memory_stats_serialization() {
        let stats = MemoryStats {
            total_allocations: 100,
            total_reallocations: 10,
            total_deallocations: 50,
            unmatched_deallocations: 2,
            active_allocations: 50,
            total_allocated: 1024,
            total_deallocated: 512,
            current_memory: 512,
            peak_memory: 1024,
        };

        let json = serde_json::to_string(&stats);
        assert!(json.is_ok());

        let deserialized: Result<MemoryStats, _> = serde_json::from_str(&json.unwrap());
        assert!(deserialized.is_ok());
    }
}