| 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_H_ | |
| 8 #define NET_DISK_CACHE_BLOCKFILE_BACKEND_IMPL_H_ | |
| 9 | |
| 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_files.h" | |
| 15 #include "net/disk_cache/blockfile/eviction.h" | |
| 16 #include "net/disk_cache/blockfile/in_flight_backend_io.h" | |
| 17 #include "net/disk_cache/blockfile/rankings.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" | |
| 22 | |
| 23 namespace base { | |
| 24 class SingleThreadTaskRunner; | |
| 25 } // namespace base | |
| 26 | |
| 27 namespace net { | |
| 28 class NetLog; | |
| 29 } // namespace net | |
| 30 | |
| 31 namespace disk_cache { | |
| 32 | |
| 33 struct Index; | |
| 34 | |
| 35 enum BackendFlags { | |
| 36 kNone = 0, | |
| 37 kMask = 1, // A mask (for the index table) was specified. | |
| 38 kMaxSize = 1 << 1, // A maximum size was provided. | |
| 39 kUnitTestMode = 1 << 2, // We are modifying the behavior for testing. | |
| 40 kUpgradeMode = 1 << 3, // This is the upgrade tool (dump). | |
| 41 kNewEviction = 1 << 4, // Use of new eviction was specified. | |
| 42 kNoRandom = 1 << 5, // Don't add randomness to the behavior. | |
| 43 kNoLoadProtection = 1 << 6, // Don't act conservatively under load. | |
| 44 kNoBuffering = 1 << 7 // Disable extended IO buffering. | |
| 45 }; | |
| 46 | |
| 47 // This class implements the Backend interface. An object of this | |
| 48 // class handles the operations of the cache for a particular profile. | |
| 49 class NET_EXPORT_PRIVATE BackendImpl : public Backend { | |
| 50 friend class Eviction; | |
| 51 public: | |
| 52 BackendImpl(const base::FilePath& path, | |
| 53 const scoped_refptr<base::SingleThreadTaskRunner>& cache_thread, | |
| 54 net::NetLog* net_log); | |
| 55 // mask can be used to limit the usable size of the hash table, for testing. | |
| 56 BackendImpl(const base::FilePath& path, | |
| 57 uint32 mask, | |
| 58 const scoped_refptr<base::SingleThreadTaskRunner>& cache_thread, | |
| 59 net::NetLog* net_log); | |
| 60 ~BackendImpl() override; | |
| 61 | |
| 62 // Performs general initialization for this current instance of the cache. | |
| 63 int Init(const CompletionCallback& callback); | |
| 64 | |
| 65 // Performs the actual initialization and final cleanup on destruction. | |
| 66 int SyncInit(); | |
| 67 void CleanupCache(); | |
| 68 | |
| 69 // Synchronous implementation of the asynchronous interface. | |
| 70 int SyncOpenEntry(const std::string& key, Entry** entry); | |
| 71 int SyncCreateEntry(const std::string& key, Entry** entry); | |
| 72 int SyncDoomEntry(const std::string& key); | |
| 73 int SyncDoomAllEntries(); | |
| 74 int SyncDoomEntriesBetween(base::Time initial_time, | |
| 75 base::Time end_time); | |
| 76 int SyncDoomEntriesSince(base::Time initial_time); | |
| 77 int SyncOpenNextEntry(Rankings::Iterator* iterator, Entry** next_entry); | |
| 78 void SyncEndEnumeration(scoped_ptr<Rankings::Iterator> iterator); | |
| 79 void SyncOnExternalCacheHit(const std::string& key); | |
| 80 | |
| 81 // Open or create an entry for the given |key| or |iter|. | |
| 82 EntryImpl* OpenEntryImpl(const std::string& key); | |
| 83 EntryImpl* CreateEntryImpl(const std::string& key); | |
| 84 EntryImpl* OpenNextEntryImpl(Rankings::Iterator* iter); | |
| 85 | |
| 86 // Sets the maximum size for the total amount of data stored by this instance. | |
| 87 bool SetMaxSize(int max_bytes); | |
| 88 | |
| 89 // Sets the cache type for this backend. | |
| 90 void SetType(net::CacheType type); | |
| 91 | |
| 92 // Returns the full name for an external storage file. | |
| 93 base::FilePath GetFileName(Addr address) const; | |
| 94 | |
| 95 // Returns the actual file used to store a given (non-external) address. | |
| 96 MappedFile* File(Addr address); | |
| 97 | |
| 98 // Returns a weak pointer to the background queue. | |
| 99 base::WeakPtr<InFlightBackendIO> GetBackgroundQueue(); | |
| 100 | |
| 101 // Creates an external storage file. | |
| 102 bool CreateExternalFile(Addr* address); | |
| 103 | |
| 104 // Creates a new storage block of size block_count. | |
| 105 bool CreateBlock(FileType block_type, int block_count, | |
| 106 Addr* block_address); | |
| 107 | |
| 108 // Deletes a given storage block. deep set to true can be used to zero-fill | |
| 109 // the related storage in addition of releasing the related block. | |
| 110 void DeleteBlock(Addr block_address, bool deep); | |
| 111 | |
| 112 // Retrieves a pointer to the LRU-related data. | |
| 113 LruData* GetLruData(); | |
| 114 | |
| 115 // Updates the ranking information for an entry. | |
| 116 void UpdateRank(EntryImpl* entry, bool modified); | |
| 117 | |
| 118 // A node was recovered from a crash, it may not be on the index, so this | |
| 119 // method checks it and takes the appropriate action. | |
| 120 void RecoveredEntry(CacheRankingsBlock* rankings); | |
| 121 | |
| 122 // Permanently deletes an entry, but still keeps track of it. | |
| 123 void InternalDoomEntry(EntryImpl* entry); | |
| 124 | |
| 125 #if defined(NET_BUILD_STRESS_CACHE) | |
| 126 // Returns the address of the entry linked to the entry at a given |address|. | |
| 127 CacheAddr GetNextAddr(Addr address); | |
| 128 | |
| 129 // Verifies that |entry| is not currently reachable through the index. | |
| 130 void NotLinked(EntryImpl* entry); | |
| 131 #endif | |
| 132 | |
| 133 // Removes all references to this entry. | |
| 134 void RemoveEntry(EntryImpl* entry); | |
| 135 | |
| 136 // This method must be called when an entry is released for the last time, so | |
| 137 // the entry should not be used anymore. |address| is the cache address of the | |
| 138 // entry. | |
| 139 void OnEntryDestroyBegin(Addr address); | |
| 140 | |
| 141 // This method must be called after all resources for an entry have been | |
| 142 // released. | |
| 143 void OnEntryDestroyEnd(); | |
| 144 | |
| 145 // If the data stored by the provided |rankings| points to an open entry, | |
| 146 // returns a pointer to that entry, otherwise returns NULL. Note that this | |
| 147 // method does NOT increase the ref counter for the entry. | |
| 148 EntryImpl* GetOpenEntry(CacheRankingsBlock* rankings) const; | |
| 149 | |
| 150 // Returns the id being used on this run of the cache. | |
| 151 int32 GetCurrentEntryId() const; | |
| 152 | |
| 153 // Returns the maximum size for a file to reside on the cache. | |
| 154 int MaxFileSize() const; | |
| 155 | |
| 156 // A user data block is being created, extended or truncated. | |
| 157 void ModifyStorageSize(int32 old_size, int32 new_size); | |
| 158 | |
| 159 // Logs requests that are denied due to being too big. | |
| 160 void TooMuchStorageRequested(int32 size); | |
| 161 | |
| 162 // Returns true if a temporary buffer is allowed to be extended. | |
| 163 bool IsAllocAllowed(int current_size, int new_size); | |
| 164 | |
| 165 // Tracks the release of |size| bytes by an entry buffer. | |
| 166 void BufferDeleted(int size); | |
| 167 | |
| 168 // Only intended for testing the two previous methods. | |
| 169 int GetTotalBuffersSize() const { | |
| 170 return buffer_bytes_; | |
| 171 } | |
| 172 | |
| 173 // Returns true if this instance seems to be under heavy load. | |
| 174 bool IsLoaded() const; | |
| 175 | |
| 176 // Returns the full histogram name, for the given base |name| and experiment, | |
| 177 // and the current cache type. The name will be "DiskCache.t.name_e" where n | |
| 178 // is the cache type and e the provided |experiment|. | |
| 179 std::string HistogramName(const char* name, int experiment) const; | |
| 180 | |
| 181 net::CacheType cache_type() const { | |
| 182 return cache_type_; | |
| 183 } | |
| 184 | |
| 185 bool read_only() const { | |
| 186 return read_only_; | |
| 187 } | |
| 188 | |
| 189 // Returns a weak pointer to this object. | |
| 190 base::WeakPtr<BackendImpl> GetWeakPtr(); | |
| 191 | |
| 192 // Returns true if we should send histograms for this user again. The caller | |
| 193 // must call this function only once per run (because it returns always the | |
| 194 // same thing on a given run). | |
| 195 bool ShouldReportAgain(); | |
| 196 | |
| 197 // Reports some data when we filled up the cache. | |
| 198 void FirstEviction(); | |
| 199 | |
| 200 // Reports a critical error (and disables the cache). | |
| 201 void CriticalError(int error); | |
| 202 | |
| 203 // Reports an uncommon, recoverable error. | |
| 204 void ReportError(int error); | |
| 205 | |
| 206 // Called when an interesting event should be logged (counted). | |
| 207 void OnEvent(Stats::Counters an_event); | |
| 208 | |
| 209 // Keeps track of payload access (doesn't include metadata). | |
| 210 void OnRead(int bytes); | |
| 211 void OnWrite(int bytes); | |
| 212 | |
| 213 // Timer callback to calculate usage statistics. | |
| 214 void OnStatsTimer(); | |
| 215 | |
| 216 // Handles the pending asynchronous IO count. | |
| 217 void IncrementIoCount(); | |
| 218 void DecrementIoCount(); | |
| 219 | |
| 220 // Sets internal parameters to enable unit testing mode. | |
| 221 void SetUnitTestMode(); | |
| 222 | |
| 223 // Sets internal parameters to enable upgrade mode (for internal tools). | |
| 224 void SetUpgradeMode(); | |
| 225 | |
| 226 // Sets the eviction algorithm to version 2. | |
| 227 void SetNewEviction(); | |
| 228 | |
| 229 // Sets an explicit set of BackendFlags. | |
| 230 void SetFlags(uint32 flags); | |
| 231 | |
| 232 // Clears the counter of references to test handling of corruptions. | |
| 233 void ClearRefCountForTest(); | |
| 234 | |
| 235 // Sends a dummy operation through the operation queue, for unit tests. | |
| 236 int FlushQueueForTest(const CompletionCallback& callback); | |
| 237 | |
| 238 // Runs the provided task on the cache thread. The task will be automatically | |
| 239 // deleted after it runs. | |
| 240 int RunTaskForTest(const base::Closure& task, | |
| 241 const CompletionCallback& callback); | |
| 242 | |
| 243 // Trims an entry (all if |empty| is true) from the list of deleted | |
| 244 // entries. This method should be called directly on the cache thread. | |
| 245 void TrimForTest(bool empty); | |
| 246 | |
| 247 // Trims an entry (all if |empty| is true) from the list of deleted | |
| 248 // entries. This method should be called directly on the cache thread. | |
| 249 void TrimDeletedListForTest(bool empty); | |
| 250 | |
| 251 // Only intended for testing | |
| 252 base::RepeatingTimer<BackendImpl>* GetTimerForTest(); | |
| 253 | |
| 254 // Performs a simple self-check, and returns the number of dirty items | |
| 255 // or an error code (negative value). | |
| 256 int SelfCheck(); | |
| 257 | |
| 258 // Ensures the index is flushed to disk (a no-op on platforms with mmap). | |
| 259 void FlushIndex(); | |
| 260 | |
| 261 // Backend implementation. | |
| 262 net::CacheType GetCacheType() const override; | |
| 263 int32 GetEntryCount() const override; | |
| 264 int OpenEntry(const std::string& key, | |
| 265 Entry** entry, | |
| 266 const CompletionCallback& callback) override; | |
| 267 int CreateEntry(const std::string& key, | |
| 268 Entry** entry, | |
| 269 const CompletionCallback& callback) override; | |
| 270 int DoomEntry(const std::string& key, | |
| 271 const CompletionCallback& callback) override; | |
| 272 int DoomAllEntries(const CompletionCallback& callback) override; | |
| 273 int DoomEntriesBetween(base::Time initial_time, | |
| 274 base::Time end_time, | |
| 275 const CompletionCallback& callback) override; | |
| 276 int DoomEntriesSince(base::Time initial_time, | |
| 277 const CompletionCallback& callback) override; | |
| 278 // NOTE: The blockfile Backend::Iterator::OpenNextEntry method does not modify | |
| 279 // the last_used field of the entry, and therefore it does not impact the | |
| 280 // eviction ranking of the entry. However, an enumeration will go through all | |
| 281 // entries on the cache only if the cache is not modified while the | |
| 282 // enumeration is taking place. Significantly altering the entry pointed by | |
| 283 // the iterator (for example, deleting the entry) will invalidate the | |
| 284 // iterator. Performing operations on an entry that modify the entry may | |
| 285 // result in loops in the iteration, skipped entries or similar. | |
| 286 scoped_ptr<Iterator> CreateIterator() override; | |
| 287 void GetStats(StatsItems* stats) override; | |
| 288 void OnExternalCacheHit(const std::string& key) override; | |
| 289 | |
| 290 private: | |
| 291 typedef base::hash_map<CacheAddr, EntryImpl*> EntriesMap; | |
| 292 class IteratorImpl; | |
| 293 | |
| 294 // Creates a new backing file for the cache index. | |
| 295 bool CreateBackingStore(disk_cache::File* file); | |
| 296 bool InitBackingStore(bool* file_created); | |
| 297 void AdjustMaxCacheSize(int table_len); | |
| 298 | |
| 299 bool InitStats(); | |
| 300 void StoreStats(); | |
| 301 | |
| 302 // Deletes the cache and starts again. | |
| 303 void RestartCache(bool failure); | |
| 304 void PrepareForRestart(); | |
| 305 | |
| 306 // Creates a new entry object. Returns zero on success, or a disk_cache error | |
| 307 // on failure. | |
| 308 int NewEntry(Addr address, EntryImpl** entry); | |
| 309 | |
| 310 // Returns a given entry from the cache. The entry to match is determined by | |
| 311 // key and hash, and the returned entry may be the matched one or it's parent | |
| 312 // on the list of entries with the same hash (or bucket). To look for a parent | |
| 313 // of a given entry, |entry_addr| should be grabbed from that entry, so that | |
| 314 // if it doesn't match the entry on the index, we know that it was replaced | |
| 315 // with a new entry; in this case |*match_error| will be set to true and the | |
| 316 // return value will be NULL. | |
| 317 EntryImpl* MatchEntry(const std::string& key, uint32 hash, bool find_parent, | |
| 318 Addr entry_addr, bool* match_error); | |
| 319 | |
| 320 // Opens the next or previous entry on a single list. If successful, | |
| 321 // |from_entry| will be updated to point to the new entry, otherwise it will | |
| 322 // be set to NULL; in other words, it is used as an explicit iterator. | |
| 323 bool OpenFollowingEntryFromList(Rankings::List list, | |
| 324 CacheRankingsBlock** from_entry, | |
| 325 EntryImpl** next_entry); | |
| 326 | |
| 327 // Returns the entry that is pointed by |next|, from the given |list|. | |
| 328 EntryImpl* GetEnumeratedEntry(CacheRankingsBlock* next, Rankings::List list); | |
| 329 | |
| 330 // Re-opens an entry that was previously deleted. | |
| 331 EntryImpl* ResurrectEntry(EntryImpl* deleted_entry); | |
| 332 | |
| 333 void DestroyInvalidEntry(EntryImpl* entry); | |
| 334 | |
| 335 // Handles the used storage count. | |
| 336 void AddStorageSize(int32 bytes); | |
| 337 void SubstractStorageSize(int32 bytes); | |
| 338 | |
| 339 // Update the number of referenced cache entries. | |
| 340 void IncreaseNumRefs(); | |
| 341 void DecreaseNumRefs(); | |
| 342 void IncreaseNumEntries(); | |
| 343 void DecreaseNumEntries(); | |
| 344 | |
| 345 // Dumps current cache statistics to the log. | |
| 346 void LogStats(); | |
| 347 | |
| 348 // Send UMA stats. | |
| 349 void ReportStats(); | |
| 350 | |
| 351 // Upgrades the index file to version 2.1. | |
| 352 void UpgradeTo2_1(); | |
| 353 | |
| 354 // Performs basic checks on the index file. Returns false on failure. | |
| 355 bool CheckIndex(); | |
| 356 | |
| 357 // Part of the self test. Returns the number or dirty entries, or an error. | |
| 358 int CheckAllEntries(); | |
| 359 | |
| 360 // Part of the self test. Returns false if the entry is corrupt. | |
| 361 bool CheckEntry(EntryImpl* cache_entry); | |
| 362 | |
| 363 // Returns the maximum total memory for the memory buffers. | |
| 364 int MaxBuffersSize(); | |
| 365 | |
| 366 InFlightBackendIO background_queue_; // The controller of pending operations. | |
| 367 scoped_refptr<MappedFile> index_; // The main cache index. | |
| 368 base::FilePath path_; // Path to the folder used as backing storage. | |
| 369 Index* data_; // Pointer to the index data. | |
| 370 BlockFiles block_files_; // Set of files used to store all data. | |
| 371 Rankings rankings_; // Rankings to be able to trim the cache. | |
| 372 uint32 mask_; // Binary mask to map a hash to the hash table. | |
| 373 int32 max_size_; // Maximum data size for this instance. | |
| 374 Eviction eviction_; // Handler of the eviction algorithm. | |
| 375 EntriesMap open_entries_; // Map of open entries. | |
| 376 int num_refs_; // Number of referenced cache entries. | |
| 377 int max_refs_; // Max number of referenced cache entries. | |
| 378 int num_pending_io_; // Number of pending IO operations. | |
| 379 int entry_count_; // Number of entries accessed lately. | |
| 380 int byte_count_; // Number of bytes read/written lately. | |
| 381 int buffer_bytes_; // Total size of the temporary entries' buffers. | |
| 382 int up_ticks_; // The number of timer ticks received (OnStatsTimer). | |
| 383 net::CacheType cache_type_; | |
| 384 int uma_report_; // Controls transmission of UMA data. | |
| 385 uint32 user_flags_; // Flags set by the user. | |
| 386 bool init_; // controls the initialization of the system. | |
| 387 bool restarted_; | |
| 388 bool unit_test_; | |
| 389 bool read_only_; // Prevents updates of the rankings data (used by tools). | |
| 390 bool disabled_; | |
| 391 bool new_eviction_; // What eviction algorithm should be used. | |
| 392 bool first_timer_; // True if the timer has not been called. | |
| 393 bool user_load_; // True if we see a high load coming from the caller. | |
| 394 | |
| 395 net::NetLog* net_log_; | |
| 396 | |
| 397 Stats stats_; // Usage statistics. | |
| 398 scoped_ptr<base::RepeatingTimer<BackendImpl> > timer_; // Usage timer. | |
| 399 base::WaitableEvent done_; // Signals the end of background work. | |
| 400 scoped_refptr<TraceObject> trace_object_; // Initializes internal tracing. | |
| 401 base::WeakPtrFactory<BackendImpl> ptr_factory_; | |
| 402 | |
| 403 DISALLOW_COPY_AND_ASSIGN(BackendImpl); | |
| 404 }; | |
| 405 | |
| 406 } // namespace disk_cache | |
| 407 | |
| 408 #endif // NET_DISK_CACHE_BLOCKFILE_BACKEND_IMPL_H_ | |
| OLD | NEW |