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_BACKEND_IMPL_H_ 8 #define NET_DISK_CACHE_BACKEND_IMPL_H_ 9 10 #include "base/containers/hash_tables.h" 11 #include "base/files/file_path.h" 12 #include "base/timer/timer.h" 13 #include "net/disk_cache/block_files.h" 14 #include "net/disk_cache/disk_cache.h" 15 #include "net/disk_cache/eviction.h" 16 #include "net/disk_cache/in_flight_backend_io.h" 17 #include "net/disk_cache/rankings.h" 18 #include "net/disk_cache/stats.h" 19 #include "net/disk_cache/stress_support.h" 20 #include "net/disk_cache/trace.h" 21 22 namespace net { 23 class NetLog; 24 } // namespace net 25 26 namespace disk_cache { 27 28 enum BackendFlags { 29 kNone = 0, 30 kMask = 1, // A mask (for the index table) was specified. 31 kMaxSize = 1 << 1, // A maximum size was provided. 32 kUnitTestMode = 1 << 2, // We are modifying the behavior for testing. 33 kUpgradeMode = 1 << 3, // This is the upgrade tool (dump). 34 kNewEviction = 1 << 4, // Use of new eviction was specified. 35 kNoRandom = 1 << 5, // Don't add randomness to the behavior. 36 kNoLoadProtection = 1 << 6, // Don't act conservatively under load. 37 kNoBuffering = 1 << 7 // Disable extended IO buffering. 38 }; 39 40 // This class implements the Backend interface. An object of this 41 // class handles the operations of the cache for a particular profile. 42 class NET_EXPORT_PRIVATE BackendImpl : public Backend { 43 friend class Eviction; 44 public: 45 BackendImpl(const base::FilePath& path, base::MessageLoopProxy* cache_thread, 46 net::NetLog* net_log); 47 // mask can be used to limit the usable size of the hash table, for testing. 48 BackendImpl(const base::FilePath& path, uint32 mask, 49 base::MessageLoopProxy* cache_thread, net::NetLog* net_log); 50 virtual ~BackendImpl(); 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(EntryImpl* entry, bool modified); 71 72 // Permanently deletes an entry, but still keeps track of it. 73 void InternalDoomEntry(EntryImpl* 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 // If the data stored by the provided |rankings| points to an open entry, 85 // returns a pointer to that entry, otherwise returns NULL. Note that this 86 // method does NOT increase the ref counter for the entry. 87 EntryImpl* GetOpenEntry(CacheRankingsBlock* rankings) const; 88 89 // Returns the id being used on this run of the cache. 90 int32 GetCurrentEntryId() const; 91 92 // Returns the maximum size for a file to reside on the cache. 93 int MaxFileSize() const; 94 95 // A user data block is being created, extended or truncated. 96 void ModifyStorageSize(int32 old_size, int32 new_size); 97 98 // Logs requests that are denied due to being too big. 99 void TooMuchStorageRequested(int32 size); 100 101 // Returns true if a temporary buffer is allowed to be extended. 102 bool IsAllocAllowed(int current_size, int new_size); 103 104 // Tracks the release of |size| bytes by an entry buffer. 105 void BufferDeleted(int size); 106 107 // Only intended for testing the two previous methods. 108 int GetTotalBuffersSize() const { 109 return buffer_bytes_; 110 } 111 112 // Returns true if this instance seems to be under heavy load. 113 bool IsLoaded() const; 114 115 // Returns the full histogram name, for the given base |name| and experiment, 116 // and the current cache type. The name will be "DiskCache.t.name_e" where n 117 // is the cache type and e the provided |experiment|. 118 std::string HistogramName(const char* name, int experiment) const; 119 120 net::CacheType cache_type() const { 121 return cache_type_; 122 } 123 124 bool read_only() const { 125 return read_only_; 126 } 127 128 // Returns a weak pointer to this object. 129 base::WeakPtr<BackendImpl> GetWeakPtr(); 130 131 // Returns true if we should send histograms for this user again. The caller 132 // must call this function only once per run (because it returns always the 133 // same thing on a given run). 134 bool ShouldReportAgain(); 135 136 // Reports some data when we filled up the cache. 137 void FirstEviction(); 138 139 // Called when an interesting event should be logged (counted). 140 void OnEvent(Stats::Counters an_event); 141 142 // Keeps track of payload access (doesn't include metadata). 143 void OnRead(int bytes); 144 void OnWrite(int bytes); 145 146 // Timer callback to calculate usage statistics. 147 void OnStatsTimer(); 148 149 // Sets internal parameters to enable unit testing mode. 150 void SetUnitTestMode(); 151 152 // Sets internal parameters to enable upgrade mode (for internal tools). 153 void SetUpgradeMode(); 154 155 // Sets the eviction algorithm to version 2. 156 void SetNewEviction(); 157 158 // Sets an explicit set of BackendFlags. 159 void SetFlags(uint32 flags); 160 161 // Sends a dummy operation through the operation queue, for unit tests. 162 int FlushQueueForTest(const CompletionCallback& callback); 163 164 // Trims an entry (all if |empty| is true) from the list of deleted 165 // entries. This method should be called directly on the cache thread. 166 void TrimForTest(bool empty); 167 168 // Trims an entry (all if |empty| is true) from the list of deleted 169 // entries. This method should be called directly on the cache thread. 170 void TrimDeletedListForTest(bool empty); 171 172 // Performs a simple self-check, and returns the number of dirty items 173 // or an error code (negative value). 174 int SelfCheck(); 175 176 // Backend implementation. 177 virtual net::CacheType GetCacheType() const OVERRIDE; 178 virtual int32 GetEntryCount() const OVERRIDE; 179 virtual int OpenEntry(const std::string& key, Entry** entry, 180 const CompletionCallback& callback) OVERRIDE; 181 virtual int CreateEntry(const std::string& key, Entry** entry, 182 const CompletionCallback& callback) OVERRIDE; 183 virtual int DoomEntry(const std::string& key, 184 const CompletionCallback& callback) OVERRIDE; 185 virtual int DoomAllEntries(const CompletionCallback& callback) OVERRIDE; 186 virtual int DoomEntriesBetween(base::Time initial_time, 187 base::Time end_time, 188 const CompletionCallback& callback) OVERRIDE; 189 virtual int DoomEntriesSince(base::Time initial_time, 190 const CompletionCallback& callback) OVERRIDE; 191 virtual int OpenNextEntry(void** iter, Entry** next_entry, 192 const CompletionCallback& callback) OVERRIDE; 193 virtual void EndEnumeration(void** iter) OVERRIDE; 194 virtual void GetStats(StatsItems* stats) OVERRIDE; 195 virtual void OnExternalCacheHit(const std::string& key) OVERRIDE; 196 197 private: 198 typedef base::hash_map<CacheAddr, EntryImpl*> EntriesMap; 199 200 void AdjustMaxCacheSize(int table_len); 201 202 bool InitStats(); 203 void StoreStats(); 204 205 // Deletes the cache and starts again. 206 void RestartCache(bool failure); 207 void PrepareForRestart(); 208 209 void CleanupCache(); 210 211 // Creates a new entry object. Returns zero on success, or a disk_cache error 212 // on failure. 213 int NewEntry(Addr address, EntryImpl** entry); 214 215 // Opens the next or previous entry on a cache iteration. 216 EntryImpl* OpenFollowingEntry(bool forward, void** iter); 217 218 // Handles the used storage count. 219 void AddStorageSize(int32 bytes); 220 void SubstractStorageSize(int32 bytes); 221 222 // Update the number of referenced cache entries. 223 void IncreaseNumRefs(); 224 void DecreaseNumRefs(); 225 void IncreaseNumEntries(); 226 void DecreaseNumEntries(); 227 228 // Dumps current cache statistics to the log. 229 void LogStats(); 230 231 // Send UMA stats. 232 void ReportStats(); 233 234 // Reports an uncommon, recoverable error. 235 void ReportError(int error); 236 237 // Performs basic checks on the index file. Returns false on failure. 238 bool CheckIndex(); 239 240 // Part of the self test. Returns the number or dirty entries, or an error. 241 int CheckAllEntries(); 242 243 // Part of the self test. Returns false if the entry is corrupt. 244 bool CheckEntry(EntryImpl* cache_entry); 245 246 // Returns the maximum total memory for the memory buffers. 247 int MaxBuffersSize(); 248 249 scoped_refptr<MappedFile> index_; // The main cache index. 250 base::FilePath path_; // Path to the folder used as backing storage. 251 BlockFiles block_files_; // Set of files used to store all data. 252 int32 max_size_; // Maximum data size for this instance. 253 Eviction eviction_; // Handler of the eviction algorithm. 254 EntriesMap open_entries_; // Map of open entries. 255 int num_refs_; // Number of referenced cache entries. 256 int max_refs_; // Max number of referenced cache entries. 257 int entry_count_; // Number of entries accessed lately. 258 int byte_count_; // Number of bytes read/written lately. 259 int buffer_bytes_; // Total size of the temporary entries' buffers. 260 int up_ticks_; // The number of timer ticks received (OnStatsTimer). 261 net::CacheType cache_type_; 262 int uma_report_; // Controls transmission of UMA data. 263 uint32 user_flags_; // Flags set by the user. 264 bool init_; // controls the initialization of the system. 265 bool restarted_; 266 bool unit_test_; 267 bool read_only_; // Prevents updates of the rankings data (used by tools). 268 bool disabled_; 269 bool new_eviction_; // What eviction algorithm should be used. 270 bool first_timer_; // True if the timer has not been called. 271 bool user_load_; // True if we see a high load coming from the caller. 272 273 net::NetLog* net_log_; 274 275 Stats stats_; // Usage statistics. 276 scoped_ptr<base::RepeatingTimer<BackendImpl> > timer_; // Usage timer. 277 scoped_refptr<TraceObject> trace_object_; // Initializes internal tracing. 278 base::WeakPtrFactory<BackendImpl> ptr_factory_; 279 280 DISALLOW_COPY_AND_ASSIGN(BackendImpl); 281 }; 282 283 // Returns the preferred max cache size given the available disk space. 284 NET_EXPORT_PRIVATE int PreferedCacheSize(int64 available); 285 286 } // namespace disk_cache 287 288 #endif // NET_DISK_CACHE_BACKEND_IMPL_H_ 289