OLD | NEW |
---|---|
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "chrome/browser/performance_monitor/performance_monitor.h" | 5 #include "chrome/browser/performance_monitor/performance_monitor.h" |
6 | 6 |
7 #include <algorithm> | |
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/process.h" | |
15 #include "base/process_util.h" | |
16 #include "base/string_number_conversions.h" | |
9 #include "base/threading/worker_pool.h" | 17 #include "base/threading/worker_pool.h" |
10 #include "base/time.h" | 18 #include "base/time.h" |
11 #include "chrome/browser/performance_monitor/constants.h" | 19 #include "chrome/browser/performance_monitor/constants.h" |
12 #include "chrome/browser/performance_monitor/database.h" | 20 #include "chrome/browser/performance_monitor/database.h" |
13 #include "chrome/browser/performance_monitor/performance_monitor_util.h" | 21 #include "chrome/browser/performance_monitor/performance_monitor_util.h" |
14 #include "chrome/browser/extensions/crx_installer.h" | 22 #include "chrome/browser/extensions/crx_installer.h" |
15 #include "chrome/common/chrome_notification_types.h" | 23 #include "chrome/common/chrome_notification_types.h" |
16 #include "chrome/common/chrome_version_info.h" | 24 #include "chrome/common/chrome_version_info.h" |
17 #include "chrome/common/extensions/extension.h" | 25 #include "chrome/common/extensions/extension.h" |
18 #include "chrome/common/extensions/extension_constants.h" | 26 #include "chrome/common/extensions/extension_constants.h" |
27 #include "chrome/test/base/chrome_process_util.h" | |
19 #include "content/public/browser/browser_thread.h" | 28 #include "content/public/browser/browser_thread.h" |
20 #include "content/public/browser/notification_service.h" | 29 #include "content/public/browser/notification_service.h" |
21 | 30 |
22 using extensions::Extension; | 31 using extensions::Extension; |
23 | 32 |
33 namespace { | |
34 | |
35 template <class T> | |
36 T GetMedian(std::vector<T> vec) { | |
Yoyo Zhou
2012/07/09 21:33:36
Since you never expect this to be called with size
mitchellwrosen
2012/07/09 22:45:23
Done.
| |
37 typedef typename std::vector<T>::size_type vec_size; | |
38 | |
39 vec_size size = vec.size(); | |
40 std::sort(vec.begin(), vec.end()); | |
41 vec_size mid = size / 2; | |
42 | |
43 return size % 2 == 0 ? (vec[mid] + vec[mid - 1]) / 2 : vec[mid]; | |
44 } | |
45 | |
46 } // namespace | |
47 | |
24 namespace performance_monitor { | 48 namespace performance_monitor { |
25 | 49 |
26 PerformanceMonitor::PerformanceMonitor() : database_(NULL) { | 50 PerformanceMonitor::PerformanceMonitor() : database_(NULL) { |
27 } | 51 } |
28 | 52 |
29 PerformanceMonitor::~PerformanceMonitor() { | 53 PerformanceMonitor::~PerformanceMonitor() { |
30 } | 54 } |
31 | 55 |
32 void PerformanceMonitor::Start() { | 56 void PerformanceMonitor::Start() { |
33 content::BrowserThread::PostBlockingPoolTaskAndReply( | 57 content::BrowserThread::PostBlockingPoolTaskAndReply( |
(...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
81 FROM_HERE, | 105 FROM_HERE, |
82 base::Bind(&PerformanceMonitor::AddEventOnBackgroundThread, | 106 base::Bind(&PerformanceMonitor::AddEventOnBackgroundThread, |
83 base::Unretained(this), | 107 base::Unretained(this), |
84 base::Passed(event.Pass()))); | 108 base::Passed(event.Pass()))); |
85 } | 109 } |
86 | 110 |
87 void PerformanceMonitor::AddEventOnBackgroundThread(scoped_ptr<Event> event) { | 111 void PerformanceMonitor::AddEventOnBackgroundThread(scoped_ptr<Event> event) { |
88 database_->AddEvent(*event.get()); | 112 database_->AddEvent(*event.get()); |
89 } | 113 } |
90 | 114 |
115 void PerformanceMonitor::AddMetric(const std::string& key, | |
116 const std::string& value) { | |
117 content::BrowserThread::PostBlockingPoolSequencedTask( | |
118 Database::kDatabaseSequenceToken, | |
119 FROM_HERE, | |
120 base::Bind(&PerformanceMonitor::AddMetricOnBackgroundThread, | |
121 base::Unretained(this), | |
122 key, | |
123 value)); | |
124 } | |
125 | |
126 void PerformanceMonitor::AddMetricOnBackgroundThread(const std::string& key, | |
127 const std::string& value) { | |
128 database_->AddMetric(key, value); | |
129 } | |
130 | |
131 | |
91 void PerformanceMonitor::GetStateValueOnBackgroundThread( | 132 void PerformanceMonitor::GetStateValueOnBackgroundThread( |
92 std::string key, base::Callback<void(std::string)> callback) { | 133 std::string key, base::Callback<void(std::string)> callback) { |
93 CHECK(!content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); | 134 CHECK(!content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); |
94 std::string state_value = database_->GetStateValue(key); | 135 std::string state_value = database_->GetStateValue(key); |
95 | 136 |
96 callback.Run(state_value); | 137 callback.Run(state_value); |
97 } | 138 } |
98 | 139 |
99 void PerformanceMonitor::CheckForVersionUpdate() { | 140 void PerformanceMonitor::CheckForVersionUpdate() { |
100 base::Callback<void(std::string)> callback = | 141 base::Callback<void(std::string)> callback = |
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
132 content::BrowserThread::PostBlockingPoolSequencedTask( | 173 content::BrowserThread::PostBlockingPoolSequencedTask( |
133 Database::kDatabaseSequenceToken, | 174 Database::kDatabaseSequenceToken, |
134 FROM_HERE, | 175 FROM_HERE, |
135 base::Bind(base::IgnoreResult(&Database::AddStateValue), | 176 base::Bind(base::IgnoreResult(&Database::AddStateValue), |
136 base::Unretained(database_.get()), | 177 base::Unretained(database_.get()), |
137 kStateChromeVersion, | 178 kStateChromeVersion, |
138 current_version)); | 179 current_version)); |
139 } | 180 } |
140 } | 181 } |
141 | 182 |
183 void PerformanceMonitor::GatherStatistics() { | |
184 CHECK(!content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); | |
185 | |
186 GatherCPUUsage(); | |
187 UpdateMetricsMap(); | |
Yoyo Zhou
2012/07/09 21:33:36
I don't understand why this comes in the middle, a
mitchellwrosen
2012/07/09 22:45:23
Done. If the comment isn't clear enough, let me kn
| |
188 GatherMemoryUsage(); | |
189 } | |
190 | |
191 void PerformanceMonitor::GatherCPUUsage() { | |
192 std::vector<double> cpu_usage_vec; | |
193 | |
194 if (metrics_map_.size() != 0) { | |
Yoyo Zhou
2012/07/09 21:33:36
nit: you use size() at 227 but size() != 0 here. I
mitchellwrosen
2012/07/09 22:45:23
Done.
| |
195 for (MetricsMap::const_iterator iter = metrics_map_.begin(); | |
196 iter != metrics_map_.end(); ++iter) { | |
197 double cpu_usage = iter->second->GetCPUUsage(); | |
198 DCHECK_NE(cpu_usage, 0) << "Gathered CPU usage of 0"; | |
199 cpu_usage_vec.push_back(cpu_usage); | |
200 } | |
201 | |
202 std::sort(cpu_usage_vec.begin(), cpu_usage_vec.end()); | |
203 AddMetric(performance_monitor::kMetricCPUUsage, | |
204 base::DoubleToString(GetMedian(cpu_usage_vec))); | |
205 } | |
206 } | |
207 | |
208 void PerformanceMonitor::GatherMemoryUsage() { | |
209 std::vector<size_t> private_memory_vec; | |
210 std::vector<size_t> shared_memory_vec; | |
211 | |
212 size_t private_memory = 0; | |
213 size_t shared_memory = 0; | |
214 for (MetricsMap::const_iterator iter = metrics_map_.begin(); | |
215 iter != metrics_map_.end(); ++iter) { | |
216 if (iter->second->GetMemoryBytes(&private_memory, &shared_memory)) { | |
217 private_memory_vec.push_back(private_memory); | |
218 shared_memory_vec.push_back(shared_memory); | |
219 } else { | |
220 LOG(ERROR) << "GetMemoryBytes returned NULL (platform-specific error)"; | |
221 } | |
222 } | |
223 | |
224 std::sort(private_memory_vec.begin(), private_memory_vec.end()); | |
225 std::sort(shared_memory_vec.begin(), shared_memory_vec.end()); | |
226 | |
227 if (private_memory_vec.size()) { | |
228 AddMetric(performance_monitor::kMetricPrivateMemoryUsage, | |
229 base::Uint64ToString(GetMedian(private_memory_vec))); | |
230 | |
231 AddMetric(performance_monitor::kMetricSharedMemoryUsage, | |
232 base::Uint64ToString(GetMedian(shared_memory_vec))); | |
233 } | |
234 } | |
235 | |
236 void PerformanceMonitor::UpdateMetricsMap() { | |
237 // Remove old process handles | |
Yoyo Zhou
2012/07/09 21:33:36
nit: comments should end in .
mitchellwrosen
2012/07/09 22:45:23
Done.
| |
238 for (MetricsMap::iterator iter = metrics_map_.begin(); | |
239 iter != metrics_map_.end(); ++iter) { | |
240 if (iter->first == base::kNullProcessHandle) { | |
Yoyo Zhou
2012/07/09 21:33:36
I don't understand how the key of the metrics_map_
mitchellwrosen
2012/07/09 22:45:23
The challenge I was trying to overcome is not gath
Yoyo Zhou
2012/07/10 21:58:00
Seems like you should be checking GetTerminationSt
| |
241 // base::CloseProcessHandle(iter->first); // Necessary? | |
Yoyo Zhou
2012/07/09 21:33:36
This is commented out.
mitchellwrosen
2012/07/09 22:45:23
Yeah, I left that in there because I was hoping yo
Yoyo Zhou
2012/07/10 21:58:00
I would say "yes, you need to close it" based on t
| |
242 metrics_map_.erase(iter); | |
243 } else { | |
244 // Prime the CPUUsage to be gathered next time. | |
245 iter->second->GetCPUUsage(); | |
Yoyo Zhou
2012/07/09 21:33:36
Do you need this? Seems like it should have been p
mitchellwrosen
2012/07/09 22:45:23
This re-primes the processes already in the map. T
Yoyo Zhou
2012/07/10 21:58:00
Either seems fine.
| |
246 } | |
247 } | |
248 | |
249 // Add new process handles | |
250 base::ProcessId browser_pid = base::GetCurrentProcId(); | |
251 ChromeProcessList chrome_processes(GetRunningChromeProcesses(browser_pid)); | |
252 for (ChromeProcessList::const_iterator pid_iter = chrome_processes.begin(); | |
253 pid_iter != chrome_processes.end(); ++pid_iter) { | |
254 base::ProcessHandle process_handle; | |
255 if (base::OpenProcessHandle(*pid_iter, &process_handle)) { | |
256 linked_ptr<base::ProcessMetrics> process_metrics( | |
257 #if !defined(OS_MACOSX) | |
258 base::ProcessMetrics::CreateProcessMetrics(process_handle)); | |
259 #else | |
260 base::ProcessMetrics::CreateProcessMetrics(process_handle, | |
261 content::BrowserChildProcessHost::GetPortProvider())); | |
262 #endif | |
263 if (metrics_map_.find(process_handle) == metrics_map_.end()) { | |
Yoyo Zhou
2012/07/09 21:33:36
You can use ContainsKey from stl_util.h.
mitchellwrosen
2012/07/09 22:45:23
Done.
| |
264 // Prime the CPUUsage to be gathered next time. | |
265 process_metrics->GetCPUUsage(); | |
266 | |
267 metrics_map_[process_handle] = process_metrics; | |
268 } | |
269 } | |
270 } | |
271 } | |
272 | |
142 void PerformanceMonitor::Observe(int type, | 273 void PerformanceMonitor::Observe(int type, |
143 const content::NotificationSource& source, | 274 const content::NotificationSource& source, |
144 const content::NotificationDetails& details) { | 275 const content::NotificationDetails& details) { |
145 switch (type) { | 276 switch (type) { |
146 case chrome::NOTIFICATION_EXTENSION_INSTALLED: { | 277 case chrome::NOTIFICATION_EXTENSION_INSTALLED: { |
147 const Extension* extension = content::Details<Extension>(details).ptr(); | 278 const Extension* extension = content::Details<Extension>(details).ptr(); |
148 AddEvent(util::CreateExtensionInstallEvent(base::Time::Now(), | 279 AddEvent(util::CreateExtensionInstallEvent(base::Time::Now(), |
149 extension->id(), | 280 extension->id(), |
150 extension->name(), | 281 extension->name(), |
151 extension->url().spec(), | 282 extension->url().spec(), |
(...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
198 break; | 329 break; |
199 } | 330 } |
200 default: { | 331 default: { |
201 NOTREACHED(); | 332 NOTREACHED(); |
202 break; | 333 break; |
203 } | 334 } |
204 } | 335 } |
205 } | 336 } |
206 | 337 |
207 } // namespace performance_monitor | 338 } // namespace performance_monitor |
OLD | NEW |