| 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/persistent_histogram_allocator.h" | 9 #include "base/metrics/persistent_histogram_allocator.h" |
| 10 #include "base/stl_util.h" | 10 #include "base/stl_util.h" |
| (...skipping 186 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 197 if (min + 1 != max) | 197 if (min + 1 != max) |
| 198 return false; // SparseHistogram only supports bucket with size 1. | 198 return false; // SparseHistogram only supports bucket with size 1. |
| 199 | 199 |
| 200 *GetOrCreateSampleCountStorage(min) += | 200 *GetOrCreateSampleCountStorage(min) += |
| 201 (op == HistogramSamples::ADD) ? count : -count; | 201 (op == HistogramSamples::ADD) ? count : -count; |
| 202 } | 202 } |
| 203 return true; | 203 return true; |
| 204 } | 204 } |
| 205 | 205 |
| 206 Count* PersistentSampleMap::GetSampleCountStorage(Sample value) { | 206 Count* PersistentSampleMap::GetSampleCountStorage(Sample value) { |
| 207 DCHECK_LE(0, value); | |
| 208 | |
| 209 // If |value| is already in the map, just return that. | 207 // If |value| is already in the map, just return that. |
| 210 auto it = sample_counts_.find(value); | 208 auto it = sample_counts_.find(value); |
| 211 if (it != sample_counts_.end()) | 209 if (it != sample_counts_.end()) |
| 212 return it->second; | 210 return it->second; |
| 213 | 211 |
| 214 // Import any new samples from persistent memory looking for the value. | 212 // Import any new samples from persistent memory looking for the value. |
| 215 return ImportSamples(value); | 213 return ImportSamples(value); |
| 216 } | 214 } |
| 217 | 215 |
| 218 Count* PersistentSampleMap::GetOrCreateSampleCountStorage(Sample value) { | 216 Count* PersistentSampleMap::GetOrCreateSampleCountStorage(Sample value) { |
| (...skipping 22 matching lines...) Expand all Loading... |
| 241 // will always use the same record, whichever was first made iterable. | 239 // will always use the same record, whichever was first made iterable. |
| 242 // Thread-safety within a process where multiple threads use the same | 240 // Thread-safety within a process where multiple threads use the same |
| 243 // histogram object is delegated to the controlling histogram object which, | 241 // histogram object is delegated to the controlling histogram object which, |
| 244 // for sparse histograms, is a lock object. | 242 // for sparse histograms, is a lock object. |
| 245 count_pointer = ImportSamples(value); | 243 count_pointer = ImportSamples(value); |
| 246 DCHECK(count_pointer); | 244 DCHECK(count_pointer); |
| 247 return count_pointer; | 245 return count_pointer; |
| 248 } | 246 } |
| 249 | 247 |
| 250 Count* PersistentSampleMap::ImportSamples(Sample until_value) { | 248 Count* PersistentSampleMap::ImportSamples(Sample until_value) { |
| 249 Count* found_count = nullptr; |
| 251 PersistentMemoryAllocator::Reference ref; | 250 PersistentMemoryAllocator::Reference ref; |
| 252 while ((ref = records_->GetNext()) != 0) { | 251 while ((ref = records_->GetNext()) != 0) { |
| 253 SampleRecord* record = | 252 SampleRecord* record = |
| 254 records_->GetAsObject<SampleRecord>(ref, kTypeIdSampleRecord); | 253 records_->GetAsObject<SampleRecord>(ref, kTypeIdSampleRecord); |
| 255 if (!record) | 254 if (!record) |
| 256 continue; | 255 continue; |
| 257 | 256 |
| 258 DCHECK_EQ(id(), record->id); | 257 DCHECK_EQ(id(), record->id); |
| 259 | 258 |
| 260 // Check if the record's value is already known. | 259 // Check if the record's value is already known. |
| 261 if (!ContainsKey(sample_counts_, record->value)) { | 260 if (!ContainsKey(sample_counts_, record->value)) { |
| 262 // No: Add it to map of known values if the value is valid. | 261 // No: Add it to map of known values. |
| 263 if (record->value >= 0) | 262 sample_counts_[record->value] = &record->count; |
| 264 sample_counts_[record->value] = &record->count; | |
| 265 } else { | 263 } else { |
| 266 // Yes: Ignore it; it's a duplicate caused by a race condition -- see | 264 // Yes: Ignore it; it's a duplicate caused by a race condition -- see |
| 267 // code & comment in GetOrCreateSampleCountStorage() for details. | 265 // code & comment in GetOrCreateSampleCountStorage() for details. |
| 268 // Check that nothing ever operated on the duplicate record. | 266 // Check that nothing ever operated on the duplicate record. |
| 269 DCHECK_EQ(0, record->count); | 267 DCHECK_EQ(0, record->count); |
| 270 } | 268 } |
| 271 | 269 |
| 272 // Stop if it's the value being searched for. | 270 // Check if it's the value being searched for. There is a small chance |
| 273 if (record->value == until_value) | 271 // that some histogram is actually using the "kAllSamples" value. In |
| 274 return &record->count; | 272 // that case, keep the pointer for later but continue loading all the |
| 273 // rest of the samples, too. And because race conditions can cause |
| 274 // multiple records for a single value, be sure to only return the |
| 275 // first one found. |
| 276 if (record->value == until_value) { |
| 277 if (!found_count) |
| 278 found_count = &record->count; |
| 279 if (until_value != kAllSamples) |
| 280 break; |
| 281 } |
| 275 } | 282 } |
| 276 | 283 |
| 277 return nullptr; | 284 return found_count; |
| 278 } | 285 } |
| 279 | 286 |
| 280 } // namespace base | 287 } // namespace base |
| OLD | NEW |