OLD | NEW |
1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 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/sessions/session_restore_stats_collector.h" | 5 #include "chrome/browser/sessions/session_restore_stats_collector.h" |
6 | 6 |
7 #include <string> | 7 #include <string> |
8 | 8 |
9 #include "base/metrics/histogram.h" | 9 #include "base/metrics/histogram.h" |
10 #include "base/strings/stringprintf.h" | 10 #include "base/strings/stringprintf.h" |
| 11 #include "base/time/default_tick_clock.h" |
11 #include "content/public/browser/notification_service.h" | 12 #include "content/public/browser/notification_service.h" |
12 #include "content/public/browser/notification_types.h" | 13 #include "content/public/browser/notification_types.h" |
13 #include "content/public/browser/render_widget_host_view.h" | 14 #include "content/public/browser/render_widget_host_view.h" |
14 #include "content/public/browser/web_contents.h" | 15 #include "content/public/browser/web_contents.h" |
15 | 16 |
| 17 namespace { |
| 18 |
16 using content::NavigationController; | 19 using content::NavigationController; |
17 using content::RenderWidgetHost; | 20 using content::RenderWidgetHost; |
| 21 using content::Source; |
18 using content::WebContents; | 22 using content::WebContents; |
19 | 23 |
20 // static | 24 const char kSessionRestoreActions[] = "SessionRestore.Actions"; |
21 void SessionRestoreStatsCollector::TrackTabs( | |
22 const std::vector<SessionRestoreDelegate::RestoredTab>& tabs, | |
23 const base::TimeTicks& restore_started) { | |
24 if (!shared_collector_) | |
25 shared_collector_ = new SessionRestoreStatsCollector(restore_started); | |
26 | 25 |
27 shared_collector_->AddTabs(tabs); | 26 // The enumeration values stored in the "SessionRestore.Actions" histogram. |
| 27 enum SessionRestoreActionsUma { |
| 28 // Counts the total number of session restores that have occurred. |
| 29 SESSION_RESTORE_ACTIONS_UMA_INITIATED = 0, |
| 30 // Counts the number of session restores that have seen deferred tab loadings |
| 31 // for whatever reason (almost certainly due to memory pressure). |
| 32 SESSION_RESTORE_ACTIONS_UMA_DEFERRED_TABS = 1, |
| 33 // The size of this enum. Must be the last entry. |
| 34 SESSION_RESTORE_ACTIONS_UMA_MAX, |
| 35 }; |
| 36 |
| 37 // The enumeration of values stored in the "SessionRestore.TabActions" |
| 38 // histogram. |
| 39 enum SessionRestoreTabActionsUma { |
| 40 // Incremented for each tab created in a session restore. |
| 41 SESSION_RESTORE_TAB_ACTIONS_UMA_TAB_CREATED = 0, |
| 42 // Incremented for each tab that session restore decides not to load. |
| 43 SESSION_RESTORE_TAB_ACTIONS_UMA_TAB_LOADING_DEFERRED = 1, |
| 44 // Incremented for each tab that is successfully loaded. |
| 45 SESSION_RESTORE_TAB_ACTIONS_UMA_TAB_LOADED = 2, |
| 46 // Incremented for each session-restore-deferred tab that is subsequently |
| 47 // loaded. |
| 48 SESSION_RESTORE_TAB_ACTIONS_UMA_DEFERRED_TAB_LOADED = 3, |
| 49 // The size of this enum. Must be the last entry. |
| 50 SESSION_RESTORE_TAB_ACTIONS_UMA_MAX, |
| 51 }; |
| 52 |
| 53 } // namespace |
| 54 |
| 55 SessionRestoreStatsCollector::TabLoaderStats::TabLoaderStats() |
| 56 : tab_count(0u), tabs_loaded(0u), parallel_tab_loads(0u) { |
28 } | 57 } |
29 | 58 |
30 // static | 59 SessionRestoreStatsCollector::TabState::TabState( |
31 void SessionRestoreStatsCollector::TrackActiveTabs( | 60 NavigationController* controller) |
32 const std::vector<SessionRestoreDelegate::RestoredTab>& tabs, | 61 : controller(controller), |
33 const base::TimeTicks& restore_started) { | 62 render_widget_host(nullptr), |
34 if (!shared_collector_) | 63 is_deferred(false), |
35 shared_collector_ = new SessionRestoreStatsCollector(restore_started); | 64 loading_state(TAB_IS_NOT_LOADING) { |
36 | |
37 std::vector<SessionRestoreDelegate::RestoredTab> active_tabs; | |
38 for (auto tab : tabs) { | |
39 if (tab.is_active()) | |
40 active_tabs.push_back(tab); | |
41 } | |
42 shared_collector_->AddTabs(active_tabs); | |
43 } | 65 } |
44 | 66 |
45 SessionRestoreStatsCollector::SessionRestoreStatsCollector( | 67 SessionRestoreStatsCollector::SessionRestoreStatsCollector( |
46 const base::TimeTicks& restore_started) | 68 const base::TimeTicks& restore_started, |
47 : got_first_foreground_load_(false), | 69 scoped_ptr<StatsReportingDelegate> reporting_delegate) |
| 70 : done_tracking_non_deferred_tabs_(false), |
| 71 got_first_foreground_load_(false), |
48 got_first_paint_(false), | 72 got_first_paint_(false), |
49 restore_started_(restore_started), | 73 restore_started_(restore_started), |
50 tab_count_(0), | 74 waiting_for_load_tab_count_(0u), |
51 max_parallel_tab_loads_(0) { | 75 loading_tab_count_(0u), |
| 76 tick_clock_(new base::DefaultTickClock()), |
| 77 reporting_delegate_(reporting_delegate.Pass()) { |
52 this_retainer_ = this; | 78 this_retainer_ = this; |
53 registrar_.Add( | |
54 this, content::NOTIFICATION_RENDER_WIDGET_HOST_DID_UPDATE_BACKING_STORE, | |
55 content::NotificationService::AllSources()); | |
56 } | 79 } |
57 | 80 |
58 SessionRestoreStatsCollector::~SessionRestoreStatsCollector() { | 81 SessionRestoreStatsCollector::~SessionRestoreStatsCollector() { |
59 DCHECK((got_first_paint_ || render_widget_hosts_to_paint_.empty()) && | 82 } |
60 tabs_tracked_.empty() && render_widget_hosts_loading_.empty()); | 83 |
61 DCHECK(shared_collector_ == this); | 84 void SessionRestoreStatsCollector::TrackTabs( |
62 shared_collector_ = nullptr; | 85 const std::vector<SessionRestoreDelegate::RestoredTab>& tabs) { |
| 86 DCHECK(!done_tracking_non_deferred_tabs_); |
| 87 |
| 88 // If this is the first call to TrackTabs then start observing events. |
| 89 if (tab_loader_stats_.tab_count == 0) { |
| 90 registrar_.Add( |
| 91 this, content::NOTIFICATION_RENDER_WIDGET_HOST_DID_UPDATE_BACKING_STORE, |
| 92 content::NotificationService::AllSources()); |
| 93 } |
| 94 |
| 95 tab_loader_stats_.tab_count += tabs.size(); |
| 96 waiting_for_load_tab_count_ += tabs.size(); |
| 97 for (auto& tab : tabs) { |
| 98 TabState* tab_state = RegisterForNotifications( |
| 99 &tab.contents()->GetController()); |
| 100 // Active tabs have already started loading. |
| 101 if (tab.is_active()) |
| 102 MarkTabAsLoading(tab_state); |
| 103 } |
| 104 } |
| 105 |
| 106 void SessionRestoreStatsCollector::DeferTab(NavigationController* tab) { |
| 107 TabState* tab_state = GetTabState(tab); |
| 108 |
| 109 // If the tab is no longer being tracked it has already finished loading. |
| 110 // This can occur if the user forces the tab to load before the entire session |
| 111 // restore is over, and the TabLoader then decides it would defer loading of |
| 112 // that tab. |
| 113 if (!tab_state) |
| 114 return; |
| 115 |
| 116 // Mark this tab as deferred, if its still being tracked. A tab should not be |
| 117 // marked as deferred twice. |
| 118 DCHECK(!tab_state->is_deferred); |
| 119 tab_state->is_deferred = true; |
| 120 |
| 121 // A tab that didn't start loading before it was deferred is not to be |
| 122 // actively monitored for loading. |
| 123 if (tab_state->loading_state == TAB_IS_NOT_LOADING) { |
| 124 DCHECK_LT(0u, waiting_for_load_tab_count_); |
| 125 if (--waiting_for_load_tab_count_ == 0) |
| 126 CleanupIfDoneTracking(); |
| 127 } |
| 128 |
| 129 reporting_delegate_->ReportTabDeferred(); |
63 } | 130 } |
64 | 131 |
65 void SessionRestoreStatsCollector::Observe( | 132 void SessionRestoreStatsCollector::Observe( |
66 int type, | 133 int type, |
67 const content::NotificationSource& source, | 134 const content::NotificationSource& source, |
68 const content::NotificationDetails& details) { | 135 const content::NotificationDetails& details) { |
69 switch (type) { | 136 switch (type) { |
70 case content::NOTIFICATION_LOAD_START: { | 137 case content::NOTIFICATION_LOAD_START: { |
71 // Add this render_widget_host to the set of those we're waiting for | 138 // This occurs when a tab has started to load. This can be because of |
72 // paints on. We want to only record stats for paints that occur after | 139 // the tab loader (only for non-deferred tabs) or because the user clicked |
73 // a load has finished. | 140 // on the tab. |
74 NavigationController* tab = | 141 NavigationController* tab = Source<NavigationController>(source).ptr(); |
75 content::Source<NavigationController>(source).ptr(); | 142 TabState* tab_state = GetTabState(tab); |
76 RenderWidgetHost* render_widget_host = GetRenderWidgetHost(tab); | 143 MarkTabAsLoading(tab_state); |
77 DCHECK(render_widget_host); | |
78 render_widget_hosts_loading_.insert(render_widget_host); | |
79 break; | 144 break; |
80 } | 145 } |
81 case content::NOTIFICATION_WEB_CONTENTS_DESTROYED: { | 146 case content::NOTIFICATION_WEB_CONTENTS_DESTROYED: { |
82 WebContents* web_contents = content::Source<WebContents>(source).ptr(); | 147 // This happens when a tab has been closed. A tab can be in any state |
83 RemoveTab(&web_contents->GetController()); | 148 // when this occurs. Simply stop tracking the tab. |
| 149 WebContents* web_contents = Source<WebContents>(source).ptr(); |
| 150 NavigationController* tab = &web_contents->GetController(); |
| 151 RemoveTab(tab); |
| 152 |
| 153 // It is possible for all restored contents to be destroyed before a |
| 154 // first paint has arrived. This can be detected by |tabs_tracked_| being |
| 155 // empty and |got_first_paint_| still being false. At this point the paint |
| 156 // mechanism can be disabled and stats collection will stop. |
| 157 if (tabs_tracked_.empty() && !got_first_paint_) { |
| 158 got_first_paint_ = true; |
| 159 registrar_.Remove( |
| 160 this, |
| 161 content::NOTIFICATION_RENDER_WIDGET_HOST_DID_UPDATE_BACKING_STORE, |
| 162 content::NotificationService::AllSources()); |
| 163 } |
| 164 |
84 break; | 165 break; |
85 } | 166 } |
86 case content::NOTIFICATION_LOAD_STOP: { | 167 case content::NOTIFICATION_LOAD_STOP: { |
87 NavigationController* tab = | 168 // This occurs to loading tabs when they have finished loading. The tab |
88 content::Source<NavigationController>(source).ptr(); | 169 // may or may not already have painted at this point. |
89 RenderWidgetHost* render_widget_host = GetRenderWidgetHost(tab); | 170 |
90 render_widget_hosts_to_paint_.insert(render_widget_host); | 171 // Update the tab state and any global state as necessary. |
91 RemoveTab(tab); | 172 NavigationController* tab = Source<NavigationController>(source).ptr(); |
92 if (!got_first_foreground_load_ && render_widget_host && | 173 TabState* tab_state = GetTabState(tab); |
| 174 DCHECK(tab_state); |
| 175 tab_state->loading_state = TAB_IS_LOADED; |
| 176 DCHECK_LT(0u, loading_tab_count_); |
| 177 --loading_tab_count_; |
| 178 if (!tab_state->is_deferred) { |
| 179 DCHECK_LT(0u, waiting_for_load_tab_count_); |
| 180 --waiting_for_load_tab_count_; |
| 181 } |
| 182 |
| 183 if (tab_state->is_deferred) { |
| 184 reporting_delegate_->ReportDeferredTabLoaded(); |
| 185 } else { |
| 186 DCHECK(!done_tracking_non_deferred_tabs_); |
| 187 ++tab_loader_stats_.tabs_loaded; |
| 188 } |
| 189 |
| 190 // Update statistics for foreground tabs. |
| 191 base::TimeDelta time_to_load = tick_clock_->NowTicks() - restore_started_; |
| 192 if (!got_first_foreground_load_ && |
| 193 tab_state->render_widget_host && |
| 194 tab_state->render_widget_host->GetView() && |
| 195 tab_state->render_widget_host->GetView()->IsShowing()) { |
| 196 got_first_foreground_load_ = true; |
| 197 DCHECK(!done_tracking_non_deferred_tabs_); |
| 198 tab_loader_stats_.foreground_tab_first_loaded = time_to_load; |
| 199 } |
| 200 |
| 201 // Update statistics for all tabs, if this wasn't a deferred tab. This is |
| 202 // done here and not in CleanupIfDoneTracking because it is possible to |
| 203 // wait for a paint long after all loads have completed. |
| 204 if (!done_tracking_non_deferred_tabs_ && !tab_state->is_deferred) |
| 205 tab_loader_stats_.non_deferred_tabs_loaded = time_to_load; |
| 206 |
| 207 // By default tabs transition to being tracked for paint events after the |
| 208 // load event has been seen. However, if the first paint event has already |
| 209 // been seen then this is not necessary and the tab can be removed. |
| 210 if (got_first_paint_) |
| 211 RemoveTab(tab); |
| 212 |
| 213 break; |
| 214 } |
| 215 case content::NOTIFICATION_RENDER_WIDGET_HOST_DID_UPDATE_BACKING_STORE: { |
| 216 // This notification is across all tabs in the browser so notifications |
| 217 // will arrive for tabs that the collector is not explicitly tracking. |
| 218 |
| 219 // Only process this event if first paint hasn't been seen and this is a |
| 220 // paint of a visible tab. |
| 221 RenderWidgetHost* render_widget_host = |
| 222 Source<RenderWidgetHost>(source).ptr(); |
| 223 if (!got_first_paint_ && |
93 render_widget_host->GetView() && | 224 render_widget_host->GetView() && |
94 render_widget_host->GetView()->IsShowing()) { | 225 render_widget_host->GetView()->IsShowing()) { |
95 got_first_foreground_load_ = true; | 226 got_first_paint_ = true; |
96 base::TimeDelta time_to_load = | 227 TabState* tab_state = GetTabState(render_widget_host); |
97 base::TimeTicks::Now() - restore_started_; | 228 if (tab_state) { |
98 UMA_HISTOGRAM_CUSTOM_TIMES("SessionRestore.ForegroundTabFirstLoaded", | 229 // This is a paint for a tab that is explicitly being tracked so |
99 time_to_load, | 230 // update the statistics. Otherwise the host is for a tab that's not |
100 base::TimeDelta::FromMilliseconds(10), | 231 // being tracked thus some other tab has visibility and has rendered |
101 base::TimeDelta::FromSeconds(100), 100); | 232 // and there's no point in tracking the time to first paint. This can |
102 // Record a time for the number of tabs, to help track down | 233 // happen because the user opened a different tab or restored tabs |
103 // contention. | 234 // to an already existing browser and an existing tab was in the |
104 std::string time_for_count = base::StringPrintf( | 235 // foreground. |
105 "SessionRestore.ForegroundTabFirstLoaded_%d", tab_count_); | 236 base::TimeDelta time_to_paint = |
106 base::HistogramBase* counter_for_count = | 237 tick_clock_->NowTicks() - restore_started_; |
107 base::Histogram::FactoryTimeGet( | 238 DCHECK(!done_tracking_non_deferred_tabs_); |
108 time_for_count, base::TimeDelta::FromMilliseconds(10), | 239 tab_loader_stats_.foreground_tab_first_paint = time_to_paint; |
109 base::TimeDelta::FromSeconds(100), 100, | 240 } |
110 base::Histogram::kUmaTargetedHistogramFlag); | 241 |
111 counter_for_count->AddTime(time_to_load); | 242 // Once first paint has been observed the entire to-paint tracking |
| 243 // mechanism is no longer needed. |
| 244 registrar_.Remove( |
| 245 this, |
| 246 content::NOTIFICATION_RENDER_WIDGET_HOST_DID_UPDATE_BACKING_STORE, |
| 247 content::NotificationService::AllSources()); |
| 248 |
| 249 // Remove any tabs that have loaded. These were only being kept around |
| 250 // while waiting for a paint event. |
| 251 std::vector<NavigationController*> loaded_tabs; |
| 252 for (auto& map_entry : tabs_tracked_) { |
| 253 TabState& tab_state = map_entry.second; |
| 254 if (tab_state.loading_state == TAB_IS_LOADED) |
| 255 loaded_tabs.push_back(tab_state.controller); |
| 256 } |
| 257 for (auto& tab : loaded_tabs) |
| 258 RemoveTab(tab); |
112 } | 259 } |
113 break; | 260 break; |
114 } | 261 } |
115 case content::NOTIFICATION_RENDER_WIDGET_HOST_DID_UPDATE_BACKING_STORE: { | |
116 RenderWidgetHost* render_widget_host = | |
117 content::Source<RenderWidgetHost>(source).ptr(); | |
118 if (!got_first_paint_ && render_widget_host->GetView() && | |
119 render_widget_host->GetView()->IsShowing()) { | |
120 if (render_widget_hosts_to_paint_.find(render_widget_host) != | |
121 render_widget_hosts_to_paint_.end()) { | |
122 // Got a paint for one of our renderers, so record time. | |
123 got_first_paint_ = true; | |
124 base::TimeDelta time_to_paint = | |
125 base::TimeTicks::Now() - restore_started_; | |
126 // TODO(danduong): to remove this with 467680, to make sure we | |
127 // don't forget to clean this up. | |
128 UMA_HISTOGRAM_CUSTOM_TIMES("SessionRestore.ForegroundTabFirstPaint", | |
129 time_to_paint, | |
130 base::TimeDelta::FromMilliseconds(10), | |
131 base::TimeDelta::FromSeconds(100), 100); | |
132 // Record a time for the number of tabs, to help track down | |
133 // contention. | |
134 std::string time_for_count = base::StringPrintf( | |
135 "SessionRestore.ForegroundTabFirstPaint_%d", tab_count_); | |
136 base::HistogramBase* counter_for_count = | |
137 base::Histogram::FactoryTimeGet( | |
138 time_for_count, base::TimeDelta::FromMilliseconds(10), | |
139 base::TimeDelta::FromSeconds(100), 100, | |
140 base::Histogram::kUmaTargetedHistogramFlag); | |
141 counter_for_count->AddTime(time_to_paint); | |
142 UMA_HISTOGRAM_CUSTOM_TIMES("SessionRestore.ForegroundTabFirstPaint2", | |
143 time_to_paint, | |
144 base::TimeDelta::FromMilliseconds(100), | |
145 base::TimeDelta::FromMinutes(16), 50); | |
146 // Record a time for the number of tabs, to help track down | |
147 // contention. | |
148 std::string time_for_count2 = base::StringPrintf( | |
149 "SessionRestore.ForegroundTabFirstPaint2_%d", tab_count_); | |
150 base::HistogramBase* counter_for_count2 = | |
151 base::Histogram::FactoryTimeGet( | |
152 time_for_count2, base::TimeDelta::FromMilliseconds(100), | |
153 base::TimeDelta::FromMinutes(16), 50, | |
154 base::Histogram::kUmaTargetedHistogramFlag); | |
155 counter_for_count2->AddTime(time_to_paint); | |
156 } else if (render_widget_hosts_loading_.find(render_widget_host) == | |
157 render_widget_hosts_loading_.end()) { | |
158 // If this is a host for a tab we're not loading some other tab | |
159 // has rendered and there's no point tracking the time. This could | |
160 // happen because the user opened a different tab or restored tabs | |
161 // to an already existing browser and an existing tab painted. | |
162 got_first_paint_ = true; | |
163 } | |
164 } | |
165 break; | |
166 } | |
167 default: | 262 default: |
168 NOTREACHED() << "Unknown notification received:" << type; | 263 NOTREACHED() << "Unknown notification received:" << type; |
169 break; | 264 break; |
170 } | 265 } |
171 | 266 |
172 // Check if we are done and if so, reset |this_retainer_| as the collector no | 267 CleanupIfDoneTracking(); |
173 // longer needs to stay alive. | |
174 if ((got_first_paint_ || render_widget_hosts_to_paint_.empty()) && | |
175 tabs_tracked_.empty() && render_widget_hosts_loading_.empty()) | |
176 this_retainer_ = nullptr; | |
177 } | |
178 | |
179 void SessionRestoreStatsCollector::AddTabs( | |
180 const std::vector<SessionRestoreDelegate::RestoredTab>& tabs) { | |
181 tab_count_ += tabs.size(); | |
182 for (auto& tab : tabs) { | |
183 RegisterForNotifications(&tab.contents()->GetController()); | |
184 if (tab.is_active()) { | |
185 RenderWidgetHost* render_widget_host = | |
186 GetRenderWidgetHost(&tab.contents()->GetController()); | |
187 render_widget_hosts_loading_.insert(render_widget_host); | |
188 } | |
189 } | |
190 } | 268 } |
191 | 269 |
192 void SessionRestoreStatsCollector::RemoveTab(NavigationController* tab) { | 270 void SessionRestoreStatsCollector::RemoveTab(NavigationController* tab) { |
| 271 // Stop observing this tab. |
193 registrar_.Remove(this, content::NOTIFICATION_WEB_CONTENTS_DESTROYED, | 272 registrar_.Remove(this, content::NOTIFICATION_WEB_CONTENTS_DESTROYED, |
194 content::Source<WebContents>(tab->GetWebContents())); | 273 Source<WebContents>(tab->GetWebContents())); |
195 registrar_.Remove(this, content::NOTIFICATION_LOAD_STOP, | 274 registrar_.Remove(this, content::NOTIFICATION_LOAD_STOP, |
196 content::Source<NavigationController>(tab)); | 275 Source<NavigationController>(tab)); |
197 registrar_.Remove(this, content::NOTIFICATION_LOAD_START, | 276 registrar_.Remove(this, content::NOTIFICATION_LOAD_START, |
198 content::Source<NavigationController>(tab)); | 277 Source<NavigationController>(tab)); |
199 if (render_widget_hosts_loading_.size() > max_parallel_tab_loads_) | |
200 max_parallel_tab_loads_ = render_widget_hosts_loading_.size(); | |
201 RenderWidgetHost* render_widget_host = GetRenderWidgetHost(tab); | |
202 render_widget_hosts_loading_.erase(render_widget_host); | |
203 tabs_tracked_.erase(tab); | |
204 | 278 |
205 // If there are no more tabs loading or being tracked, restore is done and | 279 auto tab_it = tabs_tracked_.find(tab); |
206 // record the time. Note that we are not yet finished, as we might still be | 280 DCHECK(tab_it != tabs_tracked_.end()); |
207 // waiting for our first paint, which can happen after all tabs are done | 281 TabState& tab_state = tab_it->second; |
208 // loading. | |
209 // TODO(georgesak): review behaviour of ForegroundTabFirstPaint. | |
210 if (tabs_tracked_.empty() && render_widget_hosts_loading_.empty()) { | |
211 base::TimeDelta time_to_load = base::TimeTicks::Now() - restore_started_; | |
212 UMA_HISTOGRAM_CUSTOM_TIMES("SessionRestore.AllTabsLoaded", time_to_load, | |
213 base::TimeDelta::FromMilliseconds(10), | |
214 base::TimeDelta::FromSeconds(100), 100); | |
215 // Record a time for the number of tabs, to help track down contention. | |
216 std::string time_for_count = | |
217 base::StringPrintf("SessionRestore.AllTabsLoaded_%d", tab_count_); | |
218 base::HistogramBase* counter_for_count = base::Histogram::FactoryTimeGet( | |
219 time_for_count, base::TimeDelta::FromMilliseconds(10), | |
220 base::TimeDelta::FromSeconds(100), 100, | |
221 base::Histogram::kUmaTargetedHistogramFlag); | |
222 counter_for_count->AddTime(time_to_load); | |
223 | 282 |
224 UMA_HISTOGRAM_COUNTS_100("SessionRestore.ParallelTabLoads", | 283 // If this tab was waiting for a NOTIFICATION_LOAD_STOP event then update |
225 max_parallel_tab_loads_); | 284 // the loading counts. |
| 285 if (tab_state.loading_state == TAB_IS_LOADING) { |
| 286 DCHECK_LT(0u, loading_tab_count_); |
| 287 --loading_tab_count_; |
226 } | 288 } |
| 289 if (tab_state.loading_state != TAB_IS_LOADED) { |
| 290 DCHECK_LT(0u, waiting_for_load_tab_count_); |
| 291 // It's possible for waiting_for_load_tab_count_ to reach zero here. This |
| 292 // function is only called from 'Observe', so the transition will be |
| 293 // noticed there. |
| 294 --waiting_for_load_tab_count_; |
| 295 } |
| 296 |
| 297 // Remove the tab from the |tracked_tabs_| map. |
| 298 tabs_tracked_.erase(tab_it); |
227 } | 299 } |
228 | 300 |
229 void SessionRestoreStatsCollector::RegisterForNotifications( | 301 SessionRestoreStatsCollector::TabState* |
| 302 SessionRestoreStatsCollector::RegisterForNotifications( |
230 NavigationController* tab) { | 303 NavigationController* tab) { |
231 registrar_.Add(this, content::NOTIFICATION_WEB_CONTENTS_DESTROYED, | 304 registrar_.Add(this, content::NOTIFICATION_WEB_CONTENTS_DESTROYED, |
232 content::Source<WebContents>(tab->GetWebContents())); | 305 Source<WebContents>(tab->GetWebContents())); |
233 registrar_.Add(this, content::NOTIFICATION_LOAD_STOP, | 306 registrar_.Add(this, content::NOTIFICATION_LOAD_STOP, |
234 content::Source<NavigationController>(tab)); | 307 Source<NavigationController>(tab)); |
235 registrar_.Add(this, content::NOTIFICATION_LOAD_START, | 308 registrar_.Add(this, content::NOTIFICATION_LOAD_START, |
236 content::Source<NavigationController>(tab)); | 309 Source<NavigationController>(tab)); |
237 tabs_tracked_.insert(tab); | 310 auto result = tabs_tracked_.insert(std::make_pair(tab, TabState(tab))); |
| 311 DCHECK(result.second); |
| 312 TabState* tab_state = &result.first->second; |
| 313 return tab_state; |
238 } | 314 } |
239 | 315 |
240 RenderWidgetHost* SessionRestoreStatsCollector::GetRenderWidgetHost( | 316 RenderWidgetHost* SessionRestoreStatsCollector::GetRenderWidgetHost( |
241 NavigationController* tab) { | 317 NavigationController* tab) { |
242 WebContents* web_contents = tab->GetWebContents(); | 318 WebContents* web_contents = tab->GetWebContents(); |
243 if (web_contents) { | 319 if (web_contents) { |
244 content::RenderWidgetHostView* render_widget_host_view = | 320 content::RenderWidgetHostView* render_widget_host_view = |
245 web_contents->GetRenderWidgetHostView(); | 321 web_contents->GetRenderWidgetHostView(); |
246 if (render_widget_host_view) | 322 if (render_widget_host_view) |
247 return render_widget_host_view->GetRenderWidgetHost(); | 323 return render_widget_host_view->GetRenderWidgetHost(); |
248 } | 324 } |
249 return nullptr; | 325 return nullptr; |
250 } | 326 } |
251 | 327 |
252 // static | 328 SessionRestoreStatsCollector::TabState* |
253 SessionRestoreStatsCollector* SessionRestoreStatsCollector::shared_collector_ = | 329 SessionRestoreStatsCollector::GetTabState(NavigationController* tab) { |
254 nullptr; | 330 // This lookup can fail because DeferTab calls can arrive for tabs that have |
| 331 // already loaded (user forced) and are no longer tracked. |
| 332 auto it = tabs_tracked_.find(tab); |
| 333 if (it == tabs_tracked_.end()) |
| 334 return nullptr; |
| 335 return &it->second; |
| 336 } |
| 337 |
| 338 SessionRestoreStatsCollector::TabState* |
| 339 SessionRestoreStatsCollector::GetTabState(RenderWidgetHost* tab) { |
| 340 for (auto& pair : tabs_tracked_) { |
| 341 if (pair.second.render_widget_host == tab) |
| 342 return &pair.second; |
| 343 } |
| 344 // It's possible for this lookup to fail as paint events can be received for |
| 345 // tabs that aren't being tracked. |
| 346 return nullptr; |
| 347 } |
| 348 |
| 349 void SessionRestoreStatsCollector::MarkTabAsLoading(TabState* tab_state) { |
| 350 DCHECK_EQ(TAB_IS_NOT_LOADING, tab_state->loading_state); |
| 351 if (tab_state->loading_state != TAB_IS_NOT_LOADING) |
| 352 return; |
| 353 tab_state->loading_state = TAB_IS_LOADING; |
| 354 ++loading_tab_count_; |
| 355 |
| 356 if (!done_tracking_non_deferred_tabs_) { |
| 357 tab_loader_stats_.parallel_tab_loads = std::max( |
| 358 tab_loader_stats_.parallel_tab_loads, |
| 359 loading_tab_count_); |
| 360 } |
| 361 |
| 362 // Get the RenderWidgetHost for the tab and add it to the secondary index. |
| 363 RenderWidgetHost* render_widget_host = GetRenderWidgetHost( |
| 364 tab_state->controller); |
| 365 DCHECK(render_widget_host); |
| 366 tab_state->render_widget_host = render_widget_host; |
| 367 } |
| 368 |
| 369 void SessionRestoreStatsCollector::CleanupIfDoneTracking() { |
| 370 // If non-deferred tabs are no longer being tracked then report tab loader |
| 371 // statistics. |
| 372 if (!done_tracking_non_deferred_tabs_ && got_first_paint_ && |
| 373 waiting_for_load_tab_count_ == 0) { |
| 374 done_tracking_non_deferred_tabs_ = true; |
| 375 reporting_delegate_->ReportTabLoaderStats(tab_loader_stats_); |
| 376 } |
| 377 |
| 378 // If tracking is completely finished then emit collected metrics and destroy |
| 379 // this stats collector. |
| 380 if (done_tracking_non_deferred_tabs_ && tabs_tracked_.empty()) |
| 381 this_retainer_ = nullptr; |
| 382 } |
| 383 |
| 384 void SessionRestoreStatsCollector::set_tick_clock( |
| 385 scoped_ptr<base::TickClock> tick_clock) { |
| 386 tick_clock_ = tick_clock.Pass(); |
| 387 } |
| 388 |
| 389 |
| 390 SessionRestoreStatsCollector::UmaStatsReportingDelegate:: |
| 391 UmaStatsReportingDelegate() |
| 392 : got_report_tab_deferred_(false) { |
| 393 } |
| 394 |
| 395 void SessionRestoreStatsCollector::UmaStatsReportingDelegate:: |
| 396 ReportTabLoaderStats(const TabLoaderStats& tab_loader_stats) { |
| 397 UMA_HISTOGRAM_COUNTS_100("SessionRestore.TabCount", |
| 398 tab_loader_stats.tab_count); |
| 399 |
| 400 UMA_HISTOGRAM_ENUMERATION(kSessionRestoreActions, |
| 401 SESSION_RESTORE_ACTIONS_UMA_INITIATED, |
| 402 SESSION_RESTORE_ACTIONS_UMA_MAX); |
| 403 |
| 404 for (size_t i = 0; i < tab_loader_stats.tab_count; ++i) { |
| 405 UMA_HISTOGRAM_ENUMERATION("SessionRestore.TabActions", |
| 406 SESSION_RESTORE_TAB_ACTIONS_UMA_TAB_CREATED, |
| 407 SESSION_RESTORE_TAB_ACTIONS_UMA_MAX); |
| 408 } |
| 409 |
| 410 for (size_t i = 0; i < tab_loader_stats.tabs_loaded; ++i) { |
| 411 UMA_HISTOGRAM_ENUMERATION("SessionRestore.TabActions", |
| 412 SESSION_RESTORE_TAB_ACTIONS_UMA_TAB_LOADED, |
| 413 SESSION_RESTORE_TAB_ACTIONS_UMA_MAX); |
| 414 } |
| 415 |
| 416 if (!tab_loader_stats.foreground_tab_first_loaded.is_zero()) { |
| 417 UMA_HISTOGRAM_CUSTOM_TIMES("SessionRestore.ForegroundTabFirstLoaded", |
| 418 tab_loader_stats.foreground_tab_first_loaded, |
| 419 base::TimeDelta::FromMilliseconds(10), |
| 420 base::TimeDelta::FromSeconds(100), 100); |
| 421 |
| 422 // Record a time for the number of tabs, to help track down contention. |
| 423 std::string time_for_count = base::StringPrintf( |
| 424 "SessionRestore.ForegroundTabFirstLoaded_%d", |
| 425 tab_loader_stats.tab_count); |
| 426 base::HistogramBase* counter_for_count = base::Histogram::FactoryTimeGet( |
| 427 time_for_count, base::TimeDelta::FromMilliseconds(10), |
| 428 base::TimeDelta::FromSeconds(100), 100, |
| 429 base::Histogram::kUmaTargetedHistogramFlag); |
| 430 counter_for_count->AddTime(tab_loader_stats.foreground_tab_first_loaded); |
| 431 } |
| 432 |
| 433 if (!tab_loader_stats.foreground_tab_first_paint.is_zero()) { |
| 434 UMA_HISTOGRAM_CUSTOM_TIMES( |
| 435 "SessionRestore.ForegroundTabFirstPaint3", |
| 436 tab_loader_stats.foreground_tab_first_paint, |
| 437 base::TimeDelta::FromMilliseconds(100), |
| 438 base::TimeDelta::FromMinutes(16), 50); |
| 439 |
| 440 std::string time_for_count = base::StringPrintf( |
| 441 "SessionRestore.ForegroundTabFirstPaint3_%d", |
| 442 tab_loader_stats.tab_count); |
| 443 base::HistogramBase* counter_for_count = base::Histogram::FactoryTimeGet( |
| 444 time_for_count, base::TimeDelta::FromMilliseconds(100), |
| 445 base::TimeDelta::FromMinutes(16), 50, |
| 446 base::Histogram::kUmaTargetedHistogramFlag); |
| 447 counter_for_count->AddTime(tab_loader_stats.foreground_tab_first_paint); |
| 448 } |
| 449 |
| 450 if (!tab_loader_stats.non_deferred_tabs_loaded.is_zero()) { |
| 451 UMA_HISTOGRAM_CUSTOM_TIMES("SessionRestore.AllTabsLoaded", |
| 452 tab_loader_stats.non_deferred_tabs_loaded, |
| 453 base::TimeDelta::FromMilliseconds(10), |
| 454 base::TimeDelta::FromSeconds(100), 100); |
| 455 |
| 456 // Record a time for the number of tabs, to help track down contention. |
| 457 std::string time_for_count = |
| 458 base::StringPrintf("SessionRestore.AllTabsLoaded_%d", |
| 459 tab_loader_stats.tab_count); |
| 460 base::HistogramBase* counter_for_count = base::Histogram::FactoryTimeGet( |
| 461 time_for_count, base::TimeDelta::FromMilliseconds(10), |
| 462 base::TimeDelta::FromSeconds(100), 100, |
| 463 base::Histogram::kUmaTargetedHistogramFlag); |
| 464 counter_for_count->AddTime(tab_loader_stats.non_deferred_tabs_loaded); |
| 465 } |
| 466 |
| 467 UMA_HISTOGRAM_COUNTS_100("SessionRestore.ParallelTabLoads", |
| 468 tab_loader_stats.parallel_tab_loads); |
| 469 } |
| 470 |
| 471 void SessionRestoreStatsCollector::UmaStatsReportingDelegate:: |
| 472 ReportTabDeferred() { |
| 473 if (!got_report_tab_deferred_) { |
| 474 got_report_tab_deferred_ = true; |
| 475 |
| 476 UMA_HISTOGRAM_ENUMERATION(kSessionRestoreActions, |
| 477 SESSION_RESTORE_ACTIONS_UMA_DEFERRED_TABS, |
| 478 SESSION_RESTORE_ACTIONS_UMA_MAX); |
| 479 } |
| 480 |
| 481 UMA_HISTOGRAM_ENUMERATION( |
| 482 "SessionRestore.TabActions", |
| 483 SESSION_RESTORE_TAB_ACTIONS_UMA_TAB_LOADING_DEFERRED, |
| 484 SESSION_RESTORE_TAB_ACTIONS_UMA_MAX); |
| 485 } |
| 486 |
| 487 void SessionRestoreStatsCollector::UmaStatsReportingDelegate:: |
| 488 ReportDeferredTabLoaded() { |
| 489 UMA_HISTOGRAM_ENUMERATION( |
| 490 "SessionRestore.TabActions", |
| 491 SESSION_RESTORE_TAB_ACTIONS_UMA_DEFERRED_TAB_LOADED, |
| 492 SESSION_RESTORE_TAB_ACTIONS_UMA_MAX); |
| 493 } |
OLD | NEW |