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 <map> | 8 #include <map> |
9 #include <string> | 9 #include <string> |
10 | 10 |
11 #include "base/basictypes.h" | 11 #include "base/basictypes.h" |
12 #include "base/metrics/histogram_base.h" | 12 #include "base/metrics/histogram_base.h" |
13 | 13 |
14 namespace base { | 14 namespace base { |
15 | 15 |
16 class HistogramSamples; | 16 class HistogramSamples; |
17 class HistogramFlattener; | 17 class HistogramFlattener; |
18 | 18 |
19 // HistogramSnapshotManager handles the logistics of gathering up available | 19 // HistogramSnapshotManager handles the logistics of gathering up available |
20 // histograms for recording either to disk or for transmission (such as from | 20 // histograms for recording either to disk or for transmission (such as from |
21 // renderer to browser, or from browser to UMA upload). Since histograms can sit | 21 // renderer to browser, or from browser to UMA upload). Since histograms can sit |
22 // in memory for an extended period of time, and are vulnerable to memory | 22 // in memory for an extended period of time, and are vulnerable to memory |
23 // corruption, this class also validates as much rendundancy as it can before | 23 // corruption, this class also validates as much rendundancy as it can before |
24 // calling for the marginal change (a.k.a., delta) in a histogram to be | 24 // calling for the marginal change (a.k.a., delta) in a histogram to be |
25 // recorded. | 25 // recorded. |
26 class BASE_EXPORT HistogramSnapshotManager { | 26 class BASE_EXPORT HistogramSnapshotManager { |
27 struct SampleInfo { | |
28 // A histogram associated with this sample; it may be one of many if | |
29 // several have been aggregated into the same "accumulated" sample set. | |
30 // Ownership of the histogram remains elsewhere and this pointer is | |
31 // cleared by FinishDeltas() called at the end of PrepareDeltas(). | |
32 const HistogramBase* histogram; | |
33 | |
34 // The previous snapshot values that were logged to the server so can | |
35 // record only the delta with the next log. | |
36 scoped_ptr<HistogramSamples> logged; | |
37 | |
38 // The current snapshot values being accumulated. There is no need for | |
39 // this to be a scoped_ptr<> because it is only used temporarily during | |
40 // a loop after which it replaces the "logged" value. | |
41 HistogramSamples* accumulated; | |
42 | |
43 // The set of inconsistencies already seen for the histogram. | |
44 int inconsistencies; | |
Alexei Svitkine (slow)
2015/12/04 18:30:11
Nit: inconsistencies_count or num_inconsistencies
bcwhite
2015/12/08 17:32:18
It's not a count but a set of flags. I'll add to
| |
45 | |
46 SampleInfo() : histogram(nullptr), | |
47 accumulated(nullptr), | |
48 inconsistencies(0) {} | |
49 }; | |
50 | |
51 typedef std::map<uint64_t, SampleInfo> HashInfoMap; | |
52 | |
27 public: | 53 public: |
28 explicit HistogramSnapshotManager(HistogramFlattener* histogram_flattener); | 54 explicit HistogramSnapshotManager(HistogramFlattener* histogram_flattener); |
29 virtual ~HistogramSnapshotManager(); | 55 virtual ~HistogramSnapshotManager(); |
30 | 56 |
31 // Snapshot all histograms, and ask |histogram_flattener_| to record the | 57 // Snapshot all histograms, and ask |histogram_flattener_| to record the |
32 // delta. |flags_to_set| is used to set flags for each histogram. | 58 // delta. |flags_to_set| is used to set flags for each histogram. |
33 // |required_flags| is used to select histograms to be recorded. | 59 // |required_flags| is used to select histograms to be recorded. |
34 // Only histograms that have all the flags specified by the argument will be | 60 // Only histograms that have all the flags specified by the argument will be |
35 // chosen. If all histograms should be recorded, set it to | 61 // chosen. If all histograms should be recorded, set it to |
36 // |Histogram::kNoFlags|. | 62 // |Histogram::kNoFlags|. Though any "forward" iterator will work, the |
63 // histograms over which it iterates *must* remain valid until this method | |
64 // returns; the iterator cannot deallocate histograms once it iterates past | |
65 // them. | |
37 template <class ForwardHistogramIterator> | 66 template <class ForwardHistogramIterator> |
38 void PrepareDeltas(ForwardHistogramIterator begin, | 67 void PrepareDeltas(ForwardHistogramIterator begin, |
39 ForwardHistogramIterator end, | 68 ForwardHistogramIterator end, |
40 HistogramBase::Flags flags_to_set, | 69 HistogramBase::Flags flags_to_set, |
41 HistogramBase::Flags required_flags) { | 70 HistogramBase::Flags required_flags) { |
71 StartDeltas(); | |
42 for (ForwardHistogramIterator it = begin; it != end; ++it) { | 72 for (ForwardHistogramIterator it = begin; it != end; ++it) { |
43 (*it)->SetFlags(flags_to_set); | 73 (*it)->SetFlags(flags_to_set); |
44 if (((*it)->flags() & required_flags) == required_flags) | 74 if (((*it)->flags() & required_flags) == required_flags) |
45 PrepareDelta(**it); | 75 PrepareDelta(*it); |
46 } | 76 } |
77 FinishDeltas(); | |
47 } | 78 } |
48 | 79 |
49 private: | 80 private: |
50 // Snapshot this histogram, and record the delta. | 81 FRIEND_TEST_ALL_PREFIXES(HistogramSnapshotManagerTest, CheckMerge); |
51 void PrepareDelta(const HistogramBase& histogram); | 82 |
83 // Start delta accumulation. | |
84 void StartDeltas(); | |
85 | |
86 // Snapshot this histogram and record the delta. | |
87 void PrepareDelta(const HistogramBase* histogram); | |
88 | |
89 // Finish all deltas and flush them to the Flattener. | |
90 void FinishDeltas(); | |
52 | 91 |
53 // Try to detect and fix count inconsistency of logged samples. | 92 // Try to detect and fix count inconsistency of logged samples. |
54 void InspectLoggedSamplesInconsistency( | 93 void InspectLoggedSamplesInconsistency( |
55 const HistogramSamples& new_snapshot, | 94 const HistogramSamples& new_snapshot, |
56 HistogramSamples* logged_samples); | 95 HistogramSamples* logged_samples); |
57 | 96 |
58 // For histograms, track what we've already recorded (as a sample for | 97 // For histograms, track what has been previously seen. |
59 // each histogram) so that we can record only the delta with the next log. | 98 HashInfoMap known_histograms_; |
60 std::map<uint64_t, HistogramSamples*> logged_samples_; | |
61 | |
62 // List of histograms found to be corrupt, and their problems. | |
63 std::map<uint64_t, int> inconsistencies_; | |
64 | 99 |
65 // |histogram_flattener_| handles the logistics of recording the histogram | 100 // |histogram_flattener_| handles the logistics of recording the histogram |
66 // deltas. | 101 // deltas. |
67 HistogramFlattener* histogram_flattener_; // Weak. | 102 HistogramFlattener* histogram_flattener_; // Weak. |
68 | 103 |
69 DISALLOW_COPY_AND_ASSIGN(HistogramSnapshotManager); | 104 DISALLOW_COPY_AND_ASSIGN(HistogramSnapshotManager); |
70 }; | 105 }; |
71 | 106 |
72 } // namespace base | 107 } // namespace base |
73 | 108 |
74 #endif // BASE_METRICS_HISTOGRAM_SNAPSHOT_MANAGER_H_ | 109 #endif // BASE_METRICS_HISTOGRAM_SNAPSHOT_MANAGER_H_ |
OLD | NEW |