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

Unified Diff: base/metrics/sample_map.cc

Issue 11022002: Add SampleMap and use it in SparseHistogram (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Address comments Created 8 years, 3 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
new file mode 100644
index 0000000000000000000000000000000000000000..a301acd8fdfb65838fb05039b6a28f86638aa4de
--- /dev/null
+++ b/base/metrics/sample_map.cc
@@ -0,0 +1,95 @@
+// Copyright (c) 2012 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#include "base/metrics/sample_map.h"
+
+#include "base/logging.h"
+
+using std::map;
+
+namespace base {
+
+typedef HistogramBase::Count Count;
jar (doing other things) 2012/10/01 23:10:38 nit: would using work here?
kaiwang 2012/10/02 02:57:56 nope... using only works for namespace
+typedef HistogramBase::Sample Sample;
+
+SampleMap::SampleMap() {}
+
+SampleMap::~SampleMap() {}
+
+void SampleMap::Accumulate(Sample value, Count count) {
+ base::AutoLock auto_lock(lock_);
+ sample_counts_[value] += count;
+ IncreaseSum(count * value);
+ IncreaseRedundantCount(count);
+}
+
+Count SampleMap::GetCount(Sample value) const {
+ base::AutoLock auto_lock(lock_);
+ map<Sample, Count>::const_iterator it = sample_counts_.find(value);
+ if (it == sample_counts_.end())
+ return 0;
+ return it->second;
+}
+
+Count SampleMap::TotalCount() const {
+ Count count = 0;
+
+ base::AutoLock auto_lock(lock_);
+ for (map<Sample, Count>::const_iterator it = sample_counts_.begin();
+ it != sample_counts_.end();
+ ++it) {
+ count += it->second;
+ }
+ return count;
+}
+
+scoped_ptr<SampleCountIterator> SampleMap::Iterator() const {
+ return scoped_ptr<SampleCountIterator>(new SampleMapIterator(sample_counts_));
+}
+
+bool SampleMap::AddSubtractImpl(SampleCountIterator* iter,
+ HistogramSamples::Instruction instruction) {
+ HistogramBase::Sample min;
+ HistogramBase::Sample max;
+ HistogramBase::Count count;
+
+ base::AutoLock auto_lock(lock_);
+ for (; !iter->Done(); iter->Next()) {
+ iter->Get(&min, &max, &count);
+ if (min + 1 != max)
+ return false; // SparseHistogram only supports bucket with size 1.
+ sample_counts_[min] +=
+ (instruction == HistogramSamples::ADD) ? count : -count;
+ }
+ return true;
+}
+
+SampleMapIterator::SampleMapIterator(const map<Sample, Count>& sample_counts)
+ : iter_(sample_counts.begin()),
+ end_(sample_counts.end()) {}
+
+SampleMapIterator::~SampleMapIterator() {}
+
+bool SampleMapIterator::Done() const {
+ return iter_ == end_;
+}
+
+void SampleMapIterator::Next() {
+ DCHECK(!Done());
+ iter_++;
+}
+
+void SampleMapIterator::Get(HistogramBase::Sample* min,
+ HistogramBase::Sample* max,
+ HistogramBase::Count* count) const {
+ DCHECK(!Done());
jar (doing other things) 2012/10/01 23:10:38 I'm surprised we're not holding a lock when we acc
kaiwang 2012/10/02 02:57:56 As discussed locked in SnapshotSamples function
+ if (min != NULL)
+ *min = iter_->first;
+ if (max != NULL)
+ *max = iter_->first + 1;
+ if (count != NULL)
+ *count = iter_->second;
+}
+
+} // namespace base

Powered by Google App Engine
This is Rietveld 408576698