stats.h revision cedac228d2dd51db4b79ea1e72c7f249408ee061
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#ifndef NET_DISK_CACHE_BLOCKFILE_STATS_H_
6#define NET_DISK_CACHE_BLOCKFILE_STATS_H_
7
8#include <string>
9#include <vector>
10
11#include "base/basictypes.h"
12#include "net/disk_cache/blockfile/addr.h"
13
14namespace base {
15class HistogramSamples;
16}  // namespace base
17
18namespace disk_cache {
19
20typedef std::vector<std::pair<std::string, std::string> > StatsItems;
21
22// This class stores cache-specific usage information, for tunning purposes.
23class Stats {
24 public:
25  static const int kDataSizesLength = 28;
26  enum Counters {
27    MIN_COUNTER = 0,
28    OPEN_MISS = MIN_COUNTER,
29    OPEN_HIT,
30    CREATE_MISS,
31    CREATE_HIT,
32    RESURRECT_HIT,
33    CREATE_ERROR,
34    TRIM_ENTRY,
35    DOOM_ENTRY,
36    DOOM_CACHE,
37    INVALID_ENTRY,
38    OPEN_ENTRIES,  // Average number of open entries.
39    MAX_ENTRIES,  // Maximum number of open entries.
40    TIMER,
41    READ_DATA,
42    WRITE_DATA,
43    OPEN_RANKINGS,  // An entry has to be read just to modify rankings.
44    GET_RANKINGS,  // We got the ranking info without reading the whole entry.
45    FATAL_ERROR,
46    LAST_REPORT,  // Time of the last time we sent a report.
47    LAST_REPORT_TIMER,  // Timer count of the last time we sent a report.
48    DOOM_RECENT,  // The cache was partially cleared.
49    UNUSED,  // Was: ga.js was evicted from the cache.
50    MAX_COUNTER
51  };
52
53  Stats();
54  ~Stats();
55
56  // Initializes this object with |data| from disk.
57  bool Init(void* data, int num_bytes, Addr address);
58
59  // Generates a size distribution histogram.
60  void InitSizeHistogram();
61
62  // Returns the number of bytes needed to store the stats on disk.
63  int StorageSize();
64
65  // Tracks changes to the stoage space used by an entry.
66  void ModifyStorageStats(int32 old_size, int32 new_size);
67
68  // Tracks general events.
69  void OnEvent(Counters an_event);
70  void SetCounter(Counters counter, int64 value);
71  int64 GetCounter(Counters counter) const;
72
73  void GetItems(StatsItems* items);
74  int GetHitRatio() const;
75  int GetResurrectRatio() const;
76  void ResetRatios();
77
78  // Returns the lower bound of the space used by entries bigger than 512 KB.
79  int GetLargeEntriesSize();
80
81  // Writes the stats into |data|, to be stored at the given cache address.
82  // Returns the number of bytes copied.
83  int SerializeStats(void* data, int num_bytes, Addr* address);
84
85 private:
86  // Supports generation of SizeStats histogram data.
87  int GetBucketRange(size_t i) const;
88  int GetStatsBucket(int32 size);
89  int GetRatio(Counters hit, Counters miss) const;
90
91  Addr storage_addr_;
92  int data_sizes_[kDataSizesLength];
93  int64 counters_[MAX_COUNTER];
94
95  DISALLOW_COPY_AND_ASSIGN(Stats);
96};
97
98}  // namespace disk_cache
99
100#endif  // NET_DISK_CACHE_BLOCKFILE_STATS_H_
101