Patchwork A steadier steady_clock

login
register
mail settings
Submitter Sam Varshavchik
Date Oct. 21, 2012, 2 p.m.
Message ID <cone.1350828020.51066.30326.1000@monster.email-scan.com>
Download mbox | patch
Permalink /patch/192996/
State New
Headers show

Comments

Sam Varshavchik - Oct. 21, 2012, 2 p.m.
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:
Paolo Carlini - Oct. 21, 2012, 4:11 p.m.
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.
* 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.
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.
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.

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)));
     }