OLD | NEW |
---|---|
(Empty) | |
1 // Copyright 2013 The Chromium Authors. All rights reserved. | |
2 // Use of this source code is governed by a BSD-style license that can be | |
3 // found in the LICENSE file. | |
4 | |
5 #include "content/browser/power_profiler/power_profiler_service.h" | |
6 | |
7 #include "base/message_loop/message_loop.h" | |
8 #include "content/browser/power_profiler/power_profiler_helper_factory.h" | |
9 #include "content/public/browser/browser_thread.h" | |
10 | |
11 namespace content { | |
12 | |
13 static PowerProfilerService* g_power_profiler_service = NULL; | |
14 | |
15 PowerProfilerService::PowerProfilerService() | |
16 : delay_(0) | |
17 , status_(UNINITIALIZED) { | |
18 DCHECK(!g_power_profiler_service); | |
19 g_power_profiler_service = this; | |
20 | |
21 profiler_helper_ = PowerProfilerHelperFactory::Create(); | |
qsr
2014/01/17 11:50:14
You should move the initialize to the creation. Th
| |
22 | |
23 if (profiler_helper_->Initialize()) { | |
24 status_ = INITIALIZED; | |
25 InitializeDelays(); | |
26 ResetAccumulationEnergies(); | |
27 } | |
28 DCHECK(base::MessageLoop::current()); | |
29 query_power_timer_.reset(new base::OneShotTimer<PowerProfilerService>()); | |
30 } | |
31 | |
32 PowerProfilerService::~PowerProfilerService() { | |
33 DCHECK_EQ(this, g_power_profiler_service); | |
34 g_power_profiler_service = NULL; | |
35 if (query_power_timer_->IsRunning()) | |
36 query_power_timer_->Stop(); | |
37 } | |
38 | |
39 void PowerProfilerService::ResetAccumulationEnergies() { | |
40 accumulated_enegries_[PowerEvent::SOC_PACKAGE] = 0.0; | |
41 accumulated_enegries_[PowerEvent::CPU] = 0.0; | |
42 accumulated_enegries_[PowerEvent::GPU] = 0.0; | |
43 } | |
44 | |
45 void PowerProfilerService::AccumulateEnergies(double* energies, int count) { | |
46 if (count <= 0 || count > PowerEvent::ID_COUNT) | |
47 return; | |
48 | |
49 accumulated_enegries_[PowerEvent::SOC_PACKAGE] += | |
50 energies[PowerEvent::SOC_PACKAGE]; | |
51 accumulated_enegries_[PowerEvent::CPU] += energies[PowerEvent::CPU]; | |
52 accumulated_enegries_[PowerEvent::GPU] += energies[PowerEvent::GPU]; | |
53 } | |
54 | |
55 void PowerProfilerService::InitializeDelays() { | |
56 // FIXME @Pan, these data should be filled according to backend's capability | |
57 delays_[PowerProfilerHost::LOW] = 4000; | |
58 delays_[PowerProfilerHost::NORMAL] = 200; | |
59 delays_[PowerProfilerHost::HIGH] = 50; | |
60 } | |
61 | |
62 // static | |
63 PowerProfilerService* PowerProfilerService::Get() { | |
64 return g_power_profiler_service; | |
65 } | |
66 | |
67 PowerProfilerService::Status PowerProfilerService::status() { | |
68 return status_; | |
69 } | |
70 | |
71 void PowerProfilerService::AddObserver(PowerProfilerHost* obs) { | |
72 if (UNINITIALIZED == status_) | |
73 return; | |
74 | |
75 observers_.insert(obs); | |
76 UpdateResolution(); | |
77 if (observers_.size() == 1) | |
78 Start(); | |
79 } | |
80 | |
81 void PowerProfilerService::RemoveObserver(PowerProfilerHost* obs) { | |
82 observers_.erase(obs); | |
83 UpdateResolution(); | |
84 if (observers_.empty()) | |
85 Stop(); | |
86 } | |
87 | |
88 void PowerProfilerService::UpdateResolution() { | |
89 if (UNINITIALIZED == status_) | |
90 return; | |
91 | |
92 PowerProfilerHost::Resolution resolution = PowerProfilerHost::LOW; | |
93 std::set<PowerProfilerHost*>::iterator it = observers_.begin(); | |
94 for (; it != observers_.end(); ++it) | |
95 if ((*it)->GetResolution() > resolution) | |
96 resolution = (*it)->GetResolution(); | |
97 | |
98 // if delay_ will be reset, reschedule the timer immediately | |
99 if (delay_ != delays_[resolution]) { | |
100 delay_ = delays_[resolution]; | |
101 | |
102 if (query_power_timer_->IsRunning()) { | |
103 query_power_timer_->Stop(); | |
104 query_power_timer_->Start(FROM_HERE, | |
105 base::TimeDelta::FromMilliseconds(delay_), this, | |
106 &PowerProfilerService::ProcessData); | |
107 } | |
108 } | |
109 } | |
110 | |
111 void PowerProfilerService::Snapshot() { | |
112 if (PROFILING != status_) | |
113 return; | |
114 | |
115 // Get Data | |
116 PowerEvent buffer[PowerEvent::ID_COUNT]; | |
117 double enegries[3] = {0.0}; | |
qsr
2014/01/17 11:50:14
Is this 3 PowerEvent::ID_COUNT?
| |
118 size_t count = profiler_helper_->GetData(buffer, enegries, | |
119 PowerEvent::ID_COUNT); | |
120 | |
121 AccumulateEnergies(enegries, PowerEvent::ID_COUNT); | |
122 Notify(buffer, count); | |
123 } | |
124 | |
125 double PowerProfilerService::GetEnergy(PowerEvent::Type type) { | |
126 return accumulated_enegries_[type]; | |
127 } | |
128 | |
129 void PowerProfilerService::Start() { | |
130 #if defined(ENABLE_POWER_PROFILER) | |
131 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::POWER_PROFILER)); | |
132 assert (!query_power_timer_->IsRunning() && delay_); | |
133 | |
134 // once there is a new start profiler, reset energies | |
135 ResetAccumulationEnergies(); | |
136 // send out power events and refresh energies immediately. | |
137 status_ = PROFILING; | |
138 ProcessData(); | |
139 #endif // defined(ENABLE_POWER_PROFILER) | |
140 } | |
141 | |
142 void PowerProfilerService::Stop() { | |
143 // stop timer, set status to INITIALIZED | |
144 if (PROFILING == status_) { | |
145 query_power_timer_->Stop(); | |
146 status_ = INITIALIZED; | |
147 } | |
148 } | |
149 | |
150 void PowerProfilerService::Notify(PowerEvent* buffer, int count) { | |
151 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::POWER_PROFILER)); | |
152 std::set<PowerProfilerHost*>::iterator it = observers_.begin(); | |
153 for (; it != observers_.end(); ++it) | |
154 (*it)->Send(buffer, count); | |
155 } | |
156 | |
157 void PowerProfilerService::ProcessData() { | |
158 if (PROFILING != status_) | |
159 return; | |
160 | |
161 // Get Data | |
162 PowerEvent buffer[PowerEvent::ID_COUNT]; | |
163 double enegries[3] = {0.0}; | |
164 size_t count = profiler_helper_->GetData(buffer, enegries, | |
165 PowerEvent::ID_COUNT); | |
166 AccumulateEnergies(enegries, PowerEvent::ID_COUNT); | |
167 | |
168 Notify(buffer, count); | |
169 | |
170 // restart Timer | |
171 #if defined(ENABLE_POWER_PROFILER) | |
172 query_power_timer_->Start(FROM_HERE, | |
173 base::TimeDelta::FromMilliseconds(delay_), this, | |
174 &PowerProfilerService::ProcessData); | |
175 #endif // defined(ENABLE_POWER_PROFILER | |
176 } | |
177 | |
178 } // namespace base | |
OLD | NEW |