OLD | NEW |
---|---|
1 // Copyright 2016 The Chromium Authors. All rights reserved. | 1 // Copyright 2016 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/persistent_sample_map.h" | 5 #include "base/metrics/persistent_sample_map.h" |
6 | 6 |
7 #include "base/logging.h" | 7 #include "base/logging.h" |
8 #include "base/memory/ptr_util.h" | 8 #include "base/memory/ptr_util.h" |
9 #include "base/metrics/histogram_macros.h" | 9 #include "base/metrics/histogram_macros.h" |
10 #include "base/metrics/persistent_histogram_allocator.h" | 10 #include "base/metrics/persistent_histogram_allocator.h" |
11 #include "base/stl_util.h" | 11 #include "base/stl_util.h" |
12 | 12 |
13 namespace base { | 13 namespace base { |
14 | 14 |
15 typedef HistogramBase::Count Count; | 15 typedef HistogramBase::Count Count; |
16 typedef HistogramBase::Sample Sample; | 16 typedef HistogramBase::Sample Sample; |
17 | 17 |
18 namespace { | 18 namespace { |
19 | 19 |
20 enum NegativeSampleReason { | 20 enum NegativeSampleReason { |
21 PERSISTENT_SPARSE_HAVE_LOGGED_BUT_NOT_SAMPLE, | 21 PERSISTENT_SPARSE_HAVE_LOGGED_BUT_NOT_SAMPLE, |
22 PERSISTENT_SPARSE_SAMPLE_LESS_THAN_LOGGED, | 22 PERSISTENT_SPARSE_SAMPLE_LESS_THAN_LOGGED, |
23 PERSISTENT_SPARSE_ADDED_NEGATIVE_COUNT, | |
24 PERSISTENT_SPARSE_ADD_WENT_NEGATIVE, | |
23 MAX_NEGATIVE_SAMPLE_REASONS | 25 MAX_NEGATIVE_SAMPLE_REASONS |
24 }; | 26 }; |
25 | 27 |
26 // An iterator for going through a PersistentSampleMap. The logic here is | 28 // An iterator for going through a PersistentSampleMap. The logic here is |
27 // identical to that of SampleMapIterator but with different data structures. | 29 // identical to that of SampleMapIterator but with different data structures. |
28 // Changes here likely need to be duplicated there. | 30 // Changes here likely need to be duplicated there. |
29 class PersistentSampleMapIterator : public SampleCountIterator { | 31 class PersistentSampleMapIterator : public SampleCountIterator { |
30 public: | 32 public: |
31 typedef std::map<HistogramBase::Sample, HistogramBase::Count*> | 33 typedef std::map<HistogramBase::Sample, HistogramBase::Count*> |
32 SampleToCountMap; | 34 SampleToCountMap; |
(...skipping 157 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
190 iter->Get(&min, &max, &count); | 192 iter->Get(&min, &max, &count); |
191 if (count == 0) | 193 if (count == 0) |
192 continue; | 194 continue; |
193 if (min + 1 != max) | 195 if (min + 1 != max) |
194 return false; // SparseHistogram only supports bucket with size 1. | 196 return false; // SparseHistogram only supports bucket with size 1. |
195 | 197 |
196 #if 0 // TODO(bcwhite) Re-enable efficient version after crbug.com/682680. | 198 #if 0 // TODO(bcwhite) Re-enable efficient version after crbug.com/682680. |
197 *GetOrCreateSampleCountStorage(min) += | 199 *GetOrCreateSampleCountStorage(min) += |
198 (op == HistogramSamples::ADD) ? count : -count; | 200 (op == HistogramSamples::ADD) ? count : -count; |
199 #else | 201 #else |
202 NegativeSampleReason reason = MAX_NEGATIVE_SAMPLE_REASONS; | |
200 if (op == HistogramSamples::ADD) { | 203 if (op == HistogramSamples::ADD) { |
201 *GetOrCreateSampleCountStorage(min) += count; | 204 // Add should generally be adding only positive values. |
205 Count* bucket = GetOrCreateSampleCountStorage(min); | |
Alexei Svitkine (slow)
2017/04/26 19:37:58
Nit: call this bucket_value as |bucket| is often u
bcwhite
2017/04/26 20:47:14
Went with |local_count_ptr| to match with |count|
| |
206 if (count < 0) { | |
207 reason = PERSISTENT_SPARSE_ADDED_NEGATIVE_COUNT; | |
208 if (*bucket < -count) { | |
209 reason = PERSISTENT_SPARSE_ADD_WENT_NEGATIVE; | |
210 *bucket = 0; | |
211 } | |
212 } else { | |
213 *bucket += count; | |
Alexei Svitkine (slow)
2017/04/26 19:37:58
Couldn't this result in a negative too? i.e. due t
bcwhite
2017/04/26 20:47:14
Done.
| |
214 } | |
202 } else { | 215 } else { |
203 // Subtract is used only for determining deltas when reporting which | 216 // Subtract is used only for determining deltas when reporting which |
204 // means that it's in the "logged" iterator. It should have an active | 217 // means that it's in the "logged" iterator. It should have an active |
205 // sample record and thus there is no need to try to create one. | 218 // sample record and thus there is no need to try to create one. |
206 NegativeSampleReason reason = MAX_NEGATIVE_SAMPLE_REASONS; | |
207 Count* bucket = GetSampleCountStorage(min); | 219 Count* bucket = GetSampleCountStorage(min); |
208 if (bucket == nullptr) { | 220 if (bucket == nullptr) { |
209 reason = PERSISTENT_SPARSE_HAVE_LOGGED_BUT_NOT_SAMPLE; | 221 reason = PERSISTENT_SPARSE_HAVE_LOGGED_BUT_NOT_SAMPLE; |
210 } else { | 222 } else { |
211 if (*bucket < count) { | 223 if (*bucket < count) { |
212 reason = PERSISTENT_SPARSE_SAMPLE_LESS_THAN_LOGGED; | 224 reason = PERSISTENT_SPARSE_SAMPLE_LESS_THAN_LOGGED; |
213 *bucket = 0; | 225 *bucket = 0; |
214 } else { | 226 } else { |
215 *bucket -= count; | 227 *bucket -= count; |
216 } | 228 } |
217 } | 229 } |
218 if (reason != MAX_NEGATIVE_SAMPLE_REASONS) { | 230 } |
219 UMA_HISTOGRAM_ENUMERATION("UMA.NegativeSamples.Reason", reason, | 231 if (reason != MAX_NEGATIVE_SAMPLE_REASONS) { |
220 MAX_NEGATIVE_SAMPLE_REASONS); | 232 NOTREACHED(); |
221 } | 233 UMA_HISTOGRAM_ENUMERATION("UMA.NegativeSamples.Reason", reason, |
234 MAX_NEGATIVE_SAMPLE_REASONS); | |
222 } | 235 } |
223 #endif | 236 #endif |
224 } | 237 } |
225 return true; | 238 return true; |
226 } | 239 } |
227 | 240 |
228 Count* PersistentSampleMap::GetSampleCountStorage(Sample value) { | 241 Count* PersistentSampleMap::GetSampleCountStorage(Sample value) { |
229 // If |value| is already in the map, just return that. | 242 // If |value| is already in the map, just return that. |
230 auto it = sample_counts_.find(value); | 243 auto it = sample_counts_.find(value); |
231 if (it != sample_counts_.end()) | 244 if (it != sample_counts_.end()) |
(...skipping 80 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
312 found_count = &record->count; | 325 found_count = &record->count; |
313 if (!import_everything) | 326 if (!import_everything) |
314 break; | 327 break; |
315 } | 328 } |
316 } | 329 } |
317 | 330 |
318 return found_count; | 331 return found_count; |
319 } | 332 } |
320 | 333 |
321 } // namespace base | 334 } // namespace base |
OLD | NEW |