Debian Codegraph @

pyinstrument/renderers/pstatsrenderer.py

raw symbols SCIP
1 from __future__ import annotations
2
3 import marshal
4 from typing import Any, Dict, Tuple
5
6 from pyinstrument import processors
7 from pyinstrument.frame import Frame
8 from pyinstrument.renderers.base import FrameRenderer, ProcessorList
9 from pyinstrument.session import Session
10
11 # pyright: strict
12
13 FrameKey = Tuple[str, int, str]
14 CallerValue = Tuple[float, int, float, float]
15 FrameValue = Tuple[float, int, float, float, Dict[FrameKey, CallerValue]]
16 StatsDict = Dict[FrameKey, FrameValue]
17
18
19 class PstatsRenderer(FrameRenderer):
20 """
21 Outputs a marshaled dict, containing processed frames in pstat format,
22 suitable for processing by gprof2dot and snakeviz.
23 """
24
25 output_file_extension = "pstats"
26 output_is_binary = True
27
28 def __init__(self, **kwargs: Any):
29 super().__init__(**kwargs)
30
31 def frame_key(self, frame: Frame) -> FrameKey:
32 return (frame.file_path or "", frame.line_no or 0, frame.function)
33
34 def render_frame(self, frame: Frame | None, stats: StatsDict) -> None:
35 if frame is None:
36 return
37
38 key = self.frame_key(frame)
39
40 if key not in stats:
41 # create a new entry
42 # being a statistical profiler, we don't know the exact call time or
43 # number of calls, they're stubbed out
44 call_time = -1
45 number_calls = -1
46 total_time = 0
47 cumulative_time = 0
48 callers: dict[FrameKey, CallerValue] = {}
49 else:
50 call_time, number_calls, total_time, cumulative_time, callers = stats[key]
51
52 # update the total time and cumulative time
53 total_time += frame.total_self_time
54 cumulative_time += frame.time
55
56 if frame.parent:
57 parent_key = self.frame_key(frame.parent)
58 if parent_key not in callers:
59 p_call_time = -1
60 p_number_calls = -1
61 p_total_time = 0
62 p_cumulative_time = 0
63 else:
64 p_call_time, p_number_calls, p_total_time, p_cumulative_time = callers[parent_key]
65
66 p_total_time += frame.total_self_time
67 p_cumulative_time += frame.time
68
69 callers[parent_key] = p_call_time, p_number_calls, p_total_time, p_cumulative_time
70
71 stats[key] = (call_time, number_calls, total_time, cumulative_time, callers)
72
73 for child in frame.children:
74 if not child.is_synthetic:
75 self.render_frame(child, stats)
76
77 def render(self, session: Session):
78 frame = self.preprocess(session.root_frame())
79
80 stats: StatsDict = {}
81 self.render_frame(frame, stats)
82
83 # marshal.dumps returns bytes, so we need to decode it to a string
84 # using surrogateescape
85 return marshal.dumps(stats).decode(encoding="utf-8", errors="surrogateescape")
86
87 def default_processors(self) -> ProcessorList:
88 return [
89 processors.remove_importlib,
90 processors.remove_tracebackhide,
91 processors.merge_consecutive_self_time,
92 processors.aggregate_repeated_calls,
93 processors.remove_irrelevant_nodes,
94 processors.remove_unnecessary_self_time_nodes,
95 processors.remove_first_pyinstrument_frames_processor,
96 ]