memscope_rs/metadata/
scope.rs1use serde::{Deserialize, Serialize};
12use std::collections::HashMap;
13use std::sync::{Arc, Mutex};
14use tracing::debug;
15
16pub type ScopeId = u64;
18
19#[derive(Debug, Clone, Serialize, Deserialize)]
21pub struct ScopeInfo {
22 pub name: String,
24 pub parent: Option<String>,
26 pub depth: usize,
28 pub variables: Vec<String>,
30 pub total_memory: usize,
32 pub peak_memory: usize,
34 pub allocation_count: usize,
36 pub lifetime_start: Option<u64>,
38 pub lifetime_end: Option<u64>,
40 pub is_active: bool,
42}
43
44#[derive(Debug, Clone, Serialize, Deserialize)]
46pub struct ScopeHierarchy {
47 pub root_scopes: Vec<String>,
49 pub scope_tree: HashMap<String, Vec<String>>,
51 pub max_depth: usize,
53 pub total_scopes: usize,
55}
56
57#[derive(Debug)]
60struct ScopeData {
61 active_scopes: HashMap<ScopeId, ScopeInfo>,
63 scope_stack: HashMap<String, Vec<ScopeId>>,
65 next_scope_id: u64,
67 hierarchy: ScopeHierarchy,
69}
70
71impl ScopeData {
72 fn new() -> Self {
73 Self {
74 active_scopes: HashMap::new(),
75 scope_stack: HashMap::new(),
76 next_scope_id: 1,
77 hierarchy: ScopeHierarchy {
78 root_scopes: Vec::new(),
79 scope_tree: HashMap::new(),
80 max_depth: 0,
81 total_scopes: 0,
82 },
83 }
84 }
85}
86
87#[derive(Debug)]
89pub struct ScopeTracker {
90 data: Arc<Mutex<ScopeData>>,
92}
93
94impl ScopeTracker {
95 pub fn new() -> Self {
97 debug!("Creating new ScopeTracker");
98 Self {
99 data: Arc::new(Mutex::new(ScopeData::new())),
100 }
101 }
102
103 pub fn enter_scope(&self, name: String) -> ScopeId {
105 let thread_id = format!("{:?}", std::thread::current().id());
106 let timestamp = std::time::SystemTime::now()
107 .duration_since(std::time::UNIX_EPOCH)
108 .unwrap_or_default()
109 .as_nanos() as u64;
110
111 let mut data = self.data.lock().expect("Failed to acquire scope_data lock");
113
114 let scope_id = data.next_scope_id;
116 data.next_scope_id += 1;
117
118 let (parent, depth) = data
120 .scope_stack
121 .get(&thread_id)
122 .and_then(|thread_stack| thread_stack.last())
123 .and_then(|&parent_id| data.active_scopes.get(&parent_id))
124 .map(|active| (Some(active.name.clone()), active.depth + 1))
125 .unwrap_or((None, 0));
126
127 let scope_info = ScopeInfo {
128 name: name.clone(),
129 parent: parent.clone(),
130 depth,
131 variables: Vec::new(),
132 total_memory: 0,
133 peak_memory: 0,
134 allocation_count: 0,
135 lifetime_start: Some(timestamp),
136 lifetime_end: None,
137 is_active: true,
138 };
139
140 data.active_scopes.insert(scope_id, scope_info.clone());
142
143 data.scope_stack
145 .entry(thread_id)
146 .or_default()
147 .push(scope_id);
148
149 if let Some(parent_name) = &parent {
151 data.hierarchy
152 .scope_tree
153 .entry(parent_name.clone())
154 .or_default()
155 .push(name.clone());
156 } else if !data.hierarchy.root_scopes.contains(&name) {
157 data.hierarchy.root_scopes.push(name.clone());
158 }
159 data.hierarchy.total_scopes += 1;
160 if depth > data.hierarchy.max_depth {
161 data.hierarchy.max_depth = depth;
162 }
163
164 debug!("Entered scope '{}' with id {}", name, scope_id);
165 scope_id
166 }
167
168 pub fn exit_scope(&self) -> Option<ScopeId> {
170 let thread_id = format!("{:?}", std::thread::current().id());
171 let timestamp = std::time::SystemTime::now()
172 .duration_since(std::time::UNIX_EPOCH)
173 .unwrap_or_default()
174 .as_nanos() as u64;
175
176 let mut data = self.data.lock().expect("Failed to acquire scope_data lock");
177
178 let scope_id = data
179 .scope_stack
180 .get_mut(&thread_id)
181 .and_then(|thread_stack| thread_stack.pop());
182
183 if let Some(scope_id) = scope_id {
184 if let Some(scope) = data.active_scopes.get_mut(&scope_id) {
185 scope.lifetime_end = Some(timestamp);
186 scope.is_active = false;
187 }
188 debug!("Exited scope with id {}", scope_id);
189 Some(scope_id)
190 } else {
191 debug!("No scope to exit for thread {}", thread_id);
192 None
193 }
194 }
195
196 pub fn get_scope(&self, scope_id: ScopeId) -> Option<ScopeInfo> {
198 let data = self.data.lock().expect("Failed to acquire scope_data lock");
199 data.active_scopes.get(&scope_id).cloned()
200 }
201
202 pub fn get_all_scopes(&self) -> Vec<(ScopeId, ScopeInfo)> {
204 let data = self.data.lock().expect("Failed to acquire scope_data lock");
205 data.active_scopes
206 .iter()
207 .map(|(k, v)| (*k, v.clone()))
208 .collect()
209 }
210
211 pub fn get_hierarchy(&self) -> ScopeHierarchy {
213 let data = self.data.lock().expect("Failed to acquire scope_data lock");
214 data.hierarchy.clone()
215 }
216
217 pub fn clear(&self) {
219 let mut data = self.data.lock().expect("Failed to acquire scope_data lock");
220 data.active_scopes.clear();
221 data.scope_stack.clear();
222 data.next_scope_id = 1;
223 data.hierarchy = ScopeHierarchy {
224 root_scopes: Vec::new(),
225 scope_tree: HashMap::new(),
226 max_depth: 0,
227 total_scopes: 0,
228 };
229 debug!("Cleared all scopes");
230 }
231}
232
233impl Default for ScopeTracker {
234 fn default() -> Self {
235 Self::new()
236 }
237}
238
239#[cfg(test)]
240mod tests {
241 use super::*;
242
243 #[test]
244 fn test_scope_tracker_creation() {
245 let tracker = ScopeTracker::new();
246 assert_eq!(tracker.get_all_scopes().len(), 0);
247 }
248
249 #[test]
250 fn test_enter_exit_scope() {
251 let tracker = ScopeTracker::new();
252 let scope_id = tracker.enter_scope("test_scope".to_string());
253 assert!(scope_id > 0);
254
255 let scope = tracker.get_scope(scope_id);
256 assert!(scope.is_some());
257 assert!(scope.unwrap().is_active);
258
259 let exited = tracker.exit_scope();
260 assert!(exited.is_some());
261
262 let scope = tracker.get_scope(scope_id);
263 assert!(scope.is_some());
264 assert!(!scope.unwrap().is_active);
265 }
266
267 #[test]
268 fn test_nested_scopes() {
269 let tracker = ScopeTracker::new();
270 let outer_id = tracker.enter_scope("outer".to_string());
271 let inner_id = tracker.enter_scope("inner".to_string());
272
273 let outer = tracker.get_scope(outer_id).unwrap();
274 let inner = tracker.get_scope(inner_id).unwrap();
275
276 assert_eq!(inner.depth, outer.depth + 1);
277 assert_eq!(inner.parent, Some("outer".to_string()));
278
279 tracker.exit_scope();
280 tracker.exit_scope();
281 }
282
283 #[test]
284 fn test_hierarchy() {
285 let tracker = ScopeTracker::new();
286 tracker.enter_scope("root".to_string());
287 tracker.enter_scope("child1".to_string());
288 tracker.exit_scope();
289 tracker.enter_scope("child2".to_string());
290 tracker.exit_scope();
291 tracker.exit_scope();
292
293 let hierarchy = tracker.get_hierarchy();
294 assert_eq!(hierarchy.root_scopes.len(), 1);
295 assert_eq!(hierarchy.total_scopes, 3);
296 }
297}