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
) {
49 base::AutoLock
auto_lock(lock_
);
50 samples_
.Accumulate(value
, 1);
53 scoped_ptr
<HistogramSamples
> SparseHistogram::SnapshotSamples() const {
54 scoped_ptr
<SampleMap
> snapshot(new SampleMap());
56 base::AutoLock
auto_lock(lock_
);
57 snapshot
->Add(samples_
);
58 return snapshot
.Pass();
61 void SparseHistogram::AddSamples(const HistogramSamples
& samples
) {
62 base::AutoLock
auto_lock(lock_
);
63 samples_
.Add(samples
);
66 bool SparseHistogram::AddSamplesFromPickle(PickleIterator
* iter
) {
67 base::AutoLock
auto_lock(lock_
);
68 return samples_
.AddFromPickle(iter
);
71 void SparseHistogram::WriteHTMLGraph(std::string
* output
) const {
72 output
->append("<PRE>");
73 WriteAsciiImpl(true, "<br>", output
);
74 output
->append("</PRE>");
77 void SparseHistogram::WriteAscii(std::string
* output
) const {
78 WriteAsciiImpl(true, "\n", output
);
81 bool SparseHistogram::SerializeInfoImpl(Pickle
* pickle
) const {
82 return pickle
->WriteString(histogram_name()) && pickle
->WriteInt(flags());
85 SparseHistogram::SparseHistogram(const std::string
& name
)
86 : HistogramBase(name
) {}
88 HistogramBase
* SparseHistogram::DeserializeInfoImpl(PickleIterator
* iter
) {
89 std::string histogram_name
;
91 if (!iter
->ReadString(&histogram_name
) || !iter
->ReadInt(&flags
)) {
92 DLOG(ERROR
) << "Pickle error decoding Histogram: " << histogram_name
;
96 DCHECK(flags
& HistogramBase::kIPCSerializationSourceFlag
);
97 flags
&= ~HistogramBase::kIPCSerializationSourceFlag
;
99 return SparseHistogram::FactoryGet(histogram_name
, flags
);
102 void SparseHistogram::GetParameters(DictionaryValue
* params
) const {
103 // TODO(kaiwang): Implement. (See HistogramBase::WriteJSON.)
106 void SparseHistogram::GetCountAndBucketData(Count
* count
,
108 ListValue
* buckets
) const {
109 // TODO(kaiwang): Implement. (See HistogramBase::WriteJSON.)
112 void SparseHistogram::WriteAsciiImpl(bool graph_it
,
113 const std::string
& newline
,
114 std::string
* output
) const {
115 // Get a local copy of the data so we are consistent.
116 scoped_ptr
<HistogramSamples
> snapshot
= SnapshotSamples();
117 Count total_count
= snapshot
->TotalCount();
118 double scaled_total_count
= total_count
/ 100.0;
120 WriteAsciiHeader(total_count
, output
);
121 output
->append(newline
);
123 // Determine how wide the largest bucket range is (how many digits to print),
124 // so that we'll be able to right-align starts for the graphical bars.
125 // Determine which bucket has the largest sample count so that we can
126 // normalize the graphical bar-width relative to that sample count.
127 Count largest_count
= 0;
128 Sample largest_sample
= 0;
129 scoped_ptr
<SampleCountIterator
> it
= snapshot
->Iterator();
130 while (!it
->Done()) {
134 it
->Get(&min
, &max
, &count
);
135 if (min
> largest_sample
)
136 largest_sample
= min
;
137 if (count
> largest_count
)
138 largest_count
= count
;
141 size_t print_width
= GetSimpleAsciiBucketRange(largest_sample
).size() + 1;
143 // iterate over each item and display them
144 it
= snapshot
->Iterator();
145 while (!it
->Done()) {
149 it
->Get(&min
, &max
, &count
);
151 // value is min, so display it
152 std::string range
= GetSimpleAsciiBucketRange(min
);
153 output
->append(range
);
154 for (size_t j
= 0; range
.size() + j
< print_width
+ 1; ++j
)
155 output
->push_back(' ');
158 WriteAsciiBucketGraph(count
, largest_count
, output
);
159 WriteAsciiBucketValue(count
, scaled_total_count
, output
);
160 output
->append(newline
);
165 void SparseHistogram::WriteAsciiHeader(const Count total_count
,
166 std::string
* output
) const {
167 StringAppendF(output
,
168 "Histogram: %s recorded %d samples",
169 histogram_name().c_str(),
171 if (flags() & ~kHexRangePrintingFlag
)
172 StringAppendF(output
, " (flags = 0x%x)", flags() & ~kHexRangePrintingFlag
);