| 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 { |
| 18 | 19 |
| 19 HostCache::Entry::Entry(int error, const AddressList& addrlist, | 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 }; |
| 55 |
| 56 HostCache::Entry::Entry(int error, |
| 57 const AddressList& addresses, |
| 20 base::TimeDelta ttl) | 58 base::TimeDelta ttl) |
| 21 : error(error), | 59 : error_(error), addresses_(addresses), ttl_(ttl) { |
| 22 addrlist(addrlist), | |
| 23 ttl(ttl) { | |
| 24 DCHECK(ttl >= base::TimeDelta()); | 60 DCHECK(ttl >= base::TimeDelta()); |
| 25 } | 61 } |
| 26 | 62 |
| 27 HostCache::Entry::Entry(int error, const AddressList& addrlist) | 63 HostCache::Entry::Entry(int error, const AddressList& addresses) |
| 28 : error(error), | 64 : error_(error), |
| 29 addrlist(addrlist), | 65 addresses_(addresses), |
| 30 ttl(base::TimeDelta::FromSeconds(-1)) { | 66 ttl_(base::TimeDelta::FromSeconds(-1)) {} |
| 67 |
| 68 HostCache::Entry::~Entry() {} |
| 69 |
| 70 HostCache::Entry::Entry(const HostCache::Entry& entry, |
| 71 base::TimeTicks now, |
| 72 base::TimeDelta ttl, |
| 73 int network_changes) |
| 74 : error_(entry.error()), |
| 75 addresses_(entry.addresses()), |
| 76 ttl_(entry.ttl()), |
| 77 expires_(now + ttl), |
| 78 network_changes_(network_changes), |
| 79 total_hits_(0), |
| 80 stale_hits_(0) {} |
| 81 |
| 82 bool HostCache::Entry::IsStale(base::TimeTicks now, int network_changes) const { |
| 83 EntryStaleness stale; |
| 84 stale.expired_by = now - expires_; |
| 85 stale.network_changes = network_changes - network_changes_; |
| 86 stale.stale_hits = stale_hits_; |
| 87 return stale.is_stale(); |
| 31 } | 88 } |
| 32 | 89 |
| 33 HostCache::Entry::~Entry() { | 90 void HostCache::Entry::CountHit(bool hit_is_stale) { |
| 91 ++total_hits_; |
| 92 if (hit_is_stale) |
| 93 ++stale_hits_; |
| 34 } | 94 } |
| 35 | 95 |
| 36 //----------------------------------------------------------------------------- | 96 void HostCache::Entry::GetStaleness(base::TimeTicks now, |
| 97 int network_changes, |
| 98 EntryStaleness* out) const { |
| 99 DCHECK(out); |
| 100 out->expired_by = now - expires_; |
| 101 out->network_changes = network_changes - network_changes_; |
| 102 out->stale_hits = stale_hits_; |
| 103 } |
| 37 | 104 |
| 38 HostCache::HostCache(size_t max_entries) | 105 HostCache::HostCache(size_t max_entries) |
| 39 : entries_(max_entries) { | 106 : max_entries_(max_entries), network_changes_(0) {} |
| 40 } | |
| 41 | 107 |
| 42 HostCache::~HostCache() { | 108 HostCache::~HostCache() { |
| 109 RecordEraseAll(ERASE_DESTRUCT, base::TimeTicks::Now()); |
| 43 } | 110 } |
| 44 | 111 |
| 45 const HostCache::Entry* HostCache::Lookup(const Key& key, | 112 const HostCache::Entry* HostCache::Lookup(const Key& key, |
| 46 base::TimeTicks now) { | 113 base::TimeTicks now) { |
| 47 DCHECK(CalledOnValidThread()); | 114 DCHECK(CalledOnValidThread()); |
| 48 if (caching_is_disabled()) | 115 if (caching_is_disabled()) |
| 49 return NULL; | 116 return nullptr; |
| 50 | 117 |
| 51 return entries_.Get(key, now); | 118 HostCache::Entry* entry = LookupInternal(key); |
| 119 if (!entry) { |
| 120 RecordLookup(LOOKUP_MISS_ABSENT, now, nullptr); |
| 121 return nullptr; |
| 122 } |
| 123 if (entry->IsStale(now, network_changes_)) { |
| 124 RecordLookup(LOOKUP_MISS_STALE, now, entry); |
| 125 return nullptr; |
| 126 } |
| 127 |
| 128 entry->CountHit(/* hit_is_stale= */ false); |
| 129 RecordLookup(LOOKUP_HIT_VALID, now, entry); |
| 130 return entry; |
| 131 } |
| 132 |
| 133 const HostCache::Entry* HostCache::LookupStale( |
| 134 const Key& key, |
| 135 base::TimeTicks now, |
| 136 HostCache::EntryStaleness* stale_out) { |
| 137 DCHECK(CalledOnValidThread()); |
| 138 if (caching_is_disabled()) |
| 139 return nullptr; |
| 140 |
| 141 HostCache::Entry* entry = LookupInternal(key); |
| 142 if (!entry) { |
| 143 RecordLookup(LOOKUP_MISS_ABSENT, now, nullptr); |
| 144 return nullptr; |
| 145 } |
| 146 |
| 147 bool is_stale = entry->IsStale(now, network_changes_); |
| 148 entry->CountHit(/* hit_is_stale= */ is_stale); |
| 149 RecordLookup(is_stale ? LOOKUP_HIT_STALE : LOOKUP_HIT_VALID, now, entry); |
| 150 |
| 151 if (stale_out) |
| 152 entry->GetStaleness(now, network_changes_, stale_out); |
| 153 return entry; |
| 154 } |
| 155 |
| 156 HostCache::Entry* HostCache::LookupInternal(const Key& key) { |
| 157 auto it = entries_.find(key); |
| 158 return (it != entries_.end()) ? &it->second : nullptr; |
| 52 } | 159 } |
| 53 | 160 |
| 54 void HostCache::Set(const Key& key, | 161 void HostCache::Set(const Key& key, |
| 55 const Entry& entry, | 162 const Entry& entry, |
| 56 base::TimeTicks now, | 163 base::TimeTicks now, |
| 57 base::TimeDelta ttl) { | 164 base::TimeDelta ttl) { |
| 58 TRACE_EVENT0("net", "HostCache::Set"); | 165 TRACE_EVENT0("net", "HostCache::Set"); |
| 59 DCHECK(CalledOnValidThread()); | 166 DCHECK(CalledOnValidThread()); |
| 60 if (caching_is_disabled()) | 167 if (caching_is_disabled()) |
| 61 return; | 168 return; |
| 62 | 169 |
| 63 entries_.Put(key, entry, now, now + ttl); | 170 auto it = entries_.find(key); |
| 171 if (it != entries_.end()) { |
| 172 bool is_stale = it->second.IsStale(now, network_changes_); |
| 173 RecordSet(is_stale ? SET_UPDATE_STALE : SET_UPDATE_VALID, now, &it->second, |
| 174 entry); |
| 175 // TODO(juliatuttle): Remember some old metadata (hit count or frequency or |
| 176 // something like that) if it's useful for better eviction algorithms? |
| 177 entries_.erase(it); |
| 178 } else { |
| 179 if (size() == max_entries_) |
| 180 EvictOneEntry(now); |
| 181 RecordSet(SET_INSERT, now, nullptr, entry); |
| 182 } |
| 183 |
| 184 DCHECK_GT(max_entries_, size()); |
| 185 DCHECK_EQ(0u, entries_.count(key)); |
| 186 entries_.insert( |
| 187 std::make_pair(Key(key), Entry(entry, now, ttl, network_changes_))); |
| 188 DCHECK_GE(max_entries_, size()); |
| 189 } |
| 190 |
| 191 void HostCache::OnNetworkChange() { |
| 192 ++network_changes_; |
| 64 } | 193 } |
| 65 | 194 |
| 66 void HostCache::clear() { | 195 void HostCache::clear() { |
| 67 DCHECK(CalledOnValidThread()); | 196 DCHECK(CalledOnValidThread()); |
| 68 entries_.Clear(); | 197 RecordEraseAll(ERASE_CLEAR, base::TimeTicks::Now()); |
| 198 entries_.clear(); |
| 69 } | 199 } |
| 70 | 200 |
| 71 size_t HostCache::size() const { | 201 size_t HostCache::size() const { |
| 72 DCHECK(CalledOnValidThread()); | 202 DCHECK(CalledOnValidThread()); |
| 73 return entries_.size(); | 203 return entries_.size(); |
| 74 } | 204 } |
| 75 | 205 |
| 76 size_t HostCache::max_entries() const { | 206 size_t HostCache::max_entries() const { |
| 77 DCHECK(CalledOnValidThread()); | 207 DCHECK(CalledOnValidThread()); |
| 78 return entries_.max_entries(); | 208 return max_entries_; |
| 79 } | |
| 80 | |
| 81 // Note that this map may contain expired entries. | |
| 82 const HostCache::EntryMap& HostCache::entries() const { | |
| 83 DCHECK(CalledOnValidThread()); | |
| 84 return entries_; | |
| 85 } | 209 } |
| 86 | 210 |
| 87 // static | 211 // static |
| 88 std::unique_ptr<HostCache> HostCache::CreateDefaultCache() { | 212 std::unique_ptr<HostCache> HostCache::CreateDefaultCache() { |
| 89 // Cache capacity is determined by the field trial. | 213 // Cache capacity is determined by the field trial. |
| 90 #if defined(ENABLE_BUILT_IN_DNS) | 214 #if defined(ENABLE_BUILT_IN_DNS) |
| 91 const size_t kDefaultMaxEntries = 1000; | 215 const size_t kDefaultMaxEntries = 1000; |
| 92 #else | 216 #else |
| 93 const size_t kDefaultMaxEntries = 100; | 217 const size_t kDefaultMaxEntries = 100; |
| 94 #endif | 218 #endif |
| 95 const size_t kSaneMaxEntries = 1 << 20; | 219 const size_t kSaneMaxEntries = 1 << 20; |
| 96 size_t max_entries = 0; | 220 size_t max_entries = 0; |
| 97 base::StringToSizeT(base::FieldTrialList::FindFullName("HostCacheSize"), | 221 base::StringToSizeT(base::FieldTrialList::FindFullName("HostCacheSize"), |
| 98 &max_entries); | 222 &max_entries); |
| 99 if ((max_entries == 0) || (max_entries > kSaneMaxEntries)) | 223 if ((max_entries == 0) || (max_entries > kSaneMaxEntries)) |
| 100 max_entries = kDefaultMaxEntries; | 224 max_entries = kDefaultMaxEntries; |
| 101 return base::WrapUnique(new HostCache(max_entries)); | 225 return base::WrapUnique(new HostCache(max_entries)); |
| 102 } | 226 } |
| 103 | 227 |
| 104 void HostCache::EvictionHandler::Handle( | 228 void HostCache::EvictOneEntry(base::TimeTicks now) { |
| 105 const Key& key, | 229 DCHECK_LT(0u, entries_.size()); |
| 106 const Entry& entry, | 230 |
| 107 const base::TimeTicks& expiration, | 231 auto oldest_it = entries_.begin(); |
| 108 const base::TimeTicks& now, | 232 for (auto it = entries_.begin(); it != entries_.end(); ++it) { |
| 109 bool on_get) const { | 233 if (it->second.expires() < oldest_it->second.expires()) |
| 110 if (on_get) { | 234 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 } | 235 } |
| 116 if (expiration > now) { | 236 |
| 117 UMA_HISTOGRAM_CUSTOM_TIMES("DNS.CacheEvicted", expiration - now, | 237 RecordErase(ERASE_EVICT, now, oldest_it->second); |
| 118 base::TimeDelta::FromSeconds(1), base::TimeDelta::FromDays(1), 100); | 238 entries_.erase(oldest_it); |
| 119 } else { | 239 } |
| 120 UMA_HISTOGRAM_CUSTOM_TIMES("DNS.CacheExpired", now - expiration, | 240 |
| 121 base::TimeDelta::FromSeconds(1), base::TimeDelta::FromDays(1), 100); | 241 void HostCache::RecordSet(SetOutcome outcome, |
| 242 base::TimeTicks now, |
| 243 const Entry* old_entry, |
| 244 const Entry& new_entry) { |
| 245 CACHE_HISTOGRAM_ENUM("Set", outcome, MAX_SET_OUTCOME); |
| 246 switch (outcome) { |
| 247 case SET_INSERT: |
| 248 case SET_UPDATE_VALID: |
| 249 // Nothing to log here. |
| 250 break; |
| 251 case SET_UPDATE_STALE: { |
| 252 EntryStaleness stale; |
| 253 old_entry->GetStaleness(now, network_changes_, &stale); |
| 254 CACHE_HISTOGRAM_TIME("UpdateStale.ExpiredBy", stale.expired_by); |
| 255 CACHE_HISTOGRAM_COUNT("UpdateStale.NetworkChanges", |
| 256 stale.network_changes); |
| 257 CACHE_HISTOGRAM_COUNT("UpdateStale.StaleHits", stale.stale_hits); |
| 258 if (old_entry->error() == OK && new_entry.error() == OK) { |
| 259 AddressListDeltaType delta = FindAddressListDeltaType( |
| 260 old_entry->addresses(), new_entry.addresses()); |
| 261 RecordUpdateStale(delta, stale); |
| 262 } |
| 263 break; |
| 264 } |
| 265 case MAX_SET_OUTCOME: |
| 266 NOTREACHED(); |
| 267 break; |
| 122 } | 268 } |
| 123 } | 269 } |
| 124 | 270 |
| 271 void HostCache::RecordUpdateStale(AddressListDeltaType delta, |
| 272 const EntryStaleness& stale) { |
| 273 CACHE_HISTOGRAM_ENUM("UpdateStale.AddressListDelta", delta, MAX_DELTA_TYPE); |
| 274 switch (delta) { |
| 275 case DELTA_IDENTICAL: |
| 276 CACHE_HISTOGRAM_TIME("UpdateStale.ExpiredBy_Identical", stale.expired_by); |
| 277 CACHE_HISTOGRAM_COUNT("UpdateStale.NetworkChanges_Identical", |
| 278 stale.network_changes); |
| 279 break; |
| 280 case DELTA_REORDERED: |
| 281 CACHE_HISTOGRAM_TIME("UpdateStale.ExpiredBy_Reordered", stale.expired_by); |
| 282 CACHE_HISTOGRAM_COUNT("UpdateStale.NetworkChanges_Reordered", |
| 283 stale.network_changes); |
| 284 break; |
| 285 case DELTA_OVERLAP: |
| 286 CACHE_HISTOGRAM_TIME("UpdateStale.ExpiredBy_Overlap", stale.expired_by); |
| 287 CACHE_HISTOGRAM_COUNT("UpdateStale.NetworkChanges_Overlap", |
| 288 stale.network_changes); |
| 289 break; |
| 290 case DELTA_DISJOINT: |
| 291 CACHE_HISTOGRAM_TIME("UpdateStale.ExpiredBy_Disjoint", stale.expired_by); |
| 292 CACHE_HISTOGRAM_COUNT("UpdateStale.NetworkChanges_Dijsoint", |
| 293 stale.network_changes); |
| 294 break; |
| 295 case MAX_DELTA_TYPE: |
| 296 NOTREACHED(); |
| 297 break; |
| 298 } |
| 299 } |
| 300 |
| 301 void HostCache::RecordLookup(LookupOutcome outcome, |
| 302 base::TimeTicks now, |
| 303 const Entry* entry) { |
| 304 CACHE_HISTOGRAM_ENUM("Lookup", outcome, MAX_LOOKUP_OUTCOME); |
| 305 switch (outcome) { |
| 306 case LOOKUP_MISS_ABSENT: |
| 307 case LOOKUP_MISS_STALE: |
| 308 case LOOKUP_HIT_VALID: |
| 309 // Nothing to log here. |
| 310 break; |
| 311 case LOOKUP_HIT_STALE: |
| 312 CACHE_HISTOGRAM_TIME("LookupStale.ExpiredBy", now - entry->expires()); |
| 313 CACHE_HISTOGRAM_COUNT("LookupStale.NetworkChanges", |
| 314 network_changes_ - entry->network_changes()); |
| 315 break; |
| 316 case MAX_LOOKUP_OUTCOME: |
| 317 NOTREACHED(); |
| 318 break; |
| 319 } |
| 320 } |
| 321 |
| 322 void HostCache::RecordErase(EraseReason reason, |
| 323 base::TimeTicks now, |
| 324 const Entry& entry) { |
| 325 HostCache::EntryStaleness stale; |
| 326 entry.GetStaleness(now, network_changes_, &stale); |
| 327 CACHE_HISTOGRAM_ENUM("Erase", reason, MAX_ERASE_REASON); |
| 328 if (stale.is_stale()) { |
| 329 CACHE_HISTOGRAM_TIME("EvictStale.ExpiredBy", stale.expired_by); |
| 330 CACHE_HISTOGRAM_COUNT("EvictStale.NetworkChanges", stale.network_changes); |
| 331 CACHE_HISTOGRAM_COUNT("EvictStale.StaleHits", entry.stale_hits()); |
| 332 } else { |
| 333 CACHE_HISTOGRAM_TIME("EvictValid.ValidFor", -stale.expired_by); |
| 334 } |
| 335 } |
| 336 |
| 337 void HostCache::RecordEraseAll(EraseReason reason, base::TimeTicks now) { |
| 338 for (const auto& it : entries_) |
| 339 RecordErase(reason, now, it.second); |
| 340 } |
| 341 |
| 125 } // namespace net | 342 } // namespace net |
| OLD | NEW |