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 base { | 13 namespace base { |
14 | 14 |
15 HistogramSnapshotManager::HistogramSnapshotManager( | 15 HistogramSnapshotManager::HistogramSnapshotManager( |
16 HistogramFlattener* histogram_flattener) | 16 HistogramFlattener* histogram_flattener) |
17 : histogram_flattener_(histogram_flattener) { | 17 : preparing_deltas_(false), |
| 18 histogram_flattener_(histogram_flattener) { |
18 DCHECK(histogram_flattener_); | 19 DCHECK(histogram_flattener_); |
19 } | 20 } |
20 | 21 |
21 HistogramSnapshotManager::~HistogramSnapshotManager() { | 22 HistogramSnapshotManager::~HistogramSnapshotManager() { |
22 STLDeleteValues(&logged_samples_); | |
23 } | 23 } |
24 | 24 |
25 void HistogramSnapshotManager::PrepareDelta(const HistogramBase& histogram) { | 25 void HistogramSnapshotManager::StartDeltas() { |
| 26 // Ensure that start/finish calls do not get nested. |
| 27 DCHECK(!preparing_deltas_); |
| 28 preparing_deltas_ = true; |
| 29 |
| 30 #ifdef DEBUG |
| 31 for (const auto& iter : known_histograms) { |
| 32 CHECK(!iter->second.histogram); |
| 33 CHECK(!iter->second.accumulated_samples); |
| 34 CHECK(!(iter->second.inconsistencies & |
| 35 HistogramBase::NEW_INCONSISTENCY_FOUND)); |
| 36 } |
| 37 #endif |
| 38 } |
| 39 |
| 40 void HistogramSnapshotManager::PrepareDelta(HistogramBase* histogram) { |
| 41 PrepareSamples(histogram, histogram->SnapshotDelta()); |
| 42 } |
| 43 |
| 44 void HistogramSnapshotManager::PrepareAbsolute(const HistogramBase* histogram) { |
| 45 PrepareSamples(histogram, histogram->SnapshotSamples()); |
| 46 } |
| 47 |
| 48 void HistogramSnapshotManager::FinishDeltas() { |
| 49 DCHECK(preparing_deltas_); |
| 50 |
| 51 // Iterate over all known histograms to see what should be recorded. |
| 52 for (auto& iter : known_histograms_) { |
| 53 SampleInfo* sample_info = &iter.second; |
| 54 |
| 55 // First, record any histograms in which corruption was detected. |
| 56 if (sample_info->inconsistencies & HistogramBase::NEW_INCONSISTENCY_FOUND) { |
| 57 sample_info->inconsistencies &= ~HistogramBase::NEW_INCONSISTENCY_FOUND; |
| 58 histogram_flattener_->UniqueInconsistencyDetected( |
| 59 static_cast<HistogramBase::Inconsistency>( |
| 60 sample_info->inconsistencies)); |
| 61 } |
| 62 |
| 63 // Second, record actual accumulated deltas. |
| 64 if (sample_info->accumulated_samples) { |
| 65 // TODO(bcwhite): Investigate using redundant_count() below to avoid |
| 66 // additional pass through all the samples to calculate real total. |
| 67 if (sample_info->accumulated_samples->TotalCount() > 0) { |
| 68 histogram_flattener_->RecordDelta(*sample_info->histogram, |
| 69 *sample_info->accumulated_samples); |
| 70 } |
| 71 delete sample_info->accumulated_samples; |
| 72 sample_info->accumulated_samples = nullptr; |
| 73 } |
| 74 |
| 75 // The Histogram pointer must be cleared at this point because the owner |
| 76 // is only required to keep it alive until FinishDeltas() completes. |
| 77 sample_info->histogram = nullptr; |
| 78 } |
| 79 |
| 80 preparing_deltas_ = false; |
| 81 } |
| 82 |
| 83 void HistogramSnapshotManager::PrepareSamples( |
| 84 const HistogramBase* histogram, |
| 85 scoped_ptr<HistogramSamples> samples) { |
26 DCHECK(histogram_flattener_); | 86 DCHECK(histogram_flattener_); |
27 | 87 |
28 // Get up-to-date snapshot of sample stats. | 88 // Get information known about this histogram. |
29 scoped_ptr<HistogramSamples> snapshot(histogram.SnapshotSamples()); | 89 SampleInfo* sample_info = &known_histograms_[histogram->name_hash()]; |
| 90 if (sample_info->histogram) { |
| 91 DCHECK_EQ(sample_info->histogram->histogram_name(), |
| 92 histogram->histogram_name()) << "hash collision"; |
| 93 } else { |
| 94 // First time this histogram has been seen; datafill. |
| 95 sample_info->histogram = histogram; |
| 96 } |
30 | 97 |
31 // Crash if we detect that our histograms have been overwritten. This may be | 98 // 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 | 99 // 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. | 100 // crashes with other events, such as plugins, or usage patterns, etc. |
34 int corruption = histogram.FindCorruption(*snapshot); | 101 uint32_t corruption = histogram->FindCorruption(*samples); |
35 if (HistogramBase::BUCKET_ORDER_ERROR & corruption) { | 102 if (HistogramBase::BUCKET_ORDER_ERROR & corruption) { |
36 // The checksum should have caught this, so crash separately if it didn't. | 103 // The checksum should have caught this, so crash separately if it didn't. |
37 CHECK_NE(0, HistogramBase::RANGE_CHECKSUM_ERROR & corruption); | 104 CHECK_NE(0U, HistogramBase::RANGE_CHECKSUM_ERROR & corruption); |
38 CHECK(false); // Crash for the bucket order corruption. | 105 CHECK(false); // Crash for the bucket order corruption. |
39 } | 106 } |
40 // Checksum corruption might not have caused order corruption. | 107 // Checksum corruption might not have caused order corruption. |
41 CHECK_EQ(0, HistogramBase::RANGE_CHECKSUM_ERROR & corruption); | 108 CHECK_EQ(0U, HistogramBase::RANGE_CHECKSUM_ERROR & corruption); |
42 | 109 |
43 // Note, at this point corruption can only be COUNT_HIGH_ERROR or | 110 // 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 | 111 // COUNT_LOW_ERROR and they never arise together, so we don't need to extract |
45 // bits from corruption. | 112 // bits from corruption. |
46 const uint64_t histogram_hash = histogram.name_hash(); | |
47 if (corruption) { | 113 if (corruption) { |
48 DLOG(ERROR) << "Histogram: " << histogram.histogram_name() | 114 DLOG(ERROR) << "Histogram: \"" << histogram->histogram_name() |
49 << " has data corruption: " << corruption; | 115 << "\" has data corruption: " << corruption; |
50 histogram_flattener_->InconsistencyDetected( | 116 histogram_flattener_->InconsistencyDetected( |
51 static_cast<HistogramBase::Inconsistency>(corruption)); | 117 static_cast<HistogramBase::Inconsistency>(corruption)); |
52 // Don't record corrupt data to metrics services. | 118 // Don't record corrupt data to metrics services. |
53 int old_corruption = inconsistencies_[histogram_hash]; | 119 const uint32_t old_corruption = sample_info->inconsistencies; |
54 if (old_corruption == (corruption | old_corruption)) | 120 if (old_corruption == (corruption | old_corruption)) |
55 return; // We've already seen this corruption for this histogram. | 121 return; // We've already seen this corruption for this histogram. |
56 inconsistencies_[histogram_hash] |= corruption; | 122 sample_info->inconsistencies |= |
57 histogram_flattener_->UniqueInconsistencyDetected( | 123 corruption | HistogramBase::NEW_INCONSISTENCY_FOUND; |
58 static_cast<HistogramBase::Inconsistency>(corruption)); | 124 // TODO(bcwhite): Can we clear the inconsistency for future collection? |
59 return; | 125 return; |
60 } | 126 } |
61 | 127 |
62 HistogramSamples* to_log; | 128 if (!sample_info->accumulated_samples) { |
63 auto it = logged_samples_.find(histogram_hash); | 129 // This histogram has not been seen before; add it as a new entry. |
64 if (it == logged_samples_.end()) { | 130 sample_info->accumulated_samples = samples.release(); |
65 to_log = snapshot.release(); | |
66 | |
67 // This histogram has not been logged before, add a new entry. | |
68 logged_samples_[histogram_hash] = to_log; | |
69 } else { | 131 } else { |
70 HistogramSamples* already_logged = it->second; | 132 // There are previous values from this histogram; add them together. |
71 InspectLoggedSamplesInconsistency(*snapshot, already_logged); | 133 sample_info->accumulated_samples->Add(*samples); |
72 snapshot->Subtract(*already_logged); | |
73 already_logged->Add(*snapshot); | |
74 to_log = snapshot.get(); | |
75 } | 134 } |
76 | |
77 if (to_log->TotalCount() > 0) | |
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 |