blob: a853dce3505f450c5ef814371f63f1fa92e7523f [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]7c7a42752012-08-09 05:14:1513namespace base {
14
[email protected]b4af2ec2012-10-05 21:29:4415typedef HistogramBase::Count Count;
16typedef HistogramBase::Sample Sample;
17
[email protected]7c7a42752012-08-09 05:14:1518// static
asvitkine24d3e9a2015-05-27 05:22:1419HistogramBase* SparseHistogram::FactoryGet(const std::string& name,
20 int32 flags) {
[email protected]cc7dec212013-03-01 03:53:2521 HistogramBase* histogram = StatisticsRecorder::FindHistogram(name);
22
23 if (!histogram) {
24 // To avoid racy destruction at shutdown, the following will be leaked.
25 HistogramBase* tentative_histogram = new SparseHistogram(name);
26 tentative_histogram->SetFlags(flags);
27 histogram =
28 StatisticsRecorder::RegisterOrDeleteDuplicate(tentative_histogram);
29 }
30 DCHECK_EQ(SPARSE_HISTOGRAM, histogram->GetHistogramType());
[email protected]7c7a42752012-08-09 05:14:1531 return histogram;
32}
33
34SparseHistogram::~SparseHistogram() {}
35
[email protected]07c02402012-10-31 06:20:2536HistogramType SparseHistogram::GetHistogramType() const {
37 return SPARSE_HISTOGRAM;
38}
39
[email protected]15ce3842013-06-27 14:38:4540bool SparseHistogram::HasConstructionArguments(
41 Sample expected_minimum,
42 Sample expected_maximum,
43 size_t expected_bucket_count) const {
[email protected]abae9b022012-10-24 08:18:5244 // SparseHistogram never has min/max/bucket_count limit.
45 return false;
46}
47
[email protected]7c7a42752012-08-09 05:14:1548void SparseHistogram::Add(Sample value) {
simonhatchdf5a8142015-07-15 22:22:5749 {
50 base::AutoLock auto_lock(lock_);
51 samples_.Accumulate(value, 1);
52 }
53
54 FindAndRunCallback(value);
[email protected]7c7a42752012-08-09 05:14:1555}
56
[email protected]877ef562012-10-20 02:56:1857scoped_ptr<HistogramSamples> SparseHistogram::SnapshotSamples() const {
[email protected]b4af2ec2012-10-05 21:29:4458 scoped_ptr<SampleMap> snapshot(new SampleMap());
59
[email protected]7c7a42752012-08-09 05:14:1560 base::AutoLock auto_lock(lock_);
[email protected]c50c21d2013-01-11 21:52:4461 snapshot->Add(samples_);
dcheng84b60292014-10-15 17:47:4462 return snapshot.Pass();
[email protected]7c7a42752012-08-09 05:14:1563}
64
[email protected]c50c21d2013-01-11 21:52:4465void SparseHistogram::AddSamples(const HistogramSamples& samples) {
66 base::AutoLock auto_lock(lock_);
67 samples_.Add(samples);
68}
69
70bool SparseHistogram::AddSamplesFromPickle(PickleIterator* iter) {
71 base::AutoLock auto_lock(lock_);
72 return samples_.AddFromPickle(iter);
73}
74
asvitkine24d3e9a2015-05-27 05:22:1475void SparseHistogram::WriteHTMLGraph(std::string* output) const {
[email protected]f2bb3202013-04-05 21:21:5476 output->append("<PRE>");
77 WriteAsciiImpl(true, "<br>", output);
78 output->append("</PRE>");
[email protected]7c7a42752012-08-09 05:14:1579}
80
asvitkine24d3e9a2015-05-27 05:22:1481void SparseHistogram::WriteAscii(std::string* output) const {
[email protected]f2bb3202013-04-05 21:21:5482 WriteAsciiImpl(true, "\n", output);
[email protected]7c7a42752012-08-09 05:14:1583}
84
[email protected]c50c21d2013-01-11 21:52:4485bool SparseHistogram::SerializeInfoImpl(Pickle* pickle) const {
86 return pickle->WriteString(histogram_name()) && pickle->WriteInt(flags());
87}
88
asvitkine24d3e9a2015-05-27 05:22:1489SparseHistogram::SparseHistogram(const std::string& name)
[email protected]c50c21d2013-01-11 21:52:4490 : HistogramBase(name) {}
91
92HistogramBase* SparseHistogram::DeserializeInfoImpl(PickleIterator* iter) {
asvitkine24d3e9a2015-05-27 05:22:1493 std::string histogram_name;
[email protected]c50c21d2013-01-11 21:52:4494 int flags;
95 if (!iter->ReadString(&histogram_name) || !iter->ReadInt(&flags)) {
96 DLOG(ERROR) << "Pickle error decoding Histogram: " << histogram_name;
97 return NULL;
98 }
99
100 DCHECK(flags & HistogramBase::kIPCSerializationSourceFlag);
101 flags &= ~HistogramBase::kIPCSerializationSourceFlag;
102
103 return SparseHistogram::FactoryGet(histogram_name, flags);
104}
[email protected]7c7a42752012-08-09 05:14:15105
[email protected]24a7ec52012-10-08 10:31:50106void SparseHistogram::GetParameters(DictionaryValue* params) const {
107 // TODO(kaiwang): Implement. (See HistogramBase::WriteJSON.)
108}
109
110void SparseHistogram::GetCountAndBucketData(Count* count,
[email protected]cdd98fc2013-05-10 09:32:58111 int64* sum,
[email protected]24a7ec52012-10-08 10:31:50112 ListValue* buckets) const {
113 // TODO(kaiwang): Implement. (See HistogramBase::WriteJSON.)
114}
115
[email protected]f2bb3202013-04-05 21:21:54116void 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;
123
124 WriteAsciiHeader(total_count, output);
125 output->append(newline);
126
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();
asvitkine24d3e9a2015-05-27 05:22:14134 while (!it->Done()) {
[email protected]f2bb3202013-04-05 21:21:54135 Sample min;
136 Sample max;
137 Count count;
138 it->Get(&min, &max, &count);
139 if (min > largest_sample)
140 largest_sample = min;
141 if (count > largest_count)
142 largest_count = count;
143 it->Next();
144 }
145 size_t print_width = GetSimpleAsciiBucketRange(largest_sample).size() + 1;
146
147 // iterate over each item and display them
148 it = snapshot->Iterator();
asvitkine24d3e9a2015-05-27 05:22:14149 while (!it->Done()) {
[email protected]f2bb3202013-04-05 21:21:54150 Sample min;
151 Sample max;
152 Count count;
153 it->Get(&min, &max, &count);
154
155 // value is min, so display it
asvitkine24d3e9a2015-05-27 05:22:14156 std::string range = GetSimpleAsciiBucketRange(min);
[email protected]f2bb3202013-04-05 21:21:54157 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