OLD | NEW |
1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file |
2 // for details. All rights reserved. Use of this source code is governed by a | 2 // for details. All rights reserved. Use of this source code is governed by a |
3 // BSD-style license that can be found in the LICENSE file. | 3 // BSD-style license that can be found in the LICENSE file. |
4 | 4 |
5 #include "platform/globals.h" | 5 #include "platform/globals.h" |
6 #if defined(TARGET_OS_MACOS) | 6 #if defined(HOST_OS_MACOS) |
7 | 7 |
8 #include <errno.h> // NOLINT | 8 #include <errno.h> // NOLINT |
9 #include <mach/clock.h> // NOLINT | 9 #include <mach/clock.h> // NOLINT |
10 #include <mach/mach.h> // NOLINT | 10 #include <mach/mach.h> // NOLINT |
11 #include <mach/mach_time.h> // NOLINT | 11 #include <mach/mach_time.h> // NOLINT |
12 #include <netdb.h> // NOLINT | 12 #include <netdb.h> // NOLINT |
13 #if TARGET_OS_IOS | 13 #if HOST_OS_IOS |
14 #include <sys/sysctl.h> // NOLINT | 14 #include <sys/sysctl.h> // NOLINT |
15 #endif | 15 #endif |
16 #include <sys/time.h> // NOLINT | 16 #include <sys/time.h> // NOLINT |
17 #include <time.h> // NOLINT | 17 #include <time.h> // NOLINT |
18 | 18 |
19 #include "bin/utils.h" | 19 #include "bin/utils.h" |
20 #include "platform/assert.h" | 20 #include "platform/assert.h" |
21 #include "platform/utils.h" | 21 #include "platform/utils.h" |
22 | 22 |
23 namespace dart { | 23 namespace dart { |
(...skipping 93 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
117 kern_return_t kr = mach_timebase_info(&timebase_info); | 117 kern_return_t kr = mach_timebase_info(&timebase_info); |
118 ASSERT(KERN_SUCCESS == kr); | 118 ASSERT(KERN_SUCCESS == kr); |
119 } | 119 } |
120 | 120 |
121 | 121 |
122 int64_t TimerUtils::GetCurrentMonotonicMillis() { | 122 int64_t TimerUtils::GetCurrentMonotonicMillis() { |
123 return GetCurrentMonotonicMicros() / 1000; | 123 return GetCurrentMonotonicMicros() / 1000; |
124 } | 124 } |
125 | 125 |
126 | 126 |
127 #if TARGET_OS_IOS | 127 #if HOST_OS_IOS |
128 static int64_t GetCurrentTimeMicros() { | 128 static int64_t GetCurrentTimeMicros() { |
129 // gettimeofday has microsecond resolution. | 129 // gettimeofday has microsecond resolution. |
130 struct timeval tv; | 130 struct timeval tv; |
131 if (gettimeofday(&tv, NULL) < 0) { | 131 if (gettimeofday(&tv, NULL) < 0) { |
132 UNREACHABLE(); | 132 UNREACHABLE(); |
133 return 0; | 133 return 0; |
134 } | 134 } |
135 return (static_cast<int64_t>(tv.tv_sec) * 1000000) + tv.tv_usec; | 135 return (static_cast<int64_t>(tv.tv_sec) * 1000000) + tv.tv_usec; |
136 } | 136 } |
137 #endif // TARGET_OS_IOS | 137 #endif // HOST_OS_IOS |
138 | 138 |
139 | 139 |
140 int64_t TimerUtils::GetCurrentMonotonicMicros() { | 140 int64_t TimerUtils::GetCurrentMonotonicMicros() { |
141 #if TARGET_OS_IOS | 141 #if HOST_OS_IOS |
142 // On iOS mach_absolute_time stops while the device is sleeping. Instead use | 142 // On iOS mach_absolute_time stops while the device is sleeping. Instead use |
143 // now - KERN_BOOTTIME to get a time difference that is not impacted by clock | 143 // now - KERN_BOOTTIME to get a time difference that is not impacted by clock |
144 // changes. KERN_BOOTTIME will be updated by the system whenever the system | 144 // changes. KERN_BOOTTIME will be updated by the system whenever the system |
145 // clock change. | 145 // clock change. |
146 struct timeval boottime; | 146 struct timeval boottime; |
147 int mib[2] = {CTL_KERN, KERN_BOOTTIME}; | 147 int mib[2] = {CTL_KERN, KERN_BOOTTIME}; |
148 size_t size = sizeof(boottime); | 148 size_t size = sizeof(boottime); |
149 int kr = sysctl(mib, sizeof(mib) / sizeof(mib[0]), &boottime, &size, NULL, 0); | 149 int kr = sysctl(mib, sizeof(mib) / sizeof(mib[0]), &boottime, &size, NULL, 0); |
150 ASSERT(KERN_SUCCESS == kr); | 150 ASSERT(KERN_SUCCESS == kr); |
151 int64_t now = GetCurrentTimeMicros(); | 151 int64_t now = GetCurrentTimeMicros(); |
152 int64_t origin = boottime.tv_sec * kMicrosecondsPerSecond; | 152 int64_t origin = boottime.tv_sec * kMicrosecondsPerSecond; |
153 origin += boottime.tv_usec; | 153 origin += boottime.tv_usec; |
154 return now - origin; | 154 return now - origin; |
155 #else | 155 #else |
156 ASSERT(timebase_info.denom != 0); | 156 ASSERT(timebase_info.denom != 0); |
157 // timebase_info converts absolute time tick units into nanoseconds. Convert | 157 // timebase_info converts absolute time tick units into nanoseconds. Convert |
158 // to microseconds. | 158 // to microseconds. |
159 int64_t result = mach_absolute_time() / kNanosecondsPerMicrosecond; | 159 int64_t result = mach_absolute_time() / kNanosecondsPerMicrosecond; |
160 result *= timebase_info.numer; | 160 result *= timebase_info.numer; |
161 result /= timebase_info.denom; | 161 result /= timebase_info.denom; |
162 return result; | 162 return result; |
163 #endif // TARGET_OS_IOS | 163 #endif // HOST_OS_IOS |
164 } | 164 } |
165 | 165 |
166 | 166 |
167 void TimerUtils::Sleep(int64_t millis) { | 167 void TimerUtils::Sleep(int64_t millis) { |
168 struct timespec req; // requested. | 168 struct timespec req; // requested. |
169 struct timespec rem; // remainder. | 169 struct timespec rem; // remainder. |
170 int64_t micros = millis * kMicrosecondsPerMillisecond; | 170 int64_t micros = millis * kMicrosecondsPerMillisecond; |
171 int64_t seconds = micros / kMicrosecondsPerSecond; | 171 int64_t seconds = micros / kMicrosecondsPerSecond; |
172 micros = micros - seconds * kMicrosecondsPerSecond; | 172 micros = micros - seconds * kMicrosecondsPerSecond; |
173 int64_t nanos = micros * kNanosecondsPerMicrosecond; | 173 int64_t nanos = micros * kNanosecondsPerMicrosecond; |
174 req.tv_sec = seconds; | 174 req.tv_sec = seconds; |
175 req.tv_nsec = nanos; | 175 req.tv_nsec = nanos; |
176 while (true) { | 176 while (true) { |
177 int r = nanosleep(&req, &rem); | 177 int r = nanosleep(&req, &rem); |
178 if (r == 0) { | 178 if (r == 0) { |
179 break; | 179 break; |
180 } | 180 } |
181 // We should only ever see an interrupt error. | 181 // We should only ever see an interrupt error. |
182 ASSERT(errno == EINTR); | 182 ASSERT(errno == EINTR); |
183 // Copy remainder into requested and repeat. | 183 // Copy remainder into requested and repeat. |
184 req = rem; | 184 req = rem; |
185 } | 185 } |
186 } | 186 } |
187 | 187 |
188 } // namespace bin | 188 } // namespace bin |
189 } // namespace dart | 189 } // namespace dart |
190 | 190 |
191 #endif // defined(TARGET_OS_MACOS) | 191 #endif // defined(HOST_OS_MACOS) |
OLD | NEW |