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 #include <vector> | |
12 | 13 |
13 #include "base/gtest_prod_util.h" | 14 #include "base/gtest_prod_util.h" |
14 #include "base/macros.h" | 15 #include "base/macros.h" |
15 #include "base/metrics/histogram_base.h" | 16 #include "base/metrics/histogram_base.h" |
16 | 17 |
17 namespace base { | 18 namespace base { |
18 | 19 |
19 class HistogramSamples; | 20 class HistogramSamples; |
20 class HistogramFlattener; | 21 class HistogramFlattener; |
21 | 22 |
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
54 FinishDeltas(); | 55 FinishDeltas(); |
55 } | 56 } |
56 | 57 |
57 // When the collection is not so simple as can be done using a single | 58 // When the collection is not so simple as can be done using a single |
58 // iterator, the steps can be performed separately. Call PerpareDelta() | 59 // iterator, the steps can be performed separately. Call PerpareDelta() |
59 // as many times as necessary with a single StartDeltas() before and | 60 // as many times as necessary with a single StartDeltas() before and |
60 // a single FinishDeltas() after. All passed histograms must live | 61 // a single FinishDeltas() after. All passed histograms must live |
61 // until FinishDeltas() completes. PrepareOnce() works the same | 62 // until FinishDeltas() completes. PrepareOnce() works the same |
62 // but assumes there were no previous logged values and no future deltas | 63 // but assumes there were no previous logged values and no future deltas |
63 // will be created (and thus can work on read-only histograms). | 64 // will be created (and thus can work on read-only histograms). |
65 // Use Prepare*TakingOwnership() if it is desireable to have this class | |
66 // automatically delete the histogram once it is "finished". | |
64 void StartDeltas(); | 67 void StartDeltas(); |
65 void PrepareDelta(HistogramBase* histogram); | 68 void PrepareDelta(HistogramBase* histogram); |
69 void PrepareDeltaTakingOwnership(scoped_ptr<HistogramBase> histogram); | |
66 void PrepareOnce(const HistogramBase* histogram); | 70 void PrepareOnce(const HistogramBase* histogram); |
71 void PrepareOnceTakingOwnership(scoped_ptr<const HistogramBase> histogram); | |
67 void FinishDeltas(); | 72 void FinishDeltas(); |
68 | 73 |
69 private: | 74 private: |
70 FRIEND_TEST_ALL_PREFIXES(HistogramSnapshotManagerTest, CheckMerge); | 75 FRIEND_TEST_ALL_PREFIXES(HistogramSnapshotManagerTest, CheckMerge); |
71 | 76 |
72 // During a snapshot, samples are acquired and aggregated. This structure | 77 // During a snapshot, samples are acquired and aggregated. This structure |
73 // contains all the information collected for a given histogram. Once a | 78 // contains all the information collected for a given histogram. Once a |
74 // snapshot operation is finished, it is generally emptied except for | 79 // snapshot operation is finished, it is generally emptied except for |
75 // information that must persist from one report to the next, such as | 80 // information that must persist from one report to the next, such as |
76 // the "inconsistencies". | 81 // the "inconsistencies". |
77 struct SampleInfo { | 82 struct SampleInfo { |
78 SampleInfo() : histogram(nullptr), | 83 SampleInfo() : histogram(nullptr), |
grt (UTC plus 2)
2016/02/15 15:42:39
move the initializers down to the members themselv
bcwhite
2016/02/15 19:22:10
Cool. I didn't even know about that.
| |
79 accumulated_samples(nullptr), | 84 accumulated_samples(nullptr), |
80 inconsistencies(0) {} | 85 inconsistencies(0) {} |
81 | 86 |
82 // A histogram associated with this sample; it may be one of many if | 87 // A histogram associated with this sample; it may be one of many if |
83 // several have been aggregated into the same "accumulated" sample set. | 88 // several have been aggregated into the same "accumulated" sample set. |
84 // Ownership of the histogram remains elsewhere and this pointer is | 89 // Ownership of the histogram remains elsewhere and this pointer is |
85 // cleared by FinishDeltas(). | 90 // cleared by FinishDeltas(). |
86 const HistogramBase* histogram; | 91 const HistogramBase* histogram; |
87 | 92 |
88 // The current snapshot-delta values being accumulated. | 93 // The current snapshot-delta values being accumulated. |
89 // TODO(bcwhite): Change this to a scoped_ptr once all build architectures | 94 // TODO(bcwhite): Change this to a scoped_ptr once all build architectures |
90 // support such as the value of a std::map. | 95 // support such as the value of a std::map. |
91 HistogramSamples* accumulated_samples; | 96 HistogramSamples* accumulated_samples; |
92 | 97 |
93 // The set of inconsistencies (flags) already seen for the histogram. | 98 // The set of inconsistencies (flags) already seen for the histogram. |
94 // See HistogramBase::Inconsistency for values. | 99 // See HistogramBase::Inconsistency for values. |
95 int inconsistencies; | 100 int inconsistencies; |
grt (UTC plus 2)
2016/02/15 15:42:39
use unsigned types for bitfields since some shifts
bcwhite
2016/02/15 19:22:10
Okay but I'll do it elsewhere, either in the paren
grt (UTC plus 2)
2016/02/15 19:53:55
sgtm
| |
96 }; | 101 }; |
97 | 102 |
98 // Capture and hold samples from a histogram. This does all the heavy | 103 // Capture and hold samples from a histogram. This does all the heavy |
99 // lifting for PrepareDelta() and PrepareOnce(). | 104 // lifting for PrepareDelta() and PrepareOnce(). |
100 void PrepareSamples(const HistogramBase* histogram, | 105 void PrepareSamples(const HistogramBase* histogram, |
101 scoped_ptr<HistogramSamples> samples); | 106 scoped_ptr<HistogramSamples> samples, |
107 bool take_ownership); | |
102 | 108 |
103 // Try to detect and fix count inconsistency of logged samples. | 109 // Try to detect and fix count inconsistency of logged samples. |
104 void InspectLoggedSamplesInconsistency( | 110 void InspectLoggedSamplesInconsistency( |
105 const HistogramSamples& new_snapshot, | 111 const HistogramSamples& new_snapshot, |
106 HistogramSamples* logged_samples); | 112 HistogramSamples* logged_samples); |
107 | 113 |
108 // For histograms, track what has been previously seen, indexed | 114 // For histograms, track what has been previously seen, indexed |
109 // by the hash of the histogram name. | 115 // by the hash of the histogram name. |
110 std::map<uint64_t, SampleInfo> known_histograms_; | 116 std::map<uint64_t, SampleInfo> known_histograms_; |
111 | 117 |
118 // Collection of histograms of which ownership has been passed to this | |
119 // object. They will be deleted by FinishDeltas(). | |
120 std::vector<scoped_ptr<const HistogramBase>> owned_histograms_; | |
121 | |
112 // Indicates if deltas are currently being prepared. | 122 // Indicates if deltas are currently being prepared. |
113 bool preparing_deltas_; | 123 bool preparing_deltas_; |
114 | 124 |
115 // |histogram_flattener_| handles the logistics of recording the histogram | 125 // |histogram_flattener_| handles the logistics of recording the histogram |
116 // deltas. | 126 // deltas. |
117 HistogramFlattener* histogram_flattener_; // Weak. | 127 HistogramFlattener* histogram_flattener_; // Weak. |
118 | 128 |
119 DISALLOW_COPY_AND_ASSIGN(HistogramSnapshotManager); | 129 DISALLOW_COPY_AND_ASSIGN(HistogramSnapshotManager); |
120 }; | 130 }; |
121 | 131 |
122 } // namespace base | 132 } // namespace base |
123 | 133 |
124 #endif // BASE_METRICS_HISTOGRAM_SNAPSHOT_MANAGER_H_ | 134 #endif // BASE_METRICS_HISTOGRAM_SNAPSHOT_MANAGER_H_ |
OLD | NEW |