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

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: Nits Created 8 years, 4 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 <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 kAccessFlags = base::kProcessAccessDuplicateHandle |
Devlin 2012/07/31 18:43:20 Do we need all these accesses? I don't see why we
mitchellwrosen 2012/07/31 18:51:19 Yessir, I ran the test with all possible subsets o
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
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 GatherCPUUsageOnBackgroundThread();
186 UpdateMetricsMapOnBackgroundThread();
187 GatherMemoryUsageOnBackgroundThread();
188 }
189
190 void PerformanceMonitor::GatherCPUUsageOnBackgroundThread() {
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, base::DoubleToString(cpu_usage));
199 }
200 }
201
202 void PerformanceMonitor::GatherMemoryUsageOnBackgroundThread() {
203 size_t private_memory_sum = 0;
204 size_t shared_memory_sum = 0;
205 for (MetricsMap::const_iterator iter = metrics_map_.begin();
206 iter != metrics_map_.end(); ++iter) {
207 size_t private_memory = 0;
208 size_t shared_memory = 0;
209 if (iter->second->GetMemoryBytes(&private_memory, &shared_memory)) {
210 private_memory_sum += private_memory;
211 shared_memory_sum += shared_memory;
212 } else {
213 LOG(WARNING) << "GetMemoryBytes returned NULL (platform-specific error)";
214 }
215 }
216
217 database_->AddMetric(METRIC_PRIVATE_MEMORY_USAGE,
218 base::Uint64ToString(private_memory_sum));
219 database_->AddMetric(METRIC_SHARED_MEMORY_USAGE,
220 base::Uint64ToString(shared_memory_sum));
221 }
222
223 void PerformanceMonitor::UpdateMetricsMapOnBackgroundThread() {
224 // Remove old process handles. Use two iterators to safely call erase() on the
225 // current element.
226 for (MetricsMap::iterator iter_next = metrics_map_.begin();
227 iter_next != metrics_map_.end();) {
228 MetricsMap::iterator iter = iter_next++;
229
230 if (base::GetTerminationStatus(iter->first, NULL) !=
231 base::TERMINATION_STATUS_STILL_RUNNING) {
232 base::CloseProcessHandle(iter->first);
233 metrics_map_.erase(iter);
234 }
235 }
236
237 // Add new process handles.
238 base::ProcessId browser_pid = base::GetCurrentProcId();
239 ChromeProcessList chrome_processes(GetRunningChromeProcesses(browser_pid));
240 for (ChromeProcessList::const_iterator pid_iter = chrome_processes.begin();
241 pid_iter != chrome_processes.end(); ++pid_iter) {
242 base::ProcessHandle process_handle;
243 if (base::OpenProcessHandleWithAccess(*pid_iter,
244 kAccessFlags,
245 &process_handle) &&
246 !ContainsKey(metrics_map_, process_handle)) {
247 #if defined(OS_MACOSX)
248 linked_ptr<base::ProcessMetrics> process_metrics(
249 base::ProcessMetrics::CreateProcessMetrics(process_handle,
250 content::BrowserChildProcessHost::GetPortProvider()));
251 #else
252 linked_ptr<base::ProcessMetrics> process_metrics(
253 base::ProcessMetrics::CreateProcessMetrics(process_handle));
254 #endif
255 // Prime the CPUUsage to be gathered next time.
256 process_metrics->GetCPUUsage();
257
258 metrics_map_[process_handle] = process_metrics;
259 }
260 }
261 }
262
168 void PerformanceMonitor::Observe(int type, 263 void PerformanceMonitor::Observe(int type,
169 const content::NotificationSource& source, 264 const content::NotificationSource& source,
170 const content::NotificationDetails& details) { 265 const content::NotificationDetails& details) {
171 switch (type) { 266 switch (type) {
172 case chrome::NOTIFICATION_EXTENSION_INSTALLED: { 267 case chrome::NOTIFICATION_EXTENSION_INSTALLED: {
173 const Extension* extension = content::Details<Extension>(details).ptr(); 268 const Extension* extension = content::Details<Extension>(details).ptr();
174 AddEvent(util::CreateExtensionInstallEvent(base::Time::Now(), 269 AddEvent(util::CreateExtensionInstallEvent(base::Time::Now(),
175 extension->id(), 270 extension->id(),
176 extension->name(), 271 extension->name(),
177 extension->url().spec(), 272 extension->url().spec(),
(...skipping 83 matching lines...) Expand 10 before | Expand all | Expand 10 after
261 break; 356 break;
262 } 357 }
263 default: { 358 default: {
264 NOTREACHED(); 359 NOTREACHED();
265 break; 360 break;
266 } 361 }
267 } 362 }
268 } 363 }
269 364
270 } // namespace performance_monitor 365 } // namespace performance_monitor
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698