Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(610)

Unified Diff: runtime/bin/utils_macos.cc

Issue 1519563003: Use a monotonic clock in the implementation of Timer. (Closed) Base URL: git@github.com:dart-lang/sdk.git@master
Patch Set: Created 5 years ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « runtime/bin/utils_linux.cc ('k') | runtime/bin/utils_win.cc » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: runtime/bin/utils_macos.cc
diff --git a/runtime/bin/utils_macos.cc b/runtime/bin/utils_macos.cc
index 1d792c9c7fae7390224418be2310402411915e24..80cd0386ed25114eeccbf48b5bdc4eb039a43291 100644
--- a/runtime/bin/utils_macos.cc
+++ b/runtime/bin/utils_macos.cc
@@ -7,6 +7,9 @@
#include <errno.h> // NOLINT
#include <netdb.h> // NOLINT
+#include <mach/mach.h> // NOLINT
+#include <mach/clock.h> // NOLINT
+#include <mach/mach_time.h> // NOLINT
#include <sys/time.h> // NOLINT
#include <time.h> // NOLINT
@@ -71,17 +74,44 @@ bool ShellUtils::GetUtf8Argv(int argc, char** argv) {
return false;
}
-int64_t TimerUtils::GetCurrentTimeMilliseconds() {
- return GetCurrentTimeMicros() / 1000;
+int64_t TimerUtils::GetCurrentMonotonicMillis() {
+ return GetCurrentMonotonicMicros() / 1000;
}
-int64_t TimerUtils::GetCurrentTimeMicros() {
- struct timeval tv;
- if (gettimeofday(&tv, NULL) < 0) {
- UNREACHABLE();
- return 0;
+int64_t TimerUtils::GetCurrentMonotonicMicros() {
+#if TARGET_OS_IOS
+ // On iOS mach_absolute_time stops while the device is sleeping. Instead use
+ // now - KERN_BOOTTIME to get a time difference that is not impacted by clock
+ // changes. KERN_BOOTTIME will be updated by the system whenever the system
+ // clock change.
+ struct timeval boottime;
+ int mib[2] = {CTL_KERN, KERN_BOOTTIME};
+ size_t size = sizeof(boottime);
+ int kr = sysctl(mib, sizeof(mib) / sizeof(mib[0]), &boottime, &size, NULL, 0);
+ ASSERT(KERN_SUCCESS == kr);
+ int64_t now = GetCurrentTimeMicros();
+ int64_t origin = boottime.tv_sec * kMicrosecondsPerSecond;
+ origin += boottime.tv_usec;
+ return now - origin;
+#else
+ static mach_timebase_info_data_t timebase_info;
+ if (timebase_info.denom == 0) {
+ // Zero-initialization of statics guarantees that denom will be 0 before
+ // calling mach_timebase_info. mach_timebase_info will never set denom to
+ // 0 as that would be invalid, so the zero-check can be used to determine
+ // whether mach_timebase_info has already been called. This is
+ // recommended by Apple's QA1398.
+ kern_return_t kr = mach_timebase_info(&timebase_info);
+ ASSERT(KERN_SUCCESS == kr);
}
- return (static_cast<int64_t>(tv.tv_sec) * 1000000) + tv.tv_usec;
+
+ // timebase_info converts absolute time tick units into nanoseconds. Convert
+ // to microseconds.
+ int64_t result = mach_absolute_time() / kNanosecondsPerMicrosecond;
+ result *= timebase_info.numer;
+ result /= timebase_info.denom;
+ return result;
+#endif // TARGET_OS_IOS
}
void TimerUtils::Sleep(int64_t millis) {
« no previous file with comments | « runtime/bin/utils_linux.cc ('k') | runtime/bin/utils_win.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698