diff mbox

A steadier steady_clock

Message ID cone.1350828020.51066.30326.1000@monster.email-scan.com
State New
Headers show

Commit Message

Sam Varshavchik Oct. 21, 2012, 2 p.m. UTC
Based on a casual browsing of clock_gettime(3), CLOCK_MONOTONIC_RAW seems to  
be a better fit for std::chrono::steady_clock's requirements as given in  
20.11.7.2, with recent Linux kernels,

Something like this:

Comments

Paolo Carlini Oct. 21, 2012, 4:11 p.m. UTC | #1
On 10/21/2012 04:00 PM, Sam Varshavchik wrote:
> Based on a casual browsing of clock_gettime(3), CLOCK_MONOTONIC_RAW 
> seems to be a better fit for std::chrono::steady_clock's requirements 
> as given in 20.11.7.2, with recent Linux kernels,
>
> Something like this:
Please always CC library patches to libstdc++@gcc.gnu.org.
>
>
> Index: libstdc++-v3/src/c++11/chrono.cc
> ===================================================================
> --- libstdc++-v3/src/c++11/chrono.cc    (revision 192652)
> +++ libstdc++-v3/src/c++11/chrono.cc    (working copy)
> @@ -70,7 +70,11 @@
>     {
>       timespec tp;
>       // -EINVAL, -EFAULT
> +#ifdef CLOCK_MONOTONIC_RAW
> +      clock_gettime(CLOCK_MONOTONIC_RAW, &tp);
> +#else
>       clock_gettime(CLOCK_MONOTONIC, &tp);
> +#endif
>       return time_point(duration(chrono::seconds(tp.tv_sec)
>                  + chrono::nanoseconds(tp.tv_nsec)));
>     }
>
Florian Weimer Oct. 21, 2012, 9:07 p.m. UTC | #2
* Sam Varshavchik:

> Based on a casual browsing of clock_gettime(3), CLOCK_MONOTONIC_RAW
> seems to be a better fit for std::chrono::steady_clock's requirements
> as given in  20.11.7.2, with recent Linux kernels,

Are the Linux clock semantics documented somewhere in detail?

> +#ifdef CLOCK_MONOTONIC_RAW
> +      clock_gettime(CLOCK_MONOTONIC_RAW, &tp);
> +#else
>       clock_gettime(CLOCK_MONOTONIC, &tp);
> +#endif

If a #define is available at compile time, it's not necessarily the
case that the feature is present at run time.  If CLOCK_MONOTONIC_RAW
is indeed what we want, we need fallback code.
Ulrich Drepper Oct. 21, 2012, 11:33 p.m. UTC | #3
On Sun, Oct 21, 2012 at 12:11 PM, Paolo Carlini
<paolo.carlini@oracle.com> wrote:
\>> @@ -70,7 +70,11 @@
>>     {
>>       timespec tp;
>>       // -EINVAL, -EFAULT
>> +#ifdef CLOCK_MONOTONIC_RAW
>> +      clock_gettime(CLOCK_MONOTONIC_RAW, &tp);
>> +#else
>>       clock_gettime(CLOCK_MONOTONIC, &tp);
>> +#endif
>>       return time_point(duration(chrono::seconds(tp.tv_sec)
>>                  + chrono::nanoseconds(tp.tv_nsec)));
>>     }

That'll have to be something like

#ifdef CLOCK_MONOTONIC_RAW
  if (clock_gettime(CLOCK_MONOTONIC_RAW, &tp) != 0)
#endif
    clock_gettime(CLOCK_MONOTONIC, &tp);

Only way out of this is when you introduce a check for a minimum
kernel ABI somewhere, just like glibc does.
Sam Varshavchik Oct. 22, 2012, 1:54 a.m. UTC | #4
Florian Weimer writes:

> * Sam Varshavchik:
>
> > Based on a casual browsing of clock_gettime(3), CLOCK_MONOTONIC_RAW
> > seems to be a better fit for std::chrono::steady_clock's requirements
> > as given in  20.11.7.2, with recent Linux kernels,
>
> Are the Linux clock semantics documented somewhere in detail?

clock_gettime(3) says:

# CLOCK_MONOTONIC_RAW (since Linux 2.6.28; Linux-specific)
# Similar  to  CLOCK_MONOTONIC, but provides access to a raw hard‐
# ware-based time that is not subject to NTP adjustments.

That looks like a better fit for what 20.11.7.2 says steady_clock should be.
diff mbox

Patch

Index: libstdc++-v3/src/c++11/chrono.cc
===================================================================
--- libstdc++-v3/src/c++11/chrono.cc	(revision 192652)
+++ libstdc++-v3/src/c++11/chrono.cc	(working copy)
@@ -70,7 +70,11 @@ 
     {
       timespec tp;
       // -EINVAL, -EFAULT
+#ifdef CLOCK_MONOTONIC_RAW
+      clock_gettime(CLOCK_MONOTONIC_RAW, &tp);
+#else
       clock_gettime(CLOCK_MONOTONIC, &tp);
+#endif
       return time_point(duration(chrono::seconds(tp.tv_sec)
 				 + chrono::nanoseconds(tp.tv_nsec)));
     }