[pypy-commit] pypy kill_ll_time: Move time.clock(), and fix sanbox tests.

amauryfa noreply at buildbot.pypy.org
Sat Jan 10 17:29:51 CET 2015


Author: Amaury Forgeot d'Arc <amauryfa at gmail.com>
Branch: kill_ll_time
Changeset: r75286:5c699a5a1e58
Date: 2015-01-10 16:42 +0100
http://bitbucket.org/pypy/pypy/changeset/5c699a5a1e58/

Log:	Move time.clock(), and fix sanbox tests.

diff --git a/rpython/rlib/rtime.py b/rpython/rlib/rtime.py
--- a/rpython/rlib/rtime.py
+++ b/rpython/rlib/rtime.py
@@ -7,6 +7,7 @@
 from rpython.rlib.rarithmetic import intmask
 
 if sys.platform == 'win32':
+    _WIN32 = True
     TIME_H = 'time.h'
     FTIME = '_ftime64'
     STRUCT_TIMEB = 'struct __timeb64'
@@ -14,6 +15,7 @@
                 TIME_H, 'sys/types.h', 'sys/timeb.h']
     need_rusage = False
 else:
+    _WIN32 = False
     TIME_H = 'sys/time.h'
     FTIME = 'ftime'
     STRUCT_TIMEB = 'struct timeb'
@@ -30,6 +32,13 @@
     includes=includes
 )
 
+if sys.platform.startswith('freebsd') or sys.platform.startswith('netbsd'):
+    libraries = ['compat']
+elif sys.platform == 'linux2':
+    libraries = ['rt']
+else:
+    libraries = []
+
 class CConfig:
     _compilation_info_ = eci
     TIMEVAL = rffi_platform.Struct('struct timeval', [('tv_sec', rffi.INT),
@@ -116,3 +125,66 @@
     else:
         return float(c_time(void))
 
+
+if _WIN32:
+    # hacking to avoid LARGE_INTEGER which is a union...
+    A = lltype.FixedSizeArray(lltype.SignedLongLong, 1)
+    QueryPerformanceCounter = external(
+        'QueryPerformanceCounter', [lltype.Ptr(A)], lltype.Void,
+        releasegil=False)
+    QueryPerformanceFrequency = external(
+        'QueryPerformanceFrequency', [lltype.Ptr(A)], rffi.INT,
+        releasegil=False)
+    class ClockState(object):
+        divisor = 0.0
+        counter_start = 0
+    _clock_state = ClockState()
+elif CLOCK_PROCESS_CPUTIME_ID is not None:
+    # Linux and other POSIX systems with clock_gettime()
+    class CConfigForClockGetTime:
+        _compilation_info_ = ExternalCompilationInfo(
+            includes=['time.h'],
+            libraries=libraries
+        )
+        TIMESPEC = rffi_platform.Struct(
+            'struct timespec', [
+                ('tv_sec', rffi.LONG),
+                ('tv_nsec', rffi.LONG)])
+
+    cconfig = rffi_platform.configure(CConfigForClockGetTime)
+    TIMESPEC = cconfig['TIMESPEC']
+    c_clock_gettime = external('clock_gettime',
+                               [lltype.Signed, lltype.Ptr(TIMESPEC)],
+                               rffi.INT, releasegil=False)
+else:
+    c_getrusage = self.llexternal('getrusage', 
+                                  [rffi.INT, lltype.Ptr(RUSAGE)],
+                                  lltype.Void,
+                                  releasegil=False)
+
+ at replacement_for(time.clock, sandboxed_name='ll_time.ll_time_clock')
+def clock():
+    if _WIN32:
+        with lltype.static_alloc(A) as a:
+            if _clock_state.divisor == 0.0:
+                QueryPerformanceCounter(a)
+                _clock_state.counter_start = a[0]
+                QueryPerformanceFrequency(a)
+                _clock_state.divisor = float(a[0])
+            QueryPerformanceCounter(a)
+            diff = a[0] - _clock_state.counter_start
+        return float(diff) / _clock_state.divisor
+    elif self.CLOCK_PROCESS_CPUTIME_ID is not None:
+        with lltype.static_alloc(TIMESPEC) as a:
+            c_clock_gettime(CLOCK_PROCESS_CPUTIME_ID, a)
+            result = (float(rffi.getintfield(a, 'c_tv_sec')) +
+                      float(rffi.getintfield(a, 'c_tv_nsec')) * 0.000000001)
+        return result
+    else:
+        with lltype.static_alloc(RUSAGE) as a:
+            c_getrusage(RUSAGE_SELF, a)
+            result = (decode_timeval(a.c_ru_utime) +
+                      decode_timeval(a.c_ru_stime))
+        return result
+
+    
diff --git a/rpython/rtyper/extfuncregistry.py b/rpython/rtyper/extfuncregistry.py
--- a/rpython/rtyper/extfuncregistry.py
+++ b/rpython/rtyper/extfuncregistry.py
@@ -2,6 +2,10 @@
 
 from rpython.rtyper.extfunc import register_external
 
+# Register functions as side-effect
+from rpython.rlib import rtime
+from rpython.rlib import rtermios
+
 # ___________________________
 # math functions
 
diff --git a/rpython/rtyper/module/ll_time.py b/rpython/rtyper/module/ll_time.py
--- a/rpython/rtyper/module/ll_time.py
+++ b/rpython/rtyper/module/ll_time.py
@@ -84,66 +84,6 @@
         self.configure(CConfig)
         self.TIMEVALP = lltype.Ptr(self.TIMEVAL)
 
-    @registering(time.clock)
-    def register_time_clock(self):
-        if sys.platform == 'win32':
-            # hacking to avoid LARGE_INTEGER which is a union...
-            A = lltype.FixedSizeArray(lltype.SignedLongLong, 1)
-            QueryPerformanceCounter = self.llexternal(
-                'QueryPerformanceCounter', [lltype.Ptr(A)], lltype.Void,
-                releasegil=False)
-            QueryPerformanceFrequency = self.llexternal(
-                'QueryPerformanceFrequency', [lltype.Ptr(A)], rffi.INT,
-                releasegil=False)
-            class State(object):
-                pass
-            state = State()
-            state.divisor = 0.0
-            state.counter_start = 0
-            def time_clock_llimpl():
-                a = lltype.malloc(A, flavor='raw')
-                if state.divisor == 0.0:
-                    QueryPerformanceCounter(a)
-                    state.counter_start = a[0]
-                    QueryPerformanceFrequency(a)
-                    state.divisor = float(a[0])
-                QueryPerformanceCounter(a)
-                diff = a[0] - state.counter_start
-                lltype.free(a, flavor='raw')
-                return float(diff) / state.divisor
-        elif self.CLOCK_PROCESS_CPUTIME_ID is not None:
-            # Linux and other POSIX systems with clock_gettime()
-            self.configure(CConfigForClockGetTime)
-            TIMESPEC = self.TIMESPEC
-            CLOCK_PROCESS_CPUTIME_ID = self.CLOCK_PROCESS_CPUTIME_ID
-            c_clock_gettime = self.llexternal('clock_gettime',
-                [lltype.Signed, lltype.Ptr(TIMESPEC)],
-                rffi.INT, releasegil=False)
-            def time_clock_llimpl():
-                a = lltype.malloc(TIMESPEC, flavor='raw')
-                c_clock_gettime(CLOCK_PROCESS_CPUTIME_ID, a)
-                result = (float(rffi.getintfield(a, 'c_tv_sec')) +
-                          float(rffi.getintfield(a, 'c_tv_nsec')) * 0.000000001)
-                lltype.free(a, flavor='raw')
-                return result
-        else:
-            RUSAGE = self.RUSAGE
-            RUSAGE_SELF = self.RUSAGE_SELF or 0
-            c_getrusage = self.llexternal('getrusage', 
-                                          [rffi.INT, lltype.Ptr(RUSAGE)],
-                                          lltype.Void,
-                                          releasegil=False)
-            def time_clock_llimpl():
-                a = lltype.malloc(RUSAGE, flavor='raw')
-                c_getrusage(RUSAGE_SELF, a)
-                result = (decode_timeval(a.c_ru_utime) +
-                          decode_timeval(a.c_ru_stime))
-                lltype.free(a, flavor='raw')
-                return result
-
-        return extdef([], float, llimpl=time_clock_llimpl,
-                      export_name='ll_time.ll_time_clock')
-
     @registering(time.sleep)
     def register_time_sleep(self):
         if sys.platform == 'win32':
diff --git a/rpython/translator/c/database.py b/rpython/translator/c/database.py
--- a/rpython/translator/c/database.py
+++ b/rpython/translator/c/database.py
@@ -385,6 +385,9 @@
             return False
         if hasattr(fnobj, '_safe_not_sandboxed'):
             return not fnobj._safe_not_sandboxed
+        elif getattr(getattr(fnobj, '_callable', None),
+                     '_sandbox_external_name', None):
+            return True
         else:
             return "if_external"
 
diff --git a/rpython/translator/sandbox/rsandbox.py b/rpython/translator/sandbox/rsandbox.py
--- a/rpython/translator/sandbox/rsandbox.py
+++ b/rpython/translator/sandbox/rsandbox.py
@@ -115,7 +115,11 @@
     trampoline marshals its input arguments, dumps them to STDOUT,
     and waits for an answer on STDIN.
     """
-    fnname = fnobj._name
+    if getattr(getattr(fnobj, '_callable', None),
+               '_sandbox_external_name', None):
+        fnname = fnobj._callable._sandbox_external_name
+    else:
+        fnname = fnobj._name
     if hasattr(fnobj, 'graph'):
         # get the annotation of the input arguments and the result
         graph = fnobj.graph


More information about the pypy-commit mailing list