@@ -115,25 +115,22 @@ def self.time
115
115
const_set ( key . substring ( section . size , key . length ) , value )
116
116
end
117
117
118
- def self . clock_gettime ( id , unit = :float_second )
119
- if id . is_a? ( Symbol )
120
- id = case id
121
- when :GETTIMEOFDAY_BASED_CLOCK_REALTIME ,
122
- :TIME_BASED_CLOCK_REALTIME
123
- CLOCK_REALTIME
124
- when :MACH_ABSOLUTE_TIME_BASED_CLOCK_MONOTONIC ,
125
- :TIMES_BASED_CLOCK_MONOTONIC
126
- CLOCK_MONOTONIC
127
- when :GETRUSAGE_BASED_CLOCK_PROCESS_CPUTIME_ID ,
128
- :TIMES_BASED_CLOCK_PROCESS_CPUTIME_ID ,
129
- :CLOCK_BASED_CLOCK_PROCESS_CPUTIME_ID
130
- CLOCK_THREAD_CPUTIME_ID
131
- else
132
- raise Errno ::EINVAL
133
- end
118
+ def self . clock_getres ( id , unit = :float_second )
119
+ case id = normalize_clock_id ( id )
120
+ when CLOCK_REALTIME
121
+ res = 1_000_000
122
+ when CLOCK_MONOTONIC
123
+ res = 1
124
+ else
125
+ res = Truffle ::POSIX . truffleposix_clock_getres ( id )
126
+ Errno . handle if res == 0
134
127
end
135
128
136
- case id
129
+ nanoseconds_to_unit ( res , unit )
130
+ end
131
+
132
+ def self . clock_gettime ( id , unit = :float_second )
133
+ case id = normalize_clock_id ( id )
137
134
when CLOCK_REALTIME
138
135
time = Truffle . invoke_primitive ( :process_time_currenttimemillis ) * 1_000_000
139
136
when CLOCK_MONOTONIC
@@ -143,21 +140,43 @@ def self.clock_gettime(id, unit=:float_second)
143
140
Errno . handle if time == 0
144
141
end
145
142
143
+ nanoseconds_to_unit ( time , unit )
144
+ end
145
+
146
+ def self . normalize_clock_id ( id )
147
+ return id unless id . is_a? ( Symobl )
148
+ case id
149
+ when :GETTIMEOFDAY_BASED_CLOCK_REALTIME ,
150
+ :TIME_BASED_CLOCK_REALTIME
151
+ CLOCK_REALTIME
152
+ when :MACH_ABSOLUTE_TIME_BASED_CLOCK_MONOTONIC ,
153
+ :TIMES_BASED_CLOCK_MONOTONIC
154
+ CLOCK_MONOTONIC
155
+ when :GETRUSAGE_BASED_CLOCK_PROCESS_CPUTIME_ID ,
156
+ :TIMES_BASED_CLOCK_PROCESS_CPUTIME_ID ,
157
+ :CLOCK_BASED_CLOCK_PROCESS_CPUTIME_ID
158
+ CLOCK_THREAD_CPUTIME_ID
159
+ else
160
+ raise Errno ::EINVAL
161
+ end
162
+ end
163
+
164
+ def self . nanoseconds_to_unit ( nanoseconds , unit = :nanosecond )
146
165
case unit
147
166
when :nanosecond
148
- time
167
+ nanoseconds
149
168
when :microsecond
150
- time / 1_000
169
+ nanoseconds / 1_000
151
170
when :millisecond
152
- time / 1_000_000
171
+ nanoseconds / 1_000_000
153
172
when :second
154
- time / 1_000_000_000
173
+ nanoseconds / 1_000_000_000
155
174
when :float_microsecond
156
- time / 1e3
175
+ nanoseconds / 1e3
157
176
when :float_millisecond
158
- time / 1e6
177
+ nanoseconds / 1e6
159
178
when :float_second , nil
160
- time / 1e9
179
+ nanoseconds / 1e9
161
180
else
162
181
raise ArgumentError , "unexpected unit: #{ unit } "
163
182
end
0 commit comments