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 #ifndef BASE_METRICS_HISTOGRAM_SNAPSHOT_MANAGER_H_ | 5 #ifndef BASE_METRICS_HISTOGRAM_SNAPSHOT_MANAGER_H_ |
6 #define BASE_METRICS_HISTOGRAM_SNAPSHOT_MANAGER_H_ | 6 #define BASE_METRICS_HISTOGRAM_SNAPSHOT_MANAGER_H_ |
7 | 7 |
8 #include <stdint.h> | 8 #include <stdint.h> |
9 | 9 |
10 #include <map> | 10 #include <map> |
11 #include <string> | 11 #include <string> |
12 | 12 |
13 #include "base/gtest_prod_util.h" | |
13 #include "base/macros.h" | 14 #include "base/macros.h" |
14 #include "base/metrics/histogram_base.h" | 15 #include "base/metrics/histogram_base.h" |
15 | 16 |
16 namespace base { | 17 namespace base { |
17 | 18 |
18 class HistogramSamples; | 19 class HistogramSamples; |
19 class HistogramFlattener; | 20 class HistogramFlattener; |
20 | 21 |
21 // HistogramSnapshotManager handles the logistics of gathering up available | 22 // HistogramSnapshotManager handles the logistics of gathering up available |
22 // histograms for recording either to disk or for transmission (such as from | 23 // histograms for recording either to disk or for transmission (such as from |
23 // renderer to browser, or from browser to UMA upload). Since histograms can sit | 24 // renderer to browser, or from browser to UMA upload). Since histograms can sit |
24 // in memory for an extended period of time, and are vulnerable to memory | 25 // in memory for an extended period of time, and are vulnerable to memory |
25 // corruption, this class also validates as much rendundancy as it can before | 26 // corruption, this class also validates as much rendundancy as it can before |
26 // calling for the marginal change (a.k.a., delta) in a histogram to be | 27 // calling for the marginal change (a.k.a., delta) in a histogram to be |
27 // recorded. | 28 // recorded. |
28 class BASE_EXPORT HistogramSnapshotManager { | 29 class BASE_EXPORT HistogramSnapshotManager { |
30 struct SampleInfo { | |
Alexei Svitkine (slow)
2016/02/09 19:46:51
Add a comment about this struct overall - what's i
bcwhite
2016/02/11 16:42:39
Done.
| |
31 // A histogram associated with this sample; it may be one of many if | |
32 // several have been aggregated into the same "accumulated" sample set. | |
33 // Ownership of the histogram remains elsewhere and this pointer is | |
34 // cleared by FinishDeltas(). | |
35 const HistogramBase* histogram; | |
36 | |
37 // The current snapshot-delta values being accumulated. There is no need | |
38 // for this to be a scoped_ptr<> because it is only used temporarily | |
Alexei Svitkine (slow)
2016/02/09 19:46:51
I don't think it's any better to avoid the scoped_
bcwhite
2016/02/11 16:42:39
Done.
bcwhite
2016/02/12 02:03:07
It turns out that using a scoped_ptr here is a rea
| |
39 // during a loop after which it is deleted. | |
40 HistogramSamples* accumulated; | |
Alexei Svitkine (slow)
2016/02/09 19:46:51
Nit: accumulated_samples
bcwhite
2016/02/11 16:42:39
Done.
| |
41 | |
42 // The set of inconsistencies (flags) already seen for the histogram. | |
Alexei Svitkine (slow)
2016/02/09 19:46:51
Mention what flags? Where are they defined?
bcwhite
2016/02/11 16:42:39
Done.
| |
43 int inconsistencies; | |
44 | |
45 SampleInfo() : histogram(nullptr), | |
46 accumulated(nullptr), | |
47 inconsistencies(0) {} | |
48 }; | |
49 | |
50 typedef std::map<uint64_t, SampleInfo> HashInfoMap; | |
Alexei Svitkine (slow)
2016/02/09 19:46:51
You only use this typedef once, so I'd just not ha
bcwhite
2016/02/11 16:42:39
I guess all other uses became "auto". Handy, that
| |
51 | |
Alexei Svitkine (slow)
2016/02/09 19:46:51
You keep placing this above the public: section -
bcwhite
2016/02/11 16:42:39
There is no longer any access to it from public so
| |
29 public: | 52 public: |
30 explicit HistogramSnapshotManager(HistogramFlattener* histogram_flattener); | 53 explicit HistogramSnapshotManager(HistogramFlattener* histogram_flattener); |
31 virtual ~HistogramSnapshotManager(); | 54 virtual ~HistogramSnapshotManager(); |
32 | 55 |
33 // Snapshot all histograms, and ask |histogram_flattener_| to record the | 56 // Snapshot all histograms, and ask |histogram_flattener_| to record the |
34 // delta. |flags_to_set| is used to set flags for each histogram. | 57 // delta. |flags_to_set| is used to set flags for each histogram. |
35 // |required_flags| is used to select histograms to be recorded. | 58 // |required_flags| is used to select histograms to be recorded. |
36 // Only histograms that have all the flags specified by the argument will be | 59 // Only histograms that have all the flags specified by the argument will be |
37 // chosen. If all histograms should be recorded, set it to | 60 // chosen. If all histograms should be recorded, set it to |
38 // |Histogram::kNoFlags|. | 61 // |Histogram::kNoFlags|. Though any "forward" iterator will work, the |
62 // histograms over which it iterates *must* remain valid until this method | |
63 // returns; the iterator cannot deallocate histograms once it iterates past | |
64 // them. | |
39 template <class ForwardHistogramIterator> | 65 template <class ForwardHistogramIterator> |
40 void PrepareDeltas(ForwardHistogramIterator begin, | 66 void PrepareDeltas(ForwardHistogramIterator begin, |
41 ForwardHistogramIterator end, | 67 ForwardHistogramIterator end, |
42 HistogramBase::Flags flags_to_set, | 68 HistogramBase::Flags flags_to_set, |
43 HistogramBase::Flags required_flags) { | 69 HistogramBase::Flags required_flags) { |
70 StartDeltas(); | |
44 for (ForwardHistogramIterator it = begin; it != end; ++it) { | 71 for (ForwardHistogramIterator it = begin; it != end; ++it) { |
45 (*it)->SetFlags(flags_to_set); | 72 (*it)->SetFlags(flags_to_set); |
46 if (((*it)->flags() & required_flags) == required_flags) | 73 if (((*it)->flags() & required_flags) == required_flags) |
47 PrepareDelta(**it); | 74 PrepareDelta(*it); |
48 } | 75 } |
76 FinishDeltas(); | |
49 } | 77 } |
50 | 78 |
79 // When the collection is not so simple as can be done using a single | |
80 // iterator, the steps can be performed separately. Call PerpareDelta() | |
81 // as many times as necessary with a single StartDeltas() before and | |
82 // a single FinishDeltas() after. All passed histograms must live | |
83 // until FinishDeltas() completes. PrepareOnce() works the same | |
Alexei Svitkine (slow)
2016/02/09 19:46:51
Nit: I find PrepareOnce() not very clear - how abo
bcwhite
2016/02/11 16:42:39
PrepareAbsolute?
| |
84 // but assumes there were no previous logged values and no future deltas | |
85 // will be created (and thus can work on read-only histograms). | |
86 void StartDeltas(); | |
87 void PrepareDelta(HistogramBase* histogram); | |
88 void PrepareOnce(const HistogramBase* histogram); | |
Alexei Svitkine (slow)
2016/02/09 19:46:51
Nit: Make the param a const ref, which is best pra
bcwhite
2016/02/11 16:42:39
Since it's an either/or call to these two methods,
Alexei Svitkine (slow)
2016/02/16 16:46:05
But it would be different callers to them, right?
bcwhite
2016/02/16 18:06:10
Same caller. FileMetricsProvider calls PrepareDel
| |
89 void FinishDeltas(); | |
90 | |
51 private: | 91 private: |
52 // Snapshot this histogram, and record the delta. | 92 FRIEND_TEST_ALL_PREFIXES(HistogramSnapshotManagerTest, CheckMerge); |
53 void PrepareDelta(const HistogramBase& histogram); | 93 |
94 // Capture and hold samples from a histogram. This does all the heavy | |
95 // lifting for PrepareDelta() and PrepareOnce(). | |
96 void PrepareSamples(const HistogramBase* histogram, | |
97 scoped_ptr<HistogramSamples> samples); | |
54 | 98 |
55 // Try to detect and fix count inconsistency of logged samples. | 99 // Try to detect and fix count inconsistency of logged samples. |
56 void InspectLoggedSamplesInconsistency( | 100 void InspectLoggedSamplesInconsistency( |
57 const HistogramSamples& new_snapshot, | 101 const HistogramSamples& new_snapshot, |
58 HistogramSamples* logged_samples); | 102 HistogramSamples* logged_samples); |
59 | 103 |
60 // For histograms, track what we've already recorded (as a sample for | 104 // For histograms, track what has been previously seen, indexed |
61 // each histogram) so that we can record only the delta with the next log. | 105 // by the hash of the histogram name. |
62 // The information is indexed by the hash of the histogram name. | 106 HashInfoMap known_histograms_; |
63 std::map<uint64_t, HistogramSamples*> logged_samples_; | |
64 | 107 |
65 // Set of histograms found to be corrupt and their problems, indexed | 108 // Indicates if deltas are currently being prepared. |
66 // by the hash of the histogram name. | 109 bool preparing_deltas_; |
67 std::map<uint64_t, int> inconsistencies_; | |
68 | 110 |
69 // |histogram_flattener_| handles the logistics of recording the histogram | 111 // |histogram_flattener_| handles the logistics of recording the histogram |
70 // deltas. | 112 // deltas. |
71 HistogramFlattener* histogram_flattener_; // Weak. | 113 HistogramFlattener* histogram_flattener_; // Weak. |
72 | 114 |
73 DISALLOW_COPY_AND_ASSIGN(HistogramSnapshotManager); | 115 DISALLOW_COPY_AND_ASSIGN(HistogramSnapshotManager); |
74 }; | 116 }; |
75 | 117 |
76 } // namespace base | 118 } // namespace base |
77 | 119 |
78 #endif // BASE_METRICS_HISTOGRAM_SNAPSHOT_MANAGER_H_ | 120 #endif // BASE_METRICS_HISTOGRAM_SNAPSHOT_MANAGER_H_ |
OLD | NEW |