OLD | NEW |
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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 #include "base/timer/hi_res_timer_manager.h" | 5 #include "base/timer/hi_res_timer_manager.h" |
6 | 6 |
| 7 #include <algorithm> |
| 8 |
| 9 #include "base/atomicops.h" |
| 10 #include "base/metrics/histogram_macros.h" |
7 #include "base/power_monitor/power_monitor.h" | 11 #include "base/power_monitor/power_monitor.h" |
| 12 #include "base/task_scheduler/post_task.h" |
8 #include "base/time/time.h" | 13 #include "base/time/time.h" |
9 | 14 |
10 namespace base { | 15 namespace base { |
11 | 16 |
| 17 namespace { |
| 18 |
| 19 constexpr TimeDelta kUsageSampleInterval = TimeDelta::FromMinutes(10); |
| 20 |
| 21 void ReportHighResolutionTimerUsage() { |
| 22 UMA_HISTOGRAM_PERCENTAGE("Windows.HighResolutionTimerUsage", |
| 23 Time::GetHighResolutionTimerUsage()); |
| 24 // Reset usage for the next interval. |
| 25 Time::ResetHighResolutionTimerUsage(); |
| 26 } |
| 27 |
| 28 } // namespace |
| 29 |
12 HighResolutionTimerManager::HighResolutionTimerManager() | 30 HighResolutionTimerManager::HighResolutionTimerManager() |
13 : hi_res_clock_available_(false) { | 31 : hi_res_clock_available_(false) { |
14 base::PowerMonitor* power_monitor = base::PowerMonitor::Get(); | 32 PowerMonitor* power_monitor = PowerMonitor::Get(); |
15 DCHECK(power_monitor != NULL); | 33 DCHECK(power_monitor != NULL); |
16 power_monitor->AddObserver(this); | 34 power_monitor->AddObserver(this); |
17 UseHiResClock(!power_monitor->IsOnBatteryPower()); | 35 UseHiResClock(!power_monitor->IsOnBatteryPower()); |
| 36 |
| 37 // Start polling the high resolution timer usage. |
| 38 Time::ResetHighResolutionTimerUsage(); |
| 39 timer_.Start(FROM_HERE, kUsageSampleInterval, |
| 40 Bind(&ReportHighResolutionTimerUsage)); |
18 } | 41 } |
19 | 42 |
20 HighResolutionTimerManager::~HighResolutionTimerManager() { | 43 HighResolutionTimerManager::~HighResolutionTimerManager() { |
21 base::PowerMonitor::Get()->RemoveObserver(this); | 44 PowerMonitor::Get()->RemoveObserver(this); |
22 UseHiResClock(false); | 45 UseHiResClock(false); |
23 } | 46 } |
24 | 47 |
25 void HighResolutionTimerManager::OnPowerStateChange(bool on_battery_power) { | 48 void HighResolutionTimerManager::OnPowerStateChange(bool on_battery_power) { |
26 UseHiResClock(!on_battery_power); | 49 UseHiResClock(!on_battery_power); |
27 } | 50 } |
28 | 51 |
| 52 void HighResolutionTimerManager::OnSuspend() { |
| 53 // Stop polling the usage to avoid including the standby time. |
| 54 timer_.Stop(); |
| 55 } |
| 56 |
| 57 void HighResolutionTimerManager::OnResume() { |
| 58 // Resume polling the usage. |
| 59 Time::ResetHighResolutionTimerUsage(); |
| 60 timer_.Reset(); |
| 61 } |
| 62 |
29 void HighResolutionTimerManager::UseHiResClock(bool use) { | 63 void HighResolutionTimerManager::UseHiResClock(bool use) { |
30 if (use == hi_res_clock_available_) | 64 if (use == hi_res_clock_available_) |
31 return; | 65 return; |
32 hi_res_clock_available_ = use; | 66 hi_res_clock_available_ = use; |
33 base::Time::EnableHighResolutionTimer(use); | 67 Time::EnableHighResolutionTimer(use); |
34 } | 68 } |
35 | 69 |
36 } // namespace base | 70 } // namespace base |
OLD | NEW |