• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
HistogramSnapshotManager(HistogramFlattener * histogram_flattener)17 HistogramSnapshotManager::HistogramSnapshotManager(
18     HistogramFlattener* histogram_flattener)
19     : histogram_flattener_(histogram_flattener) {
20   DCHECK(histogram_flattener_);
21 }
22 
~HistogramSnapshotManager()23 HistogramSnapshotManager::~HistogramSnapshotManager() {
24 }
25 
PrepareDelta(HistogramBase * histogram)26 void HistogramSnapshotManager::PrepareDelta(HistogramBase* histogram) {
27   PrepareSamples(histogram, histogram->SnapshotDelta());
28 }
29 
PrepareFinalDelta(const HistogramBase * histogram)30 void HistogramSnapshotManager::PrepareFinalDelta(
31     const HistogramBase* histogram) {
32   PrepareSamples(histogram, histogram->SnapshotFinalDelta());
33 }
34 
PrepareSamples(const HistogramBase * histogram,std::unique_ptr<HistogramSamples> samples)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     const char* histogram_name = histogram->histogram_name().c_str();
57     int32_t flags = histogram->flags();
58     // The checksum should have caught this, so crash separately if it didn't.
59     CHECK_NE(0U, HistogramBase::RANGE_CHECKSUM_ERROR & corruption);
60     CHECK(false);  // Crash for the bucket order corruption.
61     // Ensure that compiler keeps around pointers to |histogram| and its
62     // internal |bucket_ranges_| for any minidumps.
63     base::debug::Alias(&ranges_ptr);
64     base::debug::Alias(&histogram_name);
65     base::debug::Alias(&flags);
66   }
67   // Checksum corruption might not have caused order corruption.
68   CHECK_EQ(0U, HistogramBase::RANGE_CHECKSUM_ERROR & corruption);
69 
70   // Note, at this point corruption can only be COUNT_HIGH_ERROR or
71   // COUNT_LOW_ERROR and they never arise together, so we don't need to extract
72   // bits from corruption.
73   if (corruption) {
74     DLOG(ERROR) << "Histogram: \"" << histogram->histogram_name()
75                 << "\" has data corruption: " << corruption;
76     histogram_flattener_->InconsistencyDetected(
77         static_cast<HistogramBase::Inconsistency>(corruption));
78     // Don't record corrupt data to metrics services.
79     const uint32_t old_corruption = sample_info->inconsistencies;
80     if (old_corruption == (corruption | old_corruption))
81       return;  // We've already seen this corruption for this histogram.
82     sample_info->inconsistencies |= corruption;
83     histogram_flattener_->UniqueInconsistencyDetected(
84         static_cast<HistogramBase::Inconsistency>(corruption));
85     return;
86   }
87 
88   if (samples->TotalCount() > 0)
89     histogram_flattener_->RecordDelta(*histogram, *samples);
90 }
91 
InspectLoggedSamplesInconsistency(const HistogramSamples & new_snapshot,HistogramSamples * logged_samples)92 void HistogramSnapshotManager::InspectLoggedSamplesInconsistency(
93       const HistogramSamples& new_snapshot,
94       HistogramSamples* logged_samples) {
95   HistogramBase::Count discrepancy =
96       logged_samples->TotalCount() - logged_samples->redundant_count();
97   if (!discrepancy)
98     return;
99 
100   histogram_flattener_->InconsistencyDetectedInLoggedCount(discrepancy);
101   if (discrepancy > Histogram::kCommonRaceBasedCountMismatch) {
102     // Fix logged_samples.
103     logged_samples->Subtract(*logged_samples);
104     logged_samples->Add(new_snapshot);
105   }
106 }
107 
108 }  // namespace base
109