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> |
11 #endif | 11 #endif |
12 #if V8_OS_MACOSX | 12 #if V8_OS_MACOSX |
13 #include <mach/mach_time.h> | 13 #include <mach/mach_time.h> |
14 #endif | 14 #endif |
15 | 15 |
16 #include <cstring> | 16 #include <cstring> |
17 #include <ostream> | 17 #include <ostream> |
18 | 18 |
19 #if V8_OS_WIN | 19 #if V8_OS_WIN |
20 #include "src/base/atomicops.h" | |
21 #include "src/base/lazy-instance.h" | 20 #include "src/base/lazy-instance.h" |
22 #include "src/base/win32-headers.h" | 21 #include "src/base/win32-headers.h" |
23 #endif | 22 #endif |
24 #include "src/base/cpu.h" | 23 #include "src/base/cpu.h" |
25 #include "src/base/logging.h" | 24 #include "src/base/logging.h" |
26 #include "src/base/platform/platform.h" | 25 #include "src/base/platform/platform.h" |
27 | 26 |
28 namespace v8 { | 27 namespace v8 { |
29 namespace base { | 28 namespace base { |
30 | 29 |
(...skipping 397 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
428 | 427 |
429 bool IsHighResolution() override { return true; } | 428 bool IsHighResolution() override { return true; } |
430 | 429 |
431 private: | 430 private: |
432 int64_t ticks_per_second_; | 431 int64_t ticks_per_second_; |
433 }; | 432 }; |
434 | 433 |
435 | 434 |
436 class RolloverProtectedTickClock final : public TickClock { | 435 class RolloverProtectedTickClock final : public TickClock { |
437 public: | 436 public: |
438 RolloverProtectedTickClock() : rollover_(0) {} | 437 // We initialize rollover_ms_ to 1 to ensure that we will never |
| 438 // return 0 from TimeTicks::HighResolutionNow() and TimeTicks::Now() below. |
| 439 RolloverProtectedTickClock() : last_seen_now_(0), rollover_ms_(1) {} |
439 virtual ~RolloverProtectedTickClock() {} | 440 virtual ~RolloverProtectedTickClock() {} |
440 | 441 |
441 int64_t Now() override { | 442 int64_t Now() override { |
| 443 LockGuard<Mutex> lock_guard(&mutex_); |
442 // We use timeGetTime() to implement TimeTicks::Now(), which rolls over | 444 // We use timeGetTime() to implement TimeTicks::Now(), which rolls over |
443 // every ~49.7 days. We try to track rollover ourselves, which works if | 445 // every ~49.7 days. We try to track rollover ourselves, which works if |
444 // TimeTicks::Now() is called at least every 24 days. | 446 // TimeTicks::Now() is called at least every 49 days. |
445 // Note that we do not use GetTickCount() here, since timeGetTime() gives | 447 // Note that we do not use GetTickCount() here, since timeGetTime() gives |
446 // more predictable delta values, as described here: | 448 // more predictable delta values, as described here: |
447 // http://blogs.msdn.com/b/larryosterman/archive/2009/09/02/what-s-the-diffe
rence-between-gettickcount-and-timegettime.aspx | 449 // http://blogs.msdn.com/b/larryosterman/archive/2009/09/02/what-s-the-diffe
rence-between-gettickcount-and-timegettime.aspx |
448 // timeGetTime() provides 1ms granularity when combined with | 450 // timeGetTime() provides 1ms granularity when combined with |
449 // timeBeginPeriod(). If the host application for V8 wants fast timers, it | 451 // timeBeginPeriod(). If the host application for V8 wants fast timers, it |
450 // can use timeBeginPeriod() to increase the resolution. | 452 // can use timeBeginPeriod() to increase the resolution. |
451 // We use a lock-free version because the sampler thread calls it | 453 DWORD now = timeGetTime(); |
452 // while having the rest of the world stopped, that could cause a deadlock. | 454 if (now < last_seen_now_) { |
453 base::Atomic32 rollover = base::Acquire_Load(&rollover_); | 455 rollover_ms_ += V8_INT64_C(0x100000000); // ~49.7 days. |
454 base::Atomic32 now = static_cast<base::Atomic32>(timeGetTime()); | |
455 if ((now >> 31) != (rollover & 1)) { | |
456 base::Release_CompareAndSwap(&rollover_, rollover, rollover + 1); | |
457 ++rollover; | |
458 } | 456 } |
459 int64_t rollover_ms = static_cast<int64_t>(rollover) << 31; | 457 last_seen_now_ = now; |
460 return (rollover_ms | now) * Time::kMicrosecondsPerMillisecond; | 458 return (now + rollover_ms_) * Time::kMicrosecondsPerMillisecond; |
461 } | 459 } |
462 | 460 |
463 bool IsHighResolution() override { return false; } | 461 bool IsHighResolution() override { return false; } |
464 | 462 |
465 private: | 463 private: |
466 base::Atomic32 rollover_; | 464 Mutex mutex_; |
| 465 DWORD last_seen_now_; |
| 466 int64_t rollover_ms_; |
467 }; | 467 }; |
468 | 468 |
469 | 469 |
470 static LazyStaticInstance<RolloverProtectedTickClock, | 470 static LazyStaticInstance<RolloverProtectedTickClock, |
471 DefaultConstructTrait<RolloverProtectedTickClock>, | 471 DefaultConstructTrait<RolloverProtectedTickClock>, |
472 ThreadSafeInitOnceTrait>::type tick_clock = | 472 ThreadSafeInitOnceTrait>::type tick_clock = |
473 LAZY_STATIC_INSTANCE_INITIALIZER; | 473 LAZY_STATIC_INSTANCE_INITIALIZER; |
474 | 474 |
475 | 475 |
476 struct CreateHighResTickClockTrait { | 476 struct CreateHighResTickClockTrait { |
(...skipping 161 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
638 | 638 |
639 | 639 |
640 // static | 640 // static |
641 bool TimeTicks::KernelTimestampAvailable() { | 641 bool TimeTicks::KernelTimestampAvailable() { |
642 return kernel_tick_clock.Pointer()->Available(); | 642 return kernel_tick_clock.Pointer()->Available(); |
643 } | 643 } |
644 | 644 |
645 #endif // V8_OS_WIN | 645 #endif // V8_OS_WIN |
646 | 646 |
647 } } // namespace v8::base | 647 } } // namespace v8::base |
OLD | NEW |