Debian Codegraph @

rpython/tool/jitlogparser/storage.py

raw symbols SCIP
1
2 """ This file represents a storage mechanism that let us invent unique names
3 for all loops and bridges, so http requests can refer to them by name
4 """
5
6 import py
7 import os
8 import linecache
9 from rpython.tool.disassembler import dis
10 from rpython.tool.jitlogparser.module_finder import gather_all_code_objs
11
12 class LoopStorage(object):
13 def __init__(self, extrapath=None):
14 self.loops = None
15 self.functions = {}
16 self.codes = {}
17 self.disassembled_codes = {}
18 self.extrapath = extrapath
19
20 def load_code(self, fname):
21 try:
22 return self.codes[fname]
23 except KeyError:
24 if os.path.isabs(fname):
25 res = gather_all_code_objs(fname)
26 else:
27 if self.extrapath is None:
28 raise IOError("Cannot find %s" % fname)
29 res = gather_all_code_objs(os.path.join(self.extrapath, fname))
30 self.codes[fname] = res
31 return res
32
33 def disassemble_code(self, fname, startlineno, name, generic_format=False):
34 # 'generic_format' is False for PyPy2 (returns a
35 # disassembler.CodeRepresentation) or True otherwise (returns a
36 # GenericCode, without attempting any disassembly)
37 try:
38 if py.path.local(fname).check(file=False):
39 return None # cannot find source file
40 except py.error.EACCES:
41 return None # cannot open the file
42 key = (fname, startlineno, name)
43 try:
44 return self.disassembled_codes[key]
45 except KeyError:
46 pass
47 if generic_format:
48 res = GenericCode(fname, startlineno, name)
49 else:
50 codeobjs = self.load_code(fname)
51 if (startlineno, name) not in codeobjs:
52 # cannot find the code obj at this line: this can happen for
53 # various reasons, e.g. because the .py files changed since
54 # the log was produced, or because the co_firstlineno
55 # attribute of the code object is wrong (e.g., code objects
56 # produced by gateway.applevel(), such as the ones found in
57 # nanos.py)
58 return None
59 code = codeobjs[(startlineno, name)]
60 res = dis(code)
61 self.disassembled_codes[key] = res
62 return res
63
64 def reconnect_loops(self, loops):
65 """ Re-connect loops in a way that entry bridges are filtered out
66 and normal bridges are associated with guards. Returning list of
67 normal loops.
68 """
69 res = []
70 guard_dict = {}
71 for loop_no, loop in enumerate(loops):
72 for op in loop.operations:
73 if op.name.startswith('guard_') or op.name.startswith('vec_guard_'):
74 guard_dict[int(op.descr[len('<Guard0x'):-1], 16)] = (op, loop)
75 for loop in loops:
76 if loop.comment:
77 comment = loop.comment.strip()
78 if 'entry bridge' in comment:
79 pass
80 elif comment.startswith('# bridge out of'):
81 no = int(comment[len('# bridge out of Guard 0x'):].split(' ', 1)[0], 16)
82 op, parent = guard_dict[no]
83 op.bridge = loop
84 op.percentage = ((getattr(loop, 'count', 1) * 100) /
85 max(getattr(parent, 'count', 1), 1))
86 loop.no = no
87 continue
88 res.append(loop)
89 self.loops = res
90 return res
91
92
93 class GenericCode(object):
94 def __init__(self, fname, startlineno, name):
95 self.filename = fname
96 self.startlineno = startlineno
97 self.name = name
98 self._first_bytecodes = {} # {lineno: bytecode_name}
99 self._source = None
100
101 def __repr__(self):
102 return 'GenericCode(%r, %r, %r)' % (
103 self.filename, self.startlineno, self.name)
104
105 def get_opcode_from_info(self, info):
106 lineno = ~info.bytecode_no
107 bname = info.bytecode_name
108 if self._first_bytecodes.setdefault(lineno, bname) == bname:
109 # this is the first opcode of the line---or, at least,
110 # the first time we ask for an Opcode on that line.
111 line_starts_here = True
112 else:
113 line_starts_here = False
114 return GenericOpcode(lineno, line_starts_here, bname)
115
116 @property
117 def source(self):
118 if self._source is None:
119 src = linecache.getlines(self.filename)
120 if self.startlineno > 0:
121 src = src[self.startlineno - 1:]
122 self._source = [s.rstrip('\n\r') for s in src]
123 return self._source
124
125
126 class GenericOpcode(object):
127 def __init__(self, lineno, line_starts_here, bytecode_extra=''):
128 self.lineno = lineno
129 self.line_starts_here = line_starts_here
130 self.bytecode_extra = bytecode_extra
131
132 def __repr__(self):
133 return 'GenericOpcode(%r, %r, %r)' % (
134 self.lineno, self.line_starts_here, self.bytecode_extra)
135
136 def __eq__(self, other):
137 if not isinstance(other, GenericOpcode):
138 return NotImplemented
139 return (self.lineno == other.lineno and
140 self.line_starts_here == other.line_starts_here and
141 self.bytecode_extra == other.bytecode_extra)
142
143 def __ne__(self, other):
144 if not isinstance(other, GenericOpcode):
145 return NotImplemented
146 return not (self == other)
147
148 def __hash__(self):
149 return hash((self.lineno, self.line_starts_here, self.bytecode_extra))
150
151 def match_name(self, opcode_name):
152 return (self.bytecode_extra == opcode_name or
153 self.bytecode_extra.endswith(' ' + opcode_name))