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" |
| 8 #include "base/metrics/histogram_flattener.h" |
| 9 #include "base/metrics/histogram_samples.h" |
7 #include "base/metrics/statistics_recorder.h" | 10 #include "base/metrics/statistics_recorder.h" |
| 11 #include "base/stl_util.h" |
8 | 12 |
9 using base::Histogram; | 13 using std::map; |
10 using base::StatisticsRecorder; | 14 using std::string; |
11 | 15 |
12 namespace base { | 16 namespace base { |
13 | 17 |
14 HistogramSnapshotManager::HistogramSnapshotManager( | 18 HistogramSnapshotManager::HistogramSnapshotManager( |
15 HistogramFlattener* histogram_flattener) | 19 HistogramFlattener* histogram_flattener) |
16 : histogram_flattener_(histogram_flattener) { | 20 : histogram_flattener_(histogram_flattener) { |
17 DCHECK(histogram_flattener_); | 21 DCHECK(histogram_flattener_); |
18 } | 22 } |
19 | 23 |
20 HistogramSnapshotManager::~HistogramSnapshotManager() {} | 24 HistogramSnapshotManager::~HistogramSnapshotManager() { |
| 25 STLDeleteValues(&logged_samples_); |
| 26 } |
21 | 27 |
22 void HistogramSnapshotManager::PrepareDeltas(Histogram::Flags flag_to_set, | 28 void HistogramSnapshotManager::PrepareDeltas(Histogram::Flags flag_to_set, |
23 bool record_only_uma) { | 29 bool record_only_uma) { |
24 StatisticsRecorder::Histograms histograms; | 30 StatisticsRecorder::Histograms histograms; |
25 StatisticsRecorder::GetHistograms(&histograms); | 31 StatisticsRecorder::GetHistograms(&histograms); |
26 for (StatisticsRecorder::Histograms::const_iterator it = histograms.begin(); | 32 for (StatisticsRecorder::Histograms::const_iterator it = histograms.begin(); |
27 histograms.end() != it; | 33 histograms.end() != it; |
28 ++it) { | 34 ++it) { |
29 (*it)->SetFlags(flag_to_set); | 35 (*it)->SetFlags(flag_to_set); |
30 if (record_only_uma && | 36 if (record_only_uma && |
31 0 == ((*it)->flags() & Histogram::kUmaTargetedHistogramFlag)) | 37 0 == ((*it)->flags() & Histogram::kUmaTargetedHistogramFlag)) |
32 continue; | 38 continue; |
33 PrepareDelta(**it); | 39 PrepareDelta(**it); |
34 } | 40 } |
35 } | 41 } |
36 | 42 |
37 void HistogramSnapshotManager::PrepareDelta(const Histogram& histogram) { | 43 void HistogramSnapshotManager::PrepareDelta(const Histogram& histogram) { |
38 DCHECK(histogram_flattener_); | 44 DCHECK(histogram_flattener_); |
39 | 45 |
40 // Get up-to-date snapshot of sample stats. | 46 // Get up-to-date snapshot of sample stats. |
41 Histogram::SampleSet snapshot; | 47 scoped_ptr<HistogramSamples> snapshot(histogram.SnapshotSamples()); |
42 histogram.SnapshotSample(&snapshot); | |
43 const std::string& histogram_name = histogram.histogram_name(); | 48 const std::string& histogram_name = histogram.histogram_name(); |
44 | 49 |
45 int corruption = histogram.FindCorruption(snapshot); | 50 int corruption = histogram.FindCorruption(*snapshot); |
46 | 51 |
47 // Crash if we detect that our histograms have been overwritten. This may be | 52 // Crash if we detect that our histograms have been overwritten. This may be |
48 // a fair distance from the memory smasher, but we hope to correlate these | 53 // a fair distance from the memory smasher, but we hope to correlate these |
49 // crashes with other events, such as plugins, or usage patterns, etc. | 54 // crashes with other events, such as plugins, or usage patterns, etc. |
50 if (Histogram::BUCKET_ORDER_ERROR & corruption) { | 55 if (Histogram::BUCKET_ORDER_ERROR & corruption) { |
51 // The checksum should have caught this, so crash separately if it didn't. | 56 // The checksum should have caught this, so crash separately if it didn't. |
52 CHECK_NE(0, Histogram::RANGE_CHECKSUM_ERROR & corruption); | 57 CHECK_NE(0, Histogram::RANGE_CHECKSUM_ERROR & corruption); |
53 CHECK(false); // Crash for the bucket order corruption. | 58 CHECK(false); // Crash for the bucket order corruption. |
54 } | 59 } |
55 // Checksum corruption might not have caused order corruption. | 60 // Checksum corruption might not have caused order corruption. |
56 CHECK_EQ(0, Histogram::RANGE_CHECKSUM_ERROR & corruption); | 61 CHECK_EQ(0, Histogram::RANGE_CHECKSUM_ERROR & corruption); |
57 | 62 |
58 // Note, at this point corruption can only be COUNT_HIGH_ERROR or | 63 // Note, at this point corruption can only be COUNT_HIGH_ERROR or |
59 // COUNT_LOW_ERROR and they never arise together, so we don't need to extract | 64 // COUNT_LOW_ERROR and they never arise together, so we don't need to extract |
60 // bits from corruption. | 65 // bits from corruption. |
61 if (corruption) { | 66 if (corruption) { |
62 NOTREACHED(); | 67 DLOG(ERROR) << "Histogram: " << histogram_name |
| 68 << " has data corruption: " << corruption; |
63 histogram_flattener_->InconsistencyDetected( | 69 histogram_flattener_->InconsistencyDetected( |
64 static_cast<Histogram::Inconsistencies>(corruption)); | 70 static_cast<Histogram::Inconsistencies>(corruption)); |
65 // Don't record corrupt data to metrics survices. | 71 // Don't record corrupt data to metrics services. |
66 if (NULL == inconsistencies_.get()) | 72 int old_corruption = inconsistencies_[histogram_name]; |
67 inconsistencies_.reset(new ProblemMap); | |
68 int old_corruption = (*inconsistencies_)[histogram_name]; | |
69 if (old_corruption == (corruption | old_corruption)) | 73 if (old_corruption == (corruption | old_corruption)) |
70 return; // We've already seen this corruption for this histogram. | 74 return; // We've already seen this corruption for this histogram. |
71 (*inconsistencies_)[histogram_name] |= corruption; | 75 inconsistencies_[histogram_name] |= corruption; |
72 histogram_flattener_->UniqueInconsistencyDetected( | 76 histogram_flattener_->UniqueInconsistencyDetected( |
73 static_cast<Histogram::Inconsistencies>(corruption)); | 77 static_cast<Histogram::Inconsistencies>(corruption)); |
74 return; | 78 return; |
75 } | 79 } |
76 | 80 |
77 // Find the already recorded stats, or create an empty set. Remove from our | 81 HistogramSamples* to_log; |
78 // snapshot anything that we've already recorded. | 82 map<string, HistogramSamples*>::iterator it = |
79 LoggedSampleMap::iterator it = logged_samples_.find(histogram_name); | 83 logged_samples_.find(histogram_name); |
80 Histogram::SampleSet* already_logged; | 84 if (it == logged_samples_.end()) { |
81 if (logged_samples_.end() == it) { | 85 to_log = snapshot.release(); |
82 // Add new entry | 86 |
83 already_logged = &logged_samples_[histogram.histogram_name()]; | 87 // This histogram has not been logged before, add a new entry. |
84 // Complete initialization. | 88 logged_samples_[histogram_name] = to_log; |
85 already_logged->Resize(histogram.bucket_count()); | |
86 } else { | 89 } else { |
87 already_logged = &(it->second); | 90 HistogramSamples* already_logged = it->second; |
88 int64 discrepancy(already_logged->TotalCount() - | 91 InspectLoggedSamplesInconsistency(*snapshot, already_logged); |
89 already_logged->redundant_count()); | 92 snapshot->Subtract(*already_logged); |
90 if (discrepancy) { | 93 already_logged->Add(*snapshot); |
91 NOTREACHED(); // Already_logged has become corrupt. | 94 to_log = snapshot.get(); |
92 int problem = static_cast<int>(discrepancy); | |
93 if (problem != discrepancy) | |
94 problem = INT_MAX; | |
95 histogram_flattener_->InconsistencyDetectedInLoggedCount(problem); | |
96 // With no valid baseline, we'll act like we've recorded everything in our | |
97 // snapshot. | |
98 already_logged->Subtract(*already_logged); | |
99 already_logged->Add(snapshot); | |
100 } | |
101 // Deduct any stats we've already logged from our snapshot. | |
102 snapshot.Subtract(*already_logged); | |
103 } | 95 } |
104 | 96 |
105 // Snapshot now contains only a delta to what we've already_logged. | 97 if (to_log->redundant_count() > 0) |
106 if (snapshot.redundant_count() > 0) { | 98 histogram_flattener_->RecordDelta(histogram, *to_log); |
107 histogram_flattener_->RecordDelta(histogram, snapshot); | 99 } |
108 // Add new data into our running total. | 100 |
109 already_logged->Add(snapshot); | 101 void HistogramSnapshotManager::InspectLoggedSamplesInconsistency( |
| 102 const HistogramSamples& new_snapshot, |
| 103 HistogramSamples* logged_samples) { |
| 104 HistogramBase::Count discrepancy = |
| 105 logged_samples->TotalCount() - logged_samples->redundant_count(); |
| 106 if (!discrepancy) |
| 107 return; |
| 108 |
| 109 histogram_flattener_->InconsistencyDetectedInLoggedCount(discrepancy); |
| 110 if (discrepancy > Histogram::kCommonRaceBasedCountMismatch) { |
| 111 // Fix logged_samples. |
| 112 logged_samples->Subtract(*logged_samples); |
| 113 logged_samples->Add(new_snapshot); |
110 } | 114 } |
111 } | 115 } |
| 116 |
112 } // namespace base | 117 } // namespace base |
OLD | NEW |