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 <utility> | 7 #include <utility> |
8 | 8 |
| 9 #include "base/metrics/metrics_hashes.h" |
9 #include "base/metrics/sample_map.h" | 10 #include "base/metrics/sample_map.h" |
10 #include "base/metrics/statistics_recorder.h" | 11 #include "base/metrics/statistics_recorder.h" |
11 #include "base/pickle.h" | 12 #include "base/pickle.h" |
12 #include "base/strings/stringprintf.h" | 13 #include "base/strings/stringprintf.h" |
13 #include "base/synchronization/lock.h" | 14 #include "base/synchronization/lock.h" |
14 | 15 |
15 namespace base { | 16 namespace base { |
16 | 17 |
17 typedef HistogramBase::Count Count; | 18 typedef HistogramBase::Count Count; |
18 typedef HistogramBase::Sample Sample; | 19 typedef HistogramBase::Sample Sample; |
19 | 20 |
20 // static | 21 // static |
21 HistogramBase* SparseHistogram::FactoryGet(const std::string& name, | 22 HistogramBase* SparseHistogram::FactoryGet(const std::string& name, |
22 int32 flags) { | 23 int32 flags) { |
23 HistogramBase* histogram = StatisticsRecorder::FindHistogram(name); | 24 HistogramBase* histogram = StatisticsRecorder::FindHistogram(name); |
24 | 25 |
25 if (!histogram) { | 26 if (!histogram) { |
26 // To avoid racy destruction at shutdown, the following will be leaked. | 27 // To avoid racy destruction at shutdown, the following will be leaked. |
27 HistogramBase* tentative_histogram = new SparseHistogram(name); | 28 HistogramBase* tentative_histogram = new SparseHistogram(name); |
28 tentative_histogram->SetFlags(flags); | 29 tentative_histogram->SetFlags(flags); |
29 histogram = | 30 histogram = |
30 StatisticsRecorder::RegisterOrDeleteDuplicate(tentative_histogram); | 31 StatisticsRecorder::RegisterOrDeleteDuplicate(tentative_histogram); |
31 } | 32 } |
32 DCHECK_EQ(SPARSE_HISTOGRAM, histogram->GetHistogramType()); | 33 DCHECK_EQ(SPARSE_HISTOGRAM, histogram->GetHistogramType()); |
33 return histogram; | 34 return histogram; |
34 } | 35 } |
35 | 36 |
36 SparseHistogram::~SparseHistogram() {} | 37 SparseHistogram::~SparseHistogram() {} |
37 | 38 |
| 39 uint64_t SparseHistogram::name_hash() const { |
| 40 return samples_.id(); |
| 41 } |
| 42 |
38 HistogramType SparseHistogram::GetHistogramType() const { | 43 HistogramType SparseHistogram::GetHistogramType() const { |
39 return SPARSE_HISTOGRAM; | 44 return SPARSE_HISTOGRAM; |
40 } | 45 } |
41 | 46 |
42 bool SparseHistogram::HasConstructionArguments( | 47 bool SparseHistogram::HasConstructionArguments( |
43 Sample expected_minimum, | 48 Sample expected_minimum, |
44 Sample expected_maximum, | 49 Sample expected_maximum, |
45 size_t expected_bucket_count) const { | 50 size_t expected_bucket_count) const { |
46 // SparseHistogram never has min/max/bucket_count limit. | 51 // SparseHistogram never has min/max/bucket_count limit. |
47 return false; | 52 return false; |
(...skipping 10 matching lines...) Expand all Loading... |
58 } | 63 } |
59 { | 64 { |
60 base::AutoLock auto_lock(lock_); | 65 base::AutoLock auto_lock(lock_); |
61 samples_.Accumulate(value, count); | 66 samples_.Accumulate(value, count); |
62 } | 67 } |
63 | 68 |
64 FindAndRunCallback(value); | 69 FindAndRunCallback(value); |
65 } | 70 } |
66 | 71 |
67 scoped_ptr<HistogramSamples> SparseHistogram::SnapshotSamples() const { | 72 scoped_ptr<HistogramSamples> SparseHistogram::SnapshotSamples() const { |
68 scoped_ptr<SampleMap> snapshot(new SampleMap()); | 73 scoped_ptr<SampleMap> snapshot(new SampleMap(name_hash())); |
69 | 74 |
70 base::AutoLock auto_lock(lock_); | 75 base::AutoLock auto_lock(lock_); |
71 snapshot->Add(samples_); | 76 snapshot->Add(samples_); |
72 return std::move(snapshot); | 77 return std::move(snapshot); |
73 } | 78 } |
74 | 79 |
75 void SparseHistogram::AddSamples(const HistogramSamples& samples) { | 80 void SparseHistogram::AddSamples(const HistogramSamples& samples) { |
76 base::AutoLock auto_lock(lock_); | 81 base::AutoLock auto_lock(lock_); |
77 samples_.Add(samples); | 82 samples_.Add(samples); |
78 } | 83 } |
(...skipping 11 matching lines...) Expand all Loading... |
90 | 95 |
91 void SparseHistogram::WriteAscii(std::string* output) const { | 96 void SparseHistogram::WriteAscii(std::string* output) const { |
92 WriteAsciiImpl(true, "\n", output); | 97 WriteAsciiImpl(true, "\n", output); |
93 } | 98 } |
94 | 99 |
95 bool SparseHistogram::SerializeInfoImpl(Pickle* pickle) const { | 100 bool SparseHistogram::SerializeInfoImpl(Pickle* pickle) const { |
96 return pickle->WriteString(histogram_name()) && pickle->WriteInt(flags()); | 101 return pickle->WriteString(histogram_name()) && pickle->WriteInt(flags()); |
97 } | 102 } |
98 | 103 |
99 SparseHistogram::SparseHistogram(const std::string& name) | 104 SparseHistogram::SparseHistogram(const std::string& name) |
100 : HistogramBase(name) {} | 105 : HistogramBase(name), |
| 106 samples_(HashMetricName(name)) {} |
101 | 107 |
102 HistogramBase* SparseHistogram::DeserializeInfoImpl(PickleIterator* iter) { | 108 HistogramBase* SparseHistogram::DeserializeInfoImpl(PickleIterator* iter) { |
103 std::string histogram_name; | 109 std::string histogram_name; |
104 int flags; | 110 int flags; |
105 if (!iter->ReadString(&histogram_name) || !iter->ReadInt(&flags)) { | 111 if (!iter->ReadString(&histogram_name) || !iter->ReadInt(&flags)) { |
106 DLOG(ERROR) << "Pickle error decoding Histogram: " << histogram_name; | 112 DLOG(ERROR) << "Pickle error decoding Histogram: " << histogram_name; |
107 return NULL; | 113 return NULL; |
108 } | 114 } |
109 | 115 |
110 DCHECK(flags & HistogramBase::kIPCSerializationSourceFlag); | 116 DCHECK(flags & HistogramBase::kIPCSerializationSourceFlag); |
(...skipping 69 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
180 std::string* output) const { | 186 std::string* output) const { |
181 StringAppendF(output, | 187 StringAppendF(output, |
182 "Histogram: %s recorded %d samples", | 188 "Histogram: %s recorded %d samples", |
183 histogram_name().c_str(), | 189 histogram_name().c_str(), |
184 total_count); | 190 total_count); |
185 if (flags() & ~kHexRangePrintingFlag) | 191 if (flags() & ~kHexRangePrintingFlag) |
186 StringAppendF(output, " (flags = 0x%x)", flags() & ~kHexRangePrintingFlag); | 192 StringAppendF(output, " (flags = 0x%x)", flags() & ~kHexRangePrintingFlag); |
187 } | 193 } |
188 | 194 |
189 } // namespace base | 195 } // namespace base |
OLD | NEW |