[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 | |
[email protected] | 877ef56 | 2012-10-20 02:56:18 | [diff] [blame] | 7 | #include "base/metrics/sample_map.h" |
[email protected] | 7c7a4275 | 2012-08-09 05:14:15 | [diff] [blame] | 8 | #include "base/metrics/statistics_recorder.h" |
[email protected] | c50c21d | 2013-01-11 21:52:44 | [diff] [blame] | 9 | #include "base/pickle.h" |
[email protected] | d529cb0 | 2013-06-10 19:06:57 | [diff] [blame] | 10 | #include "base/strings/stringprintf.h" |
[email protected] | b4af2ec | 2012-10-05 21:29:44 | [diff] [blame] | 11 | #include "base/synchronization/lock.h" |
[email protected] | 7c7a4275 | 2012-08-09 05:14:15 | [diff] [blame] | 12 | |
[email protected] | 7c7a4275 | 2012-08-09 05:14:15 | [diff] [blame] | 13 | namespace base { |
| 14 | |
[email protected] | b4af2ec | 2012-10-05 21:29:44 | [diff] [blame] | 15 | typedef HistogramBase::Count Count; |
| 16 | typedef HistogramBase::Sample Sample; |
| 17 | |
[email protected] | 7c7a4275 | 2012-08-09 05:14:15 | [diff] [blame] | 18 | // static |
asvitkine | 24d3e9a | 2015-05-27 05:22:14 | [diff] [blame] | 19 | HistogramBase* SparseHistogram::FactoryGet(const std::string& name, |
| 20 | int32 flags) { |
[email protected] | cc7dec21 | 2013-03-01 03:53:25 | [diff] [blame] | 21 | 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] | 7c7a4275 | 2012-08-09 05:14:15 | [diff] [blame] | 31 | return histogram; |
| 32 | } |
| 33 | |
| 34 | SparseHistogram::~SparseHistogram() {} |
| 35 | |
[email protected] | 07c0240 | 2012-10-31 06:20:25 | [diff] [blame] | 36 | HistogramType SparseHistogram::GetHistogramType() const { |
| 37 | return SPARSE_HISTOGRAM; |
| 38 | } |
| 39 | |
[email protected] | 15ce384 | 2013-06-27 14:38:45 | [diff] [blame] | 40 | bool SparseHistogram::HasConstructionArguments( |
| 41 | Sample expected_minimum, |
| 42 | Sample expected_maximum, |
| 43 | size_t expected_bucket_count) const { |
[email protected] | abae9b02 | 2012-10-24 08:18:52 | [diff] [blame] | 44 | // SparseHistogram never has min/max/bucket_count limit. |
| 45 | return false; |
| 46 | } |
| 47 | |
[email protected] | 7c7a4275 | 2012-08-09 05:14:15 | [diff] [blame] | 48 | void SparseHistogram::Add(Sample value) { |
simonhatch | df5a814 | 2015-07-15 22:22:57 | [diff] [blame^] | 49 | { |
| 50 | base::AutoLock auto_lock(lock_); |
| 51 | samples_.Accumulate(value, 1); |
| 52 | } |
| 53 | |
| 54 | FindAndRunCallback(value); |
[email protected] | 7c7a4275 | 2012-08-09 05:14:15 | [diff] [blame] | 55 | } |
| 56 | |
[email protected] | 877ef56 | 2012-10-20 02:56:18 | [diff] [blame] | 57 | scoped_ptr<HistogramSamples> SparseHistogram::SnapshotSamples() const { |
[email protected] | b4af2ec | 2012-10-05 21:29:44 | [diff] [blame] | 58 | scoped_ptr<SampleMap> snapshot(new SampleMap()); |
| 59 | |
[email protected] | 7c7a4275 | 2012-08-09 05:14:15 | [diff] [blame] | 60 | base::AutoLock auto_lock(lock_); |
[email protected] | c50c21d | 2013-01-11 21:52:44 | [diff] [blame] | 61 | snapshot->Add(samples_); |
dcheng | 84b6029 | 2014-10-15 17:47:44 | [diff] [blame] | 62 | return snapshot.Pass(); |
[email protected] | 7c7a4275 | 2012-08-09 05:14:15 | [diff] [blame] | 63 | } |
| 64 | |
[email protected] | c50c21d | 2013-01-11 21:52:44 | [diff] [blame] | 65 | void SparseHistogram::AddSamples(const HistogramSamples& samples) { |
| 66 | base::AutoLock auto_lock(lock_); |
| 67 | samples_.Add(samples); |
| 68 | } |
| 69 | |
| 70 | bool SparseHistogram::AddSamplesFromPickle(PickleIterator* iter) { |
| 71 | base::AutoLock auto_lock(lock_); |
| 72 | return samples_.AddFromPickle(iter); |
| 73 | } |
| 74 | |
asvitkine | 24d3e9a | 2015-05-27 05:22:14 | [diff] [blame] | 75 | void SparseHistogram::WriteHTMLGraph(std::string* output) const { |
[email protected] | f2bb320 | 2013-04-05 21:21:54 | [diff] [blame] | 76 | output->append("<PRE>"); |
| 77 | WriteAsciiImpl(true, "<br>", output); |
| 78 | output->append("</PRE>"); |
[email protected] | 7c7a4275 | 2012-08-09 05:14:15 | [diff] [blame] | 79 | } |
| 80 | |
asvitkine | 24d3e9a | 2015-05-27 05:22:14 | [diff] [blame] | 81 | void SparseHistogram::WriteAscii(std::string* output) const { |
[email protected] | f2bb320 | 2013-04-05 21:21:54 | [diff] [blame] | 82 | WriteAsciiImpl(true, "\n", output); |
[email protected] | 7c7a4275 | 2012-08-09 05:14:15 | [diff] [blame] | 83 | } |
| 84 | |
[email protected] | c50c21d | 2013-01-11 21:52:44 | [diff] [blame] | 85 | bool SparseHistogram::SerializeInfoImpl(Pickle* pickle) const { |
| 86 | return pickle->WriteString(histogram_name()) && pickle->WriteInt(flags()); |
| 87 | } |
| 88 | |
asvitkine | 24d3e9a | 2015-05-27 05:22:14 | [diff] [blame] | 89 | SparseHistogram::SparseHistogram(const std::string& name) |
[email protected] | c50c21d | 2013-01-11 21:52:44 | [diff] [blame] | 90 | : HistogramBase(name) {} |
| 91 | |
| 92 | HistogramBase* SparseHistogram::DeserializeInfoImpl(PickleIterator* iter) { |
asvitkine | 24d3e9a | 2015-05-27 05:22:14 | [diff] [blame] | 93 | std::string histogram_name; |
[email protected] | c50c21d | 2013-01-11 21:52:44 | [diff] [blame] | 94 | 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] | 7c7a4275 | 2012-08-09 05:14:15 | [diff] [blame] | 105 | |
[email protected] | 24a7ec5 | 2012-10-08 10:31:50 | [diff] [blame] | 106 | void SparseHistogram::GetParameters(DictionaryValue* params) const { |
| 107 | // TODO(kaiwang): Implement. (See HistogramBase::WriteJSON.) |
| 108 | } |
| 109 | |
| 110 | void SparseHistogram::GetCountAndBucketData(Count* count, |
[email protected] | cdd98fc | 2013-05-10 09:32:58 | [diff] [blame] | 111 | int64* sum, |
[email protected] | 24a7ec5 | 2012-10-08 10:31:50 | [diff] [blame] | 112 | ListValue* buckets) const { |
| 113 | // TODO(kaiwang): Implement. (See HistogramBase::WriteJSON.) |
| 114 | } |
| 115 | |
[email protected] | f2bb320 | 2013-04-05 21:21:54 | [diff] [blame] | 116 | void 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(); |
asvitkine | 24d3e9a | 2015-05-27 05:22:14 | [diff] [blame] | 134 | while (!it->Done()) { |
[email protected] | f2bb320 | 2013-04-05 21:21:54 | [diff] [blame] | 135 | 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(); |
asvitkine | 24d3e9a | 2015-05-27 05:22:14 | [diff] [blame] | 149 | while (!it->Done()) { |
[email protected] | f2bb320 | 2013-04-05 21:21:54 | [diff] [blame] | 150 | Sample min; |
| 151 | Sample max; |
| 152 | Count count; |
| 153 | it->Get(&min, &max, &count); |
| 154 | |
| 155 | // value is min, so display it |
asvitkine | 24d3e9a | 2015-05-27 05:22:14 | [diff] [blame] | 156 | std::string range = GetSimpleAsciiBucketRange(min); |
[email protected] | f2bb320 | 2013-04-05 21:21:54 | [diff] [blame] | 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 | |
| 169 | void 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] | 7c7a4275 | 2012-08-09 05:14:15 | [diff] [blame] | 179 | } // namespace base |