Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(678)

Side by Side Diff: chrome/browser/net/cache_stats.cc

Issue 10736066: Adding histograms showing fraction of page load times (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src/
Patch Set: Created 8 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
(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/browser_process.h"
15 #include "chrome/browser/io_thread.h"
16 #include "chrome/browser/profiles/profile.h"
17 #include "chrome/browser/ui/tab_contents/tab_contents.h"
18 #include "content/public/browser/browser_thread.h"
19 #include "content/public/browser/render_process_host.h"
20 #include "content/public/browser/render_view_host.h"
21 #include "content/public/browser/resource_request_info.h"
22 #include "content/public/browser/web_contents.h"
23 #include "net/url_request/url_request.h"
24
25 using content::BrowserThread;
26 using content::ResourceRequestInfo;
27 using content::RenderViewHost;
28
29 #if defined(COMPILER_GCC)
30
31 namespace BASE_HASH_NAMESPACE {
32 template <>
33 struct hash<const net::URLRequest*> {
34 std::size_t operator()(const net::URLRequest* value) const {
35 return reinterpret_cast<std::size_t>(value);
36 }
37 };
38 }
39
40 #endif
41
42 namespace chrome_browser_net {
43
44 namespace {
45
46 bool GetRenderView(const net::URLRequest& request,
47 int* process_id, int* route_id) {
48 const ResourceRequestInfo* info = ResourceRequestInfo::ForRequest(&request);
49 if (!info)
50 return false;
51
52 return info->GetAssociatedRenderView(process_id, route_id);
53 }
54
55 void CallCacheStatsTabEventOnIOThread(std::pair<int, int> render_view_id,
56 CacheStats::TabEvent event,
57 IOThread* io_thread) {
58 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
59 static CacheStats* cache_stats = io_thread->globals()->cache_stats.get();
willchan no longer on Chromium 2012/07/26 01:39:37 Why bother with the static?
tburkard 2012/07/26 02:10:40 Done.
60 cache_stats->OnTabEvent(render_view_id, event);
61 }
62
63 // Times after a load has started at which stats are collected.
64 const int kStatsCollectionTimesMs[] = {
65 500,
66 1000,
67 2000,
68 3000,
69 4000,
70 5000,
71 7500,
72 10000,
73 15000,
74 20000
75 };
76
77 static int kTabLoadStatsAutoCleanupTimeoutSeconds = 30;
78
79 } // namespace
80
81 // Helper struct keeping stats about the page load progress & cache usage
82 // stats during the pageload so far for a given RenderView, identified
83 // by a pair of process id and route id.
84 struct CacheStats::TabLoadStats {
85 TabLoadStats(std::pair<int, int> render_view_id, CacheStats* owner)
86 : render_view_id(render_view_id),
87 num_active(0),
88 spinner_started(false),
89 timer(false, false) {
90 // Initialize the timer to do an automatic cleanup. If a pageload is
91 // started for the TabLoadStats within that timeframe, CacheStats
92 // will start using the timer, thereby cancelling the cleanup.
93 // Once CacheStats starts the timer, the object is guaranteed to be
94 // destroyed eventually, so there is no more need for automatic cleanup at
95 // that point.
96 timer.Start(FROM_HERE,
97 base::TimeDelta::FromSeconds(
98 kTabLoadStatsAutoCleanupTimeoutSeconds),
99 base::Bind(&CacheStats::RemoveTabLoadStats,
100 base::Unretained(owner),
101 render_view_id));
102 }
103
104 std::pair<int, int> render_view_id;
105 int num_active;
106 bool spinner_started;
107 base::TimeTicks load_start_time;
108 base::TimeTicks cache_start_time;
109 base::TimeDelta cache_total_time;
110 base::Timer timer;
111 typedef base::hash_map<const net::URLRequest*, int> PerRequestNumActiveMap;
112 PerRequestNumActiveMap per_request_num_active;
113 };
114
115 CacheStatsTabHelper::CacheStatsTabHelper(TabContents* tab)
116 : content::WebContentsObserver(tab->web_contents()),
117 cache_stats_(NULL) {
118 is_profile_otr_ = tab->profile()->IsOffTheRecord();
119 }
120
121 void CacheStatsTabHelper::DidStartProvisionalLoadForFrame(
122 int64 frame_id,
123 bool is_main_frame,
124 const GURL& validated_url,
125 bool is_error_page,
126 content::RenderViewHost* render_view_host) {
127 if (!is_main_frame)
128 return;
129 if (!validated_url.SchemeIs("http"))
130 return;
131 NotifyCacheStats(CacheStats::SPINNER_START, render_view_host);
132 }
133
134 void CacheStatsTabHelper::DidStopLoading(RenderViewHost* render_view_host) {
135 NotifyCacheStats(CacheStats::SPINNER_STOP, render_view_host);
136 }
137
138 CacheStatsTabHelper::~CacheStatsTabHelper() {
139 }
140
141 void CacheStatsTabHelper::NotifyCacheStats(
142 CacheStats::TabEvent event,
143 RenderViewHost* render_view_host) {
144 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
145 if (is_profile_otr_)
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 if (tab_load_stats_.count(render_view_id) < 1)
184 tab_load_stats_[render_view_id] = new TabLoadStats(render_view_id, this);
185 return tab_load_stats_[render_view_id];
186 }
187
188 void CacheStats::RemoveTabLoadStats(std::pair<int, int> render_view_id) {
189 TabLoadStatsMap::iterator it = tab_load_stats_.find(render_view_id);
190 if (it != tab_load_stats_.end()) {
191 delete it->second;
192 tab_load_stats_.erase(it);
193 }
194 }
195
196 void CacheStats::OnCacheWaitStateChange(
197 const net::URLRequest& request,
198 net::NetworkDelegate::CacheWaitState state) {
199 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
200 int process_id, route_id;
201 if (!GetRenderView(request, &process_id, &route_id))
202 return;
203 TabLoadStats* stats =
204 GetTabLoadStats(std::pair<int, int>(process_id, route_id));
205 if (main_request_contexts_.count(request.context()) < 1)
206 return;
207 bool newly_started = false;
208 bool newly_finished = false;
209 std::pair<TabLoadStats::PerRequestNumActiveMap::iterator, bool> insert_ret =
210 stats->per_request_num_active.insert(
211 std::pair<const net::URLRequest*, int>(&request, 0));
212 TabLoadStats::PerRequestNumActiveMap::iterator entry = insert_ret.first;
213 DCHECK_GE(entry->second, 0);
214 switch (state) {
215 case net::NetworkDelegate::CACHE_WAIT_STATE_START:
216 DCHECK(entry->second == 0);
217 if (entry->second == 0)
218 newly_started = true;
219 entry->second++;
220 break;
221 case net::NetworkDelegate::CACHE_WAIT_STATE_FINISH:
222 if (entry->second > 0) {
223 entry->second--;
224 if (entry->second == 0)
225 newly_finished = true;
226 }
227 break;
228 case net::NetworkDelegate::CACHE_WAIT_STATE_RESET:
229 if (entry->second > 0) {
230 entry->second = 0;
231 newly_finished = true;
232 }
233 break;
234 }
235 DCHECK_GE(entry->second, 0);
236 if (newly_started) {
237 DCHECK(!newly_finished);
238 if (stats->num_active == 0) {
239 stats->cache_start_time = base::TimeTicks::Now();
240 }
241 stats->num_active++;
242 }
243 if (newly_finished) {
244 DCHECK(!newly_started);
245 if (stats->num_active == 1) {
246 stats->cache_total_time +=
247 base::TimeTicks::Now() - stats->cache_start_time;
248 }
249 stats->num_active--;
250 }
251 }
252
253 void CacheStats::OnTabEvent(std::pair<int, int> render_view_id,
254 TabEvent event) {
255 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
256 TabLoadStats* stats = GetTabLoadStats(render_view_id);
257 if (event == SPINNER_START) {
258 stats->spinner_started = true;
259 stats->cache_total_time = base::TimeDelta();
260 stats->cache_start_time = base::TimeTicks::Now();
261 stats->load_start_time = base::TimeTicks::Now();
262 ScheduleTimer(stats, 0);
263 } else {
264 DCHECK_EQ(event, SPINNER_STOP);
265 if (stats->spinner_started) {
266 stats->spinner_started = false;
267 base::TimeDelta load_time =
268 base::TimeTicks::Now() - stats->load_start_time;
269 if (stats->num_active > 1)
270 stats->cache_total_time +=
271 base::TimeTicks::Now() - stats->cache_start_time;
272 RecordCacheFractionHistogram(load_time, stats->cache_total_time, true);
273 }
274 RemoveTabLoadStats(render_view_id);
275 }
276 }
277
278 void CacheStats::ScheduleTimer(TabLoadStats* stats, int 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::TimerCb,
287 base::Unretained(this),
288 base::Unretained(stats),
289 timer_index));
290 }
291
292 void CacheStats::TimerCb(TabLoadStats* stats, int timer_index) {
293 DCHECK(stats->spinner_started);
294 base::TimeDelta load_time = base::TimeTicks::Now() - stats->load_start_time;
295 base::TimeDelta cache_time = stats->cache_total_time;
296 if (stats->num_active > 1)
297 cache_time += base::TimeTicks::Now() - stats->cache_start_time;
298 RecordCacheFractionHistogram(load_time, cache_time, false);
299 timer_index++;
300 if (timer_index < static_cast<int>(arraysize(kStatsCollectionTimesMs)))
301 ScheduleTimer(stats, timer_index);
302 else
303 RemoveTabLoadStats(stats->render_view_id);
304 }
305
306 void CacheStats::RecordCacheFractionHistogram(base::TimeDelta elapsed,
307 base::TimeDelta cache_time,
308 bool is_load_done) {
309 if (elapsed.InMilliseconds() <= 0)
310 return;
311
312 int64 cache_fraction_percentage =
313 100 * cache_time.InMilliseconds() / elapsed.InMilliseconds();
314
315 DCHECK(cache_fraction_percentage >= 0 && cache_fraction_percentage < 100);
316
317 int index = 0;
318 while (index + 1 < static_cast<int>(arraysize(kStatsCollectionTimesMs)) &&
319 base::TimeDelta::FromMilliseconds(kStatsCollectionTimesMs[index + 1]) <
320 elapsed) {
321 index++;
322 }
323
324 if (is_load_done) {
325 final_histograms_[index]->Add(cache_fraction_percentage);
326 } else {
327 intermediate_histograms_[index]->Add(cache_fraction_percentage);
328 }
329 }
330
331 void CacheStats::RegisterURLRequestContext(
332 const net::URLRequestContext* context,
333 ChromeURLRequestContext::ContextType type) {
334 if (type == ChromeURLRequestContext::CONTEXT_TYPE_MAIN)
335 main_request_contexts_.insert(context);
336 }
337
338 void CacheStats::UnregisterURLRequestContext(
339 const net::URLRequestContext* context) {
340 main_request_contexts_.erase(context);
341 }
342
343 } // namespace chrome_browser_net
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698