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; | |
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; | |
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()); |
244 entries_.clear(); | 253 entries_.clear(); |
254 if (delegate_) | |
Julia Tuttle
2017/06/16 17:04:54
Maybe actually check if the cache is already empty
mgersh
2017/06/16 17:32:49
Done.
| |
255 delegate_->ScheduleWrite(); | |
245 } | 256 } |
246 | 257 |
247 void HostCache::ClearForHosts( | 258 void HostCache::ClearForHosts( |
248 const base::Callback<bool(const std::string&)>& host_filter) { | 259 const base::Callback<bool(const std::string&)>& host_filter) { |
249 DCHECK_CALLED_ON_VALID_THREAD(thread_checker_); | 260 DCHECK_CALLED_ON_VALID_THREAD(thread_checker_); |
250 | 261 |
251 if (host_filter.is_null()) { | 262 if (host_filter.is_null()) { |
252 clear(); | 263 clear(); |
253 return; | 264 return; |
254 } | 265 } |
255 | 266 |
256 base::TimeTicks now = base::TimeTicks::Now(); | 267 base::TimeTicks now = base::TimeTicks::Now(); |
257 for (EntryMap::iterator it = entries_.begin(); it != entries_.end();) { | 268 for (EntryMap::iterator it = entries_.begin(); it != entries_.end();) { |
258 EntryMap::iterator next_it = std::next(it); | 269 EntryMap::iterator next_it = std::next(it); |
259 | 270 |
260 if (host_filter.Run(it->first.hostname)) { | 271 if (host_filter.Run(it->first.hostname)) { |
261 RecordErase(ERASE_CLEAR, now, it->second); | 272 RecordErase(ERASE_CLEAR, now, it->second); |
262 entries_.erase(it); | 273 entries_.erase(it); |
263 } | 274 } |
264 | 275 |
265 it = next_it; | 276 it = next_it; |
266 } | 277 } |
278 | |
279 if (delegate_) | |
Julia Tuttle
2017/06/16 17:04:54
Ditto; see if we actually remove any entries.
mgersh
2017/06/16 17:32:49
Done.
| |
280 delegate_->ScheduleWrite(); | |
267 } | 281 } |
268 | 282 |
269 std::unique_ptr<base::ListValue> HostCache::GetAsListValue( | 283 std::unique_ptr<base::ListValue> HostCache::GetAsListValue( |
270 bool include_staleness) const { | 284 bool include_staleness) const { |
271 std::unique_ptr<base::ListValue> entry_list(new base::ListValue()); | 285 std::unique_ptr<base::ListValue> entry_list(new base::ListValue()); |
272 | 286 |
273 for (const auto& pair : entries_) { | 287 for (const auto& pair : entries_) { |
274 const Key& key = pair.first; | 288 const Key& key = pair.first; |
275 const Entry& entry = pair.second; | 289 const Entry& entry = pair.second; |
276 | 290 |
(...skipping 134 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
411 | 425 |
412 if (!eviction_callback_.is_null()) | 426 if (!eviction_callback_.is_null()) |
413 eviction_callback_.Run(oldest_it->first, oldest_it->second); | 427 eviction_callback_.Run(oldest_it->first, oldest_it->second); |
414 RecordErase(ERASE_EVICT, now, oldest_it->second); | 428 RecordErase(ERASE_EVICT, now, oldest_it->second); |
415 entries_.erase(oldest_it); | 429 entries_.erase(oldest_it); |
416 } | 430 } |
417 | 431 |
418 void HostCache::RecordSet(SetOutcome outcome, | 432 void HostCache::RecordSet(SetOutcome outcome, |
419 base::TimeTicks now, | 433 base::TimeTicks now, |
420 const Entry* old_entry, | 434 const Entry* old_entry, |
421 const Entry& new_entry) { | 435 const Entry& new_entry, |
436 AddressListDeltaType delta) { | |
422 CACHE_HISTOGRAM_ENUM("Set", outcome, MAX_SET_OUTCOME); | 437 CACHE_HISTOGRAM_ENUM("Set", outcome, MAX_SET_OUTCOME); |
423 switch (outcome) { | 438 switch (outcome) { |
424 case SET_INSERT: | 439 case SET_INSERT: |
425 case SET_UPDATE_VALID: | 440 case SET_UPDATE_VALID: |
426 // Nothing to log here. | 441 // Nothing to log here. |
427 break; | 442 break; |
428 case SET_UPDATE_STALE: { | 443 case SET_UPDATE_STALE: { |
429 EntryStaleness stale; | 444 EntryStaleness stale; |
430 old_entry->GetStaleness(now, network_changes_, &stale); | 445 old_entry->GetStaleness(now, network_changes_, &stale); |
431 CACHE_HISTOGRAM_TIME("UpdateStale.ExpiredBy", stale.expired_by); | 446 CACHE_HISTOGRAM_TIME("UpdateStale.ExpiredBy", stale.expired_by); |
432 CACHE_HISTOGRAM_COUNT("UpdateStale.NetworkChanges", | 447 CACHE_HISTOGRAM_COUNT("UpdateStale.NetworkChanges", |
433 stale.network_changes); | 448 stale.network_changes); |
434 CACHE_HISTOGRAM_COUNT("UpdateStale.StaleHits", stale.stale_hits); | 449 CACHE_HISTOGRAM_COUNT("UpdateStale.StaleHits", stale.stale_hits); |
435 if (old_entry->error() == OK && new_entry.error() == OK) { | 450 if (old_entry->error() == OK && new_entry.error() == OK) { |
436 AddressListDeltaType delta = FindAddressListDeltaType( | |
437 old_entry->addresses(), new_entry.addresses()); | |
438 RecordUpdateStale(delta, stale); | 451 RecordUpdateStale(delta, stale); |
439 } | 452 } |
440 break; | 453 break; |
441 } | 454 } |
442 case MAX_SET_OUTCOME: | 455 case MAX_SET_OUTCOME: |
443 NOTREACHED(); | 456 NOTREACHED(); |
444 break; | 457 break; |
445 } | 458 } |
446 } | 459 } |
447 | 460 |
(...skipping 62 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
510 CACHE_HISTOGRAM_TIME("EraseValid.ValidFor", -stale.expired_by); | 523 CACHE_HISTOGRAM_TIME("EraseValid.ValidFor", -stale.expired_by); |
511 } | 524 } |
512 } | 525 } |
513 | 526 |
514 void HostCache::RecordEraseAll(EraseReason reason, base::TimeTicks now) { | 527 void HostCache::RecordEraseAll(EraseReason reason, base::TimeTicks now) { |
515 for (const auto& it : entries_) | 528 for (const auto& it : entries_) |
516 RecordErase(reason, now, it.second); | 529 RecordErase(reason, now, it.second); |
517 } | 530 } |
518 | 531 |
519 } // namespace net | 532 } // namespace net |
OLD | NEW |