blob: 5653456a29fb552470871f8611cb6337ec65bc50 [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
danakj0c8d4aa2015-11-25 05:29:587#include <utility>
8
bcwhiteb036e4322015-12-10 18:36:349#include "base/metrics/metrics_hashes.h"
[email protected]877ef562012-10-20 02:56:1810#include "base/metrics/sample_map.h"
[email protected]7c7a42752012-08-09 05:14:1511#include "base/metrics/statistics_recorder.h"
[email protected]c50c21d2013-01-11 21:52:4412#include "base/pickle.h"
[email protected]d529cb02013-06-10 19:06:5713#include "base/strings/stringprintf.h"
[email protected]b4af2ec2012-10-05 21:29:4414#include "base/synchronization/lock.h"
[email protected]7c7a42752012-08-09 05:14:1515
[email protected]7c7a42752012-08-09 05:14:1516namespace 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
asvitkine24d3e9a2015-05-27 05:22:1422HistogramBase* SparseHistogram::FactoryGet(const std::string& name,
avi9b6f42932015-12-26 22:15:1423 int32_t flags) {
[email protected]cc7dec212013-03-01 03:53:2524 HistogramBase* histogram = StatisticsRecorder::FindHistogram(name);
25
26 if (!histogram) {
27 // To avoid racy destruction at shutdown, the following will be leaked.
28 HistogramBase* tentative_histogram = new SparseHistogram(name);
29 tentative_histogram->SetFlags(flags);
30 histogram =
31 StatisticsRecorder::RegisterOrDeleteDuplicate(tentative_histogram);
32 }
33 DCHECK_EQ(SPARSE_HISTOGRAM, histogram->GetHistogramType());
[email protected]7c7a42752012-08-09 05:14:1534 return histogram;
35}
36
37SparseHistogram::~SparseHistogram() {}
38
bcwhiteb036e4322015-12-10 18:36:3439uint64_t SparseHistogram::name_hash() const {
40 return samples_.id();
41}
42
[email protected]07c02402012-10-31 06:20:2543HistogramType SparseHistogram::GetHistogramType() const {
44 return SPARSE_HISTOGRAM;
45}
46
[email protected]15ce3842013-06-27 14:38:4547bool SparseHistogram::HasConstructionArguments(
48 Sample expected_minimum,
49 Sample expected_maximum,
jam1eacd7e2016-02-08 22:48:1650 uint32_t expected_bucket_count) const {
[email protected]abae9b022012-10-24 08:18:5251 // SparseHistogram never has min/max/bucket_count limit.
52 return false;
53}
54
[email protected]7c7a42752012-08-09 05:14:1555void SparseHistogram::Add(Sample value) {
amohammadkhan6779b5c32015-08-05 20:31:1156 AddCount(value, 1);
57}
58
59void SparseHistogram::AddCount(Sample value, int count) {
60 if (count <= 0) {
61 NOTREACHED();
62 return;
63 }
simonhatchdf5a8142015-07-15 22:22:5764 {
65 base::AutoLock auto_lock(lock_);
amohammadkhan6779b5c32015-08-05 20:31:1166 samples_.Accumulate(value, count);
simonhatchdf5a8142015-07-15 22:22:5767 }
68
69 FindAndRunCallback(value);
[email protected]7c7a42752012-08-09 05:14:1570}
71
[email protected]877ef562012-10-20 02:56:1872scoped_ptr<HistogramSamples> SparseHistogram::SnapshotSamples() const {
bcwhiteb036e4322015-12-10 18:36:3473 scoped_ptr<SampleMap> snapshot(new SampleMap(name_hash()));
[email protected]b4af2ec2012-10-05 21:29:4474
[email protected]7c7a42752012-08-09 05:14:1575 base::AutoLock auto_lock(lock_);
[email protected]c50c21d2013-01-11 21:52:4476 snapshot->Add(samples_);
danakj0c8d4aa2015-11-25 05:29:5877 return std::move(snapshot);
[email protected]7c7a42752012-08-09 05:14:1578}
79
bcwhitec85a1f822016-02-18 21:22:1480scoped_ptr<HistogramSamples> SparseHistogram::SnapshotDelta() {
81 scoped_ptr<SampleMap> snapshot(new SampleMap(name_hash()));
82 base::AutoLock auto_lock(lock_);
83 snapshot->Add(samples_);
84
85 // Subtract what was previously logged and update that information.
86 snapshot->Subtract(logged_samples_);
87 logged_samples_.Add(*snapshot);
88 return std::move(snapshot);
89}
90
[email protected]c50c21d2013-01-11 21:52:4491void SparseHistogram::AddSamples(const HistogramSamples& samples) {
92 base::AutoLock auto_lock(lock_);
93 samples_.Add(samples);
94}
95
96bool SparseHistogram::AddSamplesFromPickle(PickleIterator* iter) {
97 base::AutoLock auto_lock(lock_);
98 return samples_.AddFromPickle(iter);
99}
100
asvitkine24d3e9a2015-05-27 05:22:14101void SparseHistogram::WriteHTMLGraph(std::string* output) const {
[email protected]f2bb3202013-04-05 21:21:54102 output->append("<PRE>");
103 WriteAsciiImpl(true, "<br>", output);
104 output->append("</PRE>");
[email protected]7c7a42752012-08-09 05:14:15105}
106
asvitkine24d3e9a2015-05-27 05:22:14107void SparseHistogram::WriteAscii(std::string* output) const {
[email protected]f2bb3202013-04-05 21:21:54108 WriteAsciiImpl(true, "\n", output);
[email protected]7c7a42752012-08-09 05:14:15109}
110
[email protected]c50c21d2013-01-11 21:52:44111bool SparseHistogram::SerializeInfoImpl(Pickle* pickle) const {
112 return pickle->WriteString(histogram_name()) && pickle->WriteInt(flags());
113}
114
asvitkine24d3e9a2015-05-27 05:22:14115SparseHistogram::SparseHistogram(const std::string& name)
bcwhiteb036e4322015-12-10 18:36:34116 : HistogramBase(name),
117 samples_(HashMetricName(name)) {}
[email protected]c50c21d2013-01-11 21:52:44118
119HistogramBase* SparseHistogram::DeserializeInfoImpl(PickleIterator* iter) {
asvitkine24d3e9a2015-05-27 05:22:14120 std::string histogram_name;
[email protected]c50c21d2013-01-11 21:52:44121 int flags;
122 if (!iter->ReadString(&histogram_name) || !iter->ReadInt(&flags)) {
123 DLOG(ERROR) << "Pickle error decoding Histogram: " << histogram_name;
124 return NULL;
125 }
126
127 DCHECK(flags & HistogramBase::kIPCSerializationSourceFlag);
128 flags &= ~HistogramBase::kIPCSerializationSourceFlag;
129
130 return SparseHistogram::FactoryGet(histogram_name, flags);
131}
[email protected]7c7a42752012-08-09 05:14:15132
[email protected]24a7ec5e2012-10-08 10:31:50133void SparseHistogram::GetParameters(DictionaryValue* params) const {
134 // TODO(kaiwang): Implement. (See HistogramBase::WriteJSON.)
135}
136
137void SparseHistogram::GetCountAndBucketData(Count* count,
avi9b6f42932015-12-26 22:15:14138 int64_t* sum,
[email protected]24a7ec5e2012-10-08 10:31:50139 ListValue* buckets) const {
140 // TODO(kaiwang): Implement. (See HistogramBase::WriteJSON.)
141}
142
[email protected]f2bb3202013-04-05 21:21:54143void SparseHistogram::WriteAsciiImpl(bool graph_it,
144 const std::string& newline,
145 std::string* output) const {
146 // Get a local copy of the data so we are consistent.
147 scoped_ptr<HistogramSamples> snapshot = SnapshotSamples();
148 Count total_count = snapshot->TotalCount();
149 double scaled_total_count = total_count / 100.0;
150
151 WriteAsciiHeader(total_count, output);
152 output->append(newline);
153
154 // Determine how wide the largest bucket range is (how many digits to print),
155 // so that we'll be able to right-align starts for the graphical bars.
156 // Determine which bucket has the largest sample count so that we can
157 // normalize the graphical bar-width relative to that sample count.
158 Count largest_count = 0;
159 Sample largest_sample = 0;
160 scoped_ptr<SampleCountIterator> it = snapshot->Iterator();
asvitkine24d3e9a2015-05-27 05:22:14161 while (!it->Done()) {
[email protected]f2bb3202013-04-05 21:21:54162 Sample min;
163 Sample max;
164 Count count;
165 it->Get(&min, &max, &count);
166 if (min > largest_sample)
167 largest_sample = min;
168 if (count > largest_count)
169 largest_count = count;
170 it->Next();
171 }
172 size_t print_width = GetSimpleAsciiBucketRange(largest_sample).size() + 1;
173
174 // iterate over each item and display them
175 it = snapshot->Iterator();
asvitkine24d3e9a2015-05-27 05:22:14176 while (!it->Done()) {
[email protected]f2bb3202013-04-05 21:21:54177 Sample min;
178 Sample max;
179 Count count;
180 it->Get(&min, &max, &count);
181
182 // value is min, so display it
asvitkine24d3e9a2015-05-27 05:22:14183 std::string range = GetSimpleAsciiBucketRange(min);
[email protected]f2bb3202013-04-05 21:21:54184 output->append(range);
185 for (size_t j = 0; range.size() + j < print_width + 1; ++j)
186 output->push_back(' ');
187
188 if (graph_it)
189 WriteAsciiBucketGraph(count, largest_count, output);
190 WriteAsciiBucketValue(count, scaled_total_count, output);
191 output->append(newline);
192 it->Next();
193 }
194}
195
196void SparseHistogram::WriteAsciiHeader(const Count total_count,
197 std::string* output) const {
198 StringAppendF(output,
199 "Histogram: %s recorded %d samples",
200 histogram_name().c_str(),
201 total_count);
202 if (flags() & ~kHexRangePrintingFlag)
203 StringAppendF(output, " (flags = 0x%x)", flags() & ~kHexRangePrintingFlag);
204}
205
[email protected]7c7a42752012-08-09 05:14:15206} // namespace base