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/sample_map.h" | 7 #include "base/metrics/sample_map.h" |
8 #include "base/metrics/statistics_recorder.h" | 8 #include "base/metrics/statistics_recorder.h" |
| 9 #include "base/pickle.h" |
9 #include "base/synchronization/lock.h" | 10 #include "base/synchronization/lock.h" |
10 | 11 |
11 using std::map; | 12 using std::map; |
12 using std::string; | 13 using std::string; |
13 | 14 |
14 namespace base { | 15 namespace base { |
15 | 16 |
16 typedef HistogramBase::Count Count; | 17 typedef HistogramBase::Count Count; |
17 typedef HistogramBase::Sample Sample; | 18 typedef HistogramBase::Sample Sample; |
18 | 19 |
(...skipping 13 matching lines...) Expand all Loading... |
32 | 33 |
33 bool SparseHistogram::HasConstructionArguments(Sample minimum, | 34 bool SparseHistogram::HasConstructionArguments(Sample minimum, |
34 Sample maximum, | 35 Sample maximum, |
35 size_t bucket_count) const { | 36 size_t bucket_count) const { |
36 // SparseHistogram never has min/max/bucket_count limit. | 37 // SparseHistogram never has min/max/bucket_count limit. |
37 return false; | 38 return false; |
38 } | 39 } |
39 | 40 |
40 void SparseHistogram::Add(Sample value) { | 41 void SparseHistogram::Add(Sample value) { |
41 base::AutoLock auto_lock(lock_); | 42 base::AutoLock auto_lock(lock_); |
42 sample_counts_[value]++; | 43 samples_.Accumulate(value, 1); |
43 redundant_count_ += 1; | |
44 } | 44 } |
45 | 45 |
46 scoped_ptr<HistogramSamples> SparseHistogram::SnapshotSamples() const { | 46 scoped_ptr<HistogramSamples> SparseHistogram::SnapshotSamples() const { |
47 scoped_ptr<SampleMap> snapshot(new SampleMap()); | 47 scoped_ptr<SampleMap> snapshot(new SampleMap()); |
48 | 48 |
49 base::AutoLock auto_lock(lock_); | 49 base::AutoLock auto_lock(lock_); |
50 for(map<Sample, Count>::const_iterator it = sample_counts_.begin(); | 50 snapshot->Add(samples_); |
51 it != sample_counts_.end(); | |
52 ++it) { | |
53 snapshot->Accumulate(it->first, it->second); | |
54 } | |
55 snapshot->ResetRedundantCount(redundant_count_); | |
56 return snapshot.PassAs<HistogramSamples>(); | 51 return snapshot.PassAs<HistogramSamples>(); |
57 } | 52 } |
58 | 53 |
| 54 void SparseHistogram::AddSamples(const HistogramSamples& samples) { |
| 55 base::AutoLock auto_lock(lock_); |
| 56 samples_.Add(samples); |
| 57 } |
| 58 |
| 59 bool SparseHistogram::AddSamplesFromPickle(PickleIterator* iter) { |
| 60 base::AutoLock auto_lock(lock_); |
| 61 return samples_.AddFromPickle(iter); |
| 62 } |
| 63 |
59 void SparseHistogram::WriteHTMLGraph(string* output) const { | 64 void SparseHistogram::WriteHTMLGraph(string* output) const { |
60 // TODO(kaiwang): Implement. | 65 // TODO(kaiwang): Implement. |
61 } | 66 } |
62 | 67 |
63 void SparseHistogram::WriteAscii(string* output) const { | 68 void SparseHistogram::WriteAscii(string* output) const { |
64 // TODO(kaiwang): Implement. | 69 // TODO(kaiwang): Implement. |
65 } | 70 } |
66 | 71 |
| 72 bool SparseHistogram::SerializeInfoImpl(Pickle* pickle) const { |
| 73 return pickle->WriteString(histogram_name()) && pickle->WriteInt(flags()); |
| 74 } |
| 75 |
67 SparseHistogram::SparseHistogram(const string& name) | 76 SparseHistogram::SparseHistogram(const string& name) |
68 : HistogramBase(name), | 77 : HistogramBase(name) {} |
69 redundant_count_(0) {} | 78 |
| 79 HistogramBase* SparseHistogram::DeserializeHistogramInfo(PickleIterator* iter) { |
| 80 string histogram_name; |
| 81 int flags; |
| 82 if (!iter->ReadString(&histogram_name) || !iter->ReadInt(&flags)) { |
| 83 DLOG(ERROR) << "Pickle error decoding Histogram: " << histogram_name; |
| 84 return NULL; |
| 85 } |
| 86 |
| 87 DCHECK(flags & HistogramBase::kIPCSerializationSourceFlag); |
| 88 flags &= ~HistogramBase::kIPCSerializationSourceFlag; |
| 89 |
| 90 return SparseHistogram::FactoryGet(histogram_name, flags); |
| 91 } |
70 | 92 |
71 void SparseHistogram::GetParameters(DictionaryValue* params) const { | 93 void SparseHistogram::GetParameters(DictionaryValue* params) const { |
72 // TODO(kaiwang): Implement. (See HistogramBase::WriteJSON.) | 94 // TODO(kaiwang): Implement. (See HistogramBase::WriteJSON.) |
73 } | 95 } |
74 | 96 |
75 void SparseHistogram::GetCountAndBucketData(Count* count, | 97 void SparseHistogram::GetCountAndBucketData(Count* count, |
76 ListValue* buckets) const { | 98 ListValue* buckets) const { |
77 // TODO(kaiwang): Implement. (See HistogramBase::WriteJSON.) | 99 // TODO(kaiwang): Implement. (See HistogramBase::WriteJSON.) |
78 } | 100 } |
79 | 101 |
80 } // namespace base | 102 } // namespace base |
OLD | NEW |