Author: Armin Rigo <ar...@tunes.org>
Branch: 
Changeset: r75266:276ba3e27516
Date: 2015-01-09 01:01 +0100
http://bitbucket.org/pypy/pypy/changeset/276ba3e27516/

Log:    Issue #1956

        Follow-up on 9ff421c20db5: on systems that have clock_gettime(),
        reimplement the RPython clock() based on it. This is in line with
        "man clock" on Linux, and should be guaranteed to give more precise
        results.

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
@@ -47,6 +47,8 @@
 
 if sys.platform.startswith('freebsd') or sys.platform.startswith('netbsd'):
     libraries = ['compat']
+elif sys.platform == 'linux2':
+    libraries = ['rt']
 else:
     libraries = []
 
@@ -58,7 +60,15 @@
     TIMEB = platform.Struct(STRUCT_TIMEB, [('time', rffi.INT),
                                            ('millitm', rffi.INT)])
 
-constant_names = ['RUSAGE_SELF', 'EINTR']
+class CConfigForClockGetTime:
+    _compilation_info_ = ExternalCompilationInfo(
+        includes=['time.h'],
+        libraries=libraries
+    )
+    TIMESPEC = platform.Struct('struct timespec', [('tv_sec', rffi.LONG),
+                                                   ('tv_nsec', rffi.LONG)])
+
+constant_names = ['RUSAGE_SELF', 'EINTR', 'CLOCK_PROCESS_CPUTIME_ID']
 for const in constant_names:
     setattr(CConfig, const, platform.DefinedConstantInteger(const))
 defs_names = ['GETTIMEOFDAY_NO_TZ']
@@ -162,6 +172,21 @@
                 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
_______________________________________________
pypy-commit mailing list
pypy-commit@python.org
https://mail.python.org/mailman/listinfo/pypy-commit

Reply via email to