| 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 : histogram_flattener_(histogram_flattener) { |
| 18 DCHECK(histogram_flattener_); | 18 DCHECK(histogram_flattener_); |
| 19 } | 19 } |
| 20 | 20 |
| 21 HistogramSnapshotManager::~HistogramSnapshotManager() { | 21 HistogramSnapshotManager::~HistogramSnapshotManager() { |
| 22 STLDeleteValues(&logged_samples_); | 22 STLDeleteValues(&logged_samples_); |
| 23 } | 23 } |
| 24 | 24 |
| 25 void HistogramSnapshotManager::PrepareDeltas( | |
| 26 HistogramBase::Flags flag_to_set, | |
| 27 HistogramBase::Flags required_flags) { | |
| 28 StatisticsRecorder::Histograms histograms; | |
| 29 StatisticsRecorder::GetHistograms(&histograms); | |
| 30 for (StatisticsRecorder::Histograms::const_iterator it = histograms.begin(); | |
| 31 histograms.end() != it; | |
| 32 ++it) { | |
| 33 (*it)->SetFlags(flag_to_set); | |
| 34 if (((*it)->flags() & required_flags) == required_flags) | |
| 35 PrepareDelta(**it); | |
| 36 } | |
| 37 } | |
| 38 | |
| 39 void HistogramSnapshotManager::PrepareDelta(const HistogramBase& histogram) { | 25 void HistogramSnapshotManager::PrepareDelta(const HistogramBase& histogram) { |
| 40 DCHECK(histogram_flattener_); | 26 DCHECK(histogram_flattener_); |
| 41 | 27 |
| 42 // Get up-to-date snapshot of sample stats. | 28 // Get up-to-date snapshot of sample stats. |
| 43 scoped_ptr<HistogramSamples> snapshot(histogram.SnapshotSamples()); | 29 scoped_ptr<HistogramSamples> snapshot(histogram.SnapshotSamples()); |
| 44 const std::string& histogram_name = histogram.histogram_name(); | 30 const std::string& histogram_name = histogram.histogram_name(); |
| 45 const uint64_t histogram_id = histogram.name_hash(); | 31 const uint64_t histogram_hash = histogram.name_hash(); |
| 46 | 32 |
| 47 int corruption = histogram.FindCorruption(*snapshot); | 33 int corruption = histogram.FindCorruption(*snapshot); |
| 48 | 34 |
| 49 // Crash if we detect that our histograms have been overwritten. This may be | 35 // Crash if we detect that our histograms have been overwritten. This may be |
| 50 // a fair distance from the memory smasher, but we hope to correlate these | 36 // a fair distance from the memory smasher, but we hope to correlate these |
| 51 // crashes with other events, such as plugins, or usage patterns, etc. | 37 // crashes with other events, such as plugins, or usage patterns, etc. |
| 52 if (HistogramBase::BUCKET_ORDER_ERROR & corruption) { | 38 if (HistogramBase::BUCKET_ORDER_ERROR & corruption) { |
| 53 // The checksum should have caught this, so crash separately if it didn't. | 39 // The checksum should have caught this, so crash separately if it didn't. |
| 54 CHECK_NE(0, HistogramBase::RANGE_CHECKSUM_ERROR & corruption); | 40 CHECK_NE(0, HistogramBase::RANGE_CHECKSUM_ERROR & corruption); |
| 55 CHECK(false); // Crash for the bucket order corruption. | 41 CHECK(false); // Crash for the bucket order corruption. |
| 56 } | 42 } |
| 57 // Checksum corruption might not have caused order corruption. | 43 // Checksum corruption might not have caused order corruption. |
| 58 CHECK_EQ(0, HistogramBase::RANGE_CHECKSUM_ERROR & corruption); | 44 CHECK_EQ(0, HistogramBase::RANGE_CHECKSUM_ERROR & corruption); |
| 59 | 45 |
| 60 // Note, at this point corruption can only be COUNT_HIGH_ERROR or | 46 // Note, at this point corruption can only be COUNT_HIGH_ERROR or |
| 61 // COUNT_LOW_ERROR and they never arise together, so we don't need to extract | 47 // COUNT_LOW_ERROR and they never arise together, so we don't need to extract |
| 62 // bits from corruption. | 48 // bits from corruption. |
| 63 if (corruption) { | 49 if (corruption) { |
| 64 DLOG(ERROR) << "Histogram: " << histogram_name | 50 DLOG(ERROR) << "Histogram: " << histogram_name |
| 65 << " has data corruption: " << corruption; | 51 << " has data corruption: " << corruption; |
| 66 histogram_flattener_->InconsistencyDetected( | 52 histogram_flattener_->InconsistencyDetected( |
| 67 static_cast<HistogramBase::Inconsistency>(corruption)); | 53 static_cast<HistogramBase::Inconsistency>(corruption)); |
| 68 // Don't record corrupt data to metrics services. | 54 // Don't record corrupt data to metrics services. |
| 69 int old_corruption = inconsistencies_[histogram_id]; | 55 int old_corruption = inconsistencies_[histogram_hash]; |
| 70 if (old_corruption == (corruption | old_corruption)) | 56 if (old_corruption == (corruption | old_corruption)) |
| 71 return; // We've already seen this corruption for this histogram. | 57 return; // We've already seen this corruption for this histogram. |
| 72 inconsistencies_[histogram_id] |= corruption; | 58 inconsistencies_[histogram_hash] |= corruption; |
| 73 histogram_flattener_->UniqueInconsistencyDetected( | 59 histogram_flattener_->UniqueInconsistencyDetected( |
| 74 static_cast<HistogramBase::Inconsistency>(corruption)); | 60 static_cast<HistogramBase::Inconsistency>(corruption)); |
| 75 return; | 61 return; |
| 76 } | 62 } |
| 77 | 63 |
| 78 HistogramSamples* to_log; | 64 HistogramSamples* to_log; |
| 79 std::map<uint64_t, HistogramSamples*>::iterator it = | 65 std::map<uint64_t, HistogramSamples*>::iterator it = |
| 80 logged_samples_.find(histogram_id); | 66 logged_samples_.find(histogram_hash); |
| 81 if (it == logged_samples_.end()) { | 67 if (it == logged_samples_.end()) { |
| 82 to_log = snapshot.release(); | 68 to_log = snapshot.release(); |
| 83 | 69 |
| 84 // This histogram has not been logged before, add a new entry. | 70 // This histogram has not been logged before, add a new entry. |
| 85 logged_samples_[histogram_id] = to_log; | 71 logged_samples_[histogram_hash] = to_log; |
| 86 } else { | 72 } else { |
| 87 HistogramSamples* already_logged = it->second; | 73 HistogramSamples* already_logged = it->second; |
| 88 InspectLoggedSamplesInconsistency(*snapshot, already_logged); | 74 InspectLoggedSamplesInconsistency(*snapshot, already_logged); |
| 89 snapshot->Subtract(*already_logged); | 75 snapshot->Subtract(*already_logged); |
| 90 already_logged->Add(*snapshot); | 76 already_logged->Add(*snapshot); |
| 91 to_log = snapshot.get(); | 77 to_log = snapshot.get(); |
| 92 } | 78 } |
| 93 | 79 |
| 94 if (to_log->TotalCount() > 0) | 80 if (to_log->TotalCount() > 0) |
| 95 histogram_flattener_->RecordDelta(histogram, *to_log); | 81 histogram_flattener_->RecordDelta(histogram, *to_log); |
| 96 } | 82 } |
| 97 | 83 |
| 98 void HistogramSnapshotManager::InspectLoggedSamplesInconsistency( | 84 void HistogramSnapshotManager::InspectLoggedSamplesInconsistency( |
| 99 const HistogramSamples& new_snapshot, | 85 const HistogramSamples& new_snapshot, |
| 100 HistogramSamples* logged_samples) { | 86 HistogramSamples* logged_samples) { |
| 101 HistogramBase::Count discrepancy = | 87 HistogramBase::Count discrepancy = |
| 102 logged_samples->TotalCount() - logged_samples->redundant_count(); | 88 logged_samples->TotalCount() - logged_samples->redundant_count(); |
| 103 if (!discrepancy) | 89 if (!discrepancy) |
| 104 return; | 90 return; |
| 105 | 91 |
| 106 histogram_flattener_->InconsistencyDetectedInLoggedCount(discrepancy); | 92 histogram_flattener_->InconsistencyDetectedInLoggedCount(discrepancy); |
| 107 if (discrepancy > Histogram::kCommonRaceBasedCountMismatch) { | 93 if (discrepancy > Histogram::kCommonRaceBasedCountMismatch) { |
| 108 // Fix logged_samples. | 94 // Fix logged_samples. |
| 109 logged_samples->Subtract(*logged_samples); | 95 logged_samples->Subtract(*logged_samples); |
| 110 logged_samples->Add(new_snapshot); | 96 logged_samples->Add(new_snapshot); |
| 111 } | 97 } |
| 112 } | 98 } |
| 113 | 99 |
| 114 } // namespace base | 100 } // namespace base |
| OLD | NEW |