[pypy-svn] pypy default: add a way to get the code by the id

antocuni commits-noreply at bitbucket.org
Fri Feb 18 18:31:04 CET 2011


Author: Antonio Cuni <anto.cuni at gmail.com>
Branch: 
Changeset: r42180:fabd77a12b07
Date: 2011-02-18 18:19 +0100
http://bitbucket.org/pypy/pypy/changeset/fabd77a12b07/

Log:	add a way to get the code by the id

diff --git a/pypy/module/pypyjit/test_pypy_c/model.py b/pypy/module/pypyjit/test_pypy_c/model.py
--- a/pypy/module/pypyjit/test_pypy_c/model.py
+++ b/pypy/module/pypyjit/test_pypy_c/model.py
@@ -40,7 +40,6 @@
 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]
@@ -52,14 +51,45 @@
         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

diff --git a/pypy/module/pypyjit/test_pypy_c/test_model.py b/pypy/module/pypyjit/test_pypy_c/test_model.py
--- a/pypy/module/pypyjit/test_pypy_c/test_model.py
+++ b/pypy/module/pypyjit/test_pypy_c/test_model.py
@@ -79,8 +79,8 @@
     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)
@@ -95,3 +95,15 @@
         #
         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'


More information about the Pypy-commit mailing list