| 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 <utility> | 7 #include <utility> |
| 8 | 8 |
| 9 #include "base/logging.h" | 9 #include "base/logging.h" |
| 10 #include "base/memory/ptr_util.h" | 10 #include "base/memory/ptr_util.h" |
| (...skipping 127 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 138 void HostCache::Entry::GetStaleness(base::TimeTicks now, | 138 void HostCache::Entry::GetStaleness(base::TimeTicks now, |
| 139 int network_changes, | 139 int network_changes, |
| 140 EntryStaleness* out) const { | 140 EntryStaleness* out) const { |
| 141 DCHECK(out); | 141 DCHECK(out); |
| 142 out->expired_by = now - expires_; | 142 out->expired_by = now - expires_; |
| 143 out->network_changes = network_changes - network_changes_; | 143 out->network_changes = network_changes - network_changes_; |
| 144 out->stale_hits = stale_hits_; | 144 out->stale_hits = stale_hits_; |
| 145 } | 145 } |
| 146 | 146 |
| 147 HostCache::HostCache(size_t max_entries) | 147 HostCache::HostCache(size_t max_entries) |
| 148 : max_entries_(max_entries), network_changes_(0) {} | 148 : max_entries_(max_entries), network_changes_(0), delegate_(nullptr) {} |
| 149 | 149 |
| 150 HostCache::~HostCache() { | 150 HostCache::~HostCache() { |
| 151 DCHECK_CALLED_ON_VALID_THREAD(thread_checker_); | 151 DCHECK_CALLED_ON_VALID_THREAD(thread_checker_); |
| 152 RecordEraseAll(ERASE_DESTRUCT, base::TimeTicks::Now()); | 152 RecordEraseAll(ERASE_DESTRUCT, base::TimeTicks::Now()); |
| 153 } | 153 } |
| 154 | 154 |
| 155 const HostCache::Entry* HostCache::Lookup(const Key& key, | 155 const HostCache::Entry* HostCache::Lookup(const Key& key, |
| 156 base::TimeTicks now) { | 156 base::TimeTicks now) { |
| 157 DCHECK_CALLED_ON_VALID_THREAD(thread_checker_); | 157 DCHECK_CALLED_ON_VALID_THREAD(thread_checker_); |
| 158 if (caching_is_disabled()) | 158 if (caching_is_disabled()) |
| (...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 203 | 203 |
| 204 void HostCache::Set(const Key& key, | 204 void HostCache::Set(const Key& key, |
| 205 const Entry& entry, | 205 const Entry& entry, |
| 206 base::TimeTicks now, | 206 base::TimeTicks now, |
| 207 base::TimeDelta ttl) { | 207 base::TimeDelta ttl) { |
| 208 TRACE_EVENT0(kNetTracingCategory, "HostCache::Set"); | 208 TRACE_EVENT0(kNetTracingCategory, "HostCache::Set"); |
| 209 DCHECK_CALLED_ON_VALID_THREAD(thread_checker_); | 209 DCHECK_CALLED_ON_VALID_THREAD(thread_checker_); |
| 210 if (caching_is_disabled()) | 210 if (caching_is_disabled()) |
| 211 return; | 211 return; |
| 212 | 212 |
| 213 bool result_changed = false; |
| 213 auto it = entries_.find(key); | 214 auto it = entries_.find(key); |
| 214 if (it != entries_.end()) { | 215 if (it != entries_.end()) { |
| 215 bool is_stale = it->second.IsStale(now, network_changes_); | 216 bool is_stale = it->second.IsStale(now, network_changes_); |
| 217 AddressListDeltaType delta = |
| 218 FindAddressListDeltaType(it->second.addresses(), entry.addresses()); |
| 216 RecordSet(is_stale ? SET_UPDATE_STALE : SET_UPDATE_VALID, now, &it->second, | 219 RecordSet(is_stale ? SET_UPDATE_STALE : SET_UPDATE_VALID, now, &it->second, |
| 217 entry); | 220 entry, delta); |
| 218 // TODO(juliatuttle): Remember some old metadata (hit count or frequency or | 221 // TODO(juliatuttle): Remember some old metadata (hit count or frequency or |
| 219 // something like that) if it's useful for better eviction algorithms? | 222 // something like that) if it's useful for better eviction algorithms? |
| 223 result_changed = |
| 224 entry.error() == OK && |
| 225 (it->second.error() != entry.error() || delta != DELTA_IDENTICAL); |
| 220 entries_.erase(it); | 226 entries_.erase(it); |
| 221 } else { | 227 } else { |
| 228 result_changed = true; |
| 222 if (size() == max_entries_) | 229 if (size() == max_entries_) |
| 223 EvictOneEntry(now); | 230 EvictOneEntry(now); |
| 224 RecordSet(SET_INSERT, now, nullptr, entry); | 231 RecordSet(SET_INSERT, now, nullptr, entry, DELTA_DISJOINT); |
| 225 } | 232 } |
| 226 | 233 |
| 227 AddEntry(Key(key), Entry(entry, now, ttl, network_changes_)); | 234 AddEntry(Key(key), Entry(entry, now, ttl, network_changes_)); |
| 235 |
| 236 if (delegate_ && result_changed) |
| 237 delegate_->ScheduleWrite(); |
| 228 } | 238 } |
| 229 | 239 |
| 230 void HostCache::AddEntry(const Key& key, const Entry& entry) { | 240 void HostCache::AddEntry(const Key& key, const Entry& entry) { |
| 231 DCHECK_GT(max_entries_, size()); | 241 DCHECK_GT(max_entries_, size()); |
| 232 DCHECK_EQ(0u, entries_.count(key)); | 242 DCHECK_EQ(0u, entries_.count(key)); |
| 233 entries_.insert(std::make_pair(key, entry)); | 243 entries_.insert(std::make_pair(key, entry)); |
| 234 DCHECK_GE(max_entries_, size()); | 244 DCHECK_GE(max_entries_, size()); |
| 235 } | 245 } |
| 236 | 246 |
| 237 void HostCache::OnNetworkChange() { | 247 void HostCache::OnNetworkChange() { |
| 238 ++network_changes_; | 248 ++network_changes_; |
| 239 } | 249 } |
| 240 | 250 |
| 241 void HostCache::clear() { | 251 void HostCache::clear() { |
| 242 DCHECK_CALLED_ON_VALID_THREAD(thread_checker_); | 252 DCHECK_CALLED_ON_VALID_THREAD(thread_checker_); |
| 243 RecordEraseAll(ERASE_CLEAR, base::TimeTicks::Now()); | 253 RecordEraseAll(ERASE_CLEAR, base::TimeTicks::Now()); |
| 254 |
| 255 // Don't bother scheduling a write if there's nothing to clear. |
| 256 if (size() == 0) |
| 257 return; |
| 258 |
| 244 entries_.clear(); | 259 entries_.clear(); |
| 260 if (delegate_) |
| 261 delegate_->ScheduleWrite(); |
| 245 } | 262 } |
| 246 | 263 |
| 247 void HostCache::ClearForHosts( | 264 void HostCache::ClearForHosts( |
| 248 const base::Callback<bool(const std::string&)>& host_filter) { | 265 const base::Callback<bool(const std::string&)>& host_filter) { |
| 249 DCHECK_CALLED_ON_VALID_THREAD(thread_checker_); | 266 DCHECK_CALLED_ON_VALID_THREAD(thread_checker_); |
| 250 | 267 |
| 251 if (host_filter.is_null()) { | 268 if (host_filter.is_null()) { |
| 252 clear(); | 269 clear(); |
| 253 return; | 270 return; |
| 254 } | 271 } |
| 255 | 272 |
| 273 bool changed = false; |
| 256 base::TimeTicks now = base::TimeTicks::Now(); | 274 base::TimeTicks now = base::TimeTicks::Now(); |
| 257 for (EntryMap::iterator it = entries_.begin(); it != entries_.end();) { | 275 for (EntryMap::iterator it = entries_.begin(); it != entries_.end();) { |
| 258 EntryMap::iterator next_it = std::next(it); | 276 EntryMap::iterator next_it = std::next(it); |
| 259 | 277 |
| 260 if (host_filter.Run(it->first.hostname)) { | 278 if (host_filter.Run(it->first.hostname)) { |
| 261 RecordErase(ERASE_CLEAR, now, it->second); | 279 RecordErase(ERASE_CLEAR, now, it->second); |
| 262 entries_.erase(it); | 280 entries_.erase(it); |
| 281 changed = true; |
| 263 } | 282 } |
| 264 | 283 |
| 265 it = next_it; | 284 it = next_it; |
| 266 } | 285 } |
| 286 |
| 287 if (delegate_ && changed) |
| 288 delegate_->ScheduleWrite(); |
| 267 } | 289 } |
| 268 | 290 |
| 269 std::unique_ptr<base::ListValue> HostCache::GetAsListValue( | 291 std::unique_ptr<base::ListValue> HostCache::GetAsListValue( |
| 270 bool include_staleness) const { | 292 bool include_staleness) const { |
| 271 std::unique_ptr<base::ListValue> entry_list(new base::ListValue()); | 293 std::unique_ptr<base::ListValue> entry_list(new base::ListValue()); |
| 272 | 294 |
| 273 for (const auto& pair : entries_) { | 295 for (const auto& pair : entries_) { |
| 274 const Key& key = pair.first; | 296 const Key& key = pair.first; |
| 275 const Entry& entry = pair.second; | 297 const Entry& entry = pair.second; |
| 276 | 298 |
| (...skipping 133 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 410 | 432 |
| 411 if (!eviction_callback_.is_null()) | 433 if (!eviction_callback_.is_null()) |
| 412 eviction_callback_.Run(oldest_it->first, oldest_it->second); | 434 eviction_callback_.Run(oldest_it->first, oldest_it->second); |
| 413 RecordErase(ERASE_EVICT, now, oldest_it->second); | 435 RecordErase(ERASE_EVICT, now, oldest_it->second); |
| 414 entries_.erase(oldest_it); | 436 entries_.erase(oldest_it); |
| 415 } | 437 } |
| 416 | 438 |
| 417 void HostCache::RecordSet(SetOutcome outcome, | 439 void HostCache::RecordSet(SetOutcome outcome, |
| 418 base::TimeTicks now, | 440 base::TimeTicks now, |
| 419 const Entry* old_entry, | 441 const Entry* old_entry, |
| 420 const Entry& new_entry) { | 442 const Entry& new_entry, |
| 443 AddressListDeltaType delta) { |
| 421 CACHE_HISTOGRAM_ENUM("Set", outcome, MAX_SET_OUTCOME); | 444 CACHE_HISTOGRAM_ENUM("Set", outcome, MAX_SET_OUTCOME); |
| 422 switch (outcome) { | 445 switch (outcome) { |
| 423 case SET_INSERT: | 446 case SET_INSERT: |
| 424 case SET_UPDATE_VALID: | 447 case SET_UPDATE_VALID: |
| 425 // Nothing to log here. | 448 // Nothing to log here. |
| 426 break; | 449 break; |
| 427 case SET_UPDATE_STALE: { | 450 case SET_UPDATE_STALE: { |
| 428 EntryStaleness stale; | 451 EntryStaleness stale; |
| 429 old_entry->GetStaleness(now, network_changes_, &stale); | 452 old_entry->GetStaleness(now, network_changes_, &stale); |
| 430 CACHE_HISTOGRAM_TIME("UpdateStale.ExpiredBy", stale.expired_by); | 453 CACHE_HISTOGRAM_TIME("UpdateStale.ExpiredBy", stale.expired_by); |
| 431 CACHE_HISTOGRAM_COUNT("UpdateStale.NetworkChanges", | 454 CACHE_HISTOGRAM_COUNT("UpdateStale.NetworkChanges", |
| 432 stale.network_changes); | 455 stale.network_changes); |
| 433 CACHE_HISTOGRAM_COUNT("UpdateStale.StaleHits", stale.stale_hits); | 456 CACHE_HISTOGRAM_COUNT("UpdateStale.StaleHits", stale.stale_hits); |
| 434 if (old_entry->error() == OK && new_entry.error() == OK) { | 457 if (old_entry->error() == OK && new_entry.error() == OK) { |
| 435 AddressListDeltaType delta = FindAddressListDeltaType( | |
| 436 old_entry->addresses(), new_entry.addresses()); | |
| 437 RecordUpdateStale(delta, stale); | 458 RecordUpdateStale(delta, stale); |
| 438 } | 459 } |
| 439 break; | 460 break; |
| 440 } | 461 } |
| 441 case MAX_SET_OUTCOME: | 462 case MAX_SET_OUTCOME: |
| 442 NOTREACHED(); | 463 NOTREACHED(); |
| 443 break; | 464 break; |
| 444 } | 465 } |
| 445 } | 466 } |
| 446 | 467 |
| (...skipping 62 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 509 CACHE_HISTOGRAM_TIME("EraseValid.ValidFor", -stale.expired_by); | 530 CACHE_HISTOGRAM_TIME("EraseValid.ValidFor", -stale.expired_by); |
| 510 } | 531 } |
| 511 } | 532 } |
| 512 | 533 |
| 513 void HostCache::RecordEraseAll(EraseReason reason, base::TimeTicks now) { | 534 void HostCache::RecordEraseAll(EraseReason reason, base::TimeTicks now) { |
| 514 for (const auto& it : entries_) | 535 for (const auto& it : entries_) |
| 515 RecordErase(reason, now, it.second); | 536 RecordErase(reason, now, it.second); |
| 516 } | 537 } |
| 517 | 538 |
| 518 } // namespace net | 539 } // namespace net |
| OLD | NEW |