OLD | NEW |
1 // Copyright (c) 2006-2008 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2006-2008 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 // The eviction policy is a very simple pure LRU, so the elements at the end of | 5 // The eviction policy is a very simple pure LRU, so the elements at the end of |
6 // the list are evicted until kCleanUpMargin free space is available. There is | 6 // the list are evicted until kCleanUpMargin free space is available. There is |
7 // only one list in use (Rankings::NO_USE), and elements are sent to the front | 7 // only one list in use (Rankings::NO_USE), and elements are sent to the front |
8 // of the list whenever they are accessed. | 8 // of the list whenever they are accessed. |
9 | 9 |
10 // The new (in-development) eviction policy ads re-use as a factor to evict | 10 // The new (in-development) eviction policy ads re-use as a factor to evict |
(...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
59 void Eviction::Init(BackendImpl* backend) { | 59 void Eviction::Init(BackendImpl* backend) { |
60 // We grab a bunch of info from the backend to make the code a little cleaner | 60 // We grab a bunch of info from the backend to make the code a little cleaner |
61 // when we're actually doing work. | 61 // when we're actually doing work. |
62 backend_ = backend; | 62 backend_ = backend; |
63 rankings_ = &backend->rankings_; | 63 rankings_ = &backend->rankings_; |
64 header_ = &backend_->data_->header; | 64 header_ = &backend_->data_->header; |
65 max_size_ = LowWaterAdjust(backend_->max_size_); | 65 max_size_ = LowWaterAdjust(backend_->max_size_); |
66 new_eviction_ = backend->new_eviction_; | 66 new_eviction_ = backend->new_eviction_; |
67 first_trim_ = true; | 67 first_trim_ = true; |
68 trimming_ = false; | 68 trimming_ = false; |
| 69 delay_trim_ = false; |
69 } | 70 } |
70 | 71 |
71 void Eviction::TrimCache(bool empty) { | 72 void Eviction::TrimCache(bool empty) { |
72 if (new_eviction_) | 73 if (new_eviction_) |
73 return TrimCacheV2(empty); | 74 return TrimCacheV2(empty); |
74 | 75 |
75 Trace("*** Trim Cache ***"); | 76 Trace("*** Trim Cache ***"); |
76 if (backend_->disabled_ || trimming_) | 77 if (backend_->disabled_ || trimming_) |
77 return; | 78 return; |
78 | 79 |
| 80 if (!empty && backend_->IsLoaded()) |
| 81 return PostDelayedTrim(); |
| 82 |
79 trimming_ = true; | 83 trimming_ = true; |
80 Time start = Time::Now(); | 84 Time start = Time::Now(); |
81 Rankings::ScopedRankingsBlock node(rankings_); | 85 Rankings::ScopedRankingsBlock node(rankings_); |
82 Rankings::ScopedRankingsBlock next(rankings_, | 86 Rankings::ScopedRankingsBlock next(rankings_, |
83 rankings_->GetPrev(node.get(), Rankings::NO_USE)); | 87 rankings_->GetPrev(node.get(), Rankings::NO_USE)); |
84 DCHECK(next.get()); | 88 DCHECK(next.get()); |
85 int target_size = empty ? 0 : max_size_; | 89 int target_size = empty ? 0 : max_size_; |
86 int deleted = 0; | |
87 while (header_->num_bytes > target_size && next.get()) { | 90 while (header_->num_bytes > target_size && next.get()) { |
88 node.reset(next.release()); | 91 node.reset(next.release()); |
89 next.reset(rankings_->GetPrev(node.get(), Rankings::NO_USE)); | 92 next.reset(rankings_->GetPrev(node.get(), Rankings::NO_USE)); |
90 if (!node->Data()->pointer || empty) { | 93 if (!node->Data()->pointer || empty) { |
91 // This entry is not being used by anybody. | 94 // This entry is not being used by anybody. |
92 if (!EvictEntry(node.get(), empty)) | 95 if (!EvictEntry(node.get(), empty)) |
93 continue; | 96 continue; |
94 | 97 |
95 if (!empty) | 98 if (!empty) { |
96 backend_->OnEvent(Stats::TRIM_ENTRY); | 99 backend_->OnEvent(Stats::TRIM_ENTRY); |
97 if (++deleted == 4 && !empty) { | 100 |
98 #if defined(OS_WIN) | 101 if ((Time::Now() - start).InMilliseconds() > 20) { |
99 MessageLoop::current()->PostTask(FROM_HERE, | 102 MessageLoop::current()->PostTask(FROM_HERE, |
100 factory_.NewRunnableMethod(&Eviction::TrimCache, false)); | 103 factory_.NewRunnableMethod(&Eviction::TrimCache, false)); |
101 break; | 104 break; |
102 #endif | 105 } |
103 } | 106 } |
104 } | 107 } |
105 } | 108 } |
106 | 109 |
107 CACHE_UMA(AGE_MS, "TotalTrimTime", backend_->GetSizeGroup(), start); | 110 CACHE_UMA(AGE_MS, "TotalTrimTime", backend_->GetSizeGroup(), start); |
108 trimming_ = false; | 111 trimming_ = false; |
109 Trace("*** Trim Cache end ***"); | 112 Trace("*** Trim Cache end ***"); |
110 return; | 113 return; |
111 } | 114 } |
112 | 115 |
(...skipping 21 matching lines...) Expand all Loading... |
134 return OnDoomEntryV2(entry); | 137 return OnDoomEntryV2(entry); |
135 | 138 |
136 rankings_->Remove(entry->rankings(), GetListForEntry(entry)); | 139 rankings_->Remove(entry->rankings(), GetListForEntry(entry)); |
137 } | 140 } |
138 | 141 |
139 void Eviction::OnDestroyEntry(EntryImpl* entry) { | 142 void Eviction::OnDestroyEntry(EntryImpl* entry) { |
140 if (new_eviction_) | 143 if (new_eviction_) |
141 return OnDestroyEntryV2(entry); | 144 return OnDestroyEntryV2(entry); |
142 } | 145 } |
143 | 146 |
| 147 void Eviction::PostDelayedTrim() { |
| 148 // Prevent posting multiple tasks. |
| 149 if (delay_trim_) |
| 150 return; |
| 151 delay_trim_ = true; |
| 152 MessageLoop::current()->PostDelayedTask(FROM_HERE, |
| 153 factory_.NewRunnableMethod(&Eviction::DelayedTrim), 1000); |
| 154 } |
| 155 |
| 156 void Eviction::DelayedTrim() { |
| 157 delay_trim_ = false; |
| 158 TrimCache(false); |
| 159 } |
| 160 |
144 void Eviction::ReportTrimTimes(EntryImpl* entry) { | 161 void Eviction::ReportTrimTimes(EntryImpl* entry) { |
145 if (first_trim_) { | 162 if (first_trim_) { |
146 first_trim_ = false; | 163 first_trim_ = false; |
147 if (backend_->ShouldReportAgain()) { | 164 if (backend_->ShouldReportAgain()) { |
148 CACHE_UMA(AGE, "TrimAge", 0, entry->GetLastUsed()); | 165 CACHE_UMA(AGE, "TrimAge", 0, entry->GetLastUsed()); |
149 ReportListStats(); | 166 ReportListStats(); |
150 } | 167 } |
151 | 168 |
152 if (header_->lru.filled) | 169 if (header_->lru.filled) |
153 return; | 170 return; |
(...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
204 return true; | 221 return true; |
205 } | 222 } |
206 | 223 |
207 // ----------------------------------------------------------------------- | 224 // ----------------------------------------------------------------------- |
208 | 225 |
209 void Eviction::TrimCacheV2(bool empty) { | 226 void Eviction::TrimCacheV2(bool empty) { |
210 Trace("*** Trim Cache ***"); | 227 Trace("*** Trim Cache ***"); |
211 if (backend_->disabled_ || trimming_) | 228 if (backend_->disabled_ || trimming_) |
212 return; | 229 return; |
213 | 230 |
| 231 if (!empty && backend_->IsLoaded()) |
| 232 return PostDelayedTrim(); |
| 233 |
214 trimming_ = true; | 234 trimming_ = true; |
215 Time start = Time::Now(); | 235 Time start = Time::Now(); |
216 | 236 |
217 const int kListsToSearch = 3; | 237 const int kListsToSearch = 3; |
218 Rankings::ScopedRankingsBlock next[kListsToSearch]; | 238 Rankings::ScopedRankingsBlock next[kListsToSearch]; |
219 int list = Rankings::LAST_ELEMENT; | 239 int list = Rankings::LAST_ELEMENT; |
220 | 240 |
221 // Get a node from each list. | 241 // Get a node from each list. |
222 for (int i = 0; i < kListsToSearch; i++) { | 242 for (int i = 0; i < kListsToSearch; i++) { |
223 bool done = false; | 243 bool done = false; |
(...skipping 17 matching lines...) Expand all Loading... |
241 !NodeIsOldEnough(next[list].get(), 0)) | 261 !NodeIsOldEnough(next[list].get(), 0)) |
242 list = 0; | 262 list = 0; |
243 } | 263 } |
244 | 264 |
245 if (empty) | 265 if (empty) |
246 list = 0; | 266 list = 0; |
247 | 267 |
248 Rankings::ScopedRankingsBlock node(rankings_); | 268 Rankings::ScopedRankingsBlock node(rankings_); |
249 | 269 |
250 int target_size = empty ? 0 : max_size_; | 270 int target_size = empty ? 0 : max_size_; |
251 int deleted = 0; | |
252 for (; list < kListsToSearch; list++) { | 271 for (; list < kListsToSearch; list++) { |
253 while (header_->num_bytes > target_size && next[list].get()) { | 272 while (header_->num_bytes > target_size && next[list].get()) { |
254 node.reset(next[list].release()); | 273 node.reset(next[list].release()); |
255 next[list].reset(rankings_->GetPrev(node.get(), | 274 next[list].reset(rankings_->GetPrev(node.get(), |
256 static_cast<Rankings::List>(list))); | 275 static_cast<Rankings::List>(list))); |
257 if (!node->Data()->pointer || empty) { | 276 if (!node->Data()->pointer || empty) { |
258 // This entry is not being used by anybody. | 277 // This entry is not being used by anybody. |
259 if (!EvictEntry(node.get(), empty)) | 278 if (!EvictEntry(node.get(), empty)) |
260 continue; | 279 continue; |
261 | 280 |
262 if (++deleted == 4 && !empty) { | 281 if (!empty && (Time::Now() - start).InMilliseconds() > 20) { |
263 MessageLoop::current()->PostTask(FROM_HERE, | 282 MessageLoop::current()->PostTask(FROM_HERE, |
264 factory_.NewRunnableMethod(&Eviction::TrimCache, false)); | 283 factory_.NewRunnableMethod(&Eviction::TrimCache, false)); |
265 break; | 284 break; |
266 } | 285 } |
267 } | 286 } |
268 } | 287 } |
269 if (!empty) | 288 if (!empty) |
270 list = kListsToSearch; | 289 list = kListsToSearch; |
271 } | 290 } |
272 | 291 |
(...skipping 181 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
454 Time::FromInternalValue(last2.get()->Data()->last_used)); | 473 Time::FromInternalValue(last2.get()->Data()->last_used)); |
455 if (last3.get()) | 474 if (last3.get()) |
456 CACHE_UMA(AGE, "HighUseAge", header_->experiment, | 475 CACHE_UMA(AGE, "HighUseAge", header_->experiment, |
457 Time::FromInternalValue(last3.get()->Data()->last_used)); | 476 Time::FromInternalValue(last3.get()->Data()->last_used)); |
458 if (last4.get()) | 477 if (last4.get()) |
459 CACHE_UMA(AGE, "DeletedAge", header_->experiment, | 478 CACHE_UMA(AGE, "DeletedAge", header_->experiment, |
460 Time::FromInternalValue(last4.get()->Data()->last_used)); | 479 Time::FromInternalValue(last4.get()->Data()->last_used)); |
461 } | 480 } |
462 | 481 |
463 } // namespace disk_cache | 482 } // namespace disk_cache |
OLD | NEW |