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/stringprintf.h" |
10 #include "base/synchronization/lock.h" | 11 #include "base/synchronization/lock.h" |
11 | 12 |
12 using std::map; | 13 using std::map; |
13 using std::string; | 14 using std::string; |
14 | 15 |
15 namespace base { | 16 namespace base { |
16 | 17 |
17 typedef HistogramBase::Count Count; | 18 typedef HistogramBase::Count Count; |
18 typedef HistogramBase::Sample Sample; | 19 typedef HistogramBase::Sample Sample; |
19 | 20 |
(...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
62 base::AutoLock auto_lock(lock_); | 63 base::AutoLock auto_lock(lock_); |
63 samples_.Add(samples); | 64 samples_.Add(samples); |
64 } | 65 } |
65 | 66 |
66 bool SparseHistogram::AddSamplesFromPickle(PickleIterator* iter) { | 67 bool SparseHistogram::AddSamplesFromPickle(PickleIterator* iter) { |
67 base::AutoLock auto_lock(lock_); | 68 base::AutoLock auto_lock(lock_); |
68 return samples_.AddFromPickle(iter); | 69 return samples_.AddFromPickle(iter); |
69 } | 70 } |
70 | 71 |
71 void SparseHistogram::WriteHTMLGraph(string* output) const { | 72 void SparseHistogram::WriteHTMLGraph(string* output) const { |
72 // TODO(kaiwang): Implement. | 73 output->append("<PRE>"); |
| 74 WriteAsciiImpl(true, "<br>", output); |
| 75 output->append("</PRE>"); |
73 } | 76 } |
74 | 77 |
75 void SparseHistogram::WriteAscii(string* output) const { | 78 void SparseHistogram::WriteAscii(string* output) const { |
76 // TODO(kaiwang): Implement. | 79 WriteAsciiImpl(true, "\n", output); |
77 } | 80 } |
78 | 81 |
79 bool SparseHistogram::SerializeInfoImpl(Pickle* pickle) const { | 82 bool SparseHistogram::SerializeInfoImpl(Pickle* pickle) const { |
80 return pickle->WriteString(histogram_name()) && pickle->WriteInt(flags()); | 83 return pickle->WriteString(histogram_name()) && pickle->WriteInt(flags()); |
81 } | 84 } |
82 | 85 |
83 SparseHistogram::SparseHistogram(const string& name) | 86 SparseHistogram::SparseHistogram(const string& name) |
84 : HistogramBase(name) {} | 87 : HistogramBase(name) {} |
85 | 88 |
86 HistogramBase* SparseHistogram::DeserializeInfoImpl(PickleIterator* iter) { | 89 HistogramBase* SparseHistogram::DeserializeInfoImpl(PickleIterator* iter) { |
(...skipping 12 matching lines...) Expand all Loading... |
99 | 102 |
100 void SparseHistogram::GetParameters(DictionaryValue* params) const { | 103 void SparseHistogram::GetParameters(DictionaryValue* params) const { |
101 // TODO(kaiwang): Implement. (See HistogramBase::WriteJSON.) | 104 // TODO(kaiwang): Implement. (See HistogramBase::WriteJSON.) |
102 } | 105 } |
103 | 106 |
104 void SparseHistogram::GetCountAndBucketData(Count* count, | 107 void SparseHistogram::GetCountAndBucketData(Count* count, |
105 ListValue* buckets) const { | 108 ListValue* buckets) const { |
106 // TODO(kaiwang): Implement. (See HistogramBase::WriteJSON.) | 109 // TODO(kaiwang): Implement. (See HistogramBase::WriteJSON.) |
107 } | 110 } |
108 | 111 |
| 112 void SparseHistogram::WriteAsciiImpl(bool graph_it, |
| 113 const std::string& newline, |
| 114 std::string* output) const { |
| 115 // Get a local copy of the data so we are consistent. |
| 116 scoped_ptr<HistogramSamples> snapshot = SnapshotSamples(); |
| 117 Count total_count = snapshot->TotalCount(); |
| 118 double scaled_total_count = total_count / 100.0; |
| 119 |
| 120 WriteAsciiHeader(total_count, output); |
| 121 output->append(newline); |
| 122 |
| 123 // Determine how wide the largest bucket range is (how many digits to print), |
| 124 // so that we'll be able to right-align starts for the graphical bars. |
| 125 // Determine which bucket has the largest sample count so that we can |
| 126 // normalize the graphical bar-width relative to that sample count. |
| 127 Count largest_count = 0; |
| 128 Sample largest_sample = 0; |
| 129 scoped_ptr<SampleCountIterator> it = snapshot->Iterator(); |
| 130 while (!it->Done()) |
| 131 { |
| 132 Sample min; |
| 133 Sample max; |
| 134 Count count; |
| 135 it->Get(&min, &max, &count); |
| 136 if (min > largest_sample) |
| 137 largest_sample = min; |
| 138 if (count > largest_count) |
| 139 largest_count = count; |
| 140 it->Next(); |
| 141 } |
| 142 size_t print_width = GetSimpleAsciiBucketRange(largest_sample).size() + 1; |
| 143 |
| 144 // iterate over each item and display them |
| 145 it = snapshot->Iterator(); |
| 146 while (!it->Done()) |
| 147 { |
| 148 Sample min; |
| 149 Sample max; |
| 150 Count count; |
| 151 it->Get(&min, &max, &count); |
| 152 |
| 153 // value is min, so display it |
| 154 string range = GetSimpleAsciiBucketRange(min); |
| 155 output->append(range); |
| 156 for (size_t j = 0; range.size() + j < print_width + 1; ++j) |
| 157 output->push_back(' '); |
| 158 |
| 159 if (graph_it) |
| 160 WriteAsciiBucketGraph(count, largest_count, output); |
| 161 WriteAsciiBucketValue(count, scaled_total_count, output); |
| 162 output->append(newline); |
| 163 it->Next(); |
| 164 } |
| 165 } |
| 166 |
| 167 void SparseHistogram::WriteAsciiHeader(const Count total_count, |
| 168 std::string* output) const { |
| 169 StringAppendF(output, |
| 170 "Histogram: %s recorded %d samples", |
| 171 histogram_name().c_str(), |
| 172 total_count); |
| 173 if (flags() & ~kHexRangePrintingFlag) |
| 174 StringAppendF(output, " (flags = 0x%x)", flags() & ~kHexRangePrintingFlag); |
| 175 } |
| 176 |
109 } // namespace base | 177 } // namespace base |
OLD | NEW |