| 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 void HistogramUpdateStale(const HostCache::EntryInternal& old_entry, |
| 30 const HostCache::Entry& new_entry, |
| 31 base::TimeDelta expired_by, |
| 32 unsigned network_changes, |
| 33 unsigned stale_hits) { |
| 34 AddressListDeltaType delta = |
| 35 FindAddressListDeltaType(old_entry.addrlist, new_entry.addrlist); |
| 36 CACHE_HISTOGRAM_ENUM("UpdateStaleDelta", delta, MAX_DELTA_TYPE); |
| 37 switch (delta) { |
| 38 case DELTA_IDENTICAL: |
| 39 CACHE_HISTOGRAM_TIME("UpdateStaleExpiredBy_Identical", expired_by); |
| 40 CACHE_HISTOGRAM_COUNT("UpdateStaleNetworkChanges_Identical", |
| 41 network_changes); |
| 42 break; |
| 43 case DELTA_REORDERED: |
| 44 CACHE_HISTOGRAM_TIME("UpdateStaleExpiredBy_Reordered", expired_by); |
| 45 CACHE_HISTOGRAM_COUNT("UpdateStaleNetworkChanges_Reordered", |
| 46 network_changes); |
| 47 break; |
| 48 case DELTA_OVERLAP: |
| 49 CACHE_HISTOGRAM_TIME("UpdateStaleExpiredBy_Overlap", expired_by); |
| 50 CACHE_HISTOGRAM_COUNT("UpdateStaleNetworkChanges_Overlap", |
| 51 network_changes); |
| 52 break; |
| 53 case DELTA_DISJOINT: |
| 54 CACHE_HISTOGRAM_TIME("UpdateStaleExpiredBy_Disjoint", expired_by); |
| 55 CACHE_HISTOGRAM_COUNT("UpdateStaleNetworkChanges_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 HistogramSet(SetOutcome outcome, |
| 73 base::TimeTicks now, |
| 74 unsigned 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 unsigned stale_hits = old_entry->stale_hits; |
| 88 CACHE_HISTOGRAM_TIME("UpdateStaleExpiredBy", expired_by); |
| 89 CACHE_HISTOGRAM_COUNT("UpdateStaleNetworkChanges", network_changes); |
| 90 CACHE_HISTOGRAM_COUNT("UpdateStaleHits", stale_hits); |
| 91 if (old_entry->error == OK && new_entry.error == OK) { |
| 92 HistogramUpdateStale(*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 HistogramLookup(LookupOutcome outcome, |
| 113 base::TimeTicks now, |
| 114 unsigned 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("LookupStaleExpiredBy", now - entry->expires); |
| 126 CACHE_HISTOGRAM_COUNT("LookupStaleNetworkChanges", |
| 127 network_changes - entry->network_changes); |
| 128 CACHE_HISTOGRAM_COUNT("LookupStaleHits", 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 HistogramErase(EraseReason reason, |
| 145 base::TimeTicks now, |
| 146 unsigned network_changes, |
| 147 const HostCache::EntryInternal& entry) { |
| 148 HostCache::StaleEntryInfo stale; |
| 149 entry.CheckStale(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("EvictStaleExpiredBy", stale.expired_by); |
| 155 CACHE_HISTOGRAM_COUNT("EvictStaleNetworkChanges", |
| 156 stale.network_changes); |
| 157 CACHE_HISTOGRAM_COUNT("EvictStaleHits", entry.stale_hits); |
| 158 } else { |
| 159 CACHE_HISTOGRAM_TIME("EvictValidFor", -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("ClearStaleExpiredBy", stale.expired_by); |
| 167 CACHE_HISTOGRAM_COUNT("ClearStaleNetworkChanges", |
| 168 stale.network_changes); |
| 169 CACHE_HISTOGRAM_COUNT("ClearStaleHits", entry.stale_hits); |
| 170 } else { |
| 171 CACHE_HISTOGRAM_TIME("ClearValidFor", -stale.expired_by); |
| 172 } |
| 173 break; |
| 174 case ERASE_DESTRUCT: |
| 175 break; |
| 176 default: |
| 177 NOTREACHED(); |
| 178 break; |
| 179 } |
| 180 } |
| 181 |
| 182 void HistogramEraseAll(EraseReason reason, |
| 183 base::TimeTicks now, |
| 184 unsigned network_changes, |
| 185 const HostCache::EntryMap& entries) { |
| 186 for (const auto& it : entries) |
| 187 HistogramErase(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_(0u) {} |
| 40 } | |
| 41 | 210 |
| 42 HostCache::~HostCache() { | 211 HostCache::~HostCache() { |
| 212 HistogramEraseAll(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) { |
| 218 return LookupStale(key, now, nullptr); |
| 219 } |
| 220 |
| 221 const HostCache::Entry* HostCache::LookupStale( |
| 222 const Key& key, |
| 223 base::TimeTicks now, |
| 224 HostCache::StaleEntryInfo* stale_out) { |
| 47 DCHECK(CalledOnValidThread()); | 225 DCHECK(CalledOnValidThread()); |
| 48 if (caching_is_disabled()) | 226 if (caching_is_disabled()) |
| 49 return NULL; | 227 return nullptr; |
| 50 | 228 |
| 51 return entries_.Get(key, now); | 229 auto it = entries_.find(key); |
| 230 if (it == entries_.end()) { |
| 231 HistogramLookup(LOOKUP_MISS_ABSENT, now, network_changes_, nullptr); |
| 232 return nullptr; |
| 233 } |
| 234 |
| 235 bool is_stale = it->second.CheckStale(now, network_changes_, stale_out); |
| 236 if (is_stale && !stale_out) { |
| 237 HistogramLookup(LOOKUP_MISS_STALE, now, network_changes_, &it->second); |
| 238 return nullptr; |
| 239 } |
| 240 |
| 241 if (is_stale) { |
| 242 HistogramLookup(LOOKUP_HIT_STALE, now, network_changes_, &it->second); |
| 243 stale_out->stale_hits = ++it->second.stale_hits; |
| 244 } else { |
| 245 HistogramLookup(LOOKUP_HIT_VALID, now, network_changes_, &it->second); |
| 246 } |
| 247 ++it->second.total_hits; |
| 248 |
| 249 return &it->second; |
| 52 } | 250 } |
| 53 | 251 |
| 54 void HostCache::Set(const Key& key, | 252 void HostCache::Set(const Key& key, |
| 55 const Entry& entry, | 253 const Entry& entry, |
| 56 base::TimeTicks now, | 254 base::TimeTicks now, |
| 57 base::TimeDelta ttl) { | 255 base::TimeDelta ttl) { |
| 58 TRACE_EVENT0("net", "HostCache::Set"); | 256 TRACE_EVENT0("net", "HostCache::Set"); |
| 59 DCHECK(CalledOnValidThread()); | 257 DCHECK(CalledOnValidThread()); |
| 60 if (caching_is_disabled()) | 258 if (caching_is_disabled()) |
| 61 return; | 259 return; |
| 62 | 260 |
| 63 entries_.Put(key, entry, now, now + ttl); | 261 auto it = entries_.find(key); |
| 262 if (it != entries_.end()) { |
| 263 bool is_stale = it->second.CheckStale(now, network_changes_, nullptr); |
| 264 if (is_stale) |
| 265 HistogramSet(SET_UPDATE_STALE, now, network_changes_, &it->second, entry); |
| 266 else |
| 267 HistogramSet(SET_UPDATE_VALID, now, network_changes_, &it->second, entry); |
| 268 // TODO(juliatuttle): Remember some old metadata, if it's useful? |
| 269 entries_.erase(it); |
| 270 } else { |
| 271 if (size() == max_entries_) |
| 272 Evict(now); |
| 273 HistogramSet(SET_INSERT, now, network_changes_, nullptr, entry); |
| 274 } |
| 275 |
| 276 DCHECK_GT(max_entries_, size()); |
| 277 DCHECK_EQ(0u, entries_.count(key)); |
| 278 entries_.insert(std::make_pair( |
| 279 Key(key), EntryInternal(entry, now, ttl, network_changes_))); |
| 280 DCHECK_GE(max_entries_, size()); |
| 281 } |
| 282 |
| 283 void HostCache::OnNetworkChange() { |
| 284 ++network_changes_; |
| 64 } | 285 } |
| 65 | 286 |
| 66 void HostCache::clear() { | 287 void HostCache::clear() { |
| 67 DCHECK(CalledOnValidThread()); | 288 DCHECK(CalledOnValidThread()); |
| 68 entries_.Clear(); | 289 HistogramEraseAll(ERASE_CLEAR, base::TimeTicks::Now(), network_changes_, |
| 290 entries_); |
| 291 entries_.clear(); |
| 69 } | 292 } |
| 70 | 293 |
| 71 size_t HostCache::size() const { | 294 size_t HostCache::size() const { |
| 72 DCHECK(CalledOnValidThread()); | 295 DCHECK(CalledOnValidThread()); |
| 73 return entries_.size(); | 296 return entries_.size(); |
| 74 } | 297 } |
| 75 | 298 |
| 76 size_t HostCache::max_entries() const { | 299 size_t HostCache::max_entries() const { |
| 77 DCHECK(CalledOnValidThread()); | 300 DCHECK(CalledOnValidThread()); |
| 78 return entries_.max_entries(); | 301 return max_entries_; |
| 302 } |
| 303 |
| 304 std::unique_ptr<base::Value> HostCache::GetEntriesAsValue() const { |
| 305 base::ListValue* entry_list = new base::ListValue(); |
| 306 |
| 307 for (auto& pair : entries_) { |
| 308 const Key& key = pair.first; |
| 309 const EntryInternal& entry = pair.second; |
| 310 |
| 311 base::DictionaryValue* entry_dict = new base::DictionaryValue(); |
| 312 |
| 313 entry_dict->SetString("hostname", key.hostname); |
| 314 entry_dict->SetInteger("address_family", |
| 315 static_cast<int>(key.address_family)); |
| 316 entry_dict->SetString("expiration", |
| 317 NetLog::TickCountToString(entry.expires)); |
| 318 |
| 319 if (entry.error != OK) { |
| 320 entry_dict->SetInteger("error", entry.error); |
| 321 } else { |
| 322 base::ListValue* address_list = new base::ListValue(); |
| 323 for (size_t i = 0; i < entry.addrlist.size(); ++i) |
| 324 address_list->AppendString(entry.addrlist[i].ToStringWithoutPort()); |
| 325 entry_dict->Set("addresses", address_list); |
| 326 } |
| 327 |
| 328 entry_list->Append(entry_dict); |
| 329 } |
| 330 |
| 331 return base::WrapUnique(entry_list); |
| 79 } | 332 } |
| 80 | 333 |
| 81 // Note that this map may contain expired entries. | 334 // Note that this map may contain expired entries. |
| 82 const HostCache::EntryMap& HostCache::entries() const { | 335 /* const HostCache::EntryMap& HostCache::entries() const { |
| 83 DCHECK(CalledOnValidThread()); | 336 DCHECK(CalledOnValidThread()); |
| 84 return entries_; | 337 return entries_; |
| 85 } | 338 } */ |
| 86 | 339 |
| 87 // static | 340 // static |
| 88 std::unique_ptr<HostCache> HostCache::CreateDefaultCache() { | 341 std::unique_ptr<HostCache> HostCache::CreateDefaultCache() { |
| 89 // Cache capacity is determined by the field trial. | 342 // Cache capacity is determined by the field trial. |
| 90 #if defined(ENABLE_BUILT_IN_DNS) | 343 #if defined(ENABLE_BUILT_IN_DNS) |
| 91 const size_t kDefaultMaxEntries = 1000; | 344 const size_t kDefaultMaxEntries = 1000; |
| 92 #else | 345 #else |
| 93 const size_t kDefaultMaxEntries = 100; | 346 const size_t kDefaultMaxEntries = 100; |
| 94 #endif | 347 #endif |
| 95 const size_t kSaneMaxEntries = 1 << 20; | 348 const size_t kSaneMaxEntries = 1 << 20; |
| 96 size_t max_entries = 0; | 349 size_t max_entries = 0; |
| 97 base::StringToSizeT(base::FieldTrialList::FindFullName("HostCacheSize"), | 350 base::StringToSizeT(base::FieldTrialList::FindFullName("HostCacheSize"), |
| 98 &max_entries); | 351 &max_entries); |
| 99 if ((max_entries == 0) || (max_entries > kSaneMaxEntries)) | 352 if ((max_entries == 0) || (max_entries > kSaneMaxEntries)) |
| 100 max_entries = kDefaultMaxEntries; | 353 max_entries = kDefaultMaxEntries; |
| 101 return base::WrapUnique(new HostCache(max_entries)); | 354 return base::WrapUnique(new HostCache(max_entries)); |
| 102 } | 355 } |
| 103 | 356 |
| 104 void HostCache::EvictionHandler::Handle( | 357 HostCache::EntryInternal::EntryInternal(const HostCache::Entry& entry, |
| 105 const Key& key, | 358 base::TimeTicks now, |
| 106 const Entry& entry, | 359 base::TimeDelta ttl, |
| 107 const base::TimeTicks& expiration, | 360 unsigned network_changes) |
| 108 const base::TimeTicks& now, | 361 : HostCache::Entry(entry), |
| 109 bool on_get) const { | 362 added(now), |
| 110 if (on_get) { | 363 expires(now + ttl), |
| 111 DCHECK(now >= expiration); | 364 network_changes(network_changes), |
| 112 UMA_HISTOGRAM_CUSTOM_TIMES("DNS.CacheExpiredOnGet", now - expiration, | 365 total_hits(0u), |
| 113 base::TimeDelta::FromSeconds(1), base::TimeDelta::FromDays(1), 100); | 366 stale_hits(0u) {} |
| 114 return; | 367 |
| 368 bool HostCache::EntryInternal::CheckStale( |
| 369 base::TimeTicks now, |
| 370 unsigned network_changes, |
| 371 HostCache::StaleEntryInfo* out) const { |
| 372 StaleEntryInfo stale; |
| 373 stale.expired_by = now - expires; |
| 374 stale.network_changes = network_changes - this->network_changes; |
| 375 stale.stale_hits = stale_hits; |
| 376 |
| 377 if (out) |
| 378 *out = stale; |
| 379 return stale.is_stale(); |
| 380 } |
| 381 |
| 382 void HostCache::Evict(base::TimeTicks now) { |
| 383 DCHECK_LT(0u, entries_.size()); |
| 384 |
| 385 EntryMap::iterator it = SelectEntryToEvict(); |
| 386 HistogramErase(ERASE_EVICT, now, network_changes_, it->second); |
| 387 entries_.erase(it); |
| 388 } |
| 389 |
| 390 HostCache::EntryMap::iterator HostCache::SelectEntryToEvict() { |
| 391 DCHECK_LT(0u, entries_.size()); |
| 392 |
| 393 auto oldest_it = entries_.begin(); |
| 394 for (auto it = entries_.begin(); it != entries_.end(); ++it) { |
| 395 if (it->second.expires < oldest_it->second.expires) |
| 396 oldest_it = it; |
| 115 } | 397 } |
| 116 if (expiration > now) { | 398 return oldest_it; |
| 117 UMA_HISTOGRAM_CUSTOM_TIMES("DNS.CacheEvicted", expiration - now, | |
| 118 base::TimeDelta::FromSeconds(1), base::TimeDelta::FromDays(1), 100); | |
| 119 } else { | |
| 120 UMA_HISTOGRAM_CUSTOM_TIMES("DNS.CacheExpired", now - expiration, | |
| 121 base::TimeDelta::FromSeconds(1), base::TimeDelta::FromDays(1), 100); | |
| 122 } | |
| 123 } | 399 } |
| 124 | 400 |
| 125 } // namespace net | 401 } // namespace net |
| OLD | NEW |