| 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 <stddef.h> | 7 #include <stddef.h> |
| 8 #include <utility> | 8 #include <utility> |
| 9 | 9 |
| 10 #include "base/memory/ptr_util.h" | 10 #include "base/memory/ptr_util.h" |
| 11 #include "base/process/process_iterator.h" | 11 #include "base/process/process_iterator.h" |
| 12 #include "base/strings/utf_string_conversions.h" | 12 #include "base/strings/utf_string_conversions.h" |
| 13 #include "base/time/time.h" | 13 #include "base/time/time.h" |
| 14 #include "content/public/browser/browser_child_process_host.h" | 14 #include "content/public/browser/browser_child_process_host.h" |
| 15 #include "content/public/browser/browser_child_process_host_iterator.h" | 15 #include "content/public/browser/browser_child_process_host_iterator.h" |
| 16 #include "content/public/browser/browser_thread.h" | 16 #include "content/public/browser/browser_thread.h" |
| 17 #include "content/public/browser/child_process_data.h" | 17 #include "content/public/browser/child_process_data.h" |
| 18 #include "content/public/browser/render_process_host.h" | 18 #include "content/public/browser/render_process_host.h" |
| 19 #include "content/public/browser/render_widget_host.h" |
| 20 #include "content/public/browser/render_widget_host_iterator.h" |
| 21 #include "content/public/browser/render_widget_host_view.h" |
| 19 #include "content/public/common/content_constants.h" | 22 #include "content/public/common/content_constants.h" |
| 20 #include "extensions/features/features.h" | 23 #include "extensions/features/features.h" |
| 21 | 24 |
| 22 #if BUILDFLAG(ENABLE_EXTENSIONS) | 25 #if BUILDFLAG(ENABLE_EXTENSIONS) |
| 23 #include "extensions/browser/extension_host.h" | 26 #include "extensions/browser/extension_host.h" |
| 24 #include "extensions/browser/extension_registry.h" | 27 #include "extensions/browser/extension_registry.h" |
| 25 #include "extensions/common/manifest_handlers/background_info.h" | 28 #include "extensions/common/manifest_handlers/background_info.h" |
| 26 #endif | 29 #endif |
| 27 | 30 |
| 28 using content::BrowserThread; | 31 using content::BrowserThread; |
| 29 | 32 |
| 30 namespace performance_monitor { | 33 namespace performance_monitor { |
| 31 | 34 |
| 32 namespace { | 35 namespace { |
| 33 | 36 |
| 34 // The default interval at which PerformanceMonitor performs its timed | 37 // The default interval at which PerformanceMonitor performs its timed |
| 35 // collections. | 38 // collections. |
| 36 const int kGatherIntervalInSeconds = 120; | 39 const int kGatherIntervalInSeconds = 120; |
| 37 | 40 |
| 38 base::LazyInstance<PerformanceMonitor> g_monitor = LAZY_INSTANCE_INITIALIZER; | 41 base::LazyInstance<PerformanceMonitor> g_monitor = LAZY_INSTANCE_INITIALIZER; |
| 39 | 42 |
| 40 void GatherMetricsForRenderProcess(content::RenderProcessHost* host, | 43 void GatherMetricsForRenderProcess( |
| 41 ProcessMetricsMetadata* data) { | 44 content::RenderProcessHost* host, |
| 45 const std::vector<content::RenderWidgetHost*>& widgets, |
| 46 ProcessMetricsMetadata* data) { |
| 42 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 47 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| 48 |
| 49 for (auto* widget : widgets) { |
| 50 DCHECK_EQ(host, widget->GetProcess()); |
| 51 |
| 52 content::RenderWidgetHostView* rwhv = widget->GetView(); |
| 53 if (rwhv && rwhv->IsRenderWidgetHostViewChildFrame()) { |
| 54 data->contains_oopif = true; |
| 55 break; |
| 56 } |
| 57 } |
| 58 |
| 43 #if BUILDFLAG(ENABLE_EXTENSIONS) | 59 #if BUILDFLAG(ENABLE_EXTENSIONS) |
| 44 content::BrowserContext* browser_context = host->GetBrowserContext(); | 60 content::BrowserContext* browser_context = host->GetBrowserContext(); |
| 45 extensions::ProcessMap* extension_process_map = | 61 extensions::ProcessMap* extension_process_map = |
| 46 extensions::ProcessMap::Get(browser_context); | 62 extensions::ProcessMap::Get(browser_context); |
| 47 | 63 |
| 48 std::set<std::string> extension_ids = | 64 std::set<std::string> extension_ids = |
| 49 extension_process_map->GetExtensionsInProcess(host->GetID()); | 65 extension_process_map->GetExtensionsInProcess(host->GetID()); |
| 50 | 66 |
| 51 // We only collect more granular metrics when there's only one extension | 67 // We only collect more granular metrics when there's only one extension |
| 52 // running in a given renderer, to reduce noise. | 68 // running in a given renderer, to reduce noise. |
| (...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 88 } | 104 } |
| 89 | 105 |
| 90 void PerformanceMonitor::GatherMetricsMapOnUIThread() { | 106 void PerformanceMonitor::GatherMetricsMapOnUIThread() { |
| 91 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 107 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| 92 | 108 |
| 93 static int current_update_sequence = 0; | 109 static int current_update_sequence = 0; |
| 94 // Even in the "somewhat" unlikely event this wraps around, | 110 // Even in the "somewhat" unlikely event this wraps around, |
| 95 // it doesn't matter. We just check it for inequality. | 111 // it doesn't matter. We just check it for inequality. |
| 96 current_update_sequence++; | 112 current_update_sequence++; |
| 97 | 113 |
| 114 // First pass, collate the widgets by process ID. |
| 115 std::map<content::RenderProcessHost*, std::vector<content::RenderWidgetHost*>> |
| 116 widgets_by_rph; |
| 117 std::unique_ptr<content::RenderWidgetHostIterator> widget_it( |
| 118 content::RenderWidgetHost::GetRenderWidgetHosts()); |
| 119 while (content::RenderWidgetHost* widget = widget_it->GetNextHost()) { |
| 120 // Ignore processes that don't have a connection, such as crashed tabs. |
| 121 if (!widget->GetProcess()->HasConnection()) |
| 122 continue; |
| 123 widgets_by_rph[widget->GetProcess()].push_back(widget); |
| 124 } |
| 125 |
| 98 // Find all render child processes; has to be done on the UI thread. | 126 // Find all render child processes; has to be done on the UI thread. |
| 99 for (content::RenderProcessHost::iterator rph_iter = | 127 for (content::RenderProcessHost::iterator rph_iter = |
| 100 content::RenderProcessHost::AllHostsIterator(); | 128 content::RenderProcessHost::AllHostsIterator(); |
| 101 !rph_iter.IsAtEnd(); rph_iter.Advance()) { | 129 !rph_iter.IsAtEnd(); rph_iter.Advance()) { |
| 102 content::RenderProcessHost* host = rph_iter.GetCurrentValue(); | 130 content::RenderProcessHost* host = rph_iter.GetCurrentValue(); |
| 103 ProcessMetricsMetadata data; | 131 ProcessMetricsMetadata data; |
| 104 data.process_type = content::PROCESS_TYPE_RENDERER; | 132 data.process_type = content::PROCESS_TYPE_RENDERER; |
| 105 data.handle = host->GetHandle(); | 133 data.handle = host->GetHandle(); |
| 106 | 134 |
| 107 GatherMetricsForRenderProcess(host, &data); | 135 GatherMetricsForRenderProcess(host, widgets_by_rph[host], &data); |
| 108 MarkProcessAsAlive(data, current_update_sequence); | 136 MarkProcessAsAlive(data, current_update_sequence); |
| 109 } | 137 } |
| 110 | 138 |
| 111 BrowserThread::PostTask( | 139 BrowserThread::PostTask( |
| 112 BrowserThread::IO, FROM_HERE, | 140 BrowserThread::IO, FROM_HERE, |
| 113 base::Bind(&PerformanceMonitor::GatherMetricsMapOnIOThread, | 141 base::Bind(&PerformanceMonitor::GatherMetricsMapOnIOThread, |
| 114 base::Unretained(this), current_update_sequence)); | 142 base::Unretained(this), current_update_sequence)); |
| 115 } | 143 } |
| 116 | 144 |
| 117 void PerformanceMonitor::MarkProcessAsAlive( | 145 void PerformanceMonitor::MarkProcessAsAlive( |
| (...skipping 89 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 207 | 235 |
| 208 void PerformanceMonitor::RunTriggersUIThread() { | 236 void PerformanceMonitor::RunTriggersUIThread() { |
| 209 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 237 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| 210 for (auto& metrics : metrics_map_) | 238 for (auto& metrics : metrics_map_) |
| 211 metrics.second->RunPerformanceTriggers(); | 239 metrics.second->RunPerformanceTriggers(); |
| 212 | 240 |
| 213 StartGatherCycle(); | 241 StartGatherCycle(); |
| 214 } | 242 } |
| 215 | 243 |
| 216 } // namespace performance_monitor | 244 } // namespace performance_monitor |
| OLD | NEW |