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

Side by Side Diff: net/dns/host_cache.cc

Issue 1908543002: DNS: Retain stale entries in HostCache and return when requested (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Make requested changes. Created 4 years, 7 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
OLDNEW
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 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 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "net/dns/host_cache.h" 5 #include "net/dns/host_cache.h"
6 6
7 #include "base/logging.h" 7 #include "base/logging.h"
8 #include "base/memory/ptr_util.h" 8 #include "base/memory/ptr_util.h"
9 #include "base/metrics/field_trial.h" 9 #include "base/metrics/field_trial.h"
10 #include "base/metrics/histogram_macros.h" 10 #include "base/metrics/histogram_macros.h"
11 #include "base/strings/string_number_conversions.h" 11 #include "base/strings/string_number_conversions.h"
12 #include "base/trace_event/trace_event.h" 12 #include "base/trace_event/trace_event.h"
13 #include "net/base/net_errors.h" 13 #include "net/base/net_errors.h"
14 #include "net/dns/dns_util.h"
14 15
15 namespace net { 16 namespace net {
16 17
17 //----------------------------------------------------------------------------- 18 namespace {
19
20 #define CACHE_HISTOGRAM_TIME(name, time) \
21 UMA_HISTOGRAM_LONG_TIMES("DNS.HostCache." name, time)
22
23 #define CACHE_HISTOGRAM_COUNT(name, count) \
24 UMA_HISTOGRAM_COUNTS_1000("DNS.HostCache." name, count)
25
26 #define CACHE_HISTOGRAM_ENUM(name, value, max) \
27 UMA_HISTOGRAM_ENUMERATION("DNS.HostCache." name, value, max)
28
29 void RecordUpdateStale(const HostCache::EntryInternal& old_entry,
30 const HostCache::Entry& new_entry,
31 base::TimeDelta expired_by,
32 int network_changes,
33 int stale_hits) {
34 AddressListDeltaType delta =
35 FindAddressListDeltaType(old_entry.addrlist, new_entry.addrlist);
36 CACHE_HISTOGRAM_ENUM("UpdateStale.AddressListDelta", delta, MAX_DELTA_TYPE);
37 switch (delta) {
38 case DELTA_IDENTICAL:
39 CACHE_HISTOGRAM_TIME("UpdateStale.ExpiredBy_Identical", expired_by);
40 CACHE_HISTOGRAM_COUNT("UpdateStale.NetworkChanges_Identical",
41 network_changes);
42 break;
43 case DELTA_REORDERED:
44 CACHE_HISTOGRAM_TIME("UpdateStale.ExpiredBy_Reordered", expired_by);
45 CACHE_HISTOGRAM_COUNT("UpdateStale.NetworkChanges_Reordered",
46 network_changes);
47 break;
48 case DELTA_OVERLAP:
49 CACHE_HISTOGRAM_TIME("UpdateStale.ExpiredBy_Overlap", expired_by);
50 CACHE_HISTOGRAM_COUNT("UpdateStale.NetworkChanges_Overlap",
51 network_changes);
52 break;
53 case DELTA_DISJOINT:
54 CACHE_HISTOGRAM_TIME("UpdateStale.ExpiredBy_Disjoint", expired_by);
55 CACHE_HISTOGRAM_COUNT("UpdateStale.NetworkChanges_Dijsoint",
56 network_changes);
57 break;
58 case MAX_DELTA_TYPE:
59 NOTREACHED();
60 break;
61 }
62 }
63
64 // Used in histograms; do not modify existing values.
65 enum SetOutcome {
66 SET_INSERT = 0,
67 SET_UPDATE_VALID = 1,
68 SET_UPDATE_STALE = 2,
69 MAX_SET_OUTCOME
70 };
71
72 void RecordSet(SetOutcome outcome,
73 base::TimeTicks now,
74 int network_changes,
75 const HostCache::EntryInternal* old_entry,
76 const HostCache::Entry& new_entry) {
77 CACHE_HISTOGRAM_ENUM("Set", outcome, MAX_SET_OUTCOME);
78 switch (outcome) {
79 case SET_INSERT:
80 // Nothing to log here.
81 break;
82 case SET_UPDATE_VALID:
83 break;
84 case SET_UPDATE_STALE: {
85 base::TimeDelta expired_by = now - old_entry->expires;
86 network_changes = network_changes - old_entry->network_changes;
87 int stale_hits = old_entry->stale_hits;
88 CACHE_HISTOGRAM_TIME("UpdateStale.ExpiredBy", expired_by);
89 CACHE_HISTOGRAM_COUNT("UpdateStale.NetworkChanges", network_changes);
90 CACHE_HISTOGRAM_COUNT("UpdateStale.StaleHits", stale_hits);
91 if (old_entry->error == OK && new_entry.error == OK) {
92 RecordUpdateStale(*old_entry, new_entry, expired_by, network_changes,
93 stale_hits);
94 }
95 break;
96 }
97 case MAX_SET_OUTCOME:
98 NOTREACHED();
99 break;
100 }
101 }
102
103 // Used in histograms; do not modify existing values.
104 enum LookupOutcome {
105 LOOKUP_MISS_ABSENT = 0,
106 LOOKUP_MISS_STALE = 1,
107 LOOKUP_HIT_VALID = 2,
108 LOOKUP_HIT_STALE = 3,
109 MAX_LOOKUP_OUTCOME
110 };
111
112 void RecordLookup(LookupOutcome outcome,
113 base::TimeTicks now,
114 int network_changes,
115 const HostCache::EntryInternal* entry) {
116 CACHE_HISTOGRAM_ENUM("Lookup", outcome, MAX_LOOKUP_OUTCOME);
117 switch (outcome) {
118 case LOOKUP_MISS_ABSENT:
119 // Nothing to log here.
120 break;
121 case LOOKUP_MISS_STALE:
122 case LOOKUP_HIT_VALID:
123 break;
124 case LOOKUP_HIT_STALE:
125 CACHE_HISTOGRAM_TIME("LookupStale.ExpiredBy", now - entry->expires);
126 CACHE_HISTOGRAM_COUNT("LookupStale.NetworkChanges",
127 network_changes - entry->network_changes);
128 CACHE_HISTOGRAM_COUNT("LookupStale.Hits", entry->stale_hits);
129 break;
130 case MAX_LOOKUP_OUTCOME:
131 NOTREACHED();
132 break;
133 }
134 }
135
136 // Used in histograms; do not modify existing values.
137 enum EraseReason {
138 ERASE_EVICT = 0,
139 ERASE_CLEAR = 1,
140 ERASE_DESTRUCT = 2,
141 MAX_ERASE_REASON
142 };
143
144 void RecordErase(EraseReason reason,
145 base::TimeTicks now,
146 int network_changes,
147 const HostCache::EntryInternal& entry) {
148 HostCache::EntryStaleness stale;
149 entry.GetStaleness(now, network_changes, &stale);
150 CACHE_HISTOGRAM_ENUM("Erase", reason, MAX_ERASE_REASON);
151 switch (reason) {
152 case ERASE_EVICT:
153 if (stale.is_stale()) {
154 CACHE_HISTOGRAM_TIME("EvictStale.ExpiredBy", stale.expired_by);
155 CACHE_HISTOGRAM_COUNT("EvictStale.NetworkChanges",
156 stale.network_changes);
157 CACHE_HISTOGRAM_COUNT("EvictStale.StaleHits", entry.stale_hits);
158 } else {
159 CACHE_HISTOGRAM_TIME("EvictValid.ValidFor", -stale.expired_by);
160 }
161 break;
162 case ERASE_CLEAR:
163 // TODO(juliatuttle): Remove these once we stop clearing the cache on
164 // network change.
165 if (stale.is_stale()) {
166 CACHE_HISTOGRAM_TIME("ClearStale.ExpiredBy", stale.expired_by);
167 CACHE_HISTOGRAM_COUNT("ClearStale.NetworkChanges",
168 stale.network_changes);
169 CACHE_HISTOGRAM_COUNT("ClearStale.StaleHits", entry.stale_hits);
170 } else {
171 CACHE_HISTOGRAM_TIME("ClearValid.ValidFor", -stale.expired_by);
172 }
173 break;
174 case ERASE_DESTRUCT:
175 break;
176 default:
177 NOTREACHED();
178 break;
179 }
180 }
181
182 void RecordEraseAll(EraseReason reason,
183 base::TimeTicks now,
184 int network_changes,
185 const HostCache::EntryMap& entries) {
186 for (const auto& it : entries)
187 RecordErase(reason, now, network_changes, it.second);
188 }
189
190 } // namespace
18 191
19 HostCache::Entry::Entry(int error, const AddressList& addrlist, 192 HostCache::Entry::Entry(int error, const AddressList& addrlist,
20 base::TimeDelta ttl) 193 base::TimeDelta ttl)
21 : error(error), 194 : error(error),
22 addrlist(addrlist), 195 addrlist(addrlist),
23 ttl(ttl) { 196 ttl(ttl) {
24 DCHECK(ttl >= base::TimeDelta()); 197 DCHECK(ttl >= base::TimeDelta());
25 } 198 }
26 199
27 HostCache::Entry::Entry(int error, const AddressList& addrlist) 200 HostCache::Entry::Entry(int error, const AddressList& addrlist)
28 : error(error), 201 : error(error),
29 addrlist(addrlist), 202 addrlist(addrlist),
30 ttl(base::TimeDelta::FromSeconds(-1)) { 203 ttl(base::TimeDelta::FromSeconds(-1)) {
31 } 204 }
32 205
33 HostCache::Entry::~Entry() { 206 HostCache::Entry::~Entry() {}
34 }
35
36 //-----------------------------------------------------------------------------
37 207
38 HostCache::HostCache(size_t max_entries) 208 HostCache::HostCache(size_t max_entries)
39 : entries_(max_entries) { 209 : max_entries_(max_entries), network_changes_(0) {}
40 }
41 210
42 HostCache::~HostCache() { 211 HostCache::~HostCache() {
212 RecordEraseAll(ERASE_DESTRUCT, base::TimeTicks::Now(), network_changes_,
213 entries_);
43 } 214 }
44 215
45 const HostCache::Entry* HostCache::Lookup(const Key& key, 216 const HostCache::Entry* HostCache::Lookup(const Key& key,
46 base::TimeTicks now) { 217 base::TimeTicks now) {
47 DCHECK(CalledOnValidThread()); 218 DCHECK(CalledOnValidThread());
48 if (caching_is_disabled()) 219 if (caching_is_disabled())
49 return NULL; 220 return nullptr;
50 221
51 return entries_.Get(key, now); 222 HostCache::EntryInternal* entry = LookupInternal(key);
223 if (!entry) {
224 RecordLookup(LOOKUP_MISS_ABSENT, now, network_changes_, nullptr);
225 return nullptr;
226 }
227 if (entry->IsStale(now, network_changes_)) {
228 RecordLookup(LOOKUP_MISS_STALE, now, network_changes_, entry);
229 return nullptr;
230 }
231
232 entry->CountHit(/* hit_is_stale= */ false);
233 RecordLookup(LOOKUP_HIT_VALID, now, network_changes_, entry);
234 return entry;
235 }
236
237 const HostCache::Entry* HostCache::LookupStale(
238 const Key& key,
239 base::TimeTicks now,
240 HostCache::EntryStaleness* stale_out) {
241 DCHECK(CalledOnValidThread());
242 if (caching_is_disabled())
243 return nullptr;
244
245 HostCache::EntryInternal* entry = LookupInternal(key);
246 if (!entry) {
247 RecordLookup(LOOKUP_MISS_ABSENT, now, network_changes_, nullptr);
248 return nullptr;
249 }
250
251 bool is_stale = entry->IsStale(now, network_changes_);
252 entry->CountHit(is_stale);
253 RecordLookup(is_stale ? LOOKUP_HIT_STALE : LOOKUP_HIT_VALID, now,
254 network_changes_, entry);
255
256 if (stale_out)
257 entry->GetStaleness(now, network_changes_, stale_out);
258 return entry;
259 }
260
261 HostCache::EntryInternal* HostCache::LookupInternal(const Key& key) {
262 auto it = entries_.find(key);
263 return (it != entries_.end()) ? &it->second : nullptr;
52 } 264 }
53 265
54 void HostCache::Set(const Key& key, 266 void HostCache::Set(const Key& key,
55 const Entry& entry, 267 const Entry& entry,
56 base::TimeTicks now, 268 base::TimeTicks now,
57 base::TimeDelta ttl) { 269 base::TimeDelta ttl) {
58 TRACE_EVENT0("net", "HostCache::Set"); 270 TRACE_EVENT0("net", "HostCache::Set");
59 DCHECK(CalledOnValidThread()); 271 DCHECK(CalledOnValidThread());
60 if (caching_is_disabled()) 272 if (caching_is_disabled())
61 return; 273 return;
62 274
63 entries_.Put(key, entry, now, now + ttl); 275 auto it = entries_.find(key);
276 if (it != entries_.end()) {
277 bool is_stale = it->second.IsStale(now, network_changes_);
278 RecordSet(is_stale ? SET_UPDATE_STALE : SET_UPDATE_VALID, now,
279 network_changes_, &it->second, entry);
280 // TODO(juliatuttle): Remember some old metadata, if it's useful?
Ryan Sleevi 2016/04/29 23:09:37 I generally push on TODOs to include a little more
Julia Tuttle 2016/05/03 20:36:19 Done.
281 entries_.erase(it);
282 } else {
283 if (size() == max_entries_)
284 EvictOneEntry(now);
285 RecordSet(SET_INSERT, now, network_changes_, nullptr, entry);
286 }
287
288 DCHECK_GT(max_entries_, size());
289 DCHECK_EQ(0u, entries_.count(key));
290 entries_.insert(std::make_pair(
291 Key(key), EntryInternal(entry, now, ttl, network_changes_)));
292 DCHECK_GE(max_entries_, size());
293 }
294
295 void HostCache::OnNetworkChange() {
296 ++network_changes_;
64 } 297 }
65 298
66 void HostCache::clear() { 299 void HostCache::clear() {
Ryan Sleevi 2016/04/29 23:09:38 Seems like this should be renamed Clear(), because
Julia Tuttle 2016/05/03 20:36:19 Should it? The histograms seem like an implementat
67 DCHECK(CalledOnValidThread()); 300 DCHECK(CalledOnValidThread());
68 entries_.Clear(); 301 RecordEraseAll(ERASE_CLEAR, base::TimeTicks::Now(), network_changes_,
302 entries_);
303 entries_.clear();
69 } 304 }
70 305
71 size_t HostCache::size() const { 306 size_t HostCache::size() const {
72 DCHECK(CalledOnValidThread()); 307 DCHECK(CalledOnValidThread());
73 return entries_.size(); 308 return entries_.size();
74 } 309 }
75 310
76 size_t HostCache::max_entries() const { 311 size_t HostCache::max_entries() const {
77 DCHECK(CalledOnValidThread()); 312 DCHECK(CalledOnValidThread());
78 return entries_.max_entries(); 313 return max_entries_;
79 } 314 }
80 315
81 // Note that this map may contain expired entries. 316 std::unique_ptr<base::Value> HostCache::GetEntriesAsValue() const {
82 const HostCache::EntryMap& HostCache::entries() const { 317 std::unique_ptr<base::ListValue> entry_list(new base::ListValue());
83 DCHECK(CalledOnValidThread()); 318
84 return entries_; 319 for (const auto& pair : entries_) {
320 const Key& key = pair.first;
321 const EntryInternal& entry = pair.second;
322
323 base::DictionaryValue* entry_dict = new base::DictionaryValue();
324
325 entry_dict->SetString("hostname", key.hostname);
326 entry_dict->SetInteger("address_family",
327 static_cast<int>(key.address_family));
328 entry_dict->SetString("expiration",
329 NetLog::TickCountToString(entry.expires));
330
331 if (entry.error != OK) {
332 entry_dict->SetInteger("error", entry.error);
333 } else {
334 base::ListValue* address_list = new base::ListValue();
335 for (size_t i = 0; i < entry.addrlist.size(); ++i)
336 address_list->AppendString(entry.addrlist[i].ToStringWithoutPort());
337 entry_dict->Set("addresses", address_list);
338 }
339
340 entry_list->Append(entry_dict);
341 }
342
343 return std::move(entry_list);
85 } 344 }
86 345
87 // static 346 // static
88 std::unique_ptr<HostCache> HostCache::CreateDefaultCache() { 347 std::unique_ptr<HostCache> HostCache::CreateDefaultCache() {
89 // Cache capacity is determined by the field trial. 348 // Cache capacity is determined by the field trial.
90 #if defined(ENABLE_BUILT_IN_DNS) 349 #if defined(ENABLE_BUILT_IN_DNS)
91 const size_t kDefaultMaxEntries = 1000; 350 const size_t kDefaultMaxEntries = 1000;
92 #else 351 #else
93 const size_t kDefaultMaxEntries = 100; 352 const size_t kDefaultMaxEntries = 100;
94 #endif 353 #endif
95 const size_t kSaneMaxEntries = 1 << 20; 354 const size_t kSaneMaxEntries = 1 << 20;
96 size_t max_entries = 0; 355 size_t max_entries = 0;
97 base::StringToSizeT(base::FieldTrialList::FindFullName("HostCacheSize"), 356 base::StringToSizeT(base::FieldTrialList::FindFullName("HostCacheSize"),
98 &max_entries); 357 &max_entries);
99 if ((max_entries == 0) || (max_entries > kSaneMaxEntries)) 358 if ((max_entries == 0) || (max_entries > kSaneMaxEntries))
100 max_entries = kDefaultMaxEntries; 359 max_entries = kDefaultMaxEntries;
101 return base::WrapUnique(new HostCache(max_entries)); 360 return base::WrapUnique(new HostCache(max_entries));
102 } 361 }
103 362
104 void HostCache::EvictionHandler::Handle( 363 HostCache::EntryInternal::EntryInternal(const HostCache::Entry& entry,
105 const Key& key, 364 base::TimeTicks now,
106 const Entry& entry, 365 base::TimeDelta ttl,
107 const base::TimeTicks& expiration, 366 int network_changes)
108 const base::TimeTicks& now, 367 : HostCache::Entry(entry),
109 bool on_get) const { 368 expires(now + ttl),
110 if (on_get) { 369 network_changes(network_changes),
111 DCHECK(now >= expiration); 370 total_hits(0),
112 UMA_HISTOGRAM_CUSTOM_TIMES("DNS.CacheExpiredOnGet", now - expiration, 371 stale_hits(0) {}
113 base::TimeDelta::FromSeconds(1), base::TimeDelta::FromDays(1), 100); 372
114 return; 373 bool HostCache::EntryInternal::IsStale(base::TimeTicks now,
374 int network_changes) const {
375 EntryStaleness stale;
376 stale.expired_by = now - expires;
377 stale.network_changes = network_changes - this->network_changes;
378 stale.stale_hits = stale_hits;
379 return stale.is_stale();
380 }
381
382 void HostCache::EntryInternal::CountHit(bool hit_is_stale) {
383 ++total_hits;
384 if (hit_is_stale)
385 ++stale_hits;
386 }
387
388 void HostCache::EntryInternal::GetStaleness(base::TimeTicks now,
389 int network_changes,
390 EntryStaleness* out) const {
391 DCHECK(out);
392 out->expired_by = now - expires;
393 out->network_changes = network_changes - this->network_changes;
394 out->stale_hits = stale_hits;
395 }
396
397 void HostCache::EvictOneEntry(base::TimeTicks now) {
398 DCHECK_LT(0u, entries_.size());
399
400 auto oldest_it = entries_.begin();
401 for (auto it = entries_.begin(); it != entries_.end(); ++it) {
402 if (it->second.expires < oldest_it->second.expires)
403 oldest_it = it;
115 } 404 }
116 if (expiration > now) { 405
117 UMA_HISTOGRAM_CUSTOM_TIMES("DNS.CacheEvicted", expiration - now, 406 RecordErase(ERASE_EVICT, now, network_changes_, oldest_it->second);
118 base::TimeDelta::FromSeconds(1), base::TimeDelta::FromDays(1), 100); 407 entries_.erase(oldest_it);
119 } else {
120 UMA_HISTOGRAM_CUSTOM_TIMES("DNS.CacheExpired", now - expiration,
121 base::TimeDelta::FromSeconds(1), base::TimeDelta::FromDays(1), 100);
122 }
123 } 408 }
124 409
125 } // namespace net 410 } // namespace net
OLDNEW
« net/dns/host_cache.h ('K') | « net/dns/host_cache.h ('k') | net/dns/host_cache_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698