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
) {
50 base::AutoLock
auto_lock(lock_
);
51 samples_
.Accumulate(value
, 1);
54 FindAndRunCallback(value
);
57 scoped_ptr
<HistogramSamples
> SparseHistogram::SnapshotSamples() const {
58 scoped_ptr
<SampleMap
> snapshot(new SampleMap());
60 base::AutoLock
auto_lock(lock_
);
61 snapshot
->Add(samples_
);
62 return snapshot
.Pass();
65 void SparseHistogram::AddSamples(const HistogramSamples
& samples
) {
66 base::AutoLock
auto_lock(lock_
);
67 samples_
.Add(samples
);
70 bool SparseHistogram::AddSamplesFromPickle(PickleIterator
* iter
) {
71 base::AutoLock
auto_lock(lock_
);
72 return samples_
.AddFromPickle(iter
);
75 void SparseHistogram::WriteHTMLGraph(std::string
* output
) const {
76 output
->append("<PRE>");
77 WriteAsciiImpl(true, "<br>", output
);
78 output
->append("</PRE>");
81 void SparseHistogram::WriteAscii(std::string
* output
) const {
82 WriteAsciiImpl(true, "\n", output
);
85 bool SparseHistogram::SerializeInfoImpl(Pickle
* pickle
) const {
86 return pickle
->WriteString(histogram_name()) && pickle
->WriteInt(flags());
89 SparseHistogram::SparseHistogram(const std::string
& name
)
90 : HistogramBase(name
) {}
92 HistogramBase
* SparseHistogram::DeserializeInfoImpl(PickleIterator
* iter
) {
93 std::string histogram_name
;
95 if (!iter
->ReadString(&histogram_name
) || !iter
->ReadInt(&flags
)) {
96 DLOG(ERROR
) << "Pickle error decoding Histogram: " << histogram_name
;
100 DCHECK(flags
& HistogramBase::kIPCSerializationSourceFlag
);
101 flags
&= ~HistogramBase::kIPCSerializationSourceFlag
;
103 return SparseHistogram::FactoryGet(histogram_name
, flags
);
106 void SparseHistogram::GetParameters(DictionaryValue
* params
) const {
107 // TODO(kaiwang): Implement. (See HistogramBase::WriteJSON.)
110 void SparseHistogram::GetCountAndBucketData(Count
* count
,
112 ListValue
* buckets
) const {
113 // TODO(kaiwang): Implement. (See HistogramBase::WriteJSON.)
116 void SparseHistogram::WriteAsciiImpl(bool graph_it
,
117 const std::string
& newline
,
118 std::string
* output
) const {
119 // Get a local copy of the data so we are consistent.
120 scoped_ptr
<HistogramSamples
> snapshot
= SnapshotSamples();
121 Count total_count
= snapshot
->TotalCount();
122 double scaled_total_count
= total_count
/ 100.0;
124 WriteAsciiHeader(total_count
, output
);
125 output
->append(newline
);
127 // Determine how wide the largest bucket range is (how many digits to print),
128 // so that we'll be able to right-align starts for the graphical bars.
129 // Determine which bucket has the largest sample count so that we can
130 // normalize the graphical bar-width relative to that sample count.
131 Count largest_count
= 0;
132 Sample largest_sample
= 0;
133 scoped_ptr
<SampleCountIterator
> it
= snapshot
->Iterator();
134 while (!it
->Done()) {
138 it
->Get(&min
, &max
, &count
);
139 if (min
> largest_sample
)
140 largest_sample
= min
;
141 if (count
> largest_count
)
142 largest_count
= count
;
145 size_t print_width
= GetSimpleAsciiBucketRange(largest_sample
).size() + 1;
147 // iterate over each item and display them
148 it
= snapshot
->Iterator();
149 while (!it
->Done()) {
153 it
->Get(&min
, &max
, &count
);
155 // value is min, so display it
156 std::string range
= GetSimpleAsciiBucketRange(min
);
157 output
->append(range
);
158 for (size_t j
= 0; range
.size() + j
< print_width
+ 1; ++j
)
159 output
->push_back(' ');
162 WriteAsciiBucketGraph(count
, largest_count
, output
);
163 WriteAsciiBucketValue(count
, scaled_total_count
, output
);
164 output
->append(newline
);
169 void SparseHistogram::WriteAsciiHeader(const Count total_count
,
170 std::string
* output
) const {
171 StringAppendF(output
,
172 "Histogram: %s recorded %d samples",
173 histogram_name().c_str(),
175 if (flags() & ~kHexRangePrintingFlag
)
176 StringAppendF(output
, " (flags = 0x%x)", flags() & ~kHexRangePrintingFlag
);