Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 // Copyright 2014 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/bind.h" | |
| 8 #include "base/message_loop/message_loop.h" | |
| 9 #include "base/threading/sequenced_worker_pool.h" | |
| 10 #include "content/browser/power_profiler/power_data_provider_factory.h" | |
| 11 #include "content/public/browser/browser_thread.h" | |
| 12 | |
| 13 namespace { | |
| 14 | |
| 15 // Default sampling period, as recommended by Intel Power Gadget. | |
| 16 // http://software.intel.com/en-us/blogs/2013/10/03/using-the-intel-power-gadget -api-on-windows | |
|
jeremy
2014/02/18 11:35:33
Excellent, thanks! I might even say that it's sect
Pan
2014/02/18 12:58:39
Done.
| |
| 17 const int kDefaultSamplePeriodMS = 50; | |
|
jeremy
2014/02/18 11:35:33
Sorry, this should end with Ms not MS, my mistake.
Pan
2014/02/18 12:58:39
Done.
| |
| 18 | |
| 19 } // namespace | |
| 20 | |
| 21 namespace content { | |
| 22 | |
| 23 PowerProfilerService::PowerProfilerService() | |
| 24 : status_(UNINITIALIZED), | |
| 25 sample_period_(base::TimeDelta::FromMilliseconds(kDefaultSamplePeriodMS)), | |
| 26 data_provider_(PowerDataProviderFactory::Create()) { | |
| 27 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | |
| 28 | |
| 29 if (data_provider_.get()) { | |
|
jeremy
2014/02/18 11:35:33
I don't think you need this if() , that would mean
qsr
2014/02/18 11:38:09
This is in fact needed. The factory is not guarant
Pan
2014/02/18 12:58:39
right, needed here.
| |
| 30 status_ = INITIALIZED; | |
| 31 task_runner_ = BrowserThread::GetBlockingPool()->GetSequencedTaskRunner( | |
| 32 BrowserThread::GetBlockingPool()->GetSequenceToken()); | |
| 33 } | |
| 34 } | |
| 35 | |
| 36 PowerProfilerService::PowerProfilerService( | |
| 37 scoped_ptr<PowerDataProvider> provider, | |
| 38 scoped_refptr<base::TaskRunner> task_runner, | |
| 39 const base::TimeDelta& sample_period) | |
| 40 : task_runner_(task_runner), | |
| 41 status_(UNINITIALIZED), | |
| 42 sample_period_(sample_period), | |
| 43 data_provider_(provider.Pass()) { | |
| 44 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | |
| 45 | |
| 46 if (data_provider_.get()) | |
| 47 status_ = INITIALIZED; | |
| 48 } | |
| 49 | |
| 50 PowerProfilerService::~PowerProfilerService() { | |
| 51 } | |
| 52 | |
| 53 bool PowerProfilerService::IsAvailable() { | |
| 54 return status_ != UNINITIALIZED; | |
| 55 } | |
| 56 | |
| 57 PowerProfilerService* PowerProfilerService::GetInstance() { | |
| 58 return Singleton<PowerProfilerService>::get(); | |
| 59 } | |
| 60 | |
| 61 void PowerProfilerService::AddObserver(PowerProfilerObserver* observer) { | |
| 62 if (status_ == UNINITIALIZED) | |
| 63 return; | |
|
jeremy
2014/02/18 11:35:33
Should these ifs be DCHECKS or removed? My concern
Pan
2014/02/18 12:58:39
status_ should be UNINTIALIZED if no provider.
| |
| 64 | |
| 65 observers_.AddObserver(observer); | |
| 66 if (status_ != PROFILING) | |
| 67 Start(); | |
| 68 } | |
| 69 | |
| 70 void PowerProfilerService::RemoveObserver(PowerProfilerObserver* observer) { | |
| 71 observers_.RemoveObserver(observer); | |
| 72 | |
| 73 if (!observers_.might_have_observers()) | |
| 74 Stop(); | |
| 75 } | |
| 76 | |
| 77 void PowerProfilerService::Start() { | |
| 78 DCHECK(status_ == INITIALIZED); | |
| 79 status_ = PROFILING; | |
| 80 | |
| 81 // Send out power events immediately. | |
| 82 QueryData(); | |
| 83 | |
| 84 query_power_timer_.Start(FROM_HERE, | |
| 85 sample_period_, this, &PowerProfilerService::QueryData); | |
| 86 } | |
| 87 | |
| 88 void PowerProfilerService::Stop() { | |
| 89 DCHECK(status_ == PROFILING); | |
| 90 | |
| 91 query_power_timer_.Stop(); | |
| 92 status_ = INITIALIZED; | |
| 93 } | |
| 94 | |
| 95 void PowerProfilerService::QueryData() { | |
| 96 task_runner_->PostTask( | |
| 97 FROM_HERE, base::Bind(&PowerProfilerService::QueryDataOnTaskRunner, | |
| 98 base::Unretained(this))); | |
| 99 } | |
| 100 | |
| 101 void PowerProfilerService::Notify(const PowerEventVector& events) { | |
| 102 FOR_EACH_OBSERVER(PowerProfilerObserver, observers_, OnPowerEvent(events)); | |
| 103 } | |
| 104 | |
| 105 void PowerProfilerService::QueryDataOnTaskRunner() { | |
| 106 DCHECK(task_runner_->RunsTasksOnCurrentThread() && status_ == PROFILING); | |
|
jeremy
2014/02/18 11:35:33
Would recommend breaking into 2 DCHECK() statement
Pan
2014/02/18 12:58:39
Done.
| |
| 107 | |
| 108 // Get data and notify. | |
| 109 PowerEventVector events = data_provider_->GetData(); | |
| 110 if (events.size() != 0) { | |
| 111 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, base::Bind( | |
| 112 &PowerProfilerService::Notify, base::Unretained(this), events)); | |
| 113 } | |
| 114 } | |
| 115 | |
| 116 } // namespace content | |
| OLD | NEW |