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