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