Skip to main content

memscope_rs/capture/types/
timeline.rs

1//! Timeline tracking types.
2//!
3//! This module contains types for timeline visualization,
4//! including memory snapshots, allocation events, and time ranges.
5
6use serde::{Deserialize, Serialize};
7
8use super::scope::{AllocationEventType, ScopeEventType};
9use super::stats::TypeMemoryUsage;
10
11/// Timeline data for visualization.
12#[derive(Debug, Clone, Serialize)]
13pub struct TimelineData {
14    /// Time range.
15    pub time_range: TimeRange,
16    /// Allocation events.
17    pub allocation_events: Vec<AllocationEvent>,
18    /// Scope events.
19    pub scope_events: Vec<ScopeEvent>,
20    /// Memory snapshots.
21    pub memory_snapshots: Vec<MemorySnapshot>,
22}
23
24/// Time range for timeline visualization.
25#[derive(Debug, Clone, Serialize)]
26pub struct TimeRange {
27    /// Start time.
28    pub start_time: u64,
29    /// End time.
30    pub end_time: u64,
31    /// Duration in milliseconds.
32    pub duration_ms: u64,
33}
34
35/// Memory snapshot at a point in time.
36#[derive(Debug, Clone, Serialize)]
37pub struct MemorySnapshot {
38    /// Timestamp.
39    pub timestamp: u64,
40    /// Total memory.
41    pub total_memory: usize,
42    /// Active allocations.
43    pub active_allocations: usize,
44    /// Fragmentation ratio.
45    pub fragmentation_ratio: f64,
46    /// Top types by memory usage.
47    pub top_types: Vec<TypeMemoryUsage>,
48}
49
50/// Allocation event for timeline.
51#[derive(Debug, Clone, Serialize)]
52pub struct AllocationEvent {
53    /// Timestamp.
54    pub timestamp: u64,
55    /// Event type.
56    pub event_type: AllocationEventType,
57    /// Memory pointer address.
58    pub ptr: usize,
59    /// Size in bytes.
60    pub size: usize,
61    /// Variable name.
62    pub var_name: Option<String>,
63    /// Type name.
64    pub type_name: Option<String>,
65}
66
67/// Scope event for timeline.
68#[derive(Debug, Clone, Serialize)]
69pub struct ScopeEvent {
70    /// Timestamp.
71    pub timestamp: u64,
72    /// Event type.
73    pub event_type: ScopeEventType,
74    /// Scope name.
75    pub scope_name: String,
76    /// Memory usage.
77    pub memory_usage: usize,
78    /// Variable count.
79    pub variable_count: usize,
80}
81
82/// Stack trace data for analysis.
83#[derive(Debug, Clone, Serialize)]
84pub struct StackTraceData {
85    /// Memory allocation hotspots.
86    pub hotspots: Vec<StackTraceHotspot>,
87    /// Detected allocation patterns.
88    pub allocation_patterns: Vec<AllocationPattern>,
89    /// Total number of samples.
90    pub total_samples: usize,
91}
92
93/// Stack trace hotspot.
94#[derive(Debug, Clone, Serialize)]
95pub struct StackTraceHotspot {
96    /// Function name.
97    pub function_name: String,
98    /// Number of allocations.
99    pub allocation_count: usize,
100    /// Total bytes allocated.
101    pub total_bytes: usize,
102    /// Average allocation size.
103    pub average_size: f64,
104    /// Percentage of total allocations.
105    pub percentage: f64,
106}
107
108/// Allocation pattern analysis.
109#[derive(Debug, Clone, Serialize)]
110pub struct AllocationPattern {
111    /// Pattern type.
112    pub pattern_type: String,
113    /// Frequency of occurrence.
114    pub frequency: usize,
115    /// Total bytes allocated.
116    pub total_bytes: usize,
117    /// Description.
118    pub description: String,
119}
120
121/// Stack frame for stack traces.
122#[derive(Debug, Clone, Serialize, Deserialize)]
123pub struct StackFrame {
124    /// Function name.
125    pub function_name: String,
126    /// Source file name.
127    pub file_name: Option<String>,
128    /// Line number in source code.
129    pub line_number: Option<u32>,
130    /// Module path.
131    pub module_path: Option<String>,
132}
133
134/// Safety violation types.
135#[derive(Debug, Clone, Serialize)]
136pub enum SafetyViolation {
137    /// Potential memory leak detected.
138    PotentialLeak {
139        /// Memory pointer.
140        ptr: usize,
141        /// Size in bytes.
142        size: usize,
143        /// Age in milliseconds.
144        age_ms: u64,
145        /// Description.
146        description: String,
147    },
148    /// Use after free violation detected.
149    UseAfterFree {
150        /// Memory pointer.
151        ptr: usize,
152        /// Description.
153        description: String,
154    },
155    /// Double free violation detected.
156    DoubleFree {
157        /// Memory pointer.
158        ptr: usize,
159        /// Description.
160        description: String,
161    },
162    /// Buffer overflow detected.
163    BufferOverflow {
164        /// Memory pointer.
165        ptr: usize,
166        /// Size in bytes.
167        size: usize,
168        /// Description.
169        description: String,
170    },
171}
172
173/// Allocation hotspot information.
174#[derive(Debug, Clone, Serialize)]
175pub struct AllocationHotspot {
176    /// Location information.
177    pub location: HotspotLocation,
178    /// Number of allocations.
179    pub allocation_count: usize,
180    /// Total bytes allocated.
181    pub total_bytes: usize,
182    /// Average allocation size.
183    pub average_size: f64,
184    /// Frequency of occurrence.
185    pub frequency: f64,
186}
187
188/// Hotspot location information.
189#[derive(Debug, Clone, Serialize)]
190pub struct HotspotLocation {
191    /// Function name.
192    pub function_name: String,
193    /// File path.
194    pub file_path: Option<String>,
195    /// Line number.
196    pub line_number: Option<u32>,
197    /// Module path.
198    pub module_path: Option<String>,
199}
200
201#[cfg(test)]
202mod tests {
203    use super::*;
204
205    #[test]
206    fn test_timeline_data() {
207        let timeline = TimelineData {
208            time_range: TimeRange {
209                start_time: 0,
210                end_time: 1000,
211                duration_ms: 1000,
212            },
213            allocation_events: vec![],
214            scope_events: vec![],
215            memory_snapshots: vec![],
216        };
217
218        assert_eq!(timeline.time_range.duration_ms, 1000);
219    }
220
221    #[test]
222    fn test_memory_snapshot() {
223        let snapshot = MemorySnapshot {
224            timestamp: 100,
225            total_memory: 1024 * 1024,
226            active_allocations: 10,
227            fragmentation_ratio: 0.1,
228            top_types: vec![],
229        };
230
231        assert_eq!(snapshot.active_allocations, 10);
232    }
233
234    #[test]
235    fn test_safety_violation() {
236        let violation = SafetyViolation::PotentialLeak {
237            ptr: 0x1000,
238            size: 1024,
239            age_ms: 5000,
240            description: "test leak".to_string(),
241        };
242
243        assert!(matches!(violation, SafetyViolation::PotentialLeak { .. }));
244    }
245}