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_V3_H_
8 #define NET_DISK_CACHE_BLOCKFILE_BACKEND_IMPL_V3_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_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"
24 class SingleThreadTaskRunner;
31 namespace disk_cache {
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 {
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.
50 BackendImplV3(const base::FilePath& path,
51 const scoped_refptr<base::SingleThreadTaskRunner>& cache_thread,
52 net::NetLog* net_log);
53 virtual ~BackendImplV3();
55 // Performs general initialization for this current instance of the cache.
56 int Init(const CompletionCallback& callback);
58 // Sets the maximum size for the total amount of data stored by this instance.
59 bool SetMaxSize(int max_bytes);
61 // Sets the cache type for this backend.
62 void SetType(net::CacheType type);
64 // Creates a new storage block of size block_count.
65 bool CreateBlock(FileType block_type, int block_count,
68 // Updates the ranking information for an entry.
69 void UpdateRank(EntryImplV3* entry, bool modified);
71 // Permanently deletes an entry, but still keeps track of it.
72 void InternalDoomEntry(EntryImplV3* entry);
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
77 void OnEntryDestroyBegin(Addr address);
79 // This method must be called after all resources for an entry have been
81 void OnEntryDestroyEnd();
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;
88 // Returns the id being used on this run of the cache.
89 int32 GetCurrentEntryId() const;
91 // Returns the maximum size for a file to reside on the cache.
92 int MaxFileSize() const;
94 // A user data block is being created, extended or truncated.
95 void ModifyStorageSize(int32 old_size, int32 new_size);
97 // Logs requests that are denied due to being too big.
98 void TooMuchStorageRequested(int32 size);
100 // Returns true if a temporary buffer is allowed to be extended.
101 bool IsAllocAllowed(int current_size, int new_size);
103 // Tracks the release of |size| bytes by an entry buffer.
104 void BufferDeleted(int size);
106 // Only intended for testing the two previous methods.
107 int GetTotalBuffersSize() const {
108 return buffer_bytes_;
111 // Returns true if this instance seems to be under heavy load.
112 bool IsLoaded() const;
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;
118 net::CacheType cache_type() const {
122 bool read_only() const {
126 // Returns a weak pointer to this object.
127 base::WeakPtr<BackendImplV3> GetWeakPtr();
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();
134 // Reports some data when we filled up the cache.
135 void FirstEviction();
137 // Called when an interesting event should be logged (counted).
138 void OnEvent(Stats::Counters an_event);
140 // Keeps track of payload access (doesn't include metadata).
141 void OnRead(int bytes);
142 void OnWrite(int bytes);
144 // Timer callback to calculate usage statistics and perform backups.
147 // Sets internal parameters to enable unit testing mode.
148 void SetUnitTestMode();
150 // Sets internal parameters to enable upgrade mode (for internal tools).
151 void SetUpgradeMode();
153 // Sets the eviction algorithm to version 2.
154 void SetNewEviction();
156 // Sets an explicit set of BackendFlags.
157 void SetFlags(uint32 flags);
159 // Sends a dummy operation through the operation queue, for unit tests.
160 int FlushQueueForTest(const CompletionCallback& callback);
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);
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);
170 // Performs a simple self-check, and returns the number of dirty items
171 // or an error code (negative value).
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,
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;
194 friend class EvictionV3;
195 typedef base::hash_map<CacheAddr, EntryImplV3*> EntriesMap;
197 class NotImplementedIterator;
200 void AdjustMaxCacheSize();
201 bool InitStats(void* stats_data);
204 // Deletes the cache and starts again.
205 void RestartCache(bool failure);
206 void PrepareForRestart();
208 // Performs final cleanup.
211 // Creates a new entry object. Returns zero on success, or a disk_cache error
213 int NewEntry(Addr address, EntryImplV3** entry);
215 // Handles the used storage count.
216 void AddStorageSize(int32 bytes);
217 void SubstractStorageSize(int32 bytes);
219 // Update the number of referenced cache entries.
220 void IncreaseNumRefs();
221 void DecreaseNumRefs();
222 void IncreaseNumEntries();
223 void DecreaseNumEntries();
225 // Dumps current cache statistics to the log.
231 // Reports an uncommon, recoverable error.
232 void ReportError(int error);
234 // Performs basic checks on the index file. Returns false on failure.
237 // Part of the self test. Returns the number or dirty entries, or an error.
238 int CheckAllEntries();
240 // Part of the self test. Returns false if the entry is corrupt.
241 bool CheckEntry(EntryImplV3* cache_entry);
243 // Returns the maximum total memory for the memory buffers.
244 int MaxBuffersSize();
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.
263 bool read_only_; // Prevents updates of the rankings data (used by tools).
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.
269 net::NetLog* net_log_;
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_;
276 DISALLOW_COPY_AND_ASSIGN(BackendImplV3);
279 } // namespace disk_cache
281 #endif // NET_DISK_CACHE_BLOCKFILE_BACKEND_IMPL_V3_H_