Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(188)

Side by Side Diff: base/metrics/histogram.cc

Issue 141393002: Return a NULL histogram pointer on construction error (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: add unit test, move impl out of .h Created 6 years, 11 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « no previous file | base/metrics/histogram_unittest.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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 // Histogram is an object that aggregates statistics, and can summarize them in 5 // Histogram is an object that aggregates statistics, and can summarize them in
6 // various forms, including ASCII graphical, HTML, and numerically (as a 6 // various forms, including ASCII graphical, HTML, and numerically (as a
7 // vector of numbers corresponding to each of the aggregating buckets). 7 // vector of numbers corresponding to each of the aggregating buckets).
8 // See header file for details and examples. 8 // See header file for details and examples.
9 9
10 #include "base/metrics/histogram.h" 10 #include "base/metrics/histogram.h"
(...skipping 58 matching lines...) Expand 10 before | Expand all | Expand 10 after
69 } 69 }
70 70
71 bool ValidateRangeChecksum(const HistogramBase& histogram, 71 bool ValidateRangeChecksum(const HistogramBase& histogram,
72 uint32 range_checksum) { 72 uint32 range_checksum) {
73 const Histogram& casted_histogram = 73 const Histogram& casted_histogram =
74 static_cast<const Histogram&>(histogram); 74 static_cast<const Histogram&>(histogram);
75 75
76 return casted_histogram.bucket_ranges()->checksum() == range_checksum; 76 return casted_histogram.bucket_ranges()->checksum() == range_checksum;
77 } 77 }
78 78
79 // DummyHistogram is an object that overrides Histogram's Add* functions
80 // to provide no functionality. This can be returned in the case of mismatched
81 // construction arguments to any of the other Histograms' FactoryGet calls.
82 class DummyHistogram : public Histogram {
83 public:
84 virtual void Add(Sample value) OVERRIDE {}
85 virtual void AddSamples(const HistogramSamples& samples) OVERRIDE {}
86 virtual bool AddSamplesFromPickle(PickleIterator* iter) OVERRIDE;
87
88 DummyHistogram();
89
90 private:
91 DISALLOW_COPY_AND_ASSIGN(DummyHistogram);
92 };
93
94 bool DummyHistogram::AddSamplesFromPickle(PickleIterator* iter) {
Alexei Svitkine (slow) 2014/01/17 20:06:31 Nit: You can probably define these inline above.
elijahtaylor1 2014/01/17 20:25:34 I got an error on some windows trybots before for
Alexei Svitkine (slow) 2014/01/17 22:14:27 I think it should work - there's code elsewhere th
95 return false;
96 }
97
98 DummyHistogram::DummyHistogram() : Histogram("Dummy", 0, 1, NULL) {}
99
79 } // namespace 100 } // namespace
80 101
81 typedef HistogramBase::Count Count; 102 typedef HistogramBase::Count Count;
82 typedef HistogramBase::Sample Sample; 103 typedef HistogramBase::Sample Sample;
83 104
84 // static 105 // static
85 const size_t Histogram::kBucketCount_MAX = 16384u; 106 const size_t Histogram::kBucketCount_MAX = 16384u;
86 107
87 HistogramBase* Histogram::FactoryGet(const string& name, 108 HistogramBase* Histogram::FactoryGet(const string& name,
88 Sample minimum, 109 Sample minimum,
(...skipping 14 matching lines...) Expand all
103 124
104 Histogram* tentative_histogram = 125 Histogram* tentative_histogram =
105 new Histogram(name, minimum, maximum, registered_ranges); 126 new Histogram(name, minimum, maximum, registered_ranges);
106 127
107 tentative_histogram->SetFlags(flags); 128 tentative_histogram->SetFlags(flags);
108 histogram = 129 histogram =
109 StatisticsRecorder::RegisterOrDeleteDuplicate(tentative_histogram); 130 StatisticsRecorder::RegisterOrDeleteDuplicate(tentative_histogram);
110 } 131 }
111 132
112 DCHECK_EQ(HISTOGRAM, histogram->GetHistogramType()); 133 DCHECK_EQ(HISTOGRAM, histogram->GetHistogramType());
113 CHECK(histogram->HasConstructionArguments(minimum, maximum, bucket_count)); 134 if (!histogram->HasConstructionArguments(minimum, maximum, bucket_count)) {
135 // The construction arguments do not match the existing histogram. This can
136 // come about if an extension updates in the middle of a chrome run and has
137 // changed one of them. We return a dummy object here instead of crashing
138 // so poor use of extension/Pepper APIs do not cause crashes in Chrome.
139 DLOG(ERROR) << "Histogram " << name << " has bad construction arguments";
140 DummyHistogram* tentative_histogram = new DummyHistogram;
141 histogram =
142 StatisticsRecorder::RegisterOrDeleteDuplicate(tentative_histogram);
143 }
114 return histogram; 144 return histogram;
115 } 145 }
116 146
117 HistogramBase* Histogram::FactoryTimeGet(const string& name, 147 HistogramBase* Histogram::FactoryTimeGet(const string& name,
118 TimeDelta minimum, 148 TimeDelta minimum,
119 TimeDelta maximum, 149 TimeDelta maximum,
120 size_t bucket_count, 150 size_t bucket_count,
121 int32 flags) { 151 int32 flags) {
122 return FactoryGet(name, minimum.InMilliseconds(), maximum.InMilliseconds(), 152 return FactoryGet(name, minimum.InMilliseconds(), maximum.InMilliseconds(),
123 bucket_count, flags); 153 bucket_count, flags);
(...skipping 436 matching lines...) Expand 10 before | Expand all | Expand 10 after
560 descriptions[i].description; 590 descriptions[i].description;
561 } 591 }
562 } 592 }
563 593
564 tentative_histogram->SetFlags(flags); 594 tentative_histogram->SetFlags(flags);
565 histogram = 595 histogram =
566 StatisticsRecorder::RegisterOrDeleteDuplicate(tentative_histogram); 596 StatisticsRecorder::RegisterOrDeleteDuplicate(tentative_histogram);
567 } 597 }
568 598
569 DCHECK_EQ(LINEAR_HISTOGRAM, histogram->GetHistogramType()); 599 DCHECK_EQ(LINEAR_HISTOGRAM, histogram->GetHistogramType());
570 CHECK(histogram->HasConstructionArguments(minimum, maximum, bucket_count)); 600 if (!histogram->HasConstructionArguments(minimum, maximum, bucket_count)) {
601 // The construction arguments do not match the existing histogram. This can
602 // come about if an extension updates in the middle of a chrome run and has
603 // changed one of them. We return a dummy object here instead of crashing
604 // so poor use of extension/Pepper APIs do not cause crashes in Chrome.
605 DLOG(ERROR) << "Histogram " << name << " has bad construction arguments";
606 DummyHistogram* tentative_histogram = new DummyHistogram;
607 histogram =
608 StatisticsRecorder::RegisterOrDeleteDuplicate(tentative_histogram);
609 }
571 return histogram; 610 return histogram;
572 } 611 }
573 612
574 HistogramType LinearHistogram::GetHistogramType() const { 613 HistogramType LinearHistogram::GetHistogramType() const {
575 return LINEAR_HISTOGRAM; 614 return LINEAR_HISTOGRAM;
576 } 615 }
577 616
578 LinearHistogram::LinearHistogram(const string& name, 617 LinearHistogram::LinearHistogram(const string& name,
579 Sample minimum, 618 Sample minimum,
580 Sample maximum, 619 Sample maximum,
(...skipping 244 matching lines...) Expand 10 before | Expand all | Expand 10 after
825 864
826 BucketRanges* bucket_ranges = new BucketRanges(ranges.size()); 865 BucketRanges* bucket_ranges = new BucketRanges(ranges.size());
827 for (size_t i = 0; i < ranges.size(); i++) { 866 for (size_t i = 0; i < ranges.size(); i++) {
828 bucket_ranges->set_range(i, ranges[i]); 867 bucket_ranges->set_range(i, ranges[i]);
829 } 868 }
830 bucket_ranges->ResetChecksum(); 869 bucket_ranges->ResetChecksum();
831 return bucket_ranges; 870 return bucket_ranges;
832 } 871 }
833 872
834 } // namespace base 873 } // namespace base
OLDNEW
« no previous file with comments | « no previous file | base/metrics/histogram_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698