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 timer_(FROM_HERE, |
33 kUsageSampleInterval, | |
34 Bind(&ReportHighResolutionTimerUsage), | |
35 true /* is_repeating */) { | |
gab
2017/07/01 06:55:30
Use base::RepeatingTimer?
| |
36 PowerMonitor* power_monitor = PowerMonitor::Get(); | |
15 DCHECK(power_monitor != NULL); | 37 DCHECK(power_monitor != NULL); |
16 power_monitor->AddObserver(this); | 38 power_monitor->AddObserver(this); |
17 UseHiResClock(!power_monitor->IsOnBatteryPower()); | 39 UseHiResClock(!power_monitor->IsOnBatteryPower()); |
40 | |
41 // Start polling the high resolution timer usage. | |
42 Time::ResetHighResolutionTimerUsage(); | |
43 timer_.Reset(); | |
18 } | 44 } |
19 | 45 |
20 HighResolutionTimerManager::~HighResolutionTimerManager() { | 46 HighResolutionTimerManager::~HighResolutionTimerManager() { |
21 base::PowerMonitor::Get()->RemoveObserver(this); | 47 PowerMonitor::Get()->RemoveObserver(this); |
22 UseHiResClock(false); | 48 UseHiResClock(false); |
23 } | 49 } |
24 | 50 |
25 void HighResolutionTimerManager::OnPowerStateChange(bool on_battery_power) { | 51 void HighResolutionTimerManager::OnPowerStateChange(bool on_battery_power) { |
26 UseHiResClock(!on_battery_power); | 52 UseHiResClock(!on_battery_power); |
27 } | 53 } |
28 | 54 |
55 void HighResolutionTimerManager::OnSuspend() { | |
56 // Stop polling the usage to avoid including the standby time. | |
57 timer_.Stop(); | |
58 } | |
59 | |
60 void HighResolutionTimerManager::OnResume() { | |
61 // Resume polling the usage. | |
62 Time::ResetHighResolutionTimerUsage(); | |
63 timer_.Reset(); | |
64 } | |
65 | |
29 void HighResolutionTimerManager::UseHiResClock(bool use) { | 66 void HighResolutionTimerManager::UseHiResClock(bool use) { |
30 if (use == hi_res_clock_available_) | 67 if (use == hi_res_clock_available_) |
31 return; | 68 return; |
32 hi_res_clock_available_ = use; | 69 hi_res_clock_available_ = use; |
33 base::Time::EnableHighResolutionTimer(use); | 70 Time::EnableHighResolutionTimer(use); |
34 } | 71 } |
35 | 72 |
36 } // namespace base | 73 } // namespace base |
OLD | NEW |