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

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

Powered by Google App Engine
This is Rietveld 408576698