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

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

Issue 1734033003: Add support for persistent sparse histograms. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: addressed review comments by Greg Created 4 years, 9 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
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2012 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/sample_map.h" 5 #include "base/metrics/sample_map.h"
6 6
7 #include "base/logging.h" 7 #include "base/logging.h"
8 #include "base/stl_util.h"
8 9
9 namespace base { 10 namespace base {
10 11
11 typedef HistogramBase::Count Count; 12 typedef HistogramBase::Count Count;
12 typedef HistogramBase::Sample Sample; 13 typedef HistogramBase::Sample Sample;
13 14
14 SampleMap::SampleMap() : SampleMap(0) {} 15 SampleMap::SampleMap() : SampleMap(0) {}
15 16
16 SampleMap::SampleMap(uint64_t id) : HistogramSamples(id) {} 17 SampleMap::SampleMap(uint64_t id) : HistogramSamples(id) {}
17 18
18 SampleMap::~SampleMap() {} 19 SampleMap::~SampleMap() {}
19 20
20 void SampleMap::Accumulate(Sample value, Count count) { 21 void SampleMap::Accumulate(Sample value, Count count) {
21 sample_counts_[value] += count; 22 sample_counts_[value] += count;
22 IncreaseSum(static_cast<int64_t>(count) * value); 23 IncreaseSum(static_cast<int64_t>(count) * value);
23 IncreaseRedundantCount(count); 24 IncreaseRedundantCount(count);
24 } 25 }
25 26
26 Count SampleMap::GetCount(Sample value) const { 27 Count SampleMap::GetCount(Sample value) const {
27 std::map<Sample, Count>::const_iterator it = sample_counts_.find(value); 28 std::map<Sample, Count>::const_iterator it = sample_counts_.find(value);
28 if (it == sample_counts_.end()) 29 if (it == sample_counts_.end())
29 return 0; 30 return 0;
30 return it->second; 31 return it->second;
31 } 32 }
32 33
33 Count SampleMap::TotalCount() const { 34 Count SampleMap::TotalCount() const {
34 Count count = 0; 35 Count count = 0;
35 for (const auto& entry : sample_counts_) { 36 for (const auto& entry : sample_counts_)
36 count += entry.second; 37 count += entry.second;
37 }
38 return count; 38 return count;
39 } 39 }
40 40
41 scoped_ptr<SampleCountIterator> SampleMap::Iterator() const { 41 scoped_ptr<SampleCountIterator> SampleMap::Iterator() const {
42 return scoped_ptr<SampleCountIterator>(new SampleMapIterator(sample_counts_)); 42 return make_scoped_ptr(new SampleMapIterator(sample_counts_));
43 } 43 }
44 44
45 bool SampleMap::AddSubtractImpl(SampleCountIterator* iter, 45 bool SampleMap::AddSubtractImpl(SampleCountIterator* iter, Operator op) {
46 HistogramSamples::Operator op) {
47 Sample min; 46 Sample min;
48 Sample max; 47 Sample max;
49 Count count; 48 Count count;
50 for (; !iter->Done(); iter->Next()) { 49 for (; !iter->Done(); iter->Next()) {
51 iter->Get(&min, &max, &count); 50 iter->Get(&min, &max, &count);
52 if (min + 1 != max) 51 if (min + 1 != max)
53 return false; // SparseHistogram only supports bucket with size 1. 52 return false; // SparseHistogram only supports bucket with size 1.
54 53
55 sample_counts_[min] += (op == HistogramSamples::ADD) ? count : -count; 54 sample_counts_[min] += (op == HistogramSamples::ADD) ? count : -count;
56 } 55 }
(...skipping 13 matching lines...) Expand all
70 } 69 }
71 70
72 void SampleMapIterator::Next() { 71 void SampleMapIterator::Next() {
73 DCHECK(!Done()); 72 DCHECK(!Done());
74 ++iter_; 73 ++iter_;
75 SkipEmptyBuckets(); 74 SkipEmptyBuckets();
76 } 75 }
77 76
78 void SampleMapIterator::Get(Sample* min, Sample* max, Count* count) const { 77 void SampleMapIterator::Get(Sample* min, Sample* max, Count* count) const {
79 DCHECK(!Done()); 78 DCHECK(!Done());
80 if (min != NULL) 79 if (min)
81 *min = iter_->first; 80 *min = iter_->first;
82 if (max != NULL) 81 if (max)
83 *max = iter_->first + 1; 82 *max = iter_->first + 1;
84 if (count != NULL) 83 if (count)
85 *count = iter_->second; 84 *count = iter_->second;
86 } 85 }
87 86
88 void SampleMapIterator::SkipEmptyBuckets() { 87 void SampleMapIterator::SkipEmptyBuckets() {
89 while (!Done() && iter_->second == 0) { 88 while (!Done() && iter_->second == 0)
90 ++iter_; 89 ++iter_;
90 }
91
92
93 // ----- PersistentSampleMap ---------------------------------------------------
94
95 namespace {
96
97 // This structure holds an entry for a PersistentSampleMap within a persistent
98 // memory allocator. The "id" must be unique across all maps held by an
99 // allocator or they will get attached to the wrong sample map.
100 struct SampleRecord {
101 uint64_t id; // Unique identifier of owner.
102 Sample value; // The value for which this record holds a count.
103 Count count; // The count associated with the above value.
104 };
105
106 // The type-id used to identify sample records inside an allocator.
107 const uint32_t kTypeIdSampleRecord = 0x8FE6A69F + 1; // SHA1(SampleRecord) v1
108
109 } // namespace
110
111 PersistentSampleMap::PersistentSampleMap(
112 uint64_t id,
113 PersistentMemoryAllocator* allocator,
114 Metadata* meta)
115 : HistogramSamples(id, meta),
116 allocator_(allocator) {
117 // This is created once but will continue to return new iterables even when
118 // it has previously reached the end.
119 allocator->CreateIterator(&sample_iter_);
120
121 // Load all existing samples during construction. It's no worse to do it
122 // here than at some point in the future and could be better if construction
123 // takes place on some background thread. New samples could be created at
124 // any time by parallel threads; if so, they'll get loaded when needed.
125 ImportSamples(kAllSamples);
126 }
127
128 PersistentSampleMap::~PersistentSampleMap() {}
129
130 void PersistentSampleMap::Accumulate(Sample value, Count count) {
131 *GetSampleCountStorage(value, /*create=*/true) += count;
132 IncreaseSum(static_cast<int64_t>(count) * value);
133 IncreaseRedundantCount(count);
134 }
135
136 Count PersistentSampleMap::GetCount(Sample value) const {
137 // Have to override "const" to make sure all samples have been loaded before
138 // being able to know what value to return.
139 Count* count_pointer =
140 const_cast<PersistentSampleMap*>(this)->GetSampleCountStorage(
141 value, /*create=*/false);
142 return count_pointer ? *count_pointer : 0;
143 }
144
145 Count PersistentSampleMap::TotalCount() const {
146 // Have to override "const" in order to make sure all samples have been
147 // loaded before trying to iterate over the map.
148 const_cast<PersistentSampleMap*>(this)->ImportSamples(kAllSamples);
149
150 Count count = 0;
151 for (const auto& entry : sample_counts_)
152 count += *entry.second;
153 return count;
154 }
155
156 scoped_ptr<SampleCountIterator> PersistentSampleMap::Iterator() const {
157 // Have to override "const" in order to make sure all samples have been
158 // loaded before trying to iterate over the map.
159 const_cast<PersistentSampleMap*>(this)->ImportSamples(kAllSamples);
160 return make_scoped_ptr(new PersistentSampleMapIterator(sample_counts_));
161 }
162
163 bool PersistentSampleMap::AddSubtractImpl(SampleCountIterator* iter,
164 Operator op) {
165 Sample min;
166 Sample max;
167 Count count;
168 for (; !iter->Done(); iter->Next()) {
169 iter->Get(&min, &max, &count);
170 if (min + 1 != max)
171 return false; // SparseHistogram only supports bucket with size 1.
172
173 *GetSampleCountStorage(min, /*create=*/true) +=
174 (op == HistogramSamples::ADD) ? count : -count;
91 } 175 }
176 return true;
177 }
178
179 Count* PersistentSampleMap::GetSampleCountStorage(Sample value,
180 bool create_if_necessary) {
181 DCHECK_LE(0, value);
182
183 // If |value| is already in the map, just return that.
184 auto it = sample_counts_.find(value);
185 if (it != sample_counts_.end())
186 return it->second;
187
188 // Import any new samples from persistent memory looking for the value.
189 Count* count_pointer = ImportSamples(value);
190 if (count_pointer)
191 return count_pointer;
192
193 // Stop here if no creation of new samples is desired.
Alexei Svitkine (slow) 2016/03/03 17:43:38 I prefer these to be separate methods. You can hav
bcwhite 2016/03/07 15:30:39 Done.
194 if (!create_if_necessary)
195 return nullptr;
196
197 // Create a new record in persistent memory for the value.
198 PersistentMemoryAllocator::Reference ref =
199 allocator_->Allocate(sizeof(SampleRecord), kTypeIdSampleRecord);
200 SampleRecord* record =
201 allocator_->GetAsObject<SampleRecord>(ref, kTypeIdSampleRecord);
202 if (!record) {
203 // If the allocator was unable to create a record then it is full or
204 // corrupt. Instead, allocate the counter from the heap. This sample will
205 // not be persistent, will not be shared, and will leak but it's better
206 // than crashing.
207 NOTREACHED() << "full=" << allocator_->IsFull()
208 << ", corrupt=" << allocator_->IsCorrupt();
209 count_pointer = new Count(0);
210 sample_counts_[value] = count_pointer;
211 return count_pointer;
212 }
213 record->id = id();
214 record->value = value;
215 // record->count = 0 because allocator guarantees zero'd memory.
216 allocator_->MakeIterable(ref);
217
218 // A race condition could cause two of the above records to be created. The
219 // allocator, however, forces a strict ordering on iterable objects so use
220 // the import method to actually add the just-created record. This ensures
221 // that all PersistentSampleMap objects will always use the same record,
222 // whichever was first made iterable.
223 count_pointer = ImportSamples(value);
224 DCHECK(count_pointer);
225 return count_pointer;
226 }
227
228 Count* PersistentSampleMap::ImportSamples(Sample until_value) {
229 // TODO(bcwhite): This import operates in O(V+N) total time per sparse
230 // histogram where V is the number of values for this object and N is
231 // the number of other iterable objects in the allocator. This becomes
232 // O(S*(SV+N)) or O(S^2*V + SN) overall where S is the number of sparse
233 // histograms.
234 //
235 // This is actually okay when histograms are expected to exist for the
236 // lifetime of the program, spreading the cost out, and S and V are
237 // relatively small, as is the current case.
238 //
239 // However, it is not so good for objects that are created, detroyed, and
240 // recreated on a periodic basis, such as when making a snapshot of
241 // sparse histograms owned by another, ongoing process. In that case, the
242 // entire cost is compressed into a single sequential operation... on the
243 // UI thread no less.
244 //
245 // This will be addressed in a future CL.
246
247 uint32_t type_id;
248 PersistentMemoryAllocator::Reference ref;
249 while ((ref = allocator_->GetNextIterable(&sample_iter_, &type_id)) != 0) {
250 if (type_id == kTypeIdSampleRecord) {
251 SampleRecord* record =
252 allocator_->GetAsObject<SampleRecord>(ref, kTypeIdSampleRecord);
253 if (!record)
254 continue;
255
256 // A sample record has been found but may not be for this histogram.
257 if (record->id != id())
258 continue;
259
260 // Check if the record's value is already known.
261 if (!ContainsKey(sample_counts_, record->value)) {
262 // No: Add it to map of known values if the value is valid.
263 if (record->value >= 0)
264 sample_counts_[record->value] = &record->count;
265 } else {
266 // Yes: Ignore it; it's a duplicate caused by a race condition.
267 DCHECK_EQ(0, record->count); // Duplicate record should never be used.
268 }
269
270 // Stop if it's the value being searched for.
271 if (record->value == until_value)
272 return &record->count;
273 }
274 }
275
276 return nullptr;
277 }
278
279 PersistentSampleMapIterator::PersistentSampleMapIterator(
280 const SampleToCountMap& sample_counts)
281 : iter_(sample_counts.begin()),
282 end_(sample_counts.end()) {
283 SkipEmptyBuckets();
284 }
285
286 PersistentSampleMapIterator::~PersistentSampleMapIterator() {}
287
288 bool PersistentSampleMapIterator::Done() const {
289 return iter_ == end_;
290 }
291
292 void PersistentSampleMapIterator::Next() {
293 DCHECK(!Done());
294 ++iter_;
295 SkipEmptyBuckets();
296 }
297
298 void PersistentSampleMapIterator::Get(Sample* min,
299 Sample* max,
300 Count* count) const {
301 DCHECK(!Done());
302 if (min)
303 *min = iter_->first;
304 if (max)
305 *max = iter_->first + 1;
306 if (count)
307 *count = *iter_->second;
308 }
309
310 void PersistentSampleMapIterator::SkipEmptyBuckets() {
311 while (!Done() && *iter_->second == 0)
312 ++iter_;
92 } 313 }
93 314
94 } // namespace base 315 } // namespace base
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698