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