memscope_rs/capture/types/
scope.rs1use serde::{Deserialize, Serialize};
7use std::collections::HashMap;
8
9pub use super::allocation::{ImpactLevel, RiskDistribution};
11
12#[derive(Debug, Clone, Serialize)]
14pub struct ScopeInfo {
15 pub name: String,
17 pub parent: Option<String>,
19 pub children: Vec<String>,
21 pub depth: usize,
23 pub variables: Vec<String>,
25 pub total_memory: usize,
27 pub peak_memory: usize,
29 pub allocation_count: usize,
31 pub lifetime_start: Option<u64>,
33 pub lifetime_end: Option<u64>,
35 pub is_active: bool,
37 pub start_time: u64,
39 pub end_time: Option<u64>,
41 pub memory_usage: usize,
43 pub child_scopes: Vec<String>,
45 pub parent_scope: Option<String>,
47}
48
49#[derive(Debug, Clone, Default, Serialize)]
51pub struct ScopeHierarchy {
52 pub root_scopes: Vec<String>,
54 pub scope_tree: HashMap<String, ScopeInfo>,
56 pub max_depth: usize,
58 pub total_scopes: usize,
60 pub relationships: HashMap<String, Vec<String>>,
62 pub depth_map: HashMap<String, usize>,
64}
65
66#[derive(Debug, Clone, Serialize, Deserialize)]
68pub struct TypeLifecyclePattern {
69 pub type_name: String,
71 pub average_lifetime_ms: f64,
73 pub typical_size: usize,
75 pub growth_pattern: String,
77 pub risk_level: String,
79 pub instance_count: usize,
81}
82
83#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
85pub enum GrowthReason {
86 Initial,
88 Expansion,
90 Reallocation,
92 Optimization,
94 UserRequested,
96}
97
98#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
100pub enum AllocationEventType {
101 Allocate,
103 Deallocate,
105 Reallocate,
107 Move,
109 Borrow,
111 Return,
113}
114
115#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
117pub enum ScopeEventType {
118 Enter,
120 Exit,
122 Create,
124 Destroy,
126}
127
128#[derive(Debug, Clone, Serialize)]
130pub struct GrowthEvent {
131 pub timestamp: u64,
133 pub old_size: usize,
135 pub new_size: usize,
137 pub growth_factor: f64,
139 pub reason: GrowthReason,
141 pub var_name: String,
143}
144
145#[derive(Debug, Clone, Serialize)]
147pub struct BorrowEvent {
148 pub timestamp: u64,
150 pub ptr: usize,
152 pub borrow_type: String,
154 pub duration_ms: u64,
156 pub var_name: String,
158}
159
160#[derive(Debug, Clone, Serialize)]
162pub struct MoveEvent {
163 pub timestamp: u64,
165 pub from_ptr: usize,
167 pub to_ptr: usize,
169 pub size: usize,
171 pub var_name: String,
173}
174
175#[derive(Debug, Clone, Serialize)]
177pub struct VariableRelationship {
178 pub source_var: String,
180 pub target_var: String,
182 pub relationship_type: String,
184 pub strength: f64,
186}
187
188#[derive(Debug, Clone, Serialize)]
190pub struct PotentialLeak {
191 pub ptr: usize,
193 pub size: usize,
195 pub age_ms: u64,
197 pub var_name: Option<String>,
199 pub type_name: Option<String>,
201 pub severity: String,
203}
204
205#[derive(Debug, Clone, Default, Serialize)]
207pub struct ScopeAnalysis {
208 pub total_scopes: usize,
210 pub active_scopes: usize,
212 pub max_depth: usize,
214 pub average_lifetime: f64,
216 pub memory_efficiency: f64,
218 pub scopes: Vec<ScopeInfo>,
220 pub scope_hierarchy: ScopeHierarchy,
222 pub cross_scope_references: Vec<String>,
224}
225
226#[derive(Debug, Clone, Default, Serialize, Deserialize)]
228pub struct ScopeLifecycleMetrics {
229 pub scope_name: String,
231 pub variable_count: usize,
233 pub average_lifetime_ms: f64,
235 pub total_memory_usage: usize,
237 pub peak_memory_usage: usize,
239 pub allocation_frequency: f64,
241 pub deallocation_efficiency: f64,
243 pub completed_allocations: usize,
245 pub memory_growth_events: usize,
247 pub peak_concurrent_variables: usize,
249 pub memory_efficiency_ratio: f64,
251 pub ownership_transfer_events: usize,
253 pub fragmentation_score: f64,
255 pub instant_allocations: usize,
257 pub short_term_allocations: usize,
259 pub medium_term_allocations: usize,
261 pub long_term_allocations: usize,
263 pub suspected_leaks: usize,
265 pub risk_distribution: RiskDistribution,
267 pub scope_metrics: Vec<ScopeLifecycleMetrics>,
269 pub type_lifecycle_patterns: Vec<TypeLifecyclePattern>,
271}
272
273#[cfg(test)]
274mod tests {
275 use super::*;
276
277 #[test]
278 fn test_risk_distribution_default() {
279 let risk = RiskDistribution::default();
280
281 assert_eq!(risk.low_risk, 0);
282 assert_eq!(risk.medium_risk, 0);
283 assert_eq!(risk.high_risk, 0);
284 assert_eq!(risk.critical_risk, 0);
285 }
286
287 #[test]
288 fn test_allocation_event_type_variants() {
289 let events = vec![
290 AllocationEventType::Allocate,
291 AllocationEventType::Deallocate,
292 AllocationEventType::Reallocate,
293 AllocationEventType::Move,
294 AllocationEventType::Borrow,
295 AllocationEventType::Return,
296 ];
297
298 for event in events {
299 assert!(!format!("{event:?}").is_empty());
300 }
301 }
302
303 #[test]
304 fn test_scope_event_type_variants() {
305 let events = vec![
306 ScopeEventType::Enter,
307 ScopeEventType::Exit,
308 ScopeEventType::Create,
309 ScopeEventType::Destroy,
310 ];
311
312 for event in events {
313 assert!(!format!("{event:?}").is_empty());
314 }
315 }
316
317 #[test]
318 fn test_growth_reason_variants() {
319 let reasons = vec![
320 GrowthReason::Initial,
321 GrowthReason::Expansion,
322 GrowthReason::Reallocation,
323 GrowthReason::Optimization,
324 GrowthReason::UserRequested,
325 ];
326
327 for reason in reasons {
328 assert!(!format!("{reason:?}").is_empty());
329 }
330 }
331}