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

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

Issue 23825004: PerformanceMonitor: UMA alert for high browser CPU usage. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Buildfix Created 7 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 | Annotate | Revision Log
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 <set> 7 #include <set>
8 #include <vector> 8 #include <vector>
9 9
10 #include "base/bind.h" 10 #include "base/bind.h"
11 #include "base/command_line.h" 11 #include "base/command_line.h"
12 #include "base/logging.h" 12 #include "base/logging.h"
13 #include "base/memory/singleton.h"
13 #include "base/process/process_iterator.h" 14 #include "base/process/process_iterator.h"
14 #include "base/stl_util.h" 15 #include "base/stl_util.h"
15 #include "base/strings/string_number_conversions.h" 16 #include "base/strings/string_number_conversions.h"
16 #include "base/threading/worker_pool.h" 17 #include "base/threading/worker_pool.h"
17 #include "base/time/time.h" 18 #include "base/time/time.h"
18 #include "chrome/browser/browser_process.h" 19 #include "chrome/browser/browser_process.h"
19 #include "chrome/browser/browser_shutdown.h" 20 #include "chrome/browser/browser_shutdown.h"
20 #include "chrome/browser/chrome_notification_types.h" 21 #include "chrome/browser/chrome_notification_types.h"
21 #include "chrome/browser/extensions/crx_installer.h" 22 #include "chrome/browser/extensions/crx_installer.h"
22 #include "chrome/browser/performance_monitor/constants.h" 23 #include "chrome/browser/performance_monitor/constants.h"
23 #include "chrome/browser/performance_monitor/performance_monitor_util.h" 24 #include "chrome/browser/performance_monitor/performance_monitor_util.h"
24 #include "chrome/browser/profiles/profile.h" 25 #include "chrome/browser/profiles/profile.h"
25 #include "chrome/browser/profiles/profile_manager.h" 26 #include "chrome/browser/profiles/profile_manager.h"
26 #include "chrome/browser/ui/browser.h" 27 #include "chrome/browser/ui/browser.h"
27 #include "chrome/browser/ui/browser_iterator.h" 28 #include "chrome/browser/ui/browser_iterator.h"
28 #include "chrome/common/chrome_switches.h" 29 #include "chrome/common/chrome_switches.h"
29 #include "chrome/common/chrome_version_info.h" 30 #include "chrome/common/chrome_version_info.h"
30 #include "chrome/common/extensions/extension.h" 31 #include "chrome/common/extensions/extension.h"
31 #include "chrome/common/extensions/extension_constants.h" 32 #include "chrome/common/extensions/extension_constants.h"
32 #include "chrome/test/base/chrome_process_util.h"
33 #include "content/public/browser/browser_child_process_host.h" 33 #include "content/public/browser/browser_child_process_host.h"
34 #include "content/public/browser/browser_child_process_host_iterator.h"
34 #include "content/public/browser/browser_thread.h" 35 #include "content/public/browser/browser_thread.h"
36 #include "content/public/browser/child_process_data.h"
35 #include "content/public/browser/load_notification_details.h" 37 #include "content/public/browser/load_notification_details.h"
36 #include "content/public/browser/notification_service.h" 38 #include "content/public/browser/notification_service.h"
37 #include "content/public/browser/notification_types.h" 39 #include "content/public/browser/notification_types.h"
38 #include "content/public/browser/render_view_host.h" 40 #include "content/public/browser/render_view_host.h"
39 #include "content/public/browser/render_widget_host.h" 41 #include "content/public/browser/render_widget_host.h"
40 #include "content/public/browser/web_contents.h" 42 #include "content/public/browser/web_contents.h"
41 #include "net/url_request/url_request.h" 43 #include "net/url_request/url_request.h"
42 44
43 using content::BrowserThread; 45 using content::BrowserThread;
44 using extensions::Extension; 46 using extensions::Extension;
45 47
46 namespace performance_monitor { 48 namespace performance_monitor {
47 49
48 namespace { 50 namespace {
49 51
50 const uint32 kAccessFlags = base::kProcessAccessDuplicateHandle | 52 const uint32 kAccessFlags = base::kProcessAccessDuplicateHandle |
51 base::kProcessAccessQueryInformation | 53 base::kProcessAccessQueryInformation |
52 base::kProcessAccessTerminate | 54 base::kProcessAccessTerminate |
53 base::kProcessAccessWaitForTermination; 55 base::kProcessAccessWaitForTermination;
54 56
55 bool g_started_initialization = false;
56
57 std::string TimeToString(base::Time time) { 57 std::string TimeToString(base::Time time) {
58 int64 time_int64 = time.ToInternalValue(); 58 int64 time_int64 = time.ToInternalValue();
59 return base::Int64ToString(time_int64); 59 return base::Int64ToString(time_int64);
60 } 60 }
61 61
62 bool StringToTime(std::string time, base::Time* output) { 62 bool StringToTime(std::string time, base::Time* output) {
63 int64 time_int64 = 0; 63 int64 time_int64 = 0;
64 if (!base::StringToInt64(time, &time_int64)) 64 if (!base::StringToInt64(time, &time_int64))
65 return false; 65 return false;
66 *output = base::Time::FromInternalValue(time_int64); 66 *output = base::Time::FromInternalValue(time_int64);
(...skipping 24 matching lines...) Expand all
91 } 91 }
92 92
93 } // namespace 93 } // namespace
94 94
95 bool PerformanceMonitor::initialized_ = false; 95 bool PerformanceMonitor::initialized_ = false;
96 96
97 PerformanceMonitor::PerformanceDataForIOThread::PerformanceDataForIOThread() 97 PerformanceMonitor::PerformanceDataForIOThread::PerformanceDataForIOThread()
98 : network_bytes_read(0) { 98 : network_bytes_read(0) {
99 } 99 }
100 100
101 PerformanceMonitor::PerformanceMonitor() : metrics_map_(new MetricsMap) {} 101 PerformanceMonitor::PerformanceMonitor()
102 : gather_interval_in_seconds_(kDefaultGatherIntervalInSeconds),
103 database_logging_enabled_(false),
104 timer_(FROM_HERE,
105 base::TimeDelta::FromSeconds(kSampleInterval),
106 this,
107 &PerformanceMonitor::DoTimedCollections),
108 disable_timer_autostart_(false) {}
102 109
103 PerformanceMonitor::~PerformanceMonitor() { 110 PerformanceMonitor::~PerformanceMonitor() {
104 BrowserThread::PostBlockingPoolSequencedTask( 111 BrowserThread::PostBlockingPoolSequencedTask(
105 Database::kDatabaseSequenceToken, 112 Database::kDatabaseSequenceToken,
106 FROM_HERE, 113 FROM_HERE,
107 base::Bind(&DeleteDatabaseOnBackgroundThread, database_.release())); 114 base::Bind(&DeleteDatabaseOnBackgroundThread, database_.release()));
108 } 115 }
109 116
110 bool PerformanceMonitor::SetDatabasePath(const base::FilePath& path) { 117 bool PerformanceMonitor::SetDatabasePath(const base::FilePath& path) {
111 if (!database_.get()) { 118 if (!database_.get()) {
112 database_path_ = path; 119 database_path_ = path;
113 return true; 120 return true;
114 } 121 }
115 122
116 // PerformanceMonitor already initialized with another path. 123 // PerformanceMonitor already initialized with another path.
117 return false; 124 return false;
118 } 125 }
119 126
120 // static 127 // static
121 PerformanceMonitor* PerformanceMonitor::GetInstance() { 128 PerformanceMonitor* PerformanceMonitor::GetInstance() {
122 return Singleton<PerformanceMonitor>::get(); 129 return Singleton<PerformanceMonitor>::get();
123 } 130 }
124 131
125 void PerformanceMonitor::Start() { 132 void PerformanceMonitor::Start() {
126 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); 133 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
127 134
128 // Avoid responding to multiple calls to Start(). 135 static bool has_initialized = false;
129 if (g_started_initialization) 136 DCHECK(!has_initialized);
130 return; 137 has_initialized = true;
131 138
132 g_started_initialization = true; 139 if (CommandLine::ForCurrentProcess()
140 ->HasSwitch(switches::kPerformanceMonitorGathering) &&
141 !CommandLine::ForCurrentProcess()
142 ->GetSwitchValueASCII(switches::kPerformanceMonitorGathering)
143 .empty()) {
144 int specified_interval = 0;
145 if (!base::StringToInt(
146 CommandLine::ForCurrentProcess()
147 ->GetSwitchValueASCII(switches::kPerformanceMonitorGathering),
148 &specified_interval) ||
149 specified_interval <= 0) {
150 LOG(ERROR) << "Invalid value for switch: '"
151 << switches::kPerformanceMonitorGathering
152 << "'; please use an integer greater than 0.";
153 } else {
154 gather_interval_in_seconds_ = specified_interval;
155 // Currently, database logging is only enabled if we specify
156 // a manual update interval; for testing purposes.
157 database_logging_enabled_ = true;
158 }
159 }
160
161 gather_interval_in_seconds_ =
162 std::max(gather_interval_in_seconds_, kSampleInterval);
163
164 next_collection_time_ = base::Time::Now() + base::TimeDelta::FromSeconds(
165 gather_interval_in_seconds_);
166
133 util::PostTaskToDatabaseThreadAndReply( 167 util::PostTaskToDatabaseThreadAndReply(
134 FROM_HERE, 168 FROM_HERE,
135 base::Bind(&PerformanceMonitor::InitOnBackgroundThread, 169 base::Bind(&PerformanceMonitor::InitOnBackgroundThread,
136 base::Unretained(this)), 170 base::Unretained(this)),
137 base::Bind(&PerformanceMonitor::FinishInit, 171 base::Bind(&PerformanceMonitor::FinishInit,
138 base::Unretained(this))); 172 base::Unretained(this)));
139 } 173 }
140 174
141 void PerformanceMonitor::InitOnBackgroundThread() { 175 void PerformanceMonitor::InitOnBackgroundThread() {
142 CHECK(!BrowserThread::CurrentlyOn(BrowserThread::UI)); 176 CHECK(!BrowserThread::CurrentlyOn(BrowserThread::UI));
143 database_ = Database::Create(database_path_);
144 if (!database_) {
145 LOG(ERROR) << "Could not initialize database; aborting initialization.";
146 return;
147 }
148 177
149 // Initialize the io thread's performance data to the value in the database; 178 if (database_logging_enabled_) {
150 // if there isn't a recording in the database, the value stays at 0. 179 database_ = Database::Create(database_path_);
151 Metric metric; 180 if (!database_) {
152 if (database_->GetRecentStatsForActivityAndMetric(METRIC_NETWORK_BYTES_READ, 181 LOG(ERROR) << "Could not initialize database; aborting initialization.";
153 &metric)) { 182 database_logging_enabled_ = false;
154 performance_data_for_io_thread_.network_bytes_read = metric.value; 183 return;
184 }
185
186 // Initialize the io thread's performance data to the value in the database;
187 // if there isn't a recording in the database, the value stays at 0.
188 Metric metric;
189 if (database_->GetRecentStatsForActivityAndMetric(METRIC_NETWORK_BYTES_READ,
190 &metric)) {
191 performance_data_for_io_thread_.network_bytes_read = metric.value;
192 }
155 } 193 }
156 } 194 }
157 195
158 void PerformanceMonitor::FinishInit() { 196 void PerformanceMonitor::FinishInit() {
159 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); 197 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
160 // Short-circuit the initialization process if the database wasn't properly
161 // created. This will prevent PerformanceMonitor from performing any actions,
162 // including observing events.
163 if (!database_)
164 return;
165 198
166 RegisterForNotifications(); 199 // Events and notifications are only useful if we're
167 CheckForUncleanExits(); 200 // logging to the database.
168 BrowserThread::PostBlockingPoolSequencedTask( 201 if (database_logging_enabled_) {
169 Database::kDatabaseSequenceToken, 202 RegisterForNotifications();
170 FROM_HERE, 203 CheckForUncleanExits();
171 base::Bind(&PerformanceMonitor::CheckForVersionUpdateOnBackgroundThread, 204 BrowserThread::PostBlockingPoolSequencedTask(
172 base::Unretained(this))); 205 Database::kDatabaseSequenceToken,
173 206 FROM_HERE,
174 int gather_interval_in_seconds = kDefaultGatherIntervalInSeconds; 207 base::Bind(&PerformanceMonitor::CheckForVersionUpdateOnBackgroundThread,
175 if (CommandLine::ForCurrentProcess()->HasSwitch( 208 base::Unretained(this)));
176 switches::kPerformanceMonitorGathering) &&
177 !CommandLine::ForCurrentProcess()->GetSwitchValueASCII(
178 switches::kPerformanceMonitorGathering).empty()) {
179 int specified_interval = 0;
180 if (!base::StringToInt(
181 CommandLine::ForCurrentProcess()->GetSwitchValueASCII(
182 switches::kPerformanceMonitorGathering),
183 &specified_interval) || specified_interval <= 0) {
184 LOG(ERROR) << "Invalid value for switch: '"
185 << switches::kPerformanceMonitorGathering
186 << "'; please use an integer greater than 0.";
187 } else {
188 gather_interval_in_seconds = specified_interval;
189 }
190 } 209 }
191 210
192 timer_.Start(FROM_HERE, 211 // Start our periodic gathering of metrics.
193 base::TimeDelta::FromSeconds(gather_interval_in_seconds), 212 if (!disable_timer_autostart_) {
194 this, 213 timer_.Reset();
195 &PerformanceMonitor::DoTimedCollections); 214 }
196 215
197 // Post a task to the background thread to a function which does nothing. 216 // Post a task to the background thread to a function which does nothing.
198 // This will force any tasks the database is performing to finish prior to 217 // This will force any tasks the database is performing to finish prior to
199 // the reply being sent, since they use the same thread. 218 // the reply being sent, since they use the same thread.
200 // 219 //
201 // Important! Make sure that methods in FinishInit() only rely on posting 220 // Important! Make sure that methods in FinishInit() only rely on posting
202 // to the background thread, and do not rely upon a reply from the background 221 // to the background thread, and do not rely upon a reply from the background
203 // thread; this is necessary for this notification to be valid. 222 // thread; this is necessary for this notification to be valid.
204 util::PostTaskToDatabaseThreadAndReply( 223 util::PostTaskToDatabaseThreadAndReply(
205 FROM_HERE, 224 FROM_HERE,
206 base::Bind(&base::DoNothing), 225 base::Bind(&base::DoNothing),
207 base::Bind(&PerformanceMonitor::NotifyInitialized, 226 base::Bind(&PerformanceMonitor::NotifyInitialized,
208 base::Unretained(this))); 227 base::Unretained(this)));
209 } 228 }
210 229
211 void PerformanceMonitor::RegisterForNotifications() { 230 void PerformanceMonitor::RegisterForNotifications() {
231 DCHECK(database_logging_enabled_);
232
212 // Extensions 233 // Extensions
213 registrar_.Add(this, chrome::NOTIFICATION_EXTENSION_INSTALLED, 234 registrar_.Add(this, chrome::NOTIFICATION_EXTENSION_INSTALLED,
214 content::NotificationService::AllSources()); 235 content::NotificationService::AllSources());
215 registrar_.Add(this, chrome::NOTIFICATION_EXTENSION_ENABLED, 236 registrar_.Add(this, chrome::NOTIFICATION_EXTENSION_ENABLED,
216 content::NotificationService::AllSources()); 237 content::NotificationService::AllSources());
217 registrar_.Add(this, chrome::NOTIFICATION_EXTENSION_UNLOADED, 238 registrar_.Add(this, chrome::NOTIFICATION_EXTENSION_UNLOADED,
218 content::NotificationService::AllSources()); 239 content::NotificationService::AllSources());
219 registrar_.Add(this, chrome::NOTIFICATION_CRX_INSTALLER_DONE, 240 registrar_.Add(this, chrome::NOTIFICATION_CRX_INSTALLER_DONE,
220 content::NotificationService::AllSources()); 241 content::NotificationService::AllSources());
221 registrar_.Add(this, chrome::NOTIFICATION_EXTENSION_UNINSTALLED, 242 registrar_.Add(this, chrome::NOTIFICATION_EXTENSION_UNINSTALLED,
(...skipping 11 matching lines...) Expand all
233 254
234 // Page load times 255 // Page load times
235 registrar_.Add(this, content::NOTIFICATION_LOAD_STOP, 256 registrar_.Add(this, content::NOTIFICATION_LOAD_STOP,
236 content::NotificationService::AllSources()); 257 content::NotificationService::AllSources());
237 } 258 }
238 259
239 // We check if profiles exited cleanly initialization time in case they were 260 // We check if profiles exited cleanly initialization time in case they were
240 // loaded prior to PerformanceMonitor's initialization. Later profiles will be 261 // loaded prior to PerformanceMonitor's initialization. Later profiles will be
241 // checked through the PROFILE_ADDED notification. 262 // checked through the PROFILE_ADDED notification.
242 void PerformanceMonitor::CheckForUncleanExits() { 263 void PerformanceMonitor::CheckForUncleanExits() {
264 DCHECK(database_logging_enabled_);
265
243 std::vector<Profile*> profiles = 266 std::vector<Profile*> profiles =
244 g_browser_process->profile_manager()->GetLoadedProfiles(); 267 g_browser_process->profile_manager()->GetLoadedProfiles();
245 268
246 for (std::vector<Profile*>::const_iterator iter = profiles.begin(); 269 for (std::vector<Profile*>::const_iterator iter = profiles.begin();
247 iter != profiles.end(); ++iter) { 270 iter != profiles.end(); ++iter) {
248 if ((*iter)->GetLastSessionExitType() == Profile::EXIT_CRASHED) { 271 if ((*iter)->GetLastSessionExitType() == Profile::EXIT_CRASHED) {
249 BrowserThread::PostBlockingPoolSequencedTask( 272 BrowserThread::PostBlockingPoolSequencedTask(
250 Database::kDatabaseSequenceToken, 273 Database::kDatabaseSequenceToken,
251 FROM_HERE, 274 FROM_HERE,
252 base::Bind(&PerformanceMonitor::AddUncleanExitEventOnBackgroundThread, 275 base::Bind(&PerformanceMonitor::AddUncleanExitEventOnBackgroundThread,
253 base::Unretained(this), 276 base::Unretained(this),
254 (*iter)->GetDebugName())); 277 (*iter)->GetDebugName()));
255 } 278 }
256 } 279 }
257 } 280 }
258 281
259 void PerformanceMonitor::AddUncleanExitEventOnBackgroundThread( 282 void PerformanceMonitor::AddUncleanExitEventOnBackgroundThread(
260 const std::string& profile_name) { 283 const std::string& profile_name) {
284
285 DCHECK(database_logging_enabled_);
261 std::string database_key = kStateProfilePrefix + profile_name; 286 std::string database_key = kStateProfilePrefix + profile_name;
262 std::string last_active_string = database_->GetStateValue(database_key); 287 std::string last_active_string = database_->GetStateValue(database_key);
263 288
264 // Check if there was no previous time; this should only happen if the profile 289 // Check if there was no previous time; this should only happen if the profile
265 // was last used prior to PerformanceMonitor's integration. Do nothing in this 290 // was last used prior to PerformanceMonitor's integration. Do nothing in this
266 // case, since the event was prior to the beginning of our recording. 291 // case, since the event was prior to the beginning of our recording.
267 if (last_active_string.empty()) 292 if (last_active_string.empty())
268 return; 293 return;
269 294
270 base::Time last_active_time; 295 base::Time last_active_time;
271 CHECK(StringToTime(last_active_string, &last_active_time)); 296 CHECK(StringToTime(last_active_string, &last_active_time));
272 297
273 scoped_ptr<Event> event = 298 scoped_ptr<Event> event =
274 util::CreateUncleanExitEvent(last_active_time, profile_name); 299 util::CreateUncleanExitEvent(last_active_time, profile_name);
275 300
276 database_->AddEvent(*event.get()); 301 database_->AddEvent(*event.get());
277 } 302 }
278 303
279 void PerformanceMonitor::CheckForVersionUpdateOnBackgroundThread() { 304 void PerformanceMonitor::CheckForVersionUpdateOnBackgroundThread() {
280 CHECK(!BrowserThread::CurrentlyOn(BrowserThread::UI)); 305 CHECK(!BrowserThread::CurrentlyOn(BrowserThread::UI));
306 DCHECK(database_logging_enabled_);
281 307
282 chrome::VersionInfo version; 308 chrome::VersionInfo version;
283 DCHECK(version.is_valid()); 309 DCHECK(version.is_valid());
284 std::string current_version = version.Version(); 310 std::string current_version = version.Version();
285 311
286 std::string previous_version = database_->GetStateValue(kStateChromeVersion); 312 std::string previous_version = database_->GetStateValue(kStateChromeVersion);
287 313
288 // We should never have a current_version which is older than the 314 // We should never have a current_version which is older than the
289 // previous_version. 315 // previous_version.
290 DCHECK(current_version >= previous_version); 316 DCHECK(current_version >= previous_version);
291 317
292 // If this is the first run, there will not be a stored value for Chrome 318 // If this is the first run, there will not be a stored value for Chrome
293 // version; we insert the current version and will insert an event for the 319 // version; we insert the current version and will insert an event for the
294 // next update of Chrome. If the previous version is older than the current 320 // next update of Chrome. If the previous version is older than the current
295 // version, update the state in the database and insert an event. 321 // version, update the state in the database and insert an event.
296 if (current_version > previous_version) { 322 if (current_version > previous_version) {
297 database_->AddStateValue(kStateChromeVersion, current_version); 323 database_->AddStateValue(kStateChromeVersion, current_version);
298 if (!previous_version.empty()) { 324 if (!previous_version.empty()) {
299 scoped_ptr<Event> event = util::CreateChromeUpdateEvent( 325 scoped_ptr<Event> event = util::CreateChromeUpdateEvent(
300 base::Time::Now(), previous_version, current_version); 326 base::Time::Now(), previous_version, current_version);
301 database_->AddEvent(*event.get()); 327 database_->AddEvent(*event.get());
302 } 328 }
303 } 329 }
304 } 330 }
305 331
306 void PerformanceMonitor::AddEvent(scoped_ptr<Event> event) { 332 void PerformanceMonitor::AddEvent(scoped_ptr<Event> event) {
333 DCHECK(database_logging_enabled_);
334
307 BrowserThread::PostBlockingPoolSequencedTask( 335 BrowserThread::PostBlockingPoolSequencedTask(
308 Database::kDatabaseSequenceToken, 336 Database::kDatabaseSequenceToken,
309 FROM_HERE, 337 FROM_HERE,
310 base::Bind(&PerformanceMonitor::AddEventOnBackgroundThread, 338 base::Bind(&PerformanceMonitor::AddEventOnBackgroundThread,
311 base::Unretained(this), 339 base::Unretained(this),
312 base::Passed(&event))); 340 base::Passed(&event)));
313 } 341 }
314 342
315 void PerformanceMonitor::AddEventOnBackgroundThread(scoped_ptr<Event> event) { 343 void PerformanceMonitor::AddEventOnBackgroundThread(scoped_ptr<Event> event) {
316 database_->AddEvent(*event.get()); 344 database_->AddEvent(*event.get());
317 } 345 }
318 346
319 void PerformanceMonitor::AddMetricOnBackgroundThread(const Metric& metric) { 347 void PerformanceMonitor::AddMetricOnBackgroundThread(const Metric& metric) {
320 DCHECK(!BrowserThread::CurrentlyOn(BrowserThread::UI)); 348 DCHECK(!BrowserThread::CurrentlyOn(BrowserThread::UI));
349 DCHECK(database_logging_enabled_);
350
321 database_->AddMetric(metric); 351 database_->AddMetric(metric);
322 } 352 }
323 353
324 void PerformanceMonitor::NotifyInitialized() { 354 void PerformanceMonitor::NotifyInitialized() {
325 content::NotificationService::current()->Notify( 355 content::NotificationService::current()
326 chrome::NOTIFICATION_PERFORMANCE_MONITOR_INITIALIZED, 356 ->Notify(chrome::NOTIFICATION_PERFORMANCE_MONITOR_INITIALIZED,
327 content::Source<PerformanceMonitor>(this), 357 content::Source<PerformanceMonitor>(this),
328 content::NotificationService::NoDetails()); 358 content::NotificationService::NoDetails());
329 359
330 initialized_ = true; 360 initialized_ = true;
331 } 361 }
332 362
333 void PerformanceMonitor::GatherStatisticsOnBackgroundThread() { 363 void PerformanceMonitor::DoTimedCollections() {
334 CHECK(!BrowserThread::CurrentlyOn(BrowserThread::UI));
335 364
336 // Because the CPU usage is gathered as an average since the last time the 365 // The profile list is only useful for the logged events.
337 // function was called, while the memory usage is gathered as an instantaneous 366 if (database_logging_enabled_) {
338 // usage, the CPU usage is gathered before the metrics map is wiped. 367 UpdateLiveProfiles();
339 GatherCPUUsageOnBackgroundThread(); 368 }
340 UpdateMetricsMapOnBackgroundThread(); 369
341 GatherMemoryUsageOnBackgroundThread(); 370 static int current_update_sequence = 0;
371 // Even in the "somewhat" unlikely event this wraps around,
372 // it doesn't matter. We just check it for inequality.
373 current_update_sequence++;
374
375 // Find all render child processes; has to be done on the UI thread.
376 for (content::RenderProcessHost::iterator i =
377 content::RenderProcessHost::AllHostsIterator();
378 !i.IsAtEnd();
379 i.Advance()) {
380
381 base::ProcessHandle handle = i.GetCurrentValue()->GetHandle();
382 ProcessIsAlive(
383 handle, content::PROCESS_TYPE_RENDERER, current_update_sequence);
384 }
385
386 BrowserThread::PostTask(
387 BrowserThread::IO,
388 FROM_HERE,
389 base::Bind(&PerformanceMonitor::GatherMetricsMapOnIOThread,
390 base::Unretained(this),
391 current_update_sequence));
342 } 392 }
343 393
344 void PerformanceMonitor::GatherCPUUsageOnBackgroundThread() { 394 void PerformanceMonitor::ProcessIsAlive(const base::ProcessHandle& handle,
345 if (metrics_map_->size()) { 395 int process_type,
346 double cpu_usage = 0; 396 int current_update_sequence) {
347 for (MetricsMap::const_iterator iter = metrics_map_->begin();
348 iter != metrics_map_->end(); ++iter) {
349 cpu_usage += iter->second->GetCPUUsage();
350 }
351 397
352 database_->AddMetric(Metric(METRIC_CPU_USAGE, 398 if (handle == 0) {
353 base::Time::Now(), 399 // Process may not be valid yet.
354 cpu_usage)); 400 return;
401 }
402
403 MetricsMap::iterator process_metrics_iter = metrics_map_.find(handle);
404 if (process_metrics_iter == metrics_map_.end()) {
405 // If we're not already watching the process, let's initialize it.
406 metrics_map_[handle]
407 .Initialize(handle, process_type, current_update_sequence);
408 } else {
409 // If we are watching the process, touch it to keep it alive.
410 ProcessMetricsHistory& process_metrics = process_metrics_iter->second;
411 process_metrics.SetLastUpdateSequence(current_update_sequence);
355 } 412 }
356 } 413 }
357 414
358 void PerformanceMonitor::GatherMemoryUsageOnBackgroundThread() {
359 size_t private_memory_sum = 0;
360 size_t shared_memory_sum = 0;
361 for (MetricsMap::const_iterator iter = metrics_map_->begin();
362 iter != metrics_map_->end(); ++iter) {
363 size_t private_memory = 0;
364 size_t shared_memory = 0;
365 if (iter->second->GetMemoryBytes(&private_memory, &shared_memory)) {
366 private_memory_sum += private_memory;
367 shared_memory_sum += shared_memory;
368 } else {
369 LOG(WARNING) << "GetMemoryBytes returned NULL (platform-specific error)";
370 }
371 }
372
373 database_->AddMetric(Metric(METRIC_PRIVATE_MEMORY_USAGE,
374 base::Time::Now(),
375 static_cast<double>(private_memory_sum)));
376 database_->AddMetric(Metric(METRIC_SHARED_MEMORY_USAGE,
377 base::Time::Now(),
378 static_cast<double>(shared_memory_sum)));
379 }
380
381 void PerformanceMonitor::UpdateMetricsMapOnBackgroundThread() {
382 MetricsMap* new_map = new MetricsMap;
383
384 base::ProcessId browser_pid = base::GetCurrentProcId();
385 ChromeProcessList chrome_processes(GetRunningChromeProcesses(browser_pid));
386 for (ChromeProcessList::const_iterator pid_iter = chrome_processes.begin();
387 pid_iter != chrome_processes.end(); ++pid_iter) {
388 base::ProcessHandle handle;
389 if (base::OpenProcessHandleWithAccess(*pid_iter, kAccessFlags, &handle)) {
390 // If we were already watching this process, transfer it to the new map.
391 if (ContainsKey(*metrics_map_, handle)) {
392 (*new_map)[handle] = (*metrics_map_)[handle];
393 continue;
394 }
395
396 // Otherwise, gather information and prime the CPU usage to be gathered.
397 #if defined (OS_MACOSX)
398 linked_ptr<base::ProcessMetrics> process_metrics(
399 base::ProcessMetrics::CreateProcessMetrics(
400 handle, content::BrowserChildProcessHost::GetPortProvider()));
401 #else
402 linked_ptr<base::ProcessMetrics> process_metrics(
403 base::ProcessMetrics::CreateProcessMetrics(handle));
404 #endif
405
406 process_metrics->GetCPUUsage();
407
408 (*new_map)[handle] = process_metrics;
409 }
410 }
411
412 metrics_map_.reset(new_map);
413 }
414
415 void PerformanceMonitor::UpdateLiveProfiles() { 415 void PerformanceMonitor::UpdateLiveProfiles() {
416 std::string time = TimeToString(base::Time::Now()); 416 std::string time = TimeToString(base::Time::Now());
417 scoped_ptr<std::set<std::string> > active_profiles( 417 scoped_ptr<std::set<std::string> > active_profiles(
418 new std::set<std::string>()); 418 new std::set<std::string>());
419 419
420 for (chrome::BrowserIterator it; !it.done(); it.Next()) 420 for (chrome::BrowserIterator it; !it.done(); it.Next())
421 active_profiles->insert(it->profile()->GetDebugName()); 421 active_profiles->insert(it->profile()->GetDebugName());
422 422
423 BrowserThread::PostBlockingPoolSequencedTask( 423 BrowserThread::PostBlockingPoolSequencedTask(
424 Database::kDatabaseSequenceToken, 424 Database::kDatabaseSequenceToken,
425 FROM_HERE, 425 FROM_HERE,
426 base::Bind(&PerformanceMonitor::UpdateLiveProfilesHelper, 426 base::Bind(&PerformanceMonitor::UpdateLiveProfilesHelper,
427 base::Unretained(this), 427 base::Unretained(this),
428 base::Passed(&active_profiles), 428 base::Passed(&active_profiles),
429 time)); 429 time));
430 } 430 }
431 431
432 void PerformanceMonitor::UpdateLiveProfilesHelper( 432 void PerformanceMonitor::UpdateLiveProfilesHelper(
433 scoped_ptr<std::set<std::string> > active_profiles, 433 scoped_ptr<std::set<std::string> > active_profiles,
434 std::string time) { 434 std::string time) {
435 CHECK(!BrowserThread::CurrentlyOn(BrowserThread::UI)); 435 CHECK(!BrowserThread::CurrentlyOn(BrowserThread::UI));
436 DCHECK(database_logging_enabled_);
436 437
437 for (std::set<std::string>::const_iterator iter = active_profiles->begin(); 438 for (std::set<std::string>::const_iterator iter = active_profiles->begin();
438 iter != active_profiles->end(); ++iter) { 439 iter != active_profiles->end(); ++iter) {
439 database_->AddStateValue(kStateProfilePrefix + *iter, time); 440 database_->AddStateValue(kStateProfilePrefix + *iter, time);
440 } 441 }
441 } 442 }
442 443
443 void PerformanceMonitor::DoTimedCollections() { 444 void PerformanceMonitor::GatherMetricsMapOnIOThread(
444 UpdateLiveProfiles(); 445 int current_update_sequence) {
446 CHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
447
448 // Find all child processes (does not include renderers),
449 // which has to be done on the IO thread.
450 for (content::BrowserChildProcessHostIterator iter; !iter.Done(); ++iter) {
451 const content::ChildProcessData& child_process_data = iter.GetData();
452 base::ProcessHandle handle = child_process_data.handle;
453 ProcessIsAlive(
454 handle, child_process_data.process_type, current_update_sequence);
455 }
456
457 // Add the current (browser) process.
458 ProcessIsAlive(base::GetCurrentProcessHandle(),
459 content::PROCESS_TYPE_BROWSER,
460 current_update_sequence);
445 461
446 BrowserThread::PostBlockingPoolSequencedTask( 462 BrowserThread::PostBlockingPoolSequencedTask(
447 Database::kDatabaseSequenceToken, 463 Database::kDatabaseSequenceToken,
448 FROM_HERE, 464 FROM_HERE,
449 base::Bind(&PerformanceMonitor::GatherStatisticsOnBackgroundThread, 465 base::Bind(&PerformanceMonitor::StoreMetricsOnBackgroundThread,
450 base::Unretained(this))); 466 base::Unretained(this),
467 current_update_sequence,
468 performance_data_for_io_thread_));
469 }
470
471 void PerformanceMonitor::StoreMetricsOnBackgroundThread(
472 int current_update_sequence,
473 const PerformanceDataForIOThread& performance_data_for_io_thread) {
474 CHECK(!BrowserThread::CurrentlyOn(BrowserThread::UI));
475
476 // Update metrics for all watched processes; remove dead entries from the map.
477 MetricsMap::iterator iter = metrics_map_.begin();
478 while (iter != metrics_map_.end()) {
479 ProcessMetricsHistory& process_metrics = iter->second;
480 if (process_metrics.GetLastUpdateSequence() != current_update_sequence) {
481 // Not touched this iteration; let's get rid of it.
482 metrics_map_.erase(iter++);
483 } else {
484 process_metrics.SampleMetrics();
485 ++iter;
486 }
487 }
488
489 base::Time time_now = base::Time::Now();
490
491 // Time for a collection to be made, of previously sampled metrics.
492 // The timing can be off by kSampleInterval during any one particular run,
493 // but will be correct over time.
494 if (time_now > next_collection_time_) {
495 next_collection_time_ +=
496 base::TimeDelta::FromSeconds(gather_interval_in_seconds_);
497
498 if (!metrics_map_.empty()) {
499 double cpu_usage = 0.0;
500 size_t private_memory_sum = 0;
501 size_t shared_memory_sum = 0;
502
503 for (MetricsMap::iterator iter = metrics_map_.begin();
504 iter != metrics_map_.end();
505 ++iter) {
506
507 ProcessMetricsHistory& process_metrics = iter->second;
508 cpu_usage += process_metrics.GetAverageCPUUsage();
509
510 size_t private_memory = 0;
511 size_t shared_memory = 0;
512 process_metrics.GetAverageMemoryBytes(&private_memory, &shared_memory);
513 private_memory_sum += private_memory;
514 shared_memory_sum += shared_memory;
515
516 process_metrics.EndOfCycle();
517 }
518
519 if (database_logging_enabled_) {
520 database_->AddMetric(Metric(METRIC_CPU_USAGE, time_now, cpu_usage));
521 database_->AddMetric(Metric(METRIC_PRIVATE_MEMORY_USAGE,
522 time_now,
523 static_cast<double>(private_memory_sum)));
524 database_->AddMetric(Metric(METRIC_SHARED_MEMORY_USAGE,
525 time_now,
526 static_cast<double>(shared_memory_sum)));
527 }
528 }
529
530 if (database_logging_enabled_) {
531 database_->AddMetric(
532 Metric(METRIC_NETWORK_BYTES_READ,
533 time_now,
534 static_cast<double>(
535 performance_data_for_io_thread.network_bytes_read)));
536 }
537 }
451 538
452 BrowserThread::PostTask( 539 BrowserThread::PostTask(
453 BrowserThread::IO, 540 BrowserThread::UI,
454 FROM_HERE, 541 FROM_HERE,
455 base::Bind(&PerformanceMonitor::CallInsertIOData, 542 base::Bind(&PerformanceMonitor::ResetMetricsTimerOnUIThread,
456 base::Unretained(this))); 543 base::Unretained(this)));
457 } 544 }
458 545
459 void PerformanceMonitor::CallInsertIOData() { 546 void PerformanceMonitor::ResetMetricsTimerOnUIThread() {
460 CHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); 547 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
461 548
462 BrowserThread::PostBlockingPoolSequencedTask( 549 // Start up the timer again; we avoid the use of a repeating timer
463 Database::kDatabaseSequenceToken, 550 // to not have concurrent metrics gathering running.
464 FROM_HERE, 551 timer_.Reset();
465 base::Bind(&PerformanceMonitor::InsertIOData,
466 base::Unretained(this),
467 performance_data_for_io_thread_));
468 }
469
470 void PerformanceMonitor::InsertIOData(
471 const PerformanceDataForIOThread& performance_data_for_io_thread) {
472 CHECK(!BrowserThread::CurrentlyOn(BrowserThread::UI));
473 database_->AddMetric(
474 Metric(METRIC_NETWORK_BYTES_READ,
475 base::Time::Now(),
476 static_cast<double>(
477 performance_data_for_io_thread.network_bytes_read)));
478 } 552 }
479 553
480 void PerformanceMonitor::BytesReadOnIOThread(const net::URLRequest& request, 554 void PerformanceMonitor::BytesReadOnIOThread(const net::URLRequest& request,
481 const int bytes_read) { 555 const int bytes_read) {
482 CHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); 556 CHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
483 557
484 if (initialized_ && !request.url().SchemeIsFile()) 558 if (initialized_ && !request.url().SchemeIsFile())
485 performance_data_for_io_thread_.network_bytes_read += bytes_read; 559 performance_data_for_io_thread_.network_bytes_read += bytes_read;
486 } 560 }
487 561
488 void PerformanceMonitor::Observe(int type, 562 void PerformanceMonitor::Observe(int type,
489 const content::NotificationSource& source, 563 const content::NotificationSource& source,
490 const content::NotificationDetails& details) { 564 const content::NotificationDetails& details) {
565 DCHECK(database_logging_enabled_);
566
491 switch (type) { 567 switch (type) {
492 case chrome::NOTIFICATION_EXTENSION_INSTALLED: { 568 case chrome::NOTIFICATION_EXTENSION_INSTALLED: {
493 AddExtensionEvent( 569 AddExtensionEvent(
494 EVENT_EXTENSION_INSTALL, 570 EVENT_EXTENSION_INSTALL,
495 content::Details<const extensions::InstalledExtensionInfo>(details)-> 571 content::Details<const extensions::InstalledExtensionInfo>(details)->
496 extension); 572 extension);
497 break; 573 break;
498 } 574 }
499 case chrome::NOTIFICATION_EXTENSION_ENABLED: { 575 case chrome::NOTIFICATION_EXTENSION_ENABLED: {
500 AddExtensionEvent(EVENT_EXTENSION_ENABLE, 576 AddExtensionEvent(EVENT_EXTENSION_ENABLE,
(...skipping 137 matching lines...) Expand 10 before | Expand all | Expand 10 after
638 if (!url_list.empty()) 714 if (!url_list.empty())
639 url_list += ", "; 715 url_list += ", ";
640 716
641 url_list += url; 717 url_list += url;
642 } 718 }
643 719
644 AddEvent(util::CreateRendererFailureEvent(base::Time::Now(), type, url_list)); 720 AddEvent(util::CreateRendererFailureEvent(base::Time::Now(), type, url_list));
645 } 721 }
646 722
647 } // namespace performance_monitor 723 } // namespace performance_monitor
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698