[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] | b4af2ec | 2012-10-05 21:29:44 | [diff] [blame] | 13 | using std::map; |
[email protected] | 7c7a4275 | 2012-08-09 05:14:15 | [diff] [blame] | 14 | using std::string; |
| 15 | |
| 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 |
[email protected] | b4af2ec | 2012-10-05 21:29:44 | [diff] [blame] | 22 | HistogramBase* SparseHistogram::FactoryGet(const string& name, int32 flags) { |
[email protected] | cc7dec21 | 2013-03-01 03:53:25 | [diff] [blame] | 23 | 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] | 7c7a4275 | 2012-08-09 05:14:15 | [diff] [blame] | 33 | return histogram; |
| 34 | } |
| 35 | |
| 36 | SparseHistogram::~SparseHistogram() {} |
| 37 | |
[email protected] | 07c0240 | 2012-10-31 06:20:25 | [diff] [blame] | 38 | HistogramType SparseHistogram::GetHistogramType() const { |
| 39 | return SPARSE_HISTOGRAM; |
| 40 | } |
| 41 | |
[email protected] | 15ce384 | 2013-06-27 14:38:45 | [diff] [blame^] | 42 | bool SparseHistogram::HasConstructionArguments( |
| 43 | Sample expected_minimum, |
| 44 | Sample expected_maximum, |
| 45 | size_t expected_bucket_count) const { |
[email protected] | abae9b02 | 2012-10-24 08:18:52 | [diff] [blame] | 46 | // SparseHistogram never has min/max/bucket_count limit. |
| 47 | return false; |
| 48 | } |
| 49 | |
[email protected] | 7c7a4275 | 2012-08-09 05:14:15 | [diff] [blame] | 50 | void SparseHistogram::Add(Sample value) { |
| 51 | base::AutoLock auto_lock(lock_); |
[email protected] | c50c21d | 2013-01-11 21:52:44 | [diff] [blame] | 52 | samples_.Accumulate(value, 1); |
[email protected] | 7c7a4275 | 2012-08-09 05:14:15 | [diff] [blame] | 53 | } |
| 54 | |
[email protected] | 877ef56 | 2012-10-20 02:56:18 | [diff] [blame] | 55 | scoped_ptr<HistogramSamples> SparseHistogram::SnapshotSamples() const { |
[email protected] | b4af2ec | 2012-10-05 21:29:44 | [diff] [blame] | 56 | scoped_ptr<SampleMap> snapshot(new SampleMap()); |
| 57 | |
[email protected] | 7c7a4275 | 2012-08-09 05:14:15 | [diff] [blame] | 58 | base::AutoLock auto_lock(lock_); |
[email protected] | c50c21d | 2013-01-11 21:52:44 | [diff] [blame] | 59 | snapshot->Add(samples_); |
[email protected] | 877ef56 | 2012-10-20 02:56:18 | [diff] [blame] | 60 | return snapshot.PassAs<HistogramSamples>(); |
[email protected] | 7c7a4275 | 2012-08-09 05:14:15 | [diff] [blame] | 61 | } |
| 62 | |
[email protected] | c50c21d | 2013-01-11 21:52:44 | [diff] [blame] | 63 | void SparseHistogram::AddSamples(const HistogramSamples& samples) { |
| 64 | base::AutoLock auto_lock(lock_); |
| 65 | samples_.Add(samples); |
| 66 | } |
| 67 | |
| 68 | bool SparseHistogram::AddSamplesFromPickle(PickleIterator* iter) { |
| 69 | base::AutoLock auto_lock(lock_); |
| 70 | return samples_.AddFromPickle(iter); |
| 71 | } |
| 72 | |
[email protected] | 7c7a4275 | 2012-08-09 05:14:15 | [diff] [blame] | 73 | void SparseHistogram::WriteHTMLGraph(string* output) const { |
[email protected] | f2bb320 | 2013-04-05 21:21:54 | [diff] [blame] | 74 | output->append("<PRE>"); |
| 75 | WriteAsciiImpl(true, "<br>", output); |
| 76 | output->append("</PRE>"); |
[email protected] | 7c7a4275 | 2012-08-09 05:14:15 | [diff] [blame] | 77 | } |
| 78 | |
| 79 | void SparseHistogram::WriteAscii(string* output) const { |
[email protected] | f2bb320 | 2013-04-05 21:21:54 | [diff] [blame] | 80 | WriteAsciiImpl(true, "\n", output); |
[email protected] | 7c7a4275 | 2012-08-09 05:14:15 | [diff] [blame] | 81 | } |
| 82 | |
[email protected] | c50c21d | 2013-01-11 21:52:44 | [diff] [blame] | 83 | bool SparseHistogram::SerializeInfoImpl(Pickle* pickle) const { |
| 84 | return pickle->WriteString(histogram_name()) && pickle->WriteInt(flags()); |
| 85 | } |
| 86 | |
[email protected] | 7c7a4275 | 2012-08-09 05:14:15 | [diff] [blame] | 87 | SparseHistogram::SparseHistogram(const string& name) |
[email protected] | c50c21d | 2013-01-11 21:52:44 | [diff] [blame] | 88 | : HistogramBase(name) {} |
| 89 | |
| 90 | HistogramBase* 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] | 7c7a4275 | 2012-08-09 05:14:15 | [diff] [blame] | 103 | |
[email protected] | 24a7ec5 | 2012-10-08 10:31:50 | [diff] [blame] | 104 | void SparseHistogram::GetParameters(DictionaryValue* params) const { |
| 105 | // TODO(kaiwang): Implement. (See HistogramBase::WriteJSON.) |
| 106 | } |
| 107 | |
| 108 | void SparseHistogram::GetCountAndBucketData(Count* count, |
[email protected] | cdd98fc | 2013-05-10 09:32:58 | [diff] [blame] | 109 | int64* sum, |
[email protected] | 24a7ec5 | 2012-10-08 10:31:50 | [diff] [blame] | 110 | ListValue* buckets) const { |
| 111 | // TODO(kaiwang): Implement. (See HistogramBase::WriteJSON.) |
| 112 | } |
| 113 | |
[email protected] | f2bb320 | 2013-04-05 21:21:54 | [diff] [blame] | 114 | void 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 | |
| 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 |