clock_getres(*args)
public
Returns the time resolution returned by POSIX clock_getres() function.
clock_id specifies a kind of clock. See the document of Process.clock_gettime for
details.
clock_id can be a symbol as Process.clock_gettime.
However the result may not be accurate. For example,
+Process.clock_getres(:GETTIMEOFDAY_BASED_CLOCK_REALTIME)+ returns 1.0e-06
which means 1 microsecond, but actual resolution can be more coarse.
If the given clock_id is not supported, Errno::EINVAL is raised.
unit specifies a type of the return value. Process.clock_getres
accepts unit as Process.clock_gettime. The default value,
:float_second, is also same as Process.clock_gettime.
Process.clock_getres also
accepts :hertz as unit. :hertz means a the
reciprocal of :float_second.
:hertz can be used to obtain the exact value of the clock ticks
per second for times() function and
CLOCKS_PER_SEC for clock() function.
+Process.clock_getres(:TIMES_BASED_CLOCK_PROCESS_CPUTIME_ID, :hertz)+
returns the clock ticks per second.
+Process.clock_getres(:CLOCK_BASED_CLOCK_PROCESS_CPUTIME_ID, :hertz)+
returns CLOCKS_PER_SEC.
p Process.clock_getres(Process::CLOCK_MONOTONIC)
Show source
VALUE
rb_clock_getres(int argc, VALUE *argv)
{
struct timetick tt;
timetick_int_t numerators[2];
timetick_int_t denominators[2];
int num_numerators = 0;
int num_denominators = 0;
VALUE unit = (rb_check_arity(argc, 1, 2) == 2) ? argv[1] : Qnil;
VALUE clk_id = argv[0];
if (SYMBOL_P(clk_id)) {
if (clk_id == RUBY_GETTIMEOFDAY_BASED_CLOCK_REALTIME) {
tt.giga_count = 0;
tt.count = 1000;
denominators[num_denominators++] = 1000000000;
goto success;
}
if (clk_id == RUBY_TIME_BASED_CLOCK_REALTIME) {
tt.giga_count = 1;
tt.count = 0;
denominators[num_denominators++] = 1000000000;
goto success;
}
if (clk_id == RUBY_TIMES_BASED_CLOCK_MONOTONIC) {
tt.count = 1;
tt.giga_count = 0;
denominators[num_denominators++] = get_clk_tck();
goto success;
}
if (clk_id == RUBY_GETRUSAGE_BASED_CLOCK_PROCESS_CPUTIME_ID) {
tt.giga_count = 0;
tt.count = 1000;
denominators[num_denominators++] = 1000000000;
goto success;
}
if (clk_id == RUBY_TIMES_BASED_CLOCK_PROCESS_CPUTIME_ID) {
tt.count = 1;
tt.giga_count = 0;
denominators[num_denominators++] = get_clk_tck();
goto success;
}
if (clk_id == RUBY_CLOCK_BASED_CLOCK_PROCESS_CPUTIME_ID) {
tt.count = 1;
tt.giga_count = 0;
denominators[num_denominators++] = CLOCKS_PER_SEC;
goto success;
}
if (clk_id == RUBY_MACH_ABSOLUTE_TIME_BASED_CLOCK_MONOTONIC) {
mach_timebase_info_data_t *info = get_mach_timebase_info();
tt.count = 1;
tt.giga_count = 0;
numerators[num_numerators++] = info->numer;
denominators[num_denominators++] = info->denom;
denominators[num_denominators++] = 1000000000;
goto success;
}
}
else {
struct timespec ts;
clockid_t c = NUM2CLOCKID(clk_id);
int ret = clock_getres(c, &ts);
if (ret == -1)
rb_sys_fail("clock_getres");
tt.count = (int32_t)ts.tv_nsec;
tt.giga_count = ts.tv_sec;
denominators[num_denominators++] = 1000000000;
goto success;
}
/* EINVAL emulates clock_getres behavior when clock_id is invalid. */
rb_syserr_fail(EINVAL, 0);
success:
if (unit == ID2SYM(id_hertz)) {
return timetick2dblnum_reciprocal(&tt, numerators, num_numerators, denominators, num_denominators);
}
else {
return make_clock_result(&tt, numerators, num_numerators, denominators, num_denominators, unit);
}
}