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 <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; | |
|
mef
2017/06/20 16:18:12
nit: Maybe initialize to false?
nit: Maybe name it
mgersh
2017/06/20 17:16:29
Done.
| |
| 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 it->second.error() != entry.error() || delta != DELTA_IDENTICAL; | |
|
mef
2017/06/20 16:18:12
Would we want to schedule write if new entry.error
mgersh
2017/06/20 17:16:29
I could go either way on this, but I guess the wri
| |
| 220 entries_.erase(it); | 225 entries_.erase(it); |
| 221 } else { | 226 } else { |
| 227 result_changed = true; | |
| 222 if (size() == max_entries_) | 228 if (size() == max_entries_) |
| 223 EvictOneEntry(now); | 229 EvictOneEntry(now); |
| 224 RecordSet(SET_INSERT, now, nullptr, entry); | 230 RecordSet(SET_INSERT, now, nullptr, entry, DELTA_DISJOINT); |
| 225 } | 231 } |
| 226 | 232 |
| 227 AddEntry(Key(key), Entry(entry, now, ttl, network_changes_)); | 233 AddEntry(Key(key), Entry(entry, now, ttl, network_changes_)); |
| 234 | |
| 235 if (delegate_ && result_changed) | |
| 236 delegate_->ScheduleWrite(); | |
| 228 } | 237 } |
| 229 | 238 |
| 230 void HostCache::AddEntry(const Key& key, const Entry& entry) { | 239 void HostCache::AddEntry(const Key& key, const Entry& entry) { |
| 231 DCHECK_GT(max_entries_, size()); | 240 DCHECK_GT(max_entries_, size()); |
| 232 DCHECK_EQ(0u, entries_.count(key)); | 241 DCHECK_EQ(0u, entries_.count(key)); |
| 233 entries_.insert(std::make_pair(key, entry)); | 242 entries_.insert(std::make_pair(key, entry)); |
| 234 DCHECK_GE(max_entries_, size()); | 243 DCHECK_GE(max_entries_, size()); |
| 235 } | 244 } |
| 236 | 245 |
| 237 void HostCache::OnNetworkChange() { | 246 void HostCache::OnNetworkChange() { |
| 238 ++network_changes_; | 247 ++network_changes_; |
| 239 } | 248 } |
| 240 | 249 |
| 241 void HostCache::clear() { | 250 void HostCache::clear() { |
| 242 DCHECK_CALLED_ON_VALID_THREAD(thread_checker_); | 251 DCHECK_CALLED_ON_VALID_THREAD(thread_checker_); |
| 243 RecordEraseAll(ERASE_CLEAR, base::TimeTicks::Now()); | 252 RecordEraseAll(ERASE_CLEAR, base::TimeTicks::Now()); |
| 253 | |
| 254 // Don't bother scheduling a write if there's nothing to clear. | |
| 255 if (size() == 0) | |
| 256 return; | |
| 257 | |
| 244 entries_.clear(); | 258 entries_.clear(); |
| 259 if (delegate_) | |
| 260 delegate_->ScheduleWrite(); | |
| 245 } | 261 } |
| 246 | 262 |
| 247 void HostCache::ClearForHosts( | 263 void HostCache::ClearForHosts( |
| 248 const base::Callback<bool(const std::string&)>& host_filter) { | 264 const base::Callback<bool(const std::string&)>& host_filter) { |
| 249 DCHECK_CALLED_ON_VALID_THREAD(thread_checker_); | 265 DCHECK_CALLED_ON_VALID_THREAD(thread_checker_); |
| 250 | 266 |
| 251 if (host_filter.is_null()) { | 267 if (host_filter.is_null()) { |
| 252 clear(); | 268 clear(); |
| 253 return; | 269 return; |
| 254 } | 270 } |
| 255 | 271 |
| 272 bool changed = false; | |
| 256 base::TimeTicks now = base::TimeTicks::Now(); | 273 base::TimeTicks now = base::TimeTicks::Now(); |
| 257 for (EntryMap::iterator it = entries_.begin(); it != entries_.end();) { | 274 for (EntryMap::iterator it = entries_.begin(); it != entries_.end();) { |
| 258 EntryMap::iterator next_it = std::next(it); | 275 EntryMap::iterator next_it = std::next(it); |
| 259 | 276 |
| 260 if (host_filter.Run(it->first.hostname)) { | 277 if (host_filter.Run(it->first.hostname)) { |
| 261 RecordErase(ERASE_CLEAR, now, it->second); | 278 RecordErase(ERASE_CLEAR, now, it->second); |
| 262 entries_.erase(it); | 279 entries_.erase(it); |
| 280 changed = true; | |
| 263 } | 281 } |
| 264 | 282 |
| 265 it = next_it; | 283 it = next_it; |
| 266 } | 284 } |
| 285 | |
| 286 if (delegate_ && changed) | |
| 287 delegate_->ScheduleWrite(); | |
| 267 } | 288 } |
| 268 | 289 |
| 269 std::unique_ptr<base::ListValue> HostCache::GetAsListValue( | 290 std::unique_ptr<base::ListValue> HostCache::GetAsListValue( |
| 270 bool include_staleness) const { | 291 bool include_staleness) const { |
| 271 std::unique_ptr<base::ListValue> entry_list(new base::ListValue()); | 292 std::unique_ptr<base::ListValue> entry_list(new base::ListValue()); |
| 272 | 293 |
| 273 for (const auto& pair : entries_) { | 294 for (const auto& pair : entries_) { |
| 274 const Key& key = pair.first; | 295 const Key& key = pair.first; |
| 275 const Entry& entry = pair.second; | 296 const Entry& entry = pair.second; |
| 276 | 297 |
| (...skipping 134 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 411 | 432 |
| 412 if (!eviction_callback_.is_null()) | 433 if (!eviction_callback_.is_null()) |
| 413 eviction_callback_.Run(oldest_it->first, oldest_it->second); | 434 eviction_callback_.Run(oldest_it->first, oldest_it->second); |
| 414 RecordErase(ERASE_EVICT, now, oldest_it->second); | 435 RecordErase(ERASE_EVICT, now, oldest_it->second); |
| 415 entries_.erase(oldest_it); | 436 entries_.erase(oldest_it); |
| 416 } | 437 } |
| 417 | 438 |
| 418 void HostCache::RecordSet(SetOutcome outcome, | 439 void HostCache::RecordSet(SetOutcome outcome, |
| 419 base::TimeTicks now, | 440 base::TimeTicks now, |
| 420 const Entry* old_entry, | 441 const Entry* old_entry, |
| 421 const Entry& new_entry) { | 442 const Entry& new_entry, |
| 443 AddressListDeltaType delta) { | |
| 422 CACHE_HISTOGRAM_ENUM("Set", outcome, MAX_SET_OUTCOME); | 444 CACHE_HISTOGRAM_ENUM("Set", outcome, MAX_SET_OUTCOME); |
| 423 switch (outcome) { | 445 switch (outcome) { |
| 424 case SET_INSERT: | 446 case SET_INSERT: |
| 425 case SET_UPDATE_VALID: | 447 case SET_UPDATE_VALID: |
| 426 // Nothing to log here. | 448 // Nothing to log here. |
| 427 break; | 449 break; |
| 428 case SET_UPDATE_STALE: { | 450 case SET_UPDATE_STALE: { |
| 429 EntryStaleness stale; | 451 EntryStaleness stale; |
| 430 old_entry->GetStaleness(now, network_changes_, &stale); | 452 old_entry->GetStaleness(now, network_changes_, &stale); |
| 431 CACHE_HISTOGRAM_TIME("UpdateStale.ExpiredBy", stale.expired_by); | 453 CACHE_HISTOGRAM_TIME("UpdateStale.ExpiredBy", stale.expired_by); |
| 432 CACHE_HISTOGRAM_COUNT("UpdateStale.NetworkChanges", | 454 CACHE_HISTOGRAM_COUNT("UpdateStale.NetworkChanges", |
| 433 stale.network_changes); | 455 stale.network_changes); |
| 434 CACHE_HISTOGRAM_COUNT("UpdateStale.StaleHits", stale.stale_hits); | 456 CACHE_HISTOGRAM_COUNT("UpdateStale.StaleHits", stale.stale_hits); |
| 435 if (old_entry->error() == OK && new_entry.error() == OK) { | 457 if (old_entry->error() == OK && new_entry.error() == OK) { |
| 436 AddressListDeltaType delta = FindAddressListDeltaType( | |
| 437 old_entry->addresses(), new_entry.addresses()); | |
| 438 RecordUpdateStale(delta, stale); | 458 RecordUpdateStale(delta, stale); |
| 439 } | 459 } |
| 440 break; | 460 break; |
| 441 } | 461 } |
| 442 case MAX_SET_OUTCOME: | 462 case MAX_SET_OUTCOME: |
| 443 NOTREACHED(); | 463 NOTREACHED(); |
| 444 break; | 464 break; |
| 445 } | 465 } |
| 446 } | 466 } |
| 447 | 467 |
| (...skipping 62 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 510 CACHE_HISTOGRAM_TIME("EraseValid.ValidFor", -stale.expired_by); | 530 CACHE_HISTOGRAM_TIME("EraseValid.ValidFor", -stale.expired_by); |
| 511 } | 531 } |
| 512 } | 532 } |
| 513 | 533 |
| 514 void HostCache::RecordEraseAll(EraseReason reason, base::TimeTicks now) { | 534 void HostCache::RecordEraseAll(EraseReason reason, base::TimeTicks now) { |
| 515 for (const auto& it : entries_) | 535 for (const auto& it : entries_) |
| 516 RecordErase(reason, now, it.second); | 536 RecordErase(reason, now, it.second); |
| 517 } | 537 } |
| 518 | 538 |
| 519 } // namespace net | 539 } // namespace net |
| OLD | NEW |