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