Issue #8658 has been updated by akr (Akira Tanaka).

File clock_gettime-2.patch added

kosaki (Motohiro KOSAKI) wrote:
> First, Process.times() returns user time and system time and they are process  specific. But Process::CLOCK_MONOTONIC is not per-process time.

Yes.  Users can choose any clock with Process.clock_gettime unlike other proposals (#8640, #8096).

It seems many people use CLOCK_REALTIME to measure a time interval, though.

> Second, Linux's CLOCK_MONOTONIC_RAW has the same behavior BSD's CLOCK_MONOTONIC. And, an application which measures a performance need to use CLOCK_MONOTONIC_RAW for avoiding ntp confusing. Then, we should do 1) exporse CLOCK_MONOTONIC_RAW or 2)  Process.clock_gettime(Process::CLOCK_MONOTONIC) uses  CLOCK_MONOTONIC_RAW internally.

OS specific CLOCK_* constants can be defined.
Since Process.clock_gettime is a primitive, exchange clk_id is not a good idea.

> Third, using float is a good ruby convention. If we need to use inter (for precision and performance?), the method should have a precision explanation, likes get_time_nanosecond. I mean, ruby interpreter can't warn nor detect following mistake.
> 
> a = foo # this is usec
> b = bar # this is nsec
> c = a + b
> 
> then, we should warn by method name verbosely. IMHO.

Hm.  It is acceptable as far as the exact result (number of nanoseconds) can be obtained.

After thinking while, I find Process.clock_gettime(clk_id, unit).
unit is an optional argument and :nanoseconds specifies the nanoseconds.
This can help performance on ILP33 because :microseconds with CLOCK_PROCESS_CPUTIME_ID
will not use Bignum until 1073 seconds after process start up.

I updated the patch.

----------------------------------------
Feature #8658: Process.clock_gettime
https://bugs.ruby-lang.org/issues/8658#change-40590

Author: akr (Akira Tanaka)
Status: Open
Priority: Normal
Assignee: 
Category: 
Target version: 


How about adding a new method, Process.clock_gettime(clk_id) ?

Recently there were two feature request for measuring time.
Feature #8640 https://bugs.ruby-lang.org/issues/8640
Feature #8096 https://bugs.ruby-lang.org/issues/8096

It seems they are somewhat different.

clock_gettime() function defined by POSIX is a good
candidate for providing as a method.
I think it can supports the both request.

Also, it has less possible design choices than the requests
because clock_gettime() is defined by POSIX.
People familiar to POSIX can learn the method more easily.

I wrote a patch to implement Process.clock_gettime.
This method can be used as follows.

  % ./ruby -e 'p Process.clock_gettime(Process::CLOCK_MONOTONIC)'
  2701692957811563

Several considerations:

I implemented the method as a module function of Process.
It is same as Process.times.
I expect clock_gettime is used mainly for measuring
time interval and wall clock time is not important.
So I didn't use Time.

The method returns a number of nanoseconds as an integer.
It is not so unexpected if user knows clock_gettime() in POSIX.

clock_gettime() returns it as struct timespec
which contains two fields: tv_sec and tv_nsec.

Although tv_sec is time_t, Time is not appropriate because
the origin (zero) can be other than the Epoch.
Actually CLOCK_MONOTONIC means elapsed time since
the system start-up time on Linux.

Also, I expect the result is subtracted in most case:
  t1 = Process.clock_gettime(...)
  ...
  t2 = Process.clock_gettime(...)
  t = t2 - t1
So the result should be easy to subtract.
An array such as [sec, nsec] is difficult to subtract.

The result is an integer, not a float.
IEEE 754 double is not enough to represent the result
of clock_gettime(CLOCK_REALTIME).
It contains 19 digits in decimal now but IEEE 754 double
can represent only 15 digits.

On LP64 systems, Fixnum can represent 2**62-1.
So (2**62-1)/(365.25*24*60*60*1e9)=146.1 years are representable
without object allocation.

On ILP32 and LLP64 systems, Fixnum can represent 2**30-1.
So (2**30-1)/1e9=1.07 seconds are representable
without object allocation.
This means Bignum allocations are mostly required except
the origin is very recent.

clock_gettime() is defined by POSIX.
Linux, NetBSD, FreeBSD, OpenBSD has it, at least.

If clock_gettime() is not available,
an emulation layer for CLOCK_REALTIME is implementable
using gettimeofday().
(not implemented yet, though.)

Any comments?



-- 
http://bugs.ruby-lang.org/