| 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/stl_util.h" | 8 #include "base/stl_util.h" |
| 9 | 9 |
| 10 namespace base { | 10 namespace base { |
| (...skipping 77 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 88 // The type-id used to identify sample records inside an allocator. | 88 // The type-id used to identify sample records inside an allocator. |
| 89 const uint32_t kTypeIdSampleRecord = 0x8FE6A69F + 1; // SHA1(SampleRecord) v1 | 89 const uint32_t kTypeIdSampleRecord = 0x8FE6A69F + 1; // SHA1(SampleRecord) v1 |
| 90 | 90 |
| 91 } // namespace | 91 } // namespace |
| 92 | 92 |
| 93 PersistentSampleMap::PersistentSampleMap( | 93 PersistentSampleMap::PersistentSampleMap( |
| 94 uint64_t id, | 94 uint64_t id, |
| 95 PersistentMemoryAllocator* allocator, | 95 PersistentMemoryAllocator* allocator, |
| 96 Metadata* meta) | 96 Metadata* meta) |
| 97 : HistogramSamples(id, meta), | 97 : HistogramSamples(id, meta), |
| 98 allocator_(allocator) { | 98 allocator_(allocator), |
| 99 // This is created once but will continue to return new iterables even when | 99 sample_iter_(allocator) { |
| 100 // it has previously reached the end. | |
| 101 allocator->CreateIterator(&sample_iter_); | |
| 102 | |
| 103 // Load all existing samples during construction. It's no worse to do it | 100 // Load all existing samples during construction. It's no worse to do it |
| 104 // here than at some point in the future and could be better if construction | 101 // here than at some point in the future and could be better if construction |
| 105 // takes place on some background thread. New samples could be created at | 102 // takes place on some background thread. New samples could be created at |
| 106 // any time by parallel threads; if so, they'll get loaded when needed. | 103 // any time by parallel threads; if so, they'll get loaded when needed. |
| 107 ImportSamples(kAllSamples); | 104 ImportSamples(kAllSamples); |
| 108 } | 105 } |
| 109 | 106 |
| 110 PersistentSampleMap::~PersistentSampleMap() {} | 107 PersistentSampleMap::~PersistentSampleMap() {} |
| 111 | 108 |
| 112 void PersistentSampleMap::Accumulate(Sample value, Count count) { | 109 void PersistentSampleMap::Accumulate(Sample value, Count count) { |
| (...skipping 110 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 223 // relatively small, as is the current case. | 220 // relatively small, as is the current case. |
| 224 // | 221 // |
| 225 // However, it is not so good for objects that are created, detroyed, and | 222 // However, it is not so good for objects that are created, detroyed, and |
| 226 // recreated on a periodic basis, such as when making a snapshot of | 223 // recreated on a periodic basis, such as when making a snapshot of |
| 227 // sparse histograms owned by another, ongoing process. In that case, the | 224 // sparse histograms owned by another, ongoing process. In that case, the |
| 228 // entire cost is compressed into a single sequential operation... on the | 225 // entire cost is compressed into a single sequential operation... on the |
| 229 // UI thread no less. | 226 // UI thread no less. |
| 230 // | 227 // |
| 231 // This will be addressed in a future CL. | 228 // This will be addressed in a future CL. |
| 232 | 229 |
| 233 uint32_t type_id; | |
| 234 PersistentMemoryAllocator::Reference ref; | 230 PersistentMemoryAllocator::Reference ref; |
| 235 while ((ref = allocator_->GetNextIterable(&sample_iter_, &type_id)) != 0) { | 231 while ((ref = sample_iter_.GetNextOfType(kTypeIdSampleRecord)) != 0) { |
| 236 if (type_id == kTypeIdSampleRecord) { | 232 SampleRecord* record = |
| 237 SampleRecord* record = | 233 allocator_->GetAsObject<SampleRecord>(ref, kTypeIdSampleRecord); |
| 238 allocator_->GetAsObject<SampleRecord>(ref, kTypeIdSampleRecord); | 234 if (!record) |
| 239 if (!record) | 235 continue; |
| 240 continue; | |
| 241 | 236 |
| 242 // A sample record has been found but may not be for this histogram. | 237 // A sample record has been found but may not be for this histogram. |
| 243 if (record->id != id()) | 238 if (record->id != id()) |
| 244 continue; | 239 continue; |
| 245 | 240 |
| 246 // Check if the record's value is already known. | 241 // Check if the record's value is already known. |
| 247 if (!ContainsKey(sample_counts_, record->value)) { | 242 if (!ContainsKey(sample_counts_, record->value)) { |
| 248 // No: Add it to map of known values if the value is valid. | 243 // No: Add it to map of known values if the value is valid. |
| 249 if (record->value >= 0) | 244 if (record->value >= 0) |
| 250 sample_counts_[record->value] = &record->count; | 245 sample_counts_[record->value] = &record->count; |
| 251 } else { | 246 } else { |
| 252 // Yes: Ignore it; it's a duplicate caused by a race condition -- see | 247 // Yes: Ignore it; it's a duplicate caused by a race condition -- see |
| 253 // code & comment in GetOrCreateSampleCountStorage() for details. | 248 // code & comment in GetOrCreateSampleCountStorage() for details. |
| 254 // Check that nothing ever operated on the duplicate record. | 249 // Check that nothing ever operated on the duplicate record. |
| 255 DCHECK_EQ(0, record->count); | 250 DCHECK_EQ(0, record->count); |
| 256 } | 251 } |
| 257 | 252 |
| 258 // Stop if it's the value being searched for. | 253 // Stop if it's the value being searched for. |
| 259 if (record->value == until_value) | 254 if (record->value == until_value) |
| 260 return &record->count; | 255 return &record->count; |
| 261 } | |
| 262 } | 256 } |
| 263 | 257 |
| 264 return nullptr; | 258 return nullptr; |
| 265 } | 259 } |
| 266 | 260 |
| 267 } // namespace base | 261 } // namespace base |
| OLD | NEW |