OLD | NEW |
---|---|
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/performance_monitor/performance_monitor.h" | 5 #include "chrome/browser/performance_monitor/performance_monitor.h" |
6 | 6 |
7 #include <vector> | |
8 | |
7 #include "base/bind.h" | 9 #include "base/bind.h" |
8 #include "base/logging.h" | 10 #include "base/logging.h" |
9 #include "base/process_util.h" | 11 #include "base/stl_util.h" |
12 #include "base/string_number_conversions.h" | |
10 #include "base/threading/worker_pool.h" | 13 #include "base/threading/worker_pool.h" |
11 #include "base/time.h" | 14 #include "base/time.h" |
12 #include "chrome/browser/browser_shutdown.h" | 15 #include "chrome/browser/browser_shutdown.h" |
13 #include "chrome/browser/performance_monitor/constants.h" | 16 #include "chrome/browser/performance_monitor/constants.h" |
14 #include "chrome/browser/performance_monitor/database.h" | |
15 #include "chrome/browser/performance_monitor/performance_monitor_util.h" | 17 #include "chrome/browser/performance_monitor/performance_monitor_util.h" |
16 #include "chrome/browser/extensions/crx_installer.h" | 18 #include "chrome/browser/extensions/crx_installer.h" |
17 #include "chrome/common/chrome_notification_types.h" | 19 #include "chrome/common/chrome_notification_types.h" |
18 #include "chrome/common/chrome_version_info.h" | 20 #include "chrome/common/chrome_version_info.h" |
19 #include "chrome/common/extensions/extension.h" | 21 #include "chrome/common/extensions/extension.h" |
20 #include "chrome/common/extensions/extension_constants.h" | 22 #include "chrome/common/extensions/extension_constants.h" |
23 #include "chrome/test/base/chrome_process_util.h" | |
24 #include "content/public/browser/browser_child_process_host.h" | |
21 #include "content/public/browser/browser_thread.h" | 25 #include "content/public/browser/browser_thread.h" |
22 #include "content/public/browser/notification_service.h" | 26 #include "content/public/browser/notification_service.h" |
23 #include "content/public/browser/notification_types.h" | 27 #include "content/public/browser/notification_types.h" |
24 #include "content/public/browser/render_process_host.h" | 28 #include "content/public/browser/render_process_host.h" |
25 #include "content/public/browser/web_contents.h" | 29 #include "content/public/browser/web_contents.h" |
26 | 30 |
27 using content::BrowserThread; | 31 using content::BrowserThread; |
28 using extensions::Extension; | 32 using extensions::Extension; |
29 | 33 |
34 namespace { | |
35 const uint32 access_flags = base::kProcessAccessDuplicateHandle | | |
Devlin
2012/07/27 22:09:47
const uint32 kAccessFlags
mitchellwrosen
2012/07/30 21:50:40
Done.
| |
36 base::kProcessAccessQueryInformation | | |
37 base::kProcessAccessTerminate | | |
38 base::kProcessAccessWaitForTermination; | |
39 } // namespace | |
40 | |
30 namespace performance_monitor { | 41 namespace performance_monitor { |
31 | 42 |
32 PerformanceMonitor::PerformanceMonitor() : database_(NULL) { | 43 PerformanceMonitor::PerformanceMonitor() : database_(NULL) { |
33 } | 44 } |
34 | 45 |
35 PerformanceMonitor::~PerformanceMonitor() { | 46 PerformanceMonitor::~PerformanceMonitor() { |
36 } | 47 } |
37 | 48 |
38 bool PerformanceMonitor::SetDatabasePath(const FilePath& path) { | 49 bool PerformanceMonitor::SetDatabasePath(const FilePath& path) { |
39 if (!database_.get()) { | 50 if (!database_.get()) { |
(...skipping 118 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
158 base::Bind(callback, state_value)); | 169 base::Bind(callback, state_value)); |
159 } | 170 } |
160 | 171 |
161 void PerformanceMonitor::NotifyInitialized() { | 172 void PerformanceMonitor::NotifyInitialized() { |
162 content::NotificationService::current()->Notify( | 173 content::NotificationService::current()->Notify( |
163 chrome::NOTIFICATION_PERFORMANCE_MONITOR_INITIALIZED, | 174 chrome::NOTIFICATION_PERFORMANCE_MONITOR_INITIALIZED, |
164 content::Source<PerformanceMonitor>(this), | 175 content::Source<PerformanceMonitor>(this), |
165 content::NotificationService::NoDetails()); | 176 content::NotificationService::NoDetails()); |
166 } | 177 } |
167 | 178 |
179 void PerformanceMonitor::GatherStatisticsOnBackgroundThread() { | |
180 CHECK(!BrowserThread::CurrentlyOn(BrowserThread::UI)); | |
181 | |
182 // Because the CPU usage is gathered as an average since the last time the | |
183 // function was called, while the memory usage is gathered as an instantaneous | |
184 // usage, the CPU usage is gathered before the metrics map is wiped. | |
185 GatherCPUUsage(); | |
186 UpdateMetricsMap(); | |
187 GatherMemoryUsage(); | |
188 } | |
189 | |
190 void PerformanceMonitor::GatherCPUUsage() { | |
Devlin
2012/07/27 22:09:47
If these are done on the background thread, they s
mitchellwrosen
2012/07/30 21:50:40
Done.
| |
191 if (metrics_map_.size()) { | |
192 double cpu_usage = 0; | |
193 for (MetricsMap::const_iterator iter = metrics_map_.begin(); | |
194 iter != metrics_map_.end(); ++iter) { | |
195 cpu_usage += iter->second->GetCPUUsage(); | |
196 } | |
197 | |
198 database_->AddMetric(METRIC_CPU_USAGE, | |
199 base::DoubleToString(cpu_usage)); | |
200 } | |
201 } | |
202 | |
203 void PerformanceMonitor::GatherMemoryUsage() { | |
204 size_t private_memory_sum = 0; | |
205 size_t shared_memory_sum = 0; | |
206 for (MetricsMap::const_iterator iter = metrics_map_.begin(); | |
207 iter != metrics_map_.end(); ++iter) { | |
208 size_t private_memory = 0; | |
209 size_t shared_memory = 0; | |
210 if (iter->second->GetMemoryBytes(&private_memory, &shared_memory)) { | |
211 private_memory_sum += private_memory; | |
212 shared_memory_sum += shared_memory; | |
213 } else { | |
214 LOG(WARNING) << "GetMemoryBytes returned NULL (platform-specific error)"; | |
215 } | |
216 } | |
217 | |
218 database_->AddMetric(METRIC_PRIVATE_MEMORY_USAGE, | |
219 base::Uint64ToString(private_memory_sum)); | |
220 database_->AddMetric(METRIC_SHARED_MEMORY_USAGE, | |
221 base::Uint64ToString(shared_memory_sum)); | |
222 } | |
223 | |
224 void PerformanceMonitor::UpdateMetricsMap() { | |
225 // Remove old process handles. Use two iterators to safely call erase() on the | |
226 // current element. | |
227 for (MetricsMap::iterator iter_next = metrics_map_.begin(); | |
228 iter_next != metrics_map_.end();) { | |
229 MetricsMap::iterator iter = iter_next++; | |
230 | |
231 if (base::GetTerminationStatus(iter->first, NULL) != | |
232 base::TERMINATION_STATUS_STILL_RUNNING) { | |
233 base::CloseProcessHandle(iter->first); | |
234 metrics_map_.erase(iter); | |
235 } | |
236 } | |
237 | |
238 // Add new process handles. | |
239 base::ProcessId browser_pid = base::GetCurrentProcId(); | |
240 ChromeProcessList chrome_processes(GetRunningChromeProcesses(browser_pid)); | |
241 for (ChromeProcessList::const_iterator pid_iter = chrome_processes.begin(); | |
242 pid_iter != chrome_processes.end(); ++pid_iter) { | |
243 base::ProcessHandle process_handle; | |
244 if (base::OpenProcessHandleWithAccess(*pid_iter, | |
245 access_flags, | |
246 &process_handle) && | |
247 !ContainsKey(metrics_map_, process_handle)) { | |
248 #if defined(OS_MACOSX) | |
249 linked_ptr<base::ProcessMetrics> process_metrics( | |
250 base::ProcessMetrics::CreateProcessMetrics(process_handle, | |
251 content::BrowserChildProcessHost::GetPortProvider())); | |
252 #else | |
253 linked_ptr<base::ProcessMetrics> process_metrics( | |
254 base::ProcessMetrics::CreateProcessMetrics(process_handle)); | |
255 #endif | |
256 // Prime the CPUUsage to be gathered next time. | |
257 process_metrics->GetCPUUsage(); | |
258 | |
259 metrics_map_[process_handle] = process_metrics; | |
260 } | |
261 } | |
262 } | |
263 | |
168 void PerformanceMonitor::Observe(int type, | 264 void PerformanceMonitor::Observe(int type, |
169 const content::NotificationSource& source, | 265 const content::NotificationSource& source, |
170 const content::NotificationDetails& details) { | 266 const content::NotificationDetails& details) { |
171 switch (type) { | 267 switch (type) { |
172 case chrome::NOTIFICATION_EXTENSION_INSTALLED: { | 268 case chrome::NOTIFICATION_EXTENSION_INSTALLED: { |
173 const Extension* extension = content::Details<Extension>(details).ptr(); | 269 const Extension* extension = content::Details<Extension>(details).ptr(); |
174 AddEvent(util::CreateExtensionInstallEvent(base::Time::Now(), | 270 AddEvent(util::CreateExtensionInstallEvent(base::Time::Now(), |
175 extension->id(), | 271 extension->id(), |
176 extension->name(), | 272 extension->name(), |
177 extension->url().spec(), | 273 extension->url().spec(), |
(...skipping 83 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
261 break; | 357 break; |
262 } | 358 } |
263 default: { | 359 default: { |
264 NOTREACHED(); | 360 NOTREACHED(); |
265 break; | 361 break; |
266 } | 362 } |
267 } | 363 } |
268 } | 364 } |
269 | 365 |
270 } // namespace performance_monitor | 366 } // namespace performance_monitor |
OLD | NEW |