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