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 #ifndef NET_DISK_CACHE_STATS_H_
      6 #define NET_DISK_CACHE_STATS_H_
      7 
      8 #include <string>
      9 #include <vector>
     10 
     11 #include "base/basictypes.h"
     12 #include "base/scoped_ptr.h"
     13 #include "net/disk_cache/stats_histogram.h"
     14 
     15 namespace disk_cache {
     16 
     17 class BackendImpl;
     18 
     19 typedef std::vector<std::pair<std::string, std::string> > StatsItems;
     20 
     21 // This class stores cache-specific usage information, for tunning purposes.
     22 class Stats {
     23  public:
     24   static const int kDataSizesLength = 28;
     25   enum Counters {
     26     MIN_COUNTER = 0,
     27     OPEN_MISS = MIN_COUNTER,
     28     OPEN_HIT,
     29     CREATE_MISS,
     30     CREATE_HIT,
     31     RESURRECT_HIT,
     32     CREATE_ERROR,
     33     TRIM_ENTRY,
     34     DOOM_ENTRY,
     35     DOOM_CACHE,
     36     INVALID_ENTRY,
     37     OPEN_ENTRIES,  // Average number of open entries.
     38     MAX_ENTRIES,  // Maximum number of open entries.
     39     TIMER,
     40     READ_DATA,
     41     WRITE_DATA,
     42     OPEN_RANKINGS,  // An entry has to be read just to modify rankings.
     43     GET_RANKINGS,  // We got the ranking info without reading the whole entry.
     44     FATAL_ERROR,
     45     LAST_REPORT,  // Time of the last time we sent a report.
     46     LAST_REPORT_TIMER,  // Timer count of the last time we sent a report.
     47     MAX_COUNTER
     48   };
     49 
     50   Stats() : backend_(NULL) {}
     51   ~Stats();
     52 
     53   bool Init(BackendImpl* backend, uint32* storage_addr);
     54 
     55   // Tracks changes to the stoage space used by an entry.
     56   void ModifyStorageStats(int32 old_size, int32 new_size);
     57 
     58   // Tracks general events.
     59   void OnEvent(Counters an_event);
     60   void SetCounter(Counters counter, int64 value);
     61   int64 GetCounter(Counters counter) const;
     62 
     63   void GetItems(StatsItems* items);
     64   int GetHitRatio() const;
     65   int GetResurrectRatio() const;
     66   void ResetRatios();
     67 
     68   // Returns the lower bound of the space used by entries bigger than 512 KB.
     69   int GetLargeEntriesSize();
     70 
     71   // Saves the stats to disk.
     72   void Store();
     73 
     74   // Support for StatsHistograms. Together, these methods allow StatsHistograms
     75   // to take a snapshot of the data_sizes_ as the histogram data.
     76   int GetBucketRange(size_t i) const;
     77   void Snapshot(StatsHistogram::StatsSamples* samples) const;
     78 
     79  private:
     80   int GetStatsBucket(int32 size);
     81   int GetRatio(Counters hit, Counters miss) const;
     82 
     83   BackendImpl* backend_;
     84   uint32 storage_addr_;
     85   int data_sizes_[kDataSizesLength];
     86   int64 counters_[MAX_COUNTER];
     87   scoped_refptr<StatsHistogram> size_histogram_;
     88 
     89   DISALLOW_COPY_AND_ASSIGN(Stats);
     90 };
     91 
     92 }  // namespace disk_cache
     93 
     94 #endif  // NET_DISK_CACHE_STATS_H_
     95