| 1 |
|
| 2 |
import py |
| 3 |
from pypy.interpreter.gateway import interp2app |
| 4 |
from pypy.interpreter.pycode import PyCode |
| 5 |
from rpython.jit.metainterp.history import JitCellToken, ConstInt, ConstPtr,\ |
| 6 |
BasicFailDescr |
| 7 |
from rpython.jit.metainterp.resoperation import rop |
| 8 |
from rpython.jit.metainterp.logger import Logger |
| 9 |
from rpython.rtyper.annlowlevel import (cast_instance_to_base_ptr, |
| 10 |
cast_base_ptr_to_instance) |
| 11 |
from rpython.rtyper.lltypesystem import lltype, llmemory |
| 12 |
from rpython.rtyper.rclass import OBJECT |
| 13 |
from pypy.module.pypyjit.interp_jit import pypyjitdriver |
| 14 |
from pypy.module.pypyjit.hooks import pypy_hooks |
| 15 |
from rpython.jit.tool.oparser import parse |
| 16 |
from rpython.rlib.jit import JitDebugInfo, AsmInfo, Counters |
| 17 |
|
| 18 |
|
| 19 |
class MockJitDriverSD(object): |
| 20 |
class warmstate(object): |
| 21 |
@staticmethod |
| 22 |
def get_location_str(boxes): |
| 23 |
ll_code = lltype.cast_opaque_ptr(lltype.Ptr(OBJECT), |
| 24 |
boxes[2].getref_base()) |
| 25 |
pycode = cast_base_ptr_to_instance(PyCode, ll_code) |
| 26 |
return pycode.co_name |
| 27 |
|
| 28 |
jitdriver = pypyjitdriver |
| 29 |
|
| 30 |
|
| 31 |
class MockSD(object): |
| 32 |
class cpu(object): |
| 33 |
pass |
| 34 |
|
| 35 |
jitdrivers_sd = [MockJitDriverSD] |
| 36 |
|
| 37 |
|
| 38 |
class AppTestJitHook(object): |
| 39 |
spaceconfig = dict(usemodules=('pypyjit',)) |
| 40 |
|
| 41 |
def setup_class(cls): |
| 42 |
if cls.runappdirect: |
| 43 |
py.test.skip("Can't run this test with -A") |
| 44 |
w_f = cls.space.appexec([], """(): |
| 45 |
def function(): |
| 46 |
pass |
| 47 |
return function |
| 48 |
""") |
| 49 |
cls.w_f = w_f |
| 50 |
ll_code = cast_instance_to_base_ptr(w_f.code) |
| 51 |
code_gcref = lltype.cast_opaque_ptr(llmemory.GCREF, ll_code) |
| 52 |
logger = Logger(MockSD()) |
| 53 |
|
| 54 |
oplist = parse(""" |
| 55 |
[i1, i2, p2] |
| 56 |
i3 = int_add(i1, i2) |
| 57 |
debug_merge_point(0, 0, 0, 0, 0, ConstPtr(ptr0)) |
| 58 |
guard_nonnull(p2) [] |
| 59 |
guard_true(i3) [] |
| 60 |
""", namespace={'ptr0': code_gcref}).operations |
| 61 |
greenkey = [ConstInt(0), ConstInt(0), ConstPtr(code_gcref)] |
| 62 |
offset = {} |
| 63 |
for i, op in enumerate(oplist): |
| 64 |
if i != 1: |
| 65 |
offset[op] = i |
| 66 |
|
| 67 |
oplist_no_descrs = parse(""" |
| 68 |
[i1, i2, p2] |
| 69 |
i3 = int_add(i1, i2) |
| 70 |
debug_merge_point(0, 0, 0, 0, 0, ConstPtr(ptr0)) |
| 71 |
guard_nonnull(p2) [] |
| 72 |
guard_true(i3) [] |
| 73 |
""", namespace={'ptr0': code_gcref}).operations |
| 74 |
for op in oplist_no_descrs: |
| 75 |
if op.is_guard(): |
| 76 |
op.setdescr(None) |
| 77 |
|
| 78 |
class FailDescr(BasicFailDescr): |
| 79 |
def get_jitcounter_hash(self): |
| 80 |
from rpython.rlib.rarithmetic import r_uint |
| 81 |
return r_uint(13) |
| 82 |
|
| 83 |
oplist[-1].setdescr(FailDescr()) |
| 84 |
oplist[-2].setdescr(FailDescr()) |
| 85 |
|
| 86 |
token = JitCellToken() |
| 87 |
token.number = 0 |
| 88 |
di_loop = JitDebugInfo(MockJitDriverSD, logger, token, oplist, 'loop', |
| 89 |
greenkey) |
| 90 |
di_loop_optimize = JitDebugInfo(MockJitDriverSD, logger, JitCellToken(), |
| 91 |
oplist, 'loop', greenkey) |
| 92 |
di_loop.asminfo = AsmInfo(offset, 0x42, 12) |
| 93 |
di_bridge = JitDebugInfo(MockJitDriverSD, logger, JitCellToken(), |
| 94 |
oplist, 'bridge', fail_descr=FailDescr()) |
| 95 |
di_bridge.asminfo = AsmInfo(offset, 0, 0) |
| 96 |
|
| 97 |
def interp_on_compile(): |
| 98 |
di_loop.oplist = cls.oplist |
| 99 |
if pypy_hooks.are_hooks_enabled(): |
| 100 |
pypy_hooks.after_compile(di_loop) |
| 101 |
|
| 102 |
def interp_on_compile_bridge(): |
| 103 |
if pypy_hooks.are_hooks_enabled(): |
| 104 |
pypy_hooks.after_compile_bridge(di_bridge) |
| 105 |
|
| 106 |
def interp_on_optimize(): |
| 107 |
if pypy_hooks.are_hooks_enabled(): |
| 108 |
di_loop_optimize.oplist = cls.oplist |
| 109 |
pypy_hooks.before_compile(di_loop_optimize) |
| 110 |
|
| 111 |
def interp_on_abort(): |
| 112 |
if pypy_hooks.are_hooks_enabled(): |
| 113 |
pypy_hooks.on_abort(Counters.ABORT_TOO_LONG, pypyjitdriver, |
| 114 |
greenkey, 'blah', Logger(MockSD), |
| 115 |
cls.oplist_no_descrs) |
| 116 |
|
| 117 |
space = cls.space |
| 118 |
cls.w_on_compile = space.wrap(interp2app(interp_on_compile)) |
| 119 |
cls.w_on_compile_bridge = space.wrap(interp2app(interp_on_compile_bridge)) |
| 120 |
cls.w_on_abort = space.wrap(interp2app(interp_on_abort)) |
| 121 |
cls.w_int_add_num = space.wrap(rop.INT_ADD) |
| 122 |
cls.w_dmp_num = space.wrap(rop.DEBUG_MERGE_POINT) |
| 123 |
cls.w_on_optimize = space.wrap(interp2app(interp_on_optimize)) |
| 124 |
cls.orig_oplist = oplist |
| 125 |
cls.orig_oplist_no_descrs = oplist_no_descrs |
| 126 |
cls.w_sorted_keys = space.wrap(sorted(Counters.counter_names)) |
| 127 |
|
| 128 |
def setup_method(self, meth): |
| 129 |
self.__class__.oplist = self.orig_oplist[:] |
| 130 |
self.__class__.oplist_no_descrs = self.orig_oplist_no_descrs[:] |
| 131 |
|
| 132 |
def test_on_compile(self): |
| 133 |
import pypyjit |
| 134 |
all = [] |
| 135 |
|
| 136 |
def hook(info): |
| 137 |
all.append(info) |
| 138 |
|
| 139 |
self.on_compile() |
| 140 |
pypyjit.set_compile_hook(hook) |
| 141 |
assert not all |
| 142 |
self.on_compile() |
| 143 |
assert len(all) == 1 |
| 144 |
info = all[0] |
| 145 |
assert info.jitdriver_name == 'pypyjit' |
| 146 |
assert info.greenkey[0].co_name == 'function' |
| 147 |
assert info.greenkey[1] == 0 |
| 148 |
assert info.greenkey[2] == False |
| 149 |
assert info.loop_no == 0 |
| 150 |
assert info.type == 'loop' |
| 151 |
assert info.asmaddr == 0x42 |
| 152 |
assert info.asmlen == 12 |
| 153 |
raises(TypeError, 'info.bridge_no') |
| 154 |
assert len(info.operations) == 4 |
| 155 |
int_add = info.operations[0] |
| 156 |
dmp = info.operations[1] |
| 157 |
assert isinstance(dmp, pypyjit.DebugMergePoint) |
| 158 |
assert dmp.pycode is self.f.__code__ |
| 159 |
assert dmp.greenkey == (self.f.__code__, 0, False) |
| 160 |
assert dmp.call_depth == 0 |
| 161 |
assert dmp.call_id == 0 |
| 162 |
assert dmp.offset == -1 |
| 163 |
assert int_add.name == 'int_add' |
| 164 |
assert int_add.offset == 0 |
| 165 |
self.on_compile_bridge() |
| 166 |
expected = ('<JitLoopInfo pypyjit, 4 operations, starting at ' |
| 167 |
'<(%s, 0, False)>>' % repr(self.f.__code__)) |
| 168 |
assert repr(all[0]) == expected |
| 169 |
assert len(all) == 2 |
| 170 |
pypyjit.set_compile_hook(None) |
| 171 |
self.on_compile() |
| 172 |
assert len(all) == 2 |
| 173 |
|
| 174 |
def test_on_compile_exception(self): |
| 175 |
import pypyjit, sys |
| 176 |
from io import StringIO |
| 177 |
|
| 178 |
def hook(*args): |
| 179 |
1/0 |
| 180 |
|
| 181 |
pypyjit.set_compile_hook(hook) |
| 182 |
s = StringIO() |
| 183 |
prev = sys.stderr |
| 184 |
sys.stderr = s |
| 185 |
try: |
| 186 |
self.on_compile() |
| 187 |
finally: |
| 188 |
sys.stderr = prev |
| 189 |
assert 'jit hook' in s.getvalue() |
| 190 |
assert 'ZeroDivisionError' in s.getvalue() |
| 191 |
|
| 192 |
def test_on_compile_crashes(self): |
| 193 |
import pypyjit |
| 194 |
loops = [] |
| 195 |
def hook(loop): |
| 196 |
loops.append(loop) |
| 197 |
pypyjit.set_compile_hook(hook) |
| 198 |
self.on_compile() |
| 199 |
loop = loops[0] |
| 200 |
op = loop.operations[2] |
| 201 |
assert op.name == 'guard_nonnull' |
| 202 |
|
| 203 |
def test_non_reentrant(self): |
| 204 |
import pypyjit |
| 205 |
l = [] |
| 206 |
|
| 207 |
def hook(*args): |
| 208 |
l.append(None) |
| 209 |
self.on_compile() |
| 210 |
self.on_compile_bridge() |
| 211 |
|
| 212 |
pypyjit.set_compile_hook(hook) |
| 213 |
self.on_compile() |
| 214 |
assert len(l) == 1 |
| 215 |
self.on_compile_bridge() |
| 216 |
assert len(l) == 2 |
| 217 |
|
| 218 |
def test_on_compile_types(self): |
| 219 |
import pypyjit |
| 220 |
l = [] |
| 221 |
|
| 222 |
def hook(info): |
| 223 |
l.append(info) |
| 224 |
|
| 225 |
pypyjit.set_compile_hook(hook) |
| 226 |
self.on_compile() |
| 227 |
op = l[0].operations[1] |
| 228 |
assert isinstance(op, pypyjit.ResOperation) |
| 229 |
assert 'function' in repr(op) |
| 230 |
|
| 231 |
def test_on_abort(self): |
| 232 |
import pypyjit |
| 233 |
l = [] |
| 234 |
|
| 235 |
def hook(jitdriver_name, greenkey, reason, operations): |
| 236 |
l.append((jitdriver_name, reason, operations)) |
| 237 |
|
| 238 |
pypyjit.set_abort_hook(hook) |
| 239 |
self.on_abort() |
| 240 |
assert len(l) == 1 |
| 241 |
name, reason, ops = l[0] |
| 242 |
assert name == 'pypyjit' |
| 243 |
assert reason == 'ABORT_TOO_LONG' |
| 244 |
assert len(ops) == 4 |
| 245 |
assert ops[2].hash == 0 |
| 246 |
|
| 247 |
def test_creation(self): |
| 248 |
from pypyjit import ResOperation |
| 249 |
|
| 250 |
op = ResOperation("int_add", -1, "int_add(1, 2)") |
| 251 |
assert op.name == 'int_add' |
| 252 |
assert repr(op) == "int_add(1, 2)" |
| 253 |
|
| 254 |
def test_creation_dmp(self): |
| 255 |
from pypyjit import DebugMergePoint |
| 256 |
|
| 257 |
def f(): |
| 258 |
pass |
| 259 |
|
| 260 |
op = DebugMergePoint("debug_merge_point", 'repr', 'pypyjit', 2, 3, (f.__code__, 0, 0)) |
| 261 |
assert op.bytecode_no == 0 |
| 262 |
assert op.pycode is f.__code__ |
| 263 |
assert repr(op) == 'repr' |
| 264 |
assert op.jitdriver_name == 'pypyjit' |
| 265 |
assert op.name == 'debug_merge_point' |
| 266 |
assert op.call_depth == 2 |
| 267 |
assert op.call_id == 3 |
| 268 |
op = DebugMergePoint('debug_merge_point', 'repr', 'notmain', |
| 269 |
5, 4, ('str',)) |
| 270 |
raises(AttributeError, 'op.pycode') |
| 271 |
assert op.call_depth == 5 |
| 272 |
|
| 273 |
def test_get_stats_snapshot(self): |
| 274 |
skip("a bit no idea how to test it") |
| 275 |
from pypyjit import get_stats_snapshot |
| 276 |
|
| 277 |
stats = get_stats_snapshot() |
| 278 |
assert stats.w_loop_run_times == [] |
| 279 |
assert isinstance(stats.w_counters, dict) |
| 280 |
assert sorted(stats.w_counters.keys()) == self.sorted_keys |
| 281 |
|