71
71
@ CoreFunctions (defineModule = "time" )
72
72
public final class TimeModuleBuiltins extends PythonBuiltins {
73
73
private static final int DELAY_NANOS = 10 ;
74
+ private static final long PERF_COUNTER_START = TruffleOptions .AOT ? 0 : System .nanoTime ();
74
75
75
76
@ Override
76
77
protected List <? extends NodeFactory <? extends PythonBuiltinBaseNode >> getNodeFactories () {
@@ -182,6 +183,35 @@ public double time() {
182
183
}
183
184
}
184
185
186
+ // time.time_ns()
187
+ @ Builtin (name = "time_ns" , minNumOfPositionalArgs = 0 , doc = "Similar to time() but returns time as an integer number of nanoseconds since the epoch." )
188
+ @ GenerateNodeFactory
189
+ public abstract static class PythonTimeNsNode extends PythonBuiltinNode {
190
+
191
+ /**
192
+ * The maximum date, which are systems able to handle is 2262 04 11. This corresponds to the
193
+ * 64 bit long.
194
+ *
195
+ * @return
196
+ */
197
+ @ Specialization
198
+ public long time () {
199
+ return timeNanoSeconds ();
200
+ }
201
+
202
+ @ TruffleBoundary
203
+ private static long timeNanoSeconds () {
204
+ Instant now = Instant .now ();
205
+ // From java we are not able to obtain the nano seconds resolution. It depends on the
206
+ // jdk
207
+ // JKD 1.8 the resolution is usually miliseconds (for example 1576081173486000000)
208
+ // From JDK 9 including JDK 11 the resolution is usually microseconds (for example
209
+ // 1576082578022393000)
210
+ // To obtain really nanosecond resulution we have to fake the nanoseconds
211
+ return now .getEpochSecond () * 1000000000L + now .getNano ();
212
+ }
213
+ }
214
+
185
215
// time.monotonic()
186
216
@ Builtin (name = "monotonic" , minNumOfPositionalArgs = 0 )
187
217
@ GenerateNodeFactory
@@ -190,15 +220,44 @@ public abstract static class PythonMonotonicNode extends PythonBuiltinNode {
190
220
@ Specialization
191
221
@ TruffleBoundary
192
222
public double time () {
223
+ return System .nanoTime () / 1000000000D ;
224
+ }
225
+ }
226
+
227
+ // time.monotonic_ns()
228
+ @ Builtin (name = "monotonic_ns" , minNumOfPositionalArgs = 0 , doc = "Similar to monotonic(), but return time as nanoseconds." )
229
+ @ GenerateNodeFactory
230
+ public abstract static class PythonMonotonicNsNode extends PythonBuiltinNode {
231
+
232
+ @ Specialization
233
+ @ TruffleBoundary
234
+ public long time () {
193
235
return System .nanoTime ();
194
236
}
195
237
}
196
238
197
239
@ Builtin (name = "perf_counter" , minNumOfPositionalArgs = 0 )
198
240
@ GenerateNodeFactory
199
- public abstract static class PythonPerfCounterNode extends PythonClockNode {
241
+ public abstract static class PythonPerfCounterNode extends PythonBuiltinNode {
242
+ @ Specialization
243
+ @ TruffleBoundary
244
+ public double counter () {
245
+ return (System .nanoTime () - PERF_COUNTER_START ) / 1000_000_000.0 ;
246
+ }
247
+ }
248
+
249
+ @ Builtin (name = "perf_counter_ns" , minNumOfPositionalArgs = 0 )
250
+ @ GenerateNodeFactory
251
+ public abstract static class PythonPerfCounterNsNode extends PythonBuiltinNode {
252
+
253
+ @ Specialization
254
+ @ TruffleBoundary
255
+ public long counter () {
256
+ return System .nanoTime () - PERF_COUNTER_START ;
257
+ }
200
258
}
201
259
260
+ // TODO time.clock in 3.8 is removed in 3.5 is deprecated
202
261
// time.clock()
203
262
@ Builtin (name = "clock" , minNumOfPositionalArgs = 0 )
204
263
@ GenerateNodeFactory
0 commit comments