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 ~BackendImplV3() override
;
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 net::CacheType
GetCacheType() const override
;
176 int32
GetEntryCount() const override
;
177 int OpenEntry(const std::string
& key
,
179 const CompletionCallback
& callback
) override
;
180 int CreateEntry(const std::string
& key
,
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
,
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
;
196 friend class EvictionV3
;
197 typedef base::hash_map
<CacheAddr
, EntryImplV3
*> EntriesMap
;
199 class NotImplementedIterator
;
202 void AdjustMaxCacheSize();
203 bool InitStats(void* stats_data
);
206 // Deletes the cache and starts again.
207 void RestartCache(bool failure
);
208 void PrepareForRestart();
210 // Performs final cleanup.
213 // Creates a new entry object. Returns zero on success, or a disk_cache error
215 int NewEntry(Addr address
, EntryImplV3
** entry
);
217 // Handles the used storage count.
218 void AddStorageSize(int32 bytes
);
219 void SubstractStorageSize(int32 bytes
);
221 // Update the number of referenced cache entries.
222 void IncreaseNumRefs();
223 void DecreaseNumRefs();
224 void IncreaseNumEntries();
225 void DecreaseNumEntries();
227 // Dumps current cache statistics to the log.
233 // Reports an uncommon, recoverable error.
234 void ReportError(int error
);
236 // Performs basic checks on the index file. Returns false on failure.
239 // Part of the self test. Returns the number or dirty entries, or an error.
240 int CheckAllEntries();
242 // Part of the self test. Returns false if the entry is corrupt.
243 bool CheckEntry(EntryImplV3
* cache_entry
);
245 // Returns the maximum total memory for the memory buffers.
246 int MaxBuffersSize();
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.
265 bool read_only_
; // Prevents updates of the rankings data (used by tools).
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.
271 net::NetLog
* net_log_
;
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_
;
278 DISALLOW_COPY_AND_ASSIGN(BackendImplV3
);
281 } // namespace disk_cache
283 #endif // NET_DISK_CACHE_BLOCKFILE_BACKEND_IMPL_V3_H_