Home | History | Annotate | Download | only in disk_cache
      1 // Copyright (c) 2006-2008 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/file_path.h"
     11 #include "base/hash_tables.h"
     12 #include "base/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/rankings.h"
     17 #include "net/disk_cache/stats.h"
     18 #include "net/disk_cache/trace.h"
     19 
     20 namespace disk_cache {
     21 
     22 enum BackendFlags {
     23   kNone = 0,
     24   kMask = 1,                    // A mask (for the index table) was specified.
     25   kMaxSize = 1 << 1,            // A maximum size was provided.
     26   kUnitTestMode = 1 << 2,       // We are modifying the behavior for testing.
     27   kUpgradeMode = 1 << 3,        // This is the upgrade tool (dump).
     28   kNewEviction = 1 << 4,        // Use of new eviction was specified.
     29   kNoRandom = 1 << 5,           // Don't add randomness to the behavior.
     30   kNoLoadProtection = 1 << 6    // Don't act conservatively under load.
     31 };
     32 
     33 // This class implements the Backend interface. An object of this
     34 // class handles the operations of the cache for a particular profile.
     35 class BackendImpl : public Backend {
     36   friend class Eviction;
     37  public:
     38   explicit BackendImpl(const FilePath& path)
     39       : path_(path), block_files_(path), mask_(0), max_size_(0),
     40         cache_type_(net::DISK_CACHE), uma_report_(0), user_flags_(0),
     41         init_(false), restarted_(false), unit_test_(false), read_only_(false),
     42         new_eviction_(false), first_timer_(true),
     43         ALLOW_THIS_IN_INITIALIZER_LIST(factory_(this)) {}
     44   // mask can be used to limit the usable size of the hash table, for testing.
     45   BackendImpl(const FilePath& path, uint32 mask)
     46       : path_(path), block_files_(path), mask_(mask), max_size_(0),
     47         cache_type_(net::DISK_CACHE), uma_report_(0), user_flags_(kMask),
     48         init_(false), restarted_(false), unit_test_(false), read_only_(false),
     49         new_eviction_(false), first_timer_(true),
     50         ALLOW_THIS_IN_INITIALIZER_LIST(factory_(this)) {}
     51   ~BackendImpl();
     52 
     53   // Returns a new backend with the desired flags. See the declaration of
     54   // CreateCacheBackend().
     55   static Backend* CreateBackend(const FilePath& full_path, bool force,
     56                                 int max_bytes, net::CacheType type,
     57                                 BackendFlags flags);
     58 
     59   // Performs general initialization for this current instance of the cache.
     60   bool Init();
     61 
     62   // Backend interface.
     63   virtual int32 GetEntryCount() const;
     64   virtual bool OpenEntry(const std::string& key, Entry** entry);
     65   virtual int OpenEntry(const std::string& key, Entry** entry,
     66                         CompletionCallback* callback);
     67   virtual bool CreateEntry(const std::string& key, Entry** entry);
     68   virtual int CreateEntry(const std::string& key, Entry** entry,
     69                           CompletionCallback* callback);
     70   virtual bool DoomEntry(const std::string& key);
     71   virtual int DoomEntry(const std::string& key, CompletionCallback* callback);
     72   virtual bool DoomAllEntries();
     73   virtual int DoomAllEntries(CompletionCallback* callback);
     74   virtual bool DoomEntriesBetween(const base::Time initial_time,
     75                                   const base::Time end_time);
     76   virtual int DoomEntriesBetween(const base::Time initial_time,
     77                                  const base::Time end_time,
     78                                  CompletionCallback* callback);
     79   virtual bool DoomEntriesSince(const base::Time initial_time);
     80   virtual int DoomEntriesSince(const base::Time initial_time,
     81                                CompletionCallback* callback);
     82   virtual bool OpenNextEntry(void** iter, Entry** next_entry);
     83   virtual int OpenNextEntry(void** iter, Entry** next_entry,
     84                             CompletionCallback* callback);
     85   virtual void EndEnumeration(void** iter);
     86   virtual void GetStats(StatsItems* stats);
     87 
     88   // Sets the maximum size for the total amount of data stored by this instance.
     89   bool SetMaxSize(int max_bytes);
     90 
     91   // Sets the cache type for this backend.
     92   void SetType(net::CacheType type);
     93 
     94   // Returns the full name for an external storage file.
     95   FilePath GetFileName(Addr address) const;
     96 
     97   // Returns the actual file used to store a given (non-external) address.
     98   MappedFile* File(Addr address);
     99 
    100   // Creates an external storage file.
    101   bool CreateExternalFile(Addr* address);
    102 
    103   // Creates a new storage block of size block_count.
    104   bool CreateBlock(FileType block_type, int block_count,
    105                    Addr* block_address);
    106 
    107   // Deletes a given storage block. deep set to true can be used to zero-fill
    108   // the related storage in addition of releasing the related block.
    109   void DeleteBlock(Addr block_address, bool deep);
    110 
    111   // Retrieves a pointer to the lru-related data.
    112   LruData* GetLruData();
    113 
    114   // Updates the ranking information for an entry.
    115   void UpdateRank(EntryImpl* entry, bool modified);
    116 
    117   // A node was recovered from a crash, it may not be on the index, so this
    118   // method checks it and takes the appropriate action.
    119   void RecoveredEntry(CacheRankingsBlock* rankings);
    120 
    121   // Permanently deletes an entry, but still keeps track of it.
    122   void InternalDoomEntry(EntryImpl* entry);
    123 
    124   // Removes all references to this entry.
    125   void RemoveEntry(EntryImpl* entry);
    126 
    127   // This method must be called whenever an entry is released for the last time.
    128   // |address| is the cache address of the entry.
    129   void CacheEntryDestroyed(Addr address);
    130 
    131   // If the data stored by the provided |rankings| points to an open entry,
    132   // returns a pointer to that entry, otherwise returns NULL. Note that this
    133   // method does NOT increase the ref counter for the entry.
    134   EntryImpl* GetOpenEntry(CacheRankingsBlock* rankings) const;
    135 
    136   // Returns the id being used on this run of the cache.
    137   int32 GetCurrentEntryId() const;
    138 
    139   // Returns the maximum size for a file to reside on the cache.
    140   int MaxFileSize() const;
    141 
    142   // A user data block is being created, extended or truncated.
    143   void ModifyStorageSize(int32 old_size, int32 new_size);
    144 
    145   // Logs requests that are denied due to being too big.
    146   void TooMuchStorageRequested(int32 size);
    147 
    148   // Returns true if this instance seems to be under heavy load.
    149   bool IsLoaded() const;
    150 
    151   // Returns the full histogram name, for the given base |name| and experiment,
    152   // and the current cache type. The name will be "DiskCache.t.name_e" where n
    153   // is the cache type and e the provided |experiment|.
    154   std::string HistogramName(const char* name, int experiment) const;
    155 
    156   net::CacheType cache_type() const {
    157     return cache_type_;
    158   }
    159 
    160   // Returns the group for this client, based on the current cache size.
    161   int GetSizeGroup() const;
    162 
    163   // Returns true if we should send histograms for this user again. The caller
    164   // must call this function only once per run (because it returns always the
    165   // same thing on a given run).
    166   bool ShouldReportAgain();
    167 
    168   // Reports some data when we filled up the cache.
    169   void FirstEviction();
    170 
    171   // Reports a critical error (and disables the cache).
    172   void CriticalError(int error);
    173 
    174   // Reports an uncommon, recoverable error.
    175   void ReportError(int error);
    176 
    177   // Called when an interesting event should be logged (counted).
    178   void OnEvent(Stats::Counters an_event);
    179 
    180   // Timer callback to calculate usage statistics.
    181   void OnStatsTimer();
    182 
    183   // Handles the pending asynchronous IO count.
    184   void IncrementIoCount();
    185   void DecrementIoCount();
    186 
    187   // Sets internal parameters to enable unit testing mode.
    188   void SetUnitTestMode();
    189 
    190   // Sets internal parameters to enable upgrade mode (for internal tools).
    191   void SetUpgradeMode();
    192 
    193   // Sets the eviction algorithm to version 2.
    194   void SetNewEviction();
    195 
    196   // Sets an explicit set of BackendFlags.
    197   void SetFlags(uint32 flags);
    198 
    199   // Clears the counter of references to test handling of corruptions.
    200   void ClearRefCountForTest();
    201 
    202   // Peforms a simple self-check, and returns the number of dirty items
    203   // or an error code (negative value).
    204   int SelfCheck();
    205 
    206   // Same bahavior as OpenNextEntry but walks the list from back to front.
    207   bool OpenPrevEntry(void** iter, Entry** prev_entry);
    208 
    209  private:
    210   typedef base::hash_map<CacheAddr, EntryImpl*> EntriesMap;
    211 
    212   // Creates a new backing file for the cache index.
    213   bool CreateBackingStore(disk_cache::File* file);
    214   bool InitBackingStore(bool* file_created);
    215   void AdjustMaxCacheSize(int table_len);
    216 
    217   // Deletes the cache and starts again.
    218   void RestartCache();
    219   void PrepareForRestart();
    220 
    221   // Creates a new entry object and checks to see if it is dirty. Returns zero
    222   // on success, or a disk_cache error on failure.
    223   int NewEntry(Addr address, EntryImpl** entry, bool* dirty);
    224 
    225   // Returns a given entry from the cache. The entry to match is determined by
    226   // key and hash, and the returned entry may be the matched one or it's parent
    227   // on the list of entries with the same hash (or bucket).
    228   EntryImpl* MatchEntry(const std::string& key, uint32 hash, bool find_parent);
    229 
    230   // Opens the next or previous entry on a cache iteration.
    231   bool OpenFollowingEntry(bool forward, void** iter, Entry** next_entry);
    232 
    233   // Opens the next or previous entry on a single list. If successfull,
    234   // |from_entry| will be updated to point to the new entry, otherwise it will
    235   // be set to NULL; in other words, it is used as an explicit iterator.
    236   bool OpenFollowingEntryFromList(bool forward, Rankings::List list,
    237                                   CacheRankingsBlock** from_entry,
    238                                   EntryImpl** next_entry);
    239 
    240   // Returns the entry that is pointed by |next|. If we are trimming the cache,
    241   // |to_evict| should be true so that we don't perform extra disk writes.
    242   EntryImpl* GetEnumeratedEntry(CacheRankingsBlock* next, bool to_evict);
    243 
    244   // Re-opens an entry that was previously deleted.
    245   bool ResurrectEntry(EntryImpl* deleted_entry, Entry** entry);
    246 
    247   void DestroyInvalidEntry(EntryImpl* entry);
    248   void DestroyInvalidEntryFromEnumeration(EntryImpl* entry);
    249 
    250   // Handles the used storage count.
    251   void AddStorageSize(int32 bytes);
    252   void SubstractStorageSize(int32 bytes);
    253 
    254   // Update the number of referenced cache entries.
    255   void IncreaseNumRefs();
    256   void DecreaseNumRefs();
    257   void IncreaseNumEntries();
    258   void DecreaseNumEntries();
    259 
    260   // Dumps current cache statistics to the log.
    261   void LogStats();
    262 
    263   // Send UMA stats.
    264   void ReportStats();
    265 
    266   // Upgrades the index file to version 2.1.
    267   void UpgradeTo2_1();
    268 
    269   // Performs basic checks on the index file. Returns false on failure.
    270   bool CheckIndex();
    271 
    272   // Part of the selt test. Returns the number or dirty entries, or an error.
    273   int CheckAllEntries();
    274 
    275   // Part of the self test. Returns false if the entry is corrupt.
    276   bool CheckEntry(EntryImpl* cache_entry);
    277 
    278   scoped_refptr<MappedFile> index_;  // The main cache index.
    279   FilePath path_;  // Path to the folder used as backing storage.
    280   Index* data_;  // Pointer to the index data.
    281   BlockFiles block_files_;  // Set of files used to store all data.
    282   Rankings rankings_;  // Rankings to be able to trim the cache.
    283   uint32 mask_;  // Binary mask to map a hash to the hash table.
    284   int32 max_size_;  // Maximum data size for this instance.
    285   Eviction eviction_;  // Handler of the eviction algorithm.
    286   EntriesMap open_entries_;  // Map of open entries.
    287   int num_refs_;  // Number of referenced cache entries.
    288   int max_refs_;  // Max number of referenced cache entries.
    289   int num_pending_io_;  // Number of pending IO operations.
    290   net::CacheType cache_type_;
    291   int uma_report_;  // Controls transmision of UMA data.
    292   uint32 user_flags_;  // Flags set by the user.
    293   bool init_;  // controls the initialization of the system.
    294   bool restarted_;
    295   bool unit_test_;
    296   bool read_only_;  // Prevents updates of the rankings data (used by tools).
    297   bool disabled_;
    298   bool new_eviction_;  // What eviction algorithm should be used.
    299   bool first_timer_;  // True if the timer has not been called.
    300 
    301   Stats stats_;  // Usage statistcs.
    302   base::RepeatingTimer<BackendImpl> timer_;  // Usage timer.
    303   scoped_refptr<TraceObject> trace_object_;  // Inits internal tracing.
    304   ScopedRunnableMethodFactory<BackendImpl> factory_;
    305 
    306   DISALLOW_EVIL_CONSTRUCTORS(BackendImpl);
    307 };
    308 
    309 // Returns the prefered max cache size given the available disk space.
    310 int PreferedCacheSize(int64 available);
    311 
    312 }  // namespace disk_cache
    313 
    314 #endif  // NET_DISK_CACHE_BACKEND_IMPL_H_
    315