Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 // Copyright (c) 2015 The Chromium Authors. All rights reserved. | |
| 2 // Use of this source code is governed by a BSD-style license that can be | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #include "base/metrics/histogram_persistence.h" | |
| 6 | |
| 7 #include "base/logging.h" | |
| 8 #include "base/memory/scoped_ptr.h" | |
| 9 #include "base/metrics/histogram.h" | |
| 10 #include "base/metrics/histogram_base.h" | |
| 11 #include "base/metrics/histogram_samples.h" | |
| 12 #include "base/metrics/statistics_recorder.h" | |
| 13 #include "base/synchronization/lock.h" | |
| 14 | |
| 15 namespace base { | |
| 16 | |
| 17 namespace { | |
| 18 | |
| 19 // Type identifiers used when storing in persistent memory so they can be | |
| 20 // identified during extraction; the first 4 bytes of the SHA1 of the name | |
| 21 // is used as a unique integer. A "version number" is added to the base | |
| 22 // so that, if the structure of that object changes, stored older versions | |
| 23 // will be safely ignored. | |
| 24 enum : uint32_t { | |
| 25 kTypeIdHistogram = 0xF1645910 + 1, // SHA1(Histogram) v1 | |
| 26 kTypeIdRangesArray = 0xBCEA225A + 1, // SHA1(RangesArray) v1 | |
| 27 kTypeIdCountsArray = 0x53215530 + 1, // SHA1(CountsArray) v1 | |
| 28 }; | |
| 29 | |
| 30 // This data must be held in persistent memory in order for processes to | |
| 31 // locate and use histograms created elsewhere. | |
| 32 struct PersistentHistogramData { | |
| 33 int histogram_type; | |
| 34 int flags; | |
| 35 int minimum; | |
| 36 int maximum; | |
| 37 size_t bucket_count; | |
| 38 PersistentMemoryAllocator::Reference ranges_ref; | |
| 39 uint32_t ranges_checksum; | |
| 40 PersistentMemoryAllocator::Reference counts_ref; | |
| 41 HistogramSamples::Metadata samples_metadata; | |
| 42 | |
| 43 // Space for the histogram name will be added during the actual allocation | |
| 44 // request. This must be the last field of the structure. A zero-size array | |
| 45 // or a "flexible" array would be preferred but is not (yet) valid C++. | |
| 46 char name[1]; | |
| 47 }; | |
| 48 | |
| 49 // The object held here will obviously not be destructed at process exit | |
| 50 // but that's okay since PersistentMemoryAllocator objects are explicitly | |
| 51 // forbidden from doing anything essential at exit anyway due to the fact | |
| 52 // that they depend on data managed elsewhere and which could be destructed | |
| 53 // first. | |
| 54 PersistentMemoryAllocator* g_allocator = nullptr; | |
| 55 | |
| 56 } // namespace | |
| 57 | |
| 58 const Feature kPersistentHistogramsFeature{ | |
| 59 "PersistentHistograms", FEATURE_DISABLED_BY_DEFAULT | |
| 60 }; | |
| 61 | |
| 62 void SetPersistentHistogramMemoryAllocator( | |
| 63 PersistentMemoryAllocator* allocator) { | |
| 64 // Releasing or changing an allocator is extremely dangerous because it | |
| 65 // likely has histograms stored within it. If the backing memory is also | |
| 66 // also released, future accesses to those histograms will seg-fault. | |
| 67 // It's not a fatal CHECK() because tests do this knowing that all | |
| 68 // such persistent histograms have already been forgotten. | |
| 69 if (g_allocator) { | |
| 70 LOG(WARNING) << "Active PersistentMemoryAllocator has been released." | |
| 71 << " Some existing histogram pointers may be invalid."; | |
| 72 delete g_allocator; | |
| 73 } | |
| 74 g_allocator = allocator; | |
| 75 } | |
| 76 | |
| 77 PersistentMemoryAllocator* GetPersistentHistogramMemoryAllocator() { | |
| 78 return g_allocator; | |
| 79 } | |
| 80 | |
| 81 PersistentMemoryAllocator* ReleasePersistentHistogramMemoryAllocator() { | |
| 82 PersistentMemoryAllocator* allocator = g_allocator; | |
| 83 g_allocator = nullptr; | |
| 84 return allocator; | |
| 85 }; | |
| 86 | |
| 87 HistogramBase* CreatePersistentHistogram( | |
| 88 PersistentMemoryAllocator* allocator, | |
| 89 PersistentHistogramData* histogram_data_ptr) { | |
| 90 if (!histogram_data_ptr) { | |
| 91 LOG(WARNING) << "Persistent histogram data was not valid; skipped."; | |
| 92 NOTREACHED(); | |
| 93 return nullptr; | |
| 94 } | |
| 95 | |
| 96 // Copy the histogram_data to local storage because anything in persistent | |
| 97 // memory cannot be trusted as it could be changed at any moment by a | |
| 98 // malicious actor that shares access. The contents of histogram_data are | |
| 99 // validated below; the local copy is to ensure that the contents cannot | |
| 100 // be externally changed between validation and use. | |
| 101 PersistentHistogramData histogram_data = *histogram_data_ptr; | |
| 102 std::string name(histogram_data_ptr->name); | |
|
Alexei Svitkine (slow)
2016/01/18 19:25:35
Nit: move this closer to where it's used - i.e. ab
bcwhite
2016/01/22 15:24:53
Done.
| |
| 103 | |
| 104 HistogramBase::Sample* ranges_data = | |
| 105 allocator->GetAsObject<HistogramBase::Sample>(histogram_data.ranges_ref, | |
| 106 kTypeIdRangesArray); | |
| 107 if (!ranges_data || histogram_data.bucket_count < 2 || | |
| 108 histogram_data.bucket_count + 1 > | |
| 109 std::numeric_limits<size_t>::max() / sizeof(HistogramBase::Sample) || | |
| 110 allocator->GetAllocSize(histogram_data.ranges_ref) < | |
| 111 (histogram_data.bucket_count + 1) * sizeof(HistogramBase::Sample)) { | |
| 112 LOG(WARNING) << "Persistent histogram referenced invalid ranges array" | |
| 113 << "; skipped."; | |
| 114 NOTREACHED(); | |
| 115 return nullptr; | |
| 116 } | |
| 117 // To avoid racy destruction at shutdown, the following will be leaked. | |
| 118 BucketRanges* ranges = new BucketRanges(histogram_data.bucket_count + 1); | |
| 119 bool bad_ranges = false; | |
| 120 for (size_t i = 0; i < ranges->size(); ++i) { | |
| 121 if (i > 0 && ranges_data[i] <= ranges_data[i - 1]) | |
| 122 bad_ranges = true; | |
| 123 ranges->set_range(i, ranges_data[i]); | |
| 124 } | |
| 125 ranges->ResetChecksum(); | |
| 126 if (bad_ranges || ranges->checksum() != histogram_data.ranges_checksum) { | |
|
Alexei Svitkine (slow)
2016/01/18 19:25:35
Nit: Can you make a helper CreateRanges() in the a
bcwhite
2016/01/22 15:24:53
Done.
| |
| 127 LOG(WARNING) << "Persistent histogram referenced invalid ranges array" | |
| 128 << "; skipped."; | |
| 129 NOTREACHED(); | |
| 130 return nullptr; | |
| 131 } | |
| 132 const BucketRanges* registered_ranges = | |
| 133 StatisticsRecorder::RegisterOrDeleteDuplicateRanges(ranges); | |
| 134 | |
| 135 HistogramBase::AtomicCount* counts_data = | |
| 136 allocator->GetAsObject<HistogramBase::AtomicCount>( | |
| 137 histogram_data.counts_ref, kTypeIdCountsArray); | |
| 138 if (!counts_data || | |
| 139 allocator->GetAllocSize(histogram_data.counts_ref) < | |
| 140 histogram_data.bucket_count * sizeof(HistogramBase::AtomicCount)) { | |
| 141 LOG(WARNING) << "Persistent histogram referenced invalid counts array" | |
| 142 << "; skipped."; | |
| 143 NOTREACHED(); | |
| 144 return nullptr; | |
| 145 } | |
| 146 | |
| 147 HistogramBase* histogram = nullptr; | |
| 148 switch (histogram_data.histogram_type) { | |
| 149 case HISTOGRAM: | |
| 150 histogram = Histogram::PersistentGet( | |
| 151 name, | |
| 152 histogram_data.minimum, | |
| 153 histogram_data.maximum, | |
| 154 registered_ranges, | |
| 155 counts_data, | |
| 156 histogram_data.bucket_count, | |
| 157 &histogram_data_ptr->samples_metadata); | |
| 158 break; | |
| 159 case LINEAR_HISTOGRAM: | |
| 160 histogram = LinearHistogram::PersistentGet( | |
| 161 name, | |
| 162 histogram_data.minimum, | |
| 163 histogram_data.maximum, | |
| 164 registered_ranges, | |
| 165 counts_data, | |
| 166 histogram_data.bucket_count, | |
| 167 &histogram_data_ptr->samples_metadata); | |
| 168 break; | |
| 169 case BOOLEAN_HISTOGRAM: | |
| 170 histogram = BooleanHistogram::PersistentGet( | |
| 171 name, | |
| 172 registered_ranges, | |
| 173 counts_data, | |
| 174 &histogram_data_ptr->samples_metadata); | |
| 175 break; | |
| 176 case CUSTOM_HISTOGRAM: | |
| 177 histogram = CustomHistogram::PersistentGet( | |
| 178 name, | |
| 179 registered_ranges, | |
| 180 counts_data, | |
| 181 histogram_data.bucket_count, | |
| 182 &histogram_data_ptr->samples_metadata); | |
| 183 break; | |
| 184 } | |
| 185 | |
| 186 if (histogram) { | |
| 187 DCHECK_EQ(histogram_data.histogram_type, histogram->GetHistogramType()); | |
| 188 histogram->SetFlags(histogram_data.flags); | |
| 189 } | |
| 190 | |
| 191 return histogram; | |
| 192 } | |
| 193 | |
| 194 HistogramBase* GetPersistentHistogram( | |
| 195 PersistentMemoryAllocator* allocator, | |
| 196 int32_t ref) { | |
| 197 // Unfortunately, the above "pickle" methods cannot be used as part of the | |
| 198 // persistance because the deserialization methods always create local | |
| 199 // count data (these must referenced the persistent counts) and always add | |
| 200 // it to the local list of known histograms (these may be simple references | |
| 201 // to histograms in other processes). | |
| 202 PersistentHistogramData* histogram_data = | |
| 203 allocator->GetAsObject<PersistentHistogramData>(ref, kTypeIdHistogram); | |
| 204 size_t length = allocator->GetAllocSize(ref); | |
| 205 if (!histogram_data || | |
| 206 reinterpret_cast<char*>(histogram_data)[length - 1] != '\0') { | |
| 207 LOG(WARNING) << "Persistent histogram data was invalid or had invalid name" | |
| 208 << "; skipped."; | |
| 209 NOTREACHED(); | |
| 210 return nullptr; | |
| 211 } | |
| 212 return CreatePersistentHistogram(allocator, histogram_data); | |
| 213 } | |
| 214 | |
| 215 HistogramBase* GetNextPersistentHistogram( | |
| 216 PersistentMemoryAllocator* allocator, | |
| 217 PersistentMemoryAllocator::Iterator* iter) { | |
| 218 PersistentMemoryAllocator::Reference ref; | |
| 219 uint32_t type_id; | |
| 220 while ((ref = allocator->GetNextIterable(iter, &type_id)) != 0) { | |
| 221 if (type_id == kTypeIdHistogram) | |
| 222 return GetPersistentHistogram(allocator, ref); | |
| 223 } | |
| 224 return nullptr; | |
| 225 } | |
| 226 | |
| 227 void FinalizePersistentHistogram(PersistentMemoryAllocator::Reference ref, | |
| 228 bool registered) { | |
| 229 // If the created persistent histogram was registered then it needs to | |
| 230 // be marked as "iterable" in order to be found by other processes. | |
| 231 if (registered) | |
| 232 GetPersistentHistogramMemoryAllocator()->MakeIterable(ref); | |
| 233 // If it wasn't registered then a race condition must have caused | |
| 234 // two to be created. The allocator does not support releasing the | |
| 235 // acquired memory so just change the type to be empty. | |
| 236 else | |
| 237 GetPersistentHistogramMemoryAllocator()->SetType(ref, 0); | |
| 238 } | |
| 239 | |
| 240 HistogramBase* AllocatePersistentHistogram( | |
| 241 PersistentMemoryAllocator* allocator, | |
| 242 HistogramType histogram_type, | |
| 243 const std::string& name, | |
| 244 int minimum, | |
| 245 int maximum, | |
| 246 const BucketRanges* bucket_ranges, | |
| 247 int32_t flags, | |
| 248 PersistentMemoryAllocator::Reference* ref_ptr) { | |
| 249 if (allocator) { | |
|
Alexei Svitkine (slow)
2016/01/18 19:25:35
Do an early return instead, since in this case, th
bcwhite
2016/01/22 15:24:53
Done.
| |
| 250 size_t bucket_count = bucket_ranges->bucket_count(); | |
| 251 CHECK(bucket_count <= std::numeric_limits<int32_t>::max() / | |
|
Alexei Svitkine (slow)
2016/01/18 19:25:35
DCHECK_LE?
Or if there's a reason it should be a
bcwhite
2016/01/22 15:24:53
Done.
| |
| 252 sizeof(HistogramBase::AtomicCount)); | |
| 253 size_t counts_memory = bucket_count * sizeof(HistogramBase::AtomicCount); | |
|
Alexei Svitkine (slow)
2016/01/18 19:25:35
Nit: counts_size
ranges_size
bcwhite
2016/01/22 15:24:53
Did _bytes instead.
| |
| 254 size_t ranges_memory = (bucket_count + 1) * sizeof(HistogramBase::Sample); | |
| 255 PersistentMemoryAllocator::Reference ranges_ref = | |
| 256 allocator->Allocate(ranges_memory, kTypeIdRangesArray); | |
| 257 PersistentMemoryAllocator::Reference counts_ref = | |
| 258 allocator->Allocate(counts_memory, kTypeIdCountsArray); | |
| 259 PersistentMemoryAllocator::Reference histogram_ref = | |
| 260 allocator->Allocate(offsetof(PersistentHistogramData, name) + | |
|
Alexei Svitkine (slow)
2016/01/18 19:25:35
Can you add a static_assert somewhere that checks
bcwhite
2016/01/22 15:24:53
Not accurately because the compiler will add paddi
| |
| 261 name.length() + 1, kTypeIdHistogram); | |
| 262 HistogramBase::Sample* ranges_data = | |
| 263 allocator->GetAsObject<HistogramBase::Sample>(ranges_ref, | |
| 264 kTypeIdRangesArray); | |
| 265 PersistentHistogramData* histogram_data = | |
| 266 allocator->GetAsObject<PersistentHistogramData>(histogram_ref, | |
| 267 kTypeIdHistogram); | |
| 268 | |
| 269 // Only continue here if all allocations were successful. If they weren't | |
| 270 // there is no way to free the space but that's not really a problem since | |
| 271 // the allocations only fail because the space is full and so any future | |
| 272 // attempts will also fail. | |
| 273 if (counts_ref && ranges_data && histogram_data) { | |
| 274 strcpy(histogram_data->name, name.c_str()); | |
| 275 for (size_t i = 0; i < bucket_ranges->size(); ++i) | |
| 276 ranges_data[i] = bucket_ranges->range(i); | |
| 277 | |
| 278 histogram_data->histogram_type = histogram_type; | |
| 279 histogram_data->flags = flags; | |
| 280 histogram_data->minimum = minimum; | |
| 281 histogram_data->maximum = maximum; | |
| 282 histogram_data->bucket_count = bucket_count; | |
| 283 histogram_data->ranges_ref = ranges_ref; | |
| 284 histogram_data->ranges_checksum = bucket_ranges->checksum(); | |
| 285 histogram_data->counts_ref = counts_ref; | |
| 286 | |
| 287 // Create the histogram using resources in persistent memory. This ends up | |
| 288 // resolving the "ref" values stored in histogram_data instad of just | |
| 289 // using what is already known above but avoids duplicating the switch | |
| 290 // statement here and serves as a double-check that everything is | |
| 291 // correct before commiting the new histogram to persistent space. | |
| 292 HistogramBase* histogram = | |
| 293 CreatePersistentHistogram(allocator, histogram_data); | |
| 294 DCHECK(histogram); | |
| 295 if (ref_ptr != nullptr) | |
| 296 *ref_ptr = histogram_ref; | |
| 297 return histogram; | |
| 298 } | |
| 299 | |
| 300 LOG(WARNING) << "Could not create histogram \"" << name | |
| 301 << "\" in persistent memory (full=" << allocator->IsFull() | |
| 302 << ", corrupt=" << allocator->IsCorrupt() << ")"; | |
| 303 } | |
| 304 | |
| 305 return nullptr; | |
| 306 } | |
| 307 | |
| 308 void ImportPersistentHistograms() { | |
| 309 // Each call resumes from where it last left off so need persistant iterator. | |
| 310 // The lock protects against concurrent access to the iterator and is created | |
| 311 // dynamically so as to not require destruction during program exit. | |
| 312 static PersistentMemoryAllocator::Iterator iter; | |
| 313 static base::Lock* lock = new base::Lock(); | |
| 314 | |
| 315 if (g_allocator) { | |
| 316 base::AutoLock auto_lock(*lock); | |
| 317 if (iter.is_clear()) | |
| 318 g_allocator->CreateIterator(&iter); | |
| 319 | |
| 320 for (;;) { | |
| 321 HistogramBase* h = GetNextPersistentHistogram(g_allocator, &iter); | |
|
Alexei Svitkine (slow)
2016/01/18 19:25:35
Nit: histogram
bcwhite
2016/01/22 15:24:53
Done.
| |
| 322 if (!h) | |
| 323 break; | |
| 324 StatisticsRecorder::RegisterOrDeleteDuplicate(h); | |
| 325 } | |
| 326 } | |
| 327 } | |
| 328 | |
| 329 } // namespace base | |
| OLD | NEW |