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