OLD | NEW |
| (Empty) |
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 "chrome/common/metrics/histogram_sender.h" | |
6 | |
7 using base::Histogram; | |
8 using base::StatisticsRecorder; | |
9 | |
10 HistogramSender::HistogramSender() {} | |
11 | |
12 HistogramSender::~HistogramSender() {} | |
13 | |
14 void HistogramSender::TransmitAllHistograms(Histogram::Flags flag_to_set, | |
15 bool send_only_uma) { | |
16 StatisticsRecorder::Histograms histograms; | |
17 StatisticsRecorder::GetHistograms(&histograms); | |
18 for (StatisticsRecorder::Histograms::const_iterator it = histograms.begin(); | |
19 histograms.end() != it; | |
20 ++it) { | |
21 (*it)->SetFlags(flag_to_set); | |
22 if (send_only_uma && | |
23 0 == ((*it)->flags() & Histogram::kUmaTargetedHistogramFlag)) | |
24 continue; | |
25 TransmitHistogram(**it); | |
26 } | |
27 } | |
28 | |
29 void HistogramSender::TransmitHistogram(const Histogram& histogram) { | |
30 // Get up-to-date snapshot of sample stats. | |
31 Histogram::SampleSet snapshot; | |
32 histogram.SnapshotSample(&snapshot); | |
33 const std::string& histogram_name = histogram.histogram_name(); | |
34 | |
35 int corruption = histogram.FindCorruption(snapshot); | |
36 | |
37 // Crash if we detect that our histograms have been overwritten. This may be | |
38 // a fair distance from the memory smasher, but we hope to correlate these | |
39 // crashes with other events, such as plugins, or usage patterns, etc. | |
40 if (Histogram::BUCKET_ORDER_ERROR & corruption) { | |
41 // The checksum should have caught this, so crash separately if it didn't. | |
42 CHECK_NE(0, Histogram::RANGE_CHECKSUM_ERROR & corruption); | |
43 CHECK(false); // Crash for the bucket order corruption. | |
44 } | |
45 // Checksum corruption might not have caused order corruption. | |
46 CHECK_EQ(0, Histogram::RANGE_CHECKSUM_ERROR & corruption); | |
47 | |
48 if (corruption) { | |
49 NOTREACHED(); | |
50 InconsistencyDetected(corruption); | |
51 // Don't send corrupt data to metrics survices. | |
52 if (NULL == inconsistencies_.get()) | |
53 inconsistencies_.reset(new ProblemMap); | |
54 int old_corruption = (*inconsistencies_)[histogram_name]; | |
55 if (old_corruption == (corruption | old_corruption)) | |
56 return; // We've already seen this corruption for this histogram. | |
57 (*inconsistencies_)[histogram_name] |= corruption; | |
58 UniqueInconsistencyDetected(corruption); | |
59 return; | |
60 } | |
61 | |
62 // Find the already sent stats, or create an empty set. Remove from our | |
63 // snapshot anything that we've already sent. | |
64 LoggedSampleMap::iterator it = logged_samples_.find(histogram_name); | |
65 Histogram::SampleSet* already_logged; | |
66 if (logged_samples_.end() == it) { | |
67 // Add new entry | |
68 already_logged = &logged_samples_[histogram.histogram_name()]; | |
69 already_logged->Resize(histogram); // Complete initialization. | |
70 } else { | |
71 already_logged = &(it->second); | |
72 int64 discrepancy(already_logged->TotalCount() - | |
73 already_logged->redundant_count()); | |
74 if (discrepancy) { | |
75 NOTREACHED(); // Already_logged has become corrupt. | |
76 int problem = static_cast<int>(discrepancy); | |
77 if (problem != discrepancy) | |
78 problem = INT_MAX; | |
79 SnapshotProblemResolved(problem); | |
80 // With no valid baseline, we'll act like we've sent everything in our | |
81 // snapshot. | |
82 already_logged->Subtract(*already_logged); | |
83 already_logged->Add(snapshot); | |
84 } | |
85 // Deduct any stats we've already logged from our snapshot. | |
86 snapshot.Subtract(*already_logged); | |
87 } | |
88 | |
89 // Snapshot now contains only a delta to what we've already_logged. | |
90 if (snapshot.redundant_count() > 0) { | |
91 TransmitHistogramDelta(histogram, snapshot); | |
92 // Add new data into our running total. | |
93 already_logged->Add(snapshot); | |
94 } | |
95 } | |
OLD | NEW |