blob: 737ccad72f23553837ffebf47c2c4eaf59213ed5 [file] [log] [blame]
[email protected]7c7a42752012-08-09 05:14:151// 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.
4
5#include "base/metrics/sparse_histogram.h"
6
[email protected]877ef562012-10-20 02:56:187#include "base/metrics/sample_map.h"
[email protected]7c7a42752012-08-09 05:14:158#include "base/metrics/statistics_recorder.h"
[email protected]c50c21d2013-01-11 21:52:449#include "base/pickle.h"
[email protected]d529cb02013-06-10 19:06:5710#include "base/strings/stringprintf.h"
[email protected]b4af2ec2012-10-05 21:29:4411#include "base/synchronization/lock.h"
[email protected]7c7a42752012-08-09 05:14:1512
[email protected]b4af2ec2012-10-05 21:29:4413using std::map;
[email protected]7c7a42752012-08-09 05:14:1514using std::string;
15
16namespace base {
17
[email protected]b4af2ec2012-10-05 21:29:4418typedef HistogramBase::Count Count;
19typedef HistogramBase::Sample Sample;
20
[email protected]7c7a42752012-08-09 05:14:1521// static
[email protected]b4af2ec2012-10-05 21:29:4422HistogramBase* SparseHistogram::FactoryGet(const string& name, int32 flags) {
[email protected]cc7dec212013-03-01 03:53:2523 HistogramBase* histogram = StatisticsRecorder::FindHistogram(name);
24
25 if (!histogram) {
26 // To avoid racy destruction at shutdown, the following will be leaked.
27 HistogramBase* tentative_histogram = new SparseHistogram(name);
28 tentative_histogram->SetFlags(flags);
29 histogram =
30 StatisticsRecorder::RegisterOrDeleteDuplicate(tentative_histogram);
31 }
32 DCHECK_EQ(SPARSE_HISTOGRAM, histogram->GetHistogramType());
[email protected]7c7a42752012-08-09 05:14:1533 return histogram;
34}
35
36SparseHistogram::~SparseHistogram() {}
37
[email protected]07c02402012-10-31 06:20:2538HistogramType SparseHistogram::GetHistogramType() const {
39 return SPARSE_HISTOGRAM;
40}
41
[email protected]15ce3842013-06-27 14:38:4542bool SparseHistogram::HasConstructionArguments(
43 Sample expected_minimum,
44 Sample expected_maximum,
45 size_t expected_bucket_count) const {
[email protected]abae9b022012-10-24 08:18:5246 // SparseHistogram never has min/max/bucket_count limit.
47 return false;
48}
49
[email protected]7c7a42752012-08-09 05:14:1550void SparseHistogram::Add(Sample value) {
51 base::AutoLock auto_lock(lock_);
[email protected]c50c21d2013-01-11 21:52:4452 samples_.Accumulate(value, 1);
[email protected]7c7a42752012-08-09 05:14:1553}
54
[email protected]877ef562012-10-20 02:56:1855scoped_ptr<HistogramSamples> SparseHistogram::SnapshotSamples() const {
[email protected]b4af2ec2012-10-05 21:29:4456 scoped_ptr<SampleMap> snapshot(new SampleMap());
57
[email protected]7c7a42752012-08-09 05:14:1558 base::AutoLock auto_lock(lock_);
[email protected]c50c21d2013-01-11 21:52:4459 snapshot->Add(samples_);
[email protected]877ef562012-10-20 02:56:1860 return snapshot.PassAs<HistogramSamples>();
[email protected]7c7a42752012-08-09 05:14:1561}
62
[email protected]c50c21d2013-01-11 21:52:4463void SparseHistogram::AddSamples(const HistogramSamples& samples) {
64 base::AutoLock auto_lock(lock_);
65 samples_.Add(samples);
66}
67
68bool SparseHistogram::AddSamplesFromPickle(PickleIterator* iter) {
69 base::AutoLock auto_lock(lock_);
70 return samples_.AddFromPickle(iter);
71}
72
[email protected]7c7a42752012-08-09 05:14:1573void SparseHistogram::WriteHTMLGraph(string* output) const {
[email protected]f2bb3202013-04-05 21:21:5474 output->append("<PRE>");
75 WriteAsciiImpl(true, "<br>", output);
76 output->append("</PRE>");
[email protected]7c7a42752012-08-09 05:14:1577}
78
79void SparseHistogram::WriteAscii(string* output) const {
[email protected]f2bb3202013-04-05 21:21:5480 WriteAsciiImpl(true, "\n", output);
[email protected]7c7a42752012-08-09 05:14:1581}
82
[email protected]c50c21d2013-01-11 21:52:4483bool SparseHistogram::SerializeInfoImpl(Pickle* pickle) const {
84 return pickle->WriteString(histogram_name()) && pickle->WriteInt(flags());
85}
86
[email protected]7c7a42752012-08-09 05:14:1587SparseHistogram::SparseHistogram(const string& name)
[email protected]c50c21d2013-01-11 21:52:4488 : HistogramBase(name) {}
89
90HistogramBase* SparseHistogram::DeserializeInfoImpl(PickleIterator* iter) {
91 string histogram_name;
92 int flags;
93 if (!iter->ReadString(&histogram_name) || !iter->ReadInt(&flags)) {
94 DLOG(ERROR) << "Pickle error decoding Histogram: " << histogram_name;
95 return NULL;
96 }
97
98 DCHECK(flags & HistogramBase::kIPCSerializationSourceFlag);
99 flags &= ~HistogramBase::kIPCSerializationSourceFlag;
100
101 return SparseHistogram::FactoryGet(histogram_name, flags);
102}
[email protected]7c7a42752012-08-09 05:14:15103
[email protected]24a7ec52012-10-08 10:31:50104void SparseHistogram::GetParameters(DictionaryValue* params) const {
105 // TODO(kaiwang): Implement. (See HistogramBase::WriteJSON.)
106}
107
108void SparseHistogram::GetCountAndBucketData(Count* count,
[email protected]cdd98fc2013-05-10 09:32:58109 int64* sum,
[email protected]24a7ec52012-10-08 10:31:50110 ListValue* buckets) const {
111 // TODO(kaiwang): Implement. (See HistogramBase::WriteJSON.)
112}
113
[email protected]f2bb3202013-04-05 21:21:54114void SparseHistogram::WriteAsciiImpl(bool graph_it,
115 const std::string& newline,
116 std::string* output) const {
117 // Get a local copy of the data so we are consistent.
118 scoped_ptr<HistogramSamples> snapshot = SnapshotSamples();
119 Count total_count = snapshot->TotalCount();
120 double scaled_total_count = total_count / 100.0;
121
122 WriteAsciiHeader(total_count, output);
123 output->append(newline);
124
125 // Determine how wide the largest bucket range is (how many digits to print),
126 // so that we'll be able to right-align starts for the graphical bars.
127 // Determine which bucket has the largest sample count so that we can
128 // normalize the graphical bar-width relative to that sample count.
129 Count largest_count = 0;
130 Sample largest_sample = 0;
131 scoped_ptr<SampleCountIterator> it = snapshot->Iterator();
132 while (!it->Done())
133 {
134 Sample min;
135 Sample max;
136 Count count;
137 it->Get(&min, &max, &count);
138 if (min > largest_sample)
139 largest_sample = min;
140 if (count > largest_count)
141 largest_count = count;
142 it->Next();
143 }
144 size_t print_width = GetSimpleAsciiBucketRange(largest_sample).size() + 1;
145
146 // iterate over each item and display them
147 it = snapshot->Iterator();
148 while (!it->Done())
149 {
150 Sample min;
151 Sample max;
152 Count count;
153 it->Get(&min, &max, &count);
154
155 // value is min, so display it
156 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(' ');
160
161 if (graph_it)
162 WriteAsciiBucketGraph(count, largest_count, output);
163 WriteAsciiBucketValue(count, scaled_total_count, output);
164 output->append(newline);
165 it->Next();
166 }
167}
168
169void 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(),
174 total_count);
175 if (flags() & ~kHexRangePrintingFlag)
176 StringAppendF(output, " (flags = 0x%x)", flags() & ~kHexRangePrintingFlag);
177}
178
[email protected]7c7a42752012-08-09 05:14:15179} // namespace base