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