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.
4
5// See net/disk_cache/disk_cache.h for the public interface of the cache.
6
7#ifndef NET_DISK_CACHE_BLOCKFILE_BACKEND_IMPL_H_
8#define NET_DISK_CACHE_BLOCKFILE_BACKEND_IMPL_H_
9
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"
22
23namespace base {
24class SingleThreadTaskRunner;
25}  // namespace base
26
27namespace net {
28class NetLog;
29}  // namespace net
30
31namespace disk_cache {
32
33struct Index;
34
35enum 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.
45};
46
47// This class implements the Backend interface. An object of this
48// class handles the operations of the cache for a particular profile.
49class 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  virtual ~BackendImpl();
61
62  // Performs general initialization for this current instance of the cache.
63  int Init(const CompletionCallback& callback);
64
65  // Performs the actual initialization and final cleanup on destruction.
66  int SyncInit();
67  void CleanupCache();
68
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);
80
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);
85
86  // Sets the maximum size for the total amount of data stored by this instance.
87  bool SetMaxSize(int max_bytes);
88
89  // Sets the cache type for this backend.
90  void SetType(net::CacheType type);
91
92  // Returns the full name for an external storage file.
93  base::FilePath GetFileName(Addr address) const;
94
95  // Returns the actual file used to store a given (non-external) address.
96  MappedFile* File(Addr address);
97
98  // Returns a weak pointer to the background queue.
99  base::WeakPtr<InFlightBackendIO> GetBackgroundQueue();
100
101  // Creates an external storage file.
102  bool CreateExternalFile(Addr* address);
103
104  // Creates a new storage block of size block_count.
105  bool CreateBlock(FileType block_type, int block_count,
106                   Addr* block_address);
107
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);
111
112  // Retrieves a pointer to the LRU-related data.
113  LruData* GetLruData();
114
115  // Updates the ranking information for an entry.
116  void UpdateRank(EntryImpl* entry, bool modified);
117
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);
121
122  // Permanently deletes an entry, but still keeps track of it.
123  void InternalDoomEntry(EntryImpl* entry);
124
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);
128
129  // Verifies that |entry| is not currently reachable through the index.
130  void NotLinked(EntryImpl* entry);
131#endif
132
133  // Removes all references to this entry.
134  void RemoveEntry(EntryImpl* entry);
135
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);
140
141  // This method must be called after all resources for an entry have been
142  // released.
143  void OnEntryDestroyEnd();
144
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;
149
150  // Returns the id being used on this run of the cache.
151  int32 GetCurrentEntryId() const;
152
153  // Returns the maximum size for a file to reside on the cache.
154  int MaxFileSize() const;
155
156  // A user data block is being created, extended or truncated.
157  void ModifyStorageSize(int32 old_size, int32 new_size);
158
159  // Logs requests that are denied due to being too big.
160  void TooMuchStorageRequested(int32 size);
161
162  // Returns true if a temporary buffer is allowed to be extended.
163  bool IsAllocAllowed(int current_size, int new_size);
164
165  // Tracks the release of |size| bytes by an entry buffer.
166  void BufferDeleted(int size);
167
168  // Only intended for testing the two previous methods.
169  int GetTotalBuffersSize() const {
170    return buffer_bytes_;
171  }
172
173  // Returns true if this instance seems to be under heavy load.
174  bool IsLoaded() const;
175
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;
180
181  net::CacheType cache_type() const {
182    return cache_type_;
183  }
184
185  bool read_only() const {
186    return read_only_;
187  }
188
189  // Returns a weak pointer to this object.
190  base::WeakPtr<BackendImpl> GetWeakPtr();
191
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();
196
197  // Reports some data when we filled up the cache.
198  void FirstEviction();
199
200  // Reports a critical error (and disables the cache).
201  void CriticalError(int error);
202
203  // Reports an uncommon, recoverable error.
204  void ReportError(int error);
205
206  // Called when an interesting event should be logged (counted).
207  void OnEvent(Stats::Counters an_event);
208
209  // Keeps track of payload access (doesn't include metadata).
210  void OnRead(int bytes);
211  void OnWrite(int bytes);
212
213  // Timer callback to calculate usage statistics.
214  void OnStatsTimer();
215
216  // Handles the pending asynchronous IO count.
217  void IncrementIoCount();
218  void DecrementIoCount();
219
220  // Sets internal parameters to enable unit testing mode.
221  void SetUnitTestMode();
222
223  // Sets internal parameters to enable upgrade mode (for internal tools).
224  void SetUpgradeMode();
225
226  // Sets the eviction algorithm to version 2.
227  void SetNewEviction();
228
229  // Sets an explicit set of BackendFlags.
230  void SetFlags(uint32 flags);
231
232  // Clears the counter of references to test handling of corruptions.
233  void ClearRefCountForTest();
234
235  // Sends a dummy operation through the operation queue, for unit tests.
236  int FlushQueueForTest(const CompletionCallback& callback);
237
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);
242
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);
246
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);
250
251  // Only intended for testing
252  base::RepeatingTimer<BackendImpl>* GetTimerForTest();
253
254  // Performs a simple self-check, and returns the number of dirty items
255  // or an error code (negative value).
256  int SelfCheck();
257
258  // Ensures the index is flushed to disk (a no-op on platforms with mmap).
259  void FlushIndex();
260
261  // Backend implementation.
262  virtual net::CacheType GetCacheType() const OVERRIDE;
263  virtual int32 GetEntryCount() const OVERRIDE;
264  virtual int OpenEntry(const std::string& key, Entry** entry,
265                        const CompletionCallback& callback) OVERRIDE;
266  virtual int CreateEntry(const std::string& key, Entry** entry,
267                          const CompletionCallback& callback) OVERRIDE;
268  virtual int DoomEntry(const std::string& key,
269                        const CompletionCallback& callback) OVERRIDE;
270  virtual int DoomAllEntries(const CompletionCallback& callback) OVERRIDE;
271  virtual int DoomEntriesBetween(base::Time initial_time,
272                                 base::Time end_time,
273                                 const CompletionCallback& callback) OVERRIDE;
274  virtual int DoomEntriesSince(base::Time initial_time,
275                               const CompletionCallback& callback) OVERRIDE;
276  // NOTE: The blockfile Backend::Iterator::OpenNextEntry method does not modify
277  // the last_used field of the entry, and therefore it does not impact the
278  // eviction ranking of the entry. However, an enumeration will go through all
279  // entries on the cache only if the cache is not modified while the
280  // enumeration is taking place. Significantly altering the entry pointed by
281  // the iterator (for example, deleting the entry) will invalidate the
282  // iterator. Performing operations on an entry that modify the entry may
283  // result in loops in the iteration, skipped entries or similar.
284  virtual scoped_ptr<Iterator> CreateIterator() OVERRIDE;
285  virtual void GetStats(StatsItems* stats) OVERRIDE;
286  virtual void OnExternalCacheHit(const std::string& key) OVERRIDE;
287
288 private:
289  typedef base::hash_map<CacheAddr, EntryImpl*> EntriesMap;
290  class IteratorImpl;
291
292  // Creates a new backing file for the cache index.
293  bool CreateBackingStore(disk_cache::File* file);
294  bool InitBackingStore(bool* file_created);
295  void AdjustMaxCacheSize(int table_len);
296
297  bool InitStats();
298  void StoreStats();
299
300  // Deletes the cache and starts again.
301  void RestartCache(bool failure);
302  void PrepareForRestart();
303
304  // Creates a new entry object. Returns zero on success, or a disk_cache error
305  // on failure.
306  int NewEntry(Addr address, EntryImpl** entry);
307
308  // Returns a given entry from the cache. The entry to match is determined by
309  // key and hash, and the returned entry may be the matched one or it's parent
310  // on the list of entries with the same hash (or bucket). To look for a parent
311  // of a given entry, |entry_addr| should be grabbed from that entry, so that
312  // if it doesn't match the entry on the index, we know that it was replaced
313  // with a new entry; in this case |*match_error| will be set to true and the
314  // return value will be NULL.
315  EntryImpl* MatchEntry(const std::string& key, uint32 hash, bool find_parent,
316                        Addr entry_addr, bool* match_error);
317
318  // Opens the next or previous entry on a single list. If successful,
319  // |from_entry| will be updated to point to the new entry, otherwise it will
320  // be set to NULL; in other words, it is used as an explicit iterator.
321  bool OpenFollowingEntryFromList(Rankings::List list,
322                                  CacheRankingsBlock** from_entry,
323                                  EntryImpl** next_entry);
324
325  // Returns the entry that is pointed by |next|, from the given |list|.
326  EntryImpl* GetEnumeratedEntry(CacheRankingsBlock* next, Rankings::List list);
327
328  // Re-opens an entry that was previously deleted.
329  EntryImpl* ResurrectEntry(EntryImpl* deleted_entry);
330
331  void DestroyInvalidEntry(EntryImpl* entry);
332
333  // Handles the used storage count.
334  void AddStorageSize(int32 bytes);
335  void SubstractStorageSize(int32 bytes);
336
337  // Update the number of referenced cache entries.
338  void IncreaseNumRefs();
339  void DecreaseNumRefs();
340  void IncreaseNumEntries();
341  void DecreaseNumEntries();
342
343  // Dumps current cache statistics to the log.
344  void LogStats();
345
346  // Send UMA stats.
347  void ReportStats();
348
349  // Upgrades the index file to version 2.1.
350  void UpgradeTo2_1();
351
352  // Performs basic checks on the index file. Returns false on failure.
353  bool CheckIndex();
354
355  // Part of the self test. Returns the number or dirty entries, or an error.
356  int CheckAllEntries();
357
358  // Part of the self test. Returns false if the entry is corrupt.
359  bool CheckEntry(EntryImpl* cache_entry);
360
361  // Returns the maximum total memory for the memory buffers.
362  int MaxBuffersSize();
363
364  InFlightBackendIO background_queue_;  // The controller of pending operations.
365  scoped_refptr<MappedFile> index_;  // The main cache index.
366  base::FilePath path_;  // Path to the folder used as backing storage.
367  Index* data_;  // Pointer to the index data.
368  BlockFiles block_files_;  // Set of files used to store all data.
369  Rankings rankings_;  // Rankings to be able to trim the cache.
370  uint32 mask_;  // Binary mask to map a hash to the hash table.
371  int32 max_size_;  // Maximum data size for this instance.
372  Eviction eviction_;  // Handler of the eviction algorithm.
373  EntriesMap open_entries_;  // Map of open entries.
374  int num_refs_;  // Number of referenced cache entries.
375  int max_refs_;  // Max number of referenced cache entries.
376  int num_pending_io_;  // Number of pending IO operations.
377  int entry_count_;  // Number of entries accessed lately.
378  int byte_count_;  // Number of bytes read/written lately.
379  int buffer_bytes_;  // Total size of the temporary entries' buffers.
380  int up_ticks_;  // The number of timer ticks received (OnStatsTimer).
381  net::CacheType cache_type_;
382  int uma_report_;  // Controls transmission of UMA data.
383  uint32 user_flags_;  // Flags set by the user.
384  bool init_;  // controls the initialization of the system.
385  bool restarted_;
386  bool unit_test_;
387  bool read_only_;  // Prevents updates of the rankings data (used by tools).
388  bool disabled_;
389  bool new_eviction_;  // What eviction algorithm should be used.
390  bool first_timer_;  // True if the timer has not been called.
391  bool user_load_;  // True if we see a high load coming from the caller.
392
393  net::NetLog* net_log_;
394
395  Stats stats_;  // Usage statistics.
396  scoped_ptr<base::RepeatingTimer<BackendImpl> > timer_;  // Usage timer.
397  base::WaitableEvent done_;  // Signals the end of background work.
398  scoped_refptr<TraceObject> trace_object_;  // Initializes internal tracing.
399  base::WeakPtrFactory<BackendImpl> ptr_factory_;
400
401  DISALLOW_COPY_AND_ASSIGN(BackendImpl);
402};
403
404}  // namespace disk_cache
405
406#endif  // NET_DISK_CACHE_BLOCKFILE_BACKEND_IMPL_H_
407