backend_impl_v3.h revision a1401311d1ab56c4ed0a474bd38c108f75cb0cd9
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_V3_H_
8#define NET_DISK_CACHE_BLOCKFILE_BACKEND_IMPL_V3_H_
9
10#include "base/containers/hash_tables.h"
11#include "base/files/file_path.h"
12#include "base/timer/timer.h"
13#include "net/disk_cache/blockfile/block_bitmaps_v3.h"
14#include "net/disk_cache/blockfile/block_files.h"
15#include "net/disk_cache/blockfile/eviction_v3.h"
16#include "net/disk_cache/blockfile/index_table_v3.h"
17#include "net/disk_cache/blockfile/stats.h"
18#include "net/disk_cache/blockfile/stress_support.h"
19#include "net/disk_cache/blockfile/trace.h"
20#include "net/disk_cache/disk_cache.h"
21
22namespace net {
23class NetLog;
24}  // namespace net
25
26namespace disk_cache {
27
28class EntryImplV3;
29
30// This class implements the Backend interface. An object of this
31// class handles the operations of the cache for a particular profile.
32class NET_EXPORT_PRIVATE BackendImplV3 : public Backend {
33 public:
34  enum BackendFlags {
35    MAX_SIZE = 1 << 1,            // A maximum size was provided.
36    UNIT_TEST_MODE = 1 << 2,      // We are modifying the behavior for testing.
37    UPGRADE_MODE = 1 << 3,        // This is the upgrade tool (dump).
38    EVICTION_V2 = 1 << 4,         // Use of new eviction was specified.
39    BASIC_UNIT_TEST = 1 << 5,     // Identifies almost all unit tests.
40    NO_LOAD_PROTECTION = 1 << 6,  // Don't act conservatively under load.
41    NO_BUFFERING = 1 << 7,        // Disable extended IO buffering.
42    NO_CLEAN_ON_EXIT = 1 << 8     // Avoid saving data at exit time.
43  };
44
45  BackendImplV3(const base::FilePath& path,
46                base::MessageLoopProxy* cache_thread,
47                net::NetLog* net_log);
48  virtual ~BackendImplV3();
49
50  // Performs general initialization for this current instance of the cache.
51  int Init(const CompletionCallback& callback);
52
53  // Same behavior as OpenNextEntry but walks the list from back to front.
54  int OpenPrevEntry(void** iter, Entry** prev_entry,
55                    const CompletionCallback& callback);
56
57  // Sets the maximum size for the total amount of data stored by this instance.
58  bool SetMaxSize(int max_bytes);
59
60  // Sets the cache type for this backend.
61  void SetType(net::CacheType type);
62
63  // Creates a new storage block of size block_count.
64  bool CreateBlock(FileType block_type, int block_count,
65                   Addr* block_address);
66
67  // Updates the ranking information for an entry.
68  void UpdateRank(EntryImplV3* entry, bool modified);
69
70  // Permanently deletes an entry, but still keeps track of it.
71  void InternalDoomEntry(EntryImplV3* entry);
72
73  // This method must be called when an entry is released for the last time, so
74  // the entry should not be used anymore. |address| is the cache address of the
75  // entry.
76  void OnEntryDestroyBegin(Addr address);
77
78  // This method must be called after all resources for an entry have been
79  // released.
80  void OnEntryDestroyEnd();
81
82  // If the |address| corresponds to an open entry, returns a pointer to that
83  // entry, otherwise returns NULL. Note that this method does not increase the
84  // ref counter for the entry.
85  EntryImplV3* GetOpenEntry(Addr address) const;
86
87  // Returns the id being used on this run of the cache.
88  int32 GetCurrentEntryId() const;
89
90  // Returns the maximum size for a file to reside on the cache.
91  int MaxFileSize() const;
92
93  // A user data block is being created, extended or truncated.
94  void ModifyStorageSize(int32 old_size, int32 new_size);
95
96  // Logs requests that are denied due to being too big.
97  void TooMuchStorageRequested(int32 size);
98
99  // Returns true if a temporary buffer is allowed to be extended.
100  bool IsAllocAllowed(int current_size, int new_size);
101
102  // Tracks the release of |size| bytes by an entry buffer.
103  void BufferDeleted(int size);
104
105  // Only intended for testing the two previous methods.
106  int GetTotalBuffersSize() const {
107    return buffer_bytes_;
108  }
109
110  // Returns true if this instance seems to be under heavy load.
111  bool IsLoaded() const;
112
113  // Returns the full histogram name, for the given base |name| and the current
114  // cache type. The name will be "DiskCache3.name_type".
115  std::string HistogramName(const char* name) const;
116
117  net::CacheType cache_type() const {
118    return cache_type_;
119  }
120
121  bool read_only() const {
122    return read_only_;
123  }
124
125  // Returns a weak pointer to this object.
126  base::WeakPtr<BackendImplV3> GetWeakPtr();
127
128  // Returns true if we should send histograms for this user again. The caller
129  // must call this function only once per run (because it returns always the
130  // same thing on a given run).
131  bool ShouldReportAgain();
132
133  // Reports some data when we filled up the cache.
134  void FirstEviction();
135
136  // Called when an interesting event should be logged (counted).
137  void OnEvent(Stats::Counters an_event);
138
139  // Keeps track of payload access (doesn't include metadata).
140  void OnRead(int bytes);
141  void OnWrite(int bytes);
142
143  // Timer callback to calculate usage statistics and perform backups.
144  void OnTimerTick();
145
146  // Sets internal parameters to enable unit testing mode.
147  void SetUnitTestMode();
148
149  // Sets internal parameters to enable upgrade mode (for internal tools).
150  void SetUpgradeMode();
151
152  // Sets the eviction algorithm to version 2.
153  void SetNewEviction();
154
155  // Sets an explicit set of BackendFlags.
156  void SetFlags(uint32 flags);
157
158  // Sends a dummy operation through the operation queue, for unit tests.
159  int FlushQueueForTest(const CompletionCallback& callback);
160
161  // Trims an entry (all if |empty| is true) from the list of deleted
162  // entries. This method should be called directly on the cache thread.
163  void TrimForTest(bool empty);
164
165  // Trims an entry (all if |empty| is true) from the list of deleted
166  // entries. This method should be called directly on the cache thread.
167  void TrimDeletedListForTest(bool empty);
168
169  // Performs a simple self-check, and returns the number of dirty items
170  // or an error code (negative value).
171  int SelfCheck();
172
173  // Backend implementation.
174  virtual net::CacheType GetCacheType() const OVERRIDE;
175  virtual int32 GetEntryCount() const OVERRIDE;
176  virtual int OpenEntry(const std::string& key, Entry** entry,
177                        const CompletionCallback& callback) OVERRIDE;
178  virtual int CreateEntry(const std::string& key, Entry** entry,
179                          const CompletionCallback& callback) OVERRIDE;
180  virtual int DoomEntry(const std::string& key,
181                        const CompletionCallback& callback) OVERRIDE;
182  virtual int DoomAllEntries(const CompletionCallback& callback) OVERRIDE;
183  virtual int DoomEntriesBetween(base::Time initial_time,
184                                 base::Time end_time,
185                                 const CompletionCallback& callback) OVERRIDE;
186  virtual int DoomEntriesSince(base::Time initial_time,
187                               const CompletionCallback& callback) OVERRIDE;
188  virtual int OpenNextEntry(void** iter, Entry** next_entry,
189                            const CompletionCallback& callback) OVERRIDE;
190  virtual void EndEnumeration(void** iter) OVERRIDE;
191  virtual void GetStats(StatsItems* stats) OVERRIDE;
192  virtual void OnExternalCacheHit(const std::string& key) OVERRIDE;
193
194 private:
195  friend class EvictionV3;
196  typedef base::hash_map<CacheAddr, EntryImplV3*> EntriesMap;
197
198  void AdjustMaxCacheSize();
199  bool InitStats(void* stats_data);
200  void StoreStats();
201
202  // Deletes the cache and starts again.
203  void RestartCache(bool failure);
204  void PrepareForRestart();
205
206  // Performs final cleanup.
207  void CleanupCache();
208
209  // Creates a new entry object. Returns zero on success, or a disk_cache error
210  // on failure.
211  int NewEntry(Addr address, EntryImplV3** entry);
212
213  // Opens the next or previous entry on a cache iteration.
214  int OpenFollowingEntry(bool forward, void** iter, Entry** next_entry,
215                         const CompletionCallback& callback);
216
217  // Handles the used storage count.
218  void AddStorageSize(int32 bytes);
219  void SubstractStorageSize(int32 bytes);
220
221  // Update the number of referenced cache entries.
222  void IncreaseNumRefs();
223  void DecreaseNumRefs();
224  void IncreaseNumEntries();
225  void DecreaseNumEntries();
226
227  // Dumps current cache statistics to the log.
228  void LogStats();
229
230  // Send UMA stats.
231  void ReportStats();
232
233  // Reports an uncommon, recoverable error.
234  void ReportError(int error);
235
236  // Performs basic checks on the index file. Returns false on failure.
237  bool CheckIndex();
238
239  // Part of the self test. Returns the number or dirty entries, or an error.
240  int CheckAllEntries();
241
242  // Part of the self test. Returns false if the entry is corrupt.
243  bool CheckEntry(EntryImplV3* cache_entry);
244
245  // Returns the maximum total memory for the memory buffers.
246  int MaxBuffersSize();
247
248  IndexTable index_;
249  base::FilePath path_;  // Path to the folder used as backing storage.
250  BlockBitmaps block_files_;
251  int32 max_size_;  // Maximum data size for this instance.
252  EvictionV3 eviction_;  // Handler of the eviction algorithm.
253  EntriesMap open_entries_;
254  int num_refs_;  // Number of referenced cache entries.
255  int max_refs_;  // Max number of referenced cache entries.
256  int entry_count_;  // Number of entries accessed lately.
257  int byte_count_;  // Number of bytes read/written lately.
258  int buffer_bytes_;  // Total size of the temporary entries' buffers.
259  int up_ticks_;  // The number of timer ticks received (OnTimerTick).
260  net::CacheType cache_type_;
261  int uma_report_;  // Controls transmission of UMA data.
262  uint32 user_flags_;  // Flags set by the user.
263  bool init_;  // controls the initialization of the system.
264  bool restarted_;
265  bool read_only_;  // Prevents updates of the rankings data (used by tools).
266  bool disabled_;
267  bool lru_eviction_;  // What eviction algorithm should be used.
268  bool first_timer_;  // True if the timer has not been called.
269  bool user_load_;  // True if we see a high load coming from the caller.
270
271  net::NetLog* net_log_;
272
273  Stats stats_;  // Usage statistics.
274  scoped_ptr<base::RepeatingTimer<BackendImplV3> > timer_;  // Usage timer.
275  scoped_refptr<TraceObject> trace_object_;  // Initializes internal tracing.
276  base::WeakPtrFactory<BackendImplV3> ptr_factory_;
277
278  DISALLOW_COPY_AND_ASSIGN(BackendImplV3);
279};
280
281}  // namespace disk_cache
282
283#endif  // NET_DISK_CACHE_BLOCKFILE_BACKEND_IMPL_V3_H_
284