Roll src/third_party/WebKit d9c6159:8139f33 (svn 201974:201975)
[chromium-blink-merge.git] / net / disk_cache / blockfile / backend_impl.h
blobd3a81ac2d1ba6767685d9832187df6218eb76e3e
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
3 // found in the LICENSE file.
5 // See net/disk_cache/disk_cache.h for the public interface of the cache.
7 #ifndef NET_DISK_CACHE_BLOCKFILE_BACKEND_IMPL_H_
8 #define NET_DISK_CACHE_BLOCKFILE_BACKEND_IMPL_H_
10 #include "base/containers/hash_tables.h"
11 #include "base/files/file_path.h"
12 #include "base/memory/ref_counted.h"
13 #include "base/timer/timer.h"
14 #include "net/disk_cache/blockfile/block_files.h"
15 #include "net/disk_cache/blockfile/eviction.h"
16 #include "net/disk_cache/blockfile/in_flight_backend_io.h"
17 #include "net/disk_cache/blockfile/rankings.h"
18 #include "net/disk_cache/blockfile/stats.h"
19 #include "net/disk_cache/blockfile/stress_support.h"
20 #include "net/disk_cache/blockfile/trace.h"
21 #include "net/disk_cache/disk_cache.h"
23 namespace base {
24 class SingleThreadTaskRunner;
25 } // namespace base
27 namespace net {
28 class NetLog;
29 } // namespace net
31 namespace disk_cache {
33 struct Index;
35 enum BackendFlags {
36 kNone = 0,
37 kMask = 1, // A mask (for the index table) was specified.
38 kMaxSize = 1 << 1, // A maximum size was provided.
39 kUnitTestMode = 1 << 2, // We are modifying the behavior for testing.
40 kUpgradeMode = 1 << 3, // This is the upgrade tool (dump).
41 kNewEviction = 1 << 4, // Use of new eviction was specified.
42 kNoRandom = 1 << 5, // Don't add randomness to the behavior.
43 kNoLoadProtection = 1 << 6, // Don't act conservatively under load.
44 kNoBuffering = 1 << 7 // Disable extended IO buffering.
47 // This class implements the Backend interface. An object of this
48 // class handles the operations of the cache for a particular profile.
49 class NET_EXPORT_PRIVATE BackendImpl : public Backend {
50 friend class Eviction;
51 public:
52 BackendImpl(const base::FilePath& path,
53 const scoped_refptr<base::SingleThreadTaskRunner>& cache_thread,
54 net::NetLog* net_log);
55 // mask can be used to limit the usable size of the hash table, for testing.
56 BackendImpl(const base::FilePath& path,
57 uint32 mask,
58 const scoped_refptr<base::SingleThreadTaskRunner>& cache_thread,
59 net::NetLog* net_log);
60 ~BackendImpl() override;
62 // Performs general initialization for this current instance of the cache.
63 int Init(const CompletionCallback& callback);
65 // Performs the actual initialization and final cleanup on destruction.
66 int SyncInit();
67 void CleanupCache();
69 // Synchronous implementation of the asynchronous interface.
70 int SyncOpenEntry(const std::string& key, Entry** entry);
71 int SyncCreateEntry(const std::string& key, Entry** entry);
72 int SyncDoomEntry(const std::string& key);
73 int SyncDoomAllEntries();
74 int SyncDoomEntriesBetween(base::Time initial_time,
75 base::Time end_time);
76 int SyncDoomEntriesSince(base::Time initial_time);
77 int SyncOpenNextEntry(Rankings::Iterator* iterator, Entry** next_entry);
78 void SyncEndEnumeration(scoped_ptr<Rankings::Iterator> iterator);
79 void SyncOnExternalCacheHit(const std::string& key);
81 // Open or create an entry for the given |key| or |iter|.
82 EntryImpl* OpenEntryImpl(const std::string& key);
83 EntryImpl* CreateEntryImpl(const std::string& key);
84 EntryImpl* OpenNextEntryImpl(Rankings::Iterator* iter);
86 // Sets the maximum size for the total amount of data stored by this instance.
87 bool SetMaxSize(int max_bytes);
89 // Sets the cache type for this backend.
90 void SetType(net::CacheType type);
92 // Returns the full name for an external storage file.
93 base::FilePath GetFileName(Addr address) const;
95 // Returns the actual file used to store a given (non-external) address.
96 MappedFile* File(Addr address);
98 // Returns a weak pointer to the background queue.
99 base::WeakPtr<InFlightBackendIO> GetBackgroundQueue();
101 // Creates an external storage file.
102 bool CreateExternalFile(Addr* address);
104 // Creates a new storage block of size block_count.
105 bool CreateBlock(FileType block_type, int block_count,
106 Addr* block_address);
108 // Deletes a given storage block. deep set to true can be used to zero-fill
109 // the related storage in addition of releasing the related block.
110 void DeleteBlock(Addr block_address, bool deep);
112 // Retrieves a pointer to the LRU-related data.
113 LruData* GetLruData();
115 // Updates the ranking information for an entry.
116 void UpdateRank(EntryImpl* entry, bool modified);
118 // A node was recovered from a crash, it may not be on the index, so this
119 // method checks it and takes the appropriate action.
120 void RecoveredEntry(CacheRankingsBlock* rankings);
122 // Permanently deletes an entry, but still keeps track of it.
123 void InternalDoomEntry(EntryImpl* entry);
125 #if defined(NET_BUILD_STRESS_CACHE)
126 // Returns the address of the entry linked to the entry at a given |address|.
127 CacheAddr GetNextAddr(Addr address);
129 // Verifies that |entry| is not currently reachable through the index.
130 void NotLinked(EntryImpl* entry);
131 #endif
133 // Removes all references to this entry.
134 void RemoveEntry(EntryImpl* entry);
136 // This method must be called when an entry is released for the last time, so
137 // the entry should not be used anymore. |address| is the cache address of the
138 // entry.
139 void OnEntryDestroyBegin(Addr address);
141 // This method must be called after all resources for an entry have been
142 // released.
143 void OnEntryDestroyEnd();
145 // If the data stored by the provided |rankings| points to an open entry,
146 // returns a pointer to that entry, otherwise returns NULL. Note that this
147 // method does NOT increase the ref counter for the entry.
148 EntryImpl* GetOpenEntry(CacheRankingsBlock* rankings) const;
150 // Returns the id being used on this run of the cache.
151 int32 GetCurrentEntryId() const;
153 // Returns the maximum size for a file to reside on the cache.
154 int MaxFileSize() const;
156 // A user data block is being created, extended or truncated.
157 void ModifyStorageSize(int32 old_size, int32 new_size);
159 // Logs requests that are denied due to being too big.
160 void TooMuchStorageRequested(int32 size);
162 // Returns true if a temporary buffer is allowed to be extended.
163 bool IsAllocAllowed(int current_size, int new_size);
165 // Tracks the release of |size| bytes by an entry buffer.
166 void BufferDeleted(int size);
168 // Only intended for testing the two previous methods.
169 int GetTotalBuffersSize() const {
170 return buffer_bytes_;
173 // Returns true if this instance seems to be under heavy load.
174 bool IsLoaded() const;
176 // Returns the full histogram name, for the given base |name| and experiment,
177 // and the current cache type. The name will be "DiskCache.t.name_e" where n
178 // is the cache type and e the provided |experiment|.
179 std::string HistogramName(const char* name, int experiment) const;
181 net::CacheType cache_type() const {
182 return cache_type_;
185 bool read_only() const {
186 return read_only_;
189 // Returns a weak pointer to this object.
190 base::WeakPtr<BackendImpl> GetWeakPtr();
192 // Returns true if we should send histograms for this user again. The caller
193 // must call this function only once per run (because it returns always the
194 // same thing on a given run).
195 bool ShouldReportAgain();
197 // Reports some data when we filled up the cache.
198 void FirstEviction();
200 // Reports a critical error (and disables the cache).
201 void CriticalError(int error);
203 // Reports an uncommon, recoverable error.
204 void ReportError(int error);
206 // Called when an interesting event should be logged (counted).
207 void OnEvent(Stats::Counters an_event);
209 // Keeps track of payload access (doesn't include metadata).
210 void OnRead(int bytes);
211 void OnWrite(int bytes);
213 // Timer callback to calculate usage statistics.
214 void OnStatsTimer();
216 // Handles the pending asynchronous IO count.
217 void IncrementIoCount();
218 void DecrementIoCount();
220 // Sets internal parameters to enable unit testing mode.
221 void SetUnitTestMode();
223 // Sets internal parameters to enable upgrade mode (for internal tools).
224 void SetUpgradeMode();
226 // Sets the eviction algorithm to version 2.
227 void SetNewEviction();
229 // Sets an explicit set of BackendFlags.
230 void SetFlags(uint32 flags);
232 // Clears the counter of references to test handling of corruptions.
233 void ClearRefCountForTest();
235 // Sends a dummy operation through the operation queue, for unit tests.
236 int FlushQueueForTest(const CompletionCallback& callback);
238 // Runs the provided task on the cache thread. The task will be automatically
239 // deleted after it runs.
240 int RunTaskForTest(const base::Closure& task,
241 const CompletionCallback& callback);
243 // 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.
245 void TrimForTest(bool empty);
247 // Trims an entry (all if |empty| is true) from the list of deleted
248 // entries. This method should be called directly on the cache thread.
249 void TrimDeletedListForTest(bool empty);
251 // Only intended for testing
252 base::RepeatingTimer<BackendImpl>* GetTimerForTest();
254 // Performs a simple self-check, and returns the number of dirty items
255 // or an error code (negative value).
256 int SelfCheck();
258 // Ensures the index is flushed to disk (a no-op on platforms with mmap).
259 void FlushIndex();
261 // Backend implementation.
262 net::CacheType GetCacheType() const override;
263 int32 GetEntryCount() const override;
264 int OpenEntry(const std::string& key,
265 Entry** entry,
266 const CompletionCallback& callback) override;
267 int CreateEntry(const std::string& key,
268 Entry** entry,
269 const CompletionCallback& callback) override;
270 int DoomEntry(const std::string& key,
271 const CompletionCallback& callback) override;
272 int DoomAllEntries(const CompletionCallback& callback) override;
273 int DoomEntriesBetween(base::Time initial_time,
274 base::Time end_time,
275 const CompletionCallback& callback) override;
276 int DoomEntriesSince(base::Time initial_time,
277 const CompletionCallback& callback) override;
278 // NOTE: The blockfile Backend::Iterator::OpenNextEntry method does not modify
279 // the last_used field of the entry, and therefore it does not impact the
280 // eviction ranking of the entry. However, an enumeration will go through all
281 // entries on the cache only if the cache is not modified while the
282 // enumeration is taking place. Significantly altering the entry pointed by
283 // the iterator (for example, deleting the entry) will invalidate the
284 // iterator. Performing operations on an entry that modify the entry may
285 // result in loops in the iteration, skipped entries or similar.
286 scoped_ptr<Iterator> CreateIterator() override;
287 void GetStats(StatsItems* stats) override;
288 void OnExternalCacheHit(const std::string& key) override;
290 private:
291 typedef base::hash_map<CacheAddr, EntryImpl*> EntriesMap;
292 class IteratorImpl;
294 // Creates a new backing file for the cache index.
295 bool CreateBackingStore(disk_cache::File* file);
296 bool InitBackingStore(bool* file_created);
297 void AdjustMaxCacheSize(int table_len);
299 bool InitStats();
300 void StoreStats();
302 // Deletes the cache and starts again.
303 void RestartCache(bool failure);
304 void PrepareForRestart();
306 // Creates a new entry object. Returns zero on success, or a disk_cache error
307 // on failure.
308 int NewEntry(Addr address, EntryImpl** entry);
310 // Returns a given entry from the cache. The entry to match is determined by
311 // key and hash, and the returned entry may be the matched one or it's parent
312 // on the list of entries with the same hash (or bucket). To look for a parent
313 // of a given entry, |entry_addr| should be grabbed from that entry, so that
314 // if it doesn't match the entry on the index, we know that it was replaced
315 // with a new entry; in this case |*match_error| will be set to true and the
316 // return value will be NULL.
317 EntryImpl* MatchEntry(const std::string& key, uint32 hash, bool find_parent,
318 Addr entry_addr, bool* match_error);
320 // Opens the next or previous entry on a single list. If successful,
321 // |from_entry| will be updated to point to the new entry, otherwise it will
322 // be set to NULL; in other words, it is used as an explicit iterator.
323 bool OpenFollowingEntryFromList(Rankings::List list,
324 CacheRankingsBlock** from_entry,
325 EntryImpl** next_entry);
327 // Returns the entry that is pointed by |next|, from the given |list|.
328 EntryImpl* GetEnumeratedEntry(CacheRankingsBlock* next, Rankings::List list);
330 // Re-opens an entry that was previously deleted.
331 EntryImpl* ResurrectEntry(EntryImpl* deleted_entry);
333 void DestroyInvalidEntry(EntryImpl* entry);
335 // Handles the used storage count.
336 void AddStorageSize(int32 bytes);
337 void SubstractStorageSize(int32 bytes);
339 // Update the number of referenced cache entries.
340 void IncreaseNumRefs();
341 void DecreaseNumRefs();
342 void IncreaseNumEntries();
343 void DecreaseNumEntries();
345 // Dumps current cache statistics to the log.
346 void LogStats();
348 // Send UMA stats.
349 void ReportStats();
351 // Upgrades the index file to version 2.1.
352 void UpgradeTo2_1();
354 // Performs basic checks on the index file. Returns false on failure.
355 bool CheckIndex();
357 // Part of the self test. Returns the number or dirty entries, or an error.
358 int CheckAllEntries();
360 // Part of the self test. Returns false if the entry is corrupt.
361 bool CheckEntry(EntryImpl* cache_entry);
363 // Returns the maximum total memory for the memory buffers.
364 int MaxBuffersSize();
366 InFlightBackendIO background_queue_; // The controller of pending operations.
367 scoped_refptr<MappedFile> index_; // The main cache index.
368 base::FilePath path_; // Path to the folder used as backing storage.
369 Index* data_; // Pointer to the index data.
370 BlockFiles block_files_; // Set of files used to store all data.
371 Rankings rankings_; // Rankings to be able to trim the cache.
372 uint32 mask_; // Binary mask to map a hash to the hash table.
373 int32 max_size_; // Maximum data size for this instance.
374 Eviction eviction_; // Handler of the eviction algorithm.
375 EntriesMap open_entries_; // Map of open entries.
376 int num_refs_; // Number of referenced cache entries.
377 int max_refs_; // Max number of referenced cache entries.
378 int num_pending_io_; // Number of pending IO operations.
379 int entry_count_; // Number of entries accessed lately.
380 int byte_count_; // Number of bytes read/written lately.
381 int buffer_bytes_; // Total size of the temporary entries' buffers.
382 int up_ticks_; // The number of timer ticks received (OnStatsTimer).
383 net::CacheType cache_type_;
384 int uma_report_; // Controls transmission of UMA data.
385 uint32 user_flags_; // Flags set by the user.
386 bool init_; // controls the initialization of the system.
387 bool restarted_;
388 bool unit_test_;
389 bool read_only_; // Prevents updates of the rankings data (used by tools).
390 bool disabled_;
391 bool new_eviction_; // What eviction algorithm should be used.
392 bool first_timer_; // True if the timer has not been called.
393 bool user_load_; // True if we see a high load coming from the caller.
395 net::NetLog* net_log_;
397 Stats stats_; // Usage statistics.
398 scoped_ptr<base::RepeatingTimer<BackendImpl> > timer_; // Usage timer.
399 base::WaitableEvent done_; // Signals the end of background work.
400 scoped_refptr<TraceObject> trace_object_; // Initializes internal tracing.
401 base::WeakPtrFactory<BackendImpl> ptr_factory_;
403 DISALLOW_COPY_AND_ASSIGN(BackendImpl);
406 } // namespace disk_cache
408 #endif // NET_DISK_CACHE_BLOCKFILE_BACKEND_IMPL_H_