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. PrepareAbsolute() works the same | 62 // until FinishDeltas() completes. PrepareAbsolute() 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 PrepareAbsolute(const HistogramBase* histogram); | 70 void PrepareAbsolute(const HistogramBase* histogram); |
| 71 void PrepareAbsoluteTakingOwnership( |
| 72 scoped_ptr<const HistogramBase> histogram); |
67 void FinishDeltas(); | 73 void FinishDeltas(); |
68 | 74 |
69 private: | 75 private: |
70 FRIEND_TEST_ALL_PREFIXES(HistogramSnapshotManagerTest, CheckMerge); | 76 FRIEND_TEST_ALL_PREFIXES(HistogramSnapshotManagerTest, CheckMerge); |
71 | 77 |
72 // During a snapshot, samples are acquired and aggregated. This structure | 78 // During a snapshot, samples are acquired and aggregated. This structure |
73 // contains all the information collected for a given histogram. Once a | 79 // contains all the information collected for a given histogram. Once a |
74 // snapshot operation is finished, it is generally emptied except for | 80 // snapshot operation is finished, it is generally emptied except for |
75 // information that must persist from one report to the next, such as | 81 // information that must persist from one report to the next, such as |
76 // the "inconsistencies". | 82 // the "inconsistencies". |
77 struct SampleInfo { | 83 struct SampleInfo { |
78 SampleInfo() : histogram(nullptr), | |
79 accumulated_samples(nullptr), | |
80 inconsistencies(0) {} | |
81 | |
82 // A histogram associated with this sample; it may be one of many if | 84 // A histogram associated with this sample; it may be one of many if |
83 // several have been aggregated into the same "accumulated" sample set. | 85 // several have been aggregated into the same "accumulated" sample set. |
84 // Ownership of the histogram remains elsewhere and this pointer is | 86 // Ownership of the histogram remains elsewhere and this pointer is |
85 // cleared by FinishDeltas(). | 87 // cleared by FinishDeltas(). |
86 const HistogramBase* histogram; | 88 const HistogramBase* histogram = nullptr; |
87 | 89 |
88 // The current snapshot-delta values being accumulated. | 90 // The current snapshot-delta values being accumulated. |
89 // TODO(bcwhite): Change this to a scoped_ptr once all build architectures | 91 // TODO(bcwhite): Change this to a scoped_ptr once all build architectures |
90 // support such as the value of a std::map. | 92 // support such as the value of a std::map. |
91 HistogramSamples* accumulated_samples; | 93 HistogramSamples* accumulated_samples = nullptr; |
92 | 94 |
93 // The set of inconsistencies (flags) already seen for the histogram. | 95 // The set of inconsistencies (flags) already seen for the histogram. |
94 // See HistogramBase::Inconsistency for values. | 96 // See HistogramBase::Inconsistency for values. |
95 uint32_t inconsistencies; | 97 uint32_t inconsistencies = 0; |
96 }; | 98 }; |
97 | 99 |
98 // Capture and hold samples from a histogram. This does all the heavy | 100 // Capture and hold samples from a histogram. This does all the heavy |
99 // lifting for PrepareDelta() and PrepareAbsolute(). | 101 // lifting for PrepareDelta() and PrepareAbsolute(). |
100 void PrepareSamples(const HistogramBase* histogram, | 102 void PrepareSamples(const HistogramBase* histogram, |
101 scoped_ptr<HistogramSamples> samples); | 103 scoped_ptr<HistogramSamples> samples); |
102 | 104 |
103 // Try to detect and fix count inconsistency of logged samples. | 105 // Try to detect and fix count inconsistency of logged samples. |
104 void InspectLoggedSamplesInconsistency( | 106 void InspectLoggedSamplesInconsistency( |
105 const HistogramSamples& new_snapshot, | 107 const HistogramSamples& new_snapshot, |
106 HistogramSamples* logged_samples); | 108 HistogramSamples* logged_samples); |
107 | 109 |
108 // For histograms, track what has been previously seen, indexed | 110 // For histograms, track what has been previously seen, indexed |
109 // by the hash of the histogram name. | 111 // by the hash of the histogram name. |
110 std::map<uint64_t, SampleInfo> known_histograms_; | 112 std::map<uint64_t, SampleInfo> known_histograms_; |
111 | 113 |
| 114 // Collection of histograms of which ownership has been passed to this |
| 115 // object. They will be deleted by FinishDeltas(). |
| 116 std::vector<scoped_ptr<const HistogramBase>> owned_histograms_; |
| 117 |
112 // Indicates if deltas are currently being prepared. | 118 // Indicates if deltas are currently being prepared. |
113 bool preparing_deltas_; | 119 bool preparing_deltas_; |
114 | 120 |
115 // |histogram_flattener_| handles the logistics of recording the histogram | 121 // |histogram_flattener_| handles the logistics of recording the histogram |
116 // deltas. | 122 // deltas. |
117 HistogramFlattener* histogram_flattener_; // Weak. | 123 HistogramFlattener* histogram_flattener_; // Weak. |
118 | 124 |
119 DISALLOW_COPY_AND_ASSIGN(HistogramSnapshotManager); | 125 DISALLOW_COPY_AND_ASSIGN(HistogramSnapshotManager); |
120 }; | 126 }; |
121 | 127 |
122 } // namespace base | 128 } // namespace base |
123 | 129 |
124 #endif // BASE_METRICS_HISTOGRAM_SNAPSHOT_MANAGER_H_ | 130 #endif // BASE_METRICS_HISTOGRAM_SNAPSHOT_MANAGER_H_ |
OLD | NEW |