QUIC - cleanup changes to sync chromium tree with internal source.
[chromium-blink-merge.git] / base / metrics / sparse_histogram.cc
blob39c276d7877290a9a93123b728061cb06099c51b
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.
5 #include "base/metrics/sparse_histogram.h"
7 #include "base/metrics/sample_map.h"
8 #include "base/metrics/statistics_recorder.h"
9 #include "base/pickle.h"
10 #include "base/strings/stringprintf.h"
11 #include "base/synchronization/lock.h"
13 namespace base {
15 typedef HistogramBase::Count Count;
16 typedef HistogramBase::Sample Sample;
18 // static
19 HistogramBase* SparseHistogram::FactoryGet(const std::string& name,
20 int32 flags) {
21 HistogramBase* histogram = StatisticsRecorder::FindHistogram(name);
23 if (!histogram) {
24 // To avoid racy destruction at shutdown, the following will be leaked.
25 HistogramBase* tentative_histogram = new SparseHistogram(name);
26 tentative_histogram->SetFlags(flags);
27 histogram =
28 StatisticsRecorder::RegisterOrDeleteDuplicate(tentative_histogram);
30 DCHECK_EQ(SPARSE_HISTOGRAM, histogram->GetHistogramType());
31 return histogram;
34 SparseHistogram::~SparseHistogram() {}
36 HistogramType SparseHistogram::GetHistogramType() const {
37 return SPARSE_HISTOGRAM;
40 bool SparseHistogram::HasConstructionArguments(
41 Sample expected_minimum,
42 Sample expected_maximum,
43 size_t expected_bucket_count) const {
44 // SparseHistogram never has min/max/bucket_count limit.
45 return false;
48 void SparseHistogram::Add(Sample value) {
49 AddCount(value, 1);
52 void SparseHistogram::AddCount(Sample value, int count) {
53 if (count <= 0) {
54 NOTREACHED();
55 return;
58 base::AutoLock auto_lock(lock_);
59 samples_.Accumulate(value, count);
62 FindAndRunCallback(value);
65 scoped_ptr<HistogramSamples> SparseHistogram::SnapshotSamples() const {
66 scoped_ptr<SampleMap> snapshot(new SampleMap());
68 base::AutoLock auto_lock(lock_);
69 snapshot->Add(samples_);
70 return snapshot.Pass();
73 void SparseHistogram::AddSamples(const HistogramSamples& samples) {
74 base::AutoLock auto_lock(lock_);
75 samples_.Add(samples);
78 bool SparseHistogram::AddSamplesFromPickle(PickleIterator* iter) {
79 base::AutoLock auto_lock(lock_);
80 return samples_.AddFromPickle(iter);
83 void SparseHistogram::WriteHTMLGraph(std::string* output) const {
84 output->append("<PRE>");
85 WriteAsciiImpl(true, "<br>", output);
86 output->append("</PRE>");
89 void SparseHistogram::WriteAscii(std::string* output) const {
90 WriteAsciiImpl(true, "\n", output);
93 bool SparseHistogram::SerializeInfoImpl(Pickle* pickle) const {
94 return pickle->WriteString(histogram_name()) && pickle->WriteInt(flags());
97 SparseHistogram::SparseHistogram(const std::string& name)
98 : HistogramBase(name) {}
100 HistogramBase* SparseHistogram::DeserializeInfoImpl(PickleIterator* iter) {
101 std::string histogram_name;
102 int flags;
103 if (!iter->ReadString(&histogram_name) || !iter->ReadInt(&flags)) {
104 DLOG(ERROR) << "Pickle error decoding Histogram: " << histogram_name;
105 return NULL;
108 DCHECK(flags & HistogramBase::kIPCSerializationSourceFlag);
109 flags &= ~HistogramBase::kIPCSerializationSourceFlag;
111 return SparseHistogram::FactoryGet(histogram_name, flags);
114 void SparseHistogram::GetParameters(DictionaryValue* params) const {
115 // TODO(kaiwang): Implement. (See HistogramBase::WriteJSON.)
118 void SparseHistogram::GetCountAndBucketData(Count* count,
119 int64* sum,
120 ListValue* buckets) const {
121 // TODO(kaiwang): Implement. (See HistogramBase::WriteJSON.)
124 void SparseHistogram::WriteAsciiImpl(bool graph_it,
125 const std::string& newline,
126 std::string* output) const {
127 // Get a local copy of the data so we are consistent.
128 scoped_ptr<HistogramSamples> snapshot = SnapshotSamples();
129 Count total_count = snapshot->TotalCount();
130 double scaled_total_count = total_count / 100.0;
132 WriteAsciiHeader(total_count, output);
133 output->append(newline);
135 // Determine how wide the largest bucket range is (how many digits to print),
136 // so that we'll be able to right-align starts for the graphical bars.
137 // Determine which bucket has the largest sample count so that we can
138 // normalize the graphical bar-width relative to that sample count.
139 Count largest_count = 0;
140 Sample largest_sample = 0;
141 scoped_ptr<SampleCountIterator> it = snapshot->Iterator();
142 while (!it->Done()) {
143 Sample min;
144 Sample max;
145 Count count;
146 it->Get(&min, &max, &count);
147 if (min > largest_sample)
148 largest_sample = min;
149 if (count > largest_count)
150 largest_count = count;
151 it->Next();
153 size_t print_width = GetSimpleAsciiBucketRange(largest_sample).size() + 1;
155 // iterate over each item and display them
156 it = snapshot->Iterator();
157 while (!it->Done()) {
158 Sample min;
159 Sample max;
160 Count count;
161 it->Get(&min, &max, &count);
163 // value is min, so display it
164 std::string range = GetSimpleAsciiBucketRange(min);
165 output->append(range);
166 for (size_t j = 0; range.size() + j < print_width + 1; ++j)
167 output->push_back(' ');
169 if (graph_it)
170 WriteAsciiBucketGraph(count, largest_count, output);
171 WriteAsciiBucketValue(count, scaled_total_count, output);
172 output->append(newline);
173 it->Next();
177 void SparseHistogram::WriteAsciiHeader(const Count total_count,
178 std::string* output) const {
179 StringAppendF(output,
180 "Histogram: %s recorded %d samples",
181 histogram_name().c_str(),
182 total_count);
183 if (flags() & ~kHexRangePrintingFlag)
184 StringAppendF(output, " (flags = 0x%x)", flags() & ~kHexRangePrintingFlag);
187 } // namespace base