Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(544)

Side by Side Diff: base/time/time_win.cc

Issue 446203002: Initialize the now_funciton to the HighResNowWrapper in case High Res is supported (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Enable HiRes by default instead of using LowRes then switching to it. Created 6 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « base/time/time.h ('k') | chrome/browser/chrome_browser_main_win.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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 18 matching lines...) Expand all
29 // 29 //
30 // To work around all this, we're going to generally use timeGetTime(). We 30 // To work around all this, we're going to generally use timeGetTime(). We
31 // will only increase the system-wide timer if we're not running on battery 31 // will only increase the system-wide timer if we're not running on battery
32 // power. Using timeBeginPeriod(1) is a requirement in order to make our 32 // power. Using timeBeginPeriod(1) is a requirement in order to make our
33 // message loop waits have the same resolution that our time measurements 33 // message loop waits have the same resolution that our time measurements
34 // do. Otherwise, WaitForSingleObject(..., 1) will no less than 15ms when 34 // do. Otherwise, WaitForSingleObject(..., 1) will no less than 15ms when
35 // there is nothing else to waken the Wait. 35 // there is nothing else to waken the Wait.
36 36
37 #include "base/time/time.h" 37 #include "base/time/time.h"
38 38
39
40
jar (doing other things) 2014/08/22 19:11:53 nit: kill spaces (which you noticed!)
fmeawad 2014/08/22 21:13:12 Done.
39 #pragma comment(lib, "winmm.lib") 41 #pragma comment(lib, "winmm.lib")
40 #include <windows.h> 42 #include <windows.h>
41 #include <mmsystem.h> 43 #include <mmsystem.h>
42 44
43 #include "base/basictypes.h" 45 #include "base/basictypes.h"
44 #include "base/cpu.h" 46 #include "base/cpu.h"
45 #include "base/lazy_instance.h" 47 #include "base/lazy_instance.h"
46 #include "base/logging.h" 48 #include "base/logging.h"
47 #include "base/synchronization/lock.h" 49 #include "base/synchronization/lock.h"
48 50
(...skipping 303 matching lines...) Expand 10 before | Expand all | Expand 10 after
352 // reality due to bugs in BIOS or HAL on some, especially old computers. 354 // 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 355 // With recent updates on HAL and newer BIOS, QPC is getting more reliable but
354 // it should be used with caution. 356 // it should be used with caution.
355 // 357 //
356 // (3) System time. The system time provides a low-resolution (typically 10ms 358 // (3) System time. The system time provides a low-resolution (typically 10ms
357 // to 55 milliseconds) time stamp but is comparatively less expensive to 359 // to 55 milliseconds) time stamp but is comparatively less expensive to
358 // retrieve and more reliable. 360 // retrieve and more reliable.
359 class HighResNowSingleton { 361 class HighResNowSingleton {
360 public: 362 public:
361 HighResNowSingleton() 363 HighResNowSingleton()
362 : ticks_per_second_(0), 364 : ticks_per_second_(0),
363 skew_(0) { 365 skew_(0) {
364 InitializeClock();
365 366
366 base::CPU cpu; 367 base::CPU cpu;
367 if (IsBuggyAthlon(cpu)) 368 if (IsBuggyAthlon(cpu))
368 DisableHighResClock(); 369 return;
370
371 // Synchronize the QPC clock with GetSystemTimeAsFileTime.
372 LARGE_INTEGER ticks_per_sec = {0};
373 if (!QueryPerformanceFrequency(&ticks_per_sec))
374 return; // QPC is not available.
375 ticks_per_second_ = ticks_per_sec.QuadPart;
376
377 skew_ = UnreliableNow() - ReliableNow();
369 } 378 }
370 379
371 bool IsUsingHighResClock() { 380 bool IsUsingHighResClock() {
372 return ticks_per_second_ != 0.0; 381 return ticks_per_second_ != 0;
373 }
374
375 void DisableHighResClock() {
376 ticks_per_second_ = 0.0;
377 } 382 }
378 383
379 TimeDelta Now() { 384 TimeDelta Now() {
380 if (IsUsingHighResClock()) 385 if (IsUsingHighResClock())
381 return TimeDelta::FromMicroseconds(UnreliableNow()); 386 return TimeDelta::FromMicroseconds(UnreliableNow());
382 387
383 // Just fallback to the slower clock. 388 // Just fallback to the slower clock.
384 return RolloverProtectedNow(); 389 return RolloverProtectedNow();
385 } 390 }
386 391
(...skipping 14 matching lines...) Expand all
401 // overflow and precision issues. 406 // overflow and precision issues.
402 int64 whole_seconds = qpc_value / ticks_per_second_; 407 int64 whole_seconds = qpc_value / ticks_per_second_;
403 int64 leftover_ticks = qpc_value - (whole_seconds * ticks_per_second_); 408 int64 leftover_ticks = qpc_value - (whole_seconds * ticks_per_second_);
404 int64 microseconds = (whole_seconds * Time::kMicrosecondsPerSecond) + 409 int64 microseconds = (whole_seconds * Time::kMicrosecondsPerSecond) +
405 ((leftover_ticks * Time::kMicrosecondsPerSecond) / 410 ((leftover_ticks * Time::kMicrosecondsPerSecond) /
406 ticks_per_second_); 411 ticks_per_second_);
407 return microseconds; 412 return microseconds;
408 } 413 }
409 414
410 private: 415 private:
411 // Synchronize the QPC clock with GetSystemTimeAsFileTime.
412 void InitializeClock() {
413 LARGE_INTEGER ticks_per_sec = {0};
414 if (!QueryPerformanceFrequency(&ticks_per_sec))
415 return; // Broken, we don't guarantee this function works.
416 ticks_per_second_ = ticks_per_sec.QuadPart;
417
418 skew_ = UnreliableNow() - ReliableNow();
419 }
420
421 // Get the number of microseconds since boot in an unreliable fashion. 416 // Get the number of microseconds since boot in an unreliable fashion.
422 int64 UnreliableNow() { 417 int64 UnreliableNow() {
423 LARGE_INTEGER now; 418 LARGE_INTEGER now;
424 QueryPerformanceCounter(&now); 419 QueryPerformanceCounter(&now);
425 return QPCValueToMicroseconds(now.QuadPart); 420 return QPCValueToMicroseconds(now.QuadPart);
426 } 421 }
427 422
428 // Get the number of microseconds since boot in a reliable fashion. 423 // Get the number of microseconds since boot in a reliable fashion.
429 int64 ReliableNow() { 424 int64 ReliableNow() {
430 return RolloverProtectedNow().InMicroseconds(); 425 return RolloverProtectedNow().InMicroseconds();
431 } 426 }
432 427
433 int64 ticks_per_second_; // 0 indicates QPF failed and we're broken. 428 int64 ticks_per_second_; // 0 indicates QPF failed and we're broken.
434 int64 skew_; // Skew between lo-res and hi-res clocks (for debugging). 429 int64 skew_; // Skew between lo-res and hi-res clocks (for debugging).
435 }; 430 };
436 431
437 static base::LazyInstance<HighResNowSingleton>::Leaky 432 static base::LazyInstance<HighResNowSingleton>::Leaky
438 leaky_high_res_now_singleton = LAZY_INSTANCE_INITIALIZER; 433 leaky_high_res_now_singleton = LAZY_INSTANCE_INITIALIZER;
439 434
440 HighResNowSingleton* GetHighResNowSingleton() { 435 HighResNowSingleton* GetHighResNowSingleton() {
441 return leaky_high_res_now_singleton.Pointer(); 436 return leaky_high_res_now_singleton.Pointer();
442 } 437 }
443 438
444 TimeDelta HighResNowWrapper() { 439 TimeDelta HighResNowWrapper() {
445 return GetHighResNowSingleton()->Now(); 440 return GetHighResNowSingleton()->Now();
446 } 441 }
447 442
448 typedef TimeDelta (*NowFunction)(void); 443 typedef TimeDelta (*NowFunction)(void);
449 NowFunction now_function = RolloverProtectedNow;
450 444
451 bool CPUReliablySupportsHighResTime() { 445 bool CPUReliablySupportsHighResTime() {
452 base::CPU cpu; 446 base::CPU cpu;
453 if (!cpu.has_non_stop_time_stamp_counter() || 447 if (!cpu.has_non_stop_time_stamp_counter() ||
454 !GetHighResNowSingleton()->IsUsingHighResClock()) 448 !GetHighResNowSingleton()->IsUsingHighResClock())
455 return false; 449 return false;
456 450
457 if (IsBuggyAthlon(cpu)) 451 if (IsBuggyAthlon(cpu))
458 return false; 452 return false;
459 453
460 return true; 454 return true;
461 } 455 }
462 456
457 NowFunction GetNowFunction() {
458 if (!CPUReliablySupportsHighResTime())
459 return RolloverProtectedNow;
460 return HighResNowWrapper;
461 }
462
463 NowFunction now_function = GetNowFunction();
464
463 } // namespace 465 } // namespace
464 466
465 // static 467 // static
466 TimeTicks::TickFunctionType TimeTicks::SetMockTickFunction( 468 TimeTicks::TickFunctionType TimeTicks::SetMockTickFunction(
467 TickFunctionType ticker) { 469 TickFunctionType ticker) {
468 base::AutoLock locked(rollover_lock); 470 base::AutoLock locked(rollover_lock);
469 TickFunctionType old = tick_function; 471 TickFunctionType old = tick_function;
470 tick_function = ticker; 472 tick_function = ticker;
471 rollover_ms = 0; 473 rollover_ms = 0;
472 last_seen_now = 0; 474 last_seen_now = 0;
473 return old; 475 return old;
474 } 476 }
475 477
476 // static 478 // static
477 bool TimeTicks::SetNowIsHighResNowIfSupported() {
478 if (!CPUReliablySupportsHighResTime()) {
479 return false;
480 }
481
482 now_function = HighResNowWrapper;
483 return true;
484 }
485
486 // static
487 TimeTicks TimeTicks::Now() { 479 TimeTicks TimeTicks::Now() {
488 return TimeTicks() + now_function(); 480 return TimeTicks() + now_function();
489 } 481 }
490 482
491 // static 483 // static
492 TimeTicks TimeTicks::HighResNow() { 484 TimeTicks TimeTicks::HighResNow() {
493 return TimeTicks() + HighResNowWrapper(); 485 return TimeTicks() + HighResNowWrapper();
494 } 486 }
495 487
496 // static 488 // static
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after
531 return TimeTicks() + TimeDelta::FromMilliseconds(timeGetTime()); 523 return TimeTicks() + TimeDelta::FromMilliseconds(timeGetTime());
532 } 524 }
533 } 525 }
534 526
535 // TimeDelta ------------------------------------------------------------------ 527 // TimeDelta ------------------------------------------------------------------
536 528
537 // static 529 // static
538 TimeDelta TimeDelta::FromQPCValue(LONGLONG qpc_value) { 530 TimeDelta TimeDelta::FromQPCValue(LONGLONG qpc_value) {
539 return TimeDelta(GetHighResNowSingleton()->QPCValueToMicroseconds(qpc_value)); 531 return TimeDelta(GetHighResNowSingleton()->QPCValueToMicroseconds(qpc_value));
540 } 532 }
OLDNEW
« no previous file with comments | « base/time/time.h ('k') | chrome/browser/chrome_browser_main_win.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698