Author: Richard Plangger <planri...@gmail.com>
Branch: new-jit-log
Changeset: r83499:f42dc412ebd9
Date: 2016-04-01 17:19 +0200
http://bitbucket.org/pypy/pypy/changeset/f42dc412ebd9/

Log:    exposed enable_jitlog in module _vmprof, JITLOG env variable now
        controls the jitlog output file (bugfix), setup_once is now called
        while initializing the metainterp_sd

diff --git a/pypy/module/_vmprof/__init__.py b/pypy/module/_vmprof/__init__.py
--- a/pypy/module/_vmprof/__init__.py
+++ b/pypy/module/_vmprof/__init__.py
@@ -10,6 +10,7 @@
 
     interpleveldefs = {
         'enable': 'interp_vmprof.enable',
+        'enable_jitlog': 'interp_vmprof.enable_jitlog',
         'disable': 'interp_vmprof.disable',
         'write_all_code_objects': 'interp_vmprof.write_all_code_objects',
         'VMProfError': 'space.fromcache(interp_vmprof.Cache).w_VMProfError',
diff --git a/pypy/module/_vmprof/interp_vmprof.py 
b/pypy/module/_vmprof/interp_vmprof.py
--- a/pypy/module/_vmprof/interp_vmprof.py
+++ b/pypy/module/_vmprof/interp_vmprof.py
@@ -69,6 +69,14 @@
     except rvmprof.VMProfError, e:
         raise VMProfError(space, e)
 
+@unwrap_spec(fileno=int)
+def enable_jitlog(space, fileno):
+    """ Enable PyPy's logging facility. """
+    try:
+        rvmprof.enable_jitlog(fileno)
+    except rvmprof.VMProfError, e:
+        raise VMProfError(space, e)
+
 def write_all_code_objects(space):
     """ Needed on cpython, just empty function here
     """
diff --git a/rpython/jit/backend/x86/test/test_jitlog.py 
b/rpython/jit/backend/x86/test/test_jitlog.py
--- a/rpython/jit/backend/x86/test/test_jitlog.py
+++ b/rpython/jit/backend/x86/test/test_jitlog.py
@@ -1,4 +1,5 @@
 import re
+import os
 from rpython.rlib import debug
 from rpython.jit.tool.oparser import pure_parse
 from rpython.jit.metainterp import logger
@@ -15,13 +16,34 @@
 
 class TestLogger(Jit386Mixin):
 
-    def test_log_loop(self):
-        myjitdriver = JitDriver(greens = [], reds = ['x', 'y', 'res'])
+    def test_explicit_enable(self):
         vmprof = rvmprof.VMProf()
         fileno, name = tempfile.mkstemp()
+        self.run_sample_loop(lambda: vmprof.enable_jitlog(fileno))
+        assert os.path.exists(name)
+        with open(name, 'rb') as fd:
+            # check the file header
+            assert fd.read(3) == '\x23\xfe\xaf'
+            assert len(fd.read()) > 0
+        print(name)
+
+    def test_venv(self):
+        fileno, name = tempfile.mkstemp()
+        os.environ["JITLOG"] = name
+        self.run_sample_loop(None)
+        assert os.path.exists(name)
+        with open(name, 'rb') as fd:
+            # check the file header
+            assert fd.read(3) == '\x23\xfe\xaf'
+            assert len(fd.read()) > 0
+        print(name)
+
+    def run_sample_loop(self, func):
+        myjitdriver = JitDriver(greens = [], reds = ['x', 'y', 'res'])
         def f(x, y):
             res = 0
-            vmprof.enable(fileno, 0.1)
+            if func:
+                func()
             while y > 0:
                 myjitdriver.can_enter_jit(x=x, y=y, res=res)
                 myjitdriver.jit_merge_point(x=x, y=y, res=res)
@@ -34,4 +56,3 @@
             return res
         res = self.meta_interp(f, [6, 20])
         self.check_trace_count(2)
-        print(name)
diff --git a/rpython/jit/metainterp/jitlog.py b/rpython/jit/metainterp/jitlog.py
--- a/rpython/jit/metainterp/jitlog.py
+++ b/rpython/jit/metainterp/jitlog.py
@@ -29,6 +29,8 @@
 MARK_JIT_BRIDGE_COUNTER = 0x21
 MARK_JIT_ENTRY_COUNTER = 0x22
 
+MARK_JITLOG_HEADER = 0x23
+
 IS_32_BIT = sys.maxint == 2**31-1
 
 @always_inline
@@ -69,10 +71,16 @@
         self.is_setup = False
 
     def setup_once(self):
+        if self.is_setup:
+            return
         self.is_setup = True
         self.cintf.jitlog_try_init_using_env()
         if not self.cintf.jitlog_enabled():
             return
+
+        header = encode_le_16bit(0xaffe)
+        self._write_marked(MARK_JITLOG_HEADER, header)
+
         count = len(resoperations.opname)
         mark = MARK_RESOP_META
         for opnum, opname in resoperations.opname.items():
@@ -81,12 +89,11 @@
 
     def teardown(self):
         self.cintf.jitlog_teardown()
+        self.is_setup = False
 
     def _write_marked(self, mark, line):
         if not we_are_translated():
             assert self.cintf.jitlog_enabled()
-        if not self.is_setup:
-            self.setup_once()
         self.cintf.jitlog_write_marked(mark, line, len(line))
 
     def log_jit_counter(self, struct):
diff --git a/rpython/jit/metainterp/pyjitpl.py 
b/rpython/jit/metainterp/pyjitpl.py
--- a/rpython/jit/metainterp/pyjitpl.py
+++ b/rpython/jit/metainterp/pyjitpl.py
@@ -1819,6 +1819,7 @@
     def _setup_once(self):
         """Runtime setup needed by the various components of the JIT."""
         if not self.globaldata.initialized:
+            self.jitlog.setup_once()
             debug_print(self.jit_starting_line)
             self.cpu.setup_once()
             if not self.profiler.initialized:
diff --git a/rpython/rlib/rvmprof/__init__.py b/rpython/rlib/rvmprof/__init__.py
--- a/rpython/rlib/rvmprof/__init__.py
+++ b/rpython/rlib/rvmprof/__init__.py
@@ -35,5 +35,8 @@
 def enable(fileno, interval):
     _get_vmprof().enable(fileno, interval)
 
+def enable_jitlog(fileno):
+    _get_vmprof().enable_jitlog(fileno)
+
 def disable():
     _get_vmprof().disable()
diff --git a/rpython/rlib/rvmprof/cintf.py b/rpython/rlib/rvmprof/cintf.py
--- a/rpython/rlib/rvmprof/cintf.py
+++ b/rpython/rlib/rvmprof/cintf.py
@@ -57,11 +57,10 @@
                                             _nowrapper=True)
 
     # jit log functions
-    jitlog_init = rffi.llexternal("jitlog_init", [rffi.INT, rffi.CCHARP],
+    jitlog_init = rffi.llexternal("jitlog_init", [rffi.INT],
                                   rffi.CCHARP, compilation_info=eci)
     jitlog_try_init_using_env = rffi.llexternal("jitlog_try_init_using_env",
-                                  [], lltype.Void, compilation_info=eci,
-                                  releasegil=False)
+                                  [], lltype.Void, compilation_info=eci)
     jitlog_write_marked = rffi.llexternal("jitlog_write_marked",
                                   [rffi.INT, rffi.CCHARP, rffi.INT],
                                   lltype.Void, compilation_info=eci,
diff --git a/rpython/rlib/rvmprof/rvmprof.py b/rpython/rlib/rvmprof/rvmprof.py
--- a/rpython/rlib/rvmprof/rvmprof.py
+++ b/rpython/rlib/rvmprof/rvmprof.py
@@ -109,17 +109,15 @@
         if p_error:
             raise VMProfError(rffi.charp2str(p_error))
 
-        self.enable_jitlog(fileno, "")
-
         self._gather_all_code_objs()
         res = self.cintf.vmprof_enable()
         if res < 0:
             raise VMProfError(os.strerror(rposix.get_saved_errno()))
         self.is_enabled = True
 
-    def enable_jitlog(self, fileno, regexp):
+    def enable_jitlog(self, fileno):
         # initialize the jit log
-        p_error = self.cintf.jitlog_init(fileno, regexp)
+        p_error = self.cintf.jitlog_init(fileno)
         if p_error:
             raise VMProfError(rffi.charp2str(p_error))
 
diff --git a/rpython/rlib/rvmprof/src/jitlog_main.h 
b/rpython/rlib/rvmprof/src/jitlog_main.h
--- a/rpython/rlib/rvmprof/src/jitlog_main.h
+++ b/rpython/rlib/rvmprof/src/jitlog_main.h
@@ -5,7 +5,6 @@
 #include <fcntl.h>
 
 static int jitlog_fd = -1;
-static char * jitlog_prefix = NULL;
 static int jitlog_ready = 0;
 
 RPY_EXTERN
@@ -25,7 +24,8 @@
         mode_t mode = S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH;
         jitlog_fd = open(filename, O_WRONLY | O_CREAT, mode);
         if (jitlog_fd == -1) {
-            perror("could not open");
+            dprintf(2, "could not open '%s': ", filename);
+            perror(NULL);
             exit(-1);
         }
     } else {
@@ -41,10 +41,9 @@
 }
 
 RPY_EXTERN
-char *jitlog_init(int fd, const char * prefix)
+char *jitlog_init(int fd)
 {
     jitlog_fd = fd;
-    jitlog_prefix = strdup(prefix);
     jitlog_ready = 1;
     return NULL;
 }
@@ -59,10 +58,6 @@
     // close the jitlog file descriptor
     close(jitlog_fd);
     jitlog_fd = -1;
-    // free the prefix
-    if (jitlog_prefix != NULL) {
-        free(jitlog_prefix);
-    }
 }
 
 RPY_EXTERN
diff --git a/rpython/rlib/rvmprof/src/rvmprof.h 
b/rpython/rlib/rvmprof/src/rvmprof.h
--- a/rpython/rlib/rvmprof/src/rvmprof.h
+++ b/rpython/rlib/rvmprof/src/rvmprof.h
@@ -9,7 +9,7 @@
 RPY_EXTERN long vmprof_stack_pop(void*);
 RPY_EXTERN void vmprof_stack_free(void*);
 
-RPY_EXTERN char * jitlog_init(int, const char*);
+RPY_EXTERN char * jitlog_init(int);
 RPY_EXTERN void jitlog_try_init_using_env(void);
 RPY_EXTERN int jitlog_enabled();
 RPY_EXTERN void jitlog_write_marked(int, char*, int);
_______________________________________________
pypy-commit mailing list
pypy-commit@python.org
https://mail.python.org/mailman/listinfo/pypy-commit

Reply via email to