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 subtle::Atomic32 g_report_id = 0; | |
22 | |
23 // Forward declaration. | |
24 void MaybeReportHighResolutionTimerUsageAndReschedule(int report_id); | |
25 | |
26 void ScheduleHighResolutionTimerUsageReport() { | |
27 Time::ResetHighResolutionTimerUsage(); | |
28 int next_report_id = subtle::NoBarrier_AtomicIncrement(&g_report_id, 1); | |
29 PostDelayedTaskWithTraits( | |
30 FROM_HERE, | |
31 {TaskPriority::BACKGROUND, TaskShutdownBehavior::CONTINUE_ON_SHUTDOWN}, | |
32 Bind(&MaybeReportHighResolutionTimerUsageAndReschedule, next_report_id), | |
33 kUsageSampleInterval); | |
34 } | |
35 | |
36 void ReportHighResolutionTimerUsage() { | |
37 UMA_HISTOGRAM_PERCENTAGE("Windows.HighResolutionTimerUsage", | |
38 Time::GetHighResolutionTimerUsage()); | |
39 } | |
40 | |
41 void MaybeReportHighResolutionTimerUsageAndReschedule(int report_id) { | |
42 if (report_id != subtle::NoBarrier_Load(&g_report_id)) | |
43 return; | |
44 | |
45 ReportHighResolutionTimerUsage(); | |
46 ScheduleHighResolutionTimerUsageReport(); | |
47 } | |
48 | |
49 } // namespace | |
50 | |
12 HighResolutionTimerManager::HighResolutionTimerManager() | 51 HighResolutionTimerManager::HighResolutionTimerManager() |
13 : hi_res_clock_available_(false) { | 52 : hi_res_clock_available_(false) { |
14 base::PowerMonitor* power_monitor = base::PowerMonitor::Get(); | 53 PowerMonitor* power_monitor = PowerMonitor::Get(); |
15 DCHECK(power_monitor != NULL); | 54 DCHECK(power_monitor != NULL); |
16 power_monitor->AddObserver(this); | 55 power_monitor->AddObserver(this); |
17 UseHiResClock(!power_monitor->IsOnBatteryPower()); | 56 UseHiResClock(!power_monitor->IsOnBatteryPower()); |
57 ScheduleHighResolutionTimerUsageReport(); | |
18 } | 58 } |
19 | 59 |
20 HighResolutionTimerManager::~HighResolutionTimerManager() { | 60 HighResolutionTimerManager::~HighResolutionTimerManager() { |
21 base::PowerMonitor::Get()->RemoveObserver(this); | 61 PowerMonitor::Get()->RemoveObserver(this); |
22 UseHiResClock(false); | 62 UseHiResClock(false); |
23 } | 63 } |
24 | 64 |
25 void HighResolutionTimerManager::OnPowerStateChange(bool on_battery_power) { | 65 void HighResolutionTimerManager::OnPowerStateChange(bool on_battery_power) { |
26 UseHiResClock(!on_battery_power); | 66 UseHiResClock(!on_battery_power); |
27 } | 67 } |
28 | 68 |
69 void HighResolutionTimerManager::OnSuspend() { | |
70 // Skip to next report id to prevent a queued usage report from being logged | |
71 // on resume and including the standby time. | |
72 subtle::NoBarrier_AtomicIncrement(&g_report_id, 1); | |
73 } | |
74 | |
75 void HighResolutionTimerManager::OnResume() { | |
76 ScheduleHighResolutionTimerUsageReport(); | |
gab
2017/06/28 16:39:33
It's possible that the task fired from here runs a
stanisc
2017/06/28 22:28:35
report_id was supposed to protect from running con
gab
2017/07/01 06:55:30
FWIW atomics merely provide eventual consistency s
| |
77 } | |
78 | |
29 void HighResolutionTimerManager::UseHiResClock(bool use) { | 79 void HighResolutionTimerManager::UseHiResClock(bool use) { |
30 if (use == hi_res_clock_available_) | 80 if (use == hi_res_clock_available_) |
31 return; | 81 return; |
32 hi_res_clock_available_ = use; | 82 hi_res_clock_available_ = use; |
33 base::Time::EnableHighResolutionTimer(use); | 83 Time::EnableHighResolutionTimer(use); |
34 } | 84 } |
35 | 85 |
36 } // namespace base | 86 } // namespace base |
OLD | NEW |