| 1 |
import math |
| 2 |
import py |
| 3 |
from rpython.jit.tool.traceviewer import splitloops, FinalBlock, Block,\ |
| 4 |
split_one_loop, postprocess, main, get_gradient_color, guard_number,\ |
| 5 |
find_name_key |
| 6 |
|
| 7 |
|
| 8 |
def test_gradient_color(): |
| 9 |
assert get_gradient_color(0.0000000001) == '#01FF00' |
| 10 |
assert get_gradient_color(100000000000) == '#FF0100' |
| 11 |
assert get_gradient_color(math.exp(1.8)) == '#FFFF00' |
| 12 |
assert get_gradient_color(math.exp(1.9)) == '#FFB400' |
| 13 |
assert get_gradient_color(math.exp(1.7)) == '#B4FF00' |
| 14 |
|
| 15 |
|
| 16 |
def preparse(data): |
| 17 |
return "\n".join([i.strip() for i in data.split("\n") if i.strip()]) |
| 18 |
|
| 19 |
class TestSplitLoops(object): |
| 20 |
def test_no_of_loops(self): |
| 21 |
data = [preparse(""" |
| 22 |
# Loop 0 : loop with 39 ops |
| 23 |
debug_merge_point('', 0) |
| 24 |
guard_class(p4, 141310752, descr=<Guard5>) [p0, p1] |
| 25 |
p60 = getfield_gc(p4, descr=<GcPtrFieldDescr 16>) |
| 26 |
guard_nonnull(p60, descr=<Guard6>) [p0, p1] |
| 27 |
"""), preparse(""" |
| 28 |
# Loop 1 : loop with 46 ops |
| 29 |
p21 = getfield_gc(p4, descr=<GcPtrFieldDescr 16>) |
| 30 |
""")] |
| 31 |
loops = splitloops(data) |
| 32 |
assert len(loops) == 2 |
| 33 |
|
| 34 |
def test_no_of_loops_hexguards(self): |
| 35 |
data = [preparse(""" |
| 36 |
# Loop 0 : loop with 39 ops |
| 37 |
debug_merge_point('', 0) |
| 38 |
guard_class(p4, 141310752, descr=<Guard0x10abcdef0>) [p0, p1] |
| 39 |
p60 = getfield_gc(p4, descr=<GcPtrFieldDescr 16>) |
| 40 |
guard_nonnull(p60, descr=<Guard0x10abcdef1>) [p0, p1] |
| 41 |
"""), preparse(""" |
| 42 |
# Loop 1 : loop with 46 ops |
| 43 |
p21 = getfield_gc(p4, descr=<GcPtrFieldDescr 16>) |
| 44 |
""")] |
| 45 |
loops = splitloops(data) |
| 46 |
assert len(loops) == 2 |
| 47 |
|
| 48 |
def test_split_one_loop(self): |
| 49 |
real_loops = [FinalBlock(preparse(""" |
| 50 |
p21 = getfield_gc(p4, descr=<GcPtrFieldDescr 16>) |
| 51 |
guard_class(p4, 141310752, descr=<Guard51>) [p0, p1] |
| 52 |
"""), None), FinalBlock(preparse(""" |
| 53 |
p60 = getfield_gc(p4, descr=<GcPtrFieldDescr 16>) |
| 54 |
guard_nonnull(p60, descr=<Guard5>) [p0, p1] |
| 55 |
"""), None)] |
| 56 |
real_loops[0].loop_no = 0 |
| 57 |
real_loops[1].loop_no = 1 |
| 58 |
allloops = real_loops[:] |
| 59 |
split_one_loop(real_loops, 'Guard5', 'extra', 1, 5, allloops) |
| 60 |
loop = real_loops[1] |
| 61 |
assert isinstance(loop, Block) |
| 62 |
assert loop.content.endswith('p1]') |
| 63 |
loop.left = allloops[loop.left] |
| 64 |
loop.right = allloops[loop.right] |
| 65 |
assert loop.left.content == '' |
| 66 |
assert loop.right.content == 'extra' |
| 67 |
|
| 68 |
def test_split_one_loop_hexguards(self): |
| 69 |
real_loops = [FinalBlock(preparse(""" |
| 70 |
p21 = getfield_gc(p4, descr=<GcPtrFieldDescr 16>) |
| 71 |
guard_class(p4, 141310752, descr=<Guard0x10abcdef2>) [p0, p1] |
| 72 |
"""), None), FinalBlock(preparse(""" |
| 73 |
p60 = getfield_gc(p4, descr=<GcPtrFieldDescr 16>) |
| 74 |
guard_nonnull(p60, descr=<Guard0x10abcdef0>) [p0, p1] |
| 75 |
"""), None)] |
| 76 |
real_loops[0].loop_no = 0 |
| 77 |
real_loops[1].loop_no = 1 |
| 78 |
allloops = real_loops[:] |
| 79 |
split_one_loop(real_loops, 'Guard0x10abcdef0', 'extra', 1, guard_number(("0x10abcdef0", "0x")), allloops) |
| 80 |
loop = real_loops[1] |
| 81 |
assert isinstance(loop, Block) |
| 82 |
assert loop.content.endswith('p1]') |
| 83 |
loop.left = allloops[loop.left] |
| 84 |
loop.right = allloops[loop.right] |
| 85 |
assert loop.left.content == '' |
| 86 |
assert loop.right.content == 'extra' |
| 87 |
|
| 88 |
def test_postparse(self): |
| 89 |
real_loops = [FinalBlock("debug_merge_point('<code object _runCallbacks, file '/tmp/x/twisted-trunk/twisted/internet/defer.py', line 357> #40 POP_TOP', 0)", None)] |
| 90 |
postprocess(real_loops, real_loops[:], {}) |
| 91 |
assert real_loops[0].header.startswith("_runCallbacks, file '/tmp/x/twisted-trunk/twisted/internet/defer.py', line 357") |
| 92 |
|
| 93 |
def test_postparse_new(self): |
| 94 |
real_loops = [FinalBlock("debug_merge_point(0, 0, '<code object _optimize_charset. file '/usr/local/Cellar/pypy/2.0-beta2/lib-python/2.7/sre_compile.py'. line 207> #351 LOAD_FAST')", None)] |
| 95 |
postprocess(real_loops, real_loops[:], {}) |
| 96 |
assert real_loops[0].header.startswith("_optimize_charset. file '/usr/local/Cellar/pypy/2.0-beta2/lib-python/2.7/sre_compile.py'. line 207") |
| 97 |
|
| 98 |
def test_load_actual(self): |
| 99 |
fname = py.path.local(__file__).join('..', 'data.log.bz2') |
| 100 |
main(str(fname), False, view=False) |
| 101 |
|
| 102 |
|
| 103 |
def test_load_actual_f(self): |
| 104 |
fname = py.path.local(__file__).join('..', 'f.pypylog.bz2') |
| 105 |
main(str(fname), False, view=False) |
| 106 |
|
| 107 |
|
| 108 |
def test_non_contiguous_loops(self): |
| 109 |
data = [preparse(""" |
| 110 |
# Loop 1 : loop with 39 ops |
| 111 |
debug_merge_point('', 0) |
| 112 |
guard_class(p4, 141310752, descr=<Guard5>) [p0, p1] |
| 113 |
p60 = getfield_gc(p4, descr=<GcPtrFieldDescr 16>) |
| 114 |
guard_nonnull(p60, descr=<Guard6>) [p0, p1] |
| 115 |
"""), preparse(""" |
| 116 |
# Loop 4 : loop with 46 ops |
| 117 |
p21 = getfield_gc(p4, descr=<GcPtrFieldDescr 16>) |
| 118 |
""")] |
| 119 |
real_loops, all_loops = splitloops(data) |
| 120 |
assert len(all_loops) == 2 |
| 121 |
assert len(real_loops) == 5 |
| 122 |
|
| 123 |
class TestMergPointStringExtraciton(object): |
| 124 |
|
| 125 |
def test_find_name_key(self): |
| 126 |
def find(s): |
| 127 |
return find_name_key(FinalBlock(s, None)) |
| 128 |
assert find(r"debug_merge_point(0, 0, '<code object f5. file 'f.py'. line 34> #63 GET_ITER')") \ |
| 129 |
== (r"f5. file 'f.py'. line 34 #63 GET_ITER", r"<code object f5. file 'f.py'. line 34> #63 GET_ITER") |
| 130 |
assert find(r"debug_merge_point(0, 0, '<code object f5. file 'f.py'. line 34> <generator>')") \ |
| 131 |
== (r"f5. file 'f.py'. line 34 <generator>", r"<code object f5. file 'f.py'. line 34> <generator>") |
| 132 |
assert find(r"debug_merge_point(0, 0, 'cffi_callback <code object f5. file 'f.py'. line 34>')") \ |
| 133 |
== (r"f5. file 'f.py'. line 34 (cffi_callback)", r"cffi_callback <code object f5. file 'f.py'. line 34>") |
| 134 |
assert find(r"debug_merge_point(0, 0, 'cffi_callback <?>')") \ |
| 135 |
== (r"? (cffi_callback)", r"cffi_callback <?>") |
| 136 |
assert find(r"debug_merge_point(0, 0, 'cffi_call_python somestr')") \ |
| 137 |
== (r"somestr (cffi_call_python)", r"cffi_call_python somestr") |
| 138 |
assert find(r"debug_merge_point(0, 0, '(SequenceableCollection >> #replaceFrom:to:with:startingAt:) [8]: <0x14>pushTemporaryVariableBytecode(4)')") \ |
| 139 |
== (r"SequenceableCollection>>#replaceFrom:to:with:startingAt: @ 8 <pushTemporaryVariableBytecode>", r"(SequenceableCollection >> #replaceFrom:to:with:startingAt:) [8]: <0x14>pushTemporaryVariableBytecode(4)") |
| 140 |
assert find(r"debug_merge_point(1, 4, '(Magnitude >> #min:max:) [0]: <0x70>pushReceiverBytecode')") \ |
| 141 |
== (r"Magnitude>>#min:max: @ 0 <pushReceiverBytecode>", r"(Magnitude >> #min:max:) [0]: <0x70>pushReceiverBytecode") |
| 142 |
assert find(r"debug_merge_point(0, 0, '(#DoIt) [0]: <0x70>pushReceiverBytecode')") \ |
| 143 |
== (r"#DoIt @ 0 <pushReceiverBytecode>", r"(#DoIt) [0]: <0x70>pushReceiverBytecode") |
| 144 |
|
| 145 |
assert find(r"debug_merge_point(0, 0, '54: LOAD LIST 4')") \ |
| 146 |
== (r"? @ 54 <LOAD LIST 4>", r"54: LOAD LIST 4") |
| 147 |
assert find(r"debug_merge_point(0, 0, '44: LOAD_MEMBER_DOT function: barfoo')") \ |
| 148 |
== (r"barfoo @ 44 <LOAD_MEMBER_DOT>", r"44: LOAD_MEMBER_DOT function: barfoo") |
| 149 |
assert find(r"debug_merge_point(0, 0, '87: end of opcodes')") \ |
| 150 |
== (r"? @ 87 <end of opcodes>", r"87: end of opcodes") |
| 151 |
assert find(r"debug_merge_point(0, 0, 'Green_Ast is None')") \ |
| 152 |
== (r"Green_Ast is None", r"Green_Ast is None") |
| 153 |
assert find(r"debug_merge_point(0, 0, 'Label(safe_return_multi_vals:pycket.interpreter:565)')") \ |
| 154 |
== (r"Label(safe_return_multi_vals:pycket.interpreter:565)", r"Label(safe_return_multi_vals:pycket.interpreter:565)") |
| 155 |
assert find(r"debug_merge_point(0, 0, '(*node2 item AppRand1_289 AppRand2_116)')") \ |
| 156 |
== (r"(*node2 item AppRand1_289 AppRand2_116)", r"(*node2 item AppRand1_289 AppRand2_116)") |
| 157 |
assert find(r"debug_merge_point(0, 0, '(let ([if_2417 (let ([AppRand0_2026 (* Zr Zr)][AppRand1_1531 (* Zi Zi)]) (let ([AppRand0_2027 (+ AppRand0_2026 AppRand1_1531)]) (> AppRand0_2027 LIMIT-SQR)))]) (if if_2417 0 (let ([if_2416 (= i ITERATIONS)]) (if if_2416 1 (let ([Zr199 (let ([AppRand0_2041 (* Zr Zr)][AppRand1_1540 (* Zi Zi)]) (let ([AppRand0_2042 (- AppRand0_2041 AppRand1_1540)]) (+ AppRand0_2042 Cr)))][Zi206 (let ([AppRand1_1541 (* Zr Zi)]) (let ([AppRand0_2043 (* 2.0 AppRand1_1541)]) (+ AppRand0_2043 Ci)))]) (let ([Zr211 (let ([AppRand0_2038 (* Zr199 Zr199)][AppRand1_1538 (* Zi206 Zi206)]) (let ([AppRand0_2039 (- AppRand0_2038 AppRand1_1538)]) (+ AppRand0_2039 Cr)))][Zi218 (let ([AppRand1_1539 (* Zr199 Zi206)]) (let ([AppRand0_2040 (* 2.0 AppRand1_1539)]) (+ AppRand0_2040 Ci)))]) (let ([Zr223 (let ([AppRand0_2035 (* Zr211 Zr211)][AppRand1_1536 (* Zi218 Zi218)]) (let ([AppRand0_2036 (- AppRand0_2035 AppRand1_1536)]) (+ AppRand0_2036 Cr)))][Zi230 (let ([AppRand1_1537 (* Zr211 Zi218)]) (let ([AppRand0_2037 (* 2.0 AppRand1_1537)]) (+ AppRand0_2037 Ci)))]) (let ([Zr235 (let ([AppRand0_2032 (* Zr223 Zr223)][AppRand1_1534 (* Zi230 Zi230)]) (let ([AppRand0_2033 (- AppRand0_2032 AppRand1_1534)]) (+ AppRand0_2033 Cr)))][Zi242 (let ([AppRand1_1535 (* Zr223 Zi230)]) (let ([AppRand0_2034 (* 2.0 AppRand1_1535)]) (+ AppRand0_2034 Ci)))]) (let ([Zr247 (let ([AppRand0_2029 (* Zr235 Zr235)][AppRand1_1532 (* Zi242 Zi242)]) (let ([AppRand0_2030 (- AppRand0_2029 AppRand1_1532)]) (+ AppRand0_2030 Cr)))][Zi254 (let ([AppRand1_1533 (* Zr235 Zi242)]) (let ([AppRand0_2031 (* 2.0 AppRand1_1533)]) (+ AppRand0_2031 Ci)))]) (let ([AppRand0_2028 (+ i 5)]) (loop AppRand0_2028 Zr247 Zi254))))))))))) from (loop AppRand0_2028 Zr247 Zi254)')") \ |
| 158 |
== (r"(let ([if_2417 (let ([AppRand0_2026 (* Zr Zr)][AppRand1_1531 (* ...", r"(let ([if_2417 (let ([AppRand0_2026 (* Zr Zr)][AppRand1_1531 (* Zi Zi)]) (let ([AppRand0_2027 (+ AppRand0_2026 AppRand1_1531)]) (> AppRand0_2027 LIMIT-SQR)))]) (if if_2417 0 (let ([if_2416 (= i ITERATIONS)]) (if if_2416 1 (let ([Zr199 (let ([AppRand0_2041 (* Zr Zr)][AppRand1_1540 (* Zi Zi)]) (let ([AppRand0_2042 (- AppRand0_2041 AppRand1_1540)]) (+ AppRand0_2042 Cr)))][Zi206 (let ([AppRand1_1541 (* Zr Zi)]) (let ([AppRand0_2043 (* 2.0 AppRand1_1541)]) (+ AppRand0_2043 Ci)))]) (let ([Zr211 (let ([AppRand0_2038 (* Zr199 Zr199)][AppRand1_1538 (* Zi206 Zi206)]) (let ([AppRand0_2039 (- AppRand0_2038 AppRand1_1538)]) (+ AppRand0_2039 Cr)))][Zi218 (let ([AppRand1_1539 (* Zr199 Zi206)]) (let ([AppRand0_2040 (* 2.0 AppRand1_1539)]) (+ AppRand0_2040 Ci)))]) (let ([Zr223 (let ([AppRand0_2035 (* Zr211 Zr211)][AppRand1_1536 (* Zi218 Zi218)]) (let ([AppRand0_2036 (- AppRand0_2035 AppRand1_1536)]) (+ AppRand0_2036 Cr)))][Zi230 (let ([AppRand1_1537 (* Zr211 Zi218)]) (let ([AppRand0_2037 (* 2.0 AppRand1_1537)]) (+ AppRand0_2037 Ci)))]) (let ([Zr235 (let ([AppRand0_2032 (* Zr223 Zr223)][AppRand1_1534 (* Zi230 Zi230)]) (let ([AppRand0_2033 (- AppRand0_2032 AppRand1_1534)]) (+ AppRand0_2033 Cr)))][Zi242 (let ([AppRand1_1535 (* Zr223 Zi230)]) (let ([AppRand0_2034 (* 2.0 AppRand1_1535)]) (+ AppRand0_2034 Ci)))]) (let ([Zr247 (let ([AppRand0_2029 (* Zr235 Zr235)][AppRand1_1532 (* Zi242 Zi242)]) (let ([AppRand0_2030 (- AppRand0_2029 AppRand1_1532)]) (+ AppRand0_2030 Cr)))][Zi254 (let ([AppRand1_1533 (* Zr235 Zi242)]) (let ([AppRand0_2031 (* 2.0 AppRand1_1533)]) (+ AppRand0_2031 Ci)))]) (let ([AppRand0_2028 (+ i 5)]) (loop AppRand0_2028 Zr247 Zi254))))))))))) from (loop AppRand0_2028 Zr247 Zi254)") |
| 159 |
assert find(r"debug_merge_point(0, 0, 'times at LOAD_SELF')") \ |
| 160 |
== (r"times at LOAD_SELF", r"times at LOAD_SELF") |
| 161 |
assert find(r"debug_merge_point(1, 1, 'block in <main> at LOAD_DEREF')") \ |
| 162 |
== (r"block in <main> at LOAD_DEREF", r"block in <main> at LOAD_DEREF") |
| 163 |
assert find(r"debug_merge_point(0, 0, '<main> at SEND')") \ |
| 164 |
== (r"<main> at SEND", r"<main> at SEND") |