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.
5 #include "chrome/browser/net/url_info.h"
13 #include "base/format_macros.h"
14 #include "base/lazy_instance.h"
15 #include "base/logging.h"
16 #include "base/metrics/histogram.h"
17 #include "base/strings/stringprintf.h"
20 using base::TimeDelta
;
21 using base::TimeTicks
;
23 namespace chrome_browser_net
{
27 // The number of OS cache entries we can guarantee(?) before cache eviction
28 // might likely take place.
29 const int kMaxGuaranteedDnsCacheSize
= 50;
31 // Common low end TTL for sites is 5 minutes. However, DNS servers give us the
32 // remaining time, not the original 5 minutes. Hence it doesn't much matter
33 // whether we found something in the local cache, or an ISP cache, it will on
34 // average be 2.5 minutes before it expires. We could try to model this with
35 // 180 seconds, but simpler is just to do the lookups all the time (wasting OS
36 // calls(?)), and let that OS cache decide what to do (with TTL in hand). We
37 // use a small time to help get some duplicate suppression, in case a page has
38 // a TON of copies of the same domain name, so that we don't thrash the OS to
39 // death. Hopefully it is small enough that we're not hurting our cache hit
40 // rate (i.e., we could always ask the OS).
41 const int kDefaultCacheExpirationDuration
= 5;
43 TimeDelta
MaxNonNetworkDnsLookupDuration() {
44 return TimeDelta::FromMilliseconds(15);
47 bool detailed_logging_enabled
= false;
51 cache_expiration_duration
=
52 TimeDelta::FromSeconds(kDefaultCacheExpirationDuration
);
54 TimeDelta cache_expiration_duration
;
57 base::LazyInstance
<GlobalState
>::Leaky global_state
;
59 } // anonymous namespace
61 // Use command line switch to enable detailed logging.
62 void EnablePredictorDetailedLog(bool enable
) {
63 detailed_logging_enabled
= enable
;
67 int UrlInfo::sequence_counter
= 1;
71 old_prequeue_state_(state_
),
72 resolve_duration_(NullDuration()),
73 queue_duration_(NullDuration()),
75 motivation_(NO_PREFETCH_MOTIVATION
),
79 UrlInfo::~UrlInfo() {}
81 bool UrlInfo::NeedsDnsUpdate() {
83 case PENDING
: // Just now created info.
86 case QUEUED
: // In queue.
87 case ASSIGNED
: // It's being resolved.
88 case ASSIGNED_BUT_MARKED
: // It's being resolved.
89 return false; // We're already working on it
91 case NO_SUCH_NAME
: // Lookup failed.
92 case FOUND
: // Lookup succeeded.
93 return !IsStillCached(); // See if DNS cache expired.
101 // Used by test ONLY. The value is otherwise constant.
103 void UrlInfo::set_cache_expiration(TimeDelta time
) {
104 global_state
.Pointer()->cache_expiration_duration
= time
;
108 TimeDelta
UrlInfo::get_cache_expiration() {
109 return global_state
.Get().cache_expiration_duration
;
112 void UrlInfo::SetQueuedState(ResolutionMotivation motivation
) {
113 DCHECK(PENDING
== state_
|| FOUND
== state_
|| NO_SUCH_NAME
== state_
);
114 old_prequeue_state_
= state_
;
116 queue_duration_
= resolve_duration_
= NullDuration();
117 SetMotivation(motivation
);
118 GetDuration(); // Set time_
119 DLogResultsStats("DNS Prefetch in queue");
122 void UrlInfo::SetAssignedState() {
123 DCHECK(QUEUED
== state_
);
125 queue_duration_
= GetDuration();
126 DLogResultsStats("DNS Prefetch assigned");
127 UMA_HISTOGRAM_TIMES("DNS.PrefetchQueue", queue_duration_
);
130 void UrlInfo::RemoveFromQueue() {
131 DCHECK(ASSIGNED
== state_
);
132 state_
= old_prequeue_state_
;
133 DLogResultsStats("DNS Prefetch reset to prequeue");
134 const TimeDelta kBoundary
= TimeDelta::FromSeconds(2);
135 if (queue_duration_
> kBoundary
) {
136 UMA_HISTOGRAM_MEDIUM_TIMES("DNS.QueueRecycledDeltaOver2",
137 queue_duration_
- kBoundary
);
140 // Make a custom linear histogram for the region from 0 to boundary.
141 static const size_t kBucketCount
= 52;
142 static base::HistogramBase
* histogram(NULL
);
144 histogram
= base::LinearHistogram::FactoryTimeGet(
145 "DNS.QueueRecycledUnder2", TimeDelta(), kBoundary
, kBucketCount
,
146 base::HistogramBase::kUmaTargetedHistogramFlag
);
147 histogram
->AddTime(queue_duration_
);
150 void UrlInfo::SetPendingDeleteState() {
151 DCHECK(ASSIGNED
== state_
|| ASSIGNED_BUT_MARKED
== state_
);
152 state_
= ASSIGNED_BUT_MARKED
;
155 void UrlInfo::SetFoundState() {
156 DCHECK(ASSIGNED
== state_
);
158 resolve_duration_
= GetDuration();
159 const TimeDelta max_duration
= MaxNonNetworkDnsLookupDuration();
160 if (max_duration
<= resolve_duration_
) {
161 UMA_HISTOGRAM_CUSTOM_TIMES("DNS.PrefetchResolution", resolve_duration_
,
162 max_duration
, TimeDelta::FromMinutes(15), 100);
164 sequence_number_
= sequence_counter
++;
165 DLogResultsStats("DNS PrefetchFound");
168 void UrlInfo::SetNoSuchNameState() {
169 DCHECK(ASSIGNED
== state_
);
170 state_
= NO_SUCH_NAME
;
171 resolve_duration_
= GetDuration();
172 if (MaxNonNetworkDnsLookupDuration() <= resolve_duration_
) {
173 DHISTOGRAM_TIMES("DNS.PrefetchNotFoundName", resolve_duration_
);
175 sequence_number_
= sequence_counter
++;
176 DLogResultsStats("DNS PrefetchNotFound");
179 void UrlInfo::SetUrl(const GURL
& url
) {
180 if (url_
.is_empty()) // Not yet initialized.
183 DCHECK_EQ(url_
, url
);
186 // IsStillCached() guesses if the DNS cache still has IP data,
187 // or at least remembers results about "not finding host."
188 bool UrlInfo::IsStillCached() const {
189 DCHECK(FOUND
== state_
|| NO_SUCH_NAME
== state_
);
191 // Default MS OS does not cache failures. Hence we could return false almost
192 // all the time for that case. However, we'd never try again to prefetch
193 // the value if we returned false that way. Hence we'll just let the lookup
194 // time out the same way as FOUND case.
196 if (sequence_counter
- sequence_number_
> kMaxGuaranteedDnsCacheSize
)
199 TimeDelta time_since_resolution
= TimeTicks::Now() - time_
;
200 return time_since_resolution
< global_state
.Get().cache_expiration_duration
;
203 void UrlInfo::DLogResultsStats(const char* message
) const {
204 if (!detailed_logging_enabled
)
206 DVLOG(1) << "\t" << message
<< "\tq=" << queue_duration().InMilliseconds()
207 << "ms,\tr=" << resolve_duration().InMilliseconds()
208 << "ms,\tp=" << sequence_number_
<< "\t" << url_
.spec();
211 //------------------------------------------------------------------------------
212 // This last section supports HTML output, such as seen in about:dns.
213 //------------------------------------------------------------------------------
215 // Preclude any possibility of Java Script or markup in the text, by only
216 // allowing alphanumerics, '.', '-', ':', and whitespace.
217 static std::string
RemoveJs(const std::string
& text
) {
218 std::string
output(text
);
219 size_t length
= output
.length();
220 for (size_t i
= 0; i
< length
; i
++) {
221 char next
= output
[i
];
222 if (isalnum(next
) || isspace(next
) || strchr(".-:/", next
) != NULL
)
229 class MinMaxAverage
{
232 : sum_(0), square_sum_(0), count_(0),
233 minimum_(kint64max
), maximum_(kint64min
) {
236 // Return values for use in printf formatted as "%d"
237 int sample(int64 value
) {
239 square_sum_
+= value
* value
;
241 minimum_
= std::min(minimum_
, value
);
242 maximum_
= std::max(maximum_
, value
);
243 return static_cast<int>(value
);
245 int minimum() const { return static_cast<int>(minimum_
); }
246 int maximum() const { return static_cast<int>(maximum_
); }
247 int average() const { return static_cast<int>(sum_
/count_
); }
248 int sum() const { return static_cast<int>(sum_
); }
250 int standard_deviation() const {
251 double average
= static_cast<float>(sum_
) / count_
;
252 double variance
= static_cast<float>(square_sum_
)/count_
254 return static_cast<int>(floor(sqrt(variance
) + .5));
264 // DISALLOW_COPY_AND_ASSIGN(MinMaxAverage);
267 static std::string
HoursMinutesSeconds(int seconds
) {
269 int print_seconds
= seconds
% 60;
270 int minutes
= seconds
/ 60;
271 int print_minutes
= minutes
% 60;
272 int print_hours
= minutes
/60;
274 base::StringAppendF(&result
, "%.2d:", print_hours
);
275 if (print_hours
|| print_minutes
)
276 base::StringAppendF(&result
, "%2.2d:", print_minutes
);
277 base::StringAppendF(&result
, "%2.2d", print_seconds
);
282 void UrlInfo::GetHtmlTable(const UrlInfoTable
& host_infos
,
283 const char* description
,
285 std::string
* output
) {
286 if (0 == host_infos
.size())
288 output
->append(description
);
289 base::StringAppendF(output
, "%" PRIuS
" %s", host_infos
.size(),
290 (1 == host_infos
.size()) ? "hostname" : "hostnames");
293 output
->append("<br><br>");
297 output
->append("<br><table border=1>"
298 "<tr><th>Host name</th>"
299 "<th>How long ago<br>(HH:MM:SS)</th>"
300 "<th>Motivation</th>"
303 const char* row_format
= "<tr align=right><td>%s</td>" // Host name.
304 "<td>%s</td>" // How long ago.
305 "<td>%s</td>" // Motivation.
308 // Print bulk of table, and gather stats at same time.
309 MinMaxAverage queue
, when
;
310 TimeTicks current_time
= TimeTicks::Now();
311 for (UrlInfoTable::const_iterator
it(host_infos
.begin());
312 it
!= host_infos
.end(); it
++) {
313 queue
.sample((it
->queue_duration_
.InMilliseconds()));
317 RemoveJs(it
->url_
.spec()).c_str(),
318 HoursMinutesSeconds(when
.sample(
319 (current_time
- it
->time_
).InSeconds())).c_str(),
320 it
->GetAsciiMotivation().c_str());
322 output
->append("</table>");
327 "Prefetch Queue Durations: min=%d, avg=%d, max=%d<br><br>",
328 queue
.minimum(), queue
.average(), queue
.maximum());
331 output
->append("<br>");
334 void UrlInfo::SetMotivation(ResolutionMotivation motivation
) {
335 motivation_
= motivation
;
336 if (motivation
< LINKED_MAX_MOTIVATED
)
340 std::string
UrlInfo::GetAsciiMotivation() const {
341 switch (motivation_
) {
342 case MOUSE_OVER_MOTIVATED
:
343 return "[mouse-over]";
345 case PAGE_SCAN_MOTIVATED
:
346 return "[page scan]";
348 case OMNIBOX_MOTIVATED
:
351 case STARTUP_LIST_MOTIVATED
:
352 return "[startup list]";
354 case NO_PREFETCH_MOTIVATION
:
357 case STATIC_REFERAL_MOTIVATED
:
358 return RemoveJs(referring_url_
.spec()) + "*";
360 case LEARNED_REFERAL_MOTIVATED
:
361 return RemoveJs(referring_url_
.spec());
364 return std::string();
368 } // namespace chrome_browser_net