| 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/metrics/field_trial.h" | 8 #include "base/metrics/field_trial.h" |
| 9 #include "base/metrics/histogram.h" | 9 #include "base/metrics/histogram.h" |
| 10 #include "base/strings/string_number_conversions.h" | 10 #include "base/strings/string_number_conversions.h" |
| 11 #include "net/base/net_errors.h" | 11 #include "net/base/net_errors.h" |
| 12 | 12 |
| 13 namespace net { | 13 namespace net { |
| 14 | 14 |
| 15 //----------------------------------------------------------------------------- | 15 //----------------------------------------------------------------------------- |
| 16 | 16 |
| 17 HostCache::Entry::Entry(int error, const AddressList& addrlist, | 17 HostCache::Entry::Entry(int error, |
| 18 const AddressList& addrlist, |
| 18 base::TimeDelta ttl) | 19 base::TimeDelta ttl) |
| 19 : error(error), | 20 : error(error), addrlist(addrlist), ttl(ttl) { |
| 20 addrlist(addrlist), | |
| 21 ttl(ttl) { | |
| 22 DCHECK(ttl >= base::TimeDelta()); | 21 DCHECK(ttl >= base::TimeDelta()); |
| 23 } | 22 } |
| 24 | 23 |
| 25 HostCache::Entry::Entry(int error, const AddressList& addrlist) | 24 HostCache::Entry::Entry(int error, const AddressList& addrlist) |
| 26 : error(error), | 25 : error(error), addrlist(addrlist), ttl(base::TimeDelta::FromSeconds(-1)) { |
| 27 addrlist(addrlist), | |
| 28 ttl(base::TimeDelta::FromSeconds(-1)) { | |
| 29 } | 26 } |
| 30 | 27 |
| 31 HostCache::Entry::~Entry() { | 28 HostCache::Entry::~Entry() { |
| 32 } | 29 } |
| 33 | 30 |
| 34 //----------------------------------------------------------------------------- | 31 //----------------------------------------------------------------------------- |
| 35 | 32 |
| 36 HostCache::HostCache(size_t max_entries) | 33 HostCache::HostCache(size_t max_entries) : entries_(max_entries) { |
| 37 : entries_(max_entries) { | |
| 38 } | 34 } |
| 39 | 35 |
| 40 HostCache::~HostCache() { | 36 HostCache::~HostCache() { |
| 41 } | 37 } |
| 42 | 38 |
| 43 const HostCache::Entry* HostCache::Lookup(const Key& key, | 39 const HostCache::Entry* HostCache::Lookup(const Key& key, base::TimeTicks now) { |
| 44 base::TimeTicks now) { | |
| 45 DCHECK(CalledOnValidThread()); | 40 DCHECK(CalledOnValidThread()); |
| 46 if (caching_is_disabled()) | 41 if (caching_is_disabled()) |
| 47 return NULL; | 42 return NULL; |
| 48 | 43 |
| 49 return entries_.Get(key, now); | 44 return entries_.Get(key, now); |
| 50 } | 45 } |
| 51 | 46 |
| 52 void HostCache::Set(const Key& key, | 47 void HostCache::Set(const Key& key, |
| 53 const Entry& entry, | 48 const Entry& entry, |
| 54 base::TimeTicks now, | 49 base::TimeTicks now, |
| (...skipping 21 matching lines...) Expand all Loading... |
| 76 } | 71 } |
| 77 | 72 |
| 78 // Note that this map may contain expired entries. | 73 // Note that this map may contain expired entries. |
| 79 const HostCache::EntryMap& HostCache::entries() const { | 74 const HostCache::EntryMap& HostCache::entries() const { |
| 80 DCHECK(CalledOnValidThread()); | 75 DCHECK(CalledOnValidThread()); |
| 81 return entries_; | 76 return entries_; |
| 82 } | 77 } |
| 83 | 78 |
| 84 // static | 79 // static |
| 85 scoped_ptr<HostCache> HostCache::CreateDefaultCache() { | 80 scoped_ptr<HostCache> HostCache::CreateDefaultCache() { |
| 86 // Cache capacity is determined by the field trial. | 81 // Cache capacity is determined by the field trial. |
| 87 #if defined(ENABLE_BUILT_IN_DNS) | 82 #if defined(ENABLE_BUILT_IN_DNS) |
| 88 const size_t kDefaultMaxEntries = 1000; | 83 const size_t kDefaultMaxEntries = 1000; |
| 89 #else | 84 #else |
| 90 const size_t kDefaultMaxEntries = 100; | 85 const size_t kDefaultMaxEntries = 100; |
| 91 #endif | 86 #endif |
| 92 const size_t kSaneMaxEntries = 1 << 20; | 87 const size_t kSaneMaxEntries = 1 << 20; |
| 93 size_t max_entries = 0; | 88 size_t max_entries = 0; |
| 94 base::StringToSizeT(base::FieldTrialList::FindFullName("HostCacheSize"), | 89 base::StringToSizeT(base::FieldTrialList::FindFullName("HostCacheSize"), |
| 95 &max_entries); | 90 &max_entries); |
| 96 if ((max_entries == 0) || (max_entries > kSaneMaxEntries)) | 91 if ((max_entries == 0) || (max_entries > kSaneMaxEntries)) |
| 97 max_entries = kDefaultMaxEntries; | 92 max_entries = kDefaultMaxEntries; |
| 98 return make_scoped_ptr(new HostCache(max_entries)); | 93 return make_scoped_ptr(new HostCache(max_entries)); |
| 99 } | 94 } |
| 100 | 95 |
| 101 void HostCache::EvictionHandler::Handle( | 96 void HostCache::EvictionHandler::Handle(const Key& key, |
| 102 const Key& key, | 97 const Entry& entry, |
| 103 const Entry& entry, | 98 const base::TimeTicks& expiration, |
| 104 const base::TimeTicks& expiration, | 99 const base::TimeTicks& now, |
| 105 const base::TimeTicks& now, | 100 bool on_get) const { |
| 106 bool on_get) const { | |
| 107 if (on_get) { | 101 if (on_get) { |
| 108 DCHECK(now >= expiration); | 102 DCHECK(now >= expiration); |
| 109 UMA_HISTOGRAM_CUSTOM_TIMES("DNS.CacheExpiredOnGet", now - expiration, | 103 UMA_HISTOGRAM_CUSTOM_TIMES("DNS.CacheExpiredOnGet", |
| 110 base::TimeDelta::FromSeconds(1), base::TimeDelta::FromDays(1), 100); | 104 now - expiration, |
| 105 base::TimeDelta::FromSeconds(1), |
| 106 base::TimeDelta::FromDays(1), |
| 107 100); |
| 111 return; | 108 return; |
| 112 } | 109 } |
| 113 if (expiration > now) { | 110 if (expiration > now) { |
| 114 UMA_HISTOGRAM_CUSTOM_TIMES("DNS.CacheEvicted", expiration - now, | 111 UMA_HISTOGRAM_CUSTOM_TIMES("DNS.CacheEvicted", |
| 115 base::TimeDelta::FromSeconds(1), base::TimeDelta::FromDays(1), 100); | 112 expiration - now, |
| 113 base::TimeDelta::FromSeconds(1), |
| 114 base::TimeDelta::FromDays(1), |
| 115 100); |
| 116 } else { | 116 } else { |
| 117 UMA_HISTOGRAM_CUSTOM_TIMES("DNS.CacheExpired", now - expiration, | 117 UMA_HISTOGRAM_CUSTOM_TIMES("DNS.CacheExpired", |
| 118 base::TimeDelta::FromSeconds(1), base::TimeDelta::FromDays(1), 100); | 118 now - expiration, |
| 119 base::TimeDelta::FromSeconds(1), |
| 120 base::TimeDelta::FromDays(1), |
| 121 100); |
| 119 } | 122 } |
| 120 } | 123 } |
| 121 | 124 |
| 122 } // namespace net | 125 } // namespace net |
| OLD | NEW |