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

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: Add destructors to avoid compiler warning on clang 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..9da8d23b0c140ab128921c3112c4eafe397113cf
--- /dev/null
+++ b/base/metrics/sample_map.cc
@@ -0,0 +1,94 @@
+// 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;
+typedef HistogramBase::Sample Sample;
+
+SampleMap::SampleMap() {}
+
+SampleMap::~SampleMap() {}
+
+void SampleMap::Accumulate(Sample value, Count count) {
+ base::AutoLock auto_lock(lock_);
+ sample_count_[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_count_.find(value);
+ if (it == sample_count_.end())
+ return 0;
+ return it->second;
+}
+
+Count SampleMap::TotalCount() const {
+ Count count = 0;
+ map<Sample, Count>::const_iterator it;
Ilya Sherman 2012/10/01 21:24:50 nit: Please define this within the for loop's vari
kaiwang 2012/10/01 22:38:52 Done.
+
+ base::AutoLock auto_lock(lock_);
+ for (it = sample_count_.begin(); it != sample_count_.end(); ++it) {
+ count += it->second;
+ }
+ return count;
+}
+
+scoped_ptr<SampleCountIterator> SampleMap::Iterator() const {
+ return scoped_ptr<SampleCountIterator>(new SampleMapIterator(sample_count_));
+}
+
+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.
Ilya Sherman 2012/10/01 21:24:50 nit: Should this be a DCHECK?
kaiwang 2012/10/01 22:38:52 As in SampleVector, this function can be called in
+ sample_count_[min] +=
+ (instruction == HistogramSamples::ADD) ? count : -count;
Ilya Sherman 2012/10/01 21:24:50 nit: I don't think you need the "HistogramSamples:
kaiwang 2012/10/01 22:38:52 Right, but I think this is a bit easier for code r
+ }
+ return true;
+}
+
+SampleMapIterator::SampleMapIterator(const map<Sample, Count>& sample_count)
+ : iter_(sample_count.begin()),
+ end_(sample_count.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());
+ 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