| OLD | NEW |
| 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/sparse_histogram.h" | 5 #include "base/metrics/sparse_histogram.h" |
| 6 | 6 |
| 7 #include "base/metrics/statistics_recorder.h" | 7 #include "base/metrics/statistics_recorder.h" |
| 8 #include "base/synchronization/lock.h" |
| 8 | 9 |
| 10 using std::map; |
| 9 using std::string; | 11 using std::string; |
| 10 | 12 |
| 11 namespace base { | 13 namespace base { |
| 12 | 14 |
| 15 typedef HistogramBase::Count Count; |
| 16 typedef HistogramBase::Sample Sample; |
| 17 |
| 13 // static | 18 // static |
| 14 HistogramBase* SparseHistogram::FactoryGet(const string& name, | 19 HistogramBase* SparseHistogram::FactoryGet(const string& name, int32 flags) { |
| 15 int32 flags) { | |
| 16 // TODO(kaiwang): Register and get SparseHistogram with StatisticsRecorder. | 20 // TODO(kaiwang): Register and get SparseHistogram with StatisticsRecorder. |
| 17 HistogramBase* histogram = new SparseHistogram(name); | 21 HistogramBase* histogram = new SparseHistogram(name); |
| 18 histogram->SetFlags(flags); | 22 histogram->SetFlags(flags); |
| 19 return histogram; | 23 return histogram; |
| 20 } | 24 } |
| 21 | 25 |
| 22 SparseHistogram::~SparseHistogram() {} | 26 SparseHistogram::~SparseHistogram() {} |
| 23 | 27 |
| 24 void SparseHistogram::Add(Sample value) { | 28 void SparseHistogram::Add(Sample value) { |
| 25 base::AutoLock auto_lock(lock_); | 29 base::AutoLock auto_lock(lock_); |
| 26 samples_[value]++; | 30 sample_counts_[value]++; |
| 31 redundant_count_ += 1; |
| 27 } | 32 } |
| 28 | 33 |
| 29 void SparseHistogram::SnapshotSample(std::map<Sample, Count>* samples) const { | 34 scoped_ptr<SampleMap> SparseHistogram::SnapshotSamples() const { |
| 35 scoped_ptr<SampleMap> snapshot(new SampleMap()); |
| 36 |
| 30 base::AutoLock auto_lock(lock_); | 37 base::AutoLock auto_lock(lock_); |
| 31 *samples = samples_; | 38 for(map<Sample, Count>::const_iterator it = sample_counts_.begin(); |
| 39 it != sample_counts_.end(); |
| 40 ++it) { |
| 41 snapshot->Accumulate(it->first, it->second); |
| 42 } |
| 43 snapshot->ResetRedundantCount(redundant_count_); |
| 44 return snapshot.Pass(); |
| 32 } | 45 } |
| 33 | 46 |
| 34 void SparseHistogram::WriteHTMLGraph(string* output) const { | 47 void SparseHistogram::WriteHTMLGraph(string* output) const { |
| 35 // TODO(kaiwang): Implement. | 48 // TODO(kaiwang): Implement. |
| 36 } | 49 } |
| 37 | 50 |
| 38 void SparseHistogram::WriteAscii(string* output) const { | 51 void SparseHistogram::WriteAscii(string* output) const { |
| 39 // TODO(kaiwang): Implement. | 52 // TODO(kaiwang): Implement. |
| 40 } | 53 } |
| 41 | 54 |
| 42 SparseHistogram::SparseHistogram(const string& name) | 55 SparseHistogram::SparseHistogram(const string& name) |
| 43 : HistogramBase(name) {} | 56 : HistogramBase(name), |
| 57 redundant_count_(0) {} |
| 44 | 58 |
| 45 } // namespace base | 59 } // namespace base |
| OLD | NEW |