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 #include "base/metrics/histogram_snapshot_manager.h" | 5 #include "base/metrics/histogram_snapshot_manager.h" |
6 | 6 |
7 #include "base/memory/scoped_ptr.h" | 7 #include "base/memory/scoped_ptr.h" |
8 #include "base/metrics/histogram_flattener.h" | 8 #include "base/metrics/histogram_flattener.h" |
9 #include "base/metrics/histogram_samples.h" | 9 #include "base/metrics/histogram_samples.h" |
10 #include "base/metrics/statistics_recorder.h" | 10 #include "base/metrics/statistics_recorder.h" |
11 #include "base/stl_util.h" | 11 #include "base/stl_util.h" |
12 | 12 |
13 namespace { | |
14 const int kNewInconsistency = (int)0x80000000; | |
Alexei Svitkine (slow)
2016/02/09 19:46:51
Needs a comment.
bcwhite
2016/02/11 16:42:39
Done.
| |
15 } // namespace | |
16 | |
13 namespace base { | 17 namespace base { |
14 | 18 |
15 HistogramSnapshotManager::HistogramSnapshotManager( | 19 HistogramSnapshotManager::HistogramSnapshotManager( |
16 HistogramFlattener* histogram_flattener) | 20 HistogramFlattener* histogram_flattener) |
17 : histogram_flattener_(histogram_flattener) { | 21 : preparing_deltas_(false), |
22 histogram_flattener_(histogram_flattener) { | |
18 DCHECK(histogram_flattener_); | 23 DCHECK(histogram_flattener_); |
19 } | 24 } |
20 | 25 |
21 HistogramSnapshotManager::~HistogramSnapshotManager() { | 26 HistogramSnapshotManager::~HistogramSnapshotManager() { |
22 STLDeleteValues(&logged_samples_); | |
23 } | 27 } |
24 | 28 |
25 void HistogramSnapshotManager::PrepareDelta(const HistogramBase& histogram) { | 29 void HistogramSnapshotManager::StartDeltas() { |
30 // Ensure that start/finish calls to not get nested. | |
31 DCHECK(!preparing_deltas_); | |
32 preparing_deltas_ = true; | |
33 | |
34 #ifdef DEBUG | |
35 for (auto iter : known_histograms) { | |
Alexei Svitkine (slow)
2016/02/09 19:46:51
Nit: (const auto& entry : known_histogram)
bcwhite
2016/02/11 16:42:38
Done.
| |
36 CHECK(!iter->second.histogram); | |
37 CHECK(!iter->second.accumulated); | |
38 CHECK(!(iter->second.inconsistencies & kNewInconsistency)); | |
39 } | |
40 #endif | |
41 } | |
42 | |
43 void HistogramSnapshotManager::PrepareDelta(HistogramBase* histogram) { | |
44 PrepareSamples(histogram, histogram->SnapshotDelta()); | |
45 } | |
46 | |
47 void HistogramSnapshotManager::PrepareOnce(const HistogramBase* histogram) { | |
48 PrepareSamples(histogram, histogram->SnapshotSamples()); | |
49 } | |
50 | |
51 void HistogramSnapshotManager::PrepareSamples( | |
52 const HistogramBase* histogram, | |
53 scoped_ptr<HistogramSamples> samples) { | |
26 DCHECK(histogram_flattener_); | 54 DCHECK(histogram_flattener_); |
27 | 55 |
28 // Get up-to-date snapshot of sample stats. | 56 // Get information known about this histogram. |
29 scoped_ptr<HistogramSamples> snapshot(histogram.SnapshotSamples()); | 57 SampleInfo* sample_info = &known_histograms_[histogram->name_hash()]; |
58 if (sample_info->histogram) { | |
59 DCHECK_EQ(sample_info->histogram->histogram_name(), | |
60 histogram->histogram_name()) << "hash collision"; | |
61 } else { | |
62 // First time this histogram has been seen; datafill. | |
63 sample_info->histogram = histogram; | |
64 } | |
30 | 65 |
31 // Crash if we detect that our histograms have been overwritten. This may be | 66 // Crash if we detect that our histograms have been overwritten. This may be |
32 // a fair distance from the memory smasher, but we hope to correlate these | 67 // a fair distance from the memory smasher, but we hope to correlate these |
33 // crashes with other events, such as plugins, or usage patterns, etc. | 68 // crashes with other events, such as plugins, or usage patterns, etc. |
34 int corruption = histogram.FindCorruption(*snapshot); | 69 int corruption = histogram->FindCorruption(*samples); |
35 if (HistogramBase::BUCKET_ORDER_ERROR & corruption) { | 70 if (HistogramBase::BUCKET_ORDER_ERROR & corruption) { |
36 // The checksum should have caught this, so crash separately if it didn't. | 71 // The checksum should have caught this, so crash separately if it didn't. |
37 CHECK_NE(0, HistogramBase::RANGE_CHECKSUM_ERROR & corruption); | 72 CHECK_NE(0, HistogramBase::RANGE_CHECKSUM_ERROR & corruption); |
38 CHECK(false); // Crash for the bucket order corruption. | 73 CHECK(false); // Crash for the bucket order corruption. |
39 } | 74 } |
40 // Checksum corruption might not have caused order corruption. | 75 // Checksum corruption might not have caused order corruption. |
41 CHECK_EQ(0, HistogramBase::RANGE_CHECKSUM_ERROR & corruption); | 76 CHECK_EQ(0, HistogramBase::RANGE_CHECKSUM_ERROR & corruption); |
42 | 77 |
43 // Note, at this point corruption can only be COUNT_HIGH_ERROR or | 78 // Note, at this point corruption can only be COUNT_HIGH_ERROR or |
44 // COUNT_LOW_ERROR and they never arise together, so we don't need to extract | 79 // COUNT_LOW_ERROR and they never arise together, so we don't need to extract |
45 // bits from corruption. | 80 // bits from corruption. |
46 const uint64_t histogram_hash = histogram.name_hash(); | |
47 if (corruption) { | 81 if (corruption) { |
48 DLOG(ERROR) << "Histogram: " << histogram.histogram_name() | 82 DLOG(ERROR) << "Histogram: \"" << histogram->histogram_name() |
49 << " has data corruption: " << corruption; | 83 << "\" has data corruption: " << corruption; |
50 histogram_flattener_->InconsistencyDetected( | 84 histogram_flattener_->InconsistencyDetected( |
51 static_cast<HistogramBase::Inconsistency>(corruption)); | 85 static_cast<HistogramBase::Inconsistency>(corruption)); |
52 // Don't record corrupt data to metrics services. | 86 // Don't record corrupt data to metrics services. |
53 int old_corruption = inconsistencies_[histogram_hash]; | 87 const int old_corruption = sample_info->inconsistencies; |
54 if (old_corruption == (corruption | old_corruption)) | 88 if (old_corruption == (corruption | old_corruption)) |
55 return; // We've already seen this corruption for this histogram. | 89 return; // We've already seen this corruption for this histogram. |
56 inconsistencies_[histogram_hash] |= corruption; | 90 sample_info->inconsistencies |= corruption | kNewInconsistency; |
Alexei Svitkine (slow)
2016/02/09 19:46:51
Can kNewInconsistency be added to the existing enu
bcwhite
2016/02/11 16:42:39
I can but it's used only internally here. It woul
Alexei Svitkine (slow)
2016/02/16 16:46:05
Yeah, I think it's still worth adding to the enum
bcwhite
2016/02/16 19:55:59
Done.
| |
57 histogram_flattener_->UniqueInconsistencyDetected( | 91 // TODO(bcwhite): Can we clear the inconsistency for future collection? |
58 static_cast<HistogramBase::Inconsistency>(corruption)); | |
59 return; | 92 return; |
60 } | 93 } |
61 | 94 |
62 HistogramSamples* to_log; | 95 if (!sample_info->accumulated) { |
63 auto it = logged_samples_.find(histogram_hash); | 96 // This histogram has not been seen before; add a new entry. |
64 if (it == logged_samples_.end()) { | 97 sample_info->accumulated = samples.release(); |
65 to_log = snapshot.release(); | 98 } else { |
99 // There are previous values from this histogram; add them together. | |
100 sample_info->accumulated->Add(*samples); | |
101 } | |
102 } | |
66 | 103 |
67 // This histogram has not been logged before, add a new entry. | 104 void HistogramSnapshotManager::FinishDeltas() { |
Alexei Svitkine (slow)
2016/02/09 19:46:51
Please order the function in the .cc file in the s
bcwhite
2016/02/11 16:42:39
Done.
| |
68 logged_samples_[histogram_hash] = to_log; | 105 DCHECK(preparing_deltas_); |
69 } else { | 106 |
70 HistogramSamples* already_logged = it->second; | 107 // Iterate over all known histograms to see what should be recorded. |
71 InspectLoggedSamplesInconsistency(*snapshot, already_logged); | 108 for (auto& iter : known_histograms_) { |
72 snapshot->Subtract(*already_logged); | 109 SampleInfo* sample_info = &iter.second; |
73 already_logged->Add(*snapshot); | 110 |
74 to_log = snapshot.get(); | 111 // First, record any histograms in which corruption was detected. |
112 if (sample_info->inconsistencies & kNewInconsistency) { | |
113 sample_info->inconsistencies &= ~kNewInconsistency; | |
114 histogram_flattener_->UniqueInconsistencyDetected( | |
Alexei Svitkine (slow)
2016/02/09 19:46:51
I don't understand why you moved it here rather th
bcwhite
2016/02/11 16:42:39
If done during aggregation, multiple reports could
| |
115 static_cast<HistogramBase::Inconsistency>( | |
116 sample_info->inconsistencies)); | |
117 } | |
118 | |
119 // Second, record actual accumulated deltas. | |
120 if (sample_info->accumulated) { | |
121 if (sample_info->accumulated->redundant_count() > 0) { | |
122 histogram_flattener_->RecordDelta(*sample_info->histogram, | |
123 *sample_info->accumulated); | |
124 } | |
125 delete sample_info->accumulated; | |
126 sample_info->accumulated = nullptr; | |
127 } | |
128 | |
129 // The Histogram pointer must be cleared at this point because the owner | |
130 // is only required to keep it alive until FinishDeltas() completes. | |
131 sample_info->histogram = nullptr; | |
75 } | 132 } |
76 | 133 |
77 if (to_log->TotalCount() > 0) | 134 preparing_deltas_ = false; |
78 histogram_flattener_->RecordDelta(histogram, *to_log); | |
79 } | 135 } |
80 | 136 |
81 void HistogramSnapshotManager::InspectLoggedSamplesInconsistency( | 137 void HistogramSnapshotManager::InspectLoggedSamplesInconsistency( |
82 const HistogramSamples& new_snapshot, | 138 const HistogramSamples& new_snapshot, |
83 HistogramSamples* logged_samples) { | 139 HistogramSamples* logged_samples) { |
84 HistogramBase::Count discrepancy = | 140 HistogramBase::Count discrepancy = |
85 logged_samples->TotalCount() - logged_samples->redundant_count(); | 141 logged_samples->TotalCount() - logged_samples->redundant_count(); |
86 if (!discrepancy) | 142 if (!discrepancy) |
87 return; | 143 return; |
88 | 144 |
89 histogram_flattener_->InconsistencyDetectedInLoggedCount(discrepancy); | 145 histogram_flattener_->InconsistencyDetectedInLoggedCount(discrepancy); |
90 if (discrepancy > Histogram::kCommonRaceBasedCountMismatch) { | 146 if (discrepancy > Histogram::kCommonRaceBasedCountMismatch) { |
91 // Fix logged_samples. | 147 // Fix logged_samples. |
92 logged_samples->Subtract(*logged_samples); | 148 logged_samples->Subtract(*logged_samples); |
93 logged_samples->Add(new_snapshot); | 149 logged_samples->Add(new_snapshot); |
94 } | 150 } |
95 } | 151 } |
96 | 152 |
97 } // namespace base | 153 } // namespace base |
OLD | NEW |