| 1 |
|
| 2 |
""" Usage: traceviewer.py [--use-threshold] loopfile |
| 3 |
""" |
| 4 |
from __future__ import print_function |
| 5 |
|
| 6 |
import optparse |
| 7 |
import sys |
| 8 |
import re |
| 9 |
import math |
| 10 |
import py |
| 11 |
|
| 12 |
from rpython.translator.tool.graphpage import GraphPage |
| 13 |
from rpython.translator.tool.make_dot import DotGen |
| 14 |
from rpython.tool import logparser |
| 15 |
from rpython.tool import progressbar |
| 16 |
|
| 17 |
class SubPage(GraphPage): |
| 18 |
def compute(self, graph): |
| 19 |
self.links = {} |
| 20 |
dotgen = DotGen(str(graph.no)) |
| 21 |
|
| 22 |
counter = 0 |
| 23 |
lines = graph.content.split("\n") |
| 24 |
lines_so_far = [] |
| 25 |
for line in lines: |
| 26 |
line = re.sub('.\[.*\]', '', line) |
| 27 |
boxes = re.findall('([pif]\d+)', line) |
| 28 |
for box in boxes: |
| 29 |
self.links[box] = box |
| 30 |
if 'debug_merge_point' in line: |
| 31 |
dotgen.emit_node('node%d' % counter, shape="box", |
| 32 |
label="\n".join(lines_so_far)) |
| 33 |
if counter != 0: |
| 34 |
dotgen.emit_edge('node%d' % (counter - 1), 'node%d' % counter) |
| 35 |
counter += 1 |
| 36 |
lines_so_far = [] |
| 37 |
lines_so_far.append(line) |
| 38 |
dotgen.emit_node('node%d' % counter, shape="box", |
| 39 |
label="\n".join(lines_so_far)) |
| 40 |
dotgen.emit_edge('node%d' % (counter - 1), 'node%d' % counter) |
| 41 |
self.source = dotgen.generate(target=None) |
| 42 |
|
| 43 |
class Page(GraphPage): |
| 44 |
def compute(self, graphs, counts): |
| 45 |
dotgen = DotGen('trace') |
| 46 |
self.loops = graphs |
| 47 |
self.links = {} |
| 48 |
self.cache = {} |
| 49 |
for loop in self.loops: |
| 50 |
loop.generate(dotgen, counts) |
| 51 |
loop.getlinks(self.links) |
| 52 |
self.cache["loop" + str(loop.no)] = loop |
| 53 |
self.source = dotgen.generate(target=None) |
| 54 |
|
| 55 |
def followlink(self, label): |
| 56 |
return SubPage(self.cache[label]) |
| 57 |
|
| 58 |
BOX_COLOR = (128, 0, 96) |
| 59 |
|
| 60 |
GUARDNO_RE = "((0x)?[\da-f]+)" |
| 61 |
def guard_number(guardno_match): |
| 62 |
if (len(guardno_match) == 1 |
| 63 |
or guardno_match[1] != "0x" |
| 64 |
): |
| 65 |
return int(guardno_match[0]) |
| 66 |
else: |
| 67 |
return int(guardno_match[0], 16) |
| 68 |
|
| 69 |
def guard_number_string(guardno_match): |
| 70 |
return guardno_match[0] |
| 71 |
|
| 72 |
class BasicBlock(object): |
| 73 |
counter = 0 |
| 74 |
startlineno = 0 |
| 75 |
|
| 76 |
def __init__(self, content): |
| 77 |
self.content = content |
| 78 |
self.no = self.counter |
| 79 |
self.__class__.counter += 1 |
| 80 |
|
| 81 |
def name(self): |
| 82 |
return 'node' + str(self.no) |
| 83 |
|
| 84 |
def getlinks(self, links): |
| 85 |
links[self.linksource] = self.name() |
| 86 |
|
| 87 |
def generate(self, dotgen, counts): |
| 88 |
val = counts.get(self.key, 0) |
| 89 |
if False: |
| 90 |
fillcolor = get_gradient_color(self.ratio) |
| 91 |
else: |
| 92 |
fillcolor = "white" |
| 93 |
dotgen.emit_node(self.name(), label=self.header, |
| 94 |
shape='box', fillcolor=fillcolor) |
| 95 |
|
| 96 |
def get_content(self): |
| 97 |
return self._content |
| 98 |
|
| 99 |
def set_content(self, content): |
| 100 |
self._content = content |
| 101 |
groups = re.findall('Guard' + GUARDNO_RE, content) |
| 102 |
if not groups: |
| 103 |
self.first_guard = -1 |
| 104 |
self.last_guard = -1 |
| 105 |
else: |
| 106 |
|
| 107 |
groups = sorted(map(guard_number, groups)) |
| 108 |
self.first_guard = groups[0] |
| 109 |
self.last_guard = groups[-1] |
| 110 |
|
| 111 |
content = property(get_content, set_content) |
| 112 |
|
| 113 |
def get_gradient_color(ratio): |
| 114 |
if ratio == 0: |
| 115 |
return 'white' |
| 116 |
ratio = math.log(ratio) |
| 117 |
|
| 118 |
|
| 119 |
|
| 120 |
|
| 121 |
ratio -= 1.8 |
| 122 |
ratio = math.atan(ratio * 5) / (math.pi/2) |
| 123 |
|
| 124 |
if ratio >= 0.0: |
| 125 |
|
| 126 |
return '#FF%02X00' % (int((1.0-ratio)*255.5),) |
| 127 |
else: |
| 128 |
|
| 129 |
return '#%02XFF00' % (int((1.0+ratio)*255.5),) |
| 130 |
|
| 131 |
class FinalBlock(BasicBlock): |
| 132 |
def __init__(self, content, target): |
| 133 |
self.target = target |
| 134 |
BasicBlock.__init__(self, content) |
| 135 |
|
| 136 |
def postprocess(self, loops, memo, counts): |
| 137 |
postprocess_loop(self.target, loops, memo, counts) |
| 138 |
|
| 139 |
def generate(self, dotgen, counts): |
| 140 |
BasicBlock.generate(self, dotgen, counts) |
| 141 |
if self.target is not None: |
| 142 |
dotgen.emit_edge(self.name(), self.target.name()) |
| 143 |
|
| 144 |
class Block(BasicBlock): |
| 145 |
def __init__(self, content, left, right): |
| 146 |
self.left = left |
| 147 |
self.right = right |
| 148 |
BasicBlock.__init__(self, content) |
| 149 |
|
| 150 |
def postprocess(self, loops, memo, counts): |
| 151 |
postprocess_loop(self.left, loops, memo, counts) |
| 152 |
postprocess_loop(self.right, loops, memo, counts) |
| 153 |
|
| 154 |
def generate(self, dotgen, counts): |
| 155 |
BasicBlock.generate(self, dotgen, counts) |
| 156 |
dotgen.emit_edge(self.name(), self.left.name()) |
| 157 |
dotgen.emit_edge(self.name(), self.right.name()) |
| 158 |
|
| 159 |
def split_one_loop(real_loops, guard_s, guard_content, lineno, no, allloops): |
| 160 |
for i in range(len(allloops) - 1, -1, -1): |
| 161 |
loop = allloops[i] |
| 162 |
if no < loop.first_guard or no > loop.last_guard: |
| 163 |
continue |
| 164 |
content = loop.content |
| 165 |
pos = content.find(guard_s + '>') |
| 166 |
if pos != -1: |
| 167 |
newpos = content.rfind("\n", 0, pos) |
| 168 |
oldpos = content.find("\n", pos) |
| 169 |
assert newpos != -1 |
| 170 |
if oldpos == -1: |
| 171 |
oldpos = len(content) |
| 172 |
if isinstance(loop, Block): |
| 173 |
left = Block(content[oldpos:], loop.left, loop.right) |
| 174 |
else: |
| 175 |
left = FinalBlock(content[oldpos:], None) |
| 176 |
right = FinalBlock(guard_content, None) |
| 177 |
mother = Block(content[:oldpos], len(allloops), len(allloops) + 1) |
| 178 |
allloops[i] = mother |
| 179 |
allloops.append(left) |
| 180 |
allloops.append(right) |
| 181 |
if hasattr(loop, 'loop_no'): |
| 182 |
real_loops[loop.loop_no] = mother |
| 183 |
mother.loop_no = loop.loop_no |
| 184 |
mother.guard_s = guard_s |
| 185 |
mother.startlineno = loop.startlineno |
| 186 |
left.startlineno = loop.startlineno + content.count("\n", 0, pos) |
| 187 |
right.startlineno = lineno |
| 188 |
return |
| 189 |
else: |
| 190 |
raise Exception("Did not find") |
| 191 |
|
| 192 |
MAX_LOOPS = 300 |
| 193 |
LINE_CUTOFF = 300 |
| 194 |
|
| 195 |
def fillappend(lst, elem, no): |
| 196 |
there = len(lst) |
| 197 |
assert there <= no |
| 198 |
for _ in range(no - there): |
| 199 |
lst.append(None) |
| 200 |
lst.append(elem) |
| 201 |
|
| 202 |
def splitloops(loops): |
| 203 |
real_loops = [] |
| 204 |
counter = 1 |
| 205 |
bar = progressbar.ProgressBar(color='blue') |
| 206 |
allloops = [] |
| 207 |
for i, loop in enumerate(loops): |
| 208 |
if i > MAX_LOOPS: |
| 209 |
return real_loops, allloops |
| 210 |
bar.render((i * 100) / len(loops)) |
| 211 |
firstline = loop[:loop.find("\n")] |
| 212 |
m = re.match('# Loop (\d+)', firstline) |
| 213 |
if m: |
| 214 |
no = int(m.group(1)) |
| 215 |
assert len(real_loops) <= no |
| 216 |
_loop = FinalBlock(loop, None) |
| 217 |
fillappend(real_loops, _loop, no) |
| 218 |
_loop.startlineno = counter |
| 219 |
_loop.loop_no = no |
| 220 |
allloops.append(_loop) |
| 221 |
else: |
| 222 |
m = re.search("bridge out of Guard " + GUARDNO_RE, firstline) |
| 223 |
assert m |
| 224 |
guard_s = 'Guard' + guard_number_string(m.groups()) |
| 225 |
split_one_loop(real_loops, guard_s, loop, counter, |
| 226 |
guard_number(m.groups()), allloops) |
| 227 |
counter += loop.count("\n") + 2 |
| 228 |
return real_loops, allloops |
| 229 |
|
| 230 |
|
| 231 |
def find_name_key(l): |
| 232 |
m = re.search("debug_merge_point\((?:\d+,\ )*'(.*)'(?:, \d+)*\)", l.content) |
| 233 |
if m is None: |
| 234 |
|
| 235 |
return '?', '?' |
| 236 |
info = m.group(1) |
| 237 |
|
| 238 |
|
| 239 |
|
| 240 |
|
| 241 |
m = re.search("^(<code object (.*?)> (.*?))$", info) |
| 242 |
if m: |
| 243 |
return m.group(2) + " " + m.group(3), m.group(1) |
| 244 |
|
| 245 |
|
| 246 |
|
| 247 |
|
| 248 |
m = re.search("^((cffi_callback) <code object (.*?)>)$", info) |
| 249 |
if m: |
| 250 |
return "%s (%s)" %(m.group(3), m.group(2)), m.group(1) |
| 251 |
m = re.search("^((cffi_callback) <\?>)$", info) |
| 252 |
if m: |
| 253 |
return "? (%s)" %(m.group(2)), m.group(1) |
| 254 |
m = re.search("^((cffi_call_python) (.*))$", info) |
| 255 |
if m: |
| 256 |
return "%s (%s)" %(m.group(3), m.group(2)), m.group(1) |
| 257 |
|
| 258 |
|
| 259 |
|
| 260 |
m = re.search("^(\(((.+?) >> )?(#.*)\) \[(\d+)\].+?>(.*?)(?:\(\d+\))?)$", info) |
| 261 |
if m: |
| 262 |
if m.group(3): |
| 263 |
return "%s>>%s @ %s <%s>" % (m.group(3), m.group(4), m.group(5), m.group(6)), m.group(1) |
| 264 |
else: |
| 265 |
return "%s @ %s <%s>" % (m.group(4), m.group(5), m.group(6)), m.group(1) |
| 266 |
|
| 267 |
|
| 268 |
|
| 269 |
|
| 270 |
|
| 271 |
m = re.search("^((\d+): (.+?)(:? function: (.+?))?)$", info) |
| 272 |
if m: |
| 273 |
if m.group(5): |
| 274 |
return "%s @ %s <%s>" % (m.group(5), m.group(2), m.group(3)), m.group(1) |
| 275 |
else: |
| 276 |
return "? @ %s <%s>" % (m.group(2), m.group(3)), m.group(1) |
| 277 |
|
| 278 |
|
| 279 |
|
| 280 |
|
| 281 |
|
| 282 |
if info[0] == '(' and info[-1] == ')': |
| 283 |
if len(info) > 64: |
| 284 |
return info[:64] +'...', info |
| 285 |
|
| 286 |
|
| 287 |
|
| 288 |
|
| 289 |
|
| 290 |
|
| 291 |
|
| 292 |
return info, info |
| 293 |
|
| 294 |
def postprocess_loop(loop, loops, memo, counts): |
| 295 |
|
| 296 |
if loop in memo: |
| 297 |
return |
| 298 |
memo.add(loop) |
| 299 |
if loop is None: |
| 300 |
return |
| 301 |
name, loop.key = find_name_key(loop) |
| 302 |
opsno = loop.content.count("\n") |
| 303 |
lastline = loop.content[loop.content.rfind("\n", 0, len(loop.content) - 2):] |
| 304 |
m = re.search('descr=<Loop(\d+)', lastline) |
| 305 |
if m is not None: |
| 306 |
assert isinstance(loop, FinalBlock) |
| 307 |
loop.target = loops[int(m.group(1))] |
| 308 |
bcodes = loop.content.count('debug_merge_point') |
| 309 |
loop.linksource = "loop" + str(loop.no) |
| 310 |
loop.header = ("%s loop%d\nrun %s times\n%d operations\n%d opcodes" % |
| 311 |
(name, loop.no, counts.get(loop.key, '?'), opsno, bcodes)) |
| 312 |
loop.header += "\n" * (opsno / 100) |
| 313 |
if bcodes == 0: |
| 314 |
loop.ratio = opsno |
| 315 |
else: |
| 316 |
loop.ratio = float(opsno) / bcodes |
| 317 |
content = loop.content |
| 318 |
loop.content = "Logfile at %d\n" % loop.startlineno + content |
| 319 |
loop.postprocess(loops, memo, counts) |
| 320 |
|
| 321 |
def postprocess(loops, allloops, counts): |
| 322 |
for loop in allloops: |
| 323 |
if isinstance(loop, Block): |
| 324 |
loop.left = allloops[loop.left] |
| 325 |
loop.right = allloops[loop.right] |
| 326 |
memo = set() |
| 327 |
for loop in loops: |
| 328 |
postprocess_loop(loop, loops, memo, counts) |
| 329 |
|
| 330 |
class Counts(dict): |
| 331 |
pass |
| 332 |
|
| 333 |
def main(loopfile, use_threshold, view=True): |
| 334 |
countname = py.path.local(loopfile + '.count') |
| 335 |
if countname.check(): |
| 336 |
|
| 337 |
|
| 338 |
|
| 339 |
counts = Counts([]) |
| 340 |
l = list(sorted(counts.values())) |
| 341 |
if len(l) > 20 and use_threshold: |
| 342 |
counts.threshold = l[-20] |
| 343 |
else: |
| 344 |
counts.threshold = 0 |
| 345 |
for_print = [(v, k) for k, v in counts.iteritems()] |
| 346 |
for_print.sort() |
| 347 |
else: |
| 348 |
counts = {} |
| 349 |
log = logparser.parse_log_file(loopfile) |
| 350 |
loops = logparser.extract_category(log, "jit-log-opt-") |
| 351 |
real_loops, allloops = splitloops(loops) |
| 352 |
postprocess(real_loops, allloops, counts) |
| 353 |
if view: |
| 354 |
Page(allloops, counts).display() |
| 355 |
|
| 356 |
if __name__ == '__main__': |
| 357 |
parser = optparse.OptionParser(usage=__doc__) |
| 358 |
parser.add_option('--use-threshold', dest='use_threshold', |
| 359 |
action="store_true", default=False) |
| 360 |
options, args = parser.parse_args(sys.argv) |
| 361 |
if len(args) != 2: |
| 362 |
print(__doc__) |
| 363 |
sys.exit(1) |
| 364 |
main(args[1], options.use_threshold) |