@@ -60,18 +60,13 @@ def export(self, filename):
6060
6161 lines .sort (key = lambda x : (- x [1 ], x [0 ]))
6262
63- with open (filename , "w" ,
64- encoding = "utf-8" , errors = "surrogatepass" ) as f :
63+ with open (filename , "w" ) as f :
6564 for stack , count in lines :
6665 f .write (f"{ stack } { count } \n " )
6766 print (f"Collapsed stack output written to { filename } " )
6867 return True
6968
7069
71- # Bounded by the unwinder's maximum captured stack depth (MAX_FRAMES).
72- _FLAMEGRAPH_RECURSION_MARGIN = 2000
73-
74-
7570class FlamegraphCollector (StackTraceCollector ):
7671 def __init__ (self , * args , ** kwargs ):
7772 super ().__init__ (* args , ** kwargs )
@@ -170,41 +165,34 @@ def set_mode(self, mode):
170165 self .stats ["mode" ] = mode
171166
172167 def export (self , filename ):
173- # Converting the call tree recurses to the sampled stack depth.
174- old_limit = sys .getrecursionlimit ()
175- sys .setrecursionlimit (old_limit + _FLAMEGRAPH_RECURSION_MARGIN )
176- try :
177- flamegraph_data = self ._convert_to_flamegraph_format ()
178-
179- # Debug output with string table statistics
180- num_functions = len (flamegraph_data .get ("children" , []))
181- total_time = flamegraph_data .get ("value" , 0 )
182- string_count = len (self ._string_table )
183- s1 = "" if num_functions == 1 else "s"
184- s2 = "" if total_time == 1 else "s"
185- s3 = "" if string_count == 1 else "s"
168+ flamegraph_data = self ._convert_to_flamegraph_format ()
169+
170+ # Debug output with string table statistics
171+ num_functions = len (flamegraph_data .get ("children" , []))
172+ total_time = flamegraph_data .get ("value" , 0 )
173+ string_count = len (self ._string_table )
174+ s1 = "" if num_functions == 1 else "s"
175+ s2 = "" if total_time == 1 else "s"
176+ s3 = "" if string_count == 1 else "s"
177+ print (
178+ f"Flamegraph data: { num_functions } root function{ s1 } , "
179+ f"{ total_time } total sample{ s2 } , "
180+ f"{ string_count } unique string{ s3 } "
181+ )
182+
183+ if num_functions == 0 :
186184 print (
187- f"Flamegraph data: { num_functions } root function{ s1 } , "
188- f"{ total_time } total sample{ s2 } , "
189- f"{ string_count } unique string{ s3 } "
185+ "Warning: No functions found in profiling data. Check if sampling captured any data."
190186 )
187+ return False
191188
192- if num_functions == 0 :
193- print (
194- "Warning: No functions found in profiling data. "
195- "Check if sampling captured any data."
196- )
197- return False
198-
199- html_content = self ._create_flamegraph_html (flamegraph_data )
189+ html_content = self ._create_flamegraph_html (flamegraph_data )
200190
201- with open (filename , "w" , encoding = "utf-8" ) as f :
202- f .write (html_content )
191+ with open (filename , "w" , encoding = "utf-8" ) as f :
192+ f .write (html_content )
203193
204- print (f"Flamegraph saved to: { filename } " )
205- return True
206- finally :
207- sys .setrecursionlimit (old_limit )
194+ print (f"Flamegraph saved to: { filename } " )
195+ return True
208196
209197 @staticmethod
210198 @functools .lru_cache (maxsize = None )
0 commit comments