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> | |
Devlin
2012/07/17 18:07:58
map and string are included in the .h file; you ca
mitchellwrosen
2012/07/20 19:42:36
Isn't there a style rule against relying on transi
Aaron Boodman
2012/07/20 21:07:05
The rule seems to be special cased to omit a .cc f
| |
9 #include <string> | |
10 #include <vector> | |
11 | |
7 #include "base/bind.h" | 12 #include "base/bind.h" |
8 #include "base/logging.h" | 13 #include "base/logging.h" |
14 #include "base/memory/linked_ptr.h" | |
15 #include "base/process.h" | |
Devlin
2012/07/17 18:07:58
linked_ptr, process, process_util already included
| |
16 #include "base/process_util.h" | |
17 #include "base/stl_util.h" | |
18 #include "base/string_number_conversions.h" | |
9 #include "base/threading/worker_pool.h" | 19 #include "base/threading/worker_pool.h" |
10 #include "base/time.h" | 20 #include "base/time.h" |
11 #include "chrome/browser/performance_monitor/constants.h" | 21 #include "chrome/browser/performance_monitor/constants.h" |
12 #include "chrome/browser/performance_monitor/database.h" | 22 #include "chrome/browser/performance_monitor/database.h" |
13 #include "chrome/browser/performance_monitor/performance_monitor_util.h" | 23 #include "chrome/browser/performance_monitor/performance_monitor_util.h" |
14 #include "chrome/browser/extensions/crx_installer.h" | 24 #include "chrome/browser/extensions/crx_installer.h" |
15 #include "chrome/common/chrome_notification_types.h" | 25 #include "chrome/common/chrome_notification_types.h" |
16 #include "chrome/common/chrome_version_info.h" | 26 #include "chrome/common/chrome_version_info.h" |
17 #include "chrome/common/extensions/extension.h" | 27 #include "chrome/common/extensions/extension.h" |
18 #include "chrome/common/extensions/extension_constants.h" | 28 #include "chrome/common/extensions/extension_constants.h" |
29 #include "chrome/test/base/chrome_process_util.h" | |
19 #include "content/public/browser/browser_thread.h" | 30 #include "content/public/browser/browser_thread.h" |
20 #include "content/public/browser/notification_service.h" | 31 #include "content/public/browser/notification_service.h" |
21 | 32 |
22 using extensions::Extension; | 33 using extensions::Extension; |
23 | 34 |
35 namespace { | |
36 | |
37 template <class T> | |
38 T GetMedian(std::vector<T> vec) { | |
39 typedef typename std::vector<T>::size_type vec_size; | |
40 | |
41 vec_size size = vec.size(); | |
42 DCHECK(size != 0); | |
43 std::sort(vec.begin(), vec.end()); | |
44 vec_size mid = size / 2; | |
45 | |
46 return size % 2 == 0 ? (vec[mid] + vec[mid - 1]) / 2 : vec[mid]; | |
47 } | |
48 | |
49 } // namespace | |
50 | |
24 namespace performance_monitor { | 51 namespace performance_monitor { |
25 | 52 |
26 PerformanceMonitor::PerformanceMonitor() : database_(NULL) { | 53 PerformanceMonitor::PerformanceMonitor() : database_(NULL) { |
27 } | 54 } |
28 | 55 |
29 PerformanceMonitor::~PerformanceMonitor() { | 56 PerformanceMonitor::~PerformanceMonitor() { |
30 } | 57 } |
31 | 58 |
32 void PerformanceMonitor::Start() { | 59 void PerformanceMonitor::Start() { |
33 content::BrowserThread::PostBlockingPoolTaskAndReply( | 60 content::BrowserThread::PostBlockingPoolTaskAndReply( |
(...skipping 103 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
137 current_version)); | 164 current_version)); |
138 if (!previous_version.empty()) { | 165 if (!previous_version.empty()) { |
139 AddEvent(util::CreateChromeUpdateEvent( | 166 AddEvent(util::CreateChromeUpdateEvent( |
140 base::Time::Now(), | 167 base::Time::Now(), |
141 previous_version, | 168 previous_version, |
142 current_version)); | 169 current_version)); |
143 } | 170 } |
144 } | 171 } |
145 } | 172 } |
146 | 173 |
174 void PerformanceMonitor::GatherStatisticsOnBackgroundThread() { | |
175 CHECK(!content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); | |
176 | |
177 // Because the CPU usage is gathered as an average since the last time the | |
178 // function was called, while the memory usage is gathered as an instantaneous | |
179 // usage, the CPU usage is gathered before the metrics map is wiped. | |
180 GatherCPUUsage(); | |
181 UpdateMetricsMap(); | |
182 GatherMemoryUsage(); | |
183 } | |
184 | |
185 void PerformanceMonitor::GatherCPUUsage() { | |
186 std::vector<double> cpu_usage_vec; | |
187 | |
188 if (metrics_map_.size()) { | |
189 MetricsMap::const_iterator iter = metrics_map_.begin(); | |
190 for (; iter != metrics_map_.end(); ++iter) | |
Devlin
2012/07/17 18:07:58
Why not declare iter here, as normal?
mitchellwrosen
2012/07/20 19:42:36
Done.
| |
191 cpu_usage_vec.push_back(iter->second->GetCPUUsage()); | |
192 | |
193 std::sort(cpu_usage_vec.begin(), cpu_usage_vec.end()); | |
194 database_->AddMetric(performance_monitor::kMetricCPUUsage, | |
195 base::DoubleToString(GetMedian(cpu_usage_vec))); | |
196 } | |
197 } | |
198 | |
199 void PerformanceMonitor::GatherMemoryUsage() { | |
200 std::vector<size_t> private_memory_vec; | |
201 std::vector<size_t> shared_memory_vec; | |
202 | |
203 size_t private_memory = 0; | |
204 size_t shared_memory = 0; | |
205 for (MetricsMap::const_iterator iter = metrics_map_.begin(); | |
206 iter != metrics_map_.end(); ++iter) { | |
207 if (iter->second->GetMemoryBytes(&private_memory, &shared_memory)) { | |
208 private_memory_vec.push_back(private_memory); | |
209 shared_memory_vec.push_back(shared_memory); | |
210 } else { | |
211 LOG(ERROR) << "GetMemoryBytes returned NULL (platform-specific error)"; | |
212 } | |
213 } | |
214 | |
215 if (!private_memory_vec.size()) | |
216 return; | |
217 | |
218 std::sort(private_memory_vec.begin(), private_memory_vec.end()); | |
219 std::sort(shared_memory_vec.begin(), shared_memory_vec.end()); | |
220 | |
221 database_->AddMetric(performance_monitor::kMetricPrivateMemoryUsage, | |
222 base::Uint64ToString(GetMedian(private_memory_vec))); | |
223 database_->AddMetric(performance_monitor::kMetricSharedMemoryUsage, | |
224 base::Uint64ToString(GetMedian(shared_memory_vec))); | |
225 } | |
226 | |
227 void PerformanceMonitor::UpdateMetricsMap() { | |
228 // Remove old process handles. Use two iteraterors to safely call erase() | |
Yoyo Zhou
2012/07/19 21:35:35
typo: iterators
mitchellwrosen
2012/07/20 19:42:36
Done.
| |
229 // on the current element, and advance by a call to iter = iter_next++. | |
Devlin
2012/07/17 18:07:58
style violation - always use ++i, not i++
Yoyo Zhou
2012/07/19 21:35:35
It's fine if you're assigning the old value of i.
| |
230 // The third iterator saves us from calling end() every loop. | |
231 for (MetricsMap::iterator iter_next = metrics_map_.begin(), | |
232 iter_end = metrics_map_.end(), | |
233 iter = (iter_next == iter_end) ? iter_next : iter_next++; | |
Yoyo Zhou
2012/07/19 21:35:35
Yikes, it's super unreadable to squeeze everything
mitchellwrosen
2012/07/20 19:42:36
Does it look better now? I can make it cleaner by
Yoyo Zhou
2012/07/20 20:47:30
It still looks scary. I have to think too much to
mitchellwrosen
2012/07/20 23:05:27
I'm not sure I understand. If only iter_next is in
Yoyo Zhou
2012/07/20 23:14:26
As I mentioned, you can look at the example in ala
| |
234 iter != iter_next; | |
235 iter = (iter_next == iter_end) ? iter_next : iter_next++) { | |
236 if (base::GetTerminationStatus(iter->first, NULL) != | |
237 base::TERMINATION_STATUS_STILL_RUNNING) { | |
238 base::CloseProcessHandle(iter->first); | |
239 metrics_map_.erase(iter); | |
240 } else { | |
241 // Prime the CPUUsage to be gathered next time. | |
242 iter->second->GetCPUUsage(); | |
243 } | |
244 } | |
245 | |
246 // Add new process handles. | |
247 base::ProcessId browser_pid = base::GetCurrentProcId(); | |
248 ChromeProcessList chrome_processes(GetRunningChromeProcesses(browser_pid)); | |
249 for (ChromeProcessList::const_iterator pid_iter = chrome_processes.begin(); | |
250 pid_iter != chrome_processes.end(); ++pid_iter) { | |
251 base::ProcessHandle process_handle; | |
252 if (base::OpenProcessHandle(*pid_iter, &process_handle)) { | |
253 #if !defined(OS_MACOSX) | |
254 linked_ptr<base::ProcessMetrics> process_metrics( | |
255 base::ProcessMetrics::CreateProcessMetrics(process_handle)); | |
256 #else | |
257 linked_ptr<base::ProcessMetrics> process_metrics( | |
258 base::ProcessMetrics::CreateProcessMetrics(process_handle, | |
259 content::BrowserChildProcessHost::GetPortProvider())); | |
260 #endif | |
261 if (!ContainsKey(metrics_map_, process_handle)) { | |
262 // Prime the CPUUsage to be gathered next time. | |
263 process_metrics->GetCPUUsage(); | |
264 | |
265 metrics_map_[process_handle] = process_metrics; | |
266 } | |
267 } | |
268 } | |
269 } | |
270 | |
147 void PerformanceMonitor::Observe(int type, | 271 void PerformanceMonitor::Observe(int type, |
148 const content::NotificationSource& source, | 272 const content::NotificationSource& source, |
149 const content::NotificationDetails& details) { | 273 const content::NotificationDetails& details) { |
150 switch (type) { | 274 switch (type) { |
151 case chrome::NOTIFICATION_EXTENSION_INSTALLED: { | 275 case chrome::NOTIFICATION_EXTENSION_INSTALLED: { |
152 const Extension* extension = content::Details<Extension>(details).ptr(); | 276 const Extension* extension = content::Details<Extension>(details).ptr(); |
153 AddEvent(util::CreateExtensionInstallEvent(base::Time::Now(), | 277 AddEvent(util::CreateExtensionInstallEvent(base::Time::Now(), |
154 extension->id(), | 278 extension->id(), |
155 extension->name(), | 279 extension->name(), |
156 extension->url().spec(), | 280 extension->url().spec(), |
(...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
203 break; | 327 break; |
204 } | 328 } |
205 default: { | 329 default: { |
206 NOTREACHED(); | 330 NOTREACHED(); |
207 break; | 331 break; |
208 } | 332 } |
209 } | 333 } |
210 } | 334 } |
211 | 335 |
212 } // namespace performance_monitor | 336 } // namespace performance_monitor |
OLD | NEW |