| 6 |
| 7 | #include "base/logging.h" |
| 8 | #include "base/metrics/histogram_macros.h" |
| 9 | #include "base/metrics/persistent_histogram_allocator.h" |
| 10 | #include "base/numerics/safe_conversions.h" |
| 11 | #include "base/stl_util.h" |
| 12 |
| 13 | namespace base { |
| 14 |
| 15 | typedef HistogramBase::Count Count; |
| 16 | typedef HistogramBase::Sample Sample; |
| 17 |
| 18 | namespace { |
| 19 |
| 20 | // An iterator for going through a PersistentSampleMap. The logic here is |
| 21 | // identical to that of SampleMapIterator but with different data structures. |
| 22 | // Changes here likely need to be duplicated there. |
| 23 | class PersistentSampleMapIterator : public SampleCountIterator { |
| 24 | public: |
| 25 | typedef std::map<HistogramBase::Sample, HistogramBase::Count*> |
| 26 | SampleToCountMap; |
| 27 |
| 28 | explicit PersistentSampleMapIterator(const SampleToCountMap& sample_counts); |
| 29 | ~PersistentSampleMapIterator() override; |
| 30 |
| 31 | // SampleCountIterator: |
| 32 | bool Done() const override; |
| 33 | void Next() override; |
| 34 | void Get(HistogramBase::Sample* min, |
| 35 | int64_t* max, |
| 36 | HistogramBase::Count* count) const override; |
| 37 |
| 38 | private: |
| 39 | void SkipEmptyBuckets(); |
| 40 |
| 41 | SampleToCountMap::const_iterator iter_; |
| 42 | const SampleToCountMap::const_iterator end_; |
| 43 | }; |
| 44 |
| 45 | PersistentSampleMapIterator::PersistentSampleMapIterator( |
| 46 | const SampleToCountMap& sample_counts) |
| 47 | : iter_(sample_counts.begin()), |
| 48 | end_(sample_counts.end()) { |
| 49 | SkipEmptyBuckets(); |
| 50 | } |
| 51 |
| 52 | PersistentSampleMapIterator::~PersistentSampleMapIterator() = default; |
| 53 |
| 54 | bool PersistentSampleMapIterator::Done() const { |
| 55 | return iter_ == end_; |
| 56 | } |
| 57 |
| 58 | void PersistentSampleMapIterator::Next() { |
| 59 | DCHECK(!Done()); |
| 60 | ++iter_; |
| 61 | SkipEmptyBuckets(); |
| 62 | } |
| 63 |
| 64 | void PersistentSampleMapIterator::Get(Sample* min, |
| 65 | int64_t* max, |
| 66 | Count* count) const { |
| 67 | DCHECK(!Done()); |
| 68 | if (min) |
| 69 | *min = iter_->first; |
| 70 | if (max) |
| 71 | *max = strict_cast<int64_t>(iter_->first) + 1; |
| 72 | if (count) |
| 73 | *count = *iter_->second; |
| 74 | } |
| 75 |
| 76 | void PersistentSampleMapIterator::SkipEmptyBuckets() { |
| 77 | while (!Done() && *iter_->second == 0) { |
| 78 | ++iter_; |
| 79 | } |
| 80 | } |
| 81 |
| 82 | // This structure holds an entry for a PersistentSampleMap within a persistent |
| 83 | // memory allocator. The "id" must be unique across all maps held by an |
| 84 | // allocator or they will get attached to the wrong sample map. |
| 85 | struct SampleRecord { |
| 86 | // SHA1(SampleRecord): Increment this if structure changes! |
| 87 | static constexpr uint32_t kPersistentTypeId = 0x8FE6A69F + 1; |
| 88 |
| 89 | // Expected size for 32/64-bit check. |
| 90 | static constexpr size_t kExpectedInstanceSize = 16; |
| 91 |
| 92 | uint64_t id; // Unique identifier of owner. |
| 93 | Sample value; // The value for which this record holds a count. |
| 94 | Count count; // The count associated with the above value. |
| 95 | }; |
| 96 |
| 97 | } // namespace |
| 98 |
| 99 | PersistentSampleMap::PersistentSampleMap( |
| 100 | uint64_t id, |
| 101 | PersistentHistogramAllocator* allocator, |
| 102 | Metadata* meta) |
| 103 | : HistogramSamples(id, meta), allocator_(allocator) {} |
| 104 |
| 105 | PersistentSampleMap::~PersistentSampleMap() { |
| 106 | if (records_) |
| 107 | records_->Release(this); |
| 108 | } |
| 109 |
| 110 | void PersistentSampleMap::Accumulate(Sample value, Count count) { |
| 111 | #if 0 // TODO(bcwhite) Re-enable efficient version after crbug.com/682680. |
| 112 | *GetOrCreateSampleCountStorage(value) += count; |
| 113 | #else |
| 114 | Count* local_count_ptr = GetOrCreateSampleCountStorage(value); |
| 115 | if (count < 0) { |
| 116 | if (*local_count_ptr < -count) |
| 117 | RecordNegativeSample(SAMPLES_ACCUMULATE_WENT_NEGATIVE, -count); |
| 118 | else |
| 119 | RecordNegativeSample(SAMPLES_ACCUMULATE_NEGATIVE_COUNT, -count); |
| 120 | *local_count_ptr += count; |
| 121 | } else { |
| 122 | Sample old_value = *local_count_ptr; |
| 123 | Sample new_value = old_value + count; |
| 124 | *local_count_ptr = new_value; |
| 125 | if ((new_value >= 0) != (old_value >= 0)) |
| 126 | RecordNegativeSample(SAMPLES_ACCUMULATE_OVERFLOW, count); |
| 127 | } |
| 128 | #endif |
| 129 | IncreaseSumAndCount(strict_cast<int64_t>(count) * value, count); |
| 130 | } |
| 131 |
| 132 | Count PersistentSampleMap::GetCount(Sample value) const { |
| 133 | // Have to override "const" to make sure all samples have been loaded before |
| 134 | // being able to know what value to return. |
| 135 | Count* count_pointer = |
| 136 | const_cast<PersistentSampleMap*>(this)->GetSampleCountStorage(value); |
| 137 | return count_pointer ? *count_pointer : 0; |
| 138 | } |
| 139 |
| 140 | Count PersistentSampleMap::TotalCount() const { |
| 141 | // Have to override "const" in order to make sure all samples have been |
| 142 | // loaded before trying to iterate over the map. |
| 143 | const_cast<PersistentSampleMap*>(this)->ImportSamples(-1, true); |
| 144 |
| 145 | Count count = 0; |
| 146 | for (const auto& entry : sample_counts_) { |
| 147 | count += *entry.second; |
| 148 | } |
| 149 | return count; |
| 150 | } |
| 151 |
| 152 | std::unique_ptr<SampleCountIterator> PersistentSampleMap::Iterator() const { |
| 153 | // Have to override "const" in order to make sure all samples have been |
| 154 | // loaded before trying to iterate over the map. |
| 155 | const_cast<PersistentSampleMap*>(this)->ImportSamples(-1, true); |
| 156 | return std::make_unique<PersistentSampleMapIterator>(sample_counts_); |
| 157 | } |
| 158 |
| 159 | // static |
| 160 | PersistentMemoryAllocator::Reference |
| 161 | PersistentSampleMap::GetNextPersistentRecord( |
| 162 | PersistentMemoryAllocator::Iterator& iterator, |
| 163 | uint64_t* sample_map_id) { |
| 164 | const SampleRecord* record = iterator.GetNextOfObject<SampleRecord>(); |
| 165 | if (!record) |
| 166 | return 0; |
| 167 |
| 168 | *sample_map_id = record->id; |
| 169 | return iterator.GetAsReference(record); |
| 170 | } |
| 171 |
| 172 | // static |
| 173 | PersistentMemoryAllocator::Reference |
| 174 | PersistentSampleMap::CreatePersistentRecord( |
| 175 | PersistentMemoryAllocator* allocator, |
| 176 | uint64_t sample_map_id, |
| 177 | Sample value) { |
| 178 | SampleRecord* record = allocator->New<SampleRecord>(); |
| 179 | if (!record) { |
| 180 | NOTREACHED() << "full=" << allocator->IsFull() |
| 181 | << ", corrupt=" << allocator->IsCorrupt(); |
| 182 | return 0; |
| 183 | } |
| 184 |
| 185 | record->id = sample_map_id; |
| 186 | record->value = value; |
| 187 | record->count = 0; |
| 188 |
| 189 | PersistentMemoryAllocator::Reference ref = allocator->GetAsReference(record); |
| 190 | allocator->MakeIterable(ref); |
| 191 | return ref; |
| 192 | } |
| 193 |
| 194 | bool PersistentSampleMap::AddSubtractImpl(SampleCountIterator* iter, |
| 195 | Operator op) { |
| 196 | Sample min; |
| 197 | int64_t max; |
| 198 | Count count; |
| 199 | for (; !iter->Done(); iter->Next()) { |
| 200 | iter->Get(&min, &max, &count); |
| 201 | if (count == 0) |
| 202 | continue; |
| 203 | if (strict_cast<int64_t>(min) + 1 != max) |
| 204 | return false; // SparseHistogram only supports bucket with size 1. |
| 205 | *GetOrCreateSampleCountStorage(min) += |
| 206 | (op == HistogramSamples::ADD) ? count : -count; |
| 207 | } |
| 208 | return true; |
| 209 | } |
| 210 |
| 211 | Count* PersistentSampleMap::GetSampleCountStorage(Sample value) { |
| 212 | // If |value| is already in the map, just return that. |
| 213 | auto it = sample_counts_.find(value); |
| 214 | if (it != sample_counts_.end()) |
| 215 | return it->second; |
| 216 |
| 217 | // Import any new samples from persistent memory looking for the value. |
| 218 | return ImportSamples(value, false); |
| 219 | } |
| 220 |
| 221 | Count* PersistentSampleMap::GetOrCreateSampleCountStorage(Sample value) { |
| 222 | // Get any existing count storage. |
| 223 | Count* count_pointer = GetSampleCountStorage(value); |
| 224 | if (count_pointer) |
| 225 | return count_pointer; |
| 226 |
| 227 | // Create a new record in persistent memory for the value. |records_| will |
| 228 | // have been initialized by the GetSampleCountStorage() call above. |
| 229 | DCHECK(records_); |
| 230 | PersistentMemoryAllocator::Reference ref = records_->CreateNew(value); |
| 231 | if (!ref) { |
| 232 | // If a new record could not be created then the underlying allocator is |
| 233 | // full or corrupt. Instead, allocate the counter from the heap. This |
| 234 | // sample will not be persistent, will not be shared, and will leak... |
| 235 | // but it's better than crashing. |
| 236 | count_pointer = new Count(0); |
| 237 | sample_counts_[value] = count_pointer; |
| 238 | return count_pointer; |
| 239 | } |
| 240 |
| 241 | // A race condition between two independent processes (i.e. two independent |
| 242 | // histogram objects sharing the same sample data) could cause two of the |
| 243 | // above records to be created. The allocator, however, forces a strict |
| 244 | // ordering on iterable objects so use the import method to actually add the |
| 245 | // just-created record. This ensures that all PersistentSampleMap objects |
| 246 | // will always use the same record, whichever was first made iterable. |
| 247 | // Thread-safety within a process where multiple threads use the same |
| 248 | // histogram object is delegated to the controlling histogram object which, |
| 249 | // for sparse histograms, is a lock object. |
| 250 | count_pointer = ImportSamples(value, false); |
| 251 | DCHECK(count_pointer); |
| 252 | return count_pointer; |
| 253 | } |
| 254 |
| 255 | PersistentSampleMapRecords* PersistentSampleMap::GetRecords() { |
| 256 | // The |records_| pointer is lazily fetched from the |allocator_| only on |
| 257 | // first use. Sometimes duplicate histograms are created by race conditions |
| 258 | // and if both were to grab the records object, there would be a conflict. |
| 259 | // Use of a histogram, and thus a call to this method, won't occur until |
| 260 | // after the histogram has been de-dup'd. |
| 261 | if (!records_) |
| 262 | records_ = allocator_->UseSampleMapRecords(id(), this); |
| 263 | return records_; |
| 264 | } |
| 265 |
| 266 | Count* PersistentSampleMap::ImportSamples(Sample until_value, |
| 267 | bool import_everything) { |
| 268 | Count* found_count = nullptr; |
| 269 | PersistentMemoryAllocator::Reference ref; |
| 270 | PersistentSampleMapRecords* records = GetRecords(); |
| 271 | while ((ref = records->GetNext()) != 0) { |
| 272 | SampleRecord* record = records->GetAsObject<SampleRecord>(ref); |
| 273 | if (!record) |
| 274 | continue; |
| 275 |
| 276 | DCHECK_EQ(id(), record->id); |
| 277 |
| 278 | // Check if the record's value is already known. |
| 279 | if (!Contains(sample_counts_, record->value)) { |
| 280 | // No: Add it to map of known values. |
| 281 | sample_counts_[record->value] = &record->count; |
| 282 | } else { |
| 283 | // Yes: Ignore it; it's a duplicate caused by a race condition -- see |
| 284 | // code & comment in GetOrCreateSampleCountStorage() for details. |
| 285 | // Check that nothing ever operated on the duplicate record. |
| 286 | DCHECK_EQ(0, record->count); |
| 287 | } |
| 288 |
| 289 | // Check if it's the value being searched for and, if so, keep a pointer |
| 290 | // to return later. Stop here unless everything is being imported. |
| 291 | // Because race conditions can cause multiple records for a single value, |
| 292 | // be sure to return the first one found. |
| 293 | if (record->value == until_value) { |
| 294 | if (!found_count) |
| 295 | found_count = &record->count; |
| 296 | if (!import_everything) |
| 297 | break; |
| 298 | } |
| 299 | } |
| 300 |
| 301 | return found_count; |
| 302 | } |
| 303 |
| 304 | } // namespace base |
| 305 |