Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
57 changes: 34 additions & 23 deletions Lib/profiling/sampling/stack_collector.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,10 @@ def export(self, filename):
return True


# Bounded by the unwinder's maximum captured stack depth (MAX_FRAMES).
_FLAMEGRAPH_RECURSION_MARGIN = 2000


class FlamegraphCollector(StackTraceCollector):
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
Expand Down Expand Up @@ -166,34 +170,41 @@ def set_mode(self, mode):
self.stats["mode"] = mode

def export(self, filename):
flamegraph_data = self._convert_to_flamegraph_format()

# Debug output with string table statistics
num_functions = len(flamegraph_data.get("children", []))
total_time = flamegraph_data.get("value", 0)
string_count = len(self._string_table)
s1 = "" if num_functions == 1 else "s"
s2 = "" if total_time == 1 else "s"
s3 = "" if string_count == 1 else "s"
print(
f"Flamegraph data: {num_functions} root function{s1}, "
f"{total_time} total sample{s2}, "
f"{string_count} unique string{s3}"
)

if num_functions == 0:
# Converting the call tree recurses to the sampled stack depth.
old_limit = sys.getrecursionlimit()
sys.setrecursionlimit(old_limit + _FLAMEGRAPH_RECURSION_MARGIN)
try:
flamegraph_data = self._convert_to_flamegraph_format()

# Debug output with string table statistics
num_functions = len(flamegraph_data.get("children", []))
total_time = flamegraph_data.get("value", 0)
string_count = len(self._string_table)
s1 = "" if num_functions == 1 else "s"
s2 = "" if total_time == 1 else "s"
s3 = "" if string_count == 1 else "s"
print(
"Warning: No functions found in profiling data. Check if sampling captured any data."
f"Flamegraph data: {num_functions} root function{s1}, "
f"{total_time} total sample{s2}, "
f"{string_count} unique string{s3}"
)
return False

html_content = self._create_flamegraph_html(flamegraph_data)
if num_functions == 0:
print(
"Warning: No functions found in profiling data. "
"Check if sampling captured any data."
)
return False

with open(filename, "w", encoding="utf-8") as f:
f.write(html_content)
html_content = self._create_flamegraph_html(flamegraph_data)

print(f"Flamegraph saved to: {filename}")
return True
with open(filename, "w", encoding="utf-8") as f:
f.write(html_content)

print(f"Flamegraph saved to: {filename}")
return True
finally:
sys.setrecursionlimit(old_limit)

@staticmethod
@functools.lru_cache(maxsize=None)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,11 @@
import marshal
import opcode
import os
import sys
import tempfile
import unittest

from test.support import is_emscripten
from test.support import is_emscripten, set_recursion_limit

try:
import _remote_debugging # noqa: F401
Expand Down Expand Up @@ -610,6 +611,38 @@ def test_flamegraph_collector_empty_export_fails(self):
self.assertFalse(export_ok)
self.assertEqual(os.path.getsize(flamegraph_out.name), 0)

def test_flamegraph_deep_stack_export(self):
flamegraph_out = tempfile.NamedTemporaryFile(
suffix=".html", delete=False
)
self.addCleanup(close_and_unlink, flamegraph_out)

collector = FlamegraphCollector(1000)
# Deeper than the default recursion limit.
frames = [MockFrameInfo("f.py", i + 1, f"f{i}") for i in range(1536)]
collector.collect(
[MockInterpreterInfo(0, [MockThreadInfo(1, frames)])])

with set_recursion_limit(1000), captured_stdout(), captured_stderr():
export_ok = collector.export(flamegraph_out.name)
self.assertEqual(sys.getrecursionlimit(), 1000)

self.assertTrue(export_ok)
self.assertGreater(os.path.getsize(flamegraph_out.name), 0)

def test_flamegraph_export_restores_recursion_limit(self):
collector = FlamegraphCollector(1000)
frame = MockFrameInfo("f.py", 1, "f")
with set_recursion_limit(500), captured_stdout(), captured_stderr():
self.assertFalse(collector.export(None))
self.assertEqual(sys.getrecursionlimit(), 500)
collector.collect([
MockInterpreterInfo(0, [MockThreadInfo(1, [
frame, MockFrameInfo("f.py", 2, "caller")])])])
with self.assertRaises(TypeError):
collector.export(None)
self.assertEqual(sys.getrecursionlimit(), 500)

def test_gecko_collector_basic(self):
"""Test basic GeckoCollector functionality."""
collector = GeckoCollector(1000)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Fix the sampling profiler's flamegraph export so that profiling a deeply
recursive program no longer fails with :exc:`RecursionError` instead of
producing a flamegraph. Patch by tonghuaroot.
Loading