1use super::allocation::{AllocationStats, AllocationTracker, SizeHistogram};
7use crate::memory::{PoolStats, GLOBAL_BUFFER_POOL, GLOBAL_STRING_POOL};
8use std::time::{Duration, Instant, SystemTime, UNIX_EPOCH};
9
10#[derive(Debug, Clone)]
16pub struct MemorySnapshot {
17 pub timestamp: u64,
19 pub instant: Instant,
21 pub stats: AllocationStats,
23 pub histogram: SizeHistogram,
25 pub pools: PoolSnapshot,
27 pub label: String,
29}
30
31impl MemorySnapshot {
32 pub fn capture(tracker: &AllocationTracker) -> Self {
34 Self {
35 timestamp: SystemTime::now()
36 .duration_since(UNIX_EPOCH)
37 .map(|d| d.as_millis() as u64)
38 .unwrap_or(0),
39 instant: Instant::now(),
40 stats: tracker.stats(),
41 histogram: tracker.histogram(),
42 pools: PoolSnapshot::capture(),
43 label: String::new(),
44 }
45 }
46
47 pub fn capture_labeled(tracker: &AllocationTracker, label: impl Into<String>) -> Self {
49 let mut snap = Self::capture(tracker);
50 snap.label = label.into();
51 snap
52 }
53
54 pub fn diff(&self, other: &MemorySnapshot) -> SnapshotDiff {
56 SnapshotDiff {
57 time_delta: self
58 .instant
59 .checked_duration_since(other.instant)
60 .unwrap_or_default(),
61 allocations_delta: self.stats.total_allocations as i64
62 - other.stats.total_allocations as i64,
63 deallocations_delta: self.stats.total_deallocations as i64
64 - other.stats.total_deallocations as i64,
65 bytes_delta: self.stats.current_bytes as i64 - other.stats.current_bytes as i64,
66 peak_delta: self.stats.peak_bytes as i64 - other.stats.peak_bytes as i64,
67 string_pool_delta: self.pools.string_pool.count as i64
68 - other.pools.string_pool.count as i64,
69 buffer_pool_delta: self.pools.buffer_pool_available as i64
70 - other.pools.buffer_pool_available as i64,
71 from_label: other.label.clone(),
72 to_label: self.label.clone(),
73 }
74 }
75
76 pub fn current_bytes(&self) -> usize {
78 self.stats.current_bytes
79 }
80
81 pub fn peak_bytes(&self) -> usize {
83 self.stats.peak_bytes
84 }
85}
86
87#[derive(Debug, Clone, Default)]
93pub struct PoolSnapshot {
94 pub string_pool: PoolStats,
96 pub buffer_pool_available: usize,
98}
99
100impl PoolSnapshot {
101 pub fn capture() -> Self {
103 Self {
104 string_pool: GLOBAL_STRING_POOL.stats(),
105 buffer_pool_available: GLOBAL_BUFFER_POOL.available(),
106 }
107 }
108}
109
110#[derive(Debug, Clone)]
116pub struct SnapshotDiff {
117 pub time_delta: Duration,
119 pub allocations_delta: i64,
121 pub deallocations_delta: i64,
123 pub bytes_delta: i64,
125 pub peak_delta: i64,
127 pub string_pool_delta: i64,
129 pub buffer_pool_delta: i64,
131 pub from_label: String,
133 pub to_label: String,
135}
136
137impl SnapshotDiff {
138 pub fn has_leaks(&self) -> bool {
140 self.bytes_delta > 0 && self.allocations_delta > self.deallocations_delta
141 }
142
143 pub fn significant_growth(&self, from_bytes: usize) -> bool {
145 if from_bytes == 0 {
146 return self.bytes_delta > 0;
147 }
148 (self.bytes_delta as f64 / from_bytes as f64).abs() > 0.1
149 }
150
151 pub fn net_allocations(&self) -> i64 {
153 self.allocations_delta - self.deallocations_delta
154 }
155
156 pub fn report(&self) -> String {
158 let mut s = String::new();
159
160 if !self.from_label.is_empty() || !self.to_label.is_empty() {
162 s.push_str(&format!(
163 "=== Snapshot Diff: '{}' -> '{}' ===\n",
164 if self.from_label.is_empty() {
165 "start"
166 } else {
167 &self.from_label
168 },
169 if self.to_label.is_empty() {
170 "end"
171 } else {
172 &self.to_label
173 },
174 ));
175 } else {
176 s.push_str("=== Snapshot Diff ===\n");
177 }
178
179 s.push_str(&format!("Time elapsed: {:?}\n\n", self.time_delta));
180
181 s.push_str("Allocations:\n");
183 s.push_str(&format!(
184 " New allocations: {:+}\n",
185 self.allocations_delta
186 ));
187 s.push_str(&format!(
188 " New deallocations: {:+}\n",
189 self.deallocations_delta
190 ));
191 s.push_str(&format!(
192 " Net allocations: {:+}\n",
193 self.net_allocations()
194 ));
195
196 s.push_str("\nMemory:\n");
198 let bytes_str = if self.bytes_delta >= 0 {
199 format!("+{} bytes (+{:.2} KB)", self.bytes_delta, self.bytes_delta as f64 / 1024.0)
200 } else {
201 format!("{} bytes ({:.2} KB)", self.bytes_delta, self.bytes_delta as f64 / 1024.0)
202 };
203 s.push_str(&format!(" Current bytes: {}\n", bytes_str));
204
205 let peak_str = if self.peak_delta >= 0 {
206 format!("+{}", self.peak_delta)
207 } else {
208 format!("{}", self.peak_delta)
209 };
210 s.push_str(&format!(" Peak bytes: {}\n", peak_str));
211
212 s.push_str("\nPools:\n");
214 s.push_str(&format!(
215 " String pool entries: {:+}\n",
216 self.string_pool_delta
217 ));
218 s.push_str(&format!(
219 " Buffer pool available: {:+}\n",
220 self.buffer_pool_delta
221 ));
222
223 s.push_str("\nAssessment:\n");
225 if self.has_leaks() {
226 s.push_str(" ⚠️ Potential memory leak detected!\n");
227 s.push_str(&format!(
228 " {} bytes held across {} net allocations\n",
229 self.bytes_delta,
230 self.net_allocations()
231 ));
232 } else if self.bytes_delta > 0 {
233 s.push_str(" ⚡ Memory increased (may be normal caching)\n");
234 } else if self.bytes_delta < 0 {
235 s.push_str(" ✅ Memory decreased (cleanup working)\n");
236 } else {
237 s.push_str(" ✅ No memory change\n");
238 }
239
240 s
241 }
242}
243
244impl std::fmt::Display for SnapshotDiff {
245 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
246 write!(f, "{}", self.report())
247 }
248}
249
250pub struct SnapshotSeries {
256 snapshots: Vec<MemorySnapshot>,
257 max_snapshots: usize,
258}
259
260impl SnapshotSeries {
261 pub fn new(max_snapshots: usize) -> Self {
263 Self {
264 snapshots: Vec::with_capacity(max_snapshots),
265 max_snapshots,
266 }
267 }
268
269 pub fn add(&mut self, snapshot: MemorySnapshot) {
271 if self.snapshots.len() >= self.max_snapshots {
272 self.snapshots.remove(0);
273 }
274 self.snapshots.push(snapshot);
275 }
276
277 pub fn snapshots(&self) -> &[MemorySnapshot] {
279 &self.snapshots
280 }
281
282 pub fn first(&self) -> Option<&MemorySnapshot> {
284 self.snapshots.first()
285 }
286
287 pub fn last(&self) -> Option<&MemorySnapshot> {
289 self.snapshots.last()
290 }
291
292 pub fn total_diff(&self) -> Option<SnapshotDiff> {
294 match (self.first(), self.last()) {
295 (Some(first), Some(last)) if !std::ptr::eq(first, last) => {
296 Some(last.diff(first))
297 }
298 _ => None,
299 }
300 }
301
302 pub fn has_growth_trend(&self) -> bool {
304 if self.snapshots.len() < 3 {
305 return false;
306 }
307
308 let growing = self
310 .snapshots
311 .windows(2)
312 .filter(|w| w[1].stats.current_bytes > w[0].stats.current_bytes)
313 .count();
314
315 growing as f64 / (self.snapshots.len() - 1) as f64 > 0.7
317 }
318
319 pub fn growth_rate(&self) -> f64 {
321 if let Some(diff) = self.total_diff() {
322 if diff.time_delta.as_secs_f64() > 0.0 {
323 return diff.bytes_delta as f64 / diff.time_delta.as_secs_f64();
324 }
325 }
326 0.0
327 }
328
329 pub fn clear(&mut self) {
331 self.snapshots.clear();
332 }
333}
334
335impl Default for SnapshotSeries {
336 fn default() -> Self {
337 Self::new(100)
338 }
339}
340
341#[cfg(test)]
342mod tests {
343 use super::*;
344 use super::super::allocation::AllocationTracker;
345
346 #[test]
347 fn test_memory_snapshot() {
348 let tracker = AllocationTracker::new();
349 let snapshot = MemorySnapshot::capture(&tracker);
350
351 assert!(snapshot.timestamp > 0);
352 }
353
354 #[test]
355 fn test_snapshot_diff() {
356 let diff = SnapshotDiff {
357 time_delta: Duration::from_secs(10),
358 allocations_delta: 100,
359 deallocations_delta: 80,
360 bytes_delta: 2000,
361 peak_delta: 500,
362 string_pool_delta: 5,
363 buffer_pool_delta: -2,
364 from_label: "start".to_string(),
365 to_label: "end".to_string(),
366 };
367
368 assert_eq!(diff.net_allocations(), 20);
369 assert!(diff.has_leaks());
370 }
371
372 #[test]
373 fn test_snapshot_diff_no_leaks() {
374 let diff = SnapshotDiff {
375 time_delta: Duration::from_secs(10),
376 allocations_delta: 100,
377 deallocations_delta: 100,
378 bytes_delta: 0,
379 peak_delta: 0,
380 string_pool_delta: 0,
381 buffer_pool_delta: 0,
382 from_label: String::new(),
383 to_label: String::new(),
384 };
385
386 assert!(!diff.has_leaks());
387 }
388
389 #[test]
390 fn test_snapshot_series() {
391 let tracker = AllocationTracker::new();
392 let mut series = SnapshotSeries::new(5);
393
394 for i in 0..3 {
395 let mut snap = MemorySnapshot::capture(&tracker);
396 snap.label = format!("snap_{}", i);
397 series.add(snap);
398 }
399
400 assert_eq!(series.snapshots().len(), 3);
401 assert_eq!(series.first().unwrap().label, "snap_0");
402 assert_eq!(series.last().unwrap().label, "snap_2");
403 }
404
405 #[test]
406 fn test_snapshot_series_max_capacity() {
407 let tracker = AllocationTracker::new();
408 let mut series = SnapshotSeries::new(3);
409
410 for i in 0..5 {
411 let mut snap = MemorySnapshot::capture(&tracker);
412 snap.label = format!("snap_{}", i);
413 series.add(snap);
414 }
415
416 assert_eq!(series.snapshots().len(), 3);
417 assert_eq!(series.first().unwrap().label, "snap_2");
418 assert_eq!(series.last().unwrap().label, "snap_4");
419 }
420
421 #[test]
422 fn test_snapshot_diff_report() {
423 let diff = SnapshotDiff {
424 time_delta: Duration::from_secs(10),
425 allocations_delta: 100,
426 deallocations_delta: 80,
427 bytes_delta: 2000,
428 peak_delta: 500,
429 string_pool_delta: 5,
430 buffer_pool_delta: -2,
431 from_label: "before".to_string(),
432 to_label: "after".to_string(),
433 };
434
435 let report = diff.report();
436 assert!(report.contains("before"));
437 assert!(report.contains("after"));
438 assert!(report.contains("+2000 bytes"));
439 assert!(report.contains("Potential memory leak"));
440 }
441}
442