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