1*635a8641SAndroid Build Coastguard Worker // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2*635a8641SAndroid Build Coastguard Worker // Use of this source code is governed by a BSD-style license that can be
3*635a8641SAndroid Build Coastguard Worker // found in the LICENSE file.
4*635a8641SAndroid Build Coastguard Worker
5*635a8641SAndroid Build Coastguard Worker #include "base/metrics/histogram_snapshot_manager.h"
6*635a8641SAndroid Build Coastguard Worker
7*635a8641SAndroid Build Coastguard Worker #include <memory>
8*635a8641SAndroid Build Coastguard Worker
9*635a8641SAndroid Build Coastguard Worker #include "base/debug/alias.h"
10*635a8641SAndroid Build Coastguard Worker #include "base/metrics/histogram_flattener.h"
11*635a8641SAndroid Build Coastguard Worker #include "base/metrics/histogram_samples.h"
12*635a8641SAndroid Build Coastguard Worker #include "base/metrics/statistics_recorder.h"
13*635a8641SAndroid Build Coastguard Worker #include "base/stl_util.h"
14*635a8641SAndroid Build Coastguard Worker
15*635a8641SAndroid Build Coastguard Worker namespace base {
16*635a8641SAndroid Build Coastguard Worker
17*635a8641SAndroid Build Coastguard Worker namespace {
18*635a8641SAndroid Build Coastguard Worker
19*635a8641SAndroid Build Coastguard Worker // A simple object to set an "active" flag and clear it upon destruction. It is
20*635a8641SAndroid Build Coastguard Worker // an error if the flag is already set.
21*635a8641SAndroid Build Coastguard Worker class MakeActive {
22*635a8641SAndroid Build Coastguard Worker public:
MakeActive(std::atomic<bool> * is_active)23*635a8641SAndroid Build Coastguard Worker MakeActive(std::atomic<bool>* is_active) : is_active_(is_active) {
24*635a8641SAndroid Build Coastguard Worker bool was_active = is_active_->exchange(true, std::memory_order_relaxed);
25*635a8641SAndroid Build Coastguard Worker CHECK(!was_active);
26*635a8641SAndroid Build Coastguard Worker }
~MakeActive()27*635a8641SAndroid Build Coastguard Worker ~MakeActive() { is_active_->store(false, std::memory_order_relaxed); }
28*635a8641SAndroid Build Coastguard Worker
29*635a8641SAndroid Build Coastguard Worker private:
30*635a8641SAndroid Build Coastguard Worker std::atomic<bool>* is_active_;
31*635a8641SAndroid Build Coastguard Worker
32*635a8641SAndroid Build Coastguard Worker DISALLOW_COPY_AND_ASSIGN(MakeActive);
33*635a8641SAndroid Build Coastguard Worker };
34*635a8641SAndroid Build Coastguard Worker
35*635a8641SAndroid Build Coastguard Worker } // namespace
36*635a8641SAndroid Build Coastguard Worker
HistogramSnapshotManager(HistogramFlattener * histogram_flattener)37*635a8641SAndroid Build Coastguard Worker HistogramSnapshotManager::HistogramSnapshotManager(
38*635a8641SAndroid Build Coastguard Worker HistogramFlattener* histogram_flattener)
39*635a8641SAndroid Build Coastguard Worker : histogram_flattener_(histogram_flattener) {
40*635a8641SAndroid Build Coastguard Worker DCHECK(histogram_flattener_);
41*635a8641SAndroid Build Coastguard Worker is_active_.store(false, std::memory_order_relaxed);
42*635a8641SAndroid Build Coastguard Worker }
43*635a8641SAndroid Build Coastguard Worker
44*635a8641SAndroid Build Coastguard Worker HistogramSnapshotManager::~HistogramSnapshotManager() = default;
45*635a8641SAndroid Build Coastguard Worker
PrepareDeltas(const std::vector<HistogramBase * > & histograms,HistogramBase::Flags flags_to_set,HistogramBase::Flags required_flags)46*635a8641SAndroid Build Coastguard Worker void HistogramSnapshotManager::PrepareDeltas(
47*635a8641SAndroid Build Coastguard Worker const std::vector<HistogramBase*>& histograms,
48*635a8641SAndroid Build Coastguard Worker HistogramBase::Flags flags_to_set,
49*635a8641SAndroid Build Coastguard Worker HistogramBase::Flags required_flags) {
50*635a8641SAndroid Build Coastguard Worker for (HistogramBase* const histogram : histograms) {
51*635a8641SAndroid Build Coastguard Worker histogram->SetFlags(flags_to_set);
52*635a8641SAndroid Build Coastguard Worker if ((histogram->flags() & required_flags) == required_flags)
53*635a8641SAndroid Build Coastguard Worker PrepareDelta(histogram);
54*635a8641SAndroid Build Coastguard Worker }
55*635a8641SAndroid Build Coastguard Worker }
56*635a8641SAndroid Build Coastguard Worker
PrepareDelta(HistogramBase * histogram)57*635a8641SAndroid Build Coastguard Worker void HistogramSnapshotManager::PrepareDelta(HistogramBase* histogram) {
58*635a8641SAndroid Build Coastguard Worker histogram->ValidateHistogramContents();
59*635a8641SAndroid Build Coastguard Worker PrepareSamples(histogram, histogram->SnapshotDelta());
60*635a8641SAndroid Build Coastguard Worker }
61*635a8641SAndroid Build Coastguard Worker
PrepareFinalDelta(const HistogramBase * histogram)62*635a8641SAndroid Build Coastguard Worker void HistogramSnapshotManager::PrepareFinalDelta(
63*635a8641SAndroid Build Coastguard Worker const HistogramBase* histogram) {
64*635a8641SAndroid Build Coastguard Worker histogram->ValidateHistogramContents();
65*635a8641SAndroid Build Coastguard Worker PrepareSamples(histogram, histogram->SnapshotFinalDelta());
66*635a8641SAndroid Build Coastguard Worker }
67*635a8641SAndroid Build Coastguard Worker
PrepareSamples(const HistogramBase * histogram,std::unique_ptr<HistogramSamples> samples)68*635a8641SAndroid Build Coastguard Worker void HistogramSnapshotManager::PrepareSamples(
69*635a8641SAndroid Build Coastguard Worker const HistogramBase* histogram,
70*635a8641SAndroid Build Coastguard Worker std::unique_ptr<HistogramSamples> samples) {
71*635a8641SAndroid Build Coastguard Worker DCHECK(histogram_flattener_);
72*635a8641SAndroid Build Coastguard Worker
73*635a8641SAndroid Build Coastguard Worker // Ensure that there is no concurrent access going on while accessing the
74*635a8641SAndroid Build Coastguard Worker // set of known histograms. The flag will be reset when this object goes
75*635a8641SAndroid Build Coastguard Worker // out of scope.
76*635a8641SAndroid Build Coastguard Worker MakeActive make_active(&is_active_);
77*635a8641SAndroid Build Coastguard Worker
78*635a8641SAndroid Build Coastguard Worker // Get information known about this histogram. If it did not previously
79*635a8641SAndroid Build Coastguard Worker // exist, one will be created and initialized.
80*635a8641SAndroid Build Coastguard Worker SampleInfo* sample_info = &known_histograms_[histogram->name_hash()];
81*635a8641SAndroid Build Coastguard Worker
82*635a8641SAndroid Build Coastguard Worker // Crash if we detect that our histograms have been overwritten. This may be
83*635a8641SAndroid Build Coastguard Worker // a fair distance from the memory smasher, but we hope to correlate these
84*635a8641SAndroid Build Coastguard Worker // crashes with other events, such as plugins, or usage patterns, etc.
85*635a8641SAndroid Build Coastguard Worker uint32_t corruption = histogram->FindCorruption(*samples);
86*635a8641SAndroid Build Coastguard Worker if (HistogramBase::BUCKET_ORDER_ERROR & corruption) {
87*635a8641SAndroid Build Coastguard Worker // Extract fields useful during debug.
88*635a8641SAndroid Build Coastguard Worker const BucketRanges* ranges =
89*635a8641SAndroid Build Coastguard Worker static_cast<const Histogram*>(histogram)->bucket_ranges();
90*635a8641SAndroid Build Coastguard Worker uint32_t ranges_checksum = ranges->checksum();
91*635a8641SAndroid Build Coastguard Worker uint32_t ranges_calc_checksum = ranges->CalculateChecksum();
92*635a8641SAndroid Build Coastguard Worker int32_t flags = histogram->flags();
93*635a8641SAndroid Build Coastguard Worker // The checksum should have caught this, so crash separately if it didn't.
94*635a8641SAndroid Build Coastguard Worker CHECK_NE(0U, HistogramBase::RANGE_CHECKSUM_ERROR & corruption);
95*635a8641SAndroid Build Coastguard Worker CHECK(false); // Crash for the bucket order corruption.
96*635a8641SAndroid Build Coastguard Worker // Ensure that compiler keeps around pointers to |histogram| and its
97*635a8641SAndroid Build Coastguard Worker // internal |bucket_ranges_| for any minidumps.
98*635a8641SAndroid Build Coastguard Worker base::debug::Alias(&ranges_checksum);
99*635a8641SAndroid Build Coastguard Worker base::debug::Alias(&ranges_calc_checksum);
100*635a8641SAndroid Build Coastguard Worker base::debug::Alias(&flags);
101*635a8641SAndroid Build Coastguard Worker }
102*635a8641SAndroid Build Coastguard Worker // Checksum corruption might not have caused order corruption.
103*635a8641SAndroid Build Coastguard Worker CHECK_EQ(0U, HistogramBase::RANGE_CHECKSUM_ERROR & corruption);
104*635a8641SAndroid Build Coastguard Worker
105*635a8641SAndroid Build Coastguard Worker // Note, at this point corruption can only be COUNT_HIGH_ERROR or
106*635a8641SAndroid Build Coastguard Worker // COUNT_LOW_ERROR and they never arise together, so we don't need to extract
107*635a8641SAndroid Build Coastguard Worker // bits from corruption.
108*635a8641SAndroid Build Coastguard Worker if (corruption) {
109*635a8641SAndroid Build Coastguard Worker DLOG(ERROR) << "Histogram: \"" << histogram->histogram_name()
110*635a8641SAndroid Build Coastguard Worker << "\" has data corruption: " << corruption;
111*635a8641SAndroid Build Coastguard Worker // Don't record corrupt data to metrics services.
112*635a8641SAndroid Build Coastguard Worker const uint32_t old_corruption = sample_info->inconsistencies;
113*635a8641SAndroid Build Coastguard Worker if (old_corruption == (corruption | old_corruption))
114*635a8641SAndroid Build Coastguard Worker return; // We've already seen this corruption for this histogram.
115*635a8641SAndroid Build Coastguard Worker sample_info->inconsistencies |= corruption;
116*635a8641SAndroid Build Coastguard Worker return;
117*635a8641SAndroid Build Coastguard Worker }
118*635a8641SAndroid Build Coastguard Worker
119*635a8641SAndroid Build Coastguard Worker if (samples->TotalCount() > 0)
120*635a8641SAndroid Build Coastguard Worker histogram_flattener_->RecordDelta(*histogram, *samples);
121*635a8641SAndroid Build Coastguard Worker }
122*635a8641SAndroid Build Coastguard Worker
123*635a8641SAndroid Build Coastguard Worker } // namespace base
124