OLD | NEW |
---|---|
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 // SampleMap implements HistogramSamples interface. It is used by the | 5 // SampleMap implements HistogramSamples interface. It is used by the |
6 // SparseHistogram class to store samples. | 6 // SparseHistogram class to store samples. |
7 | 7 |
8 #ifndef BASE_METRICS_SAMPLE_MAP_H_ | 8 #ifndef BASE_METRICS_SAMPLE_MAP_H_ |
9 #define BASE_METRICS_SAMPLE_MAP_H_ | 9 #define BASE_METRICS_SAMPLE_MAP_H_ |
10 | 10 |
11 #include <stdint.h> | 11 #include <stdint.h> |
12 | 12 |
13 #include <map> | 13 #include <map> |
14 | 14 |
15 #include "base/compiler_specific.h" | 15 #include "base/compiler_specific.h" |
16 #include "base/macros.h" | 16 #include "base/macros.h" |
17 #include "base/memory/scoped_ptr.h" | 17 #include "base/memory/scoped_ptr.h" |
18 #include "base/metrics/histogram_base.h" | 18 #include "base/metrics/histogram_base.h" |
19 #include "base/metrics/histogram_samples.h" | 19 #include "base/metrics/histogram_samples.h" |
20 #include "base/metrics/persistent_memory_allocator.h" | |
20 | 21 |
21 namespace base { | 22 namespace base { |
22 | 23 |
23 class BASE_EXPORT SampleMap : public HistogramSamples { | 24 class BASE_EXPORT SampleMap : public HistogramSamples { |
24 public: | 25 public: |
25 SampleMap(); | 26 SampleMap(); |
26 explicit SampleMap(uint64_t id); | 27 explicit SampleMap(uint64_t id); |
27 ~SampleMap() override; | 28 ~SampleMap() override; |
28 | 29 |
29 // HistogramSamples implementation: | 30 // HistogramSamples implementation: |
30 void Accumulate(HistogramBase::Sample value, | 31 void Accumulate(HistogramBase::Sample value, |
31 HistogramBase::Count count) override; | 32 HistogramBase::Count count) override; |
32 HistogramBase::Count GetCount(HistogramBase::Sample value) const override; | 33 HistogramBase::Count GetCount(HistogramBase::Sample value) const override; |
33 HistogramBase::Count TotalCount() const override; | 34 HistogramBase::Count TotalCount() const override; |
34 scoped_ptr<SampleCountIterator> Iterator() const override; | 35 scoped_ptr<SampleCountIterator> Iterator() const override; |
35 | 36 |
36 protected: | 37 protected: |
37 bool AddSubtractImpl( | 38 // Performs arithemetic. |op| is ADD or SUBTRACT. |
38 SampleCountIterator* iter, | 39 bool AddSubtractImpl(SampleCountIterator* iter, Operator op) override; |
39 HistogramSamples::Operator op) override; // |op| is ADD or SUBTRACT. | |
40 | 40 |
41 private: | 41 private: |
42 std::map<HistogramBase::Sample, HistogramBase::Count> sample_counts_; | 42 std::map<HistogramBase::Sample, HistogramBase::Count> sample_counts_; |
43 | 43 |
44 DISALLOW_COPY_AND_ASSIGN(SampleMap); | 44 DISALLOW_COPY_AND_ASSIGN(SampleMap); |
45 }; | 45 }; |
46 | 46 |
47 class BASE_EXPORT SampleMapIterator : public SampleCountIterator { | 47 class BASE_EXPORT SampleMapIterator : public SampleCountIterator { |
48 public: | 48 public: |
49 typedef std::map<HistogramBase::Sample, HistogramBase::Count> | 49 typedef std::map<HistogramBase::Sample, HistogramBase::Count> |
50 SampleToCountMap; | 50 SampleToCountMap; |
51 | 51 |
52 explicit SampleMapIterator(const SampleToCountMap& sample_counts); | 52 explicit SampleMapIterator(const SampleToCountMap& sample_counts); |
53 ~SampleMapIterator() override; | 53 ~SampleMapIterator() override; |
54 | 54 |
55 // SampleCountIterator implementation: | 55 // SampleCountIterator implementation: |
56 bool Done() const override; | 56 bool Done() const override; |
57 void Next() override; | 57 void Next() override; |
58 void Get(HistogramBase::Sample* min, | 58 void Get(HistogramBase::Sample* min, |
59 HistogramBase::Sample* max, | 59 HistogramBase::Sample* max, |
60 HistogramBase::Count* count) const override; | 60 HistogramBase::Count* count) const override; |
61 | 61 |
62 private: | 62 private: |
63 void SkipEmptyBuckets(); | 63 void SkipEmptyBuckets(); |
64 | 64 |
65 SampleToCountMap::const_iterator iter_; | 65 SampleToCountMap::const_iterator iter_; |
66 const SampleToCountMap::const_iterator end_; | 66 const SampleToCountMap::const_iterator end_; |
67 }; | 67 }; |
68 | 68 |
69 | |
70 // A sample-map similar to the above but holds its samples in persistent memory | |
71 // in order to be live across process restarts or be shared between processes. | |
72 class BASE_EXPORT PersistentSampleMap : public HistogramSamples { | |
Alexei Svitkine (slow)
2016/03/03 17:43:38
I would make a separate file for this.
bcwhite
2016/03/07 15:30:39
Done.
| |
73 public: | |
74 PersistentSampleMap(uint64_t id, | |
75 PersistentMemoryAllocator* allocator, | |
76 Metadata* meta); | |
77 ~PersistentSampleMap() override; | |
78 | |
79 // HistogramSamples implementation: | |
Alexei Svitkine (slow)
2016/03/03 17:43:38
Nit: // HistogramSamples:
bcwhite
2016/03/07 15:30:39
Done. Above, too.
| |
80 void Accumulate(HistogramBase::Sample value, | |
81 HistogramBase::Count count) override; | |
82 HistogramBase::Count GetCount(HistogramBase::Sample value) const override; | |
83 HistogramBase::Count TotalCount() const override; | |
84 scoped_ptr<SampleCountIterator> Iterator() const override; | |
85 | |
86 protected: | |
87 // Performs arithemetic. |op| is ADD or SUBTRACT. | |
88 bool AddSubtractImpl(SampleCountIterator* iter, Operator op) override; | |
89 | |
90 // Gets a pointer to a "count" corresponding to a given |value|. It can | |
91 // optionally create the bucket if it doesn't already exist. | |
92 HistogramBase::Count* GetSampleCountStorage(HistogramBase::Sample value, | |
93 bool create_if_necessary); | |
94 | |
95 private: | |
96 enum : HistogramBase::Sample { kAllSamples = -1 }; | |
97 | |
98 // Imports samples from persistent memory, stopping after importing | |
99 // |until_value| or when there is nothing left to import. Pass kAllSamples | |
100 // to import everything. Returns a pointer to the count for |until_value| | |
101 // if it is found, NULL otherwise. | |
102 HistogramBase::Count* ImportSamples(HistogramBase::Sample until_value); | |
103 | |
104 // All created/loaded sample values and their associated counts. | |
105 std::map<HistogramBase::Sample, HistogramBase::Count*> sample_counts_; | |
106 | |
107 // The persistent memory allocator holding samples and an iterator through it. | |
108 PersistentMemoryAllocator* allocator_; | |
109 PersistentMemoryAllocator::Iterator sample_iter_; | |
110 | |
111 DISALLOW_COPY_AND_ASSIGN(PersistentSampleMap); | |
112 }; | |
113 | |
114 class BASE_EXPORT PersistentSampleMapIterator : public SampleCountIterator { | |
Alexei Svitkine (slow)
2016/03/03 17:43:38
How is this class different than SampleMapIterator
bcwhite
2016/03/07 15:30:39
It operates on a PersistentSampleMap instead of a
| |
115 public: | |
116 typedef std::map<HistogramBase::Sample, HistogramBase::Count*> | |
117 SampleToCountMap; | |
118 | |
119 explicit PersistentSampleMapIterator(const SampleToCountMap& sample_counts); | |
120 ~PersistentSampleMapIterator() override; | |
121 | |
122 // SampleCountIterator implementation: | |
123 bool Done() const override; | |
124 void Next() override; | |
125 void Get(HistogramBase::Sample* min, | |
126 HistogramBase::Sample* max, | |
127 HistogramBase::Count* count) const override; | |
128 | |
129 private: | |
130 void SkipEmptyBuckets(); | |
131 | |
132 SampleToCountMap::const_iterator iter_; | |
133 const SampleToCountMap::const_iterator end_; | |
134 }; | |
135 | |
69 } // namespace base | 136 } // namespace base |
70 | 137 |
71 #endif // BASE_METRICS_SAMPLE_MAP_H_ | 138 #endif // BASE_METRICS_SAMPLE_MAP_H_ |
OLD | NEW |