Method: Process#clock_gettime
- Defined in:
- process.c
#clock_gettime(clock_id[, unit]) ⇒ Numeric (private)
Returns a time returned by POSIX clock_gettime() function.
p Process.clock_gettime(Process::CLOCK_MONOTONIC)
#=> 896053.968060096
clock_id
specifies a kind of clock. It is specified as a constant which begins with Process::CLOCK_
such as Process::CLOCK_REALTIME and Process::CLOCK_MONOTONIC.
The supported constants depends on OS and version. Ruby provides following types of clock_id
if available.
- CLOCK_REALTIME
-
SUSv2 to 4, Linux 2.5.63, FreeBSD 3.0, NetBSD 2.0, OpenBSD 2.1, macOS 10.12
- CLOCK_MONOTONIC
-
SUSv3 to 4, Linux 2.5.63, FreeBSD 3.0, NetBSD 2.0, OpenBSD 3.4, macOS 10.12
- CLOCK_PROCESS_CPUTIME_ID
-
SUSv3 to 4, Linux 2.5.63, FreeBSD 9.3, OpenBSD 5.4, macOS 10.12
- CLOCK_THREAD_CPUTIME_ID
-
SUSv3 to 4, Linux 2.5.63, FreeBSD 7.1, OpenBSD 5.4, macOS 10.12
- CLOCK_VIRTUAL
-
FreeBSD 3.0, OpenBSD 2.1
- CLOCK_PROF
-
FreeBSD 3.0, OpenBSD 2.1
- CLOCK_REALTIME_FAST
-
FreeBSD 8.1
- CLOCK_REALTIME_PRECISE
-
FreeBSD 8.1
- CLOCK_REALTIME_COARSE
-
Linux 2.6.32
- CLOCK_REALTIME_ALARM
-
Linux 3.0
- CLOCK_MONOTONIC_FAST
-
FreeBSD 8.1
- CLOCK_MONOTONIC_PRECISE
-
FreeBSD 8.1
- CLOCK_MONOTONIC_COARSE
-
Linux 2.6.32
- CLOCK_MONOTONIC_RAW
-
Linux 2.6.28, macOS 10.12
- CLOCK_MONOTONIC_RAW_APPROX
-
macOS 10.12
- CLOCK_BOOTTIME
-
Linux 2.6.39
- CLOCK_BOOTTIME_ALARM
-
Linux 3.0
- CLOCK_UPTIME
-
FreeBSD 7.0, OpenBSD 5.5
- CLOCK_UPTIME_FAST
-
FreeBSD 8.1
- CLOCK_UPTIME_RAW
-
macOS 10.12
- CLOCK_UPTIME_RAW_APPROX
-
macOS 10.12
- CLOCK_UPTIME_PRECISE
-
FreeBSD 8.1
- CLOCK_SECOND
-
FreeBSD 8.1
- CLOCK_TAI
-
Linux 3.10
Note that SUS stands for Single Unix Specification. SUS contains POSIX and clock_gettime is defined in the POSIX part. SUS defines CLOCK_REALTIME mandatory but CLOCK_MONOTONIC, CLOCK_PROCESS_CPUTIME_ID and CLOCK_THREAD_CPUTIME_ID are optional.
Also, several symbols are accepted as clock_id
. There are emulations for clock_gettime().
For example, Process::CLOCK_REALTIME is defined as :GETTIMEOFDAY_BASED_CLOCK_REALTIME
when clock_gettime() is not available.
Emulations for CLOCK_REALTIME
:
- :GETTIMEOFDAY_BASED_CLOCK_REALTIME
-
Use gettimeofday() defined by SUS. (SUSv4 obsoleted it, though.) The resolution is 1 microsecond.
- :TIME_BASED_CLOCK_REALTIME
-
Use time() defined by ISO C. The resolution is 1 second.
Emulations for CLOCK_MONOTONIC
:
- :MACH_ABSOLUTE_TIME_BASED_CLOCK_MONOTONIC
-
Use mach_absolute_time(), available on Darwin. The resolution is CPU dependent.
- :TIMES_BASED_CLOCK_MONOTONIC
-
Use the result value of times() defined by POSIX. POSIX defines it as “times() shall return the elapsed real time, in clock ticks, since an arbitrary point in the past (for example, system start-up time)”. For example, GNU/Linux returns a value based on jiffies and it is monotonic. However, 4.4BSD uses gettimeofday() and it is not monotonic. (FreeBSD uses clock_gettime(CLOCK_MONOTONIC) instead, though.) The resolution is the clock tick. “getconf CLK_TCK” command shows the clock ticks per second. (The clock ticks per second is defined by HZ macro in older systems.) If it is 100 and clock_t is 32 bits integer type, the resolution is 10 millisecond and cannot represent over 497 days.
Emulations for CLOCK_PROCESS_CPUTIME_ID
:
- :GETRUSAGE_BASED_CLOCK_PROCESS_CPUTIME_ID
-
Use getrusage() defined by SUS. getrusage() is used with RUSAGE_SELF to obtain the time only for the calling process (excluding the time for child processes). The result is addition of user time (ru_utime) and system time (ru_stime). The resolution is 1 microsecond.
- :TIMES_BASED_CLOCK_PROCESS_CPUTIME_ID
-
Use times() defined by POSIX. The result is addition of user time (tms_utime) and system time (tms_stime). tms_cutime and tms_cstime are ignored to exclude the time for child processes. The resolution is the clock tick. “getconf CLK_TCK” command shows the clock ticks per second. (The clock ticks per second is defined by HZ macro in older systems.) If it is 100, the resolution is 10 millisecond.
- :CLOCK_BASED_CLOCK_PROCESS_CPUTIME_ID
-
Use clock() defined by ISO C. The resolution is 1/CLOCKS_PER_SEC. CLOCKS_PER_SEC is the C-level macro defined by time.h. SUS defines CLOCKS_PER_SEC is 1000000. Non-Unix systems may define it a different value, though. If CLOCKS_PER_SEC is 1000000 as SUS, the resolution is 1 microsecond. If CLOCKS_PER_SEC is 1000000 and clock_t is 32 bits integer type, it cannot represent over 72 minutes.
If the given clock_id
is not supported, Errno::EINVAL is raised.
unit
specifies a type of the return value.
- :float_second
-
number of seconds as a float (default)
- :float_millisecond
-
number of milliseconds as a float
- :float_microsecond
-
number of microseconds as a float
- :second
-
number of seconds as an integer
- :millisecond
-
number of milliseconds as an integer
- :microsecond
-
number of microseconds as an integer
- :nanosecond
-
number of nanoseconds as an integer
The underlying function, clock_gettime(), returns a number of nanoseconds. Float object (IEEE 754 double) is not enough to represent the return value for CLOCK_REALTIME. If the exact nanoseconds value is required, use :nanoseconds
as the unit
.
The origin (zero) of the returned value varies. For example, system start up time, process start up time, the Epoch, etc.
The origin in CLOCK_REALTIME is defined as the Epoch (1970-01-01 00:00:00 UTC). But some systems count leap seconds and others doesn’t. So the result can be interpreted differently across systems. Time.now is recommended over CLOCK_REALTIME.
8249 8250 8251 8252 8253 8254 8255 8256 8257 8258 8259 8260 8261 8262 8263 8264 8265 8266 8267 8268 8269 8270 8271 8272 8273 8274 8275 8276 8277 8278 8279 8280 8281 8282 8283 8284 8285 8286 8287 8288 8289 8290 8291 8292 8293 8294 8295 8296 8297 8298 8299 8300 8301 8302 8303 8304 8305 8306 8307 8308 8309 8310 8311 8312 8313 8314 8315 8316 8317 8318 8319 8320 8321 8322 8323 8324 8325 8326 8327 8328 8329 8330 8331 8332 8333 8334 8335 8336 8337 8338 8339 8340 8341 8342 8343 8344 8345 8346 8347 8348 8349 8350 8351 8352 8353 8354 8355 8356 8357 8358 8359 8360 8361 8362 8363 8364 8365 8366 8367 8368 8369 8370 8371 8372 8373 8374 8375 8376 8377 8378 8379 8380 8381 8382 8383 8384 8385 8386 8387 8388 8389 8390 8391 8392 8393 8394 8395 8396 8397 8398 8399 8400 8401 8402 8403 8404 8405 8406 |
# File 'process.c', line 8249
static VALUE
rb_clock_gettime(int argc, VALUE *argv, VALUE _)
{
int ret;
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)) {
/*
* Non-clock_gettime clocks are provided by symbol clk_id.
*/
#ifdef HAVE_GETTIMEOFDAY
/*
* GETTIMEOFDAY_BASED_CLOCK_REALTIME is used for
* CLOCK_REALTIME if clock_gettime is not available.
*/
#define RUBY_GETTIMEOFDAY_BASED_CLOCK_REALTIME ID2SYM(id_GETTIMEOFDAY_BASED_CLOCK_REALTIME)
if (clk_id == RUBY_GETTIMEOFDAY_BASED_CLOCK_REALTIME) {
struct timeval tv;
ret = gettimeofday(&tv, 0);
if (ret != 0)
rb_sys_fail("gettimeofday");
tt.giga_count = tv.tv_sec;
tt.count = (int32_t)tv.tv_usec * 1000;
denominators[num_denominators++] = 1000000000;
goto success;
}
#endif
#define RUBY_TIME_BASED_CLOCK_REALTIME ID2SYM(id_TIME_BASED_CLOCK_REALTIME)
if (clk_id == RUBY_TIME_BASED_CLOCK_REALTIME) {
time_t t;
t = time(NULL);
if (t == (time_t)-1)
rb_sys_fail("time");
tt.giga_count = t;
tt.count = 0;
denominators[num_denominators++] = 1000000000;
goto success;
}
#ifdef HAVE_TIMES
#define RUBY_TIMES_BASED_CLOCK_MONOTONIC \
ID2SYM(id_TIMES_BASED_CLOCK_MONOTONIC)
if (clk_id == RUBY_TIMES_BASED_CLOCK_MONOTONIC) {
struct tms buf;
clock_t c;
unsigned_clock_t uc;
c = times(&buf);
if (c == (clock_t)-1)
rb_sys_fail("times");
uc = (unsigned_clock_t)c;
tt.count = (int32_t)(uc % 1000000000);
tt.giga_count = (uc / 1000000000);
denominators[num_denominators++] = get_clk_tck();
goto success;
}
#endif
#ifdef RUSAGE_SELF
#define RUBY_GETRUSAGE_BASED_CLOCK_PROCESS_CPUTIME_ID \
ID2SYM(id_GETRUSAGE_BASED_CLOCK_PROCESS_CPUTIME_ID)
if (clk_id == RUBY_GETRUSAGE_BASED_CLOCK_PROCESS_CPUTIME_ID) {
struct rusage usage;
int32_t usec;
ret = getrusage(RUSAGE_SELF, &usage);
if (ret != 0)
rb_sys_fail("getrusage");
tt.giga_count = usage.ru_utime.tv_sec + usage.ru_stime.tv_sec;
usec = (int32_t)(usage.ru_utime.tv_usec + usage.ru_stime.tv_usec);
if (1000000 <= usec) {
tt.giga_count++;
usec -= 1000000;
}
tt.count = usec * 1000;
denominators[num_denominators++] = 1000000000;
goto success;
}
#endif
#ifdef HAVE_TIMES
#define RUBY_TIMES_BASED_CLOCK_PROCESS_CPUTIME_ID \
ID2SYM(id_TIMES_BASED_CLOCK_PROCESS_CPUTIME_ID)
if (clk_id == RUBY_TIMES_BASED_CLOCK_PROCESS_CPUTIME_ID) {
struct tms buf;
unsigned_clock_t utime, stime;
if (times(&buf) == (clock_t)-1)
rb_sys_fail("times");
utime = (unsigned_clock_t)buf.tms_utime;
stime = (unsigned_clock_t)buf.tms_stime;
tt.count = (int32_t)((utime % 1000000000) + (stime % 1000000000));
tt.giga_count = (utime / 1000000000) + (stime / 1000000000);
if (1000000000 <= tt.count) {
tt.count -= 1000000000;
tt.giga_count++;
}
denominators[num_denominators++] = get_clk_tck();
goto success;
}
#endif
#define RUBY_CLOCK_BASED_CLOCK_PROCESS_CPUTIME_ID \
ID2SYM(id_CLOCK_BASED_CLOCK_PROCESS_CPUTIME_ID)
if (clk_id == RUBY_CLOCK_BASED_CLOCK_PROCESS_CPUTIME_ID) {
clock_t c;
unsigned_clock_t uc;
errno = 0;
c = clock();
if (c == (clock_t)-1)
rb_sys_fail("clock");
uc = (unsigned_clock_t)c;
tt.count = (int32_t)(uc % 1000000000);
tt.giga_count = uc / 1000000000;
denominators[num_denominators++] = CLOCKS_PER_SEC;
goto success;
}
#ifdef __APPLE__
#define RUBY_MACH_ABSOLUTE_TIME_BASED_CLOCK_MONOTONIC ID2SYM(id_MACH_ABSOLUTE_TIME_BASED_CLOCK_MONOTONIC)
if (clk_id == RUBY_MACH_ABSOLUTE_TIME_BASED_CLOCK_MONOTONIC) {
const mach_timebase_info_data_t *info = get_mach_timebase_info();
uint64_t t = mach_absolute_time();
tt.count = (int32_t)(t % 1000000000);
tt.giga_count = t / 1000000000;
numerators[num_numerators++] = info->numer;
denominators[num_denominators++] = info->denom;
denominators[num_denominators++] = 1000000000;
goto success;
}
#endif
}
else {
#if defined(HAVE_CLOCK_GETTIME)
struct timespec ts;
clockid_t c;
c = NUM2CLOCKID(clk_id);
ret = clock_gettime(c, &ts);
if (ret == -1)
rb_sys_fail("clock_gettime");
tt.count = (int32_t)ts.tv_nsec;
tt.giga_count = ts.tv_sec;
denominators[num_denominators++] = 1000000000;
goto success;
#endif
}
/* EINVAL emulates clock_gettime behavior when clock_id is invalid. */
rb_syserr_fail(EINVAL, 0);
success:
return make_clock_result(&tt, numerators, num_numerators, denominators, num_denominators, unit);
}
|