[PATCH] D27429: [Chrono][Darwin] On Darwin use CLOCK_UPTIME_RAW instead of CLOCK_MONOTONIC

2017-04-13 Thread Bruno Cardoso Lopes via Phabricator via cfe-commits
bruno closed this revision.
bruno added a comment.

r291466 & r291517


https://reviews.llvm.org/D27429



___
cfe-commits mailing list
cfe-commits@lists.llvm.org
http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[PATCH] D27429: [Chrono][Darwin] On Darwin use CLOCK_UPTIME_RAW instead of CLOCK_MONOTONIC

2017-01-06 Thread Eric Fiselier via Phabricator via cfe-commits
EricWF accepted this revision.
EricWF added a comment.
This revision is now accepted and ready to land.

In https://reviews.llvm.org/D27429#637507, @bruno wrote:

> @EricWF ok to commit?


Yes sorry This LGTM.  (I thought I had approved this yesterday, but I forgot to 
hit enter).




Comment at: src/chrono.cpp:218
+#error "Never use CLOCK_MONOTONIC for steady_clock::now on Apple platforms"
+#endif
+

howard.hinnant wrote:
> Nice, thanks!
This code is currently dead due to the structure of the `#ifdef`s. However I 
think it's great documentation and great future proofing!


https://reviews.llvm.org/D27429



___
cfe-commits mailing list
cfe-commits@lists.llvm.org
http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[PATCH] D27429: [Chrono][Darwin] On Darwin use CLOCK_UPTIME_RAW instead of CLOCK_MONOTONIC

2017-01-05 Thread Bruno Cardoso Lopes via Phabricator via cfe-commits
bruno added a comment.

@EricWF ok to commit?


https://reviews.llvm.org/D27429



___
cfe-commits mailing list
cfe-commits@lists.llvm.org
http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[PATCH] D27429: [Chrono][Darwin] On Darwin use CLOCK_UPTIME_RAW instead of CLOCK_MONOTONIC

2017-01-04 Thread Howard Hinnant via Phabricator via cfe-commits
howard.hinnant added inline comments.



Comment at: src/chrono.cpp:218
+#error "Never use CLOCK_MONOTONIC for steady_clock::now on Apple platforms"
+#endif
+

Nice, thanks!


https://reviews.llvm.org/D27429



___
cfe-commits mailing list
cfe-commits@lists.llvm.org
http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[PATCH] D27429: [Chrono][Darwin] On Darwin use CLOCK_UPTIME_RAW instead of CLOCK_MONOTONIC

2017-01-04 Thread Bruno Cardoso Lopes via Phabricator via cfe-commits
bruno updated this revision to Diff 83158.
bruno added a comment.

Thanks for the reviews. @howard.hinnant thanks for the great explanation & 
examples.

Attached a new version of the patch, addressing all suggestions. The logic 
became a bit simpler after Saleem's r290804, which already moved the 
CLOCK_MONOTONIC to be the last condition.

On top of that, I also conditionalized the presence of `clock_gettime`, which 
isn't available before 10.12 on macosx and specific versions on other apple 
platforms. Check that by looking at the deployment target.


https://reviews.llvm.org/D27429

Files:
  src/chrono.cpp


Index: src/chrono.cpp
===
--- src/chrono.cpp
+++ src/chrono.cpp
@@ -12,6 +12,28 @@
 #include "system_error"  // __throw_system_error
 #include // clock_gettime, CLOCK_MONOTONIC and CLOCK_REALTIME
 
+#if (__APPLE__)
+#if defined(__ENVIRONMENT_MAC_OS_X_VERSION_MIN_REQUIRED__)
+#if __ENVIRONMENT_MAC_OS_X_VERSION_MIN_REQUIRED__ >= 101200
+#define _LIBCXX_USE_CLOCK_GETTIME
+#endif
+#elif defined(__ENVIRONMENT_IPHONE_OS_VERSION_MIN_REQUIRED__)
+#if __ENVIRONMENT_IPHONE_OS_VERSION_MIN_REQUIRED__ >= 10
+#define _LIBCXX_USE_CLOCK_GETTIME
+#endif
+#elif defined(__ENVIRONMENT_TV_OS_VERSION_MIN_REQUIRED__)
+#if __ENVIRONMENT_TV_OS_VERSION_MIN_REQUIRED__ >= 10
+#define _LIBCXX_USE_CLOCK_GETTIME
+#endif
+#elif defined(__ENVIRONMENT_WATCH_OS_VERSION_MIN_REQUIRED__)
+#if __ENVIRONMENT_WATCH_OS_VERSION_MIN_REQUIRED__ >= 3
+#define _LIBCXX_USE_CLOCK_GETTIME
+#endif
+#endif // __ENVIRONMENT_.*_VERSION_MIN_REQUIRED__
+#else
+#define _LIBCXX_USE_CLOCK_GETTIME
+#endif // __APPLE__
+
 #if defined(_LIBCPP_WIN32API)
 #define WIN32_LEAN_AND_MEAN
 #define VC_EXTRA_LEAN
@@ -70,16 +92,16 @@
static_cast<__int64>(ft.dwLowDateTime)};
   return time_point(duration_cast(d - nt_to_unix_epoch));
 #else
-#ifdef CLOCK_REALTIME
+#if defined(_LIBCXX_USE_CLOCK_GETTIME) && defined(CLOCK_REALTIME)
 struct timespec tp;
 if (0 != clock_gettime(CLOCK_REALTIME, ))
 __throw_system_error(errno, "clock_gettime(CLOCK_REALTIME) failed");
 return time_point(seconds(tp.tv_sec) + microseconds(tp.tv_nsec / 1000));
-#else  // !CLOCK_REALTIME
+#else
 timeval tv;
 gettimeofday(, 0);
 return time_point(seconds(tv.tv_sec) + microseconds(tv.tv_usec));
-#endif  // CLOCK_REALTIME
+#endif // _LIBCXX_USE_CLOCK_GETTIME && CLOCK_REALTIME
 #endif
 }
 
@@ -106,6 +128,18 @@
 
 #if defined(__APPLE__)
 
+// Darwin libc versions >= 1133 provide ns precision via CLOCK_UPTIME_RAW
+#if defined(_LIBCXX_USE_CLOCK_GETTIME) && defined(CLOCK_UPTIME_RAW)
+steady_clock::time_point
+steady_clock::now() _NOEXCEPT
+{
+struct timespec tp;
+if (0 != clock_gettime(CLOCK_UPTIME_RAW, ))
+__throw_system_error(errno, "clock_gettime(CLOCK_UPTIME_RAW) failed");
+return time_point(seconds(tp.tv_sec) + nanoseconds(tp.tv_nsec));
+}
+
+#else
 //   mach_absolute_time() * MachInfo.numer / MachInfo.denom is the number of
 //   nanoseconds since the computer booted up.  MachInfo.numer and 
MachInfo.denom
 //   are run time constants supplied by the OS.  This clock has no relationship
@@ -157,6 +191,7 @@
 static FP fp = init_steady_clock();
 return time_point(duration(fp()));
 }
+#endif // defined(_LIBCXX_USE_CLOCK_GETTIME) && defined(CLOCK_UPTIME_RAW)
 
 #elif defined(_LIBCPP_WIN32API)
 
@@ -175,6 +210,13 @@
 
 #elif defined(CLOCK_MONOTONIC)
 
+// On Apple platforms only CLOCK_UPTIME_RAW or mach_absolute_time are able to
+// time functions in the nanosecond range. Thus, they are the only acceptable
+// implementations of steady_clock.
+#ifdef __APPLE__
+#error "Never use CLOCK_MONOTONIC for steady_clock::now on Apple platforms"
+#endif
+
 steady_clock::time_point
 steady_clock::now() _NOEXCEPT
 {


Index: src/chrono.cpp
===
--- src/chrono.cpp
+++ src/chrono.cpp
@@ -12,6 +12,28 @@
 #include "system_error"  // __throw_system_error
 #include // clock_gettime, CLOCK_MONOTONIC and CLOCK_REALTIME
 
+#if (__APPLE__)
+#if defined(__ENVIRONMENT_MAC_OS_X_VERSION_MIN_REQUIRED__)
+#if __ENVIRONMENT_MAC_OS_X_VERSION_MIN_REQUIRED__ >= 101200
+#define _LIBCXX_USE_CLOCK_GETTIME
+#endif
+#elif defined(__ENVIRONMENT_IPHONE_OS_VERSION_MIN_REQUIRED__)
+#if __ENVIRONMENT_IPHONE_OS_VERSION_MIN_REQUIRED__ >= 10
+#define _LIBCXX_USE_CLOCK_GETTIME
+#endif
+#elif defined(__ENVIRONMENT_TV_OS_VERSION_MIN_REQUIRED__)
+#if __ENVIRONMENT_TV_OS_VERSION_MIN_REQUIRED__ >= 10
+#define _LIBCXX_USE_CLOCK_GETTIME
+#endif
+#elif defined(__ENVIRONMENT_WATCH_OS_VERSION_MIN_REQUIRED__)
+#if __ENVIRONMENT_WATCH_OS_VERSION_MIN_REQUIRED__ >= 3
+#define _LIBCXX_USE_CLOCK_GETTIME
+#endif
+#endif // __ENVIRONMENT_.*_VERSION_MIN_REQUIRED__
+#else
+#define _LIBCXX_USE_CLOCK_GETTIME
+#endif // __APPLE__
+
 #if defined(_LIBCPP_WIN32API)
 #define WIN32_LEAN_AND_MEAN
 #define VC_EXTRA_LEAN
@@ -70,16 +92,16 

[PATCH] D27429: [Chrono][Darwin] On Darwin use CLOCK_UPTIME_RAW instead of CLOCK_MONOTONIC

2016-12-23 Thread Eric Fiselier via Phabricator via cfe-commits
EricWF requested changes to this revision.
EricWF added a comment.
This revision now requires changes to proceed.

Marking as changes requested.

Please make sure we fall back to the old implementation on Apple platforms when 
`CLOCK_UPTIME_RAW` isn't available.


https://reviews.llvm.org/D27429



___
cfe-commits mailing list
cfe-commits@lists.llvm.org
http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[PATCH] D27429: [Chrono][Darwin] On Darwin use CLOCK_UPTIME_RAW instead of CLOCK_MONOTONIC

2016-12-11 Thread Howard Hinnant via Phabricator via cfe-commits
howard.hinnant added a comment.

Thanks Eric.

Fwiw `CLOCK_MONOTONIC` won't meet the requirements of the standard either.  The 
standard appears to require `steady_clock` to be a perfect clock and there is 
no such thing. The wording used to only require monotonic, but the committee 
got a little too enthusiastic.   :-)


https://reviews.llvm.org/D27429



___
cfe-commits mailing list
cfe-commits@lists.llvm.org
http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[PATCH] D27429: [Chrono][Darwin] On Darwin use CLOCK_UPTIME_RAW instead of CLOCK_MONOTONIC

2016-12-11 Thread Eric Fiselier via Phabricator via cfe-commits
EricWF added a comment.

Howard Thank you for your excellent analysis. Although I still don't think that 
`CLOCK_UPTIME_RAW` meets the requirements of `steady_clock` but I would rather 
relax the standard here than provide a poor implementation.

In https://reviews.llvm.org/D27429#618998, @howard.hinnant wrote:

> I feel strongly enough about this that I would like to see a static_assert 
> that the CLOCK_MONOTONIC is never accidentally chosen by the preprocessor 
> when targeting macOS, or iOS.


I agree. This patch LGTM after ensuring that CLOCK_MONOTONIC is never selected 
on Apple platforms.


https://reviews.llvm.org/D27429



___
cfe-commits mailing list
cfe-commits@lists.llvm.org
http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[PATCH] D27429: [Chrono][Darwin] On Darwin use CLOCK_UPTIME_RAW instead of CLOCK_MONOTONIC

2016-12-11 Thread Howard Hinnant via Phabricator via cfe-commits
howard.hinnant added a comment.

One more comment:  `steady_clock::now()` is not allowed to throw an exception 
because it shall satisfy the requirements of `TrivialClock` ([time.clock]/p1).  
And [time.clock.req]/p4/b4 says that a `TrivialClock::now()` does not throw 
exceptions.


https://reviews.llvm.org/D27429



___
cfe-commits mailing list
cfe-commits@lists.llvm.org
http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[PATCH] D27429: [Chrono][Darwin] On Darwin use CLOCK_UPTIME_RAW instead of CLOCK_MONOTONIC

2016-12-09 Thread Howard Hinnant via Phabricator via cfe-commits
howard.hinnant added a comment.

I would like to offer two thoughts:

1. Timing with `steady_clock` is often used to time very short events.  It 
should be respected as the "high_resolution_clock".  This means that two 
consecutive calls to `steady_clock::now()` should return nanoseconds resolution 
and should not be equal unless the implementation is actually able to complete 
a call to `steady_clock::now()` in less than 1ns.

2. Here's test of thought 1:



  #include 
  #include 
  #include 
  #include 
  
  std::chrono::milliseconds
  uptime()
  {
  using namespace std::chrono;
  timeval ts;
  auto ts_len = sizeof(ts);
  int mib[2] = { CTL_KERN, KERN_BOOTTIME };
  auto constexpr mib_len = sizeof(mib)/sizeof(mib[0]);
  if (sysctl(mib, mib_len, , _len, nullptr, 0) == 0)
  {
  system_clock::time_point boot{seconds{ts.tv_sec} + 
microseconds{ts.tv_usec}};
  return duration_cast(system_clock::now() - boot);
  }
  return 0ms;
  }
  
  std::chrono::nanoseconds
  get_uptime_raw()
  {
  using namespace std::chrono;
  struct timespec tp;
  clock_gettime(CLOCK_UPTIME_RAW, );
  return seconds(tp.tv_sec) + nanoseconds(tp.tv_nsec);
  }
  
  std::chrono::nanoseconds
  get_monotonic()
  {
  using namespace std::chrono;
  struct timespec tp;
  clock_gettime(CLOCK_MONOTONIC, );
  return seconds(tp.tv_sec) + nanoseconds(tp.tv_nsec);
  }
  
  #include "date.h"
  #include 
  
  template 
  void
  display(Duration time)
  {
  using namespace date;
  auto d = floor(time);
  time -= d;
  std::cout << d.count() << " days " << make_time(time) << '\n';
  }
  
  int
  main()
  {
  using namespace std::chrono;
  for (int i = 0; i < 4; ++i)
  {
  std::cout << i << '\n';
  {
  auto t0 = uptime();
  auto t1 = uptime();
  std::cout << "boot time : ";  display(t0);
  std::cout << "boot time : ";  display(t1);
  std::cout << "delta boot time   : " << nanoseconds{t1 
- t0}.count() << "ns\n";
  }
  {
  auto t0 = get_uptime_raw();
  auto t1 = get_uptime_raw();
  std::cout << "CLOCK_UPTIME_RAW  : "; display(t0);
  std::cout << "CLOCK_UPTIME_RAW  : "; display(t1);
  std::cout << "delta CLOCK_UPTIME_RAW time   : " << nanoseconds{t1 
- t0}.count() << "ns\n";
  }
  {
  auto t0 = get_monotonic();
  auto t1 = get_monotonic();
  std::cout << "CLOCK_MONOTONIC   : "; display(t0);
  std::cout << "CLOCK_MONOTONIC   : "; display(t1);
  std::cout << "delta CLOCK_MONOTONIC time: " << nanoseconds{t1 
- t0}.count() << "ns\n";
  }
  {
  auto t0 = std::chrono::steady_clock::now().time_since_epoch();
  auto t1 = std::chrono::steady_clock::now().time_since_epoch();
  std::cout << "mach_absolute_time: "; display(t0);
  std::cout << "mach_absolute_time: "; display(t1);
  std::cout << "delta mach_absolute_time time : " << nanoseconds{t1 
- t0}.count() << "ns\n";
  }
  std::cout << '\n';
  }
  }

Sorry, it requires "date.h" from https://github.com/HowardHinnant/date .  It is 
header-only and portable.  It's just used for matting purposes if it really 
stresses you out.

For me this outputs (at -O3):

  Jade:~/Development/cljunk> a.out
  0
  boot time : 11 days 22:30:42.827
  boot time : 11 days 22:30:42.827
  delta boot time   : 0ns
  CLOCK_UPTIME_RAW  : 11 days 22:22:28.960672112
  CLOCK_UPTIME_RAW  : 11 days 22:22:28.960672266
  delta CLOCK_UPTIME_RAW time   : 154ns
  CLOCK_MONOTONIC   : 11 days 22:30:42.827318000
  CLOCK_MONOTONIC   : 11 days 22:30:42.827318000
  delta CLOCK_MONOTONIC time: 0ns
  mach_absolute_time: 11 days 22:22:28.960714394
  mach_absolute_time: 11 days 22:22:28.960714504
  delta mach_absolute_time time : 110ns
  
  1
  boot time : 11 days 22:30:42.827
  boot time : 11 days 22:30:42.827
  delta boot time   : 0ns
  CLOCK_UPTIME_RAW  : 11 days 22:22:28.960761867
  CLOCK_UPTIME_RAW  : 11 days 22:22:28.960761932
  delta CLOCK_UPTIME_RAW time   : 65ns
  CLOCK_MONOTONIC   : 11 days 22:30:42.827402000
  CLOCK_MONOTONIC   : 11 days 22:30:42.827402000
  delta CLOCK_MONOTONIC time: 0ns
  mach_absolute_time: 11 days 22:22:28.960793667
  mach_absolute_time: 11 days 22:22:28.960793747
  delta mach_absolute_time time : 80ns
  
  2
  boot time : 11 days 22:30:42.827
  boot time : 11 days 22:30:42.827
  delta boot 

Re: [PATCH] D27429: [Chrono][Darwin] On Darwin use CLOCK_UPTIME_RAW instead of CLOCK_MONOTONIC

2016-12-09 Thread Duncan P. N. Exon Smith via cfe-commits
Two points to make here:

1. CLOCK_UPTIME_RAW exactly matches the previous behaviour, so even if it's 
wrong, it's not a regression.  See the next sentence from the one Eric quoted:

 CLOCK_UPTIME_RAW   clock that increments monotonically, in the same manner 
as
CLOCK_MONOTONIC_RAW, but that does not increment while 
the
system is asleep.  The returned value is identical to 
the
result of mach_absolute_time() after the appropriate
mach_timebase conversion is applied.

The previous implementation used mach_absolute_time() with "the appropriate 
mach_timebase conversion".  Using CLOCK_UPTIME_RAW is maintaining the status 
quo.

Moreover, libc++ is currently incorrectly/accidentally/incidentally using 
CLOCK_MONOTONIC, which has frequency and time adjustments, and is definitely 
un-steady.  I think this patch should be committed promptly to maintain the 
status quo and match what's shipping (and has shipped for 5 years) on Darwin.

Then we can argue about whether to change how steady_clock works going forward 
(maybe we should).

2. I find your argument unconvincing.  Why must the clock continue to increment 
when the system is asleep?  The text only says that it advances relative to 
real time, not that it advances when the system isn't running.

> On 2016-Dec-09, at 09:15, Eric Fiselier via Phabricator 
>  wrote:
> 
> EricWF added a comment.
> 
> `CLOCK_MONOTONIC_RAW` however seems to meet the requirements..
> 
> 
> https://reviews.llvm.org/D27429
> 
> 
> 

___
cfe-commits mailing list
cfe-commits@lists.llvm.org
http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[PATCH] D27429: [Chrono][Darwin] On Darwin use CLOCK_UPTIME_RAW instead of CLOCK_MONOTONIC

2016-12-09 Thread Eric Fiselier via Phabricator via cfe-commits
EricWF added a comment.

`CLOCK_MONOTONIC_RAW` however seems to meet the requirements..


https://reviews.llvm.org/D27429



___
cfe-commits mailing list
cfe-commits@lists.llvm.org
http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[PATCH] D27429: [Chrono][Darwin] On Darwin use CLOCK_UPTIME_RAW instead of CLOCK_MONOTONIC

2016-12-09 Thread Marshall Clow via Phabricator via cfe-commits
mclow.lists added a comment.

I agree with @EricWF .   If `CLOCK_UPTIME_RAW` doesn't meet the requirements of 
`steady_clock`  (i.e, monotonically increasing, and advances in real time), 
then we can't use it.


https://reviews.llvm.org/D27429



___
cfe-commits mailing list
cfe-commits@lists.llvm.org
http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[PATCH] D27429: [Chrono][Darwin] On Darwin use CLOCK_UPTIME_RAW instead of CLOCK_MONOTONIC

2016-12-07 Thread Eric Fiselier via Phabricator via cfe-commits
EricWF added a comment.

I'm not sure `CLOCK_UPTIME_RAW` meets the requirements of `steady_clock`. The 
manpage for `clock_gettime` on OS X specifies `CLOCK_UPTIME_RAW`  as:

> CLOCK_UPTIME_RAW   clock that increments monotonically, in the same manner as 
> CLOCK_MONOTONIC_RAW, but that does not increment while the system is asleep.

And C++17 [time.clock.steady]p1 says:

> Objects of class steady_clock represent clocks for which values of time_point 
> never decrease as physical time advances and for which values of time_point 
> advance at a steady rate relative to real time. That is, the clock may not be 
> adjusted.

So if `CLOCK_UPTIME_RAW` doesn't update while the system is asleep does it 
advance at a steady rate relative to real time? I'm not convinced it does.

@mclow.lists Do you agree with this interpretation?


https://reviews.llvm.org/D27429



___
cfe-commits mailing list
cfe-commits@lists.llvm.org
http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


Re: [PATCH] D27429: [Chrono][Darwin] On Darwin use CLOCK_UPTIME_RAW instead of CLOCK_MONOTONIC

2016-12-05 Thread Duncan P. N. Exon Smith via cfe-commits
From a Darwin perspective, this LGTM (it's what we're shipping on macOS 10.12 
and iOS 10).

Eric, do you have a good idea for how to test this?

> On 2016-Dec-05, at 14:36, Bruno Cardoso Lopes via Phabricator 
>  wrote:
> 
> bruno created this revision.
> bruno added reviewers: mclow.lists, dexonsmith, EricWF.
> bruno added a subscriber: cfe-commits.
> 
> CLOCK_MONOTONIC is only defined on Darwin on libc versions >= 1133 and its 
> behaviour differs from Linux. CLOCK_UPTIME on Darwin actually matches
> CLOCK_MONOTONIC on Linux, due to historical coincidence (Linux doesn't match 
> POSIX here but Darwin does). Use CLOCK_UPTIME_RAW on Darwin since the _RAW 
> version gives nanosecond precision and is lower overhead.
> 
> 
> https://reviews.llvm.org/D27429
> 
> Files:
>  src/chrono.cpp
> 
> 
> Index: src/chrono.cpp
> ===
> --- src/chrono.cpp
> +++ src/chrono.cpp
> @@ -75,8 +75,19 @@
> steady_clock::now() _NOEXCEPT
> {
> struct timespec tp;
> +#if defined(__APPLE__) && defined(CLOCK_UPTIME_RAW)
> +// CLOCK_MONOTONIC is only defined on Darwin on libc versions >= 1133 and
> +// its behaviour differs from Linux.
> +// CLOCK_UPTIME on Darwin actually matches CLOCK_MONOTONIC on Linux, due 
> to
> +// historical coincidence (Linux doesn't match POSIX here but Darwin 
> does).
> +// Use CLOCK_UPTIME_RAW on Darwin since the _RAW version gives nanosecond
> +// precision and is lower overhead.
> +if (0 != clock_gettime(CLOCK_UPTIME_RAW, ))
> +__throw_system_error(errno, "clock_gettime(CLOCK_UPTIME_RAW) 
> failed");
> +#else
> if (0 != clock_gettime(CLOCK_MONOTONIC, ))
> __throw_system_error(errno, "clock_gettime(CLOCK_MONOTONIC) failed");
> +#endif
> return time_point(seconds(tp.tv_sec) + nanoseconds(tp.tv_nsec));
> }
> 
> 
> 
> 

___
cfe-commits mailing list
cfe-commits@lists.llvm.org
http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[PATCH] D27429: [Chrono][Darwin] On Darwin use CLOCK_UPTIME_RAW instead of CLOCK_MONOTONIC

2016-12-05 Thread Bruno Cardoso Lopes via Phabricator via cfe-commits
bruno created this revision.
bruno added reviewers: mclow.lists, dexonsmith, EricWF.
bruno added a subscriber: cfe-commits.

CLOCK_MONOTONIC is only defined on Darwin on libc versions >= 1133 and its 
behaviour differs from Linux. CLOCK_UPTIME on Darwin actually matches
CLOCK_MONOTONIC on Linux, due to historical coincidence (Linux doesn't match 
POSIX here but Darwin does). Use CLOCK_UPTIME_RAW on Darwin since the _RAW 
version gives nanosecond precision and is lower overhead.


https://reviews.llvm.org/D27429

Files:
  src/chrono.cpp


Index: src/chrono.cpp
===
--- src/chrono.cpp
+++ src/chrono.cpp
@@ -75,8 +75,19 @@
 steady_clock::now() _NOEXCEPT
 {
 struct timespec tp;
+#if defined(__APPLE__) && defined(CLOCK_UPTIME_RAW)
+// CLOCK_MONOTONIC is only defined on Darwin on libc versions >= 1133 and
+// its behaviour differs from Linux.
+// CLOCK_UPTIME on Darwin actually matches CLOCK_MONOTONIC on Linux, due to
+// historical coincidence (Linux doesn't match POSIX here but Darwin does).
+// Use CLOCK_UPTIME_RAW on Darwin since the _RAW version gives nanosecond
+// precision and is lower overhead.
+if (0 != clock_gettime(CLOCK_UPTIME_RAW, ))
+__throw_system_error(errno, "clock_gettime(CLOCK_UPTIME_RAW) failed");
+#else
 if (0 != clock_gettime(CLOCK_MONOTONIC, ))
 __throw_system_error(errno, "clock_gettime(CLOCK_MONOTONIC) failed");
+#endif
 return time_point(seconds(tp.tv_sec) + nanoseconds(tp.tv_nsec));
 }
 


Index: src/chrono.cpp
===
--- src/chrono.cpp
+++ src/chrono.cpp
@@ -75,8 +75,19 @@
 steady_clock::now() _NOEXCEPT
 {
 struct timespec tp;
+#if defined(__APPLE__) && defined(CLOCK_UPTIME_RAW)
+// CLOCK_MONOTONIC is only defined on Darwin on libc versions >= 1133 and
+// its behaviour differs from Linux.
+// CLOCK_UPTIME on Darwin actually matches CLOCK_MONOTONIC on Linux, due to
+// historical coincidence (Linux doesn't match POSIX here but Darwin does).
+// Use CLOCK_UPTIME_RAW on Darwin since the _RAW version gives nanosecond
+// precision and is lower overhead.
+if (0 != clock_gettime(CLOCK_UPTIME_RAW, ))
+__throw_system_error(errno, "clock_gettime(CLOCK_UPTIME_RAW) failed");
+#else
 if (0 != clock_gettime(CLOCK_MONOTONIC, ))
 __throw_system_error(errno, "clock_gettime(CLOCK_MONOTONIC) failed");
+#endif
 return time_point(seconds(tp.tv_sec) + nanoseconds(tp.tv_nsec));
 }
 
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits