Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(299)

Side by Side Diff: base/metrics/histogram_persistence.cc

Issue 1425533011: Support "shared" histograms between processes. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@shmem-alloc
Patch Set: addressed review comments by Alexei Created 4 years, 11 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(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
103 HistogramBase::Sample* ranges_data =
104 allocator->GetAsObject<HistogramBase::Sample>(histogram_data.ranges_ref,
105 kTypeIdRangesArray);
106 if (!ranges_data || histogram_data.bucket_count < 2 ||
107 histogram_data.bucket_count + 1 >
108 std::numeric_limits<size_t>::max() / sizeof(HistogramBase::Sample) ||
109 allocator->GetAllocSize(histogram_data.ranges_ref) <
110 (histogram_data.bucket_count + 1) * sizeof(HistogramBase::Sample)) {
111 LOG(WARNING) << "Persistent histogram referenced invalid ranges array"
112 << "; skipped.";
113 NOTREACHED();
114 return nullptr;
115 }
116 // To avoid racy destruction at shutdown, the following will be leaked.
117 BucketRanges* ranges = new BucketRanges(histogram_data.bucket_count + 1);
118 bool bad_ranges = false;
119 for (size_t i = 0; i < ranges->size(); ++i) {
120 if (i > 0 && ranges_data[i] <= ranges_data[i - 1])
121 bad_ranges = true;
122 ranges->set_range(i, ranges_data[i]);
123 }
124 ranges->ResetChecksum();
125 if (bad_ranges || ranges->checksum() != histogram_data.ranges_checksum) {
126 LOG(WARNING) << "Persistent histogram referenced invalid ranges array"
127 << "; skipped.";
128 NOTREACHED();
129 return nullptr;
130 }
131 const BucketRanges* registered_ranges =
132 StatisticsRecorder::RegisterOrDeleteDuplicateRanges(ranges);
133
134 HistogramBase::AtomicCount* counts_data =
135 allocator->GetAsObject<HistogramBase::AtomicCount>(
136 histogram_data.counts_ref, kTypeIdCountsArray);
137 if (!counts_data ||
138 allocator->GetAllocSize(histogram_data.counts_ref) <
139 histogram_data.bucket_count * sizeof(HistogramBase::AtomicCount)) {
140 LOG(WARNING) << "Persistent histogram referenced invalid counts array"
141 << "; skipped.";
142 NOTREACHED();
143 return nullptr;
144 }
145
146 HistogramBase* histogram = nullptr;
147 switch (histogram_data.histogram_type) {
148 case HISTOGRAM:
149 histogram = Histogram::PersistentGet(
150 histogram_data_ptr->name,
151 histogram_data.minimum,
152 histogram_data.maximum,
153 registered_ranges,
154 counts_data,
155 histogram_data.bucket_count,
156 &histogram_data_ptr->samples_metadata);
157 break;
158 case LINEAR_HISTOGRAM:
159 histogram = LinearHistogram::PersistentGet(
160 histogram_data_ptr->name,
161 histogram_data.minimum,
162 histogram_data.maximum,
163 registered_ranges,
164 counts_data,
165 histogram_data.bucket_count,
166 &histogram_data_ptr->samples_metadata);
167 break;
168 case BOOLEAN_HISTOGRAM:
169 histogram = BooleanHistogram::PersistentGet(
170 histogram_data_ptr->name,
171 registered_ranges,
172 counts_data,
173 &histogram_data_ptr->samples_metadata);
174 break;
175 case CUSTOM_HISTOGRAM:
176 histogram = CustomHistogram::PersistentGet(
177 histogram_data_ptr->name,
178 registered_ranges,
179 counts_data,
180 histogram_data.bucket_count,
181 &histogram_data_ptr->samples_metadata);
182 break;
183 }
184
185 if (histogram) {
186 DCHECK_EQ(histogram_data.histogram_type, histogram->GetHistogramType());
187 histogram->SetFlags(histogram_data.flags);
188 }
189
190 return histogram;
191 }
192
193 HistogramBase* GetPersistentHistogram(
194 PersistentMemoryAllocator* allocator,
195 int32_t ref) {
196 // Unfortunately, the above "pickle" methods cannot be used as part of the
197 // persistance because the deserialization methods always create local
198 // count data (these must referenced the persistent counts) and always add
199 // it to the local list of known histograms (these may be simple references
200 // to histograms in other processes).
201 PersistentHistogramData* histogram_data =
202 allocator->GetAsObject<PersistentHistogramData>(ref, kTypeIdHistogram);
203 size_t length = allocator->GetAllocSize(ref);
204 if (!histogram_data ||
205 reinterpret_cast<char*>(histogram_data)[length - 1] != '\0') {
206 LOG(WARNING) << "Persistent histogram data was invalid or had invalid name"
207 << "; skipped.";
208 NOTREACHED();
209 return nullptr;
210 }
211 return CreatePersistentHistogram(allocator, histogram_data);
212 }
213
214 HistogramBase* GetNextPersistentHistogram(
215 PersistentMemoryAllocator* allocator,
216 PersistentMemoryAllocator::Iterator* iter) {
217 PersistentMemoryAllocator::Reference ref;
218 uint32_t type_id;
219 while ((ref = allocator->GetNextIterable(iter, &type_id)) != 0) {
220 if (type_id == kTypeIdHistogram)
221 return GetPersistentHistogram(allocator, ref);
222 }
223 return nullptr;
224 }
225
226 void FinalizePersistentHistogram(PersistentMemoryAllocator::Reference ref,
227 bool registered) {
228 // If the created persistent histogram was registered then it needs to
229 // be marked as "iterable" in order to be found by other processes.
230 if (registered)
231 GetPersistentHistogramMemoryAllocator()->MakeIterable(ref);
232 // If it wasn't registered then a race condition must have caused
233 // two to be created. The allocator does not support releasing the
234 // acquired memory so just change the type to be empty.
235 else
236 GetPersistentHistogramMemoryAllocator()->SetType(ref, 0);
237 }
238
239 HistogramBase* AllocatePersistentHistogram(
240 PersistentMemoryAllocator* allocator,
241 HistogramType histogram_type,
242 const std::string& name,
243 int minimum,
244 int maximum,
245 const BucketRanges* bucket_ranges,
246 int32_t flags,
247 PersistentMemoryAllocator::Reference* ref_ptr) {
248 if (allocator) {
249 size_t bucket_count = bucket_ranges->bucket_count();
250 CHECK(bucket_count <= std::numeric_limits<int32_t>::max() /
251 sizeof(HistogramBase::AtomicCount));
252 size_t counts_memory = bucket_count * sizeof(HistogramBase::AtomicCount);
253 size_t ranges_memory = (bucket_count + 1) * sizeof(HistogramBase::Sample);
254 PersistentMemoryAllocator::Reference ranges_ref =
255 allocator->Allocate(ranges_memory, kTypeIdRangesArray);
256 PersistentMemoryAllocator::Reference counts_ref =
257 allocator->Allocate(counts_memory, kTypeIdCountsArray);
258 PersistentMemoryAllocator::Reference histogram_ref =
259 allocator->Allocate(offsetof(PersistentHistogramData, name) +
260 name.length() + 1, kTypeIdHistogram);
261 HistogramBase::Sample* ranges_data =
262 allocator->GetAsObject<HistogramBase::Sample>(ranges_ref,
263 kTypeIdRangesArray);
264 PersistentHistogramData* histogram_data =
265 allocator->GetAsObject<PersistentHistogramData>(histogram_ref,
266 kTypeIdHistogram);
267
268 // Only continue here if all allocations were successful. If they weren't
269 // there is no way to free the space but that's not really a problem since
270 // the allocations only fail because the space is full and so any future
271 // attempts will also fail.
272 if (counts_ref && ranges_data && histogram_data) {
273 strcpy(histogram_data->name, name.c_str());
274 for (size_t i = 0; i < bucket_ranges->size(); ++i)
275 ranges_data[i] = bucket_ranges->range(i);
276
277 histogram_data->histogram_type = histogram_type;
278 histogram_data->flags = flags;
279 histogram_data->minimum = minimum;
280 histogram_data->maximum = maximum;
281 histogram_data->bucket_count = bucket_count;
282 histogram_data->ranges_ref = ranges_ref;
283 histogram_data->ranges_checksum = bucket_ranges->checksum();
284 histogram_data->counts_ref = counts_ref;
285
286 // Create the histogram using resources in persistent memory. This ends up
287 // resolving the "ref" values stored in histogram_data instad of just
288 // using what is already known above but avoids duplicating the switch
289 // statement here and serves as a double-check that everything is
290 // correct before commiting the new histogram to persistent space.
291 HistogramBase* histogram =
292 CreatePersistentHistogram(allocator, histogram_data);
293 DCHECK(histogram);
294 if (ref_ptr != nullptr)
295 *ref_ptr = histogram_ref;
296 return histogram;
297 }
298
299 LOG(WARNING) << "Could not create histogram \"" << name
300 << "\" in persistent memory (full=" << allocator->IsFull()
301 << ", corrupt=" << allocator->IsCorrupt() << ")";
302 }
303
304 return nullptr;
305 }
306
307 void ImportPersistentHistograms() {
308 // Each call resumes from where it last left off so need persistant iterator.
309 // The lock protects against concurrent access to the iterator.
310 static PersistentMemoryAllocator::Iterator iter;
311 static base::Lock lock;
312
313 if (g_allocator) {
314 base::AutoLock auto_lock(lock);
315 if (iter.is_clear())
316 g_allocator->CreateIterator(&iter);
317
318 for (;;) {
319 HistogramBase* h = GetNextPersistentHistogram(g_allocator, &iter);
320 if (!h)
321 break;
322 StatisticsRecorder::RegisterOrDeleteDuplicate(h);
323 }
324 }
325 }
326
327 } // namespace base
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698