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