| 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 : preparing_deltas_(false), | 17 : preparing_deltas_(false), |
| 18 histogram_flattener_(histogram_flattener) { | 18 histogram_flattener_(histogram_flattener) { |
| 19 DCHECK(histogram_flattener_); | 19 DCHECK(histogram_flattener_); |
| 20 } | 20 } |
| 21 | 21 |
| 22 HistogramSnapshotManager::~HistogramSnapshotManager() { | 22 HistogramSnapshotManager::~HistogramSnapshotManager() { |
| 23 } | 23 } |
| 24 | 24 |
| 25 void HistogramSnapshotManager::StartDeltas() { | 25 void HistogramSnapshotManager::StartDeltas() { |
| 26 // Ensure that start/finish calls do not get nested. | 26 // Ensure that start/finish calls do not get nested. |
| 27 DCHECK(!preparing_deltas_); | 27 DCHECK(!preparing_deltas_); |
| 28 preparing_deltas_ = true; | 28 preparing_deltas_ = true; |
| 29 | 29 |
| 30 DCHECK(owned_histograms_.empty()); |
| 31 |
| 30 #ifdef DEBUG | 32 #ifdef DEBUG |
| 31 for (const auto& iter : known_histograms) { | |
| 32 CHECK(!iter->second.histogram); | 33 CHECK(!iter->second.histogram); |
| 33 CHECK(!iter->second.accumulated_samples); | 34 CHECK(!iter->second.accumulated_samples); |
| 34 CHECK(!(iter->second.inconsistencies & | 35 CHECK(!(iter->second.inconsistencies & |
| 35 HistogramBase::NEW_INCONSISTENCY_FOUND)); | 36 HistogramBase::NEW_INCONSISTENCY_FOUND)); |
| 36 } | 37 } |
| 37 #endif | 38 #endif |
| 38 } | 39 } |
| 39 | 40 |
| 40 void HistogramSnapshotManager::PrepareDelta(HistogramBase* histogram) { | 41 void HistogramSnapshotManager::PrepareDelta(HistogramBase* histogram) { |
| 41 PrepareSamples(histogram, histogram->SnapshotDelta()); | 42 PrepareSamples(histogram, histogram->SnapshotDelta(), false); |
| 43 } |
| 44 |
| 45 void HistogramSnapshotManager::PrepareDeltaTakingOwnership( |
| 46 scoped_ptr<HistogramBase> histogram) { |
| 47 // Snapshot must be done before releasing the pointer. |
| 48 scoped_ptr<HistogramSamples> samples = histogram->SnapshotDelta(); |
| 49 PrepareSamples(histogram.release(), std::move(samples), true); |
| 42 } | 50 } |
| 43 | 51 |
| 44 void HistogramSnapshotManager::PrepareAbsolute(const HistogramBase* histogram) { | 52 void HistogramSnapshotManager::PrepareAbsolute(const HistogramBase* histogram) { |
| 45 PrepareSamples(histogram, histogram->SnapshotSamples()); | 53 PrepareSamples(histogram, histogram->SnapshotSamples(), false); |
| 54 } |
| 55 |
| 56 void HistogramSnapshotManager::PrepareAbsoluteTakingOwnership( |
| 57 scoped_ptr<const HistogramBase> histogram) { |
| 58 // Snapshot must be done before releasing the pointer. |
| 59 scoped_ptr<HistogramSamples> samples = histogram->SnapshotSamples(); |
| 60 PrepareSamples(histogram.release(), std::move(samples), true); |
| 46 } | 61 } |
| 47 | 62 |
| 48 void HistogramSnapshotManager::FinishDeltas() { | 63 void HistogramSnapshotManager::FinishDeltas() { |
| 49 DCHECK(preparing_deltas_); | 64 DCHECK(preparing_deltas_); |
| 50 | 65 |
| 51 // Iterate over all known histograms to see what should be recorded. | 66 // Iterate over all known histograms to see what should be recorded. |
| 52 for (auto& iter : known_histograms_) { | 67 for (auto& hash_and_info : known_histograms_) { |
| 53 SampleInfo* sample_info = &iter.second; | 68 SampleInfo* sample_info = &hash_and_info.second; |
| 54 | 69 |
| 55 // First, record any histograms in which corruption was detected. | 70 // First, record any histograms in which corruption was detected. |
| 56 if (sample_info->inconsistencies & HistogramBase::NEW_INCONSISTENCY_FOUND) { | 71 if (sample_info->inconsistencies & HistogramBase::NEW_INCONSISTENCY_FOUND) { |
| 57 sample_info->inconsistencies &= ~HistogramBase::NEW_INCONSISTENCY_FOUND; | 72 sample_info->inconsistencies &= ~HistogramBase::NEW_INCONSISTENCY_FOUND; |
| 58 histogram_flattener_->UniqueInconsistencyDetected( | 73 histogram_flattener_->UniqueInconsistencyDetected( |
| 59 static_cast<HistogramBase::Inconsistency>( | 74 static_cast<HistogramBase::Inconsistency>( |
| 60 sample_info->inconsistencies)); | 75 sample_info->inconsistencies)); |
| 61 } | 76 } |
| 62 | 77 |
| 63 // Second, record actual accumulated deltas. | 78 // Second, record actual accumulated deltas. |
| 64 if (sample_info->accumulated_samples) { | 79 if (sample_info->accumulated_samples) { |
| 65 // TODO(bcwhite): Investigate using redundant_count() below to avoid | 80 // TODO(bcwhite): Investigate using redundant_count() below to avoid |
| 66 // additional pass through all the samples to calculate real total. | 81 // additional pass through all the samples to calculate real total. |
| 67 if (sample_info->accumulated_samples->TotalCount() > 0) { | 82 if (sample_info->accumulated_samples->TotalCount() > 0) { |
| 68 histogram_flattener_->RecordDelta(*sample_info->histogram, | 83 histogram_flattener_->RecordDelta(*sample_info->histogram, |
| 69 *sample_info->accumulated_samples); | 84 *sample_info->accumulated_samples); |
| 70 } | 85 } |
| 71 delete sample_info->accumulated_samples; | 86 delete sample_info->accumulated_samples; |
| 72 sample_info->accumulated_samples = nullptr; | 87 sample_info->accumulated_samples = nullptr; |
| 73 } | 88 } |
| 74 | 89 |
| 75 // The Histogram pointer must be cleared at this point because the owner | 90 // The Histogram pointer must be cleared at this point because the owner |
| 76 // is only required to keep it alive until FinishDeltas() completes. | 91 // is only required to keep it alive until FinishDeltas() completes. |
| 77 sample_info->histogram = nullptr; | 92 sample_info->histogram = nullptr; |
| 78 } | 93 } |
| 79 | 94 |
| 95 owned_histograms_.clear(); |
| 80 preparing_deltas_ = false; | 96 preparing_deltas_ = false; |
| 81 } | 97 } |
| 82 | 98 |
| 83 void HistogramSnapshotManager::PrepareSamples( | 99 void HistogramSnapshotManager::PrepareSamples( |
| 84 const HistogramBase* histogram, | 100 const HistogramBase* histogram, |
| 85 scoped_ptr<HistogramSamples> samples) { | 101 scoped_ptr<HistogramSamples> samples, |
| 102 bool take_ownership) { |
| 86 DCHECK(histogram_flattener_); | 103 DCHECK(histogram_flattener_); |
| 104 scoped_ptr<const HistogramBase> owned_histogram; |
| 87 | 105 |
| 88 // Get information known about this histogram. | 106 // Get information known about this histogram. |
| 89 SampleInfo* sample_info = &known_histograms_[histogram->name_hash()]; | 107 SampleInfo* sample_info = &known_histograms_[histogram->name_hash()]; |
| 90 if (sample_info->histogram) { | 108 if (sample_info->histogram) { |
| 91 DCHECK_EQ(sample_info->histogram->histogram_name(), | 109 DCHECK_EQ(sample_info->histogram->histogram_name(), |
| 92 histogram->histogram_name()) << "hash collision"; | 110 histogram->histogram_name()) << "hash collision"; |
| 111 // Passed histogram won't be needed past end of this method so store |
| 112 // it in a scoped_ptr that will release it when returning. |
| 113 if (take_ownership) |
| 114 owned_histogram.reset(histogram); |
| 93 } else { | 115 } else { |
| 94 // First time this histogram has been seen; datafill. | 116 // First time this histogram has been seen; datafill. |
| 95 sample_info->histogram = histogram; | 117 sample_info->histogram = histogram; |
| 118 // The histogram data will be needed up until "finished" so store |
| 119 // in member vector for releasing when that is done. |
| 120 if (take_ownership) |
| 121 owned_histograms_.push_back(make_scoped_ptr(histogram)); |
| 96 } | 122 } |
| 97 | 123 |
| 98 // Crash if we detect that our histograms have been overwritten. This may be | 124 // Crash if we detect that our histograms have been overwritten. This may be |
| 99 // a fair distance from the memory smasher, but we hope to correlate these | 125 // a fair distance from the memory smasher, but we hope to correlate these |
| 100 // crashes with other events, such as plugins, or usage patterns, etc. | 126 // crashes with other events, such as plugins, or usage patterns, etc. |
| 101 unsigned corruption = histogram->FindCorruption(*samples); | 127 unsigned corruption = histogram->FindCorruption(*samples); |
| 102 if (HistogramBase::BUCKET_ORDER_ERROR & corruption) { | 128 if (HistogramBase::BUCKET_ORDER_ERROR & corruption) { |
| 103 // The checksum should have caught this, so crash separately if it didn't. | 129 // The checksum should have caught this, so crash separately if it didn't. |
| 104 CHECK_NE(0U, HistogramBase::RANGE_CHECKSUM_ERROR & corruption); | 130 CHECK_NE(0U, HistogramBase::RANGE_CHECKSUM_ERROR & corruption); |
| 105 CHECK(false); // Crash for the bucket order corruption. | 131 CHECK(false); // Crash for the bucket order corruption. |
| (...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 144 | 170 |
| 145 histogram_flattener_->InconsistencyDetectedInLoggedCount(discrepancy); | 171 histogram_flattener_->InconsistencyDetectedInLoggedCount(discrepancy); |
| 146 if (discrepancy > Histogram::kCommonRaceBasedCountMismatch) { | 172 if (discrepancy > Histogram::kCommonRaceBasedCountMismatch) { |
| 147 // Fix logged_samples. | 173 // Fix logged_samples. |
| 148 logged_samples->Subtract(*logged_samples); | 174 logged_samples->Subtract(*logged_samples); |
| 149 logged_samples->Add(new_snapshot); | 175 logged_samples->Add(new_snapshot); |
| 150 } | 176 } |
| 151 } | 177 } |
| 152 | 178 |
| 153 } // namespace base | 179 } // namespace base |
| OLD | NEW |