Author: Armin Rigo <ar...@tunes.org>
Branch: py3k
Changeset: r87485:863ec7aea4f4
Date: 2016-10-01 10:37 +0200
http://bitbucket.org/pypy/pypy/changeset/863ec7aea4f4/

Log:    hg merge default

diff --git a/pypy/module/__pypy__/__init__.py b/pypy/module/__pypy__/__init__.py
--- a/pypy/module/__pypy__/__init__.py
+++ b/pypy/module/__pypy__/__init__.py
@@ -2,6 +2,8 @@
 
 from pypy.interpreter.mixedmodule import MixedModule
 from pypy.module.imp.importing import get_pyc_magic
+from rpython.rlib import rtime
+
 
 class BuildersModule(MixedModule):
     appleveldefs = {}
@@ -14,16 +16,15 @@
 class TimeModule(MixedModule):
     appleveldefs = {}
     interpleveldefs = {}
-    if sys.platform.startswith("linux") or 'bsd' in sys.platform:
-        from pypy.module.__pypy__ import interp_time
+    if rtime.HAS_CLOCK_GETTIME:
         interpleveldefs["clock_gettime"] = "interp_time.clock_gettime"
         interpleveldefs["clock_getres"] = "interp_time.clock_getres"
         for name in [
             "CLOCK_REALTIME", "CLOCK_MONOTONIC", "CLOCK_MONOTONIC_RAW",
             "CLOCK_PROCESS_CPUTIME_ID", "CLOCK_THREAD_CPUTIME_ID"
         ]:
-            if getattr(interp_time, name) is not None:
-                interpleveldefs[name] = "space.wrap(interp_time.%s)" % name
+            if getattr(rtime, name) is not None:
+                interpleveldefs[name] = "space.wrap(%d)" % getattr(rtime, name)
 
 
 class ThreadModule(MixedModule):
diff --git a/pypy/module/__pypy__/interp_time.py 
b/pypy/module/__pypy__/interp_time.py
--- a/pypy/module/__pypy__/interp_time.py
+++ b/pypy/module/__pypy__/interp_time.py
@@ -4,71 +4,28 @@
 from pypy.interpreter.error import exception_from_saved_errno
 from pypy.interpreter.gateway import unwrap_spec
 from rpython.rtyper.lltypesystem import rffi, lltype
-from rpython.rtyper.tool import rffi_platform
-from rpython.translator.tool.cbuild import ExternalCompilationInfo
+from rpython.rlib import rtime
+from rpython.rlib.rtime import HAS_CLOCK_GETTIME
 
-if sys.platform == 'linux2':
-    libraries = ["rt"]
-else:
-    libraries = []
-
-
-class CConfig:
-    _compilation_info_ = ExternalCompilationInfo(
-        includes=["time.h"],
-        libraries=libraries,
-    )
-
-    HAS_CLOCK_GETTIME = rffi_platform.Has('clock_gettime')
-
-    CLOCK_REALTIME = rffi_platform.DefinedConstantInteger("CLOCK_REALTIME")
-    CLOCK_MONOTONIC = rffi_platform.DefinedConstantInteger("CLOCK_MONOTONIC")
-    CLOCK_MONOTONIC_RAW = 
rffi_platform.DefinedConstantInteger("CLOCK_MONOTONIC_RAW")
-    CLOCK_PROCESS_CPUTIME_ID = 
rffi_platform.DefinedConstantInteger("CLOCK_PROCESS_CPUTIME_ID")
-    CLOCK_THREAD_CPUTIME_ID = 
rffi_platform.DefinedConstantInteger("CLOCK_THREAD_CPUTIME_ID")
-
-cconfig = rffi_platform.configure(CConfig)
-
-HAS_CLOCK_GETTIME = cconfig["HAS_CLOCK_GETTIME"]
-
-CLOCK_REALTIME = cconfig["CLOCK_REALTIME"]
-CLOCK_MONOTONIC = cconfig["CLOCK_MONOTONIC"]
-CLOCK_MONOTONIC_RAW = cconfig["CLOCK_MONOTONIC_RAW"]
-CLOCK_PROCESS_CPUTIME_ID = cconfig["CLOCK_PROCESS_CPUTIME_ID"]
-CLOCK_THREAD_CPUTIME_ID = cconfig["CLOCK_THREAD_CPUTIME_ID"]
 
 if HAS_CLOCK_GETTIME:
-    #redo it for timespec
-    CConfig.TIMESPEC = rffi_platform.Struct("struct timespec", [
-        ("tv_sec", rffi.TIME_T),
-        ("tv_nsec", rffi.LONG),
-    ])
-    cconfig = rffi_platform.configure(CConfig)
-    TIMESPEC = cconfig['TIMESPEC']
-
-    c_clock_gettime = rffi.llexternal("clock_gettime",
-        [lltype.Signed, lltype.Ptr(TIMESPEC)], rffi.INT,
-        compilation_info=CConfig._compilation_info_, releasegil=False,
-        save_err=rffi.RFFI_SAVE_ERRNO
-    )
-    c_clock_getres = rffi.llexternal("clock_getres",
-        [lltype.Signed, lltype.Ptr(TIMESPEC)], rffi.INT,
-        compilation_info=CConfig._compilation_info_, releasegil=False,
-        save_err=rffi.RFFI_SAVE_ERRNO
-    )
 
     @unwrap_spec(clk_id="c_int")
     def clock_gettime(space, clk_id):
-        with lltype.scoped_alloc(TIMESPEC) as tp:
-            ret = c_clock_gettime(clk_id, tp)
+        with lltype.scoped_alloc(rtime.TIMESPEC) as tp:
+            ret = rtime.c_clock_gettime(clk_id, tp)
             if ret != 0:
                 raise exception_from_saved_errno(space, space.w_IOError)
-            return space.wrap(int(tp.c_tv_sec) + 1e-9 * int(tp.c_tv_nsec))
+            t = (float(rffi.getintfield(tp, 'c_tv_sec')) +
+                 float(rffi.getintfield(tp, 'c_tv_nsec')) * 0.000000001)
+        return space.wrap(t)
 
     @unwrap_spec(clk_id="c_int")
     def clock_getres(space, clk_id):
-        with lltype.scoped_alloc(TIMESPEC) as tp:
-            ret = c_clock_getres(clk_id, tp)
+        with lltype.scoped_alloc(rtime.TIMESPEC) as tp:
+            ret = rtime.c_clock_getres(clk_id, tp)
             if ret != 0:
                 raise exception_from_saved_errno(space, space.w_IOError)
-            return space.wrap(int(tp.c_tv_sec) + 1e-9 * int(tp.c_tv_nsec))
+            t = (float(rffi.getintfield(tp, 'c_tv_sec')) +
+                 float(rffi.getintfield(tp, 'c_tv_nsec')) * 0.000000001)
+        return space.wrap(t)
diff --git a/pypy/tool/build_cffi_imports.py b/pypy/tool/build_cffi_imports.py
--- a/pypy/tool/build_cffi_imports.py
+++ b/pypy/tool/build_cffi_imports.py
@@ -1,5 +1,4 @@
-import sys, shutil
-from rpython.tool.runsubprocess import run_subprocess
+import sys, shutil, os
 
 class MissingDependenciesError(Exception):
     pass
@@ -20,6 +19,8 @@
     }
 
 def create_cffi_import_libraries(pypy_c, options, basedir):
+    from rpython.tool.runsubprocess import run_subprocess
+
     shutil.rmtree(str(basedir.join('lib_pypy', '__pycache__')),
                   ignore_errors=True)
     failures = []
@@ -44,11 +45,16 @@
     return failures
 
 if __name__ == '__main__':
-    import py, os
     if '__pypy__' not in sys.builtin_module_names:
         print >> sys.stderr, 'Call with a pypy interpreter'
         sys.exit(1)
 
+    tool_dir = os.path.dirname(os.path.abspath(sys.argv[0]))
+    base_dir = os.path.dirname(os.path.dirname(tool_dir))
+    sys.path.insert(0, base_dir)
+
+    import py
+
     class Options(object):
         pass
 
diff --git a/rpython/rlib/rtime.py b/rpython/rlib/rtime.py
--- a/rpython/rlib/rtime.py
+++ b/rpython/rlib/rtime.py
@@ -67,10 +67,17 @@
         includes=['time.h'],
         libraries=libraries
     )
+    _NO_MISSING_RT = rffi_platform.Has('printf("%d", clock_gettime(0, 0))')
     TIMESPEC = rffi_platform.Struct('struct timespec', [('tv_sec', rffi.LONG),
                                                         ('tv_nsec', 
rffi.LONG)])
 
-constant_names = ['RUSAGE_SELF', 'EINTR', 'CLOCK_PROCESS_CPUTIME_ID']
+constant_names = ['RUSAGE_SELF', 'EINTR',
+                  'CLOCK_REALTIME',
+                  'CLOCK_MONOTONIC',
+                  'CLOCK_MONOTONIC_RAW',
+                  'CLOCK_PROCESS_CPUTIME_ID',
+                  'CLOCK_THREAD_CPUTIME_ID',
+]
 for const in constant_names:
     setattr(CConfig, const, rffi_platform.DefinedConstantInteger(const))
 defs_names = ['GETTIMEOFDAY_NO_TZ']
@@ -158,16 +165,37 @@
         divisor = 0.0
         counter_start = 0
     state = State()
-elif CLOCK_PROCESS_CPUTIME_ID is not None:
+
+HAS_CLOCK_GETTIME = (CLOCK_MONOTONIC is not None)
+if HAS_CLOCK_GETTIME:
     # Linux and other POSIX systems with clock_gettime()
+    # TIMESPEC:
     globals().update(rffi_platform.configure(CConfigForClockGetTime))
-    TIMESPEC = TIMESPEC
-    CLOCK_PROCESS_CPUTIME_ID = CLOCK_PROCESS_CPUTIME_ID
-    eci_with_lrt = eci.merge(ExternalCompilationInfo(libraries=['rt']))
+    # do we need to add -lrt?
+    eciclock = CConfigForClockGetTime._compilation_info_
+    if not _NO_MISSING_RT:
+        eciclock = eciclock.merge(ExternalCompilationInfo(libraries=['rt']))
+    # the functions:
+    c_clock_getres = external("clock_getres",
+                              [lltype.Signed, lltype.Ptr(TIMESPEC)],
+                              rffi.INT, releasegil=False,
+                              save_err=rffi.RFFI_SAVE_ERRNO,
+                              compilation_info=eciclock)
     c_clock_gettime = external('clock_gettime',
                                [lltype.Signed, lltype.Ptr(TIMESPEC)],
                                rffi.INT, releasegil=False,
-                               compilation_info=eci_with_lrt)
+                               save_err=rffi.RFFI_SAVE_ERRNO,
+                               compilation_info=eciclock)
+    c_clock_settime = external('clock_settime',
+                               [lltype.Signed, lltype.Ptr(TIMESPEC)],
+                               rffi.INT, releasegil=False,
+                               save_err=rffi.RFFI_SAVE_ERRNO,
+                               compilation_info=eciclock)
+    # Note: there is no higher-level functions here to access
+    # clock_gettime().  The issue is that we'd need a way that keeps
+    # nanosecond precision, depending on the usage, so we can't have a
+    # nice function that returns the time as a float.
+
 if need_rusage:
     RUSAGE = RUSAGE
     RUSAGE_SELF = RUSAGE_SELF or 0
@@ -191,18 +219,16 @@
 def clock():
     if _WIN32:
         return win_perf_counter()
-    elif CLOCK_PROCESS_CPUTIME_ID is not None:
+    elif HAS_CLOCK_GETTIME and CLOCK_PROCESS_CPUTIME_ID is not None:
         with lltype.scoped_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.scoped_alloc(RUSAGE) as a:
-            c_getrusage(RUSAGE_SELF, a)
-            result = (decode_timeval(a.c_ru_utime) +
-                      decode_timeval(a.c_ru_stime))
-        return result
+            if c_clock_gettime(CLOCK_PROCESS_CPUTIME_ID, a) == 0:
+                return (float(rffi.getintfield(a, 'c_tv_sec')) +
+                        float(rffi.getintfield(a, 'c_tv_nsec')) * 0.000000001)
+    with lltype.scoped_alloc(RUSAGE) as a:
+        c_getrusage(RUSAGE_SELF, a)
+        result = (decode_timeval(a.c_ru_utime) +
+                  decode_timeval(a.c_ru_stime))
+    return result
 
 # _______________________________________________________________
 # time.sleep()
diff --git a/rpython/rlib/test/test_rtime.py b/rpython/rlib/test/test_rtime.py
--- a/rpython/rlib/test/test_rtime.py
+++ b/rpython/rlib/test/test_rtime.py
@@ -1,7 +1,9 @@
 
 from rpython.rtyper.test.tool import BaseRtypingTest
+from rpython.rtyper.lltypesystem import lltype, rffi
+from rpython.rlib import rtime
 
-import time, sys
+import time, sys, py
 
 class TestTime(BaseRtypingTest):
     def test_time_time(self):
@@ -53,3 +55,27 @@
         t1 = time.time()
         assert t0 <= t1
         assert t1 - t0 >= 0.15
+
+    def test_clock_gettime(self):
+        if not rtime.HAS_CLOCK_GETTIME:
+            py.test.skip("no clock_gettime()")
+        lst = []
+        for i in range(50):
+            with lltype.scoped_alloc(rtime.TIMESPEC) as a1:
+                res = rtime.c_clock_gettime(rtime.CLOCK_MONOTONIC, a1)
+                assert res == 0
+                t = (float(rffi.getintfield(a1, 'c_tv_sec')) +
+                     float(rffi.getintfield(a1, 'c_tv_nsec')) * 0.000000001)
+                lst.append(t)
+        assert lst == sorted(lst)
+
+    def test_clock_getres(self):
+        if not rtime.HAS_CLOCK_GETTIME:
+            py.test.skip("no clock_gettime()")
+        lst = []
+        with lltype.scoped_alloc(rtime.TIMESPEC) as a1:
+            res = rtime.c_clock_getres(rtime.CLOCK_MONOTONIC, a1)
+            assert res == 0
+            t = (float(rffi.getintfield(a1, 'c_tv_sec')) +
+                 float(rffi.getintfield(a1, 'c_tv_nsec')) * 0.000000001)
+        assert 0.0 < t <= 1.0
_______________________________________________
pypy-commit mailing list
pypy-commit@python.org
https://mail.python.org/mailman/listinfo/pypy-commit

Reply via email to