| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "base/metrics/sparse_histogram.h" | 5 #include "base/metrics/sparse_histogram.h" |
| 6 | 6 |
| 7 #include "base/metrics/sample_map.h" | 7 #include "base/metrics/sample_map.h" |
| 8 #include "base/metrics/statistics_recorder.h" | 8 #include "base/metrics/statistics_recorder.h" |
| 9 #include "base/pickle.h" | 9 #include "base/pickle.h" |
| 10 #include "base/strings/stringprintf.h" | 10 #include "base/strings/stringprintf.h" |
| 11 #include "base/synchronization/lock.h" | 11 #include "base/synchronization/lock.h" |
| 12 | 12 |
| 13 using std::map; | |
| 14 using std::string; | |
| 15 | |
| 16 namespace base { | 13 namespace base { |
| 17 | 14 |
| 18 typedef HistogramBase::Count Count; | 15 typedef HistogramBase::Count Count; |
| 19 typedef HistogramBase::Sample Sample; | 16 typedef HistogramBase::Sample Sample; |
| 20 | 17 |
| 21 // static | 18 // static |
| 22 HistogramBase* SparseHistogram::FactoryGet(const string& name, int32 flags) { | 19 HistogramBase* SparseHistogram::FactoryGet(const std::string& name, |
| 20 int32 flags) { |
| 23 HistogramBase* histogram = StatisticsRecorder::FindHistogram(name); | 21 HistogramBase* histogram = StatisticsRecorder::FindHistogram(name); |
| 24 | 22 |
| 25 if (!histogram) { | 23 if (!histogram) { |
| 26 // To avoid racy destruction at shutdown, the following will be leaked. | 24 // To avoid racy destruction at shutdown, the following will be leaked. |
| 27 HistogramBase* tentative_histogram = new SparseHistogram(name); | 25 HistogramBase* tentative_histogram = new SparseHistogram(name); |
| 28 tentative_histogram->SetFlags(flags); | 26 tentative_histogram->SetFlags(flags); |
| 29 histogram = | 27 histogram = |
| 30 StatisticsRecorder::RegisterOrDeleteDuplicate(tentative_histogram); | 28 StatisticsRecorder::RegisterOrDeleteDuplicate(tentative_histogram); |
| 31 } | 29 } |
| 32 DCHECK_EQ(SPARSE_HISTOGRAM, histogram->GetHistogramType()); | 30 DCHECK_EQ(SPARSE_HISTOGRAM, histogram->GetHistogramType()); |
| (...skipping 30 matching lines...) Expand all Loading... |
| 63 void SparseHistogram::AddSamples(const HistogramSamples& samples) { | 61 void SparseHistogram::AddSamples(const HistogramSamples& samples) { |
| 64 base::AutoLock auto_lock(lock_); | 62 base::AutoLock auto_lock(lock_); |
| 65 samples_.Add(samples); | 63 samples_.Add(samples); |
| 66 } | 64 } |
| 67 | 65 |
| 68 bool SparseHistogram::AddSamplesFromPickle(PickleIterator* iter) { | 66 bool SparseHistogram::AddSamplesFromPickle(PickleIterator* iter) { |
| 69 base::AutoLock auto_lock(lock_); | 67 base::AutoLock auto_lock(lock_); |
| 70 return samples_.AddFromPickle(iter); | 68 return samples_.AddFromPickle(iter); |
| 71 } | 69 } |
| 72 | 70 |
| 73 void SparseHistogram::WriteHTMLGraph(string* output) const { | 71 void SparseHistogram::WriteHTMLGraph(std::string* output) const { |
| 74 output->append("<PRE>"); | 72 output->append("<PRE>"); |
| 75 WriteAsciiImpl(true, "<br>", output); | 73 WriteAsciiImpl(true, "<br>", output); |
| 76 output->append("</PRE>"); | 74 output->append("</PRE>"); |
| 77 } | 75 } |
| 78 | 76 |
| 79 void SparseHistogram::WriteAscii(string* output) const { | 77 void SparseHistogram::WriteAscii(std::string* output) const { |
| 80 WriteAsciiImpl(true, "\n", output); | 78 WriteAsciiImpl(true, "\n", output); |
| 81 } | 79 } |
| 82 | 80 |
| 83 bool SparseHistogram::SerializeInfoImpl(Pickle* pickle) const { | 81 bool SparseHistogram::SerializeInfoImpl(Pickle* pickle) const { |
| 84 return pickle->WriteString(histogram_name()) && pickle->WriteInt(flags()); | 82 return pickle->WriteString(histogram_name()) && pickle->WriteInt(flags()); |
| 85 } | 83 } |
| 86 | 84 |
| 87 SparseHistogram::SparseHistogram(const string& name) | 85 SparseHistogram::SparseHistogram(const std::string& name) |
| 88 : HistogramBase(name) {} | 86 : HistogramBase(name) {} |
| 89 | 87 |
| 90 HistogramBase* SparseHistogram::DeserializeInfoImpl(PickleIterator* iter) { | 88 HistogramBase* SparseHistogram::DeserializeInfoImpl(PickleIterator* iter) { |
| 91 string histogram_name; | 89 std::string histogram_name; |
| 92 int flags; | 90 int flags; |
| 93 if (!iter->ReadString(&histogram_name) || !iter->ReadInt(&flags)) { | 91 if (!iter->ReadString(&histogram_name) || !iter->ReadInt(&flags)) { |
| 94 DLOG(ERROR) << "Pickle error decoding Histogram: " << histogram_name; | 92 DLOG(ERROR) << "Pickle error decoding Histogram: " << histogram_name; |
| 95 return NULL; | 93 return NULL; |
| 96 } | 94 } |
| 97 | 95 |
| 98 DCHECK(flags & HistogramBase::kIPCSerializationSourceFlag); | 96 DCHECK(flags & HistogramBase::kIPCSerializationSourceFlag); |
| 99 flags &= ~HistogramBase::kIPCSerializationSourceFlag; | 97 flags &= ~HistogramBase::kIPCSerializationSourceFlag; |
| 100 | 98 |
| 101 return SparseHistogram::FactoryGet(histogram_name, flags); | 99 return SparseHistogram::FactoryGet(histogram_name, flags); |
| (...skipping 20 matching lines...) Expand all Loading... |
| 122 WriteAsciiHeader(total_count, output); | 120 WriteAsciiHeader(total_count, output); |
| 123 output->append(newline); | 121 output->append(newline); |
| 124 | 122 |
| 125 // Determine how wide the largest bucket range is (how many digits to print), | 123 // 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. | 124 // 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 | 125 // Determine which bucket has the largest sample count so that we can |
| 128 // normalize the graphical bar-width relative to that sample count. | 126 // normalize the graphical bar-width relative to that sample count. |
| 129 Count largest_count = 0; | 127 Count largest_count = 0; |
| 130 Sample largest_sample = 0; | 128 Sample largest_sample = 0; |
| 131 scoped_ptr<SampleCountIterator> it = snapshot->Iterator(); | 129 scoped_ptr<SampleCountIterator> it = snapshot->Iterator(); |
| 132 while (!it->Done()) | 130 while (!it->Done()) { |
| 133 { | |
| 134 Sample min; | 131 Sample min; |
| 135 Sample max; | 132 Sample max; |
| 136 Count count; | 133 Count count; |
| 137 it->Get(&min, &max, &count); | 134 it->Get(&min, &max, &count); |
| 138 if (min > largest_sample) | 135 if (min > largest_sample) |
| 139 largest_sample = min; | 136 largest_sample = min; |
| 140 if (count > largest_count) | 137 if (count > largest_count) |
| 141 largest_count = count; | 138 largest_count = count; |
| 142 it->Next(); | 139 it->Next(); |
| 143 } | 140 } |
| 144 size_t print_width = GetSimpleAsciiBucketRange(largest_sample).size() + 1; | 141 size_t print_width = GetSimpleAsciiBucketRange(largest_sample).size() + 1; |
| 145 | 142 |
| 146 // iterate over each item and display them | 143 // iterate over each item and display them |
| 147 it = snapshot->Iterator(); | 144 it = snapshot->Iterator(); |
| 148 while (!it->Done()) | 145 while (!it->Done()) { |
| 149 { | |
| 150 Sample min; | 146 Sample min; |
| 151 Sample max; | 147 Sample max; |
| 152 Count count; | 148 Count count; |
| 153 it->Get(&min, &max, &count); | 149 it->Get(&min, &max, &count); |
| 154 | 150 |
| 155 // value is min, so display it | 151 // value is min, so display it |
| 156 string range = GetSimpleAsciiBucketRange(min); | 152 std::string range = GetSimpleAsciiBucketRange(min); |
| 157 output->append(range); | 153 output->append(range); |
| 158 for (size_t j = 0; range.size() + j < print_width + 1; ++j) | 154 for (size_t j = 0; range.size() + j < print_width + 1; ++j) |
| 159 output->push_back(' '); | 155 output->push_back(' '); |
| 160 | 156 |
| 161 if (graph_it) | 157 if (graph_it) |
| 162 WriteAsciiBucketGraph(count, largest_count, output); | 158 WriteAsciiBucketGraph(count, largest_count, output); |
| 163 WriteAsciiBucketValue(count, scaled_total_count, output); | 159 WriteAsciiBucketValue(count, scaled_total_count, output); |
| 164 output->append(newline); | 160 output->append(newline); |
| 165 it->Next(); | 161 it->Next(); |
| 166 } | 162 } |
| 167 } | 163 } |
| 168 | 164 |
| 169 void SparseHistogram::WriteAsciiHeader(const Count total_count, | 165 void SparseHistogram::WriteAsciiHeader(const Count total_count, |
| 170 std::string* output) const { | 166 std::string* output) const { |
| 171 StringAppendF(output, | 167 StringAppendF(output, |
| 172 "Histogram: %s recorded %d samples", | 168 "Histogram: %s recorded %d samples", |
| 173 histogram_name().c_str(), | 169 histogram_name().c_str(), |
| 174 total_count); | 170 total_count); |
| 175 if (flags() & ~kHexRangePrintingFlag) | 171 if (flags() & ~kHexRangePrintingFlag) |
| 176 StringAppendF(output, " (flags = 0x%x)", flags() & ~kHexRangePrintingFlag); | 172 StringAppendF(output, " (flags = 0x%x)", flags() & ~kHexRangePrintingFlag); |
| 177 } | 173 } |
| 178 | 174 |
| 179 } // namespace base | 175 } // namespace base |
| OLD | NEW |