Debian Codegraph @

rpython/rlib/rtimer.py

raw symbols SCIP
1 import time
2
3 from rpython.rlib.rarithmetic import r_longlong, r_uint
4 from rpython.rlib.rarithmetic import intmask, longlongmask
5 from rpython.rtyper.extregistry import ExtRegistryEntry
6 from rpython.rtyper.lltypesystem import lltype, rffi
7
8 _is_64_bit = r_uint.BITS > 32
9
10 from rpython.annotator.model import SomeInteger
11 if _is_64_bit:
12 s_TIMESTAMP = SomeInteger()
13 TIMESTAMP_type = lltype.Signed
14 else:
15 s_TIMESTAMP = SomeInteger(knowntype=r_longlong)
16 TIMESTAMP_type = rffi.LONGLONG
17
18
19 # unit of values returned by read_timestamp. Should be in sync with the ones
20 # defined in translator/c/debug_print.h
21 UNIT_TSC = 0
22 UNIT_NS = 1 # nanoseconds
23 UNIT_QUERY_PERFORMANCE_COUNTER = 2
24
25 def read_timestamp():
26 # Returns a longlong on 32-bit, and a regular int on 64-bit.
27 # When running on top of python, build the result a bit arbitrarily.
28 x = long(time.time() * 500000000)
29 if _is_64_bit:
30 return intmask(x)
31 else:
32 return longlongmask(x)
33
34 def get_timestamp_unit():
35 # an unit which is as arbitrary as the way we build the result of
36 # read_timestamp :)
37 return UNIT_NS
38
39
40 class ReadTimestampEntry(ExtRegistryEntry):
41 _about_ = read_timestamp
42
43 def compute_result_annotation(self):
44 return s_TIMESTAMP
45
46 def specialize_call(self, hop):
47 hop.exception_cannot_occur()
48 return hop.genop("ll_read_timestamp", [], resulttype=TIMESTAMP_type)
49
50
51 class ReadTimestampEntry(ExtRegistryEntry):
52 _about_ = get_timestamp_unit
53
54 def compute_result_annotation(self):
55 from rpython.annotator.model import SomeInteger
56 return SomeInteger(nonneg=True)
57
58 def specialize_call(self, hop):
59 hop.exception_cannot_occur()
60 return hop.genop("ll_get_timestamp_unit", [], resulttype=lltype.Signed)