OLD | NEW |
(Empty) | |
| 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 |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "chrome/browser/net/cache_stats.h" |
| 6 |
| 7 #include "base/metrics/histogram.h" |
| 8 #include "base/stl_util.h" |
| 9 #include "base/string_number_conversions.h" |
| 10 #include "base/timer.h" |
| 11 #include "chrome/browser/browser_process.h" |
| 12 #include "chrome/browser/io_thread.h" |
| 13 #include "chrome/browser/profiles/profile.h" |
| 14 #include "chrome/browser/ui/tab_contents/tab_contents.h" |
| 15 #include "content/public/browser/browser_thread.h" |
| 16 #include "content/public/browser/render_process_host.h" |
| 17 #include "content/public/browser/render_view_host.h" |
| 18 #include "content/public/browser/resource_request_info.h" |
| 19 #include "content/public/browser/web_contents.h" |
| 20 #include "net/url_request/url_request.h" |
| 21 |
| 22 using content::BrowserThread; |
| 23 using content::RenderViewHost; |
| 24 using content::ResourceRequestInfo; |
| 25 |
| 26 #if defined(COMPILER_GCC) |
| 27 |
| 28 namespace BASE_HASH_NAMESPACE { |
| 29 template <> |
| 30 struct hash<const net::URLRequest*> { |
| 31 std::size_t operator()(const net::URLRequest* value) const { |
| 32 return reinterpret_cast<std::size_t>(value); |
| 33 } |
| 34 }; |
| 35 } |
| 36 |
| 37 #endif |
| 38 |
| 39 namespace { |
| 40 |
| 41 bool GetRenderView(const net::URLRequest& request, |
| 42 int* process_id, int* route_id) { |
| 43 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
| 44 const ResourceRequestInfo* info = ResourceRequestInfo::ForRequest(&request); |
| 45 if (!info) |
| 46 return false; |
| 47 return info->GetAssociatedRenderView(process_id, route_id); |
| 48 } |
| 49 |
| 50 void CallCacheStatsTabEventOnIOThread( |
| 51 std::pair<int, int> render_view_id, |
| 52 chrome_browser_net::CacheStats::TabEvent event, |
| 53 IOThread* io_thread) { |
| 54 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
| 55 if (io_thread) |
| 56 io_thread->globals()->cache_stats->OnTabEvent(render_view_id, event); |
| 57 } |
| 58 |
| 59 // Times after a load has started at which stats are collected. |
| 60 const int kStatsCollectionTimesMs[] = { |
| 61 500, |
| 62 1000, |
| 63 2000, |
| 64 3000, |
| 65 4000, |
| 66 5000, |
| 67 7500, |
| 68 10000, |
| 69 15000, |
| 70 20000 |
| 71 }; |
| 72 |
| 73 static int kTabLoadStatsAutoCleanupTimeoutSeconds = 30; |
| 74 |
| 75 } // namespace |
| 76 |
| 77 namespace chrome_browser_net { |
| 78 |
| 79 // Helper struct keeping stats about the page load progress & cache usage |
| 80 // stats during the pageload so far for a given RenderView, identified |
| 81 // by a pair of process id and route id. |
| 82 struct CacheStats::TabLoadStats { |
| 83 TabLoadStats(std::pair<int, int> render_view_id, CacheStats* owner) |
| 84 : render_view_id(render_view_id), |
| 85 num_active(0), |
| 86 spinner_started(false), |
| 87 next_timer_index(0), |
| 88 timer(false, false) { |
| 89 // Initialize the timer to do an automatic cleanup. If a pageload is |
| 90 // started for the TabLoadStats within that timeframe, CacheStats |
| 91 // will start using the timer, thereby cancelling the cleanup. |
| 92 // Once CacheStats starts the timer, the object is guaranteed to be |
| 93 // destroyed eventually, so there is no more need for automatic cleanup at |
| 94 // that point. |
| 95 timer.Start(FROM_HERE, |
| 96 base::TimeDelta::FromSeconds( |
| 97 kTabLoadStatsAutoCleanupTimeoutSeconds), |
| 98 base::Bind(&CacheStats::RemoveTabLoadStats, |
| 99 base::Unretained(owner), |
| 100 render_view_id)); |
| 101 } |
| 102 |
| 103 std::pair<int, int> render_view_id; |
| 104 int num_active; |
| 105 bool spinner_started; |
| 106 base::TimeTicks load_start_time; |
| 107 base::TimeTicks cache_start_time; |
| 108 base::TimeDelta cache_total_time; |
| 109 int next_timer_index; |
| 110 base::Timer timer; |
| 111 // URLRequest's for which there are outstanding cache transactions. |
| 112 base::hash_set<const net::URLRequest*> active_requests; |
| 113 }; |
| 114 |
| 115 CacheStatsTabHelper::CacheStatsTabHelper(TabContents* tab) |
| 116 : content::WebContentsObserver(tab->web_contents()), |
| 117 cache_stats_(NULL) { |
| 118 is_otr_profile_ = tab->profile()->IsOffTheRecord(); |
| 119 } |
| 120 |
| 121 CacheStatsTabHelper::~CacheStatsTabHelper() { |
| 122 } |
| 123 |
| 124 void CacheStatsTabHelper::DidStartProvisionalLoadForFrame( |
| 125 int64 frame_id, |
| 126 bool is_main_frame, |
| 127 const GURL& validated_url, |
| 128 bool is_error_page, |
| 129 content::RenderViewHost* render_view_host) { |
| 130 if (!is_main_frame) |
| 131 return; |
| 132 if (!validated_url.SchemeIs("http")) |
| 133 return; |
| 134 NotifyCacheStats(CacheStats::SPINNER_START, render_view_host); |
| 135 } |
| 136 |
| 137 void CacheStatsTabHelper::DidStopLoading(RenderViewHost* render_view_host) { |
| 138 NotifyCacheStats(CacheStats::SPINNER_STOP, render_view_host); |
| 139 } |
| 140 |
| 141 void CacheStatsTabHelper::NotifyCacheStats( |
| 142 CacheStats::TabEvent event, |
| 143 RenderViewHost* render_view_host) { |
| 144 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 145 if (is_otr_profile_) |
| 146 return; |
| 147 int process_id = render_view_host->GetProcess()->GetID(); |
| 148 int route_id = render_view_host->GetRoutingID(); |
| 149 BrowserThread::PostTask( |
| 150 BrowserThread::IO, FROM_HERE, |
| 151 base::Bind(&CallCacheStatsTabEventOnIOThread, |
| 152 std::pair<int, int>(process_id, route_id), |
| 153 event, |
| 154 base::Unretained(g_browser_process->io_thread()))); |
| 155 } |
| 156 |
| 157 CacheStats::CacheStats() { |
| 158 for (int i = 0; |
| 159 i < static_cast<int>(arraysize(kStatsCollectionTimesMs)); |
| 160 i++) { |
| 161 final_histograms_.push_back( |
| 162 base::LinearHistogram::FactoryGet( |
| 163 "CacheStats.FractionCacheUseFinalPLT_" + |
| 164 base::IntToString(kStatsCollectionTimesMs[i]), |
| 165 0, 101, 102, base::Histogram::kUmaTargetedHistogramFlag)); |
| 166 intermediate_histograms_.push_back( |
| 167 base::LinearHistogram::FactoryGet( |
| 168 "DiskCache.FractionCacheUseIntermediatePLT_" + |
| 169 base::IntToString(kStatsCollectionTimesMs[i]), |
| 170 0, 101, 102, base::Histogram::kNoFlags)); |
| 171 } |
| 172 DCHECK_EQ(final_histograms_.size(), arraysize(kStatsCollectionTimesMs)); |
| 173 DCHECK_EQ(intermediate_histograms_.size(), |
| 174 arraysize(kStatsCollectionTimesMs)); |
| 175 } |
| 176 |
| 177 CacheStats::~CacheStats() { |
| 178 STLDeleteValues(&tab_load_stats_); |
| 179 } |
| 180 |
| 181 CacheStats::TabLoadStats* CacheStats::GetTabLoadStats( |
| 182 std::pair<int, int> render_view_id) { |
| 183 TabLoadStatsMap::const_iterator it = tab_load_stats_.find(render_view_id); |
| 184 if (it != tab_load_stats_.end()) |
| 185 return it->second; |
| 186 TabLoadStats* new_tab_load_stats = new TabLoadStats(render_view_id, this); |
| 187 tab_load_stats_[render_view_id] = new_tab_load_stats; |
| 188 return new_tab_load_stats; |
| 189 } |
| 190 |
| 191 void CacheStats::RemoveTabLoadStats(std::pair<int, int> render_view_id) { |
| 192 TabLoadStatsMap::iterator it = tab_load_stats_.find(render_view_id); |
| 193 if (it != tab_load_stats_.end()) { |
| 194 delete it->second; |
| 195 tab_load_stats_.erase(it); |
| 196 } |
| 197 } |
| 198 |
| 199 void CacheStats::OnCacheWaitStateChange( |
| 200 const net::URLRequest& request, |
| 201 net::NetworkDelegate::CacheWaitState state) { |
| 202 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
| 203 if (main_request_contexts_.count(request.context()) < 1) |
| 204 return; |
| 205 int process_id, route_id; |
| 206 if (!GetRenderView(request, &process_id, &route_id)) |
| 207 return; |
| 208 TabLoadStats* stats = |
| 209 GetTabLoadStats(std::pair<int, int>(process_id, route_id)); |
| 210 bool newly_started = false; |
| 211 bool newly_finished = false; |
| 212 switch (state) { |
| 213 case net::NetworkDelegate::CACHE_WAIT_STATE_START: |
| 214 DCHECK(stats->active_requests.count(&request) == 0); |
| 215 newly_started = true; |
| 216 stats->active_requests.insert(&request); |
| 217 break; |
| 218 case net::NetworkDelegate::CACHE_WAIT_STATE_FINISH: |
| 219 if (stats->active_requests.count(&request) > 0) { |
| 220 stats->active_requests.erase(&request); |
| 221 newly_finished = true; |
| 222 } |
| 223 break; |
| 224 case net::NetworkDelegate::CACHE_WAIT_STATE_RESET: |
| 225 if (stats->active_requests.count(&request) > 0) { |
| 226 stats->active_requests.erase(&request); |
| 227 newly_finished = true; |
| 228 } |
| 229 break; |
| 230 } |
| 231 DCHECK_GE(stats->num_active, 0); |
| 232 if (newly_started) { |
| 233 DCHECK(!newly_finished); |
| 234 if (stats->num_active == 0) { |
| 235 stats->cache_start_time = base::TimeTicks::Now(); |
| 236 } |
| 237 stats->num_active++; |
| 238 } |
| 239 if (newly_finished) { |
| 240 DCHECK(!newly_started); |
| 241 if (stats->num_active == 1) { |
| 242 stats->cache_total_time += |
| 243 base::TimeTicks::Now() - stats->cache_start_time; |
| 244 } |
| 245 stats->num_active--; |
| 246 } |
| 247 DCHECK_GE(stats->num_active, 0); |
| 248 } |
| 249 |
| 250 void CacheStats::OnTabEvent(std::pair<int, int> render_view_id, |
| 251 TabEvent event) { |
| 252 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
| 253 TabLoadStats* stats = GetTabLoadStats(render_view_id); |
| 254 if (event == SPINNER_START) { |
| 255 stats->spinner_started = true; |
| 256 stats->cache_total_time = base::TimeDelta(); |
| 257 stats->cache_start_time = base::TimeTicks::Now(); |
| 258 stats->load_start_time = base::TimeTicks::Now(); |
| 259 stats->next_timer_index = 0; |
| 260 ScheduleTimer(stats); |
| 261 } else { |
| 262 DCHECK_EQ(event, SPINNER_STOP); |
| 263 if (stats->spinner_started) { |
| 264 stats->spinner_started = false; |
| 265 base::TimeDelta load_time = |
| 266 base::TimeTicks::Now() - stats->load_start_time; |
| 267 if (stats->num_active > 1) |
| 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 } |
| 273 RemoveTabLoadStats(render_view_id); |
| 274 } |
| 275 } |
| 276 |
| 277 void CacheStats::ScheduleTimer(TabLoadStats* stats) { |
| 278 int timer_index = stats->next_timer_index; |
| 279 DCHECK(timer_index >= 0 && |
| 280 timer_index < static_cast<int>(arraysize(kStatsCollectionTimesMs))); |
| 281 base::TimeDelta delta = |
| 282 base::TimeDelta::FromMilliseconds(kStatsCollectionTimesMs[timer_index]); |
| 283 delta -= base::TimeTicks::Now() - stats->load_start_time; |
| 284 stats->timer.Start(FROM_HERE, |
| 285 delta, |
| 286 base::Bind(&CacheStats::TimerCallback, |
| 287 base::Unretained(this), |
| 288 base::Unretained(stats))); |
| 289 } |
| 290 |
| 291 void CacheStats::TimerCallback(TabLoadStats* stats) { |
| 292 DCHECK(stats->spinner_started); |
| 293 base::TimeDelta load_time = base::TimeTicks::Now() - stats->load_start_time; |
| 294 base::TimeDelta cache_time = stats->cache_total_time; |
| 295 if (stats->num_active > 1) |
| 296 cache_time += base::TimeTicks::Now() - stats->cache_start_time; |
| 297 RecordCacheFractionHistogram(load_time, cache_time, false, |
| 298 stats->next_timer_index); |
| 299 stats->next_timer_index++; |
| 300 if (stats->next_timer_index < |
| 301 static_cast<int>(arraysize(kStatsCollectionTimesMs))) { |
| 302 ScheduleTimer(stats); |
| 303 } else { |
| 304 RemoveTabLoadStats(stats->render_view_id); |
| 305 } |
| 306 } |
| 307 |
| 308 void CacheStats::RecordCacheFractionHistogram(base::TimeDelta elapsed, |
| 309 base::TimeDelta cache_time, |
| 310 bool is_load_done, |
| 311 int timer_index) { |
| 312 DCHECK(timer_index >= 0 && |
| 313 timer_index < static_cast<int>(arraysize(kStatsCollectionTimesMs))); |
| 314 |
| 315 if (elapsed.InMilliseconds() <= 0) |
| 316 return; |
| 317 |
| 318 int64 cache_fraction_percentage = |
| 319 100 * cache_time.InMilliseconds() / elapsed.InMilliseconds(); |
| 320 |
| 321 DCHECK(cache_fraction_percentage >= 0 && cache_fraction_percentage <= 100); |
| 322 |
| 323 if (is_load_done) { |
| 324 final_histograms_[timer_index]->Add(cache_fraction_percentage); |
| 325 } else { |
| 326 intermediate_histograms_[timer_index]->Add(cache_fraction_percentage); |
| 327 } |
| 328 } |
| 329 |
| 330 void CacheStats::RegisterURLRequestContext( |
| 331 const net::URLRequestContext* context, |
| 332 ChromeURLRequestContext::ContextType type) { |
| 333 if (type == ChromeURLRequestContext::CONTEXT_TYPE_MAIN) |
| 334 main_request_contexts_.insert(context); |
| 335 } |
| 336 |
| 337 void CacheStats::UnregisterURLRequestContext( |
| 338 const net::URLRequestContext* context) { |
| 339 main_request_contexts_.erase(context); |
| 340 } |
| 341 |
| 342 } // namespace chrome_browser_net |
OLD | NEW |