Skip to content

Commit 9ca1bc5

Browse files
adding Process.clock_getres
1 parent 89376cb commit 9ca1bc5

File tree

3 files changed

+53
-24
lines changed

3 files changed

+53
-24
lines changed

src/main/c/truffleposix/truffleposix.c

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -399,6 +399,15 @@ int64_t truffleposix_clock_gettime(int clock) {
399399
}
400400
return ((int64_t) timespec.tv_sec * 1000000000) + (int64_t) timespec.tv_nsec;
401401
}
402+
403+
int64_t truffleposix_clock_getres(int clock) {
404+
struct timespec timespec;
405+
int ret = clock_getres((clockid_t) clock, &timespec);
406+
if (ret != 0) {
407+
return 0;
408+
}
409+
return ((int64_t) timespec.tv_sec * 1000000000) + (int64_t) timespec.tv_nsec;
410+
}
402411
#endif
403412

404413
#define CHECK(call, label) if ((error = call) != 0) { perror(#call); goto label; }

src/main/ruby/core/posix.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -169,6 +169,7 @@ def self.attach_function_eagerly(native_name, argument_types, return_type,
169169
attach_function :chmod, [:string, :mode_t], :int
170170
attach_function :chown, [:string, :uid_t, :gid_t], :int
171171
attach_function :chroot, [:string], :int
172+
attach_function :truffleposix_clock_getres, [:int], :int64_t, LIBTRUFFLEPOSIX
172173
attach_function :truffleposix_clock_gettime, [:int], :int64_t, LIBTRUFFLEPOSIX
173174
attach_function :close, [:int], :int
174175
attach_function :closedir, [:pointer], :int

src/main/ruby/core/process.rb

Lines changed: 43 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -115,25 +115,22 @@ def self.time
115115
const_set(key.substring(section.size, key.length), value)
116116
end
117117

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
134127
end
135128

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)
137134
when CLOCK_REALTIME
138135
time = Truffle.invoke_primitive(:process_time_currenttimemillis) * 1_000_000
139136
when CLOCK_MONOTONIC
@@ -143,21 +140,43 @@ def self.clock_gettime(id, unit=:float_second)
143140
Errno.handle if time == 0
144141
end
145142

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)
146165
case unit
147166
when :nanosecond
148-
time
167+
nanoseconds
149168
when :microsecond
150-
time / 1_000
169+
nanoseconds / 1_000
151170
when :millisecond
152-
time / 1_000_000
171+
nanoseconds / 1_000_000
153172
when :second
154-
time / 1_000_000_000
173+
nanoseconds / 1_000_000_000
155174
when :float_microsecond
156-
time / 1e3
175+
nanoseconds / 1e3
157176
when :float_millisecond
158-
time / 1e6
177+
nanoseconds / 1e6
159178
when :float_second, nil
160-
time / 1e9
179+
nanoseconds / 1e9
161180
else
162181
raise ArgumentError, "unexpected unit: #{unit}"
163182
end

0 commit comments

Comments
 (0)