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