| 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()); |
| 42 } | 43 } |
| 43 | 44 |
| 45 void HistogramSnapshotManager::PrepareDeltaTakingOwnership( |
| 46 scoped_ptr<HistogramBase> histogram) { |
| 47 PrepareSamples(histogram.get(), histogram->SnapshotDelta()); |
| 48 owned_histograms_.push_back(std::move(histogram)); |
| 49 } |
| 50 |
| 44 void HistogramSnapshotManager::PrepareAbsolute(const HistogramBase* histogram) { | 51 void HistogramSnapshotManager::PrepareAbsolute(const HistogramBase* histogram) { |
| 45 PrepareSamples(histogram, histogram->SnapshotSamples()); | 52 PrepareSamples(histogram, histogram->SnapshotSamples()); |
| 46 } | 53 } |
| 47 | 54 |
| 55 void HistogramSnapshotManager::PrepareAbsoluteTakingOwnership( |
| 56 scoped_ptr<const HistogramBase> histogram) { |
| 57 PrepareSamples(histogram.get(), histogram->SnapshotSamples()); |
| 58 owned_histograms_.push_back(std::move(histogram)); |
| 59 } |
| 60 |
| 48 void HistogramSnapshotManager::FinishDeltas() { | 61 void HistogramSnapshotManager::FinishDeltas() { |
| 49 DCHECK(preparing_deltas_); | 62 DCHECK(preparing_deltas_); |
| 50 | 63 |
| 51 // Iterate over all known histograms to see what should be recorded. | 64 // Iterate over all known histograms to see what should be recorded. |
| 52 for (auto& iter : known_histograms_) { | 65 for (auto& hash_and_info : known_histograms_) { |
| 53 SampleInfo* sample_info = &iter.second; | 66 SampleInfo* sample_info = &hash_and_info.second; |
| 54 | 67 |
| 55 // First, record any histograms in which corruption was detected. | 68 // First, record any histograms in which corruption was detected. |
| 56 if (sample_info->inconsistencies & HistogramBase::NEW_INCONSISTENCY_FOUND) { | 69 if (sample_info->inconsistencies & HistogramBase::NEW_INCONSISTENCY_FOUND) { |
| 57 sample_info->inconsistencies &= ~HistogramBase::NEW_INCONSISTENCY_FOUND; | 70 sample_info->inconsistencies &= ~HistogramBase::NEW_INCONSISTENCY_FOUND; |
| 58 histogram_flattener_->UniqueInconsistencyDetected( | 71 histogram_flattener_->UniqueInconsistencyDetected( |
| 59 static_cast<HistogramBase::Inconsistency>( | 72 static_cast<HistogramBase::Inconsistency>( |
| 60 sample_info->inconsistencies)); | 73 sample_info->inconsistencies)); |
| 61 } | 74 } |
| 62 | 75 |
| 63 // Second, record actual accumulated deltas. | 76 // Second, record actual accumulated deltas. |
| 64 if (sample_info->accumulated_samples) { | 77 if (sample_info->accumulated_samples) { |
| 65 // TODO(bcwhite): Investigate using redundant_count() below to avoid | 78 // TODO(bcwhite): Investigate using redundant_count() below to avoid |
| 66 // additional pass through all the samples to calculate real total. | 79 // additional pass through all the samples to calculate real total. |
| 67 if (sample_info->accumulated_samples->TotalCount() > 0) { | 80 if (sample_info->accumulated_samples->TotalCount() > 0) { |
| 68 histogram_flattener_->RecordDelta(*sample_info->histogram, | 81 histogram_flattener_->RecordDelta(*sample_info->histogram, |
| 69 *sample_info->accumulated_samples); | 82 *sample_info->accumulated_samples); |
| 70 } | 83 } |
| 71 delete sample_info->accumulated_samples; | 84 delete sample_info->accumulated_samples; |
| 72 sample_info->accumulated_samples = nullptr; | 85 sample_info->accumulated_samples = nullptr; |
| 73 } | 86 } |
| 74 | 87 |
| 75 // The Histogram pointer must be cleared at this point because the owner | 88 // The Histogram pointer must be cleared at this point because the owner |
| 76 // is only required to keep it alive until FinishDeltas() completes. | 89 // is only required to keep it alive until FinishDeltas() completes. |
| 77 sample_info->histogram = nullptr; | 90 sample_info->histogram = nullptr; |
| 78 } | 91 } |
| 79 | 92 |
| 93 owned_histograms_.clear(); |
| 80 preparing_deltas_ = false; | 94 preparing_deltas_ = false; |
| 81 } | 95 } |
| 82 | 96 |
| 83 void HistogramSnapshotManager::PrepareSamples( | 97 void HistogramSnapshotManager::PrepareSamples( |
| 84 const HistogramBase* histogram, | 98 const HistogramBase* histogram, |
| 85 scoped_ptr<HistogramSamples> samples) { | 99 scoped_ptr<HistogramSamples> samples) { |
| 86 DCHECK(histogram_flattener_); | 100 DCHECK(histogram_flattener_); |
| 87 | 101 |
| 88 // Get information known about this histogram. | 102 // Get information known about this histogram. |
| 89 SampleInfo* sample_info = &known_histograms_[histogram->name_hash()]; | 103 SampleInfo* sample_info = &known_histograms_[histogram->name_hash()]; |
| (...skipping 54 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 144 | 158 |
| 145 histogram_flattener_->InconsistencyDetectedInLoggedCount(discrepancy); | 159 histogram_flattener_->InconsistencyDetectedInLoggedCount(discrepancy); |
| 146 if (discrepancy > Histogram::kCommonRaceBasedCountMismatch) { | 160 if (discrepancy > Histogram::kCommonRaceBasedCountMismatch) { |
| 147 // Fix logged_samples. | 161 // Fix logged_samples. |
| 148 logged_samples->Subtract(*logged_samples); | 162 logged_samples->Subtract(*logged_samples); |
| 149 logged_samples->Add(new_snapshot); | 163 logged_samples->Add(new_snapshot); |
| 150 } | 164 } |
| 151 } | 165 } |
| 152 | 166 |
| 153 } // namespace base | 167 } // namespace base |
| OLD | NEW |