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 3 // found in the LICENSE file. 4 5 #include "base/metrics/histogram_snapshot_manager.h" 6 7 #include <memory> 8 9 #include "base/debug/alias.h" 10 #include "base/metrics/histogram_flattener.h" 11 #include "base/metrics/histogram_samples.h" 12 #include "base/metrics/statistics_recorder.h" 13 #include "base/stl_util.h" 14 15 namespace base { 16 17 HistogramSnapshotManager::HistogramSnapshotManager( 18 HistogramFlattener* histogram_flattener) 19 : histogram_flattener_(histogram_flattener) { 20 DCHECK(histogram_flattener_); 21 } 22 23 HistogramSnapshotManager::~HistogramSnapshotManager() { 24 } 25 26 void HistogramSnapshotManager::PrepareDelta(HistogramBase* histogram) { 27 PrepareSamples(histogram, histogram->SnapshotDelta()); 28 } 29 30 void HistogramSnapshotManager::PrepareFinalDelta( 31 const HistogramBase* histogram) { 32 PrepareSamples(histogram, histogram->SnapshotFinalDelta()); 33 } 34 35 void HistogramSnapshotManager::PrepareSamples( 36 const HistogramBase* histogram, 37 std::unique_ptr<HistogramSamples> samples) { 38 DCHECK(histogram_flattener_); 39 40 // Get information known about this histogram. If it did not previously 41 // exist, one will be created and initialized. 42 SampleInfo* sample_info = &known_histograms_[histogram->name_hash()]; 43 44 // Crash if we detect that our histograms have been overwritten. This may be 45 // a fair distance from the memory smasher, but we hope to correlate these 46 // crashes with other events, such as plugins, or usage patterns, etc. 47 uint32_t corruption = histogram->FindCorruption(*samples); 48 if (HistogramBase::BUCKET_ORDER_ERROR & corruption) { 49 // Extract fields useful during debug. 50 const BucketRanges* ranges = 51 static_cast<const Histogram*>(histogram)->bucket_ranges(); 52 std::vector<HistogramBase::Sample> ranges_copy; 53 for (size_t i = 0; i < ranges->size(); ++i) 54 ranges_copy.push_back(ranges->range(i)); 55 HistogramBase::Sample* ranges_ptr = &ranges_copy[0]; 56 uint32_t ranges_checksum = ranges->checksum(); 57 uint32_t ranges_calc_checksum = ranges->CalculateChecksum(); 58 const char* histogram_name = histogram->histogram_name().c_str(); 59 int32_t flags = histogram->flags(); 60 // The checksum should have caught this, so crash separately if it didn't. 61 CHECK_NE(0U, HistogramBase::RANGE_CHECKSUM_ERROR & corruption); 62 CHECK(false); // Crash for the bucket order corruption. 63 // Ensure that compiler keeps around pointers to |histogram| and its 64 // internal |bucket_ranges_| for any minidumps. 65 base::debug::Alias(&ranges_ptr); 66 base::debug::Alias(&ranges_checksum); 67 base::debug::Alias(&ranges_calc_checksum); 68 base::debug::Alias(&histogram_name); 69 base::debug::Alias(&flags); 70 } 71 // Checksum corruption might not have caused order corruption. 72 CHECK_EQ(0U, HistogramBase::RANGE_CHECKSUM_ERROR & corruption); 73 74 // Note, at this point corruption can only be COUNT_HIGH_ERROR or 75 // COUNT_LOW_ERROR and they never arise together, so we don't need to extract 76 // bits from corruption. 77 if (corruption) { 78 DLOG(ERROR) << "Histogram: \"" << histogram->histogram_name() 79 << "\" has data corruption: " << corruption; 80 histogram_flattener_->InconsistencyDetected( 81 static_cast<HistogramBase::Inconsistency>(corruption)); 82 // Don't record corrupt data to metrics services. 83 const uint32_t old_corruption = sample_info->inconsistencies; 84 if (old_corruption == (corruption | old_corruption)) 85 return; // We've already seen this corruption for this histogram. 86 sample_info->inconsistencies |= corruption; 87 histogram_flattener_->UniqueInconsistencyDetected( 88 static_cast<HistogramBase::Inconsistency>(corruption)); 89 return; 90 } 91 92 if (samples->TotalCount() > 0) 93 histogram_flattener_->RecordDelta(*histogram, *samples); 94 } 95 96 void HistogramSnapshotManager::InspectLoggedSamplesInconsistency( 97 const HistogramSamples& new_snapshot, 98 HistogramSamples* logged_samples) { 99 HistogramBase::Count discrepancy = 100 logged_samples->TotalCount() - logged_samples->redundant_count(); 101 if (!discrepancy) 102 return; 103 104 histogram_flattener_->InconsistencyDetectedInLoggedCount(discrepancy); 105 if (discrepancy > Histogram::kCommonRaceBasedCountMismatch) { 106 // Fix logged_samples. 107 logged_samples->Subtract(*logged_samples); 108 logged_samples->Add(new_snapshot); 109 } 110 } 111 112 } // namespace base 113