1// Copyright (c) 2011 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#pragma once
8
9#include <string>
10#include <vector>
11
12#include "base/basictypes.h"
13#include "base/memory/scoped_ptr.h"
14#include "net/disk_cache/stats_histogram.h"
15
16namespace disk_cache {
17
18class BackendImpl;
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    MAX_COUNTER
50  };
51
52  Stats();
53  ~Stats();
54
55  bool Init(BackendImpl* backend, uint32* storage_addr);
56
57  // Tracks changes to the stoage space used by an entry.
58  void ModifyStorageStats(int32 old_size, int32 new_size);
59
60  // Tracks general events.
61  void OnEvent(Counters an_event);
62  void SetCounter(Counters counter, int64 value);
63  int64 GetCounter(Counters counter) const;
64
65  void GetItems(StatsItems* items);
66  int GetHitRatio() const;
67  int GetResurrectRatio() const;
68  void ResetRatios();
69
70  // Returns the lower bound of the space used by entries bigger than 512 KB.
71  int GetLargeEntriesSize();
72
73  // Saves the stats to disk.
74  void Store();
75
76  // Support for StatsHistograms. Together, these methods allow StatsHistograms
77  // to take a snapshot of the data_sizes_ as the histogram data.
78  int GetBucketRange(size_t i) const;
79  void Snapshot(StatsHistogram::StatsSamples* samples) const;
80
81 private:
82  int GetStatsBucket(int32 size);
83  int GetRatio(Counters hit, Counters miss) const;
84
85  BackendImpl* backend_;
86  uint32 storage_addr_;
87  int data_sizes_[kDataSizesLength];
88  int64 counters_[MAX_COUNTER];
89  StatsHistogram* size_histogram_;
90
91  DISALLOW_COPY_AND_ASSIGN(Stats);
92};
93
94}  // namespace disk_cache
95
96#endif  // NET_DISK_CACHE_STATS_H_
97