Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(459)

Side by Side Diff: net/disk_cache/v3/backend_impl_v3.h

Issue 15203004: Disk cache: Reference CL for the implementation of file format version 3. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src/
Patch Set: Created 7 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « net/disk_cache/storage_block_unittest.cc ('k') | net/disk_cache/v3/backend_impl_v3.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Property Changes:
Added: svn:eol-style
+ LF
OLDNEW
(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_V3_BACKEND_IMPL_V3_H_
8 #define NET_DISK_CACHE_V3_BACKEND_IMPL_V3_H_
9
10 #include "base/files/file_path.h"
11 #include "base/hash_tables.h"
12 #include "base/synchronization/waitable_event.h"
13 #include "base/timer.h"
14 #include "net/disk_cache/block_files.h"
15 #include "net/disk_cache/disk_cache.h"
16 #include "net/disk_cache/stats.h"
17 #include "net/disk_cache/stress_support.h"
18 #include "net/disk_cache/trace.h"
19 #include "net/disk_cache/v3/block_bitmaps.h"
20 #include "net/disk_cache/v3/eviction_v3.h"
21 #include "net/disk_cache/v3/index_table.h"
22
23 namespace net {
24 class NetLog;
25 } // namespace net
26
27 namespace disk_cache {
28
29 class IndexTable;
30 class EntryImplV3;
31
32 // This class implements the Backend interface. An object of this
33 // class handles the operations of the cache for a particular profile.
34 class NET_EXPORT_PRIVATE BackendImplV3 : public Backend {
35 friend class EvictionV3;
36 public:
37 enum BackendFlags {
38 MAX_SIZE = 1 << 1, // A maximum size was provided.
39 UNIT_TEST_MODE = 1 << 2, // We are modifying the behavior for testing.
40 UPGRADE_MODE = 1 << 3, // This is the upgrade tool (dump).
41 EVICTION_V2 = 1 << 4, // Use of new eviction was specified.
42 BASIC_UNIT_TEST = 1 << 5, // Identifies almost all unit tests.
43 NO_LOAD_PROTECTION = 1 << 6, // Don't act conservatively under load.
44 NO_BUFFERING = 1 << 7, // Disable extended IO buffering.
45 NO_CLEAN_ON_EXIT // Avoid saving data at exit time.
46 };
47
48 BackendImplV3(const base::FilePath& path, base::MessageLoopProxy* cache_thread ,
49 net::NetLog* net_log);
50 virtual ~BackendImplV3();
51
52 // Performs general initialization for this current instance of the cache.
53 int Init(const CompletionCallback& callback);
54
55 // Same behavior as OpenNextEntry but walks the list from back to front.
56 int OpenPrevEntry(void** iter, Entry** prev_entry,
57 const CompletionCallback& callback);
58
59 // Sets the maximum size for the total amount of data stored by this instance.
60 bool SetMaxSize(int max_bytes);
61
62 // Sets the cache type for this backend.
63 void SetType(net::CacheType type);
64
65 // Creates a new storage block of size block_count.
66 bool CreateBlock(FileType block_type, int block_count,
67 Addr* block_address);
68
69 // Updates the ranking information for an entry.
70 void UpdateRank(EntryImplV3* entry, bool modified);
71
72 // Permanently deletes an entry, but still keeps track of it.
73 void InternalDoomEntry(EntryImplV3* entry);
74
75 // This method must be called when an entry is released for the last time, so
76 // the entry should not be used anymore. |address| is the cache address of the
77 // entry.
78 void OnEntryDestroyBegin(Addr address);
79
80 // This method must be called after all resources for an entry have been
81 // released.
82 void OnEntryDestroyEnd();
83
84 // Methods that perform file IO. Each method result in a task posted to the
85 // cache thread, with the |callback| invoked when the operation completes.
86 // Any error would result in the |entry| being automatically deleted. Note
87 // that these methods do not operate directly on entries, but rather on the
88 // |address| provided. For example, Delete() deletes the data pointed by
89 // |address|, it doesn't delete |entry|.
90 void ReadData(EntryImplV3* entry, Addr address, int offset,
91 net::IOBuffer* buffer, int buffer_len,
92 const CompletionCallback& callback);
93 void WriteData(EntryImplV3* entry, Addr address, int offset,
94 net::IOBuffer* buffer, int buffer_len,
95 const CompletionCallback& callback);
96 void MoveData(EntryImplV3* entry, Addr source, Addr destination, int len,
97 const CompletionCallback& callback);
98 void Truncate(EntryImplV3* entry, Addr address, int offset);
99 void Delete(EntryImplV3* entry, Addr address);
100 void Close(EntryImplV3* entry, Addr address);
101
102 // Delete or fix a given |cell| from the index table.
103 void DeleteCell(IndexCell* cell, int32 cell_id);
104 void FixCell(IndexCell* cell, int32 cell_id);
105
106 // Evicts the entry located at |address|, with the given |hash|. Returns true
107 // if the entry can be evicted and the caller should expect a completion
108 // notification, false otherwise.
109 // This method is used by the eviction module, and OnEvictEntryComplete() will
110 // be called to notify the end of the operation.
111 bool EvictEntry(uint32 hash, Addr address);
112
113 // If the |address| corresponds to an open entry, returns a pointer to that
114 // entry, otherwise returns NULL. Note that this method increases the ref
115 // counter for the entry.
116 EntryImplV3* GetOpenEntry(Addr address) const;
117
118 // Returns the maximum size for a file to reside on the cache.
119 int MaxFileSize() const;
120
121 // A user data block is being created, extended or truncated.
122 void ModifyStorageSize(int32 old_size, int32 new_size);
123
124 // Logs requests that are denied due to being too big.
125 void TooMuchStorageRequested(int32 size);
126
127 // Returns true if a temporary buffer is allowed to be extended.
128 bool IsAllocAllowed(int current_size, int new_size, bool force);
129
130 // Tracks the release of |size| bytes by an entry buffer.
131 void BufferDeleted(int size);
132
133 // Only intended for testing the two previous methods.
134 int GetTotalBuffersSize() const {
135 return buffer_bytes_;
136 }
137
138 // Returns true if this instance seems to be under heavy load.
139 bool IsLoaded() const;
140
141 // Returns the current time. For tests to run properly, use this method
142 // instead of calling base::Time::Now() directly.
143 base::Time GetCurrentTime() const;
144
145 // Returns the full histogram name, for the given base |name| and experiment,
146 // and the current cache type. The name will be "DiskCache.t.name_e" where n
147 // is the cache type and e the provided |experiment|.
148 std::string HistogramName(const char* name, int experiment) const;
149
150 net::CacheType cache_type() const {
151 return cache_type_;
152 }
153
154 bool read_only() const {
155 return read_only_;
156 }
157
158 // Returns a weak pointer to this object.
159 base::WeakPtr<BackendImplV3> GetWeakPtr();
160
161 // Returns true if we should send histograms for this user again. The caller
162 // must call this function only once per run (because it returns always the
163 // same thing on a given run).
164 bool ShouldReportAgain();
165
166 // Reports some data when we filled up the cache.
167 void FirstEviction();
168
169 // Called when an interesting event should be logged (counted).
170 void OnEvent(Stats::Counters an_event);
171
172 // Keeps track of payload access (doesn't include metadata).
173 void OnRead(int bytes);
174 void OnWrite(int bytes);
175
176 // Increases the size of the index file.
177 void GrowIndex();
178
179 // Increases the size of the block files.
180 void GrowBlockFiles();
181
182 // Saved the index to the backup file.
183 void SaveIndex(net::IOBuffer* buffer, int buffer_len);
184
185 // Timer callback to calculate usage statistics and perform backups.
186 void OnTimerTick();
187
188 // Sets internal parameters to enable unit testing mode.
189 void SetUnitTestMode();
190
191 // Sets internal parameters to enable upgrade mode (for internal tools).
192 void SetUpgradeMode();
193
194 // Sets the eviction algorithm to version 2.
195 void SetNewEviction();
196
197 // Sets an explicit set of BackendFlags.
198 void SetFlags(uint32 flags);
199
200 // Sends a dummy operation through the operation queue, for unit tests.
201 int FlushQueueForTest(const CompletionCallback& callback);
202
203 // Performs final cleanup, for unit tests.
204 int CleanupForTest(const CompletionCallback& callback);
205
206 // Trims an entry (all if |empty| is true) from the list of deleted
207 // entries. This method should be called directly on the cache thread.
208 void TrimForTest(bool empty);
209
210 // Trims an entry (all if |empty| is true) from the list of deleted
211 // entries. This method should be called directly on the cache thread.
212 void TrimDeletedListForTest(bool empty);
213
214 // Simulates that aditional |seconds| have elapsed, for testing purposes.
215 void AddDelayForTest(int seconds);
216
217 // Returns a net error code to inform the caller when the entry with the
218 // desired |key| is completely closed.
219 int WaitForEntryToCloseForTest(const std::string& key,
220 const CompletionCallback& callback);
221
222 // Performs a simple self-check, and returns the number of dirty items
223 // or an error code (negative value).
224 int SelfCheck();
225
226 // Backend implementation.
227 virtual net::CacheType GetCacheType() const OVERRIDE;
228 virtual int32 GetEntryCount() const OVERRIDE;
229 virtual int OpenEntry(const std::string& key, Entry** entry,
230 const CompletionCallback& callback) OVERRIDE;
231 virtual int CreateEntry(const std::string& key, Entry** entry,
232 const CompletionCallback& callback) OVERRIDE;
233 virtual int DoomEntry(const std::string& key,
234 const CompletionCallback& callback) OVERRIDE;
235 virtual int DoomAllEntries(const CompletionCallback& callback) OVERRIDE;
236 virtual int DoomEntriesBetween(base::Time initial_time,
237 base::Time end_time,
238 const CompletionCallback& callback) OVERRIDE;
239 virtual int DoomEntriesSince(base::Time initial_time,
240 const CompletionCallback& callback) OVERRIDE;
241 virtual int OpenNextEntry(void** iter, Entry** next_entry,
242 const CompletionCallback& callback) OVERRIDE;
243 virtual void EndEnumeration(void** iter) OVERRIDE;
244 virtual void GetStats(StatsItems* stats) OVERRIDE;
245 virtual void OnExternalCacheHit(const std::string& key) OVERRIDE;
246
247 private:
248 typedef base::hash_map<CacheAddr, EntryImplV3*> EntriesMap;
249 class IOCallback;
250 class Worker;
251 class WorkItem;
252
253 void AdjustMaxCacheSize();
254 bool InitStats(void* stats_data);
255 void StoreStats();
256
257 // Deletes the cache and starts again.
258 void RestartCache(const CompletionCallback& callback);
259 void PrepareForRestart();
260
261 // Performs final cleanup.
262 void CleanupCache();
263
264 // Creates a new entry object. Returns zero on success, or a disk_cache error
265 // on failure.
266 int NewEntry(WorkItem* work_item, EntryImplV3** entry);
267
268 // Scans |entries| and the list of open entries looking for a match. Returns
269 // NULL if no match is fouond.
270 EntryImplV3* LookupOpenEntry(const EntrySet& entries, const std::string key);
271
272 // Opens the next or previous entry on a cache iteration.
273 int OpenFollowingEntry(bool forward, void** iter, Entry** next_entry,
274 const CompletionCallback& callback);
275
276 // Continue an enumeration by getting more cells or the next entry to open.
277 bool GetMoreCells(WorkItem* work_item);
278 bool OpenNext(WorkItem* work_item);
279
280 // Doom |entry| if directed by the work_item.
281 void Doom(EntryImplV3* entry, WorkItem* work_item);
282
283 // Update the user visible iterator an tell the user about it.
284 void UpdateIterator(EntryImplV3* entry, WorkItem* work_item);
285
286 // Handles the used storage count.
287 void AddStorageSize(int32 bytes);
288 void SubstractStorageSize(int32 bytes);
289
290 // Update the number of referenced cache entries.
291 void IncreaseNumRefs();
292 void DecreaseNumRefs();
293 void IncreaseNumEntries();
294 void DecreaseNumEntries();
295
296 // Methods to post and receive notifications about tasks executed on the
297 // cache thread.
298 void PostWorkItem(WorkItem* work_item);
299 void OnWorkDone(WorkItem* work_item);
300 void OnInitComplete(WorkItem* work_item);
301 void OnGrowIndexComplete(WorkItem* work_item);
302 void OnGrowFilesComplete(WorkItem* work_item);
303 void OnOperationComplete(WorkItem* work_item);
304 void OnOpenEntryComplete(WorkItem* work_item);
305 void OnOpenForResurrectComplete(WorkItem* work_item);
306 void OnEvictEntryComplete(WorkItem* work_item);
307 void OnOpenNextComplete(WorkItem* work_item);
308
309 // Last part of CreateEntry(). |short_record| may point to stored data about
310 // a previously evicted entry matching this |key|.
311 int OnCreateEntryComplete(const std::string& key, uint32 hash,
312 ShortEntryRecord* short_record, Entry** entry,
313 const CompletionCallback& callback);
314
315 // Dumps current cache statistics to the log.
316 void LogStats();
317
318 // Send UMA stats.
319 void ReportStats();
320
321 // Reports an uncommon, recoverable error.
322 void ReportError(int error);
323
324 // Performs basic checks on the index file. Returns false on failure.
325 bool CheckIndex();
326
327 // Part of the self test. Returns the number or dirty entries, or an error.
328 int CheckAllEntries();
329
330 // Part of the self test. Returns false if the entry is corrupt.
331 bool CheckEntry(EntryImplV3* cache_entry);
332
333 // Returns the maximum total memory for the memory buffers.
334 int MaxBuffersSize();
335
336 IndexTable index_;
337 base::FilePath path_; // Path to the folder used as backing storage.
338 BlockBitmaps block_files_;
339 int32 max_size_; // Maximum data size for this instance.
340 EvictionV3 eviction_; // Handler of the eviction algorithm.
341 EntriesMap open_entries_; // Map of open entries.
342 int num_refs_; // Number of referenced cache entries.
343 int max_refs_; // Max number of referenced cache entries.
344 int entry_count_; // Number of entries accessed lately.
345 int byte_count_; // Number of bytes read/written lately.
346 int buffer_bytes_; // Total size of the temporary entries' buffers.
347 int up_ticks_; // The number of timer ticks received (OnTimerTick).
348 int test_seconds_; // The "current time" for tests.
349 net::CacheType cache_type_;
350 int uma_report_; // Controls transmission of UMA data.
351 uint32 user_flags_; // Flags set by the user.
352 bool init_; // controls the initialization of the system.
353 bool restarted_;
354 bool read_only_; // Prevents updates of the rankings data (used by tools).
355 bool disabled_;
356 bool lru_eviction_; // What eviction algorithm should be used.
357 bool first_timer_; // True if the timer has not been called.
358 bool user_load_; // True if we see a high load coming from the caller.
359 bool growing_index_;
360 bool growing_files_;
361
362 net::NetLog* net_log_;
363
364 Stats stats_; // Usage statistics.
365 scoped_ptr<base::RepeatingTimer<BackendImplV3> > timer_; // Usage timer.
366 scoped_refptr<TraceObject> trace_object_; // Initializes internal tracing.
367 scoped_refptr<base::MessageLoopProxy> cache_thread_;
368 scoped_refptr<Worker> worker_;
369 base::WeakPtrFactory<BackendImplV3> ptr_factory_;
370
371 DISALLOW_COPY_AND_ASSIGN(BackendImplV3);
372 };
373
374 } // namespace disk_cache
375
376 #endif // NET_DISK_CACHE_V3_BACKEND_IMPL_V3_H_
OLDNEW
« no previous file with comments | « net/disk_cache/storage_block_unittest.cc ('k') | net/disk_cache/v3/backend_impl_v3.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698