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"; |
Alexei Svitkine (slow)
2015/06/18 17:48:48
Can this be inlined in the macro? (After you make
chrisha
2015/06/18 20:09:04
Done.
| |
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) { | |
Alexei Svitkine (slow)
2015/06/18 17:48:48
Nit: const auto&
chrisha
2015/06/18 20:09:04
Done.
| |
98 TabState* tab_state = | |
99 RegisterForNotifications(&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 ReleaseIfDoneTracking(); | |
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); |
93 render_widget_host->GetView() && | 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_ && tab_state->render_widget_host && | |
193 tab_state->render_widget_host->GetView() && | |
194 tab_state->render_widget_host->GetView()->IsShowing()) { | |
195 got_first_foreground_load_ = true; | |
196 DCHECK(!done_tracking_non_deferred_tabs_); | |
197 tab_loader_stats_.foreground_tab_first_loaded = time_to_load; | |
198 } | |
199 | |
200 // Update statistics for all tabs, if this wasn't a deferred tab. This is | |
201 // done here and not in ReleaseIfDoneTracking because it is possible to | |
202 // wait for a paint long after all loads have completed. | |
203 if (!done_tracking_non_deferred_tabs_ && !tab_state->is_deferred) | |
204 tab_loader_stats_.non_deferred_tabs_loaded = time_to_load; | |
205 | |
206 // By default tabs transition to being tracked for paint events after the | |
207 // load event has been seen. However, if the first paint event has already | |
208 // been seen then this is not necessary and the tab can be removed. | |
209 if (got_first_paint_) | |
210 RemoveTab(tab); | |
211 | |
212 break; | |
213 } | |
214 case content::NOTIFICATION_RENDER_WIDGET_HOST_DID_UPDATE_BACKING_STORE: { | |
215 // This notification is across all tabs in the browser so notifications | |
216 // will arrive for tabs that the collector is not explicitly tracking. | |
217 | |
218 // Only process this event if first paint hasn't been seen and this is a | |
219 // paint of a visible tab. | |
220 RenderWidgetHost* render_widget_host = | |
221 Source<RenderWidgetHost>(source).ptr(); | |
222 if (!got_first_paint_ && render_widget_host->GetView() && | |
94 render_widget_host->GetView()->IsShowing()) { | 223 render_widget_host->GetView()->IsShowing()) { |
95 got_first_foreground_load_ = true; | 224 got_first_paint_ = true; |
96 base::TimeDelta time_to_load = | 225 TabState* tab_state = GetTabState(render_widget_host); |
97 base::TimeTicks::Now() - restore_started_; | 226 if (tab_state) { |
98 UMA_HISTOGRAM_CUSTOM_TIMES("SessionRestore.ForegroundTabFirstLoaded", | 227 // This is a paint for a tab that is explicitly being tracked so |
99 time_to_load, | 228 // update the statistics. Otherwise the host is for a tab that's not |
100 base::TimeDelta::FromMilliseconds(10), | 229 // being tracked thus some other tab has visibility and has rendered |
101 base::TimeDelta::FromSeconds(100), 100); | 230 // 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 | 231 // happen because the user opened a different tab or restored tabs |
103 // contention. | 232 // to an already existing browser and an existing tab was in the |
104 std::string time_for_count = base::StringPrintf( | 233 // foreground. |
105 "SessionRestore.ForegroundTabFirstLoaded_%d", tab_count_); | 234 base::TimeDelta time_to_paint = |
106 base::HistogramBase* counter_for_count = | 235 tick_clock_->NowTicks() - restore_started_; |
107 base::Histogram::FactoryTimeGet( | 236 DCHECK(!done_tracking_non_deferred_tabs_); |
108 time_for_count, base::TimeDelta::FromMilliseconds(10), | 237 tab_loader_stats_.foreground_tab_first_paint = time_to_paint; |
109 base::TimeDelta::FromSeconds(100), 100, | 238 } |
110 base::Histogram::kUmaTargetedHistogramFlag); | 239 |
111 counter_for_count->AddTime(time_to_load); | 240 // Once first paint has been observed the entire to-paint tracking |
241 // mechanism is no longer needed. | |
242 registrar_.Remove( | |
243 this, | |
244 content::NOTIFICATION_RENDER_WIDGET_HOST_DID_UPDATE_BACKING_STORE, | |
245 content::NotificationService::AllSources()); | |
246 | |
247 // Remove any tabs that have loaded. These were only being kept around | |
248 // while waiting for a paint event. | |
249 std::vector<NavigationController*> loaded_tabs; | |
250 for (auto& map_entry : tabs_tracked_) { | |
251 TabState& tab_state = map_entry.second; | |
252 if (tab_state.loading_state == TAB_IS_LOADED) | |
253 loaded_tabs.push_back(tab_state.controller); | |
254 } | |
255 for (auto& tab : loaded_tabs) | |
256 RemoveTab(tab); | |
112 } | 257 } |
113 break; | 258 break; |
114 } | 259 } |
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: | 260 default: |
168 NOTREACHED() << "Unknown notification received:" << type; | 261 NOTREACHED() << "Unknown notification received:" << type; |
169 break; | 262 break; |
170 } | 263 } |
171 | 264 |
172 // Check if we are done and if so, reset |this_retainer_| as the collector no | 265 ReleaseIfDoneTracking(); |
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 } | 266 } |
191 | 267 |
192 void SessionRestoreStatsCollector::RemoveTab(NavigationController* tab) { | 268 void SessionRestoreStatsCollector::RemoveTab(NavigationController* tab) { |
269 // Stop observing this tab. | |
193 registrar_.Remove(this, content::NOTIFICATION_WEB_CONTENTS_DESTROYED, | 270 registrar_.Remove(this, content::NOTIFICATION_WEB_CONTENTS_DESTROYED, |
194 content::Source<WebContents>(tab->GetWebContents())); | 271 Source<WebContents>(tab->GetWebContents())); |
195 registrar_.Remove(this, content::NOTIFICATION_LOAD_STOP, | 272 registrar_.Remove(this, content::NOTIFICATION_LOAD_STOP, |
196 content::Source<NavigationController>(tab)); | 273 Source<NavigationController>(tab)); |
197 registrar_.Remove(this, content::NOTIFICATION_LOAD_START, | 274 registrar_.Remove(this, content::NOTIFICATION_LOAD_START, |
198 content::Source<NavigationController>(tab)); | 275 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 | 276 |
205 // If there are no more tabs loading or being tracked, restore is done and | 277 auto tab_it = tabs_tracked_.find(tab); |
206 // record the time. Note that we are not yet finished, as we might still be | 278 DCHECK(tab_it != tabs_tracked_.end()); |
207 // waiting for our first paint, which can happen after all tabs are done | 279 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 | 280 |
224 UMA_HISTOGRAM_COUNTS_100("SessionRestore.ParallelTabLoads", | 281 // If this tab was waiting for a NOTIFICATION_LOAD_STOP event then update |
225 max_parallel_tab_loads_); | 282 // the loading counts. |
283 if (tab_state.loading_state == TAB_IS_LOADING) { | |
284 DCHECK_LT(0u, loading_tab_count_); | |
285 --loading_tab_count_; | |
226 } | 286 } |
287 if (tab_state.loading_state != TAB_IS_LOADED) { | |
288 DCHECK_LT(0u, waiting_for_load_tab_count_); | |
289 // It's possible for waiting_for_load_tab_count_ to reach zero here. This | |
290 // function is only called from 'Observe', so the transition will be | |
291 // noticed there. | |
292 --waiting_for_load_tab_count_; | |
293 } | |
294 | |
295 // Remove the tab from the |tracked_tabs_| map. | |
296 tabs_tracked_.erase(tab_it); | |
227 } | 297 } |
228 | 298 |
229 void SessionRestoreStatsCollector::RegisterForNotifications( | 299 SessionRestoreStatsCollector::TabState* |
300 SessionRestoreStatsCollector::RegisterForNotifications( | |
230 NavigationController* tab) { | 301 NavigationController* tab) { |
231 registrar_.Add(this, content::NOTIFICATION_WEB_CONTENTS_DESTROYED, | 302 registrar_.Add(this, content::NOTIFICATION_WEB_CONTENTS_DESTROYED, |
232 content::Source<WebContents>(tab->GetWebContents())); | 303 Source<WebContents>(tab->GetWebContents())); |
233 registrar_.Add(this, content::NOTIFICATION_LOAD_STOP, | 304 registrar_.Add(this, content::NOTIFICATION_LOAD_STOP, |
234 content::Source<NavigationController>(tab)); | 305 Source<NavigationController>(tab)); |
235 registrar_.Add(this, content::NOTIFICATION_LOAD_START, | 306 registrar_.Add(this, content::NOTIFICATION_LOAD_START, |
236 content::Source<NavigationController>(tab)); | 307 Source<NavigationController>(tab)); |
237 tabs_tracked_.insert(tab); | 308 auto result = tabs_tracked_.insert(std::make_pair(tab, TabState(tab))); |
309 DCHECK(result.second); | |
310 TabState* tab_state = &result.first->second; | |
311 return tab_state; | |
238 } | 312 } |
239 | 313 |
240 RenderWidgetHost* SessionRestoreStatsCollector::GetRenderWidgetHost( | 314 RenderWidgetHost* SessionRestoreStatsCollector::GetRenderWidgetHost( |
241 NavigationController* tab) { | 315 NavigationController* tab) { |
242 WebContents* web_contents = tab->GetWebContents(); | 316 WebContents* web_contents = tab->GetWebContents(); |
243 if (web_contents) { | 317 if (web_contents) { |
244 content::RenderWidgetHostView* render_widget_host_view = | 318 content::RenderWidgetHostView* render_widget_host_view = |
245 web_contents->GetRenderWidgetHostView(); | 319 web_contents->GetRenderWidgetHostView(); |
246 if (render_widget_host_view) | 320 if (render_widget_host_view) |
247 return render_widget_host_view->GetRenderWidgetHost(); | 321 return render_widget_host_view->GetRenderWidgetHost(); |
248 } | 322 } |
249 return nullptr; | 323 return nullptr; |
250 } | 324 } |
251 | 325 |
252 // static | 326 SessionRestoreStatsCollector::TabState* |
253 SessionRestoreStatsCollector* SessionRestoreStatsCollector::shared_collector_ = | 327 SessionRestoreStatsCollector::GetTabState(NavigationController* tab) { |
254 nullptr; | 328 // This lookup can fail because DeferTab calls can arrive for tabs that have |
329 // already loaded (user forced) and are no longer tracked. | |
330 auto it = tabs_tracked_.find(tab); | |
331 if (it == tabs_tracked_.end()) | |
332 return nullptr; | |
333 return &it->second; | |
334 } | |
335 | |
336 SessionRestoreStatsCollector::TabState* | |
337 SessionRestoreStatsCollector::GetTabState(RenderWidgetHost* tab) { | |
338 for (auto& pair : tabs_tracked_) { | |
339 if (pair.second.render_widget_host == tab) | |
340 return &pair.second; | |
341 } | |
342 // It's possible for this lookup to fail as paint events can be received for | |
343 // tabs that aren't being tracked. | |
344 return nullptr; | |
345 } | |
346 | |
347 void SessionRestoreStatsCollector::MarkTabAsLoading(TabState* tab_state) { | |
348 DCHECK_EQ(TAB_IS_NOT_LOADING, tab_state->loading_state); | |
349 if (tab_state->loading_state != TAB_IS_NOT_LOADING) | |
350 return; | |
351 tab_state->loading_state = TAB_IS_LOADING; | |
352 ++loading_tab_count_; | |
353 | |
354 if (!done_tracking_non_deferred_tabs_) { | |
355 tab_loader_stats_.parallel_tab_loads = | |
356 std::max(tab_loader_stats_.parallel_tab_loads, loading_tab_count_); | |
357 } | |
358 | |
359 // Get the RenderWidgetHost for the tab and add it to the secondary index. | |
360 RenderWidgetHost* render_widget_host = | |
361 GetRenderWidgetHost(tab_state->controller); | |
362 DCHECK(render_widget_host); | |
363 tab_state->render_widget_host = render_widget_host; | |
364 } | |
365 | |
366 void SessionRestoreStatsCollector::ReleaseIfDoneTracking() { | |
367 // If non-deferred tabs are no longer being tracked then report tab loader | |
368 // statistics. | |
369 if (!done_tracking_non_deferred_tabs_ && got_first_paint_ && | |
370 waiting_for_load_tab_count_ == 0) { | |
371 done_tracking_non_deferred_tabs_ = true; | |
372 reporting_delegate_->ReportTabLoaderStats(tab_loader_stats_); | |
373 } | |
374 | |
375 // If tracking is completely finished then emit collected metrics and destroy | |
376 // this stats collector. | |
377 if (done_tracking_non_deferred_tabs_ && tabs_tracked_.empty()) | |
378 this_retainer_ = nullptr; | |
379 } | |
380 | |
381 SessionRestoreStatsCollector::UmaStatsReportingDelegate:: | |
382 UmaStatsReportingDelegate() | |
383 : got_report_tab_deferred_(false) { | |
384 } | |
385 | |
386 void SessionRestoreStatsCollector::UmaStatsReportingDelegate:: | |
387 ReportTabLoaderStats(const TabLoaderStats& tab_loader_stats) { | |
388 UMA_HISTOGRAM_COUNTS_100("SessionRestore.TabCount", | |
389 tab_loader_stats.tab_count); | |
390 | |
391 UMA_HISTOGRAM_ENUMERATION(kSessionRestoreActions, | |
392 SESSION_RESTORE_ACTIONS_UMA_INITIATED, | |
393 SESSION_RESTORE_ACTIONS_UMA_MAX); | |
394 | |
395 for (size_t i = 0; i < tab_loader_stats.tab_count; ++i) { | |
396 UMA_HISTOGRAM_ENUMERATION("SessionRestore.TabActions", | |
397 SESSION_RESTORE_TAB_ACTIONS_UMA_TAB_CREATED, | |
398 SESSION_RESTORE_TAB_ACTIONS_UMA_MAX); | |
399 } | |
400 | |
401 for (size_t i = 0; i < tab_loader_stats.tabs_loaded; ++i) { | |
402 UMA_HISTOGRAM_ENUMERATION("SessionRestore.TabActions", | |
403 SESSION_RESTORE_TAB_ACTIONS_UMA_TAB_LOADED, | |
404 SESSION_RESTORE_TAB_ACTIONS_UMA_MAX); | |
Alexei Svitkine (slow)
2015/06/18 17:48:48
Please make a helper function for this histogram i
chrisha
2015/06/18 20:09:04
Done for the two enumeration histograms. The other
| |
405 } | |
406 | |
407 if (!tab_loader_stats.foreground_tab_first_loaded.is_zero()) { | |
408 UMA_HISTOGRAM_CUSTOM_TIMES("SessionRestore.ForegroundTabFirstLoaded", | |
409 tab_loader_stats.foreground_tab_first_loaded, | |
410 base::TimeDelta::FromMilliseconds(10), | |
411 base::TimeDelta::FromSeconds(100), 100); | |
412 | |
413 // Record a time for the number of tabs, to help track down contention. | |
414 std::string time_for_count = | |
415 base::StringPrintf("SessionRestore.ForegroundTabFirstLoaded_%d", | |
416 tab_loader_stats.tab_count); | |
417 base::HistogramBase* counter_for_count = base::Histogram::FactoryTimeGet( | |
418 time_for_count, base::TimeDelta::FromMilliseconds(10), | |
419 base::TimeDelta::FromSeconds(100), 100, | |
420 base::Histogram::kUmaTargetedHistogramFlag); | |
421 counter_for_count->AddTime(tab_loader_stats.foreground_tab_first_loaded); | |
422 } | |
423 | |
424 if (!tab_loader_stats.foreground_tab_first_paint.is_zero()) { | |
425 UMA_HISTOGRAM_CUSTOM_TIMES("SessionRestore.ForegroundTabFirstPaint3", | |
426 tab_loader_stats.foreground_tab_first_paint, | |
427 base::TimeDelta::FromMilliseconds(100), | |
428 base::TimeDelta::FromMinutes(16), 50); | |
429 | |
430 std::string time_for_count = | |
431 base::StringPrintf("SessionRestore.ForegroundTabFirstPaint3_%d", | |
432 tab_loader_stats.tab_count); | |
433 base::HistogramBase* counter_for_count = base::Histogram::FactoryTimeGet( | |
434 time_for_count, base::TimeDelta::FromMilliseconds(100), | |
435 base::TimeDelta::FromMinutes(16), 50, | |
436 base::Histogram::kUmaTargetedHistogramFlag); | |
437 counter_for_count->AddTime(tab_loader_stats.foreground_tab_first_paint); | |
438 } | |
439 | |
440 if (!tab_loader_stats.non_deferred_tabs_loaded.is_zero()) { | |
441 UMA_HISTOGRAM_CUSTOM_TIMES("SessionRestore.AllTabsLoaded", | |
442 tab_loader_stats.non_deferred_tabs_loaded, | |
443 base::TimeDelta::FromMilliseconds(10), | |
444 base::TimeDelta::FromSeconds(100), 100); | |
445 | |
446 // Record a time for the number of tabs, to help track down contention. | |
447 std::string time_for_count = base::StringPrintf( | |
448 "SessionRestore.AllTabsLoaded_%d", tab_loader_stats.tab_count); | |
449 base::HistogramBase* counter_for_count = base::Histogram::FactoryTimeGet( | |
450 time_for_count, base::TimeDelta::FromMilliseconds(10), | |
451 base::TimeDelta::FromSeconds(100), 100, | |
452 base::Histogram::kUmaTargetedHistogramFlag); | |
453 counter_for_count->AddTime(tab_loader_stats.non_deferred_tabs_loaded); | |
454 } | |
455 | |
456 UMA_HISTOGRAM_COUNTS_100("SessionRestore.ParallelTabLoads", | |
457 tab_loader_stats.parallel_tab_loads); | |
458 } | |
459 | |
460 void SessionRestoreStatsCollector::UmaStatsReportingDelegate:: | |
461 ReportTabDeferred() { | |
462 if (!got_report_tab_deferred_) { | |
463 got_report_tab_deferred_ = true; | |
464 | |
465 UMA_HISTOGRAM_ENUMERATION(kSessionRestoreActions, | |
466 SESSION_RESTORE_ACTIONS_UMA_DEFERRED_TABS, | |
467 SESSION_RESTORE_ACTIONS_UMA_MAX); | |
468 } | |
469 | |
470 UMA_HISTOGRAM_ENUMERATION( | |
471 "SessionRestore.TabActions", | |
472 SESSION_RESTORE_TAB_ACTIONS_UMA_TAB_LOADING_DEFERRED, | |
473 SESSION_RESTORE_TAB_ACTIONS_UMA_MAX); | |
474 } | |
475 | |
476 void SessionRestoreStatsCollector::UmaStatsReportingDelegate:: | |
477 ReportDeferredTabLoaded() { | |
478 UMA_HISTOGRAM_ENUMERATION("SessionRestore.TabActions", | |
479 SESSION_RESTORE_TAB_ACTIONS_UMA_DEFERRED_TAB_LOADED, | |
480 SESSION_RESTORE_TAB_ACTIONS_UMA_MAX); | |
481 } | |
OLD | NEW |