Author: Antonio Cuni <anto.c...@gmail.com>
Branch: gc-hooks
Changeset: r94344:76a781fe5853
Date: 2018-04-16 11:20 +0200
http://bitbucket.org/pypy/pypy/changeset/76a781fe5853/

Log:    add rlib.rtimer.get_timestamp_unit, which tells some info about the
        value returned by read_timestamp; will expose an app-level interface
        later

diff --git a/rpython/rlib/rtimer.py b/rpython/rlib/rtimer.py
--- a/rpython/rlib/rtimer.py
+++ b/rpython/rlib/rtimer.py
@@ -7,6 +7,11 @@
 
 _is_64_bit = r_uint.BITS > 32
 
+# unit of values returned by read_timestamp. Should be in sync with the ones
+# defined in translator/c/debug_print.h
+UNIT_TSC = 0
+UNIT_NS = 1
+UNIT_QUERY_PERFORMANCE_COUNTER = 2
 
 def read_timestamp():
     # Returns a longlong on 32-bit, and a regular int on 64-bit.
@@ -17,6 +22,11 @@
     else:
         return longlongmask(x)
 
+def get_timestamp_unit():
+    # an unit which is as arbitrary as the way we build the result of
+    # read_timestamp :)
+    return UNIT_NS
+
 
 class ReadTimestampEntry(ExtRegistryEntry):
     _about_ = read_timestamp
@@ -35,3 +45,15 @@
         else:
             resulttype = rffi.LONGLONG
         return hop.genop("ll_read_timestamp", [], resulttype=resulttype)
+
+
+class ReadTimestampEntry(ExtRegistryEntry):
+    _about_ = get_timestamp_unit
+
+    def compute_result_annotation(self):
+        from rpython.annotator.model import SomeInteger
+        return SomeInteger(nonneg=True)
+
+    def specialize_call(self, hop):
+        hop.exception_cannot_occur()
+        return hop.genop("ll_get_timestamp_unit", [], resulttype=lltype.Signed)
diff --git a/rpython/rlib/test/test_rtimer.py b/rpython/rlib/test/test_rtimer.py
--- a/rpython/rlib/test/test_rtimer.py
+++ b/rpython/rlib/test/test_rtimer.py
@@ -1,28 +1,56 @@
 import time
-
-from rpython.rlib.rtimer import read_timestamp
+import platform
+from rpython.rlib import rtimer
 from rpython.rtyper.test.test_llinterp import interpret
 from rpython.translator.c.test.test_genc import compile
 
-def timer():
-    t1 = read_timestamp()
-    start = time.time()
-    while time.time() - start < 0.1:
-        # busy wait
-        pass
-    t2 = read_timestamp()
-    return t2 - t1
+class TestTimer(object):
 
-def test_timer():
-    diff = timer()
-    # We're counting ticks, verify they look correct
-    assert diff > 1000
+    @staticmethod
+    def timer():
+        t1 = rtimer.read_timestamp()
+        start = time.time()
+        while time.time() - start < 0.1:
+            # busy wait
+            pass
+        t2 = rtimer.read_timestamp()
+        return t2 - t1
 
-def test_annotation():
-    diff = interpret(timer, [])
-    assert diff > 1000
+    def test_direct(self):
+        diff = self.timer()
+        # We're counting ticks, verify they look correct
+        assert diff > 1000
 
-def test_compile_c():
-    function = compile(timer, [])
-    diff = function()
-    assert diff > 1000
\ No newline at end of file
+    def test_annotation(self):
+        diff = interpret(self.timer, [])
+        assert diff > 1000
+
+    def test_compile_c(self):
+        function = compile(self.timer, [])
+        diff = function()
+        assert diff > 1000
+
+
+class TestGetUnit(object):
+
+    @staticmethod
+    def get_unit():
+        return rtimer.get_timestamp_unit()
+
+    def test_direct(self):
+        unit = self.get_unit()
+        assert unit == rtimer.UNIT_NS
+
+    def test_annotation(self):
+        unit = interpret(self.get_unit, [])
+        assert unit == rtimer.UNIT_NS
+
+    def test_compile_c(self):
+        function = compile(self.get_unit, [])
+        unit = function()
+        if platform.processor() in ('x86', 'x86_64'):
+            assert unit == rtimer.UNIT_TSC
+        else:
+            assert unit in (rtimer.UNIT_TSC,
+                            rtimer.UNIT_NS,
+                            rtimer.UNIT_QUERY_PERFORMANCE_COUNTER)
diff --git a/rpython/rtyper/lltypesystem/lloperation.py 
b/rpython/rtyper/lltypesystem/lloperation.py
--- a/rpython/rtyper/lltypesystem/lloperation.py
+++ b/rpython/rtyper/lltypesystem/lloperation.py
@@ -445,6 +445,7 @@
     'get_write_barrier_from_array_failing_case': LLOp(sideeffects=False),
     'gc_get_type_info_group': LLOp(sideeffects=False),
     'll_read_timestamp': LLOp(canrun=True),
+    'll_get_timestamp_unit': LLOp(canrun=True),
 
     # __________ GC operations __________
 
diff --git a/rpython/rtyper/lltypesystem/opimpl.py 
b/rpython/rtyper/lltypesystem/opimpl.py
--- a/rpython/rtyper/lltypesystem/opimpl.py
+++ b/rpython/rtyper/lltypesystem/opimpl.py
@@ -696,6 +696,10 @@
     from rpython.rlib.rtimer import read_timestamp
     return read_timestamp()
 
+def op_ll_get_timestamp_unit():
+    from rpython.rlib.rtimer import get_timestamp_unit
+    return get_timestamp_unit()
+
 def op_debug_fatalerror(ll_msg):
     from rpython.rtyper.lltypesystem import lltype, rstr
     from rpython.rtyper.llinterp import LLFatalError
diff --git a/rpython/translator/c/src/asm_gcc_x86.h 
b/rpython/translator/c/src/asm_gcc_x86.h
--- a/rpython/translator/c/src/asm_gcc_x86.h
+++ b/rpython/translator/c/src/asm_gcc_x86.h
@@ -70,6 +70,7 @@
 // lfence
 // I don't know how important it is, comment talks about time warps
 
+#define READ_TIMESTAMP_UNIT TIMESTAMP_UNIT_TSC
 
 #ifndef PYPY_CPU_HAS_STANDARD_PRECISION
 /* On x86-32, we have to use the following hacks to set and restore
diff --git a/rpython/translator/c/src/asm_gcc_x86_64.h 
b/rpython/translator/c/src/asm_gcc_x86_64.h
--- a/rpython/translator/c/src/asm_gcc_x86_64.h
+++ b/rpython/translator/c/src/asm_gcc_x86_64.h
@@ -7,5 +7,6 @@
     val = (_rdx << 32) | _rax;                          \
 } while (0)
 
+#define READ_TIMESTAMP_UNIT TIMESTAMP_UNIT_TSC
 
 #define RPy_YieldProcessor()  asm("pause")
diff --git a/rpython/translator/c/src/asm_msvc.h 
b/rpython/translator/c/src/asm_msvc.h
--- a/rpython/translator/c/src/asm_msvc.h
+++ b/rpython/translator/c/src/asm_msvc.h
@@ -13,3 +13,4 @@
 #include <intrin.h>
 #pragma intrinsic(__rdtsc)
 #define READ_TIMESTAMP(val)   do { val = (long long)__rdtsc(); } while (0)
+#define READ_TIMESTAMP_UNIT TIMESTAMP_UNIT_TSC
diff --git a/rpython/translator/c/src/debug_print.h 
b/rpython/translator/c/src/debug_print.h
--- a/rpython/translator/c/src/debug_print.h
+++ b/rpython/translator/c/src/debug_print.h
@@ -51,6 +51,11 @@
 RPY_EXTERN long pypy_have_debug_prints;
 RPY_EXPORTED FILE *pypy_debug_file;
 
+/* these should be in sync with the values defined in rlib/rtimer.py */
+#define TIMESTAMP_UNIT_TSC 0
+#define TIMESTAMP_UNIT_NS 1
+#define TIMESTAMP_UNIT_QUERY_PERFORMANCE_COUNTER 2
+
 #define OP_LL_READ_TIMESTAMP(val) READ_TIMESTAMP(val)
 
 #include "src/asm.h"
@@ -62,11 +67,15 @@
 
 #  ifdef _WIN32
 #    define READ_TIMESTAMP(val) QueryPerformanceCounter((LARGE_INTEGER*)&(val))
+#    define READ_TIMESTAMP_UNIT TIMESTAMP_UNIT_QUERY_PERFORMANCE_COUNTER
 #  else
 
 RPY_EXTERN long long pypy_read_timestamp(void);
 
 #    define READ_TIMESTAMP(val)  (val) = pypy_read_timestamp()
+#    define READ_TIMESTAMP_UNIT TIMESTAMP_UNIT_NS
 
 #  endif
 #endif
+
+#define OP_LL_GET_TIMESTAMP_UNIT(res) res = READ_TIMESTAMP_UNIT
_______________________________________________
pypy-commit mailing list
pypy-commit@python.org
https://mail.python.org/mailman/listinfo/pypy-commit

Reply via email to