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

Side by Side Diff: chrome/browser/performance_monitor/performance_monitor.cc

Issue 10656052: Performance monitor stats gathering. (Closed) Base URL: http://git.chromium.org/chromium/src.git@cpm_main
Patch Set: Fixes Created 8 years, 5 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/performance_monitor/performance_monitor.h" 5 #include "chrome/browser/performance_monitor/performance_monitor.h"
6 6
7 #include <algorithm>
8 #include <map>
9 #include <string>
10 #include <vector>
11
7 #include "base/bind.h" 12 #include "base/bind.h"
8 #include "base/logging.h" 13 #include "base/logging.h"
14 #include "base/memory/linked_ptr.h"
15 #include "base/process.h"
9 #include "base/process_util.h" 16 #include "base/process_util.h"
17 #include "base/stl_util.h"
18 #include "base/string_number_conversions.h"
10 #include "base/threading/worker_pool.h" 19 #include "base/threading/worker_pool.h"
11 #include "base/time.h" 20 #include "base/time.h"
12 #include "chrome/browser/browser_shutdown.h" 21 #include "chrome/browser/browser_shutdown.h"
13 #include "chrome/browser/performance_monitor/constants.h" 22 #include "chrome/browser/performance_monitor/constants.h"
14 #include "chrome/browser/performance_monitor/database.h" 23 #include "chrome/browser/performance_monitor/database.h"
15 #include "chrome/browser/performance_monitor/performance_monitor_util.h" 24 #include "chrome/browser/performance_monitor/performance_monitor_util.h"
16 #include "chrome/browser/extensions/crx_installer.h" 25 #include "chrome/browser/extensions/crx_installer.h"
17 #include "chrome/common/chrome_notification_types.h" 26 #include "chrome/common/chrome_notification_types.h"
18 #include "chrome/common/chrome_version_info.h" 27 #include "chrome/common/chrome_version_info.h"
19 #include "chrome/common/extensions/extension.h" 28 #include "chrome/common/extensions/extension.h"
20 #include "chrome/common/extensions/extension_constants.h" 29 #include "chrome/common/extensions/extension_constants.h"
30 #include "chrome/test/base/chrome_process_util.h"
21 #include "content/public/browser/browser_thread.h" 31 #include "content/public/browser/browser_thread.h"
22 #include "content/public/browser/notification_service.h" 32 #include "content/public/browser/notification_service.h"
23 #include "content/public/browser/notification_types.h" 33 #include "content/public/browser/notification_types.h"
24 #include "content/public/browser/render_process_host.h" 34 #include "content/public/browser/render_process_host.h"
25 #include "content/public/browser/web_contents.h" 35 #include "content/public/browser/web_contents.h"
26 36
27 using content::BrowserThread; 37 using content::BrowserThread;
28 using extensions::Extension; 38 using extensions::Extension;
29 39
40 namespace {
41
42 template <class T>
43 T GetMedian(std::vector<T> vec) {
44 typedef typename std::vector<T>::size_type vec_size;
45
46 vec_size size = vec.size();
47 DCHECK(size != 0);
48 std::sort(vec.begin(), vec.end());
49 vec_size mid = size / 2;
50
51 return size % 2 == 0 ? (vec[mid] + vec[mid - 1]) / 2 : vec[mid];
52 }
53
54 } // namespace
55
30 namespace performance_monitor { 56 namespace performance_monitor {
31 57
32 PerformanceMonitor::PerformanceMonitor() : database_(NULL) { 58 PerformanceMonitor::PerformanceMonitor() : database_(NULL) {
33 } 59 }
34 60
35 PerformanceMonitor::~PerformanceMonitor() { 61 PerformanceMonitor::~PerformanceMonitor() {
36 } 62 }
37 63
38 bool PerformanceMonitor::SetDatabasePath(const FilePath& path) { 64 bool PerformanceMonitor::SetDatabasePath(const FilePath& path) {
39 if (!database_.get()) { 65 if (!database_.get()) {
(...skipping 117 matching lines...) Expand 10 before | Expand all | Expand 10 after
157 base::Bind(callback, state_value)); 183 base::Bind(callback, state_value));
158 } 184 }
159 185
160 void PerformanceMonitor::NotifyInitialized() { 186 void PerformanceMonitor::NotifyInitialized() {
161 content::NotificationService::current()->Notify( 187 content::NotificationService::current()->Notify(
162 chrome::NOTIFICATION_PERFORMANCE_MONITOR_INITIALIZED, 188 chrome::NOTIFICATION_PERFORMANCE_MONITOR_INITIALIZED,
163 content::Source<PerformanceMonitor>(this), 189 content::Source<PerformanceMonitor>(this),
164 content::NotificationService::NoDetails()); 190 content::NotificationService::NoDetails());
165 } 191 }
166 192
193 void PerformanceMonitor::GatherStatisticsOnBackgroundThread() {
194 CHECK(!content::BrowserThread::CurrentlyOn(content::BrowserThread::UI));
195
196 // Because the CPU usage is gathered as an average since the last time the
197 // function was called, while the memory usage is gathered as an instantaneous
198 // usage, the CPU usage is gathered before the metrics map is wiped.
199 GatherCPUUsage();
200 UpdateMetricsMap();
201 GatherMemoryUsage();
202 }
203
204 void PerformanceMonitor::GatherCPUUsage() {
205 std::vector<double> cpu_usage_vec;
206
207 if (metrics_map_.size()) {
208 for (MetricsMap::const_iterator iter = metrics_map_.begin();
209 iter != metrics_map_.end(); ++iter)
210 cpu_usage_vec.push_back(iter->second->GetCPUUsage());
211
212 std::sort(cpu_usage_vec.begin(), cpu_usage_vec.end());
213 database_->AddMetric(performance_monitor::kMetricCPUUsage,
214 base::DoubleToString(GetMedian(cpu_usage_vec)));
215 }
216 }
217
218 void PerformanceMonitor::GatherMemoryUsage() {
219 std::vector<size_t> private_memory_vec;
220 std::vector<size_t> shared_memory_vec;
221
222 size_t private_memory = 0;
223 size_t shared_memory = 0;
224 for (MetricsMap::const_iterator iter = metrics_map_.begin();
225 iter != metrics_map_.end(); ++iter) {
226 if (iter->second->GetMemoryBytes(&private_memory, &shared_memory)) {
227 private_memory_vec.push_back(private_memory);
228 shared_memory_vec.push_back(shared_memory);
229 } else {
230 LOG(ERROR) << "GetMemoryBytes returned NULL (platform-specific error)";
231 }
232 }
233
234 if (!private_memory_vec.size())
235 return;
236
237 std::sort(private_memory_vec.begin(), private_memory_vec.end());
238 std::sort(shared_memory_vec.begin(), shared_memory_vec.end());
239
240 database_->AddMetric(performance_monitor::kMetricPrivateMemoryUsage,
241 base::Uint64ToString(GetMedian(private_memory_vec)));
242 database_->AddMetric(performance_monitor::kMetricSharedMemoryUsage,
243 base::Uint64ToString(GetMedian(shared_memory_vec)));
244 }
245
246 void PerformanceMonitor::UpdateMetricsMap() {
247 // Remove old process handles. Use two iterators to safely call erase()
248 // on the current element, and advance by a call to iter = iter_next++.
249 // The third iterator saves us from calling end() every loop.
250 for (MetricsMap::iterator iter = metrics_map_.begin(), iter_next = iter,
251 iter_end = metrics_map_.end(); iter != iter_end; iter = iter_next;) {
252 ++iter_next;
253
254 if (base::GetTerminationStatus(iter->first, NULL) !=
255 base::TERMINATION_STATUS_STILL_RUNNING) {
256 base::CloseProcessHandle(iter->first);
257 metrics_map_.erase(iter);
258 } else {
259 // Prime the CPUUsage to be gathered next time.
260 iter->second->GetCPUUsage();
261 }
262 }
263
264 // Add new process handles.
265 base::ProcessId browser_pid = base::GetCurrentProcId();
266 ChromeProcessList chrome_processes(GetRunningChromeProcesses(browser_pid));
267 for (ChromeProcessList::const_iterator pid_iter = chrome_processes.begin();
268 pid_iter != chrome_processes.end(); ++pid_iter) {
269 base::ProcessHandle process_handle;
270 if (base::OpenProcessHandle(*pid_iter, &process_handle) &&
271 !ContainsKey(metrics_map_, process_handle)) {
272 #if !defined(OS_MACOSX)
273 linked_ptr<base::ProcessMetrics> process_metrics(
274 base::ProcessMetrics::CreateProcessMetrics(process_handle));
275 #else
276 linked_ptr<base::ProcessMetrics> process_metrics(
277 base::ProcessMetrics::CreateProcessMetrics(process_handle,
278 content::BrowserChildProcessHost::GetPortProvider()));
279 #endif
280 // Prime the CPUUsage to be gathered next time.
281 process_metrics->GetCPUUsage();
282
283 metrics_map_[process_handle] = process_metrics;
284 }
285 }
286 }
287
167 void PerformanceMonitor::Observe(int type, 288 void PerformanceMonitor::Observe(int type,
168 const content::NotificationSource& source, 289 const content::NotificationSource& source,
169 const content::NotificationDetails& details) { 290 const content::NotificationDetails& details) {
170 switch (type) { 291 switch (type) {
171 case chrome::NOTIFICATION_EXTENSION_INSTALLED: { 292 case chrome::NOTIFICATION_EXTENSION_INSTALLED: {
172 const Extension* extension = content::Details<Extension>(details).ptr(); 293 const Extension* extension = content::Details<Extension>(details).ptr();
173 AddEvent(util::CreateExtensionInstallEvent(base::Time::Now(), 294 AddEvent(util::CreateExtensionInstallEvent(base::Time::Now(),
174 extension->id(), 295 extension->id(),
175 extension->name(), 296 extension->name(),
176 extension->url().spec(), 297 extension->url().spec(),
(...skipping 83 matching lines...) Expand 10 before | Expand all | Expand 10 after
260 break; 381 break;
261 } 382 }
262 default: { 383 default: {
263 NOTREACHED(); 384 NOTREACHED();
264 break; 385 break;
265 } 386 }
266 } 387 }
267 } 388 }
268 389
269 } // namespace performance_monitor 390 } // namespace performance_monitor
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698