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