[email protected] | 7c7a4275 | 2012-08-09 05:14:15 | [diff] [blame] | 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. |
| 4 | |
| 5 | #include "base/metrics/sparse_histogram.h" |
| 6 | |
danakj | 0c8d4aa | 2015-11-25 05:29:58 | [diff] [blame] | 7 | #include <utility> |
| 8 | |
bcwhite | b036e432 | 2015-12-10 18:36:34 | [diff] [blame] | 9 | #include "base/metrics/metrics_hashes.h" |
[email protected] | 877ef56 | 2012-10-20 02:56:18 | [diff] [blame] | 10 | #include "base/metrics/sample_map.h" |
[email protected] | 7c7a4275 | 2012-08-09 05:14:15 | [diff] [blame] | 11 | #include "base/metrics/statistics_recorder.h" |
[email protected] | c50c21d | 2013-01-11 21:52:44 | [diff] [blame] | 12 | #include "base/pickle.h" |
[email protected] | d529cb0 | 2013-06-10 19:06:57 | [diff] [blame] | 13 | #include "base/strings/stringprintf.h" |
[email protected] | b4af2ec | 2012-10-05 21:29:44 | [diff] [blame] | 14 | #include "base/synchronization/lock.h" |
[email protected] | 7c7a4275 | 2012-08-09 05:14:15 | [diff] [blame] | 15 | |
[email protected] | 7c7a4275 | 2012-08-09 05:14:15 | [diff] [blame] | 16 | namespace base { |
| 17 | |
[email protected] | b4af2ec | 2012-10-05 21:29:44 | [diff] [blame] | 18 | typedef HistogramBase::Count Count; |
| 19 | typedef HistogramBase::Sample Sample; |
| 20 | |
[email protected] | 7c7a4275 | 2012-08-09 05:14:15 | [diff] [blame] | 21 | // static |
asvitkine | 24d3e9a | 2015-05-27 05:22:14 | [diff] [blame] | 22 | HistogramBase* SparseHistogram::FactoryGet(const std::string& name, |
avi | 9b6f4293 | 2015-12-26 22:15:14 | [diff] [blame] | 23 | int32_t flags) { |
[email protected] | cc7dec21 | 2013-03-01 03:53:25 | [diff] [blame] | 24 | 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] | 7c7a4275 | 2012-08-09 05:14:15 | [diff] [blame] | 34 | return histogram; |
| 35 | } |
| 36 | |
| 37 | SparseHistogram::~SparseHistogram() {} |
| 38 | |
bcwhite | b036e432 | 2015-12-10 18:36:34 | [diff] [blame] | 39 | uint64_t SparseHistogram::name_hash() const { |
| 40 | return samples_.id(); |
| 41 | } |
| 42 | |
[email protected] | 07c0240 | 2012-10-31 06:20:25 | [diff] [blame] | 43 | HistogramType SparseHistogram::GetHistogramType() const { |
| 44 | return SPARSE_HISTOGRAM; |
| 45 | } |
| 46 | |
[email protected] | 15ce384 | 2013-06-27 14:38:45 | [diff] [blame] | 47 | bool SparseHistogram::HasConstructionArguments( |
| 48 | Sample expected_minimum, |
| 49 | Sample expected_maximum, |
jam | 1eacd7e | 2016-02-08 22:48:16 | [diff] [blame] | 50 | uint32_t expected_bucket_count) const { |
[email protected] | abae9b02 | 2012-10-24 08:18:52 | [diff] [blame] | 51 | // SparseHistogram never has min/max/bucket_count limit. |
| 52 | return false; |
| 53 | } |
| 54 | |
[email protected] | 7c7a4275 | 2012-08-09 05:14:15 | [diff] [blame] | 55 | void SparseHistogram::Add(Sample value) { |
amohammadkhan | 6779b5c3 | 2015-08-05 20:31:11 | [diff] [blame] | 56 | AddCount(value, 1); |
| 57 | } |
| 58 | |
| 59 | void SparseHistogram::AddCount(Sample value, int count) { |
| 60 | if (count <= 0) { |
| 61 | NOTREACHED(); |
| 62 | return; |
| 63 | } |
simonhatch | df5a814 | 2015-07-15 22:22:57 | [diff] [blame] | 64 | { |
| 65 | base::AutoLock auto_lock(lock_); |
amohammadkhan | 6779b5c3 | 2015-08-05 20:31:11 | [diff] [blame] | 66 | samples_.Accumulate(value, count); |
simonhatch | df5a814 | 2015-07-15 22:22:57 | [diff] [blame] | 67 | } |
| 68 | |
| 69 | FindAndRunCallback(value); |
[email protected] | 7c7a4275 | 2012-08-09 05:14:15 | [diff] [blame] | 70 | } |
| 71 | |
[email protected] | 877ef56 | 2012-10-20 02:56:18 | [diff] [blame] | 72 | scoped_ptr<HistogramSamples> SparseHistogram::SnapshotSamples() const { |
bcwhite | b036e432 | 2015-12-10 18:36:34 | [diff] [blame] | 73 | scoped_ptr<SampleMap> snapshot(new SampleMap(name_hash())); |
[email protected] | b4af2ec | 2012-10-05 21:29:44 | [diff] [blame] | 74 | |
[email protected] | 7c7a4275 | 2012-08-09 05:14:15 | [diff] [blame] | 75 | base::AutoLock auto_lock(lock_); |
[email protected] | c50c21d | 2013-01-11 21:52:44 | [diff] [blame] | 76 | snapshot->Add(samples_); |
danakj | 0c8d4aa | 2015-11-25 05:29:58 | [diff] [blame] | 77 | return std::move(snapshot); |
[email protected] | 7c7a4275 | 2012-08-09 05:14:15 | [diff] [blame] | 78 | } |
| 79 | |
bcwhite | c85a1f82 | 2016-02-18 21:22:14 | [diff] [blame] | 80 | scoped_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] | c50c21d | 2013-01-11 21:52:44 | [diff] [blame] | 91 | void SparseHistogram::AddSamples(const HistogramSamples& samples) { |
| 92 | base::AutoLock auto_lock(lock_); |
| 93 | samples_.Add(samples); |
| 94 | } |
| 95 | |
| 96 | bool SparseHistogram::AddSamplesFromPickle(PickleIterator* iter) { |
| 97 | base::AutoLock auto_lock(lock_); |
| 98 | return samples_.AddFromPickle(iter); |
| 99 | } |
| 100 | |
asvitkine | 24d3e9a | 2015-05-27 05:22:14 | [diff] [blame] | 101 | void SparseHistogram::WriteHTMLGraph(std::string* output) const { |
[email protected] | f2bb320 | 2013-04-05 21:21:54 | [diff] [blame] | 102 | output->append("<PRE>"); |
| 103 | WriteAsciiImpl(true, "<br>", output); |
| 104 | output->append("</PRE>"); |
[email protected] | 7c7a4275 | 2012-08-09 05:14:15 | [diff] [blame] | 105 | } |
| 106 | |
asvitkine | 24d3e9a | 2015-05-27 05:22:14 | [diff] [blame] | 107 | void SparseHistogram::WriteAscii(std::string* output) const { |
[email protected] | f2bb320 | 2013-04-05 21:21:54 | [diff] [blame] | 108 | WriteAsciiImpl(true, "\n", output); |
[email protected] | 7c7a4275 | 2012-08-09 05:14:15 | [diff] [blame] | 109 | } |
| 110 | |
[email protected] | c50c21d | 2013-01-11 21:52:44 | [diff] [blame] | 111 | bool SparseHistogram::SerializeInfoImpl(Pickle* pickle) const { |
| 112 | return pickle->WriteString(histogram_name()) && pickle->WriteInt(flags()); |
| 113 | } |
| 114 | |
asvitkine | 24d3e9a | 2015-05-27 05:22:14 | [diff] [blame] | 115 | SparseHistogram::SparseHistogram(const std::string& name) |
bcwhite | b036e432 | 2015-12-10 18:36:34 | [diff] [blame] | 116 | : HistogramBase(name), |
| 117 | samples_(HashMetricName(name)) {} |
[email protected] | c50c21d | 2013-01-11 21:52:44 | [diff] [blame] | 118 | |
| 119 | HistogramBase* SparseHistogram::DeserializeInfoImpl(PickleIterator* iter) { |
asvitkine | 24d3e9a | 2015-05-27 05:22:14 | [diff] [blame] | 120 | std::string histogram_name; |
[email protected] | c50c21d | 2013-01-11 21:52:44 | [diff] [blame] | 121 | 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] | 7c7a4275 | 2012-08-09 05:14:15 | [diff] [blame] | 132 | |
[email protected] | 24a7ec5e | 2012-10-08 10:31:50 | [diff] [blame] | 133 | void SparseHistogram::GetParameters(DictionaryValue* params) const { |
| 134 | // TODO(kaiwang): Implement. (See HistogramBase::WriteJSON.) |
| 135 | } |
| 136 | |
| 137 | void SparseHistogram::GetCountAndBucketData(Count* count, |
avi | 9b6f4293 | 2015-12-26 22:15:14 | [diff] [blame] | 138 | int64_t* sum, |
[email protected] | 24a7ec5e | 2012-10-08 10:31:50 | [diff] [blame] | 139 | ListValue* buckets) const { |
| 140 | // TODO(kaiwang): Implement. (See HistogramBase::WriteJSON.) |
| 141 | } |
| 142 | |
[email protected] | f2bb320 | 2013-04-05 21:21:54 | [diff] [blame] | 143 | void 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(); |
asvitkine | 24d3e9a | 2015-05-27 05:22:14 | [diff] [blame] | 161 | while (!it->Done()) { |
[email protected] | f2bb320 | 2013-04-05 21:21:54 | [diff] [blame] | 162 | 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(); |
asvitkine | 24d3e9a | 2015-05-27 05:22:14 | [diff] [blame] | 176 | while (!it->Done()) { |
[email protected] | f2bb320 | 2013-04-05 21:21:54 | [diff] [blame] | 177 | Sample min; |
| 178 | Sample max; |
| 179 | Count count; |
| 180 | it->Get(&min, &max, &count); |
| 181 | |
| 182 | // value is min, so display it |
asvitkine | 24d3e9a | 2015-05-27 05:22:14 | [diff] [blame] | 183 | std::string range = GetSimpleAsciiBucketRange(min); |
[email protected] | f2bb320 | 2013-04-05 21:21:54 | [diff] [blame] | 184 | 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 | |
| 196 | void 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] | 7c7a4275 | 2012-08-09 05:14:15 | [diff] [blame] | 206 | } // namespace base |