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