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/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"
26 namespace disk_cache
{
30 // This class implements the Backend interface. An object of this
31 // class handles the operations of the cache for a particular profile.
32 class NET_EXPORT_PRIVATE BackendImplV3
: public Backend
{
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.
45 BackendImplV3(const base::FilePath
& path
,
46 base::MessageLoopProxy
* cache_thread
,
47 net::NetLog
* net_log
);
48 virtual ~BackendImplV3();
50 // Performs general initialization for this current instance of the cache.
51 int Init(const CompletionCallback
& callback
);
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
);
57 // Sets the maximum size for the total amount of data stored by this instance.
58 bool SetMaxSize(int max_bytes
);
60 // Sets the cache type for this backend.
61 void SetType(net::CacheType type
);
63 // Creates a new storage block of size block_count.
64 bool CreateBlock(FileType block_type
, int block_count
,
67 // Updates the ranking information for an entry.
68 void UpdateRank(EntryImplV3
* entry
, bool modified
);
70 // Permanently deletes an entry, but still keeps track of it.
71 void InternalDoomEntry(EntryImplV3
* entry
);
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
76 void OnEntryDestroyBegin(Addr address
);
78 // This method must be called after all resources for an entry have been
80 void OnEntryDestroyEnd();
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;
87 // Returns the id being used on this run of the cache.
88 int32
GetCurrentEntryId() const;
90 // Returns the maximum size for a file to reside on the cache.
91 int MaxFileSize() const;
93 // A user data block is being created, extended or truncated.
94 void ModifyStorageSize(int32 old_size
, int32 new_size
);
96 // Logs requests that are denied due to being too big.
97 void TooMuchStorageRequested(int32 size
);
99 // Returns true if a temporary buffer is allowed to be extended.
100 bool IsAllocAllowed(int current_size
, int new_size
);
102 // Tracks the release of |size| bytes by an entry buffer.
103 void BufferDeleted(int size
);
105 // Only intended for testing the two previous methods.
106 int GetTotalBuffersSize() const {
107 return buffer_bytes_
;
110 // Returns true if this instance seems to be under heavy load.
111 bool IsLoaded() const;
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;
117 net::CacheType
cache_type() const {
121 bool read_only() const {
125 // Returns a weak pointer to this object.
126 base::WeakPtr
<BackendImplV3
> GetWeakPtr();
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();
133 // Reports some data when we filled up the cache.
134 void FirstEviction();
136 // Called when an interesting event should be logged (counted).
137 void OnEvent(Stats::Counters an_event
);
139 // Keeps track of payload access (doesn't include metadata).
140 void OnRead(int bytes
);
141 void OnWrite(int bytes
);
143 // Timer callback to calculate usage statistics and perform backups.
146 // Sets internal parameters to enable unit testing mode.
147 void SetUnitTestMode();
149 // Sets internal parameters to enable upgrade mode (for internal tools).
150 void SetUpgradeMode();
152 // Sets the eviction algorithm to version 2.
153 void SetNewEviction();
155 // Sets an explicit set of BackendFlags.
156 void SetFlags(uint32 flags
);
158 // Sends a dummy operation through the operation queue, for unit tests.
159 int FlushQueueForTest(const CompletionCallback
& callback
);
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
);
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
);
169 // Performs a simple self-check, and returns the number of dirty items
170 // or an error code (negative value).
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
,
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
;
195 friend class EvictionV3
;
196 typedef base::hash_map
<CacheAddr
, EntryImplV3
*> EntriesMap
;
199 void AdjustMaxCacheSize();
200 bool InitStats(void* stats_data
);
203 // Deletes the cache and starts again.
204 void RestartCache(bool failure
);
205 void PrepareForRestart();
207 // Performs final cleanup.
210 // Creates a new entry object. Returns zero on success, or a disk_cache error
212 int NewEntry(Addr address
, EntryImplV3
** entry
);
214 // Opens the next or previous entry on a cache iteration.
215 int OpenFollowingEntry(bool forward
, void** iter
, Entry
** next_entry
,
216 const CompletionCallback
& callback
);
218 // Handles the used storage count.
219 void AddStorageSize(int32 bytes
);
220 void SubstractStorageSize(int32 bytes
);
222 // Update the number of referenced cache entries.
223 void IncreaseNumRefs();
224 void DecreaseNumRefs();
225 void IncreaseNumEntries();
226 void DecreaseNumEntries();
228 // Dumps current cache statistics to the log.
234 // Reports an uncommon, recoverable error.
235 void ReportError(int error
);
237 // Performs basic checks on the index file. Returns false on failure.
240 // Part of the self test. Returns the number or dirty entries, or an error.
241 int CheckAllEntries();
243 // Part of the self test. Returns false if the entry is corrupt.
244 bool CheckEntry(EntryImplV3
* cache_entry
);
246 // Returns the maximum total memory for the memory buffers.
247 int MaxBuffersSize();
250 base::FilePath path_
; // Path to the folder used as backing storage.
251 BlockBitmaps block_files_
;
252 int32 max_size_
; // Maximum data size for this instance.
253 EvictionV3 eviction_
; // Handler of the eviction algorithm.
254 EntriesMap open_entries_
;
255 int num_refs_
; // Number of referenced cache entries.
256 int max_refs_
; // Max number of referenced cache entries.
257 int entry_count_
; // Number of entries accessed lately.
258 int byte_count_
; // Number of bytes read/written lately.
259 int buffer_bytes_
; // Total size of the temporary entries' buffers.
260 int up_ticks_
; // The number of timer ticks received (OnTimerTick).
261 net::CacheType cache_type_
;
262 int uma_report_
; // Controls transmission of UMA data.
263 uint32 user_flags_
; // Flags set by the user.
264 bool init_
; // controls the initialization of the system.
266 bool read_only_
; // Prevents updates of the rankings data (used by tools).
268 bool lru_eviction_
; // What eviction algorithm should be used.
269 bool first_timer_
; // True if the timer has not been called.
270 bool user_load_
; // True if we see a high load coming from the caller.
272 net::NetLog
* net_log_
;
274 Stats stats_
; // Usage statistics.
275 scoped_ptr
<base::RepeatingTimer
<BackendImplV3
> > timer_
; // Usage timer.
276 scoped_refptr
<TraceObject
> trace_object_
; // Initializes internal tracing.
277 base::WeakPtrFactory
<BackendImplV3
> ptr_factory_
;
279 DISALLOW_COPY_AND_ASSIGN(BackendImplV3
);
282 } // namespace disk_cache
284 #endif // NET_DISK_CACHE_BLOCKFILE_BACKEND_IMPL_V3_H_