Index: content/browser/power_profiler/power_profiler_service.cc |
diff --git a/content/browser/power_profiler/power_profiler_service.cc b/content/browser/power_profiler/power_profiler_service.cc |
new file mode 100644 |
index 0000000000000000000000000000000000000000..20857b042f83447f02c48302bd107e4bc374f821 |
--- /dev/null |
+++ b/content/browser/power_profiler/power_profiler_service.cc |
@@ -0,0 +1,178 @@ |
+// Copyright 2013 The Chromium Authors. All rights reserved. |
+// Use of this source code is governed by a BSD-style license that can be |
+// found in the LICENSE file. |
+ |
+#include "content/browser/power_profiler/power_profiler_service.h" |
+ |
+#include "base/message_loop/message_loop.h" |
+#include "content/browser/power_profiler/power_profiler_helper_factory.h" |
+#include "content/public/browser/browser_thread.h" |
+ |
+namespace content { |
+ |
+static PowerProfilerService* g_power_profiler_service = NULL; |
+ |
+PowerProfilerService::PowerProfilerService() |
+ : delay_(0) |
+ , status_(UNINITIALIZED) { |
+ DCHECK(!g_power_profiler_service); |
+ g_power_profiler_service = this; |
+ |
+ profiler_helper_ = PowerProfilerHelperFactory::Create(); |
qsr
2014/01/17 11:50:14
You should move the initialize to the creation. Th
|
+ |
+ if (profiler_helper_->Initialize()) { |
+ status_ = INITIALIZED; |
+ InitializeDelays(); |
+ ResetAccumulationEnergies(); |
+ } |
+ DCHECK(base::MessageLoop::current()); |
+ query_power_timer_.reset(new base::OneShotTimer<PowerProfilerService>()); |
+} |
+ |
+PowerProfilerService::~PowerProfilerService() { |
+ DCHECK_EQ(this, g_power_profiler_service); |
+ g_power_profiler_service = NULL; |
+ if (query_power_timer_->IsRunning()) |
+ query_power_timer_->Stop(); |
+} |
+ |
+void PowerProfilerService::ResetAccumulationEnergies() { |
+ accumulated_enegries_[PowerEvent::SOC_PACKAGE] = 0.0; |
+ accumulated_enegries_[PowerEvent::CPU] = 0.0; |
+ accumulated_enegries_[PowerEvent::GPU] = 0.0; |
+} |
+ |
+void PowerProfilerService::AccumulateEnergies(double* energies, int count) { |
+ if (count <= 0 || count > PowerEvent::ID_COUNT) |
+ return; |
+ |
+ accumulated_enegries_[PowerEvent::SOC_PACKAGE] += |
+ energies[PowerEvent::SOC_PACKAGE]; |
+ accumulated_enegries_[PowerEvent::CPU] += energies[PowerEvent::CPU]; |
+ accumulated_enegries_[PowerEvent::GPU] += energies[PowerEvent::GPU]; |
+} |
+ |
+void PowerProfilerService::InitializeDelays() { |
+ // FIXME @Pan, these data should be filled according to backend's capability |
+ delays_[PowerProfilerHost::LOW] = 4000; |
+ delays_[PowerProfilerHost::NORMAL] = 200; |
+ delays_[PowerProfilerHost::HIGH] = 50; |
+} |
+ |
+// static |
+PowerProfilerService* PowerProfilerService::Get() { |
+ return g_power_profiler_service; |
+} |
+ |
+PowerProfilerService::Status PowerProfilerService::status() { |
+ return status_; |
+} |
+ |
+void PowerProfilerService::AddObserver(PowerProfilerHost* obs) { |
+ if (UNINITIALIZED == status_) |
+ return; |
+ |
+ observers_.insert(obs); |
+ UpdateResolution(); |
+ if (observers_.size() == 1) |
+ Start(); |
+} |
+ |
+void PowerProfilerService::RemoveObserver(PowerProfilerHost* obs) { |
+ observers_.erase(obs); |
+ UpdateResolution(); |
+ if (observers_.empty()) |
+ Stop(); |
+} |
+ |
+void PowerProfilerService::UpdateResolution() { |
+ if (UNINITIALIZED == status_) |
+ return; |
+ |
+ PowerProfilerHost::Resolution resolution = PowerProfilerHost::LOW; |
+ std::set<PowerProfilerHost*>::iterator it = observers_.begin(); |
+ for (; it != observers_.end(); ++it) |
+ if ((*it)->GetResolution() > resolution) |
+ resolution = (*it)->GetResolution(); |
+ |
+ // if delay_ will be reset, reschedule the timer immediately |
+ if (delay_ != delays_[resolution]) { |
+ delay_ = delays_[resolution]; |
+ |
+ if (query_power_timer_->IsRunning()) { |
+ query_power_timer_->Stop(); |
+ query_power_timer_->Start(FROM_HERE, |
+ base::TimeDelta::FromMilliseconds(delay_), this, |
+ &PowerProfilerService::ProcessData); |
+ } |
+ } |
+} |
+ |
+void PowerProfilerService::Snapshot() { |
+ if (PROFILING != status_) |
+ return; |
+ |
+ // Get Data |
+ PowerEvent buffer[PowerEvent::ID_COUNT]; |
+ double enegries[3] = {0.0}; |
qsr
2014/01/17 11:50:14
Is this 3 PowerEvent::ID_COUNT?
|
+ size_t count = profiler_helper_->GetData(buffer, enegries, |
+ PowerEvent::ID_COUNT); |
+ |
+ AccumulateEnergies(enegries, PowerEvent::ID_COUNT); |
+ Notify(buffer, count); |
+} |
+ |
+double PowerProfilerService::GetEnergy(PowerEvent::Type type) { |
+ return accumulated_enegries_[type]; |
+} |
+ |
+void PowerProfilerService::Start() { |
+#if defined(ENABLE_POWER_PROFILER) |
+ DCHECK(BrowserThread::CurrentlyOn(BrowserThread::POWER_PROFILER)); |
+ assert (!query_power_timer_->IsRunning() && delay_); |
+ |
+ // once there is a new start profiler, reset energies |
+ ResetAccumulationEnergies(); |
+ // send out power events and refresh energies immediately. |
+ status_ = PROFILING; |
+ ProcessData(); |
+#endif // defined(ENABLE_POWER_PROFILER) |
+} |
+ |
+void PowerProfilerService::Stop() { |
+ // stop timer, set status to INITIALIZED |
+ if (PROFILING == status_) { |
+ query_power_timer_->Stop(); |
+ status_ = INITIALIZED; |
+ } |
+} |
+ |
+void PowerProfilerService::Notify(PowerEvent* buffer, int count) { |
+ DCHECK(BrowserThread::CurrentlyOn(BrowserThread::POWER_PROFILER)); |
+ std::set<PowerProfilerHost*>::iterator it = observers_.begin(); |
+ for (; it != observers_.end(); ++it) |
+ (*it)->Send(buffer, count); |
+} |
+ |
+void PowerProfilerService::ProcessData() { |
+ if (PROFILING != status_) |
+ return; |
+ |
+ // Get Data |
+ PowerEvent buffer[PowerEvent::ID_COUNT]; |
+ double enegries[3] = {0.0}; |
+ size_t count = profiler_helper_->GetData(buffer, enegries, |
+ PowerEvent::ID_COUNT); |
+ AccumulateEnergies(enegries, PowerEvent::ID_COUNT); |
+ |
+ Notify(buffer, count); |
+ |
+ // restart Timer |
+#if defined(ENABLE_POWER_PROFILER) |
+ query_power_timer_->Start(FROM_HERE, |
+ base::TimeDelta::FromMilliseconds(delay_), this, |
+ &PowerProfilerService::ProcessData); |
+#endif // defined(ENABLE_POWER_PROFILER |
+} |
+ |
+} // namespace base |