Feature #8809

Process.clock_getres

Added by Akira Tanaka 8 months ago. Updated 8 months ago.

[ruby-core:56780]
Status:Closed
Priority:Normal
Assignee:-
Category:-
Target version:-

Description

How about Process.clock_getres method?

POSIX defines clock_getres function to provide resolution information
of clocks.

I made a pacth to invoke clock_getres function.

Process.clockgetres(Process::CLOCKMONOTONIC) #=> 1.0e-09
Process.clockgetres(Process::CLOCKMONOTONIC_COARSE) #=> 0.00400025

The result means that the resolution of CLOCKMONOTONIC is 1ns and
the resolution of CLOCK
MONOTONIC_COARSE is 4.00025ms.

Process.clockgetres has optional unit argument as Process.clockgettime.

Process.clockgetres(Process::CLOCKMONOTONIC, :nanosecond) #=> 1
Process.clockgetres(Process::CLOCKMONOTONIC_COARSE, :nanosecond) #=> 4000250

It supports emulated clocks as well.

Process.clockgetres(:SUSGETTIMEOFDAYBASEDCLOCKREALTIME) #=> 1.0000000000000002e-06
Process.clock
getres(:SUSGETRUSAGEBASEDCLOCKPROCESSCPUTIMEID) #=> 1.0000000000000002e-06

The unit argument can be :hertz, which means the reciprocal of the second.

Process.clockgetres(:SUSGETRUSAGEBASEDCLOCKPROCESSCPUTIME_ID, :hertz) #=> 1000000.0

Note that
Process.clockgetres(:POSIXTIMESBASEDCLOCKPROCESSCPUTIMEID, :hertz) is the clock ticks per second (CLKTCK) and
Process.clockgetres(:ISOCCLOCKBASEDCLOCKPROCESSCPUTIMEID, :hertz) is CLOCKPERSEC.
I wanted to access them easily to investigate emulated clock behaviors on
various OSes.

Any comments?

clock_getres.patch Magnifier (4.39 KB) Akira Tanaka, 08/22/2013 11:33 PM

clock_getres-2.patch Magnifier (6.06 KB) Akira Tanaka, 08/24/2013 09:24 AM

clock_getres-3.patch Magnifier (6 KB) Akira Tanaka, 08/24/2013 10:41 AM

Associated revisions

Revision 42744
Added by Akira Tanaka 8 months ago

  • process.c (rbclockgetres): New method.
    (timetick2dblnum_reciprocal): New function.

  • configure.in: Check clock_getres.

[Feature #8809] accepted at
DevelopersMeeting20130831Japan
https://bugs.ruby-lang.org/projects/ruby/wiki/DevelopersMeeting20130831Japan

History

#1 Updated by David MacMahon 8 months ago

On Aug 22, 2013, at 7:33 AM, akr (Akira Tanaka) wrote:

I made a pacth to invoke clock_getres function.

Thanks for making a patch! It makes the discussion much less abstract (more real?). I think I will try to follow your example in the future.

Process.clockgetres(Process::CLOCKMONOTONIC) #=> 1.0e-09
Process.clockgetres(Process::CLOCKMONOTONIC_COARSE) #=> 0.00400025

The result means that the resolution of CLOCKMONOTONIC is 1ns and
the resolution of CLOCK
MONOTONIC_COARSE is 4.00025ms.

Did you consider having these methods return Rational rather than Float?

Process.clockgetres has optional unit argument as Process.clockgettime.

Process.clockgetres(Process::CLOCKMONOTONIC, :nanosecond) #=> 1
Process.clockgetres(Process::CLOCKMONOTONIC_COARSE, :nanosecond) #=> 4000250

It supports emulated clocks as well.

Process.clockgetres(:SUSGETTIMEOFDAYBASEDCLOCKREALTIME) #=> 1.0000000000000002e-06
Process.clock
getres(:SUSGETRUSAGEBASEDCLOCKPROCESSCPUTIMEID) #=> 1.0000000000000002e-06

The unit argument can be :hertz, which means the reciprocal of the second.

Process.clockgetres(:SUSGETRUSAGEBASEDCLOCKPROCESSCPUTIME_ID, :hertz) #=> 1000000.0

How would you feel about supporting :ns and :hz as equivalents for :nanosecond and :hertz?

Note that
Process.clockgetres(:POSIXTIMESBASEDCLOCKPROCESSCPUTIMEID, :hertz) is the clock ticks per second (CLKTCK) and
Process.clockgetres(:ISOCCLOCKBASEDCLOCKPROCESSCPUTIMEID, :hertz) is CLOCKPERSEC.
I wanted to access them easily to investigate emulated clock behaviors on
various OSes.

Those are some long symbols! Are these intended only for experimental/investigative use?

Any comments?

I appreciate having access to POSIX functionality, so I'm all for this idea!

Thanks,
Dave

#2 Updated by Akira Tanaka 8 months ago

2013/8/23 David MacMahon davidm@astro.berkeley.edu:

Process.clockgetres(Process::CLOCKMONOTONIC) #=> 1.0e-09
Process.clockgetres(Process::CLOCKMONOTONIC_COARSE) #=> 0.00400025

The result means that the resolution of CLOCKMONOTONIC is 1ns and
the resolution of CLOCK
MONOTONIC_COARSE is 4.00025ms.

Did you consider having these methods return Rational rather than Float?

Process.clockgetres can return rational if it supports
:rational
second as a unit.

The current default of unit is :float_second and
I think float is good enough.

Process.clockgetres(:SUSGETRUSAGEBASEDCLOCKPROCESSCPUTIME_ID, :hertz) #=> 1000000.0

How would you feel about supporting :ns and :hz as equivalents for :nanosecond and :hertz?

It is difficult to support :microsecond in that style
because the SI prefix, Greek m, is not representable in ASCII.

Someone may argue :hz should be :Hz.

I feel :float_s is bit curious.

So it is difficult to adopt :ns style as canonical style of unit.

I think several aliases are possible but
I'd like to concentrate to main feature.
The discussion for what aliases should be added or not can be diverge.

Note that
Process.clockgetres(:POSIXTIMESBASEDCLOCKPROCESSCPUTIMEID, :hertz) is the clock ticks per second (CLKTCK) and
Process.clockgetres(:ISOCCLOCKBASEDCLOCKPROCESSCPUTIMEID, :hertz) is CLOCKPERSEC.
I wanted to access them easily to investigate emulated clock behaviors on
various OSes.

Those are some long symbols! Are these intended only for experimental/investigative use?

I choose the long symbols that is longer than Process::CLOCKPROCESSCPUTIMEID.
Basically users should use Process::CLOCK
PROCESSCPUTIMEID if no reason.
--
Tanaka Akira

#3 Updated by David MacMahon 8 months ago

On Aug 22, 2013, at 3:37 PM, Tanaka Akira wrote:

Process.clockgetres can return rational if it supports
:rational
second as a unit.

The current default of unit is :float_second and
I think float is good enough.

Agreed. Plus, if someone really wants, they can request nanosecond precision, which is all that clock_getres supports (at least on Linux).

Process.clockgetres(:SUSGETRUSAGEBASEDCLOCKPROCESSCPUTIME_ID, :hertz) #=> 1000000.0

How would you feel about supporting :ns and :hz as equivalents for :nanosecond and :hertz?

It is difficult to support :microsecond in that style
because the SI prefix, Greek m, is not representable in ASCII.

I know it's not SI, but I often use ASCII "u" for Greek m ("µ"), so :microsecond would be aliased by :us.

Someone may argue :hz should be :Hz.

No doubt! :-)

I feel :float_s is bit curious.

How about separating the type and the resolution into two different parameters?

 Process.clock_getres(Process::CLOCK_MONOTONIC, :float, :second)

...or...

 Process.clock_getres(Process::CLOCK_MONOTONIC, Float, :second)

So it is difficult to adopt :ns style as canonical style of unit.

I think several aliases are possible but
I'd like to concentrate to main feature.
The discussion for what aliases should be added or not can be diverge.

Agreed. I think the main feature is great!

Note that
Process.clockgetres(:POSIXTIMESBASEDCLOCKPROCESSCPUTIMEID, :hertz) is the clock ticks per second (CLKTCK) and
Process.clockgetres(:ISOCCLOCKBASEDCLOCKPROCESSCPUTIMEID, :hertz) is CLOCKPERSEC.
I wanted to access them easily to investigate emulated clock behaviors on
various OSes.

Those are some long symbols! Are these intended only for experimental/investigative use?

I choose the long symbols that is longer than Process::CLOCKPROCESSCPUTIMEID.
Basically users should use Process::CLOCK
PROCESSCPUTIMEID if no reason.

Sounds good.

Dave

#4 Updated by Akira Tanaka 8 months ago

david_macmahon (David MacMahon) wrote:

I know it's not SI, but I often use ASCII "u" for Greek m ("µ"), so :microsecond would be aliased by :us.

It may be possible.

I found ISO 2955.
ISO 2955: Information processing - Representation units in Systems with limited Character sets

I feel :float_s is bit curious.

How about separating the type and the resolution into two different parameters?

 Process.clock_getres(Process::CLOCK_MONOTONIC, :float, :second)

...or...

 Process.clock_getres(Process::CLOCK_MONOTONIC, Float, :second)

I think most useful combinations are follows.

  • float second
  • integer nanosecond (clockgettime/clockgetres native format)

The current design makes us possible to specify
former as no unit argument and
later as :nanosecond.

Your design force us longer description for integer nanosecond.

#5 Updated by Akira Tanaka 8 months ago

I updated the patch.

#6 Updated by Akira Tanaka 8 months ago

I updated the patch again.

#7 Updated by Akira Tanaka 8 months ago

  • Status changed from Open to Closed
  • % Done changed from 0 to 100

This issue was solved with changeset r42744.
Akira, thank you for reporting this issue.
Your contribution to Ruby is greatly appreciated.
May Ruby be with you.


  • process.c (rbclockgetres): New method.
    (timetick2dblnum_reciprocal): New function.

  • configure.in: Check clock_getres.

[Feature #8809] accepted at
DevelopersMeeting20130831Japan
https://bugs.ruby-lang.org/projects/ruby/wiki/DevelopersMeeting20130831Japan

Also available in: Atom PDF