OLD | NEW |
---|---|
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium 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 | 5 |
6 // Windows Timer Primer | 6 // Windows Timer Primer |
7 // | 7 // |
8 // A good article: http://www.ddj.com/windows/184416651 | 8 // A good article: http://www.ddj.com/windows/184416651 |
9 // A good mozilla bug: http://bugzilla.mozilla.org/show_bug.cgi?id=363258 | 9 // A good mozilla bug: http://bugzilla.mozilla.org/show_bug.cgi?id=363258 |
10 // | 10 // |
(...skipping 65 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
76 return FileTimeToMicroseconds(ft); | 76 return FileTimeToMicroseconds(ft); |
77 } | 77 } |
78 | 78 |
79 // Time between resampling the un-granular clock for this API. 60 seconds. | 79 // Time between resampling the un-granular clock for this API. 60 seconds. |
80 const int kMaxMillisecondsToAvoidDrift = 60 * Time::kMillisecondsPerSecond; | 80 const int kMaxMillisecondsToAvoidDrift = 60 * Time::kMillisecondsPerSecond; |
81 | 81 |
82 int64 initial_time = 0; | 82 int64 initial_time = 0; |
83 TimeTicks initial_ticks; | 83 TimeTicks initial_ticks; |
84 | 84 |
85 void InitializeClock() { | 85 void InitializeClock() { |
86 initial_ticks = TimeTicks::Now(); | 86 initial_ticks = TimeTicks::Now(); |
jar (doing other things)
2014/08/08 00:30:53
None of this seems to be lock protected. How do y
fmeawad
2014/08/15 00:38:02
I could not locate a design doc, this particular c
| |
87 initial_time = CurrentWallclockMicroseconds(); | 87 initial_time = CurrentWallclockMicroseconds(); |
88 } | 88 } |
89 | 89 |
90 } // namespace | 90 } // namespace |
91 | 91 |
92 // Time ----------------------------------------------------------------------- | 92 // Time ----------------------------------------------------------------------- |
93 | 93 |
94 // The internal representation of Time uses FILETIME, whose epoch is 1601-01-01 | 94 // The internal representation of Time uses FILETIME, whose epoch is 1601-01-01 |
95 // 00:00:00 UTC. ((1970-1601)*365+89)*24*60*60*1000*1000, where 89 is the | 95 // 00:00:00 UTC. ((1970-1601)*365+89)*24*60*60*1000*1000, where 89 is the |
96 // number of leap year days between 1601 and 1970: (1970-1601)/4 excluding | 96 // number of leap year days between 1601 and 1970: (1970-1601)/4 excluding |
97 // 1700, 1800, and 1900. | 97 // 1700, 1800, and 1900. |
98 // static | 98 // static |
99 const int64 Time::kTimeTToMicrosecondsOffset = GG_INT64_C(11644473600000000); | 99 const int64 Time::kTimeTToMicrosecondsOffset = GG_INT64_C(11644473600000000); |
100 | 100 |
101 bool Time::high_resolution_timer_enabled_ = false; | 101 bool Time::high_resolution_timer_enabled_ = false; |
102 int Time::high_resolution_timer_activated_ = 0; | 102 int Time::high_resolution_timer_activated_ = 0; |
103 | 103 |
104 // static | 104 // static |
105 Time Time::Now() { | 105 Time Time::Now() { |
106 if (initial_time == 0) | 106 if (initial_time == 0) |
jar (doing other things)
2014/08/08 00:30:53
This sure seems scary, given that Time::Now() can
fmeawad
2014/08/15 00:38:02
With the lock added to InitializeClock and the sco
| |
107 InitializeClock(); | 107 InitializeClock(); |
108 | 108 |
109 // We implement time using the high-resolution timers so that we can get | 109 // We implement time using the high-resolution timers so that we can get |
110 // timeouts which are smaller than 10-15ms. If we just used | 110 // timeouts which are smaller than 10-15ms. If we just used |
111 // CurrentWallclockMicroseconds(), we'd have the less-granular timer. | 111 // CurrentWallclockMicroseconds(), we'd have the less-granular timer. |
112 // | 112 // |
113 // To make this work, we initialize the clock (initial_time) and the | 113 // To make this work, we initialize the clock (initial_time) and the |
114 // counter (initial_ctr). To compute the initial time, we can check | 114 // counter (initial_ctr). To compute the initial time, we can check |
115 // the number of ticks that have elapsed, and compute the delta. | 115 // the number of ticks that have elapsed, and compute the delta. |
116 // | 116 // |
117 // To avoid any drift, we periodically resync the counters to the system | 117 // To avoid any drift, we periodically resync the counters to the system |
118 // clock. | 118 // clock. |
119 while (true) { | 119 while (true) { |
120 TimeTicks ticks = TimeTicks::Now(); | 120 TimeTicks ticks = TimeTicks::Now(); |
121 | 121 |
122 // Calculate the time elapsed since we started our timer | 122 // Calculate the time elapsed since we started our timer |
123 TimeDelta elapsed = ticks - initial_ticks; | 123 TimeDelta elapsed = ticks - initial_ticks; |
124 | 124 |
125 // Check if enough time has elapsed that we need to resync the clock. | 125 // Check if enough time has elapsed that we need to resync the clock. |
126 if (elapsed.InMilliseconds() > kMaxMillisecondsToAvoidDrift) { | 126 if (elapsed.InMilliseconds() > kMaxMillisecondsToAvoidDrift) { |
127 InitializeClock(); | 127 InitializeClock(); |
128 continue; | 128 continue; |
129 } | 129 } |
130 | 130 |
131 return Time(elapsed + Time(initial_time)); | 131 return Time(elapsed + Time(initial_time)); |
jar (doing other things)
2014/08/08 00:30:53
This seems to access initial_time in a racy fashio
fmeawad
2014/08/15 00:38:02
Now we read initial_ticks and initial_time in a pr
| |
132 } | 132 } |
133 } | 133 } |
134 | 134 |
135 // static | 135 // static |
136 Time Time::NowFromSystemTime() { | 136 Time Time::NowFromSystemTime() { |
137 // Force resync. | 137 // Force resync. |
138 InitializeClock(); | 138 InitializeClock(); |
139 return Time(initial_time); | 139 return Time(initial_time); |
140 } | 140 } |
141 | 141 |
(...skipping 210 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
352 // reality due to bugs in BIOS or HAL on some, especially old computers. | 352 // reality due to bugs in BIOS or HAL on some, especially old computers. |
353 // With recent updates on HAL and newer BIOS, QPC is getting more reliable but | 353 // With recent updates on HAL and newer BIOS, QPC is getting more reliable but |
354 // it should be used with caution. | 354 // it should be used with caution. |
355 // | 355 // |
356 // (3) System time. The system time provides a low-resolution (typically 10ms | 356 // (3) System time. The system time provides a low-resolution (typically 10ms |
357 // to 55 milliseconds) time stamp but is comparatively less expensive to | 357 // to 55 milliseconds) time stamp but is comparatively less expensive to |
358 // retrieve and more reliable. | 358 // retrieve and more reliable. |
359 class HighResNowSingleton { | 359 class HighResNowSingleton { |
360 public: | 360 public: |
361 HighResNowSingleton() | 361 HighResNowSingleton() |
362 : ticks_per_second_(0), | 362 : ticks_per_second_(0), |
jar (doing other things)
2014/08/08 00:30:53
nit: [Yeah... this wasn't your code... but I had t
fmeawad
2014/08/15 00:38:02
Done.
| |
363 skew_(0) { | 363 skew_(0) { |
364 InitializeClock(); | 364 InitializeClock(); |
365 | 365 |
366 base::CPU cpu; | 366 base::CPU cpu; |
367 if (IsBuggyAthlon(cpu)) | 367 if (IsBuggyAthlon(cpu)) |
368 DisableHighResClock(); | 368 DisableHighResClock(); |
369 } | 369 } |
370 | 370 |
371 bool IsUsingHighResClock() { | 371 bool IsUsingHighResClock() { |
372 return ticks_per_second_ != 0.0; | 372 return ticks_per_second_ != 0.0; |
(...skipping 26 matching lines...) Expand all Loading... | |
399 int64 whole_seconds = qpc_value / ticks_per_second_; | 399 int64 whole_seconds = qpc_value / ticks_per_second_; |
400 int64 leftover_ticks = qpc_value % ticks_per_second_; | 400 int64 leftover_ticks = qpc_value % ticks_per_second_; |
401 int64 microseconds = (whole_seconds * Time::kMicrosecondsPerSecond) + | 401 int64 microseconds = (whole_seconds * Time::kMicrosecondsPerSecond) + |
402 ((leftover_ticks * Time::kMicrosecondsPerSecond) / | 402 ((leftover_ticks * Time::kMicrosecondsPerSecond) / |
403 ticks_per_second_); | 403 ticks_per_second_); |
404 return microseconds; | 404 return microseconds; |
405 } | 405 } |
406 | 406 |
407 private: | 407 private: |
408 // Synchronize the QPC clock with GetSystemTimeAsFileTime. | 408 // Synchronize the QPC clock with GetSystemTimeAsFileTime. |
409 void InitializeClock() { | 409 void InitializeClock() { |
jar (doing other things)
2014/08/08 00:30:53
Is this private method called exactly once, on lin
fmeawad
2014/08/15 00:38:02
I factored it inside its caller.
| |
410 LARGE_INTEGER ticks_per_sec = {0}; | 410 LARGE_INTEGER ticks_per_sec = {0}; |
411 if (!QueryPerformanceFrequency(&ticks_per_sec)) | 411 if (!QueryPerformanceFrequency(&ticks_per_sec)) |
412 return; // Broken, we don't guarantee this function works. | 412 return; // Broken, we don't guarantee this function works. |
413 ticks_per_second_ = ticks_per_sec.QuadPart; | 413 ticks_per_second_ = ticks_per_sec.QuadPart; |
414 | 414 |
415 skew_ = UnreliableNow() - ReliableNow(); | 415 skew_ = UnreliableNow() - ReliableNow(); |
416 } | 416 } |
417 | 417 |
418 // Get the number of microseconds since boot in an unreliable fashion. | 418 // Get the number of microseconds since boot in an unreliable fashion. |
419 int64 UnreliableNow() { | 419 int64 UnreliableNow() { |
(...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
469 return old; | 469 return old; |
470 } | 470 } |
471 | 471 |
472 // static | 472 // static |
473 bool TimeTicks::SetNowIsHighResNowIfSupported() { | 473 bool TimeTicks::SetNowIsHighResNowIfSupported() { |
474 if (!CPUReliablySupportsHighResTime()) { | 474 if (!CPUReliablySupportsHighResTime()) { |
475 return false; | 475 return false; |
476 } | 476 } |
477 | 477 |
478 now_function = HighResNowWrapper; | 478 now_function = HighResNowWrapper; |
479 InitializeClock(); | |
479 return true; | 480 return true; |
480 } | 481 } |
481 | 482 |
482 // static | 483 // static |
483 TimeTicks TimeTicks::Now() { | 484 TimeTicks TimeTicks::Now() { |
484 return TimeTicks() + now_function(); | 485 return TimeTicks() + now_function(); |
485 } | 486 } |
486 | 487 |
487 // static | 488 // static |
488 TimeTicks TimeTicks::HighResNow() { | 489 TimeTicks TimeTicks::HighResNow() { |
(...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
527 return TimeTicks() + TimeDelta::FromMilliseconds(timeGetTime()); | 528 return TimeTicks() + TimeDelta::FromMilliseconds(timeGetTime()); |
528 } | 529 } |
529 } | 530 } |
530 | 531 |
531 // TimeDelta ------------------------------------------------------------------ | 532 // TimeDelta ------------------------------------------------------------------ |
532 | 533 |
533 // static | 534 // static |
534 TimeDelta TimeDelta::FromQPCValue(LONGLONG qpc_value) { | 535 TimeDelta TimeDelta::FromQPCValue(LONGLONG qpc_value) { |
535 return TimeDelta(GetHighResNowSingleton()->QPCValueToMicroseconds(qpc_value)); | 536 return TimeDelta(GetHighResNowSingleton()->QPCValueToMicroseconds(qpc_value)); |
536 } | 537 } |
OLD | NEW |