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 | |
jar (doing other things)
2013/04/04 00:45:09
nit: Period at end of line.
jrummell
2013/04/04 22:38:59
Done.
| |
116 scoped_ptr<HistogramSamples> snapshot = SnapshotSamples(); | |
117 Count sample_count = snapshot->TotalCount(); | |
118 | |
119 WriteAsciiHeader(sample_count, output); | |
120 output->append(newline); | |
121 | |
122 // determine largest value (needed to determine how much space to print) | |
123 // and largest count in a bucket | |
jar (doing other things)
2013/04/04 00:45:09
nit: Upper case start of sentence, and add a perio
jrummell
2013/04/04 22:38:59
Done.
| |
124 Count max_size = 0; | |
125 Sample largest = 0; | |
jar (doing other things)
2013/04/04 00:45:09
nit: suggest largest_count as a better name. I wa
jrummell
2013/04/04 22:38:59
Done.
| |
126 scoped_ptr<SampleCountIterator> it = snapshot->Iterator(); | |
127 while (!it->Done()) | |
128 { | |
129 Sample min; | |
130 Sample max; | |
131 Count count; | |
132 it->Get(&min, &max, &count); | |
133 if (min > largest) | |
134 largest = min; | |
135 if (count > max_size) | |
136 max_size = count; | |
137 it->Next(); | |
138 } | |
139 size_t print_width = GetAsciiBucketRange(largest).size() + 1; | |
140 | |
141 // iterate over each item and display them | |
142 it = snapshot->Iterator(); | |
143 while (!it->Done()) | |
144 { | |
145 Sample min; | |
146 Sample max; | |
147 Count count; | |
148 it->Get(&min, &max, &count); | |
149 | |
150 // value is min, so display it | |
151 string range = GetAsciiBucketRange(min); | |
152 output->append(range); | |
153 for (size_t j = 0; range.size() + j < print_width + 1; ++j) | |
154 output->push_back(' '); | |
155 | |
156 if (graph_it) | |
157 WriteAsciiBucketGraph(count, max_size, output); | |
158 WriteAsciiBucketValue(count, sample_count, output); | |
159 output->append(newline); | |
160 it->Next(); | |
161 } | |
162 } | |
163 | |
164 void SparseHistogram::WriteAsciiHeader(const Count sample_count, | |
165 std::string* output) const { | |
166 StringAppendF(output, | |
167 "Histogram: %s recorded %d samples", | |
168 histogram_name().c_str(), | |
169 sample_count); | |
170 if (flags() & ~kHexRangePrintingFlag) | |
171 StringAppendF(output, " (flags = 0x%x)", flags() & ~kHexRangePrintingFlag); | |
172 } | |
173 | |
174 void SparseHistogram::WriteAsciiBucketValue(const Count current, | |
175 const Count total, | |
176 std::string* output) const { | |
177 float percentage = (float)current / (float)total * 100.0; | |
178 StringAppendF(output, " (%d = %3.1f%%)", current, percentage); | |
179 } | |
180 | |
181 void SparseHistogram::WriteAsciiBucketGraph(double current_size, | |
182 double max_size, | |
183 std::string* output) const { | |
184 const int k_line_length = 72; // Maximal horizontal width of graph. | |
jar (doing other things)
2013/04/04 00:45:09
nit: This method is also repeated in histogram.cc.
jrummell
2013/04/04 22:38:59
Done.
| |
185 int x_count = static_cast<int>(k_line_length * (current_size / max_size) | |
186 + 0.5); | |
187 int x_remainder = k_line_length - x_count; | |
188 | |
189 while (0 < x_count--) | |
190 output->append("-"); | |
191 output->append("O"); | |
192 while (0 < x_remainder--) | |
193 output->append(" "); | |
194 } | |
195 | |
196 std::string SparseHistogram::GetAsciiBucketRange(const Sample sample) const { | |
jar (doing other things)
2013/04/04 00:45:09
nit: This code is repeated in histogram.cc.
Could
jrummell
2013/04/04 22:38:59
Done.
| |
197 std::string result; | |
198 if (kHexRangePrintingFlag & flags()) | |
199 StringAppendF(&result, "%#x", sample); | |
200 else | |
201 StringAppendF(&result, "%d", sample); | |
202 return result; | |
203 } | |
204 | |
109 } // namespace base | 205 } // namespace base |
OLD | NEW |