| 1 |
from __future__ import print_function |
| 2 |
|
| 3 |
import re, sys |
| 4 |
|
| 5 |
from rpython.jit.metainterp.resoperation import opname |
| 6 |
from rpython.jit.tool.oparser import OpParser |
| 7 |
from rpython.tool.logparser import parse_log_file, extract_category |
| 8 |
from copy import copy |
| 9 |
|
| 10 |
def parse_code_data(arg): |
| 11 |
name = None |
| 12 |
lineno = 0 |
| 13 |
filename = None |
| 14 |
bytecode_no = 0 |
| 15 |
bytecode_name = None |
| 16 |
mask = 0 |
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
m = re.match(r'(.+?);(.+?):(\d+)-(\d+)~(.*)', arg) |
| 23 |
if m is not None: |
| 24 |
mask = -1 |
| 25 |
else: |
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
m = re.search(r'<code object ([<>\w]+)[\.,] file \'(.+?)\'[\.,] ' |
| 30 |
r'line (\d+)> #(\d+) (\w+)', arg) |
| 31 |
if m is None: |
| 32 |
|
| 33 |
if arg: |
| 34 |
bytecode_name = arg |
| 35 |
else: |
| 36 |
name, filename, lineno, bytecode_no, bytecode_name = m.groups() |
| 37 |
return name, bytecode_name, filename, int(lineno), int(bytecode_no) ^ mask |
| 38 |
|
| 39 |
class Op(object): |
| 40 |
bridge = None |
| 41 |
offset = None |
| 42 |
asm = None |
| 43 |
failargs = () |
| 44 |
|
| 45 |
def __init__(self, name, args, res, descr, failargs=None): |
| 46 |
self.name = name |
| 47 |
self.args = args |
| 48 |
self.res = res |
| 49 |
self.descr = descr |
| 50 |
self._is_guard = name.startswith('guard_') |
| 51 |
if self._is_guard: |
| 52 |
self.guard_no = int(self.descr[len('<Guard0x'):-1], 16) |
| 53 |
self.failargs = failargs |
| 54 |
|
| 55 |
def as_json(self): |
| 56 |
d = { |
| 57 |
'name': self.name, |
| 58 |
'args': self.args, |
| 59 |
'res': self.res, |
| 60 |
} |
| 61 |
if self.descr is not None: |
| 62 |
d['descr'] = self.descr |
| 63 |
if self.bridge is not None: |
| 64 |
d['bridge'] = self.bridge.as_json() |
| 65 |
if self.asm is not None: |
| 66 |
d['asm'] = self.asm |
| 67 |
return d |
| 68 |
|
| 69 |
def setfailargs(self, failargs): |
| 70 |
self.failargs = failargs |
| 71 |
|
| 72 |
def getarg(self, i): |
| 73 |
return self.args[i] |
| 74 |
|
| 75 |
def getargs(self): |
| 76 |
return self.args[:] |
| 77 |
|
| 78 |
def getres(self): |
| 79 |
return self.res |
| 80 |
|
| 81 |
def getdescr(self): |
| 82 |
return self.descr |
| 83 |
|
| 84 |
def is_guard(self): |
| 85 |
return self._is_guard |
| 86 |
|
| 87 |
def repr(self): |
| 88 |
args = self.getargs() |
| 89 |
if self.descr is not None: |
| 90 |
args.append('descr=%s' % self.descr) |
| 91 |
arglist = ', '.join(args) |
| 92 |
if self.res is not None: |
| 93 |
return '%s = %s(%s)' % (self.getres(), self.name, arglist) |
| 94 |
else: |
| 95 |
return '%s(%s)' % (self.name, arglist) |
| 96 |
|
| 97 |
def __repr__(self): |
| 98 |
return self.repr() |
| 99 |
|
| 100 |
class SimpleParser(OpParser): |
| 101 |
|
| 102 |
|
| 103 |
Op = Op |
| 104 |
use_mock_model = True |
| 105 |
|
| 106 |
def postprocess(self, loop, backend_dump=None, backend_tp=None, |
| 107 |
dump_start=0): |
| 108 |
if backend_dump is not None: |
| 109 |
raw_asm = self._asm_disassemble(backend_dump.decode('hex'), |
| 110 |
backend_tp, dump_start) |
| 111 |
|
| 112 |
|
| 113 |
|
| 114 |
|
| 115 |
asm = [] |
| 116 |
start = dump_start |
| 117 |
for elem in raw_asm: |
| 118 |
if len(elem.split("\t")) < 3: |
| 119 |
continue |
| 120 |
e = elem.split("\t") |
| 121 |
adr = e[0] |
| 122 |
v = elem |
| 123 |
if not start: |
| 124 |
start = int(adr.strip(":"), 16) |
| 125 |
ofs = int(adr.strip(":"), 16) - start |
| 126 |
if ofs >= 0: |
| 127 |
asm.append((ofs, v.strip("\n"))) |
| 128 |
asm_index = 0 |
| 129 |
for i, op in enumerate(loop.operations): |
| 130 |
end = 0 |
| 131 |
j = i + 1 |
| 132 |
while end == 0: |
| 133 |
if j == len(loop.operations): |
| 134 |
end = loop.last_offset |
| 135 |
break |
| 136 |
if loop.operations[j].offset is None: |
| 137 |
j += 1 |
| 138 |
else: |
| 139 |
end = loop.operations[j].offset |
| 140 |
if op.offset is not None: |
| 141 |
while asm[asm_index][0] < op.offset: |
| 142 |
asm_index += 1 |
| 143 |
end_index = asm_index |
| 144 |
while asm[end_index][0] < end and end_index < len(asm) - 1: |
| 145 |
end_index += 1 |
| 146 |
op.asm = '\n'.join([asm[i][1] for i in range(asm_index, end_index)]) |
| 147 |
return loop |
| 148 |
|
| 149 |
def _asm_disassemble(self, d, tp, origin_addr): |
| 150 |
from rpython.jit.backend.tool.viewcode import machine_code_dump |
| 151 |
return list(machine_code_dump(d, origin_addr, tp)) |
| 152 |
|
| 153 |
@classmethod |
| 154 |
def parse_from_input(cls, input, **kwds): |
| 155 |
parser = cls(input, None, {}, 'lltype', None, |
| 156 |
nonstrict=True) |
| 157 |
loop = parser.parse() |
| 158 |
return parser.postprocess(loop, **kwds) |
| 159 |
|
| 160 |
def parse_args(self, opname, argspec): |
| 161 |
if not argspec.strip(): |
| 162 |
return [], None |
| 163 |
if opname == 'debug_merge_point': |
| 164 |
return argspec.split(", ", 2), None |
| 165 |
else: |
| 166 |
args = argspec.split(', ') |
| 167 |
descr = None |
| 168 |
if args[-1].startswith('descr='): |
| 169 |
descr = args[-1][len('descr='):] |
| 170 |
args = args[:-1] |
| 171 |
if args == ['']: |
| 172 |
args = [] |
| 173 |
return (args, descr) |
| 174 |
|
| 175 |
def box_for_var(self, res): |
| 176 |
return res |
| 177 |
|
| 178 |
def create_op(self, opnum, args, res, descr, fail_args): |
| 179 |
return self.Op(intern(opname[opnum].lower()), args, res, |
| 180 |
descr, fail_args) |
| 181 |
|
| 182 |
def create_op_no_result(self, opnum, args, descr, fail_args): |
| 183 |
return self.Op(intern(opname[opnum].lower()), args, None, |
| 184 |
descr, fail_args) |
| 185 |
|
| 186 |
def update_memo(self, val, name): |
| 187 |
pass |
| 188 |
|
| 189 |
class NonCodeError(Exception): |
| 190 |
pass |
| 191 |
|
| 192 |
class TraceForOpcode(object): |
| 193 |
code = None |
| 194 |
is_bytecode = True |
| 195 |
inline_level = None |
| 196 |
has_dmp = False |
| 197 |
|
| 198 |
def __init__(self, operations, storage, loopname): |
| 199 |
for op in operations: |
| 200 |
if op.name == 'debug_merge_point': |
| 201 |
self.inline_level = int(op.args[0]) |
| 202 |
parsed = parse_code_data(op.args[2][1:-1]) |
| 203 |
(self.name, self.bytecode_name, self.filename, |
| 204 |
self.startlineno, self.bytecode_no) = parsed |
| 205 |
break |
| 206 |
else: |
| 207 |
self.inline_level = 0 |
| 208 |
parsed = parse_code_data(loopname) |
| 209 |
(self.name, self.bytecode_name, self.filename, |
| 210 |
self.startlineno, self.bytecode_no) = parsed |
| 211 |
self.operations = operations |
| 212 |
self.storage = storage |
| 213 |
generic_format = (self.bytecode_no < 0) |
| 214 |
self.code = storage.disassemble_code(self.filename, self.startlineno, |
| 215 |
self.name, generic_format) |
| 216 |
|
| 217 |
def repr(self): |
| 218 |
if self.filename is None: |
| 219 |
return self.bytecode_name |
| 220 |
return "%s, file '%s', line %d" % (self.name, self.filename, |
| 221 |
self.startlineno) |
| 222 |
|
| 223 |
def getcode(self): |
| 224 |
return self.code |
| 225 |
|
| 226 |
def has_valid_code(self): |
| 227 |
return self.code is not None |
| 228 |
|
| 229 |
def getopcode(self): |
| 230 |
if self.code is None: |
| 231 |
return None |
| 232 |
return self.code.get_opcode_from_info(self) |
| 233 |
|
| 234 |
def getlineno(self): |
| 235 |
code = self.getopcode() |
| 236 |
if code is None: |
| 237 |
return None |
| 238 |
return code.lineno |
| 239 |
lineno = property(getlineno) |
| 240 |
|
| 241 |
def getline_starts_here(self): |
| 242 |
return self.getopcode().line_starts_here |
| 243 |
line_starts_here = property(getline_starts_here) |
| 244 |
|
| 245 |
def __repr__(self): |
| 246 |
return "[%s\n]" % "\n ".join([repr(op) for op in self.operations]) |
| 247 |
|
| 248 |
def pretty_print(self, out): |
| 249 |
pass |
| 250 |
|
| 251 |
class Function(object): |
| 252 |
filename = None |
| 253 |
name = None |
| 254 |
startlineno = 0 |
| 255 |
_linerange = None |
| 256 |
_lineset = None |
| 257 |
is_bytecode = False |
| 258 |
inline_level = None |
| 259 |
bytecode_name = None |
| 260 |
|
| 261 |
|
| 262 |
TraceForOpcode = TraceForOpcode |
| 263 |
|
| 264 |
def __init__(self, chunks, path, storage, inputargs=''): |
| 265 |
self.path = path |
| 266 |
self.inputargs = inputargs |
| 267 |
self.chunks = chunks |
| 268 |
for chunk in self.chunks: |
| 269 |
if chunk.bytecode_name is not None: |
| 270 |
self.startlineno = chunk.startlineno |
| 271 |
self.filename = chunk.filename |
| 272 |
self.name = chunk.name |
| 273 |
self.inline_level = chunk.inline_level |
| 274 |
break |
| 275 |
self.storage = storage |
| 276 |
|
| 277 |
@classmethod |
| 278 |
def from_operations(cls, operations, storage, limit=None, inputargs='', |
| 279 |
loopname=''): |
| 280 |
""" Slice given operation list into a chain of TraceForOpcode chunks. |
| 281 |
Also detect inlined functions and make them Function |
| 282 |
""" |
| 283 |
stack = [] |
| 284 |
|
| 285 |
def getpath(stack): |
| 286 |
return ",".join([str(len(v)) for v in stack]) |
| 287 |
|
| 288 |
def append_to_res(bc): |
| 289 |
if bc.inline_level is not None: |
| 290 |
if bc.inline_level == len(stack) - 1: |
| 291 |
pass |
| 292 |
elif bc.inline_level > len(stack) - 1: |
| 293 |
stack.append([]) |
| 294 |
else: |
| 295 |
while bc.inline_level + 1 < len(stack): |
| 296 |
last = stack.pop() |
| 297 |
stack[-1].append(cls(last, getpath(stack), storage)) |
| 298 |
stack[-1].append(bc) |
| 299 |
|
| 300 |
so_far = [] |
| 301 |
stack = [] |
| 302 |
nothing_yet = True |
| 303 |
for op in operations: |
| 304 |
if op.name == 'debug_merge_point': |
| 305 |
if so_far: |
| 306 |
opc = cls.TraceForOpcode(so_far, storage, loopname) |
| 307 |
if nothing_yet: |
| 308 |
nothing_yet = False |
| 309 |
for i in xrange(opc.inline_level + 1): |
| 310 |
stack.append([]) |
| 311 |
append_to_res(opc) |
| 312 |
if limit: |
| 313 |
break |
| 314 |
so_far = [] |
| 315 |
so_far.append(op) |
| 316 |
if so_far: |
| 317 |
append_to_res(cls.TraceForOpcode(so_far, storage, loopname)) |
| 318 |
|
| 319 |
if not stack: |
| 320 |
|
| 321 |
return cls([], getpath(stack), storage, inputargs) |
| 322 |
while True: |
| 323 |
next = stack.pop() |
| 324 |
if not stack: |
| 325 |
return cls(next, getpath(stack), storage, inputargs) |
| 326 |
stack[-1].append(cls(next, getpath(stack), storage)) |
| 327 |
|
| 328 |
|
| 329 |
def getlinerange(self): |
| 330 |
if self._linerange is None: |
| 331 |
self._compute_linerange() |
| 332 |
return self._linerange |
| 333 |
linerange = property(getlinerange) |
| 334 |
|
| 335 |
def getlineset(self): |
| 336 |
if self._lineset is None: |
| 337 |
self._compute_linerange() |
| 338 |
return self._lineset |
| 339 |
lineset = property(getlineset) |
| 340 |
|
| 341 |
def has_valid_code(self): |
| 342 |
for chunk in self.chunks: |
| 343 |
if chunk.has_valid_code(): |
| 344 |
return True |
| 345 |
return False |
| 346 |
|
| 347 |
def _compute_linerange(self): |
| 348 |
self._lineset = set() |
| 349 |
minline = sys.maxint |
| 350 |
maxline = -1 |
| 351 |
for chunk in self.chunks: |
| 352 |
if chunk.is_bytecode and chunk.has_valid_code(): |
| 353 |
lineno = chunk.lineno |
| 354 |
minline = min(minline, lineno) |
| 355 |
maxline = max(maxline, lineno) |
| 356 |
if chunk.line_starts_here or len(chunk.operations) > 1: |
| 357 |
self._lineset.add(lineno) |
| 358 |
if minline == sys.maxint: |
| 359 |
minline = 0 |
| 360 |
maxline = 0 |
| 361 |
self._linerange = minline, maxline |
| 362 |
|
| 363 |
def repr(self): |
| 364 |
if self.filename is None: |
| 365 |
return self.chunks[0].bytecode_name |
| 366 |
return "%s, file '%s', line %d" % (self.name, self.filename, |
| 367 |
self.startlineno) |
| 368 |
|
| 369 |
def __repr__(self): |
| 370 |
return "[%s]" % ", ".join([repr(chunk) for chunk in self.chunks]) |
| 371 |
|
| 372 |
def pretty_print(self, out): |
| 373 |
print("Loop starting at %s in %s at %d" % |
| 374 |
(self.name, self.filename, self.startlineno), file=out) |
| 375 |
lineno = -1 |
| 376 |
for chunk in self.chunks: |
| 377 |
if chunk.filename is not None and chunk.lineno != lineno: |
| 378 |
lineno = chunk.lineno |
| 379 |
source = chunk.getcode().source[chunk.lineno - |
| 380 |
chunk.startlineno] |
| 381 |
print(" ", source, file=out) |
| 382 |
chunk.pretty_print(out) |
| 383 |
|
| 384 |
|
| 385 |
def adjust_bridges(loop, bridges): |
| 386 |
""" Slice given loop according to given bridges to follow. Returns a plain |
| 387 |
list of operations. |
| 388 |
""" |
| 389 |
ops = loop.operations |
| 390 |
res = [] |
| 391 |
i = 0 |
| 392 |
while i < len(ops): |
| 393 |
op = ops[i] |
| 394 |
if op.is_guard() and bridges.get('loop-' + hex(op.guard_no)[2:], None): |
| 395 |
res.append(op) |
| 396 |
i = 0 |
| 397 |
if hasattr(op.bridge, 'force_asm'): |
| 398 |
op.bridge.force_asm() |
| 399 |
ops = op.bridge.operations |
| 400 |
else: |
| 401 |
res.append(op) |
| 402 |
i += 1 |
| 403 |
return res |
| 404 |
|
| 405 |
def parse_addresses(part, callback=None): |
| 406 |
hex_re = '0x(-?[\da-f]+)' |
| 407 |
addrs = {} |
| 408 |
if callback is None: |
| 409 |
def callback(addr, stop_addr, bootstrap_addr, name, code_name): |
| 410 |
addrs.setdefault(bootstrap_addr, []).append(name) |
| 411 |
for entry in part: |
| 412 |
m = re.search('has address %(hex)s to %(hex)s \(bootstrap %(hex)s' % |
| 413 |
{'hex': hex_re}, entry) |
| 414 |
if not m: |
| 415 |
|
| 416 |
m = re.search('has address ' + hex_re + ' to ' + hex_re, entry) |
| 417 |
addr = int(m.group(1), 16) |
| 418 |
bootstrap_addr = addr |
| 419 |
stop_addr = int(m.group(2), 16) |
| 420 |
entry = entry.lower() |
| 421 |
m = re.search('guard ' + hex_re, entry) |
| 422 |
name = 'guard ' + m.group(1) |
| 423 |
code_name = 'bridge' |
| 424 |
else: |
| 425 |
name = entry[:entry.find('(') - 1].lower() |
| 426 |
addr = int(m.group(1), 16) |
| 427 |
stop_addr = int(m.group(2), 16) |
| 428 |
bootstrap_addr = int(m.group(3), 16) |
| 429 |
code_name = entry[entry.find('(') + 1:m.span(0)[0] - 2] |
| 430 |
callback(addr, stop_addr, bootstrap_addr, name, code_name) |
| 431 |
return addrs |
| 432 |
|
| 433 |
def import_log(logname, ParserCls=SimpleParser): |
| 434 |
log = parse_log_file(logname) |
| 435 |
addrs = parse_addresses(extract_category(log, 'jit-backend-addr')) |
| 436 |
from rpython.jit.backend.tool.viewcode import World |
| 437 |
world = World() |
| 438 |
for entry in extract_category(log, 'jit-backend-dump'): |
| 439 |
world.parse(entry.splitlines(True)) |
| 440 |
dumps = {} |
| 441 |
for r in world.ranges: |
| 442 |
for pos1 in range(r.addr, r.addr + len(r.data)): |
| 443 |
if pos1 in addrs and addrs[pos1]: |
| 444 |
name = addrs[pos1].pop(0) |
| 445 |
data = r.data.encode('hex') |
| 446 |
dumps[name] = (world.backend_name, r.addr, data) |
| 447 |
loops = [] |
| 448 |
cat = extract_category(log, 'jit-log-opt') |
| 449 |
if not cat: |
| 450 |
cat = extract_category(log, 'jit-log-rewritten') |
| 451 |
if not cat: |
| 452 |
cat = extract_category(log, 'jit-log-noopt') |
| 453 |
for entry in cat: |
| 454 |
parser = ParserCls(entry, None, {}, 'lltype', None, |
| 455 |
nonstrict=True) |
| 456 |
loop = parser.parse() |
| 457 |
comm = loop.comment |
| 458 |
comm = comm.lower() |
| 459 |
if comm.startswith('# bridge'): |
| 460 |
m = re.search('guard 0x(-?[\da-f]+)', comm) |
| 461 |
name = 'guard ' + m.group(1) |
| 462 |
elif "(" in comm: |
| 463 |
name = comm[2:comm.find('(')-1] |
| 464 |
else: |
| 465 |
name = " ".join(comm[2:].split(" ", 2)[:2]) |
| 466 |
if name in dumps: |
| 467 |
bname, start_ofs, dump = dumps[name] |
| 468 |
loop.force_asm = (lambda dump=dump, start_ofs=start_ofs, |
| 469 |
bname=bname, loop=loop: |
| 470 |
parser.postprocess(loop, backend_tp=bname, |
| 471 |
backend_dump=dump, |
| 472 |
dump_start=start_ofs)) |
| 473 |
loops += split_trace(loop) |
| 474 |
return log, loops |
| 475 |
|
| 476 |
def split_trace(trace): |
| 477 |
labels = [0] |
| 478 |
if trace.comment and 'Guard' in trace.comment: |
| 479 |
descrs = ['bridge %d' % int( |
| 480 |
re.search('Guard 0x(-?[\da-f]+)', trace.comment).group(1), 16)] |
| 481 |
else: |
| 482 |
descrs = ['entry ' + re.search('Loop (\d+)', trace.comment).group(1)] |
| 483 |
for i, op in enumerate(trace.operations): |
| 484 |
if op.name == 'label': |
| 485 |
labels.append(i) |
| 486 |
descrs.append(op.descr) |
| 487 |
labels.append(len(trace.operations) - 1) |
| 488 |
parts = [] |
| 489 |
for i in range(len(labels) - 1): |
| 490 |
start, stop = labels[i], labels[i+1] |
| 491 |
part = copy(trace) |
| 492 |
part.operations = trace.operations[start : stop + 1] |
| 493 |
part.descr = descrs[i] |
| 494 |
part.comment = trace.comment |
| 495 |
parts.append(part) |
| 496 |
|
| 497 |
return parts |
| 498 |
|
| 499 |
def parse_log_counts(input, loops): |
| 500 |
if not input: |
| 501 |
return |
| 502 |
lines = input[-1].splitlines() |
| 503 |
mapping = {} |
| 504 |
for loop in loops: |
| 505 |
mapping[loop.descr] = loop |
| 506 |
for line in lines: |
| 507 |
if line: |
| 508 |
num, count = line.split(':', 2) |
| 509 |
try: |
| 510 |
mapping[num].count = int(count) |
| 511 |
except KeyError: |
| 512 |
pass |
| 513 |
|
| 514 |
def mangle_descr(descr): |
| 515 |
if descr.startswith('TargetToken('): |
| 516 |
return descr[len('TargetToken('):-1] |
| 517 |
if descr.startswith('<Guard'): |
| 518 |
return 'bridge-' + str(int(descr[len('<Guard0x'):-1], 16)) |
| 519 |
if descr.startswith('<Loop'): |
| 520 |
return 'entry-' + descr[len('<Loop'):-1] |
| 521 |
return descr.replace(" ", '-') |
| 522 |
|
| 523 |
|
| 524 |
if __name__ == '__main__': |
| 525 |
import_log(sys.argv[1]) |