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