Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(50)

Side by Side Diff: content/browser/power_profiler/power_profiler_service.cc

Issue 140583003: Chrome power profiler service (Closed) Base URL: http://git.chromium.org/chromium/src.git@master
Patch Set: Created 6 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(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 // Section 3.1 of http://software.intel.com/en-us/blogs/2013/10/03/using-the-int el-power-gadget-api-on-windows
17 const int kDefaultSamplePeriodMs = 50;
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 13:13:40 nit: if (!...get()) return ...
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;
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());
107 DCHECK(status_ == PROFILING);
108
109 // Get data and notify.
110 PowerEventVector events = data_provider_->GetData();
111 if (events.size() != 0) {
112 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, base::Bind(
113 &PowerProfilerService::Notify, base::Unretained(this), events));
114 }
115 }
116
117 } // namespace content
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698