OLD | NEW |
---|---|
1 // Copyright 2013 the V8 project authors. All rights reserved. | 1 // Copyright 2013 the V8 project authors. All rights reserved. |
2 // Redistribution and use in source and binary forms, with or without | 2 // Redistribution and use in source and binary forms, with or without |
3 // modification, are permitted provided that the following conditions are | 3 // modification, are permitted provided that the following conditions are |
4 // met: | 4 // met: |
5 // | 5 // |
6 // * Redistributions of source code must retain the above copyright | 6 // * Redistributions of source code must retain the above copyright |
7 // notice, this list of conditions and the following disclaimer. | 7 // notice, this list of conditions and the following disclaimer. |
8 // * Redistributions in binary form must reproduce the above | 8 // * Redistributions in binary form must reproduce the above |
9 // copyright notice, this list of conditions and the following | 9 // copyright notice, this list of conditions and the following |
10 // disclaimer in the documentation and/or other materials provided | 10 // disclaimer in the documentation and/or other materials provided |
(...skipping 10 matching lines...) Expand all Loading... | |
21 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT | 21 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
22 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, | 22 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
23 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY | 23 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
24 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | 24 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
25 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE | 25 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
26 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | 26 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
27 | 27 |
28 #include "platform/time.h" | 28 #include "platform/time.h" |
29 | 29 |
30 #if V8_OS_POSIX | 30 #if V8_OS_POSIX |
31 #include <errno.h> | |
31 #include <sys/time.h> | 32 #include <sys/time.h> |
32 #endif | 33 #endif |
33 #if V8_OS_MACOSX | 34 #if V8_OS_MACOSX |
34 #include <mach/mach_time.h> | 35 #include <mach/mach_time.h> |
35 #endif | 36 #endif |
36 | 37 |
37 #include <cstring> | 38 #include <cstring> |
38 | 39 |
39 #include "checks.h" | 40 #include "checks.h" |
40 #include "cpu.h" | 41 #include "cpu.h" |
(...skipping 222 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
263 return ft; | 264 return ft; |
264 } | 265 } |
265 uint64_t us = static_cast<uint64_t>(us_ + kTimeToEpochInMicroseconds) * 10; | 266 uint64_t us = static_cast<uint64_t>(us_ + kTimeToEpochInMicroseconds) * 10; |
266 ft.dwLowDateTime = static_cast<DWORD>(us); | 267 ft.dwLowDateTime = static_cast<DWORD>(us); |
267 ft.dwHighDateTime = static_cast<DWORD>(us >> 32); | 268 ft.dwHighDateTime = static_cast<DWORD>(us >> 32); |
268 return ft; | 269 return ft; |
269 } | 270 } |
270 | 271 |
271 #elif V8_OS_POSIX | 272 #elif V8_OS_POSIX |
272 | 273 |
274 // OS X doesn't support POSIX high-res timers and Solaris uses gethrtime(). | |
275 #if V8_OS_MACOSX || V8_OS_SOLARIS | |
273 Time Time::Now() { | 276 Time Time::Now() { |
274 struct timeval tv; | 277 struct timeval tv; |
275 int result = gettimeofday(&tv, NULL); | 278 int result = gettimeofday(&tv, NULL); |
276 ASSERT_EQ(0, result); | 279 ASSERT_EQ(0, result); |
277 USE(result); | 280 USE(result); |
278 return FromTimeval(tv); | 281 return FromTimeval(tv); |
279 } | 282 } |
283 #else | |
284 inline int GetBestMonotonicClock(struct timespec* ts) { | |
285 #if V8_OS_LINUX | |
286 // Prefer CLOCK_MONOTONIC_COARSE. It's a high-res clock that's available as | |
287 // of 2.6.32. It can be fully serviced from the vDSO, even on virtualized | |
288 // systems, which saves the overhead of a system call. The coarseness is not | |
289 // an issue for us because it still has sub-microsecond resolution. | |
290 static bool no_clock_monotonic_coarse; | |
291 if (no_clock_monotonic_coarse == false) { | |
Sven Panne
2013/10/30 12:54:11
http://refactoring.com/catalog/removeDoubleNegativ
bnoordhuis
2013/10/30 13:38:56
Agree on the double negative but I wrote it this w
Sven Panne
2013/10/30 14:26:50
OK, I was a bit confused. What I meant was the fac
| |
292 if (clock_gettime(6 /* CLOCK_MONOTONIC_COARSE */, ts) == 0) { | |
Benedikt Meurer
2013/10/30 12:49:15
I don't like hardcoding the constant here. Better
| |
293 return 0; | |
294 } | |
295 if (errno != EINVAL) { | |
296 return -1; | |
297 } | |
298 no_clock_monotonic_coarse = true; | |
299 } | |
300 #endif // V8_OS_LINUX | |
301 return clock_gettime(CLOCK_MONOTONIC, ts); | |
302 } | |
303 | |
304 | |
305 Time Time::Now() { | |
Benedikt Meurer
2013/10/30 12:49:15
Time::Now() has to use gettimeofday() on POSIX sys
bnoordhuis
2013/10/30 13:38:56
You mean CLOCK_REALTIME_COARSE is not allowed? If
Benedikt Meurer
2013/10/30 13:50:04
CLOCK_MONOTONIC != CLOCK_REALTIME. Time values hav
bnoordhuis
2013/10/30 14:18:40
Trying to deepen my understanding here. Is that b
| |
306 struct timespec ts; | |
307 int result = GetBestMonotonicClock(&ts); | |
308 ASSERT_EQ(0, result); | |
309 USE(result); | |
310 return FromTimespec(ts); | |
311 } | |
312 #endif // V8_OS_MACOSX || V8_OS_SOLARIS | |
280 | 313 |
281 | 314 |
282 Time Time::NowFromSystemTime() { | 315 Time Time::NowFromSystemTime() { |
283 return Now(); | 316 return Now(); |
284 } | 317 } |
285 | 318 |
286 | 319 |
287 Time Time::FromTimespec(struct timespec ts) { | 320 Time Time::FromTimespec(struct timespec ts) { |
288 ASSERT(ts.tv_nsec >= 0); | 321 ASSERT(ts.tv_nsec >= 0); |
289 ASSERT(ts.tv_nsec < static_cast<long>(kNanosecondsPerSecond)); // NOLINT | 322 ASSERT(ts.tv_nsec < static_cast<long>(kNanosecondsPerSecond)); // NOLINT |
(...skipping 273 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
563 // TODO(bmeurer): This is a temporary hack to support cross-compiling | 596 // TODO(bmeurer): This is a temporary hack to support cross-compiling |
564 // Chrome for Android in AOSP. Remove this once AOSP is fixed, also | 597 // Chrome for Android in AOSP. Remove this once AOSP is fixed, also |
565 // cleanup the tools/gyp/v8.gyp file. | 598 // cleanup the tools/gyp/v8.gyp file. |
566 struct timeval tv; | 599 struct timeval tv; |
567 int result = gettimeofday(&tv, NULL); | 600 int result = gettimeofday(&tv, NULL); |
568 ASSERT_EQ(0, result); | 601 ASSERT_EQ(0, result); |
569 USE(result); | 602 USE(result); |
570 ticks = (tv.tv_sec * Time::kMicrosecondsPerSecond + tv.tv_usec); | 603 ticks = (tv.tv_sec * Time::kMicrosecondsPerSecond + tv.tv_usec); |
571 #elif V8_OS_POSIX | 604 #elif V8_OS_POSIX |
572 struct timespec ts; | 605 struct timespec ts; |
573 int result = clock_gettime(CLOCK_MONOTONIC, &ts); | 606 int result = GetBestMonotonicClock(&ts); |
Benedikt Meurer
2013/10/30 12:49:15
Please take a look at the Xorg GetTimeInMillis() f
bnoordhuis
2013/10/30 13:38:56
What in particular do you want to steal^Wcopy? Th
Benedikt Meurer
2013/10/30 13:50:04
static clockid_t clockid;
if (!clockid) {
#ifd
bnoordhuis
2013/10/30 14:18:40
I'm a man of many qualities but mind reading is no
| |
574 ASSERT_EQ(0, result); | 607 ASSERT_EQ(0, result); |
575 USE(result); | 608 USE(result); |
576 ticks = (ts.tv_sec * Time::kMicrosecondsPerSecond + | 609 ticks = (ts.tv_sec * Time::kMicrosecondsPerSecond + |
577 ts.tv_nsec / Time::kNanosecondsPerMicrosecond); | 610 ts.tv_nsec / Time::kNanosecondsPerMicrosecond); |
578 #endif // V8_OS_MACOSX | 611 #endif // V8_OS_MACOSX |
579 // Make sure we never return 0 here. | 612 // Make sure we never return 0 here. |
580 return TimeTicks(ticks + 1); | 613 return TimeTicks(ticks + 1); |
581 } | 614 } |
582 | 615 |
583 | 616 |
584 // static | 617 // static |
585 bool TimeTicks::IsHighResolutionClockWorking() { | 618 bool TimeTicks::IsHighResolutionClockWorking() { |
586 return true; | 619 return true; |
587 } | 620 } |
588 | 621 |
589 #endif // V8_OS_WIN | 622 #endif // V8_OS_WIN |
590 | 623 |
591 } } // namespace v8::internal | 624 } } // namespace v8::internal |
OLD | NEW |