|
| 1 | +""" |
| 2 | +Debug memory diagnostics -- temporary endpoints for production investigation. |
| 3 | +Gated behind ENABLE_DEBUG=true env var. |
| 4 | +Uses only stdlib: tracemalloc, gc, resource. |
| 5 | +""" |
| 6 | + |
| 7 | +import gc |
| 8 | +import linecache |
| 9 | +import resource |
| 10 | +import tracemalloc |
| 11 | +from collections import Counter |
| 12 | +from typing import Any |
| 13 | + |
| 14 | +_snapshot: tracemalloc.Snapshot | None = None |
| 15 | + |
| 16 | + |
| 17 | +def start_tracemalloc() -> None: |
| 18 | + if not tracemalloc.is_tracing(): |
| 19 | + tracemalloc.start(25) |
| 20 | + |
| 21 | + |
| 22 | +def get_memory_info() -> dict[str, Any]: |
| 23 | + """Current RSS, tracemalloc top allocations, GC stats, top object types.""" |
| 24 | + rusage = resource.getrusage(resource.RUSAGE_SELF) |
| 25 | + rss_mb = rusage.ru_maxrss / (1024 * 1024) # macOS returns bytes, Linux returns KB |
| 26 | + import platform |
| 27 | + |
| 28 | + if platform.system() == "Linux": |
| 29 | + rss_mb = rusage.ru_maxrss / 1024 |
| 30 | + |
| 31 | + result: dict[str, Any] = { |
| 32 | + "rss_max_mb": round(rss_mb, 2), |
| 33 | + "tracemalloc_tracing": tracemalloc.is_tracing(), |
| 34 | + } |
| 35 | + |
| 36 | + if tracemalloc.is_tracing(): |
| 37 | + current, peak = tracemalloc.get_traced_memory() |
| 38 | + result["tracemalloc_current_mb"] = round(current / (1024 * 1024), 2) |
| 39 | + result["tracemalloc_peak_mb"] = round(peak / (1024 * 1024), 2) |
| 40 | + |
| 41 | + snapshot = tracemalloc.take_snapshot() |
| 42 | + snapshot = snapshot.filter_traces( |
| 43 | + ( |
| 44 | + tracemalloc.Filter(False, "<frozen *>"), |
| 45 | + tracemalloc.Filter(False, "<unknown>"), |
| 46 | + tracemalloc.Filter(False, tracemalloc.__file__), |
| 47 | + ) |
| 48 | + ) |
| 49 | + top_stats = snapshot.statistics("lineno")[:20] |
| 50 | + result["top_allocations"] = [ |
| 51 | + { |
| 52 | + "file": str(stat.traceback), |
| 53 | + "size_kb": round(stat.size / 1024, 1), |
| 54 | + "count": stat.count, |
| 55 | + } |
| 56 | + for stat in top_stats |
| 57 | + ] |
| 58 | + |
| 59 | + gc_stats = gc.get_stats() |
| 60 | + result["gc"] = { |
| 61 | + "generations": gc_stats, |
| 62 | + "garbage_count": len(gc.garbage), |
| 63 | + } |
| 64 | + |
| 65 | + type_counts = Counter(type(obj).__name__ for obj in gc.get_objects()) |
| 66 | + result["top_object_types"] = type_counts.most_common(25) |
| 67 | + |
| 68 | + return result |
| 69 | + |
| 70 | + |
| 71 | +def take_snapshot() -> dict[str, str]: |
| 72 | + """Take a tracemalloc snapshot as baseline for future diffs.""" |
| 73 | + global _snapshot |
| 74 | + if not tracemalloc.is_tracing(): |
| 75 | + return {"error": "tracemalloc is not tracing, set ENABLE_DEBUG=true"} |
| 76 | + _snapshot = tracemalloc.take_snapshot() |
| 77 | + _snapshot = _snapshot.filter_traces( |
| 78 | + ( |
| 79 | + tracemalloc.Filter(False, "<frozen *>"), |
| 80 | + tracemalloc.Filter(False, "<unknown>"), |
| 81 | + ) |
| 82 | + ) |
| 83 | + return {"status": "snapshot taken"} |
| 84 | + |
| 85 | + |
| 86 | +def get_diff() -> dict[str, Any]: |
| 87 | + """Compare current allocations to the last snapshot.""" |
| 88 | + global _snapshot |
| 89 | + if _snapshot is None: |
| 90 | + return {"error": "no baseline snapshot -- call /debug/memory/snapshot first"} |
| 91 | + if not tracemalloc.is_tracing(): |
| 92 | + return {"error": "tracemalloc is not tracing"} |
| 93 | + |
| 94 | + current = tracemalloc.take_snapshot() |
| 95 | + current = current.filter_traces( |
| 96 | + ( |
| 97 | + tracemalloc.Filter(False, "<frozen *>"), |
| 98 | + tracemalloc.Filter(False, "<unknown>"), |
| 99 | + ) |
| 100 | + ) |
| 101 | + diff_stats = current.compare_to(_snapshot, "lineno")[:30] |
| 102 | + |
| 103 | + # Clear linecache to avoid stale data |
| 104 | + linecache.clearcache() |
| 105 | + |
| 106 | + return { |
| 107 | + "diff_since_snapshot": [ |
| 108 | + { |
| 109 | + "file": str(stat.traceback), |
| 110 | + "size_diff_kb": round(stat.size_diff / 1024, 1), |
| 111 | + "size_kb": round(stat.size / 1024, 1), |
| 112 | + "count_diff": stat.count_diff, |
| 113 | + "count": stat.count, |
| 114 | + } |
| 115 | + for stat in diff_stats |
| 116 | + if stat.size_diff > 0 |
| 117 | + ] |
| 118 | + } |
0 commit comments