Commits

Antonio Cuni committed fabd77a

add a way to get the code by the id

Comments (0)

Files changed (2)

pypy/module/pypyjit/test_pypy_c/model.py

 class Log(object):
     def __init__(self, func, rawtraces):
         storage = LoopStorage()
-        storage.ids = self.find_ids(func)
         traces = [parse(rawtrace) for rawtrace in rawtraces]
         traces = storage.reconnect_loops(traces)
         self.loops = [LoopWithIds.from_trace(trace, storage) for trace in traces]
         return [loop for loop in self.loops
                 if loop.filename == filename and self._filter(loop, **kwds)]
 
+    def by_id(self, id, **kwds):
+        return [loop for loop in self.loops
+                if loop.has_id(id) and self._filter(loop, **kwds)]
+
 
 class LoopWithIds(Function):
 
     is_entry_bridge = False
 
+    def __init__(self, *args, **kwds):
+        Function.__init__(self, *args, **kwds)
+        self.compute_ids()
+
     @classmethod
     def from_trace(cls, trace, storage):
         res = cls.from_operations(trace.operations, storage)
-        if 'entry bridge' in trace.comment:
-            res.is_entry_bridge = True
+        res.is_entry_bridge = 'entry bridge' in trace.comment
         return res
+
+    def compute_ids(self):
+        self.ids = set()
+        self.code = None
+        if not self.filename:
+            return
+        self.code = self.chunks[0].getcode()
+        ids = find_ids(self.code)
+        all_my_opcodes = self.get_set_of_opcodes()
+        # XXX: for now, we just look for the first opcode in the id range
+        for id, opcodes in ids.iteritems():
+            targetop = opcodes[0]
+            if targetop in all_my_opcodes:
+                self.ids.add(id)
+
+    def get_set_of_opcodes(self):
+        res = set()
+        for chunk in self.chunks:
+            opcode = self.code.map[chunk.bytecode_no]
+            res.add(opcode)
+        return res
+
+    def has_id(self, id):
+        return id in self.ids

pypy/module/pypyjit/test_pypy_c/test_model.py

     def test_parse_jitlog(self):
         def f():
             i = 0
-            while i < 1003: # default threshold is 10
-                i += 1 # ID: increment
+            while i < 1003:
+                i += 1
             return i
         #
         log = self.run(f)
         #
         loops = log.by_filename(self.filepath, is_entry_bridge='*')
         assert len(loops) == 2
+
+    def test_by_id(self):
+        def f():
+            i = 0
+            while i < 1003:
+                i += 1 # ID: increment
+            return i
+        #
+        log = self.run(f)
+        loop, = log.by_id('increment')
+        assert loop.filename == self.filepath
+        assert loop.code.co.co_name == 'f'