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"
15 typedef HistogramBase::Count Count
;
16 typedef HistogramBase::Sample Sample
;
19 HistogramBase
* SparseHistogram::FactoryGet(const std::string
& name
,
21 HistogramBase
* histogram
= StatisticsRecorder::FindHistogram(name
);
24 // To avoid racy destruction at shutdown, the following will be leaked.
25 HistogramBase
* tentative_histogram
= new SparseHistogram(name
);
26 tentative_histogram
->SetFlags(flags
);
28 StatisticsRecorder::RegisterOrDeleteDuplicate(tentative_histogram
);
30 DCHECK_EQ(SPARSE_HISTOGRAM
, histogram
->GetHistogramType());
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.
48 void SparseHistogram::Add(Sample value
) {
52 void SparseHistogram::AddCount(Sample value
, int count
) {
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
;
103 if (!iter
->ReadString(&histogram_name
) || !iter
->ReadInt(&flags
)) {
104 DLOG(ERROR
) << "Pickle error decoding Histogram: " << histogram_name
;
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
,
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()) {
146 it
->Get(&min
, &max
, &count
);
147 if (min
> largest_sample
)
148 largest_sample
= min
;
149 if (count
> largest_count
)
150 largest_count
= count
;
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()) {
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(' ');
170 WriteAsciiBucketGraph(count
, largest_count
, output
);
171 WriteAsciiBucketValue(count
, scaled_total_count
, output
);
172 output
->append(newline
);
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(),
183 if (flags() & ~kHexRangePrintingFlag
)
184 StringAppendF(output
, " (flags = 0x%x)", flags() & ~kHexRangePrintingFlag
);