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

Unified 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: some 'git cl format' changes and other cosmetic improvements Created 4 years, 10 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 side-by-side diff with in-line comments
Download patch
Index: base/metrics/sample_map.cc
diff --git a/base/metrics/sample_map.cc b/base/metrics/sample_map.cc
index e276b91643b6a4fa567b2e4f84eb09b94a92a38c..9a285a151155a7d7145517075701912f0e489550 100644
--- a/base/metrics/sample_map.cc
+++ b/base/metrics/sample_map.cc
@@ -91,4 +91,233 @@ void SampleMapIterator::SkipEmptyBuckets() {
}
}
+
+// ----- PersistentSampleMap ---------------------------------------------------
+
+namespace {
+
+// This structure holds an entry for a PersistentSampleMap within a persistent
+// memory allocator. The "id" must be unique across all maps held by an
+// allocator or they will get attached to the wrong sample map.
+struct SampleRecord {
+ uint64_t id; // Unique identifier of owner.
+ Sample value; // The value for which this record holds a count.
+ Count count; // The count associated with the above value.
+};
+
+// The type-id used to identify sample records inside an allocator.
+const uint32_t kTypeIdSampleRecord = 0x8FE6A69F + 1; // SHA1(SampleRecord) v1
+
+} // namespace
+
+PersistentSampleMap::PersistentSampleMap(
+ uint64_t id,
+ PersistentMemoryAllocator* allocator,
+ Metadata* meta)
+ : HistogramSamples(id, meta),
+ allocator_(allocator) {
+ // This is created once but will continue to return new iterables even when
+ // it has previously reached the end.
+ allocator->CreateIterator(&sample_iter_);
+
+ // Load all existing samples during construction. It's no worse to do it
+ // here than at some point in the future and could be better if construction
+ // takes place on some background thread. New samples could be created at
+ // any time by parallel threads; if so, they'll get loaded when needed.
+ ImportSamples(-1);
+}
+
+PersistentSampleMap::~PersistentSampleMap() {}
+
+void PersistentSampleMap::Accumulate(Sample value, Count count) {
+ *GetSampleCountPointer(value, /*create=*/true) += count;
+ IncreaseSum(static_cast<int64_t>(count) * value);
+ IncreaseRedundantCount(count);
+}
+
+Count PersistentSampleMap::GetCount(Sample value) const {
+ // Have to override "const" to make sure all samples have been loaded before
+ // being able to know what value to return.
+ Count* count_pointer =
+ const_cast<PersistentSampleMap*>(this)->GetSampleCountPointer(
+ value, /*create=*/false);
+ if (!count_pointer)
grt (UTC plus 2) 2016/03/02 20:00:42 nit: return count_pointer ? *count_pointer : 0;
bcwhite 2016/03/02 22:32:43 Done.
+ return 0;
+ return *count_pointer;
+}
+
+Count PersistentSampleMap::TotalCount() const {
+ // Have to override "const" in order to make sure all samples have been
+ // loaded before trying to iterate over the map.
+ const_cast<PersistentSampleMap*>(this)->ImportSamples(-1);
+
+ Count count = 0;
+ for (const auto& entry : sample_counts_) {
grt (UTC plus 2) 2016/03/02 20:00:42 nit: omit braces
bcwhite 2016/03/02 22:32:43 Done. Above, too.
+ count += *entry.second;
+ }
+ return count;
+}
+
+scoped_ptr<SampleCountIterator> PersistentSampleMap::Iterator() const {
+ // Have to override "const" in order to make sure all samples have been
+ // loaded before trying to iterate over the map.
+ const_cast<PersistentSampleMap*>(this)->ImportSamples(-1);
+
+ return scoped_ptr<SampleCountIterator>(
grt (UTC plus 2) 2016/03/02 20:00:42 does this work: return make_scoped_ptr(new Persi
bcwhite 2016/03/02 22:32:43 Done.
+ new PersistentSampleMapIterator(sample_counts_));
+}
+
+bool PersistentSampleMap::AddSubtractImpl(SampleCountIterator* iter,
+ HistogramSamples::Operator op) {
grt (UTC plus 2) 2016/03/02 20:00:42 HistogramSamples:: not needed
bcwhite 2016/03/02 22:32:43 Done.
+ Sample min;
+ Sample max;
+ Count count;
+ for (; !iter->Done(); iter->Next()) {
+ iter->Get(&min, &max, &count);
+ if (min + 1 != max)
+ return false; // SparseHistogram only supports bucket with size 1.
+
+ *GetSampleCountPointer(min, /*create=*/true) +=
+ (op == HistogramSamples::ADD) ? count : -count;
grt (UTC plus 2) 2016/03/02 20:00:42 HistogramSamples:: not needed. unless ADD is too o
bcwhite 2016/03/02 22:32:43 Done.
+ }
+ return true;
+}
+
+Count* PersistentSampleMap::GetSampleCountPointer(HistogramBase::Sample value,
grt (UTC plus 2) 2016/03/02 20:00:42 omit HistogramBase:: thanks to typedef at top of f
bcwhite 2016/03/02 22:32:43 Done.
+ bool create_if_necessary) {
+ DCHECK_LE(0, value);
+
+ // If |value| is already in the map, just return that.
+ auto it = sample_counts_.find(value);
+ if (it != sample_counts_.end())
+ return it->second;
+
+ // Import any new samples from persistent memory looking for the value.
+ Count* count_pointer = ImportSamples(value);
+ if (count_pointer)
+ return count_pointer;
+
+ // Stop here if no creation of new samples is desired.
+ if (!create_if_necessary)
+ return nullptr;
+
+ // Create a new record in persistent memory for the value.
+ PersistentMemoryAllocator::Reference ref =
+ allocator_->Allocate(sizeof(SampleRecord), kTypeIdSampleRecord);
+ SampleRecord* record =
+ allocator_->GetAsObject<SampleRecord>(ref, kTypeIdSampleRecord);
+ if (!record) {
+ // If the allocator was unable to create a record then it is full or
+ // corrupt. Instead, allocate the counter from the heap. This sample will
+ // not be persistent, will not be shared, and will leak but it's better
+ // than crashing.
+ NOTREACHED() << "full=" << allocator_->IsFull()
+ << ", corrupt=" << allocator_->IsCorrupt();
+ count_pointer = new Count(0);
+ sample_counts_[value] = count_pointer;
+ return count_pointer;
+ }
+ record->id = id();
+ record->value = value;
+ // record->count = 0 because allocator guarantees zero'd memory.
+ allocator_->MakeIterable(ref);
+
+ // A race condition could cause two of the above records to be created. The
+ // allocator, however, forces a strict ordering on iterable objects so use
+ // the import method to actually add the just-created record. This ensures
+ // that all PersistentSampleMap objects will always use the same record,
+ // whichever was first made iterable.
+ count_pointer = ImportSamples(value);
+ DCHECK(count_pointer);
+ return count_pointer;
+}
+
+Count* PersistentSampleMap::ImportSamples(HistogramBase::Sample until_value) {
+ // TODO(bcwhite): This import operates in O(V+N) total time per sparse
+ // histogram where V is the number of values for this object and N is
+ // the number of other iterable objects in the allocator. This becomes
+ // O(S*(SV+N)) or O(S^2*V + SN) overall where S is the number of sparse
+ // histograms.
+ //
+ // This is actually okay when histograms are expected to exist for the
+ // lifetime of the program, spreading the cost out, and S and V are
+ // relatively small, as is the current case.
+ //
+ // However, it is not so good for objects that are created, detroyed, and
+ // recreated on a periodic basis, such as when making a snapshot of
+ // sparse histograms owned by another, ongoing process. In that case, the
+ // entire cost is compressed into a single sequential operation... on the
+ // UI thread no less.
+ //
+ // This will be addressed in a future CL.
+
+ uint32_t type_id;
+ PersistentMemoryAllocator::Reference ref;
+ while ((ref = allocator_->GetNextIterable(&sample_iter_, &type_id)) != 0) {
+ if (type_id == kTypeIdSampleRecord) {
+ SampleRecord* record =
+ allocator_->GetAsObject<SampleRecord>(ref, kTypeIdSampleRecord);
+ if (!record)
+ continue;
+
+ // A sample record has been found but may not be for this histogram.
+ if (record->id != id())
+ continue;
+
+ // Check if the record's value is already known.
+ if (sample_counts_.find(record->value) == sample_counts_.end()) {
grt (UTC plus 2) 2016/03/02 20:00:42 #include "base/stl_util.h" if (!ContainsKey(sample
bcwhite 2016/03/02 22:32:43 Done.
+ // No: Add it to map of known values if the value is valid.
+ if (record->value >= 0)
+ sample_counts_[record->value] = &record->count;
+ } else {
+ // Yes: Ignore it; it's a duplicate caused by a race condition.
+ DCHECK_EQ(0, record->count); // Duplicate record should never be used.
+ }
+
+ // Stop if it's the value being searched for.
+ if (record->value == until_value)
+ return &record->count;
+ }
+ }
+
+ return nullptr;
+}
+
+PersistentSampleMapIterator::PersistentSampleMapIterator(
+ const SampleToCountMap& sample_counts)
+ : iter_(sample_counts.begin()),
+ end_(sample_counts.end()) {
+ SkipEmptyBuckets();
+}
+
+PersistentSampleMapIterator::~PersistentSampleMapIterator() {}
+
+bool PersistentSampleMapIterator::Done() const {
+ return iter_ == end_;
+}
+
+void PersistentSampleMapIterator::Next() {
+ DCHECK(!Done());
+ ++iter_;
+ SkipEmptyBuckets();
+}
+
+void PersistentSampleMapIterator::Get(Sample* min,
+ Sample* max,
+ Count* count) const {
+ DCHECK(!Done());
+ if (min != NULL)
grt (UTC plus 2) 2016/03/02 20:00:42 if (min) ...
bcwhite 2016/03/02 22:32:43 Done.
+ *min = iter_->first;
+ if (max != NULL)
+ *max = iter_->first + 1;
+ if (count != NULL)
+ *count = *iter_->second;
+}
+
+void PersistentSampleMapIterator::SkipEmptyBuckets() {
+ while (!Done() && *iter_->second == 0) {
grt (UTC plus 2) 2016/03/02 20:00:42 nit: omit braces
bcwhite 2016/03/02 22:32:43 Done.
+ ++iter_;
+ }
+}
+
} // namespace base

Powered by Google App Engine
This is Rietveld 408576698