Skip to content

Commit d5f59cd

Browse files
committed
Merge remote-tracking branch 'copiousfreetime/process-clock_getres' into process-clock_getres
2 parents 5b05acb + 968f995 commit d5f59cd

File tree

3 files changed

+55
-7
lines changed

3 files changed

+55
-7
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: 45 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,40 @@ def self.time
115115
const_set(key.substring(section.size, key.length), value)
116116
end
117117

118+
def self.clock_getres(id, unit=:float_second)
119+
res = case id
120+
when :MACH_ABSOLUTE_TIME_BASED_CLOCK_MONOTONIC,
121+
CLOCK_MONOTONIC
122+
1
123+
when :GETTIMEOFDAY_BASED_CLOCK_REALTIME,
124+
:GETRUSAGE_BASED_CLOCK_PROCESS_CPUTIME_ID,
125+
:CLOCK_BASED_CLOCK_PROCESS_CPUTIME_ID
126+
1_000
127+
when CLOCK_REALTIME
128+
1_000_000
129+
when :TIMES_BASED_CLOCK_MONOTONIC,
130+
:TIMES_BASED_CLOCK_PROCESS_CPUTIME_ID
131+
10_000_000
132+
when :TIME_BASED_CLOCK_REALTIME
133+
1_000_000_000
134+
when Symbol
135+
raise Errno::EINVAL
136+
else
137+
_res = Truffle::POSIX.truffleposix_clock_getres(id)
138+
if _res == 0 then
139+
Errno.handle
140+
else
141+
_res
142+
end
143+
end
144+
145+
if :hertz == unit then
146+
1.0 / nanoseconds_to_unit(res,:float_second)
147+
else
148+
nanoseconds_to_unit(res,unit)
149+
end
150+
end
151+
118152
def self.clock_gettime(id, unit=:float_second)
119153
if id.is_a?(Symbol)
120154
id = case id
@@ -143,21 +177,25 @@ def self.clock_gettime(id, unit=:float_second)
143177
Errno.handle if time == 0
144178
end
145179

180+
nanoseconds_to_unit(time, unit)
181+
end
182+
183+
def self.nanoseconds_to_unit(nanoseconds, unit)
146184
case unit
147185
when :nanosecond
148-
time
186+
nanoseconds
149187
when :microsecond
150-
time / 1_000
188+
nanoseconds / 1_000
151189
when :millisecond
152-
time / 1_000_000
190+
nanoseconds / 1_000_000
153191
when :second
154-
time / 1_000_000_000
192+
nanoseconds / 1_000_000_000
155193
when :float_microsecond
156-
time / 1e3
194+
nanoseconds / 1e3
157195
when :float_millisecond
158-
time / 1e6
196+
nanoseconds / 1e6
159197
when :float_second, nil
160-
time / 1e9
198+
nanoseconds / 1e9
161199
else
162200
raise ArgumentError, "unexpected unit: #{unit}"
163201
end

0 commit comments

Comments
 (0)