Skip to content

Commit a8ae4d3

Browse files
committed
Process.clock_getres
PullRequest: truffleruby/706
2 parents 5d34cfb + 37693fd commit a8ae4d3

File tree

5 files changed

+62
-16
lines changed

5 files changed

+62
-16
lines changed

CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,9 @@
1+
# 1.0 RC 15
2+
3+
New features:
4+
5+
* `Process.clock_getres` has been implemented.
6+
17
# 1.0 RC 14
28

39
Updated to Ruby 2.6.2.

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: 46 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_for_id = Truffle::POSIX.truffleposix_clock_getres(id)
138+
if res_for_id == 0 then
139+
Errno.handle
140+
else
141+
res_for_id
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,25 +177,30 @@ 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
164202
end
203+
private_class_method :nanoseconds_to_unit
165204

166205
##
167206
# Sets the process title. Calling this method does not affect the value of

test/mri/excludes/TestProcess.rb

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -28,15 +28,6 @@
2828
exclude :test_spawn_too_long_path, "unknown exec option: :rlimit_nproc"
2929
exclude :test_status_kill, "needs investigation"
3030
exclude :test_to_hash_on_arguments, "needs investigation"
31-
exclude :test_clock_getres, "needs investigation"
32-
exclude :test_clock_getres_CLOCK_BASED_CLOCK_PROCESS_CPUTIME_ID, "needs investigation"
33-
exclude :test_clock_getres_GETRUSAGE_BASED_CLOCK_PROCESS_CPUTIME_ID, "needs investigation"
34-
exclude :test_clock_getres_GETTIMEOFDAY_BASED_CLOCK_REALTIME, "needs investigation"
35-
exclude :test_clock_getres_MACH_ABSOLUTE_TIME_BASED_CLOCK_MONOTONIC, "needs investigation"
36-
exclude :test_clock_getres_TIMES_BASED_CLOCK_MONOTONIC, "needs investigation"
37-
exclude :test_clock_getres_TIMES_BASED_CLOCK_PROCESS_CPUTIME_ID, "needs investigation"
38-
exclude :test_clock_getres_TIME_BASED_CLOCK_REALTIME, "needs investigation"
39-
exclude :test_clock_getres_constants, "needs investigation"
4031
exclude :test_daemon_default, "needs investigation"
4132
exclude :test_daemon_no_threads, "needs investigation"
4233
exclude :test_daemon_nochdir_noclose, "needs investigation"

0 commit comments

Comments
 (0)