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/net/cache_stats.h" | 5 #include "chrome/browser/net/load_time_stats.h" |
6 | 6 |
7 #include "base/metrics/histogram.h" | 7 #include "base/metrics/histogram.h" |
8 #include "base/stl_util.h" | 8 #include "base/stl_util.h" |
9 #include "base/string_number_conversions.h" | 9 #include "base/string_number_conversions.h" |
10 #include "base/timer.h" | 10 #include "base/timer.h" |
11 #include "chrome/browser/browser_process.h" | 11 #include "chrome/browser/browser_process.h" |
12 #include "chrome/browser/io_thread.h" | 12 #include "chrome/browser/io_thread.h" |
13 #include "chrome/browser/profiles/profile.h" | 13 #include "chrome/browser/profiles/profile.h" |
14 #include "chrome/browser/ui/tab_contents/tab_contents.h" | 14 #include "chrome/browser/ui/tab_contents/tab_contents.h" |
15 #include "content/public/browser/browser_thread.h" | 15 #include "content/public/browser/browser_thread.h" |
16 #include "content/public/browser/render_process_host.h" | 16 #include "content/public/browser/render_process_host.h" |
17 #include "content/public/browser/render_view_host.h" | 17 #include "content/public/browser/render_view_host.h" |
18 #include "content/public/browser/resource_request_info.h" | 18 #include "content/public/browser/resource_request_info.h" |
19 #include "content/public/browser/web_contents.h" | 19 #include "content/public/browser/web_contents.h" |
20 #include "net/url_request/url_request.h" | 20 #include "net/url_request/url_request.h" |
21 | 21 |
22 using content::BrowserThread; | 22 using content::BrowserThread; |
23 using content::RenderViewHost; | 23 using content::RenderViewHost; |
24 using content::ResourceRequestInfo; | 24 using content::ResourceRequestInfo; |
25 using std::string; | |
25 | 26 |
26 #if defined(COMPILER_GCC) | 27 #if defined(COMPILER_GCC) |
27 | 28 |
28 namespace BASE_HASH_NAMESPACE { | 29 namespace BASE_HASH_NAMESPACE { |
29 template <> | 30 template <> |
30 struct hash<const net::URLRequest*> { | 31 struct hash<const net::URLRequest*> { |
31 std::size_t operator()(const net::URLRequest* value) const { | 32 std::size_t operator()(const net::URLRequest* value) const { |
32 return reinterpret_cast<std::size_t>(value); | 33 return reinterpret_cast<std::size_t>(value); |
33 } | 34 } |
34 }; | 35 }; |
35 } | 36 } |
36 | 37 |
37 #endif | 38 #endif |
38 | 39 |
39 namespace { | 40 namespace { |
40 | 41 |
41 bool GetRenderView(const net::URLRequest& request, | 42 bool GetRenderView(const net::URLRequest& request, |
42 int* process_id, int* route_id) { | 43 int* process_id, int* route_id) { |
43 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | 44 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
44 const ResourceRequestInfo* info = ResourceRequestInfo::ForRequest(&request); | 45 const ResourceRequestInfo* info = ResourceRequestInfo::ForRequest(&request); |
45 if (!info) | 46 if (!info) |
46 return false; | 47 return false; |
47 return info->GetAssociatedRenderView(process_id, route_id); | 48 return info->GetAssociatedRenderView(process_id, route_id); |
48 } | 49 } |
49 | 50 |
50 void CallCacheStatsTabEventOnIOThread( | 51 void CallLoadTimeStatsTabEventOnIOThread( |
51 std::pair<int, int> render_view_id, | 52 std::pair<int, int> render_view_id, |
52 chrome_browser_net::CacheStats::TabEvent event, | 53 chrome_browser_net::LoadTimeStats::TabEvent event, |
53 IOThread* io_thread) { | 54 IOThread* io_thread) { |
54 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | 55 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
55 if (io_thread) | 56 if (io_thread) |
56 io_thread->globals()->cache_stats->OnTabEvent(render_view_id, event); | 57 io_thread->globals()->load_time_stats->OnTabEvent(render_view_id, event); |
57 } | 58 } |
58 | 59 |
59 // Times after a load has started at which stats are collected. | 60 // Times after a load has started at which stats are collected. |
60 const int kStatsCollectionTimesMs[] = { | 61 const int kStatsCollectionTimesMs[] = { |
61 500, | 62 500, |
62 1000, | 63 1000, |
63 2000, | 64 2000, |
64 3000, | 65 3000, |
65 4000, | 66 4000, |
66 5000, | 67 5000, |
67 7500, | 68 7500, |
68 10000, | 69 10000, |
69 15000, | 70 15000, |
70 20000 | 71 20000 |
71 }; | 72 }; |
72 | 73 |
73 static int kTabLoadStatsAutoCleanupTimeoutSeconds = 30; | 74 static int kTabLoadStatsAutoCleanupTimeoutSeconds = 30; |
74 | 75 |
76 const char* kRequestStatusNames[] = { | |
77 "CacheWait", | |
78 "NetworkWait", | |
79 "Active", | |
80 "None", | |
81 "Max" | |
82 }; | |
83 | |
84 COMPILE_ASSERT(arraysize(kRequestStatusNames) == | |
85 chrome_browser_net::LoadTimeStats::REQUEST_STATUS_MAX + 1, | |
86 LoadTimeStats_RequestStatus_names_mismatch); | |
87 | |
88 const char* kHistogramTypeNames[] = { | |
89 "FinalAggregate", | |
90 "FinalCumulativePercentage", | |
91 "IntermediateAggregate", | |
92 "IntermediateCumulativePercentage", | |
93 "Max" | |
94 }; | |
95 | |
96 COMPILE_ASSERT(arraysize(kHistogramTypeNames) == | |
97 chrome_browser_net::LoadTimeStats::HISTOGRAM_MAX + 1, | |
98 LoadTimeStats_HistogramType_names_mismatch); | |
99 | |
75 } // namespace | 100 } // namespace |
76 | 101 |
77 namespace chrome_browser_net { | 102 namespace chrome_browser_net { |
78 | 103 |
79 // Helper struct keeping stats about the page load progress & cache usage | 104 // Helper struct keeping stats about the page load progress & cache usage |
80 // stats during the pageload so far for a given RenderView, identified | 105 // stats during the pageload so far for a given RenderView, identified |
81 // by a pair of process id and route id. | 106 // by a pair of process id and route id. |
82 struct CacheStats::TabLoadStats { | 107 class LoadTimeStats::TabLoadStats { |
83 TabLoadStats(std::pair<int, int> render_view_id, CacheStats* owner) | 108 public: |
84 : render_view_id(render_view_id), | 109 // Stores the time taken by all requests while they have a certain |
85 num_active(0), | 110 // RequestStatus. |
86 spinner_started(false), | 111 class PerStatusStats { |
87 next_timer_index(0), | 112 public: |
88 timer(false, false) { | 113 PerStatusStats() : num_active_(0) { |
114 } | |
115 | |
116 void UpdateTotalTimes() { | |
mmenke
2012/08/20 16:15:45
Suggest a DCHECK_GE(num_active_, 0)
tburkard
2012/08/20 19:23:39
Done.
| |
117 base::TimeTicks now = base::TimeTicks::Now(); | |
118 if (num_active_ > 0) { | |
119 total_time_ += now - last_update_time_; | |
120 total_cumulative_time_ += | |
121 (now - last_update_time_) * static_cast<int64>(num_active_); | |
122 } | |
123 last_update_time_ = now; | |
124 } | |
125 | |
126 void ResetTimes() { | |
127 last_update_time_ = base::TimeTicks::Now(); | |
128 total_time_ = base::TimeDelta(); | |
129 total_cumulative_time_ = base::TimeDelta(); | |
130 } | |
131 | |
132 int num_active() { return num_active_; } | |
133 void set_num_active(int num_active) { num_active_ = num_active; } | |
134 base::TimeDelta total_time() { return total_time_; } | |
135 base::TimeDelta total_cumulative_time() { return total_cumulative_time_; } | |
136 | |
137 private: | |
138 int num_active_; | |
139 base::TimeTicks last_update_time_; | |
140 base::TimeDelta total_time_; | |
141 base::TimeDelta total_cumulative_time_; | |
142 }; | |
143 | |
144 TabLoadStats(std::pair<int, int> render_view_id, LoadTimeStats* owner) | |
145 : render_view_id_(render_view_id), | |
146 spinner_started_(false), | |
147 next_timer_index_(0), | |
148 timer_(false, false) { | |
89 // Initialize the timer to do an automatic cleanup. If a pageload is | 149 // Initialize the timer to do an automatic cleanup. If a pageload is |
90 // started for the TabLoadStats within that timeframe, CacheStats | 150 // started for the TabLoadStats within that timeframe, LoadTimeStats |
91 // will start using the timer, thereby cancelling the cleanup. | 151 // will start using the timer, thereby cancelling the cleanup. |
92 // Once CacheStats starts the timer, the object is guaranteed to be | 152 // Once LoadTimeStats starts the timer, the object is guaranteed to be |
93 // destroyed eventually, so there is no more need for automatic cleanup at | 153 // destroyed eventually, so there is no more need for automatic cleanup at |
94 // that point. | 154 // that point. |
95 timer.Start(FROM_HERE, | 155 timer_.Start(FROM_HERE, |
96 base::TimeDelta::FromSeconds( | 156 base::TimeDelta::FromSeconds( |
97 kTabLoadStatsAutoCleanupTimeoutSeconds), | 157 kTabLoadStatsAutoCleanupTimeoutSeconds), |
98 base::Bind(&CacheStats::RemoveTabLoadStats, | 158 base::Bind(&LoadTimeStats::RemoveTabLoadStats, |
99 base::Unretained(owner), | 159 base::Unretained(owner), |
100 render_view_id)); | 160 render_view_id_)); |
101 } | 161 } |
102 | 162 |
103 std::pair<int, int> render_view_id; | 163 typedef std::pair<int, int> RenderViewId; |
104 int num_active; | 164 typedef PerStatusStats PerStatusStatsArray[REQUEST_STATUS_MAX]; |
105 bool spinner_started; | 165 typedef base::hash_map<const net::URLRequest*, RequestStatus> RequestMap; |
106 base::TimeTicks load_start_time; | 166 |
107 base::TimeTicks cache_start_time; | 167 RenderViewId& render_view_id() { return render_view_id_; } |
108 base::TimeDelta cache_total_time; | 168 PerStatusStatsArray& per_status_stats() { return per_status_stats_; } |
109 int next_timer_index; | 169 bool spinner_started() { return spinner_started_; } |
110 base::Timer timer; | 170 void set_spinner_started(bool value) { spinner_started_ = value; } |
111 // URLRequest's for which there are outstanding cache transactions. | 171 base::TimeTicks load_start_time() { return load_start_time_; } |
112 base::hash_set<const net::URLRequest*> active_requests; | 172 int next_timer_index() { return next_timer_index_; } |
173 void set_next_timer_index(int index) { next_timer_index_ = index; } | |
174 base::Timer& timer() { return timer_; } | |
175 RequestMap& active_requests() { return active_requests_; } | |
176 | |
177 private: | |
178 RenderViewId render_view_id_; | |
179 PerStatusStatsArray per_status_stats_; | |
180 bool spinner_started_; | |
181 base::TimeTicks load_start_time_; | |
182 int next_timer_index_; | |
183 base::Timer timer_; | |
184 // Currently active URLRequests. | |
185 RequestMap active_requests_; | |
113 }; | 186 }; |
114 | 187 |
115 CacheStatsTabHelper::CacheStatsTabHelper(TabContents* tab) | 188 LoadTimeStatsTabHelper::LoadTimeStatsTabHelper(TabContents* tab) |
116 : content::WebContentsObserver(tab->web_contents()), | 189 : content::WebContentsObserver(tab->web_contents()) { |
117 cache_stats_(NULL) { | |
118 is_otr_profile_ = tab->profile()->IsOffTheRecord(); | 190 is_otr_profile_ = tab->profile()->IsOffTheRecord(); |
119 } | 191 } |
120 | 192 |
121 CacheStatsTabHelper::~CacheStatsTabHelper() { | 193 LoadTimeStatsTabHelper::~LoadTimeStatsTabHelper() { |
122 } | 194 } |
123 | 195 |
124 void CacheStatsTabHelper::DidStartProvisionalLoadForFrame( | 196 void LoadTimeStatsTabHelper::DidStartProvisionalLoadForFrame( |
125 int64 frame_id, | 197 int64 frame_id, |
126 bool is_main_frame, | 198 bool is_main_frame, |
127 const GURL& validated_url, | 199 const GURL& validated_url, |
128 bool is_error_page, | 200 bool is_error_page, |
129 content::RenderViewHost* render_view_host) { | 201 content::RenderViewHost* render_view_host) { |
130 if (!is_main_frame) | 202 if (!is_main_frame) |
131 return; | 203 return; |
132 if (!validated_url.SchemeIs("http")) | 204 if (!validated_url.SchemeIs("http")) |
133 return; | 205 return; |
134 NotifyCacheStats(CacheStats::SPINNER_START, render_view_host); | 206 NotifyLoadTimeStats(LoadTimeStats::SPINNER_START, render_view_host); |
135 } | 207 } |
136 | 208 |
137 void CacheStatsTabHelper::DidStopLoading(RenderViewHost* render_view_host) { | 209 void LoadTimeStatsTabHelper::DidStopLoading(RenderViewHost* render_view_host) { |
138 NotifyCacheStats(CacheStats::SPINNER_STOP, render_view_host); | 210 NotifyLoadTimeStats(LoadTimeStats::SPINNER_STOP, render_view_host); |
139 } | 211 } |
140 | 212 |
141 void CacheStatsTabHelper::NotifyCacheStats( | 213 void LoadTimeStatsTabHelper::NotifyLoadTimeStats( |
142 CacheStats::TabEvent event, | 214 LoadTimeStats::TabEvent event, |
143 RenderViewHost* render_view_host) { | 215 RenderViewHost* render_view_host) { |
144 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 216 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
145 if (is_otr_profile_) | 217 if (is_otr_profile_) |
146 return; | 218 return; |
147 int process_id = render_view_host->GetProcess()->GetID(); | 219 int process_id = render_view_host->GetProcess()->GetID(); |
148 int route_id = render_view_host->GetRoutingID(); | 220 int route_id = render_view_host->GetRoutingID(); |
149 BrowserThread::PostTask( | 221 BrowserThread::PostTask( |
150 BrowserThread::IO, FROM_HERE, | 222 BrowserThread::IO, FROM_HERE, |
151 base::Bind(&CallCacheStatsTabEventOnIOThread, | 223 base::Bind(&CallLoadTimeStatsTabEventOnIOThread, |
152 std::pair<int, int>(process_id, route_id), | 224 std::pair<int, int>(process_id, route_id), |
153 event, | 225 event, |
154 base::Unretained(g_browser_process->io_thread()))); | 226 base::Unretained(g_browser_process->io_thread()))); |
155 } | 227 } |
156 | 228 |
157 CacheStats::CacheStats() { | 229 LoadTimeStats::LoadTimeStats() { |
158 for (int i = 0; | 230 for (int status = REQUEST_STATUS_CACHE_WAIT; |
159 i < static_cast<int>(arraysize(kStatsCollectionTimesMs)); | 231 status <= REQUEST_STATUS_ACTIVE; |
160 i++) { | 232 status++) { |
161 final_histograms_.push_back( | 233 for (int histogram_type = HISTOGRAM_FINAL_AGGREGATE; |
162 base::LinearHistogram::FactoryGet( | 234 histogram_type < HISTOGRAM_MAX; |
163 "CacheStats.FractionCacheUseFinalPLT_" + | 235 histogram_type++) { |
164 base::IntToString(kStatsCollectionTimesMs[i]), | 236 for (int i = 0; |
165 0, 101, 102, base::Histogram::kUmaTargetedHistogramFlag)); | 237 i < static_cast<int>(arraysize(kStatsCollectionTimesMs)); |
166 intermediate_histograms_.push_back( | 238 i++) { |
167 base::LinearHistogram::FactoryGet( | 239 string histogram_name = string("LoadTimeStats.Fraction_") + |
168 "CacheStats.FractionCacheUseIntermediatePLT_" + | 240 string(kRequestStatusNames[status]) + string("_") + |
169 base::IntToString(kStatsCollectionTimesMs[i]), | 241 string(kHistogramTypeNames[histogram_type]) + string("_") + |
170 0, 101, 102, base::Histogram::kUmaTargetedHistogramFlag)); | 242 base::IntToString(kStatsCollectionTimesMs[i]); |
243 histograms_[status][histogram_type].push_back( | |
244 base::LinearHistogram::FactoryGet( | |
245 histogram_name, | |
246 0, 101, 102, base::Histogram::kUmaTargetedHistogramFlag)); | |
247 } | |
248 DCHECK_EQ(histograms_[status][histogram_type].size(), | |
249 arraysize(kStatsCollectionTimesMs)); | |
250 } | |
171 } | 251 } |
172 DCHECK_EQ(final_histograms_.size(), arraysize(kStatsCollectionTimesMs)); | |
173 DCHECK_EQ(intermediate_histograms_.size(), | |
174 arraysize(kStatsCollectionTimesMs)); | |
175 } | 252 } |
176 | 253 |
177 CacheStats::~CacheStats() { | 254 LoadTimeStats::~LoadTimeStats() { |
178 STLDeleteValues(&tab_load_stats_); | 255 STLDeleteValues(&tab_load_stats_); |
179 } | 256 } |
180 | 257 |
181 CacheStats::TabLoadStats* CacheStats::GetTabLoadStats( | 258 LoadTimeStats::TabLoadStats* LoadTimeStats::GetTabLoadStats( |
182 std::pair<int, int> render_view_id) { | 259 std::pair<int, int> render_view_id) { |
183 TabLoadStatsMap::const_iterator it = tab_load_stats_.find(render_view_id); | 260 TabLoadStatsMap::const_iterator it = tab_load_stats_.find(render_view_id); |
184 if (it != tab_load_stats_.end()) | 261 if (it != tab_load_stats_.end()) |
185 return it->second; | 262 return it->second; |
186 TabLoadStats* new_tab_load_stats = new TabLoadStats(render_view_id, this); | 263 TabLoadStats* new_tab_load_stats = new TabLoadStats(render_view_id, this); |
187 tab_load_stats_[render_view_id] = new_tab_load_stats; | 264 tab_load_stats_[render_view_id] = new_tab_load_stats; |
188 return new_tab_load_stats; | 265 return new_tab_load_stats; |
189 } | 266 } |
190 | 267 |
191 void CacheStats::RemoveTabLoadStats(std::pair<int, int> render_view_id) { | 268 void LoadTimeStats::RemoveTabLoadStats(std::pair<int, int> render_view_id) { |
192 TabLoadStatsMap::iterator it = tab_load_stats_.find(render_view_id); | 269 TabLoadStatsMap::iterator it = tab_load_stats_.find(render_view_id); |
193 if (it != tab_load_stats_.end()) { | 270 if (it != tab_load_stats_.end()) { |
194 delete it->second; | 271 delete it->second; |
195 tab_load_stats_.erase(it); | 272 tab_load_stats_.erase(it); |
196 } | 273 } |
197 } | 274 } |
198 | 275 |
199 void CacheStats::OnCacheWaitStateChange( | 276 void LoadTimeStats::OnRequestWaitStateChange( |
200 const net::URLRequest& request, | 277 const net::URLRequest& request, |
201 net::NetworkDelegate::CacheWaitState state) { | 278 net::NetworkDelegate::RequestWaitState state) { |
202 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | 279 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
203 if (main_request_contexts_.count(request.context()) < 1) | 280 if (main_request_contexts_.count(request.context()) < 1) |
204 return; | 281 return; |
205 int process_id, route_id; | 282 int process_id, route_id; |
206 if (!GetRenderView(request, &process_id, &route_id)) | 283 if (!GetRenderView(request, &process_id, &route_id)) |
207 return; | 284 return; |
208 TabLoadStats* stats = | 285 TabLoadStats* stats = |
209 GetTabLoadStats(std::pair<int, int>(process_id, route_id)); | 286 GetTabLoadStats(std::pair<int, int>(process_id, route_id)); |
210 bool newly_started = false; | 287 RequestStatus old_status = REQUEST_STATUS_NONE; |
211 bool newly_finished = false; | 288 if (stats->active_requests().count(&request) > 0) |
289 old_status = stats->active_requests()[&request]; | |
290 RequestStatus new_status = REQUEST_STATUS_NONE; | |
212 switch (state) { | 291 switch (state) { |
213 case net::NetworkDelegate::CACHE_WAIT_STATE_START: | 292 case net::NetworkDelegate::REQUEST_WAIT_STATE_CACHE_START: |
214 DCHECK(stats->active_requests.count(&request) == 0); | 293 DCHECK(old_status == REQUEST_STATUS_NONE || |
215 newly_started = true; | 294 old_status == REQUEST_STATUS_ACTIVE); |
216 stats->active_requests.insert(&request); | 295 new_status = REQUEST_STATUS_CACHE_WAIT; |
217 break; | 296 break; |
218 case net::NetworkDelegate::CACHE_WAIT_STATE_FINISH: | 297 case net::NetworkDelegate::REQUEST_WAIT_STATE_CACHE_FINISH: |
219 if (stats->active_requests.count(&request) > 0) { | 298 DCHECK(old_status == REQUEST_STATUS_NONE || |
220 stats->active_requests.erase(&request); | 299 old_status == REQUEST_STATUS_CACHE_WAIT); |
221 newly_finished = true; | 300 new_status = REQUEST_STATUS_ACTIVE; |
222 } | |
223 break; | 301 break; |
224 case net::NetworkDelegate::CACHE_WAIT_STATE_RESET: | 302 case net::NetworkDelegate::REQUEST_WAIT_STATE_NETWORK_START: |
225 if (stats->active_requests.count(&request) > 0) { | 303 DCHECK(old_status == REQUEST_STATUS_NONE || |
226 stats->active_requests.erase(&request); | 304 old_status == REQUEST_STATUS_ACTIVE); |
227 newly_finished = true; | 305 new_status = REQUEST_STATUS_NETWORK_WAIT; |
228 } | 306 break; |
307 case net::NetworkDelegate::REQUEST_WAIT_STATE_NETWORK_FINISH: | |
308 DCHECK(old_status == REQUEST_STATUS_NONE || | |
309 old_status == REQUEST_STATUS_NETWORK_WAIT); | |
310 new_status = REQUEST_STATUS_ACTIVE; | |
311 break; | |
312 case net::NetworkDelegate::REQUEST_WAIT_STATE_RESET: | |
313 new_status = REQUEST_STATUS_NONE; | |
229 break; | 314 break; |
230 } | 315 } |
231 DCHECK_GE(stats->num_active, 0); | 316 if (old_status == new_status) |
232 if (newly_started) { | 317 return; |
233 DCHECK(!newly_finished); | 318 if (old_status != REQUEST_STATUS_NONE) { |
234 if (stats->num_active == 0) { | 319 TabLoadStats::PerStatusStats* status_stats = |
235 stats->cache_start_time = base::TimeTicks::Now(); | 320 &(stats->per_status_stats()[old_status]); |
236 } | 321 DCHECK_GE(status_stats->num_active(), 0); |
237 stats->num_active++; | 322 status_stats->UpdateTotalTimes(); |
323 if (status_stats->num_active() > 0) | |
324 status_stats->set_num_active(status_stats->num_active() - 1); | |
325 DCHECK_GE(status_stats->num_active(), 0); | |
238 } | 326 } |
239 if (newly_finished) { | 327 if (new_status != REQUEST_STATUS_NONE) { |
240 DCHECK(!newly_started); | 328 TabLoadStats::PerStatusStats* status_stats = |
241 if (stats->num_active == 1) { | 329 &(stats->per_status_stats()[new_status]); |
242 stats->cache_total_time += | 330 DCHECK_GE(status_stats->num_active(), 0); |
243 base::TimeTicks::Now() - stats->cache_start_time; | 331 status_stats->UpdateTotalTimes(); |
244 } | 332 status_stats->set_num_active(status_stats->num_active() + 1); |
245 stats->num_active--; | 333 DCHECK_GE(status_stats->num_active(), 0); |
334 stats->active_requests()[&request] = new_status; | |
335 } else { | |
336 stats->active_requests().erase(&request); | |
246 } | 337 } |
247 DCHECK_GE(stats->num_active, 0); | |
248 } | 338 } |
249 | 339 |
250 void CacheStats::OnTabEvent(std::pair<int, int> render_view_id, | 340 void LoadTimeStats::OnTabEvent(std::pair<int, int> render_view_id, |
251 TabEvent event) { | 341 TabEvent event) { |
252 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | 342 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
253 TabLoadStats* stats = GetTabLoadStats(render_view_id); | 343 TabLoadStats* stats = GetTabLoadStats(render_view_id); |
254 if (event == SPINNER_START) { | 344 if (event == SPINNER_START) { |
255 stats->spinner_started = true; | 345 stats->set_spinner_started(true); |
256 stats->cache_total_time = base::TimeDelta(); | 346 for (int status = REQUEST_STATUS_CACHE_WAIT; |
257 stats->cache_start_time = base::TimeTicks::Now(); | 347 status <= REQUEST_STATUS_ACTIVE; status++) { |
258 stats->load_start_time = base::TimeTicks::Now(); | 348 stats->per_status_stats()[status].ResetTimes(); |
259 stats->next_timer_index = 0; | 349 } |
350 stats->set_next_timer_index(0); | |
260 ScheduleTimer(stats); | 351 ScheduleTimer(stats); |
261 } else { | 352 } else { |
262 DCHECK_EQ(event, SPINNER_STOP); | 353 DCHECK_EQ(event, SPINNER_STOP); |
263 if (stats->spinner_started) { | 354 if (stats->spinner_started()) { |
264 stats->spinner_started = false; | 355 stats->set_spinner_started(false); |
265 base::TimeDelta load_time = | 356 base::TimeDelta load_time = |
266 base::TimeTicks::Now() - stats->load_start_time; | 357 base::TimeTicks::Now() - stats->load_start_time(); |
267 if (stats->num_active > 1) | 358 RecordHistograms(load_time, stats, true); |
268 stats->cache_total_time += | |
269 base::TimeTicks::Now() - stats->cache_start_time; | |
270 RecordCacheFractionHistogram(load_time, stats->cache_total_time, true, | |
271 stats->next_timer_index); | |
272 } | 359 } |
273 RemoveTabLoadStats(render_view_id); | 360 RemoveTabLoadStats(render_view_id); |
274 } | 361 } |
275 } | 362 } |
276 | 363 |
277 void CacheStats::ScheduleTimer(TabLoadStats* stats) { | 364 void LoadTimeStats::ScheduleTimer(TabLoadStats* stats) { |
278 int timer_index = stats->next_timer_index; | 365 int timer_index = stats->next_timer_index(); |
279 DCHECK(timer_index >= 0 && | 366 DCHECK(timer_index >= 0 && |
280 timer_index < static_cast<int>(arraysize(kStatsCollectionTimesMs))); | 367 timer_index < static_cast<int>(arraysize(kStatsCollectionTimesMs))); |
281 base::TimeDelta delta = | 368 base::TimeDelta delta = |
282 base::TimeDelta::FromMilliseconds(kStatsCollectionTimesMs[timer_index]); | 369 base::TimeDelta::FromMilliseconds(kStatsCollectionTimesMs[timer_index]); |
283 delta -= base::TimeTicks::Now() - stats->load_start_time; | 370 delta -= base::TimeTicks::Now() - stats->load_start_time(); |
284 | 371 |
285 // If the ScheduleTimer call was delayed significantly, like when one's using | 372 // If the ScheduleTimer call was delayed significantly, like when one's using |
286 // a debugger, don't try to start the timer with a negative time. | 373 // a debugger, don't try to start the timer with a negative time. |
287 if (delta < base::TimeDelta()) { | 374 if (delta < base::TimeDelta()) { |
288 RemoveTabLoadStats(stats->render_view_id); | 375 RemoveTabLoadStats(stats->render_view_id()); |
289 return; | 376 return; |
290 } | 377 } |
291 | 378 |
292 stats->timer.Start(FROM_HERE, | 379 stats->timer().Start(FROM_HERE, |
293 delta, | 380 delta, |
294 base::Bind(&CacheStats::TimerCallback, | 381 base::Bind(&LoadTimeStats::TimerCallback, |
295 base::Unretained(this), | 382 base::Unretained(this), |
296 base::Unretained(stats))); | 383 base::Unretained(stats))); |
297 } | 384 } |
298 | 385 |
299 void CacheStats::TimerCallback(TabLoadStats* stats) { | 386 void LoadTimeStats::TimerCallback(TabLoadStats* stats) { |
300 DCHECK(stats->spinner_started); | 387 DCHECK(stats->spinner_started()); |
301 base::TimeDelta load_time = base::TimeTicks::Now() - stats->load_start_time; | 388 base::TimeDelta load_time = base::TimeTicks::Now() - stats->load_start_time(); |
302 base::TimeDelta cache_time = stats->cache_total_time; | 389 RecordHistograms(load_time, stats, false); |
303 if (stats->num_active > 1) | 390 stats->set_next_timer_index(stats->next_timer_index() + 1); |
304 cache_time += base::TimeTicks::Now() - stats->cache_start_time; | 391 if (stats->next_timer_index() < |
305 RecordCacheFractionHistogram(load_time, cache_time, false, | |
306 stats->next_timer_index); | |
307 stats->next_timer_index++; | |
308 if (stats->next_timer_index < | |
309 static_cast<int>(arraysize(kStatsCollectionTimesMs))) { | 392 static_cast<int>(arraysize(kStatsCollectionTimesMs))) { |
310 ScheduleTimer(stats); | 393 ScheduleTimer(stats); |
311 } else { | 394 } else { |
312 RemoveTabLoadStats(stats->render_view_id); | 395 RemoveTabLoadStats(stats->render_view_id()); |
313 } | 396 } |
314 } | 397 } |
315 | 398 |
316 void CacheStats::RecordCacheFractionHistogram(base::TimeDelta elapsed, | 399 void LoadTimeStats::RecordHistograms(base::TimeDelta elapsed, |
317 base::TimeDelta cache_time, | 400 TabLoadStats* stats, |
318 bool is_load_done, | 401 bool is_load_done) { |
319 int timer_index) { | 402 int timer_index = stats->next_timer_index(); |
320 DCHECK(timer_index >= 0 && | 403 DCHECK(timer_index >= 0 && |
321 timer_index < static_cast<int>(arraysize(kStatsCollectionTimesMs))); | 404 timer_index < static_cast<int>(arraysize(kStatsCollectionTimesMs))); |
322 | 405 |
323 if (elapsed.InMilliseconds() <= 0) | 406 if (elapsed.InMilliseconds() <= 0) |
324 return; | 407 return; |
325 | 408 |
326 int64 cache_fraction_percentage = | 409 base::TimeDelta total_cumulative; |
327 100 * cache_time.InMilliseconds() / elapsed.InMilliseconds(); | 410 for (int status_int = REQUEST_STATUS_CACHE_WAIT; |
411 status_int <= REQUEST_STATUS_ACTIVE; | |
412 status_int++) { | |
413 RequestStatus status = static_cast<RequestStatus>(status_int); | |
mmenke
2012/08/20 16:15:45
Don't think you need status and status_int in eith
tburkard
2012/08/20 19:23:39
Done.
| |
414 total_cumulative += | |
415 stats->per_status_stats()[status].total_cumulative_time(); | |
416 } | |
328 | 417 |
329 DCHECK(cache_fraction_percentage >= 0 && cache_fraction_percentage <= 100); | 418 for (int status_int = REQUEST_STATUS_CACHE_WAIT; |
419 status_int <= REQUEST_STATUS_ACTIVE; | |
420 status_int++) { | |
421 RequestStatus status = static_cast<RequestStatus>(status_int); | |
422 TabLoadStats::PerStatusStats* status_stats = | |
423 &(stats->per_status_stats()[status]); | |
330 | 424 |
331 if (is_load_done) { | 425 int64 fraction_percentage = 100 * |
332 final_histograms_[timer_index]->Add(cache_fraction_percentage); | 426 status_stats->total_time().InMilliseconds() / elapsed.InMilliseconds(); |
333 } else { | 427 DCHECK(fraction_percentage >= 0 && fraction_percentage <= 100); |
334 intermediate_histograms_[timer_index]->Add(cache_fraction_percentage); | 428 if (is_load_done) { |
429 histograms_[status][HISTOGRAM_FINAL_AGGREGATE][timer_index]->Add( | |
430 fraction_percentage); | |
431 } else { | |
432 histograms_[status][HISTOGRAM_INTERMEDIATE_AGGREGATE][timer_index]->Add( | |
433 fraction_percentage); | |
434 } | |
435 | |
436 if (total_cumulative.InMilliseconds() > 0) { | |
437 fraction_percentage = 100 * | |
438 status_stats->total_cumulative_time().InMilliseconds() / | |
439 total_cumulative.InMilliseconds(); | |
440 DCHECK(fraction_percentage >= 0 && fraction_percentage <= 100); | |
441 if (is_load_done) { | |
442 histograms_[status][HISTOGRAM_FINAL_CUMULATIVE_PERCENTAGE] | |
443 [timer_index]->Add(fraction_percentage); | |
444 } else { | |
445 histograms_[status][HISTOGRAM_INTERMEDIATE_CUMULATIVE_PERCENTAGE] | |
446 [timer_index]->Add(fraction_percentage); | |
447 } | |
448 } | |
335 } | 449 } |
336 } | 450 } |
337 | 451 |
338 void CacheStats::RegisterURLRequestContext( | 452 void LoadTimeStats::RegisterURLRequestContext( |
339 const net::URLRequestContext* context, | 453 const net::URLRequestContext* context, |
340 ChromeURLRequestContext::ContextType type) { | 454 ChromeURLRequestContext::ContextType type) { |
341 if (type == ChromeURLRequestContext::CONTEXT_TYPE_MAIN) | 455 if (type == ChromeURLRequestContext::CONTEXT_TYPE_MAIN) |
342 main_request_contexts_.insert(context); | 456 main_request_contexts_.insert(context); |
343 } | 457 } |
344 | 458 |
345 void CacheStats::UnregisterURLRequestContext( | 459 void LoadTimeStats::UnregisterURLRequestContext( |
346 const net::URLRequestContext* context) { | 460 const net::URLRequestContext* context) { |
347 main_request_contexts_.erase(context); | 461 main_request_contexts_.erase(context); |
348 } | 462 } |
349 | 463 |
350 } // namespace chrome_browser_net | 464 } // namespace chrome_browser_net |
OLD | NEW |