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

Side by Side Diff: chrome/browser/extensions/api/system_info_cpu/cpu_info_provider.cc

Issue 10905171: Add systemInfo.cpu.onUpdated event implementation. (Closed) Base URL: http://git.chromium.org/chromium/src.git@master
Patch Set: Created 8 years, 3 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
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2012 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 "chrome/browser/extensions/api/system_info_cpu/cpu_info_provider.h" 5 #include "chrome/browser/extensions/api/system_info_cpu/cpu_info_provider.h"
6 6
7 #include "base/sys_info.h" 7 #include "base/sys_info.h"
8 #include "content/public/browser/browser_thread.h"
8 9
9 namespace extensions { 10 namespace extensions {
10 11
11 using api::experimental_system_info_cpu::CpuInfo; 12 using api::experimental_system_info_cpu::CpuInfo;
13 using api::experimental_system_info_cpu::CpuUpdateInfo;
14 using content::BrowserThread;
15
16 // Default sampling interval is 1000ms.
17 const unsigned int kDefaultSamplingIntervalMs = 1000;
18
19 CpuInfoProvider::CpuInfoProvider()
20 : is_sampling_started_(false),
21 sampling_timer_(NULL),
22 sampling_interval_(kDefaultSamplingIntervalMs) {
23 }
24
25 CpuInfoProvider::~CpuInfoProvider() {
26 DCHECK(!sampling_timer_) << "Fogot to call StopSampling?";
Mihai Parparita -not on Chrome 2012/09/16 06:23:05 "StartSampling call without a corresponding StopSa
Mihai Parparita -not on Chrome 2012/09/16 06:23:05 Also, this will still leak the sampling timer in n
27 }
12 28
13 bool CpuInfoProvider::QueryInfo(CpuInfo* info) { 29 bool CpuInfoProvider::QueryInfo(CpuInfo* info) {
14 if (info == NULL) 30 if (info == NULL)
15 return false; 31 return false;
16 32
17 info->num_of_processors = base::SysInfo::NumberOfProcessors(); 33 info->num_of_processors = base::SysInfo::NumberOfProcessors();
18 info->arch_name = base::SysInfo::CPUArchitecture(); 34 info->arch_name = base::SysInfo::CPUArchitecture();
19 info->model_name = base::SysInfo::CPUModelName(); 35 info->model_name = base::SysInfo::CPUModelName();
20 return true; 36 return true;
21 } 37 }
22 38
23 bool CpuInfoProvider::StartSampling(const QueryCpuTimeCallback& callback) { 39 bool CpuInfoProvider::StartSampling(const SamplingCallback& callback) {
24 // TODO(hongbo): Implement sampling functionality for event router. 40 if (!BrowserThread::CurrentlyOn(BrowserThread::FILE)) {
25 return false; 41 return BrowserThread::PostTask(
42 BrowserThread::FILE,
43 FROM_HERE,
44 base::Bind(base::IgnoreResult(&CpuInfoProvider::StartSampling),
45 base::Unretained(this), callback));
46 }
47
48 // We are now on the FILE thread now.
49 if (is_sampling_started_) return true;
50
51 if (!QueryCpuTimePerProcessor(&sampling_value_))
52 return false;
53
54 is_sampling_started_ = true;
55 callback_ = callback;
56
57 DCHECK(!sampling_timer_) << "The sampling timer is leaked. Did you forgot "
58 "calling StopSampling?";
59
60 // Should be deleted in StopSampling.
61 sampling_timer_ = new base::RepeatingTimer<CpuInfoProvider>();
62
63 sampling_timer_->Start(FROM_HERE,
64 base::TimeDelta::FromMilliseconds(sampling_interval_),
65 this, &CpuInfoProvider::DoStartSampling);
Mihai Parparita -not on Chrome 2012/09/16 06:23:05 DoStartSampling seems like a misnomer, since it is
66
67 return true;
26 } 68 }
27 69
28 bool CpuInfoProvider::StopSampling() { 70 bool CpuInfoProvider::StopSampling() {
29 // TODO(hongbo): Implement sampling functionality for event router. 71 if (!BrowserThread::CurrentlyOn(BrowserThread::FILE)) {
30 return false; 72 return BrowserThread::PostTask(
73 BrowserThread::FILE,
74 FROM_HERE,
75 base::Bind(base::IgnoreResult(&CpuInfoProvider::StopSampling),
76 base::Unretained(this)));
77 }
78 // We are now on the FILE thread now.
79 if (!is_sampling_started_) return false;
80 delete sampling_timer_;
81 sampling_timer_ = NULL;
82 sampling_value_.clear();
83 is_sampling_started_ = false;
84 return true;
31 } 85 }
32 86
33 // static 87 // static
34 CpuInfoProvider* CpuInfoProvider::Get() { 88 CpuInfoProvider* CpuInfoProvider::Get() {
35 return CpuInfoProvider::GetInstance<CpuInfoProvider>(); 89 return CpuInfoProvider::GetInstance<CpuInfoProvider>();
36 } 90 }
37 91
92 void CpuInfoProvider::DoStartSampling() {
93 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE));
94
95 std::vector<CpuTime> new_sampling_value;
96 if (!QueryCpuTimePerProcessor(&new_sampling_value))
97 return;
98
99 // Calculate the CPU usage information from the two sampling values.
100 double total_usage = 0;
101 scoped_ptr<CpuUpdateInfo> info(new CpuUpdateInfo());
102
103 for (size_t i = 0; i < new_sampling_value.size(); ++i) {
104 double total_time =
105 (new_sampling_value[i].user - sampling_value_[i].user) +
106 (new_sampling_value[i].kernel - sampling_value_[i].kernel) +
107 (new_sampling_value[i].idle - sampling_value_[i].idle);
108 double idle_time = new_sampling_value[i].idle - sampling_value_[i].idle;
109
110 double usage = 0;
111 if (total_time != 0)
112 usage = (100 - idle_time * 100 / total_time);
113 info->usage_per_processor.push_back(usage);
114 total_usage += usage;
115 }
116
117 info->average_usage = total_usage / new_sampling_value.size();
118
119 if (!callback_.is_null())
120 callback_.Run(info.Pass());
121 }
122
38 } // namespace extensions 123 } // namespace extensions
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698