Debian Codegraph @

rpython/tool/jitlogparser/test/test_parser.py

raw symbols SCIP
1 from rpython.tool.jitlogparser.parser import (SimpleParser, TraceForOpcode,
2 Function, adjust_bridges,
3 import_log, split_trace, Op,
4 parse_log_counts)
5 from rpython.tool.jitlogparser.storage import LoopStorage, GenericCode
6 from rpython.tool.udir import udir
7 import py, sys
8 from rpython.jit.backend.detect_cpu import autodetect
9 from rpython.jit.backend.tool.viewcode import ObjdumpNotFound
10
11 def parse(input, **kwds):
12 return SimpleParser.parse_from_input(input, **kwds)
13
14
15 def test_parse():
16 ops = parse('''
17 [i7]
18 i9 = int_lt(i7, 1003)
19 guard_true(i9, descr=<Guard0x2>) []
20 i13 = getfield_raw_i(151937600, descr=<SignedFieldDescr pypysig_long_struct.c_value 0>)
21 ''').operations
22 assert len(ops) == 3
23 assert ops[0].name == 'int_lt'
24 assert ops[1].name == 'guard_true'
25 assert ops[1].descr is not None
26 assert ops[0].res == 'i9'
27 assert ops[0].repr() == 'i9 = int_lt(i7, 1003)'
28 assert ops[2].descr is not None
29 assert len(ops[2].args) == 1
30 assert ops[-1].repr() == 'i13 = getfield_raw_i(151937600, descr=<SignedFieldDescr pypysig_long_struct.c_value 0>)'
31
32 def test_parse_non_code():
33 ops = parse('''
34 []
35 debug_merge_point(0, 0, "SomeRandomStuff")
36 ''')
37 res = Function.from_operations(ops.operations, LoopStorage())
38 assert len(res.chunks) == 1
39 assert 'SomeRandomStuff' in res.chunks[0].repr()
40
41 def test_split():
42 ops = parse('''
43 [i0]
44 label()
45 debug_merge_point(0, 0, "<code object stuff. file '/I/dont/exist.py'. line 200> #10 ADD")
46 debug_merge_point(0, 0, "<code object stuff. file '/I/dont/exist.py'. line 200> #11 SUB")
47 i1 = int_add(i0, 1)
48 debug_merge_point(0, 0, "<code object stuff. file '/I/dont/exist.py'. line 200> #11 SUB")
49 i2 = int_add(i1, 1)
50 ''')
51 res = Function.from_operations(ops.operations, LoopStorage(), loopname='<loopname>')
52 assert len(res.chunks) == 4
53 assert len(res.chunks[0].operations) == 1
54 assert len(res.chunks[1].operations) == 1
55 assert len(res.chunks[2].operations) == 2
56 assert len(res.chunks[3].operations) == 2
57 assert res.chunks[3].bytecode_no == 11
58 assert res.chunks[0].bytecode_name == '<loopname>'
59
60 def test_inlined_call():
61 ops = parse("""
62 []
63 debug_merge_point(0, 0, '<code object inlined_call. file 'source.py'. line 12> #28 CALL_FUNCTION')
64 i18 = getfield_gc_i(p0, descr=<BoolFieldDescr pypy.interpreter.pyframe.PyFrame.inst_is_being_profiled 89>)
65 debug_merge_point(1, 1, '<code object inner. file 'source.py'. line 9> #0 LOAD_FAST')
66 debug_merge_point(1, 1, '<code object inner. file 'source.py'. line 9> #3 LOAD_CONST')
67 debug_merge_point(1, 1, '<code object inner. file 'source.py'. line 9> #7 RETURN_VALUE')
68 debug_merge_point(0, 0, '<code object inlined_call. file 'source.py'. line 12> #31 STORE_FAST')
69 """)
70 res = Function.from_operations(ops.operations, LoopStorage())
71 assert len(res.chunks) == 3 # two chunks + inlined call
72 assert isinstance(res.chunks[0], TraceForOpcode)
73 assert isinstance(res.chunks[1], Function)
74 assert isinstance(res.chunks[2], TraceForOpcode)
75 assert res.chunks[1].path == "1"
76 assert len(res.chunks[1].chunks) == 3
77
78 def test_name():
79 ops = parse('''
80 [i0]
81 debug_merge_point(0, 0, "<code object stuff. file '/I/dont/exist.py'. line 200> #10 ADD")
82 debug_merge_point(0, 0, "<code object stuff. file '/I/dont/exist.py'. line 201> #11 SUB")
83 i1 = int_add(i0, 1)
84 debug_merge_point(0, 0, "<code object stuff. file '/I/dont/exist.py'. line 202> #11 SUB")
85 i2 = int_add(i1, 1)
86 ''')
87 res = Function.from_operations(ops.operations, LoopStorage())
88 assert res.repr() == res.chunks[0].repr()
89 assert res.repr() == "stuff, file '/I/dont/exist.py', line 200"
90 assert res.startlineno == 200
91 assert res.filename == '/I/dont/exist.py'
92 assert res.name == 'stuff'
93
94 def test_name_no_first():
95 ops = parse('''
96 [i0]
97 i3 = int_add(i0, 1)
98 debug_merge_point(0, 0, "<code object stuff. file '/I/dont/exist.py'. line 200> #10 ADD")
99 debug_merge_point(0, 0, "<code object stuff. file '/I/dont/exist.py'. line 201> #11 SUB")
100 i1 = int_add(i0, 1)
101 debug_merge_point(0, 0, "<code object stuff. file '/I/dont/exist.py'. line 202> #11 SUB")
102 i2 = int_add(i1, 1)
103 ''')
104 res = Function.from_operations(ops.operations, LoopStorage())
105 assert res.repr() == res.chunks[1].repr()
106
107 def test_lineno():
108 fname = str(py.path.local(__file__).join('..', 'x.py'))
109 ops = parse('''
110 [i0, i1]
111 debug_merge_point(0, 0, "<code object f. file '%(fname)s'. line 5> #0 LOAD_FAST")
112 debug_merge_point(0, 0, "<code object f. file '%(fname)s'. line 5> #3 LOAD_FAST")
113 debug_merge_point(0, 0, "<code object f. file '%(fname)s'. line 5> #6 BINARY_ADD")
114 debug_merge_point(0, 0, "<code object f. file '%(fname)s'. line 5> #7 RETURN_VALUE")
115 ''' % locals())
116 res = Function.from_operations(ops.operations, LoopStorage())
117 assert res.chunks[1].lineno == 6
118
119 def test_linerange():
120 if sys.version_info > (2, 6):
121 py.test.skip("unportable test")
122 fname = str(py.path.local(__file__).join('..', 'x.py'))
123 ops = parse('''
124 [i0, i1]
125 debug_merge_point(0, 0, "<code object g. file '%(fname)s'. line 5> #9 LOAD_FAST")
126 debug_merge_point(0, 0, "<code object g. file '%(fname)s'. line 5> #12 LOAD_CONST")
127 debug_merge_point(0, 0, "<code object g. file '%(fname)s'. line 5> #22 LOAD_CONST")
128 debug_merge_point(0, 0, "<code object g. file '%(fname)s'. line 5> #28 LOAD_CONST")
129 debug_merge_point(0, 0, "<code object g. file '%(fname)s'. line 5> #6 SETUP_LOOP")
130 ''' % locals())
131 res = Function.from_operations(ops.operations, LoopStorage())
132 assert res.linerange == (7, 9)
133 assert res.lineset == set([7, 8, 9])
134
135 def test_linerange_notstarts():
136 if sys.version_info > (2, 6):
137 py.test.skip("unportable test")
138 fname = str(py.path.local(__file__).join('..', 'x.py'))
139 ops = parse("""
140 [p6, p1]
141 debug_merge_point(0, 0, '<code object h. file '%(fname)s'. line 11> #17 FOR_ITER')
142 guard_class(p6, 144264192, descr=<Guard0x2>)
143 p12 = getfield_gc(p6, descr=<GcPtrFieldDescr pypy.objspace.std.iterobject.W_AbstractSeqIterObject.inst_w_seq 12>)
144 """ % locals())
145 res = Function.from_operations(ops.operations, LoopStorage())
146 assert res.lineset
147
148 def test_reassign_loops():
149 main = parse('''
150 [i0]
151 guard_false(i0, descr=<Guard0x18>) []
152 ''')
153 main.count = 10
154 bridge = parse('''
155 # bridge out of Guard 0x18 with 13 ops
156 [i0, i1]
157 int_add(i0, i1)
158 ''')
159 bridge.count = 3
160 entry_bridge = parse('''
161 # Loop 3 : entry bridge
162 []
163 ''')
164 loops = LoopStorage().reconnect_loops([main, bridge, entry_bridge])
165 assert len(loops) == 2
166 assert len(loops[0].operations[0].bridge.operations) == 1
167 assert loops[0].operations[0].bridge.no == 0x18
168 assert loops[0].operations[0].percentage == 30
169
170 def test_adjust_bridges():
171 main = parse('''
172 [i0]
173 guard_false(i0, descr=<Guard0x1a>)
174 guard_true(i0, descr=<Guard0x5>)
175 ''')
176 bridge = parse('''
177 # bridge out of Guard 0x1a
178 []
179 int_add(0, 1)
180 ''')
181 LoopStorage().reconnect_loops([main, bridge])
182 assert adjust_bridges(main, {})[1].name == 'guard_true'
183 assert adjust_bridges(main, {'loop-1a': True})[1].name == 'int_add'
184
185 def test_parsing_strliteral():
186 loop = parse("""
187 debug_merge_point(0, 0, 'StrLiteralSearch at 11/51 [17, 8, 3, 1, 1, 1, 1, 51, 0, 19, 51, 1]')
188 """)
189 ops = Function.from_operations(loop.operations, LoopStorage())
190 chunk = ops.chunks[0]
191 assert chunk.bytecode_name.startswith('StrLiteralSearch')
192
193 def test_parsing_assembler():
194 if sys.platform == 'win32' or not autodetect().startswith('x86'):
195 py.test.skip('x86 only test')
196 backend_dump = "554889E5534154415541564157488DA500000000488B042590C5540148C7042590C554010000000048898570FFFFFF488B042598C5540148C7042598C554010000000048898568FFFFFF488B0425A0C5540148C70425A0C554010000000048898560FFFFFF488B0425A8C5540148C70425A8C554010000000048898558FFFFFF4C8B3C2550525B0149BB30E06C96FC7F00004D8B334983C60149BB30E06C96FC7F00004D89334981FF102700000F8D000000004983C7014C8B342580F76A024983EE014C89342580F76A024983FE000F8C00000000E9AEFFFFFF488B042588F76A024829E0483B042580EC3C01760D49BB05F30894FC7F000041FFD3554889E5534154415541564157488DA550FFFFFF4889BD70FFFFFF4889B568FFFFFF48899560FFFFFF48898D58FFFFFF4D89C7E954FFFFFF49BB00F00894FC7F000041FFD34440484C3D030300000049BB00F00894FC7F000041FFD34440484C3D070304000000"
197 dump_start = 0x7f3b0b2e63d5
198 try:
199 loop = parse("""
200 # Loop 0 : loop with 19 ops
201 [p0, p1, p2, p3, i4]
202 debug_merge_point(0, 0, '<code object f. file 'x.py'. line 2> #15 COMPARE_OP')
203 +166: i6 = int_lt(i4, 10000)
204 guard_true(i6, descr=<Guard0x3>) [p1, p0, p2, p3, i4]
205 debug_merge_point(0, 0, '<code object f. file 'x.py'. line 2> #27 INPLACE_ADD')
206 +179: i8 = int_add(i4, 1)
207 debug_merge_point(0, 0, '<code object f. file 'x.py'. line 2> #31 JUMP_ABSOLUTE')
208 +183: i10 = getfield_raw_i(40564608, descr=<SignedFieldDescr pypysig_long_struct.c_value 0>)
209 +191: i12 = int_sub(i10, 1)
210 +195: setfield_raw(40564608, i12, descr=<SignedFieldDescr pypysig_long_struct.c_value 0>)
211 +203: i14 = int_lt(i12, 0)
212 guard_false(i14, descr=<Guard0x4>) [p1, p0, p2, p3, i8, None]
213 debug_merge_point(0, '<code object f. file 'x.py'. line 2> #9 LOAD_FAST')
214 +213: jump(p0, p1, p2, p3, i8, descr=<Loop0>)
215 +218: --end of the loop--""", backend_dump=backend_dump,
216 dump_start=dump_start,
217 backend_tp='x86_64')
218 except ObjdumpNotFound:
219 py.test.skip('no objdump found on path')
220 cmp = loop.operations[1]
221 assert 'jge' in cmp.asm
222 assert '0x2710' in cmp.asm
223 assert 'jmp' in loop.operations[-1].asm
224
225 def test_parsing_arm_assembler():
226 if not autodetect().startswith('arm'):
227 py.test.skip('ARM only test')
228 backend_dump = "F04F2DE9108B2DED2CD04DE20DB0A0E17CC302E3DFC040E300409CE5085084E2086000E3006084E504B084E500508CE508D04BE20000A0E10000A0E1B0A10DE30EA044E300A09AE501A08AE2B0910DE30E9044E300A089E5C0910DE30E9044E3009099E5019089E2C0A10DE30EA044E300908AE5010050E1700020E124A092E500C08AE00C90DCE5288000E3090058E10180A0030080A013297000E3090057E10170A0030070A013077088E1200059E30180A0030080A013099049E2050059E30190A0330090A023099088E1000059E30190A0130090A003099087E1000059E3700020E1010080E204200BE5D0210DE30E2044E3002092E5012082E2D0910DE30E9044E3002089E5010050E1700020E100C08AE00C90DCE5282000E3090052E10120A0030020A013297000E3090057E10170A0030070A013077082E1200059E30120A0030020A013099049E2050059E30190A0330090A023099082E1000059E30190A0130090A003099087E1000059E3700020E1010080E20D005BE10FF0A0A1700020E1D8FFFFEA68C100E301C04BE33CFF2FE105010803560000000000000068C100E301C04BE33CFF2FE105010803570000000000000068C100E301C04BE33CFF2FE105014003580000000000000068C100E301C04BE33CFF2FE1050140035900000000000000"
229 dump_start = int(-0x4ffee930)
230 loop = parse("""
231 # Loop 5 (re StrMatchIn at 92 [17, 4, 0, 20, 393237, 21, 0, 29, 9, 1, 65535, 15, 4, 9, 3, 0, 1, 21, 1, 29, 9, 1, 65535, 15, 4, 9, 2, 0, 1, 1...) : loop with 38 ops
232 [i0, i1, p2]
233 +88: label(i0, i1, p2, descr=TargetToken(1081858608))
234 debug_merge_point(0, 're StrMatchIn at 92 [17. 4. 0. 20. 393237. 21. 0. 29. 9. 1. 65535. 15. 4. 9. 3. 0. 1. 21. 1. 29. 9. 1. 65535. 15. 4. 9. 2. 0. 1. 1...')
235 +116: i3 = int_lt(i0, i1)
236 guard_true(i3, descr=<Guard0x86>) [i1, i0, p2]
237 +124: p4 = getfield_gc_r(p2, descr=<FieldP rpython.rlib.rsre.rsre_core.StrMatchContext.inst__string 36>)
238 +128: i5 = strgetitem(p4, i0)
239 +136: i7 = int_eq(40, i5)
240 +152: i9 = int_eq(41, i5)
241 +168: i10 = int_or(i7, i9)
242 +172: i12 = int_eq(i5, 32)
243 +184: i14 = int_sub(i5, 9)
244 +188: i16 = uint_lt(i14, 5)
245 +200: i17 = int_or(i12, i16)
246 +204: i18 = int_is_true(i17)
247 +216: i19 = int_or(i10, i18)
248 +220: i20 = int_is_true(i19)
249 guard_false(i20, descr=<Guard0x87>) [i1, i0, p2]
250 +228: i22 = int_add(i0, 1)
251 debug_merge_point(0, 're StrMatchIn at 92 [17. 4. 0. 20. 393237. 21. 0. 29. 9. 1. 65535. 15. 4. 9. 3. 0. 1. 21. 1. 29. 9. 1. 65535. 15. 4. 9. 2. 0. 1. 1...')
252 +232: label(i22, i1, p2, p4, descr=TargetToken(1081858656))
253 debug_merge_point(0, 're StrMatchIn at 92 [17. 4. 0. 20. 393237. 21. 0. 29. 9. 1. 65535. 15. 4. 9. 3. 0. 1. 21. 1. 29. 9. 1. 65535. 15. 4. 9. 2. 0. 1. 1...')
254 +264: i23 = int_lt(i22, i1)
255 guard_true(i23, descr=<Guard0x88>) [i1, i22, p2]
256 +272: i24 = strgetitem(p4, i22)
257 +280: i25 = int_eq(40, i24)
258 +296: i26 = int_eq(41, i24)
259 +312: i27 = int_or(i25, i26)
260 +316: i28 = int_eq(i24, 32)
261 +328: i29 = int_sub(i24, 9)
262 +332: i30 = uint_lt(i29, 5)
263 +344: i31 = int_or(i28, i30)
264 +348: i32 = int_is_true(i31)
265 +360: i33 = int_or(i27, i32)
266 +364: i34 = int_is_true(i33)
267 guard_false(i34, descr=<Guard0x8a>) [i1, i22, p2]
268 +372: i35 = int_add(i22, 1)
269 debug_merge_point(0, 're StrMatchIn at 92 [17. 4. 0. 20. 393237. 21. 0. 29. 9. 1. 65535. 15. 4. 9. 3. 0. 1. 21. 1. 29. 9. 1. 65535. 15. 4. 9. 2. 0. 1. 1...')
270 +376: jump(i35, i1, p2, p4, descr=TargetToken(1081858656))
271 +392: --end of the loop--""", backend_dump=backend_dump,
272 dump_start=dump_start,
273 backend_tp='arm_32')
274 cmp = loop.operations[2]
275 assert 'cmp' in cmp.asm
276 assert 'bkpt' in loop.operations[-1].asm # the guard that would be patched
277
278
279 def test_import_log():
280 if sys.platform == 'win32' or not autodetect().startswith('x86'):
281 py.test.skip('x86 only test')
282 _, loops = import_log(str(py.path.local(__file__).join('..',
283 'logtest.log')))
284 try:
285 for loop in loops:
286 loop.force_asm()
287 except ObjdumpNotFound:
288 py.test.skip('no objdump found on path')
289 assert 'jge' in loops[0].operations[3].asm
290
291 def test_import_log_2():
292 if sys.platform == 'win32' or not autodetect().startswith('x86'):
293 py.test.skip('x86 only test')
294 _, loops = import_log(str(py.path.local(__file__).join('..',
295 'logtest2.log')))
296 try:
297 for loop in loops:
298 loop.force_asm()
299 except ObjdumpNotFound:
300 py.test.skip('no objdump found on path')
301 assert 'cmp' in loops[1].operations[2].asm
302
303 def test_Op_repr_is_pure():
304 op = Op('foobar', ['a', 'b'], 'c', 'mydescr')
305 myrepr = 'c = foobar(a, b, descr=mydescr)'
306 assert op.repr() == myrepr
307 assert op.repr() == myrepr # do it twice
308
309 def test_split_trace():
310 loop = parse('''
311 [i7]
312 i9 = int_lt(i7, 1003)
313 label(i9, descr=grrr)
314 guard_true(i9, descr=<Guard0x2>) []
315 i13 = getfield_raw_i(151937600, descr=<SignedFieldDescr pypysig_long_struct.c_value 0>)
316 label(i13, descr=asb)
317 i19 = int_lt(i13, 1003)
318 guard_true(i19, descr=<Guard0x2>) []
319 i113 = getfield_raw_i(151937600, descr=<SignedFieldDescr pypysig_long_struct.c_value 0>)
320 ''')
321 loop.comment = 'Loop 0'
322 parts = split_trace(loop)
323 assert len(parts) == 3
324 assert len(parts[0].operations) == 2
325 assert len(parts[1].operations) == 4
326 assert len(parts[2].operations) == 4
327 assert parts[1].descr == 'grrr'
328 assert parts[2].descr == 'asb'
329
330 def test_parse_log_counts():
331 loop = parse('''
332 [i7]
333 i9 = int_lt(i7, 1003)
334 label(i9, descr=grrr)
335 guard_true(i9, descr=<Guard0xaf>) []
336 i13 = getfield_raw_i(151937600, descr=<SignedFieldDescr pypysig_long_struct.c_value 0>)
337 label(i13, descr=asb)
338 i19 = int_lt(i13, 1003)
339 guard_true(i19, descr=<Guard0x3>) []
340 i113 = getfield_raw_i(151937600, descr=<SignedFieldDescr pypysig_long_struct.c_value 0>)
341 ''')
342 bridge = parse('''
343 # bridge out of Guard 0xaf with 1 ops
344 []
345 i0 = int_lt(1, 2)
346 finish(i0)
347 ''')
348 bridge.comment = 'bridge out of Guard 0xaf with 1 ops'
349 loop.comment = 'Loop 0'
350 loops = split_trace(loop) + split_trace(bridge)
351 input = ['grrr:123\nasb:12\nbridge 175:1234']
352 parse_log_counts(input, loops)
353 assert loops[-1].count == 1234
354 assert loops[1].count == 123
355 assert loops[2].count == 12
356
357 def test_parse_nonpython():
358 loop = parse("""
359 []
360 debug_merge_point(0, 0, 'random')
361 """)
362 f = Function.from_operations(loop.operations, LoopStorage())
363 assert f.filename is None
364
365 def test_parse_2_levels_up():
366 loop = parse("""
367 []
368 debug_merge_point(0, 0, 'one')
369 debug_merge_point(1, 0, 'two')
370 debug_merge_point(2, 0, 'three')
371 debug_merge_point(0, 0, 'one')
372 """)
373 f = Function.from_operations(loop.operations, LoopStorage())
374 assert len(f.chunks) == 3
375
376 def test_parse_from_inside():
377 loop = parse("""
378 []
379 debug_merge_point(1, 0, 'two')
380 debug_merge_point(2, 0, 'three')
381 debug_merge_point(0, 0, 'one')
382 """)
383 f = Function.from_operations(loop.operations, LoopStorage())
384 assert len(f.chunks) == 2
385
386 def test_embedded_lineno():
387 # debug_merge_point() can have a text that is either:
388 #
389 # * the PyPy2's <code object %s. file '%s'. line %d> #%d %s>
390 # funcname, filename, lineno, bytecode_no, bytecode_name
391 #
392 # * a standard text of the form %s;%s:%d-%d-%d %s
393 # funcname, filename, startlineno, curlineno, endlineno, anything
394 #
395 # * or anything else, which is not specially recognized but shouldn't crash
396 #
397 sourcefile = str(udir.join('test_embedded_lineno.src'))
398 with open(sourcefile, 'w') as f:
399 print >> f, "A#1"
400 print >> f, "B#2"
401 print >> f, "C#3"
402 print >> f, "D#4"
403 print >> f, "E#5"
404 print >> f, "F#6"
405 loop = parse("""
406 []
407 debug_merge_point(0, 0, 'myfunc;%(filename)s:2-2~one')
408 debug_merge_point(0, 0, 'myfunc;%(filename)s:2-2~two')
409 debug_merge_point(0, 0, 'myfunc;%(filename)s:2-4~')
410 debug_merge_point(0, 0, 'myfunc;%(filename)s:2-4~four')
411 """ % {'filename': sourcefile})
412 f = Function.from_operations(loop.operations, LoopStorage())
413
414 expect = [(2, 'one', True),
415 (2, 'two', False),
416 (4, '', True),
417 (4, 'four', False)]
418 assert len(f.chunks) == len(expect)
419
420 code_seen = set()
421 for chunk, (expected_lineno,
422 expected_bytecode_name,
423 expected_line_starts_here) in zip(f.chunks, expect):
424 assert chunk.name == 'myfunc'
425 assert chunk.bytecode_name == expected_bytecode_name
426 assert chunk.filename == sourcefile
427 assert chunk.startlineno == 2
428 assert chunk.bytecode_no == ~expected_lineno # half-abuse
429 assert chunk.has_valid_code()
430 assert chunk.lineno == expected_lineno
431 assert chunk.line_starts_here == expected_line_starts_here
432 code_seen.add(chunk.code)
433
434 assert len(code_seen) == 1
435 code, = code_seen
436 assert code.source[0] == "B#2"
437 assert code.source[1] == "C#3"
438 assert code.source[4] == "F#6"
439 py.test.raises(IndexError, "code.source[5]")