Debian Codegraph @

pypy/module/pypyjit/interp_resop.py

raw symbols SCIP
1
2 from pypy.interpreter.typedef import (TypeDef, GetSetProperty,
3 interp_attrproperty, interp_attrproperty_w)
4 from pypy.interpreter.baseobjspace import W_Root
5 from pypy.interpreter.gateway import unwrap_spec, interp2app
6 from pypy.interpreter.pycode import PyCode
7 from pypy.interpreter.error import oefmt
8 from rpython.rtyper.lltypesystem import lltype
9 from rpython.rtyper.annlowlevel import cast_base_ptr_to_instance, hlstr
10 from rpython.rtyper.rclass import OBJECT
11 #from rpython.jit.metainterp.resoperation import rop
12 from rpython.rlib.nonconst import NonConstant
13 from rpython.rlib.rarithmetic import r_uint
14 from rpython.rlib import jit_hooks
15 from rpython.rlib.jit import Counters
16 from rpython.rlib.objectmodel import compute_unique_id
17 from pypy.module.pypyjit.interp_jit import pypyjitdriver
18
19 class Cache(object):
20 in_recursion = False
21 no = 0
22
23 def __init__(self, space):
24 self.w_compile_hook = None
25 self.w_abort_hook = None
26 self.w_trace_too_long_hook = None
27 self.compile_hook_with_ops = False
28
29 def getno(self):
30 self.no += 1
31 return self.no - 1
32
33 def wrap_greenkey(space, jitdriver, greenkey, greenkey_repr):
34 if greenkey is None:
35 return space.w_None
36 jitdriver_name = jitdriver.name
37 if jitdriver_name == 'pypyjit':
38 next_instr = greenkey[0].getint()
39 is_being_profiled = greenkey[1].getint()
40 ll_code = lltype.cast_opaque_ptr(lltype.Ptr(OBJECT),
41 greenkey[2].getref_base())
42 pycode = cast_base_ptr_to_instance(PyCode, ll_code)
43 return space.newtuple([pycode, space.newint(next_instr),
44 space.newbool(bool(is_being_profiled))])
45 else:
46 return space.newtext(greenkey_repr)
47
48 @unwrap_spec(operations=int)
49 def set_compile_hook(space, w_hook, operations=1):
50 """ set_compile_hook(hook, operations=True)
51
52 Set a compiling hook that will be called each time a loop is compiled.
53
54 The hook will be called with the pypyjit.JitLoopInfo object. Refer to it's
55 docstring for details.
56
57 Note that jit hook is not reentrant. It means that if the code
58 inside the jit hook is itself jitted, it will get compiled, but the
59 jit hook won't be called for that.
60 """
61 cache = space.fromcache(Cache)
62 if space.is_w(w_hook, space.w_None):
63 w_hook = None
64 cache.w_compile_hook = w_hook
65 cache.compile_hook_with_ops = bool(operations)
66 cache.in_recursion = NonConstant(False)
67
68 def set_abort_hook(space, w_hook):
69 """ set_abort_hook(hook)
70
71 Set a hook (callable) that will be called each time there is tracing
72 aborted due to some reason.
73
74 The hook will be called with the signature:
75
76 hook(jitdriver_name, greenkey, reason, operations)
77
78 Reason is a string, the meaning of other arguments is the same
79 as attributes on JitLoopInfo object.
80 """
81 cache = space.fromcache(Cache)
82 if space.is_w(w_hook, space.w_None):
83 w_hook = None
84 cache.w_abort_hook = w_hook
85 cache.in_recursion = NonConstant(False)
86
87 def set_trace_too_long_hook(space, w_hook):
88 """ set_trace_too_long_hook(hook)
89
90 Set a hook (callable) that will be called each time we abort
91 tracing because the trace is too long.
92
93 The hook will be called with the signature:
94
95 hook(jitdriver_name, greenkey)
96 """
97 cache = space.fromcache(Cache)
98 if space.is_w(w_hook, space.w_None):
99 w_hook = None
100 cache.w_trace_too_long_hook = w_hook
101 cache.in_recursion = NonConstant(False)
102
103 def wrap_oplist(space, logops, operations, ops_offset=None):
104 # this function is called from the JIT
105 from rpython.jit.metainterp.resoperation import rop
106
107 l_w = []
108 jitdrivers_sd = logops.metainterp_sd.jitdrivers_sd
109 for op in operations:
110 if ops_offset is None:
111 ofs = -1
112 else:
113 ofs = ops_offset.get(op, 0)
114 num = op.getopnum()
115 name = op.getopname()
116 if num == rop.DEBUG_MERGE_POINT:
117 jd_sd = jitdrivers_sd[op.getarg(0).getint()]
118 greenkey = op.getarglist()[3:]
119 repr = jd_sd.warmstate.get_location_str(greenkey)
120 w_greenkey = wrap_greenkey(space, jd_sd.jitdriver, greenkey, repr)
121 l_w.append(DebugMergePoint(space, name,
122 logops.repr_of_resop(op),
123 jd_sd.jitdriver.name,
124 op.getarg(1).getint(),
125 op.getarg(2).getint(),
126 w_greenkey))
127 elif op.is_guard():
128 descr = op.getdescr()
129 if descr is not None: # can be none in on_abort!
130 hash = op.getdescr().get_jitcounter_hash()
131 else:
132 hash = r_uint(0)
133 l_w.append(GuardOp(name, ofs, logops.repr_of_resop(op),
134 hash))
135 else:
136 l_w.append(WrappedOp(name, ofs, logops.repr_of_resop(op)))
137 return l_w
138
139 @unwrap_spec(offset=int, repr='text', name='text')
140 def descr_new_resop(space, w_tp, name, offset=-1, repr=''):
141 return WrappedOp(name, offset, repr)
142
143 @unwrap_spec(offset=int, repr='text', name='text', hash=r_uint)
144 def descr_new_guardop(space, w_tp, name, offset=-1, repr='', hash=r_uint(0)):
145 return GuardOp(name, offset, repr, hash)
146
147 @unwrap_spec(repr='text', name='text', jd_name='text', call_depth=int, call_id=int)
148 def descr_new_dmp(space, w_tp, name, repr, jd_name, call_depth, call_id,
149 w_greenkey):
150
151 return DebugMergePoint(space, name,
152 repr, jd_name, call_depth, call_id, w_greenkey)
153
154
155 class WrappedOp(W_Root):
156 """ A class representing a single ResOperation, wrapped nicely
157 """
158 def __init__(self, name, offset, repr_of_resop):
159 self.offset = offset
160 self.name = name
161 self.repr_of_resop = repr_of_resop
162
163 def descr_repr(self, space):
164 return space.newtext(self.repr_of_resop)
165
166 def descr_name(self, space):
167 return space.newtext(self.name)
168
169 class GuardOp(WrappedOp):
170 def __init__(self, name, offset, repr_of_resop, hash):
171 WrappedOp.__init__(self, name, offset, repr_of_resop)
172 self.hash = hash
173
174 class DebugMergePoint(WrappedOp):
175 """ A class representing Debug Merge Point - the entry point
176 to a jitted loop.
177 """
178
179 def __init__(self, space, name, repr_of_resop, jd_name, call_depth,
180 call_id, w_greenkey):
181
182 WrappedOp.__init__(self, name, -1, repr_of_resop)
183 self.jd_name = jd_name
184 self.call_depth = call_depth
185 self.call_id = call_id
186 self.w_greenkey = w_greenkey
187
188 def get_pycode(self, space):
189 if self.jd_name == pypyjitdriver.name:
190 return space.getitem(self.w_greenkey, space.newint(0))
191 raise oefmt(space.w_AttributeError,
192 "This DebugMergePoint doesn't belong to the main Python "
193 "JitDriver")
194
195 def get_bytecode_no(self, space):
196 if self.jd_name == pypyjitdriver.name:
197 return space.getitem(self.w_greenkey, space.newint(1))
198 raise oefmt(space.w_AttributeError,
199 "This DebugMergePoint doesn't belong to the main Python "
200 "JitDriver")
201
202 def get_jitdriver_name(self, space):
203 return space.newtext(self.jd_name)
204
205 WrappedOp.typedef = TypeDef(
206 'ResOperation',
207 __doc__ = WrappedOp.__doc__,
208 __new__ = interp2app(descr_new_resop),
209 __repr__ = interp2app(WrappedOp.descr_repr),
210 name = GetSetProperty(WrappedOp.descr_name),
211 offset = interp_attrproperty("offset", cls=WrappedOp, wrapfn="newint"),
212 )
213 WrappedOp.typedef.acceptable_as_base_class = False
214
215 GuardOp.typedef = TypeDef(
216 'GuardOp',
217 __doc__ = GuardOp.__doc__,
218 __new__ = interp2app(descr_new_guardop),
219 __repr__ = interp2app(GuardOp.descr_repr),
220 name = GetSetProperty(GuardOp.descr_name),
221 offset = interp_attrproperty("offset", cls=GuardOp, wrapfn="newint"),
222 hash = interp_attrproperty("hash", cls=GuardOp, wrapfn="newint"),
223 )
224 GuardOp.typedef.acceptable_as_base_class = False
225
226 DebugMergePoint.typedef = TypeDef(
227 'DebugMergePoint', WrappedOp.typedef,
228 __new__ = interp2app(descr_new_dmp),
229 __doc__ = DebugMergePoint.__doc__,
230 greenkey = interp_attrproperty_w("w_greenkey", cls=DebugMergePoint,
231 doc="Representation of place where the loop was compiled. "
232 "In the case of the main interpreter loop, it's a triplet "
233 "(code, ofs, is_profiled)"),
234 pycode = GetSetProperty(DebugMergePoint.get_pycode),
235 bytecode_no = GetSetProperty(DebugMergePoint.get_bytecode_no,
236 doc="offset in the bytecode"),
237 call_depth = interp_attrproperty("call_depth", cls=DebugMergePoint,
238 doc="Depth of calls within this loop", wrapfn="newint"),
239 call_id = interp_attrproperty("call_id", cls=DebugMergePoint,
240 doc="Number of applevel function traced in this loop", wrapfn="newint"),
241 jitdriver_name = GetSetProperty(DebugMergePoint.get_jitdriver_name,
242 doc="Name of the jitdriver 'pypyjit' in the case "
243 "of the main interpreter loop"),
244 )
245 DebugMergePoint.typedef.acceptable_as_base_class = False
246
247
248 class W_JitLoopInfo(W_Root):
249 """ Loop debug information
250 """
251
252 w_green_key = None
253 bridge_no = 0
254 asmaddr = 0
255 asmlen = 0
256
257 def __init__(self, space, debug_info, is_bridge=False, wrap_ops=True):
258 if wrap_ops:
259 memo = {}
260 logops = debug_info.logger._make_log_operations(memo)
261 if debug_info.asminfo is not None:
262 ofs = debug_info.asminfo.ops_offset
263 else:
264 ofs = {}
265 ops = debug_info.operations
266 self.w_ops = space.newlist(wrap_oplist(space, logops, ops, ofs))
267 else:
268 self.w_ops = space.w_None
269
270 self.jd_name = debug_info.get_jitdriver().name
271 self.type = debug_info.type
272 if is_bridge:
273 self.bridge_no = compute_unique_id(debug_info.fail_descr)
274 #self.bridge_no = debug_info.fail_descr_no
275 self.w_green_key = space.w_None
276 else:
277 self.w_green_key = wrap_greenkey(space,
278 debug_info.get_jitdriver(),
279 debug_info.greenkey,
280 debug_info.get_greenkey_repr())
281 self.loop_no = debug_info.looptoken.number
282 asminfo = debug_info.asminfo
283 if asminfo is not None:
284 self.asmaddr = asminfo.asmaddr
285 self.asmlen = asminfo.asmlen
286
287 def descr_repr(self, space):
288 lgt = space.int_w(space.len(self.w_ops))
289 if self.type == "bridge":
290 code_repr = 'bridge no %d' % self.bridge_no
291 else:
292 code_repr = space.text_w(space.repr(self.w_green_key))
293 return space.newtext('<JitLoopInfo %s, %d operations, starting at <%s>>' %
294 (self.jd_name, lgt, code_repr))
295
296 def descr_get_bridge_no(self, space):
297 if space.is_none(self.w_green_key):
298 return space.newint(self.bridge_no)
299 raise oefmt(space.w_TypeError, "not a bridge")
300
301
302 @unwrap_spec(loopno=int, asmaddr=int, asmlen=int, loop_no=int,
303 type='text', jd_name='text', bridge_no=int)
304 def descr_new_jit_loop_info(space, w_subtype, w_greenkey, w_ops, loopno,
305 asmaddr, asmlen, loop_no, type, jd_name,
306 bridge_no=-1):
307 w_info = space.allocate_instance(W_JitLoopInfo, w_subtype)
308 w_info.w_green_key = w_greenkey
309 w_info.w_ops = w_ops
310 w_info.asmaddr = asmaddr
311 w_info.asmlen = asmlen
312 w_info.loop_no = loop_no
313 w_info.type = type
314 w_info.jd_name = jd_name
315 w_info.bridge_no = bridge_no
316 return w_info
317
318 W_JitLoopInfo.typedef = TypeDef(
319 'JitLoopInfo',
320 __doc__ = W_JitLoopInfo.__doc__,
321 __new__ = interp2app(descr_new_jit_loop_info),
322 jitdriver_name = interp_attrproperty('jd_name', cls=W_JitLoopInfo,
323 doc="Name of the JitDriver, pypyjit for the main one",
324 wrapfn="newtext"),
325 greenkey = interp_attrproperty_w('w_green_key', cls=W_JitLoopInfo,
326 doc="Representation of place where the loop was compiled. "
327 "In the case of the main interpreter loop, it's a triplet "
328 "(code, ofs, is_profiled)"),
329 operations = interp_attrproperty_w('w_ops', cls=W_JitLoopInfo, doc=
330 "List of operations in this loop."),
331 loop_no = interp_attrproperty('loop_no', cls=W_JitLoopInfo, doc=
332 "Loop cardinal number",
333 wrapfn="newint"),
334 bridge_no = GetSetProperty(W_JitLoopInfo.descr_get_bridge_no,
335 doc="bridge number (if a bridge)"),
336 type = interp_attrproperty('type', cls=W_JitLoopInfo,
337 doc="Loop type",
338 wrapfn="newtext"),
339 asmaddr = interp_attrproperty('asmaddr', cls=W_JitLoopInfo,
340 doc="Address of machine code",
341 wrapfn="newint"),
342 asmlen = interp_attrproperty('asmlen', cls=W_JitLoopInfo,
343 doc="Length of machine code",
344 wrapfn="newint"),
345 __repr__ = interp2app(W_JitLoopInfo.descr_repr),
346 )
347 W_JitLoopInfo.typedef.acceptable_as_base_class = False
348
349
350 class W_JitInfoSnapshot(W_Root):
351 def __init__(self, space, w_times, w_counters, w_counter_times):
352 self.w_loop_run_times = w_times
353 self.w_counters = w_counters
354 self.w_counter_times = w_counter_times
355
356 W_JitInfoSnapshot.typedef = TypeDef(
357 "JitInfoSnapshot",
358 loop_run_times = interp_attrproperty_w("w_loop_run_times",
359 cls=W_JitInfoSnapshot),
360 counters = interp_attrproperty_w("w_counters",
361 cls=W_JitInfoSnapshot,
362 doc="various JIT counters"),
363 counter_times = interp_attrproperty_w("w_counter_times",
364 cls=W_JitInfoSnapshot,
365 doc="various JIT timers")
366 )
367 W_JitInfoSnapshot.typedef.acceptable_as_base_class = False
368
369 def get_stats_snapshot(space):
370 """ Get the jit status in the specific moment in time. Note that this
371 is eager - the attribute access is not lazy, if you need new stats
372 you need to call this function again.
373 """
374 ll_times = jit_hooks.stats_get_loop_run_times(None)
375 w_times = space.newdict()
376 if ll_times:
377 for i in range(len(ll_times)):
378 w_key = space.newtuple([space.newtext(ll_times[i].type),
379 space.newint(ll_times[i].number)])
380 space.setitem(w_times, w_key,
381 space.newint(ll_times[i].counter))
382 w_counters = space.newdict()
383 for i, counter_name in enumerate(Counters.counter_names):
384 v = jit_hooks.stats_get_counter_value(None, i)
385 space.setitem_str(w_counters, counter_name, space.newint(v))
386 w_counter_times = space.newdict()
387 tr_time = jit_hooks.stats_get_times_value(None, Counters.TRACING)
388 space.setitem_str(w_counter_times, 'TRACING', space.newfloat(tr_time))
389 b_time = jit_hooks.stats_get_times_value(None, Counters.BACKEND)
390 space.setitem_str(w_counter_times, 'BACKEND', space.newfloat(b_time))
391 return W_JitInfoSnapshot(space, w_times, w_counters, w_counter_times)
392
393 def get_stats_asmmemmgr(space):
394 """Returns the raw memory currently used by the JIT backend,
395 as a pair (total_memory_allocated, memory_in_use)."""
396 m1 = jit_hooks.stats_asmmemmgr_allocated(None)
397 m2 = jit_hooks.stats_asmmemmgr_used(None)
398 return space.newtuple2(space.newint(m1), space.newint(m2))
399
400 def enable_debug(space):
401 """ Set the jit debugging - completely necessary for some stats to work,
402 most notably assembler counters.
403 """
404 jit_hooks.stats_set_debug(None, True)
405
406 def disable_debug(space):
407 """ Disable the jit debugging. This means some very small loops will be
408 marginally faster and the counters will stop working.
409 """
410 jit_hooks.stats_set_debug(None, False)