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, | |
mmenke
2012/07/27 19:03:54
Suggest a comment that this can only be called on
tburkard
2012/07/27 23:25:44
Done.
| |
42 int* process_id, int* route_id) { | |
43 const ResourceRequestInfo* info = ResourceRequestInfo::ForRequest(&request); | |
44 if (!info) | |
45 return false; | |
46 | |
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) | |
mmenke
2012/07/27 19:03:54
Is this if really needed?
tburkard
2012/07/27 23:25:44
Yes, otherwise, some tests fail!
On 2012/07/27 19:
| |
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 timer(false, false) { | |
88 // Initialize the timer to do an automatic cleanup. If a pageload is | |
89 // started for the TabLoadStats within that timeframe, CacheStats | |
90 // will start using the timer, thereby cancelling the cleanup. | |
91 // Once CacheStats starts the timer, the object is guaranteed to be | |
92 // destroyed eventually, so there is no more need for automatic cleanup at | |
93 // that point. | |
94 timer.Start(FROM_HERE, | |
95 base::TimeDelta::FromSeconds( | |
96 kTabLoadStatsAutoCleanupTimeoutSeconds), | |
97 base::Bind(&CacheStats::RemoveTabLoadStats, | |
98 base::Unretained(owner), | |
99 render_view_id)); | |
100 } | |
101 | |
102 std::pair<int, int> render_view_id; | |
103 int num_active; | |
104 bool spinner_started; | |
105 base::TimeTicks load_start_time; | |
106 base::TimeTicks cache_start_time; | |
107 base::TimeDelta cache_total_time; | |
108 base::Timer timer; | |
mmenke
2012/07/27 19:03:54
Might want to just make this a base::ResourceReque
mmenke
2012/07/27 19:03:54
The Google style guide requires non-POD types to b
tburkard
2012/07/27 23:25:44
I wanted to use a oneshot timer at first, but it d
tburkard
2012/07/27 23:25:44
base::TimeTicks/TimeDelta are probably also not PO
mmenke
2012/07/29 23:45:57
I think that since they actually does stuff to the
| |
109 // URLRequest's for which there are outstanding cache transactions. | |
110 base::hash_set<const net::URLRequest*> active_requests; | |
111 }; | |
112 | |
113 CacheStatsTabHelper::CacheStatsTabHelper(TabContents* tab) | |
114 : content::WebContentsObserver(tab->web_contents()), | |
115 cache_stats_(NULL) { | |
116 is_otr_profile_ = tab->profile()->IsOffTheRecord(); | |
117 } | |
118 | |
119 void CacheStatsTabHelper::DidStartProvisionalLoadForFrame( | |
120 int64 frame_id, | |
121 bool is_main_frame, | |
122 const GURL& validated_url, | |
123 bool is_error_page, | |
124 content::RenderViewHost* render_view_host) { | |
125 if (!is_main_frame) | |
126 return; | |
127 if (!validated_url.SchemeIs("http")) | |
128 return; | |
129 NotifyCacheStats(CacheStats::SPINNER_START, render_view_host); | |
130 } | |
131 | |
132 void CacheStatsTabHelper::DidStopLoading(RenderViewHost* render_view_host) { | |
133 NotifyCacheStats(CacheStats::SPINNER_STOP, render_view_host); | |
134 } | |
135 | |
136 CacheStatsTabHelper::~CacheStatsTabHelper() { | |
137 } | |
mmenke
2012/07/27 19:03:54
nit: This should be up with the constructor, to m
tburkard
2012/07/27 23:25:44
Done.
| |
138 | |
139 void CacheStatsTabHelper::NotifyCacheStats( | |
140 CacheStats::TabEvent event, | |
141 RenderViewHost* render_view_host) { | |
142 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | |
143 if (is_otr_profile_) | |
144 return; | |
145 int process_id = render_view_host->GetProcess()->GetID(); | |
146 int route_id = render_view_host->GetRoutingID(); | |
147 BrowserThread::PostTask( | |
148 BrowserThread::IO, FROM_HERE, | |
149 base::Bind(&CallCacheStatsTabEventOnIOThread, | |
150 std::pair<int, int>(process_id, route_id), | |
151 event, | |
152 base::Unretained(g_browser_process->io_thread()))); | |
153 } | |
154 | |
155 CacheStats::CacheStats() { | |
156 for (int i = 0; | |
157 i < static_cast<int>(arraysize(kStatsCollectionTimesMs)); | |
158 i++) { | |
159 final_histograms_.push_back( | |
160 base::LinearHistogram::FactoryGet( | |
161 "CacheStats.FractionCacheUseFinalPLT_" + | |
162 base::IntToString(kStatsCollectionTimesMs[i]), | |
163 0, 101, 102, base::Histogram::kUmaTargetedHistogramFlag)); | |
164 intermediate_histograms_.push_back( | |
165 base::LinearHistogram::FactoryGet( | |
166 "DiskCache.FractionCacheUseIntermediatePLT_" + | |
167 base::IntToString(kStatsCollectionTimesMs[i]), | |
168 0, 101, 102, base::Histogram::kNoFlags)); | |
169 } | |
170 DCHECK_EQ(final_histograms_.size(), arraysize(kStatsCollectionTimesMs)); | |
171 DCHECK_EQ(intermediate_histograms_.size(), | |
172 arraysize(kStatsCollectionTimesMs)); | |
173 } | |
174 | |
175 CacheStats::~CacheStats() { | |
176 STLDeleteValues(&tab_load_stats_); | |
177 } | |
178 | |
179 CacheStats::TabLoadStats* CacheStats::GetTabLoadStats( | |
180 std::pair<int, int> render_view_id) { | |
181 if (tab_load_stats_.count(render_view_id) < 1) | |
mmenke
2012/07/27 19:03:54
Currently, this code could result in always doing
tburkard
2012/07/27 23:25:44
Reduced by 1, since I don't want to construct TabL
| |
182 tab_load_stats_[render_view_id] = new TabLoadStats(render_view_id, this); | |
183 return tab_load_stats_[render_view_id]; | |
184 } | |
185 | |
186 void CacheStats::RemoveTabLoadStats(std::pair<int, int> render_view_id) { | |
187 TabLoadStatsMap::iterator it = tab_load_stats_.find(render_view_id); | |
188 if (it != tab_load_stats_.end()) { | |
189 delete it->second; | |
190 tab_load_stats_.erase(it); | |
191 } | |
192 } | |
193 | |
194 void CacheStats::OnCacheWaitStateChange( | |
195 const net::URLRequest& request, | |
196 net::NetworkDelegate::CacheWaitState state) { | |
197 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | |
198 int process_id, route_id; | |
199 if (!GetRenderView(request, &process_id, &route_id)) | |
200 return; | |
201 TabLoadStats* stats = | |
202 GetTabLoadStats(std::pair<int, int>(process_id, route_id)); | |
203 if (main_request_contexts_.count(request.context()) < 1) | |
mmenke
2012/07/27 19:03:54
Can this happen? We should delete the request str
tburkard
2012/07/27 23:25:44
Not exactly sure what you mean.. but I put the che
mmenke
2012/07/27 23:38:42
Sorry - wrote that comment before I noticed we wer
| |
204 return; | |
205 bool newly_started = false; | |
206 bool newly_finished = false; | |
207 switch (state) { | |
208 case net::NetworkDelegate::CACHE_WAIT_STATE_START: | |
209 DCHECK(stats->active_requests.count(&request) == 0); | |
210 newly_started = true; | |
211 stats->active_requests.insert(&request); | |
212 break; | |
213 case net::NetworkDelegate::CACHE_WAIT_STATE_FINISH: | |
214 if (stats->active_requests.count(&request) > 0) { | |
215 stats->active_requests.erase(&request); | |
216 newly_finished = true; | |
217 } | |
218 break; | |
219 case net::NetworkDelegate::CACHE_WAIT_STATE_RESET: | |
220 if (stats->active_requests.count(&request) > 0) { | |
221 stats->active_requests.erase(&request); | |
222 newly_finished = true; | |
223 } | |
224 break; | |
225 } | |
226 DCHECK_GE(stats->num_active, 0); | |
227 if (newly_started) { | |
228 DCHECK(!newly_finished); | |
229 if (stats->num_active == 0) { | |
230 stats->cache_start_time = base::TimeTicks::Now(); | |
231 } | |
232 stats->num_active++; | |
233 } | |
234 if (newly_finished) { | |
235 DCHECK(!newly_started); | |
236 if (stats->num_active == 1) { | |
237 stats->cache_total_time += | |
238 base::TimeTicks::Now() - stats->cache_start_time; | |
239 } | |
240 stats->num_active--; | |
241 } | |
242 DCHECK_GE(stats->num_active, 0); | |
243 } | |
244 | |
245 void CacheStats::OnTabEvent(std::pair<int, int> render_view_id, | |
246 TabEvent event) { | |
247 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | |
248 TabLoadStats* stats = GetTabLoadStats(render_view_id); | |
249 if (event == SPINNER_START) { | |
250 stats->spinner_started = true; | |
251 stats->cache_total_time = base::TimeDelta(); | |
252 stats->cache_start_time = base::TimeTicks::Now(); | |
253 stats->load_start_time = base::TimeTicks::Now(); | |
254 ScheduleTimer(stats, 0); | |
255 } else { | |
256 DCHECK_EQ(event, SPINNER_STOP); | |
257 if (stats->spinner_started) { | |
258 stats->spinner_started = false; | |
259 base::TimeDelta load_time = | |
260 base::TimeTicks::Now() - stats->load_start_time; | |
261 if (stats->num_active > 1) | |
262 stats->cache_total_time += | |
263 base::TimeTicks::Now() - stats->cache_start_time; | |
264 RecordCacheFractionHistogram(load_time, stats->cache_total_time, true); | |
265 } | |
266 RemoveTabLoadStats(render_view_id); | |
267 } | |
268 } | |
269 | |
270 void CacheStats::ScheduleTimer(TabLoadStats* stats, int timer_index) { | |
271 DCHECK(timer_index >= 0 && | |
272 timer_index < static_cast<int>(arraysize(kStatsCollectionTimesMs))); | |
273 base::TimeDelta delta = | |
274 base::TimeDelta::FromMilliseconds(kStatsCollectionTimesMs[timer_index]); | |
275 delta -= base::TimeTicks::Now() - stats->load_start_time; | |
276 stats->timer.Start(FROM_HERE, | |
277 delta, | |
278 base::Bind(&CacheStats::TimerCallback, | |
279 base::Unretained(this), | |
280 base::Unretained(stats), | |
281 timer_index)); | |
282 } | |
283 | |
284 void CacheStats::TimerCallback(TabLoadStats* stats, int timer_index) { | |
285 DCHECK(stats->spinner_started); | |
286 base::TimeDelta load_time = base::TimeTicks::Now() - stats->load_start_time; | |
287 base::TimeDelta cache_time = stats->cache_total_time; | |
288 if (stats->num_active > 1) | |
289 cache_time += base::TimeTicks::Now() - stats->cache_start_time; | |
290 RecordCacheFractionHistogram(load_time, cache_time, false); | |
291 timer_index++; | |
292 if (timer_index < static_cast<int>(arraysize(kStatsCollectionTimesMs))) | |
293 ScheduleTimer(stats, timer_index); | |
294 else | |
295 RemoveTabLoadStats(stats->render_view_id); | |
296 } | |
297 | |
298 void CacheStats::RecordCacheFractionHistogram(base::TimeDelta elapsed, | |
299 base::TimeDelta cache_time, | |
300 bool is_load_done) { | |
301 if (elapsed.InMilliseconds() <= 0) | |
302 return; | |
303 | |
304 int64 cache_fraction_percentage = | |
305 100 * cache_time.InMilliseconds() / elapsed.InMilliseconds(); | |
306 | |
307 DCHECK(cache_fraction_percentage >= 0 && cache_fraction_percentage < 100); | |
mmenke
2012/07/27 19:03:54
I'd suggest using <= 100. Yes, in general, it sho
tburkard
2012/07/27 23:25:44
Done.
| |
308 | |
309 int index = 0; | |
310 while (index + 1 < static_cast<int>(arraysize(kStatsCollectionTimesMs)) && | |
311 base::TimeDelta::FromMilliseconds(kStatsCollectionTimesMs[index + 1]) < | |
mmenke
2012/07/27 19:03:54
This seems a little weird for final histograms. f
tburkard
2012/07/27 23:25:44
Fixed it slightly differently. Put the timer_inde
mmenke
2012/07/29 23:45:57
Like you solution better than mine.
On 2012/07/27
| |
312 elapsed) { | |
313 index++; | |
314 } | |
315 | |
316 if (is_load_done) { | |
317 final_histograms_[index]->Add(cache_fraction_percentage); | |
318 } else { | |
319 intermediate_histograms_[index]->Add(cache_fraction_percentage); | |
320 } | |
321 } | |
322 | |
323 void CacheStats::RegisterURLRequestContext( | |
324 const net::URLRequestContext* context, | |
325 ChromeURLRequestContext::ContextType type) { | |
326 if (type == ChromeURLRequestContext::CONTEXT_TYPE_MAIN) | |
327 main_request_contexts_.insert(context); | |
328 } | |
329 | |
330 void CacheStats::UnregisterURLRequestContext( | |
331 const net::URLRequestContext* context) { | |
332 main_request_contexts_.erase(context); | |
333 } | |
334 | |
335 } // namespace chrome_browser_net | |
OLD | NEW |