| 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/persistent_histogram_allocator.h" | 10 #include "base/metrics/persistent_histogram_allocator.h" |
| 10 #include "base/stl_util.h" | 11 #include "base/stl_util.h" |
| 11 | 12 |
| 12 namespace base { | 13 namespace base { |
| 13 | 14 |
| 14 typedef HistogramBase::Count Count; | 15 typedef HistogramBase::Count Count; |
| 15 typedef HistogramBase::Sample Sample; | 16 typedef HistogramBase::Sample Sample; |
| 16 | 17 |
| 17 namespace { | 18 namespace { |
| 18 | 19 |
| 20 enum NegativeSampleReason { |
| 21 PERSISTENT_SPARSE_HAVE_LOGGED_BUT_NOT_SAMPLE, |
| 22 PERSISTENT_SPARSE_SAMPLE_LESS_THAN_LOGGED, |
| 23 MAX_NEGATIVE_SAMPLE_REASONS |
| 24 }; |
| 25 |
| 19 // An iterator for going through a PersistentSampleMap. The logic here is | 26 // An iterator for going through a PersistentSampleMap. The logic here is |
| 20 // identical to that of SampleMapIterator but with different data structures. | 27 // identical to that of SampleMapIterator but with different data structures. |
| 21 // Changes here likely need to be duplicated there. | 28 // Changes here likely need to be duplicated there. |
| 22 class PersistentSampleMapIterator : public SampleCountIterator { | 29 class PersistentSampleMapIterator : public SampleCountIterator { |
| 23 public: | 30 public: |
| 24 typedef std::map<HistogramBase::Sample, HistogramBase::Count*> | 31 typedef std::map<HistogramBase::Sample, HistogramBase::Count*> |
| 25 SampleToCountMap; | 32 SampleToCountMap; |
| 26 | 33 |
| 27 explicit PersistentSampleMapIterator(const SampleToCountMap& sample_counts); | 34 explicit PersistentSampleMapIterator(const SampleToCountMap& sample_counts); |
| 28 ~PersistentSampleMapIterator() override; | 35 ~PersistentSampleMapIterator() override; |
| (...skipping 145 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 174 return ref; | 181 return ref; |
| 175 } | 182 } |
| 176 | 183 |
| 177 bool PersistentSampleMap::AddSubtractImpl(SampleCountIterator* iter, | 184 bool PersistentSampleMap::AddSubtractImpl(SampleCountIterator* iter, |
| 178 Operator op) { | 185 Operator op) { |
| 179 Sample min; | 186 Sample min; |
| 180 Sample max; | 187 Sample max; |
| 181 Count count; | 188 Count count; |
| 182 for (; !iter->Done(); iter->Next()) { | 189 for (; !iter->Done(); iter->Next()) { |
| 183 iter->Get(&min, &max, &count); | 190 iter->Get(&min, &max, &count); |
| 191 if (count == 0) |
| 192 continue; |
| 184 if (min + 1 != max) | 193 if (min + 1 != max) |
| 185 return false; // SparseHistogram only supports bucket with size 1. | 194 return false; // SparseHistogram only supports bucket with size 1. |
| 186 | 195 |
| 196 #if 0 // TODO(bcwhite) Re-enable efficient version after crbug.com/682680. |
| 187 *GetOrCreateSampleCountStorage(min) += | 197 *GetOrCreateSampleCountStorage(min) += |
| 188 (op == HistogramSamples::ADD) ? count : -count; | 198 (op == HistogramSamples::ADD) ? count : -count; |
| 199 #else |
| 200 if (op == HistogramSamples::ADD) { |
| 201 *GetOrCreateSampleCountStorage(min) += count; |
| 202 } else { |
| 203 // Subtract is used only for determining deltas when reporting which |
| 204 // 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. |
| 206 NegativeSampleReason reason = MAX_NEGATIVE_SAMPLE_REASONS; |
| 207 Count* bucket = GetSampleCountStorage(min); |
| 208 if (bucket == nullptr) { |
| 209 reason = PERSISTENT_SPARSE_HAVE_LOGGED_BUT_NOT_SAMPLE; |
| 210 } else { |
| 211 if (*bucket < count) { |
| 212 reason = PERSISTENT_SPARSE_SAMPLE_LESS_THAN_LOGGED; |
| 213 *bucket = 0; |
| 214 } else { |
| 215 *bucket -= count; |
| 216 } |
| 217 } |
| 218 if (reason != MAX_NEGATIVE_SAMPLE_REASONS) { |
| 219 UMA_HISTOGRAM_ENUMERATION("UMA.NegativeSamples.Reason", reason, |
| 220 MAX_NEGATIVE_SAMPLE_REASONS); |
| 221 } |
| 222 } |
| 223 #endif |
| 189 } | 224 } |
| 190 return true; | 225 return true; |
| 191 } | 226 } |
| 192 | 227 |
| 193 Count* PersistentSampleMap::GetSampleCountStorage(Sample value) { | 228 Count* PersistentSampleMap::GetSampleCountStorage(Sample value) { |
| 194 // If |value| is already in the map, just return that. | 229 // If |value| is already in the map, just return that. |
| 195 auto it = sample_counts_.find(value); | 230 auto it = sample_counts_.find(value); |
| 196 if (it != sample_counts_.end()) | 231 if (it != sample_counts_.end()) |
| 197 return it->second; | 232 return it->second; |
| 198 | 233 |
| (...skipping 78 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 277 found_count = &record->count; | 312 found_count = &record->count; |
| 278 if (!import_everything) | 313 if (!import_everything) |
| 279 break; | 314 break; |
| 280 } | 315 } |
| 281 } | 316 } |
| 282 | 317 |
| 283 return found_count; | 318 return found_count; |
| 284 } | 319 } |
| 285 | 320 |
| 286 } // namespace base | 321 } // namespace base |
| OLD | NEW |