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