diff options
author | qsr@chromium.org <qsr@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2012-12-13 10:18:58 +0000 |
---|---|---|
committer | qsr@chromium.org <qsr@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2012-12-13 10:18:58 +0000 |
commit | f98fb5e07205c3a705f09efb0034b3bb6f612c02 (patch) | |
tree | f0272af2c4251997b57dccb7630d0d30bb0e1625 /base/time_mac.cc | |
parent | 09e1834b527ba7a57792d7c2694e28f3381a8928 (diff) | |
download | chromium_src-f98fb5e07205c3a705f09efb0034b3bb6f612c02.zip chromium_src-f98fb5e07205c3a705f09efb0034b3bb6f612c02.tar.gz chromium_src-f98fb5e07205c3a705f09efb0034b3bb6f612c02.tar.bz2 |
Improve TimeTicks on iOS.
TimeTicks stops ticking on iOS when the device is sleeping. This patch compute timeticks by computing the difference between the current time and the boot time, as the system update boot time when it updates the clock.
R=stuartmorgan@chromium.org
BUG=None
Review URL: https://chromiumcodereview.appspot.com/11529011
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@172851 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'base/time_mac.cc')
-rw-r--r-- | base/time_mac.cc | 80 |
1 files changed, 53 insertions, 27 deletions
diff --git a/base/time_mac.cc b/base/time_mac.cc index ca10bc8..883a35b 100644 --- a/base/time_mac.cc +++ b/base/time_mac.cc @@ -7,13 +7,65 @@ #include <CoreFoundation/CFDate.h> #include <CoreFoundation/CFTimeZone.h> #include <mach/mach_time.h> +#include <sys/sysctl.h> #include <sys/time.h> +#include <sys/types.h> #include <time.h> #include "base/basictypes.h" #include "base/logging.h" #include "base/mac/scoped_cftyperef.h" +namespace { + +uint64_t ComputeCurrentTicks() { +#if defined(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, arraysize(mib), &boottime, &size, NULL, 0); + DCHECK_EQ(KERN_SUCCESS, kr); + base::TimeDelta time_difference = base::Time::Now() - + (base::Time::FromTimeT(boottime.tv_sec) + + base::TimeDelta::FromMicroseconds(boottime.tv_usec)); + return time_difference.InMicroseconds(); +#else + uint64_t absolute_micro; + + 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); + DCHECK_EQ(KERN_SUCCESS, kr); + } + + // mach_absolute_time is it when it comes to ticks on the Mac. Other calls + // with less precision (such as TickCount) just call through to + // mach_absolute_time. + + // timebase_info converts absolute time tick units into nanoseconds. Convert + // to microseconds up front to stave off overflows. + absolute_micro = + mach_absolute_time() / base::Time::kNanosecondsPerMicrosecond * + timebase_info.numer / timebase_info.denom; + + // Don't bother with the rollover handling that the Windows version does. + // With numer and denom = 1 (the expected case), the 64-bit absolute time + // reported in nanoseconds is enough to last nearly 585 years. + return absolute_micro; +#endif // defined(OS_IOS) +} + +} // namespace + namespace base { // The Time routines in this file use Mach and CoreFoundation APIs, since the @@ -131,33 +183,7 @@ void Time::Explode(bool is_local, Exploded* exploded) const { // static TimeTicks TimeTicks::Now() { - uint64_t absolute_micro; - - 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); - DCHECK_EQ(KERN_SUCCESS, kr); - } - - // mach_absolute_time is it when it comes to ticks on the Mac. Other calls - // with less precision (such as TickCount) just call through to - // mach_absolute_time. - - // timebase_info converts absolute time tick units into nanoseconds. Convert - // to microseconds up front to stave off overflows. - absolute_micro = mach_absolute_time() / Time::kNanosecondsPerMicrosecond * - timebase_info.numer / timebase_info.denom; - - // Don't bother with the rollover handling that the Windows version does. - // With numer and denom = 1 (the expected case), the 64-bit absolute time - // reported in nanoseconds is enough to last nearly 585 years. - - return TimeTicks(absolute_micro); + return TimeTicks(ComputeCurrentTicks()); } // static |