Update To 11.40.268.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   ~BackendImplV3() override;
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   net::CacheType GetCacheType() const override;
176   int32 GetEntryCount() const override;
177   int OpenEntry(const std::string& key,
178                 Entry** entry,
179                 const CompletionCallback& callback) override;
180   int CreateEntry(const std::string& key,
181                   Entry** entry,
182                   const CompletionCallback& callback) override;
183   int DoomEntry(const std::string& key,
184                 const CompletionCallback& callback) override;
185   int DoomAllEntries(const CompletionCallback& callback) override;
186   int DoomEntriesBetween(base::Time initial_time,
187                          base::Time end_time,
188                          const CompletionCallback& callback) override;
189   int DoomEntriesSince(base::Time initial_time,
190                        const CompletionCallback& callback) override;
191   scoped_ptr<Iterator> CreateIterator() override;
192   void GetStats(StatsItems* stats) override;
193   void OnExternalCacheHit(const std::string& key) override;
194
195  private:
196   friend class EvictionV3;
197   typedef base::hash_map<CacheAddr, EntryImplV3*> EntriesMap;
198   class IteratorImpl;
199   class NotImplementedIterator;
200   class Worker;
201
202   void AdjustMaxCacheSize();
203   bool InitStats(void* stats_data);
204   void StoreStats();
205
206   // Deletes the cache and starts again.
207   void RestartCache(bool failure);
208   void PrepareForRestart();
209
210   // Performs final cleanup.
211   void CleanupCache();
212
213   // Creates a new entry object. Returns zero on success, or a disk_cache error
214   // on failure.
215   int NewEntry(Addr address, EntryImplV3** entry);
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_