Пример #1
0
    def test_on_compile(self):
        import pypyjit
        all = []

        def hook(name, looptype, tuple_or_guard_no, ops, asmstart, asmlen):
            all.append((name, looptype, tuple_or_guard_no, ops))

        self.on_compile()
        pypyjit.set_compile_hook(hook)
        assert not all
        self.on_compile()
        assert len(all) == 1
        elem = all[0]
        assert elem[0] == 'pypyjit'
        assert elem[2][0].co_name == 'function'
        assert elem[2][1] == 0
        assert elem[2][2] == False
        assert len(elem[3]) == 4
        int_add = elem[3][0]
        dmp = elem[3][1]
        assert isinstance(dmp, pypyjit.DebugMergePoint)
        assert dmp.pycode is self.f.func_code
        assert dmp.greenkey == (self.f.func_code, 0, False)
        assert dmp.call_depth == 0
        assert dmp.call_id == 0
        assert int_add.name == 'int_add'
        assert int_add.num == self.int_add_num
        self.on_compile_bridge()
        assert len(all) == 2
        pypyjit.set_compile_hook(None)
        self.on_compile()
        assert len(all) == 2
Пример #2
0
    def test_on_compile(self):
        import pypyjit
        all = []

        def hook(name, looptype, tuple_or_guard_no, ops, asmstart, asmlen):
            all.append((name, looptype, tuple_or_guard_no, ops))

        self.on_compile()
        pypyjit.set_compile_hook(hook)
        assert not all
        self.on_compile()
        assert len(all) == 1
        elem = all[0]
        assert elem[0] == 'pypyjit'
        assert elem[2][0].co_name == 'function'
        assert elem[2][1] == 0
        assert elem[2][2] == False
        assert len(elem[3]) == 4
        int_add = elem[3][0]
        #assert int_add.name == 'int_add'
        assert int_add.num == self.int_add_num
        self.on_compile_bridge()
        assert len(all) == 2
        pypyjit.set_compile_hook(None)
        self.on_compile()
        assert len(all) == 2
Пример #3
0
    def test_on_compile(self):
        import pypyjit
        all = []

        def hook(name, looptype, tuple_or_guard_no, ops, asmstart, asmlen):
            all.append((name, looptype, tuple_or_guard_no, ops))
        
        self.on_compile()
        pypyjit.set_compile_hook(hook)
        assert not all
        self.on_compile()
        assert len(all) == 1
        elem = all[0]
        assert elem[0] == 'pypyjit'
        assert elem[2][0].co_name == 'function'
        assert elem[2][1] == 0
        assert elem[2][2] == False
        assert len(elem[3]) == 4
        int_add = elem[3][0]
        #assert int_add.name == 'int_add'
        assert int_add.num == self.int_add_num
        self.on_compile_bridge()
        assert len(all) == 2
        pypyjit.set_compile_hook(None)
        self.on_compile()
        assert len(all) == 2
Пример #4
0
    def test_on_compile(self):
        import pypyjit
        all = []

        def hook(name, looptype, tuple_or_guard_no, ops, asmstart, asmlen):
            all.append((name, looptype, tuple_or_guard_no, ops))

        self.on_compile()
        pypyjit.set_compile_hook(hook)
        assert not all
        self.on_compile()
        assert len(all) == 1
        elem = all[0]
        assert elem[0] == 'pypyjit'
        assert elem[2][0].co_name == 'function'
        assert elem[2][1] == 0
        assert elem[2][2] == False
        assert len(elem[3]) == 4
        int_add = elem[3][0]
        dmp = elem[3][1]
        assert isinstance(dmp, pypyjit.DebugMergePoint)
        assert dmp.pycode is self.f.func_code
        assert dmp.greenkey == (self.f.func_code, 0, False)
        assert dmp.call_depth == 0
        assert dmp.call_id == 0
        assert int_add.name == 'int_add'
        assert int_add.num == self.int_add_num
        self.on_compile_bridge()
        assert len(all) == 2
        pypyjit.set_compile_hook(None)
        self.on_compile()
        assert len(all) == 2
Пример #5
0
 def test_on_compile_crashes(self):
     import pypyjit
     loops = []
     def hook(loop):
         loops.append(loop)
     pypyjit.set_compile_hook(hook)
     self.on_compile()
     loop = loops[0]
     op = loop.operations[2]
     assert op.name == 'guard_nonnull'
Пример #6
0
 def test_on_compile_crashes(self):
     import pypyjit
     loops = []
     def hook(loop):
         loops.append(loop)
     pypyjit.set_compile_hook(hook)
     self.on_compile()
     loop = loops[0]
     op = loop.operations[2]
     assert op.name == 'guard_nonnull'
Пример #7
0
    def test_on_compile_types(self):
        import pypyjit
        l = []

        def hook(*args):
            l.append(args)

        pypyjit.set_compile_hook(hook)
        self.on_compile()
        op = l[0][3][1]
        assert isinstance(op, pypyjit.ResOperation)
        assert 'function' in repr(op)
Пример #8
0
    def test_on_compile_types(self):
        import pypyjit
        l = []

        def hook(*args):
            l.append(args)

        pypyjit.set_compile_hook(hook)
        self.on_compile()
        dmp = l[0][3][1]
        assert isinstance(dmp, pypyjit.DebugMergePoint)
        assert dmp.code is self.f.func_code
Пример #9
0
    def test_on_compile_types(self):
        import pypyjit
        l = []

        def hook(info):
            l.append(info)

        pypyjit.set_compile_hook(hook)
        self.on_compile()
        op = l[0].operations[1]
        assert isinstance(op, pypyjit.ResOperation)
        assert 'function' in repr(op)
Пример #10
0
 def test_on_compile_crashes(self):
     import pypyjit
     loops = []
     def hook(loop):
         loops.append(loop)
     pypyjit.set_compile_hook(hook)
     self.on_compile()
     loop = loops[0]
     op = loop.operations[2]
     # Should not crash the interpreter
     raises(IndexError, op.getarg, 2)
     assert op.name == 'guard_nonnull'
     raises(NotImplementedError, op.getarg(0).getint)
Пример #11
0
 def test_on_compile_crashes(self):
     import pypyjit
     loops = []
     def hook(loop):
         loops.append(loop)
     pypyjit.set_compile_hook(hook)
     self.on_compile()
     loop = loops[0]
     op = loop.operations[2]
     # Should not crash the interpreter
     raises(IndexError, op.getarg, 2)
     assert op.name == 'guard_nonnull'
     raises(NotImplementedError, op.getarg(0).getint)
Пример #12
0
    def test_non_reentrant(self):
        import pypyjit
        l = []

        def hook(*args):
            l.append(None)
            self.on_compile()
            self.on_compile_bridge()

        pypyjit.set_compile_hook(hook)
        self.on_compile()
        assert len(l) == 1  # and did not crash
        self.on_compile_bridge()
        assert len(l) == 2  # and did not crash
Пример #13
0
    def test_non_reentrant(self):
        import pypyjit
        l = []

        def hook(*args):
            l.append(None)
            self.on_compile()
            self.on_compile_bridge()

        pypyjit.set_compile_hook(hook)
        self.on_compile()
        assert len(l) == 1 # and did not crash
        self.on_compile_bridge()
        assert len(l) == 2 # and did not crash
Пример #14
0
    def test_on_optimize(self):
        import pypyjit
        l = []

        def hook(info):
            l.append(info.jitdriver_name)

        def optimize_hook(info):
            return []

        pypyjit.set_compile_hook(hook)
        pypyjit.set_optimize_hook(optimize_hook)
        self.on_optimize()
        self.on_compile()
        assert l == ['pypyjit']
Пример #15
0
    def test_on_optimize(self):
        import pypyjit
        l = []

        def hook(name, looptype, tuple_or_guard_no, ops, *args):
            l.append(ops)

        def optimize_hook(name, looptype, tuple_or_guard_no, ops):
            return []

        pypyjit.set_compile_hook(hook)
        pypyjit.set_optimize_hook(optimize_hook)
        self.on_optimize()
        self.on_compile()
        assert l == [[]]
Пример #16
0
    def test_on_optimize(self):
        import pypyjit
        l = []

        def hook(info):
            l.append(info.jitdriver_name)

        def optimize_hook(info):
            return []

        pypyjit.set_compile_hook(hook)
        pypyjit.set_optimize_hook(optimize_hook)
        self.on_optimize()
        self.on_compile()
        assert l == ['pypyjit']
Пример #17
0
    def test_on_optimize(self):
        import pypyjit
        l = []

        def hook(name, looptype, tuple_or_guard_no, ops, *args):
            l.append(ops)

        def optimize_hook(name, looptype, tuple_or_guard_no, ops):
            return []

        pypyjit.set_compile_hook(hook)
        pypyjit.set_optimize_hook(optimize_hook)
        self.on_optimize()
        self.on_compile()
        assert l == [[]]
Пример #18
0
 def test_no_jit(self):
     import pypyjit
     was_called = []
     def should_not_be_called(*args, **kwds):
         was_called.append((args, kwds))
     try:
         pypyjit.set_param('off')
         pypyjit.set_compile_hook(should_not_be_called)
         def f():
             pass
         for i in range(2500):
             f()
         assert not was_called
     finally:
         pypyjit.set_compile_hook(None)
         pypyjit.set_param('default')
Пример #19
0
    def test_on_compile_exception(self):
        import pypyjit, sys, cStringIO

        def hook(*args):
            1/0

        pypyjit.set_compile_hook(hook)
        s = cStringIO.StringIO()
        prev = sys.stderr
        sys.stderr = s
        try:
            self.on_compile()
        finally:
            sys.stderr = prev
        assert 'jit hook' in s.getvalue()
        assert 'ZeroDivisionError' in s.getvalue()
Пример #20
0
    def test_on_compile_exception(self):
        import pypyjit, sys, cStringIO

        def hook(*args):
            1 / 0

        pypyjit.set_compile_hook(hook)
        s = cStringIO.StringIO()
        prev = sys.stderr
        sys.stderr = s
        try:
            self.on_compile()
        finally:
            sys.stderr = prev
        assert 'jit hook' in s.getvalue()
        assert 'ZeroDivisionError' in s.getvalue()
Пример #21
0
    def enable(self, profile):
        pypyjit.set_compile_hook(self.on_compile)
        pypyjit.set_abort_hook(self.on_abort)
        self._backup_stdout = sys.stdout
        self._backup_stderr = sys.stderr
        sys.stdout = RecordingStream(sys.stdout)
        sys.stderr = RecordingStream(sys.stderr)

        self.options["build"]["gc"] = sys.pypy_translation_info["translation.gc"]
        self.options["build"]["gcrootfinder"] = sys.pypy_translation_info["translation.gcrootfinder"]
        self.options["build"]["pypy_version"] = sys._mercurial[2]

        if profile:
            self._profile_mmaps = []
            self._new_mmap()
            sys.setprofile(self.on_profile)

        self._start_time = high_res_time()
Пример #22
0
    def on_compile(self, info):
        if info.jitdriver_name != 'pypyjit':
            return
        enabled = False
        self.printer = CodePrinter()
        for op in info.operations:
            if op.name == 'debug_merge_point' and op.pycode is self.ENTER:
                enabled = True
            elif op.name == 'debug_merge_point' and op.pycode is self.EXIT:
                enabled = False
            elif op.name == 'label':
                print()
                print(Color.set(Color.green_bg, '-' * 80))
                print()
            elif enabled:
                self._print_op(op)

        pypyjit.set_compile_hook(None)
        JitView._is_hook_installed = False
Пример #23
0
    def test_on_compile(self):
        import pypyjit
        all = []

        def hook(info):
            all.append(info)

        self.on_compile()
        pypyjit.set_compile_hook(hook)
        assert not all
        self.on_compile()
        assert len(all) == 1
        info = all[0]
        assert info.jitdriver_name == 'pypyjit'
        assert info.greenkey[0].co_name == 'function'
        assert info.greenkey[1] == 0
        assert info.greenkey[2] == False
        assert info.loop_no == 0
        assert info.type == 'loop'
        assert info.asmaddr == 0x42
        assert info.asmlen == 12
        raises(TypeError, 'info.bridge_no')
        assert len(info.operations) == 4
        int_add = info.operations[0]
        dmp = info.operations[1]
        assert isinstance(dmp, pypyjit.DebugMergePoint)
        assert dmp.pycode is self.f.func_code
        assert dmp.greenkey == (self.f.func_code, 0, False)
        assert dmp.call_depth == 0
        assert dmp.call_id == 0
        assert dmp.offset == -1
        assert int_add.name == 'int_add'
        assert int_add.num == self.int_add_num
        assert int_add.offset == 0
        self.on_compile_bridge()
        expected = ('<JitLoopInfo pypyjit, 4 operations, starting at '
                    '<(%s, 0, False)>>' % repr(self.f.func_code))
        assert repr(all[0]) == expected
        assert len(all) == 2
        pypyjit.set_compile_hook(None)
        self.on_compile()
        assert len(all) == 2
Пример #24
0
    def test_on_compile(self):
        import pypyjit
        all = []

        def hook(info):
            all.append(info)

        self.on_compile()
        pypyjit.set_compile_hook(hook)
        assert not all
        self.on_compile()
        assert len(all) == 1
        info = all[0]
        assert info.jitdriver_name == 'pypyjit'
        assert info.greenkey[0].co_name == 'function'
        assert info.greenkey[1] == 0
        assert info.greenkey[2] == False
        assert info.loop_no == 0
        assert info.type == 'loop'
        assert info.asmaddr == 0x42
        assert info.asmlen == 12
        raises(TypeError, 'info.bridge_no')
        assert len(info.operations) == 4
        int_add = info.operations[0]
        dmp = info.operations[1]
        assert isinstance(dmp, pypyjit.DebugMergePoint)
        assert dmp.pycode is self.f.__code__
        assert dmp.greenkey == (self.f.__code__, 0, False)
        assert dmp.call_depth == 0
        assert dmp.call_id == 0
        assert dmp.offset == -1
        assert int_add.name == 'int_add'
        assert int_add.num == self.int_add_num
        assert int_add.offset == 0
        self.on_compile_bridge()
        expected = ('<JitLoopInfo pypyjit, 4 operations, starting at '
                    '<(%s, 0, False)>>' % repr(self.f.__code__))
        assert repr(all[0]) == expected
        assert len(all) == 2
        pypyjit.set_compile_hook(None)
        self.on_compile()
        assert len(all) == 2
Пример #25
0
    def test_no_jit(self):
        import pypyjit
        was_called = []

        def should_not_be_called(*args, **kwds):
            was_called.append((args, kwds))

        try:
            pypyjit.set_param('off')
            pypyjit.set_compile_hook(should_not_be_called)

            def f():
                pass

            for i in range(2500):
                f()
            assert not was_called
        finally:
            pypyjit.set_compile_hook(None)
            pypyjit.set_param('default')
Пример #26
0
    def disable(self, profile):
        self._end_time = high_res_time()
        self.runtime = self._end_time - self._start_time
        del self._start_time

        if profile:
            sys.setprofile(None)

        pypyjit.set_compile_hook(None)
        pypyjit.set_abort_hook(None)

        self.stdout = sys.stdout._content.getvalue()
        self.stderr = sys.stderr._content.getvalue()
        sys.stdout = self._backup_stdout
        sys.stderr = self._backup_stderr
        del self._backup_stdout
        del self._backup_stderr

        if profile:
            self._find_calls()
        del self._end_time
Пример #27
0
    def test_on_compile(self):
        import pypyjit
        all = []

        def hook(*args):
            assert args[0] == 'main'
            assert args[1] in ['loop', 'bridge']
            all.append(args[2:])
        
        self.on_compile()
        pypyjit.set_compile_hook(hook)
        assert not all
        self.on_compile()
        assert len(all) == 1
        assert all[0][0][0].co_name == 'f'
        assert all[0][0][1] == 0
        assert all[0][0][2] == False
        assert len(all[0][1]) == 2
        assert 'int_add' in all[0][1][0]
        self.on_compile_bridge()
        assert len(all) == 2
        pypyjit.set_compile_hook(None)
        self.on_compile()
        assert len(all) == 2
Пример #28
0
    logging.debug("%d, Elapsed TPS: %d, Elapsed: %f" % (count, int(count/(t1 - tstart)), (t1-tstart)))

import sys

def bork(name, greenkey, reason):
    print "ABORTED: ", name, greenkey,reason
def optimize(name, loop, greenkey, operations):
    print "OPTIMIZE: ", name, loop, greenkey
def chook(jitdriver_name, loop_type, greenkey, operations, assembler_addr, assembler_length):
    if loop_type == 'entry bridge':
        print "COMPILE: ", loop_type, greenkey

if __name__ == '__main__':

    if False:
        import pypyjit
        pypyjit.set_abort_hook(bork)
        pypyjit.set_optimize_hook(optimize)
        pypyjit.set_compile_hook(chook)

    #dumbtest()
    #sys.exit(0)

    logging.basicConfig(level=logging.DEBUG)
    fd = sys.stdin
    outfd = sys.stdout
    byline(fd, outfd)
    #at = SQLexer()
    #for i in range(128):
    #    print i, chr(i), at.map_main_alt[i]
Пример #29
0
                            if 'guard' in repr(op):
                                pypyjit.trace_next_iteration_hash(op.hash)
                                m = re.search('Guard(0x[0-9a-f]+)', repr(op))
                                self._lookup[int(m.group(1), 16)] = s[i]
                    
            #print self._lookup[loopinfo.bridge_no] in self._trace_bridges
#            self._trace_bridges[(hash(gkey[0]), gkey[1])] = set()
            #import pdb
            #pdb.set_trace()
#        else:
#            print loopinfo.bridge_no

    def finish(self):
        with open("tracedata.py", "w") as f:
            f.write("trace_too_long = set([")
            for k in self._trace_too_long_set:
                f.write(repr(k) + ", ")
            f.write("])\n")
            f.write("trace_immediately = {}\n")
            for k, v in self._trace_immediately.iteritems():
                f.write("trace_immediately[%d] = set([%s])\n" %
                        (k, ", ".join([repr(x) for x in v])))

tracer = Tracer()

pypyjit.set_trace_too_long_hook(tracer.trace_too_long)
pypyjit.set_compile_hook(tracer.on_compile, operations=True)
__pypy__.set_code_callback(tracer.code_callback)

atexit.register(tracer.finish)
Пример #30
0
 def __enter__(self):
     if not JitView._is_hook_installed:
         pypyjit.set_compile_hook(self.on_compile)
         JitView._is_hook_installed = True
Пример #31
0
        print op.num
        if op.name == "debug_merge_point":
            print dir(op.pycode)
            print op.pycode.co_code
        print dir(op)


def hook2(*args):
    l2.append(args)


def hook3(*args):
    l3.append(args)


pypyjit.set_compile_hook(hook)
pypyjit.set_optimize_hook(hook2)
pypyjit.set_abort_hook(hook3)
runpy.run_path(sys.argv[1])
pypyjit.set_compile_hook(None)
pypyjit.set_optimize_hook(None)
pypyjit.set_abort_hook(None)

for jitdriver_name, loop_type, greenkey, operations, assembler_addr, assembler_length in l:
    storage = LoopStorage()
    try:
        code = storage.load_code(loop.filename)[(loop.startlineno, loop.name)]
        if code.co_name == "<module>" and code.co_firstlineno == 1:
            with open(code.co_filename) as f:
                source = CodeRepr(f.read(), code, loop)
        else:
Пример #32
0
# simple app which computes the sum of 1..N-1, integrated with BLCR's
# checkpointing facilities to preserve the JITted code between runs.

import os


def hook(name, kind, where, ops):
    codeobj, nextinstr, is_being_profiled = where
    print '[PYPY JIT] compiling', kind, codeobj.co_name


try:
    import pypyjit
    pypyjit.set_compile_hook(hook)
except ImportError:
    print 'not on pypy, not setting compile hook'


def myfunc(N):
    res = 0
    for i in range(N):
        res += i
    return res


def main():
    x = myfunc(2000)
    print 'the result is', x


def magic_run(fn):