MCPcopy Create free account
hub / github.com/apache/brpc / ComputeCurrentTicks

Function ComputeCurrentTicks

src/butil/time/time_mac.cc:23–67  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

21namespace {
22
23uint64_t ComputeCurrentTicks() {
24#if defined(OS_IOS)
25 // On iOS mach_absolute_time stops while the device is sleeping. Instead use
26 // now - KERN_BOOTTIME to get a time difference that is not impacted by clock
27 // changes. KERN_BOOTTIME will be updated by the system whenever the system
28 // clock change.
29 struct timeval boottime;
30 int mib[2] = {CTL_KERN, KERN_BOOTTIME};
31 size_t size = sizeof(boottime);
32 int kr = sysctl(mib, arraysize(mib), &boottime, &size, NULL, 0);
33 DCHECK_EQ(KERN_SUCCESS, kr);
34 butil::TimeDelta time_difference = butil::Time::Now() -
35 (butil::Time::FromTimeT(boottime.tv_sec) +
36 butil::TimeDelta::FromMicroseconds(boottime.tv_usec));
37 return time_difference.InMicroseconds();
38#else
39 uint64_t absolute_micro;
40
41 static mach_timebase_info_data_t timebase_info;
42 if (timebase_info.denom == 0) {
43 // Zero-initialization of statics guarantees that denom will be 0 before
44 // calling mach_timebase_info. mach_timebase_info will never set denom to
45 // 0 as that would be invalid, so the zero-check can be used to determine
46 // whether mach_timebase_info has already been called. This is
47 // recommended by Apple's QA1398.
48 kern_return_t kr = mach_timebase_info(&timebase_info);
49 DCHECK(kr == KERN_SUCCESS) << "Fail to call mach_timebase_info";
50 }
51
52 // mach_absolute_time is it when it comes to ticks on the Mac. Other calls
53 // with less precision (such as TickCount) just call through to
54 // mach_absolute_time.
55
56 // timebase_info converts absolute time tick units into nanoseconds. Convert
57 // to microseconds up front to stave off overflows.
58 absolute_micro =
59 mach_absolute_time() / butil::Time::kNanosecondsPerMicrosecond *
60 timebase_info.numer / timebase_info.denom;
61
62 // Don't bother with the rollover handling that the Windows version does.
63 // With numer and denom = 1 (the expected case), the 64-bit absolute time
64 // reported in nanoseconds is enough to last nearly 585 years.
65 return absolute_micro;
66#endif // defined(OS_IOS)
67}
68
69uint64_t ComputeThreadTicks() {
70#if defined(OS_IOS)

Callers 1

NowMethod · 0.85

Calls 1

InMicrosecondsMethod · 0.80

Tested by

no test coverage detected