[pypy-commit] pypy default: Use rtime.c_clock_gettime() here

arigo pypy.commits at gmail.com
Sat Oct 1 04:37:09 EDT 2016


Author: Armin Rigo <arigo at tunes.org>
Branch: 
Changeset: r87484:d5e85ca41a7f
Date: 2016-10-01 10:36 +0200
http://bitbucket.org/pypy/pypy/changeset/d5e85ca41a7f/

Log:	Use rtime.c_clock_gettime() here

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/rpython/rlib/rtime.py b/rpython/rlib/rtime.py
--- a/rpython/rlib/rtime.py
+++ b/rpython/rlib/rtime.py
@@ -191,6 +191,10 @@
                                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


More information about the pypy-commit mailing list