Debian Codegraph @

rpython/jit/metainterp/jitprof.py

raw symbols SCIP
1
2 """ A small helper module for profiling JIT
3 """
4
5 import time
6 from rpython.rlib.debug import debug_print, debug_start, debug_stop
7 from rpython.rlib.debug import have_debug_prints
8 from rpython.jit.metainterp.jitexc import JitException
9 from rpython.rlib.jit import Counters
10
11
12 JITPROF_LINES = Counters.ncounters + 1 + 1
13 # one for TOTAL, 1 for calls, update if needed
14 _CPU_LINES = 4 # the last 4 lines are stored on the cpu
15
16 class BaseProfiler(object):
17 pass
18
19 class EmptyProfiler(BaseProfiler):
20 initialized = True
21
22 def start(self):
23 pass
24
25 def finish(self):
26 pass
27
28 def start_tracing(self):
29 pass
30
31 def end_tracing(self):
32 pass
33
34 def start_backend(self):
35 pass
36
37 def end_backend(self):
38 pass
39
40 def count(self, kind, inc=1):
41 pass
42
43 def count_ops(self, opnum, kind=Counters.OPS):
44 pass
45
46 def get_counter(self, num):
47 return 0
48
49 def get_times(self, num):
50 return 0.0
51
52 class Profiler(BaseProfiler):
53 initialized = False
54 timer = staticmethod(time.time)
55 starttime = 0
56 t1 = 0
57 times = None
58 counters = None
59 calls = 0
60 current = None
61 cpu = None
62
63 def start(self):
64 self.starttime = self.timer()
65 self.t1 = self.starttime
66 self.times = [0, 0]
67 self.counters = [0] * (Counters.ncounters - _CPU_LINES)
68 self.calls = 0
69 self.current = []
70
71 def finish(self):
72 self.tk = self.timer()
73 self.print_stats()
74
75 def _start(self, event):
76 t0 = self.t1
77 self.t1 = self.timer()
78 if self.current:
79 self.times[self.current[-1]] += self.t1 - t0
80 self.counters[event] += 1
81 self.current.append(event)
82
83 def _end(self, event):
84 t0 = self.t1
85 self.t1 = self.timer()
86 if not self.current:
87 debug_print("BROKEN PROFILER DATA!")
88 return
89 ev1 = self.current.pop()
90 if ev1 != event:
91 debug_print("BROKEN PROFILER DATA!")
92 return
93 self.times[ev1] += self.t1 - t0
94
95 def start_tracing(self): self._start(Counters.TRACING)
96 def end_tracing(self): self._end (Counters.TRACING)
97
98 def start_backend(self): self._start(Counters.BACKEND)
99 def end_backend(self): self._end (Counters.BACKEND)
100
101 def count(self, kind, inc=1):
102 self.counters[kind] += inc
103
104 def get_counter(self, num):
105 if num == Counters.TOTAL_COMPILED_LOOPS:
106 return self.cpu.tracker.total_compiled_loops
107 elif num == Counters.TOTAL_COMPILED_BRIDGES:
108 return self.cpu.tracker.total_compiled_bridges
109 elif num == Counters.TOTAL_FREED_LOOPS:
110 return self.cpu.tracker.total_freed_loops
111 elif num == Counters.TOTAL_FREED_BRIDGES:
112 return self.cpu.tracker.total_freed_bridges
113 return self.counters[num]
114
115 def get_times(self, num):
116 return self.times[num]
117
118 def count_ops(self, opnum, kind=Counters.OPS):
119 from rpython.jit.metainterp.resoperation import OpHelpers
120 self.counters[kind] += 1
121 if OpHelpers.is_call(opnum) and kind == Counters.RECORDED_OPS:
122 self.calls += 1
123
124 def print_stats(self):
125 debug_start("jit-summary")
126 if have_debug_prints():
127 self._print_stats()
128 debug_stop("jit-summary")
129
130 def _print_stats(self):
131 cnt = self.counters
132 tim = self.times
133 calls = self.calls
134 self._print_line_time("Tracing", cnt[Counters.TRACING],
135 tim[Counters.TRACING])
136 self._print_line_time("Backend", cnt[Counters.BACKEND],
137 tim[Counters.BACKEND])
138 line = "TOTAL: \t\t%f" % (self.tk - self.starttime, )
139 debug_print(line)
140 self._print_intline("ops", cnt[Counters.OPS])
141 self._print_intline("heapcached ops", cnt[Counters.HEAPCACHED_OPS])
142 self._print_intline("recorded ops", cnt[Counters.RECORDED_OPS])
143 self._print_intline(" calls", calls)
144 self._print_intline("guards", cnt[Counters.GUARDS])
145 self._print_intline("opt ops", cnt[Counters.OPT_OPS])
146 self._print_intline("opt guards", cnt[Counters.OPT_GUARDS])
147 self._print_intline("opt guards shared", cnt[Counters.OPT_GUARDS_SHARED])
148 self._print_intline("forcings", cnt[Counters.OPT_FORCINGS])
149 self._print_intline("abort: trace too long",
150 cnt[Counters.ABORT_TOO_LONG])
151 self._print_intline("abort: compiling", cnt[Counters.ABORT_BRIDGE])
152 self._print_intline("abort: vable escape", cnt[Counters.ABORT_ESCAPE])
153 self._print_intline("abort: bad loop", cnt[Counters.ABORT_BAD_LOOP])
154 self._print_intline("abort: force quasi-immut",
155 cnt[Counters.ABORT_FORCE_QUASIIMMUT])
156 self._print_intline("abort: segmenting trace",
157 cnt[Counters.ABORT_SEGMENTED_TRACE])
158 self._print_intline("virtualizables forced",
159 cnt[Counters.FORCE_VIRTUALIZABLES])
160 self._print_intline("nvirtuals", cnt[Counters.NVIRTUALS])
161 self._print_intline("nvholes", cnt[Counters.NVHOLES])
162 self._print_intline("nvreused", cnt[Counters.NVREUSED])
163 self._print_intline("vecopt tried", cnt[Counters.OPT_VECTORIZE_TRY])
164 self._print_intline("vecopt success", cnt[Counters.OPT_VECTORIZED])
165 cpu = self.cpu
166 if cpu is not None: # for some tests
167 self._print_intline("Total # of loops",
168 cpu.tracker.total_compiled_loops)
169 self._print_intline("Total # of bridges",
170 cpu.tracker.total_compiled_bridges)
171 self._print_intline("Freed # of loops",
172 cpu.tracker.total_freed_loops)
173 self._print_intline("Freed # of bridges",
174 cpu.tracker.total_freed_bridges)
175
176 def _print_line_time(self, string, i, tim):
177 final = "%s:%s\t%d\t%f" % (string, " " * max(0, 13-len(string)), i, tim)
178 debug_print(final)
179
180 def _print_intline(self, string, i):
181 final = string + ':' + " " * max(0, 16-len(string))
182 final += '\t' + str(i)
183 debug_print(final)
184
185
186 class BrokenProfilerData(JitException):
187 pass