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 // See net/disk_cache/disk_cache.h for the public interface of the cache. | 5 // See net/disk_cache/disk_cache.h for the public interface of the cache. |
6 | 6 |
7 #ifndef NET_DISK_CACHE_BACKEND_IMPL_H_ | 7 #ifndef NET_DISK_CACHE_BACKEND_IMPL_H_ |
8 #define NET_DISK_CACHE_BACKEND_IMPL_H_ | 8 #define NET_DISK_CACHE_BACKEND_IMPL_H_ |
9 | 9 |
10 #include "base/files/file_path.h" | 10 #include "base/files/file_path.h" |
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
45 BackendImpl(const base::FilePath& path, base::MessageLoopProxy* cache_thread, | 45 BackendImpl(const base::FilePath& path, base::MessageLoopProxy* cache_thread, |
46 net::NetLog* net_log); | 46 net::NetLog* net_log); |
47 // mask can be used to limit the usable size of the hash table, for testing. | 47 // mask can be used to limit the usable size of the hash table, for testing. |
48 BackendImpl(const base::FilePath& path, uint32 mask, | 48 BackendImpl(const base::FilePath& path, uint32 mask, |
49 base::MessageLoopProxy* cache_thread, net::NetLog* net_log); | 49 base::MessageLoopProxy* cache_thread, net::NetLog* net_log); |
50 virtual ~BackendImpl(); | 50 virtual ~BackendImpl(); |
51 | 51 |
52 // Performs general initialization for this current instance of the cache. | 52 // Performs general initialization for this current instance of the cache. |
53 int Init(const CompletionCallback& callback); | 53 int Init(const CompletionCallback& callback); |
54 | 54 |
55 // Performs the actual initialization and final cleanup on destruction. | |
56 int SyncInit(); | |
57 void CleanupCache(); | |
58 | |
59 // Same behavior as OpenNextEntry but walks the list from back to front. | 55 // Same behavior as OpenNextEntry but walks the list from back to front. |
60 int OpenPrevEntry(void** iter, Entry** prev_entry, | 56 int OpenPrevEntry(void** iter, Entry** prev_entry, |
61 const CompletionCallback& callback); | 57 const CompletionCallback& callback); |
62 | 58 |
63 // Synchronous implementation of the asynchronous interface. | |
64 int SyncOpenEntry(const std::string& key, Entry** entry); | |
65 int SyncCreateEntry(const std::string& key, Entry** entry); | |
66 int SyncDoomEntry(const std::string& key); | |
67 int SyncDoomAllEntries(); | |
68 int SyncDoomEntriesBetween(base::Time initial_time, | |
69 base::Time end_time); | |
70 int SyncDoomEntriesSince(base::Time initial_time); | |
71 int SyncOpenNextEntry(void** iter, Entry** next_entry); | |
72 int SyncOpenPrevEntry(void** iter, Entry** prev_entry); | |
73 void SyncEndEnumeration(void* iter); | |
74 void SyncOnExternalCacheHit(const std::string& key); | |
75 | |
76 // Open or create an entry for the given |key| or |iter|. | |
77 EntryImpl* OpenEntryImpl(const std::string& key); | |
78 EntryImpl* CreateEntryImpl(const std::string& key); | |
79 EntryImpl* OpenNextEntryImpl(void** iter); | |
80 EntryImpl* OpenPrevEntryImpl(void** iter); | |
81 | |
82 // Sets the maximum size for the total amount of data stored by this instance. | 59 // Sets the maximum size for the total amount of data stored by this instance. |
83 bool SetMaxSize(int max_bytes); | 60 bool SetMaxSize(int max_bytes); |
84 | 61 |
85 // Sets the cache type for this backend. | 62 // Sets the cache type for this backend. |
86 void SetType(net::CacheType type); | 63 void SetType(net::CacheType type); |
87 | 64 |
88 // Returns the full name for an external storage file. | |
89 base::FilePath GetFileName(Addr address) const; | |
90 | |
91 // Returns the actual file used to store a given (non-external) address. | |
92 MappedFile* File(Addr address); | |
93 | |
94 // Returns a weak pointer to the background queue. | |
95 base::WeakPtr<InFlightBackendIO> GetBackgroundQueue(); | |
96 | |
97 // Creates an external storage file. | |
98 bool CreateExternalFile(Addr* address); | |
99 | |
100 // Creates a new storage block of size block_count. | 65 // Creates a new storage block of size block_count. |
101 bool CreateBlock(FileType block_type, int block_count, | 66 bool CreateBlock(FileType block_type, int block_count, |
102 Addr* block_address); | 67 Addr* block_address); |
103 | 68 |
104 // Deletes a given storage block. deep set to true can be used to zero-fill | |
105 // the related storage in addition of releasing the related block. | |
106 void DeleteBlock(Addr block_address, bool deep); | |
107 | |
108 // Retrieves a pointer to the LRU-related data. | |
109 LruData* GetLruData(); | |
110 | |
111 // Updates the ranking information for an entry. | 69 // Updates the ranking information for an entry. |
112 void UpdateRank(EntryImpl* entry, bool modified); | 70 void UpdateRank(EntryImpl* entry, bool modified); |
113 | 71 |
114 // A node was recovered from a crash, it may not be on the index, so this | |
115 // method checks it and takes the appropriate action. | |
116 void RecoveredEntry(CacheRankingsBlock* rankings); | |
117 | |
118 // Permanently deletes an entry, but still keeps track of it. | 72 // Permanently deletes an entry, but still keeps track of it. |
119 void InternalDoomEntry(EntryImpl* entry); | 73 void InternalDoomEntry(EntryImpl* entry); |
120 | 74 |
121 #if defined(NET_BUILD_STRESS_CACHE) | |
122 // Returns the address of the entry linked to the entry at a given |address|. | |
123 CacheAddr GetNextAddr(Addr address); | |
124 | |
125 // Verifies that |entry| is not currently reachable through the index. | |
126 void NotLinked(EntryImpl* entry); | |
127 #endif | |
128 | |
129 // Removes all references to this entry. | |
130 void RemoveEntry(EntryImpl* entry); | |
131 | |
132 // This method must be called when an entry is released for the last time, so | 75 // This method must be called when an entry is released for the last time, so |
133 // the entry should not be used anymore. |address| is the cache address of the | 76 // the entry should not be used anymore. |address| is the cache address of the |
134 // entry. | 77 // entry. |
135 void OnEntryDestroyBegin(Addr address); | 78 void OnEntryDestroyBegin(Addr address); |
136 | 79 |
137 // This method must be called after all resources for an entry have been | 80 // This method must be called after all resources for an entry have been |
138 // released. | 81 // released. |
139 void OnEntryDestroyEnd(); | 82 void OnEntryDestroyEnd(); |
140 | 83 |
141 // If the data stored by the provided |rankings| points to an open entry, | 84 // If the data stored by the provided |rankings| points to an open entry, |
(...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
186 base::WeakPtr<BackendImpl> GetWeakPtr(); | 129 base::WeakPtr<BackendImpl> GetWeakPtr(); |
187 | 130 |
188 // Returns true if we should send histograms for this user again. The caller | 131 // Returns true if we should send histograms for this user again. The caller |
189 // must call this function only once per run (because it returns always the | 132 // must call this function only once per run (because it returns always the |
190 // same thing on a given run). | 133 // same thing on a given run). |
191 bool ShouldReportAgain(); | 134 bool ShouldReportAgain(); |
192 | 135 |
193 // Reports some data when we filled up the cache. | 136 // Reports some data when we filled up the cache. |
194 void FirstEviction(); | 137 void FirstEviction(); |
195 | 138 |
196 // Reports a critical error (and disables the cache). | |
197 void CriticalError(int error); | |
198 | |
199 // Reports an uncommon, recoverable error. | |
200 void ReportError(int error); | |
201 | |
202 // Called when an interesting event should be logged (counted). | 139 // Called when an interesting event should be logged (counted). |
203 void OnEvent(Stats::Counters an_event); | 140 void OnEvent(Stats::Counters an_event); |
204 | 141 |
205 // Keeps track of payload access (doesn't include metadata). | 142 // Keeps track of payload access (doesn't include metadata). |
206 void OnRead(int bytes); | 143 void OnRead(int bytes); |
207 void OnWrite(int bytes); | 144 void OnWrite(int bytes); |
208 | 145 |
209 // Timer callback to calculate usage statistics. | 146 // Timer callback to calculate usage statistics. |
210 void OnStatsTimer(); | 147 void OnStatsTimer(); |
211 | 148 |
212 // Handles the pending asynchronous IO count. | |
213 void IncrementIoCount(); | |
214 void DecrementIoCount(); | |
215 | |
216 // Sets internal parameters to enable unit testing mode. | 149 // Sets internal parameters to enable unit testing mode. |
217 void SetUnitTestMode(); | 150 void SetUnitTestMode(); |
218 | 151 |
219 // Sets internal parameters to enable upgrade mode (for internal tools). | 152 // Sets internal parameters to enable upgrade mode (for internal tools). |
220 void SetUpgradeMode(); | 153 void SetUpgradeMode(); |
221 | 154 |
222 // Sets the eviction algorithm to version 2. | 155 // Sets the eviction algorithm to version 2. |
223 void SetNewEviction(); | 156 void SetNewEviction(); |
224 | 157 |
225 // Sets an explicit set of BackendFlags. | 158 // Sets an explicit set of BackendFlags. |
226 void SetFlags(uint32 flags); | 159 void SetFlags(uint32 flags); |
227 | 160 |
228 // Clears the counter of references to test handling of corruptions. | |
229 void ClearRefCountForTest(); | |
230 | |
231 // Sends a dummy operation through the operation queue, for unit tests. | 161 // Sends a dummy operation through the operation queue, for unit tests. |
232 int FlushQueueForTest(const CompletionCallback& callback); | 162 int FlushQueueForTest(const CompletionCallback& callback); |
233 | 163 |
234 // Runs the provided task on the cache thread. The task will be automatically | |
235 // deleted after it runs. | |
236 int RunTaskForTest(const base::Closure& task, | |
237 const CompletionCallback& callback); | |
238 | |
239 // Trims an entry (all if |empty| is true) from the list of deleted | 164 // Trims an entry (all if |empty| is true) from the list of deleted |
240 // entries. This method should be called directly on the cache thread. | 165 // entries. This method should be called directly on the cache thread. |
241 void TrimForTest(bool empty); | 166 void TrimForTest(bool empty); |
242 | 167 |
243 // Trims an entry (all if |empty| is true) from the list of deleted | 168 // Trims an entry (all if |empty| is true) from the list of deleted |
244 // entries. This method should be called directly on the cache thread. | 169 // entries. This method should be called directly on the cache thread. |
245 void TrimDeletedListForTest(bool empty); | 170 void TrimDeletedListForTest(bool empty); |
246 | 171 |
247 // Performs a simple self-check, and returns the number of dirty items | 172 // Performs a simple self-check, and returns the number of dirty items |
248 // or an error code (negative value). | 173 // or an error code (negative value). |
249 int SelfCheck(); | 174 int SelfCheck(); |
250 | 175 |
251 // Ensures the index is flushed to disk (a no-op on platforms with mmap). | |
252 void FlushIndex(); | |
253 | |
254 // Backend implementation. | 176 // Backend implementation. |
255 virtual net::CacheType GetCacheType() const OVERRIDE; | 177 virtual net::CacheType GetCacheType() const OVERRIDE; |
256 virtual int32 GetEntryCount() const OVERRIDE; | 178 virtual int32 GetEntryCount() const OVERRIDE; |
257 virtual int OpenEntry(const std::string& key, Entry** entry, | 179 virtual int OpenEntry(const std::string& key, Entry** entry, |
258 const CompletionCallback& callback) OVERRIDE; | 180 const CompletionCallback& callback) OVERRIDE; |
259 virtual int CreateEntry(const std::string& key, Entry** entry, | 181 virtual int CreateEntry(const std::string& key, Entry** entry, |
260 const CompletionCallback& callback) OVERRIDE; | 182 const CompletionCallback& callback) OVERRIDE; |
261 virtual int DoomEntry(const std::string& key, | 183 virtual int DoomEntry(const std::string& key, |
262 const CompletionCallback& callback) OVERRIDE; | 184 const CompletionCallback& callback) OVERRIDE; |
263 virtual int DoomAllEntries(const CompletionCallback& callback) OVERRIDE; | 185 virtual int DoomAllEntries(const CompletionCallback& callback) OVERRIDE; |
264 virtual int DoomEntriesBetween(base::Time initial_time, | 186 virtual int DoomEntriesBetween(base::Time initial_time, |
265 base::Time end_time, | 187 base::Time end_time, |
266 const CompletionCallback& callback) OVERRIDE; | 188 const CompletionCallback& callback) OVERRIDE; |
267 virtual int DoomEntriesSince(base::Time initial_time, | 189 virtual int DoomEntriesSince(base::Time initial_time, |
268 const CompletionCallback& callback) OVERRIDE; | 190 const CompletionCallback& callback) OVERRIDE; |
269 virtual int OpenNextEntry(void** iter, Entry** next_entry, | 191 virtual int OpenNextEntry(void** iter, Entry** next_entry, |
270 const CompletionCallback& callback) OVERRIDE; | 192 const CompletionCallback& callback) OVERRIDE; |
271 virtual void EndEnumeration(void** iter) OVERRIDE; | 193 virtual void EndEnumeration(void** iter) OVERRIDE; |
272 virtual void GetStats(StatsItems* stats) OVERRIDE; | 194 virtual void GetStats(StatsItems* stats) OVERRIDE; |
273 virtual void OnExternalCacheHit(const std::string& key) OVERRIDE; | 195 virtual void OnExternalCacheHit(const std::string& key) OVERRIDE; |
274 | 196 |
275 private: | 197 private: |
276 typedef base::hash_map<CacheAddr, EntryImpl*> EntriesMap; | 198 typedef base::hash_map<CacheAddr, EntryImpl*> EntriesMap; |
277 | 199 |
278 // Creates a new backing file for the cache index. | |
279 bool CreateBackingStore(disk_cache::File* file); | |
280 bool InitBackingStore(bool* file_created); | |
281 void AdjustMaxCacheSize(int table_len); | 200 void AdjustMaxCacheSize(int table_len); |
282 | 201 |
283 bool InitStats(); | 202 bool InitStats(); |
284 void StoreStats(); | 203 void StoreStats(); |
285 | 204 |
286 // Deletes the cache and starts again. | 205 // Deletes the cache and starts again. |
287 void RestartCache(bool failure); | 206 void RestartCache(bool failure); |
288 void PrepareForRestart(); | 207 void PrepareForRestart(); |
289 | 208 |
| 209 void CleanupCache(); |
| 210 |
290 // Creates a new entry object. Returns zero on success, or a disk_cache error | 211 // Creates a new entry object. Returns zero on success, or a disk_cache error |
291 // on failure. | 212 // on failure. |
292 int NewEntry(Addr address, EntryImpl** entry); | 213 int NewEntry(Addr address, EntryImpl** entry); |
293 | 214 |
294 // Returns a given entry from the cache. The entry to match is determined by | |
295 // key and hash, and the returned entry may be the matched one or it's parent | |
296 // on the list of entries with the same hash (or bucket). To look for a parent | |
297 // of a given entry, |entry_addr| should be grabbed from that entry, so that | |
298 // if it doesn't match the entry on the index, we know that it was replaced | |
299 // with a new entry; in this case |*match_error| will be set to true and the | |
300 // return value will be NULL. | |
301 EntryImpl* MatchEntry(const std::string& key, uint32 hash, bool find_parent, | |
302 Addr entry_addr, bool* match_error); | |
303 | |
304 // Opens the next or previous entry on a cache iteration. | 215 // Opens the next or previous entry on a cache iteration. |
305 EntryImpl* OpenFollowingEntry(bool forward, void** iter); | 216 EntryImpl* OpenFollowingEntry(bool forward, void** iter); |
306 | 217 |
307 // Opens the next or previous entry on a single list. If successful, | |
308 // |from_entry| will be updated to point to the new entry, otherwise it will | |
309 // be set to NULL; in other words, it is used as an explicit iterator. | |
310 bool OpenFollowingEntryFromList(bool forward, Rankings::List list, | |
311 CacheRankingsBlock** from_entry, | |
312 EntryImpl** next_entry); | |
313 | |
314 // Returns the entry that is pointed by |next|, from the given |list|. | |
315 EntryImpl* GetEnumeratedEntry(CacheRankingsBlock* next, Rankings::List list); | |
316 | |
317 // Re-opens an entry that was previously deleted. | |
318 EntryImpl* ResurrectEntry(EntryImpl* deleted_entry); | |
319 | |
320 void DestroyInvalidEntry(EntryImpl* entry); | |
321 | |
322 // Handles the used storage count. | 218 // Handles the used storage count. |
323 void AddStorageSize(int32 bytes); | 219 void AddStorageSize(int32 bytes); |
324 void SubstractStorageSize(int32 bytes); | 220 void SubstractStorageSize(int32 bytes); |
325 | 221 |
326 // Update the number of referenced cache entries. | 222 // Update the number of referenced cache entries. |
327 void IncreaseNumRefs(); | 223 void IncreaseNumRefs(); |
328 void DecreaseNumRefs(); | 224 void DecreaseNumRefs(); |
329 void IncreaseNumEntries(); | 225 void IncreaseNumEntries(); |
330 void DecreaseNumEntries(); | 226 void DecreaseNumEntries(); |
331 | 227 |
332 // Dumps current cache statistics to the log. | 228 // Dumps current cache statistics to the log. |
333 void LogStats(); | 229 void LogStats(); |
334 | 230 |
335 // Send UMA stats. | 231 // Send UMA stats. |
336 void ReportStats(); | 232 void ReportStats(); |
337 | 233 |
338 // Upgrades the index file to version 2.1. | 234 // Reports an uncommon, recoverable error. |
339 void UpgradeTo2_1(); | 235 void ReportError(int error); |
340 | 236 |
341 // Performs basic checks on the index file. Returns false on failure. | 237 // Performs basic checks on the index file. Returns false on failure. |
342 bool CheckIndex(); | 238 bool CheckIndex(); |
343 | 239 |
344 // Part of the self test. Returns the number or dirty entries, or an error. | 240 // Part of the self test. Returns the number or dirty entries, or an error. |
345 int CheckAllEntries(); | 241 int CheckAllEntries(); |
346 | 242 |
347 // Part of the self test. Returns false if the entry is corrupt. | 243 // Part of the self test. Returns false if the entry is corrupt. |
348 bool CheckEntry(EntryImpl* cache_entry); | 244 bool CheckEntry(EntryImpl* cache_entry); |
349 | 245 |
350 // Returns the maximum total memory for the memory buffers. | 246 // Returns the maximum total memory for the memory buffers. |
351 int MaxBuffersSize(); | 247 int MaxBuffersSize(); |
352 | 248 |
353 InFlightBackendIO background_queue_; // The controller of pending operations. | |
354 scoped_refptr<MappedFile> index_; // The main cache index. | 249 scoped_refptr<MappedFile> index_; // The main cache index. |
355 base::FilePath path_; // Path to the folder used as backing storage. | 250 base::FilePath path_; // Path to the folder used as backing storage. |
356 Index* data_; // Pointer to the index data. | |
357 BlockFiles block_files_; // Set of files used to store all data. | 251 BlockFiles block_files_; // Set of files used to store all data. |
358 Rankings rankings_; // Rankings to be able to trim the cache. | |
359 uint32 mask_; // Binary mask to map a hash to the hash table. | |
360 int32 max_size_; // Maximum data size for this instance. | 252 int32 max_size_; // Maximum data size for this instance. |
361 Eviction eviction_; // Handler of the eviction algorithm. | 253 Eviction eviction_; // Handler of the eviction algorithm. |
362 EntriesMap open_entries_; // Map of open entries. | 254 EntriesMap open_entries_; // Map of open entries. |
363 int num_refs_; // Number of referenced cache entries. | 255 int num_refs_; // Number of referenced cache entries. |
364 int max_refs_; // Max number of referenced cache entries. | 256 int max_refs_; // Max number of referenced cache entries. |
365 int num_pending_io_; // Number of pending IO operations. | |
366 int entry_count_; // Number of entries accessed lately. | 257 int entry_count_; // Number of entries accessed lately. |
367 int byte_count_; // Number of bytes read/written lately. | 258 int byte_count_; // Number of bytes read/written lately. |
368 int buffer_bytes_; // Total size of the temporary entries' buffers. | 259 int buffer_bytes_; // Total size of the temporary entries' buffers. |
369 int up_ticks_; // The number of timer ticks received (OnStatsTimer). | 260 int up_ticks_; // The number of timer ticks received (OnStatsTimer). |
370 net::CacheType cache_type_; | 261 net::CacheType cache_type_; |
371 int uma_report_; // Controls transmission of UMA data. | 262 int uma_report_; // Controls transmission of UMA data. |
372 uint32 user_flags_; // Flags set by the user. | 263 uint32 user_flags_; // Flags set by the user. |
373 bool init_; // controls the initialization of the system. | 264 bool init_; // controls the initialization of the system. |
374 bool restarted_; | 265 bool restarted_; |
375 bool unit_test_; | 266 bool unit_test_; |
376 bool read_only_; // Prevents updates of the rankings data (used by tools). | 267 bool read_only_; // Prevents updates of the rankings data (used by tools). |
377 bool disabled_; | 268 bool disabled_; |
378 bool new_eviction_; // What eviction algorithm should be used. | 269 bool new_eviction_; // What eviction algorithm should be used. |
379 bool first_timer_; // True if the timer has not been called. | 270 bool first_timer_; // True if the timer has not been called. |
380 bool user_load_; // True if we see a high load coming from the caller. | 271 bool user_load_; // True if we see a high load coming from the caller. |
381 | 272 |
382 net::NetLog* net_log_; | 273 net::NetLog* net_log_; |
383 | 274 |
384 Stats stats_; // Usage statistics. | 275 Stats stats_; // Usage statistics. |
385 scoped_ptr<base::RepeatingTimer<BackendImpl> > timer_; // Usage timer. | 276 scoped_ptr<base::RepeatingTimer<BackendImpl> > timer_; // Usage timer. |
386 base::WaitableEvent done_; // Signals the end of background work. | |
387 scoped_refptr<TraceObject> trace_object_; // Initializes internal tracing. | 277 scoped_refptr<TraceObject> trace_object_; // Initializes internal tracing. |
388 base::WeakPtrFactory<BackendImpl> ptr_factory_; | 278 base::WeakPtrFactory<BackendImpl> ptr_factory_; |
389 | 279 |
390 DISALLOW_COPY_AND_ASSIGN(BackendImpl); | 280 DISALLOW_COPY_AND_ASSIGN(BackendImpl); |
391 }; | 281 }; |
392 | 282 |
393 // Returns the preferred max cache size given the available disk space. | 283 // Returns the preferred max cache size given the available disk space. |
394 NET_EXPORT_PRIVATE int PreferedCacheSize(int64 available); | 284 NET_EXPORT_PRIVATE int PreferedCacheSize(int64 available); |
395 | 285 |
396 } // namespace disk_cache | 286 } // namespace disk_cache |
397 | 287 |
398 #endif // NET_DISK_CACHE_BACKEND_IMPL_H_ | 288 #endif // NET_DISK_CACHE_BACKEND_IMPL_H_ |
OLD | NEW |