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