Maciej Fijalkowski avatar Maciej Fijalkowski committed 97429dc Draft

tests, fixes and improvements to bridge handling

Comments (0)

Files changed (2)

_jitviewer2/traces.py

         self.operations = operations
         self.count = count
         self.name = name
+        self.bridges = {} # dictionary index -> bridge
+        self.jump_target = None # if there is a jump to a trace, this will
+        # be a pointer to that trace (which starts with a correct label)
+        self.continuation = None
+        # if the trace has a natural continuation (e.g. it's an entry bridge)
+        # this will be set to not None
 
 class TraceInfo(object):
     """ Stats
     """
-    def __init__(self, loop_run_counters):
+    def __init__(self, traces_by_key, loop_run_counters):
+        self.traces_by_key = traces_by_key
         self.loop_run_counters = loop_run_counters
 
 TARGET_TOKEN = 'descr=TargetToken('
         if op.startswith('label'):
             if not prev_splice:
                 yield -1, oplist[:i]
-                prev_splice = i
             else:
                 yield cur_addr, oplist[prev_splice:i]
+            prev_splice = i
             cur_addr = int(op[op.rfind(TARGET_TOKEN) + TARGET_TOKEN_LEN:-2])
     yield cur_addr, oplist[prev_splice:]
 
     for tp, no, counter in stats_data:
         counters[(tp, no)] = counter
     traces = []
+    traces_by_key = {}
     for tp, no, trace_to_split in traces_data:
+        prev_trace = None
         for addr, trace in split_traces(trace_to_split):
             name = names.get((tp, no), 'unknown')
             if addr == -1:
             else:
                 tp = 'l'
             key = (tp, addr)
-            traces.append(Trace(trace, name, counters[key], tp, no))
-    return traces, TraceInfo(counters)
+            trace = Trace(trace, name, counters[key], tp, no)
+            if prev_trace is not None:
+                prev_trace.continuation = trace
+            prev_trace = trace
+            traces.append(trace)
+            traces_by_key[key] = trace
+    return traces, TraceInfo(traces_by_key, counters)
 
-def attach_bridges_and_jumps(traces):
+def attach_bridges_and_jumps(traces, trace_info):
     """ Indexes bridges and jumps, adding them to bridges mapping on a trace    
     """
     for trace in traces:
-        for op in trace.operations:
+        for i, op in enumerate(trace.operations):
             if op.startswith('guard_'):
                 m = re.search('descr=<Guard(\d+)>', op)
                 num = int(m.group(1))
-                import pdb
-                pdb.set_trace()
+                if ('b', num) in trace_info.traces_by_key:
+                    bridge_target = trace_info.traces_by_key[('b', num)]
+                    trace.bridges[i] = bridge_target
+            if op.startswith('jump'):
+                m = re.search('descr=TargetToken\((\d+)\)', op)
+                no = int(m.group(1))
+                trace.jump_target = trace_info.traces_by_key[('l', no)]
 
 #def plot_traces(traces):
 #    """ Plot the 2D trace tree. Returns a list of (x, y, width, height, relative_size) tuples

testing/test_main.py

         ''')
         assert len(traces) == 6
         assert '<module>' in traces[0].name
+        assert traces[0].continuation is traces[1]
+        assert traces[1].continuation is traces[2]
+        assert traces[2].continuation is None
+        assert traces[1].count == 1
+        assert traces[0].count == 1
 
     def test_main_bridges(self):
         traces, info = self.run('bridges', '''
-        s = 0
-        for i in range(100000):
+        def f():
             s = 0
-            if i & 2:
-               s += 1
-            s += 2
+            for i in range(100000):
+                s = 0
+                if i & 2:
+                   s += 1
+                s += 2
+        f()
         ''')
-        attach_bridges_and_jumps(traces)
+        attach_bridges_and_jumps(traces, info)
+        assert traces[2].jump_target is traces[2]
+        assert len(traces[2].bridges) == 1
+        assert traces[2].bridges.values()[0] is traces[5]
 
     def test_main_subfunc(self):
         py.test.skip("not yet")
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.