OLD | NEW |
1 // Copyright 2013 the V8 project authors. All rights reserved. | 1 // Copyright 2013 the V8 project authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "src/base/platform/time.h" | 5 #include "src/base/platform/time.h" |
6 | 6 |
7 #if V8_OS_POSIX | 7 #if V8_OS_POSIX |
8 #include <fcntl.h> // for O_RDONLY | 8 #include <fcntl.h> // for O_RDONLY |
9 #include <sys/time.h> | 9 #include <sys/time.h> |
10 #include <unistd.h> | 10 #include <unistd.h> |
(...skipping 383 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
394 // to 55 milliseconds) time stamp but is comparatively less expensive to | 394 // to 55 milliseconds) time stamp but is comparatively less expensive to |
395 // retrieve and more reliable. | 395 // retrieve and more reliable. |
396 class HighResolutionTickClock FINAL : public TickClock { | 396 class HighResolutionTickClock FINAL : public TickClock { |
397 public: | 397 public: |
398 explicit HighResolutionTickClock(int64_t ticks_per_second) | 398 explicit HighResolutionTickClock(int64_t ticks_per_second) |
399 : ticks_per_second_(ticks_per_second) { | 399 : ticks_per_second_(ticks_per_second) { |
400 DCHECK_LT(0, ticks_per_second); | 400 DCHECK_LT(0, ticks_per_second); |
401 } | 401 } |
402 virtual ~HighResolutionTickClock() {} | 402 virtual ~HighResolutionTickClock() {} |
403 | 403 |
404 virtual int64_t Now() OVERRIDE { | 404 int64_t Now() OVERRIDE { |
405 LARGE_INTEGER now; | 405 LARGE_INTEGER now; |
406 BOOL result = QueryPerformanceCounter(&now); | 406 BOOL result = QueryPerformanceCounter(&now); |
407 DCHECK(result); | 407 DCHECK(result); |
408 USE(result); | 408 USE(result); |
409 | 409 |
410 // Intentionally calculate microseconds in a round about manner to avoid | 410 // Intentionally calculate microseconds in a round about manner to avoid |
411 // overflow and precision issues. Think twice before simplifying! | 411 // overflow and precision issues. Think twice before simplifying! |
412 int64_t whole_seconds = now.QuadPart / ticks_per_second_; | 412 int64_t whole_seconds = now.QuadPart / ticks_per_second_; |
413 int64_t leftover_ticks = now.QuadPart % ticks_per_second_; | 413 int64_t leftover_ticks = now.QuadPart % ticks_per_second_; |
414 int64_t ticks = (whole_seconds * Time::kMicrosecondsPerSecond) + | 414 int64_t ticks = (whole_seconds * Time::kMicrosecondsPerSecond) + |
415 ((leftover_ticks * Time::kMicrosecondsPerSecond) / ticks_per_second_); | 415 ((leftover_ticks * Time::kMicrosecondsPerSecond) / ticks_per_second_); |
416 | 416 |
417 // Make sure we never return 0 here, so that TimeTicks::HighResolutionNow() | 417 // Make sure we never return 0 here, so that TimeTicks::HighResolutionNow() |
418 // will never return 0. | 418 // will never return 0. |
419 return ticks + 1; | 419 return ticks + 1; |
420 } | 420 } |
421 | 421 |
422 virtual bool IsHighResolution() OVERRIDE { | 422 bool IsHighResolution() OVERRIDE { return true; } |
423 return true; | |
424 } | |
425 | 423 |
426 private: | 424 private: |
427 int64_t ticks_per_second_; | 425 int64_t ticks_per_second_; |
428 }; | 426 }; |
429 | 427 |
430 | 428 |
431 class RolloverProtectedTickClock FINAL : public TickClock { | 429 class RolloverProtectedTickClock FINAL : public TickClock { |
432 public: | 430 public: |
433 // We initialize rollover_ms_ to 1 to ensure that we will never | 431 // We initialize rollover_ms_ to 1 to ensure that we will never |
434 // return 0 from TimeTicks::HighResolutionNow() and TimeTicks::Now() below. | 432 // return 0 from TimeTicks::HighResolutionNow() and TimeTicks::Now() below. |
435 RolloverProtectedTickClock() : last_seen_now_(0), rollover_ms_(1) {} | 433 RolloverProtectedTickClock() : last_seen_now_(0), rollover_ms_(1) {} |
436 virtual ~RolloverProtectedTickClock() {} | 434 virtual ~RolloverProtectedTickClock() {} |
437 | 435 |
438 virtual int64_t Now() OVERRIDE { | 436 int64_t Now() OVERRIDE { |
439 LockGuard<Mutex> lock_guard(&mutex_); | 437 LockGuard<Mutex> lock_guard(&mutex_); |
440 // We use timeGetTime() to implement TimeTicks::Now(), which rolls over | 438 // We use timeGetTime() to implement TimeTicks::Now(), which rolls over |
441 // every ~49.7 days. We try to track rollover ourselves, which works if | 439 // every ~49.7 days. We try to track rollover ourselves, which works if |
442 // TimeTicks::Now() is called at least every 49 days. | 440 // TimeTicks::Now() is called at least every 49 days. |
443 // Note that we do not use GetTickCount() here, since timeGetTime() gives | 441 // Note that we do not use GetTickCount() here, since timeGetTime() gives |
444 // more predictable delta values, as described here: | 442 // more predictable delta values, as described here: |
445 // http://blogs.msdn.com/b/larryosterman/archive/2009/09/02/what-s-the-diffe
rence-between-gettickcount-and-timegettime.aspx | 443 // http://blogs.msdn.com/b/larryosterman/archive/2009/09/02/what-s-the-diffe
rence-between-gettickcount-and-timegettime.aspx |
446 // timeGetTime() provides 1ms granularity when combined with | 444 // timeGetTime() provides 1ms granularity when combined with |
447 // timeBeginPeriod(). If the host application for V8 wants fast timers, it | 445 // timeBeginPeriod(). If the host application for V8 wants fast timers, it |
448 // can use timeBeginPeriod() to increase the resolution. | 446 // can use timeBeginPeriod() to increase the resolution. |
449 DWORD now = timeGetTime(); | 447 DWORD now = timeGetTime(); |
450 if (now < last_seen_now_) { | 448 if (now < last_seen_now_) { |
451 rollover_ms_ += V8_INT64_C(0x100000000); // ~49.7 days. | 449 rollover_ms_ += V8_INT64_C(0x100000000); // ~49.7 days. |
452 } | 450 } |
453 last_seen_now_ = now; | 451 last_seen_now_ = now; |
454 return (now + rollover_ms_) * Time::kMicrosecondsPerMillisecond; | 452 return (now + rollover_ms_) * Time::kMicrosecondsPerMillisecond; |
455 } | 453 } |
456 | 454 |
457 virtual bool IsHighResolution() OVERRIDE { | 455 bool IsHighResolution() OVERRIDE { return false; } |
458 return false; | |
459 } | |
460 | 456 |
461 private: | 457 private: |
462 Mutex mutex_; | 458 Mutex mutex_; |
463 DWORD last_seen_now_; | 459 DWORD last_seen_now_; |
464 int64_t rollover_ms_; | 460 int64_t rollover_ms_; |
465 }; | 461 }; |
466 | 462 |
467 | 463 |
468 static LazyStaticInstance<RolloverProtectedTickClock, | 464 static LazyStaticInstance<RolloverProtectedTickClock, |
469 DefaultConstructTrait<RolloverProtectedTickClock>, | 465 DefaultConstructTrait<RolloverProtectedTickClock>, |
(...skipping 175 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
645 | 641 |
646 | 642 |
647 // static | 643 // static |
648 bool TimeTicks::KernelTimestampAvailable() { | 644 bool TimeTicks::KernelTimestampAvailable() { |
649 return kernel_tick_clock.Pointer()->Available(); | 645 return kernel_tick_clock.Pointer()->Available(); |
650 } | 646 } |
651 | 647 |
652 #endif // V8_OS_WIN | 648 #endif // V8_OS_WIN |
653 | 649 |
654 } } // namespace v8::base | 650 } } // namespace v8::base |
OLD | NEW |